demux.c 19.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * demux.c
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
5
 * $Id$
6
 *
7
 * Author: Laurent Aimar <fenrir@via.ecp.fr>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include "demux.h"
29 30
#include <libvlc.h>
#include <vlc_codec.h>
31

32 33
static bool SkipID3Tag( demux_t * );
static bool SkipAPETag( demux_t *p_demux );
34

35
/*****************************************************************************
36
 * demux_New:
Laurent Aimar's avatar
Laurent Aimar committed
37
 *  if s is NULL then load a access_demux
38
 *****************************************************************************/
39
demux_t *__demux_New( vlc_object_t *p_obj,
40 41
                       const char *psz_access, const char *psz_demux,
                       const char *psz_path,
42
                       stream_t *s, es_out_t *out, bool b_quick )
43
{
44 45 46
    static const char typename[] = "demux";
    demux_t *p_demux = vlc_custom_create( p_obj, sizeof( *p_demux ),
                                          VLC_OBJECT_GENERIC, typename );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
47
    const char *psz_module;
48

49
    if( p_demux == NULL ) return NULL;
50 51

    /* Parse URL */
52 53 54
    p_demux->psz_access = strdup( psz_access );
    p_demux->psz_demux  = strdup( psz_demux );
    p_demux->psz_path   = strdup( psz_path );
55

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
56
    /* Take into account "demux" to be able to do :demux=dump */
57
    if( p_demux->psz_demux && *p_demux->psz_demux == '\0' )
58 59
    {
        free( p_demux->psz_demux );
60 61 62
        p_demux->psz_demux = var_GetNonEmptyString( p_obj, "demux" );
        if( p_demux->psz_demux == NULL )
            p_demux->psz_demux = strdup( "" );
63 64
    }

65 66
    if( !b_quick )
    {
Clément Stenac's avatar
Clément Stenac committed
67
        msg_Dbg( p_obj, "creating demux: access='%s' demux='%s' path='%s'",
68 69
                 p_demux->psz_access, p_demux->psz_demux, p_demux->psz_path );
    }
70 71 72 73 74 75 76

    p_demux->s          = s;
    p_demux->out        = out;

    p_demux->pf_demux   = NULL;
    p_demux->pf_control = NULL;
    p_demux->p_sys      = NULL;
77 78 79
    p_demux->info.i_update = 0;
    p_demux->info.i_title  = 0;
    p_demux->info.i_seekpoint = 0;
80

81 82
    if( s ) psz_module = p_demux->psz_demux;
    else psz_module = p_demux->psz_access;
Laurent Aimar's avatar
Laurent Aimar committed
83 84

    if( s && *psz_module == '\0' && strrchr( p_demux->psz_path, '.' ) )
85
    {
86 87 88 89
       /* XXX: add only file without any problem here and with strong detection.
        *  - no .mp3, .a52, ... (aac is added as it works only by file ext
        *     anyway
        *  - wav can't be added 'cause of a52 and dts in them as raw audio
90
         */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
91
         static const struct { char ext[5]; char demux[9]; } exttodemux[] =
92
         {
93 94 95 96 97 98 99
            { "aac",  "aac" },
            { "aiff", "aiff" },
            { "asf",  "asf" }, { "wmv",  "asf" }, { "wma",  "asf" },
            { "avi",  "avi" },
            { "au",   "au" },
            { "flac", "flac" },
            { "dv",   "dv" },
100
            { "m3u",  "playlist" },
101 102 103 104 105 106 107
            { "mkv",  "mkv" }, { "mka",  "mkv" }, { "mks",  "mkv" },
            { "mp4",  "mp4" }, { "m4a",  "mp4" }, { "mov",  "mp4" }, { "moov", "mp4" },
            { "mod",  "mod" }, { "xm",   "mod" },
            { "nsv",  "nsv" },
            { "ogg",  "ogg" }, { "ogm",  "ogg" },
            { "pva",  "pva" },
            { "rm",   "rm" },
108
            { "m4v",  "m4v" },
109 110 111 112
            { "h264", "h264" },
            { "voc",  "voc" },
            { "mid",  "smf" },
            { "rmi",  "smf" },
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
113
            { "",  "" },
114
        };
115
        /* Here, we don't mind if it does not work, it must be quick */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
116
        static const struct { char ext[4]; char demux[5]; } exttodemux_quick[] =
117 118 119 120
        {
            { "mp3", "mpga" },
            { "ogg", "ogg" },
            { "wma", "asf" },
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
121
            { "", "" }
122
        };
123

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
124
        const char *psz_ext = strrchr( p_demux->psz_path, '.' ) + 1;
125 126
        int  i;

127 128
        if( !b_quick )
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
129
            for( i = 0; exttodemux[i].ext[0]; i++ )
130 131 132 133 134 135 136 137 138
            {
                if( !strcasecmp( psz_ext, exttodemux[i].ext ) )
                {
                    psz_module = exttodemux[i].demux;
                    break;
                }
            }
        }
        else
139
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
140
            for( i = 0; exttodemux_quick[i].ext[0]; i++ )
141
            {
142 143 144 145 146
                if( !strcasecmp( psz_ext, exttodemux_quick[i].ext ) )
                {
                    psz_module = exttodemux_quick[i].demux;
                    break;
                }
147
            }
148

149 150 151
        }
    }

