copy.c 12.2 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * copy.c
 *****************************************************************************
 * Copyright (C) 2001, 2002 VideoLAN
5
 * $Id: copy.c,v 1.19 2003/11/20 18:27:44 fenrir Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29
#include <stdlib.h>                                      /* malloc(), free() */

30 31 32
#include <vlc/vlc.h>
#include <vlc/decoder.h>
#include <vlc/input.h>
33 34 35 36

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
37 38
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
39 40 41 42

vlc_module_begin();
    set_description( _("Copy packetizer") );
    set_capability( "packetizer", 1 );
43
    set_callbacks( Open, Close );
44 45
vlc_module_end();

46 47 48
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
49
struct decoder_sys_t
50
{
51 52
    int i_spu_size;
    int i_spu;
53

54 55
    block_t *p_block;
};
56

57 58
static block_t *PacketizeAV ( decoder_t *, block_t ** );
static block_t *PacketizeSPU( decoder_t *, block_t ** );
59

60
/*****************************************************************************
61
 * Open: probe the packetizer and return score
62 63 64 65 66 67
 *****************************************************************************
 * Tries to launch a decoder and return score so that the interface is able
 * to choose.
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
68 69
    decoder_t     *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
70

71
    if( p_dec->fmt_in.i_cat == AUDIO_ES || p_dec->fmt_in.i_cat == VIDEO_ES )
72
    {
73
        p_dec->pf_packetize = PacketizeAV;
74
    }
75
    else if( p_dec->fmt_in.i_cat == SPU_ES )
76
    {
77 78 79 80 81 82 83 84 85
        if( p_dec->fmt_in.i_codec == VLC_FOURCC( 's', 'p', 'u', ' ' ) ||
            p_dec->fmt_in.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) )
        {
            p_dec->pf_packetize = PacketizeSPU;
        }
        else
        {
            p_dec->pf_packetize = PacketizeAV;
        }
86
    }
87
    else
88
    {
89 90
        msg_Err( p_dec, "invalid ES type" );
        return VLC_EGENERIC;
91 92
    }

93 94 95 96 97 98 99 100
    /* Create the output format */
    memcpy( &p_dec->fmt_out, &p_dec->fmt_in, sizeof( es_format_t ) );
    if( p_dec->fmt_in.i_extra > 0 )
    {
        p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
        memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
                p_dec->fmt_in.i_extra );
    }
101

102 103
    /* Fix the value of the fourcc */
    switch( p_dec->fmt_in.i_codec )
104
    {
105
        /* video */
106 107 108 109
        case VLC_FOURCC( 'm', '4', 's', '2'):
        case VLC_FOURCC( 'M', '4', 'S', '2'):
        case VLC_FOURCC( 'm', 'p', '4', 's'):
        case VLC_FOURCC( 'M', 'P', '4', 'S'):
110 111 112 113 114 115
        case VLC_FOURCC( 'D', 'I', 'V', 'X'):
        case VLC_FOURCC( 'd', 'i', 'v', 'x'):
        case VLC_FOURCC( 'X', 'V', 'I', 'D'):
        case VLC_FOURCC( 'X', 'v', 'i', 'D'):
        case VLC_FOURCC( 'x', 'v', 'i', 'd'):
        case VLC_FOURCC( 'D', 'X', '5', '0'):
116 117
        case VLC_FOURCC( 0x04, 0,   0,   0):
        case VLC_FOURCC( '3', 'I', 'V', '2'):
118
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v');
119
            break;
120

121 122
        case VLC_FOURCC( 'm', 'p', 'g', '1' ):
        case VLC_FOURCC( 'm', 'p', 'g', '2' ):
123 124
        case VLC_FOURCC( 'm', 'p', '1', 'v' ):
        case VLC_FOURCC( 'm', 'p', '2', 'v' ):
125
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
126
            break;
127 128 129 130

        case VLC_FOURCC( 'd', 'i', 'v', '1' ):
        case VLC_FOURCC( 'M', 'P', 'G', '4' ):
        case VLC_FOURCC( 'm', 'p', 'g', '4' ):
131
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '1' );
132
            break;
