1 /*****************************************************************************
2 * libmp4.c : LibMP4 library for mp4 module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libmp4.c,v 1.9 2002/11/17 06:46:56 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 );
197 /*****************************************************************************
198 * Some basic functions to manipulate stream more easily in vlc
200 * MP4_TellAbsolute get file position
202 * MP4_SeekAbsolute seek in the file
204 * MP4_ReadData read data from the file in a buffer
206 *****************************************************************************/
207 off_t MP4_TellAbsolute( input_thread_t *p_input )
211 vlc_mutex_lock( &p_input->stream.stream_lock );
213 i_pos= p_input->stream.p_selected_area->i_tell;
214 // ( p_input->p_last_data - p_input->p_current_data );
216 vlc_mutex_unlock( &p_input->stream.stream_lock );
221 int MP4_SeekAbsolute( input_thread_t *p_input,
226 if( i_pos >= p_input->stream.p_selected_area->i_size )
231 i_filepos = MP4_TellAbsolute( p_input );
232 if( i_pos != i_filepos )
234 p_input->pf_seek( p_input, i_pos );
235 input_AccessReinit( p_input );
240 /* return 1 if success, 0 if fail */
241 int MP4_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
243 data_packet_t *p_data;
255 i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
260 memcpy( p_buff, p_data->p_payload_start, i_read );
261 input_DeletePacket( p_input->p_method_data, p_data );
271 /*****************************************************************************
272 * Some basic functions to manipulate MP4_Stream_t, an abstraction o p_input
273 * in the way that you can read from a memory buffer or from an input
275 *****************************************************************************/
277 /**** ------- First some function to make abstract from input -------- */
279 /****************************************************************************
280 * MP4_InputStream create an stram with an input
282 ****************************************************************************/
283 MP4_Stream_t *MP4_InputStream( input_thread_t *p_input )
285 MP4_Stream_t *p_stream;
287 if( !( p_stream = malloc( sizeof( MP4_Stream_t ) ) ) )
291 p_stream->b_memory = 0;
292 p_stream->p_input = p_input;
293 p_stream->i_start = 0;
294 p_stream->i_stop = 0;
295 p_stream->p_buffer = NULL;
300 /****************************************************************************
301 * MP4_MemoryStream create a memory stream
302 * if p_buffer == NULL, will allocate a buffer of i_size, else
303 * it uses p_buffer XXX you have to unallocate it yourself !
305 ****************************************************************************/
306 MP4_Stream_t *MP4_MemoryStream( input_thread_t *p_input,
307 int i_size, uint8_t *p_buffer )
309 MP4_Stream_t *p_stream;
311 if( !( p_stream = malloc( sizeof( MP4_Stream_t ) ) ) )
315 p_stream->b_memory = 1;
316 p_stream->p_input = p_input;
317 p_stream->i_start = 0;
318 p_stream->i_stop = i_size;
319 if( !p_stream->p_buffer )
321 if( !( p_stream->p_buffer = malloc( i_size ) ) )
329 p_stream->p_buffer = p_buffer;
334 /****************************************************************************
335 * MP4_ReadStream read from a MP4_Stream_t
337 ****************************************************************************/
338 int MP4_ReadStream( MP4_Stream_t *p_stream, uint8_t *p_buff, int i_size )
340 if( p_stream->b_memory )
342 if( i_size > p_stream->i_stop - p_stream->i_start )
347 p_stream->p_buffer + p_stream->i_start,
349 p_stream->i_start += i_size;
354 return( MP4_ReadData( p_stream->p_input, p_buff, i_size ) );
358 /****************************************************************************
359 * MP4_PeekStream peek from a MP4_Stream_t
361 ****************************************************************************/
362 int MP4_PeekStream( MP4_Stream_t *p_stream, uint8_t **pp_peek, int i_size )
364 if( p_stream->b_memory )
366 *pp_peek = p_stream->p_buffer + p_stream->i_start;
368 return( __MIN(i_size,p_stream->i_stop - p_stream->i_start ));
372 return( input_Peek( p_stream->p_input, pp_peek, i_size ) );
376 /****************************************************************************
377 * MP4_TellStream give absolute position in the stream
378 * XXX for a memory stream give position from begining of the buffer
379 ****************************************************************************/
380 off_t MP4_TellStream( MP4_Stream_t *p_stream )
382 if( p_stream->b_memory )
384 return( p_stream->i_start );
388 return( MP4_TellAbsolute( p_stream->p_input ) );
392 /****************************************************************************
393 * MP4_SeekStream seek in a MP4_Stream_t
395 ****************************************************************************/
396 int MP4_SeekStream( MP4_Stream_t *p_stream, off_t i_pos)
398 if( p_stream->b_memory )
400 if( i_pos < p_stream->i_stop )
402 p_stream->i_start = i_pos;
412 return( MP4_SeekAbsolute( p_stream->p_input, i_pos ) );
418 /*****************************************************************************
419 * MP4_ReadBoxCommon : Load only common parameters for all boxes
420 *****************************************************************************
421 * p_box need to be an already allocated MP4_Box_t, and all data
422 * will only be peek not read
424 * RETURN : 0 if it fail, 1 otherwise
425 *****************************************************************************/
426 int MP4_ReadBoxCommon( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
431 if( ( ( i_read = MP4_PeekStream( p_stream, &p_peek, 32 ) ) < 8 ) )
435 p_box->i_pos = MP4_TellStream( p_stream );
437 p_box->data.p_data = NULL;
438 p_box->p_father = NULL;
439 p_box->p_first = NULL;
440 p_box->p_last = NULL;
441 p_box->p_next = NULL;
443 MP4_GET4BYTES( p_box->i_shortsize );
444 MP4_GETFOURCC( p_box->i_type );
446 /* Now special case */
448 if( p_box->i_shortsize == 1 )
450 /* get the true size on 64 bits */
451 MP4_GET8BYTES( p_box->i_size );
455 p_box->i_size = p_box->i_shortsize;
456 /* XXX size of 0 means that the box extends to end of file */
459 if( p_box->i_type == FOURCC_uuid )
461 /* get extented type on 16 bytes */
462 GetUUID( &p_box->i_uuid, p_peek );
463 p_peek += 16; i_read -= 16;
467 CreateUUID( &p_box->i_uuid, p_box->i_type );
470 /* FIXME how to write uint64_t ??? */
473 msg_Dbg( p_stream->p_input, "Found Box: %c%c%c%c size %d",
474 (p_box->i_type)&0xff, (p_box->i_type>>8)&0xff,
475 (p_box->i_type>>16)&0xff, (p_box->i_type>>24)&0xff,
476 (uint32_t)p_box->i_size );
484 /*****************************************************************************
485 * MP4_MP4_NextBox : Go to the next box
486 *****************************************************************************
487 * if p_box == NULL, go to the next box in witch we are( at the begining ).
488 *****************************************************************************/
489 int MP4_NextBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
495 MP4_ReadBoxCommon( p_stream, &box );
501 return( 2 ); /* Box with infinite size */
504 if( p_box->p_father )
506 /* check if it's within p-father */
507 if( p_box->i_size + p_box->i_pos >=
508 p_box->p_father->i_size + p_box->p_father->i_pos )
510 return( 0 ); /* out of bound */
513 return( MP4_SeekStream( p_stream, p_box->i_size + p_box->i_pos ) );
515 /*****************************************************************************
516 * MP4_MP4_GotoBox : Go to this particular box
517 *****************************************************************************
518 * RETURN : 0 if it fail, 1 otherwise
519 *****************************************************************************/
520 int MP4_GotoBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
522 return( MP4_SeekStream( p_stream, p_box->i_pos ) );
526 /*****************************************************************************
527 * For all known box a loader is given,
528 * XXX: all common struct have to be already read by MP4_ReadBoxCommon
529 * after called one of theses functions, file position is unknown
530 * you need to call MP4_GotoBox to go where you want
531 *****************************************************************************/
532 int MP4_ReadBoxContainerRaw( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
536 if( MP4_TellStream( p_stream ) + 8 >
537 p_container->i_pos + p_container->i_size )
539 /* there is no box to load */
545 p_box = malloc( sizeof( MP4_Box_t ) );
547 if( MP4_ReadBox( p_stream, p_box , p_container ) )
549 /* chain this box with the father and the other at same level */
550 if( !p_container->p_first )
552 p_container->p_first = p_box;
556 p_container->p_last->p_next = p_box;
558 p_container->p_last = p_box;
567 }while( MP4_NextBox( p_stream, p_box ) == 1 );
573 int MP4_ReadBoxContainer( MP4_Stream_t *p_stream, MP4_Box_t *p_container )
576 if( p_container->i_size <= MP4_BOX_HEADERSIZE(p_container ) + 8 )
578 /* container is empty, 8 stand for the first header in this box */
583 MP4_BOX_DESCEND( p_container );
585 return( MP4_ReadBoxContainerRaw( p_stream, p_container ) );
588 void MP4_FreeBox_Common( input_thread_t *p_input, MP4_Box_t *p_box )
590 /* Up to now do nothing */
593 int MP4_ReadBoxSkip( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
597 msg_Dbg( p_stream->p_input, "Skip box: \"%c%c%c%c\"",
598 (p_box->i_type)&0xff,
599 (p_box->i_type>>8)&0xff,
600 (p_box->i_type>>16)&0xff,
601 (p_box->i_type>>24)&0xff );
606 int MP4_ReadBox_ftyp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
608 MP4_READBOX_ENTER( MP4_Box_data_ftyp_t );
610 MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
611 MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
613 if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
616 p_box->data.p_ftyp->i_compatible_brands =
617 calloc( p_box->data.p_ftyp->i_compatible_brands_count, sizeof(uint32_t));
619 for( i =0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
621 MP4_GETFOURCC( p_box->data.p_ftyp->i_compatible_brands[i] );
626 p_box->data.p_ftyp->i_compatible_brands = NULL;
629 MP4_READBOX_EXIT( 1 );
632 void MP4_FreeBox_ftyp( input_thread_t *p_input, MP4_Box_t *p_box )
634 FREE( p_box->data.p_ftyp->i_compatible_brands );
638 int MP4_ReadBox_mvhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
642 char s_creation_time[128];
643 char s_modification_time[128];
644 char s_duration[128];
646 MP4_READBOX_ENTER( MP4_Box_data_mvhd_t );
648 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
650 if( p_box->data.p_mvhd->i_version )
652 MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
653 MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
654 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
655 MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
659 MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
660 MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
661 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
662 MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
664 MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
665 MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
666 MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
669 for( i = 0; i < 2; i++ )
671 MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
673 for( i = 0; i < 9; i++ )
675 MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
677 for( i = 0; i < 6; i++ )
679 MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
682 MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
686 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
687 MP4_ConvertDate2Str( s_modification_time,
688 p_box->data.p_mvhd->i_modification_time );
689 if( p_box->data.p_mvhd->i_rate )
691 MP4_ConvertDate2Str( s_duration,
692 p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_rate );
698 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",
701 (uint32_t)p_box->data.p_mvhd->i_timescale,
703 (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
704 (float)p_box->data.p_mvhd->i_volume / 256 ,
705 (uint32_t)p_box->data.p_mvhd->i_next_track_id );
707 MP4_READBOX_EXIT( 1 );
710 int MP4_ReadBox_tkhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
714 char s_creation_time[128];
715 char s_modification_time[128];
716 char s_duration[128];
718 MP4_READBOX_ENTER( MP4_Box_data_tkhd_t );
720 MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
722 if( p_box->data.p_tkhd->i_version )
724 MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
725 MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
726 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
727 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
728 MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
732 MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
733 MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
734 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
735 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
736 MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
739 for( i = 0; i < 2; i++ )
741 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
743 MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
744 MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
745 MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
746 MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
748 for( i = 0; i < 9; i++ )
750 MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
752 MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
753 MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
756 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
757 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time );
758 MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration );
760 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",
764 p_box->data.p_tkhd->i_track_ID,
765 p_box->data.p_tkhd->i_layer,
766 (float)p_box->data.p_tkhd->i_volume / 256 ,
767 (float)p_box->data.p_tkhd->i_width / 65536,
768 (float)p_box->data.p_tkhd->i_height / 65536 );
770 MP4_READBOX_EXIT( 1 );
773 int MP4_ReadBox_tref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
776 if( p_box->i_size < MP4_BOX_HEADERSIZE(p_box ) + 8 )
778 /* container is empty, 8 stand for the first header in this box */
782 if( !MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) ) )
787 MP4_ReadBoxContainerRaw( p_stream, p_box );
790 msg_Dbg( p_stream->p_input, "Read Box: \"tref\" " );
798 int MP4_ReadBox_mdhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
803 char s_creation_time[128];
804 char s_modification_time[128];
805 char s_duration[128];
807 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t );
809 MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
811 if( p_box->data.p_mdhd->i_version )
813 MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
814 MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
815 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
816 MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
820 MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
821 MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
822 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
823 MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
825 i_language = GetWBE( p_peek );
826 for( i = 0; i < 3; i++ )
828 p_box->data.p_mdhd->i_language[i] =
829 ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
832 MP4_GET2BYTES( p_box->data.p_mdhd->i_predefined );
835 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time );
836 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time );
837 MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration );
838 msg_Dbg( p_stream->p_input, "Read Box: \"mdhd\" creation %s modification %s time scale %d duration %s language %c%c%c",
841 (uint32_t)p_box->data.p_mdhd->i_timescale,
843 p_box->data.p_mdhd->i_language[0],
844 p_box->data.p_mdhd->i_language[1],
845 p_box->data.p_mdhd->i_language[2] );
847 MP4_READBOX_EXIT( 1 );
851 int MP4_ReadBox_hdlr( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
853 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
855 MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
857 MP4_GET4BYTES( p_box->data.p_hdlr->i_predefined );
858 MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
860 p_box->data.p_hdlr->psz_name = calloc( sizeof( char ), i_read + 1 );
861 memcpy( p_box->data.p_hdlr->psz_name, p_peek, i_read );
864 msg_Dbg( p_stream->p_input, "Read Box: \"hdlr\" hanler type %c%c%c%c name %s",
865 ( p_box->data.p_hdlr->i_handler_type )&0xff,
866 ( p_box->data.p_hdlr->i_handler_type >> 8 )&0xff,
867 ( p_box->data.p_hdlr->i_handler_type >> 16 )&0xff,
868 ( p_box->data.p_hdlr->i_handler_type >> 24 )&0xff,
869 p_box->data.p_hdlr->psz_name );
872 MP4_READBOX_EXIT( 1 );
875 void MP4_FreeBox_hdlr( input_thread_t *p_input, MP4_Box_t *p_box )
877 FREE( p_box->data.p_hdlr->psz_name );
880 int MP4_ReadBox_vmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
884 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t );
886 MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
888 MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
889 for( i = 0; i < 3; i++ )
891 MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
895 msg_Dbg( p_stream->p_input, "Read Box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
896 p_box->data.p_vmhd->i_graphics_mode,
897 p_box->data.p_vmhd->i_opcolor[0],
898 p_box->data.p_vmhd->i_opcolor[1],
899 p_box->data.p_vmhd->i_opcolor[2] );
902 MP4_READBOX_EXIT( 1 );
905 int MP4_ReadBox_smhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
907 MP4_READBOX_ENTER( MP4_Box_data_smhd_t );
909 MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
913 MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
915 MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
918 msg_Dbg( p_stream->p_input, "Read Box: \"smhd\" balance %f",
919 (float)p_box->data.p_smhd->i_balance / 256 );
922 MP4_READBOX_EXIT( 1 );
926 int MP4_ReadBox_hmhd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
928 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t );
930 MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
932 MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
933 MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
935 MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
936 MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
938 MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
941 msg_Dbg( p_stream->p_input, "Read Box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
942 p_box->data.p_hmhd->i_max_PDU_size,
943 p_box->data.p_hmhd->i_avg_PDU_size,
944 p_box->data.p_hmhd->i_max_bitrate,
945 p_box->data.p_hmhd->i_avg_bitrate );
948 MP4_READBOX_EXIT( 1 );
951 int MP4_ReadBox_url( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
953 MP4_READBOX_ENTER( MP4_Box_data_url_t );
955 MP4_GETVERSIONFLAGS( p_box->data.p_url );
956 MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
959 msg_Dbg( p_stream->p_input, "Read Box: \"url\" url: %s",
960 p_box->data.p_url->psz_location );
963 MP4_READBOX_EXIT( 1 );
967 void MP4_FreeBox_url( input_thread_t *p_input, MP4_Box_t *p_box )
969 FREE( p_box->data.p_url->psz_location )
972 int MP4_ReadBox_urn( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
974 MP4_READBOX_ENTER( MP4_Box_data_urn_t );
976 MP4_GETVERSIONFLAGS( p_box->data.p_urn );
978 MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
979 MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
982 msg_Dbg( p_stream->p_input, "Read Box: \"urn\" name %s location %s",
983 p_box->data.p_urn->psz_name,
984 p_box->data.p_urn->psz_location );
986 MP4_READBOX_EXIT( 1 );
988 void MP4_FreeBox_urn( input_thread_t *p_input, MP4_Box_t *p_box )
990 FREE( p_box->data.p_urn->psz_name );
991 FREE( p_box->data.p_urn->psz_location );
995 int MP4_ReadBox_dref( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
997 MP4_READBOX_ENTER( MP4_Box_data_dref_t );
999 MP4_GETVERSIONFLAGS( p_box->data.p_dref );
1001 MP4_GET4BYTES( p_box->data.p_dref->i_entry_count );
1003 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 );
1004 MP4_ReadBoxContainerRaw( p_stream, p_box );
1007 msg_Dbg( p_stream->p_input, "Read Box: \"dref\" entry-count %d",
1008 p_box->data.p_dref->i_entry_count );
1011 MP4_READBOX_EXIT( 1 );
1015 int MP4_ReadBox_stts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1018 MP4_READBOX_ENTER( MP4_Box_data_stts_t );
1020 MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1021 MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );
1023 p_box->data.p_stts->i_sample_count =
1024 calloc( sizeof( uint32_t ), p_box->data.p_stts->i_entry_count );
1025 p_box->data.p_stts->i_sample_delta =
1026 calloc( sizeof( uint32_t ), p_box->data.p_stts->i_entry_count );
1028 for( i = 0; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
1030 MP4_GET4BYTES( p_box->data.p_stts->i_sample_count[i] );
1031 MP4_GET4BYTES( p_box->data.p_stts->i_sample_delta[i] );
1036 msg_Dbg( p_stream->p_input, "Read Box: \"stts\" entry-count %d",
1037 p_box->data.p_stts->i_entry_count );
1040 MP4_READBOX_EXIT( 1 );
1043 void MP4_FreeBox_stts( input_thread_t *p_input, MP4_Box_t *p_box )
1045 FREE( p_box->data.p_stts->i_sample_count );
1046 FREE( p_box->data.p_stts->i_sample_delta );
1050 int MP4_ReadBox_ctts( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1053 MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
1055 MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1057 MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );
1059 p_box->data.p_ctts->i_sample_count =
1060 calloc( sizeof( uint32_t ), p_box->data.p_ctts->i_entry_count );
1061 p_box->data.p_ctts->i_sample_offset =
1062 calloc( sizeof( uint32_t ), p_box->data.p_ctts->i_entry_count );
1064 for( i = 0; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
1066 MP4_GET4BYTES( p_box->data.p_ctts->i_sample_count[i] );
1067 MP4_GET4BYTES( p_box->data.p_ctts->i_sample_offset[i] );
1072 msg_Dbg( p_stream->p_input, "Read Box: \"ctts\" entry-count %d",
1073 p_box->data.p_ctts->i_entry_count );
1076 MP4_READBOX_EXIT( 1 );
1079 void MP4_FreeBox_ctts( input_thread_t *p_input, MP4_Box_t *p_box )
1081 FREE( p_box->data.p_ctts->i_sample_count );
1082 FREE( p_box->data.p_ctts->i_sample_offset );
1085 static int MP4_ReadLengthDescriptor( uint8_t **pp_peek, int64_t *i_read )
1095 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1096 } while( i_b&0x80 );
1100 int MP4_ReadBox_esds( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1102 #define es_descriptor p_box->data.p_esds->es_descriptor
1107 MP4_READBOX_ENTER( MP4_Box_data_esds_t );
1109 MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1112 MP4_GET1BYTE( i_type );
1113 if( i_type == 0x03 ) /* MP4ESDescrTag */
1115 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1117 MP4_GET2BYTES( es_descriptor.i_ES_ID );
1118 MP4_GET1BYTE( i_flags );
1119 es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1120 es_descriptor.b_url = ( (i_flags&0x40) != 0);
1121 es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1123 es_descriptor.i_stream_priority = i_flags&0x1f;
1124 if( es_descriptor.b_stream_dependence )
1126 MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1128 if( es_descriptor.b_url )
1132 MP4_GET1BYTE( i_len );
1133 es_descriptor.psz_URL = calloc( sizeof(char), i_len + 1 );
1134 memcpy( es_descriptor.psz_URL, p_peek, i_len );
1135 es_descriptor.psz_URL[i_len] = 0;
1141 es_descriptor.psz_URL = NULL;
1143 if( es_descriptor.b_OCRstream )
1145 MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1147 MP4_GET1BYTE( i_type ); /* get next type */
1150 if( i_type != 0x04)/* MP4DecConfigDescrTag */
1152 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1155 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1156 es_descriptor.p_decConfigDescr =
1157 malloc( sizeof( MP4_descriptor_decoder_config_t ));
1159 MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectTypeIndication );
1160 MP4_GET1BYTE( i_flags );
1161 es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1162 es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1163 MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1164 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1165 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1166 MP4_GET1BYTE( i_type );
1167 if( i_type != 0x05 )/* MP4DecSpecificDescrTag */
1169 MP4_READBOX_EXIT( 1 );
1172 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1173 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1174 es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1175 memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1178 MP4_READBOX_EXIT( 1 );
1180 #undef es_descriptor
1183 void MP4_FreeBox_esds( input_thread_t *p_input, MP4_Box_t *p_box )
1185 FREE( p_box->data.p_esds->es_descriptor.psz_URL );
1186 FREE( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1189 int MP4_ReadBox_sample_soun( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1193 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
1195 for( i = 0; i < 6 ; i++ )
1197 MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
1200 MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
1202 for( i = 0; i < 2 ; i++ )
1204 MP4_GET4BYTES( p_box->data.p_sample_soun->i_reserved2[i] );
1207 MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
1208 MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
1209 MP4_GET2BYTES( p_box->data.p_sample_soun->i_predefined );
1210 MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
1211 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
1212 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
1214 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 28 );
1215 MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds */
1218 msg_Dbg( p_stream->p_input, "Read Box: \"soun\" in stsd channel %d sample size %d sampl rate %f",
1219 p_box->data.p_sample_soun->i_channelcount,
1220 p_box->data.p_sample_soun->i_samplesize,
1221 (float)p_box->data.p_sample_soun->i_sampleratehi +
1222 (float)p_box->data.p_sample_soun->i_sampleratelo / 65536 );
1225 MP4_READBOX_EXIT( 1 );
1229 int MP4_ReadBox_sample_mp4a( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1233 MP4_READBOX_ENTER( MP4_Box_data_sample_mp4a_t );
1235 for( i = 0; i < 6 ; i++ )
1237 MP4_GET1BYTE( p_box->data.p_sample_mp4a->i_reserved1[i] );
1240 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_data_reference_index );
1242 for( i = 0; i < 2 ; i++ )
1244 MP4_GET4BYTES( p_box->data.p_sample_mp4a->i_reserved2[i] );
1247 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_channelcount );
1248 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_samplesize );
1249 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_predefined );
1250 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_reserved3 );
1251 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_sampleratehi );
1252 MP4_GET2BYTES( p_box->data.p_sample_mp4a->i_sampleratelo );
1254 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 28 );
1255 MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds */
1258 msg_Dbg( p_stream->p_input, "Read Box: \"mp4a\" in stsd channel %d sample size %d sampl rate %f",
1259 p_box->data.p_sample_mp4a->i_channelcount,
1260 p_box->data.p_sample_mp4a->i_samplesize,
1261 (float)p_box->data.p_sample_mp4a->i_sampleratehi +
1262 (float)p_box->data.p_sample_mp4a->i_sampleratelo / 65536 );
1265 MP4_READBOX_EXIT( 1 );
1269 int MP4_ReadBox_sample_vide( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1273 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );
1275 for( i = 0; i < 6 ; i++ )
1277 MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
1280 MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
1282 MP4_GET2BYTES( p_box->data.p_sample_vide->i_predefined1 );
1283 MP4_GET2BYTES( p_box->data.p_sample_vide->i_reserved2 );
1285 for( i = 0; i < 3 ; i++ )
1287 MP4_GET4BYTES( p_box->data.p_sample_vide->i_predefined2[i] );
1290 MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
1291 MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
1293 MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
1294 MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
1296 MP4_GET4BYTES( p_box->data.p_sample_vide->i_reserved3 );
1297 MP4_GET2BYTES( p_box->data.p_sample_vide->i_predefined3 );
1299 memcpy( &p_box->data.p_sample_vide->i_compressorname, p_peek, 32 );
1300 p_peek += 32; i_read -= 32;
1302 MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
1303 MP4_GET2BYTES( p_box->data.p_sample_vide->i_predefined4 );
1305 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 78);
1306 MP4_ReadBoxContainerRaw( p_stream, p_box );
1309 msg_Dbg( p_stream->p_input, "Read Box: \"vide\" in stsd %dx%d depth %d",
1310 p_box->data.p_sample_vide->i_width,
1311 p_box->data.p_sample_vide->i_height,
1312 p_box->data.p_sample_vide->i_depth );
1315 MP4_READBOX_EXIT( 1 );
1319 int MP4_ReadBox_stsd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1322 MP4_READBOX_ENTER( MP4_Box_data_stsd_t );
1324 MP4_GETVERSIONFLAGS( p_box->data.p_stsd );
1326 MP4_GET4BYTES( p_box->data.p_stsd->i_entry_count );
1328 MP4_SeekStream( p_stream, p_box->i_pos + MP4_BOX_HEADERSIZE( p_box ) + 8 );
1330 MP4_ReadBoxContainerRaw( p_stream, p_box );
1333 msg_Dbg( p_stream->p_input, "Read Box: \"stsd\" entry-count %d",
1334 p_box->data.p_stsd->i_entry_count );
1337 MP4_READBOX_EXIT( 1 );
1341 int MP4_ReadBox_stsz( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1345 MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
1347 MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
1349 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
1351 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
1353 p_box->data.p_stsz->i_entry_size =
1354 calloc( sizeof( uint32_t ), p_box->data.p_stsz->i_sample_count );
1356 if( !p_box->data.p_stsz->i_sample_size )
1358 for( i=0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
1360 MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
1366 msg_Dbg( p_stream->p_input, "Read Box: \"stsz\" sample-size %d sample-count %d",
1367 p_box->data.p_stsz->i_sample_size,
1368 p_box->data.p_stsz->i_sample_count );
1371 MP4_READBOX_EXIT( 1 );
1374 void MP4_FreeBox_stsz( input_thread_t *p_input, MP4_Box_t *p_box )
1376 FREE( p_box->data.p_stsz->i_entry_size );
1379 int MP4_ReadBox_stsc( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1383 MP4_READBOX_ENTER( MP4_Box_data_stsc_t );
1385 MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
1387 MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
1389 p_box->data.p_stsc->i_first_chunk =
1390 calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1391 p_box->data.p_stsc->i_samples_per_chunk =
1392 calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1393 p_box->data.p_stsc->i_sample_description_index =
1394 calloc( sizeof( uint32_t ), p_box->data.p_stsc->i_entry_count );
1396 for( i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
1398 MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
1399 MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
1400 MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
1405 msg_Dbg( p_stream->p_input, "Read Box: \"stsc\" entry-count %d",
1406 p_box->data.p_stsc->i_entry_count );
1409 MP4_READBOX_EXIT( 1 );
1412 void MP4_FreeBox_stsc( input_thread_t *p_input, MP4_Box_t *p_box )
1414 FREE( p_box->data.p_stsc->i_first_chunk );
1415 FREE( p_box->data.p_stsc->i_samples_per_chunk );
1416 FREE( p_box->data.p_stsc->i_sample_description_index );
1419 int MP4_ReadBox_stco_co64( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1423 MP4_READBOX_ENTER( MP4_Box_data_co64_t );
1425 MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
1427 MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
1429 p_box->data.p_co64->i_chunk_offset =
1430 calloc( sizeof( uint64_t ), p_box->data.p_co64->i_entry_count );
1432 for( i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
1434 if( p_box->i_type == FOURCC_stco )
1440 MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
1448 MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
1454 msg_Dbg( p_stream->p_input, "Read Box: \"co64\" entry-count %d",
1455 p_box->data.p_co64->i_entry_count );
1458 MP4_READBOX_EXIT( 1 );
1461 void MP4_FreeBox_stco_co64( input_thread_t *p_input, MP4_Box_t *p_box )
1463 FREE( p_box->data.p_co64->i_chunk_offset );
1466 int MP4_ReadBox_stss( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1470 MP4_READBOX_ENTER( MP4_Box_data_stss_t );
1472 MP4_GETVERSIONFLAGS( p_box->data.p_stss );
1474 MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
1476 p_box->data.p_stss->i_sample_number =
1477 calloc( sizeof( uint32_t ), p_box->data.p_stss->i_entry_count );
1479 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
1482 MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
1483 /* XXX in libmp4 sample begin at 0 */
1484 p_box->data.p_stss->i_sample_number[i]--;
1489 msg_Dbg( p_stream->p_input, "Read Box: \"stss\" entry-count %d",
1490 p_box->data.p_stss->i_entry_count );
1493 MP4_READBOX_EXIT( 1 );
1496 void MP4_FreeBox_stss( input_thread_t *p_input, MP4_Box_t *p_box )
1498 FREE( p_box->data.p_stss->i_sample_number )
1501 int MP4_ReadBox_stsh( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1505 MP4_READBOX_ENTER( MP4_Box_data_stsh_t );
1507 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
1510 MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
1512 p_box->data.p_stsh->i_shadowed_sample_number =
1513 calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
1515 p_box->data.p_stsh->i_sync_sample_number =
1516 calloc( sizeof( uint32_t ), p_box->data.p_stsh->i_entry_count );
1519 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
1522 MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
1523 MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
1528 msg_Dbg( p_stream->p_input, "Read Box: \"stsh\" entry-count %d",
1529 p_box->data.p_stsh->i_entry_count );
1531 MP4_READBOX_EXIT( 1 );
1534 void MP4_FreeBox_stsh( input_thread_t *p_input, MP4_Box_t *p_box )
1536 FREE( p_box->data.p_stsh->i_shadowed_sample_number )
1537 FREE( p_box->data.p_stsh->i_sync_sample_number )
1541 int MP4_ReadBox_stdp( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1545 MP4_READBOX_ENTER( MP4_Box_data_stdp_t );
1547 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
1549 p_box->data.p_stdp->i_priority =
1550 calloc( sizeof( uint16_t ), i_read / 2 );
1552 for( i = 0; i < i_read / 2 ; i++ )
1555 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
1560 msg_Dbg( p_stream->p_input, "Read Box: \"stdp\" entry-count %d",
1564 MP4_READBOX_EXIT( 1 );
1567 void MP4_FreeBox_stdp( input_thread_t *p_input, MP4_Box_t *p_box )
1569 FREE( p_box->data.p_stdp->i_priority )
1572 int MP4_ReadBox_padb( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1576 MP4_READBOX_ENTER( MP4_Box_data_padb_t );
1578 MP4_GETVERSIONFLAGS( p_box->data.p_padb );
1581 MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
1583 p_box->data.p_padb->i_reserved1 =
1584 calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1585 p_box->data.p_padb->i_pad2 =
1586 calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1587 p_box->data.p_padb->i_reserved2 =
1588 calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1589 p_box->data.p_padb->i_pad1 =
1590 calloc( sizeof( uint16_t ), ( p_box->data.p_padb->i_sample_count + 1 ) / 2 );
1593 for( i = 0; i < i_read / 2 ; i++ )
1595 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
1596 p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
1597 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
1598 p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;
1600 p_peek += 1; i_read -= 1;
1605 msg_Dbg( p_stream->p_input, "Read Box: \"stdp\" entry-count %d",
1609 MP4_READBOX_EXIT( 1 );
1612 void MP4_FreeBox_padb( input_thread_t *p_input, MP4_Box_t *p_box )
1614 FREE( p_box->data.p_padb->i_reserved1 );
1615 FREE( p_box->data.p_padb->i_pad2 );
1616 FREE( p_box->data.p_padb->i_reserved2 );
1617 FREE( p_box->data.p_padb->i_pad1 );
1620 int MP4_ReadBox_elst( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1624 MP4_READBOX_ENTER( MP4_Box_data_padb_t );
1626 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
1629 MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
1631 p_box->data.p_elst->i_segment_duration =
1632 calloc( sizeof( uint64_t ), p_box->data.p_elst->i_entry_count );
1633 p_box->data.p_elst->i_media_time =
1634 calloc( sizeof( uint64_t ), p_box->data.p_elst->i_entry_count );
1635 p_box->data.p_elst->i_media_rate_integer =
1636 calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
1637 p_box->data.p_elst->i_media_rate_fraction=
1638 calloc( sizeof( uint16_t ), p_box->data.p_elst->i_entry_count );
1641 for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
1643 if( p_box->data.p_elst->i_version == 1 )
1646 MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
1648 MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
1653 MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
1655 MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
1658 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
1659 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
1664 msg_Dbg( p_stream->p_input, "Read Box: \"elst\" entry-count %d",
1668 MP4_READBOX_EXIT( 1 );
1671 void MP4_FreeBox_elst( input_thread_t *p_input, MP4_Box_t *p_box )
1673 FREE( p_box->data.p_elst->i_segment_duration );
1674 FREE( p_box->data.p_elst->i_media_time );
1675 FREE( p_box->data.p_elst->i_media_rate_integer );
1676 FREE( p_box->data.p_elst->i_media_rate_fraction );
1679 int MP4_ReadBox_cprt( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1684 MP4_READBOX_ENTER( MP4_Box_data_cprt_t );
1686 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
1688 i_language = GetWBE( p_peek );
1689 for( i = 0; i < 3; i++ )
1691 p_box->data.p_cprt->i_language[i] =
1692 ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
1694 p_peek += 2; i_read -= 2;
1695 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
1698 msg_Dbg( p_stream->p_input, "Read Box: \"cprt\" language %c%c%c notice %s",
1699 p_box->data.p_cprt->i_language[0],
1700 p_box->data.p_cprt->i_language[1],
1701 p_box->data.p_cprt->i_language[2],
1702 p_box->data.p_cprt->psz_notice );
1705 MP4_READBOX_EXIT( 1 );
1708 void MP4_FreeBox_cprt( input_thread_t *p_input, MP4_Box_t *p_box )
1710 FREE( p_box->data.p_cprt->psz_notice );
1714 int MP4_ReadBox_dcom( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1716 MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
1718 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
1720 msg_Dbg( p_stream->p_input,
1721 "Read Box: \"dcom\" compression algorithm : %c%c%c%c",
1722 ( p_box->data.p_dcom->i_algorithm )&0xff,
1723 ( p_box->data.p_dcom->i_algorithm >> 8 )&0xff,
1724 ( p_box->data.p_dcom->i_algorithm >> 16 )&0xff,
1725 ( p_box->data.p_dcom->i_algorithm >> 24 )&0xff );
1728 MP4_READBOX_EXIT( 1 );
1731 int MP4_ReadBox_cmvd( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1733 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );
1736 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
1738 p_box->data.p_cmvd->i_compressed_size = i_read;
1740 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
1742 msg_Dbg( p_stream->p_input, "Read Box: \"cmvd\" not enough memory to load data" );
1746 /* now copy compressed data */
1747 memcpy( p_box->data.p_cmvd->p_data,
1751 p_box->data.p_cmvd->b_compressed = 1;
1754 msg_Dbg( p_stream->p_input, "Read Box: \"cmvd\" compressed data size %d",
1755 p_box->data.p_cmvd->i_compressed_size );
1758 MP4_READBOX_EXIT( 1 );
1760 void MP4_FreeBox_cmvd( input_thread_t *p_input, MP4_Box_t *p_box )
1762 FREE( p_box->data.p_cmvd->p_data );
1766 int MP4_ReadBox_cmov( MP4_Stream_t *p_stream, MP4_Box_t *p_box )
1768 MP4_Stream_t *p_stream_memory;
1780 if( !( p_box->data.p_cmov = malloc( sizeof( MP4_Box_data_cmov_t ) ) ) )
1782 msg_Err( p_stream->p_input, "out of memory" );
1785 memset( p_box->data.p_cmov, 0, sizeof( MP4_Box_data_cmov_t ) );
1787 if( !( p_box->p_father )||
1788 ( p_box->p_father->i_type != FOURCC_moov ) )
1790 msg_Warn( p_stream->p_input, "Read box: \"cmov\" box alone" );
1794 if( !(i_result = MP4_ReadBoxContainer( p_stream, p_box ) ) )
1799 if( !( p_dcom = MP4_FindBox( p_box, FOURCC_dcom ) )||
1800 !( p_cmvd = MP4_FindBox( p_box, FOURCC_cmvd ) )||
1801 !( p_cmvd->data.p_cmvd->p_data ) )
1803 msg_Warn( p_stream->p_input, "Read Box: \"cmov\" incomplete" );
1807 if( p_dcom->data.p_dcom->i_algorithm != FOURCC_zlib )
1809 msg_Dbg( p_stream->p_input, "Read Box: \"cmov\" compression algorithm : %c%c%c%c not supported",
1810 ( p_dcom->data.p_dcom->i_algorithm )&0xff,
1811 ( p_dcom->data.p_dcom->i_algorithm >> 8 )&0xff,
1812 ( p_dcom->data.p_dcom->i_algorithm >> 16 )&0xff,
1813 ( p_dcom->data.p_dcom->i_algorithm >> 24 )&0xff );
1818 msg_Dbg( p_stream->p_input,
1819 "Read Box: \"cmov\" zlib unsupported" );
1822 /* decompress data */
1823 /* allocate a new buffer */
1824 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
1826 msg_Err( p_stream->p_input,
1827 "Read Box: \"cmov\" not enough memory to uncompress data" );
1830 /* init default structures */
1831 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
1832 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
1833 z_data.next_out = p_data;
1834 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
1835 z_data.zalloc = (alloc_func)Z_NULL;
1836 z_data.zfree = (free_func)Z_NULL;
1837 z_data.opaque = (voidpf)Z_NULL;
1840 if( ( i_result = inflateInit( &z_data ) ) != Z_OK )
1842 msg_Err( p_stream->p_input,
1843 "Read Box: \"cmov\" error while uncompressing data" );
1849 i_result = inflate( &z_data, Z_NO_FLUSH );
1850 if( ( i_result != Z_OK )&&( i_result != Z_STREAM_END ) )
1852 msg_Err( p_stream->p_input,
1853 "Read Box: \"cmov\" error while uncompressing data" );
1858 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
1860 msg_Warn( p_stream->p_input,
1861 "Read Box: \"cmov\" uncompressing data size mismatch" );
1863 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
1866 i_result = inflateEnd( &z_data );
1867 if( i_result != Z_OK )
1869 msg_Warn( p_stream->p_input,
1870 "Read Box: \"cmov\" error while uncompressing data (ignored)" );
1874 free( p_cmvd->data.p_cmvd->p_data );
1875 p_cmvd->data.p_cmvd->p_data = p_data;
1876 p_cmvd->data.p_cmvd->b_compressed = 0;
1878 msg_Dbg( p_stream->p_input,
1879 "Read Box: \"cmov\" box succesfully uncompressed" );
1881 /* now create a memory stream */
1882 p_stream_memory = MP4_MemoryStream( p_stream->p_input,
1883 p_cmvd->data.p_cmvd->i_uncompressed_size,
1884 p_cmvd->data.p_cmvd->p_data );
1886 /* and read uncompressd moov */
1887 p_umov = malloc( sizeof( MP4_Box_t ) );
1889 i_result = MP4_ReadBox( p_stream_memory, p_umov, NULL );
1891 p_box->data.p_cmov->p_moov = p_umov;
1892 free( p_stream_memory );
1895 msg_Dbg( p_stream->p_input,
1896 "Read Box: \"cmov\" compressed movie header completed" );
1899 #endif /* HAVE_ZLIB_H */
1903 /**** ------------------------------------------------------------------- ****/
1904 /**** "Higher level" Functions ****/
1905 /**** ------------------------------------------------------------------- ****/
1910 int (*MP4_ReadBox_function )( MP4_Stream_t *p_stream, MP4_Box_t *p_box );
1911 void (*MP4_FreeBox_function )( input_thread_t *p_input, MP4_Box_t *p_box );
1912 } MP4_Box_Function [] =
1915 { FOURCC_moov, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1916 { FOURCC_trak, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1917 { FOURCC_mdia, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1918 { FOURCC_moof, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1919 { FOURCC_minf, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1920 { FOURCC_stbl, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1921 { FOURCC_dinf, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1922 { FOURCC_edts, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1923 { FOURCC_udta, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1924 { FOURCC_nmhd, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1925 { FOURCC_hnti, MP4_ReadBoxContainer, MP4_FreeBox_Common },
1928 { FOURCC_ftyp, MP4_ReadBox_ftyp, MP4_FreeBox_ftyp },
1929 { FOURCC_cmov, MP4_ReadBox_cmov, MP4_FreeBox_Common },
1930 { FOURCC_mvhd, MP4_ReadBox_mvhd, MP4_FreeBox_Common },
1931 { FOURCC_tkhd, MP4_ReadBox_tkhd, MP4_FreeBox_Common },
1932 { FOURCC_tref, MP4_ReadBox_tref, MP4_FreeBox_Common },
1933 { FOURCC_mdhd, MP4_ReadBox_mdhd, MP4_FreeBox_Common },
1934 { FOURCC_hdlr, MP4_ReadBox_hdlr, MP4_FreeBox_hdlr },
1935 { FOURCC_vmhd, MP4_ReadBox_vmhd, MP4_FreeBox_Common },
1936 { FOURCC_smhd, MP4_ReadBox_smhd, MP4_FreeBox_Common },
1937 { FOURCC_hmhd, MP4_ReadBox_hmhd, MP4_FreeBox_Common },
1938 { FOURCC_url, MP4_ReadBox_url, MP4_FreeBox_url },
1939 { FOURCC_urn, MP4_ReadBox_urn, MP4_FreeBox_urn },
1940 { FOURCC_dref, MP4_ReadBox_dref, MP4_FreeBox_Common },
1941 { FOURCC_stts, MP4_ReadBox_stts, MP4_FreeBox_stts },
1942 { FOURCC_ctts, MP4_ReadBox_ctts, MP4_FreeBox_ctts },
1943 { FOURCC_stsd, MP4_ReadBox_stsd, MP4_FreeBox_Common },
1944 { FOURCC_stsz, MP4_ReadBox_stsz, MP4_FreeBox_stsz },
1945 { FOURCC_stsc, MP4_ReadBox_stsc, MP4_FreeBox_stsc },
1946 { FOURCC_stco, MP4_ReadBox_stco_co64, MP4_FreeBox_stco_co64 },
1947 { FOURCC_co64, MP4_ReadBox_stco_co64, MP4_FreeBox_stco_co64 },
1948 { FOURCC_stss, MP4_ReadBox_stss, MP4_FreeBox_stss },
1949 { FOURCC_stsh, MP4_ReadBox_stsh, MP4_FreeBox_stsh },
1950 { FOURCC_stdp, MP4_ReadBox_stdp, MP4_FreeBox_stdp },
1951 { FOURCC_padb, MP4_ReadBox_padb, MP4_FreeBox_padb },
1952 { FOURCC_elst, MP4_ReadBox_elst, MP4_FreeBox_elst },
1953 { FOURCC_cprt, MP4_ReadBox_cprt, MP4_FreeBox_cprt },
1954 { FOURCC_esds, MP4_ReadBox_esds, MP4_FreeBox_esds },
1955 { FOURCC_dcom, MP4_ReadBox_dcom, MP4_FreeBox_Common },
1956 { FOURCC_cmvd, MP4_ReadBox_cmvd, MP4_FreeBox_cmvd },
1958 /* Nothing to do with this box */
1959 { FOURCC_mdat, MP4_ReadBoxSkip, MP4_FreeBox_Common },
1960 { FOURCC_skip, MP4_ReadBoxSkip, MP4_FreeBox_Common },
1961 { FOURCC_free, MP4_ReadBoxSkip, MP4_FreeBox_Common },
1962 { FOURCC_wide, MP4_ReadBoxSkip, MP4_FreeBox_Common },
1965 { FOURCC_soun, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
1966 { FOURCC__mp3, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
1967 { FOURCC_ms55, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
1968 { FOURCC_mp4a, MP4_ReadBox_sample_soun, MP4_FreeBox_Common },
1970 { FOURCC_vide, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1971 { FOURCC_mp4v, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1972 { FOURCC_SVQ1, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1973 { FOURCC_DIVX, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1974 { FOURCC_h263, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1975 { FOURCC_cvid, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1976 { FOURCC_3IV1, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1977 { FOURCC_3IV2, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1978 { FOURCC_mjpa, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1979 { FOURCC_mjpb, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1980 { FOURCC_mjqt, NULL, NULL }, /* found in mjpa/b */
1981 { FOURCC_mjht, NULL, NULL },
1983 { FOURCC_jpeg, MP4_ReadBox_sample_vide, MP4_FreeBox_Common },
1985 { FOURCC_mp4s, NULL, MP4_FreeBox_Common },
1987 /* XXX there is 2 box where we could find this entry stbl and tref*/
1988 { FOURCC_hint, NULL, MP4_FreeBox_Common },
1990 /* found in tref box */
1991 { FOURCC_dpnd, NULL, NULL },
1992 { FOURCC_ipir, NULL, NULL },
1993 { FOURCC_mpod, NULL, NULL },
1996 { FOURCC_rtp, NULL, NULL },
2005 /*****************************************************************************
2006 * MP4_ReadBox : parse the actual box and the children
2007 * XXX : Do not go to the next box
2008 *****************************************************************************/
2009 int MP4_ReadBox( MP4_Stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
2014 if( !MP4_ReadBoxCommon( p_stream, p_box ) )
2016 msg_Warn( p_stream->p_input, "Cannot read one box" );
2019 if( !p_box->i_size )
2021 msg_Dbg( p_stream->p_input, "Found an empty box (null size)" );
2024 p_box->p_father = p_father;
2026 /* Now search function to call */
2027 for( i_index = 0; ; i_index++ )
2029 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
2030 ( MP4_Box_Function[i_index].i_type == 0 ) )
2035 if( MP4_Box_Function[i_index].MP4_ReadBox_function == NULL )
2037 msg_Warn( p_stream->p_input,
2038 "Unknown box type %c%c%c%c (uncompletetly loaded)",
2039 (p_box->i_type)&0xff,
2040 (p_box->i_type>>8)&0xff,
2041 (p_box->i_type>>16)&0xff,
2042 (p_box->i_type>>24)&0xff );
2048 (MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box );
2051 if( !MP4_GotoBox( p_stream, p_box ) )
2058 /*****************************************************************************
2059 * MP4_CountBox: given a box, count how many child have the requested type
2060 * FIXME : support GUUID
2061 *****************************************************************************/
2062 int MP4_CountBox( MP4_Box_t *p_box, uint32_t i_type )
2073 p_child = p_box->p_first;
2076 if( p_child->i_type == i_type )
2080 p_child = p_child->p_next;
2087 /*****************************************************************************
2088 * MP4_FindBox: find first box with i_type child of p_box
2089 * return NULL if not found
2090 *****************************************************************************/
2092 MP4_Box_t *MP4_FindBox( MP4_Box_t *p_box, uint32_t i_type )
2101 p_child = p_box->p_first;
2104 if( p_child->i_type == i_type )
2108 p_child = p_child->p_next;
2116 /*****************************************************************************
2117 * MP4_FindNextBox: find next box with thesame type and at the same level
2119 *****************************************************************************/
2120 MP4_Box_t *MP4_FindNextBox( MP4_Box_t *p_box )
2129 p_next = p_box->p_next;
2132 if( p_next->i_type == p_box->i_type )
2136 p_next = p_next->p_next;
2140 /*****************************************************************************
2141 * MP4_FindNbBox: find the box i_number
2142 *****************************************************************************/
2143 MP4_Box_t *MP4_FindNbBox( MP4_Box_t *p_box, uint32_t i_number )
2145 MP4_Box_t *p_child = p_box->p_first;
2154 if( !( p_child = p_child->p_next ) )
2164 /*****************************************************************************
2165 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
2167 *****************************************************************************/
2168 void MP4_BoxFree( input_thread_t *p_input, MP4_Box_t *p_box )
2179 p_child = p_box->p_first;
2182 p_next = p_child->p_next;
2183 MP4_BoxFree( p_input, p_child );
2184 /* MP4_FreeBoxChildren have free all data expect p_child itself */
2189 /* Now search function to call */
2190 if( p_box->data.p_data )
2192 for( i_index = 0; ; i_index++ )
2194 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
2195 ( MP4_Box_Function[i_index].i_type == 0 ) )
2200 if( MP4_Box_Function[i_index].MP4_FreeBox_function == NULL )
2202 /* Should not happen */
2204 "cannot free box %c%c%c%c, type unknown",
2205 (p_box->i_type)&0xff,
2206 (p_box->i_type >> 8)&0xff,
2207 (p_box->i_type >> 16)&0xff,
2208 (p_box->i_type >> 24)&0xff );
2212 MP4_Box_Function[i_index].MP4_FreeBox_function( p_input, p_box );
2215 free( p_box->data.p_data );
2216 p_box->data.p_data = NULL;
2219 p_box->p_first = NULL;
2220 p_box->p_last = NULL;
2224 /*****************************************************************************
2225 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
2226 *****************************************************************************
2227 * The first box is a virtual box "root" and is the father for all first
2228 * level boxes for the file, a sort of virtual contener
2229 *****************************************************************************/
2230 int MP4_BoxGetRoot( input_thread_t *p_input, MP4_Box_t *p_root )
2233 MP4_Stream_t *p_stream;
2236 MP4_SeekAbsolute( p_input, 0 ); /* Go to the begining */
2238 p_root->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
2239 p_root->i_shortsize = 1;
2240 p_root->i_size = p_input->stream.p_selected_area->i_size;
2241 CreateUUID( &p_root->i_uuid, p_root->i_type );
2243 p_root->data.p_data = NULL;
2244 p_root->p_father = NULL;
2245 p_root->p_first = NULL;
2246 p_root->p_last = NULL;
2247 p_root->p_next = NULL;
2249 p_stream = MP4_InputStream( p_input );
2251 i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
2261 /* check if there is a cmov, if so replace
2262 compressed moov by uncompressed one */
2263 if( ( p_moov = MP4_FindBox( p_root, FOURCC_moov ) )&&
2264 ( p_cmov = MP4_FindBox( p_moov, FOURCC_cmov ) ) )
2266 /* rename the compressed moov as a box to skip */
2267 p_moov->i_type = FOURCC_skip;
2269 /* get uncompressed p_moov */
2270 p_moov = p_cmov->data.p_cmov->p_moov;
2271 p_cmov->data.p_cmov->p_moov = NULL;
2273 /* make p_root father of this new moov */
2274 p_moov->p_father = p_root;
2276 /* insert this new moov box as first child of p_root */
2277 p_moov->p_next = p_child = p_root->p_first;
2278 p_root->p_first = p_moov;
2285 static void __MP4_BoxDumpStructure( input_thread_t *p_input,
2286 MP4_Box_t *p_box, int i_level )
2292 msg_Dbg( p_input, "Dumping root Box \"%c%c%c%c \"",
2293 (p_box->i_type ) &0xff,
2294 (p_box->i_type >>8 ) &0xff,
2295 (p_box->i_type >>16 ) &0xff,
2296 (p_box->i_type >>24) &0xff );
2302 memset( str, (uint8_t)' ', 512 );
2303 for( i = 0; i < i_level; i++ )
2307 sprintf( str + i_level * 5, "+ %c%c%c%c size %d",
2308 (p_box->i_type ) &0xff,
2309 (p_box->i_type>>8 ) &0xff,
2310 (p_box->i_type>>16 ) &0xff,
2311 (p_box->i_type>>24 ) &0xff,
2312 (uint32_t)p_box->i_size );
2314 msg_Dbg( p_input, "%s", str );
2316 p_child = p_box->p_first;
2319 __MP4_BoxDumpStructure( p_input, p_child, i_level + 1 );
2320 p_child = p_child->p_next;
2325 void MP4_BoxDumpStructure( input_thread_t *p_input, MP4_Box_t *p_box )
2327 __MP4_BoxDumpStructure( p_input, p_box, 0 );
2332 /*****************************************************************************
2333 *****************************************************************************
2335 ** High level methods to acces an MP4 file
2337 *****************************************************************************
2338 *****************************************************************************/
2339 static void __get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
2342 if( !*ppsz_path[0] )
2349 while( (*ppsz_path)[i_len] &&
2350 (*ppsz_path)[i_len] != '/' && (*ppsz_path)[i_len] != '[' )
2354 if( !i_len && **ppsz_path == '/' )
2358 *ppsz_token = malloc( i_len + 1 );
2360 memcpy( *ppsz_token, *ppsz_path, i_len );
2362 (*ppsz_token)[i_len] = '\0';
2364 *ppsz_path += i_len;
2366 if( **ppsz_path == '[' )
2369 *pi_number = strtol( *ppsz_path, NULL, 10 );
2370 while( **ppsz_path && **ppsz_path != ']' )
2374 if( **ppsz_path == ']' )
2383 while( **ppsz_path == '/' )
2389 static void __MP4_BoxGet( MP4_Box_t **pp_result,
2390 MP4_Box_t *p_box, char *psz_fmt, va_list args)
2393 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN)
2404 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN)
2405 vasprintf( &psz_path, psz_fmt, args );
2407 i_size = strlen( psz_fmt ) + 1024;
2408 psz_path = calloc( i_size, sizeof( char ) );
2409 vsnprintf( psz_path, i_size, psz_fmt, args );
2410 psz_path[i_size - 1] = 0;
2413 if( !psz_path || !psz_path[0] )
2420 // fprintf( stderr, "path:'%s'\n", psz_path );
2421 psz_fmt = psz_path; /* keep this pointer, as it need to be unallocated */
2427 __get_token( &psz_path, &psz_token, &i_number );
2428 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
2429 // psz_path,psz_token,i_number );
2438 if( !strcmp( psz_token, "/" ) )
2441 while( p_box && p_box->i_type != VLC_FOURCC( 'r', 'o', 'o', 't' ) )
2443 p_box = p_box->p_father;
2454 if( !strcmp( psz_token, "." ) )
2459 if( !strcmp( psz_token, ".." ) )
2461 p_box = p_box->p_father;
2471 if( strlen( psz_token ) == 4 )
2474 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
2475 psz_token[2], psz_token[3] );
2476 p_box = p_box->p_first;
2486 if( p_box->i_type == i_fourcc )
2494 p_box = p_box->p_next;
2498 if( strlen( psz_token ) == 0 )
2500 p_box = p_box->p_first;
2515 p_box = p_box->p_next;
2520 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
2532 /*****************************************************************************
2533 * MP4_BoxGet: find a box given a path relative to p_box
2534 *****************************************************************************
2535 * Path Format: . .. / as usual
2536 * [number] to specifie box number ex: trak[12]
2538 * ex: /moov/trak[12]
2540 *****************************************************************************/
2541 MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, char *psz_fmt, ... )
2544 MP4_Box_t *p_result;
2546 va_start( args, psz_fmt );
2547 __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
2553 /*****************************************************************************
2554 * MP4_BoxCount: count box given a path relative to p_box
2555 *****************************************************************************
2556 * Path Format: . .. / as usual
2557 * [number] to specifie box number ex: trak[12]
2559 * ex: /moov/trak[12]
2561 *****************************************************************************/
2562 int MP4_BoxCount( MP4_Box_t *p_box, char *psz_fmt, ... )
2566 MP4_Box_t *p_result, *p_next;
2568 va_start( args, psz_fmt );
2569 __MP4_BoxGet( &p_result, p_box, psz_fmt, args );
2577 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
2579 if( p_next->i_type == p_result->i_type)