152
    /* Before module_need (for var_Create...) */
Gildas Bazin's avatar
 
Gildas Bazin committed
153
    vlc_object_attach( p_demux, p_obj );
154

Laurent Aimar's avatar
Laurent Aimar committed
155 156
    if( s )
    {
157 158
        /* ID3/APE tags will mess-up demuxer probing so we skip it here.
         * ID3/APE parsers will called later on in the demuxer to access the
159
         * skipped info. */
160 161
        if( !SkipID3Tag( p_demux ) )
            SkipAPETag( p_demux );
162

Laurent Aimar's avatar
Laurent Aimar committed
163
        p_demux->p_module =
164
            module_need( p_demux, "demux", psz_module,
165
                         !strcmp( psz_module, p_demux->psz_demux ) ?
166
                         true : false );
Laurent Aimar's avatar
Laurent Aimar committed
167 168 169 170
    }
    else
    {
        p_demux->p_module =
171
            module_need( p_demux, "access_demux", psz_module,
172
                         !strcmp( psz_module, p_demux->psz_access ) ?
173
                         true : false );
Laurent Aimar's avatar
Laurent Aimar committed
174
    }
175

176 177 178
    if( p_demux->p_module == NULL )
    {
        vlc_object_detach( p_demux );
Gildas Bazin's avatar
 
Gildas Bazin committed
179 180 181
        free( p_demux->psz_path );
        free( p_demux->psz_demux );
        free( p_demux->psz_access );
182
        vlc_object_release( p_demux );
183 184 185 186 187 188 189
        return NULL;
    }

    return p_demux;
}

/*****************************************************************************
190
 * demux_Delete:
191
 *****************************************************************************/
192
void demux_Delete( demux_t *p_demux )
193
{
194
    module_unneed( p_demux, p_demux->p_module );
195 196 197 198 199 200
    vlc_object_detach( p_demux );

    free( p_demux->psz_path );
    free( p_demux->psz_demux );
    free( p_demux->psz_access );

201
    vlc_object_release( p_demux );
202
}
203 204

/*****************************************************************************
205
 * demux_vaControlHelper:
206
 *****************************************************************************/
