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