]> git.sesse.net Git - vlc/blob - modules/demux/mp4/mp4.c
mp4: kill warning about sign
[vlc] / modules / demux / mp4 / mp4.c
1 /*****************************************************************************
2  * mp4.c : MP4 file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004, 2010 the VideoLAN team
5  *
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33
34 #include <vlc_demux.h>
35 #include <vlc_charset.h>                           /* EnsureUTF8 */
36 #include <vlc_meta.h>                              /* vlc_meta_t, vlc_meta_ */
37 #include <vlc_input.h>
38
39 #include "libmp4.h"
40 #include "drms.h"
41 #include "id3genres.h"                             /* for ATOM_gnre */
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 vlc_module_begin ()
50     set_category( CAT_INPUT )
51     set_subcategory( SUBCAT_INPUT_DEMUX )
52     set_description( N_("MP4 stream demuxer") )
53     set_shortname( N_("MP4") )
54     set_capability( "demux", 240 )
55     set_callbacks( Open, Close )
56 vlc_module_end ()
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static int   Demux   ( demux_t * );
62 static int   DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
63 static int   Seek    ( demux_t *, mtime_t );
64 static int   Control ( demux_t *, int, va_list );
65
66 /* Contain all information about a chunk */
67 typedef struct
68 {
69     uint64_t     i_offset; /* absolute position of this chunk in the file */
70     uint32_t     i_sample_description_index; /* index for SampleEntry to use */
71     uint32_t     i_sample_count; /* how many samples in this chunk */
72     uint32_t     i_sample_first; /* index of the first sample in this chunk */
73
74     /* now provide way to calculate pts, dts, and offset without too
75         much memory and with fast access */
76
77     /* with this we can calculate dts/pts without waste memory */
78     uint64_t     i_first_dts;   /* DTS of the first sample */
79     uint64_t     i_last_dts;    /* DTS of the last sample */
80     uint32_t     *p_sample_count_dts;
81     uint32_t     *p_sample_delta_dts;   /* dts delta */
82
83     uint32_t     *p_sample_count_pts;
84     int32_t      *p_sample_offset_pts;  /* pts-dts */
85
86     /* TODO if needed add pts
87         but quickly *add* support for edts and seeking */
88
89 } mp4_chunk_t;
90
91  /* Contain all needed information for read all track with vlc */
92 typedef struct
93 {
94     unsigned int i_track_ID;/* this should be unique */
95
96     int b_ok;               /* The track is usable */
97     int b_enable;           /* is the trak enable by default */
98     bool b_selected;  /* is the trak being played */
99     bool b_chapter;   /* True when used for chapter only */
100
101     bool b_mac_encoding;
102
103     es_format_t fmt;
104     es_out_id_t *p_es;
105
106     /* display size only ! */
107     int i_width;
108     int i_height;
109
110     /* more internal data */
111     uint64_t        i_timescale;    /* time scale for this track only */
112
113     /* elst */
114     int             i_elst;         /* current elst */
115     int64_t         i_elst_time;    /* current elst start time (in movie time scale)*/
116     MP4_Box_t       *p_elst;        /* elst (could be NULL) */
117
118     /* give the next sample to read, i_chunk is to find quickly where
119       the sample is located */
120     uint32_t         i_sample;       /* next sample to read */
121     uint32_t         i_chunk;        /* chunk where next sample is stored */
122     /* total count of chunk and sample */
123     uint32_t         i_chunk_count;
124     uint32_t         i_sample_count;
125
126     mp4_chunk_t    *chunk; /* always defined  for each chunk */
127
128     /* sample size, p_sample_size defined only if i_sample_size == 0
129         else i_sample_size is size for all sample */
130     uint32_t         i_sample_size;
131     uint32_t         *p_sample_size; /* XXX perhaps add file offset if take
132                                     too much time to do sumations each time*/
133
134     MP4_Box_t *p_stbl;  /* will contain all timing information */
135     MP4_Box_t *p_stsd;  /* will contain all data to initialize decoder */
136     MP4_Box_t *p_sample;/* point on actual sdsd */
137
138     bool b_drms;
139     void      *p_drms;
140     MP4_Box_t *p_skcr;
141
142 } mp4_track_t;
143
144
145 struct demux_sys_t
146 {
147     MP4_Box_t    *p_root;      /* container for the whole file */
148
149     mtime_t      i_pcr;
150
151     uint64_t     i_time;         /* time position of the presentation
152                                   * in movie timescale */
153     uint64_t     i_timescale;    /* movie time scale */
154     uint64_t     i_duration;     /* movie duration */
155     unsigned int i_tracks;       /* number of tracks */
156     mp4_track_t  *track;         /* array of track */
157     float        f_fps;          /* number of frame per seconds */
158
159     /* */
160     MP4_Box_t    *p_tref_chap;
161
162     /* */
163     input_title_t *p_title;
164 };
165
166 /*****************************************************************************
167  * Declaration of local function
168  *****************************************************************************/
169 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t  *, bool b_force_enable );
170 static void MP4_TrackDestroy(  mp4_track_t * );
171
172 static int  MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
173 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
174
175 static int  MP4_TrackSeek   ( demux_t *, mp4_track_t *, mtime_t );
176
177 static uint64_t MP4_TrackGetPos    ( mp4_track_t * );
178 static int      MP4_TrackSampleSize( mp4_track_t * );
179 static int      MP4_TrackNextSample( demux_t *, mp4_track_t * );
180 static void     MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
181
182 static void     MP4_UpdateSeekpoint( demux_t * );
183 static const char *MP4_ConvertMacCode( uint16_t );
184
185 /* Return time in s of a track */
186 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
187 {
188 #define chunk p_track->chunk[p_track->i_chunk]
189
190     unsigned int i_index = 0;
191     unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
192     int64_t i_dts = chunk.i_first_dts;
193
194     while( i_sample > 0 )
195     {
196         if( i_sample > chunk.p_sample_count_dts[i_index] )
197         {
198             i_dts += chunk.p_sample_count_dts[i_index] *
199                 chunk.p_sample_delta_dts[i_index];
200             i_sample -= chunk.p_sample_count_dts[i_index];
201             i_index++;
202         }
203         else
204         {
205             i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
206             break;
207         }
208     }
209
210 #undef chunk
211
212     /* now handle elst */
213     if( p_track->p_elst )
214     {
215         demux_sys_t         *p_sys = p_demux->p_sys;
216         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
217
218         /* convert to offset */
219         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
220               elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
221             elst->i_media_time[p_track->i_elst] > 0 )
222         {
223             i_dts -= elst->i_media_time[p_track->i_elst];
224         }
225
226         /* add i_elst_time */
227         i_dts += p_track->i_elst_time * p_track->i_timescale /
228             p_sys->i_timescale;
229
230         if( i_dts < 0 ) i_dts = 0;
231     }
232
233     return INT64_C(1000000) * i_dts / p_track->i_timescale;
234 }
235
236 static inline int64_t MP4_TrackGetPTSDelta( mp4_track_t *p_track )
237 {
238     mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
239     unsigned int i_index = 0;
240     unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
241
242     if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
243         return -1;
244
245     for( i_index = 0;; i_index++ )
246     {
247         if( i_sample < ck->p_sample_count_pts[i_index] )
248             return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
249                    (int64_t)p_track->i_timescale;
250
251         i_sample -= ck->p_sample_count_pts[i_index];
252     }
253 }
254
255 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
256 {
257     return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
258 }
259
260 static void LoadChapter( demux_t  *p_demux );
261
262 /*****************************************************************************
263  * Open: check file and initializes MP4 structures
264  *****************************************************************************/
265 static int Open( vlc_object_t * p_this )
266 {
267     demux_t  *p_demux = (demux_t *)p_this;
268     demux_sys_t     *p_sys;
269
270     const uint8_t   *p_peek;
271
272     MP4_Box_t       *p_ftyp;
273     MP4_Box_t       *p_rmra;
274     MP4_Box_t       *p_mvhd;
275     MP4_Box_t       *p_trak;
276
277     unsigned int    i;
278     bool      b_seekable;
279     bool      b_enabled_es;
280
281     /* A little test to see if it could be a mp4 */
282     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
283
284     switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
285     {
286         case ATOM_ftyp:
287         case ATOM_moov:
288         case ATOM_foov:
289         case ATOM_moof:
290         case ATOM_mdat:
291         case ATOM_udta:
292         case ATOM_free:
293         case ATOM_skip:
294         case ATOM_wide:
295         case VLC_FOURCC( 'p', 'n', 'o', 't' ):
296             break;
297          default:
298             return VLC_EGENERIC;
299     }
300
301     /* I need to seek */
302     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
303     if( !b_seekable )
304     {
305         msg_Warn( p_demux, "MP4 plugin discarded (not fastseekable)" );
306         return VLC_EGENERIC;
307     }
308
309     /*Set exported functions */
310     p_demux->pf_demux = Demux;
311     p_demux->pf_control = Control;
312
313     /* create our structure that will contains all data */
314     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
315
316     /* Now load all boxes ( except raw data ) */
317     if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
318     {
319         msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
320         goto error;
321     }
322
323     MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
324
325     if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
326     {
327         switch( p_ftyp->data.p_ftyp->i_major_brand )
328         {
329             case( ATOM_isom ):
330                 msg_Dbg( p_demux,
331                          "ISO Media file (isom) version %d.",
332                          p_ftyp->data.p_ftyp->i_minor_version );
333                 break;
334             case( ATOM_3gp4 ):
335             case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
336             case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
337             case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
338                 msg_Dbg( p_demux, "3GPP Media file Release: %c",
339 #ifdef WORDS_BIGENDIAN
340                         p_ftyp->data.p_ftyp->i_major_brand
341 #else
342                         p_ftyp->data.p_ftyp->i_major_brand >> 24
343 #endif
344                         );
345                 break;
346             case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
347                 msg_Dbg( p_demux, "Apple QuickTime file" );
348                 break;
349             default:
350                 msg_Dbg( p_demux,
351                          "unrecognized major file specification (%4.4s).",
352                           (char*)&p_ftyp->data.p_ftyp->i_major_brand );
353                 break;
354         }
355     }
356     else
357     {
358         msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
359     }
360
361     /* the file need to have one moov box */
362     if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
363     {
364         MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
365
366         if( !p_foov )
367         {
368             /* search also for moof box used by smoothstreaming */
369             p_foov = MP4_BoxGet( p_sys->p_root, "/moof" );
370             if( !p_foov )
371             {
372                 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
373                 goto error;
374             }
375         }
376         /* we have a free box as a moov, rename it */
377         p_foov->i_type = ATOM_moov;
378     }
379
380     if( ( p_rmra = MP4_BoxGet( p_sys->p_root,  "/moov/rmra" ) ) )
381     {
382         int        i_count = MP4_BoxCount( p_rmra, "rmda" );
383         int        i;
384
385         msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
386
387         input_thread_t *p_input = demux_GetParentInput( p_demux );
388         input_item_t *p_current = input_GetItem( p_input );
389
390         input_item_node_t *p_subitems = input_item_node_Create( p_current );
391
392         for( i = 0; i < i_count; i++ )
393         {
394             MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
395             char      *psz_ref;
396             uint32_t  i_ref_type;
397
398             if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
399             {
400                 continue;
401             }
402             i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
403
404             msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
405                      psz_ref, (char*)&i_ref_type );
406
407             if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
408             {
409                 if( strstr( psz_ref, "qt5gateQT" ) )
410                 {
411                     msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
412                     continue;
413                 }
414                 if( !strncmp( psz_ref, "http://", 7 ) ||
415                     !strncmp( psz_ref, "rtsp://", 7 ) )
416                 {
417                     ;
418                 }
419                 else
420                 {
421                     char *psz_absolute;
422                     char *psz_path = strdup( p_demux->psz_location );
423                     char *end = strrchr( psz_path, '/' );
424                     if( end ) end[1] = '\0';
425                     else *psz_path = '\0';
426
427                     if( asprintf( &psz_absolute, "%s://%s%s",
428                                   p_demux->psz_access, psz_path, psz_ref ) < 0 )
429                     {
430                         free( psz_ref );
431                         free( psz_path );
432                         vlc_object_release( p_input) ;
433                         return VLC_ENOMEM;
434                     }
435
436                     free( psz_ref );
437                     psz_ref = psz_absolute;
438                     free( psz_path );
439                 }
440                 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
441                 input_item_t *p_input = input_item_New( psz_ref, NULL );
442                 input_item_CopyOptions( p_current, p_input );
443                 input_item_node_AppendItem( p_subitems, p_input );
444                 vlc_gc_decref( p_input );
445             }
446             else
447             {
448                 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
449                          (char*)&p_rdrf->data.p_rdrf->i_ref_type );
450             }
451             free( psz_ref );
452         }
453         input_item_node_PostAndDelete( p_subitems );
454         vlc_object_release( p_input );
455     }
456
457     if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
458     {
459         if( !p_rmra )
460         {
461             msg_Err( p_demux, "cannot find /moov/mvhd" );
462             goto error;
463         }
464         else
465         {
466             msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
467             p_demux->pf_demux = DemuxRef;
468             return VLC_SUCCESS;
469         }
470     }
471     else
472     {
473         p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
474         if( p_sys->i_timescale == 0 )
475         {
476             msg_Err( p_this, "bad timescale" );
477             goto error;
478         }
479         p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
480     }
481
482     if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
483     {
484         msg_Err( p_demux, "cannot find any /moov/trak" );
485         goto error;
486     }
487     msg_Dbg( p_demux, "found %d track%c",
488                         p_sys->i_tracks,
489                         p_sys->i_tracks ? 's':' ' );
490
491     /* allocate memory */
492     p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
493     if( p_sys->track == NULL )
494         goto error;
495     memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
496
497     /* Search the first chap reference (like quicktime) and
498      * check that at least 1 stream is enabled */
499     p_sys->p_tref_chap = NULL;
500     b_enabled_es = false;
501     for( i = 0; i < p_sys->i_tracks; i++ )
502     {
503         MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
504
505
506         MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
507         if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
508             b_enabled_es = true;
509
510         MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
511         if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
512             p_sys->p_tref_chap = p_chap;
513     }
514
515     /* now process each track and extract all useful information */
516     for( i = 0; i < p_sys->i_tracks; i++ )
517     {
518         p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
519         MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
520
521         if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
522         {
523             const char *psz_cat;
524             switch( p_sys->track[i].fmt.i_cat )
525             {
526                 case( VIDEO_ES ):
527                     psz_cat = "video";
528                     break;
529                 case( AUDIO_ES ):
530                     psz_cat = "audio";
531                     break;
532                 case( SPU_ES ):
533                     psz_cat = "subtitle";
534                     break;
535
536                 default:
537                     psz_cat = "unknown";
538                     break;
539             }
540
541             msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
542                      p_sys->track[i].i_track_ID, psz_cat,
543                      p_sys->track[i].b_enable ? "enable":"disable",
544                      p_sys->track[i].fmt.psz_language ?
545                      p_sys->track[i].fmt.psz_language : "undef" );
546         }
547         else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
548         {
549             msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
550                      p_sys->track[i].i_track_ID,
551                      p_sys->track[i].fmt.psz_language ?
552                      p_sys->track[i].fmt.psz_language : "undef" );
553         }
554         else
555         {
556             msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
557                      p_sys->track[i].i_track_ID );
558         }
559     }
560
561     /* */
562     LoadChapter( p_demux );
563
564     return VLC_SUCCESS;
565
566 error:
567     if( p_sys->p_root )
568     {
569         MP4_BoxFree( p_demux->s, p_sys->p_root );
570     }
571     free( p_sys );
572     return VLC_EGENERIC;
573 }
574
575 /*****************************************************************************
576  * Demux: read packet and send them to decoders
577  *****************************************************************************
578  * TODO check for newly selected track (ie audio upt to now )
579  *****************************************************************************/
580 static int Demux( demux_t *p_demux )
581 {
582     demux_sys_t *p_sys = p_demux->p_sys;
583     unsigned int i_track;
584
585
586     unsigned int i_track_selected;
587
588     /* check for newly selected/unselected track */
589     for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
590          i_track++ )
591     {
592         mp4_track_t *tk = &p_sys->track[i_track];
593         bool b;
594
595         if( !tk->b_ok || tk->b_chapter ||
596             ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
597         {
598             continue;
599         }
600
601         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
602
603         if( tk->b_selected && !b )
604         {
605             MP4_TrackUnselect( p_demux, tk );
606         }
607         else if( !tk->b_selected && b)
608         {
609             MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
610         }
611
612         if( tk->b_selected )
613         {
614             i_track_selected++;
615         }
616     }
617
618     if( i_track_selected <= 0 )
619     {
620         p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
621         if( p_sys->i_timescale > 0 )
622         {
623             int64_t i_length = (mtime_t)1000000 *
624                                (mtime_t)p_sys->i_duration /
625                                (mtime_t)p_sys->i_timescale;
626             if( MP4_GetMoviePTS( p_sys ) >= i_length )
627                 return 0;
628             return 1;
629         }
630
631         msg_Warn( p_demux, "no track selected, exiting..." );
632         return 0;
633     }
634
635     /* */
636     MP4_UpdateSeekpoint( p_demux );
637
638     /* first wait for the good time to read a packet */
639     es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
640
641     p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
642
643     /* we will read 100ms for each stream so ...*/
644     p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
645
646     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
647     {
648         mp4_track_t *tk = &p_sys->track[i_track];
649
650         if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
651             continue;
652
653         while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
654         {
655 #if 0
656             msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
657                      MP4_TrackGetDTS( p_demux, tk ),
658                      MP4_GetMoviePTS( p_sys ) );
659 #endif
660
661             if( MP4_TrackSampleSize( tk ) > 0 )
662             {
663                 block_t *p_block;
664                 int64_t i_delta;
665
666                 /* go,go go ! */
667                 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
668                 {
669                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
670                               tk->i_track_ID );
671                     MP4_TrackUnselect( p_demux, tk );
672                     break;
673                 }
674
675                 /* now read pes */
676                 if( !(p_block =
677                          stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
678                 {
679                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
680                               tk->i_track_ID );
681                     MP4_TrackUnselect( p_demux, tk );
682                     break;
683                 }
684
685                 if( tk->b_drms && tk->p_drms )
686                 {
687                     if( tk->p_skcr )
688                     {
689                         uint32_t p_key[4];
690                         drms_get_p_key( tk->p_drms, p_key );
691
692                         for( size_t i_pos = tk->p_skcr->data.p_skcr->i_init; i_pos < p_block->i_buffer; )
693                         {
694                             int n = __MIN( tk->p_skcr->data.p_skcr->i_encr, p_block->i_buffer - i_pos );
695                             drms_decrypt( tk->p_drms, (uint32_t*)&p_block->p_buffer[i_pos], n, p_key );
696                             i_pos += n;
697                             i_pos += __MIN( tk->p_skcr->data.p_skcr->i_decr, p_block->i_buffer - i_pos );
698                         }
699                     }
700                     else
701                     {
702                         drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
703                                       p_block->i_buffer, NULL );
704                     }
705                 }
706                 else if( tk->fmt.i_cat == SPU_ES )
707                 {
708                     if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
709                         p_block->i_buffer >= 2 )
710                     {
711                         size_t i_size = GetWBE( p_block->p_buffer );
712
713                         if( i_size + 2 <= p_block->i_buffer )
714                         {
715                             char *p;
716                             /* remove the length field, and append a '\0' */
717                             memmove( &p_block->p_buffer[0],
718                                      &p_block->p_buffer[2], i_size );
719                             p_block->p_buffer[i_size] = '\0';
720                             p_block->i_buffer = i_size + 1;
721
722                             /* convert \r -> \n */
723                             while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
724                             {
725                                 *p = '\n';
726                             }
727                         }
728                         else
729                         {
730                             /* Invalid */
731                             p_block->i_buffer = 0;
732                         }
733                     }
734                 }
735                 /* dts */
736                 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
737                 /* pts */
738                 i_delta = MP4_TrackGetPTSDelta( tk );
739                 if( i_delta != -1 )
740                     p_block->i_pts = p_block->i_dts + i_delta;
741                 else if( tk->fmt.i_cat != VIDEO_ES )
742                     p_block->i_pts = p_block->i_dts;
743                 else
744                     p_block->i_pts = VLC_TS_INVALID;
745
746                 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
747                     es_out_Send( p_demux->out, tk->p_es, p_block );
748             }
749
750             /* Next sample */
751             if( MP4_TrackNextSample( p_demux, tk ) )
752                 break;
753         }
754     }
755
756     return 1;
757 }
758
759 static void MP4_UpdateSeekpoint( demux_t *p_demux )
760 {
761     demux_sys_t *p_sys = p_demux->p_sys;
762     int64_t i_time;
763     int i;
764     if( !p_sys->p_title )
765         return;
766     i_time = MP4_GetMoviePTS( p_sys );
767     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
768     {
769         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
770             break;
771     }
772     i--;
773
774     if( i != p_demux->info.i_seekpoint && i >= 0 )
775     {
776         p_demux->info.i_seekpoint = i;
777         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
778     }
779 }
780 /*****************************************************************************
781  * Seek: Go to i_date
782 ******************************************************************************/
783 static int Seek( demux_t *p_demux, mtime_t i_date )
784 {
785     demux_sys_t *p_sys = p_demux->p_sys;
786     unsigned int i_track;
787
788     /* First update update global time */
789     p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
790     p_sys->i_pcr  = i_date;
791
792     /* Now for each stream try to go to this time */
793     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
794     {
795         mp4_track_t *tk = &p_sys->track[i_track];
796         MP4_TrackSeek( p_demux, tk, i_date );
797     }
798     MP4_UpdateSeekpoint( p_demux );
799
800     es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
801
802     return VLC_SUCCESS;
803 }
804
805 /*****************************************************************************
806  * Control:
807  *****************************************************************************/
808 static int Control( demux_t *p_demux, int i_query, va_list args )
809 {
810     demux_sys_t *p_sys = p_demux->p_sys;
811
812     double f, *pf;
813     int64_t i64, *pi64;
814
815     switch( i_query )
816     {
817         case DEMUX_GET_POSITION:
818             pf = (double*)va_arg( args, double * );
819             if( p_sys->i_duration > 0 )
820             {
821                 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
822             }
823             else
824             {
825                 *pf = 0.0;
826             }
827             return VLC_SUCCESS;
828
829         case DEMUX_SET_POSITION:
830             f = (double)va_arg( args, double );
831             if( p_sys->i_timescale > 0 )
832             {
833                 i64 = (int64_t)( f * (double)1000000 *
834                                  (double)p_sys->i_duration /
835                                  (double)p_sys->i_timescale );
836                 return Seek( p_demux, i64 );
837             }
838             else return VLC_SUCCESS;
839
840         case DEMUX_GET_TIME:
841             pi64 = (int64_t*)va_arg( args, int64_t * );
842             if( p_sys->i_timescale > 0 )
843             {
844                 *pi64 = (mtime_t)1000000 *
845                         (mtime_t)p_sys->i_time /
846                         (mtime_t)p_sys->i_timescale;
847             }
848             else *pi64 = 0;
849             return VLC_SUCCESS;
850
851         case DEMUX_SET_TIME:
852             i64 = (int64_t)va_arg( args, int64_t );
853             return Seek( p_demux, i64 );
854
855         case DEMUX_GET_LENGTH:
856             pi64 = (int64_t*)va_arg( args, int64_t * );
857             if( p_sys->i_timescale > 0 )
858             {
859                 *pi64 = (mtime_t)1000000 *
860                         (mtime_t)p_sys->i_duration /
861                         (mtime_t)p_sys->i_timescale;
862             }
863             else *pi64 = 0;
864             return VLC_SUCCESS;
865
866         case DEMUX_GET_FPS:
867             pf = (double*)va_arg( args, double* );
868             *pf = p_sys->f_fps;
869             return VLC_SUCCESS;
870
871         case DEMUX_GET_META:
872         {
873             vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
874             MP4_Box_t  *p_0xa9xxx;
875
876             MP4_Box_t  *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
877             if( p_udta == NULL )
878             {
879                 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
880                 if( p_udta == NULL )
881                 {
882                     return VLC_EGENERIC;
883                 }
884             }
885
886             for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
887                  p_0xa9xxx = p_0xa9xxx->p_next )
888             {
889
890                 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
891                     continue;
892
893                 /* FIXME FIXME: should convert from whatever the character
894                  * encoding of MP4 meta data is to UTF-8. */
895 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
896     if( psz_utf ) { EnsureUTF8( psz_utf );  \
897                     fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
898
899                 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
900                 switch( p_0xa9xxx->i_type )
901                 {
902                 case ATOM_0xa9nam: /* Full name */
903                     SET( vlc_meta_SetTitle );
904                     break;
905                 case ATOM_0xa9aut:
906                     SET( vlc_meta_SetArtist );
907                     break;
908                 case ATOM_0xa9ART:
909                     SET( vlc_meta_SetArtist );
910                     break;
911                 case ATOM_0xa9cpy:
912                     SET( vlc_meta_SetCopyright );
913                     break;
914                 case ATOM_0xa9day: /* Creation Date */
915                     SET( vlc_meta_SetDate );
916                     break;
917                 case ATOM_0xa9des: /* Description */
918                     SET( vlc_meta_SetDescription );
919                     break;
920                 case ATOM_0xa9gen: /* Genre */
921                     SET( vlc_meta_SetGenre );
922                     break;
923
924                 case ATOM_gnre:
925                     if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
926                         vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
927                     break;
928
929                 case ATOM_0xa9alb: /* Album */
930                     SET( vlc_meta_SetAlbum );
931                     break;
932
933                 case ATOM_0xa9trk: /* Track */
934                     SET( vlc_meta_SetTrackNum );
935                     break;
936                 case ATOM_trkn:
937                 {
938                     char psz_trck[11];
939                     snprintf( psz_trck, sizeof( psz_trck ), "%i",
940                               p_0xa9xxx->data.p_trkn->i_track_number );
941                     vlc_meta_SetTrackNum( p_meta, psz_trck );
942                     break;
943                 }
944                 case ATOM_0xa9cmt: /* Commment */
945                     SET( vlc_meta_SetDescription );
946                     break;
947
948                 case ATOM_0xa9url: /* URL */
949                     SET( vlc_meta_SetURL );
950                     break;
951
952                 case ATOM_0xa9too: /* Encoder Tool */
953                 case ATOM_0xa9enc: /* Encoded By */
954                     SET( vlc_meta_SetEncodedBy );
955                     break;
956
957                 default:
958                     break;
959                 }
960 #undef SET
961                 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
962                 {
963                     { ATOM_0xa9wrt, N_("Writer") },
964                     { ATOM_0xa9com, N_("Composr") },
965                     { ATOM_0xa9prd, N_("Producer") },
966                     { ATOM_0xa9inf, N_("Information") },
967                     { ATOM_0xa9dir, N_("Director") },
968                     { ATOM_0xa9dis, N_("Disclaimer") },
969                     { ATOM_0xa9req, N_("Requirements") },
970                     { ATOM_0xa9fmt, N_("Original Format") },
971                     { ATOM_0xa9dsa, N_("Display Source As") },
972                     { ATOM_0xa9hst, N_("Host Computer") },
973                     { ATOM_0xa9prf, N_("Performers") },
974                     { ATOM_0xa9ope, N_("Original Performer") },
975                     { ATOM_0xa9src, N_("Providers Source Content") },
976                     { ATOM_0xa9wrn, N_("Warning") },
977                     { ATOM_0xa9swr, N_("Software") },
978                     { ATOM_0xa9lyr, N_("Lyrics") },
979                     { ATOM_0xa9mak, N_("Make") },
980                     { ATOM_0xa9mod, N_("Model") },
981                     { ATOM_0xa9PRD, N_("Product") },
982                     { ATOM_0xa9grp, N_("Grouping") },
983                     { 0, "" },
984                 };
985                 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
986                 {
987                     if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
988                     {
989                         char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" );
990                         if( psz_utf )
991                         {
992                              EnsureUTF8( psz_utf );
993                              vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
994                              free( psz_utf );
995                         }
996                         break;
997                     }
998                 }
999             }
1000             return VLC_SUCCESS;
1001         }
1002
1003         case DEMUX_GET_TITLE_INFO:
1004         {
1005             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1006             int *pi_int    = (int*)va_arg( args, int* );
1007             int *pi_title_offset = (int*)va_arg( args, int* );
1008             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
1009
1010             if( !p_sys->p_title )
1011                 return VLC_EGENERIC;
1012
1013             *pi_int = 1;
1014             *ppp_title = malloc( sizeof( input_title_t**) );
1015             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1016             *pi_title_offset = 0;
1017             *pi_seekpoint_offset = 0;
1018             return VLC_SUCCESS;
1019         }
1020         case DEMUX_SET_TITLE:
1021         {
1022             const int i_title = (int)va_arg( args, int );
1023             if( !p_sys->p_title || i_title != 0 )
1024                 return VLC_EGENERIC;
1025             return VLC_SUCCESS;
1026         }
1027         case DEMUX_SET_SEEKPOINT:
1028         {
1029             const int i_seekpoint = (int)va_arg( args, int );
1030             if( !p_sys->p_title )
1031                 return VLC_EGENERIC;
1032             return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1033         }
1034
1035         case DEMUX_SET_NEXT_DEMUX_TIME:
1036         case DEMUX_SET_GROUP:
1037         case DEMUX_HAS_UNSUPPORTED_META:
1038         case DEMUX_GET_ATTACHMENTS:
1039         case DEMUX_GET_PTS_DELAY:
1040         case DEMUX_CAN_RECORD:
1041             return VLC_EGENERIC;
1042
1043         default:
1044             msg_Warn( p_demux, "control query %u unimplemented", i_query );
1045             return VLC_EGENERIC;
1046     }
1047 }
1048
1049 /*****************************************************************************
1050  * Close: frees unused data
1051  *****************************************************************************/
1052 static void Close ( vlc_object_t * p_this )
1053 {
1054     demux_t *  p_demux = (demux_t *)p_this;
1055     demux_sys_t *p_sys = p_demux->p_sys;
1056     unsigned int i_track;
1057
1058     msg_Dbg( p_demux, "freeing all memory" );
1059
1060     MP4_BoxFree( p_demux->s, p_sys->p_root );
1061     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1062     {
1063         MP4_TrackDestroy(  &p_sys->track[i_track] );
1064     }
1065     FREENULL( p_sys->track );
1066
1067     if( p_sys->p_title )
1068         vlc_input_title_Delete( p_sys->p_title );
1069
1070     free( p_sys );
1071 }
1072
1073
1074
1075 /****************************************************************************
1076  * Local functions, specific to vlc
1077  ****************************************************************************/
1078 /* Chapters */
1079 static void LoadChapterGpac( demux_t  *p_demux, MP4_Box_t *p_chpl )
1080 {
1081     demux_sys_t *p_sys = p_demux->p_sys;
1082     int i;
1083
1084     p_sys->p_title = vlc_input_title_New();
1085     for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1086     {
1087         seekpoint_t *s = vlc_seekpoint_New();
1088
1089         s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1090         EnsureUTF8( s->psz_name );
1091         s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1092         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1093     }
1094 }
1095 static void LoadChapterApple( demux_t  *p_demux, mp4_track_t *tk )
1096 {
1097     demux_sys_t *p_sys = p_demux->p_sys;
1098
1099     for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1100     {
1101         const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1102         const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
1103         const unsigned int i_size = MP4_TrackSampleSize( tk );
1104
1105         if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1106         {
1107             char p_buffer[256];
1108             const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1109             const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1110
1111             if( i_len > 0 )
1112             {
1113                 seekpoint_t *s = vlc_seekpoint_New();
1114
1115                 s->psz_name = strndup( &p_buffer[2], i_len );
1116                 EnsureUTF8( s->psz_name );
1117
1118                 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1119
1120                 if( !p_sys->p_title )
1121                     p_sys->p_title = vlc_input_title_New();
1122                 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1123             }
1124         }
1125         if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1126                               tk->chunk[tk->i_chunk].i_sample_count )
1127             tk->i_chunk++;
1128     }
1129 }
1130 static void LoadChapter( demux_t  *p_demux )
1131 {
1132     demux_sys_t *p_sys = p_demux->p_sys;
1133     MP4_Box_t *p_chpl;
1134
1135     if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1136     {
1137         LoadChapterGpac( p_demux, p_chpl );
1138     }
1139     else if( p_sys->p_tref_chap )
1140     {
1141         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1142         unsigned int i, j;
1143
1144         /* Load the first subtitle track like quicktime */
1145         for( i = 0; i < p_chap->i_entry_count; i++ )
1146         {
1147             for( j = 0; j < p_sys->i_tracks; j++ )
1148             {
1149                 mp4_track_t *tk = &p_sys->track[j];
1150                 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1151                     tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1152                     break;
1153             }
1154             if( j < p_sys->i_tracks )
1155             {
1156                 LoadChapterApple( p_demux, &p_sys->track[j] );
1157                 break;
1158             }
1159         }
1160     }
1161 }
1162
1163 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1164 static int TrackCreateChunksIndex( demux_t *p_demux,
1165                                    mp4_track_t *p_demux_track )
1166 {
1167     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1168     MP4_Box_t *p_stsc;
1169
1170     unsigned int i_chunk;
1171     unsigned int i_index, i_last;
1172
1173     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1174           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1175         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1176     {
1177         return( VLC_EGENERIC );
1178     }
1179
1180     p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1181     if( !p_demux_track->i_chunk_count )
1182     {
1183         msg_Warn( p_demux, "no chunk defined" );
1184         return( VLC_EGENERIC );
1185     }
1186     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1187                                    sizeof( mp4_chunk_t ) );
1188     if( p_demux_track->chunk == NULL )
1189     {
1190         return VLC_ENOMEM;
1191     }
1192
1193     /* first we read chunk offset */
1194     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1195     {
1196         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1197
1198         ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1199
1200         ck->i_first_dts = 0;
1201         ck->p_sample_count_dts = NULL;
1202         ck->p_sample_delta_dts = NULL;
1203         ck->p_sample_count_pts = NULL;
1204         ck->p_sample_offset_pts = NULL;
1205     }
1206
1207     /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1208         to be used for the sample XXX begin to 1
1209         We construct it begining at the end */
1210     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1211     i_index = p_stsc->data.p_stsc->i_entry_count;
1212     if( !i_index )
1213     {
1214         msg_Warn( p_demux, "cannot read chunk table or table empty" );
1215         return( VLC_EGENERIC );
1216     }
1217
1218     while( i_index-- )
1219     {
1220         for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1221              i_chunk < i_last; i_chunk++ )
1222         {
1223             if( i_chunk >= p_demux_track->i_chunk_count )
1224             {
1225                 msg_Warn( p_demux, "corrupted chunk table" );
1226                 return VLC_EGENERIC;
1227             }
1228
1229             p_demux_track->chunk[i_chunk].i_sample_description_index =
1230                     p_stsc->data.p_stsc->i_sample_description_index[i_index];
1231             p_demux_track->chunk[i_chunk].i_sample_count =
1232                     p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1233         }
1234         i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1235     }
1236
1237     p_demux_track->chunk[0].i_sample_first = 0;
1238     for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1239     {
1240         p_demux_track->chunk[i_chunk].i_sample_first =
1241             p_demux_track->chunk[i_chunk-1].i_sample_first +
1242                 p_demux_track->chunk[i_chunk-1].i_sample_count;
1243     }
1244
1245     msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1246              p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1247
1248     return VLC_SUCCESS;
1249 }
1250
1251 static int TrackCreateSamplesIndex( demux_t *p_demux,
1252                                     mp4_track_t *p_demux_track )
1253 {
1254     MP4_Box_t *p_box;
1255     MP4_Box_data_stsz_t *stsz;
1256     MP4_Box_data_stts_t *stts;
1257     /* TODO use also stss and stsh table for seeking */
1258     /* FIXME use edit table */
1259     int64_t i_sample;
1260     int64_t i_chunk;
1261
1262     int64_t i_index;
1263     int64_t i_index_sample_used;
1264
1265     int64_t i_next_dts;
1266
1267     /* Find stsz
1268      *  Gives the sample size for each samples. There is also a stz2 table
1269      *  (compressed form) that we need to implement TODO */
1270     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1271     if( !p_box )
1272     {
1273         /* FIXME and stz2 */
1274         msg_Warn( p_demux, "cannot find STSZ box" );
1275         return VLC_EGENERIC;
1276     }
1277     stsz = p_box->data.p_stsz;
1278
1279     /* Find stts
1280      *  Gives mapping between sample and decoding time
1281      */
1282     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1283     if( !p_box )
1284     {
1285         msg_Warn( p_demux, "cannot find STTS box" );
1286         return VLC_EGENERIC;
1287     }
1288     stts = p_box->data.p_stts;
1289
1290     /* Use stsz table to create a sample number -> sample size table */
1291     p_demux_track->i_sample_count = stsz->i_sample_count;
1292     if( stsz->i_sample_size )
1293     {
1294         /* 1: all sample have the same size, so no need to construct a table */
1295         p_demux_track->i_sample_size = stsz->i_sample_size;
1296         p_demux_track->p_sample_size = NULL;
1297     }
1298     else
1299     {
1300         /* 2: each sample can have a different size */
1301         p_demux_track->i_sample_size = 0;
1302         p_demux_track->p_sample_size =
1303             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1304         if( p_demux_track->p_sample_size == NULL )
1305             return VLC_ENOMEM;
1306
1307         for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1308         {
1309             p_demux_track->p_sample_size[i_sample] =
1310                     stsz->i_entry_size[i_sample];
1311         }
1312     }
1313
1314     /* Use stts table to create a sample number -> dts table.
1315      * XXX: if we don't want to waste too much memory, we can't expand
1316      *  the box! so each chunk will contain an "extract" of this table
1317      *  for fast research (problem with raw stream where a sample is sometime
1318      *  just channels*bits_per_sample/8 */
1319
1320     i_next_dts = 0;
1321     i_index = 0; i_index_sample_used = 0;
1322     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1323     {
1324         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1325         int64_t i_entry, i_sample_count, i;
1326
1327         /* save first dts */
1328         ck->i_first_dts = i_next_dts;
1329         ck->i_last_dts  = i_next_dts;
1330
1331         /* count how many entries are needed for this chunk
1332          * for p_sample_delta_dts and p_sample_count_dts */
1333         i_sample_count = ck->i_sample_count;
1334
1335         i_entry = 0;
1336         while( i_sample_count > 0 )
1337         {
1338             i_sample_count -= stts->i_sample_count[i_index+i_entry];
1339             /* don't count already used sample in this entry */
1340             if( i_entry == 0 )
1341                 i_sample_count += i_index_sample_used;
1342
1343             i_entry++;
1344         }
1345
1346         /* allocate them */
1347         ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1348         ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1349
1350         if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1351             return VLC_ENOMEM;
1352
1353         /* now copy */
1354         i_sample_count = ck->i_sample_count;
1355         for( i = 0; i < i_entry; i++ )
1356         {
1357             int64_t i_used;
1358             int64_t i_rest;
1359
1360             i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1361
1362             i_used = __MIN( i_rest, i_sample_count );
1363
1364             i_index_sample_used += i_used;
1365             i_sample_count -= i_used;
1366             i_next_dts += i_used * stts->i_sample_delta[i_index];
1367
1368             ck->p_sample_count_dts[i] = i_used;
1369             ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1370             if( i_used > 0 )
1371                 ck->i_last_dts = i_next_dts - ck->p_sample_delta_dts[i];
1372
1373             if( i_index_sample_used >= stts->i_sample_count[i_index] )
1374             {
1375                 i_index++;
1376                 i_index_sample_used = 0;
1377             }
1378         }
1379     }
1380
1381     /* Find ctts
1382      *  Gives the delta between decoding time (dts) and composition table (pts)
1383      */
1384     p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1385     if( p_box )
1386     {
1387         MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1388
1389         msg_Warn( p_demux, "CTTS table" );
1390
1391         /* Create pts-dts table per chunk */
1392         i_index = 0; i_index_sample_used = 0;
1393         for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1394         {
1395             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1396             int64_t i_entry, i_sample_count, i;
1397
1398             /* count how many entries are needed for this chunk
1399              * for p_sample_delta_dts and p_sample_count_dts */
1400             i_sample_count = ck->i_sample_count;
1401
1402             i_entry = 0;
1403             while( i_sample_count > 0 )
1404             {
1405                 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1406
1407                 /* don't count already used sample in this entry */
1408                 if( i_entry == 0 )
1409                     i_sample_count += i_index_sample_used;
1410
1411                 i_entry++;
1412             }
1413
1414             /* allocate them */
1415             ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1416             ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1417             if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1418                 return VLC_ENOMEM;
1419
1420             /* now copy */
1421             i_sample_count = ck->i_sample_count;
1422             for( i = 0; i < i_entry; i++ )
1423             {
1424                 int64_t i_used;
1425                 int64_t i_rest;
1426
1427                 i_rest = ctts->i_sample_count[i_index] -
1428                     i_index_sample_used;
1429
1430                 i_used = __MIN( i_rest, i_sample_count );
1431
1432                 i_index_sample_used += i_used;
1433                 i_sample_count -= i_used;
1434
1435                 ck->p_sample_count_pts[i] = i_used;
1436                 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1437
1438                 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1439                 {
1440                     i_index++;
1441                     i_index_sample_used = 0;
1442                 }
1443             }
1444         }
1445     }
1446
1447     msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1448              p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1449              i_next_dts / p_demux_track->i_timescale );
1450
1451     return VLC_SUCCESS;
1452 }
1453
1454 /**
1455  * It computes the sample rate for a video track using the given sample
1456  * description index
1457  */
1458 static void TrackGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
1459                                   const mp4_track_t *p_track,
1460                                   unsigned i_sd_index,
1461                                   unsigned i_chunk )
1462 {
1463     *pi_num = 0;
1464     *pi_den = 0;
1465
1466     if( p_track->i_chunk_count <= 0 )
1467         return;
1468
1469     /* */
1470     const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1471     while( p_chunk > &p_track->chunk[0] &&
1472            p_chunk[-1].i_sample_description_index == i_sd_index )
1473     {
1474         p_chunk--;
1475     }
1476
1477     uint64_t i_sample = 0;
1478     uint64_t i_first_dts = p_chunk->i_first_dts;
1479     uint64_t i_last_dts;
1480     do
1481     {
1482         i_sample += p_chunk->i_sample_count;
1483         i_last_dts = p_chunk->i_last_dts;
1484         p_chunk++;
1485     }
1486     while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1487            p_chunk->i_sample_description_index == i_sd_index );
1488
1489     if( i_sample > 1 && i_first_dts < i_last_dts )
1490         vlc_ureduce( pi_num, pi_den,
1491                      ( i_sample - 1) *  p_track->i_timescale,
1492                      i_last_dts - i_first_dts,
1493                      UINT16_MAX);
1494 }
1495
1496 /*
1497  * TrackCreateES:
1498  * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1499  */
1500 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1501                           unsigned int i_chunk, es_out_id_t **pp_es )
1502 {
1503     const unsigned i_sample_description_index =
1504         p_track->chunk[i_chunk].i_sample_description_index;
1505     MP4_Box_t   *p_sample;
1506     MP4_Box_t   *p_esds;
1507     MP4_Box_t   *p_frma;
1508     MP4_Box_t   *p_enda;
1509     MP4_Box_t   *p_pasp;
1510
1511     if( pp_es )
1512         *pp_es = NULL;
1513
1514     if( !i_sample_description_index )
1515     {
1516         msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1517                   p_track->i_track_ID );
1518         return VLC_EGENERIC;
1519     }
1520
1521     p_sample = MP4_BoxGet(  p_track->p_stsd, "[%d]",
1522                             i_sample_description_index - 1 );
1523
1524     if( !p_sample ||
1525         ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1526     {
1527         msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1528                   p_track->i_track_ID );
1529         return VLC_EGENERIC;
1530     }
1531
1532     p_track->p_sample = p_sample;
1533
1534     if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1535     {
1536         msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1537
1538         p_sample->i_type = p_frma->data.p_frma->i_type;
1539     }
1540
1541     p_enda = MP4_BoxGet( p_sample, "wave/enda" );
1542     if( !p_enda )
1543         p_enda = MP4_BoxGet( p_sample, "enda" );
1544
1545     p_pasp = MP4_BoxGet( p_sample, "pasp" );
1546
1547     if( p_track->fmt.i_cat == AUDIO_ES && ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1548     {
1549         MP4_Box_data_sample_soun_t *p_soun;
1550
1551         p_soun = p_sample->data.p_sample_soun;
1552
1553         if( p_soun->i_qt_version == 0 )
1554         {
1555             switch( p_sample->i_type )
1556             {
1557                 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1558                     p_soun->i_qt_version = 1;
1559                     p_soun->i_sample_per_packet = 64;
1560                     p_soun->i_bytes_per_packet  = 34;
1561                     p_soun->i_bytes_per_frame   = 34 * p_soun->i_channelcount;
1562                     p_soun->i_bytes_per_sample  = 2;
1563                     break;
1564                 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1565                     p_soun->i_qt_version = 1;
1566                     p_soun->i_sample_per_packet = 6;
1567                     p_soun->i_bytes_per_packet  = 2;
1568                     p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
1569                     p_soun->i_bytes_per_sample  = 2;
1570                     break;
1571                 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1572                     p_soun->i_qt_version = 1;
1573                     p_soun->i_sample_per_packet = 12;
1574                     p_soun->i_bytes_per_packet  = 2;
1575                     p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
1576                     p_soun->i_bytes_per_sample  = 2;
1577                     break;
1578                 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1579                 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1580                     p_soun->i_samplesize = 8;
1581                     p_track->i_sample_size = p_soun->i_channelcount;
1582                     break;
1583                 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1584                 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1585                 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1586                 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1587                     /* What would be the fun if you could trust the .mov */
1588                     p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1589                     break;
1590                 default:
1591                     break;
1592             }
1593
1594         }
1595         else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1596         {
1597             p_soun->i_qt_version = 0;
1598         }
1599     }
1600     else if( p_track->fmt.i_cat == AUDIO_ES && p_sample->data.p_sample_soun->i_qt_version == 1 )
1601     {
1602         MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1603
1604         switch( p_sample->i_type )
1605         {
1606             case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1607             case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1608             {
1609                 if( p_track->i_sample_size > 1 )
1610                     p_soun->i_qt_version = 0;
1611                 break;
1612             }
1613             case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1614             case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1615             case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
1616                 p_soun->i_qt_version = 0;
1617                 break;
1618             default:
1619                 break;
1620         }
1621     }
1622
1623     /* */
1624     switch( p_track->fmt.i_cat )
1625     {
1626     case VIDEO_ES:
1627         p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1628         p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1629         p_track->fmt.video.i_bits_per_pixel =
1630             p_sample->data.p_sample_vide->i_depth;
1631
1632         /* fall on display size */
1633         if( p_track->fmt.video.i_width <= 0 )
1634             p_track->fmt.video.i_width = p_track->i_width;
1635         if( p_track->fmt.video.i_height <= 0 )
1636             p_track->fmt.video.i_height = p_track->i_height;
1637
1638         /* Find out apect ratio from display size */
1639         if( p_track->i_width > 0 && p_track->i_height > 0 &&
1640             /* Work-around buggy muxed files */
1641             p_sample->data.p_sample_vide->i_width != p_track->i_width )
1642         {
1643             p_track->fmt.video.i_sar_num = p_track->i_width  * p_track->fmt.video.i_height;
1644             p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
1645         }
1646         if( p_pasp && p_pasp->data.p_pasp->i_horizontal_spacing > 0 &&
1647                       p_pasp->data.p_pasp->i_vertical_spacing > 0 )
1648         {
1649             p_track->fmt.video.i_sar_num = p_pasp->data.p_pasp->i_horizontal_spacing;
1650             p_track->fmt.video.i_sar_den = p_pasp->data.p_pasp->i_vertical_spacing;
1651         }
1652
1653         /* Support for cropping (eg. in H263 files) */
1654         p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1655         p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1656
1657         /* Frame rate */
1658         TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
1659                               &p_track->fmt.video.i_frame_rate_base,
1660                               p_track, i_sample_description_index, i_chunk );
1661         p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
1662                                 (float)p_track->fmt.video.i_frame_rate_base;
1663         break;
1664
1665     case AUDIO_ES:
1666         p_track->fmt.audio.i_channels =
1667             p_sample->data.p_sample_soun->i_channelcount;
1668         p_track->fmt.audio.i_rate =
1669             p_sample->data.p_sample_soun->i_sampleratehi;
1670         p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1671             p_sample->data.p_sample_soun->i_sampleratehi *
1672                 p_sample->data.p_sample_soun->i_samplesize;
1673         p_track->fmt.audio.i_bitspersample =
1674             p_sample->data.p_sample_soun->i_samplesize;
1675
1676         if( p_track->i_sample_size != 0 &&
1677             p_sample->data.p_sample_soun->i_qt_version == 1 && p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1678         {
1679             msg_Err( p_demux, "Invalid sample per packet value for qt_version 1" );
1680             return VLC_EGENERIC;
1681         }
1682         break;
1683
1684     default:
1685         break;
1686     }
1687
1688
1689     /* It's a little ugly but .. there are special cases */
1690     switch( p_sample->i_type )
1691     {
1692         case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1693         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1694         {
1695             p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1696             break;
1697         }
1698         case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1699         {
1700             MP4_Box_t *p_dac3_box = MP4_BoxGet(  p_sample, "dac3", 0 );
1701
1702             p_track->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1703             if( p_dac3_box )
1704             {
1705                 static const int pi_bitrate[] = {
1706                      32,  40,  48,  56,
1707                      64,  80,  96, 112,
1708                     128, 160, 192, 224,
1709                     256, 320, 384, 448,
1710                     512, 576, 640,
1711                 };
1712                 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
1713                 p_track->fmt.audio.i_channels = 0;
1714                 p_track->fmt.i_bitrate = 0;
1715                 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
1716                     p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
1717                 p_track->fmt.audio.i_bitspersample = 0;
1718             }
1719             break;
1720         }
1721         case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1722         {
1723             p_track->fmt.i_codec = VLC_FOURCC( 'e', 'a', 'c', '3' );
1724             break;
1725         }
1726
1727         case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1728         case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1729         {
1730             MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1731
1732             if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1733                 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1734             else
1735                 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1736
1737             /* Buggy files workaround */
1738             if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1739                 p_sample->data.p_sample_soun->i_sampleratehi) )
1740             {
1741                 MP4_Box_data_sample_soun_t *p_soun =
1742                     p_sample->data.p_sample_soun;
1743
1744                 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1745                           "(%u), making both equal (report any problem).",
1746                           p_track->i_timescale, p_soun->i_sampleratehi );
1747
1748                 if( p_soun->i_sampleratehi )
1749                     p_track->i_timescale = p_soun->i_sampleratehi;
1750                 else
1751                     p_soun->i_sampleratehi = p_track->i_timescale;
1752             }
1753             break;
1754         }
1755
1756         case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1757             p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1758             break;
1759
1760         case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1761         case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1762             p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1763             /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1764             /* FIXME UTF-8 doesn't work here ? */
1765             if( p_track->b_mac_encoding )
1766                 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1767             else
1768                 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1769             break;
1770
1771         case VLC_FOURCC('y','v','1','2'):
1772             p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1773             break;
1774         case VLC_FOURCC('y','u','v','2'):
1775             p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1776             break;
1777
1778         case VLC_FOURCC('i','n','2','4'):
1779             p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1780                                     VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
1781             break;
1782         case VLC_FOURCC('f','l','3','2'):
1783             p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1784                                     VLC_CODEC_F32L : VLC_CODEC_F32B;
1785             break;
1786         case VLC_FOURCC('f','l','6','4'):
1787             p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1788                                     VLC_CODEC_F64L : VLC_CODEC_F64B;
1789             break;
1790         case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
1791         {
1792             MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1793             if( p_soun->i_qt_version == 2 &&
1794                 p_soun->i_qt_description > 20 + 28 )
1795             {
1796                 /* Flags:
1797                  *  0x01: IsFloat
1798                  *  0x02: IsBigEndian
1799                  *  0x04: IsSigned
1800                  */
1801                 static const struct {
1802                     unsigned     i_flags;
1803                     unsigned     i_mask;
1804                     unsigned     i_bits;
1805                     vlc_fourcc_t i_codec;
1806                 } p_formats[] = {
1807                     { 0x01,           0x03, 32, VLC_CODEC_F32L },
1808                     { 0x01,           0x03, 64, VLC_CODEC_F64L },
1809                     { 0x01|0x02,      0x03, 32, VLC_CODEC_F32B },
1810                     { 0x01|0x02,      0x03, 64, VLC_CODEC_F64B },
1811
1812                     { 0x00,           0x05,  8, VLC_CODEC_U8 },
1813                     { 0x00|     0x04, 0x05,  8, VLC_CODEC_S8 },
1814
1815                     { 0x00,           0x07, 16, VLC_CODEC_U16L },
1816                     { 0x00|0x02,      0x07, 16, VLC_CODEC_U16B },
1817                     { 0x00     |0x04, 0x07, 16, VLC_CODEC_S16L },
1818                     { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
1819
1820                     { 0x00,           0x07, 24, VLC_CODEC_U24L },
1821                     { 0x00|0x02,      0x07, 24, VLC_CODEC_U24B },
1822                     { 0x00     |0x04, 0x07, 24, VLC_CODEC_S24L },
1823                     { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
1824
1825                     { 0x00,           0x07, 32, VLC_CODEC_U32L },
1826                     { 0x00|0x02,      0x07, 32, VLC_CODEC_U32B },
1827                     { 0x00     |0x04, 0x07, 32, VLC_CODEC_S32L },
1828                     { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
1829
1830                     {0, 0, 0, 0}
1831                 };
1832                 uint32_t i_bits  = GetDWBE(&p_soun->p_qt_description[20 + 20]);
1833                 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
1834
1835                 for( int i = 0; p_formats[i].i_codec; i++ )
1836                 {
1837                     if( p_formats[i].i_bits == i_bits &&
1838                         (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
1839                     {
1840                         p_track->fmt.i_codec = p_formats[i].i_codec;
1841                         p_track->fmt.audio.i_bitspersample = i_bits;
1842                         p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
1843                         p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
1844
1845                         p_soun->i_qt_version = 0;
1846                         break;
1847                     }
1848                 }
1849             }
1850             break;
1851         }
1852         default:
1853             p_track->fmt.i_codec = p_sample->i_type;
1854             break;
1855     }
1856
1857     /* now see if esds is present and if so create a data packet
1858         with decoder_specific_info  */
1859 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1860     if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1861           ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1862         ( p_esds->data.p_esds )&&
1863         ( p_decconfig ) )
1864     {
1865         /* First update information based on i_objectTypeIndication */
1866         switch( p_decconfig->i_objectTypeIndication )
1867         {
1868             case( 0x20 ): /* MPEG4 VIDEO */
1869                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1870                 break;
1871             case( 0x40):
1872                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1873                 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
1874                      p_decconfig->p_decoder_specific_info[0]       == 0xF8 &&
1875                     (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
1876                 {
1877                     p_track->fmt.i_codec = VLC_CODEC_ALS;
1878                 }
1879                 break;
1880             case( 0x60):
1881             case( 0x61):
1882             case( 0x62):
1883             case( 0x63):
1884             case( 0x64):
1885             case( 0x65): /* MPEG2 video */
1886                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1887                 break;
1888             /* Theses are MPEG2-AAC */
1889             case( 0x66): /* main profile */
1890             case( 0x67): /* Low complexity profile */
1891             case( 0x68): /* Scaleable Sampling rate profile */
1892                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1893                 break;
1894             /* true MPEG 2 audio */
1895             case( 0x69):
1896                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1897                 break;
1898             case( 0x6a): /* MPEG1 video */
1899                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1900                 break;
1901             case( 0x6b): /* MPEG1 audio */
1902                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1903                 break;
1904             case( 0x6c ): /* jpeg */
1905                 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1906                 break;
1907             case( 0x6d ): /* png */
1908                 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1909                 break;
1910             case( 0x6e ): /* jpeg200 */
1911                 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1912                 break;
1913             case( 0xa3 ): /* vc1 */
1914                 p_track->fmt.i_codec = VLC_FOURCC( 'W','V','C','1' );
1915                 break;
1916
1917             /* Private ID */
1918             case( 0xe0 ): /* NeroDigital: dvd subs */
1919                 if( p_track->fmt.i_cat == SPU_ES )
1920                 {
1921                     p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1922                     if( p_track->i_width > 0 )
1923                         p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1924                     if( p_track->i_height > 0 )
1925                         p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1926                     break;
1927                 }
1928             case( 0xe1 ): /* QCelp for 3gp */
1929                 if( p_track->fmt.i_cat == AUDIO_ES )
1930                 {
1931                     p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1932                 }
1933                 break;
1934
1935             /* Fallback */
1936             default:
1937                 /* Unknown entry, but don't touch i_fourcc */
1938                 msg_Warn( p_demux,
1939                           "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1940                           p_decconfig->i_objectTypeIndication,
1941                           p_track->i_track_ID );
1942                 break;
1943         }
1944         p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1945         if( p_track->fmt.i_extra > 0 )
1946         {
1947             p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1948             memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1949                     p_track->fmt.i_extra );
1950         }
1951     }
1952     else
1953     {
1954         switch( p_sample->i_type )
1955         {
1956             /* qt decoder, send the complete chunk */
1957             case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1958             case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1959             case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1960             case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1961             case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1962             case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1963             case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1964             case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1965             case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1966             case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1967             case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1968             case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1969                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1970                 break;
1971             /* qt decoder, send the complete chunk */
1972             case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1973             case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1974             case VLC_FOURCC( 'V', 'P', '3', '1' ):
1975             case VLC_FOURCC( '3', 'I', 'V', '1' ):
1976             case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1977                 p_track->fmt.i_extra =
1978                     p_sample->data.p_sample_vide->i_qt_image_description;
1979                 if( p_track->fmt.i_extra > 0 )
1980                 {
1981                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1982                     memcpy( p_track->fmt.p_extra,
1983                             p_sample->data.p_sample_vide->p_qt_image_description,
1984                             p_track->fmt.i_extra);
1985                 }
1986                 break;
1987             case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1988                 p_track->fmt.audio.i_rate = 8000;
1989             case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1990             case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1991             case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1992                 p_track->fmt.i_extra =
1993                     p_sample->data.p_sample_soun->i_qt_description;
1994                 if( p_track->fmt.i_extra > 0 )
1995                 {
1996                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1997                     memcpy( p_track->fmt.p_extra,
1998                             p_sample->data.p_sample_soun->p_qt_description,
1999                             p_track->fmt.i_extra);
2000                 }
2001                 break;
2002
2003             /* avc1: send avcC (h264 without annexe B, ie without start code)*/
2004             case VLC_FOURCC( 'a', 'v', 'c', '1' ):
2005             {
2006                 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2007
2008                 if( p_avcC )
2009                 {
2010                     p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
2011                     if( p_track->fmt.i_extra > 0 )
2012                     {
2013                         p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
2014                         memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
2015                                 p_track->fmt.i_extra );
2016                     }
2017                 }
2018                 else
2019                 {
2020                     msg_Err( p_demux, "missing avcC" );
2021                 }
2022                 break;
2023             }
2024
2025             case VLC_FOURCC('m','s',0x00,0x02):
2026             case VLC_FOURCC('m','s',0x00,0x11):
2027             case VLC_FOURCC('Q','c','l','p'):
2028                 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2029                 break;
2030
2031             default:
2032                 break;
2033         }
2034     }
2035
2036 #undef p_decconfig
2037
2038     if( pp_es )
2039         *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2040
2041     return VLC_SUCCESS;
2042 }
2043
2044 /* given a time it return sample/chunk
2045  * it also update elst field of the track
2046  */
2047 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2048                                    int64_t i_start, uint32_t *pi_chunk,
2049                                    uint32_t *pi_sample )
2050 {
2051     demux_sys_t *p_sys = p_demux->p_sys;
2052     MP4_Box_t   *p_box_stss;
2053     uint64_t     i_dts;
2054     unsigned int i_sample;
2055     unsigned int i_chunk;
2056     int          i_index;
2057
2058     /* FIXME see if it's needed to check p_track->i_chunk_count */
2059     if( p_track->i_chunk_count == 0 )
2060         return( VLC_EGENERIC );
2061
2062     /* handle elst (find the correct one) */
2063     MP4_TrackSetELST( p_demux, p_track, i_start );
2064     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2065     {
2066         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2067         int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
2068
2069         /* now calculate i_start for this elst */
2070         /* offset */
2071         i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
2072         if( i_start < 0 )
2073         {
2074             *pi_chunk = 0;
2075             *pi_sample= 0;
2076
2077             return VLC_SUCCESS;
2078         }
2079         /* to track time scale */
2080         i_start  = i_start * p_track->i_timescale / (int64_t)1000000;
2081         /* add elst offset */
2082         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2083              elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2084             elst->i_media_time[p_track->i_elst] > 0 )
2085         {
2086             i_start += elst->i_media_time[p_track->i_elst];
2087         }
2088
2089         msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2090                  "ms (track)", p_track->i_elst,
2091                  i_mvt * 1000 / p_sys->i_timescale,
2092                  i_start * 1000 / p_track->i_timescale );
2093     }
2094     else
2095     {
2096         /* convert absolute time to in timescale unit */
2097         i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2098     }
2099
2100     /* we start from sample 0/chunk 0, hope it won't take too much time */
2101     /* *** find good chunk *** */
2102     for( i_chunk = 0; ; i_chunk++ )
2103     {
2104         if( i_chunk + 1 >= p_track->i_chunk_count )
2105         {
2106             /* at the end and can't check if i_start in this chunk,
2107                it will be check while searching i_sample */
2108             i_chunk = p_track->i_chunk_count - 1;
2109             break;
2110         }
2111
2112         if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2113             (uint64_t)i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
2114         {
2115             break;
2116         }
2117     }
2118
2119     /* *** find sample in the chunk *** */
2120     i_sample = p_track->chunk[i_chunk].i_sample_first;
2121     i_dts    = p_track->chunk[i_chunk].i_first_dts;
2122     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2123     {
2124         if( i_dts +
2125             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2126             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2127         {
2128             i_dts    +=
2129                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2130                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2131
2132             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2133             i_index++;
2134         }
2135         else
2136         {
2137             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2138             {
2139                 break;
2140             }
2141             i_sample += ( i_start - i_dts ) /
2142                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2143             break;
2144         }
2145     }
2146
2147     if( i_sample >= p_track->i_sample_count )
2148     {
2149         msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2150                   "(seeking too far) chunk=%d sample=%d",
2151                   p_track->i_track_ID, i_chunk, i_sample );
2152         return( VLC_EGENERIC );
2153     }
2154
2155
2156     /* *** Try to find nearest sync points *** */
2157     if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2158     {
2159         MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2160         msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2161                  p_track->i_track_ID );
2162         for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2163         {
2164             if( i_index >= p_stss->i_entry_count - 1 ||
2165                 i_sample < p_stss->i_sample_number[i_index+1] )
2166             {
2167                 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2168                 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
2169                          i_sample, i_sync_sample );
2170
2171                 if( i_sync_sample <= i_sample )
2172                 {
2173                     while( i_chunk > 0 &&
2174                            i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2175                         i_chunk--;
2176                 }
2177                 else
2178                 {
2179                     while( i_chunk < p_track->i_chunk_count - 1 &&
2180                            i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2181                                             p_track->chunk[i_chunk].i_sample_count )
2182                         i_chunk++;
2183                 }
2184                 i_sample = i_sync_sample;
2185                 break;
2186             }
2187         }
2188     }
2189     else
2190     {
2191         msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2192                  "Sample Box (stss)", p_track->i_track_ID );
2193     }
2194
2195     *pi_chunk  = i_chunk;
2196     *pi_sample = i_sample;
2197
2198     return VLC_SUCCESS;
2199 }
2200
2201 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2202                                  unsigned int i_chunk, unsigned int i_sample )
2203 {
2204     bool b_reselect = false;
2205
2206     /* now see if actual es is ok */
2207     if( p_track->i_chunk >= p_track->i_chunk_count ||
2208         p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2209             p_track->chunk[i_chunk].i_sample_description_index )
2210     {
2211         msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2212                   p_track->i_track_ID );
2213
2214         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2215                         p_track->p_es, &b_reselect );
2216
2217         es_out_Del( p_demux->out, p_track->p_es );
2218
2219         p_track->p_es = NULL;
2220
2221         if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2222         {
2223             msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2224                      p_track->i_track_ID );
2225
2226             p_track->b_ok       = false;
2227             p_track->b_selected = false;
2228             return VLC_EGENERIC;
2229         }
2230     }
2231
2232     /* select again the new decoder */
2233     if( b_reselect )
2234     {
2235         es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2236     }
2237
2238     p_track->i_chunk    = i_chunk;
2239     p_track->i_sample   = i_sample;
2240
2241     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2242 }
2243
2244 /****************************************************************************
2245  * MP4_TrackCreate:
2246  ****************************************************************************
2247  * Parse track information and create all needed data to run a track
2248  * If it succeed b_ok is set to 1 else to 0
2249  ****************************************************************************/
2250 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2251                              MP4_Box_t *p_box_trak,
2252                              bool b_force_enable )
2253 {
2254     demux_sys_t *p_sys = p_demux->p_sys;
2255
2256     MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2257     MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2258     MP4_Box_t *p_elst;
2259
2260     MP4_Box_t *p_mdhd;
2261     MP4_Box_t *p_udta;
2262     MP4_Box_t *p_hdlr;
2263
2264     MP4_Box_t *p_vmhd;
2265     MP4_Box_t *p_smhd;
2266
2267     MP4_Box_t *p_drms;
2268
2269     unsigned int i;
2270     char language[4];
2271
2272     /* hint track unsupported */
2273
2274     /* set default value (-> track unusable) */
2275     p_track->b_ok       = false;
2276     p_track->b_enable   = false;
2277     p_track->b_selected = false;
2278     p_track->b_chapter  = false;
2279     p_track->b_mac_encoding = false;
2280
2281     es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2282
2283     if( !p_tkhd )
2284     {
2285         return;
2286     }
2287
2288     /* do we launch this track by default ? */
2289     p_track->b_enable =
2290         ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2291     if( !p_track->b_enable )
2292         p_track->fmt.i_priority = -1;
2293
2294     p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2295     p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2296     p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2297
2298     if( p_tref )
2299     {
2300 /*        msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2301     }
2302
2303     p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2304     p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2305
2306     if( ( !p_mdhd )||( !p_hdlr ) )
2307     {
2308         return;
2309     }
2310
2311     p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2312     if( !p_track->i_timescale )
2313         return;
2314
2315     if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2316     {
2317         /* We can convert i_language_code into iso 639 code,
2318          * I won't */
2319         strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2320         p_track->b_mac_encoding = true;
2321     }
2322     else
2323     {
2324         for( i = 0; i < 3; i++ )
2325             language[i] = p_mdhd->data.p_mdhd->i_language[i];
2326         language[3] = '\0';
2327     }
2328
2329     switch( p_hdlr->data.p_hdlr->i_handler_type )
2330     {
2331         case( ATOM_soun ):
2332             if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2333             {
2334                 return;
2335             }
2336             p_track->fmt.i_cat = AUDIO_ES;
2337             break;
2338
2339         case( ATOM_vide ):
2340             if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2341             {
2342                 return;
2343             }
2344             p_track->fmt.i_cat = VIDEO_ES;
2345             break;
2346
2347         case( ATOM_text ):
2348         case( ATOM_subp ):
2349         case( ATOM_tx3g ):
2350         case( ATOM_sbtl ):
2351             p_track->fmt.i_cat = SPU_ES;
2352             break;
2353
2354         default:
2355             return;
2356     }
2357
2358     p_track->i_elst = 0;
2359     p_track->i_elst_time = 0;
2360     if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2361     {
2362         MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2363         unsigned int i;
2364
2365         msg_Warn( p_demux, "elst box found" );
2366         for( i = 0; i < elst->i_entry_count; i++ )
2367         {
2368             msg_Dbg( p_demux, "   - [%d] duration=%"PRId64"ms media time=%"PRId64
2369                      "ms) rate=%d.%d", i,
2370                      elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2371                      elst->i_media_time[i] >= 0 ?
2372                      (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2373                      INT64_C(-1),
2374                      elst->i_media_rate_integer[i],
2375                      elst->i_media_rate_fraction[i] );
2376         }
2377     }
2378
2379
2380 /*  TODO
2381     add support for:
2382     p_dinf = MP4_BoxGet( p_minf, "dinf" );
2383 */
2384     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2385         !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2386     {
2387         return;
2388     }
2389
2390     p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2391     p_track->b_drms = p_drms != NULL;
2392     p_track->p_drms = p_track->b_drms ?
2393         p_drms->data.p_sample_soun->p_drms : NULL;
2394
2395     if ( !p_drms )
2396     {
2397         p_drms = MP4_BoxGet( p_track->p_stsd, "drmi" );
2398         p_track->b_drms = p_drms != NULL;
2399         p_track->p_drms = p_track->b_drms ?
2400             p_drms->data.p_sample_vide->p_drms : NULL;
2401     }
2402
2403     if( p_drms )
2404         p_track->p_skcr = MP4_BoxGet( p_drms, "sinf/skcr" );
2405
2406     /* Set language */
2407     if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2408     {
2409         p_track->fmt.psz_language = strdup( language );
2410     }
2411
2412     p_udta = MP4_BoxGet( p_box_trak, "udta" );
2413     if( p_udta )
2414     {
2415         MP4_Box_t *p_box_iter;
2416         for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2417                  p_box_iter = p_box_iter->p_next )
2418         {
2419             switch( p_box_iter->i_type )
2420             {
2421                 case ATOM_0xa9nam:
2422                     p_track->fmt.psz_description =
2423                         strdup( p_box_iter->data.p_0xa9xxx->psz_text );
2424                     break;
2425                 case ATOM_name:
2426                     p_track->fmt.psz_description =
2427                         strdup( p_box_iter->data.p_name->psz_text );
2428                     break;
2429             }
2430         }
2431     }
2432
2433     /* Create chunk index table and sample index table */
2434     if( TrackCreateChunksIndex( p_demux,p_track  ) ||
2435         TrackCreateSamplesIndex( p_demux, p_track ) )
2436     {
2437         return; /* cannot create chunks index */
2438     }
2439
2440     p_track->i_chunk  = 0;
2441     p_track->i_sample = 0;
2442
2443     /* Mark chapter only track */
2444     if( p_sys->p_tref_chap )
2445     {
2446         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2447         unsigned int i;
2448
2449         for( i = 0; i < p_chap->i_entry_count; i++ )
2450         {
2451             if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2452             {
2453                 p_track->b_chapter = true;
2454                 p_track->b_enable = false;
2455                 break;
2456             }
2457         }
2458     }
2459
2460     /* now create es */
2461     if( b_force_enable &&
2462         ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2463     {
2464         msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2465                   p_track->i_track_ID );
2466         p_track->b_enable = true;
2467         p_track->fmt.i_priority = 0;
2468     }
2469
2470     p_track->p_es = NULL;
2471     if( TrackCreateES( p_demux,
2472                        p_track, p_track->i_chunk,
2473                        p_track->b_chapter ? NULL : &p_track->p_es ) )
2474     {
2475         msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2476                  p_track->i_track_ID );
2477         return;
2478     }
2479     p_track->b_ok = true;
2480 #if 0
2481     {
2482         int i;
2483         for( i = 0; i < p_track->i_chunk_count; i++ )
2484         {
2485             fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2486                      i, p_track->chunk[i].i_sample_count,
2487                      p_track->chunk[i].i_first_dts );
2488
2489         }
2490     }
2491 #endif
2492 }
2493
2494 /****************************************************************************
2495  * MP4_TrackDestroy:
2496  ****************************************************************************
2497  * Destroy a track created by MP4_TrackCreate.
2498  ****************************************************************************/
2499 static void MP4_TrackDestroy( mp4_track_t *p_track )
2500 {
2501     unsigned int i_chunk;
2502
2503     p_track->b_ok = false;
2504     p_track->b_enable   = false;
2505     p_track->b_selected = false;
2506
2507     es_format_Clean( &p_track->fmt );
2508
2509     for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2510     {
2511         if( p_track->chunk )
2512         {
2513            FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2514            FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2515
2516            FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2517            FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2518         }
2519     }
2520     FREENULL( p_track->chunk );
2521
2522     if( !p_track->i_sample_size )
2523     {
2524         FREENULL( p_track->p_sample_size );
2525     }
2526 }
2527
2528 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2529                             mtime_t i_start )
2530 {
2531     if( !p_track->b_ok || p_track->b_chapter )
2532     {
2533         return VLC_EGENERIC;
2534     }
2535
2536     if( p_track->b_selected )
2537     {
2538         msg_Warn( p_demux, "track[Id 0x%x] already selected",
2539                   p_track->i_track_ID );
2540         return VLC_SUCCESS;
2541     }
2542
2543     return MP4_TrackSeek( p_demux, p_track, i_start );
2544 }
2545
2546 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2547 {
2548     if( !p_track->b_ok || p_track->b_chapter )
2549     {
2550         return;
2551     }
2552
2553     if( !p_track->b_selected )
2554     {
2555         msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2556                   p_track->i_track_ID );
2557         return;
2558     }
2559     if( p_track->p_es )
2560     {
2561         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2562                         p_track->p_es, false );
2563     }
2564
2565     p_track->b_selected = false;
2566 }
2567
2568 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2569                           mtime_t i_start )
2570 {
2571     uint32_t i_chunk;
2572     uint32_t i_sample;
2573
2574     if( !p_track->b_ok || p_track->b_chapter )
2575         return VLC_EGENERIC;
2576
2577     p_track->b_selected = false;
2578
2579     if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2580                                 &i_chunk, &i_sample ) )
2581     {
2582         msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2583                   p_track->i_track_ID );
2584         return VLC_EGENERIC;
2585     }
2586
2587     p_track->b_selected = true;
2588
2589     if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2590         p_track->b_selected = true;
2591
2592     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2593 }
2594
2595
2596 /*
2597  * 3 types: for audio
2598  *
2599  */
2600 #define QT_V0_MAX_SAMPLES 1024
2601 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2602 {
2603     int i_size;
2604     MP4_Box_data_sample_soun_t *p_soun;
2605
2606     if( p_track->i_sample_size == 0 )
2607     {
2608         /* most simple case */
2609         return p_track->p_sample_size[p_track->i_sample];
2610     }
2611     if( p_track->fmt.i_cat != AUDIO_ES )
2612     {
2613         return p_track->i_sample_size;
2614     }
2615
2616     p_soun = p_track->p_sample->data.p_sample_soun;
2617
2618     if( p_soun->i_qt_version == 1 )
2619     {
2620         int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2621         if( p_track->fmt.audio.i_blockalign > 1 )
2622             i_samples = p_soun->i_sample_per_packet;
2623
2624         i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2625     }
2626     else if( p_track->i_sample_size > 256 )
2627     {
2628         /* We do that so we don't read too much data
2629          * (in this case we are likely dealing with compressed data) */
2630         i_size = p_track->i_sample_size;
2631     }
2632     else
2633     {
2634         /* Read a bunch of samples at once */
2635         int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2636             ( p_track->i_sample -
2637               p_track->chunk[p_track->i_chunk].i_sample_first );
2638
2639         i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2640         i_size = i_samples * p_track->i_sample_size;
2641     }
2642
2643     //fprintf( stderr, "size=%d\n", i_size );
2644     return i_size;
2645 }
2646
2647 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2648 {
2649     unsigned int i_sample;
2650     uint64_t i_pos;
2651
2652     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2653
2654     if( p_track->i_sample_size )
2655     {
2656         MP4_Box_data_sample_soun_t *p_soun =
2657             p_track->p_sample->data.p_sample_soun;
2658
2659         if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
2660         {
2661             i_pos += ( p_track->i_sample -
2662                        p_track->chunk[p_track->i_chunk].i_sample_first ) *
2663                      p_track->i_sample_size;
2664         }
2665         else
2666         {
2667             /* we read chunk by chunk unless a blockalign is requested */
2668             if( p_track->fmt.audio.i_blockalign > 1 )
2669                 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2670                                 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2671         }
2672     }
2673     else
2674     {
2675         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2676              i_sample < p_track->i_sample; i_sample++ )
2677         {
2678             i_pos += p_track->p_sample_size[i_sample];
2679         }
2680     }
2681
2682     return i_pos;
2683 }
2684
2685 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2686 {
2687     if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2688     {
2689         MP4_Box_data_sample_soun_t *p_soun;
2690
2691         p_soun = p_track->p_sample->data.p_sample_soun;
2692
2693         if( p_soun->i_qt_version == 1 )
2694         {
2695             /* we read chunk by chunk unless a blockalign is requested */
2696             if( p_track->fmt.audio.i_blockalign > 1 )
2697                 p_track->i_sample += p_soun->i_sample_per_packet;
2698             else
2699                 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2700         }
2701         else if( p_track->i_sample_size > 256 )
2702         {
2703             /* We do that so we don't read too much data
2704              * (in this case we are likely dealing with compressed data) */
2705             p_track->i_sample += 1;
2706         }
2707         else
2708         {
2709             /* FIXME */
2710             p_track->i_sample += QT_V0_MAX_SAMPLES;
2711             if( p_track->i_sample >
2712                 p_track->chunk[p_track->i_chunk].i_sample_first +
2713                 p_track->chunk[p_track->i_chunk].i_sample_count )
2714             {
2715                 p_track->i_sample =
2716                     p_track->chunk[p_track->i_chunk].i_sample_first +
2717                     p_track->chunk[p_track->i_chunk].i_sample_count;
2718             }
2719         }
2720     }
2721     else
2722     {
2723         p_track->i_sample++;
2724     }
2725
2726     if( p_track->i_sample >= p_track->i_sample_count )
2727         return VLC_EGENERIC;
2728
2729     /* Have we changed chunk ? */
2730     if( p_track->i_sample >=
2731             p_track->chunk[p_track->i_chunk].i_sample_first +
2732             p_track->chunk[p_track->i_chunk].i_sample_count )
2733     {
2734         if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2735                                   p_track->i_sample ) )
2736         {
2737             msg_Warn( p_demux, "track[0x%x] will be disabled "
2738                       "(cannot restart decoder)", p_track->i_track_ID );
2739             MP4_TrackUnselect( p_demux, p_track );
2740             return VLC_EGENERIC;
2741         }
2742     }
2743
2744     /* Have we changed elst */
2745     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2746     {
2747         demux_sys_t *p_sys = p_demux->p_sys;
2748         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2749         uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2750                         p_sys->i_timescale / (int64_t)1000000;
2751
2752         if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2753             i_mvt >= p_track->i_elst_time +
2754                      elst->i_segment_duration[p_track->i_elst] )
2755         {
2756             MP4_TrackSetELST( p_demux, p_track,
2757                               MP4_TrackGetDTS( p_demux, p_track ) );
2758         }
2759     }
2760
2761     return VLC_SUCCESS;
2762 }
2763
2764 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2765                               int64_t i_time )
2766 {
2767     demux_sys_t *p_sys = p_demux->p_sys;
2768     int         i_elst_last = tk->i_elst;
2769
2770     /* handle elst (find the correct one) */
2771     tk->i_elst      = 0;
2772     tk->i_elst_time = 0;
2773     if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2774     {
2775         MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2776         int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2777
2778         for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2779         {
2780             mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2781
2782             if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2783             {
2784                 break;
2785             }
2786             tk->i_elst_time += i_dur;
2787         }
2788
2789         if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2790         {
2791             /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2792             tk->i_elst = elst->i_entry_count - 1;
2793             tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2794         }
2795
2796         if( elst->i_media_time[tk->i_elst] < 0 )
2797         {
2798             /* track offset */
2799             tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2800         }
2801     }
2802     if( i_elst_last != tk->i_elst )
2803     {
2804         msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2805     }
2806 }
2807
2808 /* */
2809 static const char *MP4_ConvertMacCode( uint16_t i_code )
2810 {
2811     static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
2812         { "en",   0 }, { "fr",   1 }, { "de",   2 }, { "it",   3 }, { "nl",   4 },
2813         { "sv",   5 }, { "es",   6 }, { "da",   7 }, { "pt",   8 }, { "no",   9 },
2814         { "he",  10 }, { "ja",  11 }, { "ar",  12 }, { "fi",  13 }, { "el",  14 },
2815         { "is",  15 }, { "mt",  16 }, { "tr",  17 }, { "hr",  18 }, { "zh",  19 },
2816         { "ur",  20 }, { "hi",  21 }, { "th",  22 }, { "ko",  23 }, { "lt",  24 },
2817         { "pl",  25 }, { "hu",  26 }, { "et",  27 }, { "lv",  28 }, //{ "??",  29 },
2818         { "fo",  30 }, { "fa",  31 }, { "ru",  32 }, { "zh",  33 }, { "nl",  34 },
2819         { "ga",  35 }, { "sq",  36 }, { "ro",  37 }, { "cs",  38 }, { "sk",  39 },
2820         { "sl",  40 }, { "yi",  41 }, { "sr",  42 }, { "mk",  43 }, { "bg",  44 },
2821         { "uk",  45 }, { "be",  46 }, { "uz",  47 }, { "az",  48 }, { "kk",  48 },
2822         { "az",  50 }, { "hy",  51 }, { "ka",  52 }, { "mo",  53 }, { "ky",  54 },
2823         { "tg",  55 }, { "tk",  56 }, { "mn",  57 }, { "mn",  58 }, { "ps",  59 },
2824         { "ku",  60 }, { "ks",  61 }, { "sd",  62 }, { "bo",  63 }, { "ne",  64 },
2825         { "sa",  65 }, { "mr",  66 }, { "bn",  67 }, { "as",  68 }, { "gu",  69 },
2826         { "pa",  70 }, { "or",  71 }, { "ml",  72 }, { "kn",  73 }, { "ta",  74 },
2827         { "te",  75 }, { "si",  76 }, { "my",  77 }, { "km",  78 }, { "lo",  79 },
2828         { "vi",  80 }, { "id",  81 }, { "tl",  82 }, { "ms",  83 }, { "ms",  84 },
2829         { "am",  85 }, { "ti",  86 }, { "om",  87 }, { "so",  88 }, { "sw",  89 },
2830         { "rw",  90 }, { "rn",  91 }, { "ny",  92 }, { "mg",  93 }, { "eo",  94 },
2831
2832                                                      { "cy", 128 }, { "eu", 129 },
2833         { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
2834         { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
2835         { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
2836         { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
2837         /* */
2838         { "", 0 }
2839     };
2840     int i;
2841     for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
2842     {
2843         if( p_cvt[i].i_code == i_code )
2844             return p_cvt[i].psz_iso639_1;
2845     }
2846     return "";
2847 }