asf.c 28.3 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * asf.c : ASFv01 file input module for vlc
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: asf.c,v 1.13 2003/01/05 21:03:58 sigmunau Exp $
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8 9 10 11
 * 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.
12
 *
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 * 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
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>                                              /* strdup() */
#include <errno.h>
#include <sys/types.h>

#include <vlc/vlc.h>
#include <vlc/input.h>

#include "libasf.h"
#include "asf.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int    Activate   ( vlc_object_t * );
static void   Deactivate ( vlc_object_t * );
static int    Demux      ( input_thread_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( "ASF v1.0 demuxer (file only)" );
    set_capability( "demux", 200 );
    set_callbacks( Activate, Deactivate );
    add_shortcut( "asf" );
vlc_module_end();

54
static uint16_t GetWLE( uint8_t *p_buff )
55 56 57
{
    return( (p_buff[0]) + ( p_buff[1] <<8 ) );
}
58
static uint32_t GetDWLE( uint8_t *p_buff )
59
{
60 61 62 63 64 65 66 67
    return( p_buff[0] + ( p_buff[1] <<8 ) +
            ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
}

/*****************************************************************************
 * Activate: check file and initializes ASF structures
 *****************************************************************************/
static int Activate( vlc_object_t * p_this )
68
{
69 70 71
    input_thread_t  *p_input = (input_thread_t *)p_this;
    uint8_t         *p_peek;
    guid_t          guid;
72

73 74
    demux_sys_t     *p_demux;
    int             i_stream;
75

76 77 78 79
    /* Initialize access plug-in structures. */
    if( p_input->i_mtu == 0 )
    {
        /* Improve speed. */
80
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
81 82 83 84 85
    }

    p_input->pf_demux = Demux;

    /* a little test to see if it could be a asf stream */
86
    if( input_Peek( p_input, &p_peek, 16 ) < 16 )
87 88 89 90 91 92 93 94 95 96 97 98
    {
        msg_Warn( p_input, "ASF v1.0 plugin discarded (cannot peek)" );
        return( -1 );
    }
    GetGUID( &guid, p_peek );
    if( !CmpGUID( &guid, &asf_object_header_guid ) )
    {
        msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
        return( -1 );
    }

    /* create our structure that will contains all data */
99
    if( !( p_input->p_demux_data =
100 101 102 103 104 105
                p_demux = malloc( sizeof( demux_sys_t ) ) ) )
    {
        msg_Err( p_input, "out of memory" );
        return( -1 );
    }
    memset( p_demux, 0, sizeof( demux_sys_t ) );
106
    p_demux->i_first_pts = -1;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    /* Now load all object ( except raw data ) */
    if( !ASF_ReadObjectRoot( p_input, &p_demux->root, p_input->stream.b_seekable ) )
    {
        msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
        free( p_demux );
        return( -1 );
    }
    /* Check if we have found all mandatory asf object */
    if( !p_demux->root.p_hdr || !p_demux->root.p_data )
    {
        ASF_FreeObjectRoot( p_input, &p_demux->root );
        free( p_demux );
        msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
        return( -1 );
    }
123

124
    if( !( p_demux->p_fp = ASF_FindObject( p_demux->root.p_hdr,
125 126 127 128 129 130 131
                                    &asf_object_file_properties_guid, 0 ) ) )
    {
        ASF_FreeObjectRoot( p_input, &p_demux->root );
        free( p_demux );
        msg_Warn( p_input, "ASF v1.0 plugin discarded (missing file_properties object)" );
        return( -1 );
    }
132

133 134 135 136 137 138 139
    if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
    {
        ASF_FreeObjectRoot( p_input, &p_demux->root );
        free( p_demux );
        msg_Warn( p_input, "ASF v1.0 plugin discarded (invalid file_properties object)" );
        return( -1 );
    }
140 141

    p_demux->i_streams = ASF_CountObject( p_demux->root.p_hdr,
142 143 144 145 146 147 148 149 150 151
                                          &asf_object_stream_properties_guid );
    if( !p_demux->i_streams )
    {
        ASF_FreeObjectRoot( p_input, &p_demux->root );
        free( p_demux );
        msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
        return( -1 );
    }
    else
    {
152
        input_info_category_t *p_cat = input_InfoCategory( p_input, "Asf" );
153
        msg_Dbg( p_input, "found %d streams", p_demux->i_streams );
154
        input_AddInfo( p_cat, "Number of streams", "%d" , p_demux->i_streams );
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    }

    /*  create one program */
    vlc_mutex_lock( &p_input->stream.stream_lock );
    if( input_InitStream( p_input, 0 ) == -1)
    {
        vlc_mutex_unlock( &p_input->stream.stream_lock );
        msg_Err( p_input, "cannot init stream" );
        return( -1 );
    }
    if( input_AddProgram( p_input, 0, 0) == NULL )
    {
        vlc_mutex_unlock( &p_input->stream.stream_lock );
        msg_Err( p_input, "cannot add program" );
        return( -1 );
    }
    p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
    p_input->stream.i_mux_rate = 0 ; /* FIXME */
    vlc_mutex_unlock( &p_input->stream.stream_lock );

    for( i_stream = 0; i_stream < p_demux->i_streams; i_stream ++ )
    {
        asf_stream_t    *p_stream;
        asf_object_stream_properties_t *p_sp;
179 180 181 182
        char psz_cat[sizeof("Stream ")+10];
        input_info_category_t *p_cat;
        sprintf( psz_cat, "Stream %d", i_stream );
        p_cat = input_InfoCategory( p_input, psz_cat);
183

184 185 186
        p_sp = ASF_FindObject( p_demux->root.p_hdr,
                               &asf_object_stream_properties_guid,
                               i_stream );
187

188 189
        p_stream =
            p_demux->stream[p_sp->i_stream_number] =
190
                malloc( sizeof( asf_stream_t ) );
191 192 193 194
        memset( p_stream, 0, sizeof( asf_stream_t ) );
        p_stream->p_sp = p_sp;

        vlc_mutex_lock( &p_input->stream.stream_lock );
195
        p_stream->p_es =
196 197 198
            input_AddES( p_input,
                         p_input->stream.p_selected_program,
                         p_sp->i_stream_number,
199
                         0 );
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

        vlc_mutex_unlock( &p_input->stream.stream_lock );
        if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) )
        {
            int i_codec;
            if( p_sp->p_type_specific_data )
            {
                i_codec = GetWLE( p_sp->p_type_specific_data );
            }
            else
            {
                i_codec = -1;
            }

            p_stream->i_cat = AUDIO_ES;
215
            input_AddInfo( p_cat, "Type", "Audio" );
216
            msg_Dbg( p_input,
217
                    "adding new audio stream(codec:0x%x,ID:%d)",
218 219 220 221
                    i_codec,
                    p_sp->i_stream_number );
            switch( i_codec )
            {
222
                case( 0x01 ):
223
                    p_stream->p_es->i_fourcc =
224 225
                        VLC_FOURCC( 'a', 'r', 'a', 'w' );
                    break;
226 227
                case( 0x50 ):
                case( 0x55 ):
228
                    p_stream->p_es->i_fourcc =
229 230 231
                        VLC_FOURCC( 'm','p','g','a' );
                    break;
                case( 0x2000 ):
232
                    p_stream->p_es->i_fourcc =
233 234 235
                        VLC_FOURCC( 'a','5','2',' ' );
                    break;
                case( 0x160 ):
236
                    p_stream->p_es->i_fourcc =
237 238
                        VLC_FOURCC( 'w','m','a','1' );
                    break;
239
                case( 0x161 ):
240
                    p_stream->p_es->i_fourcc =
241
                        VLC_FOURCC( 'w','m','a','2' );
242 243
                    break;
                default:
244
                    p_stream->p_es->i_fourcc =
245
                        VLC_FOURCC( 'm','s',(i_codec >> 8)&0xff,i_codec&0xff );
246
            }
247
            input_AddInfo( p_cat, "Codec", "%.4s", (char*)&p_stream->p_es->i_fourcc );
248 249
            if( p_sp->i_type_specific_data_length > 0 )
            {
250
                WAVEFORMATEX    *p_wf;
251
                size_t          i_size;
252
                uint8_t         *p_data;
253

254 255 256 257 258 259 260 261
                i_size = p_sp->i_type_specific_data_length;

                p_wf = malloc( i_size );
                p_stream->p_es->p_demux_data = (void*)p_wf;
                p_data = p_sp->p_type_specific_data;

                p_wf->wFormatTag        = GetWLE( p_data );
                p_wf->nChannels         = GetWLE( p_data + 2 );
262
                input_AddInfo( p_cat, "Channels", "%d", p_wf->nChannels );
263
                p_wf->nSamplesPerSec    = GetDWLE( p_data + 4 );
264
                input_AddInfo( p_cat, "Sample rate", "%d", p_wf->nSamplesPerSec );
265
                p_wf->nAvgBytesPerSec   = GetDWLE( p_data + 8 );
266
                input_AddInfo( p_cat, "Avg. byterate", "%d", p_wf->nAvgBytesPerSec );
267 268
                p_wf->nBlockAlign       = GetWLE( p_data + 12 );
                p_wf->wBitsPerSample    = GetWLE( p_data + 14 );
269
                input_AddInfo( p_cat, "Bits Per Sample", "%d", p_wf->wBitsPerSample );
270 271
                p_wf->cbSize            = __MAX( 0,
                                              i_size - sizeof( WAVEFORMATEX ));
272 273 274 275 276 277
                if( i_size > sizeof( WAVEFORMATEX ) )
                {
                    memcpy( (uint8_t*)p_stream->p_es->p_demux_data + sizeof( WAVEFORMATEX ),
                            p_data + sizeof( WAVEFORMATEX ),
                            i_size - sizeof( WAVEFORMATEX ) );
                }
278
            }
279

280 281 282 283 284
        }
        else
        if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) )
        {
            p_stream->i_cat = VIDEO_ES;
285
            input_AddInfo( p_cat, "Type", "Video" );
286
            msg_Dbg( p_input,
287
                    "adding new video stream(ID:%d)",
288 289 290 291
                    p_sp->i_stream_number );
            if( p_sp->p_type_specific_data )
            {
                p_stream->p_es->i_fourcc =
292 293 294 295
                    VLC_FOURCC( p_sp->p_type_specific_data[27],
                                p_sp->p_type_specific_data[28],
                                p_sp->p_type_specific_data[29],
                                p_sp->p_type_specific_data[30] );
296 297 298
            }
            else
            {
299
                p_stream->p_es->i_fourcc =
300 301
                    VLC_FOURCC( 'u','n','d','f' );
            }
302
            input_AddInfo( p_cat, "Codec", "%.4s", (char*)&p_stream->p_es->i_fourcc );
303 304
            if( p_sp->i_type_specific_data_length > 11 )
            {
305
                BITMAPINFOHEADER *p_bih;
306
                size_t      i_size;
307 308 309
                uint8_t     *p_data;

                i_size = p_sp->i_type_specific_data_length - 11;
310

311 312 313
                p_bih = malloc( i_size );
                p_stream->p_es->p_demux_data = (void*)p_bih;
                p_data = p_sp->p_type_specific_data + 11;
314

315
                p_bih->biSize       = GetDWLE( p_data );
316
                input_AddInfo( p_cat, "Size", "%d", p_bih->biSize );
317 318
                p_bih->biWidth      = GetDWLE( p_data + 4 );
                p_bih->biHeight     = GetDWLE( p_data + 8 );
319
                input_AddInfo( p_cat, "Resolution", "%dx%d", p_bih->biWidth, p_bih->biHeight );
320
                p_bih->biPlanes     = GetDWLE( p_data + 12 );
321
                input_AddInfo( p_cat, "Planes", "%d", p_bih->biPlanes );
322
                p_bih->biBitCount   = GetDWLE( p_data + 14 );
323
                input_AddInfo( p_cat, "Bits per pixel", "%d", p_bih->biBitCount );
324
                p_bih->biCompression= GetDWLE( p_data + 16 );
325
                input_AddInfo( p_cat, "Compression Rate", "%d", p_bih->biCompression );
326
                p_bih->biSizeImage  = GetDWLE( p_data + 20 );
327
                input_AddInfo( p_cat, "Image Size", "%d", p_bih->biSizeImage );
328
                p_bih->biXPelsPerMeter = GetDWLE( p_data + 24 );
329
                input_AddInfo( p_cat, "X pixels per meter", "%d", p_bih->biXPelsPerMeter );
330
                p_bih->biYPelsPerMeter = GetDWLE( p_data + 28 );
331
                input_AddInfo( p_cat, "Y pixels per meter", "%d", p_bih->biYPelsPerMeter );
332 333 334 335 336 337 338 339 340
                p_bih->biClrUsed       = GetDWLE( p_data + 32 );
                p_bih->biClrImportant  = GetDWLE( p_data + 36 );

                if( i_size > sizeof( BITMAPINFOHEADER ) )
                {
                    memcpy( (uint8_t*)p_stream->p_es->p_demux_data + sizeof( BITMAPINFOHEADER ),
                            p_data + sizeof( BITMAPINFOHEADER ),
                            i_size - sizeof( BITMAPINFOHEADER ) );
                }
341
            }
342 343 344 345 346

        }
        else
        {
            p_stream->i_cat = UNKNOWN_ES;
347 348
            msg_Dbg( p_input,
                    "ignoring unknown stream(ID:%d)",
349 350 351 352 353 354
                    p_sp->i_stream_number );
            p_stream->p_es->i_fourcc = VLC_FOURCC( 'u','n','d','f' );
        }
        p_stream->p_es->i_cat = p_stream->i_cat;

        vlc_mutex_lock( &p_input->stream.stream_lock );
