copy.c 7.66 KB
Newer Older
1 2 3
/*****************************************************************************
 * copy.c
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
5
 * $Id$
6 7 8 9
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
35 36
#include <vlc_codec.h>
#include <vlc_block.h>
37
#include <vlc_bits.h>
38 39 40 41

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
42 43
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
44

45 46 47 48 49 50 51
vlc_module_begin ()
    set_category( CAT_SOUT )
    set_subcategory( SUBCAT_SOUT_PACKETIZER )
    set_description( N_("Copy packetizer") )
    set_capability( "packetizer", 1 )
    set_callbacks( Open, Close )
vlc_module_end ()
52

53 54 55
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
56
struct decoder_sys_t
57
{
Laurent Aimar's avatar
Laurent Aimar committed
58
    block_t *p_block;
59
    void (*pf_parse)( decoder_t *, block_t * );
Laurent Aimar's avatar
Laurent Aimar committed
60
};
61

62 63
static block_t *Packetize   ( decoder_t *, block_t ** );
static block_t *PacketizeSub( decoder_t *, block_t ** );
64
static void Flush( decoder_t * );
65

66 67
static void ParseWMV3( decoder_t *, block_t * );

68
/*****************************************************************************
69
 * Open: probe the packetizer and return score
70 71 72 73 74 75
 *****************************************************************************
 * Tries to launch a decoder and return score so that the interface is able
 * to choose.
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
Laurent Aimar's avatar
Laurent Aimar committed
76 77
    decoder_t     *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
78

79 80 81
    if( p_dec->fmt_in.i_cat != AUDIO_ES &&
        p_dec->fmt_in.i_cat != VIDEO_ES &&
        p_dec->fmt_in.i_cat != SPU_ES )
82
    {
Laurent Aimar's avatar
Laurent Aimar committed
83 84
        msg_Err( p_dec, "invalid ES type" );
        return VLC_EGENERIC;
85 86
    }

87 88 89 90
    if( p_dec->fmt_in.i_cat == SPU_ES )
        p_dec->pf_packetize = PacketizeSub;
    else
        p_dec->pf_packetize = Packetize;
91
    p_dec->pf_flush = Flush;
92

93
    p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
94 95 96
    if (unlikely(p_sys == NULL))
        return VLC_ENOMEM;

Laurent Aimar's avatar
Laurent Aimar committed
97
    p_sys->p_block    = NULL;
98 99 100 101 102 103 104 105 106
    switch( p_dec->fmt_in.i_codec )
    {
    case VLC_CODEC_WMV3:
        p_sys->pf_parse = ParseWMV3;
        break;
    default:
        p_sys->pf_parse = NULL;
        break;
    }
107

108
    vlc_fourcc_t fcc = p_dec->fmt_in.i_codec;
109 110 111
    /* Fix the value of the fourcc for audio */
    if( p_dec->fmt_in.i_cat == AUDIO_ES )
    {
112
        fcc = vlc_fourcc_GetCodecAudio( p_dec->fmt_in.i_codec,
113 114 115 116
                                                     p_dec->fmt_in.audio.i_bitspersample );
        if( !fcc )
        {
            msg_Err( p_dec, "unknown raw audio sample size" );
117
            free( p_sys );
118 119 120 121
            return VLC_EGENERIC;
        }
    }

122 123 124 125
    /* Create the output format */
    es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
    p_dec->fmt_out.i_codec = fcc;

Laurent Aimar's avatar
Laurent Aimar committed
126 127
    return VLC_SUCCESS;
}
128

Laurent Aimar's avatar
Laurent Aimar committed
129 130 131 132 133 134
/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    decoder_t     *p_dec = (decoder_t*)p_this;
135

Laurent Aimar's avatar
Laurent Aimar committed
136
    if( p_dec->p_sys->p_block )
137
    {
Laurent Aimar's avatar
Laurent Aimar committed
138
        block_ChainRelease( p_dec->p_sys->p_block );
139
    }
140

Laurent Aimar's avatar
Laurent Aimar committed
141
    free( p_dec->p_sys );
142 143
}

144 145 146 147 148 149 150 151 152 153
static void Flush( decoder_t *p_dec )
{
    block_t *p_ret = p_dec->p_sys->p_block;
    if ( p_ret )
    {
        block_Release( p_ret );
        p_dec->p_sys->p_block = NULL;
    }
}

