* ./modules/demux/mpeg/system.c: added a helper plugin for MPEG-related
[vlc.git] / modules / demux / mp4 / mp4.c
1 /*****************************************************************************
2  * mp4.c : MP4 file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: mp4.c,v 1.2 2002/08/07 00:29:36 sam 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 "libmp4.h"
35 #include "mp4.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int    MP4Init    ( vlc_object_t * );
41 static void __MP4End     ( vlc_object_t * );
42 static int    MP4Demux   ( input_thread_t * );
43
44 #define MP4End(a) __MP4End(VLC_OBJECT(a))
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 vlc_module_begin();
50     set_description( "MP4 demuxer" );
51     set_capability( "demux", 242 );
52     set_callbacks( MP4Init, __MP4End );
53 vlc_module_end();
54
55 /*****************************************************************************
56  * Declaration of local function 
57  *****************************************************************************/
58 static void MP4_ParseTrack();
59
60 static int MP4_CreateChunksIndex();
61 static int MP4_CreateSamplesIndex();
62
63 static void MP4_StartDecoder();
64 static void MP4_StopDecoder();
65
66 static int  MP4_ReadSample();
67 static int  MP4_DecodeSample();
68
69 #define MP4_Set4BytesLE( p, dw ) \
70     *((u8*)p)   = ( (dw)&0xff ); \
71     *((u8*)p+1) = ( ((dw)>> 8)&0xff ); \
72     *((u8*)p+2) = ( ((dw)>>16)&0xff ); \
73     *((u8*)p+3) = ( ((dw)>>24)&0xff )
74
75 #define MP4_Set2BytesLE( p, dw ) \
76     *((u8*)p) = ( (dw)&0xff ); \
77     *((u8*)p+1) = ( ((dw)>> 8)&0xff )
78
79     
80 /*****************************************************************************
81  * MP4Init: check file and initializes MP4 structures
82  *****************************************************************************/
83 static int MP4Init( vlc_object_t * p_this )
84 {   
85     input_thread_t *p_input = (input_thread_t *)p_this;
86     u8  *p_peek;
87     u32 i_type;
88     
89     demux_sys_t *p_demux;
90     
91     MP4_Box_t *p_moov;    
92     MP4_Box_t *p_ftyp;
93
94
95     MP4_Box_t *p_mvhd;
96     MP4_Box_t *p_trak;
97
98     int i;
99     /* I need to seek */
100     if( !p_input->stream.b_seekable )
101     {
102         msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
103         return( -1 );
104             
105     } 
106     /* Initialize access plug-in structures. */
107     if( p_input->i_mtu == 0 )
108     {
109         /* Improve speed. */
110         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
111     }
112
113     p_input->pf_demux = MP4Demux;
114
115     /* a little test to see if it could be a mp4 */
116     if( input_Peek( p_input, &p_peek, 8 ) < 8 )
117     {
118         msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
119         return( -1 );
120     }
121     i_type = ( p_peek[4] ) + ( p_peek[5] << 8 ) +
122                 ( p_peek[6] << 16 ) + ( p_peek[7] << 24);
123     switch( i_type )
124     {
125         case( FOURCC_ftyp ):
126         case( FOURCC_moov ):
127         case( FOURCC_moof ):
128         case( FOURCC_mdat ):
129         case( FOURCC_udta ): /* should never match but ... */
130         case( FOURCC_free ):
131         case( FOURCC_skip ):
132         case( FOURCC_wide ): /* not mp4 compliant but ... */
133             break;
134          default:
135             msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
136             return( -1 );
137     }
138
139     /* create our structure that will contains all data */
140     if( !( p_input->p_demux_data = 
141                 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
142     {
143         msg_Err( p_input, "out of memory" );
144         return( -1 );
145     }
146     memset( p_demux, 0, sizeof( demux_sys_t ) );
147     p_input->p_demux_data = p_demux;
148        
149
150     /* Now load all boxes ( except raw data ) */
151     if( !MP4_ReadBoxRoot( p_input, &p_demux->box_root ) )
152     {
153         msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
154         return( -1 );
155     }
156
157     MP4_DumpBoxStructure( p_input, &p_demux->box_root );
158
159     if( ( p_ftyp = MP4_FindBox( &p_demux->box_root, FOURCC_ftyp ) ) )
160     {
161         switch( p_ftyp->data.p_ftyp->i_major_brand )
162         {
163             case( FOURCC_isom ):
164                 msg_Info( p_input, 
165                           "ISO Media file (isom) version %d.",
166                           p_ftyp->data.p_ftyp->i_minor_version );
167                 break;
168             default:
169                 msg_Info( p_input,
170                           "Unrecognize major file specification (%c%c%c%c).",
171                            p_ftyp->data.p_ftyp->i_major_brand&0xff,
172                            ( p_ftyp->data.p_ftyp->i_major_brand >>  8)&0xff,
173                            ( p_ftyp->data.p_ftyp->i_major_brand >> 16 )&0xff,
174                            ( p_ftyp->data.p_ftyp->i_major_brand >> 24 )&0xff );
175                 break;
176         }
177     }
178     else
179     {
180         msg_Info( p_input, "File Type box missing(assume ISO Media file)" );
181     }
182
183     /* the file need to have one moov box */
184     if( !( p_moov = MP4_FindBox( &p_demux->box_root, FOURCC_moov ) ) )
185     {
186         msg_Warn( p_input, "MP4 plugin discarded (missing moov box)" );
187         MP4End( p_input );
188         return( -1 );
189     }
190
191     if( MP4_CountBox( &p_demux->box_root, FOURCC_moov ) != 1 )
192     {
193         msg_Warn( p_input, "more than one \"moov\" box (continuying anyway)" );
194     }
195
196     if( !(p_mvhd = MP4_FindBox( p_moov, FOURCC_mvhd ) ) )
197     {
198         msg_Err( p_input, "cannot find \"mvhd\" box" );
199         MP4End( p_input );
200         return( -1 );
201     }
202     else
203     {
204         p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
205     }
206     
207     p_demux->i_tracks = MP4_CountBox( p_moov, FOURCC_trak );
208     msg_Dbg( p_input, "find %d track%c",
209                         p_demux->i_tracks,
210                         p_demux->i_tracks ? 's':' ' );
211
212     if( !( p_trak = MP4_FindBox( p_moov, FOURCC_trak ) ) )
213     {
214         msg_Err( p_input, "cannot find /moov/trak !" );
215         MP4End( p_input );
216         return( -1 );
217     }
218
219     /* allocate memory */
220     p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
221
222     /* now process each track and extract all usefull informations */
223     for( i = 0; i < p_demux->i_tracks; i++ )
224     {
225         MP4_ParseTrack( p_input, &p_demux->track[i], p_trak );
226
227         if( p_demux->track[i].b_ok )
228         {
229             char *psz_cat;
230             switch( p_demux->track[i].i_cat )
231             {
232                 case( VIDEO_ES ):
233                     psz_cat = "video";
234                     break;
235                 case( AUDIO_ES ):
236                     psz_cat = "audio";
237                     break;
238                 default:
239                     psz_cat = "";
240                     break;
241             }
242             
243             msg_Dbg( p_input, "adding track(%d) %s (%s) language %c%c%c",
244                             i,
245                             psz_cat,
246                             p_demux->track[i].b_enable ? "enable":"disable",
247                             p_demux->track[i].i_language[0],
248                             p_demux->track[i].i_language[1], 
249                             p_demux->track[i].i_language[2] );
250         }
251         else
252         {
253             msg_Dbg( p_input, "ignoring track(%d)", i );
254         }
255
256         p_trak = MP4_FindNextBox( p_trak );
257     }
258   
259     /*  create one program */
260     vlc_mutex_lock( &p_input->stream.stream_lock );
261     if( input_InitStream( p_input, 0 ) == -1)
262     {
263         vlc_mutex_unlock( &p_input->stream.stream_lock );
264         msg_Err( p_input, "cannot init stream" );
265         MP4End( p_input );
266         return( -1 );
267     }
268     if( input_AddProgram( p_input, 0, 0) == NULL )
269     {
270         vlc_mutex_unlock( &p_input->stream.stream_lock );
271         msg_Err( p_input, "cannot add program" );
272         MP4End( p_input );
273         return( -1 );
274     }
275     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
276     p_input->stream.i_mux_rate = 0 ; /* FIXME */
277     vlc_mutex_unlock( &p_input->stream.stream_lock );
278    
279     
280     for( i = 0; i < p_demux->i_tracks; i++ )
281     {
282         /* start decoder for this track if enable by default*/
283         if( p_demux->track[i].b_enable )
284         {
285             MP4_StartDecoder( p_input, &p_demux->track[i] );
286         }
287     }
288
289     vlc_mutex_lock( &p_input->stream.stream_lock );
290     p_input->stream.p_selected_program->b_is_ok = 1;
291     vlc_mutex_unlock( &p_input->stream.stream_lock );
292         
293     return( 0 );    
294
295 }
296
297 /*****************************************************************************
298  * MP4Demux: read packet and send them to decoders 
299  *****************************************************************************/
300 static int MP4Demux( input_thread_t *p_input )
301 {
302     demux_sys_t *p_demux = p_input->p_demux_data;
303     int i_track;
304
305     /* first wait for the good time to read a packet */
306
307     input_ClockManageRef( p_input,
308                           p_input->stream.p_selected_program,
309                           p_demux->i_pcr );
310
311
312     /* update pcr XXX in mpeg scale so in 90000 unit/s */
313     p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
314     
315
316     /* we will read 100ms for each stream so ...*/
317     p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
318     
319
320     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
321     {
322         if( ( !p_demux->track[i_track].b_ok )||
323             ( !p_demux->track[i_track].p_es )||
324             ( !p_demux->track[i_track].p_es->p_decoder_fifo )||
325             ( MP4_GetTrackPTS( &p_demux->track[i_track] ) >=
326                         MP4_GetMoviePTS( p_demux ) ) )
327         {
328             continue; /* no need to read something */
329         }
330
331         while( MP4_GetTrackPTS( &p_demux->track[i_track] ) <
332                         MP4_GetMoviePTS( p_demux ) )
333         {
334
335             pes_packet_t *p_pes;
336
337             /* read a sample */
338             if( !MP4_ReadSample( p_input ,
339                                  &p_demux->track[i_track],
340                                  &p_pes ) )
341             {
342                 break;
343             }
344
345             /* send it to decoder and update time of this track 
346                  it also launch a new decoder if needed */
347             MP4_DecodeSample( p_input ,
348                               &p_demux->track[i_track],
349                               p_pes );
350         }
351
352     }
353     
354     /* now check if all tracks are finished or unhandled*/
355     
356     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
357     {
358         if( ( p_demux->track[i_track].b_ok )&&
359             ( p_demux->track[i_track].i_sample < p_demux->track[i_track].i_sample_count )&&
360             ( p_demux->track[i_track].p_es )&&
361             ( p_demux->track[i_track].p_es->p_decoder_fifo ) )
362         {
363             return( 1 );
364         }
365     }
366
367     return( 0 ); /* EOF */
368 }
369
370 /*****************************************************************************
371  * MP4End: frees unused data
372  *****************************************************************************/
373 static void __MP4End ( vlc_object_t * p_this )
374 {   
375 #define FREE( p ) \
376     if( p ) { free( p ); } 
377     int i_track;
378     input_thread_t *  p_input = (input_thread_t *)p_this;
379     demux_sys_t *p_demux = p_input->p_demux_data;
380     
381     msg_Dbg( p_input, "Freeing all memory" );
382     MP4_FreeBox( p_input, &p_demux->box_root );
383     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
384     {
385         int i_chunk;
386         for( i_chunk = 0; 
387                 i_chunk < p_demux->track[i_track].i_chunk_count; i_chunk++ )
388         {
389             if( p_demux->track[i_track].chunk )
390             {
391                FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_count_dts);
392                FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_delta_dts );
393             }
394         }
395
396         if( !p_demux->track[i_track].i_sample_size )
397         {
398             FREE( p_demux->track[i_track].p_sample_size );
399         }
400     }
401     FREE( p_demux->track );
402 #undef FREE
403 }
404
405
406 /****************************************************************************
407  * Local functions, specific to vlc
408  ****************************************************************************/
409
410 /****************************************************************************
411  * Parse track information and create all needed data to run a track
412  * If it succeed b_ok is set to 1 else to 0
413  ****************************************************************************/
414 static void MP4_ParseTrack( input_thread_t *p_input,
415                      track_data_mp4_t *p_demux_track,
416                      MP4_Box_t  * p_trak )
417 {
418     int i;
419
420     MP4_Box_t *p_tkhd = MP4_FindBox( p_trak, FOURCC_tkhd );
421     MP4_Box_t *p_tref = MP4_FindBox( p_trak, FOURCC_tref );
422     MP4_Box_t *p_edts = MP4_FindBox( p_trak, FOURCC_edts );
423     MP4_Box_t *p_mdia = MP4_FindBox( p_trak, FOURCC_mdia );
424
425     MP4_Box_t *p_mdhd;
426     MP4_Box_t *p_hdlr;
427     MP4_Box_t *p_minf;
428
429     MP4_Box_t *p_vmhd;
430     MP4_Box_t *p_smhd; 
431
432     /* hint track unsuported */
433
434     /* by default, track isn't usable */
435     p_demux_track->b_ok = 0;
436
437     /* by default, we don't known the categorie */
438     p_demux_track->i_cat = UNKNOWN_ES;
439     
440     if( ( !p_tkhd )||( !p_mdia ) )
441     {
442         return;
443     }
444
445     /* do we launch this track by default ? */
446     p_demux_track->b_enable = ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
447
448     p_demux_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
449     p_demux_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
450     p_demux_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
451     
452     if( !p_edts )
453     {
454 //        msg_Warn( p_input, "Unhandled box: edts --> FIXME" );
455     }
456
457     if( !p_tref )
458     {
459 //        msg_Warn( p_input, "Unhandled box: tref --> FIXME" );
460     } 
461
462     p_mdhd = MP4_FindBox( p_mdia, FOURCC_mdhd );
463     p_hdlr = MP4_FindBox( p_mdia, FOURCC_hdlr );
464     p_minf = MP4_FindBox( p_mdia, FOURCC_minf );
465     
466     if( ( !p_mdhd )||( !p_hdlr )||( !p_minf ) )
467     {
468         return;
469     }
470
471     p_demux_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
472
473     for( i = 0; i < 3; i++ ) 
474     {
475         p_demux_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
476     }
477     
478     switch( p_hdlr->data.p_hdlr->i_handler_type )
479     {
480         case( FOURCC_soun ):
481             if( !( p_smhd = MP4_FindBox( p_minf, FOURCC_smhd ) ) )
482             {
483                 return;
484             }
485             p_demux_track->i_cat = AUDIO_ES;
486             break;
487
488         case( FOURCC_vide ):
489             if( !( p_vmhd = MP4_FindBox( p_minf, FOURCC_vmhd ) ) )
490             {
491                 return;
492             }
493             p_demux_track->i_cat = VIDEO_ES;
494             break;
495             
496         default:
497             return;
498     }
499 /*  FIXME
500     add support to:
501     p_dinf = MP4_FindBox( p_minf, FOURCC_dinf );
502 */
503     if( !( p_demux_track->p_stbl = MP4_FindBox( p_minf, FOURCC_stbl ) ) )
504     {
505         return;
506     }
507     
508     if( !( p_demux_track->p_stsd = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsd ) ) )
509     {
510         return;
511     }
512     
513     /* Create chunk  index table */
514     if( !MP4_CreateChunksIndex( p_input,p_demux_track  ) )
515     {
516         return; /* cannot create chunks index */
517     }
518     
519     /* create sample index table needed for reading and seeking */
520     if( !MP4_CreateSamplesIndex( p_input, p_demux_track ) )
521     {
522         return; /* cannot create samples index */
523     }
524      
525     p_demux_track->b_ok = 1;        
526 }
527                      
528
529
530 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
531 static int MP4_CreateChunksIndex( input_thread_t *p_input,
532                                    track_data_mp4_t *p_demux_track )
533 {
534     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
535     MP4_Box_t *p_stsc;
536
537     int i_chunk;
538     int i_index, i_last;
539    
540
541     if( ( !(p_co64 = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stco ) )&&
542                  !(p_co64 = MP4_FindBox( p_demux_track->p_stbl, FOURCC_co64 ) ) )|| 
543         ( !(p_stsc = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsc ) ) ))
544     {
545         return( 0 );
546     }
547      
548     p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
549     if( !p_demux_track->i_chunk_count )
550     {
551         msg_Warn( p_input, "No chunk defined" );
552         return( 0 );
553     }
554     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count, 
555                                    sizeof( chunk_data_mp4_t ) );
556
557     /* first we read chunk offset */
558     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
559     {
560         p_demux_track->chunk[i_chunk].i_offset = 
561                 p_co64->data.p_co64->i_chunk_offset[i_chunk];
562     }
563
564     /* now we read index for SampleEntry( soun vide mp4a mp4v ...) 
565         to be used for the sample XXX begin to 1 
566         We construct it begining at the end */
567     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
568     i_index = p_stsc->data.p_stsc->i_entry_count;
569     if( !i_index )
570     {
571         msg_Warn( p_input, "cannot read chunk table or table empty" );
572         return( 0 );
573     }
574
575     while( i_index )
576     {
577         i_index--;
578         for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
579                 i_chunk < i_last; i_chunk++ )
580         {
581             p_demux_track->chunk[i_chunk].i_sample_description_index = 
582                     p_stsc->data.p_stsc->i_sample_description_index[i_index];
583             p_demux_track->chunk[i_chunk].i_sample_count =
584                     p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
585         }
586         i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
587     }
588
589     p_demux_track->chunk[i_chunk].i_sample_first = 0;
590     for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
591     {
592         p_demux_track->chunk[i_chunk].i_sample_first =
593             p_demux_track->chunk[i_chunk-1].i_sample_first + 
594                 p_demux_track->chunk[i_chunk-1].i_sample_count;
595         
596     }
597     
598     msg_Dbg( p_input, "read %d chunk", p_demux_track->i_chunk_count );
599     return( 1 );
600
601 }
602
603
604
605 static int MP4_CreateSamplesIndex( input_thread_t *p_input,
606                                    track_data_mp4_t *p_demux_track )
607 {
608     MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
609                           ctts make same mapping but for composition time, 
610                           not yet used and probably not usefull */
611     MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2 
612                           that uses a compressed form FIXME make them in libmp4 
613                           as a unique type */
614     /* TODO use also stss and stsh table for seeking */
615     /* FIXME use edit table */
616     int i_sample;
617     int i_chunk;
618
619     int i_index;
620     int i_index_sample_used;
621
622     u64 i_last_dts; 
623     
624     p_stts = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stts );
625     p_stsz = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsz ); /* FIXME and stz2 */
626
627     
628     if( ( !p_stts )||( !p_stsz ) )
629     {
630         msg_Warn( p_input, "cannot read sample table" );
631         return( 0 ); 
632     }
633         
634     p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
635
636
637     /* for sample size, there are 2 case */
638     if( p_stsz->data.p_stsz->i_sample_size )
639     {
640         /* 1: all sample have the same size, so no need to construct a table */
641         p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
642         p_demux_track->p_sample_size = NULL;
643     }
644     else
645     {
646         /* 2: each sample can have a different size */
647         p_demux_track->i_sample_size = 0;
648         p_demux_track->p_sample_size = 
649             calloc( p_demux_track->i_sample_count, sizeof( u32 ) );
650         
651         for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
652         {
653             p_demux_track->p_sample_size[i_sample] = 
654                     p_stsz->data.p_stsz->i_entry_size[i_sample];
655         }
656     }
657     /* we have extract all information from stsz,
658         now use stts */
659
660     /* if we don't want to waste too much memory, we can't expand
661        the box !, so each chunk will contain an "extract" of this table 
662        for fast research */
663         
664     i_last_dts = 0;
665     i_index = 0; i_index_sample_used =0;
666     /* create and init last data for each chunk */
667     for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
668     {
669
670         int i_entry, i_sample_count, i;
671         /* save last dts */
672         p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
673     /* count how many entries needed for this chunk 
674        for p_sample_delta_dts and p_sample_count_dts */
675
676         i_entry = 0;
677         i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
678         while( i_sample_count > 0 )
679         {
680             i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
681             if( i_entry == 0 )
682             {
683                 i_sample_count += i_index_sample_used; /* don't count already used sample 
684                                                    int this entry */
685             }
686             i_entry++;
687         }
688         /* allocate them */
689         p_demux_track->chunk[i_chunk].p_sample_count_dts = 
690             calloc( i_entry, sizeof( u32 ) );
691         p_demux_track->chunk[i_chunk].p_sample_delta_dts =
692             calloc( i_entry, sizeof( u32 ) );
693
694         /* now copy */
695         i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
696         for( i = 0; i < i_entry; i++ )
697         {
698             int i_used;
699             int i_rest;
700             
701             i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
702
703             i_used = __MIN( i_rest, i_sample_count );
704
705             i_index_sample_used += i_used;
706
707             p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
708
709             p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
710                         p_stts->data.p_stts->i_sample_delta[i_index];
711             
712             i_last_dts += i_used * 
713                     p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
714
715             if( i_index_sample_used >=
716                              p_stts->data.p_stts->i_sample_count[i_index] )
717             {
718                 i_index++;
719                 i_index_sample_used = 0;
720             }
721         }
722         
723     }
724
725     msg_Dbg( p_input, "read %d samples", p_demux_track->i_sample_count );
726
727     return( 1 );
728 }
729
730 static void MP4_StartDecoder( input_thread_t *p_input,
731                                  track_data_mp4_t *p_demux_track )
732 {
733     MP4_Box_t *p_sample;
734     int i;
735     int i_chunk;
736
737     int i_decoder_specific_info_len;
738     u8  *p_decoder_specific_info;
739     
740     u8  *p_init;
741  
742     MP4_Box_t *p_esds;
743
744     
745     if( (!p_demux_track->b_ok )||( p_demux_track->i_cat == UNKNOWN_ES ) )
746     {
747         return;
748     }
749     
750     msg_Dbg( p_input, "Starting decoder (track ID 0x%x)",
751                       p_demux_track->i_track_ID );
752
753     /* launch decoder according in chunk we are */
754     i_chunk = p_demux_track->i_chunk;
755
756     if( !p_demux_track->chunk[i_chunk].i_sample_description_index )
757     {
758         msg_Warn( p_input, 
759                   "invalid SampleEntry index (track ID 0x%x)",
760                   p_demux_track->i_track_ID );
761         return;
762     } 
763     
764     p_sample = MP4_FindNbBox( p_demux_track->p_stsd,
765                  p_demux_track->chunk[i_chunk].i_sample_description_index - 1);
766
767     if( ( !p_sample )||( !p_sample->data.p_data ) )
768     {
769         msg_Warn( p_input, 
770                   "cannot find SampleEntry (track ID 0x%x)",
771                   p_demux_track->i_track_ID );
772         return;
773     }
774
775     vlc_mutex_lock( &p_input->stream.stream_lock );
776     p_demux_track->p_es = input_AddES( p_input,
777                                        p_input->stream.p_selected_program, 
778                                        p_demux_track->i_track_ID,
779                                        0 );
780     vlc_mutex_unlock( &p_input->stream.stream_lock );
781     /* Initialise ES, first language as description */
782     for( i = 0; i < 3; i++ )
783     {
784         p_demux_track->p_es->psz_desc[i] = p_demux_track->i_language[i];
785     }
786     p_demux_track->p_es->psz_desc[4] = 0;
787     
788     p_demux_track->p_es->i_stream_id = p_demux_track->i_track_ID;
789
790     /* It's a little ugly but .. there are special cases */
791     switch( p_sample->i_type )
792     {
793         case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
794         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
795             p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
796             break;
797         default:
798             p_demux_track->p_es->i_fourcc = p_sample->i_type;
799             break;
800     }
801     
802     p_demux_track->p_es->i_cat = p_demux_track->i_cat;
803     
804     i_decoder_specific_info_len = 0;
805     p_decoder_specific_info = NULL;
806
807     /* now see if esds is present and if so create a data packet 
808         with decoder_specific_info  */
809 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
810     if( ( p_esds = MP4_FindBox( p_sample, FOURCC_esds ) )&&
811         ( p_esds->data.p_esds )&&
812         ( p_decconfig ) )
813     {
814         /* First update information based on i_objectTypeIndication */
815         switch( p_decconfig->i_objectTypeIndication )
816         {
817             case( 0x20 ): /* MPEG4 VIDEO */
818                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
819                 break;
820             case( 0x40):
821                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
822                 break;
823             case( 0x60):
824             case( 0x61):
825             case( 0x62):
826             case( 0x63):
827             case( 0x64):
828             case( 0x65): /* MPEG2 video */
829                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
830                 break;
831             /* Theses are MPEG2-AAC (what is this codec ?) */
832             case( 0x66): /* main profile */
833             case( 0x67): /* Low complexity profile */
834             case( 0x68): /* Scaleable Sampling rate profile */
835                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
836                 break;
837             /* true MPEG 2 audio */
838             case( 0x69): 
839                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
840                 break;
841             case( 0x6a): /* MPEG1 video */
842                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
843                 break;
844             case( 0x6b): /* MPEG1 audio */
845                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
846                 break;
847             case( 0x6c ): /* jpeg */
848                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
849                 break;
850             default:
851                 /* Unknown entry, but don't touch i_fourcc */
852                 msg_Warn( p_input, 
853                           "objectTypeIndication(0x%x) unknow (Track ID 0x%x)",
854                           p_decconfig->i_objectTypeIndication,
855                           p_demux_track->i_track_ID );
856                 break;
857         }
858         i_decoder_specific_info_len = 
859                 p_decconfig->i_decoder_specific_info_len;
860         p_decoder_specific_info = 
861                 p_decconfig->p_decoder_specific_info;
862     }
863
864 #undef p_decconfig
865
866     /* some last initialisation */
867     /* XXX I create a bitmapinfoheader_t or 
868        waveformatex_t for each stream, up to now it's the best thing 
869        I've found but it could exist a better solution :) as something 
870        like adding some new fields in p_es ...
871
872        XXX I don't set all values, only thoses that are interesting or known
873         --> bitmapinfoheader_t : width and height 
874         --> waveformatex_t : channels, samplerate, bitspersample
875         and at the end I add p_decoder_specific_info 
876         
877         TODO set more values
878      
879      */
880
881     switch( p_demux_track->i_cat )
882     {
883         case( VIDEO_ES ):    
884             /* now create a bitmapinfoheader_t for decoder and 
885                add information found in p_esds */
886             p_init = malloc( 40 + i_decoder_specific_info_len);
887             memset( p_init, 0, 40 + i_decoder_specific_info_len);
888             MP4_Set4BytesLE( p_init, 40 + i_decoder_specific_info_len );
889             if( p_sample->data.p_sample_vide->i_width )
890             {
891                 MP4_Set4BytesLE( p_init + 4, 
892                                  p_sample->data.p_sample_vide->i_width );
893             }
894             else
895             {
896                 /* use display size */
897                 MP4_Set4BytesLE( p_init + 4, p_demux_track->i_width );
898             }
899             if( p_sample->data.p_sample_vide->i_height )
900             {
901                 MP4_Set4BytesLE( p_init + 8, 
902                                  p_sample->data.p_sample_vide->i_height );
903             }
904             else
905             {
906                 MP4_Set4BytesLE( p_init + 8, p_demux_track->i_height );
907             }
908             if( i_decoder_specific_info_len )
909             {
910                 memcpy( p_init + 40, 
911                         p_decoder_specific_info,
912                         i_decoder_specific_info_len);
913             }
914             break;
915
916         case( AUDIO_ES ):
917             p_init = malloc( 18 + i_decoder_specific_info_len);
918             memset( p_init, 0, 18 + i_decoder_specific_info_len);
919             MP4_Set2BytesLE( p_init + 2, /* i_channel */
920                              p_sample->data.p_sample_soun->i_channelcount );
921             MP4_Set4BytesLE( p_init + 4, /* samplepersec */
922                              p_sample->data.p_sample_soun->i_sampleratehi );
923             MP4_Set4BytesLE( p_init + 8, /* avgbytespersec */
924                              p_sample->data.p_sample_soun->i_channelcount *
925                                 p_sample->data.p_sample_soun->i_sampleratehi *
926                              (p_sample->data.p_sample_soun->i_samplesize/8) );
927             MP4_Set2BytesLE( p_init + 14, /* bits/sample */
928                              p_sample->data.p_sample_soun->i_samplesize );
929
930             MP4_Set2BytesLE( p_init + 16, /* i_size, specific info len*/
931                              i_decoder_specific_info_len );
932             if( i_decoder_specific_info_len )
933             {
934                 memcpy( p_init + 18, 
935                         p_decoder_specific_info,
936                         i_decoder_specific_info_len);
937             }
938             break;
939
940         default:
941             p_init = NULL;
942             break;
943     }
944
945     p_demux_track->p_es->p_demux_data = (es_sys_t *)p_init;
946     vlc_mutex_lock( &p_input->stream.stream_lock );
947     input_SelectES( p_input, p_demux_track->p_es );
948     vlc_mutex_unlock( &p_input->stream.stream_lock );
949
950     p_demux_track->b_ok = 1;
951 }
952
953 static void MP4_StopDecoder( input_thread_t *p_input,
954                              track_data_mp4_t *p_demux_track )
955 {
956     msg_Dbg( p_input, "Stopping decoder (track ID 0x%x)",
957                       p_demux_track->i_track_ID );
958
959     input_UnselectES( p_input, p_demux_track->p_es );
960     p_demux_track->p_es = NULL;
961 }
962
963 static int  MP4_ReadSample( input_thread_t *p_input,
964                             track_data_mp4_t *p_demux_track,
965                             pes_packet_t **pp_pes )
966 {
967     int i_size;
968     off_t i_pos;
969
970     data_packet_t *p_data;
971
972
973     /* this track have already reach the end */
974     if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
975     {
976         *pp_pes = NULL;
977         return( 0 );
978     }
979     /* caculate size and position for this sample */
980     i_size = p_demux_track->i_sample_size ? 
981                     p_demux_track->i_sample_size : 
982                     p_demux_track->p_sample_size[p_demux_track->i_sample];
983     /* TODO */
984     i_pos  = MP4_GetTrackPos( p_demux_track );
985
986     /* go,go go ! */
987     if( ! MP4_SeekAbsolute( p_input, i_pos ) )
988     {
989         return( 0 );
990     }
991
992     /* now create a pes */
993     if( !(*pp_pes = input_NewPES( p_input->p_method_data ) ) )
994     {
995         return( 0 );
996     }
997     /* and a data packet for the data */
998     if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
999     {
1000         input_DeletePES( p_input->p_method_data, *pp_pes );
1001         *pp_pes = NULL;
1002         return( 0 );
1003     }
1004     
1005     /* initialisation of all the field */
1006     (*pp_pes)->i_dts =
1007         (*pp_pes)->i_pts = MP4_GetTrackPTS( p_demux_track );
1008     (*pp_pes)->p_first = (*pp_pes)->p_last  = p_data;
1009     (*pp_pes)->i_nb_data = 1;
1010     (*pp_pes)->i_pes_size = i_size;
1011
1012     if( !i_size )    
1013     {
1014         return( 1 );
1015     }
1016     
1017 //    msg_Dbg( p_input, "will read %d bytes", i_size );
1018     if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
1019     {
1020         input_DeletePES( p_input->p_method_data, *pp_pes );
1021         input_DeletePacket( p_input->p_method_data, p_data );
1022         return( 0 );
1023     }
1024
1025         return( 1 );
1026 }
1027
1028
1029 static int  MP4_DecodeSample( input_thread_t *p_input,
1030                               track_data_mp4_t *p_demux_track,
1031                               pes_packet_t *p_pes )
1032 {
1033
1034     if( !p_pes )
1035     {
1036         return( 0 );
1037     }
1038
1039     /* don't forget to convert in mpeg clock */
1040     /* FIXME correct ffmpeg to use dts instead of pts that it incorrect 
1041        and, set it here ( and correct avi demux ) */
1042     p_pes->i_dts =
1043         p_pes->i_pts = input_ClockGetTS( p_input,
1044                                          p_input->stream.p_selected_program,
1045                                          p_pes->i_pts * 9/100);
1046
1047     
1048     input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes );
1049     
1050     /* now update sample position */
1051     p_demux_track->i_sample++; /* easy ;) */
1052     if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1053     {
1054         /* we have reach end of the track so free decoder stuff */
1055         MP4_StopDecoder( p_input, p_demux_track );
1056         return( 1 );
1057     }
1058     /* Have we changed chunk ? */
1059     if( p_demux_track->i_sample >=
1060             p_demux_track->chunk[p_demux_track->i_chunk].i_sample_first +
1061                 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_count )
1062     {
1063         /* we haven't reached the end of the track, so see if we 
1064            have to change the decoder for the next frame because 
1065            i_sample_description_index have changed */
1066
1067         p_demux_track->i_chunk++;
1068         if( p_demux_track->chunk[p_demux_track->i_chunk-1].i_sample_description_index 
1069               != p_demux_track->chunk[p_demux_track->i_chunk].i_sample_description_index  )
1070         {
1071             /* FIXME */
1072             msg_Warn( p_input, 
1073                       "SampleEntry have changed, starting a new decoder" );
1074             MP4_StopDecoder( p_input, p_demux_track );
1075             MP4_StartDecoder( p_input, p_demux_track );
1076         }
1077     }
1078
1079     
1080     return( 1 );
1081 }
1082
1083
1084