355 356 357 358
        if( p_stream->p_es->i_fourcc != VLC_FOURCC( 'u','n','d','f' ) )
        {
            input_SelectES( p_input, p_stream->p_es );
        }
359 360
        vlc_mutex_unlock( &p_input->stream.stream_lock );
    }
361

362 363 364 365 366 367 368 369 370 371 372

    p_demux->i_data_begin = p_demux->root.p_data->i_object_pos + 50;
    if( p_demux->root.p_data->i_object_size != 0 )
    { // local file
        p_demux->i_data_end = p_demux->root.p_data->i_object_pos +
                                    p_demux->root.p_data->i_object_size;
    }
    else
    { // live/broacast
        p_demux->i_data_end = -1;
    }
373 374 375


    // go to first packet
376
    ASF_SeekAbsolute( p_input, p_demux->i_data_begin );
377 378 379 380 381 382 383 384 385

    vlc_mutex_lock( &p_input->stream.stream_lock );
    /* try to calculate movie time */
    if( p_demux->p_fp->i_data_packets_count > 0 )
    {
        int64_t i_count;
        mtime_t i_length;

        /* real number of packets */
386 387
        i_count = ( p_input->stream.p_selected_area->i_size -
                       p_demux->i_data_begin ) /
388 389
                            p_demux->p_fp->i_min_data_packet_size;
        /* calculate the time duration in s */
390 391
        i_length = (mtime_t)p_demux->p_fp->i_play_duration / 10 *
                   (mtime_t)i_count /
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
                   (mtime_t)p_demux->p_fp->i_data_packets_count /
                   (mtime_t)1000000;
        if( i_length > 0 )
        {
            p_input->stream.i_mux_rate =
                p_input->stream.p_selected_area->i_size / 50 / i_length;
        }
        else
        {
            p_input->stream.i_mux_rate = 0;
        }

    }
    else
    {
        /* cannot known */
        p_input->stream.i_mux_rate = 0;
    }



    p_input->stream.p_selected_program->b_is_ok = 1;
    vlc_mutex_unlock( &p_input->stream.stream_lock );

