]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
94f98ab19afbc357627e4f5c7d0a38f9b1d72f72
[vlc] / modules / demux / asf / asf.c
1 /*****************************************************************************
2  * asf.c : ASFv01 file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2003 VideoLAN
5  * $Id: asf.c,v 1.42 2003/11/16 22:54:12 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "codecs.h"                        /* BITMAPINFOHEADER, WAVEFORMATEX */
33 #include "libasf.h"
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  Open  ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
40
41 vlc_module_begin();
42     set_description( _("ASF v1.0 demuxer") );
43     set_capability( "demux", 200 );
44     set_callbacks( Open, Close );
45     add_shortcut( "asf" );
46 vlc_module_end();
47
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  Demux   ( input_thread_t * );
53
54 typedef struct asf_stream_s
55 {
56     int i_cat;
57
58     es_out_id_t     *p_es;
59
60     asf_object_stream_properties_t *p_sp;
61
62     mtime_t i_time;
63
64     pes_packet_t    *p_pes;     /* used to keep uncomplete frames */
65
66 } asf_stream_t;
67
68 struct demux_sys_t
69 {
70     mtime_t             i_time;     /* µs */
71     mtime_t             i_length;   /* length of file file */
72
73     asf_object_root_t            *p_root;
74     asf_object_file_properties_t *p_fp;
75
76     unsigned int        i_streams;
77     asf_stream_t        *stream[128];
78
79     int64_t             i_data_begin;
80     int64_t             i_data_end;
81 };
82
83 static mtime_t  GetMoviePTS( demux_sys_t * );
84 static int      DemuxPacket( input_thread_t *, vlc_bool_t b_play_audio );
85
86 /*****************************************************************************
87  * Open: check file and initializes ASF structures
88  *****************************************************************************/
89 static int Open( vlc_object_t * p_this )
90 {
91     input_thread_t  *p_input = (input_thread_t *)p_this;
92     uint8_t         *p_peek;
93
94     guid_t          guid;
95
96     demux_sys_t     *p_sys;
97     unsigned int    i_stream, i;
98     asf_object_content_description_t *p_cd;
99
100     vlc_bool_t      b_seekable;
101
102     input_info_category_t *p_cat;
103
104     /* a little test to see if it could be a asf stream */
105     if( input_Peek( p_input, &p_peek, 16 ) < 16 )
106     {
107         msg_Warn( p_input, "ASF plugin discarded (cannot peek)" );
108         return VLC_EGENERIC;
109     }
110     ASF_GetGUID( &guid, p_peek );
111     if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) )
112     {
113         msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
114         return VLC_EGENERIC;
115     }
116
117     /* Set p_input field */
118     p_input->pf_demux         = Demux;
119     p_input->pf_demux_control = demux_vaControlDefault;
120     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
121     memset( p_sys, 0, sizeof( demux_sys_t ) );
122     p_sys->i_time = -1;
123     p_sys->i_length = 0;
124
125     /* Now load all object ( except raw data ) */
126     stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
127     if( (p_sys->p_root = ASF_ReadObjectRoot( p_input->s, b_seekable )) == NULL )
128     {
129         msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
130         free( p_sys );
131         return VLC_EGENERIC;
132     }
133     p_sys->p_fp = p_sys->p_root->p_fp;
134
135     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
136     {
137         msg_Warn( p_input,
138                   "ASF plugin discarded (invalid file_properties object)" );
139         goto error;
140     }
141
142     p_sys->i_streams = ASF_CountObject( p_sys->p_root->p_hdr,
143                                           &asf_object_stream_properties_guid );
144     if( !p_sys->i_streams )
145     {
146         msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
147         goto error;
148     }
149
150     msg_Dbg( p_input, "found %d streams", p_sys->i_streams );
151
152     vlc_mutex_lock( &p_input->stream.stream_lock );
153     if( input_InitStream( p_input, 0 ) == -1)
154     {
155         vlc_mutex_unlock( &p_input->stream.stream_lock );
156         msg_Err( p_input, "cannot init stream" );
157         goto error;
158     }
159     p_input->stream.i_mux_rate = 0 ; /* updated later */
160     vlc_mutex_unlock( &p_input->stream.stream_lock );
161
162     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream ++ )
163     {
164         asf_stream_t    *p_stream;
165         asf_object_stream_properties_t *p_sp;
166
167         p_sp = ASF_FindObject( p_sys->p_root->p_hdr,
168                                &asf_object_stream_properties_guid,
169                                i_stream );
170
171         p_stream =
172             p_sys->stream[p_sp->i_stream_number] =
173                 malloc( sizeof( asf_stream_t ) );
174         memset( p_stream, 0, sizeof( asf_stream_t ) );
175
176         p_stream->i_time = -1;
177         p_stream->p_sp = p_sp;
178         p_stream->p_es = NULL;
179
180         if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) &&
181             p_sp->i_type_specific_data_length >= sizeof( WAVEFORMATEX ) - 2 )
182         {
183             es_format_t  fmt;
184             uint8_t      *p_data = p_sp->p_type_specific_data;
185
186             es_format_Init( &fmt, AUDIO_ES, 0 );
187             wf_tag_to_fourcc( GetWLE( &p_data[0] ), &fmt.i_codec, NULL );
188             fmt.audio.i_channels        = GetWLE(  &p_data[2] );
189             fmt.audio.i_rate      = GetDWLE( &p_data[4] );
190             fmt.i_bitrate         = GetDWLE( &p_data[8] ) * 8;
191             fmt.audio.i_blockalign      = GetWLE(  &p_data[12] );
192             fmt.audio.i_bitspersample   = GetWLE(  &p_data[14] );
193
194             if( p_sp->i_type_specific_data_length > sizeof( WAVEFORMATEX ) )
195             {
196                 fmt.i_extra_type = ES_EXTRA_TYPE_WAVEFORMATEX;
197                 fmt.i_extra = __MIN( GetWLE( &p_data[16] ),
198                                      p_sp->i_type_specific_data_length - sizeof( WAVEFORMATEX ) );
199                 fmt.p_extra = malloc( fmt.i_extra );
200                 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )], fmt.i_extra );
201             }
202
203             p_stream->i_cat = AUDIO_ES;
204             p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
205
206             msg_Dbg( p_input,
207                     "added new audio stream(codec:0x%x,ID:%d)",
208                     GetWLE( p_data ), p_sp->i_stream_number );
209         }
210         else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) &&
211                  p_sp->i_type_specific_data_length >= 11 + sizeof( BITMAPINFOHEADER ) )
212         {
213             es_format_t  fmt;
214             uint8_t      *p_data = &p_sp->p_type_specific_data[11];
215
216             es_format_Init( &fmt, VIDEO_ES,
217                             VLC_FOURCC( p_data[16], p_data[17], p_data[18], p_data[19] ) );
218             fmt.video.i_width = GetDWLE( p_data + 4 );
219             fmt.video.i_height= GetDWLE( p_data + 8 );
220
221             if( p_sp->i_type_specific_data_length > 11 + sizeof( BITMAPINFOHEADER ) )
222             {
223                 fmt.i_extra_type = ES_EXTRA_TYPE_BITMAPINFOHEADER;
224                 fmt.i_extra = __MIN( GetDWLE( p_data ),
225                                      p_sp->i_type_specific_data_length - 11 - sizeof( BITMAPINFOHEADER ) );
226                 fmt.p_extra = malloc( fmt.i_extra );
227                 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )], fmt.i_extra );
228             }
229
230             p_stream->i_cat = VIDEO_ES;
231             p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
232
233             msg_Dbg( p_input, "added new video stream(ID:%d)",
234                      p_sp->i_stream_number );
235         }
236         else
237         {
238             p_stream->i_cat = UNKNOWN_ES;
239             msg_Dbg( p_input, "ignoring unknown stream(ID:%d)",
240                      p_sp->i_stream_number );
241         }
242     }
243
244     p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
245     if( p_sys->p_root->p_data->i_object_size != 0 )
246     { /* local file */
247         p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
248                                     p_sys->p_root->p_data->i_object_size;
249     }
250     else
251     { /* live/broacast */
252         p_sys->i_data_end = -1;
253     }
254
255
256     /* go to first packet */
257     stream_Seek( p_input->s, p_sys->i_data_begin );
258
259     /* try to calculate movie time */
260     if( p_sys->p_fp->i_data_packets_count > 0 )
261     {
262         int64_t i_count;
263         int64_t i_size = stream_Size( p_input->s );
264
265         if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
266         {
267             i_size = p_sys->i_data_end;
268         }
269
270         /* real number of packets */
271         i_count = ( i_size - p_sys->i_data_begin ) /
272                   p_sys->p_fp->i_min_data_packet_size;
273
274         /* calculate the time duration in micro-s */
275         p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
276                    (mtime_t)i_count /
277                    (mtime_t)p_sys->p_fp->i_data_packets_count;
278
279         if( p_sys->i_length > 0 )
280         {
281             p_input->stream.i_mux_rate =
282                 i_size / 50 * (int64_t)1000000 / p_sys->i_length;
283         }
284     }
285
286     /* We add all info about this stream */
287     p_cat = input_InfoCategory( p_input, "Asf" );
288     if( p_sys->i_length > 0 )
289     {
290         int64_t i_second = p_sys->i_length / (int64_t)1000000;
291
292         input_AddInfo( p_cat, _("Length"), "%d:%d:%d",
293                        (int)(i_second / 36000),
294                        (int)(( i_second / 60 ) % 60),
295                        (int)(i_second % 60) );
296     }
297     input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_streams );
298
299     if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
300                                  &asf_object_content_description_guid, 0 ) ) )
301     {
302         if( *p_cd->psz_title )
303             input_AddInfo( p_cat, _("Title"), p_cd->psz_title );
304         if( p_cd->psz_author )
305             input_AddInfo( p_cat, _("Author"), p_cd->psz_author );
306         if( p_cd->psz_copyright )
307             input_AddInfo( p_cat, _("Copyright"), p_cd->psz_copyright );
308         if( *p_cd->psz_description )
309             input_AddInfo( p_cat, _("Description"), p_cd->psz_description );
310         if( *p_cd->psz_rating )
311             input_AddInfo( p_cat, _("Rating"), p_cd->psz_rating );
312     }
313
314     /* FIXME to port to new way */
315     for( i_stream = 0, i = 0; i < 128; i++ )
316     {
317         asf_object_codec_list_t *p_cl =
318             ASF_FindObject( p_sys->p_root->p_hdr,
319                             &asf_object_codec_list_guid, 0 );
320
321         if( p_sys->stream[i] )
322         {
323             char psz_cat[sizeof(_("Stream "))+10];
324             sprintf( psz_cat, _("Stream %d"), i_stream );
325             p_cat = input_InfoCategory( p_input, psz_cat);
326
327             if( p_cl && i_stream < p_cl->i_codec_entries_count )
328             {
329                 input_AddInfo( p_cat, _("Codec name"),
330                                p_cl->codec[i_stream].psz_name );
331                 input_AddInfo( p_cat, _("Codec description"),
332                                p_cl->codec[i_stream].psz_description );
333             }
334             i_stream++;
335         }
336     }
337
338     return VLC_SUCCESS;
339
340 error:
341     ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
342     free( p_sys );
343     return VLC_EGENERIC;
344 }
345
346
347 /*****************************************************************************
348  * Demux: read packet and send them to decoders
349  *****************************************************************************/
350 static int Demux( input_thread_t *p_input )
351 {
352     demux_sys_t *p_sys = p_input->p_demux_data;
353     vlc_bool_t b_play_audio;
354     int i;
355
356     /* catch seek from user */
357     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
358     {
359         int64_t i_offset;
360
361         msleep( p_input->i_pts_delay );
362
363         i_offset = stream_Tell( p_input->s ) - p_sys->i_data_begin;
364         if( i_offset  < 0 )
365         {
366             i_offset = 0;
367         }
368         if( i_offset % p_sys->p_fp->i_min_data_packet_size > 0 )
369         {
370             i_offset -= i_offset % p_sys->p_fp->i_min_data_packet_size;
371         }
372
373         if( stream_Seek( p_input->s, i_offset + p_sys->i_data_begin ) )
374         {
375             msg_Warn( p_input, "cannot resynch after seek (EOF?)" );
376             return -1;
377         }
378
379         p_sys->i_time = -1;
380         for( i = 0; i < 128 ; i++ )
381         {
382 #define p_stream p_sys->stream[i]
383             if( p_stream )
384             {
385                 p_stream->i_time = -1;
386             }
387 #undef p_stream
388         }
389     }
390
391     /* Check if we need to send the audio data to decoder */
392     b_play_audio = !p_input->stream.control.b_mute;
393
394     for( ;; )
395     {
396         mtime_t i_length;
397         mtime_t i_time_begin = GetMoviePTS( p_sys );
398         int i_result;
399
400         if( p_input->b_die )
401         {
402             break;
403         }
404
405         if( ( i_result = DemuxPacket( p_input, b_play_audio ) ) <= 0 )
406         {
407             return i_result;
408         }
409         if( i_time_begin == -1 )
410         {
411             i_time_begin = GetMoviePTS( p_sys );
412         }
413         else
414         {
415             i_length = GetMoviePTS( p_sys ) - i_time_begin;
416             if( i_length < 0 || i_length >= 40 * 1000 )
417             {
418                 break;
419             }
420         }
421     }
422
423     p_sys->i_time = GetMoviePTS( p_sys );
424     if( p_sys->i_time >= 0 )
425     {
426         input_ClockManageRef( p_input,
427                               p_input->stream.p_selected_program,
428                               p_sys->i_time * 9 / 100 );
429     }
430
431     return( 1 );
432 }
433
434 /*****************************************************************************
435  * Close: frees unused data
436  *****************************************************************************/
437 static void Close( vlc_object_t * p_this )
438 {
439     input_thread_t *p_input = (input_thread_t *)p_this;
440     demux_sys_t    *p_sys = p_input->p_demux_data;
441     int i_stream;
442
443     msg_Dbg( p_input, "Freeing all memory" );
444
445     ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
446     for( i_stream = 0; i_stream < 128; i_stream++ )
447     {
448 #define p_stream p_sys->stream[i_stream]
449         if( p_stream )
450         {
451             if( p_stream->p_pes )
452             {
453                 input_DeletePES( p_input->p_method_data, p_stream->p_pes );
454             }
455             free( p_stream );
456         }
457 #undef p_stream
458     }
459     free( p_sys );
460 }
461
462
463 /*****************************************************************************
464  *
465  *****************************************************************************/
466 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
467 {
468     mtime_t i_time;
469     int     i_stream;
470
471     i_time = -1;
472     for( i_stream = 0; i_stream < 128 ; i_stream++ )
473     {
474 #define p_stream p_sys->stream[i_stream]
475         if( p_stream && p_stream->p_es && p_stream->i_time > 0)
476         {
477             if( i_time < 0 )
478             {
479                 i_time = p_stream->i_time;
480             }
481             else
482             {
483                 i_time = __MIN( i_time, p_stream->i_time );
484             }
485         }
486 #undef p_stream
487     }
488
489     return( i_time );
490 }
491
492 #define GETVALUE2b( bits, var, def ) \
493     switch( (bits)&0x03 ) \
494     { \
495         case 1: var = p_peek[i_skip]; i_skip++; break; \
496         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
497         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
498         case 0: \
499         default: var = def; break;\
500     }
501
502 static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
503 {
504     demux_sys_t *p_sys = p_input->p_demux_data;
505     int     i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
506     uint8_t *p_peek;
507     int     i_skip;
508
509     int     i_packet_size_left;
510     int     i_packet_flags;
511     int     i_packet_property;
512
513     int     b_packet_multiple_payload;
514     int     i_packet_length;
515     int     i_packet_sequence;
516     int     i_packet_padding_length;
517
518     uint32_t    i_packet_send_time;
519     uint16_t    i_packet_duration;
520     int         i_payload;
521     int         i_payload_count;
522     int         i_payload_length_type;
523
524
525     if( stream_Peek( p_input->s, &p_peek,i_data_packet_min)<i_data_packet_min )
526     {
527         // EOF ?
528         msg_Warn( p_input, "cannot peek while getting new packet, EOF ?" );
529         return( 0 );
530     }
531     i_skip = 0;
532
533     /* *** parse error correction if present *** */
534     if( p_peek[0]&0x80 )
535     {
536         unsigned int i_error_correction_length_type;
537         unsigned int i_error_correction_data_length;
538         unsigned int i_opaque_data_present;
539
540         i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
541         i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
542         i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
543         i_skip += 1; // skip error correction flags
544
545         if( i_error_correction_length_type != 0x00 ||
546             i_opaque_data_present != 0 ||
547             i_error_correction_data_length != 0x02 )
548         {
549             goto loop_error_recovery;
550         }
551
552         i_skip += i_error_correction_data_length;
553     }
554     else
555     {
556         msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
557     }
558
559     /* sanity check */
560     if( i_skip + 2 >= i_data_packet_min )
561     {
562         goto loop_error_recovery;
563     }
564
565     i_packet_flags = p_peek[i_skip]; i_skip++;
566     i_packet_property = p_peek[i_skip]; i_skip++;
567
568     b_packet_multiple_payload = i_packet_flags&0x01;
569
570     /* read some value */
571     GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
572     GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
573     GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
574
575     i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
576     i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
577
578 //        i_packet_size_left = i_packet_length;   // XXX données reellement lu
579     /* FIXME I have to do that for some file, I don't known why */
580     i_packet_size_left = i_data_packet_min;
581
582     if( b_packet_multiple_payload )
583     {
584         i_payload_count = p_peek[i_skip] & 0x3f;
585         i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
586         i_skip++;
587     }
588     else
589     {
590         i_payload_count = 1;
591         i_payload_length_type = 0x02; // unused
592     }
593
594     for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
595     {
596         asf_stream_t   *p_stream;
597
598         int i_stream_number;
599         int i_media_object_number;
600         int i_media_object_offset;
601         int i_replicated_data_length;
602         int i_payload_data_length;
603         int i_payload_data_pos;
604         int i_sub_payload_data_length;
605         int i_tmp;
606
607         mtime_t i_pts;
608         mtime_t i_pts_delta;
609
610         if( i_skip >= i_packet_size_left )
611         {
612             /* prevent some segfault with invalid file */
613             break;
614         }
615
616         i_stream_number = p_peek[i_skip] & 0x7f;
617         i_skip++;
618
619         GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
620         GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
621         GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
622
623         if( i_replicated_data_length > 1 ) // should be at least 8 bytes
624         {
625             i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
626             i_skip += i_replicated_data_length;
627             i_pts_delta = 0;
628
629             i_media_object_offset = i_tmp;
630
631             if( i_skip >= i_packet_size_left )
632             {
633                 break;
634             }
635         }
636         else if( i_replicated_data_length == 1 )
637         {
638
639             msg_Dbg( p_input, "found compressed payload" );
640
641             i_pts = (mtime_t)i_tmp * 1000;
642             i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
643
644             i_media_object_offset = 0;
645         }
646         else
647         {
648             i_pts = (mtime_t)i_packet_send_time * 1000;
649             i_pts_delta = 0;
650
651             i_media_object_offset = i_tmp;
652         }
653
654         i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
655         if( b_packet_multiple_payload )
656         {
657             GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
658         }
659         else
660         {
661             i_payload_data_length = i_packet_length -
662                                         i_packet_padding_length - i_skip;
663         }
664
665         if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
666         {
667             break;
668         }
669
670 #if 0
671          msg_Dbg( p_input,
672                   "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
673                   i_payload + 1,
674                   i_payload_count,
675                   i_stream_number,
676                   i_media_object_number,
677                   i_media_object_offset,
678                   i_replicated_data_length,
679                   i_payload_data_length );
680 #endif
681
682         if( !( p_stream = p_sys->stream[i_stream_number] ) )
683         {
684             msg_Warn( p_input,
685                       "undeclared stream[Id 0x%x]", i_stream_number );
686             i_skip += i_payload_data_length;
687             continue;   // over payload
688         }
689
690         if( !p_stream->p_es )
691         {
692             i_skip += i_payload_data_length;
693             continue;
694         }
695
696
697         for( i_payload_data_pos = 0;
698              i_payload_data_pos < i_payload_data_length &&
699                     i_packet_size_left > 0;
700              i_payload_data_pos += i_sub_payload_data_length )
701         {
702             data_packet_t  *p_data;
703             int i_read;
704             // read sub payload length
705             if( i_replicated_data_length == 1 )
706             {
707                 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
708                 i_payload_data_pos++;
709             }
710             else
711             {
712                 i_sub_payload_data_length = i_payload_data_length;
713             }
714
715             /* FIXME I don't use i_media_object_number, sould I ? */
716             if( p_stream->p_pes && i_media_object_offset == 0 )
717             {
718                 /* send complete packet to decoder */
719                 if( p_stream->p_pes->i_pes_size > 0 )
720                 {
721                     es_out_Send( p_input->p_es_out, p_stream->p_es, p_stream->p_pes );
722                     p_stream->p_pes = NULL;
723                 }
724             }
725
726             if( !p_stream->p_pes )  // add a new PES
727             {
728                 p_stream->i_time =
729                     ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
730
731                 p_stream->p_pes = input_NewPES( p_input->p_method_data );
732                 p_stream->p_pes->i_dts =
733                     p_stream->p_pes->i_pts =
734                         input_ClockGetTS( p_input,
735                                           p_input->stream.p_selected_program,
736                                           p_stream->i_time * 9 /100 );
737
738                 //msg_Err( p_input, "stream[0x%2x] pts=%lld", i_stream_number, p_stream->p_pes->i_pts );
739                 p_stream->p_pes->p_next = NULL;
740                 p_stream->p_pes->i_nb_data = 0;
741                 p_stream->p_pes->i_pes_size = 0;
742             }
743
744             i_read = i_sub_payload_data_length + i_skip;
745             if((p_data = stream_DataPacket( p_input->s,i_read,VLC_TRUE))==NULL)
746             {
747                 msg_Warn( p_input, "cannot read data" );
748                 return( 0 );
749             }
750             p_data->p_payload_start += i_skip;
751             i_packet_size_left -= i_read;
752
753
754             if( !p_stream->p_pes->p_first )
755             {
756                 p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
757             }
758             else
759             {
760                 p_stream->p_pes->p_last->p_next = p_data;
761                 p_stream->p_pes->p_last = p_data;
762             }
763             p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
764             p_stream->p_pes->i_nb_data++;
765
766             i_skip = 0;
767             if( i_packet_size_left > 0 )
768             {
769                 if( stream_Peek( p_input->s, &p_peek, i_packet_size_left )
770                                                          < i_packet_size_left )
771                 {
772                     // EOF ?
773                     msg_Warn( p_input, "cannot peek, EOF ?" );
774                     return( 0 );
775                 }
776             }
777         }
778     }
779
780     if( i_packet_size_left > 0 )
781     {
782         if( stream_Read( p_input->s, NULL, i_packet_size_left )
783                                                          < i_packet_size_left )
784         {
785             msg_Warn( p_input, "cannot skip data, EOF ?" );
786             return( 0 );
787         }
788     }
789
790     return( 1 );
791
792 loop_error_recovery:
793     msg_Warn( p_input, "unsupported packet header" );
794     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
795     {
796         msg_Err( p_input, "unsupported packet header, fatal error" );
797         return( -1 );
798     }
799     stream_Read( p_input->s, NULL, i_data_packet_min );
800
801     return( 1 );
802 }
803
804