207
int demux_vaControlHelper( stream_t *s,
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
                            int64_t i_start, int64_t i_end,
                            int i_bitrate, int i_align,
                            int i_query, va_list args )
{
    int64_t i_tell;
    double  f, *pf;
    int64_t i64, *pi64;

    if( i_end < 0 )    i_end   = stream_Size( s );
    if( i_start < 0 )  i_start = 0;
    if( i_align <= 0 ) i_align = 1;
    i_tell = stream_Tell( s );

    switch( i_query )
    {
        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( i_bitrate > 0 && i_end > i_start )
            {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
227
                *pi64 = INT64_C(8000000) * (i_end - i_start) / i_bitrate;
228 229 230 231 232 233 234 235
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( i_bitrate > 0 && i_end > i_start )
            {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
236
                *pi64 = INT64_C(8000000) * (i_tell - i_start) / i_bitrate;
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 262 263 264 265 266 267 268 269
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_GET_POSITION:
            pf = (double*)va_arg( args, double * );
            if( i_start < i_end )
            {
                *pf = (double)( i_tell - i_start ) /
                      (double)( i_end  - i_start );
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;


        case DEMUX_SET_POSITION:
            f = (double)va_arg( args, double );
            if( i_start < i_end && f >= 0.0 && f <= 1.0 )
            {
                int64_t i_block = (f * ( i_end - i_start )) / i_align;

                if( stream_Seek( s, i_start + i_block * i_align ) )
                {
                    return VLC_EGENERIC;
                }
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_SET_TIME:
            i64 = (int64_t)va_arg( args, int64_t );
            if( i_bitrate > 0 && i64 >= 0 )
            {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
270
                int64_t i_block = i64 * i_bitrate / INT64_C(8000000) / i_align;
271 272 273 274 275 276 277 278 279 280
                if( stream_Seek( s, i_start + i_block * i_align ) )
                {
                    return VLC_EGENERIC;
                }
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        case DEMUX_GET_FPS:
        case DEMUX_GET_META:
281
        case DEMUX_HAS_UNSUPPORTED_META:
282 283
        case DEMUX_SET_NEXT_DEMUX_TIME:
        case DEMUX_GET_TITLE_INFO:
284
        case DEMUX_SET_GROUP:
285
        case DEMUX_GET_ATTACHMENTS:
286 287
        case DEMUX_CAN_RECORD:
        case DEMUX_SET_RECORD_STATE:
288 289 290 291 292 293 294 295
            return VLC_EGENERIC;

        default:
            msg_Err( s, "unknown query in demux_vaControlDefault" );
            return VLC_EGENERIC;
    }
}

Laurent Aimar's avatar
Laurent Aimar committed
296 297 298 299 300 301
/****************************************************************************
 * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
 ****************************************************************************/
typedef struct
{
    /* Data buffer */
302 303
    block_fifo_t *p_fifo;
    block_t      *p_block;
Laurent Aimar's avatar
Laurent Aimar committed
304 305 306 307 308 309 310

    int64_t     i_pos;

    /* Demuxer */
    char        *psz_name;
    es_out_t    *out;
    demux_t     *p_demux;
311

Laurent Aimar's avatar
Laurent Aimar committed
312 313
} d_stream_sys_t;

314 315
static int DStreamRead   ( stream_t *, void *p_read, unsigned int i_read );
static int DStreamPeek   ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
Laurent Aimar's avatar
Laurent Aimar committed
316
static int DStreamControl( stream_t *, int i_query, va_list );
317
static void* DStreamThread ( vlc_object_t * );
Laurent Aimar's avatar
Laurent Aimar committed
318 319


320
stream_t *__stream_DemuxNew( vlc_object_t *p_obj, const char *psz_demux,
321
                             es_out_t *out )
Laurent Aimar's avatar
Laurent Aimar committed
322 323 324 325 326
{
    /* We create a stream reader, and launch a thread */
    stream_t       *s;
    d_stream_sys_t *p_sys;

327
    s = vlc_stream_create( p_obj );
328 329
    if( s == NULL )
        return NULL;
Laurent Aimar's avatar
Laurent Aimar committed
330 331 332 333
    s->pf_read   = DStreamRead;
    s->pf_peek   = DStreamPeek;
    s->pf_control= DStreamControl;

334
    s->i_char_width = 1;
335
    s->b_little_endian = false;
336

Laurent Aimar's avatar
Laurent Aimar committed
337
    s->p_sys = malloc( sizeof( d_stream_sys_t) );
338 339 340 341 342
    if( s->p_sys == NULL )
    {
        vlc_object_release( s );
        return NULL;
    }
Laurent Aimar's avatar
Laurent Aimar committed
343 344 345 346 347
    p_sys = (d_stream_sys_t*)s->p_sys;

    p_sys->i_pos = 0;
    p_sys->out = out;
    p_sys->p_demux = NULL;
348 349 350 351
    p_sys->p_block = NULL;
    p_sys->psz_name = strdup( psz_demux );

    /* decoder fifo */
352
    if( ( p_sys->p_fifo = block_FifoNew() ) == NULL )
353
    {
354
        vlc_object_release( s );
355
        free( p_sys->psz_name );
356 357 358
        free( p_sys );
        return NULL;
    }
Laurent Aimar's avatar
Laurent Aimar committed
359

360
    if( vlc_thread_create( s, "stream out", DStreamThread,
361
                           VLC_THREAD_PRIORITY_INPUT, false ) )
Laurent Aimar's avatar
Laurent Aimar committed
362
    {
363
        vlc_object_release( s );
364
        free( p_sys->psz_name );
Laurent Aimar's avatar
Laurent Aimar committed
365 366 367 368 369 370 371
        free( p_sys );
        return NULL;
    }

    return s;
}

372
void stream_DemuxSend( stream_t *s, block_t *p_block )
Laurent Aimar's avatar
Laurent Aimar committed
373 374
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
375
    if( p_block ) block_FifoPut( p_sys->p_fifo, p_block );
Laurent Aimar's avatar
Laurent Aimar committed
376 377
}

378
void stream_DemuxDelete( stream_t *s )
Laurent Aimar's avatar
Laurent Aimar committed
379 380
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
381
    block_t *p_empty;
Laurent Aimar's avatar
Laurent Aimar committed
382

383 384 385
    vlc_object_kill( s );
    if( p_sys->p_demux )
        vlc_object_kill( p_sys->p_demux );
386 387
    p_empty = block_New( s, 1 ); p_empty->i_buffer = 0;
    block_FifoPut( p_sys->p_fifo, p_empty );
Laurent Aimar's avatar
Laurent Aimar committed
388 389
    vlc_thread_join( s );

390
    if( p_sys->p_demux ) demux_Delete( p_sys->p_demux );
391
    if( p_sys->p_block ) block_Release( p_sys->p_block );
392 393

    block_FifoRelease( p_sys->p_fifo );
Laurent Aimar's avatar
Laurent Aimar committed
394 395
    free( p_sys->psz_name );
    free( p_sys );
396

397
    vlc_object_release( s );
Laurent Aimar's avatar
Laurent Aimar committed
398 399 400
}


401
static int DStreamRead( stream_t *s, void *p_read, unsigned int i_read )
Laurent Aimar's avatar
Laurent Aimar committed
402 403
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
404 405
    uint8_t *p_out = p_read;
    int i_out = 0;
Laurent Aimar's avatar
Laurent Aimar committed
406 407 408

    //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );

409
    while( !s->b_die && !s->b_error && i_read )
Laurent Aimar's avatar
Laurent Aimar committed
410
    {
411 412
        block_t *p_block = p_sys->p_block;
        int i_copy;
413

414 415 416 417 418 419
        if( !p_block )
        {
            p_block = block_FifoGet( p_sys->p_fifo );
            if( !p_block ) s->b_error = 1;
            p_sys->p_block = p_block;
        }
Laurent Aimar's avatar
Laurent Aimar committed
420

421
        if( p_block && i_read )
Laurent Aimar's avatar
Laurent Aimar committed
422
        {
423 424 425 426 427 428 429 430 431 432 433 434
            i_copy = __MIN( i_read, p_block->i_buffer );
            if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
            i_read -= i_copy;
            i_out += i_copy;
            p_block->i_buffer -= i_copy;
            p_block->p_buffer += i_copy;

            if( !p_block->i_buffer )
            {
                block_Release( p_block );
                p_sys->p_block = NULL;
            }
Laurent Aimar's avatar
Laurent Aimar committed
435 436 437
        }
    }

438 439
    p_sys->i_pos += i_out;
    return i_out;
Laurent Aimar's avatar
Laurent Aimar committed
440
}
441

442
static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
Laurent Aimar's avatar
Laurent Aimar committed
443 444
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
445 446 447
    block_t **pp_block = &p_sys->p_block;
    int i_out = 0;
    *pp_peek = 0;
Laurent Aimar's avatar
Laurent Aimar committed
448 449

    //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
450 451

    while( !s->b_die && !s->b_error && i_peek )
Laurent Aimar's avatar
Laurent Aimar committed
452
    {
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        int i_copy;

        if( !*pp_block )
        {
            *pp_block = block_FifoGet( p_sys->p_fifo );
            if( !*pp_block ) s->b_error = 1;
        }

        if( *pp_block && i_peek )
        {
            i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
            i_peek -= i_copy;
            i_out += i_copy;

            if( i_peek ) pp_block = &(*pp_block)->p_next;
        }
Laurent Aimar's avatar
Laurent Aimar committed
469 470
    }

471 472 473 474 475
    if( p_sys->p_block )
    {
        p_sys->p_block = block_ChainGather( p_sys->p_block );
        *pp_peek = p_sys->p_block->p_buffer;
    }
Laurent Aimar's avatar
Laurent Aimar committed
476

477
    return i_out;
Laurent Aimar's avatar
Laurent Aimar committed
478 479
}

480
static int DStreamControl( stream_t *s, int i_query, va_list args )
Laurent Aimar's avatar
Laurent Aimar committed
481 482 483
{
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
    int64_t    *p_i64;
484
    bool *p_b;
Laurent Aimar's avatar
Laurent Aimar committed
485
    int        *p_int;
486

Laurent Aimar's avatar
Laurent Aimar committed
487 488 489 490 491 492 493 494
    switch( i_query )
    {
        case STREAM_GET_SIZE:
            p_i64 = (int64_t*) va_arg( args, int64_t * );
            *p_i64 = 0;
            return VLC_SUCCESS;

        case STREAM_CAN_SEEK:
495 496
            p_b = (bool*) va_arg( args, bool * );
            *p_b = false;
Laurent Aimar's avatar
Laurent Aimar committed
497 498 499
            return VLC_SUCCESS;

        case STREAM_CAN_FASTSEEK:
500 501
            p_b = (bool*) va_arg( args, bool * );
            *p_b = false;
Laurent Aimar's avatar
Laurent Aimar committed
502 503 504 505 506 507 508 509
            return VLC_SUCCESS;

        case STREAM_GET_POSITION:
            p_i64 = (int64_t*) va_arg( args, int64_t * );
            *p_i64 = p_sys->i_pos;
            return VLC_SUCCESS;

        case STREAM_SET_POSITION:
510 511 512
        {
            int64_t i64 = (int64_t)va_arg( args, int64_t );
            int i_skip;
513
            if( i64 < p_sys->i_pos ) return VLC_EGENERIC;
514 515 516 517
            i_skip = i64 - p_sys->i_pos;

            while( i_skip > 0 )
            {
518
                int i_read = DStreamRead( s, NULL, (long)i_skip );
519
                if( i_read <= 0 ) return VLC_EGENERIC;
520 521 522 523
                i_skip -= i_read;
            }
            return VLC_SUCCESS;
        }
Laurent Aimar's avatar
Laurent Aimar committed
524 525 526 527 528 529

        case STREAM_GET_MTU:
            p_int = (int*) va_arg( args, int * );
            *p_int = 0;
            return VLC_SUCCESS;

530
        case STREAM_CONTROL_ACCESS:
531
        case STREAM_GET_CONTENT_TYPE:
532
        case STREAM_SET_RECORD_STATE:
533 534
            return VLC_EGENERIC;

Laurent Aimar's avatar
Laurent Aimar committed
535 536 537 538 539 540
        default:
            msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
            return VLC_EGENERIC;
    }
}

541
static void* DStreamThread( vlc_object_t* p_this )
Laurent Aimar's avatar
Laurent Aimar committed
542
{
543
    stream_t *s = (stream_t *)p_this;
Laurent Aimar's avatar
Laurent Aimar committed
544
    d_stream_sys_t *p_sys = (d_stream_sys_t*)s->p_sys;
545
    demux_t *p_demux;
546
    int canc = vlc_savecancel ();
Laurent Aimar's avatar
Laurent Aimar committed
547 548

    /* Create the demuxer */
549
    if( !(p_demux = demux_New( s, "", p_sys->psz_name, "", s, p_sys->out,
550
                               false )) )
Laurent Aimar's avatar
Laurent Aimar committed
551
    {
552
        return NULL;
Laurent Aimar's avatar
Laurent Aimar committed
553 554 555 556 557 558 559
    }

    p_sys->p_demux = p_demux;

    /* Main loop */
    while( !s->b_die && !p_demux->b_die )
    {
560
        if( p_demux->pf_demux( p_demux ) <= 0 ) break;
Laurent Aimar's avatar
Laurent Aimar committed
561
    }
562

563
    vlc_restorecancel (canc);
564
    vlc_object_kill( p_demux );
565
    return NULL;
Laurent Aimar's avatar
Laurent Aimar committed
566
}
567 568 569 570

/****************************************************************************
 * Utility functions
 ****************************************************************************/
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
decoder_t *demux_PacketizerNew( demux_t *p_demux, es_format_t *p_fmt, const char *psz_msg )
{
    decoder_t *p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_PACKETIZER );

    if( !p_packetizer )
    {
        es_format_Clean( p_fmt );
        return NULL;
    }

    p_packetizer->pf_decode_audio = NULL;
    p_packetizer->pf_decode_video = NULL;
    p_packetizer->pf_decode_sub = NULL;
    p_packetizer->pf_packetize = NULL;

    p_packetizer->fmt_in = *p_fmt;
    es_format_Init( &p_packetizer->fmt_out, UNKNOWN_ES, 0 );

589
    p_packetizer->p_module = module_need( p_packetizer, "packetizer", NULL, 0 );
590 591 592 593 594 595 596 597 598 599 600 601 602
    if( !p_packetizer->p_module )
    {
        es_format_Clean( p_fmt );
        vlc_object_release( p_packetizer );
        msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
        return NULL;
    }

    return p_packetizer;
}
void demux_PacketizerDestroy( decoder_t *p_packetizer )
{
    if( p_packetizer->p_module )
603
        module_unneed( p_packetizer, p_packetizer->p_module );
604 605 606 607
    es_format_Clean( &p_packetizer->fmt_in );
    vlc_object_release( p_packetizer );
}

