]> git.sesse.net Git - vlc/blob - modules/demux/mp4/libmp4.c
* modules/demux/asf/*: fixed mem leak.
[vlc] / modules / demux / mp4 / libmp4.c
1 /*****************************************************************************
2  * libmp4.c : LibMP4 library for mp4 module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23 #include <stdlib.h>                                      /* malloc(), free() */
24
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #ifdef HAVE_ZLIB_H
29 #   include <zlib.h>                                  /* for compressed moov */
30 #endif
31
32 #include "libmp4.h"
33 #include "drms.h"
34
35 /*****************************************************************************
36  * Here are defined some macro to make life simpler but before using it
37  *  *look* at the code.
38  *
39  *****************************************************************************/
40 #define MP4_BOX_HEADERSIZE( p_box ) \
41   ( 8 + ( p_box->i_shortsize == 1 ? 8 : 0 ) \
42       + ( p_box->i_type == FOURCC_uuid ? 16 : 0 ) )
43
44 #define MP4_GET1BYTE( dst ) \
45     dst = *p_peek; p_peek++; i_read--
46
47 #define MP4_GET2BYTES( dst ) \
48     dst = GetWBE( p_peek ); p_peek += 2; i_read -= 2
49
50 #define MP4_GET3BYTES( dst ) \
51     dst = Get24bBE( p_peek ); p_peek += 3; i_read -= 3
52
53 #define MP4_GET4BYTES( dst ) \
54     dst = GetDWBE( p_peek ); p_peek += 4; i_read -= 4
55
56 #define MP4_GETFOURCC( dst ) \
57     dst = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] ); \
58     p_peek += 4; i_read -= 4
59
60 #define MP4_GET8BYTES( dst ) \
61     dst = GetQWBE( p_peek ); p_peek += 8; i_read -= 8
62
63 #define MP4_GETVERSIONFLAGS( p_void ) \
64     MP4_GET1BYTE( p_void->i_version ); \
65     MP4_GET3BYTES( p_void->i_flags )
66
67 #define MP4_GETSTRINGZ( p_str ) \
68     if( ( i_read > 0 )&&(p_peek[0] ) ) \
69     { \
70         p_str = calloc( sizeof( char ), __MIN( strlen( p_peek ), i_read )+1);\
71         memcpy( p_str, p_peek, __MIN( strlen( p_peek ), i_read ) ); \
72         p_str[__MIN( strlen( p_peek ), i_read )] = 0; \
73         p_peek += strlen( p_str ) + 1; \
74         i_read -= strlen( p_str ) + 1; \
75     } \
76     else \
77     { \
78         p_str = NULL; \
79     }
80
81
82 #define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t ) \
83     int64_t  i_read = p_box->i_size; \
84     uint8_t *p_peek, *p_buff; \
85     i_read = p_box->i_size; \
86     if( !( p_peek = p_buff = malloc( i_read ) ) ) \
87     { \
88         return( 0 ); \
89     } \
90     if( MP4_ReadStream( p_stream, p_peek, i_read ) )\
91     { \
92         free( p_buff ); \
93         return( 0 ); \
94     } \
95     p_peek += MP4_BOX_HEADERSIZE( p_box ); \
96     i_read -= MP4_BOX_HEADERSIZE( p_box ); \
97     if( !( p_box->data.p_data = malloc( sizeof( MP4_Box_data_TYPE_t ) ) ) ) \
98     { \
99       free( p_buff ); \
100       return( 0 ); \
101     }
102
103 #define MP4_READBOX_EXIT( i_code ) \
104     free( p_buff ); \
105     if( i_read < 0 ) \
106     { \
107         msg_Warn( p_stream->s, "Not enough data" ); \
108     } \
109     return( i_code )
110
111 #define FREE( p ) \
112     if( p ) {free( p ); p = NULL; }
113
114
115
116 /* Some assumptions:
117         * The input method HAVE to be seekable
118
119 */
120
121 static uint32_t Get24bBE( uint8_t *p )
122 {
123     return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
124 }
125
126 static void GetUUID( UUID_t *p_uuid, uint8_t *p_buff )
127 {
128     memcpy( p_uuid, p_buff, 16 );
129 }
130
131 static void CreateUUID( UUID_t *p_uuid, uint32_t i_fourcc )
132 {
133     /* made by 0xXXXXXXXX-0011-0010-8000-00aa00389b71
134             where XXXXXXXX is the fourcc */
135     /* FIXME implement this */
136 }
137
138 /* some functions for mp4 encoding of variables */
139
140 static void MP4_ConvertDate2Str( char *psz, uint64_t i_date )
141 {
142     int i_day;
143     int i_hour;
144     int i_min;
145     int i_sec;
146
147     /* date begin at 1 jan 1904 */
148     i_date += ((1904U * 365) + 17) * 24 * 60 * 60;
149
150     i_day = i_date / ( 60*60*24);
151     i_hour = ( i_date /( 60*60 ) ) % 60;
152     i_min  = ( i_date / 60 ) % 60;
153     i_sec =  i_date % 60;
154     sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds",
155                    i_day, i_hour, i_min, i_sec );
156 }
157
158 /*****************************************************************************
159  * Some prototypes.
160  *****************************************************************************/
161 static MP4_Box_t *MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_father );
162
163 /*****************************************************************************
164  * Some basic functions to manipulate MP4_Stream_t, an abstraction of stream_t
165  *  in the way that you can read from a memory buffer or from an input
166  *
167  *****************************************************************************/
168
169 /****  ------- First some function to make abstract from input --------  */
170
171 /****************************************************************************
172  * MP4_InputStream create an stram with an input
173  *
174  ****************************************************************************/
175 MP4_Stream_t *MP4_InputStream( stream_t *s )
176 {
177     MP4_Stream_t *p_stream;
178
179     if( !( p_stream = malloc( sizeof( MP4_Stream_t ) ) ) )
180     {
181         return( NULL );
182     }
183     p_stream->b_memory = 0;
184     p_stream->s = s;
185     p_stream->i_start = 0;
186     p_stream->i_stop = 0;
187     p_stream->p_buffer = NULL;
188     return( p_stream );
189 }
190
191
192 /****************************************************************************
193  * MP4_MemoryStream create a memory stream
194  * if p_buffer == NULL, will allocate a buffer of i_size, else
195  *     it uses p_buffer XXX you have to unallocate it yourself !
196  *
197  ****************************************************************************/
198 MP4_Stream_t *MP4_MemoryStream( stream_t *s,
199                                 int i_size, uint8_t *p_buffer )
200 {
201     MP4_Stream_t *p_stream;
202
203     if( !( p_stream = malloc( sizeof( MP4_Stream_t ) ) ) )
204     {
205         return( NULL );
206     }
207     p_stream->b_memory = 1;
208     p_stream->s = s;
209     p_stream->i_start = 0;
210     p_stream->i_stop = i_size;
211     if( !p_buffer )
212     {
213         if( !( p_stream->p_buffer = malloc( i_size ) ) )
214         {
215             free( p_stream );
216             return( NULL );
217         }
218     }
219     else
220     {
221         p_stream->p_buffer = p_buffer;
222     }
223
224     return( p_stream );
225 }
226 /****************************************************************************
227  * MP4_ReadStream read from a MP4_Stream_t
228  *
229  ****************************************************************************/
230 int MP4_ReadStream( MP4_Stream_t *p_stream, uint8_t *p_buff, int i_size )
231 {
232     if( p_stream->b_memory )
233     {
234         if( i_size > p_stream->i_stop - p_stream->i_start )
235         {
236             return( VLC_EGENERIC );
237         }
238         memcpy( p_buff,
239                 p_stream->p_buffer + p_stream->i_start,
240                 i_size );
241         p_stream->i_start += i_size;
242         return( VLC_SUCCESS );
243     }
244     else
245     {
246         return( stream_Read( p_stream->s, p_buff, i_size ) < i_size ? VLC_EGENERIC : VLC_SUCCESS);
247     }
248 }
249
250 /****************************************************************************
251  * MP4_PeekStream peek from a MP4_Stream_t
252  *
253  ****************************************************************************/
254 int MP4_PeekStream( MP4_Stream_t *p_stream, uint8_t **pp_peek, int i_size )
255 {
256     if( p_stream->b_memory )
257     {
258         *pp_peek = p_stream->p_buffer + p_stream->i_start;
259
260         return( __MIN(i_size,p_stream->i_stop - p_stream->i_start ));
261     }
262     else
263     {
264
265         if( stream_Size( p_stream->s ) > 0 )
266         {
267             int64_t i_max = stream_Size( p_stream->s ) - stream_Tell( p_stream->s );
268             if( i_size > i_max )
269             {
270                 i_size = i_max;
271             }
272         }
273         return( stream_Peek( p_stream->s, pp_peek, i_size ) );
274     }
275 }
276
277 /****************************************************************************
278  * MP4_TellStream give absolute position in the stream
279  * XXX for a memory stream give position from begining of the buffer
280  ****************************************************************************/
281 off_t MP4_TellStream( MP4_Stream_t *p_stream )
282 {
283     if( p_stream->b_memory )
284     {
285         return( p_stream->i_start );
286     }
287     else
288     {
289         return( stream_Tell( p_stream->s ) );
290     }
291 }
292
293 /****************************************************************************
294  * MP4_SeekStream seek in a MP4_Stream_t
295  *
296  ****************************************************************************/
297 int MP4_SeekStream( MP4_Stream_t *p_stream, off_t i_pos)
298 {
299     if( p_stream->b_memory )
300     {
301         if( i_pos < p_stream->i_stop )
302         {
303             p_stream->i_start = i_pos;
304             return( VLC_SUCCESS );
305         }
306         else
307         {
308             return( VLC_EGENERIC );
309         }
310     }
311     else
312     {
313         return( stream_Seek( p_stream->s, (int64_t)i_pos ) );
314     }
315 }
316
317
318
319 /*****************************************************************************
320  * MP4_ReadBoxCommon : Load only common parameters for all boxes
321  *****************************************************************************
322  * p_box need to be an already allocated MP4_Box_t, and all data
323  *  will only be peek not read
324  *
325  * RETURN : 0 if it fail, 1 otherwise
326  *****************************************************************************/
327 static int MP4_ReadBoxCommon( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
328 {
329     int      i_read;
330     uint8_t  *p_peek;
331
332     if( ( ( i_read = MP4_PeekStream( p_stream, &p_peek, 32 ) ) < 8 ) )
333     {
334         return( 0 );
335     }
336     p_box->i_pos = MP4_TellStream( p_stream );
337
338     p_box->data.p_data = NULL;
339     p_box->p_father = NULL;
340     p_box->p_first  = NULL;
341     p_box->p_last  = NULL;
342     p_box->p_next   = NULL;
343
344     MP4_GET4BYTES( p_box->i_shortsize );
345     MP4_GETFOURCC( p_box->i_type );
346
347     /* Now special case */
348
349     if( p_box->i_shortsize == 1 )
350     {
351         /* get the true size on 64 bits */
352         MP4_GET8BYTES( p_box->i_size );
353     }
354     else
355     {
356         p_box->i_size = p_box->i_shortsize;
357         /* XXX size of 0 means that the box extends to end of file */
358     }
359
360     if( p_box->i_type == FOURCC_uuid )
361     {
362         /* get extented type on 16 bytes */
363         GetUUID( &p_box->i_uuid, p_peek );
364         p_peek += 16; i_read -= 16;
365     }
366     else
367     {
368         CreateUUID( &p_box->i_uuid, p_box->i_type );
369     }
370 #ifdef MP4_VERBOSE
371     if( p_box->i_size )
372     {
373         msg_Dbg( p_stream->s, "found Box: %4.4s size "I64Fd,
374                  (char*)&p_box->i_type,
375                  p_box->i_size );
376     }
377 #endif
378
379     return( 1 );
380 }
381
382
383 /*****************************************************************************
384  * MP4_NextBox : Go to the next box
385  *****************************************************************************
386  * if p_box == NULL, go to the next box in witch we are( at the begining ).
387  *****************************************************************************/
388 static int MP4_NextBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
389 {
390     MP4_Box_t box;
391
392     if( !p_box )
393     {
394         MP4_ReadBoxCommon( p_stream, &box );
395         p_box = &box;
396     }
397
398     if( !p_box->i_size )
399     {
400         return( 2 ); /* Box with infinite size */
401     }
402
403     if( p_box->p_father )
404     {
405         /* check if it's within p-father */
406         if( p_box->i_size + p_box->i_pos >=
407                     p_box->p_father->i_size + p_box->p_father->i_pos )
408         {
409             return( 0 ); /* out of bound */
410         }
411     }
412     return( MP4_SeekStream( p_stream, p_box->i_size + p_box->i_pos ) ? 0 : 1 );
413 }
414
415 /*****************************************************************************
416  * For all known box a loader is given,
417  *  XXX: all common struct have to be already read by MP4_ReadBoxCommon
418  *       after called one of theses functions, file position is unknown
419  *       you need to call MP4_GotoBox to go where you want
420  *****************************************************************************/
421 static int MP4_ReadBoxContainerRaw( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
422 {
423     MP4_Box_t *p_box;
424
425     if( MP4_TellStream( p_stream ) + 8 >
426                  (off_t)(p_container->i_pos + p_container->i_size) )
427     {
428         /* there is no box to load */
429         return( 0 );
430     }
431
432     do
433     {
434         if( ( p_box = MP4_ReadBox( p_stream, p_container ) ) == NULL )
435         {
436             break;
437         }
438         /* chain this box with the father and the other at same level */
439         if( !p_container->p_first )
440         {
441             p_container->p_first = p_box;
442         }
443         else
444         {
445             p_container->p_last->p_next = p_box;
446         }
447         p_container->p_last = p_box;
448     } while( MP4_NextBox( p_stream, p_box ) == 1 );
449
450     return( 1 );
451 }
452
453
454 static int MP4_ReadBoxContainer( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
455 {
456     if( p_container->i_size <= (size_t)MP4_BOX_HEADERSIZE(p_container ) + 8 )
457     {
458         /* container is empty, 8 stand for the first header in this box */
459         return( 1 );
460     }
461
462     /* enter box */
463     MP4_SeekStream( p_stream, p_container->i_pos + MP4_BOX_HEADERSIZE( p_container ) );
464
465     return( MP4_ReadBoxContainerRaw( p_stream, p_container ) );
466 }
467
468 static void MP4_FreeBox_Common( MP4_Box_t *p_box )
469 {
470     /* Up to now do nothing */
471 }
472
473 static int MP4_ReadBoxSkip( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
474 {
475     /* XXX sometime moov is hiden in a free box */
476     if( p_box->p_father && p_box->p_father->i_type == VLC_FOURCC( 'r', 'o', 'o', 't' )&&
477         p_box->i_type == FOURCC_free )
478     {
479         uint8_t *p_peek;
480         int     i_read;
481         vlc_fourcc_t i_fcc;
482
483         i_read  = MP4_PeekStream( p_stream, &p_peek, 44 );
484
485         p_peek += MP4_BOX_HEADERSIZE( p_box ) + 4;
486         i_read -= MP4_BOX_HEADERSIZE( p_box ) + 4;
487
488         if( i_read >= 8 )
489         {
490             i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
491
492             if( i_fcc == FOURCC_cmov || i_fcc == FOURCC_mvhd )
493             {
494                 msg_Warn( p_stream->s, "detected moov hidden in a free box ..." );
495
496                 p_box->i_type = FOURCC_foov;
497                 return MP4_ReadBoxContainer( p_stream, p_box );
498             }
499         }
500     }
501     /* Nothing to do */
502 #ifdef MP4_VERBOSE
503     msg_Dbg( p_stream->s, "skip box: \"%4.4s\"",
504             (char*)&p_box->i_type );
505 #endif
506     return( 1 );
507 }
508
509 static int MP4_ReadBox_ftyp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
510 {
511     MP4_READBOX_ENTER( MP4_Box_data_ftyp_t );
512
513     MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
514     MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
515
516     if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
517     {
518         unsigned int i;
519         p_box->data.p_ftyp->i_compatible_brands =
520             calloc( p_box->data.p_ftyp->i_compatible_brands_count, sizeof(uint32_t));
521
522         for( i =0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
523         {
524             MP4_GETFOURCC( p_box->data.p_ftyp->i_compatible_brands[i] );
525         }
526     }
527     else
528     {
529         p_box->data.p_ftyp->i_compatible_brands = NULL;
530     }
531
532     MP4_READBOX_EXIT( 1 );
533 }
534
535 static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
536 {
537     FREE( p_box->data.p_ftyp->i_compatible_brands );
538 }
539
540
541 static int MP4_ReadBox_mvhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
542 {
543     unsigned int i;
544 #ifdef MP4_VERBOSE
545     char s_creation_time[128];
546     char s_modification_time[128];
547     char s_duration[128];
548 #endif
549     MP4_READBOX_ENTER( MP4_Box_data_mvhd_t );
550
551     MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
552
553     if( p_box->data.p_mvhd->i_version )
554     {
555         MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
556         MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
557         MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
558         MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
559     }
560     else
561     {
562         MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
563         MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
564         MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
565         MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
566     }
567     MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
568     MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
569     MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
570
571
572     for( i = 0; i < 2; i++ )
573     {
574         MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
575     }
576     for( i = 0; i < 9; i++ )
577     {
578         MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
579     }
580     for( i = 0; i < 6; i++ )
581     {
582         MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
583     }
584
585     MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
586
587
588 #ifdef MP4_VERBOSE
589     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
590     MP4_ConvertDate2Str( s_modification_time,
591                          p_box->data.p_mvhd->i_modification_time );
592     if( p_box->data.p_mvhd->i_rate )
593     {
594         MP4_ConvertDate2Str( s_duration,
595                  p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_rate );
596     }
597     else
598     {
599         s_duration[0] = 0;
600     }
601     msg_Dbg( p_stream->s, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
602                   s_creation_time,
603                   s_modification_time,
604                   (uint32_t)p_box->data.p_mvhd->i_timescale,
605                   s_duration,
606                   (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
607                   (float)p_box->data.p_mvhd->i_volume / 256 ,
608                   (uint32_t)p_box->data.p_mvhd->i_next_track_id );
609 #endif
610     MP4_READBOX_EXIT( 1 );
611 }
612
613 static int MP4_ReadBox_tkhd(  MP4_Stream_t *p_stream, MP4_Box_t *p_box )
614 {
615     unsigned int i;
616 #ifdef MP4_VERBOSE
617     char s_creation_time[128];
618     char s_modification_time[128];
619     char s_duration[128];
620 #endif
621     MP4_READBOX_ENTER( MP4_Box_data_tkhd_t );
622
623     MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
624
625     if( p_box->data.p_tkhd->i_version )
626     {
627         MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
628         MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
629         MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
630         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
631         MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
632     }
633     else
634     {
635         MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
636         MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
637         MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
638         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
639         MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
640     }
641
642     for( i = 0; i < 2; i++ )
643     {
644         MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
645     }
646     MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
647     MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
648     MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
649     MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
650
651     for( i = 0; i < 9; i++ )
652     {
653         MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
654     }
655     MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
656     MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
657
658 #ifdef MP4_VERBOSE
659     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
660     MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time );
661     MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration );
662
663     msg_Dbg( p_stream->s, "read box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f width %f height %f",
664                   s_creation_time,
665                   s_modification_time,
666                   s_duration,
667                   p_box->data.p_tkhd->i_track_ID,
668                   p_box->data.p_tkhd->i_layer,
669                   (float)p_box->data.p_tkhd->i_volume / 256 ,
670                   (float)p_box->data.p_tkhd->i_width / 65536,
671                   (float)p_box->data.p_tkhd->i_height / 65536 );
672 #endif
673     MP4_READBOX_EXIT( 1 );
674 }
675
676
677 static int MP4_ReadBox_mdhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
678 {
679     unsigned int i;
680     uint16_t i_language;
681 #ifdef MP4_VERBOSE
682     char s_creation_time[128];
683     char s_modification_time[128];
684     char s_duration[128];
685 #endif
686     MP4_READBOX_ENTER( MP4_Box_data_mdhd_t );
687
688     MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
689
690     if( p_box->data.p_mdhd->i_version )
691     {
692         MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
693         MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
694         MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
695         MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
696     }
697     else
698     {
699         MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
700         MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
701         MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
702         MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
703     }
704     i_language = GetWBE( p_peek );
705     for( i = 0; i < 3; i++ )
706     {
707         p_box->data.p_mdhd->i_language[i] =
708                     ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
709     }
710
711     MP4_GET2BYTES( p_box->data.p_mdhd->i_predefined );
712
713 #ifdef MP4_VERBOSE
714     MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time );
715     MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time );
716     MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration );
717     msg_Dbg( p_stream->s, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %c%c%c",
718                   s_creation_time,
719                   s_modification_time,
720                   (uint32_t)p_box->data.p_mdhd->i_timescale,
721                   s_duration,
722                   p_box->data.p_mdhd->i_language[0],
723                   p_box->data.p_mdhd->i_language[1],
724                   p_box->data.p_mdhd->i_language[2] );
725 #endif
726     MP4_READBOX_EXIT( 1 );
727 }
728
729
730 static int MP4_ReadBox_hdlr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
731 {
732     MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
733
734     MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
735
736     MP4_GET4BYTES( p_box->data.p_hdlr->i_predefined );
737     MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
738
739     p_box->data.p_hdlr->psz_name = calloc( sizeof( char ), i_read + 1 );
740     memcpy( p_box->data.p_hdlr->psz_name, p_peek, i_read );
741
742 #ifdef MP4_VERBOSE
743     msg_Dbg( p_stream->s, "read box: \"hdlr\" hanler type %4.4s name %s",
744                        (char*)&p_box->data.p_hdlr->i_handler_type,
745                        p_box->data.p_hdlr->psz_name );
746
747 #endif
748     MP4_READBOX_EXIT( 1 );
749 }
750
751 static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
752 {
753     FREE( p_box->data.p_hdlr->psz_name );
754 }
755
756 static int MP4_ReadBox_vmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
757 {
758     unsigned int i;
759
760     MP4_READBOX_ENTER( MP4_Box_data_vmhd_t );
761
762     MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
763
764     MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
765     for( i = 0; i < 3; i++ )
766     {
767         MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
768     }
769
770 #ifdef MP4_VERBOSE
771     msg_Dbg( p_stream->s, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
772                       p_box->data.p_vmhd->i_graphics_mode,
773                       p_box->data.p_vmhd->i_opcolor[0],
774                       p_box->data.p_vmhd->i_opcolor[1],
775                       p_box->data.p_vmhd->i_opcolor[2] );
776 #endif
777     MP4_READBOX_EXIT( 1 );
778 }
779
780 static int MP4_ReadBox_smhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
781 {
782     MP4_READBOX_ENTER( MP4_Box_data_smhd_t );
783
784     MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
785
786
787
788     MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
789
790     MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
791
792 #ifdef MP4_VERBOSE
793     msg_Dbg( p_stream->s, "read box: \"smhd\" balance %f",
794                       (float)p_box->data.p_smhd->i_balance / 256 );
795 #endif
796     MP4_READBOX_EXIT( 1 );
797 }
798
799
800 static int MP4_ReadBox_hmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
801 {
802     MP4_READBOX_ENTER( MP4_Box_data_hmhd_t );
803
804     MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
805
806     MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
807     MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
808
809     MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
810     MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
811
812     MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
813
814 #ifdef MP4_VERBOSE
815     msg_Dbg( p_stream->s, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
816                       p_box->data.p_hmhd->i_max_PDU_size,
817                       p_box->data.p_hmhd->i_avg_PDU_size,
818                       p_box->data.p_hmhd->i_max_bitrate,
819                       p_box->data.p_hmhd->i_avg_bitrate );
820 #endif
821     MP4_READBOX_EXIT( 1 );
822 }
823
824 static int MP4_ReadBox_url( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
825 {
826     MP4_READBOX_ENTER( MP4_Box_data_url_t );
827
828     MP4_GETVERSIONFLAGS( p_box->data.p_url );
829     MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
830
831 #ifdef MP4_VERBOSE
832     msg_Dbg( p_stream->s, "read box: \"url\" url: %s",
833                        p_box->data.p_url->psz_location );
834
835 #endif
836     MP4_READBOX_EXIT( 1 );
837 }
838
839
840 static void MP4_FreeBox_url( MP4_Box_t *p_box )
841 {
842     FREE( p_box->data.p_url->psz_location )
843 }
844
845 static int MP4_ReadBox_urn( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
846 {
847     MP4_READBOX_ENTER( MP4_Box_data_urn_t );
848
849     MP4_GETVERSIONFLAGS( p_box->data.p_urn );
850
851     MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
852     MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
853
854 #ifdef MP4_VERBOSE
855     msg_Dbg( p_stream->s, "read box: \"urn\" name %s location %s",
856                       p_box->data.p_urn->psz_name,
857                       p_box->data.p_urn->psz_location );
858 #endif
859     MP4_READBOX_EXIT( 1 );
860 }
861 static void MP4_FreeBox_urn( MP4_Box_t *p_box )
862 {
863     FREE( p_box->data.p_urn->psz_name );
864     FREE( p_box->data.p_urn->psz_location );
865 }
866
867
868 static int MP4_ReadBox_dref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
869 {
870     MP4_READBOX_ENTER( MP4_Box_data_dref_t );
871
872     MP4_GETVERSIONFLAGS( p_box->data.p_dref );
873
874     MP4_GET4BYTES( p_box->data.p_dref->i_entry_count );
875
876     MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 );
877     MP4_ReadBoxContainerRaw( p_stream, p_box );
878
879 #ifdef MP4_VERBOSE
880     msg_Dbg( p_stream->s, "read box: \"dref\" entry-count %d",
881                       p_box->data.p_dref->i_entry_count );
882
883 #endif
884     MP4_READBOX_EXIT( 1 );
885 }
886
887
888 static int MP4_ReadBox_stts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
889 {
890     unsigned int i;
891     MP4_READBOX_ENTER( MP4_Box_data_stts_t );
892
893     MP4_GETVERSIONFLAGS( p_box->data.p_stts );
894     MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );
895
896     p_box->data.p_stts->i_sample_count =
897         calloc( sizeof( uint32_t ), p_box->data.p_stts->i_entry_count );
898     p_box->data.p_stts->i_sample_delta =
899         calloc( sizeof( uint32_t ), p_box->data.p_stts->i_entry_count );
900
901     for( i = 0; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
902     {
903         MP4_GET4BYTES( p_box->data.p_stts->i_sample_count[i] );
904         MP4_GET4BYTES( p_box->data.p_stts->i_sample_delta[i] );
905     }
906
907 #ifdef MP4_VERBOSE
908     msg_Dbg( p_stream->s, "read box: \"stts\" entry-count %d",
909                       p_box->data.p_stts->i_entry_count );
910
911 #endif
912     MP4_READBOX_EXIT( 1 );
913 }
914
915 static void MP4_FreeBox_stts( MP4_Box_t *p_box )
916 {
917     FREE( p_box->data.p_stts->i_sample_count );
918     FREE( p_box->data.p_stts->i_sample_delta );
919 }
920
921 static int MP4_ReadBox_ctts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
922 {
923     unsigned int i;
924     MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
925
926     MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
927
928     MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );
929
930     p_box->data.p_ctts->i_sample_count =
931         calloc( sizeof( uint32_t ), p_box->data.p_ctts->i_entry_count );
932     p_box->data.p_ctts->i_sample_offset =
933         calloc( sizeof( uint32_t ), p_box->data.p_ctts->i_entry_count );
934
935     for( i = 0; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
936     {
937         MP4_GET4BYTES( p_box->data.p_ctts->i_sample_count[i] );
938         MP4_GET4BYTES( p_box->data.p_ctts->i_sample_offset[i] );
939     }
940
941 #ifdef MP4_VERBOSE
942     msg_Dbg( p_stream->s, "read box: \"ctts\" entry-count %d",
943                       p_box->data.p_ctts->i_entry_count );
944
945 #endif
946     MP4_READBOX_EXIT( 1 );
947 }
948
949 static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
950 {
951     FREE( p_box->data.p_ctts->i_sample_count );
952     FREE( p_box->data.p_ctts->i_sample_offset );
953 }
954
955 static int MP4_ReadLengthDescriptor( uint8_t **pp_peek, int64_t  *i_read )
956 {
957     unsigned int i_b;
958     unsigned int i_len = 0;
959     do
960     {
961         i_b = **pp_peek;
962
963         (*pp_peek)++;
964         (*i_read)--;
965         i_len = ( i_len << 7 ) + ( i_b&0x7f );
966     } while( i_b&0x80 );
967     return( i_len );
968 }
969
970 static int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
971 {
972 #define es_descriptor p_box->data.p_esds->es_descriptor
973     unsigned int i_len;
974     unsigned int i_flags;
975     unsigned int i_type;
976
977     MP4_READBOX_ENTER( MP4_Box_data_esds_t );
978
979     MP4_GETVERSIONFLAGS( p_box->data.p_esds );
980
981
982     MP4_GET1BYTE( i_type );
983     if( i_type == 0x03 ) /* MP4ESDescrTag */
984     {
985         i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
986
987 #ifdef MP4_VERBOSE
988         msg_Dbg( p_stream->s, "found esds MPEG4ESDescr (%dBytes)",
989                  i_len );
990 #endif
991
992         MP4_GET2BYTES( es_descriptor.i_ES_ID );
993         MP4_GET1BYTE( i_flags );
994         es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
995         es_descriptor.b_url = ( (i_flags&0x40) != 0);
996         es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
997
998         es_descriptor.i_stream_priority = i_flags&0x1f;
999         if( es_descriptor.b_stream_dependence )
1000         {
1001             MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1002         }
1003         if( es_descriptor.b_url )
1004         {
1005             unsigned int i_len;
1006
1007             MP4_GET1BYTE( i_len );
1008             es_descriptor.psz_URL = calloc( sizeof(char), i_len + 1 );
1009             memcpy( es_descriptor.psz_URL, p_peek, i_len );
1010             es_descriptor.psz_URL[i_len] = 0;
1011             p_peek += i_len;
1012             i_read -= i_len;
1013         }
1014         else
1015         {
1016             es_descriptor.psz_URL = NULL;
1017         }
1018         if( es_descriptor.b_OCRstream )
1019         {
1020             MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1021         }
1022         MP4_GET1BYTE( i_type ); /* get next type */
1023     }
1024
1025     if( i_type != 0x04)/* MP4DecConfigDescrTag */
1026     {
1027          es_descriptor.p_decConfigDescr = NULL;
1028          MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1029     }
1030
1031     i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1032
1033 #ifdef MP4_VERBOSE
1034         msg_Dbg( p_stream->s, "found esds MP4DecConfigDescr (%dBytes)",
1035                  i_len );
1036 #endif
1037
1038     es_descriptor.p_decConfigDescr =
1039             malloc( sizeof( MP4_descriptor_decoder_config_t ));
1040
1041     MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectTypeIndication );
1042     MP4_GET1BYTE( i_flags );
1043     es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1044     es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1045     MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1046     MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1047     MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1048     MP4_GET1BYTE( i_type );
1049     if( i_type !=  0x05 )/* MP4DecSpecificDescrTag */
1050     {
1051         es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1052         es_descriptor.p_decConfigDescr->p_decoder_specific_info  = NULL;
1053         MP4_READBOX_EXIT( 1 );
1054     }
1055
1056     i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1057
1058 #ifdef MP4_VERBOSE
1059         msg_Dbg( p_stream->s, "found esds MP4DecSpecificDescr (%dBytes)",
1060                  i_len );
1061 #endif
1062
1063     es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1064     es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1065     memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1066             p_peek, i_len );
1067
1068     MP4_READBOX_EXIT( 1 );
1069
1070 #undef es_descriptor
1071 }
1072
1073 static void MP4_FreeBox_esds( MP4_Box_t *p_box )
1074 {
1075     FREE( p_box->data.p_esds->es_descriptor.psz_URL );
1076     if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1077     {
1078         FREE( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1079     }
1080     FREE( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1081 }
1082
1083 static int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1084 {
1085     unsigned int i;
1086
1087     MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
1088     p_box->data.p_sample_soun->p_qt_description = NULL;
1089
1090     /* Sanity check needed because the "wave" box does also contain an
1091      * "mp4a" box that we don't understand. */
1092     if( i_read < 28 )
1093     {
1094         i_read -= 30;
1095         MP4_READBOX_EXIT( 1 );
1096     }
1097
1098     for( i = 0; i < 6 ; i++ )
1099     {
1100         MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
1101     }
1102
1103     MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
1104
1105     /*
1106      * XXX hack -> produce a copy of the nearly complete chunk
1107      */
1108     if( i_read > 0 )
1109     {
1110         p_box->data.p_sample_soun->i_qt_description = i_read;
1111         p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
1112         memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
1113     }
1114     else
1115     {
1116         p_box->data.p_sample_soun->i_qt_description = 0;
1117         p_box->data.p_sample_soun->p_qt_description = NULL;
1118     }
1119
1120     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
1121     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
1122     MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
1123
1124     MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
1125     MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
1126     MP4_GET2BYTES( p_box->data.p_sample_soun->i_predefined );
1127     MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
1128     MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
1129     MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
1130
1131     if( p_box->data.p_sample_soun->i_qt_version == 1 &&
1132         i_read >= 16 )
1133     {
1134         /* qt3+ */
1135         MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
1136         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
1137         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
1138         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
1139
1140 #ifdef MP4_VERBOSE
1141         msg_Dbg( p_stream->s,
1142                  "read box: \"soun\" qt3+ sample/packet=%d bytes/packet=%d "
1143                  "bytes/frame=%d bytes/sample=%d",
1144                  p_box->data.p_sample_soun->i_sample_per_packet,
1145                  p_box->data.p_sample_soun->i_bytes_per_packet,
1146                  p_box->data.p_sample_soun->i_bytes_per_frame,
1147                  p_box->data.p_sample_soun->i_bytes_per_sample );
1148 #endif
1149         MP4_SeekStream( p_stream, p_box->i_pos +
1150                         MP4_BOX_HEADERSIZE( p_box ) + 44 );
1151     }
1152     else
1153     {
1154         p_box->data.p_sample_soun->i_sample_per_packet = 0;
1155         p_box->data.p_sample_soun->i_bytes_per_packet = 0;
1156         p_box->data.p_sample_soun->i_bytes_per_frame = 0;
1157         p_box->data.p_sample_soun->i_bytes_per_sample = 0;
1158
1159         msg_Dbg( p_stream->s, "read box: \"soun\" mp4 or qt1/2 (rest="I64Fd")",
1160                  i_read );
1161         MP4_SeekStream( p_stream, p_box->i_pos +
1162                         MP4_BOX_HEADERSIZE( p_box ) + 28 );
1163     }
1164
1165     if( p_box->i_type == FOURCC_drms )
1166     {
1167         p_box->data.p_sample_soun->p_drms =
1168             drms_alloc( p_stream->s->p_vlc->psz_homedir );
1169
1170         if( p_box->data.p_sample_soun->p_drms == NULL )
1171         {
1172             msg_Err( p_stream->s, "drms_alloc() failed" );
1173         }
1174     }
1175
1176     MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds */
1177
1178 #ifdef MP4_VERBOSE
1179     msg_Dbg( p_stream->s, "read box: \"soun\" in stsd channel %d "
1180              "sample size %d sampl rate %f",
1181              p_box->data.p_sample_soun->i_channelcount,
1182              p_box->data.p_sample_soun->i_samplesize,
1183              (float)p_box->data.p_sample_soun->i_sampleratehi +
1184              (float)p_box->data.p_sample_soun->i_sampleratelo / 65536 );
1185
1186 #endif
1187     MP4_READBOX_EXIT( 1 );
1188 }
1189
1190
1191 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
1192 {
1193     FREE( p_box->data.p_sample_soun->p_qt_description );
1194
1195     if( p_box->i_type == FOURCC_drms )
1196     {
1197         if( p_box->data.p_sample_soun->p_drms )
1198         {
1199             drms_free( p_box->data.p_sample_soun->p_drms );
1200         }
1201     }
1202 }
1203
1204
1205 static int MP4_ReadBox_sample_vide( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1206 {
1207     unsigned int i;
1208
1209     MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );
1210
1211     for( i = 0; i < 6 ; i++ )
1212     {
1213         MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
1214     }
1215
1216     MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
1217
1218     /*
1219      * XXX hack -> produce a copy of the nearly complete chunk
1220      */
1221     if( i_read > 0 )
1222     {
1223         p_box->data.p_sample_vide->i_qt_image_description = i_read;
1224         p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
1225         memcpy( p_box->data.p_sample_vide->p_qt_image_description,
1226                 p_peek, i_read );
1227     }
1228     else
1229     {
1230         p_box->data.p_sample_vide->i_qt_image_description = 0;
1231         p_box->data.p_sample_vide->p_qt_image_description = NULL;
1232     }
1233
1234     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
1235     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
1236     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
1237
1238     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
1239     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
1240
1241     MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
1242     MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
1243
1244     MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
1245     MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
1246
1247     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
1248     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
1249
1250     memcpy( &p_box->data.p_sample_vide->i_compressorname, p_peek, 32 );
1251     p_peek += 32; i_read -= 32;
1252
1253     MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
1254     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
1255
1256     MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 78);
1257     MP4_ReadBoxContainerRaw( p_stream, p_box );
1258
1259 #ifdef MP4_VERBOSE
1260     msg_Dbg( p_stream->s, "read box: \"vide\" in stsd %dx%d depth %d",
1261                       p_box->data.p_sample_vide->i_width,
1262                       p_box->data.p_sample_vide->i_height,
1263                       p_box->data.p_sample_vide->i_depth );
1264
1265 #endif
1266     MP4_READBOX_EXIT( 1 );
1267 }
1268
1269
1270 static void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
1271 {
1272     FREE( p_box->data.p_sample_vide->p_qt_image_description );
1273 }
1274
1275
1276 static int MP4_ReadBox_stsd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1277 {
1278
1279     MP4_READBOX_ENTER( MP4_Box_data_stsd_t );
1280
1281     MP4_GETVERSIONFLAGS( p_box->data.p_stsd );
1282
1283     MP4_GET4BYTES( p_box->data.p_stsd->i_entry_count );
1284
1285     MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 );
1286
1287     MP4_ReadBoxContainerRaw( p_stream, p_box );
1288
1289 #ifdef MP4_VERBOSE
1290     msg_Dbg( p_stream->s, "read box: \"stsd\" entry-count %d",
1291                       p_box->data.p_stsd->i_entry_count );
1292
1293 #endif
1294     MP4_READBOX_EXIT( 1 );
1295 }
1296
1297
1298 static int MP4_ReadBox_stsz( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1299 {
1300     unsigned int i;
1301
1302     MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
1303
1304     MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
1305
1306     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
1307
1308     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
1309
1310     p_box->data.p_stsz->i_entry_size =
1311         calloc( sizeof( uint32_t ), p_box->data.p_stsz->i_sample_count );
1312
1313     if( !p_box->data.p_stsz->i_sample_size )
1314     {
1315         for( i=0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
1316         {
1317             MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
1318         }
1319     }
1320
1321 #ifdef MP4_VERBOSE
1322     msg_Dbg( p_stream->s, "read box: \"stsz\" sample-size %d sample-count %d",
1323                       p_box->data.p_stsz->i_sample_size,
1324                       p_box->data.p_stsz->i_sample_count );
1325
1326 #endif
1327     MP4_READBOX_EXIT( 1 );
1328 }
1329
1330 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
1331 {
1332     FREE( p_box->data.p_stsz->i_entry_size );
1333 }
1334
1335 static int MP4_ReadBox_stsc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1336 {
1337     unsigned int i;
1338
1339     MP4_READBOX_ENTER( MP4_Box_data_stsc_t );
1340
1341     MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
1342
1343     MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
1344
1345     p_box->data.p_stsc->i_first_chunk =
1346         calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1347     p_box->data.p_stsc->i_samples_per_chunk =
1348         calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1349     p_box->data.p_stsc->i_sample_description_index =
1350         calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1351
1352     for( i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
1353     {
1354         MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
1355         MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
1356         MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
1357     }
1358
1359 #ifdef MP4_VERBOSE
1360     msg_Dbg( p_stream->s, "read box: \"stsc\" entry-count %d",
1361                       p_box->data.p_stsc->i_entry_count );
1362
1363 #endif
1364     MP4_READBOX_EXIT( 1 );
1365 }
1366
1367 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
1368 {
1369     FREE( p_box->data.p_stsc->i_first_chunk );
1370     FREE( p_box->data.p_stsc->i_samples_per_chunk );
1371     FREE( p_box->data.p_stsc->i_sample_description_index );
1372 }
1373
1374 static int MP4_ReadBox_stco_co64( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1375 {
1376     unsigned int i;
1377
1378     MP4_READBOX_ENTER( MP4_Box_data_co64_t );
1379
1380     MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
1381
1382     MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
1383
1384     p_box->data.p_co64->i_chunk_offset =
1385         calloc( sizeof( uint64_t ), p_box->data.p_co64->i_entry_count );
1386
1387     for( i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
1388     {
1389         if( p_box->i_type == FOURCC_stco )
1390         {
1391             if( i_read < 4 )
1392             {
1393                 break;
1394             }
1395             MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
1396         }
1397         else
1398         {
1399             if( i_read < 8 )
1400             {
1401                 break;
1402             }
1403             MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
1404         }
1405     }
1406
1407 #ifdef MP4_VERBOSE
1408     msg_Dbg( p_stream->s, "read box: \"co64\" entry-count %d",
1409                       p_box->data.p_co64->i_entry_count );
1410
1411 #endif
1412     MP4_READBOX_EXIT( 1 );
1413 }
1414
1415 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
1416 {
1417     FREE( p_box->data.p_co64->i_chunk_offset );
1418 }
1419
1420 static int MP4_ReadBox_stss( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1421 {
1422     unsigned int i;
1423
1424     MP4_READBOX_ENTER( MP4_Box_data_stss_t );
1425
1426     MP4_GETVERSIONFLAGS( p_box->data.p_stss );
1427
1428     MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
1429
1430     p_box->data.p_stss->i_sample_number =
1431         calloc( sizeof( uint32_t ), p_box->data.p_stss->i_entry_count );
1432
1433     for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
1434     {
1435
1436         MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
1437         /* XXX in libmp4 sample begin at 0 */
1438         p_box->data.p_stss->i_sample_number[i]--;
1439     }
1440
1441 #ifdef MP4_VERBOSE
1442     msg_Dbg( p_stream->s, "read box: \"stss\" entry-count %d",
1443                       p_box->data.p_stss->i_entry_count );
1444
1445 #endif
1446     MP4_READBOX_EXIT( 1 );
1447 }
1448
1449 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
1450 {
1451     FREE( p_box->data.p_stss->i_sample_number )
1452 }
1453
1454 static int MP4_ReadBox_stsh( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1455 {
1456     unsigned int i;
1457
1458     MP4_READBOX_ENTER( MP4_Box_data_stsh_t );
1459
1460     MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
1461
1462
1463     MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
1464
1465     p_box->data.p_stsh->i_shadowed_sample_number =
1466         calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
1467
1468     p_box->data.p_stsh->i_sync_sample_number =
1469         calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
1470
1471
1472     for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
1473     {
1474
1475         MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
1476         MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
1477     }
1478
1479 #ifdef MP4_VERBOSE
1480     msg_Dbg( p_stream->s, "read box: \"stsh\" entry-count %d",
1481                       p_box->data.p_stsh->i_entry_count );
1482 #endif
1483     MP4_READBOX_EXIT( 1 );
1484 }
1485
1486 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
1487 {
1488     FREE( p_box->data.p_stsh->i_shadowed_sample_number )
1489     FREE( p_box->data.p_stsh->i_sync_sample_number )
1490 }
1491
1492
1493 static int MP4_ReadBox_stdp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1494 {
1495     unsigned int i;
1496
1497     MP4_READBOX_ENTER( MP4_Box_data_stdp_t );
1498
1499     MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
1500
1501     p_box->data.p_stdp->i_priority =
1502         calloc( sizeof( uint16_t ), i_read / 2 );
1503
1504     for( i = 0; i < i_read / 2 ; i++ )
1505     {
1506
1507         MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
1508     }
1509
1510 #ifdef MP4_VERBOSE
1511     msg_Dbg( p_stream->s, "read box: \"stdp\" entry-count "I64Fd,
1512                       i_read / 2 );
1513
1514 #endif
1515     MP4_READBOX_EXIT( 1 );
1516 }
1517
1518 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
1519 {
1520     FREE( p_box->data.p_stdp->i_priority )
1521 }
1522
1523 static int MP4_ReadBox_padb( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1524 {
1525     unsigned int i;
1526
1527     MP4_READBOX_ENTER( MP4_Box_data_padb_t );
1528
1529     MP4_GETVERSIONFLAGS( p_box->data.p_padb );
1530
1531
1532     MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
1533
1534     p_box->data.p_padb->i_reserved1 =
1535         calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1536     p_box->data.p_padb->i_pad2 =
1537         calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1538     p_box->data.p_padb->i_reserved2 =
1539         calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1540     p_box->data.p_padb->i_pad1 =
1541         calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1542
1543
1544     for( i = 0; i < i_read / 2 ; i++ )
1545     {
1546         p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
1547         p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
1548         p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
1549         p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;
1550
1551         p_peek += 1; i_read -= 1;
1552     }
1553
1554 #ifdef MP4_VERBOSE
1555     msg_Dbg( p_stream->s, "read box: \"stdp\" entry-count "I64Fd,
1556                       i_read / 2 );
1557
1558 #endif
1559     MP4_READBOX_EXIT( 1 );
1560 }
1561
1562 static void MP4_FreeBox_padb( MP4_Box_t *p_box )
1563 {
1564     FREE( p_box->data.p_padb->i_reserved1 );
1565     FREE( p_box->data.p_padb->i_pad2 );
1566     FREE( p_box->data.p_padb->i_reserved2 );
1567     FREE( p_box->data.p_padb->i_pad1 );
1568 }
1569
1570 static int MP4_ReadBox_elst( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1571 {
1572     unsigned int i;
1573
1574     MP4_READBOX_ENTER( MP4_Box_data_padb_t );
1575
1576     MP4_GETVERSIONFLAGS( p_box->data.p_elst );
1577
1578
1579     MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
1580
1581     p_box->data.p_elst->i_segment_duration =
1582         calloc( sizeof( uint64_t ), p_box->data.p_elst->i_entry_count );
1583     p_box->data.p_elst->i_media_time =
1584         calloc( sizeof( int64_t ),  p_box->data.p_elst->i_entry_count );
1585     p_box->data.p_elst->i_media_rate_integer =
1586         calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
1587     p_box->data.p_elst->i_media_rate_fraction=
1588         calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
1589
1590
1591     for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
1592     {
1593         if( p_box->data.p_elst->i_version == 1 )
1594         {
1595
1596             MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
1597
1598             MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
1599         }
1600         else
1601         {
1602
1603             MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
1604
1605             MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
1606             p_box->data.p_elst->i_media_time[i] = (int32_t)p_box->data.p_elst->i_media_time[i];
1607         }
1608
1609         MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
1610         MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
1611     }
1612
1613 #ifdef MP4_VERBOSE
1614     msg_Dbg( p_stream->s, "read box: \"elst\" entry-count "I64Fd,
1615                       i_read / 2 );
1616
1617 #endif
1618     MP4_READBOX_EXIT( 1 );
1619 }
1620
1621 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
1622 {
1623     FREE( p_box->data.p_elst->i_segment_duration );
1624     FREE( p_box->data.p_elst->i_media_time );
1625     FREE( p_box->data.p_elst->i_media_rate_integer );
1626     FREE( p_box->data.p_elst->i_media_rate_fraction );
1627 }
1628
1629 static int MP4_ReadBox_cprt( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1630 {
1631     unsigned int i_language;
1632     unsigned int i;
1633
1634     MP4_READBOX_ENTER( MP4_Box_data_cprt_t );
1635
1636     MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
1637
1638     i_language = GetWBE( p_peek );
1639     for( i = 0; i < 3; i++ )
1640     {
1641         p_box->data.p_cprt->i_language[i] =
1642             ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
1643     }
1644     p_peek += 2; i_read -= 2;
1645     MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
1646
1647 #ifdef MP4_VERBOSE
1648     msg_Dbg( p_stream->s, "read box: \"cprt\" language %c%c%c notice %s",
1649                       p_box->data.p_cprt->i_language[0],
1650                       p_box->data.p_cprt->i_language[1],
1651                       p_box->data.p_cprt->i_language[2],
1652                       p_box->data.p_cprt->psz_notice );
1653
1654 #endif
1655     MP4_READBOX_EXIT( 1 );
1656 }
1657
1658 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
1659 {
1660     FREE( p_box->data.p_cprt->psz_notice );
1661 }
1662
1663
1664 static int MP4_ReadBox_dcom( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1665 {
1666     MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
1667
1668     MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
1669 #ifdef MP4_VERBOSE
1670     msg_Dbg( p_stream->s,
1671              "read box: \"dcom\" compression algorithm : %4.4s",
1672                       (char*)&p_box->data.p_dcom->i_algorithm );
1673 #endif
1674     MP4_READBOX_EXIT( 1 );
1675 }
1676
1677 static int MP4_ReadBox_cmvd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1678 {
1679     MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );
1680
1681     MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
1682
1683     p_box->data.p_cmvd->i_compressed_size = i_read;
1684
1685     if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
1686     {
1687         msg_Dbg( p_stream->s, "read box: \"cmvd\" not enough memory to load data" );
1688         return( 1 );
1689     }
1690
1691     /* now copy compressed data */
1692     memcpy( p_box->data.p_cmvd->p_data,
1693             p_peek,
1694             i_read);
1695
1696     p_box->data.p_cmvd->b_compressed = 1;
1697
1698 #ifdef MP4_VERBOSE
1699     msg_Dbg( p_stream->s, "read box: \"cmvd\" compressed data size %d",
1700                       p_box->data.p_cmvd->i_compressed_size );
1701 #endif
1702
1703     MP4_READBOX_EXIT( 1 );
1704 }
1705 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
1706 {
1707     FREE( p_box->data.p_cmvd->p_data );
1708 }
1709
1710
1711 static int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1712 {
1713     MP4_Stream_t *p_stream_memory;
1714
1715     MP4_Box_t *p_dcom;
1716     MP4_Box_t *p_cmvd;
1717 #ifdef HAVE_ZLIB_H
1718     z_stream  z_data;
1719 #endif
1720     uint8_t *p_data;
1721
1722     int i_result;
1723
1724     if( !( p_box->data.p_cmov = malloc( sizeof( MP4_Box_data_cmov_t ) ) ) )
1725     {
1726         msg_Err( p_stream->s, "out of memory" );
1727         return( 0 );
1728     }
1729     memset( p_box->data.p_cmov, 0, sizeof( MP4_Box_data_cmov_t ) );
1730
1731     if( !p_box->p_father ||
1732         ( p_box->p_father->i_type != FOURCC_moov && p_box->p_father->i_type != FOURCC_foov) )
1733     {
1734         msg_Warn( p_stream->s, "Read box: \"cmov\" box alone" );
1735         return( 1 );
1736     }
1737
1738     if( !(i_result = MP4_ReadBoxContainer( p_stream, p_box ) ) )
1739     {
1740         return( 0 );
1741     }
1742
1743     if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
1744         ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
1745         p_cmvd->data.p_cmvd->p_data == NULL )
1746     {
1747         msg_Warn( p_stream->s, "read box: \"cmov\" incomplete" );
1748         return( 1 );
1749     }
1750
1751     if( p_dcom->data.p_dcom->i_algorithm != FOURCC_zlib )
1752     {
1753         msg_Dbg( p_stream->s, "read box: \"cmov\" compression algorithm : %4.4s not supported",
1754                     (char*)&p_dcom->data.p_dcom->i_algorithm );
1755         return( 1 );
1756     }
1757
1758 #ifndef HAVE_ZLIB_H
1759     msg_Dbg( p_stream->s,
1760              "read box: \"cmov\" zlib unsupported" );
1761     return( 1 );
1762 #else
1763     /* decompress data */
1764     /* allocate a new buffer */
1765     if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
1766     {
1767         msg_Err( p_stream->s,
1768                  "read box: \"cmov\" not enough memory to uncompress data" );
1769         return( 1 );
1770     }
1771     /* init default structures */
1772     z_data.next_in   = p_cmvd->data.p_cmvd->p_data;
1773     z_data.avail_in  = p_cmvd->data.p_cmvd->i_compressed_size;
1774     z_data.next_out  = p_data;
1775     z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
1776     z_data.zalloc    = (alloc_func)Z_NULL;
1777     z_data.zfree     = (free_func)Z_NULL;
1778     z_data.opaque    = (voidpf)Z_NULL;
1779
1780     /* init zlib */
1781     if( ( i_result = inflateInit( &z_data ) ) != Z_OK )
1782     {
1783         msg_Err( p_stream->s,
1784                  "read box: \"cmov\" error while uncompressing data" );
1785         free( p_data );
1786         return( 1 );
1787     }
1788
1789     /* uncompress */
1790     i_result = inflate( &z_data, Z_NO_FLUSH );
1791     if( ( i_result != Z_OK )&&( i_result != Z_STREAM_END ) )
1792     {
1793         msg_Err( p_stream->s,
1794                  "read box: \"cmov\" error while uncompressing data" );
1795         free( p_data );
1796         return( 1 );
1797     }
1798
1799     if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
1800     {
1801         msg_Warn( p_stream->s,
1802                   "read box: \"cmov\" uncompressing data size mismatch" );
1803     }
1804     p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
1805
1806     /* close zlib */
1807     i_result = inflateEnd( &z_data );
1808     if( i_result != Z_OK )
1809     {
1810         msg_Warn( p_stream->s,
1811            "read box: \"cmov\" error while uncompressing data (ignored)" );
1812     }
1813
1814
1815     free( p_cmvd->data.p_cmvd->p_data );
1816     p_cmvd->data.p_cmvd->p_data = p_data;
1817     p_cmvd->data.p_cmvd->b_compressed = 0;
1818
1819     msg_Dbg( p_stream->s,
1820              "read box: \"cmov\" box succesfully uncompressed" );
1821
1822     /* now create a memory stream */
1823     p_stream_memory = MP4_MemoryStream( p_stream->s,
1824                                         p_cmvd->data.p_cmvd->i_uncompressed_size,
1825                                         p_cmvd->data.p_cmvd->p_data );
1826
1827     /* and read uncompressd moov */
1828     p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
1829
1830     free( p_stream_memory );
1831
1832 #ifdef MP4_VERBOSE
1833     msg_Dbg( p_stream->s,
1834              "read box: \"cmov\" compressed movie header completed" );
1835 #endif
1836     return( p_box->data.p_cmov->p_moov ? 1 : 0 );
1837 #endif /* HAVE_ZLIB_H */
1838 }
1839
1840 static int MP4_ReadBox_rdrf( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1841 {
1842     uint32_t i_len;
1843     MP4_READBOX_ENTER( MP4_Box_data_rdrf_t );
1844
1845     MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
1846     MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
1847     MP4_GET4BYTES( i_len );
1848     if( i_len > 0 )
1849     {
1850         uint32_t i;
1851         p_box->data.p_rdrf->psz_ref = malloc( i_len  + 1);
1852         for( i = 0; i < i_len; i++ )
1853         {
1854             MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
1855         }
1856         p_box->data.p_rdrf->psz_ref[i_len] = '\0';
1857     }
1858     else
1859     {
1860         p_box->data.p_rdrf->psz_ref = NULL;
1861     }
1862
1863 #ifdef MP4_VERBOSE
1864     msg_Dbg( p_stream->s,
1865              "read box: \"rdrf\" type:%4.4s ref %s",
1866              (char*)&p_box->data.p_rdrf->i_ref_type,
1867              p_box->data.p_rdrf->psz_ref );
1868
1869 #endif
1870     MP4_READBOX_EXIT( 1 );
1871 }
1872
1873 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
1874 {
1875     FREE( p_box->data.p_rdrf->psz_ref )
1876 }
1877
1878
1879 static int MP4_ReadBox_rmdr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1880 {
1881     MP4_READBOX_ENTER( MP4_Box_data_rmdr_t );
1882
1883     MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
1884
1885     MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
1886
1887 #ifdef MP4_VERBOSE
1888     msg_Dbg( p_stream->s,
1889              "read box: \"rmdr\" rate:%d",
1890              p_box->data.p_rmdr->i_rate );
1891 #endif
1892     MP4_READBOX_EXIT( 1 );
1893 }
1894
1895 static int MP4_ReadBox_rmqu( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1896 {
1897     MP4_READBOX_ENTER( MP4_Box_data_rmqu_t );
1898
1899     MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
1900
1901 #ifdef MP4_VERBOSE
1902     msg_Dbg( p_stream->s,
1903              "read box: \"rmqu\" quality:%d",
1904              p_box->data.p_rmqu->i_quality );
1905 #endif
1906     MP4_READBOX_EXIT( 1 );
1907 }
1908
1909 static int MP4_ReadBox_rmvc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1910 {
1911     MP4_READBOX_ENTER( MP4_Box_data_rmvc_t );
1912     MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
1913
1914     MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
1915     MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
1916     MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
1917     MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
1918
1919 #ifdef MP4_VERBOSE
1920     msg_Dbg( p_stream->s,
1921              "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
1922              (char*)&p_box->data.p_rmvc->i_gestaltType,
1923              p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
1924              p_box->data.p_rmvc->i_checkType );
1925 #endif
1926
1927     MP4_READBOX_EXIT( 1 );
1928 }
1929
1930 static int MP4_ReadBox_drms( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1931 {
1932     MP4_Box_t *p_drms_box = p_box;
1933
1934     MP4_READBOX_ENTER( uint8_t );
1935
1936     do
1937     {
1938         p_drms_box = p_drms_box->p_father;
1939     } while( p_drms_box && p_drms_box->i_type != FOURCC_drms );
1940
1941     if( p_drms_box && p_drms_box->data.p_sample_soun->p_drms )
1942     {
1943         if( drms_init( p_drms_box->data.p_sample_soun->p_drms,
1944                        p_box->i_type, p_peek, i_read ) )
1945         {
1946             msg_Err( p_stream->s, "drms_init( %4.4s ) failed",
1947                      (char *)&p_box->i_type );
1948
1949             drms_free( p_drms_box->data.p_sample_soun->p_drms );
1950             p_drms_box->data.p_sample_soun->p_drms = NULL;
1951         }
1952     }
1953
1954     MP4_READBOX_EXIT( 1 );
1955 }
1956
1957 static int MP4_ReadBox_0xa9xxx( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1958 {
1959     int16_t i_length, i_dummy;
1960
1961     MP4_READBOX_ENTER( MP4_Box_data_0xa9xxx_t );
1962
1963     p_box->data.p_0xa9xxx->psz_text = NULL;
1964
1965     MP4_GET2BYTES( i_length );
1966     MP4_GET2BYTES( i_dummy );
1967
1968     if( i_length > 0 )
1969     {
1970         if( i_length > i_read ) i_length = i_read;
1971
1972         p_box->data.p_0xa9xxx->psz_text = malloc( i_length + 1 );
1973
1974         memcpy( p_box->data.p_0xa9xxx->psz_text,
1975                 p_peek, i_length );
1976         p_box->data.p_0xa9xxx->psz_text[i_length] = '\0';
1977
1978 #ifdef MP4_VERBOSE
1979         msg_Dbg( p_stream->s,
1980                  "read box: \"%4.4s\" text=`%s'",
1981                  (char*)&p_box->i_type,
1982                  p_box->data.p_0xa9xxx->psz_text );
1983 #endif
1984     }
1985
1986     MP4_READBOX_EXIT( 1 );
1987 }
1988 static void MP4_FreeBox_0xa9xxx( MP4_Box_t *p_box )
1989 {
1990     FREE( p_box->data.p_0xa9xxx->psz_text );
1991 }
1992
1993 /**** ------------------------------------------------------------------- ****/
1994 /****                   "Higher level" Functions                          ****/
1995 /**** ------------------------------------------------------------------- ****/
1996
1997 static struct
1998 {
1999     uint32_t i_type;
2000     int  (*MP4_ReadBox_function )( MP4_Stream_t *p_stream, MP4_Box_t *p_box );
2001     void (*MP4_FreeBox_function )( MP4_Box_t *p_box );
2002 } MP4_Box_Function [] =
2003 {
2004     /* Containers */
2005     { FOURCC_moov,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2006     { FOURCC_trak,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2007     { FOURCC_mdia,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2008     { FOURCC_moof,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2009     { FOURCC_minf,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2010     { FOURCC_stbl,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2011     { FOURCC_dinf,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2012     { FOURCC_edts,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2013     { FOURCC_udta,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2014     { FOURCC_nmhd,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2015     { FOURCC_hnti,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2016     { FOURCC_rmra,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2017     { FOURCC_rmda,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2018     { FOURCC_tref,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2019     { FOURCC_gmhd,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2020     { FOURCC_wave,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2021
2022     /* specific box */
2023     { FOURCC_ftyp,  MP4_ReadBox_ftyp,       MP4_FreeBox_ftyp },
2024     { FOURCC_cmov,  MP4_ReadBox_cmov,       MP4_FreeBox_Common },
2025     { FOURCC_mvhd,  MP4_ReadBox_mvhd,       MP4_FreeBox_Common },
2026     { FOURCC_tkhd,  MP4_ReadBox_tkhd,       MP4_FreeBox_Common },
2027     { FOURCC_mdhd,  MP4_ReadBox_mdhd,       MP4_FreeBox_Common },
2028     { FOURCC_hdlr,  MP4_ReadBox_hdlr,       MP4_FreeBox_hdlr },
2029     { FOURCC_vmhd,  MP4_ReadBox_vmhd,       MP4_FreeBox_Common },
2030     { FOURCC_smhd,  MP4_ReadBox_smhd,       MP4_FreeBox_Common },
2031     { FOURCC_hmhd,  MP4_ReadBox_hmhd,       MP4_FreeBox_Common },
2032     { FOURCC_url,   MP4_ReadBox_url,        MP4_FreeBox_url },
2033     { FOURCC_urn,   MP4_ReadBox_urn,        MP4_FreeBox_urn },
2034     { FOURCC_dref,  MP4_ReadBox_dref,       MP4_FreeBox_Common },
2035     { FOURCC_stts,  MP4_ReadBox_stts,       MP4_FreeBox_stts },
2036     { FOURCC_ctts,  MP4_ReadBox_ctts,       MP4_FreeBox_ctts },
2037     { FOURCC_stsd,  MP4_ReadBox_stsd,       MP4_FreeBox_Common },
2038     { FOURCC_stsz,  MP4_ReadBox_stsz,       MP4_FreeBox_stsz },
2039     { FOURCC_stsc,  MP4_ReadBox_stsc,       MP4_FreeBox_stsc },
2040     { FOURCC_stco,  MP4_ReadBox_stco_co64,  MP4_FreeBox_stco_co64 },
2041     { FOURCC_co64,  MP4_ReadBox_stco_co64,  MP4_FreeBox_stco_co64 },
2042     { FOURCC_stss,  MP4_ReadBox_stss,       MP4_FreeBox_stss },
2043     { FOURCC_stsh,  MP4_ReadBox_stsh,       MP4_FreeBox_stsh },
2044     { FOURCC_stdp,  MP4_ReadBox_stdp,       MP4_FreeBox_stdp },
2045     { FOURCC_padb,  MP4_ReadBox_padb,       MP4_FreeBox_padb },
2046     { FOURCC_elst,  MP4_ReadBox_elst,       MP4_FreeBox_elst },
2047     { FOURCC_cprt,  MP4_ReadBox_cprt,       MP4_FreeBox_cprt },
2048     { FOURCC_esds,  MP4_ReadBox_esds,       MP4_FreeBox_esds },
2049     { FOURCC_dcom,  MP4_ReadBox_dcom,       MP4_FreeBox_Common },
2050     { FOURCC_cmvd,  MP4_ReadBox_cmvd,       MP4_FreeBox_cmvd },
2051
2052     /* Nothing to do with this box */
2053     { FOURCC_mdat,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
2054     { FOURCC_skip,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
2055     { FOURCC_free,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
2056     { FOURCC_wide,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
2057
2058     /* for codecs */
2059     { FOURCC_soun,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2060     { FOURCC_ms02,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2061     { FOURCC_ms11,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2062     { FOURCC_ms55,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2063     { FOURCC__mp3,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2064     { FOURCC_mp4a,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2065     { FOURCC_twos,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2066     { FOURCC_sowt,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2067     { FOURCC_QDMC,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2068     { FOURCC_QDM2,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2069     { FOURCC_ima4,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2070     { FOURCC_IMA4,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2071     { FOURCC_dvi,   MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2072     { FOURCC_alaw,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2073     { FOURCC_ulaw,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2074     { FOURCC_raw,   MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2075     { FOURCC_MAC3,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2076     { FOURCC_MAC6,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2077     { FOURCC_Qclp,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2078     { FOURCC_samr,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2079     { FOURCC_OggS,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2080
2081     { FOURCC_vide,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2082     { FOURCC_mp4v,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2083     { FOURCC_SVQ1,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2084     { FOURCC_SVQ3,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2085     { FOURCC_ZyGo,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2086     { FOURCC_DIVX,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2087     { FOURCC_h263,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2088     { FOURCC_s263,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2089     { FOURCC_cvid,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2090     { FOURCC_3IV1,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2091     { FOURCC_3iv1,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2092     { FOURCC_3IV2,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2093     { FOURCC_3iv2,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2094     { FOURCC_3IVD,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2095     { FOURCC_3ivd,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2096     { FOURCC_3VID,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2097     { FOURCC_3vid,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2098     { FOURCC_mjpa,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2099     { FOURCC_mjpb,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2100     { FOURCC_mjqt,  NULL,                       NULL }, /* found in mjpa/b */
2101     { FOURCC_mjht,  NULL,                       NULL },
2102     { FOURCC_dvc,   MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2103     { FOURCC_dvp,   MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2104     { FOURCC_VP31,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2105     { FOURCC_vp31,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2106     { FOURCC_h264,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2107
2108     { FOURCC_jpeg,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2109
2110     { FOURCC_mp4s,  NULL,                       MP4_FreeBox_Common },
2111
2112     /* XXX there is 2 box where we could find this entry stbl and tref*/
2113     { FOURCC_hint,  NULL,                       MP4_FreeBox_Common },
2114
2115     /* found in tref box */
2116     { FOURCC_dpnd,  NULL,   NULL },
2117     { FOURCC_ipir,  NULL,   NULL },
2118     { FOURCC_mpod,  NULL,   NULL },
2119
2120     /* found in hnti */
2121     { FOURCC_rtp,   NULL,   NULL },
2122
2123     /* found in rmra */
2124     { FOURCC_rdrf,  MP4_ReadBox_rdrf,           MP4_FreeBox_rdrf   },
2125     { FOURCC_rmdr,  MP4_ReadBox_rmdr,           MP4_FreeBox_Common },
2126     { FOURCC_rmqu,  MP4_ReadBox_rmqu,           MP4_FreeBox_Common },
2127     { FOURCC_rmvc,  MP4_ReadBox_rmvc,           MP4_FreeBox_Common },
2128
2129     { FOURCC_drms,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2130     { FOURCC_sinf,  MP4_ReadBoxContainer,       MP4_FreeBox_Common },
2131     { FOURCC_schi,  MP4_ReadBoxContainer,       MP4_FreeBox_Common },
2132     { FOURCC_user,  MP4_ReadBox_drms,           MP4_FreeBox_Common },
2133     { FOURCC_key,   MP4_ReadBox_drms,           MP4_FreeBox_Common },
2134     { FOURCC_iviv,  MP4_ReadBox_drms,           MP4_FreeBox_Common },
2135     { FOURCC_name,  MP4_ReadBox_drms,           MP4_FreeBox_Common },
2136     { FOURCC_priv,  MP4_ReadBox_drms,           MP4_FreeBox_Common },
2137
2138     /* found in udta */
2139     { FOURCC_0xa9nam,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2140     { FOURCC_0xa9aut,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2141     { FOURCC_0xa9cpy,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2142     { FOURCC_0xa9swr,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2143     { FOURCC_0xa9inf,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2144     { FOURCC_0xa9ART,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2145     { FOURCC_0xa9dir,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2146     { FOURCC_0xa9cmt,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2147     { FOURCC_0xa9req,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2148     { FOURCC_0xa9day,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2149     { FOURCC_0xa9des,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2150     { FOURCC_0xa9fmt,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2151     { FOURCC_0xa9prd,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2152     { FOURCC_0xa9prf,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2153     { FOURCC_0xa9src,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2154
2155     /* Last entry */
2156     { 0,            NULL,                   NULL }
2157 };
2158
2159
2160
2161 /*****************************************************************************
2162  * MP4_ReadBox : parse the actual box and the children
2163  *  XXX : Do not go to the next box
2164  *****************************************************************************/
2165 static MP4_Box_t *MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_father )
2166 {
2167     MP4_Box_t    *p_box = malloc( sizeof( MP4_Box_t ) );
2168     unsigned int i_index;
2169
2170     if( !MP4_ReadBoxCommon( p_stream, p_box ) )
2171     {
2172         msg_Warn( p_stream->s, "cannot read one box" );
2173         free( p_box );
2174         return NULL;
2175     }
2176     if( !p_box->i_size )
2177     {
2178         msg_Dbg( p_stream->s, "found an empty box (null size)" );
2179         free( p_box );
2180         return NULL;
2181     }
2182     p_box->p_father = p_father;
2183
2184     /* Now search function to call */
2185     for( i_index = 0; ; i_index++ )
2186     {
2187         if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
2188             ( MP4_Box_Function[i_index].i_type == 0 ) )
2189         {
2190             break;
2191         }
2192     }
2193     if( MP4_Box_Function[i_index].MP4_ReadBox_function == NULL )
2194     {
2195         msg_Warn( p_stream->s,
2196                   "unknown box type %4.4s (uncompletetly loaded)",
2197                   (char*)&p_box->i_type );
2198     }
2199     else if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
2200     {
2201         free( p_box );
2202         return NULL;
2203     }
2204
2205     return p_box;
2206 }
2207
2208 /*****************************************************************************
2209  * MP4_FreeBox : free memory after read with MP4_ReadBox and all
2210  * the children
2211  *****************************************************************************/
2212 void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
2213 {
2214     unsigned int i_index;
2215     MP4_Box_t    *p_child;
2216
2217     if( !p_box )
2218     {
2219         return; /* hehe */
2220     }
2221
2222     for( p_child = p_box->p_first; p_child != NULL; )
2223     {
2224         MP4_Box_t *p_next;
2225
2226         p_next = p_child->p_next;
2227         MP4_BoxFree( s, p_child );
2228         p_child = p_next;
2229     }
2230
2231     /* Now search function to call */
2232     if( p_box->data.p_data )
2233     {
2234         for( i_index = 0; ; i_index++ )
2235         {
2236             if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
2237                 ( MP4_Box_Function[i_index].i_type == 0 ) )
2238             {
2239                 break;
2240             }
2241         }
2242         if( MP4_Box_Function[i_index].MP4_FreeBox_function == NULL )
2243         {
2244             /* Should not happen */
2245             msg_Warn( s,
2246                       "cannot free box %4.4s, type unknown",
2247                       (char*)&p_box->i_type );
2248         }
2249         else
2250         {
2251             MP4_Box_Function[i_index].MP4_FreeBox_function( p_box );
2252         }
2253
2254         free( p_box->data.p_data );
2255     }
2256
2257     free( p_box );
2258 }
2259
2260 /*****************************************************************************
2261  * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
2262  *****************************************************************************
2263  *  The first box is a virtual box "root" and is the father for all first
2264  *  level boxes for the file, a sort of virtual contener
2265  *****************************************************************************/
2266 MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
2267 {
2268     MP4_Box_t *p_root;
2269     MP4_Stream_t *p_stream;
2270     int i_result;
2271
2272     p_root = malloc( sizeof( MP4_Box_t ) );
2273     p_root->i_pos = 0;
2274     p_root->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
2275     p_root->i_shortsize = 1;
2276     p_root->i_size = stream_Size( s );
2277     CreateUUID( &p_root->i_uuid, p_root->i_type );
2278
2279     p_root->data.p_data = NULL;
2280     p_root->p_father = NULL;
2281     p_root->p_first  = NULL;
2282     p_root->p_last  = NULL;
2283     p_root->p_next   = NULL;
2284
2285     p_stream = MP4_InputStream( s );
2286
2287     i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
2288
2289     free( p_stream );
2290
2291     if( i_result )
2292     {
2293         MP4_Box_t *p_child;
2294         MP4_Box_t *p_moov;
2295         MP4_Box_t *p_cmov;
2296
2297         /* check if there is a cmov, if so replace
2298           compressed moov by  uncompressed one */
2299         if( ( ( p_moov = MP4_BoxGet( p_root, "moov" ) ) &&
2300               ( p_cmov = MP4_BoxGet( p_root, "moov/cmov" ) ) ) ||
2301             ( ( p_moov = MP4_BoxGet( p_root, "foov" ) ) &&
2302               ( p_cmov = MP4_BoxGet( p_root, "foov/cmov" ) ) ) )
2303         {
2304             /* rename the compressed moov as a box to skip */
2305             p_moov->i_type = FOURCC_skip;
2306
2307             /* get uncompressed p_moov */
2308             p_moov = p_cmov->data.p_cmov->p_moov;
2309             p_cmov->data.p_cmov->p_moov = NULL;
2310
2311             /* make p_root father of this new moov */
2312             p_moov->p_father = p_root;
2313
2314             /* insert this new moov box as first child of p_root */
2315             p_moov->p_next = p_child = p_root->p_first;
2316             p_root->p_first = p_moov;
2317         }
2318     }
2319
2320     return p_root;
2321 }
2322
2323
2324 static void __MP4_BoxDumpStructure( stream_t *s,
2325                                     MP4_Box_t *p_box, unsigned int i_level )
2326 {
2327     MP4_Box_t *p_child;
2328
2329     if( !i_level )
2330     {
2331         msg_Dbg( s, "dumping root Box \"%4.4s\"",
2332                           (char*)&p_box->i_type );
2333     }
2334     else
2335     {
2336         char str[512];
2337         unsigned int i;
2338         memset( str, (uint8_t)' ', 512 );
2339         for( i = 0; i < i_level; i++ )
2340         {
2341             str[i*5] = '|';
2342         }
2343         sprintf( str + i_level * 5, "+ %4.4s size %d",
2344                       (char*)&p_box->i_type,
2345                       (uint32_t)p_box->i_size );
2346
2347         msg_Dbg( s, "%s", str );
2348     }
2349     p_child = p_box->p_first;
2350     while( p_child )
2351     {
2352         __MP4_BoxDumpStructure( s, p_child, i_level + 1 );
2353         p_child = p_child->p_next;
2354     }
2355 }
2356
2357 void MP4_BoxDumpStructure( stream_t *s, MP4_Box_t *p_box )
2358 {
2359     __MP4_BoxDumpStructure( s, p_box, 0 );
2360 }
2361
2362
2363
2364 /*****************************************************************************
2365  *****************************************************************************
2366  **
2367  **  High level methods to acces an MP4 file
2368  **
2369  *****************************************************************************
2370  *****************************************************************************/
2371 static void __get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
2372 {
2373     size_t i_len ;
2374     if( !*ppsz_path[0] )
2375     {
2376         *ppsz_token = NULL;
2377         *pi_number = 0;
2378         return;
2379     }
2380     i_len = 0;
2381     while(  (*ppsz_path)[i_len] &&
2382             (*ppsz_path)[i_len] != '/' && (*ppsz_path)[i_len] != '[' )
2383     {
2384         i_len++;
2385     }
2386     if( !i_len && **ppsz_path == '/' )
2387     {
2388         i_len = 1;
2389     }
2390     *ppsz_token = malloc( i_len + 1 );
2391
2392     memcpy( *ppsz_token, *ppsz_path, i_len );
2393
2394     (*ppsz_token)[i_len] = '\0';
2395
2396     *ppsz_path += i_len;
2397
2398     if( **ppsz_path == '[' )
2399     {
2400         (*ppsz_path)++;
2401         *pi_number = strtol( *ppsz_path, NULL, 10 );
2402         while( **ppsz_path && **ppsz_path != ']' )
2403         {
2404             (*ppsz_path)++;
2405         }
2406         if( **ppsz_path == ']' )
2407         {
2408             (*ppsz_path)++;
2409         }
2410     }
2411     else
2412     {
2413         *pi_number = 0;
2414     }
2415     while( **ppsz_path == '/' )
2416     {
2417         (*ppsz_path)++;
2418     }
2419 }
2420
2421 static void __MP4_BoxGet( MP4_Box_t **pp_result,
2422                           MP4_Box_t *p_box, char *psz_fmt, va_list args)
2423 {
2424     char    *psz_path;
2425
2426     if( !p_box )
2427     {
2428         *pp_result = NULL;
2429         return;
2430     }
2431
2432     vasprintf( &psz_path, psz_fmt, args );
2433
2434     if( !psz_path || !psz_path[0] )
2435     {
2436         FREE( psz_path );
2437         *pp_result = NULL;
2438         return;
2439     }
2440
2441 //    fprintf( stderr, "path:'%s'\n", psz_path );
2442     psz_fmt = psz_path; /* keep this pointer, as it need to be unallocated */
2443     for( ; ; )
2444     {
2445         char *psz_token;
2446         int i_number;
2447
2448         __get_token( &psz_path, &psz_token, &i_number );
2449 //        fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
2450 //                 psz_path,psz_token,i_number );
2451         if( !psz_token )
2452         {
2453             FREE( psz_token );
2454             free( psz_fmt );
2455             *pp_result = p_box;
2456             return;
2457         }
2458         else
2459         if( !strcmp( psz_token, "/" ) )
2460         {
2461             /* Find root box */
2462             while( p_box && p_box->i_type != VLC_FOURCC( 'r', 'o', 'o', 't' ) )
2463             {
2464                 p_box = p_box->p_father;
2465             }
2466             if( !p_box )
2467             {
2468                 free( psz_token );
2469                 free( psz_fmt );
2470                 *pp_result = NULL;
2471                 return;
2472             }
2473         }
2474         else
2475         if( !strcmp( psz_token, "." ) )
2476         {
2477             /* Do nothing */
2478         }
2479         else
2480         if( !strcmp( psz_token, ".." ) )
2481         {
2482             p_box = p_box->p_father;
2483             if( !p_box )
2484             {
2485                 free( psz_token );
2486                 free( psz_fmt );
2487                 *pp_result = NULL;
2488                 return;
2489             }
2490         }
2491         else
2492         if( strlen( psz_token ) == 4 )
2493         {
2494             uint32_t i_fourcc;
2495             i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
2496                                    psz_token[2], psz_token[3] );
2497             p_box = p_box->p_first;
2498             for( ; ; )
2499             {
2500                 if( !p_box )
2501                 {
2502                     free( psz_token );
2503                     free( psz_fmt );
2504                     *pp_result = NULL;
2505                     return;
2506                 }
2507                 if( p_box->i_type == i_fourcc )
2508                 {
2509                     if( !i_number )
2510                     {
2511                         break;
2512                     }
2513                     i_number--;
2514                 }
2515                 p_box = p_box->p_next;
2516             }
2517         }
2518         else
2519         if( strlen( psz_token ) == 0 )
2520         {
2521             p_box = p_box->p_first;
2522             for( ; ; )
2523             {
2524                 if( !p_box )
2525                 {
2526                     free( psz_token );
2527                     free( psz_fmt );
2528                     *pp_result = NULL;
2529                     return;
2530                 }
2531                 if( !i_number )
2532                 {
2533                     break;
2534                 }
2535                 i_number--;
2536                 p_box = p_box->p_next;
2537             }
2538         }
2539         else
2540         {
2541 //            fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
2542             FREE( psz_token );
2543             free( psz_fmt );
2544             *pp_result = NULL;
2545             return;
2546         }
2547
2548         free( psz_token );
2549     }
2550 }
2551
2552 /*****************************************************************************
2553  * MP4_BoxGet: find a box given a path relative to p_box
2554  *****************************************************************************
2555  * Path Format: . .. / as usual
2556  *              [number] to specifie box number ex: trak[12]
2557  *
2558  * ex: /moov/trak[12]
2559  *     ../mdia
2560  *****************************************************************************/
2561 MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, char *psz_fmt, ... )
2562 {
2563     va_list args;
2564     MP4_Box_t *p_result;
2565
2566     va_start( args, psz_fmt );
2567     __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
2568     va_end( args );
2569
2570     return( p_result );
2571 }
2572
2573 /*****************************************************************************
2574  * MP4_BoxCount: count box given a path relative to p_box
2575  *****************************************************************************
2576  * Path Format: . .. / as usual
2577  *              [number] to specifie box number ex: trak[12]
2578  *
2579  * ex: /moov/trak[12]
2580  *     ../mdia
2581  *****************************************************************************/
2582 int MP4_BoxCount( MP4_Box_t *p_box, char *psz_fmt, ... )
2583 {
2584     va_list args;
2585     int     i_count;
2586     MP4_Box_t *p_result, *p_next;
2587
2588     va_start( args, psz_fmt );
2589     __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
2590     va_end( args );
2591     if( !p_result )
2592     {
2593         return( 0 );
2594     }
2595
2596     i_count = 1;
2597     for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
2598     {
2599         if( p_next->i_type == p_result->i_type)
2600         {
2601             i_count++;
2602         }
2603     }
2604     return( i_count );
2605 }