133

134 135 136
        case VLC_FOURCC( 'd', 'i', 'v', '2' ):
        case VLC_FOURCC( 'M', 'P', '4', '2' ):
        case VLC_FOURCC( 'm', 'p', '4', '2' ):
137
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '2' );
138
            break;
139

140 141 142 143 144 145 146 147 148 149 150 151
        case VLC_FOURCC( 'd', 'i', 'v', '3' ):
        case VLC_FOURCC( 'd', 'i', 'v', '4' ):
        case VLC_FOURCC( 'D', 'I', 'V', '4' ):
        case VLC_FOURCC( 'd', 'i', 'v', '5' ):
        case VLC_FOURCC( 'D', 'I', 'V', '5' ):
        case VLC_FOURCC( 'd', 'i', 'v', '6' ):
        case VLC_FOURCC( 'D', 'I', 'V', '6' ):
        case VLC_FOURCC( 'M', 'P', '4', '3' ):
        case VLC_FOURCC( 'm', 'p', '4', '3' ):
        case VLC_FOURCC( 'm', 'p', 'g', '3' ):
        case VLC_FOURCC( 'M', 'P', 'G', '3' ):
        case VLC_FOURCC( 'A', 'P', '4', '1' ):
152
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
153
            break;
154

155 156 157
        case VLC_FOURCC( 'h', '2', '6', '3' ):
        case VLC_FOURCC( 'U', '2', '6', '3' ):
        case VLC_FOURCC( 'u', '2', '6', '3' ):
158
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
159
            break;
160

161
        case VLC_FOURCC( 'i', '2', '6', '3' ):
162
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'I', '2', '6', '3' );
163
            break;
164

165 166 167 168 169
        case VLC_FOURCC( 'm', 'j', 'p', 'g' ):
        case VLC_FOURCC( 'm', 'j', 'p', 'a' ):
        case VLC_FOURCC( 'j', 'p', 'e', 'g' ):
        case VLC_FOURCC( 'J', 'P', 'E', 'G' ):
        case VLC_FOURCC( 'J', 'F', 'I', 'F' ):
170
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
171
            break;
172

173 174 175
        case VLC_FOURCC( 'd', 'v', 's', 'd' ):
        case VLC_FOURCC( 'D', 'V', 'S', 'D' ):
        case VLC_FOURCC( 'd', 'v', 'h', 'd' ):
176
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'd', 'v', 's', 'l' );
Gildas Bazin's avatar
 
Gildas Bazin committed
177
            break;
178

179 180
        /* audio */
        case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
181
            switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
182
            {
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                case 1:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('u','8',' ',' ');
                    break;
                case 2:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','l');
                    break;
                case 3:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','l');
                    break;
                case 4:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','3','2','l');
                    break;
                default:
                    msg_Err( p_dec, "unknown raw audio sample size !!" );
                    return VLC_EGENERIC;
198 199
            }
            break;
200

201
        case VLC_FOURCC( 't', 'w', 'o', 's' ):
202
            switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
203
            {
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
                case 1:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','8',' ',' ');
                    break;
                case 2:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
                    break;
                case 3:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
                    break;
                case 4:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','3','2','b');
                    break;
                default:
                    msg_Err( p_dec, "unknown raw audio sample size !!" );
                    return VLC_EGENERIC;
219 220
            }
            break;
221

222
        case VLC_FOURCC( 's', 'o', 'w', 't' ):
223
            switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
224
            {
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
                case 1:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','8',' ',' ');
                    break;
                case 2:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','l');
                    break;
                case 3:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','l');
                    break;
                case 4:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','3','2','l');
                    break;
                default:
                    msg_Err( p_dec, "unknown raw audio sample size !!" );
                    return VLC_EGENERIC;
240 241
            }
            break;
242

243 244
        /* subtitles */
        case VLC_FOURCC( 's', 'p', 'u', 'b' ):
245
            p_dec->fmt_out.i_codec = VLC_FOURCC( 's', 'p', 'u', ' ' );