608
static bool SkipID3Tag( demux_t *p_demux )
609
{
610
    const uint8_t *p_peek;
611 612 613 614
    uint8_t version, revision;
    int i_size;
    int b_footer;

615
    if( !p_demux->s )
616
        return false;
617 618

    /* Get 10 byte id3 header */
619
    if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
620
        return false;
621

622
    if( memcmp( p_peek, "ID3", 3 ) )
623
        return false;
624 625 626 627 628 629 630 631 632 633 634 635

    version = p_peek[3];
    revision = p_peek[4];
    b_footer = p_peek[5] & 0x10;
    i_size = (p_peek[6]<<21) + (p_peek[7]<<14) + (p_peek[8]<<7) + p_peek[9];

    if( b_footer ) i_size += 10;
    i_size += 10;

    /* Skip the entire tag */
    stream_Read( p_demux->s, NULL, i_size );

636
    msg_Dbg( p_demux, "ID3v2.%d revision %d tag found, skipping %d bytes",
637
             version, revision, i_size );
638
    return true;
639
}
640
static bool SkipAPETag( demux_t *p_demux )
641 642 643 644 645 646 647
{
    const uint8_t *p_peek;
    int i_version;
    int i_size;
    uint32_t flags;

    if( !p_demux->s )
648
        return false;
649 650 651

    /* Get 32 byte ape header */
    if( stream_Peek( p_demux->s, &p_peek, 32 ) < 32 )
652
        return false;
653 654

    if( memcmp( p_peek, "APETAGEX", 8 ) )
655
        return false;
656

657 658 659
    i_version = GetDWLE( &p_peek[8] );
    flags = GetDWLE( &p_peek[8+4+4] );
    if( ( i_version != 1000 && i_version != 2000 ) || !( flags & (1<<29) ) )
660
        return false;
661 662 663 664 665 666 667 668

    i_size = GetDWLE( &p_peek[8+4] ) + ( (flags&(1<<30)) ? 32 : 0 );

    /* Skip the entire tag */
    stream_Read( p_demux->s, NULL, i_size );

    msg_Dbg( p_demux, "AP2 v%d tag found, skipping %d bytes",
             i_version/1000, i_size );
669
    return true;
670
}
671