416

417 418 419 420
    return( 0 );
}

/*****************************************************************************
421
 * Demux: read packet and send them to decoders
422 423
 *****************************************************************************/
#define GETVALUE2b( bits, var, def ) \
424
    switch( (bits)&0x03 ) \
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    { \
        case 1: var = p_peek[i_skip]; i_skip++; break; \
        case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
        case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
        case 0: \
        default: var = def; break;\
    }

static int Demux( input_thread_t *p_input )
{
    demux_sys_t *p_demux = p_input->p_demux_data;
    int i;

    /* catch seek from user */
    if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
    {
        off_t i_offset;
442
        msleep( DEFAULT_PTS_DELAY );
443
        i_offset = ASF_TellAbsolute( p_input ) - p_demux->i_data_begin;
444

445 446 447 448 449
        if( i_offset  < 0 )
        {
            i_offset = 0;
        }
        /* XXX work only when i_min_data_packet_size == i_max_data_packet_size */
450
        i_offset += p_demux->p_fp->i_min_data_packet_size -
451
                        i_offset % p_demux->p_fp->i_min_data_packet_size;
452 453 454
        ASF_SeekAbsolute( p_input, p_demux->i_data_begin + i_offset );

        p_demux->i_time = 0;
455
        for( i = 0; i < 128 ; i++ )
456 457 458 459 460 461 462 463
        {
#define p_stream p_demux->stream[i]
            if( p_stream )
            {
                p_stream->i_time = 0;
            }
#undef p_stream
        }
464
        p_demux->i_first_pts = -1;
465 466 467 468 469
    }


    for( i = 0; i < 10; i++ ) // parse 10 packets
    {
470 471 472
        int     i_data_packet_min = p_demux->p_fp->i_min_data_packet_size;
        uint8_t *p_peek;
        int     i_skip;
473

474 475 476 477 478 479 480 481
        int     i_packet_size_left;
        int     i_packet_flags;
        int     i_packet_property;

        int     b_packet_multiple_payload;
        int     i_packet_length;
        int     i_packet_sequence;
        int     i_packet_padding_length;
482

483 484 485 486 487
        uint32_t    i_packet_send_time;
        uint16_t    i_packet_duration;
        int         i_payload;
        int         i_payload_count;
        int         i_payload_length_type;
488

489

490 491 492 493 494 495 496
        if( input_Peek( p_input, &p_peek, i_data_packet_min ) < i_data_packet_min )
        {
            // EOF ?
            msg_Err( p_input, "cannot peek while getting new packet, EOF ?" );
            return( 0 );
        }
        i_skip = 0;
497

498 499 500 501 502 503 504 505 506 507 508
        /* *** parse error correction if present *** */
        if( p_peek[0]&0x80 )
        {
            int i_error_correction_length_type;
            int i_error_correction_data_length;
            int i_opaque_data_present;

            i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
            i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
            i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
            i_skip += 1; // skip error correction flags
509

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
            if( i_error_correction_length_type != 0x00 ||
                i_opaque_data_present != 0 ||
                i_error_correction_data_length != 0x02 )
            {
                goto loop_error_recovery;
            }

            i_skip += i_error_correction_data_length;
        }
        else
        {
            msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
        }

        i_packet_flags = p_peek[i_skip]; i_skip++;
        i_packet_property = p_peek[i_skip]; i_skip++;
526

527 528 529
        b_packet_multiple_payload = i_packet_flags&0x01;

        /* read some value */
530 531 532
        GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
        GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
        GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
533 534 535

        i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
        i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
536

537 538 539
//        i_packet_size_left = i_packet_length;   // XXX donnes reellement lu
        /* FIXME I have to do that for some file, I don't known why */
        i_packet_size_left = i_data_packet_min;
540

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
        if( b_packet_multiple_payload )
        {
            i_payload_count = p_peek[i_skip] & 0x3f;
            i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
            i_skip++;
        }
        else
        {
            i_payload_count = 1;
            i_payload_length_type = 0x02; // unused
        }

        for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
        {
            asf_stream_t   *p_stream;

            int i_stream_number;
            int i_media_object_number;
            int i_media_object_offset;
            int i_replicated_data_length;
            int i_payload_data_length;
            int i_payload_data_pos;
            int i_sub_payload_data_length;
            int i_tmp;

            mtime_t i_pts;
            mtime_t i_pts_delta;

569
            i_stream_number = p_peek[i_skip] & 0x7f;
570
            i_skip++;
571

572 573 574
            GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
            GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
            GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
575

576 577 578 579 580 581 582 583 584 585 586
            if( i_replicated_data_length > 1 ) // should be at least 8 bytes
            {
                i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
                i_skip += i_replicated_data_length;
                i_pts_delta = 0;

                i_media_object_offset = i_tmp;
            }
            else if( i_replicated_data_length == 1 )
            {

587
                msg_Dbg( p_input, "found compressed payload" );
588 589 590 591 592 593 594 595 596 597 598 599 600 601

                i_pts = (mtime_t)i_tmp * 1000;
                i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;

                i_media_object_offset = 0;
            }
            else
            {
                i_pts = (mtime_t)i_packet_send_time * 1000;
                i_pts_delta = 0;

                i_media_object_offset = i_tmp;
            }

602
            i_pts = __MAX( i_pts - p_demux->p_fp->i_preroll * 1000, 0 );
603 604 605 606 607 608
            if( b_packet_multiple_payload )
            {
                GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
            }
            else
            {
609
                i_payload_data_length = i_packet_length -
610
                                            i_packet_padding_length - i_skip;
611
            }
612

613
#if 0
614
             msg_Dbg( p_input,
615
                      "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
616
                      i_payload + 1,
617 618
                      i_payload_count,
                      i_stream_number,
619 620 621
                      i_media_object_number,
                      i_media_object_offset,
                      i_replicated_data_length,
622
                      i_payload_data_length );
623
#endif
624 625 626

            if( !( p_stream = p_demux->stream[i_stream_number] ) )
            {
627
                msg_Warn( p_input,
628
                          "undeclared stream[Id 0x%x]", i_stream_number );
629 630 631 632 633 634 635 636 637 638 639
                i_skip += i_payload_data_length;
                continue;   // over payload
            }

            if( !p_stream->p_es || !p_stream->p_es->p_decoder_fifo )
            {
                i_skip += i_payload_data_length;
                continue;
            }


640 641 642
            for( i_payload_data_pos = 0;
                 i_payload_data_pos < i_payload_data_length &&
                        i_packet_size_left > 0;
643
                 i_payload_data_pos += i_sub_payload_data_length )
644 645 646 647 648 649 650 651 652 653 654 655 656 657
            {
                data_packet_t  *p_data;
                int i_read;
                // read sub payload length
                if( i_replicated_data_length == 1 )
                {
                    i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
                    i_payload_data_pos++;
                }
                else
                {
                    i_sub_payload_data_length = i_payload_data_length;
                }

658 659 660
                /* FIXME I don't use i_media_object_number, sould I ? */
                if( p_stream->p_pes && i_media_object_offset == 0 )                 {
                    /* send complete packet to decoder */
661 662 663 664 665 666 667 668 669
                    if( p_stream->p_pes->i_pes_size > 0 )
                    {
                        input_DecodePES( p_stream->p_es->p_decoder_fifo, p_stream->p_pes );
                        p_stream->p_pes = NULL;
                    }
                }

                if( !p_stream->p_pes )  // add a new PES
                {
670
                    p_stream->i_time =
671 672 673 674 675 676 677 678
                        ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );

                    if( p_demux->i_first_pts == -1 )
                    {
                        p_demux->i_first_pts = p_stream->i_time;
                    }
                    p_stream->i_time -= p_demux->i_first_pts;

679
                    p_stream->p_pes = input_NewPES( p_input->p_method_data );
680 681 682
                    p_stream->p_pes->i_dts =
                        p_stream->p_pes->i_pts =
                            input_ClockGetTS( p_input,
683
                                              p_input->stream.p_selected_program,
684
                                              ( p_stream->i_time+DEFAULT_PTS_DELAY) * 9 /100 );
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699

                    p_stream->p_pes->p_next = NULL;
                    p_stream->p_pes->i_nb_data = 0;
                    p_stream->p_pes->i_pes_size = 0;
                }

                i_read = i_sub_payload_data_length + i_skip;
                if( input_SplitBuffer( p_input, &p_data, i_read ) < i_read )
                {
                    msg_Err( p_input, "cannot read data" );
                    return( 0 );
                }
                p_data->p_payload_start += i_skip;
                i_packet_size_left -= i_read;

700

701 702 703 704 705 706 707 708 709 710 711 712 713
                if( !p_stream->p_pes->p_first )
                {
                    p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
                }
                else
                {
                    p_stream->p_pes->p_last->p_next = p_data;
                    p_stream->p_pes->p_last = p_data;
                }
                p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
                p_stream->p_pes->i_nb_data++;

                i_skip = 0;
714
                if( i_packet_size_left > 0 )
715
                {
716 717 718 719 720 721
                    if( input_Peek( p_input, &p_peek, i_packet_size_left ) < i_packet_size_left )
                    {
                        // EOF ?
                        msg_Warn( p_input, "cannot peek, EOF ?" );
                        return( 0 );
                    }
722 723 724
                }
            }
        }
725

726 727 728 729
        if( i_packet_size_left > 0 )
        {
            if( !ASF_SkipBytes( p_input, i_packet_size_left ) )
            {
730
                msg_Warn( p_input, "cannot skip data, EOF ?" );
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
                return( 0 );
            }
        }

        continue;

loop_error_recovery:
        msg_Warn( p_input, "unsupported packet header" );
        if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
        {
            msg_Err( p_input, "unsupported packet header, fatal error" );
            return( -1 );
        }
        ASF_SkipBytes( p_input, i_data_packet_min );

    }   // loop over packet
