]> git.sesse.net Git - vlc/blob - modules/demux/mp4/mp4.c
fdab28b9ac7457646e6792bae431ddda68e2f156
[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.15 2003/01/25 17:57:36 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 "codecs.h"
34 #include "libmp4.h"
35 #include "mp4.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int    MP4Init    ( vlc_object_t * );
41 static void __MP4End     ( vlc_object_t * );
42 static int    MP4Demux   ( input_thread_t * );
43
44 /* New input could have something like that... */
45 static int   MP4Seek     ( input_thread_t *, mtime_t );
46
47 #define MP4End(a) __MP4End(VLC_OBJECT(a))
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 vlc_module_begin();
53     set_description( "MP4 demuxer" );
54     set_capability( "demux", 242 );
55     set_callbacks( MP4Init, __MP4End );
56 vlc_module_end();
57
58 /*****************************************************************************
59  * Declaration of local function
60  *****************************************************************************/
61
62 static int MP4_TrackSynchro( input_thread_t *p_input, track_data_mp4_t *p_track );
63
64 static void MP4_ParseTrack();
65
66 static int MP4_CreateChunksIndex();
67 static int MP4_CreateSamplesIndex();
68
69 static void MP4_StartDecoder();
70 static void MP4_StopDecoder();
71
72 static int  MP4_ReadSample();
73 static int  MP4_DecodeSample();
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     
86 /*****************************************************************************
87  * MP4Init: check file and initializes MP4 structures
88  *****************************************************************************/
89 static int MP4Init( vlc_object_t * p_this )
90 {
91     input_thread_t  *p_input = (input_thread_t *)p_this;
92     uint8_t         *p_peek;
93
94     demux_sys_t     *p_demux;
95
96     MP4_Box_t       *p_ftyp;
97
98
99     MP4_Box_t       *p_mvhd;
100     MP4_Box_t       *p_trak;
101
102     unsigned int    i;
103     /* I need to seek */
104     if( !p_input->stream.b_seekable )
105     {
106         msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
107         return( -1 );
108
109     }
110     /* Initialize access plug-in structures. */
111     if( p_input->i_mtu == 0 )
112     {
113         /* Improve speed. */
114         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
115     }
116
117     p_input->pf_demux = MP4Demux;
118
119     /* a little test to see if it could be a mp4 */
120     if( input_Peek( p_input, &p_peek, 8 ) < 8 )
121     {
122         msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
123         return( -1 );
124     }
125
126
127     switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
128     {
129         case( FOURCC_ftyp ):
130         case( FOURCC_moov ):
131         case( FOURCC_moof ):
132         case( FOURCC_mdat ):
133         case( FOURCC_udta ):
134         case( FOURCC_free ):
135         case( FOURCC_skip ):
136         case( FOURCC_wide ):
137             break;
138          default:
139             msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
140             return( -1 );
141     }
142
143     /* create our structure that will contains all data */
144     if( !( p_input->p_demux_data =
145                 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
146     {
147         msg_Err( p_input, "out of memory" );
148         return( -1 );
149     }
150     memset( p_demux, 0, sizeof( demux_sys_t ) );
151     p_input->p_demux_data = p_demux;
152
153
154     /* Now load all boxes ( except raw data ) */
155     if( !MP4_BoxGetRoot( p_input, &p_demux->box_root ) )
156     {
157         msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
158         return( -1 );
159     }
160
161     MP4_BoxDumpStructure( p_input, &p_demux->box_root );
162
163     if( ( p_ftyp = MP4_BoxGet( &p_demux->box_root, "/ftyp" ) ) )
164     {
165         switch( p_ftyp->data.p_ftyp->i_major_brand )
166         {
167             case( FOURCC_isom ):
168                 msg_Info( p_input, 
169                           "ISO Media file (isom) version %d.",
170                           p_ftyp->data.p_ftyp->i_minor_version );
171                 break;
172             default:
173                 msg_Info( p_input,
174                           "Unrecognize major file specification (%c%c%c%c).",
175                            p_ftyp->data.p_ftyp->i_major_brand&0xff,
176                            ( p_ftyp->data.p_ftyp->i_major_brand >>  8)&0xff,
177                            ( p_ftyp->data.p_ftyp->i_major_brand >> 16 )&0xff,
178                            ( p_ftyp->data.p_ftyp->i_major_brand >> 24 )&0xff );
179                 break;
180         }
181     }
182     else
183     {
184         msg_Info( p_input, "File Type box missing(assume ISO Media file)" );
185     }
186
187     /* the file need to have one moov box */
188     if( MP4_BoxCount( &p_demux->box_root, "/moov" ) != 1 )
189     {
190         msg_Err( p_input, 
191                  "MP4 plugin discarded (%d moov box)",
192                  MP4_BoxCount( &p_demux->box_root, "/moov" ) );
193 //        MP4End( p_input );
194 //        return( -1 );
195     }
196
197     if( !(p_mvhd = MP4_BoxGet( &p_demux->box_root, "/moov/mvhd" ) ) )
198     {
199         msg_Err( p_input, "cannot find /moov/mvhd !" );
200         MP4End( p_input );
201         return( -1 );
202     }
203     else
204     {
205         p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
206         p_demux->i_duration = p_mvhd->data.p_mvhd->i_duration;
207     }
208
209     if( !( p_demux->i_tracks = 
210                 MP4_BoxCount( &p_demux->box_root, "/moov/trak" ) ) )
211     {
212         msg_Err( p_input, "cannot find any /moov/trak !" );
213         MP4End( p_input );
214         return( -1 );
215     }
216     msg_Dbg( p_input, "find %d track%c",
217                         p_demux->i_tracks,
218                         p_demux->i_tracks ? 's':' ' );
219
220     /* allocate memory */
221     p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
222
223     /* now process each track and extract all usefull informations */
224     for( i = 0; i < p_demux->i_tracks; i++ )
225     {
226         p_trak = MP4_BoxGet( &p_demux->box_root, "/moov/trak[%d]", i );
227         MP4_ParseTrack( p_input, &p_demux->track[i], p_trak );
228
229         if( p_demux->track[i].b_ok )
230         {
231             char *psz_cat;
232             switch( p_demux->track[i].i_cat )
233             {
234                 case( VIDEO_ES ):
235                     psz_cat = "video";
236                     break;
237                 case( AUDIO_ES ):
238                     psz_cat = "audio";
239                     break;
240                 default:
241                     psz_cat = "unknown";
242                     break;
243             }
244
245             msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %c%c%c",
246                             p_demux->track[i].i_track_ID,
247                             psz_cat,
248                             p_demux->track[i].b_enable ? "enable":"disable",
249                             p_demux->track[i].i_language[0],
250                             p_demux->track[i].i_language[1], 
251                             p_demux->track[i].i_language[2] );
252         }
253         else
254         {
255             msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_demux->track[i].i_track_ID );
256         }
257
258     }
259
260     /*  create one program */
261     vlc_mutex_lock( &p_input->stream.stream_lock );
262     if( input_InitStream( p_input, 0 ) == -1)
263     {
264         vlc_mutex_unlock( &p_input->stream.stream_lock );
265         msg_Err( p_input, "cannot init stream" );
266         MP4End( p_input );
267         return( -1 );
268     }
269     if( input_AddProgram( p_input, 0, 0) == NULL )
270     {
271         vlc_mutex_unlock( &p_input->stream.stream_lock );
272         msg_Err( p_input, "cannot add program" );
273         MP4End( p_input );
274         return( -1 );
275     }
276     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
277     /* XXX beurk and beurk, see MP4Demux and MP4Seek */
278     if( p_demux->i_duration/p_demux->i_timescale > 0 )
279     {
280         p_input->stream.i_mux_rate =
281             p_input->stream.p_selected_area->i_size / 50 /
282             ( p_demux->i_duration / p_demux->i_timescale );
283     }
284     else
285     {
286         p_input->stream.i_mux_rate = 0;
287     }
288     vlc_mutex_unlock( &p_input->stream.stream_lock );
289
290
291     for( i = 0; i < p_demux->i_tracks; i++ )
292     {
293         /* start decoder for this track if enable by default*/
294         if( p_demux->track[i].b_enable )
295         {
296             MP4_StartDecoder( p_input, &p_demux->track[i] );
297         }
298     }
299
300     vlc_mutex_lock( &p_input->stream.stream_lock );
301     p_input->stream.p_selected_program->b_is_ok = 1;
302     vlc_mutex_unlock( &p_input->stream.stream_lock );
303
304     return( 0 );
305
306 }
307
308 /*****************************************************************************
309  * MP4Demux: read packet and send them to decoders
310  *****************************************************************************
311  * TODO check for newly selected track (ie audio upt to now )
312  *****************************************************************************/
313 static int MP4Demux( input_thread_t *p_input )
314 {
315     demux_sys_t *p_demux = p_input->p_demux_data;
316     unsigned int i_track;
317
318     /* XXX beurk, beuRK and BEURK, 
319        but only way I've found to detect seek from interface */
320     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
321     {
322         mtime_t i_date;
323         
324         /* first wait for empty buffer, arbitrary time FIXME */
325         msleep( DEFAULT_PTS_DELAY );
326         /* *** calculate new date *** */
327
328         i_date = (mtime_t)1000000 * 
329                  (mtime_t)p_demux->i_duration /
330                  (mtime_t)p_demux->i_timescale * 
331                  (mtime_t)MP4_TellAbsolute( p_input ) /
332                  (mtime_t)p_input->stream.p_selected_area->i_size;
333         MP4Seek( p_input, i_date );
334     }
335     
336     /* first wait for the good time to read a packet */
337     input_ClockManageRef( p_input,
338                           p_input->stream.p_selected_program,
339                           p_demux->i_pcr );
340
341
342     /* update pcr XXX in mpeg scale so in 90000 unit/s */
343     p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
344     
345
346     /* we will read 100ms for each stream so ...*/
347     p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
348     
349
350     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
351     {
352         if( ( !p_demux->track[i_track].b_ok )||
353             ( !p_demux->track[i_track].p_es )||
354             ( !p_demux->track[i_track].p_es->p_decoder_fifo )||
355             ( MP4_GetTrackPTS( &p_demux->track[i_track] ) >=
356                         MP4_GetMoviePTS( p_demux ) ) )
357         {
358             continue; /* no need to read something */
359         }
360         while( MP4_GetTrackPTS( &p_demux->track[i_track] ) <
361                         MP4_GetMoviePTS( p_demux ) )
362         {
363
364             pes_packet_t *p_pes;
365
366             /* read a sample */
367             if( !MP4_ReadSample( p_input ,
368                                  &p_demux->track[i_track],
369                                  &p_pes ) )
370             {
371                 break;
372             }
373
374             /* send it to decoder and update time of this track 
375                  it also launch a new decoder if needed */
376             MP4_DecodeSample( p_input ,
377                               &p_demux->track[i_track],
378                               p_pes );
379         }
380
381     }
382     
383     /* now check if all tracks are finished or unhandled*/
384     
385     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
386     {
387         if( ( p_demux->track[i_track].b_ok )&&
388             ( p_demux->track[i_track].i_sample < p_demux->track[i_track].i_sample_count )&&
389             ( p_demux->track[i_track].p_es )&&
390             ( p_demux->track[i_track].p_es->p_decoder_fifo ) )
391         {
392             return( 1 );
393         }
394     }
395
396     return( 0 ); /* EOF */
397 }
398 /*****************************************************************************
399  * MP4Seek: Got to i_date
400  ******************************************************************************/
401 static int   MP4Seek     ( input_thread_t *p_input, mtime_t i_date )
402 {
403     demux_sys_t *p_demux = p_input->p_demux_data;
404     unsigned int i_track;
405
406     /* First update update global time */
407     p_demux->i_time = i_date * p_demux->i_timescale / 1000000;
408     p_demux->i_pcr = __MAX( MP4_GetMoviePTS( p_demux ) - DEFAULT_PTS_DELAY,
409                             0 ) * 9 / 100; 
410     
411     /* Now for each stream try to go to this time */
412     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
413     {
414         MP4_TrackSynchro( p_input, &p_demux->track[i_track] );
415     }
416     return( 1 );
417 }
418
419 /*****************************************************************************
420  * MP4End: frees unused data
421  *****************************************************************************/
422 static void __MP4End ( vlc_object_t * p_this )
423 {
424 #define FREE( p ) \
425     if( p ) { free( p ); }
426     unsigned int i_track;
427     input_thread_t *  p_input = (input_thread_t *)p_this;
428     demux_sys_t *p_demux = p_input->p_demux_data;
429
430     msg_Dbg( p_input, "Freeing all memory" );
431     MP4_BoxFree( p_input, &p_demux->box_root );
432     for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
433     {
434         unsigned int i_chunk;
435         for( i_chunk = 0;
436                 i_chunk < p_demux->track[i_track].i_chunk_count; i_chunk++ )
437         {
438             if( p_demux->track[i_track].chunk )
439             {
440                FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_count_dts);
441                FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_delta_dts );
442             }
443         }
444         FREE( p_demux->track[i_track].chunk );
445
446         if( !p_demux->track[i_track].i_sample_size )
447         {
448             FREE( p_demux->track[i_track].p_sample_size );
449         }
450     }
451     FREE( p_demux->track );
452
453     FREE( p_input->p_demux_data );
454 #undef FREE
455 }
456
457
458 /****************************************************************************
459  * Local functions, specific to vlc
460  ****************************************************************************/
461
462 /****************************************************************************
463  * MP4_TrackSynchro : synchronize a track with movie time after seek or 
464  *                    for newly selected track
465  *****************************************************************************
466  * TODO add support of Edit List (edts/elts) and Shadow Sync Sample(stsh)
467  ****************************************************************************/
468 static int MP4_TrackSynchro( input_thread_t *p_input, track_data_mp4_t *p_track )
469 {
470     demux_sys_t *p_demux = p_input->p_demux_data;
471     unsigned int i_chunk_last;
472     MP4_Box_t   *p_stss;
473
474     if( !p_track->b_ok ||
475         !p_track->p_es ||
476         !p_track->p_es->p_decoder_fifo )
477     {
478         return( 0 );
479     }
480     p_track->i_sample = 0;
481     i_chunk_last = p_track->i_chunk;
482     p_track->i_chunk = 0;
483     for( ;; )
484     {
485         if( p_track->i_sample >= p_track->i_sample_count )
486         {
487             msg_Warn( p_input, 
488                         "track[Id 0x%x] will be disabled (seeking too far)",
489                         p_track->i_track_ID );
490             MP4_StopDecoder( p_input, p_track );
491             break;
492         }
493         if( MP4_GetTrackPTS( p_track ) >= MP4_GetMoviePTS( p_demux ) )
494         {
495             break;
496         }
497         /* go one sample after */
498         p_track->i_sample++;
499         if( p_track->i_sample >= p_track->chunk[p_track->i_chunk].i_sample_first +
500                 p_track->chunk[p_track->i_chunk].i_sample_count )
501         {
502             p_track->i_chunk++;
503         }
504         
505     }
506     if( p_track->i_sample >= p_track->i_sample_count )
507     {
508         return( 0 );
509     }
510
511     /* *** Try to find nearest sync points *** */
512     if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
513     {
514         unsigned int i_index;
515         msg_Dbg( p_input,
516                     "track[Id 0x%x] using Sync Sample Box (stss)",
517                     p_track->i_track_ID );
518         for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
519         {
520             if( p_stss->data.p_stss->i_sample_number[i_index] >= p_track->i_sample )
521             {
522                 if( i_index > 0 )
523                 {
524                     msg_Dbg( p_input, "stts gives %d --> %d (sample number)", 
525                             p_track->i_sample, 
526                             p_stss->data.p_stss->i_sample_number[i_index-1] );
527                     p_track->i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
528                     /* new track.i_sample is less than old so i_chunk can only decreased */
529                     while( p_track->i_chunk > 0 && 
530                             p_track->i_sample < p_track->chunk[p_track->i_chunk].i_sample_first )
531                     {
532                         p_track->i_chunk--;
533                     }
534                 }
535                 else
536                 {
537                     msg_Dbg( p_input, "stts gives %d --> %d (sample number)", 
538                             p_track->i_sample, 
539                             p_stss->data.p_stss->i_sample_number[i_index-1] );
540                     p_track->i_sample = p_stss->data.p_stss->i_sample_number[i_index];
541                     /* new track.i_sample is more than old so i_chunk can only increased */
542                     while( p_track->i_chunk < p_track->i_chunk_count - 1 &&
543                            p_track->i_sample >= p_track->chunk[p_track->i_chunk].i_sample_first +
544                                                 p_track->chunk[p_track->i_chunk].i_sample_count )
545                     {
546                         p_track->i_chunk++;
547                     }
548                 }
549                 break;
550             }
551         }
552     }
553     else
554     {
555         msg_Dbg( p_input, 
556                     "track[Id 0x%x] doesn't provided Sync Sample Box (stss)", 
557                     p_track->i_track_ID );
558     }
559
560     /* *** If i_sample_description_index has changed restart decoder *** */
561     if( p_track->chunk[i_chunk_last].i_sample_description_index !=
562             p_track->chunk[p_track->i_chunk].i_sample_description_index )
563     {
564         msg_Warn( p_input,
565                 "SampleEntry has changed, restarting decoder" );
566         MP4_StopDecoder( p_input, p_track );
567         MP4_StartDecoder( p_input, p_track );
568     }
569     return( 1 );
570 }
571
572 /****************************************************************************
573  * Parse track information and create all needed data to run a track
574  * If it succeed b_ok is set to 1 else to 0
575  ****************************************************************************/
576 static void MP4_ParseTrack( input_thread_t *p_input,
577                      track_data_mp4_t *p_demux_track,
578                      MP4_Box_t  * p_trak )
579 {
580     unsigned int i;
581
582     MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
583     MP4_Box_t *p_tref = MP4_BoxGet( p_trak, "tref" );
584     MP4_Box_t *p_elst;
585
586     MP4_Box_t *p_mdhd;
587     MP4_Box_t *p_hdlr;
588
589     MP4_Box_t *p_vmhd;
590     MP4_Box_t *p_smhd; 
591
592     /* hint track unsuported */
593
594     /* by default, track isn't usable */
595     p_demux_track->b_ok = 0;
596
597     /* by default, we don't known the categorie */
598     p_demux_track->i_cat = UNKNOWN_ES;
599     
600     if( !p_tkhd )
601     {
602         return;
603     }
604
605     /* do we launch this track by default ? */
606     p_demux_track->b_enable = 
607         ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
608
609     p_demux_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
610     p_demux_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
611     p_demux_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
612     
613     if( ( p_elst = MP4_BoxGet( p_trak, "edts/elst" ) ) )
614     {
615 /*        msg_Warn( p_input, "Unhandled box: edts --> FIXME" ); */
616     }
617
618     if( p_tref )
619     {
620 /*        msg_Warn( p_input, "Unhandled box: tref --> FIXME" ); */
621     } 
622
623     p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
624     p_hdlr = MP4_BoxGet( p_trak, "mdia/hdlr" );
625     
626     if( ( !p_mdhd )||( !p_hdlr ) )
627     {
628         return;
629     }
630
631     p_demux_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
632
633     for( i = 0; i < 3; i++ ) 
634     {
635         p_demux_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
636     }
637     p_mdhd->data.p_mdhd->i_language[3] = 0;
638     
639     switch( p_hdlr->data.p_hdlr->i_handler_type )
640     {
641         case( FOURCC_soun ):
642             if( !( p_smhd = MP4_BoxGet( p_trak, "mdia/minf/smhd" ) ) )
643             {
644                 return;
645             }
646             p_demux_track->i_cat = AUDIO_ES;
647             break;
648
649         case( FOURCC_vide ):
650             if( !( p_vmhd = MP4_BoxGet( p_trak, "mdia/minf/vmhd" ) ) )
651             {
652                 return;
653             }
654             p_demux_track->i_cat = VIDEO_ES;
655             break;
656             
657         default:
658             return;
659     }
660 /*  TODO
661     add support for:
662     p_dinf = MP4_BoxGet( p_minf, "dinf" );
663 */
664     if( !( p_demux_track->p_stbl = MP4_BoxGet( p_trak,"mdia/minf/stbl" ) ) )
665     {
666         return;
667     }
668     
669     if( !( p_demux_track->p_stsd = MP4_BoxGet( p_trak,"mdia/minf/stbl/stsd") ) )
670     {
671         return;
672     }
673     
674     /* Create chunk  index table */
675     if( !MP4_CreateChunksIndex( p_input,p_demux_track  ) )
676     {
677         return; /* cannot create chunks index */
678     }
679     
680     /* create sample index table needed for reading and seeking */
681     if( !MP4_CreateSamplesIndex( p_input, p_demux_track ) )
682     {
683         return; /* cannot create samples index */
684     }
685      
686     p_demux_track->b_ok = 1;        
687 }
688                      
689
690
691 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
692 static int MP4_CreateChunksIndex( input_thread_t *p_input,
693                                    track_data_mp4_t *p_demux_track )
694 {
695     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
696     MP4_Box_t *p_stsc;
697
698     unsigned int i_chunk;
699     unsigned int i_index, i_last;
700    
701
702     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
703           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )|| 
704         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
705     {
706         return( 0 );
707     }
708      
709     p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
710     if( !p_demux_track->i_chunk_count )
711     {
712         msg_Warn( p_input, "No chunk defined" );
713         return( 0 );
714     }
715     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count, 
716                                    sizeof( chunk_data_mp4_t ) );
717
718     /* first we read chunk offset */
719     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
720     {
721         p_demux_track->chunk[i_chunk].i_offset = 
722                 p_co64->data.p_co64->i_chunk_offset[i_chunk];
723     }
724
725     /* now we read index for SampleEntry( soun vide mp4a mp4v ...) 
726         to be used for the sample XXX begin to 1 
727         We construct it begining at the end */
728     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
729     i_index = p_stsc->data.p_stsc->i_entry_count;
730     if( !i_index )
731     {
732         msg_Warn( p_input, "cannot read chunk table or table empty" );
733         return( 0 );
734     }
735
736     while( i_index )
737     {
738         i_index--;
739         for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
740                 i_chunk < i_last; i_chunk++ )
741         {
742             p_demux_track->chunk[i_chunk].i_sample_description_index = 
743                     p_stsc->data.p_stsc->i_sample_description_index[i_index];
744             p_demux_track->chunk[i_chunk].i_sample_count =
745                     p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
746         }
747         i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
748     }
749
750     p_demux_track->chunk[i_chunk].i_sample_first = 0;
751     for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
752     {
753         p_demux_track->chunk[i_chunk].i_sample_first =
754             p_demux_track->chunk[i_chunk-1].i_sample_first + 
755                 p_demux_track->chunk[i_chunk-1].i_sample_count;
756         
757     }
758     
759     msg_Dbg( p_input, 
760              "track[Id 0x%x] read %d chunk",
761              p_demux_track->i_track_ID,
762             p_demux_track->i_chunk_count );
763     return( 1 );
764
765 }
766
767
768
769 static int MP4_CreateSamplesIndex( input_thread_t *p_input,
770                                    track_data_mp4_t *p_demux_track )
771 {
772     MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
773                           ctts make same mapping but for composition time, 
774                           not yet used and probably not usefull */
775     MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2 
776                           that uses a compressed form FIXME make them in libmp4 
777                           as a unique type */
778     /* TODO use also stss and stsh table for seeking */
779     /* FIXME use edit table */
780     int64_t i_sample;
781     int64_t i_chunk;
782
783     int64_t i_index;
784     int64_t i_index_sample_used;
785
786     int64_t i_last_dts; 
787     
788     p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
789     p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
790
791     
792     if( ( !p_stts )||( !p_stsz ) )
793     {
794         msg_Warn( p_input, "cannot read sample table" );
795         return( 0 ); 
796     }
797         
798     p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
799
800
801     /* for sample size, there are 2 case */
802     if( p_stsz->data.p_stsz->i_sample_size )
803     {
804         /* 1: all sample have the same size, so no need to construct a table */
805         p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
806         p_demux_track->p_sample_size = NULL;
807     }
808     else
809     {
810         /* 2: each sample can have a different size */
811         p_demux_track->i_sample_size = 0;
812         p_demux_track->p_sample_size = 
813             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
814         
815         for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
816         {
817             p_demux_track->p_sample_size[i_sample] = 
818                     p_stsz->data.p_stsz->i_entry_size[i_sample];
819         }
820     }
821     /* we have extract all information from stsz,
822         now use stts */
823
824     /* if we don't want to waste too much memory, we can't expand
825        the box !, so each chunk will contain an "extract" of this table 
826        for fast research */
827         
828     i_last_dts = 0;
829     i_index = 0; i_index_sample_used =0;
830
831     /* create and init last data for each chunk */
832     for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
833     {
834
835         int64_t i_entry, i_sample_count, i;
836         /* save last dts */
837         p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
838     /* count how many entries needed for this chunk 
839        for p_sample_delta_dts and p_sample_count_dts */
840
841         i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
842
843         i_entry = 0;
844         while( i_sample_count > 0 )
845         {
846             i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
847             if( i_entry == 0 )
848             {
849                 i_sample_count += i_index_sample_used; /* don't count already used sample 
850                                                    int this entry */
851             }
852             i_entry++;
853         }
854
855         /* allocate them */
856         p_demux_track->chunk[i_chunk].p_sample_count_dts = 
857             calloc( i_entry, sizeof( uint32_t ) );
858         p_demux_track->chunk[i_chunk].p_sample_delta_dts =
859             calloc( i_entry, sizeof( uint32_t ) );
860
861         /* now copy */
862         i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
863         for( i = 0; i < i_entry; i++ )
864         {
865             int64_t i_used;
866             int64_t i_rest;
867             
868             i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
869
870             i_used = __MIN( i_rest, i_sample_count );
871
872             i_index_sample_used += i_used;
873             i_sample_count -= i_used;
874
875             p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
876
877             p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
878                         p_stts->data.p_stts->i_sample_delta[i_index];
879             
880             i_last_dts += i_used * 
881                     p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
882
883             if( i_index_sample_used >=
884                              p_stts->data.p_stts->i_sample_count[i_index] )
885             {
886
887                 i_index++;
888                 i_index_sample_used = 0;
889             }
890         }
891         
892     }
893
894     msg_Dbg( p_input, 
895              "track[Id 0x%x] read %d samples length:"I64Fd"s", 
896              p_demux_track->i_track_ID,
897              p_demux_track->i_sample_count,
898              i_last_dts / p_demux_track->i_timescale );
899
900     return( 1 );
901 }
902
903 static void MP4_StartDecoder( input_thread_t *p_input,
904                                  track_data_mp4_t *p_demux_track )
905 {
906     MP4_Box_t *  p_sample;
907     unsigned int i;
908     unsigned int i_chunk;
909
910     unsigned int i_decoder_specific_info_len;
911     uint8_t *    p_decoder_specific_info;
912     pes_packet_t *p_pes_init;
913
914     uint8_t             *p_init;
915     BITMAPINFOHEADER    *p_bih;
916     WAVEFORMATEX        *p_wf;
917
918     MP4_Box_t   *p_esds;
919
920
921     if( (!p_demux_track->b_ok )||( p_demux_track->i_cat == UNKNOWN_ES ) )
922     {
923         return;
924     }
925
926     msg_Dbg( p_input, "Starting decoder for track[Id 0x%x]",
927                       p_demux_track->i_track_ID );
928
929     /* launch decoder according in chunk we are */
930     i_chunk = p_demux_track->i_chunk;
931
932     if( !p_demux_track->chunk[i_chunk].i_sample_description_index )
933     {
934         msg_Warn( p_input, 
935                   "invalid SampleEntry index (track[Id 0x%x])",
936                   p_demux_track->i_track_ID );
937         return;
938     } 
939     p_sample = MP4_BoxGet(  p_demux_track->p_stsd, 
940                             "[%d]", 
941                 p_demux_track->chunk[i_chunk].i_sample_description_index - 1 );
942
943     if( ( !p_sample )||( !p_sample->data.p_data ) )
944     {
945         msg_Warn( p_input, 
946                   "cannot find SampleEntry (track[Id 0x%x])",
947                   p_demux_track->i_track_ID );
948         return;
949     }
950
951     vlc_mutex_lock( &p_input->stream.stream_lock );
952     p_demux_track->p_es = input_AddES( p_input,
953                                        p_input->stream.p_selected_program, 
954                                        p_demux_track->i_track_ID,
955                                        0 );
956     vlc_mutex_unlock( &p_input->stream.stream_lock );
957     /* Initialise ES, first language as description */
958     for( i = 0; i < 3; i++ )
959     {
960         p_demux_track->p_es->psz_desc[i] = p_demux_track->i_language[i];
961     }
962     p_demux_track->p_es->psz_desc[3] = '\0';
963
964     p_demux_track->p_es->i_stream_id = p_demux_track->i_track_ID;
965
966     /* It's a little ugly but .. there are special cases */
967     switch( p_sample->i_type )
968     {
969         case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
970         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
971             p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
972             break;
973         default:
974             p_demux_track->p_es->i_fourcc = p_sample->i_type;
975             break;
976     }
977
978     p_demux_track->p_es->i_cat = p_demux_track->i_cat;
979
980     i_decoder_specific_info_len = 0;
981     p_decoder_specific_info = NULL;
982     p_pes_init = NULL;
983
984     /* now see if esds is present and if so create a data packet 
985         with decoder_specific_info  */
986 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
987     if( ( p_esds = MP4_BoxGet( p_sample, "esds" ) )&&
988         ( p_esds->data.p_esds )&&
989         ( p_decconfig ) )
990     {
991         /* First update information based on i_objectTypeIndication */
992         switch( p_decconfig->i_objectTypeIndication )
993         {
994             case( 0x20 ): /* MPEG4 VIDEO */
995                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
996                 break;
997             case( 0x40):
998                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
999                 break;
1000             case( 0x60):
1001             case( 0x61):
1002             case( 0x62):
1003             case( 0x63):
1004             case( 0x64):
1005             case( 0x65): /* MPEG2 video */
1006                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1007                 break;
1008             /* Theses are MPEG2-AAC */
1009             case( 0x66): /* main profile */
1010             case( 0x67): /* Low complexity profile */
1011             case( 0x68): /* Scaleable Sampling rate profile */
1012                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1013                 break;
1014             /* true MPEG 2 audio */
1015             case( 0x69): 
1016                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1017                 break;
1018             case( 0x6a): /* MPEG1 video */
1019                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1020                 break;
1021             case( 0x6b): /* MPEG1 audio */
1022                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1023                 break;
1024             case( 0x6c ): /* jpeg */
1025                 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
1026                 break;
1027             default:
1028                 /* Unknown entry, but don't touch i_fourcc */
1029                 msg_Warn( p_input, 
1030                           "objectTypeIndication(0x%x) unknow (Track[ID 0x%x])",
1031                           p_decconfig->i_objectTypeIndication,
1032                           p_demux_track->i_track_ID );
1033                 break;
1034         }
1035         i_decoder_specific_info_len = 
1036                 p_decconfig->i_decoder_specific_info_len;
1037         p_decoder_specific_info = 
1038                 p_decconfig->p_decoder_specific_info;
1039     }
1040
1041 #undef p_decconfig
1042
1043     /* some last initialisation */
1044     /* XXX I create a bitmapinfoheader_t or 
1045        waveformatex_t for each stream, up to now it's the best thing 
1046        I've found but it could exist a better solution :) as something 
1047        like adding some new fields in p_es ...
1048
1049        XXX I don't set all values, only thoses that are interesting or known
1050         --> bitmapinfoheader_t : width and height 
1051         --> waveformatex_t : channels, samplerate, bitspersample
1052         and at the end I add p_decoder_specific_info 
1053
1054         TODO set more values
1055
1056      */
1057
1058     switch( p_demux_track->i_cat )
1059     {
1060         case( VIDEO_ES ):    
1061             /* now create a bitmapinfoheader_t for decoder and 
1062                add information found in p_esds */
1063             /* XXX XXX + 16 are for avoid segfault when ffmpeg access beyong the data */
1064             p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len + 16 );
1065             p_bih = (BITMAPINFOHEADER*)p_init;
1066
1067             p_bih->biSize     = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
1068             p_bih->biWidth    = p_sample->data.p_sample_vide->i_width;
1069             p_bih->biHeight   = p_sample->data.p_sample_vide->i_height;
1070             p_bih->biPlanes   = 1;      // FIXME
1071             p_bih->biBitCount = 0;      // FIXME
1072             p_bih->biCompression   = 0; // FIXME
1073             p_bih->biSizeImage     = 0; // FIXME
1074             p_bih->biXPelsPerMeter = 0; // FIXME
1075             p_bih->biYPelsPerMeter = 0; // FIXME
1076             p_bih->biClrUsed       = 0; // FIXME
1077             p_bih->biClrImportant  = 0; // FIXME
1078
1079             if( p_bih->biWidth == 0 )
1080             {
1081                 // fall on display size
1082                 p_bih->biWidth = p_demux_track->i_width;
1083             }
1084             if( p_bih->biHeight == 0 )
1085             {
1086                 // fall on display size
1087                 p_bih->biHeight = p_demux_track->i_height;
1088             }
1089
1090             if( i_decoder_specific_info_len )
1091             {
1092                 data_packet_t   *p_data;
1093
1094                 memcpy( p_init + sizeof( BITMAPINFOHEADER ), 
1095                         p_decoder_specific_info,
1096                         i_decoder_specific_info_len);
1097
1098                 /* If stream is mpeg4 video we send specific_info,
1099                    as it's needed to decode it (vol) */
1100                 switch( p_demux_track->p_es->i_fourcc )
1101                 {
1102                     case VLC_FOURCC( 'm','p','4','v' ):
1103                     case VLC_FOURCC( 'D','I','V','X' ):
1104                     case VLC_FOURCC( 'd','i','v','x' ):
1105                         p_pes_init = input_NewPES( p_input->p_method_data );
1106                         p_data = input_NewPacket( p_input->p_method_data,
1107                                                   i_decoder_specific_info_len);
1108                         memcpy( p_data->p_payload_start,
1109                                 p_decoder_specific_info,
1110                                 i_decoder_specific_info_len );
1111                         p_pes_init->i_dts = p_pes_init->i_pts = 0;
1112                         p_pes_init->p_first = p_pes_init->p_last = p_data;
1113                         p_pes_init->i_nb_data = 1;
1114                         p_pes_init->i_pes_size = i_decoder_specific_info_len;
1115                         break;
1116                     default:
1117                         break;
1118                 }
1119
1120             }
1121             break;
1122
1123         case( AUDIO_ES ):
1124             p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len + 16 );
1125             p_wf = (WAVEFORMATEX*)p_init;
1126
1127             p_wf->wFormatTag = 0;
1128             p_wf->nChannels = p_sample->data.p_sample_soun->i_channelcount;
1129             p_wf->nSamplesPerSec = p_sample->data.p_sample_soun->i_sampleratehi;
1130             p_wf->nAvgBytesPerSec = p_sample->data.p_sample_soun->i_channelcount *
1131                                     p_sample->data.p_sample_soun->i_sampleratehi *
1132                                     p_sample->data.p_sample_soun->i_samplesize / 8;
1133             p_wf->nBlockAlign = 0;
1134             p_wf->wBitsPerSample = p_sample->data.p_sample_soun->i_samplesize;
1135             p_wf->cbSize = i_decoder_specific_info_len;
1136
1137             if( i_decoder_specific_info_len )
1138             {
1139                 memcpy( p_init + sizeof( WAVEFORMATEX ),
1140                         p_decoder_specific_info,
1141                         i_decoder_specific_info_len);
1142             }
1143
1144             break;
1145
1146         default:
1147             p_init = NULL;
1148             break;
1149     }
1150     if( p_demux_track->i_cat == AUDIO_ES )
1151     {
1152         p_demux_track->p_es->p_waveformatex = (void*)p_init;
1153     }
1154     else if( p_demux_track->i_cat == VIDEO_ES )
1155     {
1156         p_demux_track->p_es->p_bitmapinfoheader = (void*)p_init;
1157     }
1158     vlc_mutex_lock( &p_input->stream.stream_lock );
1159     input_SelectES( p_input, p_demux_track->p_es );
1160     vlc_mutex_unlock( &p_input->stream.stream_lock );
1161
1162     if( !p_demux_track->p_es->p_decoder_fifo )
1163     {
1164         msg_Warn( p_input, "cannot start decoder" );
1165         if( p_pes_init )
1166         {
1167             input_DeletePES( p_input->p_method_data, p_pes_init );
1168         }
1169         return;
1170     }
1171
1172     if( p_pes_init != NULL )
1173     {
1174         input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes_init );
1175     }
1176     p_demux_track->b_ok = 1;
1177     p_demux_track->b_selected = 1;
1178 }
1179
1180 static void MP4_StopDecoder( input_thread_t *p_input,
1181                              track_data_mp4_t *p_demux_track )
1182 {
1183     msg_Dbg( p_input, "Stopping decoder (track[Id 0x%x])",
1184                       p_demux_track->i_track_ID );
1185
1186     input_UnselectES( p_input, p_demux_track->p_es );
1187     p_demux_track->p_es = NULL;
1188
1189     p_demux_track->b_selected = 0;
1190 }
1191
1192 static int  MP4_ReadSample( input_thread_t *p_input,
1193                             track_data_mp4_t *p_demux_track,
1194                             pes_packet_t **pp_pes )
1195 {
1196     size_t i_size;
1197     off_t i_pos;
1198
1199     data_packet_t *p_data;
1200
1201
1202     /* this track have already reach the end */
1203     if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1204     {
1205         *pp_pes = NULL;
1206         return( 0 );
1207     }
1208     /* caculate size and position for this sample */
1209     i_size = p_demux_track->i_sample_size ? 
1210                     p_demux_track->i_sample_size : 
1211                     p_demux_track->p_sample_size[p_demux_track->i_sample];
1212
1213     i_pos  = MP4_GetTrackPos( p_demux_track );
1214
1215     /* go,go go ! */
1216     if( ! MP4_SeekAbsolute( p_input, i_pos ) )
1217     {
1218         return( 0 );
1219     }
1220
1221     /* now create a pes */
1222     if( !(*pp_pes = input_NewPES( p_input->p_method_data ) ) )
1223     {
1224         return( 0 );
1225     }
1226     /* and a data packet for the data */
1227     if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
1228     {
1229         input_DeletePES( p_input->p_method_data, *pp_pes );
1230         *pp_pes = NULL;
1231         return( 0 );
1232     }
1233     
1234     /* initialisation of all the field */
1235     (*pp_pes)->i_dts =
1236         (*pp_pes)->i_pts = MP4_GetTrackPTS( p_demux_track );
1237     (*pp_pes)->p_first = (*pp_pes)->p_last  = p_data;
1238     (*pp_pes)->i_nb_data = 1;
1239     (*pp_pes)->i_pes_size = i_size;
1240
1241     if( !i_size )    
1242     {
1243         return( 1 );
1244     }
1245     
1246 /*    msg_Dbg( p_input, "will read %d bytes", i_size ); */
1247     if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
1248     {
1249         input_DeletePES( p_input->p_method_data, *pp_pes );
1250         return( 0 );
1251     }
1252
1253         return( 1 );
1254 }
1255
1256
1257 static int  MP4_DecodeSample( input_thread_t *p_input,
1258                               track_data_mp4_t *p_demux_track,
1259                               pes_packet_t *p_pes )
1260 {
1261
1262     if( !p_pes )
1263     {
1264         return( 0 );
1265     }
1266
1267     /* don't forget to convert in mpeg clock */
1268     /* FIXME correct ffmpeg to use dts instead of pts that it incorrect 
1269        and, set it here ( and correct avi demux ) */
1270     p_pes->i_dts =
1271         p_pes->i_pts = input_ClockGetTS( p_input,
1272                                          p_input->stream.p_selected_program,
1273                                          p_pes->i_pts * 9/100);
1274
1275     
1276     input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes );
1277     
1278     /* now update sample position */
1279     p_demux_track->i_sample++; /* easy ;) */
1280     if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1281     {
1282         /* we have reach end of the track so free decoder stuff */
1283         MP4_StopDecoder( p_input, p_demux_track );
1284         return( 1 );
1285     }
1286     /* Have we changed chunk ? */
1287     if( p_demux_track->i_sample >=
1288             p_demux_track->chunk[p_demux_track->i_chunk].i_sample_first +
1289                 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_count )
1290     {
1291         /* we haven't reached the end of the track, so see if we 
1292            have to change the decoder for the next frame because 
1293            i_sample_description_index has changed */
1294
1295         p_demux_track->i_chunk++;
1296         if( p_demux_track->chunk[p_demux_track->i_chunk-1].i_sample_description_index 
1297               != p_demux_track->chunk[p_demux_track->i_chunk].i_sample_description_index  )
1298         {
1299             /* FIXME */
1300             msg_Warn( p_input, 
1301                       "SampleEntry has changed, starting a new decoder" );
1302             MP4_StopDecoder( p_input, p_demux_track );
1303             MP4_StartDecoder( p_input, p_demux_track );
1304         }
1305     }
1306
1307     
1308     return( 1 );
1309 }
1310
1311
1312