]> git.sesse.net Git - vlc/blob - modules/demux/mp4/mp4.c
* modules/demux/mp4/mp4.c: work-around buggy muxers which don't set properly the...
[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             /* Work-around buggy muxed files */
1151             p_sample->data.p_sample_vide->i_width != p_track->i_width )
1152             p_track->fmt.video.i_aspect =
1153                 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1154
1155         break;
1156
1157     case( AUDIO_ES ):
1158         p_track->fmt.audio.i_channels =
1159             p_sample->data.p_sample_soun->i_channelcount;
1160         p_track->fmt.audio.i_rate =
1161             p_sample->data.p_sample_soun->i_sampleratehi;
1162         p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1163             p_sample->data.p_sample_soun->i_sampleratehi *
1164                 p_sample->data.p_sample_soun->i_samplesize;
1165         p_track->fmt.audio.i_bitspersample =
1166             p_sample->data.p_sample_soun->i_samplesize;
1167         break;
1168
1169     default:
1170         break;
1171     }
1172
1173     *pp_es = es_out_Add( p_input->p_es_out, &p_track->fmt );
1174
1175     return VLC_SUCCESS;
1176 }
1177
1178 /* given a time it return sample/chunk
1179  * it also update elst field of the track
1180  */
1181 static int  TrackTimeToSampleChunk( input_thread_t *p_input,
1182                                     mp4_track_t *p_track,
1183                                     int64_t i_start,
1184                                     uint32_t *pi_chunk,
1185                                     uint32_t *pi_sample )
1186 {
1187     demux_sys_t *p_sys = p_input->p_demux_data;
1188     MP4_Box_t   *p_stss;
1189     uint64_t     i_dts;
1190     unsigned int i_sample;
1191     unsigned int i_chunk;
1192     int          i_index;
1193
1194     /* FIXME see if it's needed to check p_track->i_chunk_count */
1195     if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1196     {
1197         return( VLC_EGENERIC );
1198     }
1199
1200     /* handle elst (find the correct one) */
1201     MP4_TrackSetELST( p_input, p_track, i_start );
1202     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1203     {
1204         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1205         int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1206
1207         /* now calculate i_start for this elst */
1208         /* offset */
1209         i_start -= p_track->i_elst_time * (int64_t)1000000 / p_sys->i_timescale;
1210         if( i_start < 0 )
1211         {
1212             *pi_chunk = 0;
1213             *pi_sample= 0;
1214
1215             return VLC_SUCCESS;
1216         }
1217         /* to track time scale */
1218         i_start  = i_start * p_track->i_timescale / (int64_t)1000000;
1219         /* add elst offset */
1220         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1221              elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1222             elst->i_media_time[p_track->i_elst] > 0 )
1223         {
1224             i_start += elst->i_media_time[p_track->i_elst];
1225         }
1226
1227         msg_Dbg( p_input, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd"ms (track)",
1228                  p_track->i_elst,
1229                  i_mvt * 1000 / p_sys->i_timescale,
1230                  i_start * 1000 / p_track->i_timescale );
1231     }
1232     else
1233     {
1234         /* convert absolute time to in timescale unit */
1235         i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1236     }
1237
1238     /* we start from sample 0/chunk 0, hope it won't take too much time */
1239     /* *** find good chunk *** */
1240     for( i_chunk = 0; ; i_chunk++ )
1241     {
1242         if( i_chunk + 1 >= p_track->i_chunk_count )
1243         {
1244             /* at the end and can't check if i_start in this chunk,
1245                it will be check while searching i_sample */
1246             i_chunk = p_track->i_chunk_count - 1;
1247             break;
1248         }
1249
1250         if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1251             i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
1252         {
1253             break;
1254         }
1255     }
1256
1257     /* *** find sample in the chunk *** */
1258     i_sample = p_track->chunk[i_chunk].i_sample_first;
1259     i_dts    = p_track->chunk[i_chunk].i_first_dts;
1260     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1261     {
1262         if( i_dts +
1263             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1264             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1265         {
1266             i_dts    +=
1267                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1268                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1269
1270             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1271             i_index++;
1272         }
1273         else
1274         {
1275             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1276             {
1277                 break;
1278             }
1279             i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1280             break;
1281         }
1282     }
1283
1284     if( i_sample >= p_track->i_sample_count )
1285     {
1286         msg_Warn( p_input,
1287                   "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1288                   p_track->i_track_ID, i_chunk, i_sample );
1289         return( VLC_EGENERIC );
1290     }
1291
1292
1293     /* *** Try to find nearest sync points *** */
1294     if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1295     {
1296         unsigned int i_index;
1297         msg_Dbg( p_input,
1298                     "track[Id 0x%x] using Sync Sample Box (stss)",
1299                     p_track->i_track_ID );
1300         for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1301         {
1302             if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1303             {
1304                 if( i_index > 0 )
1305                 {
1306                     msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1307                             i_sample,
1308                             p_stss->data.p_stss->i_sample_number[i_index-1] );
1309                     i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1310                     /* new i_sample is less than old so i_chunk can only decreased */
1311                     while( i_chunk > 0 &&
1312                             i_sample < p_track->chunk[i_chunk].i_sample_first )
1313                     {
1314                         i_chunk--;
1315                     }
1316                 }
1317                 else
1318                 {
1319                     msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1320                             i_sample,
1321                             p_stss->data.p_stss->i_sample_number[i_index] );
1322                     i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1323                     /* new i_sample is more than old so i_chunk can only increased */
1324                     while( i_chunk < p_track->i_chunk_count - 1 &&
1325                            i_sample >= p_track->chunk[i_chunk].i_sample_first +
1326                                                 p_track->chunk[i_chunk].i_sample_count )
1327                     {
1328                         i_chunk++;
1329                     }
1330                 }
1331                 break;
1332             }
1333         }
1334     }
1335     else
1336     {
1337         msg_Dbg( p_input,
1338                     "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1339                     p_track->i_track_ID );
1340     }
1341
1342     *pi_chunk  = i_chunk;
1343     *pi_sample = i_sample;
1344
1345     return VLC_SUCCESS;
1346 }
1347
1348 static int  TrackGotoChunkSample( input_thread_t   *p_input,
1349                                   mp4_track_t *p_track,
1350                                   unsigned int     i_chunk,
1351                                   unsigned int     i_sample )
1352 {
1353     vlc_bool_t b_reselect = VLC_FALSE;
1354
1355     /* now see if actual es is ok */
1356     if( p_track->i_chunk < 0 ||
1357         p_track->i_chunk >= p_track->i_chunk_count ||
1358         p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1359             p_track->chunk[i_chunk].i_sample_description_index )
1360     {
1361         msg_Warn( p_input, "recreate ES" );
1362
1363         es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_track->p_es, &b_reselect );
1364
1365         es_out_Del( p_input->p_es_out, p_track->p_es );
1366
1367         p_track->p_es = NULL;
1368
1369         if( TrackCreateES( p_input,
1370                            p_track, i_chunk,
1371                            &p_track->p_es ) )
1372         {
1373             msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1374                      p_track->i_track_ID );
1375
1376             p_track->b_ok       = VLC_FALSE;
1377             p_track->b_selected = VLC_FALSE;
1378             return VLC_EGENERIC;
1379         }
1380     }
1381
1382     /* select again the new decoder */
1383     if( b_reselect )
1384     {
1385         es_out_Control( p_input->p_es_out, ES_OUT_SET_ES, p_track->p_es );
1386     }
1387
1388     p_track->i_chunk    = i_chunk;
1389     p_track->i_sample   = i_sample;
1390
1391     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1392 }
1393
1394 /****************************************************************************
1395  * MP4_TrackCreate:
1396  ****************************************************************************
1397  * Parse track information and create all needed data to run a track
1398  * If it succeed b_ok is set to 1 else to 0
1399  ****************************************************************************/
1400 static void MP4_TrackCreate( input_thread_t *p_input,
1401                              mp4_track_t *p_track,
1402                              MP4_Box_t  * p_box_trak )
1403 {
1404     demux_sys_t *p_sys = p_input->p_demux_data;
1405
1406     MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1407     MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1408     MP4_Box_t *p_elst;
1409
1410     MP4_Box_t *p_mdhd;
1411     MP4_Box_t *p_hdlr;
1412
1413     MP4_Box_t *p_vmhd;
1414     MP4_Box_t *p_smhd;
1415
1416     MP4_Box_t *p_drms;
1417
1418     unsigned int i;
1419     char language[4];
1420
1421     /* hint track unsuported */
1422
1423     /* set default value (-> track unusable) */
1424     p_track->b_ok       = VLC_FALSE;
1425     p_track->b_enable   = VLC_FALSE;
1426     p_track->b_selected = VLC_FALSE;
1427
1428     es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1429
1430     if( !p_tkhd )
1431     {
1432         return;
1433     }
1434
1435     /* do we launch this track by default ? */
1436     p_track->b_enable =
1437         ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1438
1439     p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1440     p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1441     p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1442
1443     if( p_tref )
1444     {
1445 /*        msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1446     }
1447
1448     p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1449     p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1450
1451     if( ( !p_mdhd )||( !p_hdlr ) )
1452     {
1453         return;
1454     }
1455
1456     p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1457
1458     for( i = 0; i < 3; i++ )
1459     {
1460         language[i] = p_mdhd->data.p_mdhd->i_language[i];
1461     }
1462     language[3] = '\0';
1463
1464     switch( p_hdlr->data.p_hdlr->i_handler_type )
1465     {
1466         case( FOURCC_soun ):
1467             if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1468             {
1469                 return;
1470             }
1471             p_track->fmt.i_cat = AUDIO_ES;
1472             break;
1473
1474         case( FOURCC_vide ):
1475             if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1476             {
1477                 return;
1478             }
1479             p_track->fmt.i_cat = VIDEO_ES;
1480             break;
1481
1482         default:
1483             return;
1484     }
1485
1486     p_track->i_elst = 0;
1487     p_track->i_elst_time = 0;
1488     if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1489     {
1490         MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1491         int i;
1492
1493         msg_Warn( p_input, "elst box found" );
1494         for( i = 0; i < elst->i_entry_count; i++ )
1495         {
1496             msg_Dbg( p_input, "   - [%d] duration="I64Fd"ms media time="I64Fd"ms) rate=%d.%d",
1497                      i,
1498                      elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1499                      elst->i_media_time[i] >= 0 ?
1500                         elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
1501                      elst->i_media_rate_integer[i],
1502                      elst->i_media_rate_fraction[i] );
1503         }
1504     }
1505
1506
1507 /*  TODO
1508     add support for:
1509     p_dinf = MP4_BoxGet( p_minf, "dinf" );
1510 */
1511     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1512         !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1513     {
1514         return;
1515     }
1516
1517     p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1518     p_track->b_drms = p_drms != NULL;
1519     p_track->p_drms = p_track->b_drms ?
1520         p_drms->data.p_sample_soun->p_drms : NULL;
1521
1522     /* Set language */
1523     if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1524     {
1525         p_track->fmt.psz_language = strdup( language );
1526     }
1527
1528     /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1529     if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1530     {
1531         MP4_Box_t *p_sample;
1532
1533         p_sample = MP4_BoxGet(  p_track->p_stsd, "[0]" );
1534         if( p_sample && p_sample->data.p_sample_soun)
1535         {
1536             MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1537             if( p_soun->i_qt_version == 0 &&
1538                 p_track->i_timescale != p_soun->i_sampleratehi )
1539             {
1540                 msg_Warn( p_input,
1541                           "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1542                           "qt_version == 0\n"
1543                           "Making both equal. (report any problem)",
1544                           p_track->i_timescale, p_soun->i_sampleratehi );
1545
1546                 if( p_soun->i_sampleratehi )
1547                     p_track->i_timescale = p_soun->i_sampleratehi;
1548                 else
1549                     p_soun->i_sampleratehi = p_track->i_timescale;
1550             }
1551         }
1552     }
1553
1554     /* Create chunk index table and sample index table */
1555     if( TrackCreateChunksIndex( p_input,p_track  ) ||
1556         TrackCreateSamplesIndex( p_input, p_track ) )
1557     {
1558         return; /* cannot create chunks index */
1559     }
1560
1561     p_track->i_chunk  = 0;
1562     p_track->i_sample = 0;
1563
1564     /* now create es */
1565     if( TrackCreateES( p_input,
1566                        p_track, p_track->i_chunk,
1567                        &p_track->p_es ) )
1568     {
1569         msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1570                  p_track->i_track_ID );
1571         return;
1572     }
1573
1574 #if 0
1575     {
1576         int i;
1577         for( i = 0; i < p_track->i_chunk_count; i++ )
1578         {
1579             fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1580
1581         }
1582     }
1583 #endif
1584     p_track->b_ok = VLC_TRUE;
1585 }
1586
1587 /****************************************************************************
1588  * MP4_TrackDestroy:
1589  ****************************************************************************
1590  * Destroy a track created by MP4_TrackCreate.
1591  ****************************************************************************/
1592 static void MP4_TrackDestroy( input_thread_t *p_input,
1593                               mp4_track_t *p_track )
1594 {
1595     unsigned int i_chunk;
1596
1597     p_track->b_ok = VLC_FALSE;
1598     p_track->b_enable   = VLC_FALSE;
1599     p_track->b_selected = VLC_FALSE;
1600
1601     es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1602
1603     for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1604     {
1605         if( p_track->chunk )
1606         {
1607            FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1608            FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1609         }
1610     }
1611     FREE( p_track->chunk );
1612
1613     if( !p_track->i_sample_size )
1614     {
1615         FREE( p_track->p_sample_size );
1616     }
1617 }
1618
1619 static int  MP4_TrackSelect ( input_thread_t    *p_input,
1620                               mp4_track_t  *p_track,
1621                               mtime_t           i_start )
1622 {
1623     if( !p_track->b_ok )
1624     {
1625         return VLC_EGENERIC;
1626     }
1627
1628     if( p_track->b_selected )
1629     {
1630         msg_Warn( p_input,
1631                   "track[Id 0x%x] already selected",
1632                   p_track->i_track_ID );
1633         return VLC_SUCCESS;
1634     }
1635
1636     return MP4_TrackSeek( p_input, p_track, i_start );
1637 }
1638
1639 static void MP4_TrackUnselect(input_thread_t    *p_input,
1640                               mp4_track_t  *p_track )
1641 {
1642     if( !p_track->b_ok )
1643     {
1644         return;
1645     }
1646
1647     if( !p_track->b_selected )
1648     {
1649         msg_Warn( p_input,
1650                   "track[Id 0x%x] already unselected",
1651                   p_track->i_track_ID );
1652         return;
1653     }
1654     if( p_track->p_es )
1655     {
1656         es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_track->p_es, VLC_FALSE );
1657     }
1658
1659     p_track->b_selected = VLC_FALSE;
1660 }
1661
1662 static int  MP4_TrackSeek   ( input_thread_t    *p_input,
1663                               mp4_track_t  *p_track,
1664                               mtime_t           i_start )
1665 {
1666     uint32_t i_chunk;
1667     uint32_t i_sample;
1668
1669     if( !p_track->b_ok )
1670     {
1671         return( VLC_EGENERIC );
1672     }
1673
1674     if( TrackTimeToSampleChunk( p_input,
1675                                 p_track, i_start,
1676                                 &i_chunk, &i_sample ) )
1677     {
1678         msg_Warn( p_input,
1679                   "cannot select track[Id 0x%x]",
1680                   p_track->i_track_ID );
1681         return( VLC_EGENERIC );
1682     }
1683
1684     p_track->b_selected = VLC_TRUE;
1685
1686     if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1687     {
1688         p_track->b_selected = VLC_FALSE;
1689     }
1690     return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1691 }
1692
1693
1694 /*
1695  * 3 types: for audio
1696  * 
1697  */
1698 #define QT_V0_MAX_SAMPLES    1500
1699 static int  MP4_TrackSampleSize( mp4_track_t   *p_track )
1700 {
1701     int i_size;
1702     MP4_Box_data_sample_soun_t *p_soun;
1703
1704     if( p_track->i_sample_size == 0 )
1705     {
1706         /* most simple case */
1707         return( p_track->p_sample_size[p_track->i_sample] );
1708     }
1709     if( p_track->fmt.i_cat != AUDIO_ES )
1710     {
1711         return( p_track->i_sample_size );
1712     }
1713
1714     if( p_track->i_sample_size != 1 )
1715     {
1716         //msg_Warn( p_input, "SampleSize != 1" );
1717         return( p_track->i_sample_size );
1718     }
1719
1720     p_soun = p_track->p_sample->data.p_sample_soun;
1721
1722     if( p_soun->i_qt_version == 1 )
1723     {
1724         i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1725     }
1726     else
1727     {
1728         /* FIXME */
1729         int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1730                 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1731         if( i_samples > QT_V0_MAX_SAMPLES )
1732         {
1733             i_samples = QT_V0_MAX_SAMPLES;
1734         }
1735         i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1736     }
1737
1738     //fprintf( stderr, "size=%d\n", i_size );
1739     return( i_size );
1740 }
1741
1742
1743 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
1744 {
1745     unsigned int i_sample;
1746     uint64_t i_pos;
1747
1748
1749     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1750
1751     if( p_track->i_sample_size )
1752     {
1753         MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1754
1755         if( p_soun->i_qt_version == 0 )
1756         {
1757             i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1758                         p_soun->i_channelcount * p_soun->i_samplesize / 8;
1759         }
1760         else
1761         {
1762             /* we read chunk by chunk */
1763             i_pos += 0;
1764         }
1765     }
1766     else
1767     {
1768         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1769                 i_sample < p_track->i_sample; i_sample++ )
1770         {
1771             i_pos += p_track->p_sample_size[i_sample];
1772         }
1773
1774     }
1775     return( i_pos );
1776 }
1777
1778 static int  MP4_TrackNextSample( input_thread_t     *p_input,
1779                                  mp4_track_t   *p_track )
1780 {
1781
1782     if( p_track->fmt.i_cat == AUDIO_ES &&
1783         p_track->i_sample_size != 0 )
1784     {
1785         MP4_Box_data_sample_soun_t *p_soun;
1786
1787         p_soun = p_track->p_sample->data.p_sample_soun;
1788
1789         if( p_soun->i_qt_version == 1 )
1790         {
1791             /* chunk by chunk */
1792             p_track->i_sample =
1793                 p_track->chunk[p_track->i_chunk].i_sample_first +
1794                 p_track->chunk[p_track->i_chunk].i_sample_count;
1795         }
1796         else
1797         {
1798             /* FIXME */
1799             p_track->i_sample += QT_V0_MAX_SAMPLES;
1800             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 )
1801             {
1802                 p_track->i_sample =
1803                     p_track->chunk[p_track->i_chunk].i_sample_first +
1804                     p_track->chunk[p_track->i_chunk].i_sample_count;
1805             }
1806         }
1807     }
1808     else
1809     {
1810         p_track->i_sample++;
1811     }
1812
1813     if( p_track->i_sample >= p_track->i_sample_count )
1814     {
1815         /* we have reach end of the track so free decoder stuff */
1816         msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1817         MP4_TrackUnselect( p_input, p_track );
1818         return VLC_EGENERIC;
1819     }
1820
1821     /* Have we changed chunk ? */
1822     if( p_track->i_sample >=
1823             p_track->chunk[p_track->i_chunk].i_sample_first +
1824             p_track->chunk[p_track->i_chunk].i_sample_count )
1825     {
1826         if( TrackGotoChunkSample( p_input,
1827                                   p_track,
1828                                   p_track->i_chunk + 1,
1829                                   p_track->i_sample ) )
1830         {
1831             msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1832             MP4_TrackUnselect( p_input, p_track );
1833             return VLC_EGENERIC;
1834         }
1835     }
1836
1837     /* Have we changed elst */
1838     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1839     {
1840         demux_sys_t *p_sys = p_input->p_demux_data;
1841         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1842         int64_t i_mvt = MP4_TrackGetPTS( p_input, p_track ) * p_sys->i_timescale / (int64_t)1000000;
1843
1844         if( p_track->i_elst < elst->i_entry_count &&
1845             i_mvt >= p_track->i_elst_time + elst->i_segment_duration[p_track->i_elst] )
1846         {
1847             MP4_TrackSetELST( p_input, p_track, MP4_TrackGetPTS( p_input, p_track ) );
1848         }
1849     }
1850
1851     return VLC_SUCCESS;
1852 }
1853
1854 static void MP4_TrackSetELST( input_thread_t *p_input, mp4_track_t *tk, int64_t i_time )
1855 {
1856     demux_sys_t *p_sys = p_input->p_demux_data;
1857     int         i_elst_last = tk->i_elst;
1858
1859     /* handle elst (find the correct one) */
1860     tk->i_elst      = 0;
1861     tk->i_elst_time = 0;
1862     if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
1863     {
1864         MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
1865         int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
1866
1867         for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
1868         {
1869             mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
1870
1871             if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
1872             {
1873                 break;
1874             }
1875             tk->i_elst_time += i_dur;
1876         }
1877
1878         if( tk->i_elst >= elst->i_entry_count )
1879         {
1880             /* msg_Dbg( p_input, "invalid number of entry in elst" ); */
1881             tk->i_elst = elst->i_entry_count - 1;
1882             tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
1883         }
1884
1885         if( elst->i_media_time[tk->i_elst] < 0 )
1886         {
1887             /* track offset */
1888             tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
1889         }
1890     }
1891     if( i_elst_last != tk->i_elst )
1892     {
1893         msg_Warn( p_input, "elst old=%d new=%d", i_elst_last, tk->i_elst );
1894     }
1895 }
1896