]> git.sesse.net Git - vlc/blob - modules/demux/mp4/mp4.c
Include vlc_plugin.h as needed
[vlc] / modules / demux / mp4 / mp4.c
1 /*****************************************************************************
2  * mp4.c : MP4 file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
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/vlc.h>
32 #include <vlc_plugin.h>
33
34
35 #include <vlc_demux.h>
36 #include <vlc_playlist.h>
37 #include <vlc_md5.h>
38 #include <vlc_charset.h>
39 #include "iso_lang.h"
40 #include <vlc_meta.h>
41
42 #include "libmp4.h"
43 #include "drms.h"
44
45 #ifdef UNDER_CE
46 #define uint64_t int64_t
47 #endif
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 static int  Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
54
55 vlc_module_begin();
56     set_category( CAT_INPUT );
57     set_subcategory( SUBCAT_INPUT_DEMUX );
58     set_description( _("MP4 stream demuxer") );
59     set_capability( "demux", 242 );
60     set_callbacks( Open, Close );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int   Demux   ( demux_t * );
67 static int   DemuxRef( demux_t *p_demux ){ return 0;}
68 static int   Seek    ( demux_t *, mtime_t );
69 static int   Control ( demux_t *, int, va_list );
70
71 /* Contain all information about a chunk */
72 typedef struct
73 {
74     uint64_t     i_offset; /* absolute position of this chunk in the file */
75     uint32_t     i_sample_description_index; /* index for SampleEntry to use */
76     uint32_t     i_sample_count; /* how many samples in this chunk */
77     uint32_t     i_sample_first; /* index of the first sample in this chunk */
78
79     /* now provide way to calculate pts, dts, and offset without to
80         much memory and with fast acces */
81
82     /* with this we can calculate dts/pts without waste memory */
83     uint64_t     i_first_dts;
84     uint32_t     *p_sample_count_dts;
85     uint32_t     *p_sample_delta_dts;   /* dts delta */
86
87     uint32_t     *p_sample_count_pts;
88     int32_t      *p_sample_offset_pts;  /* pts-dts */
89
90     /* TODO if needed add pts
91         but quickly *add* support for edts and seeking */
92
93 } mp4_chunk_t;
94
95  /* Contain all needed information for read all track with vlc */
96 typedef struct
97 {
98     unsigned int i_track_ID;/* this should be unique */
99
100     int b_ok;               /* The track is usable */
101     int b_enable;           /* is the trak enable by default */
102     bool b_selected;  /* is the trak being played */
103     bool b_chapter;   /* True when used for chapter only */
104
105     es_format_t fmt;
106     es_out_id_t *p_es;
107
108     /* display size only ! */
109     int i_width;
110     int i_height;
111
112     /* more internal data */
113     uint64_t        i_timescale;    /* time scale for this track only */
114
115     /* elst */
116     int             i_elst;         /* current elst */
117     int64_t         i_elst_time;    /* current elst start time (in movie time scale)*/
118     MP4_Box_t       *p_elst;        /* elst (could be NULL) */
119
120     /* give the next sample to read, i_chunk is to find quickly where
121       the sample is located */
122     uint32_t         i_sample;       /* next sample to read */
123     uint32_t         i_chunk;        /* chunk where next sample is stored */
124     /* total count of chunk and sample */
125     uint32_t         i_chunk_count;
126     uint32_t         i_sample_count;
127
128     mp4_chunk_t    *chunk; /* always defined  for each chunk */
129
130     /* sample size, p_sample_size defined only if i_sample_size == 0
131         else i_sample_size is size for all sample */
132     uint32_t         i_sample_size;
133     uint32_t         *p_sample_size; /* XXX perhaps add file offset if take
134                                     too much time to do sumations each time*/
135
136     MP4_Box_t *p_stbl;  /* will contain all timing information */
137     MP4_Box_t *p_stsd;  /* will contain all data to initialize decoder */
138     MP4_Box_t *p_sample;/* point on actual sdsd */
139
140     bool b_drms;
141     void      *p_drms;
142
143 } mp4_track_t;
144
145
146 struct demux_sys_t
147 {
148     MP4_Box_t    *p_root;      /* container for the whole file */
149
150     mtime_t      i_pcr;
151
152     uint64_t     i_time;         /* time position of the presentation
153                                   * in movie timescale */
154     uint64_t     i_timescale;    /* movie time scale */
155     uint64_t     i_duration;     /* movie duration */
156     unsigned int i_tracks;       /* number of tracks */
157     mp4_track_t  *track;         /* array of track */
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  *);
170 static void MP4_TrackDestroy( demux_t *, 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
184 /* Return time in s of a track */
185 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
186 {
187 #define chunk p_track->chunk[p_track->i_chunk]
188
189     unsigned int i_index = 0;
190     unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
191     int64_t i_dts = chunk.i_first_dts;
192
193     while( i_sample > 0 )
194     {
195         if( i_sample > chunk.p_sample_count_dts[i_index] )
196         {
197             i_dts += chunk.p_sample_count_dts[i_index] *
198                 chunk.p_sample_delta_dts[i_index];
199             i_sample -= chunk.p_sample_count_dts[i_index];
200             i_index++;
201         }
202         else
203         {
204             i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
205             i_sample = 0;
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( demux_t *p_demux, 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 /* Function to lookup the currently playing item */
261 static bool FindItem( demux_t *p_demux, playlist_t *p_playlist,
262                      playlist_item_t **pp_item );
263
264 static void LoadChapter( demux_t  *p_demux );
265
266 /*****************************************************************************
267  * Open: check file and initializes MP4 structures
268  *****************************************************************************/
269 static int Open( vlc_object_t * p_this )
270 {
271     demux_t  *p_demux = (demux_t *)p_this;
272     demux_sys_t     *p_sys;
273
274     const uint8_t   *p_peek;
275
276     MP4_Box_t       *p_ftyp;
277     MP4_Box_t       *p_rmra;
278     MP4_Box_t       *p_mvhd;
279     MP4_Box_t       *p_trak;
280
281     unsigned int    i;
282     bool      b_seekable;
283
284     /* A little test to see if it could be a mp4 */
285     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
286
287     switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
288     {
289         case FOURCC_ftyp:
290         case FOURCC_moov:
291         case FOURCC_foov:
292         case FOURCC_moof:
293         case FOURCC_mdat:
294         case FOURCC_udta:
295         case FOURCC_free:
296         case FOURCC_skip:
297         case FOURCC_wide:
298         case VLC_FOURCC( 'p', 'n', 'o', 't' ):
299             break;
300          default:
301             return VLC_EGENERIC;
302     }
303
304     /* I need to seek */
305     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
306     if( !b_seekable )
307     {
308         msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
309         return VLC_EGENERIC;
310     }
311
312     /*Set exported functions */
313     p_demux->pf_demux = Demux;
314     p_demux->pf_control = Control;
315
316     /* create our structure that will contains all data */
317     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
318     memset( p_sys, 0, sizeof( demux_sys_t ) );
319
320     /* Now load all boxes ( except raw data ) */
321     if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
322     {
323         msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
324         goto error;
325     }
326
327     MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
328
329     if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
330     {
331         switch( p_ftyp->data.p_ftyp->i_major_brand )
332         {
333             case( FOURCC_isom ):
334                 msg_Dbg( p_demux,
335                          "ISO Media file (isom) version %d.",
336                          p_ftyp->data.p_ftyp->i_minor_version );
337                 break;
338             default:
339                 msg_Dbg( p_demux,
340                          "unrecognized major file specification (%4.4s).",
341                           (char*)&p_ftyp->data.p_ftyp->i_major_brand );
342                 break;
343         }
344     }
345     else
346     {
347         msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
348     }
349
350     /* the file need to have one moov box */
351     if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
352     {
353         MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
354
355         if( !p_foov )
356         {
357             msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
358             goto error;
359         }
360         /* we have a free box as a moov, rename it */
361         p_foov->i_type = FOURCC_moov;
362     }
363
364     if( ( p_rmra = MP4_BoxGet( p_sys->p_root,  "/moov/rmra" ) ) )
365     {
366         playlist_t *p_playlist;
367         playlist_item_t *p_current, *p_item_in_category;
368         int        i_count = MP4_BoxCount( p_rmra, "rmda" );
369         int        i;
370         bool b_play = false;
371
372         msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
373
374         p_playlist = pl_Yield( p_demux );
375         if( p_playlist )
376         {
377             b_play = FindItem( p_demux, p_playlist, &p_current );
378             p_item_in_category = playlist_ItemToNode( p_playlist, p_current, true );
379             p_current->p_input->i_type = ITEM_TYPE_PLAYLIST;
380
381             for( i = 0; i < i_count; i++ )
382             {
383                 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
384                 char      *psz_ref;
385                 uint32_t  i_ref_type;
386
387                 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
388                 {
389                     continue;
390                 }
391                 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
392
393                 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
394                          psz_ref, (char*)&i_ref_type );
395
396                 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
397                 {
398                     if( strstr( psz_ref, "qt5gateQT" ) )
399                     {
400                         msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
401                         continue;
402                     }
403                     if( !strncmp( psz_ref, "http://", 7 ) ||
404                         !strncmp( psz_ref, "rtsp://", 7 ) )
405                     {
406                         ;
407                     }
408                     else
409                     {
410                         char *psz_absolute;
411                         char *psz_path = strdup( p_demux->psz_path );
412                         char *end = strrchr( psz_path, '/' );
413                         if( end ) end[1] = '\0';
414                         else *psz_path = '\0';
415
416                         asprintf( &psz_absolute, "%s://%s%s",
417                                       p_demux->psz_access, psz_path, psz_ref );
418
419                         free( psz_ref );
420                         psz_ref = psz_absolute;
421                         free( psz_path );
422                     }
423                     if( p_current )
424                     {
425                         input_item_t *p_input;
426                         msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
427                         p_input = input_ItemNewExt( p_playlist, psz_ref, NULL,
428                                             0, NULL, -1 );
429                         input_ItemCopyOptions( p_current->p_input, p_input );
430                         /* FIXME: playlist_BothAddInput() can fail */
431                         playlist_BothAddInput( p_playlist, p_input,
432                                                p_item_in_category,
433                                                PLAYLIST_APPEND, PLAYLIST_END,
434                                                NULL, NULL, false );
435                         vlc_gc_decref( p_input );
436                     }
437                 }
438                 else
439                 {
440                     msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
441                              (char*)&p_rdrf->data.p_rdrf->i_ref_type );
442                 }
443                 free( psz_ref );
444             }
445             if( b_play && p_item_in_category &&
446                 p_item_in_category->i_children > 0 )
447             {
448                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true,
449                                   p_item_in_category, NULL );
450             }
451             vlc_object_release( p_playlist );
452         }
453         else
454         {
455             msg_Err( p_demux, "can't find playlist" );
456         }
457     }
458
459     if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
460     {
461         if( !p_rmra )
462         {
463             msg_Err( p_demux, "cannot find /moov/mvhd" );
464             goto error;
465         }
466         else
467         {
468             msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
469             p_demux->pf_demux = DemuxRef;
470             return VLC_SUCCESS;
471         }
472     }
473     else
474     {
475         p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
476         if( p_sys->i_timescale == 0 )
477         {
478             msg_Err( p_this, "bad timescale" );
479             goto error;
480         }
481         p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
482     }
483
484     if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
485     {
486         msg_Err( p_demux, "cannot find any /moov/trak" );
487         goto error;
488     }
489     msg_Dbg( p_demux, "found %d track%c",
490                         p_sys->i_tracks,
491                         p_sys->i_tracks ? 's':' ' );
492
493     /* allocate memory */
494     p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
495     if( p_sys->track == NULL )
496         goto error;
497     memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
498
499     /* Search the first chap reference (like quicktime) */
500     p_sys->p_tref_chap = NULL;
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         MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
505         if( !p_chap || p_chap->data.p_tref_generic->i_entry_count <= 0 )
506             continue;
507
508         p_sys->p_tref_chap = p_chap;
509         break;
510     }
511
512     /* now process each track and extract all usefull information */
513     for( i = 0; i < p_sys->i_tracks; i++ )
514     {
515         p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
516         MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
517
518         if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
519         {
520             const char *psz_cat;
521             switch( p_sys->track[i].fmt.i_cat )
522             {
523                 case( VIDEO_ES ):
524                     psz_cat = "video";
525                     break;
526                 case( AUDIO_ES ):
527                     psz_cat = "audio";
528                     break;
529                 case( SPU_ES ):
530                     psz_cat = "subtitle";
531                     break;
532
533                 default:
534                     psz_cat = "unknown";
535                     break;
536             }
537
538             msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
539                      p_sys->track[i].i_track_ID, psz_cat,
540                      p_sys->track[i].b_enable ? "enable":"disable",
541                      p_sys->track[i].fmt.psz_language ?
542                      p_sys->track[i].fmt.psz_language : "undef" );
543         }
544         else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
545         {
546             msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
547                      p_sys->track[i].i_track_ID,
548                      p_sys->track[i].fmt.psz_language ?
549                      p_sys->track[i].fmt.psz_language : "undef" );
550         }
551         else
552         {
553             msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
554                      p_sys->track[i].i_track_ID );
555         }
556     }
557
558     /* */
559     LoadChapter( p_demux );
560
561     return VLC_SUCCESS;
562
563 error:
564     if( p_sys->p_root )
565     {
566         MP4_BoxFree( p_demux->s, p_sys->p_root );
567     }
568     free( p_sys );
569     return VLC_EGENERIC;
570 }
571
572 /*****************************************************************************
573  * Demux: read packet and send them to decoders
574  *****************************************************************************
575  * TODO check for newly selected track (ie audio upt to now )
576  *****************************************************************************/
577 static int Demux( demux_t *p_demux )
578 {
579     demux_sys_t *p_sys = p_demux->p_sys;
580     unsigned int i_track;
581
582
583     unsigned int i_track_selected;
584
585     /* check for newly selected/unselected track */
586     for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
587          i_track++ )
588     {
589         mp4_track_t *tk = &p_sys->track[i_track];
590         bool b;
591
592         if( !tk->b_ok || tk->b_chapter ||
593             ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
594         {
595             continue;
596         }
597
598         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
599
600         if( tk->b_selected && !b )
601         {
602             MP4_TrackUnselect( p_demux, tk );
603         }
604         else if( !tk->b_selected && b)
605         {
606             MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
607         }
608
609         if( tk->b_selected )
610         {
611             i_track_selected++;
612         }
613     }
614
615     if( i_track_selected <= 0 )
616     {
617         p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
618         if( p_sys->i_timescale > 0 )
619         {
620             int64_t i_length = (mtime_t)1000000 *
621                                (mtime_t)p_sys->i_duration /
622                                (mtime_t)p_sys->i_timescale;
623             if( MP4_GetMoviePTS( p_sys ) >= i_length )
624                 return 0;
625             return 1;
626         }
627
628         msg_Warn( p_demux, "no track selected, exiting..." );
629         return 0;
630     }
631
632     /* */
633     MP4_UpdateSeekpoint( p_demux );
634
635     /* first wait for the good time to read a packet */
636     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
637
638     p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
639
640     /* we will read 100ms for each stream so ...*/
641     p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
642
643     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
644     {
645         mp4_track_t *tk = &p_sys->track[i_track];
646
647         if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
648             continue;
649
650         while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
651         {
652 #if 0
653             msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
654                      MP4_TrackGetDTS( p_demux, tk ),
655                      MP4_GetMoviePTS( p_sys ) );
656 #endif
657
658             if( MP4_TrackSampleSize( tk ) > 0 )
659             {
660                 block_t *p_block;
661                 int64_t i_delta;
662
663                 /* go,go go ! */
664                 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
665                 {
666                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
667                               tk->i_track_ID );
668                     MP4_TrackUnselect( p_demux, tk );
669                     break;
670                 }
671
672                 /* now read pes */
673                 if( !(p_block =
674                          stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
675                 {
676                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
677                               tk->i_track_ID );
678                     MP4_TrackUnselect( p_demux, tk );
679                     break;
680                 }
681
682                 if( tk->b_drms && tk->p_drms )
683                 {
684                     drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
685                                   p_block->i_buffer );
686                 }
687                 else if( tk->fmt.i_cat == SPU_ES )
688                 {
689                     if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
690                         p_block->i_buffer >= 2 )
691                     {
692                         uint16_t i_size = GetWBE( p_block->p_buffer );
693
694                         if( i_size + 2 <= p_block->i_buffer )
695                         {
696                             char *p;
697                             /* remove the length field, and append a '\0' */
698                             memmove( &p_block->p_buffer[0],
699                                      &p_block->p_buffer[2], i_size );
700                             p_block->p_buffer[i_size] = '\0';
701                             p_block->i_buffer = i_size + 1;
702
703                             /* convert \r -> \n */
704                             while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
705                             {
706                                 *p = '\n';
707                             }
708                         }
709                         else
710                         {
711                             /* Invalid */
712                             p_block->i_buffer = 0;
713                         }
714                     }
715                 }
716                 /* dts */
717                 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
718                 /* pts */
719                 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
720                 if( i_delta != -1 )
721                     p_block->i_pts = p_block->i_dts + i_delta;
722                 else if( tk->fmt.i_cat != VIDEO_ES )
723                     p_block->i_pts = p_block->i_dts;
724                 else
725                     p_block->i_pts = 0;
726
727                 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
728                     es_out_Send( p_demux->out, tk->p_es, p_block );
729             }
730
731             /* Next sample */
732             if( MP4_TrackNextSample( p_demux, tk ) )
733                 break;
734         }
735     }
736
737     return 1;
738 }
739
740 static void MP4_UpdateSeekpoint( demux_t *p_demux )
741 {
742     demux_sys_t *p_sys = p_demux->p_sys;
743     int64_t i_time;
744     int i;
745     if( !p_sys->p_title )
746         return;
747     i_time = MP4_GetMoviePTS( p_sys );
748     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
749     {
750         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
751             break;
752     }
753     i--;
754
755     if( i != p_demux->info.i_seekpoint && i >= 0 )
756     {
757         p_demux->info.i_seekpoint = i;
758         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
759     }
760 }
761 /*****************************************************************************
762  * Seek: Got to i_date
763 ******************************************************************************/
764 static int Seek( demux_t *p_demux, mtime_t i_date )
765 {
766     demux_sys_t *p_sys = p_demux->p_sys;
767     unsigned int i_track;
768
769     /* First update update global time */
770     p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
771     p_sys->i_pcr  = i_date;
772
773     /* Now for each stream try to go to this time */
774     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
775     {
776         mp4_track_t *tk = &p_sys->track[i_track];
777         MP4_TrackSeek( p_demux, tk, i_date );
778     }
779     MP4_UpdateSeekpoint( p_demux );
780     return VLC_SUCCESS;
781 }
782
783 /*****************************************************************************
784  * Control:
785  *****************************************************************************/
786 static int Control( demux_t *p_demux, int i_query, va_list args )
787 {
788     demux_sys_t *p_sys = p_demux->p_sys;
789
790     double f, *pf;
791     int64_t i64, *pi64;
792
793     switch( i_query )
794     {
795         case DEMUX_GET_POSITION:
796             pf = (double*)va_arg( args, double * );
797             if( p_sys->i_duration > 0 )
798             {
799                 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
800             }
801             else
802             {
803                 *pf = 0.0;
804             }
805             return VLC_SUCCESS;
806
807         case DEMUX_SET_POSITION:
808             f = (double)va_arg( args, double );
809             if( p_sys->i_timescale > 0 )
810             {
811                 i64 = (int64_t)( f * (double)1000000 *
812                                  (double)p_sys->i_duration /
813                                  (double)p_sys->i_timescale );
814                 return Seek( p_demux, i64 );
815             }
816             else return VLC_SUCCESS;
817
818         case DEMUX_GET_TIME:
819             pi64 = (int64_t*)va_arg( args, int64_t * );
820             if( p_sys->i_timescale > 0 )
821             {
822                 *pi64 = (mtime_t)1000000 *
823                         (mtime_t)p_sys->i_time /
824                         (mtime_t)p_sys->i_timescale;
825             }
826             else *pi64 = 0;
827             return VLC_SUCCESS;
828
829         case DEMUX_SET_TIME:
830             i64 = (int64_t)va_arg( args, int64_t );
831             return Seek( p_demux, i64 );
832
833         case DEMUX_GET_LENGTH:
834             pi64 = (int64_t*)va_arg( args, int64_t * );
835             if( p_sys->i_timescale > 0 )
836             {
837                 *pi64 = (mtime_t)1000000 *
838                         (mtime_t)p_sys->i_duration /
839                         (mtime_t)p_sys->i_timescale;
840             }
841             else *pi64 = 0;
842             return VLC_SUCCESS;
843
844         case DEMUX_GET_FPS:
845             msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
846             return VLC_EGENERIC;
847
848         case DEMUX_GET_META:
849         {
850             vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
851             MP4_Box_t  *p_0xa9xxx;
852
853             MP4_Box_t  *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
854             if( p_udta == NULL )
855             {
856                 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
857                 if( p_udta == NULL )
858                 {
859                     return VLC_EGENERIC;
860                 }
861             }
862
863             for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
864                  p_0xa9xxx = p_0xa9xxx->p_next )
865             {
866
867                 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
868                     continue;
869
870                 /* FIXME FIXME: should convert from whatever the character
871                  * encoding of MP4 meta data is to UTF-8. */
872 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
873     if( psz_utf ) { EnsureUTF8( psz_utf );  \
874                     fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
875
876                 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
877                 switch( p_0xa9xxx->i_type )
878                 {
879                 case FOURCC_0xa9nam: /* Full name */
880                     SET( vlc_meta_SetTitle );
881                     break;
882                 case FOURCC_0xa9aut:
883                     SET( vlc_meta_SetArtist );
884                     break;
885                 case FOURCC_0xa9ART:
886                     SET( vlc_meta_SetArtist );
887                     break;
888                 case FOURCC_0xa9cpy:
889                     SET( vlc_meta_SetCopyright );
890                     break;
891                 case FOURCC_0xa9day: /* Creation Date */
892                     SET( vlc_meta_SetDate );
893                     break;
894                 case FOURCC_0xa9des: /* Description */
895                     SET( vlc_meta_SetDescription );
896                     break;
897                 case FOURCC_0xa9gen: /* Genre */
898                     SET( vlc_meta_SetGenre );
899                     break;
900
901                 case FOURCC_0xa9alb: /* Album */
902                     SET( vlc_meta_SetAlbum );
903                     break;
904
905                 case FOURCC_0xa9trk: /* Track */
906                     SET( vlc_meta_SetTracknum );
907                     break;
908
909                 case FOURCC_0xa9cmt: /* Commment */
910                     SET( vlc_meta_SetDescription );
911                     break;
912
913                 case FOURCC_0xa9url: /* URL */
914                     SET( vlc_meta_SetURL );
915                     break;
916
917                 case FOURCC_0xa9enc: /* Encoded By */
918                     SET( vlc_meta_SetEncodedBy );
919                     break;
920                     
921                 case FOURCC_0xa9swr:
922                 case FOURCC_0xa9inf: /* Information */
923                 case FOURCC_0xa9dir: /* Director */
924                 case FOURCC_0xa9dis: /* Disclaimer */
925                 case FOURCC_0xa9req: /* Requirements */
926                 case FOURCC_0xa9fmt: /* Original Format */
927                 case FOURCC_0xa9dsa: /* Display Source As */
928                 case FOURCC_0xa9hst: /* Host Computer */
929                 case FOURCC_0xa9prd: /* Producer */
930                 case FOURCC_0xa9prf: /* Performers */
931                 case FOURCC_0xa9ope: /* Original Performer */
932                 case FOURCC_0xa9src: /* Providers Source Content */
933                 case FOURCC_0xa9wrt: /* Writer */
934                 case FOURCC_0xa9com: /* Composer */
935                 case FOURCC_WLOC:    /* Window Location */
936                     /* TODO one day, but they aren't really meaningfull */
937                     break;
938 #undef SET
939
940                 default:
941                     break;
942                 }
943             }
944             return VLC_SUCCESS;
945         }
946
947         case DEMUX_GET_TITLE_INFO:
948         {
949             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
950             int *pi_int    = (int*)va_arg( args, int* );
951             int *pi_title_offset = (int*)va_arg( args, int* );
952             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
953
954             if( !p_sys->p_title )
955                 return VLC_EGENERIC;
956
957             *pi_int = 1;
958             *ppp_title = malloc( sizeof( input_title_t**) );
959             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
960             *pi_title_offset = 0;
961             *pi_seekpoint_offset = 0;
962             return VLC_SUCCESS;
963         }
964         case DEMUX_SET_TITLE:
965         {
966             const int i_title = (int)va_arg( args, int );
967             if( !p_sys->p_title || i_title != 0 )
968                 return VLC_EGENERIC;
969             return VLC_SUCCESS;
970         }
971         case DEMUX_SET_SEEKPOINT:
972         {
973             const int i_seekpoint = (int)va_arg( args, int );
974             if( !p_sys->p_title )
975                 return VLC_EGENERIC;
976             return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
977         }
978
979         case DEMUX_SET_NEXT_DEMUX_TIME:
980         case DEMUX_SET_GROUP:
981         case DEMUX_HAS_UNSUPPORTED_META:
982         case DEMUX_GET_ATTACHMENTS:
983             return VLC_EGENERIC;
984
985         default:
986             msg_Warn( p_demux, "control query %u unimplemented", i_query );
987             return VLC_EGENERIC;
988     }
989 }
990
991 /*****************************************************************************
992  * Close: frees unused data
993  *****************************************************************************/
994 static void Close ( vlc_object_t * p_this )
995 {
996     demux_t *  p_demux = (demux_t *)p_this;
997     demux_sys_t *p_sys = p_demux->p_sys;
998     unsigned int i_track;
999
1000     msg_Dbg( p_demux, "freeing all memory" );
1001
1002     MP4_BoxFree( p_demux->s, p_sys->p_root );
1003     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1004     {
1005         MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
1006     }
1007     FREENULL( p_sys->track );
1008
1009     if( p_sys->p_title )
1010         vlc_input_title_Delete( p_sys->p_title );
1011
1012     free( p_sys );
1013 }
1014
1015
1016
1017 /****************************************************************************
1018  * Local functions, specific to vlc
1019  ****************************************************************************/
1020 /* Chapters */
1021 static void LoadChapterGpac( demux_t  *p_demux, MP4_Box_t *p_chpl )
1022 {
1023     demux_sys_t *p_sys = p_demux->p_sys;
1024     int i;
1025
1026     p_sys->p_title = vlc_input_title_New();
1027     for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1028     {
1029         seekpoint_t *s = vlc_seekpoint_New();
1030
1031         s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1032         EnsureUTF8( s->psz_name );
1033         s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1034         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1035     }
1036 }
1037 static void LoadChapterApple( demux_t  *p_demux, mp4_track_t *tk )
1038 {
1039     demux_sys_t *p_sys = p_demux->p_sys;
1040
1041     for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1042     {
1043         const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1044         const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1045         const unsigned int i_size = MP4_TrackSampleSize( tk );
1046
1047         if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1048         {
1049             char p_buffer[256];
1050             const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1051             const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1052
1053             if( i_len > 0 )
1054             {
1055                 seekpoint_t *s = vlc_seekpoint_New();
1056
1057                 s->psz_name = strndup( &p_buffer[2], i_len );
1058                 EnsureUTF8( s->psz_name );
1059
1060                 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1061
1062                 if( !p_sys->p_title )
1063                     p_sys->p_title = vlc_input_title_New();
1064                 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1065             }
1066         }
1067         if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1068                               tk->chunk[tk->i_chunk].i_sample_count )
1069             tk->i_chunk++;
1070     }
1071 }
1072 static void LoadChapter( demux_t  *p_demux )
1073 {
1074     demux_sys_t *p_sys = p_demux->p_sys;
1075     MP4_Box_t *p_chpl;
1076
1077     if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1078     {
1079         LoadChapterGpac( p_demux, p_chpl );
1080     }
1081     else if( p_sys->p_tref_chap )
1082     {
1083         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1084         unsigned int i, j;
1085
1086         /* Load the first subtitle track like quicktime */
1087         for( i = 0; i < p_chap->i_entry_count; i++ )
1088         {
1089             for( j = 0; j < p_sys->i_tracks; j++ )
1090             {
1091                 mp4_track_t *tk = &p_sys->track[j];
1092                 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1093                     tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1094                     break;
1095             }
1096             if( j < p_sys->i_tracks )
1097             {
1098                 LoadChapterApple( p_demux, &p_sys->track[i] );
1099                 break;
1100             }
1101         }
1102     }
1103 }
1104
1105 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1106 static int TrackCreateChunksIndex( demux_t *p_demux,
1107                                    mp4_track_t *p_demux_track )
1108 {
1109     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1110     MP4_Box_t *p_stsc;
1111
1112     unsigned int i_chunk;
1113     unsigned int i_index, i_last;
1114
1115     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1116           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1117         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1118     {
1119         return( VLC_EGENERIC );
1120     }
1121
1122     p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1123     if( !p_demux_track->i_chunk_count )
1124     {
1125         msg_Warn( p_demux, "no chunk defined" );
1126         return( VLC_EGENERIC );
1127     }
1128     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1129                                    sizeof( mp4_chunk_t ) );
1130     if( p_demux_track->chunk == NULL )
1131     {
1132         return VLC_ENOMEM;
1133     }
1134
1135     /* first we read chunk offset */
1136     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1137     {
1138         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1139
1140         ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1141
1142         ck->i_first_dts = 0;
1143         ck->p_sample_count_dts = NULL;
1144         ck->p_sample_delta_dts = NULL;
1145         ck->p_sample_count_pts = NULL;
1146         ck->p_sample_offset_pts = NULL;
1147     }
1148
1149     /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1150         to be used for the sample XXX begin to 1
1151         We construct it begining at the end */
1152     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1153     i_index = p_stsc->data.p_stsc->i_entry_count;
1154     if( !i_index )
1155     {
1156         msg_Warn( p_demux, "cannot read chunk table or table empty" );
1157         return( VLC_EGENERIC );
1158     }
1159
1160     while( i_index-- )
1161     {
1162         for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1163              i_chunk < i_last; i_chunk++ )
1164         {
1165             if( i_chunk >= p_demux_track->i_chunk_count )
1166             {
1167                 msg_Warn( p_demux, "corrupted chunk table" );
1168                 return VLC_EGENERIC;
1169             }
1170
1171             p_demux_track->chunk[i_chunk].i_sample_description_index =
1172                     p_stsc->data.p_stsc->i_sample_description_index[i_index];
1173             p_demux_track->chunk[i_chunk].i_sample_count =
1174                     p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1175         }
1176         i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1177     }
1178
1179     p_demux_track->chunk[0].i_sample_first = 0;
1180     for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1181     {
1182         p_demux_track->chunk[i_chunk].i_sample_first =
1183             p_demux_track->chunk[i_chunk-1].i_sample_first +
1184                 p_demux_track->chunk[i_chunk-1].i_sample_count;
1185     }
1186
1187     msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1188              p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1189
1190     return VLC_SUCCESS;
1191 }
1192
1193 static int TrackCreateSamplesIndex( demux_t *p_demux,
1194                                     mp4_track_t *p_demux_track )
1195 {
1196     MP4_Box_t *p_box;
1197     MP4_Box_data_stsz_t *stsz;
1198     MP4_Box_data_stts_t *stts;
1199     /* TODO use also stss and stsh table for seeking */
1200     /* FIXME use edit table */
1201     int64_t i_sample;
1202     int64_t i_chunk;
1203
1204     int64_t i_index;
1205     int64_t i_index_sample_used;
1206
1207     int64_t i_last_dts;
1208
1209     /* Find stsz
1210      *  Gives the sample size for each samples. There is also a stz2 table
1211      *  (compressed form) that we need to implement TODO */
1212     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1213     if( !p_box )
1214     {
1215         /* FIXME and stz2 */
1216         msg_Warn( p_demux, "cannot find STSZ box" );
1217         return VLC_EGENERIC;
1218     }
1219     stsz = p_box->data.p_stsz;
1220
1221     /* Find stts
1222      *  Gives mapping between sample and decoding time
1223      */
1224     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1225     if( !p_box )
1226     {
1227         msg_Warn( p_demux, "cannot find STTS box" );
1228         return VLC_EGENERIC;
1229     }
1230     stts = p_box->data.p_stts;
1231
1232     /* Use stsz table to create a sample number -> sample size table */
1233     p_demux_track->i_sample_count = stsz->i_sample_count;
1234     if( stsz->i_sample_size )
1235     {
1236         /* 1: all sample have the same size, so no need to construct a table */
1237         p_demux_track->i_sample_size = stsz->i_sample_size;
1238         p_demux_track->p_sample_size = NULL;
1239     }
1240     else
1241     {
1242         /* 2: each sample can have a different size */
1243         p_demux_track->i_sample_size = 0;
1244         p_demux_track->p_sample_size =
1245             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1246         if( p_demux_track->p_sample_size == NULL )
1247             return VLC_ENOMEM;
1248
1249         for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1250         {
1251             p_demux_track->p_sample_size[i_sample] =
1252                     stsz->i_entry_size[i_sample];
1253         }
1254     }
1255
1256     /* Use stts table to create a sample number -> dts table.
1257      * XXX: if we don't want to waste too much memory, we can't expand
1258      *  the box! so each chunk will contain an "extract" of this table
1259      *  for fast research (problem with raw stream where a sample is sometime
1260      *  just channels*bits_per_sample/8 */
1261
1262     i_last_dts = 0;
1263     i_index = 0; i_index_sample_used = 0;
1264     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1265     {
1266         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1267         int64_t i_entry, i_sample_count, i;
1268
1269         /* save last dts */
1270         ck->i_first_dts = i_last_dts;
1271
1272         /* count how many entries are needed for this chunk
1273          * for p_sample_delta_dts and p_sample_count_dts */
1274         i_sample_count = ck->i_sample_count;
1275
1276         i_entry = 0;
1277         while( i_sample_count > 0 )
1278         {
1279             i_sample_count -= stts->i_sample_count[i_index+i_entry];
1280             /* don't count already used sample in this entry */
1281             if( i_entry == 0 )
1282                 i_sample_count += i_index_sample_used;
1283
1284             i_entry++;
1285         }
1286
1287         /* allocate them */
1288         ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1289         ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1290
1291         /* now copy */
1292         i_sample_count = ck->i_sample_count;
1293         for( i = 0; i < i_entry; i++ )
1294         {
1295             int64_t i_used;
1296             int64_t i_rest;
1297
1298             i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1299
1300             i_used = __MIN( i_rest, i_sample_count );
1301
1302             i_index_sample_used += i_used;
1303             i_sample_count -= i_used;
1304
1305             ck->p_sample_count_dts[i] = i_used;
1306             ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1307
1308             i_last_dts += i_used * ck->p_sample_delta_dts[i];
1309
1310             if( i_index_sample_used >= stts->i_sample_count[i_index] )
1311             {
1312                 i_index++;
1313                 i_index_sample_used = 0;
1314             }
1315         }
1316     }
1317
1318     /* Find ctts
1319      *  Gives the delta between decoding time (dts) and composition table (pts)
1320      */
1321     p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1322     if( p_box )
1323     {
1324         MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1325
1326         msg_Warn( p_demux, "CTTS table" );
1327
1328         /* Create pts-dts table per chunk */
1329         i_index = 0; i_index_sample_used = 0;
1330         for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1331         {
1332             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1333             int64_t i_entry, i_sample_count, i;
1334
1335             /* count how many entries are needed for this chunk
1336              * for p_sample_delta_dts and p_sample_count_dts */
1337             i_sample_count = ck->i_sample_count;
1338
1339             i_entry = 0;
1340             while( i_sample_count > 0 )
1341             {
1342                 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1343
1344                 /* don't count already used sample in this entry */
1345                 if( i_entry == 0 )
1346                     i_sample_count += i_index_sample_used;
1347
1348                 i_entry++;
1349             }
1350
1351             /* allocate them */
1352             ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1353             ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1354
1355             /* now copy */
1356             i_sample_count = ck->i_sample_count;
1357             for( i = 0; i < i_entry; i++ )
1358             {
1359                 int64_t i_used;
1360                 int64_t i_rest;
1361
1362                 i_rest = ctts->i_sample_count[i_index] -
1363                     i_index_sample_used;
1364
1365                 i_used = __MIN( i_rest, i_sample_count );
1366
1367                 i_index_sample_used += i_used;
1368                 i_sample_count -= i_used;
1369
1370                 ck->p_sample_count_pts[i] = i_used;
1371                 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1372
1373                 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1374                 {
1375                     i_index++;
1376                     i_index_sample_used = 0;
1377                 }
1378             }
1379         }
1380     }
1381
1382     msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1383              p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1384              i_last_dts / p_demux_track->i_timescale );
1385
1386     return VLC_SUCCESS;
1387 }
1388
1389 /*
1390  * TrackCreateES:
1391  * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1392  */
1393 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1394                           unsigned int i_chunk, es_out_id_t **pp_es )
1395 {
1396     MP4_Box_t   *p_sample;
1397     MP4_Box_t   *p_esds;
1398     MP4_Box_t   *p_box;
1399
1400     if( pp_es )
1401         *pp_es = NULL;
1402
1403     if( !p_track->chunk[i_chunk].i_sample_description_index )
1404     {
1405         msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1406                   p_track->i_track_ID );
1407         return VLC_EGENERIC;
1408     }
1409
1410     p_sample = MP4_BoxGet(  p_track->p_stsd, "[%d]",
1411                 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1412
1413     if( !p_sample ||
1414         ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1415     {
1416         msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1417                   p_track->i_track_ID );
1418         return VLC_EGENERIC;
1419     }
1420
1421     p_track->p_sample = p_sample;
1422
1423     if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1424     {
1425         MP4_Box_data_sample_soun_t *p_soun;
1426
1427         p_soun = p_sample->data.p_sample_soun;
1428
1429         if( p_soun->i_qt_version == 0 )
1430         {
1431             switch( p_sample->i_type )
1432             {
1433                 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1434                     p_soun->i_qt_version = 1;
1435                     p_soun->i_sample_per_packet = 64;
1436                     p_soun->i_bytes_per_packet  = 34;
1437                     p_soun->i_bytes_per_frame   = 34 * p_soun->i_channelcount;
1438                     p_soun->i_bytes_per_sample  = 2;
1439                     break;
1440                 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1441                     p_soun->i_qt_version = 1;
1442                     p_soun->i_sample_per_packet = 6;
1443                     p_soun->i_bytes_per_packet  = 2;
1444                     p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
1445                     p_soun->i_bytes_per_sample  = 2;
1446                     break;
1447                 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1448                     p_soun->i_qt_version = 1;
1449                     p_soun->i_sample_per_packet = 12;
1450                     p_soun->i_bytes_per_packet  = 2;
1451                     p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
1452                     p_soun->i_bytes_per_sample  = 2;
1453                     break;
1454                 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1455                 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1456                     p_soun->i_samplesize = 8;
1457                     break;
1458                 default:
1459                     break;
1460             }
1461
1462         }
1463         else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1464         {
1465             p_soun->i_qt_version = 0;
1466         }
1467     }
1468
1469
1470     /* It's a little ugly but .. there are special cases */
1471     switch( p_sample->i_type )
1472     {
1473         case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1474         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1475             p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1476             break;
1477
1478         case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1479             p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1480
1481             /* Buggy files workaround */
1482             if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1483                 p_sample->data.p_sample_soun->i_sampleratehi) )
1484             {
1485                 MP4_Box_data_sample_soun_t *p_soun =
1486                     p_sample->data.p_sample_soun;
1487
1488                 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1489                           "(%u), making both equal (report any problem).",
1490                           p_track->i_timescale, p_soun->i_sampleratehi );
1491
1492                 if( p_soun->i_sampleratehi )
1493                     p_track->i_timescale = p_soun->i_sampleratehi;
1494                 else
1495                     p_soun->i_sampleratehi = p_track->i_timescale;
1496             }
1497             break;
1498
1499         case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1500             p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1501             break;
1502
1503         case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1504         case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1505             p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1506             /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1507             /* FIXME UTF-8 doesn't work here ? */
1508             p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1509             break;
1510
1511         case VLC_FOURCC('y','v','1','2'):
1512             p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1513             break;
1514         case VLC_FOURCC('y','u','v','2'):
1515             p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1516             break;
1517
1518         case VLC_FOURCC('i','n','2','4'):
1519             /* in in24/in32 there's enda-atom to tell it's little-endian (if present) */
1520             if( ( MP4_BoxGet( p_sample, "wave/enda" ) ) ||
1521                 ( MP4_BoxGet( p_sample, "enda" ) ) )
1522             {
1523                 p_track->fmt.i_codec = VLC_FOURCC('4','2','n','i');
1524             } else {
1525                 p_track->fmt.i_codec = p_sample->i_type;
1526             }
1527             break;
1528
1529         default:
1530             p_track->fmt.i_codec = p_sample->i_type;
1531             break;
1532     }
1533
1534     /* now see if esds is present and if so create a data packet
1535         with decoder_specific_info  */
1536 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1537     if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1538           ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1539         ( p_esds->data.p_esds )&&
1540         ( p_decconfig ) )
1541     {
1542         /* First update information based on i_objectTypeIndication */
1543         switch( p_decconfig->i_objectTypeIndication )
1544         {
1545             case( 0x20 ): /* MPEG4 VIDEO */
1546                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1547                 break;
1548             case( 0x40):
1549                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1550                 break;
1551             case( 0x60):
1552             case( 0x61):
1553             case( 0x62):
1554             case( 0x63):
1555             case( 0x64):
1556             case( 0x65): /* MPEG2 video */
1557                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1558                 break;
1559             /* Theses are MPEG2-AAC */
1560             case( 0x66): /* main profile */
1561             case( 0x67): /* Low complexity profile */
1562             case( 0x68): /* Scaleable Sampling rate profile */
1563                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1564                 break;
1565             /* true MPEG 2 audio */
1566             case( 0x69):
1567                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1568                 break;
1569             case( 0x6a): /* MPEG1 video */
1570                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1571                 break;
1572             case( 0x6b): /* MPEG1 audio */
1573                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1574                 break;
1575             case( 0x6c ): /* jpeg */
1576                 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1577                 break;
1578
1579             /* Private ID */
1580             case( 0xe0 ): /* NeroDigital: dvd subs */
1581                 if( p_track->fmt.i_cat == SPU_ES )
1582                 {
1583                     p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1584                     if( p_track->i_width > 0 )
1585                         p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1586                     if( p_track->i_height > 0 )
1587                         p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1588                     break;
1589                 }
1590             /* Fallback */
1591             default:
1592                 /* Unknown entry, but don't touch i_fourcc */
1593                 msg_Warn( p_demux,
1594                           "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1595                           p_decconfig->i_objectTypeIndication,
1596                           p_track->i_track_ID );
1597                 break;
1598         }
1599         p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1600         if( p_track->fmt.i_extra > 0 )
1601         {
1602             p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1603             memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1604                     p_track->fmt.i_extra );
1605         }
1606     }
1607     else
1608     {
1609         switch( p_sample->i_type )
1610         {
1611             /* qt decoder, send the complete chunk */
1612             case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1613             case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1614             case VLC_FOURCC( 'V', 'P', '3', '1' ):
1615             case VLC_FOURCC( '3', 'I', 'V', '1' ):
1616             case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1617                 p_track->fmt.i_extra =
1618                     p_sample->data.p_sample_vide->i_qt_image_description;
1619                 if( p_track->fmt.i_extra > 0 )
1620                 {
1621                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1622                     memcpy( p_track->fmt.p_extra,
1623                             p_sample->data.p_sample_vide->p_qt_image_description,
1624                             p_track->fmt.i_extra);
1625                 }
1626                 break;
1627             case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1628             case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1629             case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1630             case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1631             case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1632                 p_track->fmt.i_extra =
1633                     p_sample->data.p_sample_soun->i_qt_description;
1634                 if( p_track->fmt.i_extra > 0 )
1635                 {
1636                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1637                     memcpy( p_track->fmt.p_extra,
1638                             p_sample->data.p_sample_soun->p_qt_description,
1639                             p_track->fmt.i_extra);
1640                 }
1641                 break;
1642
1643             /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1644             case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1645             {
1646                 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1647
1648                 if( p_avcC )
1649                 {
1650                     p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1651                     p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1652                     memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1653                             p_track->fmt.i_extra );
1654                 }
1655                 else
1656                 {
1657                     msg_Err( p_demux, "missing avcC" );
1658                 }
1659                 break;
1660             }
1661
1662             default:
1663                 break;
1664         }
1665     }
1666
1667 #undef p_decconfig
1668
1669     /* some last initialisation */
1670     switch( p_track->fmt.i_cat )
1671     {
1672     case( VIDEO_ES ):
1673         p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1674         p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1675         p_track->fmt.video.i_bits_per_pixel =
1676             p_sample->data.p_sample_vide->i_depth;
1677
1678         /* fall on display size */
1679         if( p_track->fmt.video.i_width <= 0 )
1680             p_track->fmt.video.i_width = p_track->i_width;
1681         if( p_track->fmt.video.i_height <= 0 )
1682             p_track->fmt.video.i_height = p_track->i_height;
1683
1684         /* Find out apect ratio from display size */
1685         if( p_track->i_width > 0 && p_track->i_height > 0 &&
1686             /* Work-around buggy muxed files */
1687             p_sample->data.p_sample_vide->i_width != p_track->i_width )
1688             p_track->fmt.video.i_aspect =
1689                 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1690
1691         /* Support for cropping (eg. in H263 files) */
1692         p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1693         p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1694
1695         /* Frame rate */
1696         p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1697         p_track->fmt.video.i_frame_rate_base = 1;
1698
1699         if( p_track->fmt.video.i_frame_rate &&
1700             (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1701             p_box->data.p_stts->i_entry_count >= 1 )
1702         {
1703             p_track->fmt.video.i_frame_rate_base =
1704                 p_box->data.p_stts->i_sample_delta[0];
1705         }
1706
1707         break;
1708
1709     case( AUDIO_ES ):
1710         p_track->fmt.audio.i_channels =
1711             p_sample->data.p_sample_soun->i_channelcount;
1712         p_track->fmt.audio.i_rate =
1713             p_sample->data.p_sample_soun->i_sampleratehi;
1714         p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1715             p_sample->data.p_sample_soun->i_sampleratehi *
1716                 p_sample->data.p_sample_soun->i_samplesize;
1717         p_track->fmt.audio.i_bitspersample =
1718             p_sample->data.p_sample_soun->i_samplesize;
1719         break;
1720
1721     default:
1722         break;
1723     }
1724
1725     if( pp_es )
1726         *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1727
1728     return VLC_SUCCESS;
1729 }
1730
1731 /* given a time it return sample/chunk
1732  * it also update elst field of the track
1733  */
1734 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1735                                    int64_t i_start, uint32_t *pi_chunk,
1736                                    uint32_t *pi_sample )
1737 {
1738     demux_sys_t *p_sys = p_demux->p_sys;
1739     MP4_Box_t   *p_stss;
1740     uint64_t     i_dts;
1741     unsigned int i_sample;
1742     unsigned int i_chunk;
1743     int          i_index;
1744
1745     /* FIXME see if it's needed to check p_track->i_chunk_count */
1746     if( p_track->i_chunk_count == 0 )
1747         return( VLC_EGENERIC );
1748
1749     /* handle elst (find the correct one) */
1750     MP4_TrackSetELST( p_demux, p_track, i_start );
1751     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1752     {
1753         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1754         int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1755
1756         /* now calculate i_start for this elst */
1757         /* offset */
1758         i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
1759         if( i_start < 0 )
1760         {
1761             *pi_chunk = 0;
1762             *pi_sample= 0;
1763
1764             return VLC_SUCCESS;
1765         }
1766         /* to track time scale */
1767         i_start  = i_start * p_track->i_timescale / (int64_t)1000000;
1768         /* add elst offset */
1769         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1770              elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1771             elst->i_media_time[p_track->i_elst] > 0 )
1772         {
1773             i_start += elst->i_media_time[p_track->i_elst];
1774         }
1775
1776         msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
1777                  "ms (track)", p_track->i_elst,
1778                  i_mvt * 1000 / p_sys->i_timescale,
1779                  i_start * 1000 / p_track->i_timescale );
1780     }
1781     else
1782     {
1783         /* convert absolute time to in timescale unit */
1784         i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1785     }
1786
1787     /* we start from sample 0/chunk 0, hope it won't take too much time */
1788     /* *** find good chunk *** */
1789     for( i_chunk = 0; ; i_chunk++ )
1790     {
1791         if( i_chunk + 1 >= p_track->i_chunk_count )
1792         {
1793             /* at the end and can't check if i_start in this chunk,
1794                it will be check while searching i_sample */
1795             i_chunk = p_track->i_chunk_count - 1;
1796             break;
1797         }
1798
1799         if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1800             (uint64_t)i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
1801         {
1802             break;
1803         }
1804     }
1805
1806     /* *** find sample in the chunk *** */
1807     i_sample = p_track->chunk[i_chunk].i_sample_first;
1808     i_dts    = p_track->chunk[i_chunk].i_first_dts;
1809     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1810     {
1811         if( i_dts +
1812             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1813             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1814         {
1815             i_dts    +=
1816                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1817                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1818
1819             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1820             i_index++;
1821         }
1822         else
1823         {
1824             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1825             {
1826                 break;
1827             }
1828             i_sample += ( i_start - i_dts ) /
1829                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1830             break;
1831         }
1832     }
1833
1834     if( i_sample >= p_track->i_sample_count )
1835     {
1836         msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1837                   "(seeking too far) chunk=%d sample=%d",
1838                   p_track->i_track_ID, i_chunk, i_sample );
1839         return( VLC_EGENERIC );
1840     }
1841
1842
1843     /* *** Try to find nearest sync points *** */
1844     if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1845     {
1846         unsigned int i_index;
1847         msg_Dbg( p_demux,
1848                     "track[Id 0x%x] using Sync Sample Box (stss)",
1849                     p_track->i_track_ID );
1850         for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1851         {
1852             if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1853             {
1854                 if( i_index > 0 )
1855                 {
1856                     msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1857                             i_sample,
1858                             p_stss->data.p_stss->i_sample_number[i_index-1] );
1859                     i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1860                     /* new i_sample is less than old so i_chunk can only decreased */
1861                     while( i_chunk > 0 &&
1862                             i_sample < p_track->chunk[i_chunk].i_sample_first )
1863                     {
1864                         i_chunk--;
1865                     }
1866                 }
1867                 else
1868                 {
1869                     msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1870                             i_sample,
1871                             p_stss->data.p_stss->i_sample_number[i_index] );
1872                     i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1873                     /* new i_sample is more than old so i_chunk can only increased */
1874                     while( i_chunk < p_track->i_chunk_count - 1 &&
1875                            i_sample >= p_track->chunk[i_chunk].i_sample_first +
1876                              p_track->chunk[i_chunk].i_sample_count )
1877                     {
1878                         i_chunk++;
1879                     }
1880                 }
1881                 break;
1882             }
1883         }
1884     }
1885     else
1886     {
1887         msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1888                  "Sample Box (stss)", p_track->i_track_ID );
1889     }
1890
1891     *pi_chunk  = i_chunk;
1892     *pi_sample = i_sample;
1893
1894     return VLC_SUCCESS;
1895 }
1896
1897 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1898                                  unsigned int i_chunk, unsigned int i_sample )
1899 {
1900     bool b_reselect = false;
1901
1902     /* now see if actual es is ok */
1903     if( (p_track->i_chunk >= p_track->i_chunk_count - 1) ||
1904         (p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1905             p_track->chunk[i_chunk].i_sample_description_index) )
1906     {
1907         msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1908                   p_track->i_track_ID );
1909
1910         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1911                         p_track->p_es, &b_reselect );
1912
1913         es_out_Del( p_demux->out, p_track->p_es );
1914
1915         p_track->p_es = NULL;
1916
1917         if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1918         {
1919             msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1920                      p_track->i_track_ID );
1921
1922             p_track->b_ok       = false;
1923             p_track->b_selected = false;
1924             return VLC_EGENERIC;
1925         }
1926     }
1927
1928     /* select again the new decoder */
1929     if( b_reselect )
1930     {
1931         es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1932     }
1933
1934     p_track->i_chunk    = i_chunk;
1935     p_track->i_sample   = i_sample;
1936
1937     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1938 }
1939
1940 /****************************************************************************
1941  * MP4_TrackCreate:
1942  ****************************************************************************
1943  * Parse track information and create all needed data to run a track
1944  * If it succeed b_ok is set to 1 else to 0
1945  ****************************************************************************/
1946 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1947                              MP4_Box_t *p_box_trak )
1948 {
1949     demux_sys_t *p_sys = p_demux->p_sys;
1950
1951     MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1952     MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1953     MP4_Box_t *p_elst;
1954
1955     MP4_Box_t *p_mdhd;
1956     MP4_Box_t *p_udta;
1957     MP4_Box_t *p_hdlr;
1958
1959     MP4_Box_t *p_vmhd;
1960     MP4_Box_t *p_smhd;
1961
1962     MP4_Box_t *p_drms;
1963
1964     unsigned int i;
1965     char language[4];
1966
1967     /* hint track unsupported */
1968
1969     /* set default value (-> track unusable) */
1970     p_track->b_ok       = false;
1971     p_track->b_enable   = false;
1972     p_track->b_selected = false;
1973     p_track->b_chapter  = false;
1974
1975     es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1976
1977     if( !p_tkhd )
1978     {
1979         return;
1980     }
1981
1982     /* do we launch this track by default ? */
1983     p_track->b_enable =
1984         ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1985
1986     p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1987     p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1988     p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1989
1990     if( p_tref )
1991     {
1992 /*        msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1993     }
1994
1995     p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1996     p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1997
1998     if( ( !p_mdhd )||( !p_hdlr ) )
1999     {
2000         return;
2001     }
2002
2003     p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2004     if( !p_track->i_timescale )
2005         return;
2006
2007     for( i = 0; i < 3; i++ )
2008     {
2009         language[i] = p_mdhd->data.p_mdhd->i_language[i];
2010     }
2011     language[3] = '\0';
2012
2013     switch( p_hdlr->data.p_hdlr->i_handler_type )
2014     {
2015         case( FOURCC_soun ):
2016             if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2017             {
2018                 return;
2019             }
2020             p_track->fmt.i_cat = AUDIO_ES;
2021             break;
2022
2023         case( FOURCC_vide ):
2024             if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2025             {
2026                 return;
2027             }
2028             p_track->fmt.i_cat = VIDEO_ES;
2029             break;
2030
2031         case( FOURCC_text ):
2032         case( FOURCC_subp ):
2033         case( FOURCC_tx3g ):
2034             p_track->fmt.i_cat = SPU_ES;
2035             break;
2036
2037         default:
2038             return;
2039     }
2040
2041     p_track->i_elst = 0;
2042     p_track->i_elst_time = 0;
2043     if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2044     {
2045         MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2046         unsigned int i;
2047
2048         msg_Warn( p_demux, "elst box found" );
2049         for( i = 0; i < elst->i_entry_count; i++ )
2050         {
2051             msg_Dbg( p_demux, "   - [%d] duration=%"PRId64"ms media time=%"PRId64
2052                      "ms) rate=%d.%d", i,
2053                      elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2054                      elst->i_media_time[i] >= 0 ?
2055                      (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2056                      INT64_C(-1),
2057                      elst->i_media_rate_integer[i],
2058                      elst->i_media_rate_fraction[i] );
2059         }
2060     }
2061
2062
2063 /*  TODO
2064     add support for:
2065     p_dinf = MP4_BoxGet( p_minf, "dinf" );
2066 */
2067     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2068         !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2069     {
2070         return;
2071     }
2072
2073     p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2074     p_track->b_drms = p_drms != NULL;
2075     p_track->p_drms = p_track->b_drms ?
2076         p_drms->data.p_sample_soun->p_drms : NULL;
2077
2078     /* Set language */
2079     if( strcmp( language, "```" ) && strcmp( language, "und" ) )
2080     {
2081         p_track->fmt.psz_language = strdup( language );
2082     }
2083
2084     p_udta = MP4_BoxGet( p_box_trak, "udta" );
2085     if( p_udta )
2086     {
2087         MP4_Box_t *p_0xa9xxx;
2088         for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
2089                  p_0xa9xxx = p_0xa9xxx->p_next )
2090         {
2091             switch( p_0xa9xxx->i_type )
2092             {
2093                 case FOURCC_0xa9nam:
2094                     p_track->fmt.psz_description =
2095                         strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
2096                     break;
2097             }
2098         }
2099     }
2100
2101     /* Create chunk index table and sample index table */
2102     if( TrackCreateChunksIndex( p_demux,p_track  ) ||
2103         TrackCreateSamplesIndex( p_demux, p_track ) )
2104     {
2105         return; /* cannot create chunks index */
2106     }
2107
2108     p_track->i_chunk  = 0;
2109     p_track->i_sample = 0;
2110
2111     /* Mark chapter only track */
2112     if( !p_track->b_enable && p_sys->p_tref_chap )
2113     {
2114         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2115         unsigned int i;
2116
2117         for( i = 0; i < p_chap->i_entry_count; i++ )
2118         {
2119             if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2120             {
2121                 p_track->b_chapter = true;
2122                 break;
2123             }
2124         }
2125     }
2126
2127     /* now create es */
2128     p_track->p_es = NULL;
2129     if( TrackCreateES( p_demux,
2130                        p_track, p_track->i_chunk,
2131                        p_track->b_chapter ? NULL : &p_track->p_es ) )
2132     {
2133         msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2134                  p_track->i_track_ID );
2135         return;
2136     }
2137     p_track->b_ok = true;
2138 #if 0
2139     {
2140         int i;
2141         for( i = 0; i < p_track->i_chunk_count; i++ )
2142         {
2143             fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2144                      i, p_track->chunk[i].i_sample_count,
2145                      p_track->chunk[i].i_first_dts );
2146
2147         }
2148     }
2149 #endif
2150 }
2151
2152 /****************************************************************************
2153  * MP4_TrackDestroy:
2154  ****************************************************************************
2155  * Destroy a track created by MP4_TrackCreate.
2156  ****************************************************************************/
2157 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
2158 {
2159     unsigned int i_chunk;
2160
2161     p_track->b_ok = false;
2162     p_track->b_enable   = false;
2163     p_track->b_selected = false;
2164
2165     es_format_Clean( &p_track->fmt );
2166
2167     for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2168     {
2169         if( p_track->chunk )
2170         {
2171            FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2172            FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2173
2174            FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2175            FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2176         }
2177     }
2178     FREENULL( p_track->chunk );
2179
2180     if( !p_track->i_sample_size )
2181     {
2182         FREENULL( p_track->p_sample_size );
2183     }
2184 }
2185
2186 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2187                             mtime_t i_start )
2188 {
2189     if( !p_track->b_ok || p_track->b_chapter )
2190     {
2191         return VLC_EGENERIC;
2192     }
2193
2194     if( p_track->b_selected )
2195     {
2196         msg_Warn( p_demux, "track[Id 0x%x] already selected",
2197                   p_track->i_track_ID );
2198         return VLC_SUCCESS;
2199     }
2200
2201     return MP4_TrackSeek( p_demux, p_track, i_start );
2202 }
2203
2204 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2205 {
2206     if( !p_track->b_ok || p_track->b_chapter )
2207     {
2208         return;
2209     }
2210
2211     if( !p_track->b_selected )
2212     {
2213         msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2214                   p_track->i_track_ID );
2215         return;
2216     }
2217     if( p_track->p_es )
2218     {
2219         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2220                         p_track->p_es, false );
2221     }
2222
2223     p_track->b_selected = false;
2224 }
2225
2226 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2227                           mtime_t i_start )
2228 {
2229     uint32_t i_chunk;
2230     uint32_t i_sample;
2231
2232     if( !p_track->b_ok || p_track->b_chapter )
2233         return VLC_EGENERIC;
2234
2235     p_track->b_selected = false;
2236
2237     if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2238                                 &i_chunk, &i_sample ) )
2239     {
2240         msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2241                   p_track->i_track_ID );
2242         return VLC_EGENERIC;
2243     }
2244
2245     p_track->b_selected = true;
2246
2247     if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
2248         VLC_SUCCESS )
2249     {
2250         p_track->b_selected = true;
2251
2252         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
2253                         p_track->p_es, i_start );
2254     }
2255
2256     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2257 }
2258
2259
2260 /*
2261  * 3 types: for audio
2262  *
2263  */
2264 #define QT_V0_MAX_SAMPLES 1024
2265 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2266 {
2267     int i_size;
2268     MP4_Box_data_sample_soun_t *p_soun;
2269
2270     if( p_track->i_sample_size == 0 )
2271     {
2272         /* most simple case */
2273         return p_track->p_sample_size[p_track->i_sample];
2274     }
2275     if( p_track->fmt.i_cat != AUDIO_ES )
2276     {
2277         return p_track->i_sample_size;
2278     }
2279
2280     p_soun = p_track->p_sample->data.p_sample_soun;
2281
2282     if( p_soun->i_qt_version == 1 )
2283     {
2284         i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2285             p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2286     }
2287     else if( p_track->i_sample_size > 256 )
2288     {
2289         /* We do that so we don't read too much data
2290          * (in this case we are likely dealing with compressed data) */
2291         i_size = p_track->i_sample_size;
2292     }
2293     else
2294     {
2295         /* Read a bunch of samples at once */
2296         int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2297             ( p_track->i_sample -
2298               p_track->chunk[p_track->i_chunk].i_sample_first );
2299
2300         i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2301         i_size = i_samples * p_track->i_sample_size;
2302     }
2303
2304     //fprintf( stderr, "size=%d\n", i_size );
2305     return i_size;
2306 }
2307
2308 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2309 {
2310     unsigned int i_sample;
2311     uint64_t i_pos;
2312
2313     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2314
2315     if( p_track->i_sample_size )
2316     {
2317         MP4_Box_data_sample_soun_t *p_soun =
2318             p_track->p_sample->data.p_sample_soun;
2319
2320         if( p_soun->i_qt_version == 0 )
2321         {
2322             i_pos += ( p_track->i_sample -
2323                        p_track->chunk[p_track->i_chunk].i_sample_first ) *
2324                      p_track->i_sample_size;
2325         }
2326         else
2327         {
2328             /* we read chunk by chunk */
2329             i_pos += 0;
2330         }
2331     }
2332     else
2333     {
2334         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2335              i_sample < p_track->i_sample; i_sample++ )
2336         {
2337             i_pos += p_track->p_sample_size[i_sample];
2338         }
2339     }
2340
2341     return i_pos;
2342 }
2343
2344 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2345 {
2346     if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2347     {
2348         MP4_Box_data_sample_soun_t *p_soun;
2349
2350         p_soun = p_track->p_sample->data.p_sample_soun;
2351
2352         if( p_soun->i_qt_version == 1 )
2353         {
2354             /* chunk by chunk */
2355             p_track->i_sample =
2356                 p_track->chunk[p_track->i_chunk].i_sample_first +
2357                 p_track->chunk[p_track->i_chunk].i_sample_count;
2358         }
2359         else if( p_track->i_sample_size > 256 )
2360         {
2361             /* We do that so we don't read too much data
2362              * (in this case we are likely dealing with compressed data) */
2363             p_track->i_sample += 1;
2364         }
2365         else
2366         {
2367             /* FIXME */
2368             p_track->i_sample += QT_V0_MAX_SAMPLES;
2369             if( p_track->i_sample >
2370                 p_track->chunk[p_track->i_chunk].i_sample_first +
2371                 p_track->chunk[p_track->i_chunk].i_sample_count )
2372             {
2373                 p_track->i_sample =
2374                     p_track->chunk[p_track->i_chunk].i_sample_first +
2375                     p_track->chunk[p_track->i_chunk].i_sample_count;
2376             }
2377         }
2378     }
2379     else
2380     {
2381         p_track->i_sample++;
2382     }
2383
2384     if( p_track->i_sample >= p_track->i_sample_count )
2385         return VLC_EGENERIC;
2386
2387     /* Have we changed chunk ? */
2388     if( p_track->i_sample >=
2389             p_track->chunk[p_track->i_chunk].i_sample_first +
2390             p_track->chunk[p_track->i_chunk].i_sample_count )
2391     {
2392         if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2393                                   p_track->i_sample ) )
2394         {
2395             msg_Warn( p_demux, "track[0x%x] will be disabled "
2396                       "(cannot restart decoder)", p_track->i_track_ID );
2397             MP4_TrackUnselect( p_demux, p_track );
2398             return VLC_EGENERIC;
2399         }
2400     }
2401
2402     /* Have we changed elst */
2403     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2404     {
2405         demux_sys_t *p_sys = p_demux->p_sys;
2406         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2407         uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2408                         p_sys->i_timescale / (int64_t)1000000;
2409
2410         if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2411             i_mvt >= p_track->i_elst_time +
2412                      elst->i_segment_duration[p_track->i_elst] )
2413         {
2414             MP4_TrackSetELST( p_demux, p_track,
2415                               MP4_TrackGetDTS( p_demux, p_track ) );
2416         }
2417     }
2418
2419     return VLC_SUCCESS;
2420 }
2421
2422 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2423                               int64_t i_time )
2424 {
2425     demux_sys_t *p_sys = p_demux->p_sys;
2426     int         i_elst_last = tk->i_elst;
2427
2428     /* handle elst (find the correct one) */
2429     tk->i_elst      = 0;
2430     tk->i_elst_time = 0;
2431     if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2432     {
2433         MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2434         int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2435
2436         for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2437         {
2438             mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2439
2440             if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2441             {
2442                 break;
2443             }
2444             tk->i_elst_time += i_dur;
2445         }
2446
2447         if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2448         {
2449             /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2450             tk->i_elst = elst->i_entry_count - 1;
2451             tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2452         }
2453
2454         if( elst->i_media_time[tk->i_elst] < 0 )
2455         {
2456             /* track offset */
2457             tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2458         }
2459     }
2460     if( i_elst_last != tk->i_elst )
2461     {
2462         msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2463     }
2464 }
2465
2466 static bool FindItem( demux_t *p_demux, playlist_t *p_playlist,
2467                             playlist_item_t **pp_item )
2468 {
2469     input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
2470     bool b_play = var_CreateGetBool( p_demux, "playlist-autostart" );
2471
2472     *pp_item = NULL;
2473     if( p_input )
2474     {
2475         if( b_play && p_playlist->status.p_item &&
2476             p_playlist->status.p_item->p_input == input_GetItem(p_input) )
2477         {
2478             msg_Dbg( p_playlist, "starting playlist playback" );
2479             *pp_item = p_playlist->status.p_item;
2480             b_play = true;
2481         }
2482         else
2483         {
2484             input_item_t *p_current = input_GetItem( p_input );
2485
2486             *pp_item = playlist_ItemGetByInput( p_playlist, p_current, false );
2487             if( !*pp_item )
2488                 msg_Dbg( p_playlist, "unable to find item in playlist");
2489
2490             msg_Dbg( p_playlist, "not starting playlist playback");
2491             b_play = false;
2492         }
2493         vlc_object_release( p_input );
2494     }
2495     return b_play;
2496 }
2497
2498