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