]> git.sesse.net Git - vlc/blob - modules/demux/mp4/libmp4.c
* mp4*: demux -> demux2.
[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
1089     /* Sanity check needed because the "wave" box does also contain an
1090      * "mp4a" box that we don't understand. */
1091     if( i_read < 28 )
1092     {
1093         i_read -= 30;
1094         MP4_READBOX_EXIT( 1 );
1095     }
1096
1097     for( i = 0; i < 6 ; i++ )
1098     {
1099         MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
1100     }
1101
1102     MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
1103
1104     /*
1105      * XXX hack -> produce a copy of the nearly complete chunk
1106      */
1107     if( i_read > 0 )
1108     {
1109         p_box->data.p_sample_soun->i_qt_description = i_read;
1110         p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
1111         memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
1112     }
1113     else
1114     {
1115         p_box->data.p_sample_soun->i_qt_description = 0;
1116         p_box->data.p_sample_soun->p_qt_description = NULL;
1117     }
1118
1119     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
1120     MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
1121     MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
1122
1123     MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
1124     MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
1125     MP4_GET2BYTES( p_box->data.p_sample_soun->i_predefined );
1126     MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
1127     MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
1128     MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
1129
1130     if( p_box->data.p_sample_soun->i_qt_version == 1 &&
1131         i_read >= 16 )
1132     {
1133         /* qt3+ */
1134         MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
1135         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
1136         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
1137         MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
1138
1139 #ifdef MP4_VERBOSE
1140         msg_Dbg( p_stream->s,
1141                  "read box: \"soun\" qt3+ sample/packet=%d bytes/packet=%d bytes/frame=%d bytes/sample=%d",
1142                  p_box->data.p_sample_soun->i_sample_per_packet, p_box->data.p_sample_soun->i_bytes_per_packet,
1143                  p_box->data.p_sample_soun->i_bytes_per_frame, p_box->data.p_sample_soun->i_bytes_per_sample );
1144 #endif
1145         MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 44 );
1146     }
1147     else
1148     {
1149         p_box->data.p_sample_soun->i_sample_per_packet = 0;
1150         p_box->data.p_sample_soun->i_bytes_per_packet = 0;
1151         p_box->data.p_sample_soun->i_bytes_per_frame = 0;
1152         p_box->data.p_sample_soun->i_bytes_per_sample = 0;
1153
1154         msg_Dbg( p_stream->s, "read box: \"soun\" mp4 or qt1/2 (rest="I64Fd")", i_read );
1155         MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 28 );
1156     }
1157
1158     if( p_box->i_type == FOURCC_drms )
1159     {
1160         p_box->data.p_sample_soun->p_drms =
1161             drms_alloc( p_stream->s->p_vlc->psz_homedir );
1162
1163         if( p_box->data.p_sample_soun->p_drms == NULL )
1164         {
1165             msg_Err( p_stream->s, "drms_alloc() failed" );
1166         }
1167     }
1168
1169     MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds */
1170
1171 #ifdef MP4_VERBOSE
1172     msg_Dbg( p_stream->s, "read box: \"soun\" in stsd channel %d sample size %d sampl rate %f",
1173                       p_box->data.p_sample_soun->i_channelcount,
1174                       p_box->data.p_sample_soun->i_samplesize,
1175                       (float)p_box->data.p_sample_soun->i_sampleratehi +
1176                     (float)p_box->data.p_sample_soun->i_sampleratelo / 65536 );
1177
1178 #endif
1179     MP4_READBOX_EXIT( 1 );
1180 }
1181
1182
1183 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
1184 {
1185     FREE( p_box->data.p_sample_soun->p_qt_description );
1186
1187     if( p_box->i_type == FOURCC_drms )
1188     {
1189         if( p_box->data.p_sample_soun->p_drms )
1190         {
1191             drms_free( p_box->data.p_sample_soun->p_drms );
1192         }
1193     }
1194 }
1195
1196
1197 static int MP4_ReadBox_sample_vide( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1198 {
1199     unsigned int i;
1200
1201     MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );
1202
1203     for( i = 0; i < 6 ; i++ )
1204     {
1205         MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
1206     }
1207
1208     MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
1209
1210     /*
1211      * XXX hack -> produce a copy of the nearly complete chunk
1212      */
1213     if( i_read > 0 )
1214     {
1215         p_box->data.p_sample_vide->i_qt_image_description = i_read;
1216         p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
1217         memcpy( p_box->data.p_sample_vide->p_qt_image_description,
1218                 p_peek, i_read );
1219     }
1220     else
1221     {
1222         p_box->data.p_sample_vide->i_qt_image_description = 0;
1223         p_box->data.p_sample_vide->p_qt_image_description = NULL;
1224     }
1225
1226     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
1227     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
1228     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
1229
1230     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
1231     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
1232
1233     MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
1234     MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
1235
1236     MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
1237     MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
1238
1239     MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
1240     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
1241
1242     memcpy( &p_box->data.p_sample_vide->i_compressorname, p_peek, 32 );
1243     p_peek += 32; i_read -= 32;
1244
1245     MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
1246     MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
1247
1248     MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 78);
1249     MP4_ReadBoxContainerRaw( p_stream, p_box );
1250
1251 #ifdef MP4_VERBOSE
1252     msg_Dbg( p_stream->s, "read box: \"vide\" in stsd %dx%d depth %d",
1253                       p_box->data.p_sample_vide->i_width,
1254                       p_box->data.p_sample_vide->i_height,
1255                       p_box->data.p_sample_vide->i_depth );
1256
1257 #endif
1258     MP4_READBOX_EXIT( 1 );
1259 }
1260
1261
1262 static void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
1263 {
1264     FREE( p_box->data.p_sample_vide->p_qt_image_description );
1265 }
1266
1267
1268 static int MP4_ReadBox_stsd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1269 {
1270
1271     MP4_READBOX_ENTER( MP4_Box_data_stsd_t );
1272
1273     MP4_GETVERSIONFLAGS( p_box->data.p_stsd );
1274
1275     MP4_GET4BYTES( p_box->data.p_stsd->i_entry_count );
1276
1277     MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 );
1278
1279     MP4_ReadBoxContainerRaw( p_stream, p_box );
1280
1281 #ifdef MP4_VERBOSE
1282     msg_Dbg( p_stream->s, "read box: \"stsd\" entry-count %d",
1283                       p_box->data.p_stsd->i_entry_count );
1284
1285 #endif
1286     MP4_READBOX_EXIT( 1 );
1287 }
1288
1289
1290 static int MP4_ReadBox_stsz( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1291 {
1292     unsigned int i;
1293
1294     MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
1295
1296     MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
1297
1298     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
1299
1300     MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
1301
1302     p_box->data.p_stsz->i_entry_size =
1303         calloc( sizeof( uint32_t ), p_box->data.p_stsz->i_sample_count );
1304
1305     if( !p_box->data.p_stsz->i_sample_size )
1306     {
1307         for( i=0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
1308         {
1309             MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
1310         }
1311     }
1312
1313 #ifdef MP4_VERBOSE
1314     msg_Dbg( p_stream->s, "read box: \"stsz\" sample-size %d sample-count %d",
1315                       p_box->data.p_stsz->i_sample_size,
1316                       p_box->data.p_stsz->i_sample_count );
1317
1318 #endif
1319     MP4_READBOX_EXIT( 1 );
1320 }
1321
1322 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
1323 {
1324     FREE( p_box->data.p_stsz->i_entry_size );
1325 }
1326
1327 static int MP4_ReadBox_stsc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1328 {
1329     unsigned int i;
1330
1331     MP4_READBOX_ENTER( MP4_Box_data_stsc_t );
1332
1333     MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
1334
1335     MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
1336
1337     p_box->data.p_stsc->i_first_chunk =
1338         calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1339     p_box->data.p_stsc->i_samples_per_chunk =
1340         calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1341     p_box->data.p_stsc->i_sample_description_index =
1342         calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1343
1344     for( i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
1345     {
1346         MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
1347         MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
1348         MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
1349     }
1350
1351 #ifdef MP4_VERBOSE
1352     msg_Dbg( p_stream->s, "read box: \"stsc\" entry-count %d",
1353                       p_box->data.p_stsc->i_entry_count );
1354
1355 #endif
1356     MP4_READBOX_EXIT( 1 );
1357 }
1358
1359 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
1360 {
1361     FREE( p_box->data.p_stsc->i_first_chunk );
1362     FREE( p_box->data.p_stsc->i_samples_per_chunk );
1363     FREE( p_box->data.p_stsc->i_sample_description_index );
1364 }
1365
1366 static int MP4_ReadBox_stco_co64( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1367 {
1368     unsigned int i;
1369
1370     MP4_READBOX_ENTER( MP4_Box_data_co64_t );
1371
1372     MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
1373
1374     MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
1375
1376     p_box->data.p_co64->i_chunk_offset =
1377         calloc( sizeof( uint64_t ), p_box->data.p_co64->i_entry_count );
1378
1379     for( i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
1380     {
1381         if( p_box->i_type == FOURCC_stco )
1382         {
1383             if( i_read < 4 )
1384             {
1385                 break;
1386             }
1387             MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
1388         }
1389         else
1390         {
1391             if( i_read < 8 )
1392             {
1393                 break;
1394             }
1395             MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
1396         }
1397     }
1398
1399 #ifdef MP4_VERBOSE
1400     msg_Dbg( p_stream->s, "read box: \"co64\" entry-count %d",
1401                       p_box->data.p_co64->i_entry_count );
1402
1403 #endif
1404     MP4_READBOX_EXIT( 1 );
1405 }
1406
1407 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
1408 {
1409     FREE( p_box->data.p_co64->i_chunk_offset );
1410 }
1411
1412 static int MP4_ReadBox_stss( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1413 {
1414     unsigned int i;
1415
1416     MP4_READBOX_ENTER( MP4_Box_data_stss_t );
1417
1418     MP4_GETVERSIONFLAGS( p_box->data.p_stss );
1419
1420     MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
1421
1422     p_box->data.p_stss->i_sample_number =
1423         calloc( sizeof( uint32_t ), p_box->data.p_stss->i_entry_count );
1424
1425     for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
1426     {
1427
1428         MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
1429         /* XXX in libmp4 sample begin at 0 */
1430         p_box->data.p_stss->i_sample_number[i]--;
1431     }
1432
1433 #ifdef MP4_VERBOSE
1434     msg_Dbg( p_stream->s, "read box: \"stss\" entry-count %d",
1435                       p_box->data.p_stss->i_entry_count );
1436
1437 #endif
1438     MP4_READBOX_EXIT( 1 );
1439 }
1440
1441 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
1442 {
1443     FREE( p_box->data.p_stss->i_sample_number )
1444 }
1445
1446 static int MP4_ReadBox_stsh( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1447 {
1448     unsigned int i;
1449
1450     MP4_READBOX_ENTER( MP4_Box_data_stsh_t );
1451
1452     MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
1453
1454
1455     MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
1456
1457     p_box->data.p_stsh->i_shadowed_sample_number =
1458         calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
1459
1460     p_box->data.p_stsh->i_sync_sample_number =
1461         calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
1462
1463
1464     for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
1465     {
1466
1467         MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
1468         MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
1469     }
1470
1471 #ifdef MP4_VERBOSE
1472     msg_Dbg( p_stream->s, "read box: \"stsh\" entry-count %d",
1473                       p_box->data.p_stsh->i_entry_count );
1474 #endif
1475     MP4_READBOX_EXIT( 1 );
1476 }
1477
1478 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
1479 {
1480     FREE( p_box->data.p_stsh->i_shadowed_sample_number )
1481     FREE( p_box->data.p_stsh->i_sync_sample_number )
1482 }
1483
1484
1485 static int MP4_ReadBox_stdp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1486 {
1487     unsigned int i;
1488
1489     MP4_READBOX_ENTER( MP4_Box_data_stdp_t );
1490
1491     MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
1492
1493     p_box->data.p_stdp->i_priority =
1494         calloc( sizeof( uint16_t ), i_read / 2 );
1495
1496     for( i = 0; i < i_read / 2 ; i++ )
1497     {
1498
1499         MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
1500     }
1501
1502 #ifdef MP4_VERBOSE
1503     msg_Dbg( p_stream->s, "read box: \"stdp\" entry-count "I64Fd,
1504                       i_read / 2 );
1505
1506 #endif
1507     MP4_READBOX_EXIT( 1 );
1508 }
1509
1510 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
1511 {
1512     FREE( p_box->data.p_stdp->i_priority )
1513 }
1514
1515 static int MP4_ReadBox_padb( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1516 {
1517     unsigned int i;
1518
1519     MP4_READBOX_ENTER( MP4_Box_data_padb_t );
1520
1521     MP4_GETVERSIONFLAGS( p_box->data.p_padb );
1522
1523
1524     MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
1525
1526     p_box->data.p_padb->i_reserved1 =
1527         calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1528     p_box->data.p_padb->i_pad2 =
1529         calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1530     p_box->data.p_padb->i_reserved2 =
1531         calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1532     p_box->data.p_padb->i_pad1 =
1533         calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1534
1535
1536     for( i = 0; i < i_read / 2 ; i++ )
1537     {
1538         p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
1539         p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
1540         p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
1541         p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;
1542
1543         p_peek += 1; i_read -= 1;
1544     }
1545
1546 #ifdef MP4_VERBOSE
1547     msg_Dbg( p_stream->s, "read box: \"stdp\" entry-count "I64Fd,
1548                       i_read / 2 );
1549
1550 #endif
1551     MP4_READBOX_EXIT( 1 );
1552 }
1553
1554 static void MP4_FreeBox_padb( MP4_Box_t *p_box )
1555 {
1556     FREE( p_box->data.p_padb->i_reserved1 );
1557     FREE( p_box->data.p_padb->i_pad2 );
1558     FREE( p_box->data.p_padb->i_reserved2 );
1559     FREE( p_box->data.p_padb->i_pad1 );
1560 }
1561
1562 static int MP4_ReadBox_elst( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1563 {
1564     unsigned int i;
1565
1566     MP4_READBOX_ENTER( MP4_Box_data_padb_t );
1567
1568     MP4_GETVERSIONFLAGS( p_box->data.p_elst );
1569
1570
1571     MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
1572
1573     p_box->data.p_elst->i_segment_duration =
1574         calloc( sizeof( uint64_t ), p_box->data.p_elst->i_entry_count );
1575     p_box->data.p_elst->i_media_time =
1576         calloc( sizeof( int64_t ),  p_box->data.p_elst->i_entry_count );
1577     p_box->data.p_elst->i_media_rate_integer =
1578         calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
1579     p_box->data.p_elst->i_media_rate_fraction=
1580         calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
1581
1582
1583     for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
1584     {
1585         if( p_box->data.p_elst->i_version == 1 )
1586         {
1587
1588             MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
1589
1590             MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
1591         }
1592         else
1593         {
1594
1595             MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
1596
1597             MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
1598             p_box->data.p_elst->i_media_time[i] = (int32_t)p_box->data.p_elst->i_media_time[i];
1599         }
1600
1601         MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
1602         MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
1603     }
1604
1605 #ifdef MP4_VERBOSE
1606     msg_Dbg( p_stream->s, "read box: \"elst\" entry-count "I64Fd,
1607                       i_read / 2 );
1608
1609 #endif
1610     MP4_READBOX_EXIT( 1 );
1611 }
1612
1613 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
1614 {
1615     FREE( p_box->data.p_elst->i_segment_duration );
1616     FREE( p_box->data.p_elst->i_media_time );
1617     FREE( p_box->data.p_elst->i_media_rate_integer );
1618     FREE( p_box->data.p_elst->i_media_rate_fraction );
1619 }
1620
1621 static int MP4_ReadBox_cprt( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1622 {
1623     unsigned int i_language;
1624     unsigned int i;
1625
1626     MP4_READBOX_ENTER( MP4_Box_data_cprt_t );
1627
1628     MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
1629
1630     i_language = GetWBE( p_peek );
1631     for( i = 0; i < 3; i++ )
1632     {
1633         p_box->data.p_cprt->i_language[i] =
1634             ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
1635     }
1636     p_peek += 2; i_read -= 2;
1637     MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
1638
1639 #ifdef MP4_VERBOSE
1640     msg_Dbg( p_stream->s, "read box: \"cprt\" language %c%c%c notice %s",
1641                       p_box->data.p_cprt->i_language[0],
1642                       p_box->data.p_cprt->i_language[1],
1643                       p_box->data.p_cprt->i_language[2],
1644                       p_box->data.p_cprt->psz_notice );
1645
1646 #endif
1647     MP4_READBOX_EXIT( 1 );
1648 }
1649
1650 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
1651 {
1652     FREE( p_box->data.p_cprt->psz_notice );
1653 }
1654
1655
1656 static int MP4_ReadBox_dcom( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1657 {
1658     MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
1659
1660     MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
1661 #ifdef MP4_VERBOSE
1662     msg_Dbg( p_stream->s,
1663              "read box: \"dcom\" compression algorithm : %4.4s",
1664                       (char*)&p_box->data.p_dcom->i_algorithm );
1665 #endif
1666     MP4_READBOX_EXIT( 1 );
1667 }
1668
1669 static int MP4_ReadBox_cmvd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1670 {
1671     MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );
1672
1673     MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
1674
1675     p_box->data.p_cmvd->i_compressed_size = i_read;
1676
1677     if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
1678     {
1679         msg_Dbg( p_stream->s, "read box: \"cmvd\" not enough memory to load data" );
1680         return( 1 );
1681     }
1682
1683     /* now copy compressed data */
1684     memcpy( p_box->data.p_cmvd->p_data,
1685             p_peek,
1686             i_read);
1687
1688     p_box->data.p_cmvd->b_compressed = 1;
1689
1690 #ifdef MP4_VERBOSE
1691     msg_Dbg( p_stream->s, "read box: \"cmvd\" compressed data size %d",
1692                       p_box->data.p_cmvd->i_compressed_size );
1693 #endif
1694
1695     MP4_READBOX_EXIT( 1 );
1696 }
1697 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
1698 {
1699     FREE( p_box->data.p_cmvd->p_data );
1700 }
1701
1702
1703 static int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1704 {
1705     MP4_Stream_t *p_stream_memory;
1706
1707     MP4_Box_t *p_dcom;
1708     MP4_Box_t *p_cmvd;
1709 #ifdef HAVE_ZLIB_H
1710     z_stream  z_data;
1711 #endif
1712     uint8_t *p_data;
1713
1714     int i_result;
1715
1716     if( !( p_box->data.p_cmov = malloc( sizeof( MP4_Box_data_cmov_t ) ) ) )
1717     {
1718         msg_Err( p_stream->s, "out of memory" );
1719         return( 0 );
1720     }
1721     memset( p_box->data.p_cmov, 0, sizeof( MP4_Box_data_cmov_t ) );
1722
1723     if( !p_box->p_father ||
1724         ( p_box->p_father->i_type != FOURCC_moov && p_box->p_father->i_type != FOURCC_foov) )
1725     {
1726         msg_Warn( p_stream->s, "Read box: \"cmov\" box alone" );
1727         return( 1 );
1728     }
1729
1730     if( !(i_result = MP4_ReadBoxContainer( p_stream, p_box ) ) )
1731     {
1732         return( 0 );
1733     }
1734
1735     if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
1736         ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
1737         p_cmvd->data.p_cmvd->p_data == NULL )
1738     {
1739         msg_Warn( p_stream->s, "read box: \"cmov\" incomplete" );
1740         return( 1 );
1741     }
1742
1743     if( p_dcom->data.p_dcom->i_algorithm != FOURCC_zlib )
1744     {
1745         msg_Dbg( p_stream->s, "read box: \"cmov\" compression algorithm : %4.4s not supported",
1746                     (char*)&p_dcom->data.p_dcom->i_algorithm );
1747         return( 1 );
1748     }
1749
1750 #ifndef HAVE_ZLIB_H
1751     msg_Dbg( p_stream->s,
1752              "read box: \"cmov\" zlib unsupported" );
1753     return( 1 );
1754 #else
1755     /* decompress data */
1756     /* allocate a new buffer */
1757     if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
1758     {
1759         msg_Err( p_stream->s,
1760                  "read box: \"cmov\" not enough memory to uncompress data" );
1761         return( 1 );
1762     }
1763     /* init default structures */
1764     z_data.next_in   = p_cmvd->data.p_cmvd->p_data;
1765     z_data.avail_in  = p_cmvd->data.p_cmvd->i_compressed_size;
1766     z_data.next_out  = p_data;
1767     z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
1768     z_data.zalloc    = (alloc_func)Z_NULL;
1769     z_data.zfree     = (free_func)Z_NULL;
1770     z_data.opaque    = (voidpf)Z_NULL;
1771
1772     /* init zlib */
1773     if( ( i_result = inflateInit( &z_data ) ) != Z_OK )
1774     {
1775         msg_Err( p_stream->s,
1776                  "read box: \"cmov\" error while uncompressing data" );
1777         free( p_data );
1778         return( 1 );
1779     }
1780
1781     /* uncompress */
1782     i_result = inflate( &z_data, Z_NO_FLUSH );
1783     if( ( i_result != Z_OK )&&( i_result != Z_STREAM_END ) )
1784     {
1785         msg_Err( p_stream->s,
1786                  "read box: \"cmov\" error while uncompressing data" );
1787         free( p_data );
1788         return( 1 );
1789     }
1790
1791     if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
1792     {
1793         msg_Warn( p_stream->s,
1794                   "read box: \"cmov\" uncompressing data size mismatch" );
1795     }
1796     p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
1797
1798     /* close zlib */
1799     i_result = inflateEnd( &z_data );
1800     if( i_result != Z_OK )
1801     {
1802         msg_Warn( p_stream->s,
1803            "read box: \"cmov\" error while uncompressing data (ignored)" );
1804     }
1805
1806
1807     free( p_cmvd->data.p_cmvd->p_data );
1808     p_cmvd->data.p_cmvd->p_data = p_data;
1809     p_cmvd->data.p_cmvd->b_compressed = 0;
1810
1811     msg_Dbg( p_stream->s,
1812              "read box: \"cmov\" box succesfully uncompressed" );
1813
1814     /* now create a memory stream */
1815     p_stream_memory = MP4_MemoryStream( p_stream->s,
1816                                         p_cmvd->data.p_cmvd->i_uncompressed_size,
1817                                         p_cmvd->data.p_cmvd->p_data );
1818
1819     /* and read uncompressd moov */
1820     p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
1821
1822     free( p_stream_memory );
1823
1824 #ifdef MP4_VERBOSE
1825     msg_Dbg( p_stream->s,
1826              "read box: \"cmov\" compressed movie header completed" );
1827 #endif
1828     return( p_box->data.p_cmov->p_moov ? 1 : 0 );
1829 #endif /* HAVE_ZLIB_H */
1830 }
1831
1832 static int MP4_ReadBox_rdrf( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1833 {
1834     uint32_t i_len;
1835     MP4_READBOX_ENTER( MP4_Box_data_rdrf_t );
1836
1837     MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
1838     MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
1839     MP4_GET4BYTES( i_len );
1840     if( i_len > 0 )
1841     {
1842         uint32_t i;
1843         p_box->data.p_rdrf->psz_ref = malloc( i_len  + 1);
1844         for( i = 0; i < i_len; i++ )
1845         {
1846             MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
1847         }
1848         p_box->data.p_rdrf->psz_ref[i_len] = '\0';
1849     }
1850     else
1851     {
1852         p_box->data.p_rdrf->psz_ref = NULL;
1853     }
1854
1855 #ifdef MP4_VERBOSE
1856     msg_Dbg( p_stream->s,
1857              "read box: \"rdrf\" type:%4.4s ref %s",
1858              (char*)&p_box->data.p_rdrf->i_ref_type,
1859              p_box->data.p_rdrf->psz_ref );
1860
1861 #endif
1862     MP4_READBOX_EXIT( 1 );
1863 }
1864
1865 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
1866 {
1867     FREE( p_box->data.p_rdrf->psz_ref )
1868 }
1869
1870
1871 static int MP4_ReadBox_rmdr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1872 {
1873     MP4_READBOX_ENTER( MP4_Box_data_rmdr_t );
1874
1875     MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
1876
1877     MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
1878
1879 #ifdef MP4_VERBOSE
1880     msg_Dbg( p_stream->s,
1881              "read box: \"rmdr\" rate:%d",
1882              p_box->data.p_rmdr->i_rate );
1883 #endif
1884     MP4_READBOX_EXIT( 1 );
1885 }
1886
1887 static int MP4_ReadBox_rmqu( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1888 {
1889     MP4_READBOX_ENTER( MP4_Box_data_rmqu_t );
1890
1891     MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
1892
1893 #ifdef MP4_VERBOSE
1894     msg_Dbg( p_stream->s,
1895              "read box: \"rmqu\" quality:%d",
1896              p_box->data.p_rmqu->i_quality );
1897 #endif
1898     MP4_READBOX_EXIT( 1 );
1899 }
1900
1901 static int MP4_ReadBox_rmvc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1902 {
1903     MP4_READBOX_ENTER( MP4_Box_data_rmvc_t );
1904     MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
1905
1906     MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
1907     MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
1908     MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
1909     MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
1910
1911 #ifdef MP4_VERBOSE
1912     msg_Dbg( p_stream->s,
1913              "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
1914              (char*)&p_box->data.p_rmvc->i_gestaltType,
1915              p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
1916              p_box->data.p_rmvc->i_checkType );
1917 #endif
1918
1919     MP4_READBOX_EXIT( 1 );
1920 }
1921
1922 static int MP4_ReadBox_drms( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1923 {
1924     MP4_Box_t *p_drms_box = p_box;
1925
1926     MP4_READBOX_ENTER( uint8_t );
1927
1928     do
1929     {
1930         p_drms_box = p_drms_box->p_father;
1931     } while( p_drms_box && p_drms_box->i_type != FOURCC_drms );
1932
1933     if( p_drms_box && p_drms_box->data.p_sample_soun->p_drms )
1934     {
1935         if( drms_init( p_drms_box->data.p_sample_soun->p_drms,
1936                        p_box->i_type, p_peek, i_read ) )
1937         {
1938             msg_Err( p_stream->s, "drms_init( %4.4s ) failed",
1939                      (char *)&p_box->i_type );
1940
1941             drms_free( p_drms_box->data.p_sample_soun->p_drms );
1942             p_drms_box->data.p_sample_soun->p_drms = NULL;
1943         }
1944     }
1945
1946     MP4_READBOX_EXIT( 1 );
1947 }
1948
1949 static int MP4_ReadBox_0xa9xxx( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1950 {
1951     int16_t i_length, i_dummy;
1952
1953     MP4_READBOX_ENTER( MP4_Box_data_0xa9xxx_t );
1954
1955     p_box->data.p_0xa9xxx->psz_text = NULL;
1956
1957     MP4_GET2BYTES( i_length );
1958     MP4_GET2BYTES( i_dummy );
1959
1960     if( i_length > 0 )
1961     {
1962         if( i_length > i_read ) i_length = i_read;
1963
1964         p_box->data.p_0xa9xxx->psz_text = malloc( i_length + 1 );
1965
1966         memcpy( p_box->data.p_0xa9xxx->psz_text,
1967                 p_peek, i_length );
1968         p_box->data.p_0xa9xxx->psz_text[i_length] = '\0';
1969
1970 #ifdef MP4_VERBOSE
1971         msg_Dbg( p_stream->s,
1972                  "read box: \"%4.4s\" text=`%s'",
1973                  (char*)&p_box->i_type,
1974                  p_box->data.p_0xa9xxx->psz_text );
1975 #endif
1976     }
1977
1978     MP4_READBOX_EXIT( 1 );
1979 }
1980 static void MP4_FreeBox_0xa9xxx( MP4_Box_t *p_box )
1981 {
1982     FREE( p_box->data.p_0xa9xxx->psz_text );
1983 }
1984
1985 /**** ------------------------------------------------------------------- ****/
1986 /****                   "Higher level" Functions                          ****/
1987 /**** ------------------------------------------------------------------- ****/
1988
1989 static struct
1990 {
1991     uint32_t i_type;
1992     int  (*MP4_ReadBox_function )( MP4_Stream_t *p_stream, MP4_Box_t *p_box );
1993     void (*MP4_FreeBox_function )( MP4_Box_t *p_box );
1994 } MP4_Box_Function [] =
1995 {
1996     /* Containers */
1997     { FOURCC_moov,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
1998     { FOURCC_trak,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
1999     { FOURCC_mdia,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2000     { FOURCC_moof,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2001     { FOURCC_minf,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2002     { FOURCC_stbl,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2003     { FOURCC_dinf,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2004     { FOURCC_edts,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2005     { FOURCC_udta,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2006     { FOURCC_nmhd,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2007     { FOURCC_hnti,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2008     { FOURCC_rmra,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2009     { FOURCC_rmda,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2010     { FOURCC_tref,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2011     { FOURCC_gmhd,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2012     { FOURCC_wave,  MP4_ReadBoxContainer,   MP4_FreeBox_Common },
2013
2014     /* specific box */
2015     { FOURCC_ftyp,  MP4_ReadBox_ftyp,       MP4_FreeBox_ftyp },
2016     { FOURCC_cmov,  MP4_ReadBox_cmov,       MP4_FreeBox_Common },
2017     { FOURCC_mvhd,  MP4_ReadBox_mvhd,       MP4_FreeBox_Common },
2018     { FOURCC_tkhd,  MP4_ReadBox_tkhd,       MP4_FreeBox_Common },
2019     { FOURCC_mdhd,  MP4_ReadBox_mdhd,       MP4_FreeBox_Common },
2020     { FOURCC_hdlr,  MP4_ReadBox_hdlr,       MP4_FreeBox_hdlr },
2021     { FOURCC_vmhd,  MP4_ReadBox_vmhd,       MP4_FreeBox_Common },
2022     { FOURCC_smhd,  MP4_ReadBox_smhd,       MP4_FreeBox_Common },
2023     { FOURCC_hmhd,  MP4_ReadBox_hmhd,       MP4_FreeBox_Common },
2024     { FOURCC_url,   MP4_ReadBox_url,        MP4_FreeBox_url },
2025     { FOURCC_urn,   MP4_ReadBox_urn,        MP4_FreeBox_urn },
2026     { FOURCC_dref,  MP4_ReadBox_dref,       MP4_FreeBox_Common },
2027     { FOURCC_stts,  MP4_ReadBox_stts,       MP4_FreeBox_stts },
2028     { FOURCC_ctts,  MP4_ReadBox_ctts,       MP4_FreeBox_ctts },
2029     { FOURCC_stsd,  MP4_ReadBox_stsd,       MP4_FreeBox_Common },
2030     { FOURCC_stsz,  MP4_ReadBox_stsz,       MP4_FreeBox_stsz },
2031     { FOURCC_stsc,  MP4_ReadBox_stsc,       MP4_FreeBox_stsc },
2032     { FOURCC_stco,  MP4_ReadBox_stco_co64,  MP4_FreeBox_stco_co64 },
2033     { FOURCC_co64,  MP4_ReadBox_stco_co64,  MP4_FreeBox_stco_co64 },
2034     { FOURCC_stss,  MP4_ReadBox_stss,       MP4_FreeBox_stss },
2035     { FOURCC_stsh,  MP4_ReadBox_stsh,       MP4_FreeBox_stsh },
2036     { FOURCC_stdp,  MP4_ReadBox_stdp,       MP4_FreeBox_stdp },
2037     { FOURCC_padb,  MP4_ReadBox_padb,       MP4_FreeBox_padb },
2038     { FOURCC_elst,  MP4_ReadBox_elst,       MP4_FreeBox_elst },
2039     { FOURCC_cprt,  MP4_ReadBox_cprt,       MP4_FreeBox_cprt },
2040     { FOURCC_esds,  MP4_ReadBox_esds,       MP4_FreeBox_esds },
2041     { FOURCC_dcom,  MP4_ReadBox_dcom,       MP4_FreeBox_Common },
2042     { FOURCC_cmvd,  MP4_ReadBox_cmvd,       MP4_FreeBox_cmvd },
2043
2044     /* Nothing to do with this box */
2045     { FOURCC_mdat,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
2046     { FOURCC_skip,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
2047     { FOURCC_free,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
2048     { FOURCC_wide,  MP4_ReadBoxSkip,        MP4_FreeBox_Common },
2049
2050     /* for codecs */
2051     { FOURCC_soun,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2052     { FOURCC_ms02,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2053     { FOURCC_ms11,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2054     { FOURCC_ms55,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2055     { FOURCC__mp3,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2056     { FOURCC_mp4a,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2057     { FOURCC_twos,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2058     { FOURCC_sowt,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2059     { FOURCC_QDMC,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2060     { FOURCC_QDM2,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2061     { FOURCC_ima4,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2062     { FOURCC_IMA4,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2063     { FOURCC_dvi,   MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2064     { FOURCC_alaw,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2065     { FOURCC_ulaw,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2066     { FOURCC_raw,   MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2067     { FOURCC_MAC3,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2068     { FOURCC_MAC6,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2069     { FOURCC_Qclp,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2070     { FOURCC_samr,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2071     { FOURCC_OggS,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2072
2073     { FOURCC_vide,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2074     { FOURCC_mp4v,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2075     { FOURCC_SVQ1,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2076     { FOURCC_SVQ3,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2077     { FOURCC_ZyGo,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2078     { FOURCC_DIVX,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2079     { FOURCC_h263,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2080     { FOURCC_s263,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2081     { FOURCC_cvid,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2082     { FOURCC_3IV1,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2083     { FOURCC_3iv1,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2084     { FOURCC_3IV2,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2085     { FOURCC_3iv2,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2086     { FOURCC_3IVD,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2087     { FOURCC_3ivd,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2088     { FOURCC_3VID,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2089     { FOURCC_3vid,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2090     { FOURCC_mjpa,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2091     { FOURCC_mjpb,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2092     { FOURCC_mjqt,  NULL,                       NULL }, /* found in mjpa/b */
2093     { FOURCC_mjht,  NULL,                       NULL },
2094     { FOURCC_dvc,   MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2095     { FOURCC_dvp,   MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2096     { FOURCC_VP31,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2097     { FOURCC_vp31,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2098     { FOURCC_h264,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2099
2100     { FOURCC_jpeg,  MP4_ReadBox_sample_vide,    MP4_FreeBox_sample_vide },
2101
2102     { FOURCC_mp4s,  NULL,                       MP4_FreeBox_Common },
2103
2104     /* XXX there is 2 box where we could find this entry stbl and tref*/
2105     { FOURCC_hint,  NULL,                       MP4_FreeBox_Common },
2106
2107     /* found in tref box */
2108     { FOURCC_dpnd,  NULL,   NULL },
2109     { FOURCC_ipir,  NULL,   NULL },
2110     { FOURCC_mpod,  NULL,   NULL },
2111
2112     /* found in hnti */
2113     { FOURCC_rtp,   NULL,   NULL },
2114
2115     /* found in rmra */
2116     { FOURCC_rdrf,  MP4_ReadBox_rdrf,           MP4_FreeBox_rdrf   },
2117     { FOURCC_rmdr,  MP4_ReadBox_rmdr,           MP4_FreeBox_Common },
2118     { FOURCC_rmqu,  MP4_ReadBox_rmqu,           MP4_FreeBox_Common },
2119     { FOURCC_rmvc,  MP4_ReadBox_rmvc,           MP4_FreeBox_Common },
2120
2121     { FOURCC_drms,  MP4_ReadBox_sample_soun,    MP4_FreeBox_sample_soun },
2122     { FOURCC_sinf,  MP4_ReadBoxContainer,       MP4_FreeBox_Common },
2123     { FOURCC_schi,  MP4_ReadBoxContainer,       MP4_FreeBox_Common },
2124     { FOURCC_user,  MP4_ReadBox_drms,           MP4_FreeBox_Common },
2125     { FOURCC_key,   MP4_ReadBox_drms,           MP4_FreeBox_Common },
2126     { FOURCC_iviv,  MP4_ReadBox_drms,           MP4_FreeBox_Common },
2127     { FOURCC_name,  MP4_ReadBox_drms,           MP4_FreeBox_Common },
2128     { FOURCC_priv,  MP4_ReadBox_drms,           MP4_FreeBox_Common },
2129
2130     /* found in udta */
2131     { FOURCC_0xa9nam,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2132     { FOURCC_0xa9aut,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2133     { FOURCC_0xa9cpy,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2134     { FOURCC_0xa9swr,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2135     { FOURCC_0xa9inf,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2136     { FOURCC_0xa9ART,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2137     { FOURCC_0xa9dir,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2138     { FOURCC_0xa9cmt,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2139     { FOURCC_0xa9req,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2140     { FOURCC_0xa9day,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2141     { FOURCC_0xa9des,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2142     { FOURCC_0xa9fmt,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2143     { FOURCC_0xa9prd,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2144     { FOURCC_0xa9prf,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2145     { FOURCC_0xa9src,MP4_ReadBox_0xa9xxx,       MP4_FreeBox_0xa9xxx },
2146
2147     /* Last entry */
2148     { 0,            NULL,                   NULL }
2149 };
2150
2151
2152
2153 /*****************************************************************************
2154  * MP4_ReadBox : parse the actual box and the children
2155  *  XXX : Do not go to the next box
2156  *****************************************************************************/
2157 static MP4_Box_t *MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_father )
2158 {
2159     MP4_Box_t    *p_box = malloc( sizeof( MP4_Box_t ) );
2160     unsigned int i_index;
2161
2162     if( !MP4_ReadBoxCommon( p_stream, p_box ) )
2163     {
2164         msg_Warn( p_stream->s, "cannot read one box" );
2165         free( p_box );
2166         return NULL;
2167     }
2168     if( !p_box->i_size )
2169     {
2170         msg_Dbg( p_stream->s, "found an empty box (null size)" );
2171         free( p_box );
2172         return NULL;
2173     }
2174     p_box->p_father = p_father;
2175
2176     /* Now search function to call */
2177     for( i_index = 0; ; i_index++ )
2178     {
2179         if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
2180             ( MP4_Box_Function[i_index].i_type == 0 ) )
2181         {
2182             break;
2183         }
2184     }
2185     if( MP4_Box_Function[i_index].MP4_ReadBox_function == NULL )
2186     {
2187         msg_Warn( p_stream->s,
2188                   "unknown box type %4.4s (uncompletetly loaded)",
2189                   (char*)&p_box->i_type );
2190     }
2191     else if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
2192     {
2193         free( p_box );
2194         return NULL;
2195     }
2196
2197     return p_box;
2198 }
2199
2200 /*****************************************************************************
2201  * MP4_FreeBox : free memory after read with MP4_ReadBox and all
2202  * the children
2203  *****************************************************************************/
2204 void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
2205 {
2206     unsigned int i_index;
2207     MP4_Box_t    *p_child;
2208
2209     if( !p_box )
2210     {
2211         return; /* hehe */
2212     }
2213
2214     for( p_child = p_box->p_first; p_child != NULL; )
2215     {
2216         MP4_Box_t *p_next;
2217
2218         p_next = p_child->p_next;
2219         MP4_BoxFree( s, p_child );
2220         p_child = p_next;
2221     }
2222
2223     /* Now search function to call */
2224     if( p_box->data.p_data )
2225     {
2226         for( i_index = 0; ; i_index++ )
2227         {
2228             if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
2229                 ( MP4_Box_Function[i_index].i_type == 0 ) )
2230             {
2231                 break;
2232             }
2233         }
2234         if( MP4_Box_Function[i_index].MP4_FreeBox_function == NULL )
2235         {
2236             /* Should not happen */
2237             msg_Warn( s,
2238                       "cannot free box %4.4s, type unknown",
2239                       (char*)&p_box->i_type );
2240         }
2241         else
2242         {
2243             MP4_Box_Function[i_index].MP4_FreeBox_function( p_box );
2244         }
2245
2246         free( p_box->data.p_data );
2247     }
2248
2249     free( p_box );
2250 }
2251
2252 /*****************************************************************************
2253  * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
2254  *****************************************************************************
2255  *  The first box is a virtual box "root" and is the father for all first
2256  *  level boxes for the file, a sort of virtual contener
2257  *****************************************************************************/
2258 MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
2259 {
2260     MP4_Box_t *p_root;
2261     MP4_Stream_t *p_stream;
2262     int i_result;
2263
2264     p_root = malloc( sizeof( MP4_Box_t ) );
2265     p_root->i_pos = 0;
2266     p_root->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
2267     p_root->i_shortsize = 1;
2268     p_root->i_size = stream_Size( s );
2269     CreateUUID( &p_root->i_uuid, p_root->i_type );
2270
2271     p_root->data.p_data = NULL;
2272     p_root->p_father = NULL;
2273     p_root->p_first  = NULL;
2274     p_root->p_last  = NULL;
2275     p_root->p_next   = NULL;
2276
2277     p_stream = MP4_InputStream( s );
2278
2279     i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
2280
2281     free( p_stream );
2282
2283     if( i_result )
2284     {
2285         MP4_Box_t *p_child;
2286         MP4_Box_t *p_moov;
2287         MP4_Box_t *p_cmov;
2288
2289         /* check if there is a cmov, if so replace
2290           compressed moov by  uncompressed one */
2291         if( ( ( p_moov = MP4_BoxGet( p_root, "moov" ) ) &&
2292               ( p_cmov = MP4_BoxGet( p_root, "moov/cmov" ) ) ) ||
2293             ( ( p_moov = MP4_BoxGet( p_root, "foov" ) ) &&
2294               ( p_cmov = MP4_BoxGet( p_root, "foov/cmov" ) ) ) )
2295         {
2296             /* rename the compressed moov as a box to skip */
2297             p_moov->i_type = FOURCC_skip;
2298
2299             /* get uncompressed p_moov */
2300             p_moov = p_cmov->data.p_cmov->p_moov;
2301             p_cmov->data.p_cmov->p_moov = NULL;
2302
2303             /* make p_root father of this new moov */
2304             p_moov->p_father = p_root;
2305
2306             /* insert this new moov box as first child of p_root */
2307             p_moov->p_next = p_child = p_root->p_first;
2308             p_root->p_first = p_moov;
2309         }
2310     }
2311
2312     return p_root;
2313 }
2314
2315
2316 static void __MP4_BoxDumpStructure( stream_t *s,
2317                                     MP4_Box_t *p_box, unsigned int i_level )
2318 {
2319     MP4_Box_t *p_child;
2320
2321     if( !i_level )
2322     {
2323         msg_Dbg( s, "dumping root Box \"%4.4s\"",
2324                           (char*)&p_box->i_type );
2325     }
2326     else
2327     {
2328         char str[512];
2329         unsigned int i;
2330         memset( str, (uint8_t)' ', 512 );
2331         for( i = 0; i < i_level; i++ )
2332         {
2333             str[i*5] = '|';
2334         }
2335         sprintf( str + i_level * 5, "+ %4.4s size %d",
2336                       (char*)&p_box->i_type,
2337                       (uint32_t)p_box->i_size );
2338
2339         msg_Dbg( s, "%s", str );
2340     }
2341     p_child = p_box->p_first;
2342     while( p_child )
2343     {
2344         __MP4_BoxDumpStructure( s, p_child, i_level + 1 );
2345         p_child = p_child->p_next;
2346     }
2347 }
2348
2349 void MP4_BoxDumpStructure( stream_t *s, MP4_Box_t *p_box )
2350 {
2351     __MP4_BoxDumpStructure( s, p_box, 0 );
2352 }
2353
2354
2355
2356 /*****************************************************************************
2357  *****************************************************************************
2358  **
2359  **  High level methods to acces an MP4 file
2360  **
2361  *****************************************************************************
2362  *****************************************************************************/
2363 static void __get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
2364 {
2365     size_t i_len ;
2366     if( !*ppsz_path[0] )
2367     {
2368         *ppsz_token = NULL;
2369         *pi_number = 0;
2370         return;
2371     }
2372     i_len = 0;
2373     while(  (*ppsz_path)[i_len] &&
2374             (*ppsz_path)[i_len] != '/' && (*ppsz_path)[i_len] != '[' )
2375     {
2376         i_len++;
2377     }
2378     if( !i_len && **ppsz_path == '/' )
2379     {
2380         i_len = 1;
2381     }
2382     *ppsz_token = malloc( i_len + 1 );
2383
2384     memcpy( *ppsz_token, *ppsz_path, i_len );
2385
2386     (*ppsz_token)[i_len] = '\0';
2387
2388     *ppsz_path += i_len;
2389
2390     if( **ppsz_path == '[' )
2391     {
2392         (*ppsz_path)++;
2393         *pi_number = strtol( *ppsz_path, NULL, 10 );
2394         while( **ppsz_path && **ppsz_path != ']' )
2395         {
2396             (*ppsz_path)++;
2397         }
2398         if( **ppsz_path == ']' )
2399         {
2400             (*ppsz_path)++;
2401         }
2402     }
2403     else
2404     {
2405         *pi_number = 0;
2406     }
2407     while( **ppsz_path == '/' )
2408     {
2409         (*ppsz_path)++;
2410     }
2411 }
2412
2413 static void __MP4_BoxGet( MP4_Box_t **pp_result,
2414                           MP4_Box_t *p_box, char *psz_fmt, va_list args)
2415 {
2416     char    *psz_path;
2417
2418     if( !p_box )
2419     {
2420         *pp_result = NULL;
2421         return;
2422     }
2423
2424     vasprintf( &psz_path, psz_fmt, args );
2425
2426     if( !psz_path || !psz_path[0] )
2427     {
2428         FREE( psz_path );
2429         *pp_result = NULL;
2430         return;
2431     }
2432
2433 //    fprintf( stderr, "path:'%s'\n", psz_path );
2434     psz_fmt = psz_path; /* keep this pointer, as it need to be unallocated */
2435     for( ; ; )
2436     {
2437         char *psz_token;
2438         int i_number;
2439
2440         __get_token( &psz_path, &psz_token, &i_number );
2441 //        fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
2442 //                 psz_path,psz_token,i_number );
2443         if( !psz_token )
2444         {
2445             FREE( psz_token );
2446             free( psz_fmt );
2447             *pp_result = p_box;
2448             return;
2449         }
2450         else
2451         if( !strcmp( psz_token, "/" ) )
2452         {
2453             /* Find root box */
2454             while( p_box && p_box->i_type != VLC_FOURCC( 'r', 'o', 'o', 't' ) )
2455             {
2456                 p_box = p_box->p_father;
2457             }
2458             if( !p_box )
2459             {
2460                 free( psz_token );
2461                 free( psz_fmt );
2462                 *pp_result = NULL;
2463                 return;
2464             }
2465         }
2466         else
2467         if( !strcmp( psz_token, "." ) )
2468         {
2469             /* Do nothing */
2470         }
2471         else
2472         if( !strcmp( psz_token, ".." ) )
2473         {
2474             p_box = p_box->p_father;
2475             if( !p_box )
2476             {
2477                 free( psz_token );
2478                 free( psz_fmt );
2479                 *pp_result = NULL;
2480                 return;
2481             }
2482         }
2483         else
2484         if( strlen( psz_token ) == 4 )
2485         {
2486             uint32_t i_fourcc;
2487             i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
2488                                    psz_token[2], psz_token[3] );
2489             p_box = p_box->p_first;
2490             for( ; ; )
2491             {
2492                 if( !p_box )
2493                 {
2494                     free( psz_token );
2495                     free( psz_fmt );
2496                     *pp_result = NULL;
2497                     return;
2498                 }
2499                 if( p_box->i_type == i_fourcc )
2500                 {
2501                     if( !i_number )
2502                     {
2503                         break;
2504                     }
2505                     i_number--;
2506                 }
2507                 p_box = p_box->p_next;
2508             }
2509         }
2510         else
2511         if( strlen( psz_token ) == 0 )
2512         {
2513             p_box = p_box->p_first;
2514             for( ; ; )
2515             {
2516                 if( !p_box )
2517                 {
2518                     free( psz_token );
2519                     free( psz_fmt );
2520                     *pp_result = NULL;
2521                     return;
2522                 }
2523                 if( !i_number )
2524                 {
2525                     break;
2526                 }
2527                 i_number--;
2528                 p_box = p_box->p_next;
2529             }
2530         }
2531         else
2532         {
2533 //            fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
2534             FREE( psz_token );
2535             free( psz_fmt );
2536             *pp_result = NULL;
2537             return;
2538         }
2539
2540         free( psz_token );
2541     }
2542 }
2543
2544 /*****************************************************************************
2545  * MP4_BoxGet: find a box given a path relative to p_box
2546  *****************************************************************************
2547  * Path Format: . .. / as usual
2548  *              [number] to specifie box number ex: trak[12]
2549  *
2550  * ex: /moov/trak[12]
2551  *     ../mdia
2552  *****************************************************************************/
2553 MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, char *psz_fmt, ... )
2554 {
2555     va_list args;
2556     MP4_Box_t *p_result;
2557
2558     va_start( args, psz_fmt );
2559     __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
2560     va_end( args );
2561
2562     return( p_result );
2563 }
2564
2565 /*****************************************************************************
2566  * MP4_BoxCount: count box given a path relative to p_box
2567  *****************************************************************************
2568  * Path Format: . .. / as usual
2569  *              [number] to specifie box number ex: trak[12]
2570  *
2571  * ex: /moov/trak[12]
2572  *     ../mdia
2573  *****************************************************************************/
2574 int MP4_BoxCount( MP4_Box_t *p_box, char *psz_fmt, ... )
2575 {
2576     va_list args;
2577     int     i_count;
2578     MP4_Box_t *p_result, *p_next;
2579
2580     va_start( args, psz_fmt );
2581     __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
2582     va_end( args );
2583     if( !p_result )
2584     {
2585         return( 0 );
2586     }
2587
2588     i_count = 1;
2589     for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
2590     {
2591         if( p_next->i_type == p_result->i_type)
2592         {
2593             i_count++;
2594         }
2595     }
2596     return( i_count );
2597 }