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