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