Gildas Bazin's avatar
 
Gildas Bazin committed
246
            break;
247
    }
248

249 250 251 252
    p_dec->p_sys = p_sys = malloc( sizeof( block_t ) );
    p_sys->i_spu_size = 0;
    p_sys->i_spu      = 0;
    p_sys->p_block    = NULL;
253

254 255
    return VLC_SUCCESS;
}
256

257 258 259 260 261 262
/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    decoder_t     *p_dec = (decoder_t*)p_this;
263

264
    if( p_dec->p_sys->p_block )
265
    {
266
        block_ChainRelease( p_dec->p_sys->p_block );
267
    }
268

269
    free( p_dec->p_sys );
270 271 272
}

/*****************************************************************************
273
 * PacketizeStd: packetize an unit (here copy a complete block )
274
 *****************************************************************************/
275
static block_t *PacketizeAV ( decoder_t *p_dec, block_t **pp_block )
276
{
277 278
    block_t *p_block;
    block_t *p_ret = p_dec->p_sys->p_block;
279

280
    if( pp_block == NULL || *pp_block == NULL )
281
    {
282
        return NULL;
283
    }
284 285
    p_block = *pp_block;
    *pp_block = NULL;
286

287
    if( p_block->i_pts <= 0 )
288
    {
289 290 291
        msg_Dbg( p_dec, "need pts > 0" );
        block_Release( p_block );
        return NULL;
292
    }
293

294
    if( p_ret != NULL && p_block->i_pts > p_ret->i_pts )
295
    {
296
        p_ret->i_length = p_block->i_pts - p_ret->i_pts;
297
    }
298
    p_dec->p_sys->p_block = p_block;
299

300
    return p_ret;
301 302 303 304 305
}

/*****************************************************************************
 * PacketizeSPU: packetize an SPU unit (so gather all PES of one subtitle)
 *****************************************************************************/
306
static block_t *PacketizeSPU( decoder_t *p_dec, block_t **pp_block )
307
{
308 309
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_block;
310

311
    if( pp_block == NULL || *pp_block == NULL )
312
    {
313
        return NULL;
314
    }
315 316
    p_block = *pp_block;
    *pp_block = NULL;
317

318 319
    if( p_sys->i_spu_size <= 0 &&
        ( p_block->i_pts <= 0 || p_block->i_buffer < 4 ) )
320
    {
321 322 323
        msg_Dbg( p_dec, "invalid starting packet (size < 4 or pts <=0)" );
        block_Release( p_block );
        return NULL;
324 325
    }

326 327
    block_ChainAppend( &p_sys->p_block, p_block );
    p_sys->i_spu += p_block->i_buffer;
328

329
    if( p_sys->i_spu_size <= 0 )
330
    {
331
        int i_rle = ( ( p_block->p_buffer[2] << 8 )| p_block->p_buffer[3] ) - 4;
332

333
        p_sys->i_spu_size = ( p_block->p_buffer[0] << 8 )| p_block->p_buffer[1];
334

335 336 337
        msg_Dbg( p_dec, "i_spu_size=%d i_rle=%d", p_sys->i_spu_size, i_rle );

        if( p_sys->i_spu_size <= 0 || i_rle >= p_sys->i_spu_size )
338
        {
339 340 341 342 343 344
            p_sys->i_spu_size = 0;
            p_sys->i_spu      = 0;
            p_sys->p_block    = NULL;

            block_Release( p_block );
            return NULL;
345 346
        }
    }
347

348
    if( p_sys->i_spu >= p_sys->i_spu_size )
349
    {
350 351
        /* We have a complete sub */
        block_t *p_ret = p_sys->p_block;
352

353 354
        msg_Dbg( p_dec, "SPU packets size=%d should be %d",
                 p_sys->i_spu, p_sys->i_spu_size );
355

356 357 358 359
        p_sys->i_spu_size = 0;
        p_sys->i_spu      = 0;
        p_sys->p_block    = NULL;
        return p_ret;
360
    }
361
    return NULL;
362
}
363