72880669aeee51e2d78dfa4b40cf100f1df5a09c
[vlc.git] / modules / demux / asf / asf.c
1 /*****************************************************************************
2  * asf.c : ASFv01 file input module for vlc
3  *****************************************************************************
4  * 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  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include <stdlib.h>                                      /* malloc(), free() */
27 #include <string.h>                                              /* strdup() */
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33
34 #include "libasf.h"
35 #include "asf.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int    Activate   ( vlc_object_t * );
41 static void   Deactivate ( vlc_object_t * );
42 static int    Demux      ( input_thread_t * );
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin();
48     set_description( "ASF v1.0 demuxer (file only)" );
49     set_capability( "demux", 200 );
50     set_callbacks( Activate, Deactivate );
51     add_shortcut( "asf" );
52 vlc_module_end();
53
54 static uint16_t GetWLE( uint8_t *p_buff )
55 {
56     return( (p_buff[0]) + ( p_buff[1] <<8 ) );
57 }
58 static uint32_t GetDWLE( uint8_t *p_buff )
59 {
60     return( p_buff[0] + ( p_buff[1] <<8 ) +
61             ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
62 }
63
64 /*****************************************************************************
65  * Activate: check file and initializes ASF structures
66  *****************************************************************************/
67 static int Activate( vlc_object_t * p_this )
68 {
69     input_thread_t  *p_input = (input_thread_t *)p_this;
70     uint8_t         *p_peek;
71     guid_t          guid;
72
73     demux_sys_t     *p_demux;
74     int             i_stream;
75
76     /* Initialize access plug-in structures. */
77     if( p_input->i_mtu == 0 )
78     {
79         /* Improve speed. */
80         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
81     }
82
83     p_input->pf_demux = Demux;
84
85     /* a little test to see if it could be a asf stream */
86     if( input_Peek( p_input, &p_peek, 16 ) < 16 )
87     {
88         msg_Warn( p_input, "ASF v1.0 plugin discarded (cannot peek)" );
89         return( -1 );
90     }
91     GetGUID( &guid, p_peek );
92     if( !CmpGUID( &guid, &asf_object_header_guid ) )
93     {
94         msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
95         return( -1 );
96     }
97
98     /* create our structure that will contains all data */
99     if( !( p_input->p_demux_data =
100                 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
101     {
102         msg_Err( p_input, "out of memory" );
103         return( -1 );
104     }
105     memset( p_demux, 0, sizeof( demux_sys_t ) );
106     p_demux->i_first_pts = -1;
107
108     /* Now load all object ( except raw data ) */
109     if( !ASF_ReadObjectRoot( p_input, &p_demux->root, p_input->stream.b_seekable ) )
110     {
111         msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
112         free( p_demux );
113         return( -1 );
114     }
115     /* Check if we have found all mandatory asf object */
116     if( !p_demux->root.p_hdr || !p_demux->root.p_data )
117     {
118         ASF_FreeObjectRoot( p_input, &p_demux->root );
119         free( p_demux );
120         msg_Warn( p_input, "ASF v1.0 plugin discarded (not a valid file)" );
121         return( -1 );
122     }
123
124     if( !( p_demux->p_fp = ASF_FindObject( p_demux->root.p_hdr,
125                                     &asf_object_file_properties_guid, 0 ) ) )
126     {
127         ASF_FreeObjectRoot( p_input, &p_demux->root );
128         free( p_demux );
129         msg_Warn( p_input, "ASF v1.0 plugin discarded (missing file_properties object)" );
130         return( -1 );
131     }
132
133     if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
134     {
135         ASF_FreeObjectRoot( p_input, &p_demux->root );
136         free( p_demux );
137         msg_Warn( p_input, "ASF v1.0 plugin discarded (invalid file_properties object)" );
138         return( -1 );
139     }
140
141     p_demux->i_streams = ASF_CountObject( p_demux->root.p_hdr,
142                                           &asf_object_stream_properties_guid );
143     if( !p_demux->i_streams )
144     {
145         ASF_FreeObjectRoot( p_input, &p_demux->root );
146         free( p_demux );
147         msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
148         return( -1 );
149     }
150     else
151     {
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     /*  create one program */
158     vlc_mutex_lock( &p_input->stream.stream_lock );
159     if( input_InitStream( p_input, 0 ) == -1)
160     {
161         vlc_mutex_unlock( &p_input->stream.stream_lock );
162         msg_Err( p_input, "cannot init stream" );
163         return( -1 );
164     }
165     if( input_AddProgram( p_input, 0, 0) == NULL )
166     {
167         vlc_mutex_unlock( &p_input->stream.stream_lock );
168         msg_Err( p_input, "cannot add program" );
169         return( -1 );
170     }
171     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
172     p_input->stream.i_mux_rate = 0 ; /* FIXME */
173     vlc_mutex_unlock( &p_input->stream.stream_lock );
174
175     for( i_stream = 0; i_stream < p_demux->i_streams; i_stream ++ )
176     {
177         asf_stream_t    *p_stream;
178         asf_object_stream_properties_t *p_sp;
179         char psz_cat[sizeof("Stream ")+10];
180         input_info_category_t *p_cat;
181         sprintf( psz_cat, "Stream %d", i_stream );
182         p_cat = input_InfoCategory( p_input, psz_cat);
183
184         p_sp = ASF_FindObject( p_demux->root.p_hdr,
185                                &asf_object_stream_properties_guid,
186                                i_stream );
187
188         p_stream =
189             p_demux->stream[p_sp->i_stream_number] =
190                 malloc( sizeof( asf_stream_t ) );
191         memset( p_stream, 0, sizeof( asf_stream_t ) );
192         p_stream->p_sp = p_sp;
193
194         vlc_mutex_lock( &p_input->stream.stream_lock );
195         p_stream->p_es =
196             input_AddES( p_input,
197                          p_input->stream.p_selected_program,
198                          p_sp->i_stream_number,
199                          0 );
200
201         vlc_mutex_unlock( &p_input->stream.stream_lock );
202         if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) )
203         {
204             int i_codec;
205             if( p_sp->p_type_specific_data )
206             {
207                 i_codec = GetWLE( p_sp->p_type_specific_data );
208             }
209             else
210             {
211                 i_codec = -1;
212             }
213
214             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                     i_codec,
219                     p_sp->i_stream_number );
220             switch( i_codec )
221             {
222                 case( 0x01 ):
223                     p_stream->p_es->i_fourcc =
224                         VLC_FOURCC( 'a', 'r', 'a', 'w' );
225                     break;
226                 case( 0x50 ):
227                 case( 0x55 ):
228                     p_stream->p_es->i_fourcc =
229                         VLC_FOURCC( 'm','p','g','a' );
230                     break;
231                 case( 0x2000 ):
232                     p_stream->p_es->i_fourcc =
233                         VLC_FOURCC( 'a','5','2',' ' );
234                     break;
235                 case( 0x160 ):
236                     p_stream->p_es->i_fourcc =
237                         VLC_FOURCC( 'w','m','a','1' );
238                     break;
239                 case( 0x161 ):
240                     p_stream->p_es->i_fourcc =
241                         VLC_FOURCC( 'w','m','a','2' );
242                     break;
243                 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             if( p_sp->i_type_specific_data_length > 0 )
249             {
250                 WAVEFORMATEX    *p_wf;
251                 size_t          i_size;
252                 uint8_t         *p_data;
253
254                 i_size = p_sp->i_type_specific_data_length;
255
256                 p_wf = malloc( i_size );
257                 p_stream->p_es->p_demux_data = (void*)p_wf;
258                 p_data = p_sp->p_type_specific_data;
259
260                 p_wf->wFormatTag        = GetWLE( p_data );
261                 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                 p_wf->nBlockAlign       = GetWLE( p_data + 12 );
268                 p_wf->wBitsPerSample    = GetWLE( p_data + 14 );
269                 input_AddInfo( p_cat, "Bits Per Sample", "%d", p_wf->wBitsPerSample );
270                 p_wf->cbSize            = __MAX( 0,
271                                               i_size - sizeof( WAVEFORMATEX ));
272                 if( i_size > sizeof( WAVEFORMATEX ) )
273                 {
274                     memcpy( (uint8_t*)p_stream->p_es->p_demux_data + sizeof( WAVEFORMATEX ),
275                             p_data + sizeof( WAVEFORMATEX ),
276                             i_size - sizeof( WAVEFORMATEX ) );
277                 }
278             }
279
280         }
281         else
282         if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) )
283         {
284             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                     p_sp->i_stream_number );
289             if( p_sp->p_type_specific_data )
290             {
291                 p_stream->p_es->i_fourcc =
292                     VLC_FOURCC( p_sp->p_type_specific_data[27],
293                                 p_sp->p_type_specific_data[28],
294                                 p_sp->p_type_specific_data[29],
295                                 p_sp->p_type_specific_data[30] );
296             }
297             else
298             {
299                 p_stream->p_es->i_fourcc =
300                     VLC_FOURCC( 'u','n','d','f' );
301             }
302             input_AddInfo( p_cat, "Codec", "%.4s", (char*)&p_stream->p_es->i_fourcc );
303             if( p_sp->i_type_specific_data_length > 11 )
304             {
305                 BITMAPINFOHEADER *p_bih;
306                 size_t      i_size;
307                 uint8_t     *p_data;
308
309                 i_size = p_sp->i_type_specific_data_length - 11;
310
311                 p_bih = malloc( i_size );
312                 p_stream->p_es->p_demux_data = (void*)p_bih;
313                 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                 p_bih->biWidth      = GetDWLE( p_data + 4 );
318                 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                 p_bih->biClrUsed       = GetDWLE( p_data + 32 );
333                 p_bih->biClrImportant  = GetDWLE( p_data + 36 );
334
335                 if( i_size > sizeof( BITMAPINFOHEADER ) )
336                 {
337                     memcpy( (uint8_t*)p_stream->p_es->p_demux_data + sizeof( BITMAPINFOHEADER ),
338                             p_data + sizeof( BITMAPINFOHEADER ),
339                             i_size - sizeof( BITMAPINFOHEADER ) );
340                 }
341             }
342
343         }
344         else
345         {
346             p_stream->i_cat = UNKNOWN_ES;
347             msg_Dbg( p_input,
348                     "ignoring unknown stream(ID:%d)",
349                     p_sp->i_stream_number );
350             p_stream->p_es->i_fourcc = VLC_FOURCC( 'u','n','d','f' );
351         }
352         p_stream->p_es->i_cat = p_stream->i_cat;
353
354         vlc_mutex_lock( &p_input->stream.stream_lock );
355         if( p_stream->p_es->i_fourcc != VLC_FOURCC( 'u','n','d','f' ) )
356         {
357             input_SelectES( p_input, p_stream->p_es );
358         }
359         vlc_mutex_unlock( &p_input->stream.stream_lock );
360     }
361
362
363     p_demux->i_data_begin = p_demux->root.p_data->i_object_pos + 50;
364     if( p_demux->root.p_data->i_object_size != 0 )
365     { // local file
366         p_demux->i_data_end = p_demux->root.p_data->i_object_pos +
367                                     p_demux->root.p_data->i_object_size;
368     }
369     else
370     { // live/broacast
371         p_demux->i_data_end = -1;
372     }
373
374
375     // go to first packet
376     ASF_SeekAbsolute( p_input, p_demux->i_data_begin );
377
378     vlc_mutex_lock( &p_input->stream.stream_lock );
379     /* try to calculate movie time */
380     if( p_demux->p_fp->i_data_packets_count > 0 )
381     {
382         int64_t i_count;
383         mtime_t i_length;
384
385         /* real number of packets */
386         i_count = ( p_input->stream.p_selected_area->i_size -
387                        p_demux->i_data_begin ) /
388                             p_demux->p_fp->i_min_data_packet_size;
389         /* calculate the time duration in s */
390         i_length = (mtime_t)p_demux->p_fp->i_play_duration / 10 *
391                    (mtime_t)i_count /
392                    (mtime_t)p_demux->p_fp->i_data_packets_count /
393                    (mtime_t)1000000;
394         if( i_length > 0 )
395         {
396             p_input->stream.i_mux_rate =
397                 p_input->stream.p_selected_area->i_size / 50 / i_length;
398         }
399         else
400         {
401             p_input->stream.i_mux_rate = 0;
402         }
403
404     }
405     else
406     {
407         /* cannot known */
408         p_input->stream.i_mux_rate = 0;
409     }
410
411
412
413     p_input->stream.p_selected_program->b_is_ok = 1;
414     vlc_mutex_unlock( &p_input->stream.stream_lock );
415
416
417     return( 0 );
418 }
419
420 /*****************************************************************************
421  * Demux: read packet and send them to decoders
422  *****************************************************************************/
423 #define GETVALUE2b( bits, var, def ) \
424     switch( (bits)&0x03 ) \
425     { \
426         case 1: var = p_peek[i_skip]; i_skip++; break; \
427         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
428         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
429         case 0: \
430         default: var = def; break;\
431     }
432
433 static int Demux( input_thread_t *p_input )
434 {
435     demux_sys_t *p_demux = p_input->p_demux_data;
436     int i;
437
438     /* catch seek from user */
439     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
440     {
441         off_t i_offset;
442         msleep( DEFAULT_PTS_DELAY );
443         i_offset = ASF_TellAbsolute( p_input ) - p_demux->i_data_begin;
444
445         if( i_offset  < 0 )
446         {
447             i_offset = 0;
448         }
449         /* 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         ASF_SeekAbsolute( p_input, p_demux->i_data_begin + i_offset );
453
454         p_demux->i_time = 0;
455         for( i = 0; i < 128 ; i++ )
456         {
457 #define p_stream p_demux->stream[i]
458             if( p_stream )
459             {
460                 p_stream->i_time = 0;
461             }
462 #undef p_stream
463         }
464         p_demux->i_first_pts = -1;
465     }
466
467
468     for( i = 0; i < 10; i++ ) // parse 10 packets
469     {
470         int     i_data_packet_min = p_demux->p_fp->i_min_data_packet_size;
471         uint8_t *p_peek;
472         int     i_skip;
473
474         int     i_packet_size_left;
475         int     i_packet_flags;
476         int     i_packet_property;
477
478         int     b_packet_multiple_payload;
479         int     i_packet_length;
480         int     i_packet_sequence;
481         int     i_packet_padding_length;
482
483         uint32_t    i_packet_send_time;
484         uint16_t    i_packet_duration;
485         int         i_payload;
486         int         i_payload_count;
487         int         i_payload_length_type;
488
489
490         if( input_Peek( p_input, &p_peek, i_data_packet_min ) < i_data_packet_min )
491         {
492             // EOF ?
493             msg_Err( p_input, "cannot peek while getting new packet, EOF ?" );
494             return( 0 );
495         }
496         i_skip = 0;
497
498         /* *** parse error correction if present *** */
499         if( p_peek[0]&0x80 )
500         {
501             int i_error_correction_length_type;
502             int i_error_correction_data_length;
503             int i_opaque_data_present;
504
505             i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
506             i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
507             i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
508             i_skip += 1; // skip error correction flags
509
510             if( i_error_correction_length_type != 0x00 ||
511                 i_opaque_data_present != 0 ||
512                 i_error_correction_data_length != 0x02 )
513             {
514                 goto loop_error_recovery;
515             }
516
517             i_skip += i_error_correction_data_length;
518         }
519         else
520         {
521             msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
522         }
523
524         i_packet_flags = p_peek[i_skip]; i_skip++;
525         i_packet_property = p_peek[i_skip]; i_skip++;
526
527         b_packet_multiple_payload = i_packet_flags&0x01;
528
529         /* read some value */
530         GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
531         GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
532         GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
533
534         i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
535         i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
536
537 //        i_packet_size_left = i_packet_length;   // XXX donnĂ©es reellement lu
538         /* FIXME I have to do that for some file, I don't known why */
539         i_packet_size_left = i_data_packet_min;
540
541         if( b_packet_multiple_payload )
542         {
543             i_payload_count = p_peek[i_skip] & 0x3f;
544             i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
545             i_skip++;
546         }
547         else
548         {
549             i_payload_count = 1;
550             i_payload_length_type = 0x02; // unused
551         }
552
553         for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
554         {
555             asf_stream_t   *p_stream;
556
557             int i_stream_number;
558             int i_media_object_number;
559             int i_media_object_offset;
560             int i_replicated_data_length;
561             int i_payload_data_length;
562             int i_payload_data_pos;
563             int i_sub_payload_data_length;
564             int i_tmp;
565
566             mtime_t i_pts;
567             mtime_t i_pts_delta;
568
569             i_stream_number = p_peek[i_skip] & 0x7f;
570             i_skip++;
571
572             GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
573             GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
574             GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
575
576             if( i_replicated_data_length > 1 ) // should be at least 8 bytes
577             {
578                 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
579                 i_skip += i_replicated_data_length;
580                 i_pts_delta = 0;
581
582                 i_media_object_offset = i_tmp;
583             }
584             else if( i_replicated_data_length == 1 )
585             {
586
587                 msg_Dbg( p_input, "found compressed payload" );
588
589                 i_pts = (mtime_t)i_tmp * 1000;
590                 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
591
592                 i_media_object_offset = 0;
593             }
594             else
595             {
596                 i_pts = (mtime_t)i_packet_send_time * 1000;
597                 i_pts_delta = 0;
598
599                 i_media_object_offset = i_tmp;
600             }
601
602             i_pts = __MAX( i_pts - p_demux->p_fp->i_preroll * 1000, 0 );
603             if( b_packet_multiple_payload )
604             {
605                 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
606             }
607             else
608             {
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                       i_payload_count,
618                       i_stream_number,
619                       i_media_object_number,
620                       i_media_object_offset,
621                       i_replicated_data_length,
622                       i_payload_data_length );
623 #endif
624
625             if( !( p_stream = p_demux->stream[i_stream_number] ) )
626             {
627                 msg_Warn( p_input,
628                           "undeclared stream[Id 0x%x]", i_stream_number );
629                 i_skip += i_payload_data_length;
630                 continue;   // over payload
631             }
632
633             if( !p_stream->p_es || !p_stream->p_es->p_decoder_fifo )
634             {
635                 i_skip += i_payload_data_length;
636                 continue;
637             }
638
639
640             for( i_payload_data_pos = 0;
641                  i_payload_data_pos < i_payload_data_length &&
642                         i_packet_size_left > 0;
643                  i_payload_data_pos += i_sub_payload_data_length )
644             {
645                 data_packet_t  *p_data;
646                 int i_read;
647                 // read sub payload length
648                 if( i_replicated_data_length == 1 )
649                 {
650                     i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
651                     i_payload_data_pos++;
652                 }
653                 else
654                 {
655                     i_sub_payload_data_length = i_payload_data_length;
656                 }
657
658                 /* FIXME I don't use i_media_object_number, sould I ? */
659                 if( p_stream->p_pes && i_media_object_offset == 0 )                 {
660                     /* send complete packet to decoder */
661                     if( p_stream->p_pes->i_pes_size > 0 )
662                     {
663                         input_DecodePES( p_stream->p_es->p_decoder_fifo, p_stream->p_pes );
664                         p_stream->p_pes = NULL;
665                     }
666                 }
667
668                 if( !p_stream->p_pes )  // add a new PES
669                 {
670                     p_stream->i_time =
671                         ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
672
673                     if( p_demux->i_first_pts == -1 )
674                     {
675                         p_demux->i_first_pts = p_stream->i_time;
676                     }
677                     p_stream->i_time -= p_demux->i_first_pts;
678
679                     p_stream->p_pes = input_NewPES( p_input->p_method_data );
680                     p_stream->p_pes->i_dts =
681                         p_stream->p_pes->i_pts =
682                             input_ClockGetTS( p_input,
683                                               p_input->stream.p_selected_program,
684                                               ( p_stream->i_time+DEFAULT_PTS_DELAY) * 9 /100 );
685
686                     p_stream->p_pes->p_next = NULL;
687                     p_stream->p_pes->i_nb_data = 0;
688                     p_stream->p_pes->i_pes_size = 0;
689                 }
690
691                 i_read = i_sub_payload_data_length + i_skip;
692                 if( input_SplitBuffer( p_input, &p_data, i_read ) < i_read )
693                 {
694                     msg_Err( p_input, "cannot read data" );
695                     return( 0 );
696                 }
697                 p_data->p_payload_start += i_skip;
698                 i_packet_size_left -= i_read;
699
700
701                 if( !p_stream->p_pes->p_first )
702                 {
703                     p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
704                 }
705                 else
706                 {
707                     p_stream->p_pes->p_last->p_next = p_data;
708                     p_stream->p_pes->p_last = p_data;
709                 }
710                 p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
711                 p_stream->p_pes->i_nb_data++;
712
713                 i_skip = 0;
714                 if( i_packet_size_left > 0 )
715                 {
716                     if( input_Peek( p_input, &p_peek, i_packet_size_left ) < i_packet_size_left )
717                     {
718                         // EOF ?
719                         msg_Warn( p_input, "cannot peek, EOF ?" );
720                         return( 0 );
721                     }
722                 }
723             }
724         }
725
726         if( i_packet_size_left > 0 )
727         {
728             if( !ASF_SkipBytes( p_input, i_packet_size_left ) )
729             {
730                 msg_Warn( p_input, "cannot skip data, EOF ?" );
731                 return( 0 );
732             }
733         }
734
735         continue;
736
737 loop_error_recovery:
738         msg_Warn( p_input, "unsupported packet header" );
739         if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
740         {
741             msg_Err( p_input, "unsupported packet header, fatal error" );
742             return( -1 );
743         }
744         ASF_SkipBytes( p_input, i_data_packet_min );
745
746     }   // loop over packet
747     p_demux->i_time = -1;
748     for( i = 0; i < 128 ; i++ )
749     {
750 #define p_stream p_demux->stream[i]
751         if( p_stream && p_stream->p_es && p_stream->p_es->p_decoder_fifo )
752         {
753             if( p_demux->i_time < 0 )
754             {
755                 p_demux->i_time = p_stream->i_time;
756             }
757             else
758             {
759                 p_demux->i_time = __MIN( p_demux->i_time, p_stream->i_time );
760             }
761         }
762 #undef p_stream
763     }
764
765     if( p_demux->i_time >= 0 )
766     {
767         /* update pcr XXX in mpeg scale so in 90000 unit/s */
768         p_demux->i_pcr =( __MAX( p_demux->i_time /*- DEFAULT_PTS_DELAY*/, 0 ) ) * 9 / 100;
769
770         /* first wait for the good time to read next packets */
771         input_ClockManageRef( p_input,
772                               p_input->stream.p_selected_program,
773                               p_demux->i_pcr );
774     }
775
776
777     return( 1 );
778 }
779
780 /*****************************************************************************
781  * MP4End: frees unused data
782  *****************************************************************************/
783 static void Deactivate( vlc_object_t * p_this )
784 {
785 #define FREE( p ) \
786     if( p ) { free( p ); }
787
788     input_thread_t *  p_input = (input_thread_t *)p_this;
789     demux_sys_t *p_demux = p_input->p_demux_data;
790     int i_stream;
791
792     msg_Dbg( p_input, "Freeing all memory" );
793     ASF_FreeObjectRoot( p_input, &p_demux->root );
794     for( i_stream = 0; i_stream < 128; i_stream++ )
795     {
796 #define p_stream p_demux->stream[i_stream]
797         if( p_stream )
798         {
799             if( p_stream->p_pes )
800             {
801                 input_DeletePES( p_input->p_method_data, p_stream->p_pes );
802             }
803             free( p_stream );
804         }
805 #undef p_stream
806     }
807
808 #undef FREE
809 }
810