]> git.sesse.net Git - vlc/blob - modules/demux/asf/asf.c
* all: A little clean up.
[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.32 2003/08/17 23:42:37 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
28 #include <vlc/vlc.h>
29 #include <vlc/input.h>
30
31 #include "codecs.h"                        /* BITMAPINFOHEADER, WAVEFORMATEX */
32 #include "libasf.h"
33 #include "asf.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 static mtime_t  GetMoviePTS( demux_sys_t * );
55 static int      DemuxPacket( input_thread_t *, vlc_bool_t b_play_audio );
56
57 /*****************************************************************************
58  * Open: check file and initializes ASF structures
59  *****************************************************************************/
60 static int Open( vlc_object_t * p_this )
61 {
62     input_thread_t  *p_input = (input_thread_t *)p_this;
63     uint8_t         *p_peek;
64     guid_t          guid;
65
66     demux_sys_t     *p_demux;
67     int             i_stream;
68
69     vlc_bool_t      b_seekable;
70
71     input_info_category_t *p_cat;
72
73     /* a little test to see if it could be a asf stream */
74     if( input_Peek( p_input, &p_peek, 16 ) < 16 )
75     {
76         msg_Warn( p_input, "ASF plugin discarded (cannot peek)" );
77         return VLC_EGENERIC;
78     }
79     ASF_GetGUID( &guid, p_peek );
80     if( !ASF_CmpGUID( &guid, &asf_object_header_guid ) )
81     {
82         msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
83         return VLC_EGENERIC;
84     }
85
86     /* Set p_input field */
87     p_input->pf_demux = Demux;
88     p_input->p_demux_data = p_demux = malloc( sizeof( demux_sys_t ) );
89     memset( p_demux, 0, sizeof( demux_sys_t ) );
90     p_demux->i_time = -1;
91
92     /* Now load all object ( except raw data ) */
93     b_seekable = p_input->stream.b_seekable &&
94                  p_input->stream.i_method == INPUT_METHOD_FILE;
95
96     if( !ASF_ReadObjectRoot( p_input, &p_demux->root, b_seekable ) )
97     {
98         msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
99         free( p_demux );
100         return VLC_EGENERIC;
101     }
102     /* Check if we have found all mandatory asf object */
103     if( !p_demux->root.p_hdr || !p_demux->root.p_data )
104     {
105         msg_Warn( p_input, "ASF plugin discarded (not a valid file)" );
106         goto error;
107     }
108
109     if( !( p_demux->p_fp = ASF_FindObject( p_demux->root.p_hdr,
110                                     &asf_object_file_properties_guid, 0 ) ) )
111     {
112         msg_Warn( p_input,
113                   "ASF plugin discarded (missing file_properties object)" );
114         goto error;
115     }
116
117     if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
118     {
119         msg_Warn( p_input,
120                   "ASF plugin discarded (invalid file_properties object)" );
121         goto error;
122     }
123
124     p_demux->i_streams = ASF_CountObject( p_demux->root.p_hdr,
125                                           &asf_object_stream_properties_guid );
126     if( !p_demux->i_streams )
127     {
128         msg_Warn( p_input, "ASF plugin discarded (cannot find any stream!)" );
129         goto error;
130     }
131
132     msg_Dbg( p_input, "found %d streams", p_demux->i_streams );
133
134     p_cat = input_InfoCategory( p_input, "Asf" );
135     input_AddInfo( p_cat, _("Number of streams"), "%d" , p_demux->i_streams );
136
137     /*  create one program */
138     vlc_mutex_lock( &p_input->stream.stream_lock );
139     if( input_InitStream( p_input, 0 ) == -1)
140     {
141         vlc_mutex_unlock( &p_input->stream.stream_lock );
142         msg_Err( p_input, "cannot init stream" );
143         goto error;
144     }
145     if( input_AddProgram( p_input, 0, 0) == NULL )
146     {
147         vlc_mutex_unlock( &p_input->stream.stream_lock );
148         msg_Err( p_input, "cannot add program" );
149         goto error;
150     }
151     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
152     p_input->stream.i_mux_rate = 0 ; /* updated later */
153     vlc_mutex_unlock( &p_input->stream.stream_lock );
154
155     for( i_stream = 0; i_stream < p_demux->i_streams; i_stream ++ )
156     {
157         asf_stream_t    *p_stream;
158         asf_object_stream_properties_t *p_sp;
159         char psz_cat[sizeof("Stream ")+10];
160         sprintf( psz_cat, "Stream %d", i_stream );
161         p_cat = input_InfoCategory( p_input, psz_cat);
162
163         p_sp = ASF_FindObject( p_demux->root.p_hdr,
164                                &asf_object_stream_properties_guid,
165                                i_stream );
166
167         p_stream =
168             p_demux->stream[p_sp->i_stream_number] =
169                 malloc( sizeof( asf_stream_t ) );
170         memset( p_stream, 0, sizeof( asf_stream_t ) );
171
172         p_stream->i_time = -1;
173         p_stream->p_sp = p_sp;
174
175         vlc_mutex_lock( &p_input->stream.stream_lock );
176         p_stream->p_es = NULL;
177
178         vlc_mutex_unlock( &p_input->stream.stream_lock );
179         if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_audio ) )
180         {
181             int i_codec;
182             if( p_sp->p_type_specific_data )
183             {
184                 i_codec = GetWLE( p_sp->p_type_specific_data );
185             }
186             else
187             {
188                 i_codec = -1;
189             }
190
191             p_stream->i_cat = AUDIO_ES;
192             p_stream->p_es = input_AddES( p_input,
193                          p_input->stream.p_selected_program,
194                          p_sp->i_stream_number, AUDIO_ES, NULL, 0 );
195
196             input_AddInfo( p_cat, _("Type"), _("Audio") );
197             msg_Dbg( p_input,
198                     "adding new audio stream(codec:0x%x,ID:%d)",
199                     i_codec,
200                     p_sp->i_stream_number );
201             switch( i_codec )
202             {
203                 case( 0x01 ):
204                     p_stream->p_es->i_fourcc =
205                         VLC_FOURCC( 'a', 'r', 'a', 'w' );
206                     break;
207                 case( 0x50 ):
208                 case( 0x55 ):
209                     p_stream->p_es->i_fourcc =
210                         VLC_FOURCC( 'm','p','g','a' );
211                     break;
212                 case( 0x2000 ):
213                     p_stream->p_es->i_fourcc =
214                         VLC_FOURCC( 'a','5','2',' ' );
215                     break;
216                 case( 0x160 ):
217                     p_stream->p_es->i_fourcc =
218                         VLC_FOURCC( 'w','m','a','1' );
219                     break;
220                 case( 0x161 ):
221                     p_stream->p_es->i_fourcc =
222                         VLC_FOURCC( 'w','m','a','2' );
223                     break;
224                 default:
225                     p_stream->p_es->i_fourcc =
226                         VLC_FOURCC( 'm','s',(i_codec >> 8)&0xff,i_codec&0xff );
227             }
228             input_AddInfo( p_cat, _("Codec"), "%.4s", (char*)&p_stream->p_es->i_fourcc );
229             if( p_sp->i_type_specific_data_length > 0 )
230             {
231                 WAVEFORMATEX    *p_wf;
232                 size_t          i_size;
233                 uint8_t         *p_data;
234
235                 i_size = p_sp->i_type_specific_data_length;
236
237                 p_wf = malloc( i_size );
238                 p_stream->p_es->p_waveformatex = (void*)p_wf;
239                 p_data = p_sp->p_type_specific_data;
240
241                 p_wf->wFormatTag        = GetWLE( p_data );
242                 p_wf->nChannels         = GetWLE( p_data + 2 );
243                 input_AddInfo( p_cat, _("Channels"), "%d", p_wf->nChannels );
244                 p_wf->nSamplesPerSec    = GetDWLE( p_data + 4 );
245                 input_AddInfo( p_cat, _("Sample Rate"), "%d", p_wf->nSamplesPerSec );
246                 p_wf->nAvgBytesPerSec   = GetDWLE( p_data + 8 );
247                 input_AddInfo( p_cat, _("Avg. byterate"), "%d", p_wf->nAvgBytesPerSec );
248                 p_wf->nBlockAlign       = GetWLE( p_data + 12 );
249                 p_wf->wBitsPerSample    = GetWLE( p_data + 14 );
250                 input_AddInfo( p_cat, _("Bits Per Sample"), "%d", p_wf->wBitsPerSample );
251                 p_wf->cbSize            = __MIN( GetWLE( p_data + 16 ), i_size - sizeof( WAVEFORMATEX ));
252                 if( p_wf->cbSize > 0 )
253                 {
254                     memcpy( &p_wf[1], p_data + sizeof( WAVEFORMATEX ), p_wf->cbSize );
255                 }
256             }
257         }
258         else
259         if( ASF_CmpGUID( &p_sp->i_stream_type, &asf_object_stream_type_video ) )
260         {
261             p_stream->i_cat = VIDEO_ES;
262             p_stream->p_es = input_AddES( p_input,
263                          p_input->stream.p_selected_program,
264                          p_sp->i_stream_number, VIDEO_ES, NULL, 0 );
265
266             input_AddInfo( p_cat, _("Type"), _("Video") );
267             msg_Dbg( p_input, "adding new video stream(ID:%d)",
268                      p_sp->i_stream_number );
269             if( p_sp->p_type_specific_data )
270             {
271                 p_stream->p_es->i_fourcc =
272                     VLC_FOURCC( p_sp->p_type_specific_data[27],
273                                 p_sp->p_type_specific_data[28],
274                                 p_sp->p_type_specific_data[29],
275                                 p_sp->p_type_specific_data[30] );
276             }
277             else
278             {
279                 p_stream->p_es->i_fourcc =
280                     VLC_FOURCC( 'u','n','d','f' );
281             }
282             input_AddInfo( p_cat, _("Codec"), "%.4s", (char*)&p_stream->p_es->i_fourcc );
283             if( p_sp->i_type_specific_data_length > 11 )
284             {
285                 BITMAPINFOHEADER *p_bih;
286                 size_t      i_size;
287                 uint8_t     *p_data;
288
289                 i_size = p_sp->i_type_specific_data_length - 11;
290
291                 p_bih = malloc( i_size );
292                 p_stream->p_es->p_bitmapinfoheader = (void*)p_bih;
293                 p_data = p_sp->p_type_specific_data + 11;
294
295                 p_bih->biSize       = GetDWLE( p_data );
296                 input_AddInfo( p_cat, _("Size"), "%d", p_bih->biSize );
297                 p_bih->biWidth      = GetDWLE( p_data + 4 );
298                 p_bih->biHeight     = GetDWLE( p_data + 8 );
299                 input_AddInfo( p_cat, _("Resolution"), "%dx%d", p_bih->biWidth, p_bih->biHeight );
300                 p_bih->biPlanes     = GetDWLE( p_data + 12 );
301                 input_AddInfo( p_cat, _("Planes"), "%d", p_bih->biPlanes );
302                 p_bih->biBitCount   = GetDWLE( p_data + 14 );
303                 input_AddInfo( p_cat, _("Bits Per Pixel"), "%d", p_bih->biBitCount );
304                 p_bih->biCompression= GetDWLE( p_data + 16 );
305                 p_bih->biSizeImage  = GetDWLE( p_data + 20 );
306                 input_AddInfo( p_cat, _("Image Size"), "%d", p_bih->biSizeImage );
307                 p_bih->biXPelsPerMeter = GetDWLE( p_data + 24 );
308                 input_AddInfo( p_cat, _("X pixels per meter"), "%d", p_bih->biXPelsPerMeter );
309                 p_bih->biYPelsPerMeter = GetDWLE( p_data + 28 );
310                 input_AddInfo( p_cat, _("Y pixels per meter"), "%d", p_bih->biYPelsPerMeter );
311                 p_bih->biClrUsed       = GetDWLE( p_data + 32 );
312                 p_bih->biClrImportant  = GetDWLE( p_data + 36 );
313
314                 if( i_size > sizeof( BITMAPINFOHEADER ) )
315                 {
316                     memcpy( (uint8_t*)p_bih + sizeof( BITMAPINFOHEADER ),
317                             p_data + sizeof( BITMAPINFOHEADER ),
318                             i_size - sizeof( BITMAPINFOHEADER ) );
319                 }
320             }
321
322         }
323         else
324         {
325             p_stream->i_cat = UNKNOWN_ES;
326             msg_Dbg( p_input, "ignoring unknown stream(ID:%d)",
327                      p_sp->i_stream_number );
328         }
329
330         vlc_mutex_lock( &p_input->stream.stream_lock );
331         if( p_stream->p_es )
332         {
333             input_SelectES( p_input, p_stream->p_es );
334         }
335         vlc_mutex_unlock( &p_input->stream.stream_lock );
336     }
337
338
339     p_demux->i_data_begin = p_demux->root.p_data->i_object_pos + 50;
340     if( p_demux->root.p_data->i_object_size != 0 )
341     { // local file
342         p_demux->i_data_end = p_demux->root.p_data->i_object_pos +
343                                     p_demux->root.p_data->i_object_size;
344     }
345     else
346     { // live/broacast
347         p_demux->i_data_end = -1;
348     }
349
350
351     // go to first packet
352     ASF_SeekAbsolute( p_input, p_demux->i_data_begin );
353
354     vlc_mutex_lock( &p_input->stream.stream_lock );
355     /* try to calculate movie time */
356     if( p_demux->p_fp->i_data_packets_count > 0 )
357     {
358         int64_t i_count;
359         mtime_t i_length;
360
361         /* real number of packets */
362         i_count = ( p_input->stream.p_selected_area->i_size -
363                        p_demux->i_data_begin ) /
364                             p_demux->p_fp->i_min_data_packet_size;
365         /* calculate the time duration in s */
366         i_length = (mtime_t)p_demux->p_fp->i_play_duration / 10 *
367                    (mtime_t)i_count /
368                    (mtime_t)p_demux->p_fp->i_data_packets_count /
369                    (mtime_t)1000000;
370         if( i_length > 0 )
371         {
372             p_input->stream.i_mux_rate =
373                 p_input->stream.p_selected_area->i_size / 50 / i_length;
374         }
375         else
376         {
377             p_input->stream.i_mux_rate = 0;
378         }
379     }
380     else
381     {
382         /* cannot known */
383         p_input->stream.i_mux_rate = 0;
384     }
385
386     p_input->stream.p_selected_program->b_is_ok = 1;
387     vlc_mutex_unlock( &p_input->stream.stream_lock );
388
389     return VLC_SUCCESS;
390
391 error:
392     ASF_FreeObjectRoot( p_input, &p_demux->root );
393     free( p_demux );
394     return VLC_EGENERIC;
395 }
396
397
398 /*****************************************************************************
399  * Demux: read packet and send them to decoders
400  *****************************************************************************/
401 static int Demux( input_thread_t *p_input )
402 {
403     demux_sys_t *p_demux = p_input->p_demux_data;
404     vlc_bool_t b_play_audio;
405     int i;
406     vlc_bool_t b_stream;
407
408     b_stream = VLC_FALSE;
409     for( i = 0; i < 128; i++ )
410     {
411         if( p_demux->stream[i] &&
412             p_demux->stream[i]->p_es &&
413             p_demux->stream[i]->p_es->p_decoder_fifo )
414         {
415             b_stream = VLC_TRUE;
416         }
417     }
418     if( !b_stream )
419     {
420         msg_Warn( p_input, "no stream selected, exiting..." );
421         return( 0 );
422     }
423
424     /* catch seek from user */
425     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
426     {
427         off_t i_offset;
428
429         msleep( p_input->i_pts_delay );
430         i_offset = ASF_TellAbsolute( p_input ) - p_demux->i_data_begin;
431
432         if( i_offset  < 0 )
433         {
434             i_offset = 0;
435         }
436         i_offset += p_demux->p_fp->i_min_data_packet_size -
437                         i_offset % p_demux->p_fp->i_min_data_packet_size;
438         ASF_SeekAbsolute( p_input, p_demux->i_data_begin + i_offset );
439
440         p_demux->i_time = -1;
441         for( i = 0; i < 128 ; i++ )
442         {
443 #define p_stream p_demux->stream[i]
444             if( p_stream )
445             {
446                 p_stream->i_time = -1;
447             }
448 #undef p_stream
449         }
450     }
451
452     /* Check if we need to send the audio data to decoder */
453     b_play_audio = !p_input->stream.control.b_mute;
454
455     for( ;; )
456     {
457         mtime_t i_length;
458         mtime_t i_time_begin = GetMoviePTS( p_demux );
459         int i_result;
460
461         if( p_input->b_die )
462         {
463             break;
464         }
465
466         if( ( i_result = DemuxPacket( p_input, b_play_audio ) ) <= 0 )
467         {
468             return i_result;
469         }
470         if( i_time_begin == -1 )
471         {
472             i_time_begin = GetMoviePTS( p_demux );
473         }
474         else
475         {
476             i_length = GetMoviePTS( p_demux ) - i_time_begin;
477             if( i_length < 0 || i_length >= 40 * 1000 )
478             {
479                 break;
480             }
481         }
482     }
483
484     p_demux->i_time = GetMoviePTS( p_demux );
485     if( p_demux->i_time >= 0 )
486     {
487         input_ClockManageRef( p_input,
488                               p_input->stream.p_selected_program,
489                               p_demux->i_time * 9 / 100 );
490     }
491
492     return( 1 );
493 }
494
495 /*****************************************************************************
496  * Close: frees unused data
497  *****************************************************************************/
498 static void Close( vlc_object_t * p_this )
499 {
500     input_thread_t *p_input = (input_thread_t *)p_this;
501     demux_sys_t    *p_sys = p_input->p_demux_data;
502     int i_stream;
503
504     msg_Dbg( p_input, "Freeing all memory" );
505
506     ASF_FreeObjectRoot( p_input, &p_sys->root );
507     for( i_stream = 0; i_stream < 128; i_stream++ )
508     {
509 #define p_stream p_sys->stream[i_stream]
510         if( p_stream )
511         {
512             if( p_stream->p_pes )
513             {
514                 input_DeletePES( p_input->p_method_data, p_stream->p_pes );
515             }
516             free( p_stream );
517         }
518 #undef p_stream
519     }
520     free( p_sys );
521 }
522
523
524 /*****************************************************************************
525  *
526  *****************************************************************************/
527 static mtime_t GetMoviePTS( demux_sys_t *p_demux )
528 {
529     mtime_t i_time;
530     int     i_stream;
531
532     i_time = -1;
533     for( i_stream = 0; i_stream < 128 ; i_stream++ )
534     {
535 #define p_stream p_demux->stream[i_stream]
536         if( p_stream && p_stream->p_es && p_stream->p_es->p_decoder_fifo && p_stream->i_time > 0)
537         {
538             if( i_time < 0 )
539             {
540                 i_time = p_stream->i_time;
541             }
542             else
543             {
544                 i_time = __MIN( i_time, p_stream->i_time );
545             }
546         }
547 #undef p_stream
548     }
549
550     return( i_time );
551 }
552
553 #define GETVALUE2b( bits, var, def ) \
554     switch( (bits)&0x03 ) \
555     { \
556         case 1: var = p_peek[i_skip]; i_skip++; break; \
557         case 2: var = GetWLE( p_peek + i_skip );  i_skip+= 2; break; \
558         case 3: var = GetDWLE( p_peek + i_skip ); i_skip+= 4; break; \
559         case 0: \
560         default: var = def; break;\
561     }
562
563 static int DemuxPacket( input_thread_t *p_input, vlc_bool_t b_play_audio )
564 {
565     demux_sys_t *p_demux = p_input->p_demux_data;
566     int     i_data_packet_min = p_demux->p_fp->i_min_data_packet_size;
567     uint8_t *p_peek;
568     int     i_skip;
569
570     int     i_packet_size_left;
571     int     i_packet_flags;
572     int     i_packet_property;
573
574     int     b_packet_multiple_payload;
575     int     i_packet_length;
576     int     i_packet_sequence;
577     int     i_packet_padding_length;
578
579     uint32_t    i_packet_send_time;
580     uint16_t    i_packet_duration;
581     int         i_payload;
582     int         i_payload_count;
583     int         i_payload_length_type;
584
585
586     if( input_Peek( p_input, &p_peek, i_data_packet_min ) < i_data_packet_min )
587     {
588         // EOF ?
589         msg_Warn( p_input, "cannot peek while getting new packet, EOF ?" );
590         return( 0 );
591     }
592     i_skip = 0;
593
594     /* *** parse error correction if present *** */
595     if( p_peek[0]&0x80 )
596     {
597         unsigned int i_error_correction_length_type;
598         unsigned int i_error_correction_data_length;
599         unsigned int i_opaque_data_present;
600
601         i_error_correction_data_length = p_peek[0] & 0x0f;  // 4bits
602         i_opaque_data_present = ( p_peek[0] >> 4 )& 0x01;    // 1bit
603         i_error_correction_length_type = ( p_peek[0] >> 5 ) & 0x03; // 2bits
604         i_skip += 1; // skip error correction flags
605
606         if( i_error_correction_length_type != 0x00 ||
607             i_opaque_data_present != 0 ||
608             i_error_correction_data_length != 0x02 )
609         {
610             goto loop_error_recovery;
611         }
612
613         i_skip += i_error_correction_data_length;
614     }
615     else
616     {
617         msg_Warn( p_input, "p_peek[0]&0x80 != 0x80" );
618     }
619
620     /* sanity check */
621     if( i_skip + 2 >= i_data_packet_min )
622     {
623         goto loop_error_recovery;
624     }
625
626     i_packet_flags = p_peek[i_skip]; i_skip++;
627     i_packet_property = p_peek[i_skip]; i_skip++;
628
629     b_packet_multiple_payload = i_packet_flags&0x01;
630
631     /* read some value */
632     GETVALUE2b( i_packet_flags >> 5, i_packet_length, i_data_packet_min );
633     GETVALUE2b( i_packet_flags >> 1, i_packet_sequence, 0 );
634     GETVALUE2b( i_packet_flags >> 3, i_packet_padding_length, 0 );
635
636     i_packet_send_time = GetDWLE( p_peek + i_skip ); i_skip += 4;
637     i_packet_duration  = GetWLE( p_peek + i_skip ); i_skip += 2;
638
639 //        i_packet_size_left = i_packet_length;   // XXX données reellement lu
640     /* FIXME I have to do that for some file, I don't known why */
641     i_packet_size_left = i_data_packet_min;
642
643     if( b_packet_multiple_payload )
644     {
645         i_payload_count = p_peek[i_skip] & 0x3f;
646         i_payload_length_type = ( p_peek[i_skip] >> 6 )&0x03;
647         i_skip++;
648     }
649     else
650     {
651         i_payload_count = 1;
652         i_payload_length_type = 0x02; // unused
653     }
654
655     for( i_payload = 0; i_payload < i_payload_count ; i_payload++ )
656     {
657         asf_stream_t   *p_stream;
658
659         int i_stream_number;
660         int i_media_object_number;
661         int i_media_object_offset;
662         int i_replicated_data_length;
663         int i_payload_data_length;
664         int i_payload_data_pos;
665         int i_sub_payload_data_length;
666         int i_tmp;
667
668         mtime_t i_pts;
669         mtime_t i_pts_delta;
670
671         if( i_skip >= i_packet_size_left )
672         {
673             /* prevent some segfault with invalid file */
674             break;
675         }
676
677         i_stream_number = p_peek[i_skip] & 0x7f;
678         i_skip++;
679
680         GETVALUE2b( i_packet_property >> 4, i_media_object_number, 0 );
681         GETVALUE2b( i_packet_property >> 2, i_tmp, 0 );
682         GETVALUE2b( i_packet_property, i_replicated_data_length, 0 );
683
684         if( i_replicated_data_length > 1 ) // should be at least 8 bytes
685         {
686             i_pts = (mtime_t)GetDWLE( p_peek + i_skip + 4 ) * 1000;
687             i_skip += i_replicated_data_length;
688             i_pts_delta = 0;
689
690             i_media_object_offset = i_tmp;
691
692             if( i_skip >= i_packet_size_left )
693             {
694                 break;
695             }
696         }
697         else if( i_replicated_data_length == 1 )
698         {
699
700             msg_Dbg( p_input, "found compressed payload" );
701
702             i_pts = (mtime_t)i_tmp * 1000;
703             i_pts_delta = (mtime_t)p_peek[i_skip] * 1000; i_skip++;
704
705             i_media_object_offset = 0;
706         }
707         else
708         {
709             i_pts = (mtime_t)i_packet_send_time * 1000;
710             i_pts_delta = 0;
711
712             i_media_object_offset = i_tmp;
713         }
714
715         i_pts = __MAX( i_pts - p_demux->p_fp->i_preroll * 1000, 0 );
716         if( b_packet_multiple_payload )
717         {
718             GETVALUE2b( i_payload_length_type, i_payload_data_length, 0 );
719         }
720         else
721         {
722             i_payload_data_length = i_packet_length -
723                                         i_packet_padding_length - i_skip;
724         }
725
726         if( i_payload_data_length < 0 || i_skip + i_payload_data_length > i_packet_size_left )
727         {
728             break;
729         }
730
731 #if 0
732          msg_Dbg( p_input,
733                   "payload(%d/%d) stream_number:%d media_object_number:%d media_object_offset:%d replicated_data_length:%d payload_data_length %d",
734                   i_payload + 1,
735                   i_payload_count,
736                   i_stream_number,
737                   i_media_object_number,
738                   i_media_object_offset,
739                   i_replicated_data_length,
740                   i_payload_data_length );
741 #endif
742
743         if( !( p_stream = p_demux->stream[i_stream_number] ) )
744         {
745             msg_Warn( p_input,
746                       "undeclared stream[Id 0x%x]", i_stream_number );
747             i_skip += i_payload_data_length;
748             continue;   // over payload
749         }
750
751         if( !p_stream->p_es || !p_stream->p_es->p_decoder_fifo )
752         {
753             i_skip += i_payload_data_length;
754             continue;
755         }
756
757
758         for( i_payload_data_pos = 0;
759              i_payload_data_pos < i_payload_data_length &&
760                     i_packet_size_left > 0;
761              i_payload_data_pos += i_sub_payload_data_length )
762         {
763             data_packet_t  *p_data;
764             int i_read;
765             // read sub payload length
766             if( i_replicated_data_length == 1 )
767             {
768                 i_sub_payload_data_length = p_peek[i_skip]; i_skip++;
769                 i_payload_data_pos++;
770             }
771             else
772             {
773                 i_sub_payload_data_length = i_payload_data_length;
774             }
775
776             /* FIXME I don't use i_media_object_number, sould I ? */
777             if( p_stream->p_pes && i_media_object_offset == 0 )
778             {
779                 /* send complete packet to decoder */
780                 if( p_stream->p_pes->i_pes_size > 0 )
781                 {
782                     if( p_stream->p_es->p_decoder_fifo &&
783                         ( b_play_audio || p_stream->i_cat != AUDIO_ES ) )
784                     {
785                         p_stream->p_pes->i_rate =
786                             p_input->stream.control.i_rate;
787                         input_DecodePES( p_stream->p_es->p_decoder_fifo,
788                                          p_stream->p_pes );
789                     }
790                     else
791                     {
792                         input_DeletePES( p_input->p_method_data,
793                                          p_stream->p_pes );
794                     }
795                     p_stream->p_pes = NULL;
796                 }
797             }
798
799             if( !p_stream->p_pes )  // add a new PES
800             {
801                 p_stream->i_time =
802                     ( (mtime_t)i_pts + i_payload * (mtime_t)i_pts_delta );
803
804                 p_stream->p_pes = input_NewPES( p_input->p_method_data );
805                 p_stream->p_pes->i_dts =
806                     p_stream->p_pes->i_pts =
807                         input_ClockGetTS( p_input,
808                                           p_input->stream.p_selected_program,
809                                           p_stream->i_time * 9 /100 );
810
811                 //msg_Err( p_input, "stream[0x%2x] pts=%lld", i_stream_number, p_stream->p_pes->i_pts );
812                 p_stream->p_pes->p_next = NULL;
813                 p_stream->p_pes->i_nb_data = 0;
814                 p_stream->p_pes->i_pes_size = 0;
815             }
816
817             i_read = i_sub_payload_data_length + i_skip;
818             if( input_SplitBuffer( p_input, &p_data, i_read ) < i_read )
819             {
820                 msg_Warn( p_input, "cannot read data" );
821                 return( 0 );
822             }
823             p_data->p_payload_start += i_skip;
824             i_packet_size_left -= i_read;
825
826
827             if( !p_stream->p_pes->p_first )
828             {
829                 p_stream->p_pes->p_first = p_stream->p_pes->p_last = p_data;
830             }
831             else
832             {
833                 p_stream->p_pes->p_last->p_next = p_data;
834                 p_stream->p_pes->p_last = p_data;
835             }
836             p_stream->p_pes->i_pes_size += i_sub_payload_data_length;
837             p_stream->p_pes->i_nb_data++;
838
839             i_skip = 0;
840             if( i_packet_size_left > 0 )
841             {
842                 if( input_Peek( p_input, &p_peek, i_packet_size_left ) < i_packet_size_left )
843                 {
844                     // EOF ?
845                     msg_Warn( p_input, "cannot peek, EOF ?" );
846                     return( 0 );
847                 }
848             }
849         }
850     }
851
852     if( i_packet_size_left > 0 )
853     {
854         if( !ASF_SkipBytes( p_input, i_packet_size_left ) )
855         {
856             msg_Warn( p_input, "cannot skip data, EOF ?" );
857             return( 0 );
858         }
859     }
860
861     return( 1 );
862
863 loop_error_recovery:
864     msg_Warn( p_input, "unsupported packet header" );
865     if( p_demux->p_fp->i_min_data_packet_size != p_demux->p_fp->i_max_data_packet_size )
866     {
867         msg_Err( p_input, "unsupported packet header, fatal error" );
868         return( -1 );
869     }
870     ASF_SkipBytes( p_input, i_data_packet_min );
871
872     return( 1 );
873 }
874
875