]> git.sesse.net Git - vlc/blob - modules/demux/mp4/libmp4.c
63d7db4c0d1eaad8620740dc554cd277ed104e3f
[vlc] / modules / demux / mp4 / libmp4.c
1 /*****************************************************************************
2  * libmp4.c : LibMP4 library for mp4 module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
5  *
6  * Author: Laurent Aimar <fenrir@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <vlc_stream.h>                               /* stream_Peek*/
29
30 #ifdef HAVE_ZLIB_H
31 #   include <zlib.h>                                  /* for compressed moov */
32 #endif
33
34 #include "libmp4.h"
35 #include "languages.h"
36 #include <math.h>
37
38 /* Some assumptions:
39  * The input method HAS to be seekable
40  */
41
42 /* convert 16.16 fixed point to floating point */
43 static double conv_fx( int32_t fx ) {
44     double fp = fx;
45     fp /= 65536.;
46     return fp;
47 }
48
49 /* some functions for mp4 encoding of variables */
50 #ifdef MP4_VERBOSE
51 static void MP4_ConvertDate2Str( char *psz, uint64_t i_date, bool b_relative )
52 {
53     int i_day;
54     int i_hour;
55     int i_min;
56     int i_sec;
57
58     /* date begin at 1 jan 1904 */
59     if ( !b_relative )
60         i_date += ((INT64_C(1904) * 365) + 17) * 24 * 60 * 60;
61
62     i_day = i_date / ( 60*60*24);
63     i_hour = ( i_date /( 60*60 ) ) % 60;
64     i_min  = ( i_date / 60 ) % 60;
65     i_sec =  i_date % 60;
66     sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds", i_day, i_hour, i_min, i_sec );
67 }
68 #endif
69
70 /*****************************************************************************
71  * Some prototypes.
72  *****************************************************************************/
73 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
74
75
76 /*****************************************************************************
77  * MP4_ReadBoxCommon : Load only common parameters for all boxes
78  *****************************************************************************
79  * p_box need to be an already allocated MP4_Box_t, and all data
80  *  will only be peek not read
81  *
82  * RETURN : 0 if it fail, 1 otherwise
83  *****************************************************************************/
84 int MP4_ReadBoxCommon( stream_t *p_stream, MP4_Box_t *p_box )
85 {
86     int      i_read;
87     const uint8_t  *p_peek;
88
89     if( ( ( i_read = stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
90     {
91         return 0;
92     }
93     p_box->i_pos = stream_Tell( p_stream );
94
95     p_box->data.p_payload = NULL;
96     p_box->p_father = NULL;
97     p_box->p_first  = NULL;
98     p_box->p_last  = NULL;
99     p_box->p_next   = NULL;
100
101     MP4_GET4BYTES( p_box->i_shortsize );
102     MP4_GETFOURCC( p_box->i_type );
103
104     /* Now special case */
105
106     if( p_box->i_shortsize == 1 )
107     {
108         /* get the true size on 64 bits */
109         MP4_GET8BYTES( p_box->i_size );
110     }
111     else
112     {
113         p_box->i_size = p_box->i_shortsize;
114         /* XXX size of 0 means that the box extends to end of file */
115     }
116
117     if( p_box->i_type == ATOM_uuid )
118     {
119         /* get extented type on 16 bytes */
120         GetUUID( &p_box->i_uuid, p_peek );
121         p_peek += 16; i_read -= 16;
122     }
123     else
124     {
125         CreateUUID( &p_box->i_uuid, p_box->i_type );
126     }
127 #ifdef MP4_ULTRA_VERBOSE
128     if( p_box->i_size )
129     {
130         if MP4_BOX_TYPE_ASCII()
131             msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64" %"PRId64,
132                     (char*)&p_box->i_type, p_box->i_size, p_box->i_pos );
133         else
134             msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
135                     (char*)&p_box->i_type+1, p_box->i_size );
136     }
137 #endif
138
139     return 1;
140 }
141
142 /*****************************************************************************
143  * MP4_NextBox : Go to the next box
144  *****************************************************************************
145  * if p_box == NULL, go to the next box in which we are( at the begining ).
146  *****************************************************************************/
147 static int MP4_NextBox( stream_t *p_stream, MP4_Box_t *p_box )
148 {
149     MP4_Box_t box;
150
151     if( !p_box )
152     {
153         MP4_ReadBoxCommon( p_stream, &box );
154         p_box = &box;
155     }
156
157     if( !p_box->i_size )
158     {
159         return 2; /* Box with infinite size */
160     }
161
162     if( p_box->p_father )
163     {
164         /* if father's size == 0, it means unknown or infinite size,
165          * and we skip the followong check */
166         if( p_box->p_father->i_size > 0 )
167         {
168             const off_t i_box_end = p_box->i_size + p_box->i_pos;
169             const off_t i_father_end = p_box->p_father->i_size + p_box->p_father->i_pos;
170
171             /* check if it's within p-father */
172             if( i_box_end >= i_father_end )
173             {
174                 if( i_box_end > i_father_end )
175                     msg_Dbg( p_stream, "out of bound child" );
176                 return 0; /* out of bound */
177             }
178         }
179     }
180     if( stream_Seek( p_stream, p_box->i_size + p_box->i_pos ) )
181     {
182         return 0;
183     }
184
185     return 1;
186 }
187
188 /*****************************************************************************
189  * For all known box a loader is given,
190  *  XXX: all common struct have to be already read by MP4_ReadBoxCommon
191  *       after called one of theses functions, file position is unknown
192  *       you need to call MP4_GotoBox to go where you want
193  *****************************************************************************/
194 int MP4_ReadBoxContainerChildren( stream_t *p_stream,
195                                   MP4_Box_t *p_container, uint32_t i_last_child )
196 {
197     MP4_Box_t *p_box;
198
199     /* Size of root container is set to 0 when unknown, for exemple
200      * with a DASH stream. In that case, we skip the following check */
201     if( p_container->i_size
202             && ( stream_Tell( p_stream ) + 8 >
203         (off_t)(p_container->i_pos + p_container->i_size) )
204       )
205     {
206         /* there is no box to load */
207         return 0;
208     }
209
210     do
211     {
212         if( ( p_box = MP4_ReadBox( p_stream, p_container ) ) == NULL ) break;
213
214         /* chain this box with the father and the other at same level */
215         if( !p_container->p_first ) p_container->p_first = p_box;
216         else p_container->p_last->p_next = p_box;
217         p_container->p_last = p_box;
218
219         if( p_box->i_type == i_last_child )
220         {
221             MP4_NextBox( p_stream, p_box );
222             break;
223         }
224
225     } while( MP4_NextBox( p_stream, p_box ) == 1 );
226
227     return 1;
228 }
229
230 static int MP4_ReadBoxContainerRaw( stream_t *p_stream, MP4_Box_t *p_container )
231 {
232     return MP4_ReadBoxContainerChildren( p_stream, p_container, 0 );
233 }
234
235 static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
236 {
237     if( p_container->i_size &&
238         ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
239     {
240         /* container is empty, 8 stand for the first header in this box */
241         return 1;
242     }
243
244     /* enter box */
245     stream_Seek( p_stream, p_container->i_pos +
246                  mp4_box_headersize( p_container ) );
247
248     return MP4_ReadBoxContainerRaw( p_stream, p_container );
249 }
250
251 static void MP4_FreeBox_Common( MP4_Box_t *p_box )
252 {
253     /* Up to now do nothing */
254     (void)p_box;
255 }
256
257 static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
258 {
259     /* XXX sometime moov is hiden in a free box */
260     if( p_box->p_father &&
261         p_box->p_father->i_type == ATOM_root &&
262         p_box->i_type == ATOM_free )
263     {
264         const uint8_t *p_peek;
265         int     i_read;
266         vlc_fourcc_t i_fcc;
267
268         i_read  = stream_Peek( p_stream, &p_peek, 44 );
269
270         p_peek += mp4_box_headersize( p_box ) + 4;
271         i_read -= mp4_box_headersize( p_box ) + 4;
272
273         if( i_read >= 8 )
274         {
275             i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
276
277             if( i_fcc == ATOM_cmov || i_fcc == ATOM_mvhd )
278             {
279                 msg_Warn( p_stream, "detected moov hidden in a free box ..." );
280
281                 p_box->i_type = ATOM_foov;
282                 return MP4_ReadBoxContainer( p_stream, p_box );
283             }
284         }
285     }
286
287     /* Nothing to do */
288 #ifdef MP4_ULTRA_VERBOSE
289     if MP4_BOX_TYPE_ASCII()
290         msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
291     else
292         msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
293 #endif
294     return 1;
295 }
296
297 static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
298 {
299     MP4_READBOX_ENTER( MP4_Box_data_ftyp_t );
300
301     MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
302     MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
303
304     if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
305     {
306         uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
307             calloc( p_box->data.p_ftyp->i_compatible_brands_count,
308                     sizeof(uint32_t));
309
310         if( unlikely( tab == NULL ) )
311             MP4_READBOX_EXIT( 0 );
312
313         for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
314         {
315             MP4_GETFOURCC( tab[i] );
316         }
317     }
318     else
319     {
320         p_box->data.p_ftyp->i_compatible_brands = NULL;
321     }
322
323     MP4_READBOX_EXIT( 1 );
324 }
325
326 static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
327 {
328     FREENULL( p_box->data.p_ftyp->i_compatible_brands );
329 }
330
331
332 static int MP4_ReadBox_mvhd(  stream_t *p_stream, MP4_Box_t *p_box )
333 {
334 #ifdef MP4_VERBOSE
335     char s_creation_time[128];
336     char s_modification_time[128];
337     char s_duration[128];
338 #endif
339     MP4_READBOX_ENTER( MP4_Box_data_mvhd_t );
340
341     MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
342
343     if( p_box->data.p_mvhd->i_version )
344     {
345         MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
346         MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
347         MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
348         MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
349     }
350     else
351     {
352         MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
353         MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
354         MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
355         MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
356     }
357     MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
358     MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
359     MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
360
361
362     for( unsigned i = 0; i < 2; i++ )
363     {
364         MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
365     }
366     for( unsigned i = 0; i < 9; i++ )
367     {
368         MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
369     }
370     for( unsigned i = 0; i < 6; i++ )
371     {
372         MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
373     }
374
375     MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
376
377
378 #ifdef MP4_VERBOSE
379     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
380     MP4_ConvertDate2Str( s_modification_time,
381                          p_box->data.p_mvhd->i_modification_time, false );
382     if( p_box->data.p_mvhd->i_rate )
383     {
384         MP4_ConvertDate2Str( s_duration,
385                  p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_rate, true );
386     }
387     else
388     {
389         s_duration[0] = 0;
390     }
391     msg_Dbg( p_stream, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
392                   s_creation_time,
393                   s_modification_time,
394                   (uint32_t)p_box->data.p_mvhd->i_timescale,
395                   s_duration,
396                   (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
397                   (float)p_box->data.p_mvhd->i_volume / 256 ,
398                   (uint32_t)p_box->data.p_mvhd->i_next_track_id );
399 #endif
400     MP4_READBOX_EXIT( 1 );
401 }
402
403 static int MP4_ReadBox_mfhd(  stream_t *p_stream, MP4_Box_t *p_box )
404 {
405     MP4_READBOX_ENTER( MP4_Box_data_mfhd_t );
406
407     MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
408
409     MP4_GET4BYTES( p_box->data.p_mfhd->i_sequence_number );
410
411 #ifdef MP4_VERBOSE
412     msg_Dbg( p_stream, "read box: \"mfhd\" sequence number %d",
413                   p_box->data.p_mfhd->i_sequence_number );
414 #endif
415     MP4_READBOX_EXIT( 1 );
416 }
417
418 static int MP4_ReadBox_tfxd(  stream_t *p_stream, MP4_Box_t *p_box )
419 {
420     MP4_READBOX_ENTER( MP4_Box_data_tfxd_t );
421
422     MP4_Box_data_tfxd_t *p_tfxd_data = p_box->data.p_tfxd;
423     MP4_GETVERSIONFLAGS( p_tfxd_data );
424
425     if( p_tfxd_data->i_version == 0 )
426     {
427         MP4_GET4BYTES( p_tfxd_data->i_fragment_abs_time );
428         MP4_GET4BYTES( p_tfxd_data->i_fragment_duration );
429     }
430     else
431     {
432         MP4_GET8BYTES( p_tfxd_data->i_fragment_abs_time );
433         MP4_GET8BYTES( p_tfxd_data->i_fragment_duration );
434     }
435
436 #ifdef MP4_VERBOSE
437     msg_Dbg( p_stream, "read box: \"tfxd\" version %d, flags 0x%x, "\
438             "fragment duration %"PRIu64", fragment abs time %"PRIu64,
439                 p_tfxd_data->i_version,
440                 p_tfxd_data->i_flags,
441                 p_tfxd_data->i_fragment_duration,
442                 p_tfxd_data->i_fragment_abs_time
443            );
444 #endif
445
446     MP4_READBOX_EXIT( 1 );
447 }
448
449 static int MP4_ReadBox_tfrf(  stream_t *p_stream, MP4_Box_t *p_box )
450 {
451     MP4_READBOX_ENTER( MP4_Box_data_tfxd_t );
452
453     MP4_Box_data_tfrf_t *p_tfrf_data = p_box->data.p_tfrf;
454     MP4_GETVERSIONFLAGS( p_tfrf_data );
455
456     MP4_GET1BYTE( p_tfrf_data->i_fragment_count );
457
458     p_tfrf_data->p_tfrf_data_fields = calloc( p_tfrf_data->i_fragment_count,
459                                               sizeof( TfrfBoxDataFields_t ) );
460     if( !p_tfrf_data->p_tfrf_data_fields )
461         MP4_READBOX_EXIT( 0 );
462
463     for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
464     {
465         TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
466         if( p_tfrf_data->i_version == 0 )
467         {
468             MP4_GET4BYTES( TfrfBoxDataField->i_fragment_abs_time );
469             MP4_GET4BYTES( TfrfBoxDataField->i_fragment_duration );
470         }
471         else
472         {
473             MP4_GET8BYTES( TfrfBoxDataField->i_fragment_abs_time );
474             MP4_GET8BYTES( TfrfBoxDataField->i_fragment_duration );
475         }
476     }
477
478 #ifdef MP4_VERBOSE
479     msg_Dbg( p_stream, "read box: \"tfrf\" version %d, flags 0x%x, "\
480             "fragment count %"PRIu8, p_tfrf_data->i_version,
481                 p_tfrf_data->i_flags, p_tfrf_data->i_fragment_count );
482
483     for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
484     {
485         TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
486         msg_Dbg( p_stream, "\"tfrf\" fragment duration %"PRIu64", "\
487                                     "fragment abs time %"PRIu64,
488                     TfrfBoxDataField->i_fragment_duration,
489                     TfrfBoxDataField->i_fragment_abs_time );
490     }
491
492 #endif
493
494     MP4_READBOX_EXIT( 1 );
495 }
496
497 static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
498 {
499     FREENULL( p_box->data.p_tfrf->p_tfrf_data_fields );
500 }
501
502 static int MP4_ReadBox_stra( stream_t *p_stream, MP4_Box_t *p_box )
503 {
504     MP4_READBOX_ENTER( MP4_Box_data_stra_t );
505     MP4_Box_data_stra_t *p_stra = p_box->data.p_stra;
506
507     uint8_t i_reserved;
508     VLC_UNUSED(i_reserved);
509     MP4_GET1BYTE( p_stra->i_es_cat );
510     MP4_GET1BYTE( i_reserved );
511     MP4_GET2BYTES( p_stra->i_track_ID );
512
513     MP4_GET4BYTES( p_stra->i_timescale );
514     MP4_GET8BYTES( p_stra->i_duration );
515
516     MP4_GET4BYTES( p_stra->FourCC );
517     MP4_GET4BYTES( p_stra->Bitrate );
518     MP4_GET4BYTES( p_stra->MaxWidth );
519     MP4_GET4BYTES( p_stra->MaxHeight );
520     MP4_GET4BYTES( p_stra->SamplingRate );
521     MP4_GET4BYTES( p_stra->Channels );
522     MP4_GET4BYTES( p_stra->BitsPerSample );
523     MP4_GET4BYTES( p_stra->AudioTag );
524     MP4_GET2BYTES( p_stra->nBlockAlign );
525
526     MP4_GET1BYTE( i_reserved );
527     MP4_GET1BYTE( i_reserved );
528     MP4_GET1BYTE( i_reserved );
529     MP4_GET1BYTE( p_stra->cpd_len );
530     if( p_stra->cpd_len > i_read )
531         goto error;
532     p_stra->CodecPrivateData = malloc( p_stra->cpd_len );
533     if( unlikely( p_stra->CodecPrivateData == NULL ) )
534         goto error;
535     memcpy( p_stra->CodecPrivateData, p_peek, p_stra->cpd_len );
536
537 #ifdef MP4_VERBOSE
538     msg_Dbg( p_stream, "es_cat is %"PRIu8", birate is %"PRIu32,
539               p_stra->i_es_cat, p_stra->Bitrate );
540 #endif
541
542     MP4_READBOX_EXIT( 1 );
543 error:
544     MP4_READBOX_EXIT( 0 );
545 }
546
547 static void MP4_FreeBox_stra( MP4_Box_t *p_box )
548 {
549     FREENULL( p_box->data.p_stra->CodecPrivateData );
550 }
551
552 static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
553 {
554     if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
555         return MP4_ReadBox_tfrf( p_stream, p_box );
556     if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
557         return MP4_ReadBox_tfxd( p_stream, p_box );
558     if( !CmpUUID( &p_box->i_uuid, &SmooBoxUUID ) )
559         return MP4_ReadBoxContainer( p_stream, p_box );
560     if( !CmpUUID( &p_box->i_uuid, &StraBoxUUID ) )
561         return MP4_ReadBox_stra( p_stream, p_box );
562
563     msg_Warn( p_stream, "Unknown uuid type box" );
564     return 1;
565 }
566
567 static void MP4_FreeBox_uuid( MP4_Box_t *p_box )
568 {
569     if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
570         return MP4_FreeBox_tfrf( p_box );
571     if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
572         return MP4_FreeBox_Common( p_box );
573     if( !CmpUUID( &p_box->i_uuid, &SmooBoxUUID ) )
574         return MP4_FreeBox_Common( p_box );
575     if( !CmpUUID( &p_box->i_uuid, &StraBoxUUID ) )
576         return MP4_FreeBox_stra( p_box );
577 }
578
579 static int MP4_ReadBox_sidx(  stream_t *p_stream, MP4_Box_t *p_box )
580 {
581     MP4_READBOX_ENTER( MP4_Box_data_sidx_t );
582
583     MP4_Box_data_sidx_t *p_sidx_data = p_box->data.p_sidx;
584     MP4_GETVERSIONFLAGS( p_sidx_data );
585
586     MP4_GET4BYTES( p_sidx_data->i_reference_ID );
587     MP4_GET4BYTES( p_sidx_data->i_timescale );
588
589     if( p_sidx_data->i_version == 0 )
590     {
591         MP4_GET4BYTES( p_sidx_data->i_earliest_presentation_time );
592         MP4_GET4BYTES( p_sidx_data->i_first_offset );
593     }
594     else
595     {
596         MP4_GET8BYTES( p_sidx_data->i_earliest_presentation_time );
597         MP4_GET8BYTES( p_sidx_data->i_first_offset );
598     }
599
600     uint16_t i_reserved;
601     VLC_UNUSED(i_reserved);
602     MP4_GET2BYTES( i_reserved );
603     MP4_GET2BYTES( p_sidx_data->i_reference_count );
604     uint16_t i_count = p_sidx_data->i_reference_count;
605
606     p_sidx_data->p_items = calloc( i_count, sizeof( MP4_Box_sidx_item_t ) );
607     uint32_t tmp;
608     for( unsigned i = 0; i < i_count; i++ )
609     {
610         MP4_GET4BYTES( tmp );
611         p_sidx_data->p_items[i].b_reference_type = (bool)((tmp & 0x80000000)>>24);
612         p_sidx_data->p_items[i].i_referenced_size = tmp & 0x7fffffff;
613         MP4_GET4BYTES( p_sidx_data->p_items[i].i_subsegment_duration );
614
615         MP4_GET4BYTES( tmp );
616         p_sidx_data->p_items[i].b_starts_with_SAP = (bool)((tmp & 0x80000000)>>24);
617         p_sidx_data->p_items[i].i_SAP_type = (tmp & 0x70000000)>>24;
618         p_sidx_data->p_items[i].i_SAP_delta_time = tmp & 0xfffffff;
619     }
620
621 #ifdef MP4_VERBOSE
622     msg_Dbg( p_stream, "read box: \"sidx\" version %d, flags 0x%x, "\
623             "ref_ID %"PRIu32", timescale %"PRIu32", ref_count %"PRIu16", "\
624             "first subsegmt duration %"PRIu32,
625                 p_sidx_data->i_version,
626                 p_sidx_data->i_flags,
627                 p_sidx_data->i_reference_ID,
628                 p_sidx_data->i_timescale,
629                 p_sidx_data->i_reference_count,
630                 p_sidx_data->p_items[0].i_subsegment_duration
631            );
632 #endif
633
634     MP4_READBOX_EXIT( 1 );
635 }
636
637 static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
638 {
639     FREENULL( p_box->data.p_sidx->p_items );
640 }
641
642 static int MP4_ReadBox_tfhd(  stream_t *p_stream, MP4_Box_t *p_box )
643 {
644     MP4_READBOX_ENTER( MP4_Box_data_tfhd_t );
645
646     MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );
647
648     if( p_box->data.p_tfhd->i_version != 0 )
649     {
650         msg_Warn( p_stream, "'tfhd' box with version != 0. "\
651                 " Don't know what to do with that, please patch" );
652         MP4_READBOX_EXIT( 0 );
653     }
654
655     MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );
656
657     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
658     {
659         msg_Dbg( p_stream, "'duration-is-empty' flag is present "\
660                 "=> no samples for this time interval." );
661         p_box->data.p_tfhd->b_empty = true;
662     }
663     else
664         p_box->data.p_tfhd->b_empty = false;
665
666     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
667         MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
668     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
669         MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
670     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
671         MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
672     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
673         MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
674     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
675         MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
676
677 #ifdef MP4_VERBOSE
678     char psz_base[128] = "\0";
679     char psz_desc[128] = "\0";
680     char psz_dura[128] = "\0";
681     char psz_size[128] = "\0";
682     char psz_flag[128] = "\0";
683     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
684         snprintf(psz_base, sizeof(psz_base), "base offset %"PRId64, p_box->data.p_tfhd->i_base_data_offset);
685     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
686         snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
687     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
688         snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
689     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
690         snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
691     if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
692         snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);
693
694     msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
695                 p_box->data.p_tfhd->i_version,
696                 p_box->data.p_tfhd->i_flags,
697                 p_box->data.p_tfhd->i_track_ID,
698                 psz_base, psz_desc, psz_dura, psz_size, psz_flag );
699 #endif
700
701     MP4_READBOX_EXIT( 1 );
702 }
703
704 static int MP4_ReadBox_trun(  stream_t *p_stream, MP4_Box_t *p_box )
705 {
706     MP4_READBOX_ENTER( MP4_Box_data_trun_t );
707
708     MP4_GETVERSIONFLAGS( p_box->data.p_trun );
709
710     MP4_GET4BYTES( p_box->data.p_trun->i_sample_count );
711
712     if( p_box->data.p_trun->i_flags & MP4_TRUN_DATA_OFFSET )
713         MP4_GET4BYTES( p_box->data.p_trun->i_data_offset );
714     if( p_box->data.p_trun->i_flags & MP4_TRUN_FIRST_FLAGS )
715         MP4_GET4BYTES( p_box->data.p_trun->i_first_sample_flags );
716
717     p_box->data.p_trun->p_samples =
718       calloc( p_box->data.p_trun->i_sample_count, sizeof(MP4_descriptor_trun_sample_t) );
719     if ( p_box->data.p_trun->p_samples == NULL )
720         MP4_READBOX_EXIT( 0 );
721
722     for( unsigned int i = 0; i<p_box->data.p_trun->i_sample_count; i++ )
723     {
724         MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
725         if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
726             MP4_GET4BYTES( p_sample->i_duration );
727         if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
728             MP4_GET4BYTES( p_sample->i_size );
729         if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS )
730             MP4_GET4BYTES( p_sample->i_flags );
731         if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
732             MP4_GET4BYTES( p_sample->i_composition_time_offset );
733     }
734
735 #ifdef MP4_VERBOSE
736     msg_Dbg( p_stream, "read box: \"trun\" version %u flags 0x%x sample count %u",
737                   p_box->data.p_trun->i_version,
738                   p_box->data.p_trun->i_flags,
739                   p_box->data.p_trun->i_sample_count );
740
741     for( unsigned int i = 0; i<p_box->data.p_trun->i_sample_count; i++ )
742     {
743         MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
744         msg_Dbg( p_stream, "read box: \"trun\" sample %4.4u flags 0x%x "\
745             "duration %"PRIu32" size %"PRIu32" composition time offset %"PRIu32,
746                         i, p_sample->i_flags, p_sample->i_duration,
747                         p_sample->i_size, p_sample->i_composition_time_offset );
748     }
749 #endif
750
751     MP4_READBOX_EXIT( 1 );
752 }
753
754 static void MP4_FreeBox_trun( MP4_Box_t *p_box )
755 {
756     FREENULL( p_box->data.p_trun->p_samples );
757 }
758
759
760 static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
761 {
762 #ifdef MP4_VERBOSE
763     char s_creation_time[128];
764     char s_modification_time[128];
765     char s_duration[128];
766 #endif
767     MP4_READBOX_ENTER( MP4_Box_data_tkhd_t );
768
769     MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
770
771     if( p_box->data.p_tkhd->i_version )
772     {
773         MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
774         MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
775         MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
776         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
777         MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
778     }
779     else
780     {
781         MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
782         MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
783         MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
784         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
785         MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
786     }
787
788     for( unsigned i = 0; i < 2; i++ )
789     {
790         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
791     }
792     MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
793     MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
794     MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
795     MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
796
797     for( unsigned i = 0; i < 9; i++ )
798     {
799         MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
800     }
801     MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
802     MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
803
804     double rotation;    //angle in degrees to be rotated clockwise
805     double scale[2];    // scale factor; sx = scale[0] , sy = scale[1]
806     double translate[2];// amount to translate; tx = translate[0] , ty = translate[1]
807
808     int32_t *matrix = p_box->data.p_tkhd->i_matrix;
809
810     translate[0] = conv_fx(matrix[6]);
811     translate[1] = conv_fx(matrix[7]);
812
813     scale[0] = sqrt(conv_fx(matrix[0]) * conv_fx(matrix[0]) +
814                     conv_fx(matrix[3]) * conv_fx(matrix[3]));
815     scale[1] = sqrt(conv_fx(matrix[1]) * conv_fx(matrix[1]) +
816                     conv_fx(matrix[4]) * conv_fx(matrix[4]));
817
818     rotation = atan2(conv_fx(matrix[1]) / scale[1], conv_fx(matrix[0]) / scale[0]) * 180 / M_PI;
819
820     if (rotation < 0)
821         rotation += 360.;
822
823     p_box->data.p_tkhd->f_rotation = rotation;
824
825 #ifdef MP4_VERBOSE
826     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
827     MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time, false );
828     MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration, true );
829
830     msg_Dbg( p_stream, "read box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f rotation %f scaleX %f scaleY %f translateX %f translateY %f width %f height %f. "
831             "Matrix: %i %i %i %i %i %i %i %i %i",
832                   s_creation_time,
833                   s_modification_time,
834                   s_duration,
835                   p_box->data.p_tkhd->i_track_ID,
836                   p_box->data.p_tkhd->i_layer,
837                   (float)p_box->data.p_tkhd->i_volume / 256 ,
838                   rotation,
839                   scale[0],
840                   scale[1],
841                   translate[0],
842                   translate[1],
843                   (float)p_box->data.p_tkhd->i_width / BLOCK16x16,
844                   (float)p_box->data.p_tkhd->i_height / BLOCK16x16,
845                   p_box->data.p_tkhd->i_matrix[0],
846                   p_box->data.p_tkhd->i_matrix[1],
847                   p_box->data.p_tkhd->i_matrix[2],
848                   p_box->data.p_tkhd->i_matrix[3],
849                   p_box->data.p_tkhd->i_matrix[4],
850                   p_box->data.p_tkhd->i_matrix[5],
851                   p_box->data.p_tkhd->i_matrix[6],
852                   p_box->data.p_tkhd->i_matrix[7],
853                   p_box->data.p_tkhd->i_matrix[8] );
854 #endif
855     MP4_READBOX_EXIT( 1 );
856 }
857
858
859 static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
860 {
861     uint16_t i_language;
862 #ifdef MP4_VERBOSE
863     char s_creation_time[128];
864     char s_modification_time[128];
865     char s_duration[128];
866 #endif
867     MP4_READBOX_ENTER( MP4_Box_data_mdhd_t );
868
869     MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
870
871     if( p_box->data.p_mdhd->i_version )
872     {
873         MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
874         MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
875         MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
876         MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
877     }
878     else
879     {
880         MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
881         MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
882         MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
883         MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
884     }
885
886     MP4_GET2BYTES( i_language );
887     decodeQtLanguageCode( i_language, p_box->data.p_mdhd->rgs_language,
888                           &p_box->data.p_mdhd->b_mac_encoding );
889
890     MP4_GET2BYTES( p_box->data.p_mdhd->i_quality );
891
892 #ifdef MP4_VERBOSE
893     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time, false );
894     MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time, false );
895     MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration, true );
896     msg_Dbg( p_stream, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %3.3s",
897                   s_creation_time,
898                   s_modification_time,
899                   (uint32_t)p_box->data.p_mdhd->i_timescale,
900                   s_duration,
901                   (char*) &p_box->data.p_mdhd->rgs_language );
902 #endif
903     MP4_READBOX_EXIT( 1 );
904 }
905
906
907 static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
908 {
909     int32_t i_reserved;
910     VLC_UNUSED(i_reserved);
911
912     MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
913
914     MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
915
916     MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
917     MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
918
919     MP4_GET4BYTES( i_reserved );
920     MP4_GET4BYTES( i_reserved );
921     MP4_GET4BYTES( i_reserved );
922     p_box->data.p_hdlr->psz_name = NULL;
923
924     if( i_read > 0 )
925     {
926         uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_read + 1 );
927         if( unlikely( psz == NULL ) )
928             MP4_READBOX_EXIT( 0 );
929
930         /* Yes, I love .mp4 :( */
931         if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
932         {
933             uint8_t i_len;
934             int i_copy;
935
936             MP4_GET1BYTE( i_len );
937             i_copy = __MIN( i_read, i_len );
938
939             memcpy( psz, p_peek, i_copy );
940             p_box->data.p_hdlr->psz_name[i_copy] = '\0';
941         }
942         else
943         {
944             memcpy( psz, p_peek, i_read );
945             p_box->data.p_hdlr->psz_name[i_read] = '\0';
946         }
947     }
948
949 #ifdef MP4_VERBOSE
950         msg_Dbg( p_stream, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
951                    (char*)&p_box->data.p_hdlr->i_handler_type,
952                    p_box->data.p_hdlr->psz_name );
953
954 #endif
955     MP4_READBOX_EXIT( 1 );
956 }
957
958 static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
959 {
960     FREENULL( p_box->data.p_hdlr->psz_name );
961 }
962
963 static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
964 {
965     MP4_READBOX_ENTER( MP4_Box_data_vmhd_t );
966
967     MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
968
969     MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
970     for( unsigned i = 0; i < 3; i++ )
971     {
972         MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
973     }
974
975 #ifdef MP4_VERBOSE
976     msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
977                       p_box->data.p_vmhd->i_graphics_mode,
978                       p_box->data.p_vmhd->i_opcolor[0],
979                       p_box->data.p_vmhd->i_opcolor[1],
980                       p_box->data.p_vmhd->i_opcolor[2] );
981 #endif
982     MP4_READBOX_EXIT( 1 );
983 }
984
985 static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
986 {
987     MP4_READBOX_ENTER( MP4_Box_data_smhd_t );
988
989     MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
990
991
992
993     MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
994
995     MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
996
997 #ifdef MP4_VERBOSE
998     msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
999                       (float)p_box->data.p_smhd->i_balance / 256 );
1000 #endif
1001     MP4_READBOX_EXIT( 1 );
1002 }
1003
1004
1005 static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
1006 {
1007     MP4_READBOX_ENTER( MP4_Box_data_hmhd_t );
1008
1009     MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
1010
1011     MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
1012     MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
1013
1014     MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
1015     MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
1016
1017     MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
1018
1019 #ifdef MP4_VERBOSE
1020     msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1021                       p_box->data.p_hmhd->i_max_PDU_size,
1022                       p_box->data.p_hmhd->i_avg_PDU_size,
1023                       p_box->data.p_hmhd->i_max_bitrate,
1024                       p_box->data.p_hmhd->i_avg_bitrate );
1025 #endif
1026     MP4_READBOX_EXIT( 1 );
1027 }
1028
1029 static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
1030 {
1031     MP4_READBOX_ENTER( MP4_Box_data_url_t );
1032
1033     MP4_GETVERSIONFLAGS( p_box->data.p_url );
1034     MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
1035
1036 #ifdef MP4_VERBOSE
1037     msg_Dbg( p_stream, "read box: \"url\" url: %s",
1038                        p_box->data.p_url->psz_location );
1039
1040 #endif
1041     MP4_READBOX_EXIT( 1 );
1042 }
1043
1044
1045 static void MP4_FreeBox_url( MP4_Box_t *p_box )
1046 {
1047     FREENULL( p_box->data.p_url->psz_location );
1048 }
1049
1050 static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
1051 {
1052     MP4_READBOX_ENTER( MP4_Box_data_urn_t );
1053
1054     MP4_GETVERSIONFLAGS( p_box->data.p_urn );
1055
1056     MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
1057     MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
1058
1059 #ifdef MP4_VERBOSE
1060     msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
1061                       p_box->data.p_urn->psz_name,
1062                       p_box->data.p_urn->psz_location );
1063 #endif
1064     MP4_READBOX_EXIT( 1 );
1065 }
1066 static void MP4_FreeBox_urn( MP4_Box_t *p_box )
1067 {
1068     FREENULL( p_box->data.p_urn->psz_name );
1069     FREENULL( p_box->data.p_urn->psz_location );
1070 }
1071
1072
1073 static int MP4_ReadBox_dref( stream_t *p_stream, MP4_Box_t *p_box )
1074 {
1075     MP4_READBOX_ENTER( MP4_Box_data_dref_t );
1076
1077     MP4_GETVERSIONFLAGS( p_box->data.p_dref );
1078
1079     MP4_GET4BYTES( p_box->data.p_dref->i_entry_count );
1080
1081     stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
1082     MP4_ReadBoxContainerRaw( p_stream, p_box );
1083
1084 #ifdef MP4_VERBOSE
1085     msg_Dbg( p_stream, "read box: \"dref\" entry-count %d",
1086                       p_box->data.p_dref->i_entry_count );
1087
1088 #endif
1089     MP4_READBOX_EXIT( 1 );
1090 }
1091
1092 static void MP4_FreeBox_stts( MP4_Box_t *p_box )
1093 {
1094     FREENULL( p_box->data.p_stts->pi_sample_count );
1095     FREENULL( p_box->data.p_stts->pi_sample_delta );
1096 }
1097
1098 static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
1099 {
1100     MP4_READBOX_ENTER( MP4_Box_data_stts_t );
1101
1102     MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1103     MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );
1104
1105     p_box->data.p_stts->pi_sample_count =
1106         calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
1107     p_box->data.p_stts->pi_sample_delta =
1108         calloc( p_box->data.p_stts->i_entry_count, sizeof(int32_t) );
1109     if( p_box->data.p_stts->pi_sample_count == NULL
1110      || p_box->data.p_stts->pi_sample_delta == NULL )
1111     {
1112         MP4_READBOX_EXIT( 0 );
1113     }
1114
1115     uint32_t i = 0;
1116     for( ; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
1117     {
1118         MP4_GET4BYTES( p_box->data.p_stts->pi_sample_count[i] );
1119         MP4_GET4BYTES( p_box->data.p_stts->pi_sample_delta[i] );
1120     }
1121
1122     if ( i < p_box->data.p_stts->i_entry_count )
1123         p_box->data.p_stts->i_entry_count = i;
1124
1125 #ifdef MP4_VERBOSE
1126     msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
1127                       p_box->data.p_stts->i_entry_count );
1128
1129 #endif
1130     MP4_READBOX_EXIT( 1 );
1131 }
1132
1133
1134 static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
1135 {
1136     FREENULL( p_box->data.p_ctts->pi_sample_count );
1137     FREENULL( p_box->data.p_ctts->pi_sample_offset );
1138 }
1139
1140 static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
1141 {
1142     MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
1143
1144     MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1145
1146     MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );
1147
1148     p_box->data.p_ctts->pi_sample_count =
1149         calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
1150     p_box->data.p_ctts->pi_sample_offset =
1151         calloc( p_box->data.p_ctts->i_entry_count, sizeof(int32_t) );
1152     if( ( p_box->data.p_ctts->pi_sample_count == NULL )
1153      || ( p_box->data.p_ctts->pi_sample_offset == NULL ) )
1154     {
1155         MP4_READBOX_EXIT( 0 );
1156     }
1157
1158     uint32_t i = 0;
1159     for( ; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
1160     {
1161         MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_count[i] );
1162         MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_offset[i] );
1163     }
1164     if ( i < p_box->data.p_ctts->i_entry_count )
1165         p_box->data.p_ctts->i_entry_count = i;
1166
1167 #ifdef MP4_VERBOSE
1168     msg_Dbg( p_stream, "read box: \"ctts\" entry-count %d",
1169                       p_box->data.p_ctts->i_entry_count );
1170
1171 #endif
1172     MP4_READBOX_EXIT( 1 );
1173 }
1174
1175
1176 static int MP4_ReadLengthDescriptor( uint8_t **pp_peek, int64_t  *i_read )
1177 {
1178     unsigned int i_b;
1179     unsigned int i_len = 0;
1180     do
1181     {
1182         i_b = **pp_peek;
1183
1184         (*pp_peek)++;
1185         (*i_read)--;
1186         i_len = ( i_len << 7 ) + ( i_b&0x7f );
1187     } while( i_b&0x80 );
1188     return( i_len );
1189 }
1190
1191
1192 static void MP4_FreeBox_esds( MP4_Box_t *p_box )
1193 {
1194     FREENULL( p_box->data.p_esds->es_descriptor.psz_URL );
1195     if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1196     {
1197         FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1198         FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1199     }
1200 }
1201
1202 static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
1203 {
1204 #define es_descriptor p_box->data.p_esds->es_descriptor
1205     unsigned int i_len;
1206     unsigned int i_flags;
1207     unsigned int i_type;
1208
1209     MP4_READBOX_ENTER( MP4_Box_data_esds_t );
1210
1211     MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1212
1213
1214     MP4_GET1BYTE( i_type );
1215     if( i_type == 0x03 ) /* MP4ESDescrTag */
1216     {
1217         i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1218
1219 #ifdef MP4_VERBOSE
1220         msg_Dbg( p_stream, "found esds MPEG4ESDescr (%dBytes)",
1221                  i_len );
1222 #endif
1223
1224         MP4_GET2BYTES( es_descriptor.i_ES_ID );
1225         MP4_GET1BYTE( i_flags );
1226         es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1227         es_descriptor.b_url = ( (i_flags&0x40) != 0);
1228         es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1229
1230         es_descriptor.i_stream_priority = i_flags&0x1f;
1231         if( es_descriptor.b_stream_dependence )
1232         {
1233             MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1234         }
1235         if( es_descriptor.b_url )
1236         {
1237             unsigned int i_len;
1238
1239             MP4_GET1BYTE( i_len );
1240             i_len = __MIN(i_read, i_len);
1241             es_descriptor.psz_URL = malloc( i_len + 1 );
1242             if( es_descriptor.psz_URL )
1243             {
1244                 memcpy( es_descriptor.psz_URL, p_peek, i_len );
1245                 es_descriptor.psz_URL[i_len] = 0;
1246             }
1247             p_peek += i_len;
1248             i_read -= i_len;
1249         }
1250         else
1251         {
1252             es_descriptor.psz_URL = NULL;
1253         }
1254         if( es_descriptor.b_OCRstream )
1255         {
1256             MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1257         }
1258         MP4_GET1BYTE( i_type ); /* get next type */
1259     }
1260
1261     if( i_type != 0x04)/* MP4DecConfigDescrTag */
1262     {
1263          es_descriptor.p_decConfigDescr = NULL;
1264          MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1265     }
1266
1267     i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1268
1269 #ifdef MP4_VERBOSE
1270         msg_Dbg( p_stream, "found esds MP4DecConfigDescr (%dBytes)",
1271                  i_len );
1272 #endif
1273
1274     es_descriptor.p_decConfigDescr =
1275             calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
1276     if( unlikely( es_descriptor.p_decConfigDescr == NULL ) )
1277         MP4_READBOX_EXIT( 0 );
1278
1279     MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectTypeIndication );
1280     MP4_GET1BYTE( i_flags );
1281     es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1282     es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1283     MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1284     MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1285     MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1286     MP4_GET1BYTE( i_type );
1287     if( i_type !=  0x05 )/* MP4DecSpecificDescrTag */
1288     {
1289         es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1290         es_descriptor.p_decConfigDescr->p_decoder_specific_info  = NULL;
1291         MP4_READBOX_EXIT( 1 );
1292     }
1293
1294     i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1295
1296 #ifdef MP4_VERBOSE
1297         msg_Dbg( p_stream, "found esds MP4DecSpecificDescr (%dBytes)",
1298                  i_len );
1299 #endif
1300     if( i_len > i_read )
1301         MP4_READBOX_EXIT( 0 );
1302
1303     es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1304     es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1305     if( unlikely( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL ) )
1306         MP4_READBOX_EXIT( 0 );
1307
1308     memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1309             p_peek, i_len );
1310
1311     MP4_READBOX_EXIT( 1 );
1312 #undef es_descriptor
1313 }
1314
1315 static void MP4_FreeBox_hvcC(MP4_Box_t *p_box )
1316 {
1317     MP4_Box_data_hvcC_t *p_hvcC =  p_box->data.p_hvcC;
1318     if( p_hvcC->i_hvcC > 0 ) FREENULL( p_hvcC->p_hvcC) ;
1319 }
1320
1321 static int MP4_ReadBox_hvcC( stream_t *p_stream, MP4_Box_t *p_box )
1322 {
1323     MP4_Box_data_hvcC_t *p_hvcC;
1324
1325     MP4_READBOX_ENTER( MP4_Box_data_hvcC_t );
1326     p_hvcC = p_box->data.p_hvcC;
1327
1328     p_hvcC->i_hvcC = i_read;
1329     if( p_hvcC->i_hvcC > 0 )
1330     {
1331         uint8_t * p = p_hvcC->p_hvcC = malloc( p_hvcC->i_hvcC );
1332         if( p )
1333             memcpy( p, p_peek, i_read );
1334     }
1335     MP4_READBOX_EXIT( 1 );
1336 }
1337
1338 static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
1339 {
1340     MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1341     int i;
1342
1343     if( p_avcC->i_avcC > 0 ) FREENULL( p_avcC->p_avcC );
1344
1345     if( p_avcC->sps )
1346     {
1347         for( i = 0; i < p_avcC->i_sps; i++ )
1348             FREENULL( p_avcC->sps[i] );
1349     }
1350     if( p_avcC->pps )
1351     {
1352         for( i = 0; i < p_avcC->i_pps; i++ )
1353             FREENULL( p_avcC->pps[i] );
1354     }
1355     if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->sps );
1356     if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->i_sps_length );
1357     if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->pps );
1358     if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->i_pps_length );
1359 }
1360
1361 static int MP4_ReadBox_avcC( stream_t *p_stream, MP4_Box_t *p_box )
1362 {
1363     MP4_Box_data_avcC_t *p_avcC;
1364     int i;
1365
1366     MP4_READBOX_ENTER( MP4_Box_data_avcC_t );
1367     p_avcC = p_box->data.p_avcC;
1368
1369     p_avcC->i_avcC = i_read;
1370     if( p_avcC->i_avcC > 0 )
1371     {
1372         uint8_t * p = p_avcC->p_avcC = malloc( p_avcC->i_avcC );
1373         if( p )
1374             memcpy( p, p_peek, i_read );
1375     }
1376
1377     MP4_GET1BYTE( p_avcC->i_version );
1378     MP4_GET1BYTE( p_avcC->i_profile );
1379     MP4_GET1BYTE( p_avcC->i_profile_compatibility );
1380     MP4_GET1BYTE( p_avcC->i_level );
1381     MP4_GET1BYTE( p_avcC->i_reserved1 );
1382     p_avcC->i_length_size = (p_avcC->i_reserved1&0x03) + 1;
1383     p_avcC->i_reserved1 >>= 2;
1384
1385     MP4_GET1BYTE( p_avcC->i_reserved2 );
1386     p_avcC->i_sps = p_avcC->i_reserved2&0x1f;
1387     p_avcC->i_reserved2 >>= 5;
1388
1389     if( p_avcC->i_sps > 0 )
1390     {
1391         p_avcC->i_sps_length = calloc( p_avcC->i_sps, sizeof( uint16_t ) );
1392         p_avcC->sps = calloc( p_avcC->i_sps, sizeof( uint8_t* ) );
1393
1394         if( !p_avcC->i_sps_length || !p_avcC->sps )
1395             goto error;
1396
1397         for( i = 0; i < p_avcC->i_sps && i_read > 2; i++ )
1398         {
1399             MP4_GET2BYTES( p_avcC->i_sps_length[i] );
1400             if ( p_avcC->i_sps_length[i] > i_read )
1401                 goto error;
1402             p_avcC->sps[i] = malloc( p_avcC->i_sps_length[i] );
1403             if( p_avcC->sps[i] )
1404                 memcpy( p_avcC->sps[i], p_peek, p_avcC->i_sps_length[i] );
1405
1406             p_peek += p_avcC->i_sps_length[i];
1407             i_read -= p_avcC->i_sps_length[i];
1408         }
1409         if ( i != p_avcC->i_sps )
1410             goto error;
1411     }
1412
1413     MP4_GET1BYTE( p_avcC->i_pps );
1414     if( p_avcC->i_pps > 0 )
1415     {
1416         p_avcC->i_pps_length = calloc( p_avcC->i_pps, sizeof( uint16_t ) );
1417         p_avcC->pps = calloc( p_avcC->i_pps, sizeof( uint8_t* ) );
1418
1419         if( !p_avcC->i_pps_length || !p_avcC->pps )
1420             goto error;
1421
1422         for( i = 0; i < p_avcC->i_pps && i_read > 2; i++ )
1423         {
1424             MP4_GET2BYTES( p_avcC->i_pps_length[i] );
1425             if( p_avcC->i_pps_length[i] > i_read )
1426                 goto error;
1427             p_avcC->pps[i] = malloc( p_avcC->i_pps_length[i] );
1428             if( p_avcC->pps[i] )
1429                 memcpy( p_avcC->pps[i], p_peek, p_avcC->i_pps_length[i] );
1430
1431             p_peek += p_avcC->i_pps_length[i];
1432             i_read -= p_avcC->i_pps_length[i];
1433         }
1434         if ( i != p_avcC->i_pps )
1435             goto error;
1436     }
1437 #ifdef MP4_VERBOSE
1438     msg_Dbg( p_stream,
1439              "read box: \"avcC\" version=%d profile=0x%x level=0x%x length size=%d sps=%d pps=%d",
1440              p_avcC->i_version, p_avcC->i_profile, p_avcC->i_level,
1441              p_avcC->i_length_size,
1442              p_avcC->i_sps, p_avcC->i_pps );
1443     for( i = 0; i < p_avcC->i_sps; i++ )
1444     {
1445         msg_Dbg( p_stream, "         - sps[%d] length=%d",
1446                  i, p_avcC->i_sps_length[i] );
1447     }
1448     for( i = 0; i < p_avcC->i_pps; i++ )
1449     {
1450         msg_Dbg( p_stream, "         - pps[%d] length=%d",
1451                  i, p_avcC->i_pps_length[i] );
1452     }
1453
1454 #endif
1455     MP4_READBOX_EXIT( 1 );
1456
1457 error:
1458     MP4_FreeBox_avcC( p_box );
1459     MP4_READBOX_EXIT( 0 );
1460 }
1461
1462 static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
1463 {
1464     MP4_READBOX_ENTER( MP4_Box_data_chan_t );
1465     MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
1466
1467     if ( i_read < 16 )
1468         MP4_READBOX_EXIT( 0 );
1469
1470     MP4_GET1BYTE( p_chan->i_version );
1471     MP4_GET3BYTES( p_chan->i_channels_flags );
1472     MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
1473     MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
1474     MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
1475     if ( i_read < p_chan->layout.i_channels_description_count * 24 )
1476         MP4_READBOX_EXIT( 0 );
1477
1478     p_chan->layout.p_descriptions =
1479         malloc( p_chan->layout.i_channels_description_count * 24 );
1480
1481     if ( !p_chan->layout.p_descriptions )
1482         MP4_READBOX_EXIT( 0 );
1483
1484     for( uint32_t i=0; i<p_chan->layout.i_channels_description_count; i++ )
1485     {
1486         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
1487         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
1488         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
1489         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
1490         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
1491     }
1492
1493 #ifdef MP4_VERBOSE
1494     msg_Dbg( p_stream,
1495              "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
1496              p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
1497              p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
1498 #endif
1499     MP4_READBOX_EXIT( 1 );
1500 }
1501
1502 static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
1503 {
1504     MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
1505     free( p_chan->layout.p_descriptions );
1506 }
1507
1508 static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
1509 {
1510     MP4_READBOX_ENTER( MP4_Box_data_dec3_t );
1511
1512     MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
1513
1514     unsigned i_header;
1515     MP4_GET2BYTES( i_header );
1516
1517     p_dec3->i_data_rate = i_header >> 3;
1518     p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
1519     for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
1520         MP4_GET3BYTES( i_header );
1521         p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
1522         p_dec3->stream[i].i_bsid  = ( i_header >> 17 ) & 0x01f;
1523         p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
1524         p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
1525         p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
1526         p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
1527         if (p_dec3->stream[i].i_num_dep_sub) {
1528             MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
1529             p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
1530         } else
1531             p_dec3->stream[i].i_chan_loc = 0;
1532     }
1533
1534 #ifdef MP4_VERBOSE
1535     msg_Dbg( p_stream,
1536         "read box: \"dec3\" bitrate %dkbps %d independant substreams",
1537             p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
1538
1539     for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
1540         msg_Dbg( p_stream,
1541                 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
1542                 "num dependant subs=%d chan_loc=0x%x",
1543                 i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
1544                 p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
1545 #endif
1546     MP4_READBOX_EXIT( 1 );
1547 }
1548
1549 static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
1550 {
1551     MP4_Box_data_dac3_t *p_dac3;
1552     MP4_READBOX_ENTER( MP4_Box_data_dac3_t );
1553
1554     p_dac3 = p_box->data.p_dac3;
1555
1556     unsigned i_header;
1557     MP4_GET3BYTES( i_header );
1558
1559     p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
1560     p_dac3->i_bsid  = ( i_header >> 17 ) & 0x01f;
1561     p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
1562     p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
1563     p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
1564     p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
1565
1566 #ifdef MP4_VERBOSE
1567     msg_Dbg( p_stream,
1568              "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
1569              p_dac3->i_fscod, p_dac3->i_bsid, p_dac3->i_bsmod, p_dac3->i_acmod, p_dac3->i_lfeon, p_dac3->i_bitrate_code );
1570 #endif
1571     MP4_READBOX_EXIT( 1 );
1572 }
1573
1574 static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
1575 {
1576     MP4_Box_data_dvc1_t *p_dvc1;
1577
1578     MP4_READBOX_ENTER( MP4_Box_data_dvc1_t );
1579     p_dvc1 = p_box->data.p_dvc1;
1580
1581     MP4_GET1BYTE( p_dvc1->i_profile_level ); /* profile is on 4bits, level 3bits */
1582     uint8_t i_profile = (p_dvc1->i_profile_level & 0xf0) >> 4;
1583     if( i_profile != 0x06 && i_profile != 0x0c )
1584     {
1585         msg_Warn( p_stream, "unsupported VC-1 profile (%"PRIu8"), please report", i_profile );
1586         MP4_READBOX_EXIT( 0 );
1587     }
1588
1589
1590     p_dvc1->i_vc1 = p_box->i_size - 7; /* Header + profile_level */
1591
1592     if( p_dvc1->i_vc1 > 0 )
1593     {
1594         uint8_t *p = p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 );
1595         if( p )
1596             memcpy( p, p_peek, i_read );
1597     }
1598
1599 #ifdef MP4_VERBOSE
1600     msg_Dbg( p_stream,
1601              "read box: \"dvc1\" profile=%"PRIu8" level=%i",
1602              i_profile, p_dvc1->i_profile_level & 0x0e >> 1 );
1603 #endif
1604
1605     MP4_READBOX_EXIT( 1 );
1606 }
1607
1608 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
1609 {
1610     MP4_Box_data_enda_t *p_enda;
1611     MP4_READBOX_ENTER( MP4_Box_data_enda_t );
1612
1613     p_enda = p_box->data.p_enda;
1614
1615     MP4_GET2BYTES( p_enda->i_little_endian );
1616
1617 #ifdef MP4_VERBOSE
1618     msg_Dbg( p_stream,
1619              "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
1620 #endif
1621     MP4_READBOX_EXIT( 1 );
1622 }
1623
1624 static int MP4_ReadBox_gnre( stream_t *p_stream, MP4_Box_t *p_box )
1625 {
1626     MP4_Box_data_gnre_t *p_gnre;
1627     MP4_READBOX_ENTER( MP4_Box_data_gnre_t );
1628
1629     p_gnre = p_box->data.p_gnre;
1630
1631     uint32_t i_data_len;
1632     uint32_t i_data_tag;
1633
1634     MP4_GET4BYTES( i_data_len );
1635     MP4_GETFOURCC( i_data_tag );
1636     if( i_data_len < 10 || i_data_tag != ATOM_data )
1637         MP4_READBOX_EXIT( 0 );
1638
1639     uint32_t i_version;
1640     VLC_UNUSED(i_version);
1641     uint32_t i_reserved;
1642     VLC_UNUSED(i_reserved);
1643     MP4_GET4BYTES( i_version );
1644     MP4_GET4BYTES( i_reserved );
1645     MP4_GET2BYTES( p_gnre->i_genre );
1646     if( p_gnre->i_genre == 0 )
1647         MP4_READBOX_EXIT( 0 );
1648 #ifdef MP4_VERBOSE
1649     msg_Dbg( p_stream, "read box: \"gnre\" genre=%i", p_gnre->i_genre );
1650 #endif
1651
1652     MP4_READBOX_EXIT( 1 );
1653 }
1654
1655 static int MP4_ReadBox_trkn( stream_t *p_stream, MP4_Box_t *p_box )
1656 {
1657     MP4_Box_data_trkn_t *p_trkn;
1658     MP4_READBOX_ENTER( MP4_Box_data_trkn_t );
1659
1660     p_trkn = p_box->data.p_trkn;
1661
1662     uint32_t i_data_len;
1663     uint32_t i_data_tag;
1664
1665     MP4_GET4BYTES( i_data_len );
1666     MP4_GETFOURCC( i_data_tag );
1667     if( i_data_len < 12 || i_data_tag != ATOM_data )
1668         MP4_READBOX_EXIT( 0 );
1669
1670     uint32_t i_version;
1671     VLC_UNUSED(i_version);
1672     uint32_t i_reserved;
1673     VLC_UNUSED(i_reserved);
1674     MP4_GET4BYTES( i_version );
1675     MP4_GET4BYTES( i_reserved );
1676     MP4_GET2BYTES( i_reserved );
1677     MP4_GET2BYTES( p_trkn->i_track_number );
1678 #ifdef MP4_VERBOSE
1679     msg_Dbg( p_stream, "read box: \"trkn\" number=%i", p_trkn->i_track_number );
1680 #endif
1681     if( i_data_len > 15 )
1682     {
1683        MP4_GET2BYTES( p_trkn->i_track_total );
1684 #ifdef MP4_VERBOSE
1685        msg_Dbg( p_stream, "read box: \"trkn\" total=%i", p_trkn->i_track_total );
1686 #endif
1687     }
1688
1689     MP4_READBOX_EXIT( 1 );
1690 }
1691
1692 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
1693 {
1694     p_box->i_handler = ATOM_soun;
1695     MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
1696     p_box->data.p_sample_soun->p_qt_description = NULL;
1697
1698     /* Sanity check needed because the "wave" box does also contain an
1699      * "mp4a" box that we don't understand. */
1700     if( i_read < 28 )
1701     {
1702         i_read -= 30;
1703         MP4_READBOX_EXIT( 1 );
1704     }
1705
1706     for( unsigned i = 0; i < 6 ; i++ )
1707     {
1708         MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
1709     }
1710
1711     MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
1712
1713     /*
1714      * XXX hack -> produce a copy of the nearly complete chunk
1715      */
1716     p_box->data.p_sample_soun->i_qt_description = 0;
1717     p_box->data.p_sample_soun->p_qt_description = NULL;
1718     if( i_read > 0 )
1719     {
1720         p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
1721         if( p_box->data.p_sample_soun->p_qt_description )
1722         {
1723             p_box->data.p_sample_soun->i_qt_description = i_read;
1724             memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
1725         }
1726     }
1727
1728     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
1729     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
1730     MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
1731
1732     MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
1733     MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
1734     MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
1735     MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
1736     MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
1737     MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
1738
1739     if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
1740     {
1741         /* SoundDescriptionV1 */
1742         MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
1743         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
1744         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
1745         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
1746
1747 #ifdef MP4_VERBOSE
1748         msg_Dbg( p_stream,
1749                  "read box: \"soun\" qt3+ sample/packet=%d bytes/packet=%d "
1750                  "bytes/frame=%d bytes/sample=%d",
1751                  p_box->data.p_sample_soun->i_sample_per_packet,
1752                  p_box->data.p_sample_soun->i_bytes_per_packet,
1753                  p_box->data.p_sample_soun->i_bytes_per_frame,
1754                  p_box->data.p_sample_soun->i_bytes_per_sample );
1755 #endif
1756         stream_Seek( p_stream, p_box->i_pos +
1757                         mp4_box_headersize( p_box ) + 44 );
1758     }
1759     else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
1760     {
1761         /* SoundDescriptionV2 */
1762         double f_sample_rate;
1763         int64_t i_dummy64;
1764         uint32_t i_channel, i_extoffset, i_dummy32;
1765
1766         /* Checks */
1767         if ( p_box->data.p_sample_soun->i_channelcount != 0x3  ||
1768              p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
1769              p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
1770              p_box->data.p_sample_soun->i_reserved3 != 0x0     ||
1771              p_box->data.p_sample_soun->i_sampleratehi != 0x1  ||//65536
1772              p_box->data.p_sample_soun->i_sampleratelo != 0x0 )  //remainder
1773         {
1774             msg_Err( p_stream, "invalid stsd V2 box defaults" );
1775             MP4_READBOX_EXIT( 0 );
1776         }
1777         /* !Checks */
1778
1779         MP4_GET4BYTES( i_extoffset ); /* offset to stsd extentions */
1780         MP4_GET8BYTES( i_dummy64 );
1781         memcpy( &f_sample_rate, &i_dummy64, 8 );
1782         msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
1783         p_box->data.p_sample_soun->i_sampleratehi = (int)f_sample_rate % BLOCK16x16;
1784         p_box->data.p_sample_soun->i_sampleratelo = f_sample_rate / BLOCK16x16;
1785
1786         MP4_GET4BYTES( i_channel );
1787         p_box->data.p_sample_soun->i_channelcount = i_channel;
1788
1789         MP4_GET4BYTES( i_dummy32 );
1790         if ( i_dummy32 != 0x7F000000 )
1791         {
1792             msg_Err( p_stream, "invalid stsd V2 box" );
1793             MP4_READBOX_EXIT( 0 );
1794         }
1795
1796         MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
1797         MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
1798         MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
1799         MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
1800
1801 #ifdef MP4_VERBOSE
1802         msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
1803                            "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
1804                  f_sample_rate,
1805                  p_box->data.p_sample_soun->i_constbitsperchannel,
1806                  p_box->data.p_sample_soun->i_formatflags,
1807                  p_box->data.p_sample_soun->i_constbytesperaudiopacket,
1808                  p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
1809 #endif
1810         if ( i_extoffset < p_box->i_size )
1811             stream_Seek( p_stream, p_box->i_pos + i_extoffset );
1812         else
1813             stream_Seek( p_stream, p_box->i_pos + p_box->i_size );
1814     }
1815     else
1816     {
1817         p_box->data.p_sample_soun->i_sample_per_packet = 0;
1818         p_box->data.p_sample_soun->i_bytes_per_packet = 0;
1819         p_box->data.p_sample_soun->i_bytes_per_frame = 0;
1820         p_box->data.p_sample_soun->i_bytes_per_sample = 0;
1821
1822 #ifdef MP4_VERBOSE
1823         msg_Dbg( p_stream, "read box: \"soun\" mp4 or qt1/2 (rest=%"PRId64")",
1824                  i_read );
1825 #endif
1826         stream_Seek( p_stream, p_box->i_pos +
1827                         mp4_box_headersize( p_box ) + 28 );
1828     }
1829
1830     if( p_box->i_type == ATOM_drms )
1831     {
1832         msg_Warn( p_stream, "DRM protected streams are not supported." );
1833         MP4_READBOX_EXIT( 0 );
1834     }
1835
1836     if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
1837     {
1838         /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
1839         p_box->data.p_sample_soun->i_channelcount = 1;
1840     }
1841
1842     /* Loads extensions */
1843     MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds/wave/... */
1844
1845 #ifdef MP4_VERBOSE
1846     msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
1847              "sample size %d sample rate %f",
1848              p_box->data.p_sample_soun->i_channelcount,
1849              p_box->data.p_sample_soun->i_samplesize,
1850              (float)p_box->data.p_sample_soun->i_sampleratehi +
1851              (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
1852
1853 #endif
1854     MP4_READBOX_EXIT( 1 );
1855 }
1856
1857
1858 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
1859 {
1860     FREENULL( p_box->data.p_sample_soun->p_qt_description );
1861 }
1862
1863
1864 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
1865 {
1866     p_box->i_handler = ATOM_vide;
1867     MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );
1868
1869     for( unsigned i = 0; i < 6 ; i++ )
1870     {
1871         MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
1872     }
1873
1874     MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
1875
1876     /*
1877      * XXX hack -> produce a copy of the nearly complete chunk
1878      */
1879     if( i_read > 0 )
1880     {
1881         p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
1882         if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
1883             MP4_READBOX_EXIT( 0 );
1884         p_box->data.p_sample_vide->i_qt_image_description = i_read;
1885         memcpy( p_box->data.p_sample_vide->p_qt_image_description,
1886                 p_peek, i_read );
1887     }
1888     else
1889     {
1890         p_box->data.p_sample_vide->i_qt_image_description = 0;
1891         p_box->data.p_sample_vide->p_qt_image_description = NULL;
1892     }
1893
1894     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
1895     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
1896     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
1897
1898     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
1899     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
1900
1901     MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
1902     MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
1903
1904     MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
1905     MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
1906
1907     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
1908     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
1909
1910     memcpy( &p_box->data.p_sample_vide->i_compressorname, p_peek, 32 );
1911     p_peek += 32; i_read -= 32;
1912
1913     MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
1914     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
1915
1916     stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 78);
1917
1918     if( p_box->i_type == ATOM_drmi )
1919     {
1920         msg_Warn( p_stream, "DRM protected streams are not supported." );
1921         MP4_READBOX_EXIT( 0 );
1922     }
1923
1924     MP4_ReadBoxContainerRaw( p_stream, p_box );
1925
1926 #ifdef MP4_VERBOSE
1927     msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d",
1928                       p_box->data.p_sample_vide->i_width,
1929                       p_box->data.p_sample_vide->i_height,
1930                       p_box->data.p_sample_vide->i_depth );
1931
1932 #endif
1933     MP4_READBOX_EXIT( 1 );
1934 }
1935
1936
1937 void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
1938 {
1939     FREENULL( p_box->data.p_sample_vide->p_qt_image_description );
1940 }
1941
1942 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
1943 {
1944     stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
1945     MP4_ReadBoxContainerRaw( p_stream, p_box );
1946     return 1;
1947 }
1948
1949 static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
1950 {
1951     int32_t t;
1952
1953     p_box->i_handler = ATOM_text;
1954     MP4_READBOX_ENTER( MP4_Box_data_sample_text_t );
1955
1956     MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
1957     MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
1958
1959     MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
1960
1961     MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
1962
1963     MP4_GET4BYTES( t );
1964     switch( t )
1965     {
1966         /* FIXME search right signification */
1967         case 1: // Center
1968             p_box->data.p_sample_text->i_justification_horizontal = 1;
1969             p_box->data.p_sample_text->i_justification_vertical = 1;
1970             break;
1971         case -1:    // Flush Right
1972             p_box->data.p_sample_text->i_justification_horizontal = -1;
1973             p_box->data.p_sample_text->i_justification_vertical = -1;
1974             break;
1975         case -2:    // Flush Left
1976             p_box->data.p_sample_text->i_justification_horizontal = 0;
1977             p_box->data.p_sample_text->i_justification_vertical = 0;
1978             break;
1979         case 0: // Flush Default
1980         default:
1981             p_box->data.p_sample_text->i_justification_horizontal = 1;
1982             p_box->data.p_sample_text->i_justification_vertical = -1;
1983             break;
1984     }
1985
1986     MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[0] );
1987     MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[1] );
1988     MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[2] );
1989     p_box->data.p_sample_text->i_background_color[3] = 0xFF;
1990
1991     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
1992     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
1993     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
1994     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
1995
1996 #ifdef MP4_VERBOSE
1997     msg_Dbg( p_stream, "read box: \"text\" in stsd text" );
1998 #endif
1999     MP4_READBOX_EXIT( 1 );
2000 }
2001
2002 static int MP4_ReadBox_sample_tx3g( stream_t *p_stream, MP4_Box_t *p_box )
2003 {
2004     p_box->i_handler = ATOM_text;
2005     MP4_READBOX_ENTER( MP4_Box_data_sample_text_t );
2006
2007     MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2008     MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2009
2010     MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2011
2012     MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2013
2014     MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_horizontal );
2015     MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_vertical );
2016
2017     MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[0] );
2018     MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[1] );
2019     MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[2] );
2020     MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[3] );
2021
2022     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2023     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2024     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2025     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2026
2027     MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved3 );
2028
2029     MP4_GET2BYTES( p_box->data.p_sample_text->i_font_id );
2030     MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_face );
2031     MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_size );
2032     MP4_GET4BYTES( p_box->data.p_sample_text->i_font_color );
2033
2034 #ifdef MP4_VERBOSE
2035     msg_Dbg( p_stream, "read box: \"tx3g\" in stsd text" );
2036 #endif
2037     MP4_READBOX_EXIT( 1 );
2038 }
2039
2040
2041 #if 0
2042 /* We can't easily call it, and anyway ~ 20 bytes lost isn't a real problem */
2043 static void MP4_FreeBox_sample_text( MP4_Box_t *p_box )
2044 {
2045     FREENULL( p_box->data.p_sample_text->psz_text_name );
2046 }
2047 #endif
2048
2049
2050 static int MP4_ReadBox_stsd( stream_t *p_stream, MP4_Box_t *p_box )
2051 {
2052
2053     MP4_READBOX_ENTER( MP4_Box_data_stsd_t );
2054
2055     MP4_GETVERSIONFLAGS( p_box->data.p_stsd );
2056
2057     MP4_GET4BYTES( p_box->data.p_stsd->i_entry_count );
2058
2059     stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
2060
2061     MP4_ReadBoxContainerRaw( p_stream, p_box );
2062
2063 #ifdef MP4_VERBOSE
2064     msg_Dbg( p_stream, "read box: \"stsd\" entry-count %d",
2065                       p_box->data.p_stsd->i_entry_count );
2066
2067 #endif
2068     MP4_READBOX_EXIT( 1 );
2069 }
2070
2071
2072 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
2073 {
2074     MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
2075
2076     MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
2077
2078     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
2079     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
2080
2081     if( p_box->data.p_stsz->i_sample_size == 0 )
2082     {
2083         p_box->data.p_stsz->i_entry_size =
2084             calloc( p_box->data.p_stsz->i_sample_count, sizeof(uint32_t) );
2085         if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
2086             MP4_READBOX_EXIT( 0 );
2087
2088         for( unsigned int i = 0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
2089         {
2090             MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
2091         }
2092     }
2093     else
2094         p_box->data.p_stsz->i_entry_size = NULL;
2095
2096 #ifdef MP4_VERBOSE
2097     msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
2098                       p_box->data.p_stsz->i_sample_size,
2099                       p_box->data.p_stsz->i_sample_count );
2100
2101 #endif
2102     MP4_READBOX_EXIT( 1 );
2103 }
2104
2105 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
2106 {
2107     FREENULL( p_box->data.p_stsz->i_entry_size );
2108 }
2109
2110 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
2111 {
2112     FREENULL( p_box->data.p_stsc->i_first_chunk );
2113     FREENULL( p_box->data.p_stsc->i_samples_per_chunk );
2114     FREENULL( p_box->data.p_stsc->i_sample_description_index );
2115 }
2116
2117 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
2118 {
2119     MP4_READBOX_ENTER( MP4_Box_data_stsc_t );
2120
2121     MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
2122
2123     MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
2124
2125     p_box->data.p_stsc->i_first_chunk =
2126         calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2127     p_box->data.p_stsc->i_samples_per_chunk =
2128         calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2129     p_box->data.p_stsc->i_sample_description_index =
2130         calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2131     if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
2132      || p_box->data.p_stsc->i_samples_per_chunk == NULL
2133      || p_box->data.p_stsc->i_sample_description_index == NULL ) )
2134     {
2135         MP4_READBOX_EXIT( 0 );
2136     }
2137
2138     for( unsigned int i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
2139     {
2140         MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
2141         MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
2142         MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
2143     }
2144
2145 #ifdef MP4_VERBOSE
2146     msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
2147                       p_box->data.p_stsc->i_entry_count );
2148
2149 #endif
2150     MP4_READBOX_EXIT( 1 );
2151 }
2152
2153 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
2154 {
2155     MP4_READBOX_ENTER( MP4_Box_data_co64_t );
2156
2157     MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
2158
2159     MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
2160
2161     p_box->data.p_co64->i_chunk_offset =
2162         calloc( p_box->data.p_co64->i_entry_count, sizeof(uint64_t) );
2163     if( p_box->data.p_co64->i_chunk_offset == NULL )
2164         MP4_READBOX_EXIT( 0 );
2165
2166     for( unsigned int i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
2167     {
2168         if( p_box->i_type == ATOM_stco )
2169         {
2170             if( i_read < 4 )
2171             {
2172                 break;
2173             }
2174             MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2175         }
2176         else
2177         {
2178             if( i_read < 8 )
2179             {
2180                 break;
2181             }
2182             MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2183         }
2184     }
2185
2186 #ifdef MP4_VERBOSE
2187     msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
2188                       p_box->data.p_co64->i_entry_count );
2189
2190 #endif
2191     MP4_READBOX_EXIT( 1 );
2192 }
2193
2194 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
2195 {
2196     FREENULL( p_box->data.p_co64->i_chunk_offset );
2197 }
2198
2199 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
2200 {
2201     MP4_READBOX_ENTER( MP4_Box_data_stss_t );
2202
2203     MP4_GETVERSIONFLAGS( p_box->data.p_stss );
2204
2205     MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
2206
2207     p_box->data.p_stss->i_sample_number =
2208         calloc( p_box->data.p_stss->i_entry_count, sizeof(uint32_t) );
2209     if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
2210         MP4_READBOX_EXIT( 0 );
2211
2212     unsigned int i;
2213     for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
2214     {
2215
2216         MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
2217         /* XXX in libmp4 sample begin at 0 */
2218         p_box->data.p_stss->i_sample_number[i]--;
2219     }
2220     if ( i < p_box->data.p_stss->i_entry_count )
2221         p_box->data.p_stss->i_entry_count = i;
2222
2223 #ifdef MP4_VERBOSE
2224     msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
2225                       p_box->data.p_stss->i_entry_count );
2226
2227 #endif
2228     MP4_READBOX_EXIT( 1 );
2229 }
2230
2231 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
2232 {
2233     FREENULL( p_box->data.p_stss->i_sample_number );
2234 }
2235
2236 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
2237 {
2238     FREENULL( p_box->data.p_stsh->i_shadowed_sample_number );
2239     FREENULL( p_box->data.p_stsh->i_sync_sample_number );
2240 }
2241
2242 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
2243 {
2244     MP4_READBOX_ENTER( MP4_Box_data_stsh_t );
2245
2246     MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
2247
2248
2249     MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
2250
2251     p_box->data.p_stsh->i_shadowed_sample_number =
2252         calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2253     p_box->data.p_stsh->i_sync_sample_number =
2254         calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2255
2256     if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
2257      || p_box->data.p_stsh->i_sync_sample_number == NULL )
2258     {
2259         MP4_READBOX_EXIT( 0 );
2260     }
2261
2262     unsigned i;
2263     for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
2264     {
2265         MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
2266         MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
2267     }
2268     if ( i < p_box->data.p_stss->i_entry_count )
2269         p_box->data.p_stss->i_entry_count = i;
2270
2271 #ifdef MP4_VERBOSE
2272     msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
2273                       p_box->data.p_stsh->i_entry_count );
2274 #endif
2275     MP4_READBOX_EXIT( 1 );
2276 }
2277
2278
2279 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
2280 {
2281     MP4_READBOX_ENTER( MP4_Box_data_stdp_t );
2282
2283     MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
2284
2285     p_box->data.p_stdp->i_priority =
2286         calloc( i_read / 2, sizeof(uint16_t) );
2287
2288     if( unlikely( !p_box->data.p_stdp->i_priority ) )
2289         MP4_READBOX_EXIT( 0 );
2290
2291     for( unsigned i = 0; i < i_read / 2 ; i++ )
2292     {
2293         MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
2294     }
2295
2296 #ifdef MP4_VERBOSE
2297     msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
2298                       i_read / 2 );
2299
2300 #endif
2301     MP4_READBOX_EXIT( 1 );
2302 }
2303
2304 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
2305 {
2306     FREENULL( p_box->data.p_stdp->i_priority );
2307 }
2308
2309 static void MP4_FreeBox_padb( MP4_Box_t *p_box )
2310 {
2311     FREENULL( p_box->data.p_padb->i_reserved1 );
2312     FREENULL( p_box->data.p_padb->i_pad2 );
2313     FREENULL( p_box->data.p_padb->i_reserved2 );
2314     FREENULL( p_box->data.p_padb->i_pad1 );
2315 }
2316
2317 static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
2318 {
2319     uint32_t count;
2320
2321     MP4_READBOX_ENTER( MP4_Box_data_padb_t );
2322
2323     MP4_GETVERSIONFLAGS( p_box->data.p_padb );
2324
2325     MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
2326     count = (p_box->data.p_padb->i_sample_count + 1) / 2;
2327
2328     p_box->data.p_padb->i_reserved1 = calloc( count, sizeof(uint16_t) );
2329     p_box->data.p_padb->i_pad2 = calloc( count, sizeof(uint16_t) );
2330     p_box->data.p_padb->i_reserved2 = calloc( count, sizeof(uint16_t) );
2331     p_box->data.p_padb->i_pad1 = calloc( count, sizeof(uint16_t) );
2332     if( p_box->data.p_padb->i_reserved1 == NULL
2333      || p_box->data.p_padb->i_pad2 == NULL
2334      || p_box->data.p_padb->i_reserved2 == NULL
2335      || p_box->data.p_padb->i_pad1 == NULL )
2336     {
2337         MP4_READBOX_EXIT( 0 );
2338     }
2339
2340     for( unsigned int i = 0; i < i_read / 2 ; i++ )
2341     {
2342         if( i >= count )
2343         {
2344             MP4_READBOX_EXIT( 0 );
2345         }
2346         p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
2347         p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
2348         p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
2349         p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;
2350
2351         p_peek += 1; i_read -= 1;
2352     }
2353
2354 #ifdef MP4_VERBOSE
2355     msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
2356                       i_read / 2 );
2357
2358 #endif
2359     MP4_READBOX_EXIT( 1 );
2360 }
2361
2362 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
2363 {
2364     FREENULL( p_box->data.p_elst->i_segment_duration );
2365     FREENULL( p_box->data.p_elst->i_media_time );
2366     FREENULL( p_box->data.p_elst->i_media_rate_integer );
2367     FREENULL( p_box->data.p_elst->i_media_rate_fraction );
2368 }
2369
2370 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
2371 {
2372     MP4_READBOX_ENTER( MP4_Box_data_elst_t );
2373
2374     MP4_GETVERSIONFLAGS( p_box->data.p_elst );
2375
2376
2377     MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
2378
2379     p_box->data.p_elst->i_segment_duration =
2380         calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
2381     p_box->data.p_elst->i_media_time =
2382         calloc( p_box->data.p_elst->i_entry_count, sizeof(int64_t) );
2383     p_box->data.p_elst->i_media_rate_integer =
2384         calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
2385     p_box->data.p_elst->i_media_rate_fraction =
2386         calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
2387     if( p_box->data.p_elst->i_segment_duration == NULL
2388      || p_box->data.p_elst->i_media_time == NULL
2389      || p_box->data.p_elst->i_media_rate_integer == NULL
2390      || p_box->data.p_elst->i_media_rate_fraction == NULL )
2391     {
2392         MP4_READBOX_EXIT( 0 );
2393     }
2394
2395     unsigned i;
2396     for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
2397     {
2398         if( p_box->data.p_elst->i_version == 1 )
2399         {
2400             if ( i_read < 20 )
2401                 break;
2402             MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
2403
2404             MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
2405         }
2406         else
2407         {
2408             if ( i_read < 12 )
2409                 break;
2410             MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
2411
2412             MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
2413             p_box->data.p_elst->i_media_time[i] = (int32_t)p_box->data.p_elst->i_media_time[i];
2414         }
2415
2416         MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
2417         MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
2418     }
2419     if ( i < p_box->data.p_elst->i_entry_count )
2420         p_box->data.p_elst->i_entry_count = i;
2421 #ifdef MP4_VERBOSE
2422     msg_Dbg( p_stream, "read box: \"elst\" entry-count %lu",
2423              (unsigned long)p_box->data.p_elst->i_entry_count );
2424 #endif
2425     MP4_READBOX_EXIT( 1 );
2426 }
2427
2428 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
2429 {
2430     uint16_t i_language;
2431     bool b_mac;
2432
2433     MP4_READBOX_ENTER( MP4_Box_data_cprt_t );
2434
2435     MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
2436
2437     MP4_GET2BYTES( i_language );
2438     decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
2439
2440     MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
2441
2442 #ifdef MP4_VERBOSE
2443     msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
2444                       p_box->data.p_cprt->rgs_language,
2445                       p_box->data.p_cprt->psz_notice );
2446
2447 #endif
2448     MP4_READBOX_EXIT( 1 );
2449 }
2450
2451 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
2452 {
2453     FREENULL( p_box->data.p_cprt->psz_notice );
2454 }
2455
2456
2457 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
2458 {
2459     MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
2460
2461     MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
2462 #ifdef MP4_VERBOSE
2463     msg_Dbg( p_stream,
2464              "read box: \"dcom\" compression algorithm : %4.4s",
2465                       (char*)&p_box->data.p_dcom->i_algorithm );
2466 #endif
2467     MP4_READBOX_EXIT( 1 );
2468 }
2469
2470 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
2471 {
2472     MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );
2473
2474     MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
2475
2476     p_box->data.p_cmvd->i_compressed_size = i_read;
2477
2478     if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
2479         MP4_READBOX_EXIT( 0 );
2480
2481     /* now copy compressed data */
2482     memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
2483
2484     p_box->data.p_cmvd->b_compressed = 1;
2485
2486 #ifdef MP4_VERBOSE
2487     msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
2488                       p_box->data.p_cmvd->i_compressed_size );
2489 #endif
2490
2491     MP4_READBOX_EXIT( 1 );
2492 }
2493 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
2494 {
2495     FREENULL( p_box->data.p_cmvd->p_data );
2496 }
2497
2498
2499 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
2500 {
2501     MP4_Box_t *p_dcom;
2502     MP4_Box_t *p_cmvd;
2503
2504 #ifdef HAVE_ZLIB_H
2505     stream_t *p_stream_memory;
2506     z_stream z_data;
2507     uint8_t *p_data;
2508     int i_result;
2509 #endif
2510
2511     if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
2512         return 0;
2513
2514     if( !p_box->p_father ||
2515         ( p_box->p_father->i_type != ATOM_moov &&
2516           p_box->p_father->i_type != ATOM_foov ) )
2517     {
2518         msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
2519         return 1;
2520     }
2521
2522     if( !MP4_ReadBoxContainer( p_stream, p_box ) )
2523     {
2524         return 0;
2525     }
2526
2527     if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
2528         ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
2529         p_cmvd->data.p_cmvd->p_data == NULL )
2530     {
2531         msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
2532         return 0;
2533     }
2534
2535     if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
2536     {
2537         msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
2538                  "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
2539         return 0;
2540     }
2541
2542 #ifndef HAVE_ZLIB_H
2543     msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
2544     return 0;
2545
2546 #else
2547     /* decompress data */
2548     /* allocate a new buffer */
2549     if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
2550         return 0;
2551     /* init default structures */
2552     z_data.next_in   = p_cmvd->data.p_cmvd->p_data;
2553     z_data.avail_in  = p_cmvd->data.p_cmvd->i_compressed_size;
2554     z_data.next_out  = p_data;
2555     z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
2556     z_data.zalloc    = (alloc_func)Z_NULL;
2557     z_data.zfree     = (free_func)Z_NULL;
2558     z_data.opaque    = (voidpf)Z_NULL;
2559
2560     /* init zlib */
2561     if( inflateInit( &z_data ) != Z_OK )
2562     {
2563         msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
2564         free( p_data );
2565         return 0;
2566     }
2567
2568     /* uncompress */
2569     i_result = inflate( &z_data, Z_NO_FLUSH );
2570     if( i_result != Z_OK && i_result != Z_STREAM_END )
2571     {
2572         msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
2573         free( p_data );
2574         return 0;
2575     }
2576
2577     if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
2578     {
2579         msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
2580                   "mismatch" );
2581     }
2582     p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
2583
2584     /* close zlib */
2585     if( inflateEnd( &z_data ) != Z_OK )
2586     {
2587         msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
2588                   "data (ignored)" );
2589     }
2590
2591     free( p_cmvd->data.p_cmvd->p_data );
2592     p_cmvd->data.p_cmvd->p_data = p_data;
2593     p_cmvd->data.p_cmvd->b_compressed = 0;
2594
2595     msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
2596
2597     /* now create a memory stream */
2598     p_stream_memory =
2599         stream_MemoryNew( VLC_OBJECT(p_stream), p_cmvd->data.p_cmvd->p_data,
2600                           p_cmvd->data.p_cmvd->i_uncompressed_size, true );
2601
2602     /* and read uncompressd moov */
2603     p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
2604
2605     stream_Delete( p_stream_memory );
2606
2607 #ifdef MP4_VERBOSE
2608     msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
2609 #endif
2610
2611     return p_box->data.p_cmov->p_moov ? 1 : 0;
2612 #endif /* HAVE_ZLIB_H */
2613 }
2614
2615 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
2616 {
2617     uint32_t i_len;
2618     MP4_READBOX_ENTER( MP4_Box_data_rdrf_t );
2619
2620     MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
2621     MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
2622     MP4_GET4BYTES( i_len );
2623     i_len++;
2624
2625     if( i_len > 0 )
2626     {
2627         p_box->data.p_rdrf->psz_ref = malloc( i_len );
2628         if( p_box->data.p_rdrf->psz_ref == NULL )
2629             MP4_READBOX_EXIT( 0 );
2630         i_len--;
2631
2632         for( unsigned i = 0; i < i_len; i++ )
2633         {
2634             MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
2635         }
2636         p_box->data.p_rdrf->psz_ref[i_len] = '\0';
2637     }
2638     else
2639     {
2640         p_box->data.p_rdrf->psz_ref = NULL;
2641     }
2642
2643 #ifdef MP4_VERBOSE
2644     msg_Dbg( p_stream,
2645             "read box: \"rdrf\" type:%4.4s ref %s",
2646             (char*)&p_box->data.p_rdrf->i_ref_type,
2647             p_box->data.p_rdrf->psz_ref );
2648 #endif
2649     MP4_READBOX_EXIT( 1 );
2650 }
2651
2652 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
2653 {
2654     FREENULL( p_box->data.p_rdrf->psz_ref );
2655 }
2656
2657
2658 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
2659 {
2660     MP4_READBOX_ENTER( MP4_Box_data_rmdr_t );
2661
2662     MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
2663
2664     MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
2665
2666 #ifdef MP4_VERBOSE
2667     msg_Dbg( p_stream,
2668              "read box: \"rmdr\" rate:%d",
2669              p_box->data.p_rmdr->i_rate );
2670 #endif
2671     MP4_READBOX_EXIT( 1 );
2672 }
2673
2674 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
2675 {
2676     MP4_READBOX_ENTER( MP4_Box_data_rmqu_t );
2677
2678     MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
2679
2680 #ifdef MP4_VERBOSE
2681     msg_Dbg( p_stream,
2682              "read box: \"rmqu\" quality:%d",
2683              p_box->data.p_rmqu->i_quality );
2684 #endif
2685     MP4_READBOX_EXIT( 1 );
2686 }
2687
2688 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
2689 {
2690     MP4_READBOX_ENTER( MP4_Box_data_rmvc_t );
2691     MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
2692
2693     MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
2694     MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
2695     MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
2696     MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
2697
2698 #ifdef MP4_VERBOSE
2699     msg_Dbg( p_stream,
2700              "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
2701              (char*)&p_box->data.p_rmvc->i_gestaltType,
2702              p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
2703              p_box->data.p_rmvc->i_checkType );
2704 #endif
2705
2706     MP4_READBOX_EXIT( 1 );
2707 }
2708
2709 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
2710 {
2711     MP4_READBOX_ENTER( MP4_Box_data_frma_t );
2712
2713     MP4_GETFOURCC( p_box->data.p_frma->i_type );
2714
2715 #ifdef MP4_VERBOSE
2716     msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
2717              (char *)&p_box->data.p_frma->i_type );
2718 #endif
2719
2720     MP4_READBOX_EXIT( 1 );
2721 }
2722
2723 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
2724 {
2725     MP4_READBOX_ENTER( MP4_Box_data_skcr_t );
2726
2727     MP4_GET4BYTES( p_box->data.p_skcr->i_init );
2728     MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
2729     MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
2730
2731 #ifdef MP4_VERBOSE
2732     msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
2733              p_box->data.p_skcr->i_init,
2734              p_box->data.p_skcr->i_encr,
2735              p_box->data.p_skcr->i_decr );
2736 #endif
2737
2738     MP4_READBOX_EXIT( 1 );
2739 }
2740
2741 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
2742 {
2743     VLC_UNUSED(p_box);
2744     /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
2745      * so unless data decrypt itself by magic, there will be no playback,
2746      * but we never know... */
2747     msg_Warn( p_stream, "DRM protected streams are not supported." );
2748     return 1;
2749 }
2750
2751 static int MP4_ReadBox_name( stream_t *p_stream, MP4_Box_t *p_box )
2752 {
2753     MP4_READBOX_ENTER( MP4_Box_data_name_t );
2754
2755     p_box->data.p_name->psz_text = malloc( p_box->i_size + 1 - 8 ); /* +\0, -name, -size */
2756     if( p_box->data.p_name->psz_text == NULL )
2757         MP4_READBOX_EXIT( 0 );
2758
2759     memcpy( p_box->data.p_name->psz_text, p_peek, p_box->i_size - 8 );
2760     p_box->data.p_name->psz_text[p_box->i_size - 8] = '\0';
2761
2762 #ifdef MP4_VERBOSE
2763         msg_Dbg( p_stream, "read box: \"name\" text=`%s'",
2764                  p_box->data.p_name->psz_text );
2765 #endif
2766     MP4_READBOX_EXIT( 1 );
2767 }
2768
2769 static void MP4_FreeBox_name( MP4_Box_t *p_box )
2770 {
2771     FREENULL( p_box->data.p_name->psz_text );
2772 }
2773
2774 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
2775 {
2776     MP4_READBOX_ENTER( MP4_Box_data_data_t );
2777
2778     /* What's that header ?? */
2779     if ( i_read <= 8 )
2780         MP4_READBOX_EXIT( 0 );
2781
2782     p_box->data.p_data->p_blob = malloc( i_read - 8 );
2783     if ( !p_box->data.p_data->p_blob )
2784         MP4_READBOX_EXIT( 0 );
2785
2786     p_box->data.p_data->i_blob = i_read - 8;
2787     memcpy( p_box->data.p_data->p_blob, p_peek + 8, i_read - 8 );
2788
2789     MP4_READBOX_EXIT( 1 );
2790 }
2791
2792 static void MP4_FreeBox_data( MP4_Box_t *p_box )
2793 {
2794     free( p_box->data.p_data->p_blob );
2795 }
2796
2797 static int MP4_ReadBox_0xa9xxx( stream_t *p_stream, MP4_Box_t *p_box )
2798 {
2799     uint16_t i16;
2800
2801     MP4_READBOX_ENTER( MP4_Box_data_0xa9xxx_t );
2802
2803     p_box->data.p_0xa9xxx->psz_text = NULL;
2804
2805     MP4_GET2BYTES( i16 );
2806
2807     if( i16 > 0 )
2808     {
2809         int i_length = i16;
2810
2811         MP4_GET2BYTES( i16 );
2812         if( i_length >= i_read ) i_length = i_read + 1;
2813
2814         p_box->data.p_0xa9xxx->psz_text = malloc( i_length );
2815         if( p_box->data.p_0xa9xxx->psz_text == NULL )
2816             MP4_READBOX_EXIT( 0 );
2817
2818         i_length--;
2819         memcpy( p_box->data.p_0xa9xxx->psz_text,
2820                 p_peek, i_length );
2821         p_box->data.p_0xa9xxx->psz_text[i_length] = '\0';
2822
2823 #ifdef MP4_VERBOSE
2824         msg_Dbg( p_stream,
2825                  "read box: \"c%3.3s\" text=`%s'",
2826                  ((char*)&p_box->i_type + 1),
2827                  p_box->data.p_0xa9xxx->psz_text );
2828 #endif
2829     }
2830     else
2831     {
2832         /* try iTune/Quicktime format, rewind to start */
2833         p_peek -= 2; i_read += 2;
2834         // we are expecting a 'data' box
2835         uint32_t i_data_len;
2836         uint32_t i_data_tag;
2837
2838         MP4_GET4BYTES( i_data_len );
2839         if( i_data_len > i_read ) i_data_len = i_read;
2840         MP4_GETFOURCC( i_data_tag );
2841         if( (i_data_len > 0) && (i_data_tag == ATOM_data) )
2842         {
2843             /* data box contains a version/flags field */
2844             uint32_t i_version;
2845             uint32_t i_reserved;
2846             VLC_UNUSED(i_reserved);
2847             MP4_GET4BYTES( i_version );
2848             MP4_GET4BYTES( i_reserved );
2849             // version should be 0, flags should be 1 for text, 0 for data
2850             if( ( i_version == 0x00000001 ) && (i_data_len >= 12 ) )
2851             {
2852                 // the rest is the text
2853                 i_data_len -= 12;
2854                 p_box->data.p_0xa9xxx->psz_text = malloc( i_data_len + 1 );
2855                 if( p_box->data.p_0xa9xxx->psz_text == NULL )
2856                     MP4_READBOX_EXIT( 0 );
2857
2858                 memcpy( p_box->data.p_0xa9xxx->psz_text,
2859                         p_peek, i_data_len );
2860                 p_box->data.p_0xa9xxx->psz_text[i_data_len] = '\0';
2861 #ifdef MP4_VERBOSE
2862         msg_Dbg( p_stream,
2863                  "read box: \"c%3.3s\" text=`%s'",
2864                  ((char*)&p_box->i_type+1),
2865                  p_box->data.p_0xa9xxx->psz_text );
2866 #endif
2867             }
2868             else
2869             {
2870                 // TODO: handle data values for ID3 tag values, track num or cover art,etc...
2871             }
2872         }
2873     }
2874
2875     MP4_READBOX_EXIT( 1 );
2876 }
2877 static void MP4_FreeBox_0xa9xxx( MP4_Box_t *p_box )
2878 {
2879     FREENULL( p_box->data.p_0xa9xxx->psz_text );
2880 }
2881
2882 /* Chapter support */
2883 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
2884 {
2885     MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
2886     for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
2887         free( p_chpl->chapter[i].psz_name );
2888 }
2889
2890 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
2891 {
2892     MP4_Box_data_chpl_t *p_chpl;
2893     uint32_t i_dummy;
2894     VLC_UNUSED(i_dummy);
2895     int i;
2896     MP4_READBOX_ENTER( MP4_Box_data_chpl_t );
2897
2898     p_chpl = p_box->data.p_chpl;
2899
2900     MP4_GETVERSIONFLAGS( p_chpl );
2901
2902     MP4_GET4BYTES( i_dummy );
2903
2904     MP4_GET1BYTE( p_chpl->i_chapter );
2905
2906     for( i = 0; i < p_chpl->i_chapter; i++ )
2907     {
2908         uint64_t i_start;
2909         uint8_t i_len;
2910         int i_copy;
2911         MP4_GET8BYTES( i_start );
2912         MP4_GET1BYTE( i_len );
2913
2914         p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
2915         if( !p_chpl->chapter[i].psz_name )
2916             MP4_READBOX_EXIT( 0 );
2917
2918         i_copy = __MIN( i_len, i_read );
2919         if( i_copy > 0 )
2920             memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
2921         p_chpl->chapter[i].psz_name[i_copy] = '\0';
2922         p_chpl->chapter[i].i_start = i_start;
2923
2924         p_peek += i_copy;
2925         i_read -= i_copy;
2926     }
2927     /* Bubble sort by increasing start date */
2928     do
2929     {
2930         for( i = 0; i < p_chpl->i_chapter - 1; i++ )
2931         {
2932             if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
2933             {
2934                 char *psz = p_chpl->chapter[i+1].psz_name;
2935                 int64_t i64 = p_chpl->chapter[i+1].i_start;
2936
2937                 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
2938                 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
2939
2940                 p_chpl->chapter[i].psz_name = psz;
2941                 p_chpl->chapter[i].i_start = i64;
2942
2943                 i = -1;
2944                 break;
2945             }
2946         }
2947     } while( i == -1 );
2948
2949 #ifdef MP4_VERBOSE
2950     msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
2951                        p_chpl->i_chapter );
2952 #endif
2953     MP4_READBOX_EXIT( 1 );
2954 }
2955
2956 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
2957 {
2958     MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t );
2959
2960     p_box->data.p_tref_generic->i_track_ID = NULL;
2961     p_box->data.p_tref_generic->i_entry_count = i_read / sizeof(uint32_t);
2962     if( p_box->data.p_tref_generic->i_entry_count > 0 )
2963         p_box->data.p_tref_generic->i_track_ID = calloc( p_box->data.p_tref_generic->i_entry_count, sizeof(uint32_t) );
2964     if( p_box->data.p_tref_generic->i_track_ID == NULL )
2965         MP4_READBOX_EXIT( 0 );
2966
2967     for( unsigned i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
2968     {
2969         MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
2970     }
2971 #ifdef MP4_VERBOSE
2972         msg_Dbg( p_stream, "read box: \"chap\" %d references",
2973                  p_box->data.p_tref_generic->i_entry_count );
2974 #endif
2975
2976     MP4_READBOX_EXIT( 1 );
2977 }
2978 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
2979 {
2980     FREENULL( p_box->data.p_tref_generic->i_track_ID );
2981 }
2982
2983 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
2984 {
2985     uint8_t meta_data[8];
2986     int i_actually_read;
2987
2988     // skip over box header
2989     i_actually_read = stream_Read( p_stream, meta_data, 8 );
2990     if( i_actually_read < 8 )
2991         return 0;
2992
2993     if ( !p_box->p_father )
2994         return 0;
2995
2996     switch( p_box->p_father->i_type )
2997     {
2998     case ATOM_udta: /* itunes udta/meta */
2999         /* meta content starts with a 4 byte version/flags value (should be 0) */
3000         i_actually_read = stream_Read( p_stream, meta_data, 4 );
3001         if( i_actually_read < 4 )
3002             return 0;
3003
3004         /* then it behaves like a container */
3005         return MP4_ReadBoxContainerRaw( p_stream, p_box );
3006
3007     default: /* regular meta atom */
3008
3009         i_actually_read = stream_Read( p_stream, meta_data, 8 );
3010         if( i_actually_read < 8 )
3011             return 0;
3012
3013         /* Mandatory */
3014         if ( VLC_FOURCC( meta_data[4], meta_data[5], meta_data[6], meta_data[7] ) != ATOM_hdlr )
3015             return 0;
3016
3017         //ft
3018     }
3019     return 1;
3020 }
3021
3022 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3023 {
3024     char i_unused;
3025     VLC_UNUSED(i_unused);
3026
3027     MP4_READBOX_ENTER( MP4_Box_data_iods_t );
3028     MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3029
3030     MP4_GET1BYTE( i_unused ); /* tag */
3031     MP4_GET1BYTE( i_unused ); /* length */
3032
3033     MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3034                                                               are used for other flags */
3035     MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3036     MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3037     MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3038     MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3039     MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3040
3041 #ifdef MP4_VERBOSE
3042     msg_Dbg( p_stream,
3043              "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3044              "visual: %i, graphics: %i",
3045              p_box->data.p_iods->i_object_descriptor >> 6,
3046              p_box->data.p_iods->i_OD_profile_level,
3047              p_box->data.p_iods->i_scene_profile_level,
3048              p_box->data.p_iods->i_audio_profile_level,
3049              p_box->data.p_iods->i_visual_profile_level,
3050              p_box->data.p_iods->i_graphics_profile_level );
3051 #endif
3052
3053     MP4_READBOX_EXIT( 1 );
3054 }
3055
3056 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3057 {
3058     MP4_READBOX_ENTER( MP4_Box_data_pasp_t );
3059
3060     MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
3061     MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
3062
3063 #ifdef MP4_VERBOSE
3064     msg_Dbg( p_stream,
3065              "read box: \"paps\" %dx%d",
3066              p_box->data.p_pasp->i_horizontal_spacing,
3067              p_box->data.p_pasp->i_vertical_spacing);
3068 #endif
3069
3070     MP4_READBOX_EXIT( 1 );
3071 }
3072
3073 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
3074 {
3075     MP4_READBOX_ENTER( MP4_Box_data_mehd_t );
3076
3077     MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
3078     if( p_box->data.p_mehd->i_version == 1 )
3079         MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
3080     else /* version == 0 */
3081         MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
3082
3083 #ifdef MP4_VERBOSE
3084     msg_Dbg( p_stream,
3085              "read box: \"mehd\" frag dur. %"PRIu64"",
3086              p_box->data.p_mehd->i_fragment_duration );
3087 #endif
3088
3089     MP4_READBOX_EXIT( 1 );
3090 }
3091
3092 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
3093 {
3094     MP4_READBOX_ENTER( MP4_Box_data_trex_t );
3095     MP4_GETVERSIONFLAGS( p_box->data.p_trex );
3096
3097     MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
3098     MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
3099     MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
3100     MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
3101     MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
3102
3103 #ifdef MP4_VERBOSE
3104     msg_Dbg( p_stream,
3105              "read box: \"trex\" trackID: %"PRIu32"",
3106              p_box->data.p_trex->i_track_ID );
3107 #endif
3108
3109     MP4_READBOX_EXIT( 1 );
3110 }
3111
3112 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
3113 {
3114     uint32_t i_sample_count;
3115     MP4_READBOX_ENTER( MP4_Box_data_sdtp_t );
3116     MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
3117     MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
3118     i_sample_count = i_read;
3119
3120     p_sdtp->p_sample_table = calloc( i_sample_count, 1 );
3121
3122     if( !p_sdtp->p_sample_table )
3123         MP4_READBOX_EXIT( 0 );
3124
3125     for( uint32_t i = 0; i < i_sample_count; i++ )
3126         MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
3127
3128 #ifdef MP4_VERBOSE
3129     msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
3130     msg_Dbg( p_stream,
3131              "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
3132                  p_sdtp->p_sample_table[0],
3133                  p_sdtp->p_sample_table[1],
3134                  p_sdtp->p_sample_table[2],
3135                  p_sdtp->p_sample_table[3] );
3136 #endif
3137
3138     MP4_READBOX_EXIT( 1 );
3139 }
3140
3141 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
3142 {
3143     FREENULL( p_box->data.p_sdtp->p_sample_table );
3144 }
3145
3146 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
3147 {
3148     MP4_READBOX_ENTER( MP4_Box_data_mfro_t );
3149
3150     MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
3151     MP4_GET4BYTES( p_box->data.p_mfro->i_size );
3152
3153 #ifdef MP4_VERBOSE
3154     msg_Dbg( p_stream,
3155              "read box: \"mfro\" size: %"PRIu32"",
3156              p_box->data.p_mfro->i_size);
3157 #endif
3158
3159     MP4_READBOX_EXIT( 1 );
3160 }
3161
3162 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
3163 {
3164     uint32_t i_number_of_entries;
3165     MP4_READBOX_ENTER( MP4_Box_data_tfra_t );
3166     MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
3167     MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
3168
3169     MP4_GET4BYTES( p_tfra->i_track_ID );
3170     uint32_t i_lengths = 0;
3171     MP4_GET4BYTES( i_lengths );
3172     MP4_GET4BYTES( p_tfra->i_number_of_entries );
3173     i_number_of_entries = p_tfra->i_number_of_entries;
3174     p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
3175     p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
3176     p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
3177
3178     size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
3179     p_tfra->p_time = calloc( i_number_of_entries, size );
3180     p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
3181
3182     size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
3183     p_tfra->p_traf_number = calloc( i_number_of_entries, size );
3184     size = 1 + p_tfra->i_length_size_of_trun_num;
3185     p_tfra->p_trun_number = calloc( i_number_of_entries, size );
3186     size = 1 + p_tfra->i_length_size_of_sample_num;
3187     p_tfra->p_sample_number = calloc( i_number_of_entries, size );
3188
3189     if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
3190                         || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
3191         goto error;
3192
3193     int i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
3194             + p_tfra->i_length_size_of_trun_num
3195             + p_tfra->i_length_size_of_sample_num;
3196
3197     uint32_t i;
3198     for( i = 0; i < i_number_of_entries; i++ )
3199     {
3200
3201         if( p_tfra->i_version == 1 )
3202         {
3203             if ( i_read < i_fields_length + 16 )
3204                 break;
3205             MP4_GET8BYTES( p_tfra->p_time[i*2] );
3206             MP4_GET8BYTES( p_tfra->p_moof_offset[i*2] );
3207         }
3208         else
3209         {
3210             if ( i_read < i_fields_length + 8 )
3211                 break;
3212             MP4_GET4BYTES( p_tfra->p_time[i] );
3213             MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
3214         }
3215         switch (p_tfra->i_length_size_of_traf_num)
3216         {
3217             case 0:
3218                 MP4_GET1BYTE( p_tfra->p_traf_number[i] );
3219                 break;
3220             case 1:
3221                 MP4_GET2BYTES( p_tfra->p_traf_number[i*2] );
3222                 break;
3223             case 2:
3224                 MP4_GET3BYTES( p_tfra->p_traf_number[i*3] );
3225                 break;
3226             case 3:
3227                 MP4_GET4BYTES( p_tfra->p_traf_number[i*4] );
3228                 break;
3229             default:
3230                 goto error;
3231         }
3232
3233         switch (p_tfra->i_length_size_of_trun_num)
3234         {
3235             case 0:
3236                 MP4_GET1BYTE( p_tfra->p_trun_number[i] );
3237                 break;
3238             case 1:
3239                 MP4_GET2BYTES( p_tfra->p_trun_number[i*2] );
3240                 break;
3241             case 2:
3242                 MP4_GET3BYTES( p_tfra->p_trun_number[i*3] );
3243                 break;
3244             case 3:
3245                 MP4_GET4BYTES( p_tfra->p_trun_number[i*4] );
3246                 break;
3247             default:
3248                 goto error;
3249         }
3250
3251         switch (p_tfra->i_length_size_of_sample_num)
3252         {
3253             case 0:
3254                 MP4_GET1BYTE( p_tfra->p_sample_number[i] );
3255                 break;
3256             case 1:
3257                 MP4_GET2BYTES( p_tfra->p_sample_number[i*2] );
3258                 break;
3259             case 2:
3260                 MP4_GET3BYTES( p_tfra->p_sample_number[i*3] );
3261                 break;
3262             case 3:
3263                 MP4_GET4BYTES( p_tfra->p_sample_number[i*4] );
3264                 break;
3265             default:
3266                 goto error;
3267         }
3268     }
3269     if ( i < i_number_of_entries )
3270         i_number_of_entries = i;
3271
3272 #ifdef MP4_VERBOSE
3273     if( p_tfra->i_version == 0 )
3274     {
3275         msg_Dbg( p_stream, "time[0]: %"PRIu32", moof_offset[0]: %"PRIx32"",
3276                          p_tfra->p_time[0], p_tfra->p_moof_offset[0] );
3277
3278         msg_Dbg( p_stream, "time[1]: %"PRIu32", moof_offset[1]: %"PRIx32"",
3279                          p_tfra->p_time[1], p_tfra->p_moof_offset[1] );
3280     }
3281     else
3282     {
3283         msg_Dbg( p_stream, "time[0]: %"PRIu64", moof_offset[0]: %"PRIx64"",
3284                 ((uint64_t *)(p_tfra->p_time))[0],
3285                 ((uint64_t *)(p_tfra->p_moof_offset))[0] );
3286
3287         msg_Dbg( p_stream, "time[1]: %"PRIu64", moof_offset[1]: %"PRIx64"",
3288                 ((uint64_t *)(p_tfra->p_time))[1],
3289                 ((uint64_t *)(p_tfra->p_moof_offset))[1] );
3290     }
3291
3292     msg_Dbg( p_stream, "number_of_entries is %"PRIu32"", i_number_of_entries );
3293     msg_Dbg( p_stream, "track ID is: %"PRIu32"", p_tfra->i_track_ID );
3294 #endif
3295
3296     MP4_READBOX_EXIT( 1 );
3297 error:
3298     MP4_READBOX_EXIT( 0 );
3299 }
3300
3301 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
3302 {
3303     FREENULL( p_box->data.p_tfra->p_time );
3304     FREENULL( p_box->data.p_tfra->p_moof_offset );
3305     FREENULL( p_box->data.p_tfra->p_traf_number );
3306     FREENULL( p_box->data.p_tfra->p_trun_number );
3307     FREENULL( p_box->data.p_tfra->p_sample_number );
3308 }
3309
3310
3311 /* For generic */
3312 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
3313 {
3314     if( !p_box->p_father )
3315     {
3316         goto unknown;
3317     }
3318     if( p_box->p_father->i_type == ATOM_stsd )
3319     {
3320         MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
3321         MP4_Box_t *p_hdlr;
3322
3323         if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
3324             (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
3325         {
3326             goto unknown;
3327         }
3328         switch( p_hdlr->data.p_hdlr->i_handler_type )
3329         {
3330             case ATOM_soun:
3331                 return MP4_ReadBox_sample_soun( p_stream, p_box );
3332             case ATOM_vide:
3333                 return MP4_ReadBox_sample_vide( p_stream, p_box );
3334             case ATOM_text:
3335                 return MP4_ReadBox_sample_text( p_stream, p_box );
3336             case ATOM_tx3g:
3337             case ATOM_sbtl:
3338                 return MP4_ReadBox_sample_tx3g( p_stream, p_box );
3339             default:
3340                 msg_Warn( p_stream,
3341                           "unknown handler type in stsd (incompletely loaded)" );
3342                 return 1;
3343         }
3344     }
3345
3346 unknown:
3347     if MP4_BOX_TYPE_ASCII()
3348         msg_Warn( p_stream,
3349                 "unknown box type %4.4s (incompletely loaded)",
3350                 (char*)&p_box->i_type );
3351     else
3352         msg_Warn( p_stream,
3353                 "unknown box type c%3.3s (incompletely loaded)",
3354                 (char*)&p_box->i_type+1 );
3355     p_box->e_flags |= BOX_FLAG_INCOMPLETE;
3356
3357     return 1;
3358 }
3359
3360 /**** ------------------------------------------------------------------- ****/
3361 /****                   "Higher level" Functions                          ****/
3362 /**** ------------------------------------------------------------------- ****/
3363
3364 static const struct
3365 {
3366     uint32_t i_type;
3367     int  (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
3368     void (*MP4_FreeBox_function )( MP4_Box_t *p_box );
3369     uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
3370 } MP4_Box_Function [] =
3371 {
3372     /* Containers */
3373     { ATOM_moov,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3374     { ATOM_foov,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3375     { ATOM_trak,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_moov },
3376     { ATOM_trak,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_foov },
3377     { ATOM_mdia,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_trak },
3378     { ATOM_moof,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3379     { ATOM_minf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_mdia },
3380     { ATOM_stbl,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_minf },
3381     { ATOM_dinf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_minf },
3382     { ATOM_dinf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_meta },
3383     { ATOM_edts,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_trak },
3384     { ATOM_udta,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3385     { ATOM_nmhd,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_minf },
3386     { ATOM_hnti,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_udta },
3387     { ATOM_rmra,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_moov },
3388     { ATOM_rmda,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_rmra },
3389     { ATOM_tref,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_trak },
3390     { ATOM_gmhd,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_minf },
3391     { ATOM_wave,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3392     { ATOM_ilst,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_meta },
3393     { ATOM_mvex,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_moov },
3394     { ATOM_mvex,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_ftyp },
3395
3396     /* specific box */
3397     { ATOM_ftyp,    MP4_ReadBox_ftyp,         MP4_FreeBox_ftyp, 0 },
3398     { ATOM_cmov,    MP4_ReadBox_cmov,         MP4_FreeBox_Common, 0 },
3399     { ATOM_mvhd,    MP4_ReadBox_mvhd,         MP4_FreeBox_Common, ATOM_moov },
3400     { ATOM_mvhd,    MP4_ReadBox_mvhd,         MP4_FreeBox_Common, ATOM_foov },
3401     { ATOM_tkhd,    MP4_ReadBox_tkhd,         MP4_FreeBox_Common, ATOM_trak },
3402     { ATOM_mdhd,    MP4_ReadBox_mdhd,         MP4_FreeBox_Common, ATOM_mdia },
3403     { ATOM_hdlr,    MP4_ReadBox_hdlr,         MP4_FreeBox_hdlr,   ATOM_mdia },
3404     { ATOM_hdlr,    MP4_ReadBox_hdlr,         MP4_FreeBox_hdlr,   ATOM_meta },
3405     { ATOM_hdlr,    MP4_ReadBox_hdlr,         MP4_FreeBox_hdlr,   ATOM_minf },
3406     { ATOM_vmhd,    MP4_ReadBox_vmhd,         MP4_FreeBox_Common, ATOM_minf },
3407     { ATOM_smhd,    MP4_ReadBox_smhd,         MP4_FreeBox_Common, ATOM_minf },
3408     { ATOM_hmhd,    MP4_ReadBox_hmhd,         MP4_FreeBox_Common, ATOM_minf },
3409     { ATOM_alis,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, ATOM_dref },
3410     { ATOM_url,     MP4_ReadBox_url,          MP4_FreeBox_url, 0 },
3411     { ATOM_urn,     MP4_ReadBox_urn,          MP4_FreeBox_urn, 0 },
3412     { ATOM_dref,    MP4_ReadBox_dref,         MP4_FreeBox_Common, 0 },
3413     { ATOM_stts,    MP4_ReadBox_stts,         MP4_FreeBox_stts,   ATOM_stbl },
3414     { ATOM_ctts,    MP4_ReadBox_ctts,         MP4_FreeBox_ctts,   ATOM_stbl },
3415     { ATOM_stsd,    MP4_ReadBox_stsd,         MP4_FreeBox_Common, ATOM_stbl },
3416     { ATOM_stsz,    MP4_ReadBox_stsz,         MP4_FreeBox_stsz,   ATOM_stbl },
3417     { ATOM_stsc,    MP4_ReadBox_stsc,         MP4_FreeBox_stsc,   ATOM_stbl },
3418     { ATOM_stco,    MP4_ReadBox_stco_co64,    MP4_FreeBox_stco_co64, ATOM_stbl },
3419     { ATOM_co64,    MP4_ReadBox_stco_co64,    MP4_FreeBox_stco_co64, ATOM_stbl },
3420     { ATOM_stss,    MP4_ReadBox_stss,         MP4_FreeBox_stss, ATOM_stbl },
3421     { ATOM_stsh,    MP4_ReadBox_stsh,         MP4_FreeBox_stsh, ATOM_stbl },
3422     { ATOM_stdp,    MP4_ReadBox_stdp,         MP4_FreeBox_stdp, 0 },
3423     { ATOM_padb,    MP4_ReadBox_padb,         MP4_FreeBox_padb, 0 },
3424     { ATOM_elst,    MP4_ReadBox_elst,         MP4_FreeBox_elst, ATOM_edts },
3425     { ATOM_cprt,    MP4_ReadBox_cprt,         MP4_FreeBox_cprt, 0 },
3426     { ATOM_esds,    MP4_ReadBox_esds,         MP4_FreeBox_esds, 0 },
3427     { ATOM_dcom,    MP4_ReadBox_dcom,         MP4_FreeBox_Common, 0 },
3428     { ATOM_cmvd,    MP4_ReadBox_cmvd,         MP4_FreeBox_cmvd, 0 },
3429     { ATOM_avcC,    MP4_ReadBox_avcC,         MP4_FreeBox_avcC, 0 },
3430     { ATOM_hvcC,    MP4_ReadBox_hvcC,         MP4_FreeBox_hvcC, 0 },
3431     { ATOM_dac3,    MP4_ReadBox_dac3,         MP4_FreeBox_Common, 0 },
3432     { ATOM_dec3,    MP4_ReadBox_dec3,         MP4_FreeBox_Common, 0 },
3433     { ATOM_dvc1,    MP4_ReadBox_dvc1,         MP4_FreeBox_Common, 0 },
3434     { ATOM_enda,    MP4_ReadBox_enda,         MP4_FreeBox_Common, 0 },
3435     { ATOM_iods,    MP4_ReadBox_iods,         MP4_FreeBox_Common, 0 },
3436     { ATOM_pasp,    MP4_ReadBox_pasp,         MP4_FreeBox_Common, 0 },
3437
3438     /* Nothing to do with this box */
3439     { ATOM_mdat,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3440     { ATOM_skip,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3441     { ATOM_free,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3442     { ATOM_wide,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3443     { ATOM_binm,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3444
3445     /* Subtitles */
3446     { ATOM_tx3g,    MP4_ReadBox_sample_tx3g,      MP4_FreeBox_Common, 0 },
3447     //{ ATOM_text,    MP4_ReadBox_sample_text,      MP4_FreeBox_Common, 0 },
3448
3449     /* for codecs */
3450     { ATOM_soun,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3451     { ATOM_ac3,     MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3452     { ATOM_eac3,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3453     { ATOM_lpcm,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3454     { ATOM_ms02,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3455     { ATOM_ms11,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3456     { ATOM_ms55,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3457     { ATOM__mp3,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3458     { ATOM_mp4a,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3459     { ATOM_twos,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3460     { ATOM_sowt,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3461     { ATOM_QDMC,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3462     { ATOM_QDM2,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3463     { ATOM_ima4,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3464     { ATOM_IMA4,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3465     { ATOM_dvi,     MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3466     { ATOM_alaw,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3467     { ATOM_ulaw,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3468     { ATOM_raw,     MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3469     { ATOM_MAC3,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3470     { ATOM_MAC6,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3471     { ATOM_Qclp,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3472     { ATOM_samr,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3473     { ATOM_sawb,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3474     { ATOM_OggS,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3475     { ATOM_alac,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3476     /* Sound extensions */
3477     { ATOM_chan,    MP4_ReadBox_stsdext_chan, MP4_FreeBox_stsdext_chan, 0 },
3478
3479     { ATOM_drmi,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3480     { ATOM_vide,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3481     { ATOM_mp4v,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3482     { ATOM_SVQ1,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3483     { ATOM_SVQ3,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3484     { ATOM_ZyGo,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3485     { ATOM_DIVX,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3486     { ATOM_XVID,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3487     { ATOM_h263,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3488     { ATOM_s263,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3489     { ATOM_cvid,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3490     { ATOM_3IV1,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3491     { ATOM_3iv1,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3492     { ATOM_3IV2,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3493     { ATOM_3iv2,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3494     { ATOM_3IVD,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3495     { ATOM_3ivd,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3496     { ATOM_3VID,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3497     { ATOM_3vid,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3498     { ATOM_mjpa,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3499     { ATOM_mjpb,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3500     { ATOM_qdrw,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3501     { ATOM_mp2v,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3502     { ATOM_hdv2,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3503
3504     { ATOM_mjqt,    MP4_ReadBox_default,      NULL, 0 }, /* found in mjpa/b */
3505     { ATOM_mjht,    MP4_ReadBox_default,      NULL, 0 },
3506
3507     { ATOM_dvc,     MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3508     { ATOM_dvp,     MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3509     { ATOM_dv5n,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3510     { ATOM_dv5p,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3511     { ATOM_VP31,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3512     { ATOM_vp31,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3513     { ATOM_h264,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3514
3515     { ATOM_jpeg,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3516     { ATOM_avc1,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3517
3518     { ATOM_yv12,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, 0 },
3519     { ATOM_yuv2,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, 0 },
3520
3521     { ATOM_mp4s,    MP4_ReadBox_sample_mp4s,  MP4_FreeBox_Common, 0 },
3522
3523     /* XXX there is 2 box where we could find this entry stbl and tref*/
3524     { ATOM_hint,    MP4_ReadBox_default,      MP4_FreeBox_Common, 0 },
3525
3526     /* found in tref box */
3527     { ATOM_dpnd,    MP4_ReadBox_default,      NULL, 0 },
3528     { ATOM_ipir,    MP4_ReadBox_default,      NULL, 0 },
3529     { ATOM_mpod,    MP4_ReadBox_default,      NULL, 0 },
3530     { ATOM_chap,    MP4_ReadBox_tref_generic, MP4_FreeBox_tref_generic, 0 },
3531
3532     /* found in hnti */
3533     { ATOM_rtp,     MP4_ReadBox_default,      NULL, 0 },
3534
3535     /* found in rmra/rmda */
3536     { ATOM_rdrf,    MP4_ReadBox_rdrf,         MP4_FreeBox_rdrf  , ATOM_rmda },
3537     { ATOM_rmdr,    MP4_ReadBox_rmdr,         MP4_FreeBox_Common, ATOM_rmda },
3538     { ATOM_rmqu,    MP4_ReadBox_rmqu,         MP4_FreeBox_Common, ATOM_rmda },
3539     { ATOM_rmvc,    MP4_ReadBox_rmvc,         MP4_FreeBox_Common, ATOM_rmda },
3540
3541     { ATOM_drms,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, 0 },
3542     { ATOM_sinf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3543     { ATOM_schi,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3544     { ATOM_user,    MP4_ReadBox_drms,         MP4_FreeBox_Common, 0 },
3545     { ATOM_key,     MP4_ReadBox_drms,         MP4_FreeBox_Common, 0 },
3546     { ATOM_iviv,    MP4_ReadBox_drms,         MP4_FreeBox_Common, 0 },
3547     { ATOM_priv,    MP4_ReadBox_drms,         MP4_FreeBox_Common, 0 },
3548     { ATOM_frma,    MP4_ReadBox_frma,         MP4_FreeBox_Common, 0 },
3549     { ATOM_skcr,    MP4_ReadBox_skcr,         MP4_FreeBox_Common, 0 },
3550
3551     /* found in udta */
3552     { ATOM_0xa9nam, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3553     { ATOM_0xa9aut, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3554     { ATOM_0xa9cpy, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3555     { ATOM_0xa9swr, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3556     { ATOM_0xa9inf, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3557     { ATOM_0xa9ART, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3558     { ATOM_0xa9dir, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3559     { ATOM_0xa9cmt, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3560     { ATOM_0xa9req, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3561     { ATOM_0xa9day, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3562     { ATOM_0xa9des, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3563     { ATOM_0xa9fmt, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3564     { ATOM_0xa9prd, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3565     { ATOM_0xa9prf, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3566     { ATOM_0xa9src, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3567     { ATOM_0xa9alb, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3568     { ATOM_0xa9dis, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3569     { ATOM_0xa9enc, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3570     { ATOM_0xa9gen, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3571     { ATOM_0xa9trk, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3572     { ATOM_0xa9dsa, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3573     { ATOM_0xa9hst, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3574     { ATOM_0xa9url, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3575     { ATOM_0xa9ope, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3576     { ATOM_0xa9com, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3577     { ATOM_0xa9wrt, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3578     { ATOM_0xa9too, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3579     { ATOM_0xa9wrn, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3580     { ATOM_0xa9mak, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3581     { ATOM_0xa9mod, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3582     { ATOM_0xa9PRD, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3583     { ATOM_0xa9grp, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3584     { ATOM_0xa9lyr, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3585     { ATOM_0xa9gen, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3586     { ATOM_0xa9st3, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3587     { ATOM_0xa9ard, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3588     { ATOM_0xa9arg, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3589     { ATOM_0xa9cak, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3590     { ATOM_0xa9con, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3591     { ATOM_0xa9des, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3592     { ATOM_0xa9lnt, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3593     { ATOM_0xa9phg, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3594     { ATOM_0xa9pub, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3595     { ATOM_0xa9sne, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3596     { ATOM_0xa9sol, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3597     { ATOM_0xa9thx, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3598     { ATOM_0xa9xpd, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3599     { ATOM_chpl,    MP4_ReadBox_chpl,         MP4_FreeBox_chpl,    ATOM_udta },
3600
3601     { ATOM_gnre,    MP4_ReadBox_gnre,         MP4_FreeBox_Common,  ATOM_ilst },
3602     { ATOM_trkn,    MP4_ReadBox_trkn,         MP4_FreeBox_Common,  ATOM_ilst },
3603
3604     /* iTunes/Quicktime meta info */
3605     { ATOM_meta,    MP4_ReadBox_meta,         MP4_FreeBox_Common,  0 },
3606     { ATOM_name,    MP4_ReadBox_name,         MP4_FreeBox_name,    0 },
3607     { ATOM_covr,    MP4_ReadBoxContainer,     MP4_FreeBox_Common,  ATOM_ilst },
3608     { ATOM_data,    MP4_ReadBox_data,         MP4_FreeBox_data,    0 },
3609
3610     /* found in smoothstreaming */
3611     { ATOM_traf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common,  ATOM_moof },
3612     { ATOM_mfra,    MP4_ReadBoxContainer,     MP4_FreeBox_Common,  0 },
3613     { ATOM_mfhd,    MP4_ReadBox_mfhd,         MP4_FreeBox_Common,  ATOM_moof },
3614     { ATOM_sidx,    MP4_ReadBox_sidx,         MP4_FreeBox_sidx,    0 },
3615     { ATOM_tfhd,    MP4_ReadBox_tfhd,         MP4_FreeBox_Common,  ATOM_traf },
3616     { ATOM_trun,    MP4_ReadBox_trun,         MP4_FreeBox_trun,    ATOM_traf },
3617     { ATOM_trex,    MP4_ReadBox_trex,         MP4_FreeBox_Common,  ATOM_mvex },
3618     { ATOM_mehd,    MP4_ReadBox_mehd,         MP4_FreeBox_Common,  ATOM_mvex },
3619     { ATOM_sdtp,    MP4_ReadBox_sdtp,         MP4_FreeBox_sdtp,    0 },
3620     { ATOM_tfra,    MP4_ReadBox_tfra,         MP4_FreeBox_tfra,    ATOM_mfra },
3621     { ATOM_mfro,    MP4_ReadBox_mfro,         MP4_FreeBox_Common,  ATOM_mfra },
3622     { ATOM_uuid,    MP4_ReadBox_uuid,         MP4_FreeBox_uuid,    0 },
3623
3624     /* Last entry */
3625     { 0,              MP4_ReadBox_default,      NULL, 0 }
3626 };
3627
3628
3629 /*****************************************************************************
3630  * MP4_ReadBox : parse the actual box and the children
3631  *  XXX : Do not go to the next box
3632  *****************************************************************************/
3633 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
3634 {
3635     MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
3636     unsigned int i_index;
3637
3638     if( p_box == NULL )
3639         return NULL;
3640
3641     if( !MP4_ReadBoxCommon( p_stream, p_box ) )
3642     {
3643         msg_Warn( p_stream, "cannot read one box" );
3644         free( p_box );
3645         return NULL;
3646     }
3647     if( !p_box->i_size )
3648     {
3649         msg_Dbg( p_stream, "found an empty box (null size)" );
3650         free( p_box );
3651         return NULL;
3652     }
3653     p_box->p_father = p_father;
3654
3655     /* Now search function to call */
3656     for( i_index = 0; ; i_index++ )
3657     {
3658         if ( MP4_Box_Function[i_index].i_parent &&
3659              p_box->p_father &&
3660              p_box->p_father->i_type != MP4_Box_Function[i_index].i_parent )
3661             continue;
3662
3663         if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
3664             ( MP4_Box_Function[i_index].i_type == 0 ) )
3665         {
3666             break;
3667         }
3668     }
3669
3670     if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
3671     {
3672         MP4_BoxFree( p_stream, p_box );
3673         return NULL;
3674     }
3675
3676     return p_box;
3677 }
3678
3679 /*****************************************************************************
3680  * MP4_FreeBox : free memory after read with MP4_ReadBox and all
3681  * the children
3682  *****************************************************************************/
3683 void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
3684 {
3685     unsigned int i_index;
3686     MP4_Box_t    *p_child;
3687
3688     if( !p_box )
3689         return; /* hehe */
3690
3691     for( p_child = p_box->p_first; p_child != NULL; )
3692     {
3693         MP4_Box_t *p_next;
3694
3695         p_next = p_child->p_next;
3696         MP4_BoxFree( s, p_child );
3697         p_child = p_next;
3698     }
3699
3700     /* Now search function to call */
3701     if( p_box->data.p_payload )
3702     {
3703         for( i_index = 0; ; i_index++ )
3704         {
3705             if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
3706                 ( MP4_Box_Function[i_index].i_type == 0 ) )
3707             {
3708                 break;
3709             }
3710         }
3711         if( MP4_Box_Function[i_index].MP4_FreeBox_function == NULL )
3712         {
3713             /* Should not happen */
3714             if MP4_BOX_TYPE_ASCII()
3715                 msg_Warn( s,
3716                         "cannot free box %4.4s, type unknown",
3717                         (char*)&p_box->i_type );
3718             else
3719                 msg_Warn( s,
3720                         "cannot free box c%3.3s, type unknown",
3721                         (char*)&p_box->i_type+1 );
3722         }
3723         else
3724         {
3725             MP4_Box_Function[i_index].MP4_FreeBox_function( p_box );
3726         }
3727         free( p_box->data.p_payload );
3728     }
3729     free( p_box );
3730 }
3731
3732 /* SmooBox is a very simple MP4 box, VLC specific, used only for the stream_filter to
3733  * send information to the demux. SmooBox is actually a simplified moov box (we wanted
3734  * to avoid the hassle of building a moov box at the stream_filter level) */
3735 MP4_Box_t *MP4_BoxGetSmooBox( stream_t *s )
3736 {
3737     /* p_chunk is a virtual root container for the smoo box */
3738     MP4_Box_t *p_chunk;
3739     MP4_Box_t *p_smoo;
3740
3741     p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
3742     if( unlikely( p_chunk == NULL ) )
3743         return NULL;
3744
3745     p_chunk->i_type = ATOM_root;
3746     p_chunk->i_shortsize = 1;
3747
3748     p_smoo = MP4_ReadBox( s, p_chunk );
3749     if( !p_smoo || p_smoo->i_type != ATOM_uuid || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
3750     {
3751         msg_Warn( s, "no smoo box found!");
3752         goto error;
3753     }
3754
3755     p_chunk->p_first = p_smoo;
3756     p_chunk->p_last = p_smoo;
3757
3758     return p_chunk;
3759
3760 error:
3761     free( p_chunk );
3762     return NULL;
3763 }
3764
3765 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
3766 {
3767     /* p_chunk is a virtual root container for the moof and mdat boxes */
3768     MP4_Box_t *p_chunk;
3769     MP4_Box_t *p_tmp_box = NULL;
3770
3771     p_tmp_box = calloc( 1, sizeof( MP4_Box_t ) );
3772     if( unlikely( p_tmp_box == NULL ) )
3773         return NULL;
3774
3775     /* We might get a ftyp box or a SmooBox */
3776     MP4_ReadBoxCommon( s, p_tmp_box );
3777
3778     if( (p_tmp_box->i_type == ATOM_uuid && !CmpUUID( &p_tmp_box->i_uuid, &SmooBoxUUID )) )
3779     {
3780         free( p_tmp_box );
3781         return MP4_BoxGetSmooBox( s );
3782     }
3783     else if( p_tmp_box->i_type == ATOM_ftyp )
3784     {
3785         free( p_tmp_box );
3786         return MP4_BoxGetRoot( s );
3787     }
3788     free( p_tmp_box );
3789
3790     p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
3791     if( unlikely( p_chunk == NULL ) )
3792         return NULL;
3793
3794     p_chunk->i_type = ATOM_root;
3795     p_chunk->i_shortsize = 1;
3796
3797     MP4_ReadBoxContainerChildren( s, p_chunk, ATOM_moof );
3798
3799     p_tmp_box = p_chunk->p_first;
3800     while( p_tmp_box )
3801     {
3802         p_chunk->i_size += p_tmp_box->i_size;
3803         p_tmp_box = p_tmp_box->p_next;
3804     }
3805
3806     return p_chunk;
3807 }
3808
3809 /*****************************************************************************
3810  * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
3811  *****************************************************************************
3812  *  The first box is a virtual box "root" and is the father for all first
3813  *  level boxes for the file, a sort of virtual contener
3814  *****************************************************************************/
3815 MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
3816 {
3817     MP4_Box_t *p_root;
3818     stream_t *p_stream;
3819     int i_result;
3820
3821     p_root = malloc( sizeof( MP4_Box_t ) );
3822     if( p_root == NULL )
3823         return NULL;
3824
3825     p_root->i_pos = 0;
3826     p_root->i_type = ATOM_root;
3827     p_root->i_shortsize = 1;
3828     /* could be a DASH stream for exemple, 0 means unknown or infinite size */
3829     p_root->i_size = 0;
3830     CreateUUID( &p_root->i_uuid, p_root->i_type );
3831
3832     p_root->data.p_payload = NULL;
3833     p_root->p_father    = NULL;
3834     p_root->p_first     = NULL;
3835     p_root->p_last      = NULL;
3836     p_root->p_next      = NULL;
3837
3838     p_stream = s;
3839
3840     /* First get the moov */
3841     i_result = MP4_ReadBoxContainerChildren( p_stream, p_root, ATOM_moov );
3842
3843     if( !i_result )
3844         goto error;
3845     /* If there is a mvex box, it means fragmented MP4, and we're done */
3846     else if( MP4_BoxCount( p_root, "moov/mvex" ) > 0 )
3847         return p_root;
3848
3849     p_root->i_size = stream_Size( s );
3850     if( stream_Tell( s ) + 8 < stream_Size( s ) )
3851     {
3852         /* Get the rest of the file */
3853         i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
3854
3855         if( !i_result )
3856             goto error;
3857     }
3858
3859     MP4_Box_t *p_moov;
3860     MP4_Box_t *p_cmov;
3861
3862     /* check if there is a cmov, if so replace
3863       compressed moov by  uncompressed one */
3864     if( ( ( p_moov = MP4_BoxGet( p_root, "moov" ) ) &&
3865           ( p_cmov = MP4_BoxGet( p_root, "moov/cmov" ) ) ) ||
3866         ( ( p_moov = MP4_BoxGet( p_root, "foov" ) ) &&
3867           ( p_cmov = MP4_BoxGet( p_root, "foov/cmov" ) ) ) )
3868     {
3869         /* rename the compressed moov as a box to skip */
3870         p_moov->i_type = ATOM_skip;
3871
3872         /* get uncompressed p_moov */
3873         p_moov = p_cmov->data.p_cmov->p_moov;
3874         p_cmov->data.p_cmov->p_moov = NULL;
3875
3876         /* make p_root father of this new moov */
3877         p_moov->p_father = p_root;
3878
3879         /* insert this new moov box as first child of p_root */
3880         p_moov->p_next = p_root->p_first;
3881         p_root->p_first = p_moov;
3882     }
3883
3884     return p_root;
3885
3886 error:
3887     free( p_root );
3888     stream_Seek( p_stream, 0 );
3889     return NULL;
3890 }
3891
3892
3893 static void MP4_BoxDumpStructure_Internal( stream_t *s,
3894                                     MP4_Box_t *p_box, unsigned int i_level )
3895 {
3896     MP4_Box_t *p_child;
3897
3898     if( !i_level )
3899     {
3900         if MP4_BOX_TYPE_ASCII()
3901             msg_Dbg( s, "dumping root Box \"%4.4s\"",
3902                               (char*)&p_box->i_type );
3903         else
3904             msg_Dbg( s, "dumping root Box \"c%3.3s\"",
3905                               (char*)&p_box->i_type+1 );
3906     }
3907     else
3908     {
3909         char str[512];
3910         if( i_level >= (sizeof(str) - 1)/4 )
3911             return;
3912
3913         memset( str, ' ', sizeof(str) );
3914         for( unsigned i = 0; i < i_level; i++ )
3915         {
3916             str[i*4] = '|';
3917         }
3918         if( MP4_BOX_TYPE_ASCII() )
3919             snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
3920                       "+ %4.4s size %"PRIu64" offset %ld%s",
3921                         (char*)&p_box->i_type, p_box->i_size, p_box->i_pos,
3922                     p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
3923         else
3924             snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
3925                       "+ c%3.3s size %"PRIu64" offset %ld%s",
3926                         (char*)&p_box->i_type+1, p_box->i_size, p_box->i_pos,
3927                     p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
3928         msg_Dbg( s, "%s", str );
3929     }
3930     p_child = p_box->p_first;
3931     while( p_child )
3932     {
3933         MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
3934         p_child = p_child->p_next;
3935     }
3936 }
3937
3938 void MP4_BoxDumpStructure( stream_t *s, MP4_Box_t *p_box )
3939 {
3940     MP4_BoxDumpStructure_Internal( s, p_box, 0 );
3941 }
3942
3943
3944 /*****************************************************************************
3945  *****************************************************************************
3946  **
3947  **  High level methods to acces an MP4 file
3948  **
3949  *****************************************************************************
3950  *****************************************************************************/
3951 static void get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
3952 {
3953     size_t i_len ;
3954     if( !*ppsz_path[0] )
3955     {
3956         *ppsz_token = NULL;
3957         *pi_number = 0;
3958         return;
3959     }
3960     i_len = strcspn( *ppsz_path, "/[" );
3961     if( !i_len && **ppsz_path == '/' )
3962     {
3963         i_len = 1;
3964     }
3965     *ppsz_token = strndup( *ppsz_path, i_len );
3966     if( unlikely(!*ppsz_token) )
3967         abort();
3968
3969     *ppsz_path += i_len;
3970
3971     if( **ppsz_path == '[' )
3972     {
3973         (*ppsz_path)++;
3974         *pi_number = strtol( *ppsz_path, NULL, 10 );
3975         while( **ppsz_path && **ppsz_path != ']' )
3976         {
3977             (*ppsz_path)++;
3978         }
3979         if( **ppsz_path == ']' )
3980         {
3981             (*ppsz_path)++;
3982         }
3983     }
3984     else
3985     {
3986         *pi_number = 0;
3987     }
3988     while( **ppsz_path == '/' )
3989     {
3990         (*ppsz_path)++;
3991     }
3992 }
3993
3994 static void MP4_BoxGet_Internal( MP4_Box_t **pp_result,
3995                           MP4_Box_t *p_box, const char *psz_fmt, va_list args)
3996 {
3997     char *psz_dup;
3998     char *psz_path;
3999     char *psz_token;
4000
4001     if( !p_box )
4002     {
4003         *pp_result = NULL;
4004         return;
4005     }
4006
4007     if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
4008         psz_path = NULL;
4009
4010     if( !psz_path || !psz_path[0] )
4011     {
4012         free( psz_path );
4013         *pp_result = NULL;
4014         return;
4015     }
4016
4017 //    fprintf( stderr, "path:'%s'\n", psz_path );
4018     psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
4019     for( ; ; )
4020     {
4021         int i_number;
4022
4023         get_token( &psz_path, &psz_token, &i_number );
4024 //        fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
4025 //                 psz_path,psz_token,i_number );
4026         if( !psz_token )
4027         {
4028             free( psz_dup );
4029             *pp_result = p_box;
4030             return;
4031         }
4032         else
4033         if( !strcmp( psz_token, "/" ) )
4034         {
4035             /* Find root box */
4036             while( p_box && p_box->i_type != ATOM_root )
4037             {
4038                 p_box = p_box->p_father;
4039             }
4040             if( !p_box )
4041             {
4042                 goto error_box;
4043             }
4044         }
4045         else
4046         if( !strcmp( psz_token, "." ) )
4047         {
4048             /* Do nothing */
4049         }
4050         else
4051         if( !strcmp( psz_token, ".." ) )
4052         {
4053             p_box = p_box->p_father;
4054             if( !p_box )
4055             {
4056                 goto error_box;
4057             }
4058         }
4059         else
4060         if( strlen( psz_token ) == 4 )
4061         {
4062             uint32_t i_fourcc;
4063             i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
4064                                    psz_token[2], psz_token[3] );
4065             p_box = p_box->p_first;
4066             for( ; ; )
4067             {
4068                 if( !p_box )
4069                 {
4070                     goto error_box;
4071                 }
4072                 if( p_box->i_type == i_fourcc )
4073                 {
4074                     if( !i_number )
4075                     {
4076                         break;
4077                     }
4078                     i_number--;
4079                 }
4080                 p_box = p_box->p_next;
4081             }
4082         }
4083         else
4084         if( *psz_token == '\0' )
4085         {
4086             p_box = p_box->p_first;
4087             for( ; ; )
4088             {
4089                 if( !p_box )
4090                 {
4091                     goto error_box;
4092                 }
4093                 if( !i_number )
4094                 {
4095                     break;
4096                 }
4097                 i_number--;
4098                 p_box = p_box->p_next;
4099             }
4100         }
4101         else
4102         {
4103 //            fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
4104             goto error_box;
4105         }
4106
4107         FREENULL( psz_token );
4108     }
4109
4110     return;
4111
4112 error_box:
4113     free( psz_token );
4114     free( psz_dup );
4115     *pp_result = NULL;
4116     return;
4117 }
4118
4119 /*****************************************************************************
4120  * MP4_BoxGet: find a box given a path relative to p_box
4121  *****************************************************************************
4122  * Path Format: . .. / as usual
4123  *              [number] to specifie box number ex: trak[12]
4124  *
4125  * ex: /moov/trak[12]
4126  *     ../mdia
4127  *****************************************************************************/
4128 MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, const char *psz_fmt, ... )
4129 {
4130     va_list args;
4131     MP4_Box_t *p_result;
4132
4133     va_start( args, psz_fmt );
4134     MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
4135     va_end( args );
4136
4137     return( p_result );
4138 }
4139
4140 /*****************************************************************************
4141  * MP4_BoxCount: count box given a path relative to p_box
4142  *****************************************************************************
4143  * Path Format: . .. / as usual
4144  *              [number] to specifie box number ex: trak[12]
4145  *
4146  * ex: /moov/trak[12]
4147  *     ../mdia
4148  *****************************************************************************/
4149 int MP4_BoxCount( MP4_Box_t *p_box, const char *psz_fmt, ... )
4150 {
4151     va_list args;
4152     int     i_count;
4153     MP4_Box_t *p_result, *p_next;
4154
4155     va_start( args, psz_fmt );
4156     MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
4157     va_end( args );
4158     if( !p_result )
4159     {
4160         return( 0 );
4161     }
4162
4163     i_count = 1;
4164     for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
4165     {
4166         if( p_next->i_type == p_result->i_type)
4167         {
4168             i_count++;
4169         }
4170     }
4171     return( i_count );
4172 }