]> git.sesse.net Git - vlc/blob - modules/demux/mp4/mp4.c
* mp4: I have reworked a lot the demuxer. Now it should handle gracefully
[vlc] / 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.19 2003/03/11 18:57:50 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                 /* go,go go ! */
449                 if( ! MP4_SeekAbsolute( p_input, i_pos ) )
450                 {
451                     msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
452                     MP4_TrackUnselect( p_input, &track );
453                     break;
454                 }
455
456
457                 /* now create a pes */
458                 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
459                 {
460                     break;
461                 }
462                 /* and a data packet for the data */
463                 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
464                 {
465                     input_DeletePES( p_input->p_method_data, p_pes );
466                     break;
467                 }
468                 /* initialisation of all the field */
469                 p_pes->i_dts = p_pes->i_pts = 0;
470                 p_pes->p_first = p_pes->p_last  = p_data;
471                 p_pes->i_nb_data = 1;
472                 p_pes->i_pes_size = i_size;
473                 if( i_size > 0 )
474                 {
475                     if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
476                     {
477                         input_DeletePES( p_input->p_method_data, p_pes );
478
479                         msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
480                         MP4_TrackUnselect( p_input, &track );
481                         break;
482                     }
483                 }
484
485                 p_pes->i_dts =
486                     p_pes->i_pts = input_ClockGetTS( p_input,
487                                                      p_input->stream.p_selected_program,
488                                                      MP4_GetTrackPTS( &track ) * 9/100);
489
490                 if( track.p_es->p_decoder_fifo )
491                 {
492                     input_DecodePES( track.p_es->p_decoder_fifo, p_pes );
493                 }
494                 else
495                 {
496                     input_DeletePES( p_input->p_method_data, p_pes );
497                 }
498
499                 if( MP4_TrackNextSample( p_input, &track ) )
500                 {
501                     break;
502                 }
503             }
504         }
505 #undef track
506     }
507
508     return( 1 );
509 }
510 /*****************************************************************************
511  * MP4Seek: Got to i_date
512  ******************************************************************************/
513 static int   MP4Seek     ( input_thread_t *p_input, mtime_t i_date )
514 {
515     demux_sys_t *p_demux = p_input->p_demux_data;
516     unsigned int i_track;
517     /* First update update global time */
518     p_demux->i_time = i_date * p_demux->i_timescale / 1000000;
519     p_demux->i_pcr  = i_date* 9 / 100;
520
521     /* Now for each stream try to go to this time */
522     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
523     {
524 #define track p_demux->track[i_track]
525         if( track.b_ok && track.b_selected )
526         {
527             MP4_TrackSeek( p_input, &track, i_date );
528         }
529 #undef  track
530     }
531     return( 1 );
532 }
533
534 /*****************************************************************************
535  * MP4End: frees unused data
536  *****************************************************************************/
537 static void __MP4End ( vlc_object_t * p_this )
538 {
539     unsigned int i_track;
540     input_thread_t *  p_input = (input_thread_t *)p_this;
541     demux_sys_t *p_demux = p_input->p_demux_data;
542
543     msg_Dbg( p_input, "freeing all memory" );
544     MP4_BoxFree( p_input, &p_demux->box_root );
545     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
546     {
547         MP4_TrackDestroy( p_input, &p_demux->track[i_track] );
548     }
549     FREE( p_demux->track );
550
551     FREE( p_input->p_demux_data );
552 }
553
554
555 /****************************************************************************
556  * Local functions, specific to vlc
557  ****************************************************************************/
558
559 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
560 static int TrackCreateChunksIndex( input_thread_t *p_input,
561                                    track_data_mp4_t *p_demux_track )
562 {
563     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
564     MP4_Box_t *p_stsc;
565
566     unsigned int i_chunk;
567     unsigned int i_index, i_last;
568
569     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
570           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
571         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
572     {
573         return( VLC_EGENERIC );
574     }
575
576     p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
577     if( !p_demux_track->i_chunk_count )
578     {
579         msg_Warn( p_input, "no chunk defined" );
580         return( VLC_EGENERIC );
581     }
582     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
583                                    sizeof( chunk_data_mp4_t ) );
584
585     /* first we read chunk offset */
586     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
587     {
588         p_demux_track->chunk[i_chunk].i_offset =
589                 p_co64->data.p_co64->i_chunk_offset[i_chunk];
590     }
591
592     /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
593         to be used for the sample XXX begin to 1
594         We construct it begining at the end */
595     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
596     i_index = p_stsc->data.p_stsc->i_entry_count;
597     if( !i_index )
598     {
599         msg_Warn( p_input, "cannot read chunk table or table empty" );
600         return( VLC_EGENERIC );
601     }
602
603     while( i_index )
604     {
605         i_index--;
606         for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
607                 i_chunk < i_last; i_chunk++ )
608         {
609             p_demux_track->chunk[i_chunk].i_sample_description_index =
610                     p_stsc->data.p_stsc->i_sample_description_index[i_index];
611             p_demux_track->chunk[i_chunk].i_sample_count =
612                     p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
613         }
614         i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
615     }
616
617     p_demux_track->chunk[0].i_sample_first = 0;
618     for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
619     {
620         p_demux_track->chunk[i_chunk].i_sample_first =
621             p_demux_track->chunk[i_chunk-1].i_sample_first +
622                 p_demux_track->chunk[i_chunk-1].i_sample_count;
623     }
624
625     msg_Dbg( p_input,
626              "track[Id 0x%x] read %d chunk",
627              p_demux_track->i_track_ID,
628             p_demux_track->i_chunk_count );
629
630     return( VLC_SUCCESS );
631 }
632 static int TrackCreateSamplesIndex( input_thread_t *p_input,
633                                     track_data_mp4_t *p_demux_track )
634 {
635     MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
636                           ctts make same mapping but for composition time,
637                           not yet used and probably not usefull */
638     MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
639                           that uses a compressed form FIXME make them in libmp4
640                           as a unique type */
641     /* TODO use also stss and stsh table for seeking */
642     /* FIXME use edit table */
643     int64_t i_sample;
644     int64_t i_chunk;
645
646     int64_t i_index;
647     int64_t i_index_sample_used;
648
649     int64_t i_last_dts;
650
651     p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
652     p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
653
654     if( ( !p_stts )||( !p_stsz ) )
655     {
656         msg_Warn( p_input, "cannot read sample table" );
657         return( VLC_EGENERIC );
658     }
659
660     p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
661
662
663     /* for sample size, there are 2 case */
664     if( p_stsz->data.p_stsz->i_sample_size )
665     {
666         /* 1: all sample have the same size, so no need to construct a table */
667         p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
668         p_demux_track->p_sample_size = NULL;
669     }
670     else
671     {
672         /* 2: each sample can have a different size */
673         p_demux_track->i_sample_size = 0;
674         p_demux_track->p_sample_size =
675             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
676
677         for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
678         {
679             p_demux_track->p_sample_size[i_sample] =
680                     p_stsz->data.p_stsz->i_entry_size[i_sample];
681         }
682     }
683     /* we have extract all information from stsz,
684         now use stts */
685
686     /* if we don't want to waste too much memory, we can't expand
687        the box !, so each chunk will contain an "extract" of this table
688        for fast research */
689
690     i_last_dts = 0;
691     i_index = 0; i_index_sample_used =0;
692
693     /* create and init last data for each chunk */
694     for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
695     {
696
697         int64_t i_entry, i_sample_count, i;
698         /* save last dts */
699         p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
700     /* count how many entries needed for this chunk
701        for p_sample_delta_dts and p_sample_count_dts */
702
703         i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
704
705         i_entry = 0;
706         while( i_sample_count > 0 )
707         {
708             i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
709             if( i_entry == 0 )
710             {
711                 i_sample_count += i_index_sample_used; /* don't count already used sample
712                                                    int this entry */
713             }
714             i_entry++;
715         }
716
717         /* allocate them */
718         p_demux_track->chunk[i_chunk].p_sample_count_dts =
719             calloc( i_entry, sizeof( uint32_t ) );
720         p_demux_track->chunk[i_chunk].p_sample_delta_dts =
721             calloc( i_entry, sizeof( uint32_t ) );
722
723         /* now copy */
724         i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
725         for( i = 0; i < i_entry; i++ )
726         {
727             int64_t i_used;
728             int64_t i_rest;
729
730             i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
731
732             i_used = __MIN( i_rest, i_sample_count );
733
734             i_index_sample_used += i_used;
735             i_sample_count -= i_used;
736
737             p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
738
739             p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
740                         p_stts->data.p_stts->i_sample_delta[i_index];
741
742             i_last_dts += i_used *
743                     p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
744
745             if( i_index_sample_used >=
746                              p_stts->data.p_stts->i_sample_count[i_index] )
747             {
748
749                 i_index++;
750                 i_index_sample_used = 0;
751             }
752         }
753
754     }
755
756     msg_Dbg( p_input,
757              "track[Id 0x%x] read %d samples length:"I64Fd"s",
758              p_demux_track->i_track_ID,
759              p_demux_track->i_sample_count,
760              i_last_dts / p_demux_track->i_timescale );
761
762     return( VLC_SUCCESS );
763 }
764
765 /*
766  * TrackCreateES:
767  *  Create ES and PES to init decoder if needed, for a track starting at i_chunk
768  */
769 static int  TrackCreateES   ( input_thread_t   *p_input,
770                               track_data_mp4_t *p_track,
771                               unsigned int     i_chunk,
772                               es_descriptor_t  **pp_es,
773                               pes_packet_t     **pp_pes )
774 {
775     MP4_Box_t *  p_sample;
776     unsigned int i;
777
778     unsigned int i_decoder_specific_info_len;
779     uint8_t *    p_decoder_specific_info;
780
781     es_descriptor_t *p_es;
782     pes_packet_t    *p_pes_init;
783
784     uint8_t             *p_init;
785     BITMAPINFOHEADER    *p_bih;
786     WAVEFORMATEX        *p_wf;
787
788     MP4_Box_t   *p_esds;
789
790     if( !p_track->chunk[i_chunk].i_sample_description_index )
791     {
792         msg_Warn( p_input,
793                   "invalid SampleEntry index (track[Id 0x%x])",
794                   p_track->i_track_ID );
795         return( VLC_EGENERIC );
796     }
797
798     p_sample = MP4_BoxGet(  p_track->p_stsd,
799                             "[%d]",
800                 p_track->chunk[i_chunk].i_sample_description_index - 1 );
801
802     if( !p_sample || !p_sample->data.p_data )
803     {
804         msg_Warn( p_input,
805                   "cannot find SampleEntry (track[Id 0x%x])",
806                   p_track->i_track_ID );
807         return( VLC_EGENERIC );
808     }
809
810     p_track->p_sample = p_sample;
811
812     if( p_track->i_sample_size == 1 )
813     {
814         MP4_Box_data_sample_soun_t *p_soun;
815
816         p_soun = p_sample->data.p_sample_soun;
817
818         if( p_soun->i_qt_version == 0 )
819         {
820             switch( p_sample->i_type )
821             {
822                 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
823                     p_soun->i_qt_version = 1;
824                     p_soun->i_sample_per_packet = 64;
825                     p_soun->i_bytes_per_packet  = 34;
826                     p_soun->i_bytes_per_frame   = 34 * p_soun->i_channelcount;
827                     p_soun->i_bytes_per_sample  = 2;
828                     break;
829                 default:
830                     break;
831             }
832         }
833     }
834
835
836     vlc_mutex_lock( &p_input->stream.stream_lock );
837     p_es = input_AddES( p_input,
838                         p_input->stream.p_selected_program,
839                         p_track->i_track_ID,
840                         0 );
841     vlc_mutex_unlock( &p_input->stream.stream_lock );
842     /* Initialise ES, first language as description */
843     for( i = 0; i < 3; i++ )
844     {
845         p_es->psz_desc[i] = p_track->i_language[i];
846     }
847     p_es->psz_desc[3] = '\0';
848
849     p_es->i_stream_id = p_track->i_track_ID;
850
851     /* It's a little ugly but .. there are special cases */
852     switch( p_sample->i_type )
853     {
854         case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
855         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
856             p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
857             break;
858         case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
859             p_es->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
860             break;
861         default:
862             p_es->i_fourcc = p_sample->i_type;
863             break;
864     }
865
866     p_es->i_cat = p_track->i_cat;
867
868     i_decoder_specific_info_len = 0;
869     p_decoder_specific_info = NULL;
870     p_pes_init = NULL;
871
872     /* now see if esds is present and if so create a data packet
873         with decoder_specific_info  */
874 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
875     if( ( p_esds = MP4_BoxGet( p_sample, "esds" ) )&&
876         ( p_esds->data.p_esds )&&
877         ( p_decconfig ) )
878     {
879         /* First update information based on i_objectTypeIndication */
880         switch( p_decconfig->i_objectTypeIndication )
881         {
882             case( 0x20 ): /* MPEG4 VIDEO */
883                 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
884                 break;
885             case( 0x40):
886                 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
887                 break;
888             case( 0x60):
889             case( 0x61):
890             case( 0x62):
891             case( 0x63):
892             case( 0x64):
893             case( 0x65): /* MPEG2 video */
894                 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
895                 break;
896             /* Theses are MPEG2-AAC */
897             case( 0x66): /* main profile */
898             case( 0x67): /* Low complexity profile */
899             case( 0x68): /* Scaleable Sampling rate profile */
900                 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
901                 break;
902             /* true MPEG 2 audio */
903             case( 0x69):
904                 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
905                 break;
906             case( 0x6a): /* MPEG1 video */
907                 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
908                 break;
909             case( 0x6b): /* MPEG1 audio */
910                 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
911                 break;
912             case( 0x6c ): /* jpeg */
913                 p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
914                 break;
915             default:
916                 /* Unknown entry, but don't touch i_fourcc */
917                 msg_Warn( p_input,
918                           "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
919                           p_decconfig->i_objectTypeIndication,
920                           p_track->i_track_ID );
921                 break;
922         }
923         i_decoder_specific_info_len =
924                 p_decconfig->i_decoder_specific_info_len;
925         p_decoder_specific_info =
926                 p_decconfig->p_decoder_specific_info;
927     }
928
929 #undef p_decconfig
930
931     /* some last initialisation */
932     /* XXX I create a bitmapinfoheader_t or
933        waveformatex_t for each stream, up to now it's the best thing
934        I've found but it could exist a better solution :) as something
935        like adding some new fields in p_es ...
936
937        XXX I don't set all values, only thoses that are interesting or known
938         --> bitmapinfoheader_t : width and height
939         --> waveformatex_t : channels, samplerate, bitspersample
940         and at the end I add p_decoder_specific_info
941
942         TODO set more values
943
944      */
945
946     switch( p_track->i_cat )
947     {
948         case( VIDEO_ES ):
949             /* now create a bitmapinfoheader_t for decoder and
950                add information found in p_esds */
951             /* XXX XXX + 16 are for avoid segfault when ffmpeg access beyong the data */
952             p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len + 16 );
953             p_bih = (BITMAPINFOHEADER*)p_init;
954
955             p_bih->biSize     = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
956             p_bih->biWidth    = p_sample->data.p_sample_vide->i_width;
957             p_bih->biHeight   = p_sample->data.p_sample_vide->i_height;
958             p_bih->biPlanes   = 1;      // FIXME
959             p_bih->biBitCount = 0;      // FIXME
960             p_bih->biCompression   = 0; // FIXME
961             p_bih->biSizeImage     = 0; // FIXME
962             p_bih->biXPelsPerMeter = 0; // FIXME
963             p_bih->biYPelsPerMeter = 0; // FIXME
964             p_bih->biClrUsed       = 0; // FIXME
965             p_bih->biClrImportant  = 0; // FIXME
966
967             if( p_bih->biWidth == 0 )
968             {
969                 // fall on display size
970                 p_bih->biWidth = p_track->i_width;
971             }
972             if( p_bih->biHeight == 0 )
973             {
974                 // fall on display size
975                 p_bih->biHeight = p_track->i_height;
976             }
977
978             if( i_decoder_specific_info_len )
979             {
980                 data_packet_t   *p_data;
981
982                 memcpy( p_init + sizeof( BITMAPINFOHEADER ),
983                         p_decoder_specific_info,
984                         i_decoder_specific_info_len);
985
986                 /* If stream is mpeg4 video we send specific_info,
987                    as it's needed to decode it (vol) */
988                 switch( p_es->i_fourcc )
989                 {
990                     case VLC_FOURCC( 'm','p','4','v' ):
991                     case VLC_FOURCC( 'D','I','V','X' ):
992                     case VLC_FOURCC( 'd','i','v','x' ):
993                         p_pes_init = input_NewPES( p_input->p_method_data );
994                         p_data = input_NewPacket( p_input->p_method_data,
995                                                   i_decoder_specific_info_len);
996                         memcpy( p_data->p_payload_start,
997                                 p_decoder_specific_info,
998                                 i_decoder_specific_info_len );
999                         p_pes_init->i_dts = p_pes_init->i_pts = 0;
1000                         p_pes_init->p_first = p_pes_init->p_last = p_data;
1001                         p_pes_init->i_nb_data = 1;
1002                         p_pes_init->i_pes_size = i_decoder_specific_info_len;
1003                         break;
1004                     default:
1005                         break;
1006                 }
1007
1008             }
1009             break;
1010
1011         case( AUDIO_ES ):
1012             p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len + 16 );
1013             p_wf = (WAVEFORMATEX*)p_init;
1014
1015             p_wf->wFormatTag = 1;
1016             p_wf->nChannels = p_sample->data.p_sample_soun->i_channelcount;
1017             p_wf->nSamplesPerSec = p_sample->data.p_sample_soun->i_sampleratehi;
1018             p_wf->nAvgBytesPerSec = p_sample->data.p_sample_soun->i_channelcount *
1019                                     p_sample->data.p_sample_soun->i_sampleratehi *
1020                                     p_sample->data.p_sample_soun->i_samplesize / 8;
1021             p_wf->nBlockAlign = 0;
1022             p_wf->wBitsPerSample = p_sample->data.p_sample_soun->i_samplesize;
1023             p_wf->cbSize = i_decoder_specific_info_len;
1024
1025             if( i_decoder_specific_info_len )
1026             {
1027                 memcpy( p_init + sizeof( WAVEFORMATEX ),
1028                         p_decoder_specific_info,
1029                         i_decoder_specific_info_len);
1030             }
1031
1032             break;
1033
1034         default:
1035             p_init = NULL;
1036             break;
1037     }
1038     if( p_es->i_cat == AUDIO_ES )
1039     {
1040         p_es->p_bitmapinfoheader = NULL;
1041         p_es->p_waveformatex     = (void*)p_init;
1042     }
1043     else if( p_es->i_cat == VIDEO_ES )
1044     {
1045         p_es->p_bitmapinfoheader = (void*)p_init;
1046         p_es->p_waveformatex     = NULL;
1047     }
1048
1049     *pp_es = p_es;
1050     *pp_pes = p_pes_init;
1051     return( VLC_SUCCESS );
1052 }
1053
1054 /* given a time it return sample/chunk */
1055 static int  TrackTimeToSampleChunk( input_thread_t *p_input,
1056                                     track_data_mp4_t *p_track,
1057                                     uint64_t i_start,
1058                                     uint32_t *pi_chunk,
1059                                     uint32_t *pi_sample )
1060 {
1061     MP4_Box_t    *p_stss;
1062     uint64_t     i_dts;
1063     unsigned int i_sample;
1064     unsigned int i_chunk;
1065     int          i_index;
1066
1067     /* convert absolute time to in timescale unit */
1068     i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1069
1070     /* FIXME see if it's needed to check p_track->i_chunk_count */
1071     if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1072     {
1073         return( VLC_EGENERIC );
1074     }
1075
1076     /* we start from sample 0/chunk 0, hope it won't take too much time */
1077     /* *** find good chunk *** */
1078     for( i_chunk = 0; ; i_chunk++ )
1079     {
1080         if( i_chunk + 1 >= p_track->i_chunk_count )
1081         {
1082             /* at the end and can't check if i_start in this chunk,
1083                it will be check while searching i_sample */
1084             i_chunk = p_track->i_chunk_count - 1;
1085             break;
1086         }
1087
1088         if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1089             i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
1090         {
1091             break;
1092         }
1093     }
1094
1095     /* *** find sample in the chunk *** */
1096     i_sample = p_track->chunk[i_chunk].i_sample_first;
1097     i_dts    = p_track->chunk[i_chunk].i_first_dts;
1098     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1099     {
1100         if( i_dts +
1101             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1102             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1103         {
1104             i_dts    +=
1105                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1106                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1107
1108             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1109             i_index++;
1110         }
1111         else
1112         {
1113             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1114             {
1115                 break;
1116             }
1117             i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1118             break;
1119         }
1120     }
1121
1122     if( i_sample >= p_track->i_sample_count )
1123     {
1124         msg_Warn( p_input,
1125                   "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1126                   p_track->i_track_ID, i_chunk, i_sample );
1127         return( VLC_EGENERIC );
1128     }
1129
1130
1131     /* *** Try to find nearest sync points *** */
1132     if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1133     {
1134         unsigned int i_index;
1135         msg_Dbg( p_input,
1136                     "track[Id 0x%x] using Sync Sample Box (stss)",
1137                     p_track->i_track_ID );
1138         for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1139         {
1140             if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1141             {
1142                 if( i_index > 0 )
1143                 {
1144                     msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1145                             i_sample,
1146                             p_stss->data.p_stss->i_sample_number[i_index-1] );
1147                     i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1148                     /* new i_sample is less than old so i_chunk can only decreased */
1149                     while( i_chunk > 0 &&
1150                             i_sample < p_track->chunk[i_chunk].i_sample_first )
1151                     {
1152                         i_chunk--;
1153                     }
1154                 }
1155                 else
1156                 {
1157                     msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1158                             i_sample,
1159                             p_stss->data.p_stss->i_sample_number[i_index-1] );
1160                     i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1161                     /* new i_sample is more than old so i_chunk can only increased */
1162                     while( i_chunk < p_track->i_chunk_count - 1 &&
1163                            i_sample >= p_track->chunk[i_chunk].i_sample_first +
1164                                                 p_track->chunk[i_chunk].i_sample_count )
1165                     {
1166                         i_chunk++;
1167                     }
1168                 }
1169                 break;
1170             }
1171         }
1172     }
1173     else
1174     {
1175         msg_Dbg( p_input,
1176                     "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1177                     p_track->i_track_ID );
1178     }
1179
1180     if( pi_chunk  ) *pi_chunk  = i_chunk;
1181     if( pi_sample ) *pi_sample = i_sample;
1182     return( VLC_SUCCESS );
1183 }
1184
1185 static int  TrackGotoChunkSample( input_thread_t   *p_input,
1186                                   track_data_mp4_t *p_track,
1187                                   unsigned int     i_chunk,
1188                                   unsigned int     i_sample )
1189 {
1190     /* now see if actual es is ok */
1191     if( p_track->i_chunk < 0 ||
1192         p_track->i_chunk >= p_track->i_chunk_count ||
1193         p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1194             p_track->chunk[i_chunk].i_sample_description_index )
1195     {
1196         msg_Warn( p_input, "Recreate ES" );
1197
1198         /* no :( recreate es */
1199         vlc_mutex_lock( &p_input->stream.stream_lock );
1200         input_DelES( p_input, p_track->p_es );
1201         vlc_mutex_unlock( &p_input->stream.stream_lock );
1202
1203         if( p_track->p_pes_init )
1204         {
1205             input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1206         }
1207
1208         if( TrackCreateES( p_input,
1209                            p_track, i_chunk,
1210                            &p_track->p_es,
1211                            &p_track->p_pes_init ) )
1212         {
1213             msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1214                      p_track->i_track_ID );
1215
1216             p_track->b_ok       = VLC_FALSE;
1217             p_track->b_selected = VLC_FALSE;
1218             return( VLC_EGENERIC );
1219         }
1220     }
1221
1222     /* select again the new decoder */
1223     if( p_track->b_selected && p_track->p_es && p_track->p_es->p_decoder_fifo == NULL )
1224     {
1225         vlc_mutex_lock( &p_input->stream.stream_lock );
1226         input_SelectES( p_input, p_track->p_es );
1227         vlc_mutex_unlock( &p_input->stream.stream_lock );
1228
1229         if( p_track->p_es->p_decoder_fifo )
1230         {
1231             if( p_track->p_pes_init != NULL )
1232             {
1233                 input_DecodePES( p_track->p_es->p_decoder_fifo, p_track->p_pes_init );
1234                 p_track->p_pes_init = NULL;
1235             }
1236             p_track->b_selected = VLC_TRUE;
1237         }
1238         else
1239         {
1240             msg_Dbg( p_input, "Argg cannot select this stream" );
1241             if( p_track->p_pes_init != NULL )
1242             {
1243                 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1244                 p_track->p_pes_init = NULL;
1245             }
1246             p_track->b_selected = VLC_FALSE;
1247         }
1248     }
1249
1250     p_track->i_chunk    = i_chunk;
1251     p_track->i_sample   = i_sample;
1252
1253     return( VLC_SUCCESS );
1254 }
1255
1256 /****************************************************************************
1257  * MP4_TrackCreate:
1258  ****************************************************************************
1259  * Parse track information and create all needed data to run a track
1260  * If it succeed b_ok is set to 1 else to 0
1261  ****************************************************************************/
1262 static void MP4_TrackCreate( input_thread_t *p_input,
1263                              track_data_mp4_t *p_track,
1264                              MP4_Box_t  * p_box_trak )
1265 {
1266     unsigned int i;
1267
1268     MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1269     MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1270     MP4_Box_t *p_elst;
1271
1272     MP4_Box_t *p_mdhd;
1273     MP4_Box_t *p_hdlr;
1274
1275     MP4_Box_t *p_vmhd;
1276     MP4_Box_t *p_smhd;
1277
1278     /* hint track unsuported */
1279
1280     /* set default value (-> track unusable) */
1281     p_track->b_ok       = VLC_FALSE;
1282     p_track->b_enable   = VLC_FALSE;
1283     p_track->b_selected = VLC_FALSE;
1284
1285     p_track->i_cat = UNKNOWN_ES;
1286
1287     if( !p_tkhd )
1288     {
1289         return;
1290     }
1291
1292     /* do we launch this track by default ? */
1293     p_track->b_enable =
1294         ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1295
1296     p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1297     p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1298     p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1299
1300     if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1301     {
1302 /*        msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1303     }
1304
1305     if( p_tref )
1306     {
1307 /*        msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1308     }
1309
1310     p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1311     p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1312
1313     if( ( !p_mdhd )||( !p_hdlr ) )
1314     {
1315         return;
1316     }
1317
1318     p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1319
1320     for( i = 0; i < 3; i++ )
1321     {
1322         p_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
1323     }
1324     p_mdhd->data.p_mdhd->i_language[3] = 0;
1325
1326     switch( p_hdlr->data.p_hdlr->i_handler_type )
1327     {
1328         case( FOURCC_soun ):
1329             if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1330             {
1331                 return;
1332             }
1333             p_track->i_cat = AUDIO_ES;
1334             break;
1335
1336         case( FOURCC_vide ):
1337             if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1338             {
1339                 return;
1340             }
1341             p_track->i_cat = VIDEO_ES;
1342             break;
1343
1344         default:
1345             return;
1346     }
1347 /*  TODO
1348     add support for:
1349     p_dinf = MP4_BoxGet( p_minf, "dinf" );
1350 */
1351     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) )
1352     {
1353         return;
1354     }
1355
1356     if( !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1357     {
1358         return;
1359     }
1360
1361     /* Create chunk  index table */
1362     if( TrackCreateChunksIndex( p_input,p_track  ) )
1363     {
1364         return; /* cannot create chunks index */
1365     }
1366
1367     /* create sample index table needed for reading and seeking */
1368     if( TrackCreateSamplesIndex( p_input, p_track ) )
1369     {
1370         return; /* cannot create samples index */
1371     }
1372
1373     p_track->i_chunk  = 0;
1374     p_track->i_sample = 0;
1375     /* now create es but does not select it */
1376     /* XXX needed else vlc won't know this track exist */
1377     if( TrackCreateES( p_input,
1378                        p_track, p_track->i_chunk,
1379                        &p_track->p_es,
1380                        &p_track->p_pes_init ) )
1381     {
1382         msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1383                  p_track->i_track_ID );
1384         return;
1385     }
1386 #if 0
1387     {
1388         int i;
1389
1390         for( i = 0; i < p_track->i_chunk_count; i++ )
1391         {
1392             fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1393
1394         }
1395     }
1396 #endif
1397     p_track->b_ok = VLC_TRUE;
1398 }
1399
1400 /****************************************************************************
1401  * MP4_TrackDestroy:
1402  ****************************************************************************
1403  * Destroy a track created by MP4_TrackCreate.
1404  ****************************************************************************/
1405 static void MP4_TrackDestroy( input_thread_t *p_input,
1406                               track_data_mp4_t *p_track )
1407 {
1408     unsigned int i_chunk;
1409
1410     p_track->b_ok = VLC_FALSE;
1411     p_track->b_enable   = VLC_FALSE;
1412     p_track->b_selected = VLC_FALSE;
1413
1414     p_track->i_cat = UNKNOWN_ES;
1415
1416     if( p_track->p_pes_init )
1417     {
1418         input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1419     }
1420
1421     for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1422     {
1423         if( p_track->chunk )
1424         {
1425            FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1426            FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1427         }
1428     }
1429     FREE( p_track->chunk );
1430
1431     if( !p_track->i_sample_size )
1432     {
1433         FREE( p_track->p_sample_size );
1434     }
1435 }
1436
1437 static int  MP4_TrackSelect ( input_thread_t    *p_input,
1438                               track_data_mp4_t  *p_track,
1439                               mtime_t           i_start )
1440 {
1441     uint32_t i_chunk;
1442     uint32_t i_sample;
1443
1444     if( !p_track->b_ok )
1445     {
1446         return( VLC_EGENERIC );
1447     }
1448
1449     if( p_track->b_selected )
1450     {
1451         msg_Warn( p_input,
1452                   "track[Id 0x%x] already selected",
1453                   p_track->i_track_ID );
1454         return( VLC_SUCCESS );
1455     }
1456
1457     if( TrackTimeToSampleChunk( p_input,
1458                                 p_track, i_start,
1459                                 &i_chunk, &i_sample ) )
1460     {
1461         msg_Warn( p_input,
1462                   "cannot select track[Id 0x%x]",
1463                   p_track->i_track_ID );
1464         return( VLC_EGENERIC );
1465     }
1466
1467     p_track->b_selected = VLC_TRUE;
1468
1469     if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1470     {
1471         p_track->b_selected = VLC_FALSE;
1472     }
1473
1474     return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1475 }
1476
1477 static void MP4_TrackUnselect(input_thread_t    *p_input,
1478                               track_data_mp4_t  *p_track )
1479 {
1480     if( !p_track->b_ok )
1481     {
1482         return;
1483     }
1484
1485     if( !p_track->b_selected )
1486     {
1487         msg_Warn( p_input,
1488                   "track[Id 0x%x] already unselected",
1489                   p_track->i_track_ID );
1490         return;
1491     }
1492
1493     if( p_track->p_es->p_decoder_fifo )
1494     {
1495         vlc_mutex_lock( &p_input->stream.stream_lock );
1496         input_UnselectES( p_input, p_track->p_es );
1497         vlc_mutex_unlock( &p_input->stream.stream_lock );
1498     }
1499
1500     p_track->b_selected = VLC_FALSE;
1501 }
1502
1503 static int  MP4_TrackSeek   ( input_thread_t    *p_input,
1504                               track_data_mp4_t  *p_track,
1505                               mtime_t           i_start )
1506 {
1507     uint32_t i_chunk;
1508     uint32_t i_sample;
1509
1510     if( !p_track->b_ok )
1511     {
1512         return( VLC_EGENERIC );
1513     }
1514
1515     if( TrackTimeToSampleChunk( p_input,
1516                                 p_track, i_start,
1517                                 &i_chunk, &i_sample ) )
1518     {
1519         msg_Warn( p_input,
1520                   "cannot select track[Id 0x%x]",
1521                   p_track->i_track_ID );
1522         return( VLC_EGENERIC );
1523     }
1524
1525     p_track->b_selected = VLC_TRUE;
1526
1527     TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1528
1529     return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1530 }
1531
1532
1533
1534
1535
1536 /*
1537  * 3 types: for audio
1538  * 
1539  */
1540
1541 static int  MP4_TrackSampleSize( track_data_mp4_t   *p_track )
1542 {
1543     int i_size;
1544     MP4_Box_data_sample_soun_t *p_soun;
1545
1546     if( p_track->i_sample_size == 0 )
1547     {
1548         /* most simple case */
1549         return( p_track->p_sample_size[p_track->i_sample] );
1550     }
1551     if( p_track->i_cat != AUDIO_ES )
1552     {
1553         return( p_track->i_sample_size );
1554     }
1555
1556     if( p_track->i_sample_size != 1 )
1557     {
1558         //msg_Warn( p_input, "SampleSize != 1" );
1559         return( p_track->i_sample_size );
1560     }
1561
1562     p_soun = p_track->p_sample->data.p_sample_soun;
1563
1564     if( p_soun->i_qt_version == 1 )
1565     {
1566         i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1567     }
1568     else
1569     {
1570         //msg_Warn( p_input, "i_qt_version == 0" );
1571         /* FIXME */
1572
1573         i_size = p_track->chunk[p_track->i_chunk].i_sample_count * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1574     }
1575
1576     //fprintf( stderr, "size=%d\n", i_size );
1577     return( i_size );
1578 }
1579
1580
1581 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1582 {
1583     unsigned int i_sample;
1584     uint64_t i_pos;
1585
1586
1587     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1588
1589     if( p_track->i_sample_size )
1590     {
1591 #if 0
1592         i_pos += ( p_track->i_sample -
1593                         p_track->chunk[p_track->i_chunk].i_sample_first ) *
1594                                 MP4_TrackSampleSize( p_track );
1595 #endif
1596         /* we read chunk by chunk */
1597         i_pos += 0;
1598     }
1599     else
1600     {
1601         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1602                 i_sample < p_track->i_sample; i_sample++ )
1603         {
1604             i_pos += p_track->p_sample_size[i_sample];
1605         }
1606
1607     }
1608     return( i_pos );
1609 }
1610
1611 static int  MP4_TrackNextSample( input_thread_t     *p_input,
1612                                  track_data_mp4_t   *p_track )
1613 {
1614
1615     if( p_track->i_cat == AUDIO_ES &&
1616         p_track->i_sample_size != 0 )
1617     {
1618         MP4_Box_data_sample_soun_t *p_soun;
1619
1620         p_soun = p_track->p_sample->data.p_sample_soun;
1621
1622         if( p_soun->i_qt_version == 1 )
1623         {
1624             /* chunk by chunk */
1625             p_track->i_sample =
1626                 p_track->chunk[p_track->i_chunk].i_sample_first +
1627                 p_track->chunk[p_track->i_chunk].i_sample_count;
1628         }
1629         else
1630         {
1631             /* FIXME */
1632             p_track->i_sample =
1633                 p_track->chunk[p_track->i_chunk].i_sample_first +
1634                 p_track->chunk[p_track->i_chunk].i_sample_count;
1635         }
1636     }
1637     else
1638     {
1639         p_track->i_sample++;
1640     }
1641
1642     if( p_track->i_sample >= p_track->i_sample_count )
1643     {
1644         /* we have reach end of the track so free decoder stuff */
1645         msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1646         MP4_TrackUnselect( p_input, p_track );
1647         return( VLC_EGENERIC );
1648     }
1649
1650     /* Have we changed chunk ? */
1651     if( 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         if( TrackGotoChunkSample( p_input,
1656                                   p_track,
1657                                   p_track->i_chunk + 1,
1658                                   p_track->i_sample ) )
1659         {
1660             msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1661             MP4_TrackUnselect( p_input, p_track );
1662             return( VLC_EGENERIC );
1663         }
1664     }
1665
1666     return( VLC_SUCCESS );
1667 }
1668
1669