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