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