]> git.sesse.net Git - vlc/blob - modules/demux/mp4/libmp4.c
demux: libmp4: handle ASF atom
[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 ISO/IEC 14496-1 8.3.3 */
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 ISO/IEC 14496-1 8.3.4 */
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_objectProfileIndication );
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 ISO/IEC 14496-1 8.3.5 */
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_WMA2( stream_t *p_stream, MP4_Box_t *p_box )
1463 {
1464     MP4_READBOX_ENTER( MP4_Box_data_WMA2_t );
1465
1466     MP4_Box_data_WMA2_t *p_WMA2 = p_box->data.p_WMA2;
1467
1468     MP4_GET2BYTESLE( p_WMA2->Format.wFormatTag );
1469     MP4_GET2BYTESLE( p_WMA2->Format.nChannels );
1470     MP4_GET4BYTESLE( p_WMA2->Format.nSamplesPerSec );
1471     MP4_GET4BYTESLE( p_WMA2->Format.nAvgBytesPerSec );
1472     MP4_GET2BYTESLE( p_WMA2->Format.nBlockAlign );
1473     MP4_GET2BYTESLE( p_WMA2->Format.wBitsPerSample );
1474
1475     uint16_t i_cbSize;
1476     MP4_GET2BYTESLE( i_cbSize );
1477
1478     if ( i_read < 0 || i_cbSize > i_read )
1479         goto error;
1480
1481     p_WMA2->i_extra = i_cbSize;
1482     if ( p_WMA2->i_extra )
1483     {
1484         p_WMA2->p_extra = malloc( p_WMA2->i_extra );
1485         if ( ! p_WMA2->p_extra )
1486             goto error;
1487         memcpy( p_WMA2->p_extra, p_peek, p_WMA2->i_extra );
1488     }
1489
1490     MP4_READBOX_EXIT( 1 );
1491
1492 error:
1493     MP4_READBOX_EXIT( 0 );
1494 }
1495
1496 static void MP4_FreeBox_WMA2( MP4_Box_t *p_box )
1497 {
1498     FREENULL( p_box->data.p_WMA2->p_extra );
1499 }
1500
1501 static int MP4_ReadBox_strf( stream_t *p_stream, MP4_Box_t *p_box )
1502 {
1503     MP4_READBOX_ENTER( MP4_Box_data_strf_t );
1504
1505     MP4_Box_data_strf_t *p_strf = p_box->data.p_strf;
1506
1507     MP4_GET4BYTESLE( p_strf->bmiHeader.biSize );
1508     MP4_GET4BYTESLE( p_strf->bmiHeader.biWidth );
1509     MP4_GET4BYTESLE( p_strf->bmiHeader.biHeight );
1510     MP4_GET2BYTESLE( p_strf->bmiHeader.biPlanes );
1511     MP4_GET2BYTESLE( p_strf->bmiHeader.biBitCount );
1512     MP4_GETFOURCC( p_strf->bmiHeader.biCompression );
1513     MP4_GET4BYTESLE( p_strf->bmiHeader.biSizeImage );
1514     MP4_GET4BYTESLE( p_strf->bmiHeader.biXPelsPerMeter );
1515     MP4_GET4BYTESLE( p_strf->bmiHeader.biYPelsPerMeter );
1516     MP4_GET4BYTESLE( p_strf->bmiHeader.biClrUsed );
1517     MP4_GET4BYTESLE( p_strf->bmiHeader.biClrImportant );
1518
1519     if ( i_read < 0 )
1520         goto error;
1521
1522     p_strf->i_extra = i_read;
1523     if ( p_strf->i_extra )
1524     {
1525         p_strf->p_extra = malloc( p_strf->i_extra );
1526         if ( ! p_strf->p_extra )
1527             goto error;
1528         memcpy( p_strf->p_extra, p_peek, i_read );
1529     }
1530
1531     MP4_READBOX_EXIT( 1 );
1532
1533 error:
1534     MP4_READBOX_EXIT( 0 );
1535 }
1536
1537 static void MP4_FreeBox_strf( MP4_Box_t *p_box )
1538 {
1539     FREENULL( p_box->data.p_strf->p_extra );
1540 }
1541
1542 static int MP4_ReadBox_ASF( stream_t *p_stream, MP4_Box_t *p_box )
1543 {
1544     MP4_READBOX_ENTER( MP4_Box_data_ASF_t );
1545
1546     MP4_Box_data_ASF_t *p_asf = p_box->data.p_asf;
1547
1548     if (i_read != 8)
1549         MP4_READBOX_EXIT( 0 );
1550
1551     MP4_GET1BYTE( p_asf->i_stream_number );
1552     /* remaining is unknown */
1553
1554     MP4_READBOX_EXIT( 1 );
1555 }
1556
1557 static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
1558 {
1559     MP4_READBOX_ENTER( MP4_Box_data_chan_t );
1560     MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
1561
1562     if ( i_read < 16 )
1563         MP4_READBOX_EXIT( 0 );
1564
1565     MP4_GET1BYTE( p_chan->i_version );
1566     MP4_GET3BYTES( p_chan->i_channels_flags );
1567     MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
1568     MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
1569     MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
1570     if ( i_read < p_chan->layout.i_channels_description_count * 24 )
1571         MP4_READBOX_EXIT( 0 );
1572
1573     p_chan->layout.p_descriptions =
1574         malloc( p_chan->layout.i_channels_description_count * 24 );
1575
1576     if ( !p_chan->layout.p_descriptions )
1577         MP4_READBOX_EXIT( 0 );
1578
1579     for( uint32_t i=0; i<p_chan->layout.i_channels_description_count; i++ )
1580     {
1581         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
1582         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
1583         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
1584         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
1585         MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
1586     }
1587
1588 #ifdef MP4_VERBOSE
1589     msg_Dbg( p_stream,
1590              "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
1591              p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
1592              p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
1593 #endif
1594     MP4_READBOX_EXIT( 1 );
1595 }
1596
1597 static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
1598 {
1599     MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
1600     free( p_chan->layout.p_descriptions );
1601 }
1602
1603 static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
1604 {
1605     MP4_READBOX_ENTER( MP4_Box_data_dec3_t );
1606
1607     MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
1608
1609     unsigned i_header;
1610     MP4_GET2BYTES( i_header );
1611
1612     p_dec3->i_data_rate = i_header >> 3;
1613     p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
1614     for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
1615         MP4_GET3BYTES( i_header );
1616         p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
1617         p_dec3->stream[i].i_bsid  = ( i_header >> 17 ) & 0x01f;
1618         p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
1619         p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
1620         p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
1621         p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
1622         if (p_dec3->stream[i].i_num_dep_sub) {
1623             MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
1624             p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
1625         } else
1626             p_dec3->stream[i].i_chan_loc = 0;
1627     }
1628
1629 #ifdef MP4_VERBOSE
1630     msg_Dbg( p_stream,
1631         "read box: \"dec3\" bitrate %dkbps %d independant substreams",
1632             p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
1633
1634     for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
1635         msg_Dbg( p_stream,
1636                 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
1637                 "num dependant subs=%d chan_loc=0x%x",
1638                 i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
1639                 p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
1640 #endif
1641     MP4_READBOX_EXIT( 1 );
1642 }
1643
1644 static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
1645 {
1646     MP4_Box_data_dac3_t *p_dac3;
1647     MP4_READBOX_ENTER( MP4_Box_data_dac3_t );
1648
1649     p_dac3 = p_box->data.p_dac3;
1650
1651     unsigned i_header;
1652     MP4_GET3BYTES( i_header );
1653
1654     p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
1655     p_dac3->i_bsid  = ( i_header >> 17 ) & 0x01f;
1656     p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
1657     p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
1658     p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
1659     p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
1660
1661 #ifdef MP4_VERBOSE
1662     msg_Dbg( p_stream,
1663              "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
1664              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 );
1665 #endif
1666     MP4_READBOX_EXIT( 1 );
1667 }
1668
1669 static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
1670 {
1671     MP4_Box_data_dvc1_t *p_dvc1;
1672
1673     MP4_READBOX_ENTER( MP4_Box_data_dvc1_t );
1674     p_dvc1 = p_box->data.p_dvc1;
1675
1676     MP4_GET1BYTE( p_dvc1->i_profile_level ); /* profile is on 4bits, level 3bits */
1677     uint8_t i_profile = (p_dvc1->i_profile_level & 0xf0) >> 4;
1678     if( i_profile != 0x06 && i_profile != 0x0c )
1679     {
1680         msg_Warn( p_stream, "unsupported VC-1 profile (%"PRIu8"), please report", i_profile );
1681         MP4_READBOX_EXIT( 0 );
1682     }
1683
1684
1685     p_dvc1->i_vc1 = p_box->i_size - 7; /* Header + profile_level */
1686
1687     if( p_dvc1->i_vc1 > 0 )
1688     {
1689         uint8_t *p = p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 );
1690         if( p )
1691             memcpy( p, p_peek, i_read );
1692     }
1693
1694 #ifdef MP4_VERBOSE
1695     msg_Dbg( p_stream,
1696              "read box: \"dvc1\" profile=%"PRIu8" level=%i",
1697              i_profile, p_dvc1->i_profile_level & 0x0e >> 1 );
1698 #endif
1699
1700     MP4_READBOX_EXIT( 1 );
1701 }
1702
1703 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
1704 {
1705     MP4_Box_data_enda_t *p_enda;
1706     MP4_READBOX_ENTER( MP4_Box_data_enda_t );
1707
1708     p_enda = p_box->data.p_enda;
1709
1710     MP4_GET2BYTES( p_enda->i_little_endian );
1711
1712 #ifdef MP4_VERBOSE
1713     msg_Dbg( p_stream,
1714              "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
1715 #endif
1716     MP4_READBOX_EXIT( 1 );
1717 }
1718
1719 static int MP4_ReadBox_gnre( stream_t *p_stream, MP4_Box_t *p_box )
1720 {
1721     MP4_Box_data_gnre_t *p_gnre;
1722     MP4_READBOX_ENTER( MP4_Box_data_gnre_t );
1723
1724     p_gnre = p_box->data.p_gnre;
1725
1726     uint32_t i_data_len;
1727     uint32_t i_data_tag;
1728
1729     MP4_GET4BYTES( i_data_len );
1730     MP4_GETFOURCC( i_data_tag );
1731     if( i_data_len < 10 || i_data_tag != ATOM_data )
1732         MP4_READBOX_EXIT( 0 );
1733
1734     uint32_t i_version;
1735     VLC_UNUSED(i_version);
1736     uint32_t i_reserved;
1737     VLC_UNUSED(i_reserved);
1738     MP4_GET4BYTES( i_version );
1739     MP4_GET4BYTES( i_reserved );
1740     MP4_GET2BYTES( p_gnre->i_genre );
1741     if( p_gnre->i_genre == 0 )
1742         MP4_READBOX_EXIT( 0 );
1743 #ifdef MP4_VERBOSE
1744     msg_Dbg( p_stream, "read box: \"gnre\" genre=%i", p_gnre->i_genre );
1745 #endif
1746
1747     MP4_READBOX_EXIT( 1 );
1748 }
1749
1750 static int MP4_ReadBox_trkn( stream_t *p_stream, MP4_Box_t *p_box )
1751 {
1752     MP4_Box_data_trkn_t *p_trkn;
1753     MP4_READBOX_ENTER( MP4_Box_data_trkn_t );
1754
1755     p_trkn = p_box->data.p_trkn;
1756
1757     uint32_t i_data_len;
1758     uint32_t i_data_tag;
1759
1760     MP4_GET4BYTES( i_data_len );
1761     MP4_GETFOURCC( i_data_tag );
1762     if( i_data_len < 12 || i_data_tag != ATOM_data )
1763         MP4_READBOX_EXIT( 0 );
1764
1765     uint32_t i_version;
1766     VLC_UNUSED(i_version);
1767     uint32_t i_reserved;
1768     VLC_UNUSED(i_reserved);
1769     MP4_GET4BYTES( i_version );
1770     MP4_GET4BYTES( i_reserved );
1771     MP4_GET2BYTES( i_reserved );
1772     MP4_GET2BYTES( p_trkn->i_track_number );
1773 #ifdef MP4_VERBOSE
1774     msg_Dbg( p_stream, "read box: \"trkn\" number=%i", p_trkn->i_track_number );
1775 #endif
1776     if( i_data_len > 15 )
1777     {
1778        MP4_GET2BYTES( p_trkn->i_track_total );
1779 #ifdef MP4_VERBOSE
1780        msg_Dbg( p_stream, "read box: \"trkn\" total=%i", p_trkn->i_track_total );
1781 #endif
1782     }
1783
1784     MP4_READBOX_EXIT( 1 );
1785 }
1786
1787 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
1788 {
1789     p_box->i_handler = ATOM_soun;
1790     MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
1791     p_box->data.p_sample_soun->p_qt_description = NULL;
1792
1793     /* Sanity check needed because the "wave" box does also contain an
1794      * "mp4a" box that we don't understand. */
1795     if( i_read < 28 )
1796     {
1797         i_read -= 30;
1798         MP4_READBOX_EXIT( 1 );
1799     }
1800
1801     for( unsigned i = 0; i < 6 ; i++ )
1802     {
1803         MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
1804     }
1805
1806     MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
1807
1808     /*
1809      * XXX hack -> produce a copy of the nearly complete chunk
1810      */
1811     p_box->data.p_sample_soun->i_qt_description = 0;
1812     p_box->data.p_sample_soun->p_qt_description = NULL;
1813     if( i_read > 0 )
1814     {
1815         p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
1816         if( p_box->data.p_sample_soun->p_qt_description )
1817         {
1818             p_box->data.p_sample_soun->i_qt_description = i_read;
1819             memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
1820         }
1821     }
1822
1823     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
1824     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
1825     MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
1826
1827     MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
1828     MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
1829     MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
1830     MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
1831     MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
1832     MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
1833
1834     if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
1835     {
1836         /* SoundDescriptionV1 */
1837         MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
1838         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
1839         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
1840         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
1841
1842 #ifdef MP4_VERBOSE
1843         msg_Dbg( p_stream,
1844                  "read box: \"soun\" qt3+ sample/packet=%d bytes/packet=%d "
1845                  "bytes/frame=%d bytes/sample=%d",
1846                  p_box->data.p_sample_soun->i_sample_per_packet,
1847                  p_box->data.p_sample_soun->i_bytes_per_packet,
1848                  p_box->data.p_sample_soun->i_bytes_per_frame,
1849                  p_box->data.p_sample_soun->i_bytes_per_sample );
1850 #endif
1851         stream_Seek( p_stream, p_box->i_pos +
1852                         mp4_box_headersize( p_box ) + 44 );
1853     }
1854     else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
1855     {
1856         /* SoundDescriptionV2 */
1857         double f_sample_rate;
1858         int64_t i_dummy64;
1859         uint32_t i_channel, i_extoffset, i_dummy32;
1860
1861         /* Checks */
1862         if ( p_box->data.p_sample_soun->i_channelcount != 0x3  ||
1863              p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
1864              p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
1865              p_box->data.p_sample_soun->i_reserved3 != 0x0     ||
1866              p_box->data.p_sample_soun->i_sampleratehi != 0x1  ||//65536
1867              p_box->data.p_sample_soun->i_sampleratelo != 0x0 )  //remainder
1868         {
1869             msg_Err( p_stream, "invalid stsd V2 box defaults" );
1870             MP4_READBOX_EXIT( 0 );
1871         }
1872         /* !Checks */
1873
1874         MP4_GET4BYTES( i_extoffset ); /* offset to stsd extentions */
1875         MP4_GET8BYTES( i_dummy64 );
1876         memcpy( &f_sample_rate, &i_dummy64, 8 );
1877         msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
1878         p_box->data.p_sample_soun->i_sampleratehi = (int)f_sample_rate % BLOCK16x16;
1879         p_box->data.p_sample_soun->i_sampleratelo = f_sample_rate / BLOCK16x16;
1880
1881         MP4_GET4BYTES( i_channel );
1882         p_box->data.p_sample_soun->i_channelcount = i_channel;
1883
1884         MP4_GET4BYTES( i_dummy32 );
1885         if ( i_dummy32 != 0x7F000000 )
1886         {
1887             msg_Err( p_stream, "invalid stsd V2 box" );
1888             MP4_READBOX_EXIT( 0 );
1889         }
1890
1891         MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
1892         MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
1893         MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
1894         MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
1895
1896 #ifdef MP4_VERBOSE
1897         msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
1898                            "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
1899                  f_sample_rate,
1900                  p_box->data.p_sample_soun->i_constbitsperchannel,
1901                  p_box->data.p_sample_soun->i_formatflags,
1902                  p_box->data.p_sample_soun->i_constbytesperaudiopacket,
1903                  p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
1904 #endif
1905         if ( i_extoffset < p_box->i_size )
1906             stream_Seek( p_stream, p_box->i_pos + i_extoffset );
1907         else
1908             stream_Seek( p_stream, p_box->i_pos + p_box->i_size );
1909     }
1910     else
1911     {
1912         p_box->data.p_sample_soun->i_sample_per_packet = 0;
1913         p_box->data.p_sample_soun->i_bytes_per_packet = 0;
1914         p_box->data.p_sample_soun->i_bytes_per_frame = 0;
1915         p_box->data.p_sample_soun->i_bytes_per_sample = 0;
1916
1917 #ifdef MP4_VERBOSE
1918         msg_Dbg( p_stream, "read box: \"soun\" mp4 or qt1/2 (rest=%"PRId64")",
1919                  i_read );
1920 #endif
1921         stream_Seek( p_stream, p_box->i_pos +
1922                         mp4_box_headersize( p_box ) + 28 );
1923     }
1924
1925     if( p_box->i_type == ATOM_drms )
1926     {
1927         msg_Warn( p_stream, "DRM protected streams are not supported." );
1928         MP4_READBOX_EXIT( 0 );
1929     }
1930
1931     if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
1932     {
1933         /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
1934         p_box->data.p_sample_soun->i_channelcount = 1;
1935     }
1936
1937     /* Loads extensions */
1938     MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds/wave/... */
1939
1940 #ifdef MP4_VERBOSE
1941     msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
1942              "sample size %d sample rate %f",
1943              p_box->data.p_sample_soun->i_channelcount,
1944              p_box->data.p_sample_soun->i_samplesize,
1945              (float)p_box->data.p_sample_soun->i_sampleratehi +
1946              (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
1947
1948 #endif
1949     MP4_READBOX_EXIT( 1 );
1950 }
1951
1952
1953 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
1954 {
1955     FREENULL( p_box->data.p_sample_soun->p_qt_description );
1956 }
1957
1958
1959 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
1960 {
1961     p_box->i_handler = ATOM_vide;
1962     MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );
1963
1964     for( unsigned i = 0; i < 6 ; i++ )
1965     {
1966         MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
1967     }
1968
1969     MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
1970
1971     /*
1972      * XXX hack -> produce a copy of the nearly complete chunk
1973      */
1974     if( i_read > 0 )
1975     {
1976         p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
1977         if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
1978             MP4_READBOX_EXIT( 0 );
1979         p_box->data.p_sample_vide->i_qt_image_description = i_read;
1980         memcpy( p_box->data.p_sample_vide->p_qt_image_description,
1981                 p_peek, i_read );
1982     }
1983     else
1984     {
1985         p_box->data.p_sample_vide->i_qt_image_description = 0;
1986         p_box->data.p_sample_vide->p_qt_image_description = NULL;
1987     }
1988
1989     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
1990     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
1991     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
1992
1993     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
1994     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
1995
1996     MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
1997     MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
1998
1999     MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
2000     MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
2001
2002     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
2003     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
2004
2005     if ( i_read < 32 )
2006         MP4_READBOX_EXIT( 0 );
2007     memcpy( &p_box->data.p_sample_vide->i_compressorname, p_peek, 32 );
2008     p_peek += 32; i_read -= 32;
2009
2010     MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
2011     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
2012
2013     stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 78);
2014
2015     if( p_box->i_type == ATOM_drmi )
2016     {
2017         msg_Warn( p_stream, "DRM protected streams are not supported." );
2018         MP4_READBOX_EXIT( 0 );
2019     }
2020
2021     MP4_ReadBoxContainerRaw( p_stream, p_box );
2022
2023 #ifdef MP4_VERBOSE
2024     msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d",
2025                       p_box->data.p_sample_vide->i_width,
2026                       p_box->data.p_sample_vide->i_height,
2027                       p_box->data.p_sample_vide->i_depth );
2028
2029 #endif
2030     MP4_READBOX_EXIT( 1 );
2031 }
2032
2033
2034 void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
2035 {
2036     FREENULL( p_box->data.p_sample_vide->p_qt_image_description );
2037 }
2038
2039 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
2040 {
2041     stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
2042     MP4_ReadBoxContainerRaw( p_stream, p_box );
2043     return 1;
2044 }
2045
2046 static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
2047 {
2048     int32_t t;
2049
2050     p_box->i_handler = ATOM_text;
2051     MP4_READBOX_ENTER( MP4_Box_data_sample_text_t );
2052
2053     MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2054     MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2055
2056     MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2057
2058     MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2059
2060     MP4_GET4BYTES( t );
2061     switch( t )
2062     {
2063         /* FIXME search right signification */
2064         case 1: // Center
2065             p_box->data.p_sample_text->i_justification_horizontal = 1;
2066             p_box->data.p_sample_text->i_justification_vertical = 1;
2067             break;
2068         case -1:    // Flush Right
2069             p_box->data.p_sample_text->i_justification_horizontal = -1;
2070             p_box->data.p_sample_text->i_justification_vertical = -1;
2071             break;
2072         case -2:    // Flush Left
2073             p_box->data.p_sample_text->i_justification_horizontal = 0;
2074             p_box->data.p_sample_text->i_justification_vertical = 0;
2075             break;
2076         case 0: // Flush Default
2077         default:
2078             p_box->data.p_sample_text->i_justification_horizontal = 1;
2079             p_box->data.p_sample_text->i_justification_vertical = -1;
2080             break;
2081     }
2082
2083     MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[0] );
2084     MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[1] );
2085     MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[2] );
2086     p_box->data.p_sample_text->i_background_color[3] = 0xFF;
2087
2088     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2089     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2090     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2091     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2092
2093 #ifdef MP4_VERBOSE
2094     msg_Dbg( p_stream, "read box: \"text\" in stsd text" );
2095 #endif
2096     MP4_READBOX_EXIT( 1 );
2097 }
2098
2099 static int MP4_ReadBox_sample_tx3g( stream_t *p_stream, MP4_Box_t *p_box )
2100 {
2101     p_box->i_handler = ATOM_text;
2102     MP4_READBOX_ENTER( MP4_Box_data_sample_text_t );
2103
2104     MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2105     MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2106
2107     MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2108
2109     MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2110
2111     MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_horizontal );
2112     MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_vertical );
2113
2114     MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[0] );
2115     MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[1] );
2116     MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[2] );
2117     MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[3] );
2118
2119     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2120     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2121     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2122     MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2123
2124     MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved3 );
2125
2126     MP4_GET2BYTES( p_box->data.p_sample_text->i_font_id );
2127     MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_face );
2128     MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_size );
2129     MP4_GET4BYTES( p_box->data.p_sample_text->i_font_color );
2130
2131 #ifdef MP4_VERBOSE
2132     msg_Dbg( p_stream, "read box: \"tx3g\" in stsd text" );
2133 #endif
2134     MP4_READBOX_EXIT( 1 );
2135 }
2136
2137
2138 #if 0
2139 /* We can't easily call it, and anyway ~ 20 bytes lost isn't a real problem */
2140 static void MP4_FreeBox_sample_text( MP4_Box_t *p_box )
2141 {
2142     FREENULL( p_box->data.p_sample_text->psz_text_name );
2143 }
2144 #endif
2145
2146
2147 static int MP4_ReadBox_stsd( stream_t *p_stream, MP4_Box_t *p_box )
2148 {
2149
2150     MP4_READBOX_ENTER( MP4_Box_data_stsd_t );
2151
2152     MP4_GETVERSIONFLAGS( p_box->data.p_stsd );
2153
2154     MP4_GET4BYTES( p_box->data.p_stsd->i_entry_count );
2155
2156     stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
2157
2158     MP4_ReadBoxContainerRaw( p_stream, p_box );
2159
2160 #ifdef MP4_VERBOSE
2161     msg_Dbg( p_stream, "read box: \"stsd\" entry-count %d",
2162                       p_box->data.p_stsd->i_entry_count );
2163
2164 #endif
2165     MP4_READBOX_EXIT( 1 );
2166 }
2167
2168
2169 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
2170 {
2171     MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
2172
2173     MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
2174
2175     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
2176     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
2177
2178     if( p_box->data.p_stsz->i_sample_size == 0 )
2179     {
2180         p_box->data.p_stsz->i_entry_size =
2181             calloc( p_box->data.p_stsz->i_sample_count, sizeof(uint32_t) );
2182         if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
2183             MP4_READBOX_EXIT( 0 );
2184
2185         for( unsigned int i = 0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
2186         {
2187             MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
2188         }
2189     }
2190     else
2191         p_box->data.p_stsz->i_entry_size = NULL;
2192
2193 #ifdef MP4_VERBOSE
2194     msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
2195                       p_box->data.p_stsz->i_sample_size,
2196                       p_box->data.p_stsz->i_sample_count );
2197
2198 #endif
2199     MP4_READBOX_EXIT( 1 );
2200 }
2201
2202 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
2203 {
2204     FREENULL( p_box->data.p_stsz->i_entry_size );
2205 }
2206
2207 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
2208 {
2209     FREENULL( p_box->data.p_stsc->i_first_chunk );
2210     FREENULL( p_box->data.p_stsc->i_samples_per_chunk );
2211     FREENULL( p_box->data.p_stsc->i_sample_description_index );
2212 }
2213
2214 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
2215 {
2216     MP4_READBOX_ENTER( MP4_Box_data_stsc_t );
2217
2218     MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
2219
2220     MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
2221
2222     p_box->data.p_stsc->i_first_chunk =
2223         calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2224     p_box->data.p_stsc->i_samples_per_chunk =
2225         calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2226     p_box->data.p_stsc->i_sample_description_index =
2227         calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2228     if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
2229      || p_box->data.p_stsc->i_samples_per_chunk == NULL
2230      || p_box->data.p_stsc->i_sample_description_index == NULL ) )
2231     {
2232         MP4_READBOX_EXIT( 0 );
2233     }
2234
2235     for( unsigned int i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
2236     {
2237         MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
2238         MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
2239         MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
2240     }
2241
2242 #ifdef MP4_VERBOSE
2243     msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
2244                       p_box->data.p_stsc->i_entry_count );
2245
2246 #endif
2247     MP4_READBOX_EXIT( 1 );
2248 }
2249
2250 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
2251 {
2252     MP4_READBOX_ENTER( MP4_Box_data_co64_t );
2253
2254     MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
2255
2256     MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
2257
2258     p_box->data.p_co64->i_chunk_offset =
2259         calloc( p_box->data.p_co64->i_entry_count, sizeof(uint64_t) );
2260     if( p_box->data.p_co64->i_chunk_offset == NULL )
2261         MP4_READBOX_EXIT( 0 );
2262
2263     for( unsigned int i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
2264     {
2265         if( p_box->i_type == ATOM_stco )
2266         {
2267             if( i_read < 4 )
2268             {
2269                 break;
2270             }
2271             MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2272         }
2273         else
2274         {
2275             if( i_read < 8 )
2276             {
2277                 break;
2278             }
2279             MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2280         }
2281     }
2282
2283 #ifdef MP4_VERBOSE
2284     msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
2285                       p_box->data.p_co64->i_entry_count );
2286
2287 #endif
2288     MP4_READBOX_EXIT( 1 );
2289 }
2290
2291 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
2292 {
2293     FREENULL( p_box->data.p_co64->i_chunk_offset );
2294 }
2295
2296 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
2297 {
2298     MP4_READBOX_ENTER( MP4_Box_data_stss_t );
2299
2300     MP4_GETVERSIONFLAGS( p_box->data.p_stss );
2301
2302     MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
2303
2304     p_box->data.p_stss->i_sample_number =
2305         calloc( p_box->data.p_stss->i_entry_count, sizeof(uint32_t) );
2306     if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
2307         MP4_READBOX_EXIT( 0 );
2308
2309     unsigned int i;
2310     for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
2311     {
2312
2313         MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
2314         /* XXX in libmp4 sample begin at 0 */
2315         p_box->data.p_stss->i_sample_number[i]--;
2316     }
2317     if ( i < p_box->data.p_stss->i_entry_count )
2318         p_box->data.p_stss->i_entry_count = i;
2319
2320 #ifdef MP4_VERBOSE
2321     msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
2322                       p_box->data.p_stss->i_entry_count );
2323
2324 #endif
2325     MP4_READBOX_EXIT( 1 );
2326 }
2327
2328 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
2329 {
2330     FREENULL( p_box->data.p_stss->i_sample_number );
2331 }
2332
2333 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
2334 {
2335     FREENULL( p_box->data.p_stsh->i_shadowed_sample_number );
2336     FREENULL( p_box->data.p_stsh->i_sync_sample_number );
2337 }
2338
2339 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
2340 {
2341     MP4_READBOX_ENTER( MP4_Box_data_stsh_t );
2342
2343     MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
2344
2345
2346     MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
2347
2348     p_box->data.p_stsh->i_shadowed_sample_number =
2349         calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2350     p_box->data.p_stsh->i_sync_sample_number =
2351         calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2352
2353     if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
2354      || p_box->data.p_stsh->i_sync_sample_number == NULL )
2355     {
2356         MP4_READBOX_EXIT( 0 );
2357     }
2358
2359     unsigned i;
2360     for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
2361     {
2362         MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
2363         MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
2364     }
2365     if ( i < p_box->data.p_stss->i_entry_count )
2366         p_box->data.p_stss->i_entry_count = i;
2367
2368 #ifdef MP4_VERBOSE
2369     msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
2370                       p_box->data.p_stsh->i_entry_count );
2371 #endif
2372     MP4_READBOX_EXIT( 1 );
2373 }
2374
2375
2376 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
2377 {
2378     MP4_READBOX_ENTER( MP4_Box_data_stdp_t );
2379
2380     MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
2381
2382     p_box->data.p_stdp->i_priority =
2383         calloc( i_read / 2, sizeof(uint16_t) );
2384
2385     if( unlikely( !p_box->data.p_stdp->i_priority ) )
2386         MP4_READBOX_EXIT( 0 );
2387
2388     for( unsigned i = 0; i < i_read / 2 ; i++ )
2389     {
2390         MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
2391     }
2392
2393 #ifdef MP4_VERBOSE
2394     msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
2395                       i_read / 2 );
2396
2397 #endif
2398     MP4_READBOX_EXIT( 1 );
2399 }
2400
2401 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
2402 {
2403     FREENULL( p_box->data.p_stdp->i_priority );
2404 }
2405
2406 static void MP4_FreeBox_padb( MP4_Box_t *p_box )
2407 {
2408     FREENULL( p_box->data.p_padb->i_reserved1 );
2409     FREENULL( p_box->data.p_padb->i_pad2 );
2410     FREENULL( p_box->data.p_padb->i_reserved2 );
2411     FREENULL( p_box->data.p_padb->i_pad1 );
2412 }
2413
2414 static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
2415 {
2416     uint32_t count;
2417
2418     MP4_READBOX_ENTER( MP4_Box_data_padb_t );
2419
2420     MP4_GETVERSIONFLAGS( p_box->data.p_padb );
2421
2422     MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
2423     count = (p_box->data.p_padb->i_sample_count + 1) / 2;
2424
2425     p_box->data.p_padb->i_reserved1 = calloc( count, sizeof(uint16_t) );
2426     p_box->data.p_padb->i_pad2 = calloc( count, sizeof(uint16_t) );
2427     p_box->data.p_padb->i_reserved2 = calloc( count, sizeof(uint16_t) );
2428     p_box->data.p_padb->i_pad1 = calloc( count, sizeof(uint16_t) );
2429     if( p_box->data.p_padb->i_reserved1 == NULL
2430      || p_box->data.p_padb->i_pad2 == NULL
2431      || p_box->data.p_padb->i_reserved2 == NULL
2432      || p_box->data.p_padb->i_pad1 == NULL )
2433     {
2434         MP4_READBOX_EXIT( 0 );
2435     }
2436
2437     for( unsigned int i = 0; i < i_read / 2 ; i++ )
2438     {
2439         if( i >= count )
2440         {
2441             MP4_READBOX_EXIT( 0 );
2442         }
2443         p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
2444         p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
2445         p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
2446         p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;
2447
2448         p_peek += 1; i_read -= 1;
2449     }
2450
2451 #ifdef MP4_VERBOSE
2452     msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
2453                       i_read / 2 );
2454
2455 #endif
2456     MP4_READBOX_EXIT( 1 );
2457 }
2458
2459 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
2460 {
2461     FREENULL( p_box->data.p_elst->i_segment_duration );
2462     FREENULL( p_box->data.p_elst->i_media_time );
2463     FREENULL( p_box->data.p_elst->i_media_rate_integer );
2464     FREENULL( p_box->data.p_elst->i_media_rate_fraction );
2465 }
2466
2467 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
2468 {
2469     MP4_READBOX_ENTER( MP4_Box_data_elst_t );
2470
2471     MP4_GETVERSIONFLAGS( p_box->data.p_elst );
2472
2473
2474     MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
2475
2476     p_box->data.p_elst->i_segment_duration =
2477         calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
2478     p_box->data.p_elst->i_media_time =
2479         calloc( p_box->data.p_elst->i_entry_count, sizeof(int64_t) );
2480     p_box->data.p_elst->i_media_rate_integer =
2481         calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
2482     p_box->data.p_elst->i_media_rate_fraction =
2483         calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
2484     if( p_box->data.p_elst->i_segment_duration == NULL
2485      || p_box->data.p_elst->i_media_time == NULL
2486      || p_box->data.p_elst->i_media_rate_integer == NULL
2487      || p_box->data.p_elst->i_media_rate_fraction == NULL )
2488     {
2489         MP4_READBOX_EXIT( 0 );
2490     }
2491
2492     unsigned i;
2493     for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
2494     {
2495         if( p_box->data.p_elst->i_version == 1 )
2496         {
2497             if ( i_read < 20 )
2498                 break;
2499             MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
2500
2501             MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
2502         }
2503         else
2504         {
2505             if ( i_read < 12 )
2506                 break;
2507             MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
2508
2509             MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
2510             p_box->data.p_elst->i_media_time[i] = (int32_t)p_box->data.p_elst->i_media_time[i];
2511         }
2512
2513         MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
2514         MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
2515     }
2516     if ( i < p_box->data.p_elst->i_entry_count )
2517         p_box->data.p_elst->i_entry_count = i;
2518 #ifdef MP4_VERBOSE
2519     msg_Dbg( p_stream, "read box: \"elst\" entry-count %lu",
2520              (unsigned long)p_box->data.p_elst->i_entry_count );
2521 #endif
2522     MP4_READBOX_EXIT( 1 );
2523 }
2524
2525 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
2526 {
2527     uint16_t i_language;
2528     bool b_mac;
2529
2530     MP4_READBOX_ENTER( MP4_Box_data_cprt_t );
2531
2532     MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
2533
2534     MP4_GET2BYTES( i_language );
2535     decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
2536
2537     MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
2538
2539 #ifdef MP4_VERBOSE
2540     msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
2541                       p_box->data.p_cprt->rgs_language,
2542                       p_box->data.p_cprt->psz_notice );
2543
2544 #endif
2545     MP4_READBOX_EXIT( 1 );
2546 }
2547
2548 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
2549 {
2550     FREENULL( p_box->data.p_cprt->psz_notice );
2551 }
2552
2553
2554 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
2555 {
2556     MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
2557
2558     MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
2559 #ifdef MP4_VERBOSE
2560     msg_Dbg( p_stream,
2561              "read box: \"dcom\" compression algorithm : %4.4s",
2562                       (char*)&p_box->data.p_dcom->i_algorithm );
2563 #endif
2564     MP4_READBOX_EXIT( 1 );
2565 }
2566
2567 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
2568 {
2569     MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );
2570
2571     MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
2572
2573     p_box->data.p_cmvd->i_compressed_size = i_read;
2574
2575     if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
2576         MP4_READBOX_EXIT( 0 );
2577
2578     /* now copy compressed data */
2579     memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
2580
2581     p_box->data.p_cmvd->b_compressed = 1;
2582
2583 #ifdef MP4_VERBOSE
2584     msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
2585                       p_box->data.p_cmvd->i_compressed_size );
2586 #endif
2587
2588     MP4_READBOX_EXIT( 1 );
2589 }
2590 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
2591 {
2592     FREENULL( p_box->data.p_cmvd->p_data );
2593 }
2594
2595
2596 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
2597 {
2598     MP4_Box_t *p_dcom;
2599     MP4_Box_t *p_cmvd;
2600
2601 #ifdef HAVE_ZLIB_H
2602     stream_t *p_stream_memory;
2603     z_stream z_data;
2604     uint8_t *p_data;
2605     int i_result;
2606 #endif
2607
2608     if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
2609         return 0;
2610
2611     if( !p_box->p_father ||
2612         ( p_box->p_father->i_type != ATOM_moov &&
2613           p_box->p_father->i_type != ATOM_foov ) )
2614     {
2615         msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
2616         return 1;
2617     }
2618
2619     if( !MP4_ReadBoxContainer( p_stream, p_box ) )
2620     {
2621         return 0;
2622     }
2623
2624     if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
2625         ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
2626         p_cmvd->data.p_cmvd->p_data == NULL )
2627     {
2628         msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
2629         return 0;
2630     }
2631
2632     if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
2633     {
2634         msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
2635                  "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
2636         return 0;
2637     }
2638
2639 #ifndef HAVE_ZLIB_H
2640     msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
2641     return 0;
2642
2643 #else
2644     /* decompress data */
2645     /* allocate a new buffer */
2646     if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
2647         return 0;
2648     /* init default structures */
2649     z_data.next_in   = p_cmvd->data.p_cmvd->p_data;
2650     z_data.avail_in  = p_cmvd->data.p_cmvd->i_compressed_size;
2651     z_data.next_out  = p_data;
2652     z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
2653     z_data.zalloc    = (alloc_func)Z_NULL;
2654     z_data.zfree     = (free_func)Z_NULL;
2655     z_data.opaque    = (voidpf)Z_NULL;
2656
2657     /* init zlib */
2658     if( inflateInit( &z_data ) != Z_OK )
2659     {
2660         msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
2661         free( p_data );
2662         return 0;
2663     }
2664
2665     /* uncompress */
2666     i_result = inflate( &z_data, Z_NO_FLUSH );
2667     if( i_result != Z_OK && i_result != Z_STREAM_END )
2668     {
2669         msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
2670         free( p_data );
2671         return 0;
2672     }
2673
2674     if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
2675     {
2676         msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
2677                   "mismatch" );
2678     }
2679     p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
2680
2681     /* close zlib */
2682     if( inflateEnd( &z_data ) != Z_OK )
2683     {
2684         msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
2685                   "data (ignored)" );
2686     }
2687
2688     free( p_cmvd->data.p_cmvd->p_data );
2689     p_cmvd->data.p_cmvd->p_data = p_data;
2690     p_cmvd->data.p_cmvd->b_compressed = 0;
2691
2692     msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
2693
2694     /* now create a memory stream */
2695     p_stream_memory =
2696         stream_MemoryNew( VLC_OBJECT(p_stream), p_cmvd->data.p_cmvd->p_data,
2697                           p_cmvd->data.p_cmvd->i_uncompressed_size, true );
2698
2699     /* and read uncompressd moov */
2700     p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
2701
2702     stream_Delete( p_stream_memory );
2703
2704 #ifdef MP4_VERBOSE
2705     msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
2706 #endif
2707
2708     return p_box->data.p_cmov->p_moov ? 1 : 0;
2709 #endif /* HAVE_ZLIB_H */
2710 }
2711
2712 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
2713 {
2714     uint32_t i_len;
2715     MP4_READBOX_ENTER( MP4_Box_data_rdrf_t );
2716
2717     MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
2718     MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
2719     MP4_GET4BYTES( i_len );
2720     i_len++;
2721
2722     if( i_len > 0 )
2723     {
2724         p_box->data.p_rdrf->psz_ref = malloc( i_len );
2725         if( p_box->data.p_rdrf->psz_ref == NULL )
2726             MP4_READBOX_EXIT( 0 );
2727         i_len--;
2728
2729         for( unsigned i = 0; i < i_len; i++ )
2730         {
2731             MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
2732         }
2733         p_box->data.p_rdrf->psz_ref[i_len] = '\0';
2734     }
2735     else
2736     {
2737         p_box->data.p_rdrf->psz_ref = NULL;
2738     }
2739
2740 #ifdef MP4_VERBOSE
2741     msg_Dbg( p_stream,
2742             "read box: \"rdrf\" type:%4.4s ref %s",
2743             (char*)&p_box->data.p_rdrf->i_ref_type,
2744             p_box->data.p_rdrf->psz_ref );
2745 #endif
2746     MP4_READBOX_EXIT( 1 );
2747 }
2748
2749 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
2750 {
2751     FREENULL( p_box->data.p_rdrf->psz_ref );
2752 }
2753
2754
2755 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
2756 {
2757     MP4_READBOX_ENTER( MP4_Box_data_rmdr_t );
2758
2759     MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
2760
2761     MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
2762
2763 #ifdef MP4_VERBOSE
2764     msg_Dbg( p_stream,
2765              "read box: \"rmdr\" rate:%d",
2766              p_box->data.p_rmdr->i_rate );
2767 #endif
2768     MP4_READBOX_EXIT( 1 );
2769 }
2770
2771 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
2772 {
2773     MP4_READBOX_ENTER( MP4_Box_data_rmqu_t );
2774
2775     MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
2776
2777 #ifdef MP4_VERBOSE
2778     msg_Dbg( p_stream,
2779              "read box: \"rmqu\" quality:%d",
2780              p_box->data.p_rmqu->i_quality );
2781 #endif
2782     MP4_READBOX_EXIT( 1 );
2783 }
2784
2785 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
2786 {
2787     MP4_READBOX_ENTER( MP4_Box_data_rmvc_t );
2788     MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
2789
2790     MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
2791     MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
2792     MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
2793     MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
2794
2795 #ifdef MP4_VERBOSE
2796     msg_Dbg( p_stream,
2797              "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
2798              (char*)&p_box->data.p_rmvc->i_gestaltType,
2799              p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
2800              p_box->data.p_rmvc->i_checkType );
2801 #endif
2802
2803     MP4_READBOX_EXIT( 1 );
2804 }
2805
2806 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
2807 {
2808     MP4_READBOX_ENTER( MP4_Box_data_frma_t );
2809
2810     MP4_GETFOURCC( p_box->data.p_frma->i_type );
2811
2812 #ifdef MP4_VERBOSE
2813     msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
2814              (char *)&p_box->data.p_frma->i_type );
2815 #endif
2816
2817     MP4_READBOX_EXIT( 1 );
2818 }
2819
2820 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
2821 {
2822     MP4_READBOX_ENTER( MP4_Box_data_skcr_t );
2823
2824     MP4_GET4BYTES( p_box->data.p_skcr->i_init );
2825     MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
2826     MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
2827
2828 #ifdef MP4_VERBOSE
2829     msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
2830              p_box->data.p_skcr->i_init,
2831              p_box->data.p_skcr->i_encr,
2832              p_box->data.p_skcr->i_decr );
2833 #endif
2834
2835     MP4_READBOX_EXIT( 1 );
2836 }
2837
2838 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
2839 {
2840     VLC_UNUSED(p_box);
2841     /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
2842      * so unless data decrypt itself by magic, there will be no playback,
2843      * but we never know... */
2844     msg_Warn( p_stream, "DRM protected streams are not supported." );
2845     return 1;
2846 }
2847
2848 static int MP4_ReadBox_name( stream_t *p_stream, MP4_Box_t *p_box )
2849 {
2850     MP4_READBOX_ENTER( MP4_Box_data_name_t );
2851
2852     p_box->data.p_name->psz_text = malloc( p_box->i_size + 1 - 8 ); /* +\0, -name, -size */
2853     if( p_box->data.p_name->psz_text == NULL )
2854         MP4_READBOX_EXIT( 0 );
2855
2856     memcpy( p_box->data.p_name->psz_text, p_peek, p_box->i_size - 8 );
2857     p_box->data.p_name->psz_text[p_box->i_size - 8] = '\0';
2858
2859 #ifdef MP4_VERBOSE
2860         msg_Dbg( p_stream, "read box: \"name\" text=`%s'",
2861                  p_box->data.p_name->psz_text );
2862 #endif
2863     MP4_READBOX_EXIT( 1 );
2864 }
2865
2866 static void MP4_FreeBox_name( MP4_Box_t *p_box )
2867 {
2868     FREENULL( p_box->data.p_name->psz_text );
2869 }
2870
2871 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
2872 {
2873     MP4_READBOX_ENTER( MP4_Box_data_data_t );
2874
2875     /* What's that header ?? */
2876     if ( i_read <= 8 )
2877         MP4_READBOX_EXIT( 0 );
2878
2879     p_box->data.p_data->p_blob = malloc( i_read - 8 );
2880     if ( !p_box->data.p_data->p_blob )
2881         MP4_READBOX_EXIT( 0 );
2882
2883     p_box->data.p_data->i_blob = i_read - 8;
2884     memcpy( p_box->data.p_data->p_blob, p_peek + 8, i_read - 8 );
2885
2886     MP4_READBOX_EXIT( 1 );
2887 }
2888
2889 static void MP4_FreeBox_data( MP4_Box_t *p_box )
2890 {
2891     free( p_box->data.p_data->p_blob );
2892 }
2893
2894 static int MP4_ReadBox_0xa9xxx( stream_t *p_stream, MP4_Box_t *p_box )
2895 {
2896     uint16_t i16;
2897
2898     MP4_READBOX_ENTER( MP4_Box_data_0xa9xxx_t );
2899
2900     p_box->data.p_0xa9xxx->psz_text = NULL;
2901
2902     MP4_GET2BYTES( i16 );
2903
2904     if( i16 > 0 )
2905     {
2906         int i_length = i16;
2907
2908         MP4_GET2BYTES( i16 );
2909         if( i_length >= i_read ) i_length = i_read + 1;
2910
2911         p_box->data.p_0xa9xxx->psz_text = malloc( i_length );
2912         if( p_box->data.p_0xa9xxx->psz_text == NULL )
2913             MP4_READBOX_EXIT( 0 );
2914
2915         i_length--;
2916         memcpy( p_box->data.p_0xa9xxx->psz_text,
2917                 p_peek, i_length );
2918         p_box->data.p_0xa9xxx->psz_text[i_length] = '\0';
2919
2920 #ifdef MP4_VERBOSE
2921         msg_Dbg( p_stream,
2922                  "read box: \"c%3.3s\" text=`%s'",
2923                  ((char*)&p_box->i_type + 1),
2924                  p_box->data.p_0xa9xxx->psz_text );
2925 #endif
2926     }
2927     else
2928     {
2929         /* try iTune/Quicktime format, rewind to start */
2930         p_peek -= 2; i_read += 2;
2931         // we are expecting a 'data' box
2932         uint32_t i_data_len;
2933         uint32_t i_data_tag;
2934
2935         MP4_GET4BYTES( i_data_len );
2936         if( i_data_len > i_read ) i_data_len = i_read;
2937         MP4_GETFOURCC( i_data_tag );
2938         if( (i_data_len > 0) && (i_data_tag == ATOM_data) )
2939         {
2940             /* data box contains a version/flags field */
2941             uint32_t i_version;
2942             uint32_t i_reserved;
2943             VLC_UNUSED(i_reserved);
2944             MP4_GET4BYTES( i_version );
2945             MP4_GET4BYTES( i_reserved );
2946             // version should be 0, flags should be 1 for text, 0 for data
2947             if( ( i_version == 0x00000001 ) && (i_data_len >= 12 ) )
2948             {
2949                 // the rest is the text
2950                 i_data_len -= 12;
2951                 p_box->data.p_0xa9xxx->psz_text = malloc( i_data_len + 1 );
2952                 if( p_box->data.p_0xa9xxx->psz_text == NULL )
2953                     MP4_READBOX_EXIT( 0 );
2954
2955                 memcpy( p_box->data.p_0xa9xxx->psz_text,
2956                         p_peek, i_data_len );
2957                 p_box->data.p_0xa9xxx->psz_text[i_data_len] = '\0';
2958 #ifdef MP4_VERBOSE
2959         msg_Dbg( p_stream,
2960                  "read box: \"c%3.3s\" text=`%s'",
2961                  ((char*)&p_box->i_type+1),
2962                  p_box->data.p_0xa9xxx->psz_text );
2963 #endif
2964             }
2965             else
2966             {
2967                 // TODO: handle data values for ID3 tag values, track num or cover art,etc...
2968             }
2969         }
2970     }
2971
2972     MP4_READBOX_EXIT( 1 );
2973 }
2974 static void MP4_FreeBox_0xa9xxx( MP4_Box_t *p_box )
2975 {
2976     FREENULL( p_box->data.p_0xa9xxx->psz_text );
2977 }
2978
2979 /* Chapter support */
2980 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
2981 {
2982     MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
2983     for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
2984         free( p_chpl->chapter[i].psz_name );
2985 }
2986
2987 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
2988 {
2989     MP4_Box_data_chpl_t *p_chpl;
2990     uint32_t i_dummy;
2991     VLC_UNUSED(i_dummy);
2992     int i;
2993     MP4_READBOX_ENTER( MP4_Box_data_chpl_t );
2994
2995     p_chpl = p_box->data.p_chpl;
2996
2997     MP4_GETVERSIONFLAGS( p_chpl );
2998
2999     MP4_GET4BYTES( i_dummy );
3000
3001     MP4_GET1BYTE( p_chpl->i_chapter );
3002
3003     for( i = 0; i < p_chpl->i_chapter; i++ )
3004     {
3005         uint64_t i_start;
3006         uint8_t i_len;
3007         int i_copy;
3008         MP4_GET8BYTES( i_start );
3009         MP4_GET1BYTE( i_len );
3010
3011         p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
3012         if( !p_chpl->chapter[i].psz_name )
3013             MP4_READBOX_EXIT( 0 );
3014
3015         i_copy = __MIN( i_len, i_read );
3016         if( i_copy > 0 )
3017             memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
3018         p_chpl->chapter[i].psz_name[i_copy] = '\0';
3019         p_chpl->chapter[i].i_start = i_start;
3020
3021         p_peek += i_copy;
3022         i_read -= i_copy;
3023     }
3024     /* Bubble sort by increasing start date */
3025     do
3026     {
3027         for( i = 0; i < p_chpl->i_chapter - 1; i++ )
3028         {
3029             if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
3030             {
3031                 char *psz = p_chpl->chapter[i+1].psz_name;
3032                 int64_t i64 = p_chpl->chapter[i+1].i_start;
3033
3034                 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
3035                 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
3036
3037                 p_chpl->chapter[i].psz_name = psz;
3038                 p_chpl->chapter[i].i_start = i64;
3039
3040                 i = -1;
3041                 break;
3042             }
3043         }
3044     } while( i == -1 );
3045
3046 #ifdef MP4_VERBOSE
3047     msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
3048                        p_chpl->i_chapter );
3049 #endif
3050     MP4_READBOX_EXIT( 1 );
3051 }
3052
3053 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
3054 {
3055     MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t );
3056
3057     p_box->data.p_tref_generic->i_track_ID = NULL;
3058     p_box->data.p_tref_generic->i_entry_count = i_read / sizeof(uint32_t);
3059     if( p_box->data.p_tref_generic->i_entry_count > 0 )
3060         p_box->data.p_tref_generic->i_track_ID = calloc( p_box->data.p_tref_generic->i_entry_count, sizeof(uint32_t) );
3061     if( p_box->data.p_tref_generic->i_track_ID == NULL )
3062         MP4_READBOX_EXIT( 0 );
3063
3064     for( unsigned i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
3065     {
3066         MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
3067     }
3068 #ifdef MP4_VERBOSE
3069         msg_Dbg( p_stream, "read box: \"chap\" %d references",
3070                  p_box->data.p_tref_generic->i_entry_count );
3071 #endif
3072
3073     MP4_READBOX_EXIT( 1 );
3074 }
3075 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
3076 {
3077     FREENULL( p_box->data.p_tref_generic->i_track_ID );
3078 }
3079
3080 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
3081 {
3082     uint8_t meta_data[8];
3083     int i_actually_read;
3084
3085     // skip over box header
3086     i_actually_read = stream_Read( p_stream, meta_data, 8 );
3087     if( i_actually_read < 8 )
3088         return 0;
3089
3090     if ( !p_box->p_father )
3091         return 0;
3092
3093     switch( p_box->p_father->i_type )
3094     {
3095     case ATOM_udta: /* itunes udta/meta */
3096         /* meta content starts with a 4 byte version/flags value (should be 0) */
3097         i_actually_read = stream_Read( p_stream, meta_data, 4 );
3098         if( i_actually_read < 4 )
3099             return 0;
3100
3101         /* then it behaves like a container */
3102         return MP4_ReadBoxContainerRaw( p_stream, p_box );
3103
3104     default: /* regular meta atom */
3105
3106         i_actually_read = stream_Read( p_stream, meta_data, 8 );
3107         if( i_actually_read < 8 )
3108             return 0;
3109
3110         /* Mandatory */
3111         if ( VLC_FOURCC( meta_data[4], meta_data[5], meta_data[6], meta_data[7] ) != ATOM_hdlr )
3112             return 0;
3113
3114         //ft
3115     }
3116     return 1;
3117 }
3118
3119 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3120 {
3121     char i_unused;
3122     VLC_UNUSED(i_unused);
3123
3124     MP4_READBOX_ENTER( MP4_Box_data_iods_t );
3125     MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3126
3127     MP4_GET1BYTE( i_unused ); /* tag */
3128     MP4_GET1BYTE( i_unused ); /* length */
3129
3130     MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3131                                                               are used for other flags */
3132     MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3133     MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3134     MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3135     MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3136     MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3137
3138 #ifdef MP4_VERBOSE
3139     msg_Dbg( p_stream,
3140              "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3141              "visual: %i, graphics: %i",
3142              p_box->data.p_iods->i_object_descriptor >> 6,
3143              p_box->data.p_iods->i_OD_profile_level,
3144              p_box->data.p_iods->i_scene_profile_level,
3145              p_box->data.p_iods->i_audio_profile_level,
3146              p_box->data.p_iods->i_visual_profile_level,
3147              p_box->data.p_iods->i_graphics_profile_level );
3148 #endif
3149
3150     MP4_READBOX_EXIT( 1 );
3151 }
3152
3153 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3154 {
3155     MP4_READBOX_ENTER( MP4_Box_data_pasp_t );
3156
3157     MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
3158     MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
3159
3160 #ifdef MP4_VERBOSE
3161     msg_Dbg( p_stream,
3162              "read box: \"paps\" %dx%d",
3163              p_box->data.p_pasp->i_horizontal_spacing,
3164              p_box->data.p_pasp->i_vertical_spacing);
3165 #endif
3166
3167     MP4_READBOX_EXIT( 1 );
3168 }
3169
3170 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
3171 {
3172     MP4_READBOX_ENTER( MP4_Box_data_mehd_t );
3173
3174     MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
3175     if( p_box->data.p_mehd->i_version == 1 )
3176         MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
3177     else /* version == 0 */
3178         MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
3179
3180 #ifdef MP4_VERBOSE
3181     msg_Dbg( p_stream,
3182              "read box: \"mehd\" frag dur. %"PRIu64"",
3183              p_box->data.p_mehd->i_fragment_duration );
3184 #endif
3185
3186     MP4_READBOX_EXIT( 1 );
3187 }
3188
3189 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
3190 {
3191     MP4_READBOX_ENTER( MP4_Box_data_trex_t );
3192     MP4_GETVERSIONFLAGS( p_box->data.p_trex );
3193
3194     MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
3195     MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
3196     MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
3197     MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
3198     MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
3199
3200 #ifdef MP4_VERBOSE
3201     msg_Dbg( p_stream,
3202              "read box: \"trex\" trackID: %"PRIu32"",
3203              p_box->data.p_trex->i_track_ID );
3204 #endif
3205
3206     MP4_READBOX_EXIT( 1 );
3207 }
3208
3209 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
3210 {
3211     uint32_t i_sample_count;
3212     MP4_READBOX_ENTER( MP4_Box_data_sdtp_t );
3213     MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
3214     MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
3215     i_sample_count = i_read;
3216
3217     p_sdtp->p_sample_table = calloc( i_sample_count, 1 );
3218
3219     if( !p_sdtp->p_sample_table )
3220         MP4_READBOX_EXIT( 0 );
3221
3222     for( uint32_t i = 0; i < i_sample_count; i++ )
3223         MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
3224
3225 #ifdef MP4_VERBOSE
3226     msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
3227     msg_Dbg( p_stream,
3228              "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
3229                  p_sdtp->p_sample_table[0],
3230                  p_sdtp->p_sample_table[1],
3231                  p_sdtp->p_sample_table[2],
3232                  p_sdtp->p_sample_table[3] );
3233 #endif
3234
3235     MP4_READBOX_EXIT( 1 );
3236 }
3237
3238 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
3239 {
3240     FREENULL( p_box->data.p_sdtp->p_sample_table );
3241 }
3242
3243 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
3244 {
3245     MP4_READBOX_ENTER( MP4_Box_data_mfro_t );
3246
3247     MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
3248     MP4_GET4BYTES( p_box->data.p_mfro->i_size );
3249
3250 #ifdef MP4_VERBOSE
3251     msg_Dbg( p_stream,
3252              "read box: \"mfro\" size: %"PRIu32"",
3253              p_box->data.p_mfro->i_size);
3254 #endif
3255
3256     MP4_READBOX_EXIT( 1 );
3257 }
3258
3259 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
3260 {
3261     uint32_t i_number_of_entries;
3262     MP4_READBOX_ENTER( MP4_Box_data_tfra_t );
3263     MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
3264     MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
3265
3266     MP4_GET4BYTES( p_tfra->i_track_ID );
3267     uint32_t i_lengths = 0;
3268     MP4_GET4BYTES( i_lengths );
3269     MP4_GET4BYTES( p_tfra->i_number_of_entries );
3270     i_number_of_entries = p_tfra->i_number_of_entries;
3271     p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
3272     p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
3273     p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
3274
3275     size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
3276     p_tfra->p_time = calloc( i_number_of_entries, size );
3277     p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
3278
3279     size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
3280     p_tfra->p_traf_number = calloc( i_number_of_entries, size );
3281     size = 1 + p_tfra->i_length_size_of_trun_num;
3282     p_tfra->p_trun_number = calloc( i_number_of_entries, size );
3283     size = 1 + p_tfra->i_length_size_of_sample_num;
3284     p_tfra->p_sample_number = calloc( i_number_of_entries, size );
3285
3286     if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
3287                         || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
3288         goto error;
3289
3290     int i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
3291             + p_tfra->i_length_size_of_trun_num
3292             + p_tfra->i_length_size_of_sample_num;
3293
3294     uint32_t i;
3295     for( i = 0; i < i_number_of_entries; i++ )
3296     {
3297
3298         if( p_tfra->i_version == 1 )
3299         {
3300             if ( i_read < i_fields_length + 16 )
3301                 break;
3302             MP4_GET8BYTES( p_tfra->p_time[i*2] );
3303             MP4_GET8BYTES( p_tfra->p_moof_offset[i*2] );
3304         }
3305         else
3306         {
3307             if ( i_read < i_fields_length + 8 )
3308                 break;
3309             MP4_GET4BYTES( p_tfra->p_time[i] );
3310             MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
3311         }
3312         switch (p_tfra->i_length_size_of_traf_num)
3313         {
3314             case 0:
3315                 MP4_GET1BYTE( p_tfra->p_traf_number[i] );
3316                 break;
3317             case 1:
3318                 MP4_GET2BYTES( p_tfra->p_traf_number[i*2] );
3319                 break;
3320             case 2:
3321                 MP4_GET3BYTES( p_tfra->p_traf_number[i*3] );
3322                 break;
3323             case 3:
3324                 MP4_GET4BYTES( p_tfra->p_traf_number[i*4] );
3325                 break;
3326             default:
3327                 goto error;
3328         }
3329
3330         switch (p_tfra->i_length_size_of_trun_num)
3331         {
3332             case 0:
3333                 MP4_GET1BYTE( p_tfra->p_trun_number[i] );
3334                 break;
3335             case 1:
3336                 MP4_GET2BYTES( p_tfra->p_trun_number[i*2] );
3337                 break;
3338             case 2:
3339                 MP4_GET3BYTES( p_tfra->p_trun_number[i*3] );
3340                 break;
3341             case 3:
3342                 MP4_GET4BYTES( p_tfra->p_trun_number[i*4] );
3343                 break;
3344             default:
3345                 goto error;
3346         }
3347
3348         switch (p_tfra->i_length_size_of_sample_num)
3349         {
3350             case 0:
3351                 MP4_GET1BYTE( p_tfra->p_sample_number[i] );
3352                 break;
3353             case 1:
3354                 MP4_GET2BYTES( p_tfra->p_sample_number[i*2] );
3355                 break;
3356             case 2:
3357                 MP4_GET3BYTES( p_tfra->p_sample_number[i*3] );
3358                 break;
3359             case 3:
3360                 MP4_GET4BYTES( p_tfra->p_sample_number[i*4] );
3361                 break;
3362             default:
3363                 goto error;
3364         }
3365     }
3366     if ( i < i_number_of_entries )
3367         i_number_of_entries = i;
3368
3369 #ifdef MP4_VERBOSE
3370     if( p_tfra->i_version == 0 )
3371     {
3372         msg_Dbg( p_stream, "time[0]: %"PRIu32", moof_offset[0]: %"PRIx32"",
3373                          p_tfra->p_time[0], p_tfra->p_moof_offset[0] );
3374
3375         msg_Dbg( p_stream, "time[1]: %"PRIu32", moof_offset[1]: %"PRIx32"",
3376                          p_tfra->p_time[1], p_tfra->p_moof_offset[1] );
3377     }
3378     else
3379     {
3380         msg_Dbg( p_stream, "time[0]: %"PRIu64", moof_offset[0]: %"PRIx64"",
3381                 ((uint64_t *)(p_tfra->p_time))[0],
3382                 ((uint64_t *)(p_tfra->p_moof_offset))[0] );
3383
3384         msg_Dbg( p_stream, "time[1]: %"PRIu64", moof_offset[1]: %"PRIx64"",
3385                 ((uint64_t *)(p_tfra->p_time))[1],
3386                 ((uint64_t *)(p_tfra->p_moof_offset))[1] );
3387     }
3388
3389     msg_Dbg( p_stream, "number_of_entries is %"PRIu32"", i_number_of_entries );
3390     msg_Dbg( p_stream, "track ID is: %"PRIu32"", p_tfra->i_track_ID );
3391 #endif
3392
3393     MP4_READBOX_EXIT( 1 );
3394 error:
3395     MP4_READBOX_EXIT( 0 );
3396 }
3397
3398 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
3399 {
3400     FREENULL( p_box->data.p_tfra->p_time );
3401     FREENULL( p_box->data.p_tfra->p_moof_offset );
3402     FREENULL( p_box->data.p_tfra->p_traf_number );
3403     FREENULL( p_box->data.p_tfra->p_trun_number );
3404     FREENULL( p_box->data.p_tfra->p_sample_number );
3405 }
3406
3407
3408 /* For generic */
3409 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
3410 {
3411     if( !p_box->p_father )
3412     {
3413         goto unknown;
3414     }
3415     if( p_box->p_father->i_type == ATOM_stsd )
3416     {
3417         MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
3418         MP4_Box_t *p_hdlr;
3419
3420         if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
3421             (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
3422         {
3423             goto unknown;
3424         }
3425         switch( p_hdlr->data.p_hdlr->i_handler_type )
3426         {
3427             case ATOM_soun:
3428                 return MP4_ReadBox_sample_soun( p_stream, p_box );
3429             case ATOM_vide:
3430                 return MP4_ReadBox_sample_vide( p_stream, p_box );
3431             case ATOM_text:
3432                 return MP4_ReadBox_sample_text( p_stream, p_box );
3433             case ATOM_tx3g:
3434             case ATOM_sbtl:
3435                 return MP4_ReadBox_sample_tx3g( p_stream, p_box );
3436             default:
3437                 msg_Warn( p_stream,
3438                           "unknown handler type in stsd (incompletely loaded)" );
3439                 return 1;
3440         }
3441     }
3442
3443 unknown:
3444     if MP4_BOX_TYPE_ASCII()
3445         msg_Warn( p_stream,
3446                 "unknown box type %4.4s (incompletely loaded)",
3447                 (char*)&p_box->i_type );
3448     else
3449         msg_Warn( p_stream,
3450                 "unknown box type c%3.3s (incompletely loaded)",
3451                 (char*)&p_box->i_type+1 );
3452     p_box->e_flags |= BOX_FLAG_INCOMPLETE;
3453
3454     return 1;
3455 }
3456
3457 /**** ------------------------------------------------------------------- ****/
3458 /****                   "Higher level" Functions                          ****/
3459 /**** ------------------------------------------------------------------- ****/
3460
3461 static const struct
3462 {
3463     uint32_t i_type;
3464     int  (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
3465     void (*MP4_FreeBox_function )( MP4_Box_t *p_box );
3466     uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
3467 } MP4_Box_Function [] =
3468 {
3469     /* Containers */
3470     { ATOM_moov,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3471     { ATOM_foov,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3472     { ATOM_trak,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_moov },
3473     { ATOM_trak,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_foov },
3474     { ATOM_mdia,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_trak },
3475     { ATOM_moof,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3476     { ATOM_minf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_mdia },
3477     { ATOM_stbl,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_minf },
3478     { ATOM_dinf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_minf },
3479     { ATOM_dinf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_meta },
3480     { ATOM_edts,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_trak },
3481     { ATOM_udta,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3482     { ATOM_nmhd,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_minf },
3483     { ATOM_hnti,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_udta },
3484     { ATOM_rmra,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_moov },
3485     { ATOM_rmda,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_rmra },
3486     { ATOM_tref,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_trak },
3487     { ATOM_gmhd,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_minf },
3488     { ATOM_wave,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_stsd },
3489     { ATOM_wave,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_WMA2 }, /* flip4mac */
3490     { ATOM_ilst,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_meta },
3491     { ATOM_mvex,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_moov },
3492     { ATOM_mvex,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, ATOM_ftyp },
3493
3494     /* specific box */
3495     { ATOM_ftyp,    MP4_ReadBox_ftyp,         MP4_FreeBox_ftyp, 0 },
3496     { ATOM_cmov,    MP4_ReadBox_cmov,         MP4_FreeBox_Common, 0 },
3497     { ATOM_mvhd,    MP4_ReadBox_mvhd,         MP4_FreeBox_Common, ATOM_moov },
3498     { ATOM_mvhd,    MP4_ReadBox_mvhd,         MP4_FreeBox_Common, ATOM_foov },
3499     { ATOM_tkhd,    MP4_ReadBox_tkhd,         MP4_FreeBox_Common, ATOM_trak },
3500     { ATOM_mdhd,    MP4_ReadBox_mdhd,         MP4_FreeBox_Common, ATOM_mdia },
3501     { ATOM_hdlr,    MP4_ReadBox_hdlr,         MP4_FreeBox_hdlr,   ATOM_mdia },
3502     { ATOM_hdlr,    MP4_ReadBox_hdlr,         MP4_FreeBox_hdlr,   ATOM_meta },
3503     { ATOM_hdlr,    MP4_ReadBox_hdlr,         MP4_FreeBox_hdlr,   ATOM_minf },
3504     { ATOM_vmhd,    MP4_ReadBox_vmhd,         MP4_FreeBox_Common, ATOM_minf },
3505     { ATOM_smhd,    MP4_ReadBox_smhd,         MP4_FreeBox_Common, ATOM_minf },
3506     { ATOM_hmhd,    MP4_ReadBox_hmhd,         MP4_FreeBox_Common, ATOM_minf },
3507     { ATOM_alis,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, ATOM_dref },
3508     { ATOM_url,     MP4_ReadBox_url,          MP4_FreeBox_url, 0 },
3509     { ATOM_urn,     MP4_ReadBox_urn,          MP4_FreeBox_urn, 0 },
3510     { ATOM_dref,    MP4_ReadBox_dref,         MP4_FreeBox_Common, 0 },
3511     { ATOM_stts,    MP4_ReadBox_stts,         MP4_FreeBox_stts,   ATOM_stbl },
3512     { ATOM_ctts,    MP4_ReadBox_ctts,         MP4_FreeBox_ctts,   ATOM_stbl },
3513     { ATOM_stsd,    MP4_ReadBox_stsd,         MP4_FreeBox_Common, ATOM_stbl },
3514     { ATOM_stsz,    MP4_ReadBox_stsz,         MP4_FreeBox_stsz,   ATOM_stbl },
3515     { ATOM_stsc,    MP4_ReadBox_stsc,         MP4_FreeBox_stsc,   ATOM_stbl },
3516     { ATOM_stco,    MP4_ReadBox_stco_co64,    MP4_FreeBox_stco_co64, ATOM_stbl },
3517     { ATOM_co64,    MP4_ReadBox_stco_co64,    MP4_FreeBox_stco_co64, ATOM_stbl },
3518     { ATOM_stss,    MP4_ReadBox_stss,         MP4_FreeBox_stss, ATOM_stbl },
3519     { ATOM_stsh,    MP4_ReadBox_stsh,         MP4_FreeBox_stsh, ATOM_stbl },
3520     { ATOM_stdp,    MP4_ReadBox_stdp,         MP4_FreeBox_stdp, 0 },
3521     { ATOM_padb,    MP4_ReadBox_padb,         MP4_FreeBox_padb, 0 },
3522     { ATOM_elst,    MP4_ReadBox_elst,         MP4_FreeBox_elst, ATOM_edts },
3523     { ATOM_cprt,    MP4_ReadBox_cprt,         MP4_FreeBox_cprt, 0 },
3524     { ATOM_esds,    MP4_ReadBox_esds,         MP4_FreeBox_esds, ATOM_wave }, /* mp4a in wave chunk */
3525     { ATOM_esds,    MP4_ReadBox_esds,         MP4_FreeBox_esds, ATOM_mp4a },
3526     { ATOM_esds,    MP4_ReadBox_esds,         MP4_FreeBox_esds, ATOM_mp4v },
3527     { ATOM_dcom,    MP4_ReadBox_dcom,         MP4_FreeBox_Common, 0 },
3528     { ATOM_cmvd,    MP4_ReadBox_cmvd,         MP4_FreeBox_cmvd, 0 },
3529     { ATOM_avcC,    MP4_ReadBox_avcC,         MP4_FreeBox_avcC, ATOM_avc1 },
3530     { ATOM_hvcC,    MP4_ReadBox_hvcC,         MP4_FreeBox_hvcC, 0 },
3531     { ATOM_dac3,    MP4_ReadBox_dac3,         MP4_FreeBox_Common, 0 },
3532     { ATOM_dec3,    MP4_ReadBox_dec3,         MP4_FreeBox_Common, 0 },
3533     { ATOM_dvc1,    MP4_ReadBox_dvc1,         MP4_FreeBox_Common, 0 },
3534     { ATOM_enda,    MP4_ReadBox_enda,         MP4_FreeBox_Common, 0 },
3535     { ATOM_iods,    MP4_ReadBox_iods,         MP4_FreeBox_Common, 0 },
3536     { ATOM_pasp,    MP4_ReadBox_pasp,         MP4_FreeBox_Common, 0 },
3537
3538     /* Nothing to do with this box */
3539     { ATOM_mdat,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3540     { ATOM_skip,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3541     { ATOM_free,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3542     { ATOM_wide,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3543     { ATOM_binm,    MP4_ReadBoxSkip,          MP4_FreeBox_Common, 0 },
3544
3545     /* Subtitles */
3546     { ATOM_tx3g,    MP4_ReadBox_sample_tx3g,      MP4_FreeBox_Common, 0 },
3547     //{ ATOM_text,    MP4_ReadBox_sample_text,      MP4_FreeBox_Common, 0 },
3548
3549     /* for codecs */
3550     { ATOM_soun,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3551     { ATOM_ac3,     MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3552     { ATOM_eac3,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3553     { ATOM_lpcm,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3554     { ATOM_ms02,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3555     { ATOM_ms11,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3556     { ATOM_ms55,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3557     { ATOM__mp3,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3558     { ATOM_mp4a,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3559     { ATOM_twos,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3560     { ATOM_sowt,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3561     { ATOM_QDMC,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3562     { ATOM_QDM2,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3563     { ATOM_ima4,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3564     { ATOM_IMA4,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3565     { ATOM_dvi,     MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3566     { ATOM_alaw,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3567     { ATOM_ulaw,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3568     { ATOM_raw,     MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3569     { ATOM_MAC3,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3570     { ATOM_MAC6,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3571     { ATOM_Qclp,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3572     { ATOM_samr,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3573     { ATOM_sawb,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3574     { ATOM_OggS,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3575     { ATOM_alac,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd },
3576     { ATOM_WMA2,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, ATOM_stsd }, /* flip4mac */
3577     /* Sound extensions */
3578     { ATOM_chan,    MP4_ReadBox_stsdext_chan, MP4_FreeBox_stsdext_chan, 0 },
3579     { ATOM_WMA2,    MP4_ReadBox_WMA2,         MP4_FreeBox_WMA2,        ATOM_wave }, /* flip4mac */
3580
3581     { ATOM_drmi,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3582     { ATOM_vide,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3583     { ATOM_mp4v,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3584     { ATOM_SVQ1,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3585     { ATOM_SVQ3,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3586     { ATOM_ZyGo,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3587     { ATOM_DIVX,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3588     { ATOM_XVID,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3589     { ATOM_h263,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3590     { ATOM_s263,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3591     { ATOM_cvid,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3592     { ATOM_3IV1,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3593     { ATOM_3iv1,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3594     { ATOM_3IV2,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3595     { ATOM_3iv2,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3596     { ATOM_3IVD,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3597     { ATOM_3ivd,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3598     { ATOM_3VID,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3599     { ATOM_3vid,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3600     { ATOM_mjpa,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3601     { ATOM_mjpb,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3602     { ATOM_qdrw,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3603     { ATOM_mp2v,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3604     { ATOM_hdv2,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3605     { ATOM_WMV3,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3606
3607     { ATOM_mjqt,    MP4_ReadBox_default,      NULL, 0 }, /* found in mjpa/b */
3608     { ATOM_mjht,    MP4_ReadBox_default,      NULL, 0 },
3609
3610     { ATOM_dvc,     MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3611     { ATOM_dvp,     MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3612     { ATOM_dv5n,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3613     { ATOM_dv5p,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3614     { ATOM_VP31,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3615     { ATOM_vp31,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3616     { ATOM_h264,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3617
3618     { ATOM_jpeg,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3619     { ATOM_avc1,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, ATOM_stsd },
3620
3621     { ATOM_yv12,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, 0 },
3622     { ATOM_yuv2,    MP4_ReadBox_sample_vide,  MP4_FreeBox_sample_vide, 0 },
3623
3624     { ATOM_strf,    MP4_ReadBox_strf,         MP4_FreeBox_strf,        ATOM_WMV3 }, /* flip4mac */
3625     { ATOM_ASF ,    MP4_ReadBox_ASF,          MP4_FreeBox_Common,      ATOM_WMV3 }, /* flip4mac */
3626     { ATOM_ASF ,    MP4_ReadBox_ASF,          MP4_FreeBox_Common,      ATOM_wave }, /* flip4mac */
3627
3628     { ATOM_mp4s,    MP4_ReadBox_sample_mp4s,  MP4_FreeBox_Common, 0 },
3629
3630     /* XXX there is 2 box where we could find this entry stbl and tref*/
3631     { ATOM_hint,    MP4_ReadBox_default,      MP4_FreeBox_Common, 0 },
3632
3633     /* found in tref box */
3634     { ATOM_dpnd,    MP4_ReadBox_default,      NULL, 0 },
3635     { ATOM_ipir,    MP4_ReadBox_default,      NULL, 0 },
3636     { ATOM_mpod,    MP4_ReadBox_default,      NULL, 0 },
3637     { ATOM_chap,    MP4_ReadBox_tref_generic, MP4_FreeBox_tref_generic, 0 },
3638
3639     /* found in hnti */
3640     { ATOM_rtp,     MP4_ReadBox_default,      NULL, 0 },
3641
3642     /* found in rmra/rmda */
3643     { ATOM_rdrf,    MP4_ReadBox_rdrf,         MP4_FreeBox_rdrf  , ATOM_rmda },
3644     { ATOM_rmdr,    MP4_ReadBox_rmdr,         MP4_FreeBox_Common, ATOM_rmda },
3645     { ATOM_rmqu,    MP4_ReadBox_rmqu,         MP4_FreeBox_Common, ATOM_rmda },
3646     { ATOM_rmvc,    MP4_ReadBox_rmvc,         MP4_FreeBox_Common, ATOM_rmda },
3647
3648     { ATOM_drms,    MP4_ReadBox_sample_soun,  MP4_FreeBox_sample_soun, 0 },
3649     { ATOM_sinf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3650     { ATOM_schi,    MP4_ReadBoxContainer,     MP4_FreeBox_Common, 0 },
3651     { ATOM_user,    MP4_ReadBox_drms,         MP4_FreeBox_Common, 0 },
3652     { ATOM_key,     MP4_ReadBox_drms,         MP4_FreeBox_Common, 0 },
3653     { ATOM_iviv,    MP4_ReadBox_drms,         MP4_FreeBox_Common, 0 },
3654     { ATOM_priv,    MP4_ReadBox_drms,         MP4_FreeBox_Common, 0 },
3655     { ATOM_frma,    MP4_ReadBox_frma,         MP4_FreeBox_Common, ATOM_sinf }, /* and rinf */
3656     { ATOM_frma,    MP4_ReadBox_frma,         MP4_FreeBox_Common, ATOM_wave }, /* flip4mac */
3657     { ATOM_skcr,    MP4_ReadBox_skcr,         MP4_FreeBox_Common, 0 },
3658
3659     /* found in udta */
3660     { ATOM_0xa9nam, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3661     { ATOM_0xa9aut, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3662     { ATOM_0xa9cpy, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3663     { ATOM_0xa9swr, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3664     { ATOM_0xa9inf, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3665     { ATOM_0xa9ART, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3666     { ATOM_0xa9dir, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3667     { ATOM_0xa9cmt, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3668     { ATOM_0xa9req, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3669     { ATOM_0xa9day, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3670     { ATOM_0xa9des, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3671     { ATOM_0xa9fmt, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3672     { ATOM_0xa9prd, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3673     { ATOM_0xa9prf, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3674     { ATOM_0xa9src, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3675     { ATOM_0xa9alb, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3676     { ATOM_0xa9dis, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3677     { ATOM_0xa9enc, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3678     { ATOM_0xa9gen, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3679     { ATOM_0xa9trk, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3680     { ATOM_0xa9dsa, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3681     { ATOM_0xa9hst, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3682     { ATOM_0xa9url, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3683     { ATOM_0xa9ope, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3684     { ATOM_0xa9com, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3685     { ATOM_0xa9wrt, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3686     { ATOM_0xa9too, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3687     { ATOM_0xa9wrn, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3688     { ATOM_0xa9mak, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3689     { ATOM_0xa9mod, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3690     { ATOM_0xa9PRD, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3691     { ATOM_0xa9grp, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3692     { ATOM_0xa9lyr, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3693     { ATOM_0xa9gen, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3694     { ATOM_0xa9st3, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3695     { ATOM_0xa9ard, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3696     { ATOM_0xa9arg, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3697     { ATOM_0xa9cak, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3698     { ATOM_0xa9con, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3699     { ATOM_0xa9des, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3700     { ATOM_0xa9lnt, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3701     { ATOM_0xa9phg, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3702     { ATOM_0xa9pub, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3703     { ATOM_0xa9sne, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3704     { ATOM_0xa9sol, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3705     { ATOM_0xa9thx, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3706     { ATOM_0xa9xpd, MP4_ReadBox_0xa9xxx,      MP4_FreeBox_0xa9xxx, ATOM_ilst },
3707     { ATOM_chpl,    MP4_ReadBox_chpl,         MP4_FreeBox_chpl,    ATOM_udta },
3708
3709     { ATOM_gnre,    MP4_ReadBox_gnre,         MP4_FreeBox_Common,  ATOM_ilst },
3710     { ATOM_trkn,    MP4_ReadBox_trkn,         MP4_FreeBox_Common,  ATOM_ilst },
3711
3712     /* iTunes/Quicktime meta info */
3713     { ATOM_meta,    MP4_ReadBox_meta,         MP4_FreeBox_Common,  0 },
3714     { ATOM_name,    MP4_ReadBox_name,         MP4_FreeBox_name,    0 },
3715     { ATOM_covr,    MP4_ReadBoxContainer,     MP4_FreeBox_Common,  ATOM_ilst },
3716     { ATOM_data,    MP4_ReadBox_data,         MP4_FreeBox_data,    0 },
3717
3718     /* found in smoothstreaming */
3719     { ATOM_traf,    MP4_ReadBoxContainer,     MP4_FreeBox_Common,  ATOM_moof },
3720     { ATOM_mfra,    MP4_ReadBoxContainer,     MP4_FreeBox_Common,  0 },
3721     { ATOM_mfhd,    MP4_ReadBox_mfhd,         MP4_FreeBox_Common,  ATOM_moof },
3722     { ATOM_sidx,    MP4_ReadBox_sidx,         MP4_FreeBox_sidx,    0 },
3723     { ATOM_tfhd,    MP4_ReadBox_tfhd,         MP4_FreeBox_Common,  ATOM_traf },
3724     { ATOM_trun,    MP4_ReadBox_trun,         MP4_FreeBox_trun,    ATOM_traf },
3725     { ATOM_trex,    MP4_ReadBox_trex,         MP4_FreeBox_Common,  ATOM_mvex },
3726     { ATOM_mehd,    MP4_ReadBox_mehd,         MP4_FreeBox_Common,  ATOM_mvex },
3727     { ATOM_sdtp,    MP4_ReadBox_sdtp,         MP4_FreeBox_sdtp,    0 },
3728     { ATOM_tfra,    MP4_ReadBox_tfra,         MP4_FreeBox_tfra,    ATOM_mfra },
3729     { ATOM_mfro,    MP4_ReadBox_mfro,         MP4_FreeBox_Common,  ATOM_mfra },
3730     { ATOM_uuid,    MP4_ReadBox_uuid,         MP4_FreeBox_uuid,    0 },
3731
3732     /* Last entry */
3733     { 0,              MP4_ReadBox_default,      NULL, 0 }
3734 };
3735
3736
3737 /*****************************************************************************
3738  * MP4_ReadBox : parse the actual box and the children
3739  *  XXX : Do not go to the next box
3740  *****************************************************************************/
3741 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
3742 {
3743     MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
3744     unsigned int i_index;
3745
3746     if( p_box == NULL )
3747         return NULL;
3748
3749     if( !MP4_ReadBoxCommon( p_stream, p_box ) )
3750     {
3751         msg_Warn( p_stream, "cannot read one box" );
3752         free( p_box );
3753         return NULL;
3754     }
3755     if( !p_box->i_size )
3756     {
3757         msg_Dbg( p_stream, "found an empty box (null size)" );
3758         free( p_box );
3759         return NULL;
3760     }
3761     p_box->p_father = p_father;
3762
3763     /* Now search function to call */
3764     for( i_index = 0; ; i_index++ )
3765     {
3766         if ( MP4_Box_Function[i_index].i_parent &&
3767              p_box->p_father &&
3768              p_box->p_father->i_type != MP4_Box_Function[i_index].i_parent )
3769             continue;
3770
3771         if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
3772             ( MP4_Box_Function[i_index].i_type == 0 ) )
3773         {
3774             break;
3775         }
3776     }
3777
3778     if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
3779     {
3780         MP4_BoxFree( p_stream, p_box );
3781         return NULL;
3782     }
3783
3784     p_box->pf_free = MP4_Box_Function[i_index].MP4_FreeBox_function;
3785
3786     return p_box;
3787 }
3788
3789 /*****************************************************************************
3790  * MP4_FreeBox : free memory after read with MP4_ReadBox and all
3791  * the children
3792  *****************************************************************************/
3793 void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
3794 {
3795     MP4_Box_t    *p_child;
3796
3797     if( !p_box )
3798         return; /* hehe */
3799
3800     for( p_child = p_box->p_first; p_child != NULL; )
3801     {
3802         MP4_Box_t *p_next;
3803
3804         p_next = p_child->p_next;
3805         MP4_BoxFree( s, p_child );
3806         p_child = p_next;
3807     }
3808
3809     /* Now search function to call */
3810     if( p_box->data.p_payload )
3811     {
3812         if (unlikely( p_box->pf_free == NULL ))
3813         {
3814             /* Should not happen */
3815             if MP4_BOX_TYPE_ASCII()
3816                 msg_Warn( s,
3817                         "cannot free box %4.4s, type unknown",
3818                         (char*)&p_box->i_type );
3819             else
3820                 msg_Warn( s,
3821                         "cannot free box c%3.3s, type unknown",
3822                         (char*)&p_box->i_type+1 );
3823         }
3824         else
3825         {
3826             p_box->pf_free( p_box );
3827         }
3828         free( p_box->data.p_payload );
3829     }
3830     free( p_box );
3831 }
3832
3833 /* SmooBox is a very simple MP4 box, VLC specific, used only for the stream_filter to
3834  * send information to the demux. SmooBox is actually a simplified moov box (we wanted
3835  * to avoid the hassle of building a moov box at the stream_filter level) */
3836 MP4_Box_t *MP4_BoxGetSmooBox( stream_t *s )
3837 {
3838     /* p_chunk is a virtual root container for the smoo box */
3839     MP4_Box_t *p_chunk;
3840     MP4_Box_t *p_smoo;
3841
3842     p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
3843     if( unlikely( p_chunk == NULL ) )
3844         return NULL;
3845
3846     p_chunk->i_type = ATOM_root;
3847     p_chunk->i_shortsize = 1;
3848
3849     p_smoo = MP4_ReadBox( s, p_chunk );
3850     if( !p_smoo || p_smoo->i_type != ATOM_uuid || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
3851     {
3852         msg_Warn( s, "no smoo box found!");
3853         goto error;
3854     }
3855
3856     p_chunk->p_first = p_smoo;
3857     p_chunk->p_last = p_smoo;
3858
3859     return p_chunk;
3860
3861 error:
3862     free( p_chunk );
3863     return NULL;
3864 }
3865
3866 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
3867 {
3868     /* p_chunk is a virtual root container for the moof and mdat boxes */
3869     MP4_Box_t *p_chunk;
3870     MP4_Box_t *p_tmp_box = NULL;
3871
3872     p_tmp_box = calloc( 1, sizeof( MP4_Box_t ) );
3873     if( unlikely( p_tmp_box == NULL ) )
3874         return NULL;
3875
3876     /* We might get a ftyp box or a SmooBox */
3877     MP4_ReadBoxCommon( s, p_tmp_box );
3878
3879     if( (p_tmp_box->i_type == ATOM_uuid && !CmpUUID( &p_tmp_box->i_uuid, &SmooBoxUUID )) )
3880     {
3881         free( p_tmp_box );
3882         return MP4_BoxGetSmooBox( s );
3883     }
3884     else if( p_tmp_box->i_type == ATOM_ftyp )
3885     {
3886         free( p_tmp_box );
3887         return MP4_BoxGetRoot( s );
3888     }
3889     free( p_tmp_box );
3890
3891     p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
3892     if( unlikely( p_chunk == NULL ) )
3893         return NULL;
3894
3895     p_chunk->i_type = ATOM_root;
3896     p_chunk->i_shortsize = 1;
3897
3898     MP4_ReadBoxContainerChildren( s, p_chunk, ATOM_moof );
3899
3900     p_tmp_box = p_chunk->p_first;
3901     while( p_tmp_box )
3902     {
3903         p_chunk->i_size += p_tmp_box->i_size;
3904         p_tmp_box = p_tmp_box->p_next;
3905     }
3906
3907     return p_chunk;
3908 }
3909
3910 /*****************************************************************************
3911  * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
3912  *****************************************************************************
3913  *  The first box is a virtual box "root" and is the father for all first
3914  *  level boxes for the file, a sort of virtual contener
3915  *****************************************************************************/
3916 MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
3917 {
3918     MP4_Box_t *p_root;
3919     stream_t *p_stream;
3920     int i_result;
3921
3922     p_root = malloc( sizeof( MP4_Box_t ) );
3923     if( p_root == NULL )
3924         return NULL;
3925
3926     p_root->i_pos = 0;
3927     p_root->i_type = ATOM_root;
3928     p_root->i_shortsize = 1;
3929     /* could be a DASH stream for exemple, 0 means unknown or infinite size */
3930     p_root->i_size = 0;
3931     CreateUUID( &p_root->i_uuid, p_root->i_type );
3932
3933     p_root->data.p_payload = NULL;
3934     p_root->p_father    = NULL;
3935     p_root->p_first     = NULL;
3936     p_root->p_last      = NULL;
3937     p_root->p_next      = NULL;
3938
3939     p_stream = s;
3940
3941     /* First get the moov */
3942     i_result = MP4_ReadBoxContainerChildren( p_stream, p_root, ATOM_moov );
3943
3944     if( !i_result )
3945         goto error;
3946     /* If there is a mvex box, it means fragmented MP4, and we're done */
3947     else if( MP4_BoxCount( p_root, "moov/mvex" ) > 0 )
3948         return p_root;
3949
3950     p_root->i_size = stream_Size( s );
3951     if( stream_Tell( s ) + 8 < stream_Size( s ) )
3952     {
3953         /* Get the rest of the file */
3954         i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
3955
3956         if( !i_result )
3957             goto error;
3958     }
3959
3960     MP4_Box_t *p_moov;
3961     MP4_Box_t *p_cmov;
3962
3963     /* check if there is a cmov, if so replace
3964       compressed moov by  uncompressed one */
3965     if( ( ( p_moov = MP4_BoxGet( p_root, "moov" ) ) &&
3966           ( p_cmov = MP4_BoxGet( p_root, "moov/cmov" ) ) ) ||
3967         ( ( p_moov = MP4_BoxGet( p_root, "foov" ) ) &&
3968           ( p_cmov = MP4_BoxGet( p_root, "foov/cmov" ) ) ) )
3969     {
3970         /* rename the compressed moov as a box to skip */
3971         p_moov->i_type = ATOM_skip;
3972
3973         /* get uncompressed p_moov */
3974         p_moov = p_cmov->data.p_cmov->p_moov;
3975         p_cmov->data.p_cmov->p_moov = NULL;
3976
3977         /* make p_root father of this new moov */
3978         p_moov->p_father = p_root;
3979
3980         /* insert this new moov box as first child of p_root */
3981         p_moov->p_next = p_root->p_first;
3982         p_root->p_first = p_moov;
3983     }
3984
3985     return p_root;
3986
3987 error:
3988     free( p_root );
3989     stream_Seek( p_stream, 0 );
3990     return NULL;
3991 }
3992
3993
3994 static void MP4_BoxDumpStructure_Internal( stream_t *s,
3995                                     MP4_Box_t *p_box, unsigned int i_level )
3996 {
3997     MP4_Box_t *p_child;
3998
3999     if( !i_level )
4000     {
4001         if MP4_BOX_TYPE_ASCII()
4002             msg_Dbg( s, "dumping root Box \"%4.4s\"",
4003                               (char*)&p_box->i_type );
4004         else
4005             msg_Dbg( s, "dumping root Box \"c%3.3s\"",
4006                               (char*)&p_box->i_type+1 );
4007     }
4008     else
4009     {
4010         char str[512];
4011         if( i_level >= (sizeof(str) - 1)/4 )
4012             return;
4013
4014         memset( str, ' ', sizeof(str) );
4015         for( unsigned i = 0; i < i_level; i++ )
4016         {
4017             str[i*4] = '|';
4018         }
4019         if( MP4_BOX_TYPE_ASCII() )
4020             snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
4021                       "+ %4.4s size %"PRIu64" offset %ld%s",
4022                         (char*)&p_box->i_type, p_box->i_size, p_box->i_pos,
4023                     p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
4024         else
4025             snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
4026                       "+ c%3.3s size %"PRIu64" offset %ld%s",
4027                         (char*)&p_box->i_type+1, p_box->i_size, p_box->i_pos,
4028                     p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
4029         msg_Dbg( s, "%s", str );
4030     }
4031     p_child = p_box->p_first;
4032     while( p_child )
4033     {
4034         MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
4035         p_child = p_child->p_next;
4036     }
4037 }
4038
4039 void MP4_BoxDumpStructure( stream_t *s, MP4_Box_t *p_box )
4040 {
4041     MP4_BoxDumpStructure_Internal( s, p_box, 0 );
4042 }
4043
4044
4045 /*****************************************************************************
4046  *****************************************************************************
4047  **
4048  **  High level methods to acces an MP4 file
4049  **
4050  *****************************************************************************
4051  *****************************************************************************/
4052 static void get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
4053 {
4054     size_t i_len ;
4055     if( !*ppsz_path[0] )
4056     {
4057         *ppsz_token = NULL;
4058         *pi_number = 0;
4059         return;
4060     }
4061     i_len = strcspn( *ppsz_path, "/[" );
4062     if( !i_len && **ppsz_path == '/' )
4063     {
4064         i_len = 1;
4065     }
4066     *ppsz_token = strndup( *ppsz_path, i_len );
4067     if( unlikely(!*ppsz_token) )
4068         abort();
4069
4070     *ppsz_path += i_len;
4071
4072     if( **ppsz_path == '[' )
4073     {
4074         (*ppsz_path)++;
4075         *pi_number = strtol( *ppsz_path, NULL, 10 );
4076         while( **ppsz_path && **ppsz_path != ']' )
4077         {
4078             (*ppsz_path)++;
4079         }
4080         if( **ppsz_path == ']' )
4081         {
4082             (*ppsz_path)++;
4083         }
4084     }
4085     else
4086     {
4087         *pi_number = 0;
4088     }
4089     while( **ppsz_path == '/' )
4090     {
4091         (*ppsz_path)++;
4092     }
4093 }
4094
4095 static void MP4_BoxGet_Internal( MP4_Box_t **pp_result,
4096                           MP4_Box_t *p_box, const char *psz_fmt, va_list args)
4097 {
4098     char *psz_dup;
4099     char *psz_path;
4100     char *psz_token;
4101
4102     if( !p_box )
4103     {
4104         *pp_result = NULL;
4105         return;
4106     }
4107
4108     if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
4109         psz_path = NULL;
4110
4111     if( !psz_path || !psz_path[0] )
4112     {
4113         free( psz_path );
4114         *pp_result = NULL;
4115         return;
4116     }
4117
4118 //    fprintf( stderr, "path:'%s'\n", psz_path );
4119     psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
4120     for( ; ; )
4121     {
4122         int i_number;
4123
4124         get_token( &psz_path, &psz_token, &i_number );
4125 //        fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
4126 //                 psz_path,psz_token,i_number );
4127         if( !psz_token )
4128         {
4129             free( psz_dup );
4130             *pp_result = p_box;
4131             return;
4132         }
4133         else
4134         if( !strcmp( psz_token, "/" ) )
4135         {
4136             /* Find root box */
4137             while( p_box && p_box->i_type != ATOM_root )
4138             {
4139                 p_box = p_box->p_father;
4140             }
4141             if( !p_box )
4142             {
4143                 goto error_box;
4144             }
4145         }
4146         else
4147         if( !strcmp( psz_token, "." ) )
4148         {
4149             /* Do nothing */
4150         }
4151         else
4152         if( !strcmp( psz_token, ".." ) )
4153         {
4154             p_box = p_box->p_father;
4155             if( !p_box )
4156             {
4157                 goto error_box;
4158             }
4159         }
4160         else
4161         if( strlen( psz_token ) == 4 )
4162         {
4163             uint32_t i_fourcc;
4164             i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
4165                                    psz_token[2], psz_token[3] );
4166             p_box = p_box->p_first;
4167             for( ; ; )
4168             {
4169                 if( !p_box )
4170                 {
4171                     goto error_box;
4172                 }
4173                 if( p_box->i_type == i_fourcc )
4174                 {
4175                     if( !i_number )
4176                     {
4177                         break;
4178                     }
4179                     i_number--;
4180                 }
4181                 p_box = p_box->p_next;
4182             }
4183         }
4184         else
4185         if( *psz_token == '\0' )
4186         {
4187             p_box = p_box->p_first;
4188             for( ; ; )
4189             {
4190                 if( !p_box )
4191                 {
4192                     goto error_box;
4193                 }
4194                 if( !i_number )
4195                 {
4196                     break;
4197                 }
4198                 i_number--;
4199                 p_box = p_box->p_next;
4200             }
4201         }
4202         else
4203         {
4204 //            fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
4205             goto error_box;
4206         }
4207
4208         FREENULL( psz_token );
4209     }
4210
4211     return;
4212
4213 error_box:
4214     free( psz_token );
4215     free( psz_dup );
4216     *pp_result = NULL;
4217     return;
4218 }
4219
4220 /*****************************************************************************
4221  * MP4_BoxGet: find a box given a path relative to p_box
4222  *****************************************************************************
4223  * Path Format: . .. / as usual
4224  *              [number] to specifie box number ex: trak[12]
4225  *
4226  * ex: /moov/trak[12]
4227  *     ../mdia
4228  *****************************************************************************/
4229 MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, const char *psz_fmt, ... )
4230 {
4231     va_list args;
4232     MP4_Box_t *p_result;
4233
4234     va_start( args, psz_fmt );
4235     MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
4236     va_end( args );
4237
4238     return( p_result );
4239 }
4240
4241 /*****************************************************************************
4242  * MP4_BoxCount: count box given a path relative to p_box
4243  *****************************************************************************
4244  * Path Format: . .. / as usual
4245  *              [number] to specifie box number ex: trak[12]
4246  *
4247  * ex: /moov/trak[12]
4248  *     ../mdia
4249  *****************************************************************************/
4250 int MP4_BoxCount( MP4_Box_t *p_box, const char *psz_fmt, ... )
4251 {
4252     va_list args;
4253     int     i_count;
4254     MP4_Box_t *p_result, *p_next;
4255
4256     va_start( args, psz_fmt );
4257     MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
4258     va_end( args );
4259     if( !p_result )
4260     {
4261         return( 0 );
4262     }
4263
4264     i_count = 1;
4265     for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
4266     {
4267         if( p_next->i_type == p_result->i_type)
4268         {
4269             i_count++;
4270         }
4271     }
4272     return( i_count );
4273 }