154
/*****************************************************************************
155
 * Packetize: packetize an unit (here copy a complete block )
156
 *****************************************************************************/
157
static block_t *Packetize ( decoder_t *p_dec, block_t **pp_block )
158
{
Laurent Aimar's avatar
Laurent Aimar committed
159 160
    block_t *p_block;
    block_t *p_ret = p_dec->p_sys->p_block;
161

Laurent Aimar's avatar
Laurent Aimar committed
162
    if( pp_block == NULL || *pp_block == NULL )
163
        return NULL;
164
    if( (*pp_block)->i_flags&(BLOCK_FLAG_CORRUPTED) )
165
    {
166
        block_Release( *pp_block );
Laurent Aimar's avatar
Laurent Aimar committed
167
        return NULL;
168
    }
169

Laurent Aimar's avatar
Laurent Aimar committed
170 171
    p_block = *pp_block;
    *pp_block = NULL;
172

173
    if( p_block->i_dts <= VLC_TS_INVALID )
174 175 176 177
    {
        p_block->i_dts = p_block->i_pts;
    }

178
    if( p_block->i_dts <= VLC_TS_INVALID )
179
    {
180
        msg_Dbg( p_dec, "need valid dts" );
Laurent Aimar's avatar
Laurent Aimar committed
181 182
        block_Release( p_block );
        return NULL;
183
    }
184

185
    if( p_ret != NULL && p_block->i_pts > p_ret->i_pts )
186
    {
187 188
        if (p_dec->fmt_in.i_codec != VLC_CODEC_OPUS)
            p_ret->i_length = p_block->i_pts - p_ret->i_pts;
189
    }
Laurent Aimar's avatar
Laurent Aimar committed
190
    p_dec->p_sys->p_block = p_block;
191

192 193
    if( p_ret && p_dec->p_sys->pf_parse )
        p_dec->p_sys->pf_parse( p_dec, p_ret );
Laurent Aimar's avatar
Laurent Aimar committed
194
    return p_ret;
195
}
196 197 198 199 200 201 202 203 204

/*****************************************************************************
 * PacketizeSub: packetize an unit (here copy a complete block )
 *****************************************************************************/
static block_t *PacketizeSub( decoder_t *p_dec, block_t **pp_block )
{
    block_t *p_block;

    if( pp_block == NULL || *pp_block == NULL )
205
        return NULL;
206
    if( (*pp_block)->i_flags&(BLOCK_FLAG_CORRUPTED) )
207
    {
208
        block_Release( *pp_block );
209 210
        return NULL;
    }
211

212 213 214
    p_block = *pp_block;
    *pp_block = NULL;

215
    if( p_block->i_dts <= VLC_TS_INVALID )
216 217 218 219
    {
        p_block->i_dts = p_block->i_pts;
    }

220
    if( p_block->i_dts <= VLC_TS_INVALID )
221
    {
222
        msg_Dbg( p_dec, "need valid dts" );
223 224 225 226 227 228
        block_Release( p_block );
        return NULL;
    }

    return p_block;
}
229

Rémi Duraffort's avatar
Rémi Duraffort committed
230
/* Parse WMV3 packet and extract frame type information */
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
static void ParseWMV3( decoder_t *p_dec, block_t *p_block )
{
    bs_t s;

    /* Parse Sequence header */
    bs_init( &s, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
    if( bs_read( &s, 2 ) == 3 )
        return;
    bs_skip( &s, 22 );
    const bool b_range_reduction = bs_read( &s, 1 );
    const bool b_has_frames = bs_read( &s, 3 ) > 0;
    bs_skip( &s, 2 );
    const bool b_frame_interpolation = bs_read( &s, 1 );
    if( bs_eof( &s ) )
        return;

    /* Parse frame type */
    bs_init( &s, p_block->p_buffer, p_block->i_buffer );
    bs_skip( &s, b_frame_interpolation +
                 2 +
                 b_range_reduction );

    p_block->i_flags &= ~BLOCK_FLAG_TYPE_MASK;
    if( bs_read( &s, 1 ) )
        p_block->i_flags |= BLOCK_FLAG_TYPE_P;
    else if( !b_has_frames || bs_read( &s, 1 ) )
        p_block->i_flags |= BLOCK_FLAG_TYPE_I;
    else
        p_block->i_flags |= BLOCK_FLAG_TYPE_B;
}