1 /*****************************************************************************
2 * libmp4.c : LibMP4 library for mp4 module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libmp4.c,v 1.17 2003/03/11 18:57:50 fenrir Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
22 #include <stdlib.h> /* malloc(), free() */
24 #include <string.h> /* strdup() */
26 #include <sys/types.h>
29 #include <vlc/input.h>
32 # include <zlib.h> /* for compressed moov */
37 /*****************************************************************************
38 * Here are defined some macro to make life simpler but before using it
41 * XXX: All macro are written in capital letters
43 *****************************************************************************/
44 #define MP4_BOX_HEADERSIZE( p_box ) \
45 ( 8 + ( p_box->i_shortsize == 1 ? 8 : 0 ) \
46 + ( p_box->i_type == FOURCC_uuid ? 16 : 0 ) )
48 #define MP4_BOX_DESCEND( p_box ) \
49 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) )
51 #define MP4_GET1BYTE( dst ) \
52 dst = *p_peek; p_peek++; i_read--
54 #define MP4_GET2BYTES( dst ) \
55 dst = GetWBE( p_peek ); p_peek += 2; i_read -= 2
57 #define MP4_GET3BYTES( dst ) \
58 dst = Get24bBE( p_peek ); p_peek += 3; i_read -= 3
60 #define MP4_GET4BYTES( dst ) \
61 dst = GetDWBE( p_peek ); p_peek += 4; i_read -= 4
63 #define MP4_GETFOURCC( dst ) \
64 dst = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] ); \
65 p_peek += 4; i_read -= 4
67 #define MP4_GET8BYTES( dst ) \
68 dst = GetQWBE( p_peek ); p_peek += 8; i_read -= 8
70 #define MP4_GETVERSIONFLAGS( p_void ) \
71 MP4_GET1BYTE( p_void->i_version ); \
72 MP4_GET3BYTES( p_void->i_flags )
74 #define MP4_GETSTRINGZ( p_str ) \
75 if( ( i_read > 0 )&&(p_peek[0] ) ) \
77 p_str = calloc( sizeof( char ), __MIN( strlen( p_peek ), i_read )+1);\
78 memcpy( p_str, p_peek, __MIN( strlen( p_peek ), i_read ) ); \
79 p_str[__MIN( strlen( p_peek ), i_read )] = 0; \
80 p_peek += strlen( p_str ) + 1; \
81 i_read -= strlen( p_str ) + 1; \
89 #define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t ) \
90 int64_t i_read = p_box->i_size; \
91 uint8_t *p_peek, *p_buff; \
92 i_read = p_box->i_size; \
93 if( !( p_peek = p_buff = malloc( i_read ) ) ) \
97 if( !MP4_ReadStream( p_stream, p_peek, i_read ) )\
102 p_peek += MP4_BOX_HEADERSIZE( p_box ); \
103 i_read -= MP4_BOX_HEADERSIZE( p_box ); \
104 if( !( p_box->data.p_data = malloc( sizeof( MP4_Box_data_TYPE_t ) ) ) ) \
110 #define MP4_READBOX_EXIT( i_code ) \
114 msg_Warn( p_stream->p_input, "Not enougth data" ); \
119 if( p ) {free( p ); p = NULL; }
124 * The input method HAVE to be seekable
128 /* Some functions to manipulate memory */
129 static uint16_t GetWLE( uint8_t *p_buff )
131 return( (p_buff[0]) + ( p_buff[1] <<8 ) );
134 static uint32_t GetDWLE( uint8_t *p_buff )
136 return( p_buff[0] + ( p_buff[1] <<8 ) +
137 ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
140 static uint16_t GetWBE( uint8_t *p_buff )
142 return( (p_buff[0]<<8) + p_buff[1] );
145 static uint32_t Get24bBE( uint8_t *p_buff )
147 return( ( p_buff[0] <<16 ) + ( p_buff[1] <<8 ) + p_buff[2] );
151 static uint32_t GetDWBE( uint8_t *p_buff )
153 return( (p_buff[0] << 24) + ( p_buff[1] <<16 ) +
154 ( p_buff[2] <<8 ) + p_buff[3] );
157 static uint64_t GetQWBE( uint8_t *p_buff )
159 return( ( (uint64_t)GetDWBE( p_buff ) << 32 )|( (uint64_t)GetDWBE( p_buff + 4 ) ) );
163 static void GetUUID( UUID_t *p_uuid, uint8_t *p_buff )
170 static void CreateUUID( UUID_t *p_uuid, uint32_t i_fourcc )
172 /* made by 0xXXXXXXXX-0011-0010-8000-00aa00389b71
173 where XXXXXXXX is the fourcc */
174 /* FIXME implement this */
177 /* some functions for mp4 encoding of variables */
179 void MP4_ConvertDate2Str( char *psz, uint64_t i_date )
186 i_day = i_date / ( 60*60*24);
187 i_hour = ( i_date /( 60*60 ) ) % 60;
188 i_min = ( i_date / 60 ) % 60;
190 /* FIXME do it correctly, date begin at 1 jan 1904 */
191 sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds",
192 i_day, i_hour, i_min, i_sec );
196 static void DataDump( uint8_t *p_data, int i_data )
199 fprintf( stderr, "\nDumping %d bytes\n", i_data );
200 for( i = 0; i < i_data; i++ )
205 if( c < 32 || c > 127 ) c = '.';
206 fprintf( stderr, "%c", c );
207 if( i % 60 == 59 ) fprintf( stderr, "\n" );
209 fprintf( stderr, "\n" );
213 /*****************************************************************************
214 * Some basic functions to manipulate stream more easily in vlc
216 * MP4_TellAbsolute get file position
218 * MP4_SeekAbsolute seek in the file
220 * MP4_ReadData read data from the file in a buffer
222 *****************************************************************************/
223 off_t MP4_TellAbsolute( input_thread_t *p_input )
227 vlc_mutex_lock( &p_input->stream.stream_lock );
229 i_pos= p_input->stream.p_selected_area->i_tell;
230 // ( p_input->p_last_data - p_input->p_current_data );
232 vlc_mutex_unlock( &p_input->stream.stream_lock );
237 int MP4_SeekAbsolute( input_thread_t *p_input,
242 if( i_pos >= p_input->stream.p_selected_area->i_size )
247 i_filepos = MP4_TellAbsolute( p_input );
248 if( i_pos != i_filepos )
250 input_AccessReinit( p_input );
251 p_input->pf_seek( p_input, i_pos );
256 /* return 1 if success, 0 if fail */
257 int MP4_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
259 data_packet_t *p_data;
271 i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
276 memcpy( p_buff, p_data->p_payload_start, i_read );
277 input_DeletePacket( p_input->p_method_data, p_data );
287 /*****************************************************************************
288 * Some basic functions to manipulate MP4_Stream_t, an abstraction o p_input
289 * in the way that you can read from a memory buffer or from an input
291 *****************************************************************************/
293 /**** ------- First some function to make abstract from input -------- */
295 /****************************************************************************
296 * MP4_InputStream create an stram with an input
298 ****************************************************************************/
299 MP4_Stream_t *MP4_InputStream( input_thread_t *p_input )
301 MP4_Stream_t *p_stream;
303 if( !( p_stream = malloc( sizeof( MP4_Stream_t ) ) ) )
307 p_stream->b_memory = 0;
308 p_stream->p_input = p_input;
309 p_stream->i_start = 0;
310 p_stream->i_stop = 0;
311 p_stream->p_buffer = NULL;
316 /****************************************************************************
317 * MP4_MemoryStream create a memory stream
318 * if p_buffer == NULL, will allocate a buffer of i_size, else
319 * it uses p_buffer XXX you have to unallocate it yourself !
321 ****************************************************************************/
322 MP4_Stream_t *MP4_MemoryStream( input_thread_t *p_input,
323 int i_size, uint8_t *p_buffer )
325 MP4_Stream_t *p_stream;
327 if( !( p_stream = malloc( sizeof( MP4_Stream_t ) ) ) )
331 p_stream->b_memory = 1;
332 p_stream->p_input = p_input;
333 p_stream->i_start = 0;
334 p_stream->i_stop = i_size;
337 if( !( p_stream->p_buffer = malloc( i_size ) ) )
345 p_stream->p_buffer = p_buffer;
350 /****************************************************************************
351 * MP4_ReadStream read from a MP4_Stream_t
353 ****************************************************************************/
354 int MP4_ReadStream( MP4_Stream_t *p_stream, uint8_t *p_buff, int i_size )
356 if( p_stream->b_memory )
358 if( i_size > p_stream->i_stop - p_stream->i_start )
363 p_stream->p_buffer + p_stream->i_start,
365 p_stream->i_start += i_size;
370 return( MP4_ReadData( p_stream->p_input, p_buff, i_size ) );
374 /****************************************************************************
375 * MP4_PeekStream peek from a MP4_Stream_t
377 ****************************************************************************/
378 int MP4_PeekStream( MP4_Stream_t *p_stream, uint8_t **pp_peek, int i_size )
380 if( p_stream->b_memory )
382 *pp_peek = p_stream->p_buffer + p_stream->i_start;
384 return( __MIN(i_size,p_stream->i_stop - p_stream->i_start ));
388 return( input_Peek( p_stream->p_input, pp_peek, i_size ) );
392 /****************************************************************************
393 * MP4_TellStream give absolute position in the stream
394 * XXX for a memory stream give position from begining of the buffer
395 ****************************************************************************/
396 off_t MP4_TellStream( MP4_Stream_t *p_stream )
398 if( p_stream->b_memory )
400 return( p_stream->i_start );
404 return( MP4_TellAbsolute( p_stream->p_input ) );
408 /****************************************************************************
409 * MP4_SeekStream seek in a MP4_Stream_t
411 ****************************************************************************/
412 int MP4_SeekStream( MP4_Stream_t *p_stream, off_t i_pos)
414 if( p_stream->b_memory )
416 if( i_pos < p_stream->i_stop )
418 p_stream->i_start = i_pos;
428 return( MP4_SeekAbsolute( p_stream->p_input, i_pos ) );
434 /*****************************************************************************
435 * MP4_ReadBoxCommon : Load only common parameters for all boxes
436 *****************************************************************************
437 * p_box need to be an already allocated MP4_Box_t, and all data
438 * will only be peek not read
440 * RETURN : 0 if it fail, 1 otherwise
441 *****************************************************************************/
442 int MP4_ReadBoxCommon( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
447 if( ( ( i_read = MP4_PeekStream( p_stream, &p_peek, 32 ) ) < 8 ) )
451 p_box->i_pos = MP4_TellStream( p_stream );
453 p_box->data.p_data = NULL;
454 p_box->p_father = NULL;
455 p_box->p_first = NULL;
456 p_box->p_last = NULL;
457 p_box->p_next = NULL;
459 MP4_GET4BYTES( p_box->i_shortsize );
460 MP4_GETFOURCC( p_box->i_type );
462 /* Now special case */
464 if( p_box->i_shortsize == 1 )
466 /* get the true size on 64 bits */
467 MP4_GET8BYTES( p_box->i_size );
471 p_box->i_size = p_box->i_shortsize;
472 /* XXX size of 0 means that the box extends to end of file */
475 if( p_box->i_type == FOURCC_uuid )
477 /* get extented type on 16 bytes */
478 GetUUID( &p_box->i_uuid, p_peek );
479 p_peek += 16; i_read -= 16;
483 CreateUUID( &p_box->i_uuid, p_box->i_type );
486 /* FIXME how to write uint64_t ??? */
489 msg_Dbg( p_stream->p_input, "Found Box: %4.4s size %d",
490 (char*)&p_box->i_type,
491 (uint32_t)p_box->i_size );
499 /*****************************************************************************
500 * MP4_MP4_NextBox : Go to the next box
501 *****************************************************************************
502 * if p_box == NULL, go to the next box in witch we are( at the begining ).
503 *****************************************************************************/
504 int MP4_NextBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
510 MP4_ReadBoxCommon( p_stream, &box );
516 return( 2 ); /* Box with infinite size */
519 if( p_box->p_father )
521 /* check if it's within p-father */
522 if( p_box->i_size + p_box->i_pos >=
523 p_box->p_father->i_size + p_box->p_father->i_pos )
525 return( 0 ); /* out of bound */
528 return( MP4_SeekStream( p_stream, p_box->i_size + p_box->i_pos ) );
530 /*****************************************************************************
531 * MP4_MP4_GotoBox : Go to this particular box
532 *****************************************************************************
533 * RETURN : 0 if it fail, 1 otherwise
534 *****************************************************************************/
535 int MP4_GotoBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
537 return( MP4_SeekStream( p_stream, p_box->i_pos ) );
541 /*****************************************************************************
542 * For all known box a loader is given,
543 * XXX: all common struct have to be already read by MP4_ReadBoxCommon
544 * after called one of theses functions, file position is unknown
545 * you need to call MP4_GotoBox to go where you want
546 *****************************************************************************/
547 int MP4_ReadBoxContainerRaw( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
551 if( MP4_TellStream( p_stream ) + 8 >
552 (off_t)(p_container->i_pos + p_container->i_size) )
554 /* there is no box to load */
560 p_box = malloc( sizeof( MP4_Box_t ) );
562 if( MP4_ReadBox( p_stream, p_box , p_container ) )
564 /* chain this box with the father and the other at same level */
565 if( !p_container->p_first )
567 p_container->p_first = p_box;
571 p_container->p_last->p_next = p_box;
573 p_container->p_last = p_box;
582 }while( MP4_NextBox( p_stream, p_box ) == 1 );
588 int MP4_ReadBoxContainer( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
591 if( p_container->i_size <= (size_t)MP4_BOX_HEADERSIZE(p_container ) + 8 )
593 /* container is empty, 8 stand for the first header in this box */
598 MP4_BOX_DESCEND( p_container );
600 return( MP4_ReadBoxContainerRaw( p_stream, p_container ) );
603 void MP4_FreeBox_Common( input_thread_t *p_input, MP4_Box_t *p_box )
605 /* Up to now do nothing */
608 int MP4_ReadBoxSkip( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
612 msg_Dbg( p_stream->p_input, "Skip box: \"%4.4s\"",
613 (char*)&p_box->i_type );
618 int MP4_ReadBox_ftyp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
620 MP4_READBOX_ENTER( MP4_Box_data_ftyp_t );
622 MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
623 MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
625 if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
628 p_box->data.p_ftyp->i_compatible_brands =
629 calloc( p_box->data.p_ftyp->i_compatible_brands_count, sizeof(uint32_t));
631 for( i =0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
633 MP4_GETFOURCC( p_box->data.p_ftyp->i_compatible_brands[i] );
638 p_box->data.p_ftyp->i_compatible_brands = NULL;
641 MP4_READBOX_EXIT( 1 );
644 void MP4_FreeBox_ftyp( input_thread_t *p_input, MP4_Box_t *p_box )
646 FREE( p_box->data.p_ftyp->i_compatible_brands );
650 int MP4_ReadBox_mvhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
654 char s_creation_time[128];
655 char s_modification_time[128];
656 char s_duration[128];
658 MP4_READBOX_ENTER( MP4_Box_data_mvhd_t );
660 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
662 if( p_box->data.p_mvhd->i_version )
664 MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
665 MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
666 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
667 MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
671 MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
672 MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
673 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
674 MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
676 MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
677 MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
678 MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
681 for( i = 0; i < 2; i++ )
683 MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
685 for( i = 0; i < 9; i++ )
687 MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
689 for( i = 0; i < 6; i++ )
691 MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
694 MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
698 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
699 MP4_ConvertDate2Str( s_modification_time,
700 p_box->data.p_mvhd->i_modification_time );
701 if( p_box->data.p_mvhd->i_rate )
703 MP4_ConvertDate2Str( s_duration,
704 p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_rate );
710 msg_Dbg( p_stream->p_input, "Read Box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
713 (uint32_t)p_box->data.p_mvhd->i_timescale,
715 (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
716 (float)p_box->data.p_mvhd->i_volume / 256 ,
717 (uint32_t)p_box->data.p_mvhd->i_next_track_id );
719 MP4_READBOX_EXIT( 1 );
722 int MP4_ReadBox_tkhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
726 char s_creation_time[128];
727 char s_modification_time[128];
728 char s_duration[128];
730 MP4_READBOX_ENTER( MP4_Box_data_tkhd_t );
732 MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
734 if( p_box->data.p_tkhd->i_version )
736 MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
737 MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
738 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
739 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
740 MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
744 MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
745 MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
746 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
747 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
748 MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
751 for( i = 0; i < 2; i++ )
753 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
755 MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
756 MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
757 MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
758 MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
760 for( i = 0; i < 9; i++ )
762 MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
764 MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
765 MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
768 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
769 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time );
770 MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration );
772 msg_Dbg( p_stream->p_input, "Read Box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f width %f height %f",
776 p_box->data.p_tkhd->i_track_ID,
777 p_box->data.p_tkhd->i_layer,
778 (float)p_box->data.p_tkhd->i_volume / 256 ,
779 (float)p_box->data.p_tkhd->i_width / 65536,
780 (float)p_box->data.p_tkhd->i_height / 65536 );
782 MP4_READBOX_EXIT( 1 );
785 int MP4_ReadBox_tref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
788 if( p_box->i_size < (size_t)MP4_BOX_HEADERSIZE(p_box ) + 8 )
790 /* container is empty, 8 stand for the first header in this box */
794 if( !MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) ) )
799 MP4_ReadBoxContainerRaw( p_stream, p_box );
802 msg_Dbg( p_stream->p_input, "Read Box: \"tref\" " );
810 int MP4_ReadBox_mdhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
815 char s_creation_time[128];
816 char s_modification_time[128];
817 char s_duration[128];
819 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t );
821 MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
823 if( p_box->data.p_mdhd->i_version )
825 MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
826 MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
827 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
828 MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
832 MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
833 MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
834 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
835 MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
837 i_language = GetWBE( p_peek );
838 for( i = 0; i < 3; i++ )
840 p_box->data.p_mdhd->i_language[i] =
841 ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
844 MP4_GET2BYTES( p_box->data.p_mdhd->i_predefined );
847 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time );
848 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time );
849 MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration );
850 msg_Dbg( p_stream->p_input, "Read Box: \"mdhd\" creation %s modification %s time scale %d duration %s language %c%c%c",
853 (uint32_t)p_box->data.p_mdhd->i_timescale,
855 p_box->data.p_mdhd->i_language[0],
856 p_box->data.p_mdhd->i_language[1],
857 p_box->data.p_mdhd->i_language[2] );
859 MP4_READBOX_EXIT( 1 );
863 int MP4_ReadBox_hdlr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
865 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
867 MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
869 MP4_GET4BYTES( p_box->data.p_hdlr->i_predefined );
870 MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
872 p_box->data.p_hdlr->psz_name = calloc( sizeof( char ), i_read + 1 );
873 memcpy( p_box->data.p_hdlr->psz_name, p_peek, i_read );
876 msg_Dbg( p_stream->p_input, "Read Box: \"hdlr\" hanler type %4.4s name %s",
877 (char*)&p_box->data.p_hdlr->i_handler_type,
878 p_box->data.p_hdlr->psz_name );
881 MP4_READBOX_EXIT( 1 );
884 void MP4_FreeBox_hdlr( input_thread_t *p_input, MP4_Box_t *p_box )
886 FREE( p_box->data.p_hdlr->psz_name );
889 int MP4_ReadBox_vmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
893 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t );
895 MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
897 MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
898 for( i = 0; i < 3; i++ )
900 MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
904 msg_Dbg( p_stream->p_input, "Read Box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
905 p_box->data.p_vmhd->i_graphics_mode,
906 p_box->data.p_vmhd->i_opcolor[0],
907 p_box->data.p_vmhd->i_opcolor[1],
908 p_box->data.p_vmhd->i_opcolor[2] );
910 MP4_READBOX_EXIT( 1 );
913 int MP4_ReadBox_smhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
915 MP4_READBOX_ENTER( MP4_Box_data_smhd_t );
917 MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
921 MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
923 MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
926 msg_Dbg( p_stream->p_input, "Read Box: \"smhd\" balance %f",
927 (float)p_box->data.p_smhd->i_balance / 256 );
929 MP4_READBOX_EXIT( 1 );
933 int MP4_ReadBox_hmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
935 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t );
937 MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
939 MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
940 MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
942 MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
943 MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
945 MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
948 msg_Dbg( p_stream->p_input, "Read Box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
949 p_box->data.p_hmhd->i_max_PDU_size,
950 p_box->data.p_hmhd->i_avg_PDU_size,
951 p_box->data.p_hmhd->i_max_bitrate,
952 p_box->data.p_hmhd->i_avg_bitrate );
954 MP4_READBOX_EXIT( 1 );
957 int MP4_ReadBox_url( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
959 MP4_READBOX_ENTER( MP4_Box_data_url_t );
961 MP4_GETVERSIONFLAGS( p_box->data.p_url );
962 MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
965 msg_Dbg( p_stream->p_input, "Read Box: \"url\" url: %s",
966 p_box->data.p_url->psz_location );
969 MP4_READBOX_EXIT( 1 );
973 void MP4_FreeBox_url( input_thread_t *p_input, MP4_Box_t *p_box )
975 FREE( p_box->data.p_url->psz_location )
978 int MP4_ReadBox_urn( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
980 MP4_READBOX_ENTER( MP4_Box_data_urn_t );
982 MP4_GETVERSIONFLAGS( p_box->data.p_urn );
984 MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
985 MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
988 msg_Dbg( p_stream->p_input, "Read Box: \"urn\" name %s location %s",
989 p_box->data.p_urn->psz_name,
990 p_box->data.p_urn->psz_location );
992 MP4_READBOX_EXIT( 1 );
994 void MP4_FreeBox_urn( input_thread_t *p_input, MP4_Box_t *p_box )
996 FREE( p_box->data.p_urn->psz_name );
997 FREE( p_box->data.p_urn->psz_location );
1001 int MP4_ReadBox_dref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1003 MP4_READBOX_ENTER( MP4_Box_data_dref_t );
1005 MP4_GETVERSIONFLAGS( p_box->data.p_dref );
1007 MP4_GET4BYTES( p_box->data.p_dref->i_entry_count );
1009 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 );
1010 MP4_ReadBoxContainerRaw( p_stream, p_box );
1013 msg_Dbg( p_stream->p_input, "Read Box: \"dref\" entry-count %d",
1014 p_box->data.p_dref->i_entry_count );
1017 MP4_READBOX_EXIT( 1 );
1021 int MP4_ReadBox_stts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1024 MP4_READBOX_ENTER( MP4_Box_data_stts_t );
1026 MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1027 MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );
1029 p_box->data.p_stts->i_sample_count =
1030 calloc( sizeof( uint32_t ), p_box->data.p_stts->i_entry_count );
1031 p_box->data.p_stts->i_sample_delta =
1032 calloc( sizeof( uint32_t ), p_box->data.p_stts->i_entry_count );
1034 for( i = 0; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
1036 MP4_GET4BYTES( p_box->data.p_stts->i_sample_count[i] );
1037 MP4_GET4BYTES( p_box->data.p_stts->i_sample_delta[i] );
1041 msg_Dbg( p_stream->p_input, "Read Box: \"stts\" entry-count %d",
1042 p_box->data.p_stts->i_entry_count );
1045 MP4_READBOX_EXIT( 1 );
1048 void MP4_FreeBox_stts( input_thread_t *p_input, MP4_Box_t *p_box )
1050 FREE( p_box->data.p_stts->i_sample_count );
1051 FREE( p_box->data.p_stts->i_sample_delta );
1054 int MP4_ReadBox_ctts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1057 MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
1059 MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1061 MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );
1063 p_box->data.p_ctts->i_sample_count =
1064 calloc( sizeof( uint32_t ), p_box->data.p_ctts->i_entry_count );
1065 p_box->data.p_ctts->i_sample_offset =
1066 calloc( sizeof( uint32_t ), p_box->data.p_ctts->i_entry_count );
1068 for( i = 0; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
1070 MP4_GET4BYTES( p_box->data.p_ctts->i_sample_count[i] );
1071 MP4_GET4BYTES( p_box->data.p_ctts->i_sample_offset[i] );
1075 msg_Dbg( p_stream->p_input, "Read Box: \"ctts\" entry-count %d",
1076 p_box->data.p_ctts->i_entry_count );
1079 MP4_READBOX_EXIT( 1 );
1082 void MP4_FreeBox_ctts( input_thread_t *p_input, MP4_Box_t *p_box )
1084 FREE( p_box->data.p_ctts->i_sample_count );
1085 FREE( p_box->data.p_ctts->i_sample_offset );
1088 static int MP4_ReadLengthDescriptor( uint8_t **pp_peek, int64_t *i_read )
1091 unsigned int i_len = 0;
1098 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1099 } while( i_b&0x80 );
1103 int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1105 #define es_descriptor p_box->data.p_esds->es_descriptor
1107 unsigned int i_flags;
1108 unsigned int i_type;
1110 MP4_READBOX_ENTER( MP4_Box_data_esds_t );
1112 MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1115 MP4_GET1BYTE( i_type );
1116 if( i_type == 0x03 ) /* MP4ESDescrTag */
1118 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1120 MP4_GET2BYTES( es_descriptor.i_ES_ID );
1121 MP4_GET1BYTE( i_flags );
1122 es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1123 es_descriptor.b_url = ( (i_flags&0x40) != 0);
1124 es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1126 es_descriptor.i_stream_priority = i_flags&0x1f;
1127 if( es_descriptor.b_stream_dependence )
1129 MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1131 if( es_descriptor.b_url )
1135 MP4_GET1BYTE( i_len );
1136 es_descriptor.psz_URL = calloc( sizeof(char), i_len + 1 );
1137 memcpy( es_descriptor.psz_URL, p_peek, i_len );
1138 es_descriptor.psz_URL[i_len] = 0;
1144 es_descriptor.psz_URL = NULL;
1146 if( es_descriptor.b_OCRstream )
1148 MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1150 MP4_GET1BYTE( i_type ); /* get next type */
1153 if( i_type != 0x04)/* MP4DecConfigDescrTag */
1155 es_descriptor.p_decConfigDescr = NULL;
1156 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1159 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1160 es_descriptor.p_decConfigDescr =
1161 malloc( sizeof( MP4_descriptor_decoder_config_t ));
1163 MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectTypeIndication );
1164 MP4_GET1BYTE( i_flags );
1165 es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1166 es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1167 MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1168 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1169 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1170 MP4_GET1BYTE( i_type );
1171 if( i_type != 0x05 )/* MP4DecSpecificDescrTag */
1173 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1174 es_descriptor.p_decConfigDescr->p_decoder_specific_info = NULL;
1175 MP4_READBOX_EXIT( 1 );
1178 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1179 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1180 es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1181 memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1184 MP4_READBOX_EXIT( 1 );
1186 #undef es_descriptor
1189 void MP4_FreeBox_esds( input_thread_t *p_input, MP4_Box_t *p_box )
1191 FREE( p_box->data.p_esds->es_descriptor.psz_URL );
1192 if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1194 FREE( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1196 FREE( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1199 int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1203 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
1205 for( i = 0; i < 6 ; i++ )
1207 MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
1210 MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
1212 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
1213 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
1214 MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
1217 for( i = 0; i < 2 ; i++ )
1219 MP4_GET4BYTES( p_box->data.p_sample_soun->i_reserved2[i] );
1223 MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
1224 MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
1225 MP4_GET2BYTES( p_box->data.p_sample_soun->i_predefined );
1226 MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
1227 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
1228 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
1230 if( p_box->data.p_sample_soun->i_qt_version == 1 &&
1234 MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
1235 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
1236 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
1237 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
1240 msg_Dbg( p_stream->p_input, "" );
1241 msg_Dbg( p_stream->p_input,
1242 "Read Box: \"soun\" qt3+ sample/packet=%d bytes/packet=%d bytes/frame=%d bytes/sample=%d",
1243 p_box->data.p_sample_soun->i_sample_per_packet, p_box->data.p_sample_soun->i_bytes_per_packet,
1244 p_box->data.p_sample_soun->i_bytes_per_frame, p_box->data.p_sample_soun->i_bytes_per_sample );
1245 msg_Dbg( p_stream->p_input, "" );
1247 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 44 );
1251 p_box->data.p_sample_soun->i_sample_per_packet = 0;
1252 p_box->data.p_sample_soun->i_bytes_per_packet = 0;
1253 p_box->data.p_sample_soun->i_bytes_per_frame = 0;
1254 p_box->data.p_sample_soun->i_bytes_per_sample = 0;
1256 msg_Dbg( p_stream->p_input, "" );
1257 msg_Dbg( p_stream->p_input, "Read Box: \"soun\" mp4 or qt1/2 (rest=%d)", i_read );
1258 msg_Dbg( p_stream->p_input, "" );
1259 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 28 );
1261 MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds */
1264 msg_Dbg( p_stream->p_input, "Read Box: \"soun\" in stsd channel %d sample size %d sampl rate %f",
1265 p_box->data.p_sample_soun->i_channelcount,
1266 p_box->data.p_sample_soun->i_samplesize,
1267 (float)p_box->data.p_sample_soun->i_sampleratehi +
1268 (float)p_box->data.p_sample_soun->i_sampleratelo / 65536 );
1271 MP4_READBOX_EXIT( 1 );
1275 int MP4_ReadBox_sample_mp4a( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1279 MP4_READBOX_ENTER( MP4_Box_data_sample_mp4a_t );
1281 for( i = 0; i < 6 ; i++ )
1283 MP4_GET1BYTE( p_box->data.p_sample_mp4a->i_reserved1[i] );
1286 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_data_reference_index );
1288 for( i = 0; i < 2 ; i++ )
1290 MP4_GET4BYTES( p_box->data.p_sample_mp4a->i_reserved2[i] );
1293 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_channelcount );
1294 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_samplesize );
1295 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_predefined );
1296 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_reserved3 );
1297 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_sampleratehi );
1298 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_sampleratelo );
1300 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 28 );
1301 MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds */
1304 msg_Dbg( p_stream->p_input, "Read Box: \"mp4a\" in stsd channel %d sample size %d sampl rate %f",
1305 p_box->data.p_sample_mp4a->i_channelcount,
1306 p_box->data.p_sample_mp4a->i_samplesize,
1307 (float)p_box->data.p_sample_mp4a->i_sampleratehi +
1308 (float)p_box->data.p_sample_mp4a->i_sampleratelo / 65536 );
1311 MP4_READBOX_EXIT( 1 );
1315 int MP4_ReadBox_sample_vide( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1319 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );
1321 for( i = 0; i < 6 ; i++ )
1323 MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
1326 MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
1328 MP4_GET2BYTES( p_box->data.p_sample_vide->i_predefined1 );
1329 MP4_GET2BYTES( p_box->data.p_sample_vide->i_reserved2 );
1331 for( i = 0; i < 3 ; i++ )
1333 MP4_GET4BYTES( p_box->data.p_sample_vide->i_predefined2[i] );
1336 MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
1337 MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
1339 MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
1340 MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
1342 MP4_GET4BYTES( p_box->data.p_sample_vide->i_reserved3 );
1343 MP4_GET2BYTES( p_box->data.p_sample_vide->i_predefined3 );
1345 memcpy( &p_box->data.p_sample_vide->i_compressorname, p_peek, 32 );
1346 p_peek += 32; i_read -= 32;
1348 MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
1349 MP4_GET2BYTES( p_box->data.p_sample_vide->i_predefined4 );
1351 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 78);
1352 MP4_ReadBoxContainerRaw( p_stream, p_box );
1355 msg_Dbg( p_stream->p_input, "Read Box: \"vide\" in stsd %dx%d depth %d",
1356 p_box->data.p_sample_vide->i_width,
1357 p_box->data.p_sample_vide->i_height,
1358 p_box->data.p_sample_vide->i_depth );
1361 MP4_READBOX_EXIT( 1 );
1365 int MP4_ReadBox_stsd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1368 MP4_READBOX_ENTER( MP4_Box_data_stsd_t );
1370 MP4_GETVERSIONFLAGS( p_box->data.p_stsd );
1372 MP4_GET4BYTES( p_box->data.p_stsd->i_entry_count );
1374 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 );
1376 MP4_ReadBoxContainerRaw( p_stream, p_box );
1379 msg_Dbg( p_stream->p_input, "Read Box: \"stsd\" entry-count %d",
1380 p_box->data.p_stsd->i_entry_count );
1383 MP4_READBOX_EXIT( 1 );
1387 int MP4_ReadBox_stsz( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1391 MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
1393 MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
1395 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
1397 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
1399 p_box->data.p_stsz->i_entry_size =
1400 calloc( sizeof( uint32_t ), p_box->data.p_stsz->i_sample_count );
1402 if( !p_box->data.p_stsz->i_sample_size )
1404 for( i=0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
1406 MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
1411 msg_Dbg( p_stream->p_input, "Read Box: \"stsz\" sample-size %d sample-count %d",
1412 p_box->data.p_stsz->i_sample_size,
1413 p_box->data.p_stsz->i_sample_count );
1416 MP4_READBOX_EXIT( 1 );
1419 void MP4_FreeBox_stsz( input_thread_t *p_input, MP4_Box_t *p_box )
1421 FREE( p_box->data.p_stsz->i_entry_size );
1424 int MP4_ReadBox_stsc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1428 MP4_READBOX_ENTER( MP4_Box_data_stsc_t );
1430 MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
1432 MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
1434 p_box->data.p_stsc->i_first_chunk =
1435 calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1436 p_box->data.p_stsc->i_samples_per_chunk =
1437 calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1438 p_box->data.p_stsc->i_sample_description_index =
1439 calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1441 for( i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
1443 MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
1444 MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
1445 MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
1449 msg_Dbg( p_stream->p_input, "Read Box: \"stsc\" entry-count %d",
1450 p_box->data.p_stsc->i_entry_count );
1453 MP4_READBOX_EXIT( 1 );
1456 void MP4_FreeBox_stsc( input_thread_t *p_input, MP4_Box_t *p_box )
1458 FREE( p_box->data.p_stsc->i_first_chunk );
1459 FREE( p_box->data.p_stsc->i_samples_per_chunk );
1460 FREE( p_box->data.p_stsc->i_sample_description_index );
1463 int MP4_ReadBox_stco_co64( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1467 MP4_READBOX_ENTER( MP4_Box_data_co64_t );
1469 MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
1471 MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
1473 p_box->data.p_co64->i_chunk_offset =
1474 calloc( sizeof( uint64_t ), p_box->data.p_co64->i_entry_count );
1476 for( i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
1478 if( p_box->i_type == FOURCC_stco )
1484 MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
1492 MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
1497 msg_Dbg( p_stream->p_input, "Read Box: \"co64\" entry-count %d",
1498 p_box->data.p_co64->i_entry_count );
1501 MP4_READBOX_EXIT( 1 );
1504 void MP4_FreeBox_stco_co64( input_thread_t *p_input, MP4_Box_t *p_box )
1506 FREE( p_box->data.p_co64->i_chunk_offset );
1509 int MP4_ReadBox_stss( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1513 MP4_READBOX_ENTER( MP4_Box_data_stss_t );
1515 MP4_GETVERSIONFLAGS( p_box->data.p_stss );
1517 MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
1519 p_box->data.p_stss->i_sample_number =
1520 calloc( sizeof( uint32_t ), p_box->data.p_stss->i_entry_count );
1522 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
1525 MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
1526 /* XXX in libmp4 sample begin at 0 */
1527 p_box->data.p_stss->i_sample_number[i]--;
1531 msg_Dbg( p_stream->p_input, "Read Box: \"stss\" entry-count %d",
1532 p_box->data.p_stss->i_entry_count );
1535 MP4_READBOX_EXIT( 1 );
1538 void MP4_FreeBox_stss( input_thread_t *p_input, MP4_Box_t *p_box )
1540 FREE( p_box->data.p_stss->i_sample_number )
1543 int MP4_ReadBox_stsh( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1547 MP4_READBOX_ENTER( MP4_Box_data_stsh_t );
1549 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
1552 MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
1554 p_box->data.p_stsh->i_shadowed_sample_number =
1555 calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
1557 p_box->data.p_stsh->i_sync_sample_number =
1558 calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
1561 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
1564 MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
1565 MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
1569 msg_Dbg( p_stream->p_input, "Read Box: \"stsh\" entry-count %d",
1570 p_box->data.p_stsh->i_entry_count );
1572 MP4_READBOX_EXIT( 1 );
1575 void MP4_FreeBox_stsh( input_thread_t *p_input, MP4_Box_t *p_box )
1577 FREE( p_box->data.p_stsh->i_shadowed_sample_number )
1578 FREE( p_box->data.p_stsh->i_sync_sample_number )
1582 int MP4_ReadBox_stdp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1586 MP4_READBOX_ENTER( MP4_Box_data_stdp_t );
1588 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
1590 p_box->data.p_stdp->i_priority =
1591 calloc( sizeof( uint16_t ), i_read / 2 );
1593 for( i = 0; i < i_read / 2 ; i++ )
1596 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
1600 msg_Dbg( p_stream->p_input, "Read Box: \"stdp\" entry-count "I64Fd,
1604 MP4_READBOX_EXIT( 1 );
1607 void MP4_FreeBox_stdp( input_thread_t *p_input, MP4_Box_t *p_box )
1609 FREE( p_box->data.p_stdp->i_priority )
1612 int MP4_ReadBox_padb( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1616 MP4_READBOX_ENTER( MP4_Box_data_padb_t );
1618 MP4_GETVERSIONFLAGS( p_box->data.p_padb );
1621 MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
1623 p_box->data.p_padb->i_reserved1 =
1624 calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1625 p_box->data.p_padb->i_pad2 =
1626 calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1627 p_box->data.p_padb->i_reserved2 =
1628 calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1629 p_box->data.p_padb->i_pad1 =
1630 calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1633 for( i = 0; i < i_read / 2 ; i++ )
1635 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
1636 p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
1637 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
1638 p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;
1640 p_peek += 1; i_read -= 1;
1644 msg_Dbg( p_stream->p_input, "Read Box: \"stdp\" entry-count "I64Fd,
1648 MP4_READBOX_EXIT( 1 );
1651 void MP4_FreeBox_padb( input_thread_t *p_input, MP4_Box_t *p_box )
1653 FREE( p_box->data.p_padb->i_reserved1 );
1654 FREE( p_box->data.p_padb->i_pad2 );
1655 FREE( p_box->data.p_padb->i_reserved2 );
1656 FREE( p_box->data.p_padb->i_pad1 );
1659 int MP4_ReadBox_elst( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1663 MP4_READBOX_ENTER( MP4_Box_data_padb_t );
1665 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
1668 MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
1670 p_box->data.p_elst->i_segment_duration =
1671 calloc( sizeof( uint64_t ), p_box->data.p_elst->i_entry_count );
1672 p_box->data.p_elst->i_media_time =
1673 calloc( sizeof( uint64_t ), p_box->data.p_elst->i_entry_count );
1674 p_box->data.p_elst->i_media_rate_integer =
1675 calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
1676 p_box->data.p_elst->i_media_rate_fraction=
1677 calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
1680 for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
1682 if( p_box->data.p_elst->i_version == 1 )
1685 MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
1687 MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
1692 MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
1694 MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
1697 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
1698 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
1702 msg_Dbg( p_stream->p_input, "Read Box: \"elst\" entry-count "I64Fd,
1706 MP4_READBOX_EXIT( 1 );
1709 void MP4_FreeBox_elst( input_thread_t *p_input, MP4_Box_t *p_box )
1711 FREE( p_box->data.p_elst->i_segment_duration );
1712 FREE( p_box->data.p_elst->i_media_time );
1713 FREE( p_box->data.p_elst->i_media_rate_integer );
1714 FREE( p_box->data.p_elst->i_media_rate_fraction );
1717 int MP4_ReadBox_cprt( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1719 unsigned int i_language;
1722 MP4_READBOX_ENTER( MP4_Box_data_cprt_t );
1724 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
1726 i_language = GetWBE( p_peek );
1727 for( i = 0; i < 3; i++ )
1729 p_box->data.p_cprt->i_language[i] =
1730 ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
1732 p_peek += 2; i_read -= 2;
1733 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
1736 msg_Dbg( p_stream->p_input, "Read Box: \"cprt\" language %c%c%c notice %s",
1737 p_box->data.p_cprt->i_language[0],
1738 p_box->data.p_cprt->i_language[1],
1739 p_box->data.p_cprt->i_language[2],
1740 p_box->data.p_cprt->psz_notice );
1743 MP4_READBOX_EXIT( 1 );
1746 void MP4_FreeBox_cprt( input_thread_t *p_input, MP4_Box_t *p_box )
1748 FREE( p_box->data.p_cprt->psz_notice );
1752 int MP4_ReadBox_dcom( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1754 MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
1756 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
1758 msg_Dbg( p_stream->p_input,
1759 "Read Box: \"dcom\" compression algorithm : %4.4s",
1760 (char*)&p_box->data.p_dcom->i_algorithm );
1762 MP4_READBOX_EXIT( 1 );
1765 int MP4_ReadBox_cmvd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1767 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );
1769 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
1771 p_box->data.p_cmvd->i_compressed_size = i_read;
1773 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
1775 msg_Dbg( p_stream->p_input, "Read Box: \"cmvd\" not enough memory to load data" );
1779 /* now copy compressed data */
1780 memcpy( p_box->data.p_cmvd->p_data,
1784 p_box->data.p_cmvd->b_compressed = 1;
1787 msg_Dbg( p_stream->p_input, "Read Box: \"cmvd\" compressed data size %d",
1788 p_box->data.p_cmvd->i_compressed_size );
1791 MP4_READBOX_EXIT( 1 );
1793 void MP4_FreeBox_cmvd( input_thread_t *p_input, MP4_Box_t *p_box )
1795 FREE( p_box->data.p_cmvd->p_data );
1799 int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1801 MP4_Stream_t *p_stream_memory;
1813 if( !( p_box->data.p_cmov = malloc( sizeof( MP4_Box_data_cmov_t ) ) ) )
1815 msg_Err( p_stream->p_input, "out of memory" );
1818 memset( p_box->data.p_cmov, 0, sizeof( MP4_Box_data_cmov_t ) );
1820 if( !( p_box->p_father )||
1821 ( p_box->p_father->i_type != FOURCC_moov ) )
1823 msg_Warn( p_stream->p_input, "Read box: \"cmov\" box alone" );
1827 if( !(i_result = MP4_ReadBoxContainer( p_stream, p_box ) ) )
1832 if( !( p_dcom = MP4_FindBox( p_box, FOURCC_dcom ) )||
1833 !( p_cmvd = MP4_FindBox( p_box, FOURCC_cmvd ) )||
1834 !( p_cmvd->data.p_cmvd->p_data ) )
1836 msg_Warn( p_stream->p_input, "Read Box: \"cmov\" incomplete" );
1840 if( p_dcom->data.p_dcom->i_algorithm != FOURCC_zlib )
1842 msg_Dbg( p_stream->p_input, "Read Box: \"cmov\" compression algorithm : %4.4s not supported",
1843 (char*)&p_dcom->data.p_dcom->i_algorithm );
1848 msg_Dbg( p_stream->p_input,
1849 "Read Box: \"cmov\" zlib unsupported" );
1852 /* decompress data */
1853 /* allocate a new buffer */
1854 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
1856 msg_Err( p_stream->p_input,
1857 "Read Box: \"cmov\" not enough memory to uncompress data" );
1860 /* init default structures */
1861 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
1862 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
1863 z_data.next_out = p_data;
1864 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
1865 z_data.zalloc = (alloc_func)Z_NULL;
1866 z_data.zfree = (free_func)Z_NULL;
1867 z_data.opaque = (voidpf)Z_NULL;
1870 if( ( i_result = inflateInit( &z_data ) ) != Z_OK )
1872 msg_Err( p_stream->p_input,
1873 "Read Box: \"cmov\" error while uncompressing data" );
1879 i_result = inflate( &z_data, Z_NO_FLUSH );
1880 if( ( i_result != Z_OK )&&( i_result != Z_STREAM_END ) )
1882 msg_Err( p_stream->p_input,
1883 "Read Box: \"cmov\" error while uncompressing data" );
1888 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
1890 msg_Warn( p_stream->p_input,
1891 "Read Box: \"cmov\" uncompressing data size mismatch" );
1893 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
1896 i_result = inflateEnd( &z_data );
1897 if( i_result != Z_OK )
1899 msg_Warn( p_stream->p_input,
1900 "Read Box: \"cmov\" error while uncompressing data (ignored)" );
1904 free( p_cmvd->data.p_cmvd->p_data );
1905 p_cmvd->data.p_cmvd->p_data = p_data;
1906 p_cmvd->data.p_cmvd->b_compressed = 0;
1908 msg_Dbg( p_stream->p_input,
1909 "Read Box: \"cmov\" box succesfully uncompressed" );
1911 //DataDump( p_data, p_cmvd->data.p_cmvd->i_uncompressed_size );
1912 /* now create a memory stream */
1913 p_stream_memory = MP4_MemoryStream( p_stream->p_input,
1914 p_cmvd->data.p_cmvd->i_uncompressed_size,
1915 p_cmvd->data.p_cmvd->p_data );
1917 //DataDump( p_stream_memory->p_buffer, p_stream_memory->i_stop );
1919 /* and read uncompressd moov */
1920 p_umov = malloc( sizeof( MP4_Box_t ) );
1922 i_result = MP4_ReadBox( p_stream_memory, p_umov, NULL );
1924 p_box->data.p_cmov->p_moov = p_umov;
1925 free( p_stream_memory );
1928 msg_Dbg( p_stream->p_input,
1929 "Read Box: \"cmov\" compressed movie header completed" );
1932 #endif /* HAVE_ZLIB_H */
1935 int MP4_ReadBox_rdrf( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1938 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t );
1940 MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
1941 MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
1942 MP4_GET4BYTES( i_len );
1946 p_box->data.p_rdrf->psz_ref = malloc( i_len );
1947 for( i = 0; i < i_len; i++ )
1949 MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
1951 p_box->data.p_rdrf->psz_ref[i_len] = '\0';
1955 p_box->data.p_rdrf->psz_ref = NULL;
1959 msg_Dbg( p_stream->p_input,
1960 "Read Box: \"rdrf\" type:%4.4s ref %s",
1961 (char*)&p_box->data.p_rdrf->i_ref_type,
1962 p_box->data.p_rdrf->psz_ref );
1965 MP4_READBOX_EXIT( 1 );
1967 void MP4_FreeBox_rdrf( input_thread_t *p_input, MP4_Box_t *p_box )
1969 FREE( p_box->data.p_rdrf->psz_ref )
1973 int MP4_ReadBox_rmdr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1975 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t );
1977 MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
1979 MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
1982 msg_Dbg( p_stream->p_input,
1983 "Read Box: \"rmdr\" rate:%d",
1984 p_box->data.p_rmdr->i_rate );
1986 MP4_READBOX_EXIT( 1 );
1989 int MP4_ReadBox_rmqu( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1991 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t );
1993 MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
1996 msg_Dbg( p_stream->p_input,
1997 "Read Box: \"rmqu\" quality:%d",
1998 p_box->data.p_rmqu->i_quality );
2000 MP4_READBOX_EXIT( 1 );
2003 int MP4_ReadBox_rmvc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
2005 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t );
2006 MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
2008 MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
2009 MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
2010 MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
2011 MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
2014 msg_Dbg( p_stream->p_input,
2015 "Read Box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
2016 (char*)&p_box->data.p_rmvc->i_gestaltType,
2017 p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
2018 p_box->data.p_rmvc->i_checkType );
2021 MP4_READBOX_EXIT( 1 );
2024 /**** ------------------------------------------------------------------- ****/
2025 /**** "Higher level" Functions ****/
2026 /**** ------------------------------------------------------------------- ****/
2031 int (*MP4_ReadBox_function )( MP4_Stream_t *p_stream, MP4_Box_t *p_box );
2032 void (*MP4_FreeBox_function )( input_thread_t *p_input, MP4_Box_t *p_box );
2033 } MP4_Box_Function [] =
2036 { FOURCC_moov, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2037 { FOURCC_trak, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2038 { FOURCC_mdia, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2039 { FOURCC_moof, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2040 { FOURCC_minf, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2041 { FOURCC_stbl, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2042 { FOURCC_dinf, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2043 { FOURCC_edts, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2044 { FOURCC_udta, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2045 { FOURCC_nmhd, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2046 { FOURCC_hnti, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2047 { FOURCC_rmra, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2048 { FOURCC_rmda, MP4_ReadBoxContainer, MP4_FreeBox_Common },
2051 { FOURCC_ftyp, MP4_ReadBox_ftyp, MP4_FreeBox_ftyp },
2052 { FOURCC_cmov, MP4_ReadBox_cmov, MP4_FreeBox_Common },
2053 { FOURCC_mvhd, MP4_ReadBox_mvhd, MP4_FreeBox_Common },
2054 { FOURCC_tkhd, MP4_ReadBox_tkhd, MP4_FreeBox_Common },
2055 { FOURCC_tref, MP4_ReadBox_tref, MP4_FreeBox_Common },
2056 { FOURCC_mdhd, MP4_ReadBox_mdhd, MP4_FreeBox_Common },
2057 { FOURCC_hdlr, MP4_ReadBox_hdlr, MP4_FreeBox_hdlr },
2058 { FOURCC_vmhd, MP4_ReadBox_vmhd, MP4_FreeBox_Common },
2059 { FOURCC_smhd, MP4_ReadBox_smhd, MP4_FreeBox_Common },
2060 { FOURCC_hmhd, MP4_ReadBox_hmhd, MP4_FreeBox_Common },
2061 { FOURCC_url, MP4_ReadBox_url, MP4_FreeBox_url },
2062 { FOURCC_urn, MP4_ReadBox_urn, MP4_FreeBox_urn },
2063 { FOURCC_dref, MP4_ReadBox_dref, MP4_FreeBox_Common },
2064 { FOURCC_stts, MP4_ReadBox_stts, MP4_FreeBox_stts },
2065 { FOURCC_ctts, MP4_ReadBox_ctts, MP4_FreeBox_ctts },
2066 { FOURCC_stsd, MP4_ReadBox_stsd, MP4_FreeBox_Common },
2067 { FOURCC_stsz, MP4_ReadBox_stsz, MP4_FreeBox_stsz },
2068 { FOURCC_stsc, MP4_ReadBox_stsc, MP4_FreeBox_stsc },
2069 { FOURCC_stco, MP4_ReadBox_stco_co64, MP4_FreeBox_stco_co64 },
2070 { FOURCC_co64, MP4_ReadBox_stco_co64, MP4_FreeBox_stco_co64 },
2071 { FOURCC_stss, MP4_ReadBox_stss, MP4_FreeBox_stss },
2072 { FOURCC_stsh, MP4_ReadBox_stsh, MP4_FreeBox_stsh },
2073 { FOURCC_stdp, MP4_ReadBox_stdp, MP4_FreeBox_stdp },
2074 { FOURCC_padb, MP4_ReadBox_padb, MP4_FreeBox_padb },
2075 { FOURCC_elst, MP4_ReadBox_elst, MP4_FreeBox_elst },
2076 { FOURCC_cprt, MP4_ReadBox_cprt, MP4_FreeBox_cprt },
2077 { FOURCC_esds, MP4_ReadBox_esds, MP4_FreeBox_esds },
2078 { FOURCC_dcom, MP4_ReadBox_dcom, MP4_FreeBox_Common },
2079 { FOURCC_cmvd, MP4_ReadBox_cmvd, MP4_FreeBox_cmvd },
2081 /* Nothing to do with this box */
2082 { FOURCC_mdat, MP4_ReadBoxSkip, MP4_FreeBox_Common },
2083 { FOURCC_skip, MP4_ReadBoxSkip, MP4_FreeBox_Common },
2084 { FOURCC_free, MP4_ReadBoxSkip, MP4_FreeBox_Common },
2085 { FOURCC_wide, MP4_ReadBoxSkip, MP4_FreeBox_Common },
2088 { FOURCC_soun, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2089 { FOURCC_ms02, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2090 { FOURCC_ms11, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2091 { FOURCC_ms55, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2092 { FOURCC__mp3, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2093 { FOURCC_mp4a, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2094 { FOURCC_twos, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2095 { FOURCC_sowt, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2096 { FOURCC_QDMC, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2097 { FOURCC_QDM2, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2098 { FOURCC_ima4, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2099 { FOURCC_IMA4, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2100 { FOURCC_dvi, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2101 { FOURCC_alaw, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2102 { FOURCC_ulaw, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2103 { FOURCC_raw, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2104 { FOURCC_MAC3, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2105 { FOURCC_MAC6, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
2107 { FOURCC_vide, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2108 { FOURCC_mp4v, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2109 { FOURCC_SVQ1, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2110 { FOURCC_SVQ3, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2111 { FOURCC_DIVX, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2112 { FOURCC_h263, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2113 { FOURCC_cvid, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2114 { FOURCC_3IV1, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2115 { FOURCC_3IV2, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2116 { FOURCC_mjpa, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2117 { FOURCC_mjpb, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2118 { FOURCC_mjqt, NULL, NULL }, /* found in mjpa/b */
2119 { FOURCC_mjht, NULL, NULL },
2120 { FOURCC_dvc, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2121 { FOURCC_dvp, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2123 { FOURCC_jpeg, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
2125 { FOURCC_mp4s, NULL, MP4_FreeBox_Common },
2127 /* XXX there is 2 box where we could find this entry stbl and tref*/
2128 { FOURCC_hint, NULL, MP4_FreeBox_Common },
2130 /* found in tref box */
2131 { FOURCC_dpnd, NULL, NULL },
2132 { FOURCC_ipir, NULL, NULL },
2133 { FOURCC_mpod, NULL, NULL },
2136 { FOURCC_rtp, NULL, NULL },
2139 { FOURCC_rdrf, MP4_ReadBox_rdrf, MP4_FreeBox_rdrf },
2140 { FOURCC_rmdr, MP4_ReadBox_rmdr, MP4_FreeBox_Common },
2141 { FOURCC_rmqu, MP4_ReadBox_rmqu, MP4_FreeBox_Common },
2142 { FOURCC_rmvc, MP4_ReadBox_rmvc, MP4_FreeBox_Common },
2150 /*****************************************************************************
2151 * MP4_ReadBox : parse the actual box and the children
2152 * XXX : Do not go to the next box
2153 *****************************************************************************/
2154 int MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
2157 unsigned int i_index;
2159 if( !MP4_ReadBoxCommon( p_stream, p_box ) )
2161 msg_Warn( p_stream->p_input, "Cannot read one box" );
2164 if( !p_box->i_size )
2166 msg_Dbg( p_stream->p_input, "Found an empty box (null size)" );
2169 p_box->p_father = p_father;
2171 /* Now search function to call */
2172 for( i_index = 0; ; i_index++ )
2174 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
2175 ( MP4_Box_Function[i_index].i_type == 0 ) )
2180 if( MP4_Box_Function[i_index].MP4_ReadBox_function == NULL )
2182 msg_Warn( p_stream->p_input,
2183 "Unknown box type %4.4s (uncompletetly loaded)",
2184 (char*)&p_box->i_type );
2190 (MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box );
2193 if( !MP4_GotoBox( p_stream, p_box ) )
2200 /*****************************************************************************
2201 * MP4_CountBox: given a box, count how many child have the requested type
2202 * FIXME : support GUUID
2203 *****************************************************************************/
2204 int MP4_CountBox( MP4_Box_t *p_box, uint32_t i_type )
2206 unsigned int i_count;
2215 p_child = p_box->p_first;
2218 if( p_child->i_type == i_type )
2222 p_child = p_child->p_next;
2229 /*****************************************************************************
2230 * MP4_FindBox: find first box with i_type child of p_box
2231 * return NULL if not found
2232 *****************************************************************************/
2234 MP4_Box_t *MP4_FindBox( MP4_Box_t *p_box, uint32_t i_type )
2243 p_child = p_box->p_first;
2246 if( p_child->i_type == i_type )
2250 p_child = p_child->p_next;
2258 /*****************************************************************************
2259 * MP4_FindNextBox: find next box with thesame type and at the same level
2261 *****************************************************************************/
2262 MP4_Box_t *MP4_FindNextBox( MP4_Box_t *p_box )
2271 p_next = p_box->p_next;
2274 if( p_next->i_type == p_box->i_type )
2278 p_next = p_next->p_next;
2282 /*****************************************************************************
2283 * MP4_FindNbBox: find the box i_number
2284 *****************************************************************************/
2285 MP4_Box_t *MP4_FindNbBox( MP4_Box_t *p_box, uint32_t i_number )
2287 MP4_Box_t *p_child = p_box->p_first;
2296 if( !( p_child = p_child->p_next ) )
2306 /*****************************************************************************
2307 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
2309 *****************************************************************************/
2310 void MP4_BoxFree( input_thread_t *p_input, MP4_Box_t *p_box )
2312 unsigned int i_index;
2321 p_child = p_box->p_first;
2324 p_next = p_child->p_next;
2325 MP4_BoxFree( p_input, p_child );
2326 /* MP4_FreeBoxChildren have free all data expect p_child itself */
2331 /* Now search function to call */
2332 if( p_box->data.p_data )
2334 for( i_index = 0; ; i_index++ )
2336 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
2337 ( MP4_Box_Function[i_index].i_type == 0 ) )
2342 if( MP4_Box_Function[i_index].MP4_FreeBox_function == NULL )
2344 /* Should not happen */
2346 "cannot free box %4.4s, type unknown",
2347 (char*)&p_box->i_type );
2351 MP4_Box_Function[i_index].MP4_FreeBox_function( p_input, p_box );
2354 free( p_box->data.p_data );
2355 p_box->data.p_data = NULL;
2358 p_box->p_first = NULL;
2359 p_box->p_last = NULL;
2363 /*****************************************************************************
2364 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
2365 *****************************************************************************
2366 * The first box is a virtual box "root" and is the father for all first
2367 * level boxes for the file, a sort of virtual contener
2368 *****************************************************************************/
2369 int MP4_BoxGetRoot( input_thread_t *p_input, MP4_Box_t *p_root )
2371 MP4_Stream_t *p_stream;
2374 MP4_SeekAbsolute( p_input, 0 ); /* Go to the begining */
2376 p_root->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
2377 p_root->i_shortsize = 1;
2378 p_root->i_size = p_input->stream.p_selected_area->i_size;
2379 CreateUUID( &p_root->i_uuid, p_root->i_type );
2381 p_root->data.p_data = NULL;
2382 p_root->p_father = NULL;
2383 p_root->p_first = NULL;
2384 p_root->p_last = NULL;
2385 p_root->p_next = NULL;
2387 p_stream = MP4_InputStream( p_input );
2389 i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
2399 /* check if there is a cmov, if so replace
2400 compressed moov by uncompressed one */
2401 if( ( p_moov = MP4_FindBox( p_root, FOURCC_moov ) )&&
2402 ( p_cmov = MP4_FindBox( p_moov, FOURCC_cmov ) ) )
2404 /* rename the compressed moov as a box to skip */
2405 p_moov->i_type = FOURCC_skip;
2407 /* get uncompressed p_moov */
2408 p_moov = p_cmov->data.p_cmov->p_moov;
2409 p_cmov->data.p_cmov->p_moov = NULL;
2411 /* make p_root father of this new moov */
2412 p_moov->p_father = p_root;
2414 /* insert this new moov box as first child of p_root */
2415 p_moov->p_next = p_child = p_root->p_first;
2416 p_root->p_first = p_moov;
2423 static void __MP4_BoxDumpStructure( input_thread_t *p_input,
2424 MP4_Box_t *p_box, unsigned int i_level )
2430 msg_Dbg( p_input, "Dumping root Box \"%4.4s\"",
2431 (char*)&p_box->i_type );
2437 memset( str, (uint8_t)' ', 512 );
2438 for( i = 0; i < i_level; i++ )
2442 sprintf( str + i_level * 5, "+ %4.4s size %d",
2443 (char*)&p_box->i_type,
2444 (uint32_t)p_box->i_size );
2446 msg_Dbg( p_input, "%s", str );
2448 p_child = p_box->p_first;
2451 __MP4_BoxDumpStructure( p_input, p_child, i_level + 1 );
2452 p_child = p_child->p_next;
2456 void MP4_BoxDumpStructure( input_thread_t *p_input, MP4_Box_t *p_box )
2458 __MP4_BoxDumpStructure( p_input, p_box, 0 );
2463 /*****************************************************************************
2464 *****************************************************************************
2466 ** High level methods to acces an MP4 file
2468 *****************************************************************************
2469 *****************************************************************************/
2470 static void __get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
2473 if( !*ppsz_path[0] )
2480 while( (*ppsz_path)[i_len] &&
2481 (*ppsz_path)[i_len] != '/' && (*ppsz_path)[i_len] != '[' )
2485 if( !i_len && **ppsz_path == '/' )
2489 *ppsz_token = malloc( i_len + 1 );
2491 memcpy( *ppsz_token, *ppsz_path, i_len );
2493 (*ppsz_token)[i_len] = '\0';
2495 *ppsz_path += i_len;
2497 if( **ppsz_path == '[' )
2500 *pi_number = strtol( *ppsz_path, NULL, 10 );
2501 while( **ppsz_path && **ppsz_path != ']' )
2505 if( **ppsz_path == ']' )
2514 while( **ppsz_path == '/' )
2520 static void __MP4_BoxGet( MP4_Box_t **pp_result,
2521 MP4_Box_t *p_box, char *psz_fmt, va_list args)
2524 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
2534 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
2535 vasprintf( &psz_path, psz_fmt, args );
2537 i_size = strlen( psz_fmt ) + 1024;
2538 psz_path = calloc( i_size, sizeof( char ) );
2539 vsnprintf( psz_path, i_size, psz_fmt, args );
2540 psz_path[i_size - 1] = 0;
2543 if( !psz_path || !psz_path[0] )
2550 // fprintf( stderr, "path:'%s'\n", psz_path );
2551 psz_fmt = psz_path; /* keep this pointer, as it need to be unallocated */
2557 __get_token( &psz_path, &psz_token, &i_number );
2558 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
2559 // psz_path,psz_token,i_number );
2568 if( !strcmp( psz_token, "/" ) )
2571 while( p_box && p_box->i_type != VLC_FOURCC( 'r', 'o', 'o', 't' ) )
2573 p_box = p_box->p_father;
2584 if( !strcmp( psz_token, "." ) )
2589 if( !strcmp( psz_token, ".." ) )
2591 p_box = p_box->p_father;
2601 if( strlen( psz_token ) == 4 )
2604 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
2605 psz_token[2], psz_token[3] );
2606 p_box = p_box->p_first;
2616 if( p_box->i_type == i_fourcc )
2624 p_box = p_box->p_next;
2628 if( strlen( psz_token ) == 0 )
2630 p_box = p_box->p_first;
2645 p_box = p_box->p_next;
2650 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
2661 /*****************************************************************************
2662 * MP4_BoxGet: find a box given a path relative to p_box
2663 *****************************************************************************
2664 * Path Format: . .. / as usual
2665 * [number] to specifie box number ex: trak[12]
2667 * ex: /moov/trak[12]
2669 *****************************************************************************/
2670 MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, char *psz_fmt, ... )
2673 MP4_Box_t *p_result;
2675 va_start( args, psz_fmt );
2676 __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
2682 /*****************************************************************************
2683 * MP4_BoxCount: count box given a path relative to p_box
2684 *****************************************************************************
2685 * Path Format: . .. / as usual
2686 * [number] to specifie box number ex: trak[12]
2688 * ex: /moov/trak[12]
2690 *****************************************************************************/
2691 int MP4_BoxCount( MP4_Box_t *p_box, char *psz_fmt, ... )
2695 MP4_Box_t *p_result, *p_next;
2697 va_start( args, psz_fmt );
2698 __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
2706 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
2708 if( p_next->i_type == p_result->i_type)