]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
1e3eb475caeee6f4e6232a071743b1a359a2a5c8
[vlc] / 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.11 2002/12/03 17:00:16 fenrir 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         msg_Dbg( p_input, "found %d streams", p_demux->i_streams );
153     }
154
155     /*  create one program */
156     vlc_mutex_lock( &p_input->stream.stream_lock );
157     if( input_InitStream( p_input, 0 ) == -1)
158     {
159         vlc_mutex_unlock( &p_input->stream.stream_lock );
160         msg_Err( p_input, "cannot init stream" );
161         return( -1 );
162     }
163     if( input_AddProgram( p_input, 0, 0) == NULL )
164     {
165         vlc_mutex_unlock( &p_input->stream.stream_lock );
166         msg_Err( p_input, "cannot add program" );
167         return( -1 );
168     }
169     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
170     p_input->stream.i_mux_rate = 0 ; /* FIXME */
171     vlc_mutex_unlock( &p_input->stream.stream_lock );
172
173     for( i_stream = 0; i_stream < p_demux->i_streams; i_stream ++ )
174     {
175         asf_stream_t    *p_stream;
176         asf_object_stream_properties_t *p_sp;
177         
178         p_sp = ASF_FindObject( p_demux->root.p_hdr,
179                                &asf_object_stream_properties_guid,
180                                i_stream );
181
182         p_stream = 
183             p_demux->stream[p_sp->i_stream_number] = 
184                 malloc( sizeof( asf_stream_t ) );
185         memset( p_stream, 0, sizeof( asf_stream_t ) );
186         p_stream->p_sp = p_sp;
187
188         vlc_mutex_lock( &p_input->stream.stream_lock );
189         p_stream->p_es = 
190             input_AddES( p_input,
191                          p_input->stream.p_selected_program,
192                          p_sp->i_stream_number,
193                          0 );
194
195         vlc_mutex_unlock( &p_input->stream.stream_lock );
196         if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) )
197         {
198             int i_codec;
199             if( p_sp->p_type_specific_data )
200             {
201                 i_codec = GetWLE( p_sp->p_type_specific_data );
202             }
203             else
204             {
205                 i_codec = -1;
206             }
207
208             p_stream->i_cat = AUDIO_ES;
209             msg_Dbg( p_input,
210                     "adding new audio stream(codec:0x%x,ID:%d)", 
211                     i_codec,
212                     p_sp->i_stream_number );
213             switch( i_codec )
214             {
215                 case( 0x01 ):
216                     p_stream->p_es->i_fourcc = 
217                         VLC_FOURCC( 'a', 'r', 'a', 'w' );
218                     break;
219                 case( 0x50 ):
220                 case( 0x55 ):
221                     p_stream->p_es->i_fourcc = 
222                         VLC_FOURCC( 'm','p','g','a' );
223                     break;
224                 case( 0x2000 ):
225                     p_stream->p_es->i_fourcc = 
226                         VLC_FOURCC( 'a','5','2',' ' );
227                     break;
228                 case( 0x160 ):
229                     p_stream->p_es->i_fourcc = 
230                         VLC_FOURCC( 'w','m','a','1' );
231                     break;
232                 case( 0x161 ):
233                     p_stream->p_es->i_fourcc = 
234                         VLC_FOURCC( 'w','m','a','2' );
235                     break;
236                 default:
237                     p_stream->p_es->i_fourcc = 
238                         VLC_FOURCC( 'm','s',(i_codec >> 8)&0xff,i_codec&0xff );
239             }
240             if( p_sp->i_type_specific_data_length > 0 )
241             {
242                 WAVEFORMATEX    *p_wf;
243                 int             i_size;
244                 uint8_t         *p_data;
245                 
246                 i_size = p_sp->i_type_specific_data_length;
247
248                 p_wf = malloc( i_size );
249                 p_stream->p_es->p_demux_data = (void*)p_wf;
250                 p_data = p_sp->p_type_specific_data;
251
252                 p_wf->wFormatTag        = GetWLE( p_data );
253                 p_wf->nChannels         = GetWLE( p_data + 2 );
254                 p_wf->nSamplesPerSec    = GetDWLE( p_data + 4 );
255                 p_wf->nAvgBytesPerSec   = GetDWLE( p_data + 8 );
256                 p_wf->nBlockAlign       = GetWLE( p_data + 12 );
257                 p_wf->wBitsPerSample    = GetWLE( p_data + 14 );
258                 p_wf->cbSize            = __MAX( 0,
259                                               i_size - sizeof( WAVEFORMATEX ));
260                 if( i_size > sizeof( WAVEFORMATEX ) )
261                 {
262                     memcpy( (uint8_t*)p_stream->p_es->p_demux_data + sizeof( WAVEFORMATEX ),
263                             p_data + sizeof( WAVEFORMATEX ),
264                             i_size - sizeof( WAVEFORMATEX ) );
265                 }
266             }
267             
268         }
269         else
270         if( CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) )
271         {
272             p_stream->i_cat = VIDEO_ES;
273             msg_Dbg( p_input,
274                     "adding new video stream(ID:%d)", 
275                     p_sp->i_stream_number );
276             if( p_sp->p_type_specific_data )
277             {
278                 p_stream->p_es->i_fourcc =
279                     VLC_FOURCC( p_sp->p_type_specific_data[27],
280                                 p_sp->p_type_specific_data[28],
281                                 p_sp->p_type_specific_data[29],
282                                 p_sp->p_type_specific_data[30] );
283             }
284             else
285             {
286                 p_stream->p_es->i_fourcc = 
287                     VLC_FOURCC( 'u','n','d','f' );
288             }
289             if( p_sp->i_type_specific_data_length > 11 )
290             {
291                 BITMAPINFOHEADER *p_bih;
292                 int         i_size;
293                 uint8_t     *p_data;
294
295                 i_size = p_sp->i_type_specific_data_length - 11;
296                 
297                 p_bih = malloc( i_size );
298                 p_stream->p_es->p_demux_data = (void*)p_bih;
299                 p_data = p_sp->p_type_specific_data + 11;
300                 
301                 p_bih->biSize       = GetDWLE( p_data );
302                 p_bih->biWidth      = GetDWLE( p_data + 4 );
303                 p_bih->biHeight     = GetDWLE( p_data + 8 );
304                 p_bih->biPlanes     = GetDWLE( p_data + 12 );
305                 p_bih->biBitCount   = GetDWLE( p_data + 14 );
306                 p_bih->biCompression= GetDWLE( p_data + 16 );
307                 p_bih->biSizeImage  = GetDWLE( p_data + 20 );
308                 p_bih->biXPelsPerMeter = GetDWLE( p_data + 24 );
309                 p_bih->biYPelsPerMeter = GetDWLE( p_data + 28 );
310                 p_bih->biClrUsed       = GetDWLE( p_data + 32 );
311                 p_bih->biClrImportant  = GetDWLE( p_data + 36 );
312
313                 if( i_size > sizeof( BITMAPINFOHEADER ) )
314                 {
315                     memcpy( (uint8_t*)p_stream->p_es->p_demux_data + sizeof( BITMAPINFOHEADER ),
316                             p_data + sizeof( BITMAPINFOHEADER ),
317                             i_size - sizeof( BITMAPINFOHEADER ) );
318                 }
319             }
320
321         }
322         else
323         {
324             p_stream->i_cat = UNKNOWN_ES;
325             msg_Dbg( p_input, 
326                     "ignoring unknown stream(ID:%d)", 
327                     p_sp->i_stream_number );
328             p_stream->p_es->i_fourcc = VLC_FOURCC( 'u','n','d','f' );
329         }
330         p_stream->p_es->i_cat = p_stream->i_cat;
331
332         vlc_mutex_lock( &p_input->stream.stream_lock );
333         if( p_stream->p_es->i_fourcc != VLC_FOURCC( 'u','n','d','f' ) )
334         {
335             input_SelectES( p_input, p_stream->p_es );
336         }
337         vlc_mutex_unlock( &p_input->stream.stream_lock );
338     }
339     
340
341     p_demux->i_data_begin = p_demux->root.p_data->i_object_pos + 50;
342     if( p_demux->root.p_data->i_object_size != 0 )
343     { // local file
344         p_demux->i_data_end = p_demux->root.p_data->i_object_pos +
345                                     p_demux->root.p_data->i_object_size;
346     }
347     else
348     { // live/broacast
349         p_demux->i_data_end = -1;
350     }
351
352
353     // go to first packet
354     ASF_SeekAbsolute( p_input, p_demux->i_data_begin );
355
356     vlc_mutex_lock( &p_input->stream.stream_lock );
357     /* try to calculate movie time */
358     if( p_demux->p_fp->i_data_packets_count > 0 )
359     {
360         int64_t i_count;
361         mtime_t i_length;
362
363         /* real number of packets */
364         i_count = ( p_input->stream.p_selected_area->i_size - 
365                        p_demux->i_data_begin ) / 
366                             p_demux->p_fp->i_min_data_packet_size;
367         /* calculate the time duration in s */
368         i_length = (mtime_t)p_demux->p_fp->i_play_duration / 10 * 
369                    (mtime_t)i_count / 
370                    (mtime_t)p_demux->p_fp->i_data_packets_count /
371                    (mtime_t)1000000;
372         if( i_length > 0 )
373         {
374             p_input->stream.i_mux_rate =
375                 p_input->stream.p_selected_area->i_size / 50 / i_length;
376         }
377         else
378         {
379             p_input->stream.i_mux_rate = 0;
380         }
381
382     }
383     else
384     {
385         /* cannot known */
386         p_input->stream.i_mux_rate = 0;
387     }
388
389
390
391     p_input->stream.p_selected_program->b_is_ok = 1;
392     vlc_mutex_unlock( &p_input->stream.stream_lock );
393
394     
395     return( 0 );
396 }
397
398 /*****************************************************************************
399  * Demux: read packet and send them to decoders 
400  *****************************************************************************/
401 #define GETVALUE2b( bits, var, def ) \
402     switch( (bits)&0x03 ) \
403     { \
404         case 1: var = p_peek[i_skip]; i_skip++; break; \
405         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
406         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
407         case 0: \
408         default: var = def; break;\
409     }
410
411 static int Demux( input_thread_t *p_input )
412 {
413     demux_sys_t *p_demux = p_input->p_demux_data;
414     int i;
415
416     /* catch seek from user */
417     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
418     {
419         off_t i_offset;
420         msleep( DEFAULT_PTS_DELAY );
421         i_offset = ASF_TellAbsolute( p_input ) - p_demux->i_data_begin;
422         
423         if( i_offset  < 0 )
424         {
425             i_offset = 0;
426         }
427         /* XXX work only when i_min_data_packet_size == i_max_data_packet_size */
428         i_offset += p_demux->p_fp->i_min_data_packet_size - 
429                         i_offset % p_demux->p_fp->i_min_data_packet_size;
430         ASF_SeekAbsolute( p_input, p_demux->i_data_begin + i_offset );
431
432         p_demux->i_time = 0;
433         for( i = 0; i < 128 ; i++ )
434         {
435 #define p_stream p_demux->stream[i]
436             if( p_stream )
437             {
438                 p_stream->i_time = 0;
439             }
440 #undef p_stream
441         }
442         p_demux->i_first_pts = -1;
443     }
444
445
446     for( i = 0; i < 10; i++ ) // parse 10 packets
447     {
448         int     i_data_packet_min = p_demux->p_fp->i_min_data_packet_size;
449         uint8_t *p_peek;
450         int     i_skip;
451         
452         int     i_packet_size_left;
453         int     i_packet_flags;
454         int     i_packet_property;
455
456         int     b_packet_multiple_payload;
457         int     i_packet_length;
458         int     i_packet_sequence;
459         int     i_packet_padding_length;
460         
461         uint32_t    i_packet_send_time;
462         uint16_t    i_packet_duration;
463         int         i_payload;
464         int         i_payload_count;
465         int         i_payload_length_type;
466
467         
468         if( input_Peek( p_input, &p_peek, i_data_packet_min ) < i_data_packet_min )
469         {
470             // EOF ?
471             msg_Err( p_input, "cannot peek while getting new packet, EOF ?" );
472             return( 0 );
473         }
474         i_skip = 0;
475         
476         /* *** parse error correction if present *** */
477         if( p_peek[0]&0x80 )
478         {
479             int i_error_correction_length_type;
480             int i_error_correction_data_length;
481             int i_opaque_data_present;
482
483             i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
484             i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
485             i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
486             i_skip += 1; // skip error correction flags
487         
488             if( i_error_correction_length_type != 0x00 ||
489                 i_opaque_data_present != 0 ||
490                 i_error_correction_data_length != 0x02 )
491             {
492                 goto loop_error_recovery;
493             }
494
495             i_skip += i_error_correction_data_length;
496         }
497         else
498         {
499             msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
500         }
501
502         i_packet_flags = p_peek[i_skip]; i_skip++;
503         i_packet_property = p_peek[i_skip]; i_skip++;
504         
505         b_packet_multiple_payload = i_packet_flags&0x01;
506
507         /* read some value */
508         GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
509         GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
510         GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
511
512         i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
513         i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
514     
515 //        i_packet_size_left = i_packet_length;   // XXX données reellement lu
516         /* FIXME I have to do that for some file, I don't known why */
517         i_packet_size_left = i_data_packet_min;
518         
519         if( b_packet_multiple_payload )
520         {
521             i_payload_count = p_peek[i_skip] & 0x3f;
522             i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
523             i_skip++;
524         }
525         else
526         {
527             i_payload_count = 1;
528             i_payload_length_type = 0x02; // unused
529         }
530
531         for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
532         {
533             asf_stream_t   *p_stream;
534
535             int i_stream_number;
536             int i_media_object_number;
537             int i_media_object_offset;
538             int i_replicated_data_length;
539             int i_payload_data_length;
540             int i_payload_data_pos;
541             int i_sub_payload_data_length;
542             int i_tmp;
543
544             mtime_t i_pts;
545             mtime_t i_pts_delta;
546
547             i_stream_number = p_peek[i_skip] & 0x7f; 
548             i_skip++;
549             
550             GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
551             GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
552             GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
553             
554             if( i_replicated_data_length > 1 ) // should be at least 8 bytes
555             {
556                 i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
557                 i_skip += i_replicated_data_length;
558                 i_pts_delta = 0;
559
560                 i_media_object_offset = i_tmp;
561             }
562             else if( i_replicated_data_length == 1 )
563             {
564
565                 msg_Dbg( p_input, "found compressed payload" );
566
567                 i_pts = (mtime_t)i_tmp * 1000;
568                 i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
569
570                 i_media_object_offset = 0;
571             }
572             else
573             {
574                 i_pts = (mtime_t)i_packet_send_time * 1000;
575                 i_pts_delta = 0;
576
577                 i_media_object_offset = i_tmp;
578             }
579
580             i_pts = __MAX( i_pts - p_demux->p_fp->i_preroll * 1000, 0 );
581             if( b_packet_multiple_payload )
582             {
583                 GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
584             }
585             else
586             {
587                 i_payload_data_length = i_packet_length - 
588                                             i_packet_padding_length - i_skip;
589             }
590             
591 #if 0
592              msg_Dbg( p_input, 
593                       "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
594                       i_payload + 1, 
595                       i_payload_count,
596                       i_stream_number,
597                       i_media_object_number, 
598                       i_media_object_offset, 
599                       i_replicated_data_length, 
600                       i_payload_data_length );
601 #endif
602
603             if( !( p_stream = p_demux->stream[i_stream_number] ) )
604             {
605                 msg_Warn( p_input, 
606                           "undeclared stream[Id 0x%x]", i_stream_number );
607                 i_skip += i_payload_data_length;
608                 continue;   // over payload
609             }
610
611             if( !p_stream->p_es || !p_stream->p_es->p_decoder_fifo )
612             {
613                 i_skip += i_payload_data_length;
614                 continue;
615             }
616
617
618             for( i_payload_data_pos = 0; 
619                  i_payload_data_pos < i_payload_data_length && 
620                         i_packet_size_left > 0; 
621                  i_payload_data_pos += i_sub_payload_data_length )
622             {
623                 data_packet_t  *p_data;
624                 int i_read;
625                 // read sub payload length
626                 if( i_replicated_data_length == 1 )
627                 {
628                     i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
629                     i_payload_data_pos++;
630                 }
631                 else
632                 {
633                     i_sub_payload_data_length = i_payload_data_length;
634                 }
635
636                 /* FIXME I don't use i_media_object_number, sould I ? */
637                 if( p_stream->p_pes && i_media_object_offset == 0 )                 {
638                     /* send complete packet to decoder */
639                     if( p_stream->p_pes->i_pes_size > 0 )
640                     {
641                         input_DecodePES( p_stream->p_es->p_decoder_fifo, p_stream->p_pes );
642                         p_stream->p_pes = NULL;
643                     }
644                 }
645
646                 if( !p_stream->p_pes )  // add a new PES
647                 {
648                     p_stream->i_time = 
649                         ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
650
651                     if( p_demux->i_first_pts == -1 )
652                     {
653                         p_demux->i_first_pts = p_stream->i_time;
654                     }
655                     p_stream->i_time -= p_demux->i_first_pts;
656
657                     p_stream->p_pes = input_NewPES( p_input->p_method_data );
658                     p_stream->p_pes->i_dts = 
659                         p_stream->p_pes->i_pts = 
660                             input_ClockGetTS( p_input, 
661                                               p_input->stream.p_selected_program,
662                                               ( p_stream->i_time+DEFAULT_PTS_DELAY) * 9 /100 );
663
664                     p_stream->p_pes->p_next = NULL;
665                     p_stream->p_pes->i_nb_data = 0;
666                     p_stream->p_pes->i_pes_size = 0;
667                 }
668
669                 i_read = i_sub_payload_data_length + i_skip;
670                 if( input_SplitBuffer( p_input, &p_data, i_read ) < i_read )
671                 {
672                     msg_Err( p_input, "cannot read data" );
673                     return( 0 );
674                 }
675                 p_data->p_payload_start += i_skip;
676                 i_packet_size_left -= i_read;
677
678                 
679                 if( !p_stream->p_pes->p_first )
680                 {
681                     p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
682                 }
683                 else
684                 {
685                     p_stream->p_pes->p_last->p_next = p_data;
686                     p_stream->p_pes->p_last = p_data;
687                 }
688                 p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
689                 p_stream->p_pes->i_nb_data++;
690
691                 i_skip = 0;
692                 if( i_packet_size_left > 0 )
693                 {
694                     if( input_Peek( p_input, &p_peek, i_packet_size_left ) < i_packet_size_left )
695                     {
696                         // EOF ?
697                         msg_Warn( p_input, "cannot peek, EOF ?" );
698                         return( 0 );
699                     }
700                 }
701             }
702         }
703         
704         if( i_packet_size_left > 0 )
705         {
706             if( !ASF_SkipBytes( p_input, i_packet_size_left ) )
707             {
708                 msg_Warn( p_input, "cannot skip data, EOF ?" );
709                 return( 0 );
710             }
711         }
712
713         continue;
714
715 loop_error_recovery:
716         msg_Warn( p_input, "unsupported packet header" );
717         if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
718         {
719             msg_Err( p_input, "unsupported packet header, fatal error" );
720             return( -1 );
721         }
722         ASF_SkipBytes( p_input, i_data_packet_min );
723
724     }   // loop over packet
725     p_demux->i_time = -1;
726     for( i = 0; i < 128 ; i++ )
727     {
728 #define p_stream p_demux->stream[i]
729         if( p_stream && p_stream->p_es && p_stream->p_es->p_decoder_fifo )
730         {
731             if( p_demux->i_time < 0 )
732             {
733                 p_demux->i_time = p_stream->i_time;
734             }
735             else
736             {
737                 p_demux->i_time = __MIN( p_demux->i_time, p_stream->i_time );
738             }
739         }
740 #undef p_stream
741     }
742
743     if( p_demux->i_time >= 0 )
744     {
745         /* update pcr XXX in mpeg scale so in 90000 unit/s */
746         p_demux->i_pcr =( __MAX( p_demux->i_time /*- DEFAULT_PTS_DELAY*/, 0 ) ) * 9 / 100;
747         
748         /* first wait for the good time to read next packets */
749         input_ClockManageRef( p_input,
750                               p_input->stream.p_selected_program,
751                               p_demux->i_pcr );
752     }
753
754    
755     return( 1 );
756 }
757
758 /*****************************************************************************
759  * MP4End: frees unused data
760  *****************************************************************************/
761 static void Deactivate( vlc_object_t * p_this )
762 {   
763 #define FREE( p ) \
764     if( p ) { free( p ); } 
765    
766     input_thread_t *  p_input = (input_thread_t *)p_this;
767     demux_sys_t *p_demux = p_input->p_demux_data;
768     int i_stream;
769     
770     msg_Dbg( p_input, "Freeing all memory" );
771     ASF_FreeObjectRoot( p_input, &p_demux->root );
772     for( i_stream = 0; i_stream < 128; i_stream++ )
773     {
774 #define p_stream p_demux->stream[i_stream]
775         if( p_stream )
776         {
777             if( p_stream->p_pes )
778             {
779                 input_DeletePES( p_input->p_method_data, p_stream->p_pes );
780             }
781             free( p_stream );
782         }
783 #undef p_stream
784     }
785     
786 #undef FREE
787 }
788