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