]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
include/vlc_es.h: defines es_format_t, audio_format_t, video_format_t.
[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.43 2003/11/20 22:10:56 fenrir 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 = __MIN( GetWLE( &p_data[16] ),
197                                      p_sp->i_type_specific_data_length - sizeof( WAVEFORMATEX ) );
198                 fmt.p_extra = malloc( fmt.i_extra );
199                 memcpy( fmt.p_extra, &p_data[sizeof( WAVEFORMATEX )], fmt.i_extra );
200             }
201
202             p_stream->i_cat = AUDIO_ES;
203             p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
204
205             msg_Dbg( p_input,
206                     "added new audio stream(codec:0x%x,ID:%d)",
207                     GetWLE( p_data ), p_sp->i_stream_number );
208         }
209         else if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) &&
210                  p_sp->i_type_specific_data_length >= 11 + sizeof( BITMAPINFOHEADER ) )
211         {
212             es_format_t  fmt;
213             uint8_t      *p_data = &p_sp->p_type_specific_data[11];
214
215             es_format_Init( &fmt, VIDEO_ES,
216                             VLC_FOURCC( p_data[16], p_data[17], p_data[18], p_data[19] ) );
217             fmt.video.i_width = GetDWLE( p_data + 4 );
218             fmt.video.i_height= GetDWLE( p_data + 8 );
219
220             if( p_sp->i_type_specific_data_length > 11 + sizeof( BITMAPINFOHEADER ) )
221             {
222                 fmt.i_extra = __MIN( GetDWLE( p_data ),
223                                      p_sp->i_type_specific_data_length - 11 - sizeof( BITMAPINFOHEADER ) );
224                 fmt.p_extra = malloc( fmt.i_extra );
225                 memcpy( fmt.p_extra, &p_data[sizeof( BITMAPINFOHEADER )], fmt.i_extra );
226             }
227
228             p_stream->i_cat = VIDEO_ES;
229             p_stream->p_es = es_out_Add( p_input->p_es_out, &fmt );
230
231             msg_Dbg( p_input, "added new video stream(ID:%d)",
232                      p_sp->i_stream_number );
233         }
234         else
235         {
236             p_stream->i_cat = UNKNOWN_ES;
237             msg_Dbg( p_input, "ignoring unknown stream(ID:%d)",
238                      p_sp->i_stream_number );
239         }
240     }
241
242     p_sys->i_data_begin = p_sys->p_root->p_data->i_object_pos + 50;
243     if( p_sys->p_root->p_data->i_object_size != 0 )
244     { /* local file */
245         p_sys->i_data_end = p_sys->p_root->p_data->i_object_pos +
246                                     p_sys->p_root->p_data->i_object_size;
247     }
248     else
249     { /* live/broacast */
250         p_sys->i_data_end = -1;
251     }
252
253
254     /* go to first packet */
255     stream_Seek( p_input->s, p_sys->i_data_begin );
256
257     /* try to calculate movie time */
258     if( p_sys->p_fp->i_data_packets_count > 0 )
259     {
260         int64_t i_count;
261         int64_t i_size = stream_Size( p_input->s );
262
263         if( p_sys->i_data_end > 0 && i_size > p_sys->i_data_end )
264         {
265             i_size = p_sys->i_data_end;
266         }
267
268         /* real number of packets */
269         i_count = ( i_size - p_sys->i_data_begin ) /
270                   p_sys->p_fp->i_min_data_packet_size;
271
272         /* calculate the time duration in micro-s */
273         p_sys->i_length = (mtime_t)p_sys->p_fp->i_play_duration / 10 *
274                    (mtime_t)i_count /
275                    (mtime_t)p_sys->p_fp->i_data_packets_count;
276
277         if( p_sys->i_length > 0 )
278         {
279             p_input->stream.i_mux_rate =
280                 i_size / 50 * (int64_t)1000000 / p_sys->i_length;
281         }
282     }
283
284     /* We add all info about this stream */
285     p_cat = input_InfoCategory( p_input, "Asf" );
286     if( p_sys->i_length > 0 )
287     {
288         int64_t i_second = p_sys->i_length / (int64_t)1000000;
289
290         input_AddInfo( p_cat, _("Length"), "%d:%d:%d",
291                        (int)(i_second / 36000),
292                        (int)(( i_second / 60 ) % 60),
293                        (int)(i_second % 60) );
294     }
295     input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_streams );
296
297     if( ( p_cd = ASF_FindObject( p_sys->p_root->p_hdr,
298                                  &asf_object_content_description_guid, 0 ) ) )
299     {
300         if( *p_cd->psz_title )
301             input_AddInfo( p_cat, _("Title"), p_cd->psz_title );
302         if( p_cd->psz_author )
303             input_AddInfo( p_cat, _("Author"), p_cd->psz_author );
304         if( p_cd->psz_copyright )
305             input_AddInfo( p_cat, _("Copyright"), p_cd->psz_copyright );
306         if( *p_cd->psz_description )
307             input_AddInfo( p_cat, _("Description"), p_cd->psz_description );
308         if( *p_cd->psz_rating )
309             input_AddInfo( p_cat, _("Rating"), p_cd->psz_rating );
310     }
311
312     /* FIXME to port to new way */
313     for( i_stream = 0, i = 0; i < 128; i++ )
314     {
315         asf_object_codec_list_t *p_cl =
316             ASF_FindObject( p_sys->p_root->p_hdr,
317                             &asf_object_codec_list_guid, 0 );
318
319         if( p_sys->stream[i] )
320         {
321             char psz_cat[sizeof(_("Stream "))+10];
322             sprintf( psz_cat, _("Stream %d"), i_stream );
323             p_cat = input_InfoCategory( p_input, psz_cat);
324
325             if( p_cl && i_stream < p_cl->i_codec_entries_count )
326             {
327                 input_AddInfo( p_cat, _("Codec name"),
328                                p_cl->codec[i_stream].psz_name );
329                 input_AddInfo( p_cat, _("Codec description"),
330                                p_cl->codec[i_stream].psz_description );
331             }
332             i_stream++;
333         }
334     }
335
336     return VLC_SUCCESS;
337
338 error:
339     ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
340     free( p_sys );
341     return VLC_EGENERIC;
342 }
343
344
345 /*****************************************************************************
346  * Demux: read packet and send them to decoders
347  *****************************************************************************/
348 static int Demux( input_thread_t *p_input )
349 {
350     demux_sys_t *p_sys = p_input->p_demux_data;
351     vlc_bool_t b_play_audio;
352     int i;
353
354     /* catch seek from user */
355     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
356     {
357         int64_t i_offset;
358
359         msleep( p_input->i_pts_delay );
360
361         i_offset = stream_Tell( p_input->s ) - p_sys->i_data_begin;
362         if( i_offset  < 0 )
363         {
364             i_offset = 0;
365         }
366         if( i_offset % p_sys->p_fp->i_min_data_packet_size > 0 )
367         {
368             i_offset -= i_offset % p_sys->p_fp->i_min_data_packet_size;
369         }
370
371         if( stream_Seek( p_input->s, i_offset + p_sys->i_data_begin ) )
372         {
373             msg_Warn( p_input, "cannot resynch after seek (EOF?)" );
374             return -1;
375         }
376
377         p_sys->i_time = -1;
378         for( i = 0; i < 128 ; i++ )
379         {
380 #define p_stream p_sys->stream[i]
381             if( p_stream )
382             {
383                 p_stream->i_time = -1;
384             }
385 #undef p_stream
386         }
387     }
388
389     /* Check if we need to send the audio data to decoder */
390     b_play_audio = !p_input->stream.control.b_mute;
391
392     for( ;; )
393     {
394         mtime_t i_length;
395         mtime_t i_time_begin = GetMoviePTS( p_sys );
396         int i_result;
397
398         if( p_input->b_die )
399         {
400             break;
401         }
402
403         if( ( i_result = DemuxPacket( p_input, b_play_audio ) ) <= 0 )
404         {
405             return i_result;
406         }
407         if( i_time_begin == -1 )
408         {
409             i_time_begin = GetMoviePTS( p_sys );
410         }
411         else
412         {
413             i_length = GetMoviePTS( p_sys ) - i_time_begin;
414             if( i_length < 0 || i_length >= 40 * 1000 )
415             {
416                 break;
417             }
418         }
419     }
420
421     p_sys->i_time = GetMoviePTS( p_sys );
422     if( p_sys->i_time >= 0 )
423     {
424         input_ClockManageRef( p_input,
425                               p_input->stream.p_selected_program,
426                               p_sys->i_time * 9 / 100 );
427     }
428
429     return( 1 );
430 }
431
432 /*****************************************************************************
433  * Close: frees unused data
434  *****************************************************************************/
435 static void Close( vlc_object_t * p_this )
436 {
437     input_thread_t *p_input = (input_thread_t *)p_this;
438     demux_sys_t    *p_sys = p_input->p_demux_data;
439     int i_stream;
440
441     msg_Dbg( p_input, "Freeing all memory" );
442
443     ASF_FreeObjectRoot( p_input->s, p_sys->p_root );
444     for( i_stream = 0; i_stream < 128; i_stream++ )
445     {
446 #define p_stream p_sys->stream[i_stream]
447         if( p_stream )
448         {
449             if( p_stream->p_pes )
450             {
451                 input_DeletePES( p_input->p_method_data, p_stream->p_pes );
452             }
453             free( p_stream );
454         }
455 #undef p_stream
456     }
457     free( p_sys );
458 }
459
460
461 /*****************************************************************************
462  *
463  *****************************************************************************/
464 static mtime_t GetMoviePTS( demux_sys_t *p_sys )
465 {
466     mtime_t i_time;
467     int     i_stream;
468
469     i_time = -1;
470     for( i_stream = 0; i_stream < 128 ; i_stream++ )
471     {
472 #define p_stream p_sys->stream[i_stream]
473         if( p_stream && p_stream->p_es && p_stream->i_time > 0)
474         {
475             if( i_time < 0 )
476             {
477                 i_time = p_stream->i_time;
478             }
479             else
480             {
481                 i_time = __MIN( i_time, p_stream->i_time );
482             }
483         }
484 #undef p_stream
485     }
486
487     return( i_time );
488 }
489
490 #define GETVALUE2b( bits, var, def ) \
491     switch( (bits)&0x03 ) \
492     { \
493         case 1: var = p_peek[i_skip]; i_skip++; break; \
494         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
495         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
496         case 0: \
497         default: var = def; break;\
498     }
499
500 static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
501 {
502     demux_sys_t *p_sys = p_input->p_demux_data;
503     int     i_data_packet_min = p_sys->p_fp->i_min_data_packet_size;
504     uint8_t *p_peek;
505     int     i_skip;
506
507     int     i_packet_size_left;
508     int     i_packet_flags;
509     int     i_packet_property;
510
511     int     b_packet_multiple_payload;
512     int     i_packet_length;
513     int     i_packet_sequence;
514     int     i_packet_padding_length;
515
516     uint32_t    i_packet_send_time;
517     uint16_t    i_packet_duration;
518     int         i_payload;
519     int         i_payload_count;
520     int         i_payload_length_type;
521
522
523     if( stream_Peek( p_input->s, &p_peek,i_data_packet_min)<i_data_packet_min )
524     {
525         // EOF ?
526         msg_Warn( p_input, "cannot peek while getting new packet, EOF ?" );
527         return( 0 );
528     }
529     i_skip = 0;
530
531     /* *** parse error correction if present *** */
532     if( p_peek[0]&0x80 )
533     {
534         unsigned int i_error_correction_length_type;
535         unsigned int i_error_correction_data_length;
536         unsigned int i_opaque_data_present;
537
538         i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
539         i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
540         i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
541         i_skip += 1; // skip error correction flags
542
543         if( i_error_correction_length_type != 0x00 ||
544             i_opaque_data_present != 0 ||
545             i_error_correction_data_length != 0x02 )
546         {
547             goto loop_error_recovery;
548         }
549
550         i_skip += i_error_correction_data_length;
551     }
552     else
553     {
554         msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
555     }
556
557     /* sanity check */
558     if( i_skip + 2 >= i_data_packet_min )
559     {
560         goto loop_error_recovery;
561     }
562
563     i_packet_flags = p_peek[i_skip]; i_skip++;
564     i_packet_property = p_peek[i_skip]; i_skip++;
565
566     b_packet_multiple_payload = i_packet_flags&0x01;
567
568     /* read some value */
569     GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
570     GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
571     GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
572
573     i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
574     i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
575
576 //        i_packet_size_left = i_packet_length;   // XXX données reellement lu
577     /* FIXME I have to do that for some file, I don't known why */
578     i_packet_size_left = i_data_packet_min;
579
580     if( b_packet_multiple_payload )
581     {
582         i_payload_count = p_peek[i_skip] & 0x3f;
583         i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
584         i_skip++;
585     }
586     else
587     {
588         i_payload_count = 1;
589         i_payload_length_type = 0x02; // unused
590     }
591
592     for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
593     {
594         asf_stream_t   *p_stream;
595
596         int i_stream_number;
597         int i_media_object_number;
598         int i_media_object_offset;
599         int i_replicated_data_length;
600         int i_payload_data_length;
601         int i_payload_data_pos;
602         int i_sub_payload_data_length;
603         int i_tmp;
604
605         mtime_t i_pts;
606         mtime_t i_pts_delta;
607
608         if( i_skip >= i_packet_size_left )
609         {
610             /* prevent some segfault with invalid file */
611             break;
612         }
613
614         i_stream_number = p_peek[i_skip] & 0x7f;
615         i_skip++;
616
617         GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
618         GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
619         GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
620
621         if( i_replicated_data_length > 1 ) // should be at least 8 bytes
622         {
623             i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
624             i_skip += i_replicated_data_length;
625             i_pts_delta = 0;
626
627             i_media_object_offset = i_tmp;
628
629             if( i_skip >= i_packet_size_left )
630             {
631                 break;
632             }
633         }
634         else if( i_replicated_data_length == 1 )
635         {
636
637             msg_Dbg( p_input, "found compressed payload" );
638
639             i_pts = (mtime_t)i_tmp * 1000;
640             i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
641
642             i_media_object_offset = 0;
643         }
644         else
645         {
646             i_pts = (mtime_t)i_packet_send_time * 1000;
647             i_pts_delta = 0;
648
649             i_media_object_offset = i_tmp;
650         }
651
652         i_pts = __MAX( i_pts - p_sys->p_fp->i_preroll * 1000, 0 );
653         if( b_packet_multiple_payload )
654         {
655             GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
656         }
657         else
658         {
659             i_payload_data_length = i_packet_length -
660                                         i_packet_padding_length - i_skip;
661         }
662
663         if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
664         {
665             break;
666         }
667
668 #if 0
669          msg_Dbg( p_input,
670                   "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
671                   i_payload + 1,
672                   i_payload_count,
673                   i_stream_number,
674                   i_media_object_number,
675                   i_media_object_offset,
676                   i_replicated_data_length,
677                   i_payload_data_length );
678 #endif
679
680         if( !( p_stream = p_sys->stream[i_stream_number] ) )
681         {
682             msg_Warn( p_input,
683                       "undeclared stream[Id 0x%x]", i_stream_number );
684             i_skip += i_payload_data_length;
685             continue;   // over payload
686         }
687
688         if( !p_stream->p_es )
689         {
690             i_skip += i_payload_data_length;
691             continue;
692         }
693
694
695         for( i_payload_data_pos = 0;
696              i_payload_data_pos < i_payload_data_length &&
697                     i_packet_size_left > 0;
698              i_payload_data_pos += i_sub_payload_data_length )
699         {
700             data_packet_t  *p_data;
701             int i_read;
702             // read sub payload length
703             if( i_replicated_data_length == 1 )
704             {
705                 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
706                 i_payload_data_pos++;
707             }
708             else
709             {
710                 i_sub_payload_data_length = i_payload_data_length;
711             }
712
713             /* FIXME I don't use i_media_object_number, sould I ? */
714             if( p_stream->p_pes && i_media_object_offset == 0 )
715             {
716                 /* send complete packet to decoder */
717                 if( p_stream->p_pes->i_pes_size > 0 )
718                 {
719                     es_out_Send( p_input->p_es_out, p_stream->p_es, p_stream->p_pes );
720                     p_stream->p_pes = NULL;
721                 }
722             }
723
724             if( !p_stream->p_pes )  // add a new PES
725             {
726                 p_stream->i_time =
727                     ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
728
729                 p_stream->p_pes = input_NewPES( p_input->p_method_data );
730                 p_stream->p_pes->i_dts =
731                     p_stream->p_pes->i_pts =
732                         input_ClockGetTS( p_input,
733                                           p_input->stream.p_selected_program,
734                                           p_stream->i_time * 9 /100 );
735
736                 //msg_Err( p_input, "stream[0x%2x] pts=%lld", i_stream_number, p_stream->p_pes->i_pts );
737                 p_stream->p_pes->p_next = NULL;
738                 p_stream->p_pes->i_nb_data = 0;
739                 p_stream->p_pes->i_pes_size = 0;
740             }
741
742             i_read = i_sub_payload_data_length + i_skip;
743             if((p_data = stream_DataPacket( p_input->s,i_read,VLC_TRUE))==NULL)
744             {
745                 msg_Warn( p_input, "cannot read data" );
746                 return( 0 );
747             }
748             p_data->p_payload_start += i_skip;
749             i_packet_size_left -= i_read;
750
751
752             if( !p_stream->p_pes->p_first )
753             {
754                 p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
755             }
756             else
757             {
758                 p_stream->p_pes->p_last->p_next = p_data;
759                 p_stream->p_pes->p_last = p_data;
760             }
761             p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
762             p_stream->p_pes->i_nb_data++;
763
764             i_skip = 0;
765             if( i_packet_size_left > 0 )
766             {
767                 if( stream_Peek( p_input->s, &p_peek, i_packet_size_left )
768                                                          < i_packet_size_left )
769                 {
770                     // EOF ?
771                     msg_Warn( p_input, "cannot peek, EOF ?" );
772                     return( 0 );
773                 }
774             }
775         }
776     }
777
778     if( i_packet_size_left > 0 )
779     {
780         if( stream_Read( p_input->s, NULL, i_packet_size_left )
781                                                          < i_packet_size_left )
782         {
783             msg_Warn( p_input, "cannot skip data, EOF ?" );
784             return( 0 );
785         }
786     }
787
788     return( 1 );
789
790 loop_error_recovery:
791     msg_Warn( p_input, "unsupported packet header" );
792     if( p_sys->p_fp->i_min_data_packet_size != p_sys->p_fp->i_max_data_packet_size )
793     {
794         msg_Err( p_input, "unsupported packet header, fatal error" );
795         return( -1 );
796     }
797     stream_Read( p_input->s, NULL, i_data_packet_min );
798
799     return( 1 );
800 }
801
802