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