747
    p_demux->i_time = -1;
748
    for( i = 0; i < 128 ; i++ )
749 750 751 752
    {
#define p_stream p_demux->stream[i]
        if( p_stream && p_stream->p_es && p_stream->p_es->p_decoder_fifo )
        {
753 754 755 756 757 758 759 760
            if( p_demux->i_time < 0 )
            {
                p_demux->i_time = p_stream->i_time;
            }
            else
            {
                p_demux->i_time = __MIN( p_demux->i_time, p_stream->i_time );
            }
761 762 763
        }
#undef p_stream
    }
764 765 766 767 768

    if( p_demux->i_time >= 0 )
    {
        /* update pcr XXX in mpeg scale so in 90000 unit/s */
        p_demux->i_pcr =( __MAX( p_demux->i_time /*- DEFAULT_PTS_DELAY*/, 0 ) ) * 9 / 100;
769

770 771 772 773 774 775
        /* first wait for the good time to read next packets */
        input_ClockManageRef( p_input,
                              p_input->stream.p_selected_program,
                              p_demux->i_pcr );
    }

776

777 778 779 780 781 782 783
    return( 1 );
}

/*****************************************************************************
 * MP4End: frees unused data
 *****************************************************************************/
static void Deactivate( vlc_object_t * p_this )
784
{
785
#define FREE( p ) \
786 787
    if( p ) { free( p ); }

788 789
    input_thread_t *  p_input = (input_thread_t *)p_this;
    demux_sys_t *p_demux = p_input->p_demux_data;
790
    int i_stream;
791

792 793
    msg_Dbg( p_input, "Freeing all memory" );
    ASF_FreeObjectRoot( p_input, &p_demux->root );
794 795 796 797 798 799 800 801 802 803 804 805 806
    for( i_stream = 0; i_stream < 128; i_stream++ )
    {
#define p_stream p_demux->stream[i_stream]
        if( p_stream )
        {
            if( p_stream->p_pes )
            {
                input_DeletePES( p_input->p_method_data, p_stream->p_pes );
            }
            free( p_stream );
        }
#undef p_stream
    }
807

808 809 810
#undef FREE
}