root/modules/demux/mp4/libmp4.c

Revision fbd8d6fc5deaefb2f9eebb2146203c8f0985d9fd, 95.5 kB (checked in by Rémi Duraffort <ivoire@videolan.org>, 3 months ago)

Don't use FREENULL when that's not needed and don't free NULL just for fun.

  • Property mode set to 100644
Line 
1 /*****************************************************************************
2  * libmp4.c : LibMP4 library for mp4 module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28
29
30 #include <vlc_demux.h>
31
32 #ifdef HAVE_ZLIB_H
33 #   include <zlib.h>                                  /* for compressed moov */
34 #endif
35
36 #include "libmp4.h"
37 #include "drms.h"
38
39 /*****************************************************************************
40  * Here are defined some macro to make life simpler but before using it
41  *  *look* at the code.
42  *
43  *****************************************************************************/
44 #define MP4_BOX_HEADERSIZE( p_box )             \
45   ( 8 + ( p_box->i_shortsize == 1 ? 8 : 0 )     \
46       + ( p_box->i_type == FOURCC_uuid ? 16 : 0 ) )
47
48 #define MP4_GETX_PRIVATE(dst, code, size) do { \
49     if( (i_read) >= (size) ) { dst = (code); p_peek += (size); } \
50     else { dst = 0; }   \
51     i_read -= (size);   \
52   } while(0)
53
54 #define MP4_GET1BYTE( dst )  MP4_GETX_PRIVATE( dst, *p_peek, 1 )
55 #define MP4_GET2BYTES( dst ) MP4_GETX_PRIVATE( dst, GetWBE(p_peek), 2 )
56 #define MP4_GET3BYTES( dst ) MP4_GETX_PRIVATE( dst, Get24bBE(p_peek), 3 )
57 #define MP4_GET4BYTES( dst ) MP4_GETX_PRIVATE( dst, GetDWBE(p_peek), 4 )
58 #define MP4_GET8BYTES( dst ) MP4_GETX_PRIVATE( dst, GetQWBE(p_peek), 8 )
59 #define MP4_GETFOURCC( dst ) MP4_GETX_PRIVATE( dst, \
60                 VLC_FOURCC(p_peek[0],p_peek[1],p_peek[2],p_peek[3]), 4)
61
62 #define MP4_GETVERSIONFLAGS( p_void ) \
63     MP4_GET1BYTE( p_void->i_version ); \
64     MP4_GET3BYTES( p_void->i_flags )
65
66 #define MP4_GETSTRINGZ( p_str )         \
67     if( (i_read > 0) && (p_peek[0]) )   \
68     {       \
69         const int __i_copy__ = strnlen( (char*)p_peek, i_read-1 );  \
70         p_str = malloc( __i_copy__+1 );               \
71         if( p_str ) \
72         { \
73              memcpy( p_str, p_peek, __i_copy__ ); \
74              p_str[__i_copy__] = 0; \
75         } \
76         p_peek += __i_copy__ + 1;   \
77         i_read -= __i_copy__ + 1;   \
78     }       \
79     else    \
80     {       \
81         p_str = NULL; \
82     }
83
84 #define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t ) \
85     int64_t  i_read = p_box->i_size; \
86     uint8_t *p_peek, *p_buff; \
87     int i_actually_read; \
88     if( !( p_peek = p_buff = malloc( i_read ) ) ) \
89     { \
90         return( 0 ); \
91     } \
92     i_actually_read = stream_Read( p_stream, p_peek, i_read ); \
93     if( i_actually_read < 0 || (int64_t)i_actually_read < i_read )\
94     { \
95         free( p_buff ); \
96         return( 0 ); \
97     } \
98     p_peek += MP4_BOX_HEADERSIZE( p_box ); \
99     i_read -= MP4_BOX_HEADERSIZE( p_box ); \
100     if( !( p_box->data.p_data = calloc( 1, sizeof( MP4_Box_data_TYPE_t ) ) ) ) \
101     { \
102       free( p_buff ); \
103       return( 0 ); \
104     }
105
106 #define MP4_READBOX_EXIT( i_code ) \
107     do \
108     { \
109         free( p_buff ); \
110         if( i_read < 0 ) \
111             msg_Warn( p_stream, "Not enough data" ); \
112         return( i_code ); \
113     } while (0)
114
115
116 /* Some assumptions:
117         * The input method HAVE to be seekable
118
119 */
120
121 /* This macro is used when we want to printf the box type
122  * APPLE annotation box is :
123  *  either 0xA9 + 24-bit ASCII text string (and 0xA9 isn't printable)
124  *  either 32-bit ASCII text string
125  */
126 #define MP4_BOX_TYPE_ASCII() ( ((char*)&p_box->i_type)[0] != (char)0xA9 )
127
128 static uint32_t Get24bBE( const uint8_t *p )
129 {
130     return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
131 }
132
133 static void GetUUID( UUID_t *p_uuid, const uint8_t *p_buff )
134 {
135     memcpy( p_uuid, p_buff, 16 );
136 }
137
138 static void CreateUUID( UUID_t *p_uuid, uint32_t i_fourcc )
139 {
140     /* made by 0xXXXXXXXX-0011-0010-8000-00aa00389b71
141             where XXXXXXXX is the fourcc */
142     /* FIXME implement this */
143     (void)p_uuid;
144     (void)i_fourcc;
145 }
146
147 /* some functions for mp4 encoding of variables */
148
149 static void MP4_ConvertDate2Str( char *psz, uint64_t i_date )
150 {
151     int i_day;
152     int i_hour;
153     int i_min;
154     int i_sec;
155
156     /* date begin at 1 jan 1904 */
157     i_date += ((INT64_C(1904) * 365) + 17) * 24 * 60 * 60;
158
159     i_day = i_date / ( 60*60*24);
160     i_hour = ( i_date /( 60*60 ) ) % 60;
161     i_min  = ( i_date / 60 ) % 60;
162     i_sec =  i_date % 60;
163     sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds", i_day, i_hour, i_min, i_sec );
164 }
165
166 /*****************************************************************************
167  * Some prototypes.
168  *****************************************************************************/
169 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
170
171
172 /*****************************************************************************
173  * MP4_ReadBoxCommon : Load only common parameters for all boxes
174  *****************************************************************************
175  * p_box need to be an already allocated MP4_Box_t, and all data
176  *  will only be peek not read
177  *
178  * RETURN : 0 if it fail, 1 otherwise
179  *****************************************************************************/
180 int MP4_ReadBoxCommon( stream_t *p_stream, MP4_Box_t *p_box )
181 {
182     int      i_read;
183     const uint8_t  *p_peek;
184
185     if( ( ( i_read = stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
186     {
187         return 0;
188     }
189     p_box->i_pos = stream_Tell( p_stream );
190
191     p_box->data.p_data = NULL;
192     p_box->p_father = NULL;
193     p_box->p_first  = NULL;
194     p_box->p_last  = NULL;
195     p_box->p_next   = NULL;
196
197     MP4_GET4BYTES( p_box->i_shortsize );
198     MP4_GETFOURCC( p_box->i_type );
199
200     /* Now special case */
201
202     if( p_box->i_shortsize == 1 )
203     {
204         /* get the true size on 64 bits */
205         MP4_GET8BYTES( p_box->i_size );
206     }
207     else
208     {
209         p_box->i_size = p_box->i_shortsize;
210         /* XXX size of 0 means that the box extends to end of file */
211     }
212
213     if( p_box->i_type == FOURCC_uuid )
214     {
215         /* get extented type on 16 bytes */
216         GetUUID( &p_box->i_uuid, p_peek );
217         p_peek += 16; i_read -= 16;
218     }
219     else
220     {
221         CreateUUID( &p_box->i_uuid, p_box->i_type );
222     }
223 #ifdef MP4_VERBOSE
224     if( p_box->i_size )
225     {
226         if MP4_BOX_TYPE_ASCII()
227             msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64,
228                     (char*)&p_box->i_type, p_box->i_size );
229         else
230             msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
231                     (char*)&p_box->i_type+1, p_box->i_size );
232     }
233 #endif
234
235     return 1;
236 }
237
238 /*****************************************************************************
239  * MP4_NextBox : Go to the next box
240  *****************************************************************************
241  * if p_box == NULL, go to the next box in which we are( at the begining ).
242  *****************************************************************************/
243 static int MP4_NextBox( stream_t *p_stream, MP4_Box_t *p_box )
244 {
245     MP4_Box_t box;
246
247     if( !p_box )
248     {
249         MP4_ReadBoxCommon( p_stream, &box );
250         p_box = &box;
251     }
252
253     if( !p_box->i_size )
254     {
255         return 2; /* Box with infinite size */
256     }
257
258     if( p_box->p_father )
259     {
260         const off_t i_box_end = p_box->i_size + p_box->i_pos;
261         const off_t i_father_end = p_box->p_father->i_size + p_box->p_father->i_pos;
262
263         /* check if it's within p-father */
264         if( i_box_end >= i_father_end )
265         {
266             if( i_box_end > i_father_end )
267                 msg_Dbg( p_stream, "out of bound child" );
268             return 0; /* out of bound */
269         }
270     }
271     if( stream_Seek( p_stream, p_box->i_size + p_box->i_pos ) )
272     {
273         return 0;
274     }
275
276     return 1;
277 }
278
279 /*****************************************************************************
280  * For all known box a loader is given,
281  *  XXX: all common struct have to be already read by MP4_ReadBoxCommon
282  *       after called one of theses functions, file position is unknown
283  *       you need to call MP4_GotoBox to go where you want
284  *****************************************************************************/
285 static int MP4_ReadBoxContainerRaw( stream_t *p_stream, MP4_Box_t *p_container )
286 {
287     MP4_Box_t *p_box;
288
289     if( stream_Tell( p_stream ) + 8 >
290         (off_t)(p_container->i_pos + p_container->i_size) )
291     {
292         /* there is no box to load */
293         return 0;
294     }
295
296     do
297     {
298         if( ( p_box = MP4_ReadBox( p_stream, p_container ) ) == NULL ) break;
299
300         /* chain this box with the father and the other at same level */
301         if( !p_container->p_first ) p_container->p_first = p_box;
302         else p_container->p_last->p_next = p_box;
303         p_container->p_last = p_box;
304
305     } while( MP4_NextBox( p_stream, p_box ) == 1 );
306
307     return 1;
308 }
309
310 static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
311 {
312     if( p_container->i_size <= (size_t)MP4_BOX_HEADERSIZE(p_container ) + 8 )
313     {
314         /* container is empty, 8 stand for the first header in this box */
315         return 1;
316     }
317
318     /* enter box */
319     stream_Seek( p_stream, p_container->i_pos +
320                  MP4_BOX_HEADERSIZE( p_container ) );
321
322     return MP4_ReadBoxContainerRaw( p_stream, p_container );
323 }
324
325 static void MP4_FreeBox_Common( MP4_Box_t *p_box )
326 {
327     /* Up to now do nothing */
328     (void)p_box;
329 }
330
331 static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
332 {
333     /* XXX sometime moov is hiden in a free box */
334     if( p_box->p_father &&
335         p_box->p_father->i_type == VLC_FOURCC( 'r', 'o', 'o', 't' ) &&
336         p_box->i_type == FOURCC_free )
337     {
338         const uint8_t *p_peek;
339         int     i_read;
340         vlc_fourcc_t i_fcc;
341
342         i_read  = stream_Peek( p_stream, &p_peek, 44 );
343
344         p_peek += MP4_BOX_HEADERSIZE( p_box ) + 4;
345         i_read -= MP4_BOX_HEADERSIZE( p_box ) + 4;
346
347         if( i_read >= 8 )
348         {
349             i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
350
351             if( i_fcc == FOURCC_cmov || i_fcc == FOURCC_mvhd )
352             {
353                 msg_Warn( p_stream, "detected moov hidden in a free box ..." );
354
355                 p_box->i_type = FOURCC_foov;
356                 return MP4_ReadBoxContainer( p_stream, p_box );
357             }
358         }
359     }
360
361     /* Nothing to do */
362 #ifdef MP4_VERBOSE
363     if MP4_BOX_TYPE_ASCII()
364         msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
365     else
366         msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
367 #endif
368     return 1;
369 }
370
371 static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
372 {
373     MP4_READBOX_ENTER( MP4_Box_data_ftyp_t );
374
375     MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
376     MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
377
378     if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
379     {
380         unsigned int i;
381         uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
382             calloc( p_box->data.p_ftyp->i_compatible_brands_count,
383                     sizeof(uint32_t));
384
385         if( tab == NULL )
386             MP4_READBOX_EXIT( 0 );
387
388         for( i =0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
389         {
390             MP4_GETFOURCC( tab[i] );
391         }
392     }
393     else
394     {
395         p_box->data.p_ftyp->i_compatible_brands = NULL;
396     }
397
398     MP4_READBOX_EXIT( 1 );
399 }
400
401 static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
402 {
403     FREENULL( p_box->data.p_ftyp->i_compatible_brands );
404 }
405
406
407 static int MP4_ReadBox_mvhd(  stream_t *p_stream, MP4_Box_t *p_box )
408 {
409     unsigned int i;
410 #ifdef MP4_VERBOSE
411     char s_creation_time[128];
412     char s_modification_time[128];
413     char s_duration[128];
414 #endif
415     MP4_READBOX_ENTER( MP4_Box_data_mvhd_t );
416
417     MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
418
419     if( p_box->data.p_mvhd->i_version )
420     {
421         MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
422         MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
423         MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
424         MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
425     }
426     else
427     {
428         MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
429         MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
430         MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
431         MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
432     }
433     MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
434     MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
435     MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
436
437
438     for( i = 0; i < 2; i++ )
439     {
440         MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
441     }
442     for( i = 0; i < 9; i++ )
443     {
444         MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
445     }
446     for( i = 0; i < 6; i++ )
447     {
448         MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
449     }
450
451     MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
452
453
454 #ifdef MP4_VERBOSE
455     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
456     MP4_ConvertDate2Str( s_modification_time,
457                          p_box->data.p_mvhd->i_modification_time );
458     if( p_box->data.p_mvhd->i_rate )
459     {
460         MP4_ConvertDate2Str( s_duration,
461                  p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_rate );
462     }
463     else
464     {
465         s_duration[0] = 0;
466     }
467     msg_Dbg( p_stream, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
468                   s_creation_time,
469                   s_modification_time,
470                   (uint32_t)p_box->data.p_mvhd->i_timescale,
471                   s_duration,
472                   (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
473                   (float)p_box->data.p_mvhd->i_volume / 256 ,
474                   (uint32_t)p_box->data.p_mvhd->i_next_track_id );
475 #endif
476     MP4_READBOX_EXIT( 1 );
477 }
478
479 static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
480 {
481     unsigned int i;
482 #ifdef MP4_VERBOSE
483     char s_creation_time[128];
484     char s_modification_time[128];
485     char s_duration[128];
486 #endif
487     MP4_READBOX_ENTER( MP4_Box_data_tkhd_t );
488
489     MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
490
491     if( p_box->data.p_tkhd->i_version )
492     {
493         MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
494         MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
495         MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
496         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
497         MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
498     }
499     else
500     {
501         MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
502         MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
503         MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
504         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
505         MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
506     }
507
508     for( i = 0; i < 2; i++ )
509     {
510         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
511     }
512     MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
513     MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
514     MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
515     MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
516
517     for( i = 0; i < 9; i++ )
518     {
519         MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
520     }
521     MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
522     MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
523
524 #ifdef MP4_VERBOSE
525     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
526     MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time );
527     MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration );
528
529     msg_Dbg( p_stream, "read box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f width %f height %f",
530                   s_creation_time,
531                   s_modification_time,
532                   s_duration,
533                   p_box->data.p_tkhd->i_track_ID,
534                   p_box->data.p_tkhd->i_layer,
535                   (float)p_box->data.p_tkhd->i_volume / 256 ,
536                   (float)p_box->data.p_tkhd->i_width / 65536,
537                   (float)p_box->data.p_tkhd->i_height / 65536 );
538 #endif
539     MP4_READBOX_EXIT( 1 );
540 }
541
542
543 static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
544 {
545     unsigned int i;
546     uint16_t i_language;
547 #ifdef MP4_VERBOSE
548     char s_creation_time[128];
549     char s_modification_time[128];
550     char s_duration[128];
551 #endif
552     MP4_READBOX_ENTER( MP4_Box_data_mdhd_t );
553
554     MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
555
556     if( p_box->data.p_mdhd->i_version )
557     {
558         MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
559         MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
560         MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
561         MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
562     }
563     else
564     {
565         MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
566         MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
567         MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
568         MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
569     }
570     p_box->data.p_mdhd->i_language_code = i_language = GetWBE( p_peek );
571     for( i = 0; i < 3; i++ )
572     {
573         p_box->data.p_mdhd->i_language[i] =
574                     ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
575     }
576
577     MP4_GET2BYTES( p_box->data.p_mdhd->i_predefined );
578
579 #ifdef MP4_VERBOSE
580     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time );
581     MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time );
582     MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration );
583     msg_Dbg( p_stream, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %c%c%c",
584                   s_creation_time,
585                   s_modification_time,
586                   (uint32_t)p_box->data.p_mdhd->i_timescale,
587                   s_duration,
588                   p_box->data.p_mdhd->i_language[0],
589                   p_box->data.p_mdhd->i_language[1],
590                   p_box->data.p_mdhd->i_language[2] );
591 #endif
592     MP4_READBOX_EXIT( 1 );
593 }
594
595
596 static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
597 {
598     int32_t i_reserved;
599
600     MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
601
602     MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
603
604     MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
605     MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
606
607     MP4_GET4BYTES( i_reserved );
608     MP4_GET4BYTES( i_reserved );
609     MP4_GET4BYTES( i_reserved );
610     p_box->data.p_hdlr->psz_name = NULL;
611
612     if( i_read > 0 )
613     {
614         uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_read + 1 );
615         if( psz == NULL )
616             MP4_READBOX_EXIT( 0 );
617
618         /* Yes, I love .mp4 :( */
619         if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
620         {
621             uint8_t i_len;
622             int i_copy;
623
624             MP4_GET1BYTE( i_len );
625             i_copy = __MIN( i_read, i_len );
626
627             memcpy( psz, p_peek, i_copy );
628             p_box->data.p_hdlr->psz_name[i_copy] = '\0';
629         }
630         else
631         {
632             memcpy( psz, p_peek, i_read );
633             p_box->data.p_hdlr->psz_name[i_read] = '\0';
634         }
635     }
636
637 #ifdef MP4_VERBOSE
638         msg_Dbg( p_stream, "read box: \"hdlr\" handler type %4.4s name %s",
639                    (char*)&p_box->data.p_hdlr->i_handler_type,
640                    p_box->data.p_hdlr->psz_name );
641
642 #endif
643     MP4_READBOX_EXIT( 1 );
644 }
645
646 static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
647 {
648     FREENULL( p_box->data.p_hdlr->psz_name );
649 }
650
651 static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
652 {
653     unsigned int i;
654
655     MP4_READBOX_ENTER( MP4_Box_data_vmhd_t );
656
657     MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
658
659     MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
660     for( i = 0; i < 3; i++ )
661     {
662         MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
663     }
664
665 #ifdef MP4_VERBOSE
666     msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
667                       p_box->data.p_vmhd->i_graphics_mode,
668                       p_box->data.p_vmhd->i_opcolor[0],
669                       p_box->data.p_vmhd->i_opcolor[1],
670                       p_box->data.p_vmhd->i_opcolor[2] );
671 #endif
672     MP4_READBOX_EXIT( 1 );
673 }
674
675 static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
676 {
677     MP4_READBOX_ENTER( MP4_Box_data_smhd_t );
678
679     MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
680
681
682
683     MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
684
685     MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
686
687 #ifdef MP4_VERBOSE
688     msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
689                       (float)p_box->data.p_smhd->i_balance / 256 );
690 #endif
691     MP4_READBOX_EXIT( 1 );
692 }
693
694
695 static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
696 {
697     MP4_READBOX_ENTER( MP4_Box_data_hmhd_t );
698
699     MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
700
701     MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
702     MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
703
704     MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
705     MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
706
707     MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
708
709 #ifdef MP4_VERBOSE
710     msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
711                       p_box->data.p_hmhd->i_max_PDU_size,
712                       p_box->data.p_hmhd->i_avg_PDU_size,
713                       p_box->data.p_hmhd->i_max_bitrate,
714                       p_box->data.p_hmhd->i_avg_bitrate );
715 #endif
716     MP4_READBOX_EXIT( 1 );
717 }
718
719 static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
720 {
721     MP4_READBOX_ENTER( MP4_Box_data_url_t );
722
723     MP4_GETVERSIONFLAGS( p_box->data.p_url );
724     MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
725
726 #ifdef MP4_VERBOSE
727     msg_Dbg( p_stream, "read box: \"url\" url: %s",
728                        p_box->data.p_url->psz_location );
729
730 #endif
731     MP4_READBOX_EXIT( 1 );
732 }
733
734
735 static void MP4_FreeBox_url( MP4_Box_t *p_box )
736 {
737     FREENULL( p_box->data.p_url->psz_location );
738 }
739
740 static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
741 {
742     MP4_READBOX_ENTER( MP4_Box_data_urn_t );
743
744     MP4_GETVERSIONFLAGS( p_box->data.p_urn );
745
746     MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
747     MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
748
749 #ifdef MP4_VERBOSE
750     msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
751                       p_box->data.p_urn->psz_name,
752                       p_box->data.p_urn->psz_location );
753 #endif
754     MP4_READBOX_EXIT( 1 );
755 }
756 static void MP4_FreeBox_urn( MP4_Box_t *p_box )
757 {
758     FREENULL( p_box->data.p_urn->psz_name );
759     FREENULL( p_box->data.p_urn->psz_location );
760 }
761
762
763 static int MP4_ReadBox_dref( stream_t *p_stream, MP4_Box_t *p_box )
764 {
765     MP4_READBOX_ENTER( MP4_Box_data_dref_t );
766
767     MP4_GETVERSIONFLAGS( p_box->data.p_dref );
768
769     MP4_GET4BYTES( p_box->data.p_dref->i_entry_count );
770
771     stream_Seek( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 );
772     MP4_ReadBoxContainerRaw( p_stream, p_box );
773
774 #ifdef MP4_VERBOSE
775     msg_Dbg( p_stream, "read box: \"dref\" entry-count %d",
776                       p_box->data.p_dref->i_entry_count );
777
778 #endif
779     MP4_READBOX_EXIT( 1 );
780 }
781
782 static void MP4_FreeBox_stts( MP4_Box_t *p_box )
783 {
784     FREENULL( p_box->data.p_stts->i_sample_count );
785     FREENULL( p_box->data.p_stts->i_sample_delta );
786 }
787
788 static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
789 {
790     unsigned int i;
791     MP4_READBOX_ENTER( MP4_Box_data_stts_t );
792
793     MP4_GETVERSIONFLAGS( p_box->data.p_stts );
794     MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );
795
796     p_box->data.p_stts->i_sample_count =
797         calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
798     p_box->data.p_stts->i_sample_delta =
799         calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
800     if( p_box->data.p_stts->i_sample_count == NULL
801      || p_box->data.p_stts->i_sample_delta == NULL )
802     {
803         MP4_READBOX_EXIT( 0 );
804     }
805
806     for( i = 0; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
807     {
808         MP4_GET4BYTES( p_box->data.p_stts->i_sample_count[i] );
809         MP4_GET4BYTES( p_box->data.p_stts->i_sample_delta[i] );
810     }
811
812 #ifdef MP4_VERBOSE
813     msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
814                       p_box->data.p_stts->i_entry_count );
815
816 #endif
817     MP4_READBOX_EXIT( 1 );
818 }
819
820
821 static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
822 {
823     FREENULL( p_box->data.p_ctts->i_sample_count );
824     FREENULL( p_box->data.p_ctts->i_sample_offset );
825 }
826
827 static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
828 {
829     unsigned int i;
830     MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
831
832     MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
833
834     MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );
835
836     p_box->data.p_ctts->i_sample_count =
837         calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
838     p_box->data.p_ctts->i_sample_offset =
839         calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
840     if( ( p_box->data.p_ctts->i_sample_count == NULL )
841      || ( p_box->data.p_ctts->i_sample_offset == NULL ) )
842     {
843         MP4_READBOX_EXIT( 0 );
844     }
845
846     for( i = 0; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
847     {
848         MP4_GET4BYTES( p_box->data.p_ctts->i_sample_count[i] );
849         MP4_GET4BYTES( p_box->data.p_ctts->i_sample_offset[i] );
850     }
851
852 #ifdef MP4_VERBOSE
853     msg_Dbg( p_stream, "read box: \"ctts\" entry-count %d",
854                       p_box->data.p_ctts->i_entry_count );
855
856 #endif
857     MP4_READBOX_EXIT( 1 );
858 }
859
860
861 static int MP4_ReadLengthDescriptor( uint8_t **pp_peek, int64_t  *i_read )
862 {
863     unsigned int i_b;
864     unsigned int i_len = 0;
865     do
866     {
867         i_b = **pp_peek;
868
869         (*pp_peek)++;
870         (*i_read)--;
871         i_len = ( i_len << 7 ) + ( i_b&0x7f );
872     } while( i_b&0x80 );
873     return( i_len );
874 }
875
876
877 static void MP4_FreeBox_esds( MP4_Box_t *p_box )
878 {
879     FREENULL( p_box->data.p_esds->es_descriptor.psz_URL );
880     if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
881     {
882         FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
883         FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
884     }
885 }
886
887 static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
888 {
889 #define es_descriptor p_box->data.p_esds->es_descriptor
890     unsigned int i_len;
891     unsigned int i_flags;
892     unsigned int i_type;
893
894     MP4_READBOX_ENTER( MP4_Box_data_esds_t );
895
896     MP4_GETVERSIONFLAGS( p_box->data.p_esds );
897
898
899     MP4_GET1BYTE( i_type );
900     if( i_type == 0x03 ) /* MP4ESDescrTag */
901     {
902         i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
903
904 #ifdef MP4_VERBOSE
905         msg_Dbg( p_stream, "found esds MPEG4ESDescr (%dBytes)",
906                  i_len );
907 #endif
908
909         MP4_GET2BYTES( es_descriptor.i_ES_ID );
910         MP4_GET1BYTE( i_flags );
911         es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
912         es_descriptor.b_url = ( (i_flags&0x40) != 0);
913         es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
914
915         es_descriptor.i_stream_priority = i_flags&0x1f;
916         if( es_descriptor.b_stream_dependence )
917         {
918             MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
919         }
920         if( es_descriptor.b_url )
921         {
922             unsigned int i_len;
923
924             MP4_GET1BYTE( i_len );
925             es_descriptor.psz_URL = malloc( i_len + 1 );
926             if( es_descriptor.psz_URL )
927             {
928                 memcpy( es_descriptor.psz_URL, p_peek, i_len );
929                 es_descriptor.psz_URL[i_len] = 0;
930             }
931             p_peek += i_len;
932             i_read -= i_len;
933         }
934         else
935         {
936             es_descriptor.psz_URL = NULL;
937         }
938         if( es_descriptor.b_OCRstream )
939         {
940             MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
941         }
942         MP4_GET1BYTE( i_type ); /* get next type */
943     }
944
945     if( i_type != 0x04)/* MP4DecConfigDescrTag */
946     {
947          es_descriptor.p_decConfigDescr = NULL;
948          MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
949     }
950
951     i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
952
953 #ifdef MP4_VERBOSE
954         msg_Dbg( p_stream, "found esds MP4DecConfigDescr (%dBytes)",
955                  i_len );
956 #endif
957
958     es_descriptor.p_decConfigDescr =
959             calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
960     if( es_descriptor.p_decConfigDescr == NULL )
961         MP4_READBOX_EXIT( 0 );
962
963     MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectTypeIndication );
964     MP4_GET1BYTE( i_flags );
965     es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
966     es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
967     MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
968     MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
969     MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
970     MP4_GET1BYTE( i_type );
971     if( i_type !=  0x05 )/* MP4DecSpecificDescrTag */
972     {
973         es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
974         es_descriptor.p_decConfigDescr->p_decoder_specific_info  = NULL;
975         MP4_READBOX_EXIT( 1 );
976     }
977
978     i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
979
980 #ifdef MP4_VERBOSE
981         msg_Dbg( p_stream, "found esds MP4DecSpecificDescr (%dBytes)",
982                  i_len );
983 #endif
984     if( i_len > i_read )
985         MP4_READBOX_EXIT( 0 );
986
987     es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
988     es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
989     if( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL )
990         MP4_READBOX_EXIT( 0 );
991
992     memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
993             p_peek, i_len );
994
995     MP4_READBOX_EXIT( 1 );
996 #undef es_descriptor
997 }
998
999 static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
1000 {
1001     MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1002     int i;
1003
1004     if( p_avcC->i_avcC >