* mp4: more mov fields are parsed.
[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.22 2003/04/22 08:51:28 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 #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 #include "codecs.h"
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 /* New input could have something like that... */
45 static int   MP4Seek     ( input_thread_t *, mtime_t );
46
47 #define MP4End(a) __MP4End(VLC_OBJECT(a))
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();
53     set_description( _("MP4 demuxer") );
54     set_capability( "demux", 242 );
55     set_callbacks( MP4Init, __MP4End );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * Declaration of local function
60  *****************************************************************************/
61
62 static void MP4_TrackCreate ( input_thread_t *,
63                               track_data_mp4_t *,
64                               MP4_Box_t  * );
65 static void MP4_TrackDestroy( input_thread_t *,
66                               track_data_mp4_t * );
67
68 static int  MP4_TrackSelect ( input_thread_t *,
69                               track_data_mp4_t *,
70                               mtime_t );
71 static void MP4_TrackUnselect(input_thread_t *,
72                               track_data_mp4_t * );
73
74 static int  MP4_TrackSeek   ( input_thread_t *,
75                               track_data_mp4_t *,
76                               mtime_t );
77
78 static uint64_t MP4_GetTrackPos( track_data_mp4_t * );
79 static int  MP4_TrackSampleSize( track_data_mp4_t * );
80 static int  MP4_TrackNextSample( input_thread_t *,
81                                  track_data_mp4_t * );
82
83 #define MP4_Set4BytesLE( p, dw ) \
84     *((uint8_t*)p)   = ( (dw)&0xff ); \
85     *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff ); \
86     *((uint8_t*)p+2) = ( ((dw)>>16)&0xff ); \
87     *((uint8_t*)p+3) = ( ((dw)>>24)&0xff )
88
89 #define MP4_Set2BytesLE( p, dw ) \
90     *((uint8_t*)p) = ( (dw)&0xff ); \
91     *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff )
92
93 #define FREE( p ) \
94     if( p ) { free( p ); (p) = NULL;}
95
96 /*****************************************************************************
97  * MP4Init: check file and initializes MP4 structures
98  *****************************************************************************/
99 static int MP4Init( vlc_object_t * p_this )
100 {
101     input_thread_t  *p_input = (input_thread_t *)p_this;
102     uint8_t         *p_peek;
103
104     demux_sys_t     *p_demux;
105
106     MP4_Box_t       *p_ftyp;
107
108
109     MP4_Box_t       *p_mvhd;
110     MP4_Box_t       *p_trak;
111
112     unsigned int    i;
113     /* I need to seek */
114     if( !p_input->stream.b_seekable )
115     {
116         msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
117         return( -1 );
118
119     }
120     /* Initialize access plug-in structures. */
121     if( p_input->i_mtu == 0 )
122     {
123         /* Improve speed. */
124         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
125     }
126
127     p_input->pf_demux = MP4Demux;
128
129     /* a little test to see if it could be a mp4 */
130     if( input_Peek( p_input, &p_peek, 8 ) < 8 )
131     {
132         msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
133         return( -1 );
134     }
135
136
137     switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
138     {
139         case( FOURCC_ftyp ):
140         case( FOURCC_moov ):
141         case( FOURCC_moof ):
142         case( FOURCC_mdat ):
143         case( FOURCC_udta ):
144         case( FOURCC_free ):
145         case( FOURCC_skip ):
146         case( FOURCC_wide ):
147             break;
148          default:
149             msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
150             return( -1 );
151     }
152
153     /* create our structure that will contains all data */
154     if( !( p_input->p_demux_data =
155                 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
156     {
157         msg_Err( p_input, "out of memory" );
158         return( -1 );
159     }
160     memset( p_demux, 0, sizeof( demux_sys_t ) );
161     p_input->p_demux_data = p_demux;
162
163
164     /* Now load all boxes ( except raw data ) */
165     if( !MP4_BoxGetRoot( p_input, &p_demux->box_root ) )
166     {
167         msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
168         return( -1 );
169     }
170
171     MP4_BoxDumpStructure( p_input, &p_demux->box_root );
172
173     if( ( p_ftyp = MP4_BoxGet( &p_demux->box_root, "/ftyp" ) ) )
174     {
175         switch( p_ftyp->data.p_ftyp->i_major_brand )
176         {
177             case( FOURCC_isom ):
178                 msg_Dbg( p_input,
179                          "ISO Media file (isom) version %d.",
180                          p_ftyp->data.p_ftyp->i_minor_version );
181                 break;
182             default:
183                 msg_Dbg( p_input,
184                          "unrecognized major file specification (%4.4s).",
185                           (char*)&p_ftyp->data.p_ftyp->i_major_brand );
186                 break;
187         }
188     }
189     else
190     {
191         msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
192     }
193
194     /* the file need to have one moov box */
195     if( MP4_BoxCount( &p_demux->box_root, "/moov" ) != 1 )
196     {
197         msg_Err( p_input,
198                  "MP4 plugin discarded (%d moov boxes)",
199                  MP4_BoxCount( &p_demux->box_root, "/moov" ) );
200 //        MP4End( p_input );
201 //        return( -1 );
202     }
203
204     if( !(p_mvhd = MP4_BoxGet( &p_demux->box_root, "/moov/mvhd" ) ) )
205     {
206         msg_Err( p_input, "cannot find /moov/mvhd" );
207         MP4End( p_input );
208         return( -1 );
209     }
210     else
211     {
212         p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
213         p_demux->i_duration = p_mvhd->data.p_mvhd->i_duration;
214     }
215
216     if( !( p_demux->i_tracks =
217                 MP4_BoxCount( &p_demux->box_root, "/moov/trak" ) ) )
218     {
219         msg_Err( p_input, "cannot find any /moov/trak" );
220         MP4End( p_input );
221         return( -1 );
222     }
223     msg_Dbg( p_input, "find %d track%c",
224                         p_demux->i_tracks,
225                         p_demux->i_tracks ? 's':' ' );
226
227     /*  create one program */
228     vlc_mutex_lock( &p_input->stream.stream_lock );
229     if( input_InitStream( p_input, 0 ) == -1)
230     {
231         vlc_mutex_unlock( &p_input->stream.stream_lock );
232         msg_Err( p_input, "cannot init stream" );
233         MP4End( p_input );
234         return( -1 );
235     }
236     /* Needed to create program _before_ MP4_TrackCreate */
237     if( input_AddProgram( p_input, 0, 0) == NULL )
238     {
239         vlc_mutex_unlock( &p_input->stream.stream_lock );
240         msg_Err( p_input, "cannot add program" );
241         MP4End( p_input );
242         return( -1 );
243     }
244     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
245     /* XXX beurk and beurk, see MP4Demux and MP4Seek */
246     if( p_demux->i_duration/p_demux->i_timescale > 0 )
247     {
248         p_input->stream.i_mux_rate =
249             p_input->stream.p_selected_area->i_size / 50 /
250             ( p_demux->i_duration / p_demux->i_timescale );
251     }
252     else
253     {
254         p_input->stream.i_mux_rate = 0;
255     }
256     vlc_mutex_unlock( &p_input->stream.stream_lock );
257
258
259     /* allocate memory */
260     p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
261
262     /* now process each track and extract all usefull informations */
263     for( i = 0; i < p_demux->i_tracks; i++ )
264     {
265         p_trak = MP4_BoxGet( &p_demux->box_root, "/moov/trak[%d]", i );
266         MP4_TrackCreate( p_input, &p_demux->track[i], p_trak );
267
268         if( p_demux->track[i].b_ok )
269         {
270             char *psz_cat;
271             switch( p_demux->track[i].i_cat )
272             {
273                 case( VIDEO_ES ):
274                     psz_cat = "video";
275                     break;
276                 case( AUDIO_ES ):
277                     psz_cat = "audio";
278                     break;
279                 default:
280                     psz_cat = "unknown";
281                     break;
282             }
283
284             msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %c%c%c",
285                             p_demux->track[i].i_track_ID,
286                             psz_cat,
287                             p_demux->track[i].b_enable ? "enable":"disable",
288                             p_demux->track[i].i_language[0],
289                             p_demux->track[i].i_language[1],
290                             p_demux->track[i].i_language[2] );
291         }
292         else
293         {
294             msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_demux->track[i].i_track_ID );
295         }
296
297     }
298
299     for( i = 0; i < p_demux->i_tracks; i++ )
300     {
301         /* start decoder for this track if enable by default*/
302         if( p_demux->track[i].b_ok && p_demux->track[i].b_enable )
303         {
304             MP4_TrackSelect( p_input, &p_demux->track[i], 0 );
305         }
306     }
307
308     vlc_mutex_lock( &p_input->stream.stream_lock );
309     p_input->stream.p_selected_program->b_is_ok = 1;
310     vlc_mutex_unlock( &p_input->stream.stream_lock );
311
312     return( 0 );
313
314 }
315
316 /*****************************************************************************
317  * MP4Demux: read packet and send them to decoders
318  *****************************************************************************
319  * TODO check for newly selected track (ie audio upt to now )
320  *****************************************************************************/
321 static int MP4Demux( input_thread_t *p_input )
322 {
323     demux_sys_t *p_demux = p_input->p_demux_data;
324     unsigned int i_track;
325
326
327     unsigned int i_track_selected;
328     vlc_bool_t   b_video;
329     vlc_bool_t   b_play_audio;
330
331     /* check for newly selected/unselected track */
332     for( i_track = 0, i_track_selected = 0, b_video = VLC_FALSE;
333             i_track <  p_demux->i_tracks; i_track++ )
334     {
335 #define track   p_demux->track[i_track]
336         if( track.b_selected && track.i_sample >= track.i_sample_count )
337         {
338             msg_Warn( p_input, "track[0x%x] will be disabled", track.i_track_ID );
339             MP4_TrackUnselect( p_input, &track );
340         }
341         else if( track.b_ok )
342         {
343             if( track.b_selected && track.p_es->p_decoder_fifo == NULL )
344             {
345                 MP4_TrackUnselect( p_input, &track );
346             }
347             else if( !track.b_selected && track.p_es->p_decoder_fifo != NULL )
348             {
349                 MP4_TrackSelect( p_input, &track, MP4_GetMoviePTS( p_demux ) );
350             }
351
352             if( track.b_selected )
353             {
354                 i_track_selected++;
355
356                 if( track.i_cat == VIDEO_ES )
357                 {
358                     b_video = VLC_TRUE;
359                 }
360             }
361         }
362 #undef  track
363     }
364
365     if( i_track_selected <= 0 )
366     {
367         msg_Warn( p_input, "no track selected, exiting..." );
368         return( 0 );
369     }
370
371
372     /* XXX beurk, beuRK and BEURK,
373        but only way I've found to detect seek from interface */
374     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
375     {
376         mtime_t i_date;
377
378         /* first wait for empty buffer, arbitrary time FIXME */
379         msleep( DEFAULT_PTS_DELAY );
380         /* *** calculate new date *** */
381
382         i_date = (mtime_t)1000000 *
383                  (mtime_t)p_demux->i_duration /
384                  (mtime_t)p_demux->i_timescale *
385                  (mtime_t)MP4_TellAbsolute( p_input ) /
386                  (mtime_t)p_input->stream.p_selected_area->i_size;
387         MP4Seek( p_input, i_date );
388     }
389
390     /* first wait for the good time to read a packet */
391     input_ClockManageRef( p_input,
392                           p_input->stream.p_selected_program,
393                           p_demux->i_pcr );
394
395
396     /* update pcr XXX in mpeg scale so in 90000 unit/s */
397     p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
398
399
400     /* we will read 100ms for each stream so ...*/
401     p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
402
403
404     /* *** send audio data to decoder if rate == DEFAULT_RATE or no video *** */
405     vlc_mutex_lock( &p_input->stream.stream_lock );
406     if( p_input->stream.control.i_rate == DEFAULT_RATE || !b_video )
407     {
408         b_play_audio = VLC_TRUE;
409     }
410     else
411     {
412         b_play_audio = VLC_FALSE;
413     }
414     vlc_mutex_unlock( &p_input->stream.stream_lock );
415
416     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
417     {
418 #define track p_demux->track[i_track]
419         if( !track.b_ok ||
420             !track.b_selected ||
421             MP4_GetTrackPTS( &track ) >= MP4_GetMoviePTS( p_demux ) )
422         {
423             continue;
424         }
425         while( MP4_GetTrackPTS( &track ) < MP4_GetMoviePTS( p_demux ) )
426         {
427
428             if( !b_play_audio && track.i_cat == AUDIO_ES )
429             {
430                 if( MP4_TrackNextSample( p_input, &track ) )
431                 {
432                     break;
433                 }
434             }
435             else
436             {
437                 size_t i_size;
438                 off_t i_pos;
439
440                 data_packet_t *p_data;
441                 pes_packet_t *p_pes;
442
443                 /* caculate size and position for this sample */
444                 i_size = MP4_TrackSampleSize( &track );
445
446                 i_pos  = MP4_GetTrackPos( &track );
447
448                 //msg_Dbg( p_input, "stream %d size=%6d pos=%8lld",  i_track, i_size, i_pos );
449
450                 /* go,go go ! */
451                 if( ! MP4_SeekAbsolute( p_input, i_pos ) )
452                 {
453                     msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
454                     MP4_TrackUnselect( p_input, &track );
455                     break;
456                 }
457
458
459                 /* now create a pes */
460                 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
461                 {
462                     break;
463                 }
464                 /* and a data packet for the data */
465                 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
466                 {
467                     input_DeletePES( p_input->p_method_data, p_pes );
468                     break;
469                 }
470                 p_data->p_payload_end = p_data->p_payload_start + i_size;
471
472                 /* initialisation of all the field */
473                 p_pes->i_dts = p_pes->i_pts = 0;
474                 p_pes->p_first = p_pes->p_last  = p_data;
475                 p_pes->i_nb_data = 1;
476                 p_pes->i_pes_size = i_size;
477                 if( i_size > 0 )
478                 {
479                     if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
480                     {
481                         input_DeletePES( p_input->p_method_data, p_pes );
482
483                         msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
484                         MP4_TrackUnselect( p_input, &track );
485                         break;
486                     }
487                 }
488
489                 p_pes->i_dts =
490                     p_pes->i_pts = input_ClockGetTS( p_input,
491                                                      p_input->stream.p_selected_program,
492                                                      MP4_GetTrackPTS( &track ) * 9/100);
493
494                 if( track.p_es->p_decoder_fifo )
495                 {
496                     input_DecodePES( track.p_es->p_decoder_fifo, p_pes );
497                 }
498                 else
499                 {
500                     input_DeletePES( p_input->p_method_data, p_pes );
501                 }
502
503                 if( MP4_TrackNextSample( p_input, &track ) )
504                 {
505                     break;
506                 }
507             }
508         }
509 #undef track
510     }
511
512     return( 1 );
513 }
514 /*****************************************************************************
515  * MP4Seek: Got to i_date
516  ******************************************************************************/
517 static int   MP4Seek     ( input_thread_t *p_input, mtime_t i_date )
518 {
519     demux_sys_t *p_demux = p_input->p_demux_data;
520     unsigned int i_track;
521     /* First update update global time */
522     p_demux->i_time = i_date * p_demux->i_timescale / 1000000;
523     p_demux->i_pcr  = i_date* 9 / 100;
524
525     /* Now for each stream try to go to this time */
526     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
527     {
528 #define track p_demux->track[i_track]
529         if( track.b_ok && track.b_selected )
530         {
531             MP4_TrackSeek( p_input, &track, i_date );
532         }
533 #undef  track
534     }
535     return( 1 );
536 }
537
538 /*****************************************************************************
539  * MP4End: frees unused data
540  *****************************************************************************/
541 static void __MP4End ( vlc_object_t * p_this )
542 {
543     unsigned int i_track;
544     input_thread_t *  p_input = (input_thread_t *)p_this;
545     demux_sys_t *p_demux = p_input->p_demux_data;
546
547     msg_Dbg( p_input, "freeing all memory" );
548     MP4_BoxFree( p_input, &p_demux->box_root );
549     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
550     {
551         MP4_TrackDestroy( p_input, &p_demux->track[i_track] );
552     }
553     FREE( p_demux->track );
554
555     FREE( p_input->p_demux_data );
556 }
557
558
559 /****************************************************************************
560  * Local functions, specific to vlc
561  ****************************************************************************/
562
563 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
564 static int TrackCreateChunksIndex( input_thread_t *p_input,
565                                    track_data_mp4_t *p_demux_track )
566 {
567     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
568     MP4_Box_t *p_stsc;
569
570     unsigned int i_chunk;
571     unsigned int i_index, i_last;
572
573     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
574           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
575         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
576     {
577         return( VLC_EGENERIC );
578     }
579
580     p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
581     if( !p_demux_track->i_chunk_count )
582     {
583         msg_Warn( p_input, "no chunk defined" );
584         return( VLC_EGENERIC );
585     }
586     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
587                                    sizeof( chunk_data_mp4_t ) );
588
589     /* first we read chunk offset */
590     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
591     {
592         p_demux_track->chunk[i_chunk].i_offset =
593                 p_co64->data.p_co64->i_chunk_offset[i_chunk];
594     }
595
596     /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
597         to be used for the sample XXX begin to 1
598         We construct it begining at the end */
599     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
600     i_index = p_stsc->data.p_stsc->i_entry_count;
601     if( !i_index )
602     {
603         msg_Warn( p_input, "cannot read chunk table or table empty" );
604         return( VLC_EGENERIC );
605     }
606
607     while( i_index )
608     {
609         i_index--;
610         for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
611                 i_chunk < i_last; i_chunk++ )
612         {
613             p_demux_track->chunk[i_chunk].i_sample_description_index =
614                     p_stsc->data.p_stsc->i_sample_description_index[i_index];
615             p_demux_track->chunk[i_chunk].i_sample_count =
616                     p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
617         }
618         i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
619     }
620
621     p_demux_track->chunk[0].i_sample_first = 0;
622     for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
623     {
624         p_demux_track->chunk[i_chunk].i_sample_first =
625             p_demux_track->chunk[i_chunk-1].i_sample_first +
626                 p_demux_track->chunk[i_chunk-1].i_sample_count;
627     }
628
629     msg_Dbg( p_input,
630              "track[Id 0x%x] read %d chunk",
631              p_demux_track->i_track_ID,
632             p_demux_track->i_chunk_count );
633
634     return( VLC_SUCCESS );
635 }
636 static int TrackCreateSamplesIndex( input_thread_t *p_input,
637                                     track_data_mp4_t *p_demux_track )
638 {
639     MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
640                           ctts make same mapping but for composition time,
641                           not yet used and probably not usefull */
642     MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
643                           that uses a compressed form FIXME make them in libmp4
644                           as a unique type */
645     /* TODO use also stss and stsh table for seeking */
646     /* FIXME use edit table */
647     int64_t i_sample;
648     int64_t i_chunk;
649
650     int64_t i_index;
651     int64_t i_index_sample_used;
652
653     int64_t i_last_dts;
654
655     p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
656     p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
657
658     if( ( !p_stts )||( !p_stsz ) )
659     {
660         msg_Warn( p_input, "cannot read sample table" );
661         return( VLC_EGENERIC );
662     }
663
664     p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
665
666
667     /* for sample size, there are 2 case */
668     if( p_stsz->data.p_stsz->i_sample_size )
669     {
670         /* 1: all sample have the same size, so no need to construct a table */
671         p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
672         p_demux_track->p_sample_size = NULL;
673     }
674     else
675     {
676         /* 2: each sample can have a different size */
677         p_demux_track->i_sample_size = 0;
678         p_demux_track->p_sample_size =
679             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
680
681         for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
682         {
683             p_demux_track->p_sample_size[i_sample] =
684                     p_stsz->data.p_stsz->i_entry_size[i_sample];
685         }
686     }
687     /* we have extract all information from stsz,
688         now use stts */
689
690     /* if we don't want to waste too much memory, we can't expand
691        the box !, so each chunk will contain an "extract" of this table
692        for fast research */
693
694     i_last_dts = 0;
695     i_index = 0; i_index_sample_used =0;
696
697     /* create and init last data for each chunk */
698     for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
699     {
700
701         int64_t i_entry, i_sample_count, i;
702         /* save last dts */
703         p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
704     /* count how many entries needed for this chunk
705        for p_sample_delta_dts and p_sample_count_dts */
706
707         i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
708
709         i_entry = 0;
710         while( i_sample_count > 0 )
711         {
712             i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
713             if( i_entry == 0 )
714             {
715                 i_sample_count += i_index_sample_used; /* don't count already used sample
716                                                    int this entry */
717             }
718             i_entry++;
719         }
720
721         /* allocate them */
722         p_demux_track->chunk[i_chunk].p_sample_count_dts =
723             calloc( i_entry, sizeof( uint32_t ) );
724         p_demux_track->chunk[i_chunk].p_sample_delta_dts =
725             calloc( i_entry, sizeof( uint32_t ) );
726
727         /* now copy */
728         i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
729         for( i = 0; i < i_entry; i++ )
730         {
731             int64_t i_used;
732             int64_t i_rest;
733
734             i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
735
736             i_used = __MIN( i_rest, i_sample_count );
737
738             i_index_sample_used += i_used;
739             i_sample_count -= i_used;
740
741             p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
742
743             p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
744                         p_stts->data.p_stts->i_sample_delta[i_index];
745
746             i_last_dts += i_used *
747                     p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
748
749             if( i_index_sample_used >=
750                              p_stts->data.p_stts->i_sample_count[i_index] )
751             {
752
753                 i_index++;
754                 i_index_sample_used = 0;
755             }
756         }
757
758     }
759
760     msg_Dbg( p_input,
761              "track[Id 0x%x] read %d samples length:"I64Fd"s",
762              p_demux_track->i_track_ID,
763              p_demux_track->i_sample_count,
764              i_last_dts / p_demux_track->i_timescale );
765
766     return( VLC_SUCCESS );
767 }
768
769 /*
770  * TrackCreateES:
771  *  Create ES and PES to init decoder if needed, for a track starting at i_chunk
772  */
773 static int  TrackCreateES   ( input_thread_t   *p_input,
774                               track_data_mp4_t *p_track,
775                               unsigned int     i_chunk,
776                               es_descriptor_t  **pp_es,
777                               pes_packet_t     **pp_pes )
778 {
779     MP4_Box_t *  p_sample;
780     unsigned int i;
781
782     unsigned int i_decoder_specific_info_len;
783     uint8_t *    p_decoder_specific_info;
784
785     es_descriptor_t *p_es;
786     pes_packet_t    *p_pes_init;
787
788     uint8_t             *p_init;
789     BITMAPINFOHEADER    *p_bih;
790     WAVEFORMATEX        *p_wf;
791
792     MP4_Box_t   *p_esds;
793
794     if( !p_track->chunk[i_chunk].i_sample_description_index )
795     {
796         msg_Warn( p_input,
797                   "invalid SampleEntry index (track[Id 0x%x])",
798                   p_track->i_track_ID );
799         return( VLC_EGENERIC );
800     }
801
802     p_sample = MP4_BoxGet(  p_track->p_stsd,
803                             "[%d]",
804                 p_track->chunk[i_chunk].i_sample_description_index - 1 );
805
806     if( !p_sample || !p_sample->data.p_data )
807     {
808         msg_Warn( p_input,
809                   "cannot find SampleEntry (track[Id 0x%x])",
810                   p_track->i_track_ID );
811         return( VLC_EGENERIC );
812     }
813
814     p_track->p_sample = p_sample;
815
816     if( p_track->i_sample_size == 1 )
817     {
818         MP4_Box_data_sample_soun_t *p_soun;
819
820         p_soun = p_sample->data.p_sample_soun;
821
822         if( p_soun->i_qt_version == 0 )
823         {
824             switch( p_sample->i_type )
825             {
826                 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
827                     p_soun->i_qt_version = 1;
828                     p_soun->i_sample_per_packet = 64;
829                     p_soun->i_bytes_per_packet  = 34;
830                     p_soun->i_bytes_per_frame   = 34 * p_soun->i_channelcount;
831                     p_soun->i_bytes_per_sample  = 2;
832                     break;
833                 default:
834                     break;
835             }
836         }
837     }
838
839
840     vlc_mutex_lock( &p_input->stream.stream_lock );
841     p_es = input_AddES( p_input,
842                         p_input->stream.p_selected_program,
843                         p_track->i_track_ID,
844                         0 );
845     vlc_mutex_unlock( &p_input->stream.stream_lock );
846     /* Initialise ES, first language as description */
847     for( i = 0; i < 3; i++ )
848     {
849         p_es->psz_desc[i] = p_track->i_language[i];
850     }
851     p_es->psz_desc[3] = '\0';
852
853     p_es->i_stream_id = p_track->i_track_ID;
854
855     /* It's a little ugly but .. there are special cases */
856     switch( p_sample->i_type )
857     {
858         case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
859         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
860             p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
861             break;
862         case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
863             p_es->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
864             break;
865         default:
866             p_es->i_fourcc = p_sample->i_type;
867             break;
868     }
869
870     p_es->i_cat = p_track->i_cat;
871
872     i_decoder_specific_info_len = 0;
873     p_decoder_specific_info = NULL;
874     p_pes_init = NULL;
875
876     /* now see if esds is present and if so create a data packet
877         with decoder_specific_info  */
878 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
879     if( ( p_esds = MP4_BoxGet( p_sample, "esds" ) )&&
880         ( p_esds->data.p_esds )&&
881         ( p_decconfig ) )
882     {
883         /* First update information based on i_objectTypeIndication */
884         switch( p_decconfig->i_objectTypeIndication )
885         {
886             case( 0x20 ): /* MPEG4 VIDEO */
887                 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
888                 break;
889             case( 0x40):
890                 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
891                 break;
892             case( 0x60):
893             case( 0x61):
894             case( 0x62):
895             case( 0x63):
896             case( 0x64):
897             case( 0x65): /* MPEG2 video */
898                 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
899                 break;
900             /* Theses are MPEG2-AAC */
901             case( 0x66): /* main profile */
902             case( 0x67): /* Low complexity profile */
903             case( 0x68): /* Scaleable Sampling rate profile */
904                 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
905                 break;
906             /* true MPEG 2 audio */
907             case( 0x69):
908                 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
909                 break;
910             case( 0x6a): /* MPEG1 video */
911                 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
912                 break;
913             case( 0x6b): /* MPEG1 audio */
914                 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
915                 break;
916             case( 0x6c ): /* jpeg */
917                 p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
918                 break;
919             default:
920                 /* Unknown entry, but don't touch i_fourcc */
921                 msg_Warn( p_input,
922                           "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
923                           p_decconfig->i_objectTypeIndication,
924                           p_track->i_track_ID );
925                 break;
926         }
927         i_decoder_specific_info_len =
928                 p_decconfig->i_decoder_specific_info_len;
929         p_decoder_specific_info =
930                 p_decconfig->p_decoder_specific_info;
931     }
932     else
933     {
934         switch( p_sample->i_type )
935         {
936             /* qt decoder, send the complete chunk */
937             case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
938             case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
939                 i_decoder_specific_info_len = p_sample->data.p_sample_vide->i_qt_image_description;
940                 p_decoder_specific_info     = p_sample->data.p_sample_vide->p_qt_image_description;
941                 break;
942             case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
943             case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
944             case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
945                 i_decoder_specific_info_len = p_sample->data.p_sample_soun->i_qt_description;
946                 p_decoder_specific_info     = p_sample->data.p_sample_soun->p_qt_description;
947                 break;
948             default:
949                 break;
950         }
951     }
952
953 #undef p_decconfig
954
955     /* some last initialisation */
956     /* XXX I create a bitmapinfoheader_t or
957        waveformatex_t for each stream, up to now it's the best thing
958        I've found but it could exist a better solution :) as something
959        like adding some new fields in p_es ...
960
961        XXX I don't set all values, only thoses that are interesting or known
962         --> bitmapinfoheader_t : width and height
963         --> waveformatex_t : channels, samplerate, bitspersample
964         and at the end I add p_decoder_specific_info
965
966         TODO set more values
967
968      */
969
970     switch( p_track->i_cat )
971     {
972         case( VIDEO_ES ):
973             /* now create a bitmapinfoheader_t for decoder and
974                add information found in p_esds */
975             /* XXX XXX + 16 are for avoid segfault when ffmpeg access beyong the data */
976             p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len + 16 );
977             p_bih = (BITMAPINFOHEADER*)p_init;
978
979             p_bih->biSize     = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
980             p_bih->biWidth    = p_sample->data.p_sample_vide->i_width;
981             p_bih->biHeight   = p_sample->data.p_sample_vide->i_height;
982             p_bih->biPlanes   = 1;      // FIXME
983             p_bih->biBitCount = 0;      // FIXME
984             p_bih->biCompression   = 0; // FIXME
985             p_bih->biSizeImage     = 0; // FIXME
986             p_bih->biXPelsPerMeter = 0; // FIXME
987             p_bih->biYPelsPerMeter = 0; // FIXME
988             p_bih->biClrUsed       = 0; // FIXME
989             p_bih->biClrImportant  = 0; // FIXME
990
991             if( p_bih->biWidth == 0 )
992             {
993                 // fall on display size
994                 p_bih->biWidth = p_track->i_width;
995             }
996             if( p_bih->biHeight == 0 )
997             {
998                 // fall on display size
999                 p_bih->biHeight = p_track->i_height;
1000             }
1001
1002             if( i_decoder_specific_info_len )
1003             {
1004                 data_packet_t   *p_data;
1005
1006                 memcpy( p_init + sizeof( BITMAPINFOHEADER ),
1007                         p_decoder_specific_info,
1008                         i_decoder_specific_info_len);
1009
1010                 /* If stream is mpeg4 video we send specific_info,
1011                    as it's needed to decode it (vol) */
1012                 switch( p_es->i_fourcc )
1013                 {
1014                     case VLC_FOURCC( 'm','p','4','v' ):
1015                     case VLC_FOURCC( 'D','I','V','X' ):
1016                     case VLC_FOURCC( 'd','i','v','x' ):
1017                         p_pes_init = input_NewPES( p_input->p_method_data );
1018                         p_data = input_NewPacket( p_input->p_method_data,
1019                                                   i_decoder_specific_info_len);
1020                         p_data->p_payload_end = p_data->p_payload_start + i_decoder_specific_info_len;
1021
1022                         memcpy( p_data->p_payload_start,
1023                                 p_decoder_specific_info,
1024                                 i_decoder_specific_info_len );
1025                         p_pes_init->i_dts = p_pes_init->i_pts = 0;
1026                         p_pes_init->p_first = p_pes_init->p_last = p_data;
1027                         p_pes_init->i_nb_data = 1;
1028                         p_pes_init->i_pes_size = i_decoder_specific_info_len;
1029                         break;
1030                     default:
1031                         break;
1032                 }
1033
1034             }
1035             break;
1036
1037         case( AUDIO_ES ):
1038             p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len + 16 );
1039             p_wf = (WAVEFORMATEX*)p_init;
1040
1041             p_wf->wFormatTag = 1;
1042             p_wf->nChannels = p_sample->data.p_sample_soun->i_channelcount;
1043             p_wf->nSamplesPerSec = p_sample->data.p_sample_soun->i_sampleratehi;
1044             p_wf->nAvgBytesPerSec = p_sample->data.p_sample_soun->i_channelcount *
1045                                     p_sample->data.p_sample_soun->i_sampleratehi *
1046                                     p_sample->data.p_sample_soun->i_samplesize / 8;
1047             p_wf->nBlockAlign = 0;
1048             p_wf->wBitsPerSample = p_sample->data.p_sample_soun->i_samplesize;
1049             p_wf->cbSize = i_decoder_specific_info_len;
1050
1051             if( i_decoder_specific_info_len )
1052             {
1053                 memcpy( p_init + sizeof( WAVEFORMATEX ),
1054                         p_decoder_specific_info,
1055                         i_decoder_specific_info_len);
1056             }
1057
1058             break;
1059
1060         default:
1061             p_init = NULL;
1062             break;
1063     }
1064     if( p_es->i_cat == AUDIO_ES )
1065     {
1066         p_es->p_bitmapinfoheader = NULL;
1067         p_es->p_waveformatex     = (void*)p_init;
1068     }
1069     else if( p_es->i_cat == VIDEO_ES )
1070     {
1071         p_es->p_bitmapinfoheader = (void*)p_init;
1072         p_es->p_waveformatex     = NULL;
1073     }
1074
1075     *pp_es = p_es;
1076     *pp_pes = p_pes_init;
1077     return( VLC_SUCCESS );
1078 }
1079
1080 /* given a time it return sample/chunk */
1081 static int  TrackTimeToSampleChunk( input_thread_t *p_input,
1082                                     track_data_mp4_t *p_track,
1083                                     uint64_t i_start,
1084                                     uint32_t *pi_chunk,
1085                                     uint32_t *pi_sample )
1086 {
1087     MP4_Box_t    *p_stss;
1088     uint64_t     i_dts;
1089     unsigned int i_sample;
1090     unsigned int i_chunk;
1091     int          i_index;
1092
1093     /* convert absolute time to in timescale unit */
1094     i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1095
1096     /* FIXME see if it's needed to check p_track->i_chunk_count */
1097     if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1098     {
1099         return( VLC_EGENERIC );
1100     }
1101
1102     /* we start from sample 0/chunk 0, hope it won't take too much time */
1103     /* *** find good chunk *** */
1104     for( i_chunk = 0; ; i_chunk++ )
1105     {
1106         if( i_chunk + 1 >= p_track->i_chunk_count )
1107         {
1108             /* at the end and can't check if i_start in this chunk,
1109                it will be check while searching i_sample */
1110             i_chunk = p_track->i_chunk_count - 1;
1111             break;
1112         }
1113
1114         if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1115             i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
1116         {
1117             break;
1118         }
1119     }
1120
1121     /* *** find sample in the chunk *** */
1122     i_sample = p_track->chunk[i_chunk].i_sample_first;
1123     i_dts    = p_track->chunk[i_chunk].i_first_dts;
1124     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1125     {
1126         if( i_dts +
1127             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1128             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1129         {
1130             i_dts    +=
1131                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1132                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1133
1134             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1135             i_index++;
1136         }
1137         else
1138         {
1139             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1140             {
1141                 break;
1142             }
1143             i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1144             break;
1145         }
1146     }
1147
1148     if( i_sample >= p_track->i_sample_count )
1149     {
1150         msg_Warn( p_input,
1151                   "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1152                   p_track->i_track_ID, i_chunk, i_sample );
1153         return( VLC_EGENERIC );
1154     }
1155
1156
1157     /* *** Try to find nearest sync points *** */
1158     if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1159     {
1160         unsigned int i_index;
1161         msg_Dbg( p_input,
1162                     "track[Id 0x%x] using Sync Sample Box (stss)",
1163                     p_track->i_track_ID );
1164         for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1165         {
1166             if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1167             {
1168                 if( i_index > 0 )
1169                 {
1170                     msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1171                             i_sample,
1172                             p_stss->data.p_stss->i_sample_number[i_index-1] );
1173                     i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1174                     /* new i_sample is less than old so i_chunk can only decreased */
1175                     while( i_chunk > 0 &&
1176                             i_sample < p_track->chunk[i_chunk].i_sample_first )
1177                     {
1178                         i_chunk--;
1179                     }
1180                 }
1181                 else
1182                 {
1183                     msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1184                             i_sample,
1185                             p_stss->data.p_stss->i_sample_number[i_index] );
1186                     i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1187                     /* new i_sample is more than old so i_chunk can only increased */
1188                     while( i_chunk < p_track->i_chunk_count - 1 &&
1189                            i_sample >= p_track->chunk[i_chunk].i_sample_first +
1190                                                 p_track->chunk[i_chunk].i_sample_count )
1191                     {
1192                         i_chunk++;
1193                     }
1194                 }
1195                 break;
1196             }
1197         }
1198     }
1199     else
1200     {
1201         msg_Dbg( p_input,
1202                     "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1203                     p_track->i_track_ID );
1204     }
1205
1206     if( pi_chunk  ) *pi_chunk  = i_chunk;
1207     if( pi_sample ) *pi_sample = i_sample;
1208     return( VLC_SUCCESS );
1209 }
1210
1211 static int  TrackGotoChunkSample( input_thread_t   *p_input,
1212                                   track_data_mp4_t *p_track,
1213                                   unsigned int     i_chunk,
1214                                   unsigned int     i_sample )
1215 {
1216     /* now see if actual es is ok */
1217     if( p_track->i_chunk < 0 ||
1218         p_track->i_chunk >= p_track->i_chunk_count ||
1219         p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1220             p_track->chunk[i_chunk].i_sample_description_index )
1221     {
1222         msg_Warn( p_input, "Recreate ES" );
1223
1224         /* no :( recreate es */
1225         vlc_mutex_lock( &p_input->stream.stream_lock );
1226         input_DelES( p_input, p_track->p_es );
1227         vlc_mutex_unlock( &p_input->stream.stream_lock );
1228
1229         if( p_track->p_pes_init )
1230         {
1231             input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1232         }
1233
1234         if( TrackCreateES( p_input,
1235                            p_track, i_chunk,
1236                            &p_track->p_es,
1237                            &p_track->p_pes_init ) )
1238         {
1239             msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1240                      p_track->i_track_ID );
1241
1242             p_track->b_ok       = VLC_FALSE;
1243             p_track->b_selected = VLC_FALSE;
1244             return( VLC_EGENERIC );
1245         }
1246     }
1247
1248     /* select again the new decoder */
1249     if( p_track->b_selected && p_track->p_es && p_track->p_es->p_decoder_fifo == NULL )
1250     {
1251         vlc_mutex_lock( &p_input->stream.stream_lock );
1252         input_SelectES( p_input, p_track->p_es );
1253         vlc_mutex_unlock( &p_input->stream.stream_lock );
1254
1255         if( p_track->p_es->p_decoder_fifo )
1256         {
1257             if( p_track->p_pes_init != NULL )
1258             {
1259                 input_DecodePES( p_track->p_es->p_decoder_fifo, p_track->p_pes_init );
1260                 p_track->p_pes_init = NULL;
1261             }
1262             p_track->b_selected = VLC_TRUE;
1263         }
1264         else
1265         {
1266             msg_Dbg( p_input, "Argg cannot select this stream" );
1267             if( p_track->p_pes_init != NULL )
1268             {
1269                 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1270                 p_track->p_pes_init = NULL;
1271             }
1272             p_track->b_selected = VLC_FALSE;
1273         }
1274     }
1275
1276     p_track->i_chunk    = i_chunk;
1277     p_track->i_sample   = i_sample;
1278
1279     return( VLC_SUCCESS );
1280 }
1281
1282 /****************************************************************************
1283  * MP4_TrackCreate:
1284  ****************************************************************************
1285  * Parse track information and create all needed data to run a track
1286  * If it succeed b_ok is set to 1 else to 0
1287  ****************************************************************************/
1288 static void MP4_TrackCreate( input_thread_t *p_input,
1289                              track_data_mp4_t *p_track,
1290                              MP4_Box_t  * p_box_trak )
1291 {
1292     unsigned int i;
1293
1294     MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1295     MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1296     MP4_Box_t *p_elst;
1297
1298     MP4_Box_t *p_mdhd;
1299     MP4_Box_t *p_hdlr;
1300
1301     MP4_Box_t *p_vmhd;
1302     MP4_Box_t *p_smhd;
1303
1304     /* hint track unsuported */
1305
1306     /* set default value (-> track unusable) */
1307     p_track->b_ok       = VLC_FALSE;
1308     p_track->b_enable   = VLC_FALSE;
1309     p_track->b_selected = VLC_FALSE;
1310
1311     p_track->i_cat = UNKNOWN_ES;
1312
1313     if( !p_tkhd )
1314     {
1315         return;
1316     }
1317
1318     /* do we launch this track by default ? */
1319     p_track->b_enable =
1320         ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1321
1322     p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1323     p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1324     p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1325
1326     if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1327     {
1328 /*        msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1329     }
1330
1331     if( p_tref )
1332     {
1333 /*        msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1334     }
1335
1336     p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1337     p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1338
1339     if( ( !p_mdhd )||( !p_hdlr ) )
1340     {
1341         return;
1342     }
1343
1344     p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1345
1346     for( i = 0; i < 3; i++ )
1347     {
1348         p_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
1349     }
1350     p_mdhd->data.p_mdhd->i_language[3] = 0;
1351
1352     switch( p_hdlr->data.p_hdlr->i_handler_type )
1353     {
1354         case( FOURCC_soun ):
1355             if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1356             {
1357                 return;
1358             }
1359             p_track->i_cat = AUDIO_ES;
1360             break;
1361
1362         case( FOURCC_vide ):
1363             if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1364             {
1365                 return;
1366             }
1367             p_track->i_cat = VIDEO_ES;
1368             break;
1369
1370         default:
1371             return;
1372     }
1373 /*  TODO
1374     add support for:
1375     p_dinf = MP4_BoxGet( p_minf, "dinf" );
1376 */
1377     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) )
1378     {
1379         return;
1380     }
1381
1382     if( !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1383     {
1384         return;
1385     }
1386
1387     /* Create chunk  index table */
1388     if( TrackCreateChunksIndex( p_input,p_track  ) )
1389     {
1390         return; /* cannot create chunks index */
1391     }
1392
1393     /* create sample index table needed for reading and seeking */
1394     if( TrackCreateSamplesIndex( p_input, p_track ) )
1395     {
1396         return; /* cannot create samples index */
1397     }
1398
1399     p_track->i_chunk  = 0;
1400     p_track->i_sample = 0;
1401     /* now create es but does not select it */
1402     /* XXX needed else vlc won't know this track exist */
1403     if( TrackCreateES( p_input,
1404                        p_track, p_track->i_chunk,
1405                        &p_track->p_es,
1406                        &p_track->p_pes_init ) )
1407     {
1408         msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1409                  p_track->i_track_ID );
1410         return;
1411     }
1412 #if 0
1413     {
1414         int i;
1415
1416         for( i = 0; i < p_track->i_chunk_count; i++ )
1417         {
1418             fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1419
1420         }
1421     }
1422 #endif
1423     p_track->b_ok = VLC_TRUE;
1424 }
1425
1426 /****************************************************************************
1427  * MP4_TrackDestroy:
1428  ****************************************************************************
1429  * Destroy a track created by MP4_TrackCreate.
1430  ****************************************************************************/
1431 static void MP4_TrackDestroy( input_thread_t *p_input,
1432                               track_data_mp4_t *p_track )
1433 {
1434     unsigned int i_chunk;
1435
1436     p_track->b_ok = VLC_FALSE;
1437     p_track->b_enable   = VLC_FALSE;
1438     p_track->b_selected = VLC_FALSE;
1439
1440     p_track->i_cat = UNKNOWN_ES;
1441
1442     if( p_track->p_pes_init )
1443     {
1444         input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1445     }
1446
1447     for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1448     {
1449         if( p_track->chunk )
1450         {
1451            FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1452            FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1453         }
1454     }
1455     FREE( p_track->chunk );
1456
1457     if( !p_track->i_sample_size )
1458     {
1459         FREE( p_track->p_sample_size );
1460     }
1461 }
1462
1463 static int  MP4_TrackSelect ( input_thread_t    *p_input,
1464                               track_data_mp4_t  *p_track,
1465                               mtime_t           i_start )
1466 {
1467     uint32_t i_chunk;
1468     uint32_t i_sample;
1469
1470     if( !p_track->b_ok )
1471     {
1472         return( VLC_EGENERIC );
1473     }
1474
1475     if( p_track->b_selected )
1476     {
1477         msg_Warn( p_input,
1478                   "track[Id 0x%x] already selected",
1479                   p_track->i_track_ID );
1480         return( VLC_SUCCESS );
1481     }
1482
1483     if( TrackTimeToSampleChunk( p_input,
1484                                 p_track, i_start,
1485                                 &i_chunk, &i_sample ) )
1486     {
1487         msg_Warn( p_input,
1488                   "cannot select track[Id 0x%x]",
1489                   p_track->i_track_ID );
1490         return( VLC_EGENERIC );
1491     }
1492
1493     p_track->b_selected = VLC_TRUE;
1494
1495     if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1496     {
1497         p_track->b_selected = VLC_FALSE;
1498     }
1499
1500     return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1501 }
1502
1503 static void MP4_TrackUnselect(input_thread_t    *p_input,
1504                               track_data_mp4_t  *p_track )
1505 {
1506     if( !p_track->b_ok )
1507     {
1508         return;
1509     }
1510
1511     if( !p_track->b_selected )
1512     {
1513         msg_Warn( p_input,
1514                   "track[Id 0x%x] already unselected",
1515                   p_track->i_track_ID );
1516         return;
1517     }
1518
1519     if( p_track->p_es->p_decoder_fifo )
1520     {
1521         vlc_mutex_lock( &p_input->stream.stream_lock );
1522         input_UnselectES( p_input, p_track->p_es );
1523         vlc_mutex_unlock( &p_input->stream.stream_lock );
1524     }
1525
1526     p_track->b_selected = VLC_FALSE;
1527 }
1528
1529 static int  MP4_TrackSeek   ( input_thread_t    *p_input,
1530                               track_data_mp4_t  *p_track,
1531                               mtime_t           i_start )
1532 {
1533     uint32_t i_chunk;
1534     uint32_t i_sample;
1535
1536     if( !p_track->b_ok )
1537     {
1538         return( VLC_EGENERIC );
1539     }
1540
1541     if( TrackTimeToSampleChunk( p_input,
1542                                 p_track, i_start,
1543                                 &i_chunk, &i_sample ) )
1544     {
1545         msg_Warn( p_input,
1546                   "cannot select track[Id 0x%x]",
1547                   p_track->i_track_ID );
1548         return( VLC_EGENERIC );
1549     }
1550
1551     p_track->b_selected = VLC_TRUE;
1552
1553     TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1554
1555     return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1556 }
1557
1558
1559
1560
1561
1562 /*
1563  * 3 types: for audio
1564  * 
1565  */
1566
1567 static int  MP4_TrackSampleSize( track_data_mp4_t   *p_track )
1568 {
1569     int i_size;
1570     MP4_Box_data_sample_soun_t *p_soun;
1571
1572     if( p_track->i_sample_size == 0 )
1573     {
1574         /* most simple case */
1575         return( p_track->p_sample_size[p_track->i_sample] );
1576     }
1577     if( p_track->i_cat != AUDIO_ES )
1578     {
1579         return( p_track->i_sample_size );
1580     }
1581
1582     if( p_track->i_sample_size != 1 )
1583     {
1584         //msg_Warn( p_input, "SampleSize != 1" );
1585         return( p_track->i_sample_size );
1586     }
1587
1588     p_soun = p_track->p_sample->data.p_sample_soun;
1589
1590     if( p_soun->i_qt_version == 1 )
1591     {
1592         i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1593     }
1594     else
1595     {
1596         //msg_Warn( p_input, "i_qt_version == 0" );
1597         /* FIXME */
1598
1599         i_size = p_track->chunk[p_track->i_chunk].i_sample_count * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1600     }
1601
1602     //fprintf( stderr, "size=%d\n", i_size );
1603     return( i_size );
1604 }
1605
1606
1607 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1608 {
1609     unsigned int i_sample;
1610     uint64_t i_pos;
1611
1612
1613     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1614
1615     if( p_track->i_sample_size )
1616     {
1617 #if 0
1618         i_pos += ( p_track->i_sample -
1619                         p_track->chunk[p_track->i_chunk].i_sample_first ) *
1620                                 MP4_TrackSampleSize( p_track );
1621 #endif
1622         /* we read chunk by chunk */
1623         i_pos += 0;
1624     }
1625     else
1626     {
1627         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1628                 i_sample < p_track->i_sample; i_sample++ )
1629         {
1630             i_pos += p_track->p_sample_size[i_sample];
1631         }
1632
1633     }
1634     return( i_pos );
1635 }
1636
1637 static int  MP4_TrackNextSample( input_thread_t     *p_input,
1638                                  track_data_mp4_t   *p_track )
1639 {
1640
1641     if( p_track->i_cat == AUDIO_ES &&
1642         p_track->i_sample_size != 0 )
1643     {
1644         MP4_Box_data_sample_soun_t *p_soun;
1645
1646         p_soun = p_track->p_sample->data.p_sample_soun;
1647
1648         if( p_soun->i_qt_version == 1 )
1649         {
1650             /* chunk by chunk */
1651             p_track->i_sample =
1652                 p_track->chunk[p_track->i_chunk].i_sample_first +
1653                 p_track->chunk[p_track->i_chunk].i_sample_count;
1654         }
1655         else
1656         {
1657             /* FIXME */
1658             p_track->i_sample =
1659                 p_track->chunk[p_track->i_chunk].i_sample_first +
1660                 p_track->chunk[p_track->i_chunk].i_sample_count;
1661         }
1662     }
1663     else
1664     {
1665         p_track->i_sample++;
1666     }
1667
1668     if( p_track->i_sample >= p_track->i_sample_count )
1669     {
1670         /* we have reach end of the track so free decoder stuff */
1671         msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1672         MP4_TrackUnselect( p_input, p_track );
1673         return( VLC_EGENERIC );
1674     }
1675
1676     /* Have we changed chunk ? */
1677     if( p_track->i_sample >=
1678             p_track->chunk[p_track->i_chunk].i_sample_first +
1679             p_track->chunk[p_track->i_chunk].i_sample_count )
1680     {
1681         if( TrackGotoChunkSample( p_input,
1682                                   p_track,
1683                                   p_track->i_chunk + 1,
1684                                   p_track->i_sample ) )
1685         {
1686             msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1687             MP4_TrackUnselect( p_input, p_track );
1688             return( VLC_EGENERIC );
1689         }
1690     }
1691
1692     return( VLC_SUCCESS );
1693 }
1694
1695