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