1 /*****************************************************************************
2 * libmp4.c : LibMP4 library for mp4 module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
6 * Author: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_stream.h> /* stream_Peek*/
31 # include <zlib.h> /* for compressed moov */
35 #include "languages.h"
39 * The input method HAS to be seekable
42 /* convert 16.16 fixed point to floating point */
43 static double conv_fx( int32_t fx ) {
49 /* some functions for mp4 encoding of variables */
51 static void MP4_ConvertDate2Str( char *psz, uint64_t i_date, bool b_relative )
58 /* date begin at 1 jan 1904 */
60 i_date += ((INT64_C(1904) * 365) + 17) * 24 * 60 * 60;
62 i_day = i_date / ( 60*60*24);
63 i_hour = ( i_date /( 60*60 ) ) % 60;
64 i_min = ( i_date / 60 ) % 60;
66 sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds", i_day, i_hour, i_min, i_sec );
70 /*****************************************************************************
72 *****************************************************************************/
73 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
76 /*****************************************************************************
77 * MP4_ReadBoxCommon : Load only common parameters for all boxes
78 *****************************************************************************
79 * p_box need to be an already allocated MP4_Box_t, and all data
80 * will only be peek not read
82 * RETURN : 0 if it fail, 1 otherwise
83 *****************************************************************************/
84 int MP4_ReadBoxCommon( stream_t *p_stream, MP4_Box_t *p_box )
87 const uint8_t *p_peek;
89 if( ( ( i_read = stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
93 p_box->i_pos = stream_Tell( p_stream );
95 p_box->data.p_payload = NULL;
96 p_box->p_father = NULL;
97 p_box->p_first = NULL;
101 MP4_GET4BYTES( p_box->i_shortsize );
102 MP4_GETFOURCC( p_box->i_type );
104 /* Now special case */
106 if( p_box->i_shortsize == 1 )
108 /* get the true size on 64 bits */
109 MP4_GET8BYTES( p_box->i_size );
113 p_box->i_size = p_box->i_shortsize;
114 /* XXX size of 0 means that the box extends to end of file */
117 if( p_box->i_type == ATOM_uuid )
119 /* get extented type on 16 bytes */
120 GetUUID( &p_box->i_uuid, p_peek );
121 p_peek += 16; i_read -= 16;
125 CreateUUID( &p_box->i_uuid, p_box->i_type );
127 #ifdef MP4_ULTRA_VERBOSE
130 if MP4_BOX_TYPE_ASCII()
131 msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64" %"PRId64,
132 (char*)&p_box->i_type, p_box->i_size, p_box->i_pos );
134 msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
135 (char*)&p_box->i_type+1, p_box->i_size );
142 /*****************************************************************************
143 * MP4_NextBox : Go to the next box
144 *****************************************************************************
145 * if p_box == NULL, go to the next box in which we are( at the begining ).
146 *****************************************************************************/
147 static int MP4_NextBox( stream_t *p_stream, MP4_Box_t *p_box )
153 MP4_ReadBoxCommon( p_stream, &box );
159 return 2; /* Box with infinite size */
162 if( p_box->p_father )
164 /* if father's size == 0, it means unknown or infinite size,
165 * and we skip the followong check */
166 if( p_box->p_father->i_size > 0 )
168 const off_t i_box_end = p_box->i_size + p_box->i_pos;
169 const off_t i_father_end = p_box->p_father->i_size + p_box->p_father->i_pos;
171 /* check if it's within p-father */
172 if( i_box_end >= i_father_end )
174 if( i_box_end > i_father_end )
175 msg_Dbg( p_stream, "out of bound child" );
176 return 0; /* out of bound */
180 if( stream_Seek( p_stream, p_box->i_size + p_box->i_pos ) )
188 /*****************************************************************************
189 * For all known box a loader is given,
190 * XXX: all common struct have to be already read by MP4_ReadBoxCommon
191 * after called one of theses functions, file position is unknown
192 * you need to call MP4_GotoBox to go where you want
193 *****************************************************************************/
194 static int MP4_ReadBoxContainerChildrenIndexed( stream_t *p_stream,
195 MP4_Box_t *p_container, uint32_t i_last_child, bool b_indexed )
199 /* Size of root container is set to 0 when unknown, for exemple
200 * with a DASH stream. In that case, we skip the following check */
201 if( p_container->i_size
202 && ( stream_Tell( p_stream ) + ((b_indexed)?16:8) >
203 (off_t)(p_container->i_pos + p_container->i_size) )
206 /* there is no box to load */
212 uint32_t i_index = 0;
216 if ( stream_Read( p_stream, read, 8 ) < 8 )
218 i_index = GetDWBE(&read[4]);
220 if( ( p_box = MP4_ReadBox( p_stream, p_container ) ) == NULL ) break;
221 p_box->i_index = i_index;
223 /* chain this box with the father and the other at same level */
224 if( !p_container->p_first ) p_container->p_first = p_box;
225 else p_container->p_last->p_next = p_box;
226 p_container->p_last = p_box;
228 if( p_box->i_type == i_last_child )
230 MP4_NextBox( p_stream, p_box );
234 } while( MP4_NextBox( p_stream, p_box ) == 1 );
239 int MP4_ReadBoxContainerChildren( stream_t *p_stream, MP4_Box_t *p_container,
240 uint32_t i_last_child )
242 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
243 i_last_child, false );
246 static int MP4_ReadBoxContainerRaw( stream_t *p_stream, MP4_Box_t *p_container )
248 return MP4_ReadBoxContainerChildren( p_stream, p_container, 0 );
251 static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
253 if( p_container->i_size &&
254 ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
256 /* container is empty, 8 stand for the first header in this box */
261 stream_Seek( p_stream, p_container->i_pos +
262 mp4_box_headersize( p_container ) );
264 return MP4_ReadBoxContainerRaw( p_stream, p_container );
267 static void MP4_FreeBox_Common( MP4_Box_t *p_box )
269 /* Up to now do nothing */
273 static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
275 /* XXX sometime moov is hiden in a free box */
276 if( p_box->p_father &&
277 p_box->p_father->i_type == ATOM_root &&
278 p_box->i_type == ATOM_free )
280 const uint8_t *p_peek;
284 i_read = stream_Peek( p_stream, &p_peek, 44 );
286 p_peek += mp4_box_headersize( p_box ) + 4;
287 i_read -= mp4_box_headersize( p_box ) + 4;
291 i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
293 if( i_fcc == ATOM_cmov || i_fcc == ATOM_mvhd )
295 msg_Warn( p_stream, "detected moov hidden in a free box ..." );
297 p_box->i_type = ATOM_foov;
298 return MP4_ReadBoxContainer( p_stream, p_box );
304 #ifdef MP4_ULTRA_VERBOSE
305 if MP4_BOX_TYPE_ASCII()
306 msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
308 msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
313 static int MP4_ReadBox_ilst( stream_t *p_stream, MP4_Box_t *p_box )
315 if( p_box->i_size < 8 || stream_Read( p_stream, NULL, 8 ) < 8 )
318 /* Find our handler */
319 if ( !p_box->i_handler && p_box->p_father )
321 const MP4_Box_t *p_sibling = p_box->p_father->p_first;
324 if ( p_sibling->i_type == ATOM_hdlr && p_sibling->data.p_hdlr )
326 p_box->i_handler = p_sibling->data.p_hdlr->i_handler_type;
329 p_sibling = p_sibling->p_next;
333 switch( p_box->i_handler )
336 msg_Warn( p_stream, "no handler for ilst atom" );
339 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_box, 0, true );
341 return MP4_ReadBoxContainerChildren( p_stream, p_box, 0 );
343 msg_Warn( p_stream, "Unknown ilst handler type '%4.4s'", (char*)&p_box->i_handler );
348 static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
350 MP4_READBOX_ENTER( MP4_Box_data_ftyp_t );
352 MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
353 MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
355 if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
357 uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
358 calloc( p_box->data.p_ftyp->i_compatible_brands_count,
361 if( unlikely( tab == NULL ) )
362 MP4_READBOX_EXIT( 0 );
364 for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
366 MP4_GETFOURCC( tab[i] );
371 p_box->data.p_ftyp->i_compatible_brands = NULL;
374 MP4_READBOX_EXIT( 1 );
377 static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
379 FREENULL( p_box->data.p_ftyp->i_compatible_brands );
383 static int MP4_ReadBox_mvhd( stream_t *p_stream, MP4_Box_t *p_box )
386 char s_creation_time[128];
387 char s_modification_time[128];
388 char s_duration[128];
390 MP4_READBOX_ENTER( MP4_Box_data_mvhd_t );
392 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
394 if( p_box->data.p_mvhd->i_version )
396 MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
397 MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
398 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
399 MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
403 MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
404 MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
405 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
406 MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
408 MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
409 MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
410 MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
413 for( unsigned i = 0; i < 2; i++ )
415 MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
417 for( unsigned i = 0; i < 9; i++ )
419 MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
421 for( unsigned i = 0; i < 6; i++ )
423 MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
426 MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
430 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
431 MP4_ConvertDate2Str( s_modification_time,
432 p_box->data.p_mvhd->i_modification_time, false );
433 if( p_box->data.p_mvhd->i_rate )
435 MP4_ConvertDate2Str( s_duration,
436 p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_rate, true );
442 msg_Dbg( p_stream, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
445 (uint32_t)p_box->data.p_mvhd->i_timescale,
447 (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
448 (float)p_box->data.p_mvhd->i_volume / 256 ,
449 (uint32_t)p_box->data.p_mvhd->i_next_track_id );
451 MP4_READBOX_EXIT( 1 );
454 static int MP4_ReadBox_mfhd( stream_t *p_stream, MP4_Box_t *p_box )
456 MP4_READBOX_ENTER( MP4_Box_data_mfhd_t );
458 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
460 MP4_GET4BYTES( p_box->data.p_mfhd->i_sequence_number );
463 msg_Dbg( p_stream, "read box: \"mfhd\" sequence number %d",
464 p_box->data.p_mfhd->i_sequence_number );
466 MP4_READBOX_EXIT( 1 );
469 static int MP4_ReadBox_tfxd( stream_t *p_stream, MP4_Box_t *p_box )
471 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t );
473 MP4_Box_data_tfxd_t *p_tfxd_data = p_box->data.p_tfxd;
474 MP4_GETVERSIONFLAGS( p_tfxd_data );
476 if( p_tfxd_data->i_version == 0 )
478 MP4_GET4BYTES( p_tfxd_data->i_fragment_abs_time );
479 MP4_GET4BYTES( p_tfxd_data->i_fragment_duration );
483 MP4_GET8BYTES( p_tfxd_data->i_fragment_abs_time );
484 MP4_GET8BYTES( p_tfxd_data->i_fragment_duration );
488 msg_Dbg( p_stream, "read box: \"tfxd\" version %d, flags 0x%x, "\
489 "fragment duration %"PRIu64", fragment abs time %"PRIu64,
490 p_tfxd_data->i_version,
491 p_tfxd_data->i_flags,
492 p_tfxd_data->i_fragment_duration,
493 p_tfxd_data->i_fragment_abs_time
497 MP4_READBOX_EXIT( 1 );
500 static int MP4_ReadBox_tfrf( stream_t *p_stream, MP4_Box_t *p_box )
502 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t );
504 MP4_Box_data_tfrf_t *p_tfrf_data = p_box->data.p_tfrf;
505 MP4_GETVERSIONFLAGS( p_tfrf_data );
507 MP4_GET1BYTE( p_tfrf_data->i_fragment_count );
509 p_tfrf_data->p_tfrf_data_fields = calloc( p_tfrf_data->i_fragment_count,
510 sizeof( TfrfBoxDataFields_t ) );
511 if( !p_tfrf_data->p_tfrf_data_fields )
512 MP4_READBOX_EXIT( 0 );
514 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
516 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
517 if( p_tfrf_data->i_version == 0 )
519 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_abs_time );
520 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_duration );
524 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_abs_time );
525 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_duration );
530 msg_Dbg( p_stream, "read box: \"tfrf\" version %d, flags 0x%x, "\
531 "fragment count %"PRIu8, p_tfrf_data->i_version,
532 p_tfrf_data->i_flags, p_tfrf_data->i_fragment_count );
534 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
536 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
537 msg_Dbg( p_stream, "\"tfrf\" fragment duration %"PRIu64", "\
538 "fragment abs time %"PRIu64,
539 TfrfBoxDataField->i_fragment_duration,
540 TfrfBoxDataField->i_fragment_abs_time );
545 MP4_READBOX_EXIT( 1 );
548 static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
550 FREENULL( p_box->data.p_tfrf->p_tfrf_data_fields );
553 static int MP4_ReadBox_stra( stream_t *p_stream, MP4_Box_t *p_box )
555 MP4_READBOX_ENTER( MP4_Box_data_stra_t );
556 MP4_Box_data_stra_t *p_stra = p_box->data.p_stra;
559 VLC_UNUSED(i_reserved);
560 MP4_GET1BYTE( p_stra->i_es_cat );
561 MP4_GET1BYTE( i_reserved );
562 MP4_GET2BYTES( p_stra->i_track_ID );
564 MP4_GET4BYTES( p_stra->i_timescale );
565 MP4_GET8BYTES( p_stra->i_duration );
567 MP4_GET4BYTES( p_stra->FourCC );
568 MP4_GET4BYTES( p_stra->Bitrate );
569 MP4_GET4BYTES( p_stra->MaxWidth );
570 MP4_GET4BYTES( p_stra->MaxHeight );
571 MP4_GET4BYTES( p_stra->SamplingRate );
572 MP4_GET4BYTES( p_stra->Channels );
573 MP4_GET4BYTES( p_stra->BitsPerSample );
574 MP4_GET4BYTES( p_stra->AudioTag );
575 MP4_GET2BYTES( p_stra->nBlockAlign );
577 MP4_GET1BYTE( i_reserved );
578 MP4_GET1BYTE( i_reserved );
579 MP4_GET1BYTE( i_reserved );
580 MP4_GET1BYTE( p_stra->cpd_len );
581 if( p_stra->cpd_len > i_read )
583 p_stra->CodecPrivateData = malloc( p_stra->cpd_len );
584 if( unlikely( p_stra->CodecPrivateData == NULL ) )
586 memcpy( p_stra->CodecPrivateData, p_peek, p_stra->cpd_len );
589 msg_Dbg( p_stream, "es_cat is %"PRIu8", birate is %"PRIu32,
590 p_stra->i_es_cat, p_stra->Bitrate );
593 MP4_READBOX_EXIT( 1 );
595 MP4_READBOX_EXIT( 0 );
598 static void MP4_FreeBox_stra( MP4_Box_t *p_box )
600 FREENULL( p_box->data.p_stra->CodecPrivateData );
603 static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
605 if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
606 return MP4_ReadBox_tfrf( p_stream, p_box );
607 if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
608 return MP4_ReadBox_tfxd( p_stream, p_box );
609 if( !CmpUUID( &p_box->i_uuid, &SmooBoxUUID ) )
610 return MP4_ReadBoxContainer( p_stream, p_box );
611 if( !CmpUUID( &p_box->i_uuid, &StraBoxUUID ) )
612 return MP4_ReadBox_stra( p_stream, p_box );
614 msg_Warn( p_stream, "Unknown uuid type box" );
618 static void MP4_FreeBox_uuid( MP4_Box_t *p_box )
620 if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
621 return MP4_FreeBox_tfrf( p_box );
622 if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
623 return MP4_FreeBox_Common( p_box );
624 if( !CmpUUID( &p_box->i_uuid, &SmooBoxUUID ) )
625 return MP4_FreeBox_Common( p_box );
626 if( !CmpUUID( &p_box->i_uuid, &StraBoxUUID ) )
627 return MP4_FreeBox_stra( p_box );
630 static int MP4_ReadBox_sidx( stream_t *p_stream, MP4_Box_t *p_box )
632 MP4_READBOX_ENTER( MP4_Box_data_sidx_t );
634 MP4_Box_data_sidx_t *p_sidx_data = p_box->data.p_sidx;
635 MP4_GETVERSIONFLAGS( p_sidx_data );
637 MP4_GET4BYTES( p_sidx_data->i_reference_ID );
638 MP4_GET4BYTES( p_sidx_data->i_timescale );
640 if( p_sidx_data->i_version == 0 )
642 MP4_GET4BYTES( p_sidx_data->i_earliest_presentation_time );
643 MP4_GET4BYTES( p_sidx_data->i_first_offset );
647 MP4_GET8BYTES( p_sidx_data->i_earliest_presentation_time );
648 MP4_GET8BYTES( p_sidx_data->i_first_offset );
652 VLC_UNUSED(i_reserved);
653 MP4_GET2BYTES( i_reserved );
654 MP4_GET2BYTES( p_sidx_data->i_reference_count );
655 uint16_t i_count = p_sidx_data->i_reference_count;
657 p_sidx_data->p_items = calloc( i_count, sizeof( MP4_Box_sidx_item_t ) );
659 for( unsigned i = 0; i < i_count; i++ )
661 MP4_GET4BYTES( tmp );
662 p_sidx_data->p_items[i].b_reference_type = (bool)((tmp & 0x80000000)>>24);
663 p_sidx_data->p_items[i].i_referenced_size = tmp & 0x7fffffff;
664 MP4_GET4BYTES( p_sidx_data->p_items[i].i_subsegment_duration );
666 MP4_GET4BYTES( tmp );
667 p_sidx_data->p_items[i].b_starts_with_SAP = (bool)((tmp & 0x80000000)>>24);
668 p_sidx_data->p_items[i].i_SAP_type = (tmp & 0x70000000)>>24;
669 p_sidx_data->p_items[i].i_SAP_delta_time = tmp & 0xfffffff;
673 msg_Dbg( p_stream, "read box: \"sidx\" version %d, flags 0x%x, "\
674 "ref_ID %"PRIu32", timescale %"PRIu32", ref_count %"PRIu16", "\
675 "first subsegmt duration %"PRIu32,
676 p_sidx_data->i_version,
677 p_sidx_data->i_flags,
678 p_sidx_data->i_reference_ID,
679 p_sidx_data->i_timescale,
680 p_sidx_data->i_reference_count,
681 p_sidx_data->p_items[0].i_subsegment_duration
685 MP4_READBOX_EXIT( 1 );
688 static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
690 FREENULL( p_box->data.p_sidx->p_items );
693 static int MP4_ReadBox_tfhd( stream_t *p_stream, MP4_Box_t *p_box )
695 MP4_READBOX_ENTER( MP4_Box_data_tfhd_t );
697 MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );
699 if( p_box->data.p_tfhd->i_version != 0 )
701 msg_Warn( p_stream, "'tfhd' box with version != 0. "\
702 " Don't know what to do with that, please patch" );
703 MP4_READBOX_EXIT( 0 );
706 MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );
708 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
710 msg_Dbg( p_stream, "'duration-is-empty' flag is present "\
711 "=> no samples for this time interval." );
712 p_box->data.p_tfhd->b_empty = true;
715 p_box->data.p_tfhd->b_empty = false;
717 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
718 MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
719 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
720 MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
721 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
722 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
723 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
724 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
725 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
726 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
729 char psz_base[128] = "\0";
730 char psz_desc[128] = "\0";
731 char psz_dura[128] = "\0";
732 char psz_size[128] = "\0";
733 char psz_flag[128] = "\0";
734 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
735 snprintf(psz_base, sizeof(psz_base), "base offset %"PRId64, p_box->data.p_tfhd->i_base_data_offset);
736 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
737 snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
738 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
739 snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
740 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
741 snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
742 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
743 snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);
745 msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
746 p_box->data.p_tfhd->i_version,
747 p_box->data.p_tfhd->i_flags,
748 p_box->data.p_tfhd->i_track_ID,
749 psz_base, psz_desc, psz_dura, psz_size, psz_flag );
752 MP4_READBOX_EXIT( 1 );
755 static int MP4_ReadBox_trun( stream_t *p_stream, MP4_Box_t *p_box )
757 MP4_READBOX_ENTER( MP4_Box_data_trun_t );
759 MP4_GETVERSIONFLAGS( p_box->data.p_trun );
761 MP4_GET4BYTES( p_box->data.p_trun->i_sample_count );
763 if( p_box->data.p_trun->i_flags & MP4_TRUN_DATA_OFFSET )
764 MP4_GET4BYTES( p_box->data.p_trun->i_data_offset );
765 if( p_box->data.p_trun->i_flags & MP4_TRUN_FIRST_FLAGS )
766 MP4_GET4BYTES( p_box->data.p_trun->i_first_sample_flags );
768 p_box->data.p_trun->p_samples =
769 calloc( p_box->data.p_trun->i_sample_count, sizeof(MP4_descriptor_trun_sample_t) );
770 if ( p_box->data.p_trun->p_samples == NULL )
771 MP4_READBOX_EXIT( 0 );
773 for( unsigned int i = 0; i<p_box->data.p_trun->i_sample_count; i++ )
775 MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
776 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
777 MP4_GET4BYTES( p_sample->i_duration );
778 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
779 MP4_GET4BYTES( p_sample->i_size );
780 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS )
781 MP4_GET4BYTES( p_sample->i_flags );
782 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
783 MP4_GET4BYTES( p_sample->i_composition_time_offset );
787 msg_Dbg( p_stream, "read box: \"trun\" version %u flags 0x%x sample count %u",
788 p_box->data.p_trun->i_version,
789 p_box->data.p_trun->i_flags,
790 p_box->data.p_trun->i_sample_count );
792 for( unsigned int i = 0; i<p_box->data.p_trun->i_sample_count; i++ )
794 MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
795 msg_Dbg( p_stream, "read box: \"trun\" sample %4.4u flags 0x%x "\
796 "duration %"PRIu32" size %"PRIu32" composition time offset %"PRIu32,
797 i, p_sample->i_flags, p_sample->i_duration,
798 p_sample->i_size, p_sample->i_composition_time_offset );
802 MP4_READBOX_EXIT( 1 );
805 static void MP4_FreeBox_trun( MP4_Box_t *p_box )
807 FREENULL( p_box->data.p_trun->p_samples );
811 static int MP4_ReadBox_tkhd( stream_t *p_stream, MP4_Box_t *p_box )
814 char s_creation_time[128];
815 char s_modification_time[128];
816 char s_duration[128];
818 MP4_READBOX_ENTER( MP4_Box_data_tkhd_t );
820 MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
822 if( p_box->data.p_tkhd->i_version )
824 MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
825 MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
826 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
827 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
828 MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
832 MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
833 MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
834 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
835 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
836 MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
839 for( unsigned i = 0; i < 2; i++ )
841 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
843 MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
844 MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
845 MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
846 MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
848 for( unsigned i = 0; i < 9; i++ )
850 MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
852 MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
853 MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
855 double rotation; //angle in degrees to be rotated clockwise
856 double scale[2]; // scale factor; sx = scale[0] , sy = scale[1]
857 double translate[2];// amount to translate; tx = translate[0] , ty = translate[1]
859 int32_t *matrix = p_box->data.p_tkhd->i_matrix;
861 translate[0] = conv_fx(matrix[6]);
862 translate[1] = conv_fx(matrix[7]);
864 scale[0] = sqrt(conv_fx(matrix[0]) * conv_fx(matrix[0]) +
865 conv_fx(matrix[3]) * conv_fx(matrix[3]));
866 scale[1] = sqrt(conv_fx(matrix[1]) * conv_fx(matrix[1]) +
867 conv_fx(matrix[4]) * conv_fx(matrix[4]));
869 rotation = atan2(conv_fx(matrix[1]) / scale[1], conv_fx(matrix[0]) / scale[0]) * 180 / M_PI;
874 p_box->data.p_tkhd->f_rotation = rotation;
877 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
878 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time, false );
879 MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration, true );
881 msg_Dbg( p_stream, "read box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f rotation %f scaleX %f scaleY %f translateX %f translateY %f width %f height %f. "
882 "Matrix: %i %i %i %i %i %i %i %i %i",
886 p_box->data.p_tkhd->i_track_ID,
887 p_box->data.p_tkhd->i_layer,
888 (float)p_box->data.p_tkhd->i_volume / 256 ,
894 (float)p_box->data.p_tkhd->i_width / BLOCK16x16,
895 (float)p_box->data.p_tkhd->i_height / BLOCK16x16,
896 p_box->data.p_tkhd->i_matrix[0],
897 p_box->data.p_tkhd->i_matrix[1],
898 p_box->data.p_tkhd->i_matrix[2],
899 p_box->data.p_tkhd->i_matrix[3],
900 p_box->data.p_tkhd->i_matrix[4],
901 p_box->data.p_tkhd->i_matrix[5],
902 p_box->data.p_tkhd->i_matrix[6],
903 p_box->data.p_tkhd->i_matrix[7],
904 p_box->data.p_tkhd->i_matrix[8] );
906 MP4_READBOX_EXIT( 1 );
909 static int MP4_ReadBox_load( stream_t *p_stream, MP4_Box_t *p_box )
911 if ( p_box->i_size != 24 )
913 MP4_READBOX_ENTER( MP4_Box_data_load_t );
914 MP4_GET4BYTES( p_box->data.p_load->i_start_time );
915 MP4_GET4BYTES( p_box->data.p_load->i_duration );
916 MP4_GET4BYTES( p_box->data.p_load->i_flags );
917 MP4_GET4BYTES( p_box->data.p_load->i_hints );
918 MP4_READBOX_EXIT( 1 );
921 static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
925 char s_creation_time[128];
926 char s_modification_time[128];
927 char s_duration[128];
929 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t );
931 MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
933 if( p_box->data.p_mdhd->i_version )
935 MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
936 MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
937 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
938 MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
942 MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
943 MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
944 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
945 MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
948 MP4_GET2BYTES( i_language );
949 decodeQtLanguageCode( i_language, p_box->data.p_mdhd->rgs_language,
950 &p_box->data.p_mdhd->b_mac_encoding );
952 MP4_GET2BYTES( p_box->data.p_mdhd->i_quality );
955 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time, false );
956 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time, false );
957 MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration, true );
958 msg_Dbg( p_stream, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %3.3s",
961 (uint32_t)p_box->data.p_mdhd->i_timescale,
963 (char*) &p_box->data.p_mdhd->rgs_language );
965 MP4_READBOX_EXIT( 1 );
969 static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
972 VLC_UNUSED(i_reserved);
974 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
976 MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
978 MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
979 MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
981 MP4_GET4BYTES( i_reserved );
982 MP4_GET4BYTES( i_reserved );
983 MP4_GET4BYTES( i_reserved );
984 p_box->data.p_hdlr->psz_name = NULL;
988 uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_read + 1 );
989 if( unlikely( psz == NULL ) )
990 MP4_READBOX_EXIT( 0 );
992 /* Yes, I love .mp4 :( */
993 if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
998 MP4_GET1BYTE( i_len );
999 i_copy = __MIN( i_read, i_len );
1001 memcpy( psz, p_peek, i_copy );
1002 p_box->data.p_hdlr->psz_name[i_copy] = '\0';
1006 memcpy( psz, p_peek, i_read );
1007 p_box->data.p_hdlr->psz_name[i_read] = '\0';
1012 msg_Dbg( p_stream, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
1013 (char*)&p_box->data.p_hdlr->i_handler_type,
1014 p_box->data.p_hdlr->psz_name );
1017 MP4_READBOX_EXIT( 1 );
1020 static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
1022 FREENULL( p_box->data.p_hdlr->psz_name );
1025 static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
1027 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t );
1029 MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
1031 MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
1032 for( unsigned i = 0; i < 3; i++ )
1034 MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
1038 msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
1039 p_box->data.p_vmhd->i_graphics_mode,
1040 p_box->data.p_vmhd->i_opcolor[0],
1041 p_box->data.p_vmhd->i_opcolor[1],
1042 p_box->data.p_vmhd->i_opcolor[2] );
1044 MP4_READBOX_EXIT( 1 );
1047 static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
1049 MP4_READBOX_ENTER( MP4_Box_data_smhd_t );
1051 MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
1055 MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
1057 MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
1060 msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
1061 (float)p_box->data.p_smhd->i_balance / 256 );
1063 MP4_READBOX_EXIT( 1 );
1067 static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
1069 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t );
1071 MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
1073 MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
1074 MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
1076 MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
1077 MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
1079 MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
1082 msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1083 p_box->data.p_hmhd->i_max_PDU_size,
1084 p_box->data.p_hmhd->i_avg_PDU_size,
1085 p_box->data.p_hmhd->i_max_bitrate,
1086 p_box->data.p_hmhd->i_avg_bitrate );
1088 MP4_READBOX_EXIT( 1 );
1091 static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
1093 MP4_READBOX_ENTER( MP4_Box_data_url_t );
1095 MP4_GETVERSIONFLAGS( p_box->data.p_url );
1096 MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
1099 msg_Dbg( p_stream, "read box: \"url\" url: %s",
1100 p_box->data.p_url->psz_location );
1103 MP4_READBOX_EXIT( 1 );
1107 static void MP4_FreeBox_url( MP4_Box_t *p_box )
1109 FREENULL( p_box->data.p_url->psz_location );
1112 static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
1114 MP4_READBOX_ENTER( MP4_Box_data_urn_t );
1116 MP4_GETVERSIONFLAGS( p_box->data.p_urn );
1118 MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
1119 MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
1122 msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
1123 p_box->data.p_urn->psz_name,
1124 p_box->data.p_urn->psz_location );
1126 MP4_READBOX_EXIT( 1 );
1128 static void MP4_FreeBox_urn( MP4_Box_t *p_box )
1130 FREENULL( p_box->data.p_urn->psz_name );
1131 FREENULL( p_box->data.p_urn->psz_location );
1135 static int MP4_ReadBox_dref( stream_t *p_stream, MP4_Box_t *p_box )
1137 MP4_READBOX_ENTER( MP4_Box_data_dref_t );
1139 MP4_GETVERSIONFLAGS( p_box->data.p_dref );
1141 MP4_GET4BYTES( p_box->data.p_dref->i_entry_count );
1143 stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
1144 MP4_ReadBoxContainerRaw( p_stream, p_box );
1147 msg_Dbg( p_stream, "read box: \"dref\" entry-count %d",
1148 p_box->data.p_dref->i_entry_count );
1151 MP4_READBOX_EXIT( 1 );
1154 static void MP4_FreeBox_stts( MP4_Box_t *p_box )
1156 FREENULL( p_box->data.p_stts->pi_sample_count );
1157 FREENULL( p_box->data.p_stts->pi_sample_delta );
1160 static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
1162 MP4_READBOX_ENTER( MP4_Box_data_stts_t );
1164 MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1165 MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );
1167 p_box->data.p_stts->pi_sample_count =
1168 calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
1169 p_box->data.p_stts->pi_sample_delta =
1170 calloc( p_box->data.p_stts->i_entry_count, sizeof(int32_t) );
1171 if( p_box->data.p_stts->pi_sample_count == NULL
1172 || p_box->data.p_stts->pi_sample_delta == NULL )
1174 MP4_READBOX_EXIT( 0 );
1178 for( ; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
1180 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_count[i] );
1181 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_delta[i] );
1184 if ( i < p_box->data.p_stts->i_entry_count )
1185 p_box->data.p_stts->i_entry_count = i;
1188 msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
1189 p_box->data.p_stts->i_entry_count );
1192 MP4_READBOX_EXIT( 1 );
1196 static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
1198 FREENULL( p_box->data.p_ctts->pi_sample_count );
1199 FREENULL( p_box->data.p_ctts->pi_sample_offset );
1202 static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
1204 MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
1206 MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1208 MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );
1210 p_box->data.p_ctts->pi_sample_count =
1211 calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
1212 p_box->data.p_ctts->pi_sample_offset =
1213 calloc( p_box->data.p_ctts->i_entry_count, sizeof(int32_t) );
1214 if( ( p_box->data.p_ctts->pi_sample_count == NULL )
1215 || ( p_box->data.p_ctts->pi_sample_offset == NULL ) )
1217 MP4_READBOX_EXIT( 0 );
1221 for( ; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
1223 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_count[i] );
1224 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_offset[i] );
1226 if ( i < p_box->data.p_ctts->i_entry_count )
1227 p_box->data.p_ctts->i_entry_count = i;
1230 msg_Dbg( p_stream, "read box: \"ctts\" entry-count %d",
1231 p_box->data.p_ctts->i_entry_count );
1234 MP4_READBOX_EXIT( 1 );
1238 static int MP4_ReadLengthDescriptor( uint8_t **pp_peek, int64_t *i_read )
1241 unsigned int i_len = 0;
1248 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1249 } while( i_b&0x80 );
1254 static void MP4_FreeBox_esds( MP4_Box_t *p_box )
1256 FREENULL( p_box->data.p_esds->es_descriptor.psz_URL );
1257 if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1259 FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1260 FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1264 static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
1266 #define es_descriptor p_box->data.p_esds->es_descriptor
1268 unsigned int i_flags;
1269 unsigned int i_type;
1271 MP4_READBOX_ENTER( MP4_Box_data_esds_t );
1273 MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1276 MP4_GET1BYTE( i_type );
1277 if( i_type == 0x03 ) /* MP4ESDescrTag ISO/IEC 14496-1 8.3.3 */
1279 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1282 msg_Dbg( p_stream, "found esds MPEG4ESDescr (%dBytes)",
1286 MP4_GET2BYTES( es_descriptor.i_ES_ID );
1287 MP4_GET1BYTE( i_flags );
1288 es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1289 es_descriptor.b_url = ( (i_flags&0x40) != 0);
1290 es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1292 es_descriptor.i_stream_priority = i_flags&0x1f;
1293 if( es_descriptor.b_stream_dependence )
1295 MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1297 if( es_descriptor.b_url )
1301 MP4_GET1BYTE( i_len );
1302 i_len = __MIN(i_read, i_len);
1303 es_descriptor.psz_URL = malloc( i_len + 1 );
1304 if( es_descriptor.psz_URL )
1306 memcpy( es_descriptor.psz_URL, p_peek, i_len );
1307 es_descriptor.psz_URL[i_len] = 0;
1314 es_descriptor.psz_URL = NULL;
1316 if( es_descriptor.b_OCRstream )
1318 MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1320 MP4_GET1BYTE( i_type ); /* get next type */
1323 if( i_type != 0x04)/* MP4DecConfigDescrTag ISO/IEC 14496-1 8.3.4 */
1325 es_descriptor.p_decConfigDescr = NULL;
1326 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1329 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1332 msg_Dbg( p_stream, "found esds MP4DecConfigDescr (%dBytes)",
1336 es_descriptor.p_decConfigDescr =
1337 calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
1338 if( unlikely( es_descriptor.p_decConfigDescr == NULL ) )
1339 MP4_READBOX_EXIT( 0 );
1341 MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectProfileIndication );
1342 MP4_GET1BYTE( i_flags );
1343 es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1344 es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1345 MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1346 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1347 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1348 MP4_GET1BYTE( i_type );
1349 if( i_type != 0x05 )/* MP4DecSpecificDescrTag ISO/IEC 14496-1 8.3.5 */
1351 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1352 es_descriptor.p_decConfigDescr->p_decoder_specific_info = NULL;
1353 MP4_READBOX_EXIT( 1 );
1356 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1359 msg_Dbg( p_stream, "found esds MP4DecSpecificDescr (%dBytes)",
1362 if( i_len > i_read )
1363 MP4_READBOX_EXIT( 0 );
1365 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1366 es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1367 if( unlikely( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL ) )
1368 MP4_READBOX_EXIT( 0 );
1370 memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1373 MP4_READBOX_EXIT( 1 );
1374 #undef es_descriptor
1377 static void MP4_FreeBox_hvcC(MP4_Box_t *p_box )
1379 MP4_Box_data_hvcC_t *p_hvcC = p_box->data.p_hvcC;
1380 if( p_hvcC->i_hvcC > 0 ) FREENULL( p_hvcC->p_hvcC) ;
1383 static int MP4_ReadBox_hvcC( stream_t *p_stream, MP4_Box_t *p_box )
1385 MP4_Box_data_hvcC_t *p_hvcC;
1387 MP4_READBOX_ENTER( MP4_Box_data_hvcC_t );
1388 p_hvcC = p_box->data.p_hvcC;
1390 p_hvcC->i_hvcC = i_read;
1391 if( p_hvcC->i_hvcC > 0 )
1393 uint8_t * p = p_hvcC->p_hvcC = malloc( p_hvcC->i_hvcC );
1395 memcpy( p, p_peek, i_read );
1397 MP4_READBOX_EXIT( 1 );
1400 static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
1402 MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1405 if( p_avcC->i_avcC > 0 ) FREENULL( p_avcC->p_avcC );
1409 for( i = 0; i < p_avcC->i_sps; i++ )
1410 FREENULL( p_avcC->sps[i] );
1414 for( i = 0; i < p_avcC->i_pps; i++ )
1415 FREENULL( p_avcC->pps[i] );
1417 if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->sps );
1418 if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->i_sps_length );
1419 if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->pps );
1420 if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->i_pps_length );
1423 static int MP4_ReadBox_avcC( stream_t *p_stream, MP4_Box_t *p_box )
1425 MP4_Box_data_avcC_t *p_avcC;
1428 MP4_READBOX_ENTER( MP4_Box_data_avcC_t );
1429 p_avcC = p_box->data.p_avcC;
1431 p_avcC->i_avcC = i_read;
1432 if( p_avcC->i_avcC > 0 )
1434 uint8_t * p = p_avcC->p_avcC = malloc( p_avcC->i_avcC );
1436 memcpy( p, p_peek, i_read );
1439 MP4_GET1BYTE( p_avcC->i_version );
1440 MP4_GET1BYTE( p_avcC->i_profile );
1441 MP4_GET1BYTE( p_avcC->i_profile_compatibility );
1442 MP4_GET1BYTE( p_avcC->i_level );
1443 MP4_GET1BYTE( p_avcC->i_reserved1 );
1444 p_avcC->i_length_size = (p_avcC->i_reserved1&0x03) + 1;
1445 p_avcC->i_reserved1 >>= 2;
1447 MP4_GET1BYTE( p_avcC->i_reserved2 );
1448 p_avcC->i_sps = p_avcC->i_reserved2&0x1f;
1449 p_avcC->i_reserved2 >>= 5;
1451 if( p_avcC->i_sps > 0 )
1453 p_avcC->i_sps_length = calloc( p_avcC->i_sps, sizeof( uint16_t ) );
1454 p_avcC->sps = calloc( p_avcC->i_sps, sizeof( uint8_t* ) );
1456 if( !p_avcC->i_sps_length || !p_avcC->sps )
1459 for( i = 0; i < p_avcC->i_sps && i_read > 2; i++ )
1461 MP4_GET2BYTES( p_avcC->i_sps_length[i] );
1462 if ( p_avcC->i_sps_length[i] > i_read )
1464 p_avcC->sps[i] = malloc( p_avcC->i_sps_length[i] );
1465 if( p_avcC->sps[i] )
1466 memcpy( p_avcC->sps[i], p_peek, p_avcC->i_sps_length[i] );
1468 p_peek += p_avcC->i_sps_length[i];
1469 i_read -= p_avcC->i_sps_length[i];
1471 if ( i != p_avcC->i_sps )
1475 MP4_GET1BYTE( p_avcC->i_pps );
1476 if( p_avcC->i_pps > 0 )
1478 p_avcC->i_pps_length = calloc( p_avcC->i_pps, sizeof( uint16_t ) );
1479 p_avcC->pps = calloc( p_avcC->i_pps, sizeof( uint8_t* ) );
1481 if( !p_avcC->i_pps_length || !p_avcC->pps )
1484 for( i = 0; i < p_avcC->i_pps && i_read > 2; i++ )
1486 MP4_GET2BYTES( p_avcC->i_pps_length[i] );
1487 if( p_avcC->i_pps_length[i] > i_read )
1489 p_avcC->pps[i] = malloc( p_avcC->i_pps_length[i] );
1490 if( p_avcC->pps[i] )
1491 memcpy( p_avcC->pps[i], p_peek, p_avcC->i_pps_length[i] );
1493 p_peek += p_avcC->i_pps_length[i];
1494 i_read -= p_avcC->i_pps_length[i];
1496 if ( i != p_avcC->i_pps )
1501 "read box: \"avcC\" version=%d profile=0x%x level=0x%x length size=%d sps=%d pps=%d",
1502 p_avcC->i_version, p_avcC->i_profile, p_avcC->i_level,
1503 p_avcC->i_length_size,
1504 p_avcC->i_sps, p_avcC->i_pps );
1505 for( i = 0; i < p_avcC->i_sps; i++ )
1507 msg_Dbg( p_stream, " - sps[%d] length=%d",
1508 i, p_avcC->i_sps_length[i] );
1510 for( i = 0; i < p_avcC->i_pps; i++ )
1512 msg_Dbg( p_stream, " - pps[%d] length=%d",
1513 i, p_avcC->i_pps_length[i] );
1517 MP4_READBOX_EXIT( 1 );
1520 MP4_FreeBox_avcC( p_box );
1521 MP4_READBOX_EXIT( 0 );
1524 static int MP4_ReadBox_WMA2( stream_t *p_stream, MP4_Box_t *p_box )
1526 MP4_READBOX_ENTER( MP4_Box_data_WMA2_t );
1528 MP4_Box_data_WMA2_t *p_WMA2 = p_box->data.p_WMA2;
1530 MP4_GET2BYTESLE( p_WMA2->Format.wFormatTag );
1531 MP4_GET2BYTESLE( p_WMA2->Format.nChannels );
1532 MP4_GET4BYTESLE( p_WMA2->Format.nSamplesPerSec );
1533 MP4_GET4BYTESLE( p_WMA2->Format.nAvgBytesPerSec );
1534 MP4_GET2BYTESLE( p_WMA2->Format.nBlockAlign );
1535 MP4_GET2BYTESLE( p_WMA2->Format.wBitsPerSample );
1538 MP4_GET2BYTESLE( i_cbSize );
1540 if ( i_read < 0 || i_cbSize > i_read )
1543 p_WMA2->i_extra = i_cbSize;
1544 if ( p_WMA2->i_extra )
1546 p_WMA2->p_extra = malloc( p_WMA2->i_extra );
1547 if ( ! p_WMA2->p_extra )
1549 memcpy( p_WMA2->p_extra, p_peek, p_WMA2->i_extra );
1552 MP4_READBOX_EXIT( 1 );
1555 MP4_READBOX_EXIT( 0 );
1558 static void MP4_FreeBox_WMA2( MP4_Box_t *p_box )
1560 FREENULL( p_box->data.p_WMA2->p_extra );
1563 static int MP4_ReadBox_strf( stream_t *p_stream, MP4_Box_t *p_box )
1565 MP4_READBOX_ENTER( MP4_Box_data_strf_t );
1567 MP4_Box_data_strf_t *p_strf = p_box->data.p_strf;
1569 MP4_GET4BYTESLE( p_strf->bmiHeader.biSize );
1570 MP4_GET4BYTESLE( p_strf->bmiHeader.biWidth );
1571 MP4_GET4BYTESLE( p_strf->bmiHeader.biHeight );
1572 MP4_GET2BYTESLE( p_strf->bmiHeader.biPlanes );
1573 MP4_GET2BYTESLE( p_strf->bmiHeader.biBitCount );
1574 MP4_GETFOURCC( p_strf->bmiHeader.biCompression );
1575 MP4_GET4BYTESLE( p_strf->bmiHeader.biSizeImage );
1576 MP4_GET4BYTESLE( p_strf->bmiHeader.biXPelsPerMeter );
1577 MP4_GET4BYTESLE( p_strf->bmiHeader.biYPelsPerMeter );
1578 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrUsed );
1579 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrImportant );
1584 p_strf->i_extra = i_read;
1585 if ( p_strf->i_extra )
1587 p_strf->p_extra = malloc( p_strf->i_extra );
1588 if ( ! p_strf->p_extra )
1590 memcpy( p_strf->p_extra, p_peek, i_read );
1593 MP4_READBOX_EXIT( 1 );
1596 MP4_READBOX_EXIT( 0 );
1599 static void MP4_FreeBox_strf( MP4_Box_t *p_box )
1601 FREENULL( p_box->data.p_strf->p_extra );
1604 static int MP4_ReadBox_ASF( stream_t *p_stream, MP4_Box_t *p_box )
1606 MP4_READBOX_ENTER( MP4_Box_data_ASF_t );
1608 MP4_Box_data_ASF_t *p_asf = p_box->data.p_asf;
1611 MP4_READBOX_EXIT( 0 );
1613 MP4_GET1BYTE( p_asf->i_stream_number );
1614 /* remaining is unknown */
1616 MP4_READBOX_EXIT( 1 );
1619 static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
1621 MP4_READBOX_ENTER( MP4_Box_data_chan_t );
1622 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
1625 MP4_READBOX_EXIT( 0 );
1627 MP4_GET1BYTE( p_chan->i_version );
1628 MP4_GET3BYTES( p_chan->i_channels_flags );
1629 MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
1630 MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
1631 MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
1633 size_t i_descsize = 8 + 3 * sizeof(float);
1634 if ( (size_t)i_read < p_chan->layout.i_channels_description_count * i_descsize )
1635 MP4_READBOX_EXIT( 0 );
1637 p_chan->layout.p_descriptions =
1638 malloc( p_chan->layout.i_channels_description_count * i_descsize );
1640 if ( !p_chan->layout.p_descriptions )
1641 MP4_READBOX_EXIT( 0 );
1644 for( i=0; i<p_chan->layout.i_channels_description_count; i++ )
1648 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
1649 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
1650 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
1651 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
1652 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
1654 if ( i<p_chan->layout.i_channels_description_count )
1655 p_chan->layout.i_channels_description_count = i;
1659 "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
1660 p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
1661 p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
1663 MP4_READBOX_EXIT( 1 );
1666 static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
1668 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
1669 free( p_chan->layout.p_descriptions );
1672 static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
1674 MP4_READBOX_ENTER( MP4_Box_data_dec3_t );
1676 MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
1679 MP4_GET2BYTES( i_header );
1681 p_dec3->i_data_rate = i_header >> 3;
1682 p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
1683 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
1684 MP4_GET3BYTES( i_header );
1685 p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
1686 p_dec3->stream[i].i_bsid = ( i_header >> 17 ) & 0x01f;
1687 p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
1688 p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
1689 p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
1690 p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
1691 if (p_dec3->stream[i].i_num_dep_sub) {
1692 MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
1693 p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
1695 p_dec3->stream[i].i_chan_loc = 0;
1700 "read box: \"dec3\" bitrate %dkbps %d independant substreams",
1701 p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
1703 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
1705 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
1706 "num dependant subs=%d chan_loc=0x%x",
1707 i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
1708 p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
1710 MP4_READBOX_EXIT( 1 );
1713 static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
1715 MP4_Box_data_dac3_t *p_dac3;
1716 MP4_READBOX_ENTER( MP4_Box_data_dac3_t );
1718 p_dac3 = p_box->data.p_dac3;
1721 MP4_GET3BYTES( i_header );
1723 p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
1724 p_dac3->i_bsid = ( i_header >> 17 ) & 0x01f;
1725 p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
1726 p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
1727 p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
1728 p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
1732 "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
1733 p_dac3->i_fscod, p_dac3->i_bsid, p_dac3->i_bsmod, p_dac3->i_acmod, p_dac3->i_lfeon, p_dac3->i_bitrate_code );
1735 MP4_READBOX_EXIT( 1 );
1738 static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
1740 MP4_Box_data_dvc1_t *p_dvc1;
1742 MP4_READBOX_ENTER( MP4_Box_data_dvc1_t );
1743 p_dvc1 = p_box->data.p_dvc1;
1745 MP4_GET1BYTE( p_dvc1->i_profile_level ); /* profile is on 4bits, level 3bits */
1746 uint8_t i_profile = (p_dvc1->i_profile_level & 0xf0) >> 4;
1747 if( i_profile != 0x06 && i_profile != 0x0c )
1749 msg_Warn( p_stream, "unsupported VC-1 profile (%"PRIu8"), please report", i_profile );
1750 MP4_READBOX_EXIT( 0 );
1754 p_dvc1->i_vc1 = p_box->i_size - 7; /* Header + profile_level */
1756 if( p_dvc1->i_vc1 > 0 )
1758 uint8_t *p = p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 );
1760 memcpy( p, p_peek, i_read );
1765 "read box: \"dvc1\" profile=%"PRIu8" level=%i",
1766 i_profile, p_dvc1->i_profile_level & 0x0e >> 1 );
1769 MP4_READBOX_EXIT( 1 );
1772 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
1774 MP4_Box_data_enda_t *p_enda;
1775 MP4_READBOX_ENTER( MP4_Box_data_enda_t );
1777 p_enda = p_box->data.p_enda;
1779 MP4_GET2BYTES( p_enda->i_little_endian );
1783 "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
1785 MP4_READBOX_EXIT( 1 );
1788 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
1790 p_box->i_handler = ATOM_soun;
1791 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
1792 p_box->data.p_sample_soun->p_qt_description = NULL;
1794 /* Sanity check needed because the "wave" box does also contain an
1795 * "mp4a" box that we don't understand. */
1799 MP4_READBOX_EXIT( 1 );
1802 for( unsigned i = 0; i < 6 ; i++ )
1804 MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
1807 MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
1810 * XXX hack -> produce a copy of the nearly complete chunk
1812 p_box->data.p_sample_soun->i_qt_description = 0;
1813 p_box->data.p_sample_soun->p_qt_description = NULL;
1816 p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
1817 if( p_box->data.p_sample_soun->p_qt_description )
1819 p_box->data.p_sample_soun->i_qt_description = i_read;
1820 memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
1824 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
1825 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
1826 MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
1828 MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
1829 MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
1830 MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
1831 MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
1832 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
1833 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
1835 if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
1837 /* SoundDescriptionV1 */
1838 MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
1839 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
1840 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
1841 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
1845 "read box: \"soun\" qt3+ sample/packet=%d bytes/packet=%d "
1846 "bytes/frame=%d bytes/sample=%d",
1847 p_box->data.p_sample_soun->i_sample_per_packet,
1848 p_box->data.p_sample_soun->i_bytes_per_packet,
1849 p_box->data.p_sample_soun->i_bytes_per_frame,
1850 p_box->data.p_sample_soun->i_bytes_per_sample );
1852 stream_Seek( p_stream, p_box->i_pos +
1853 mp4_box_headersize( p_box ) + 44 );
1855 else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
1857 /* SoundDescriptionV2 */
1858 double f_sample_rate;
1860 uint32_t i_channel, i_extoffset, i_dummy32;
1863 if ( p_box->data.p_sample_soun->i_channelcount != 0x3 ||
1864 p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
1865 p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
1866 p_box->data.p_sample_soun->i_reserved3 != 0x0 ||
1867 p_box->data.p_sample_soun->i_sampleratehi != 0x1 ||//65536
1868 p_box->data.p_sample_soun->i_sampleratelo != 0x0 ) //remainder
1870 msg_Err( p_stream, "invalid stsd V2 box defaults" );
1871 MP4_READBOX_EXIT( 0 );
1875 MP4_GET4BYTES( i_extoffset ); /* offset to stsd extentions */
1876 MP4_GET8BYTES( i_dummy64 );
1877 memcpy( &f_sample_rate, &i_dummy64, 8 );
1878 msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
1879 p_box->data.p_sample_soun->i_sampleratehi = (int)f_sample_rate % BLOCK16x16;
1880 p_box->data.p_sample_soun->i_sampleratelo = f_sample_rate / BLOCK16x16;
1882 MP4_GET4BYTES( i_channel );
1883 p_box->data.p_sample_soun->i_channelcount = i_channel;
1885 MP4_GET4BYTES( i_dummy32 );
1886 if ( i_dummy32 != 0x7F000000 )
1888 msg_Err( p_stream, "invalid stsd V2 box" );
1889 MP4_READBOX_EXIT( 0 );
1892 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
1893 MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
1894 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
1895 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
1898 msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
1899 "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
1901 p_box->data.p_sample_soun->i_constbitsperchannel,
1902 p_box->data.p_sample_soun->i_formatflags,
1903 p_box->data.p_sample_soun->i_constbytesperaudiopacket,
1904 p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
1906 if ( i_extoffset < p_box->i_size )
1907 stream_Seek( p_stream, p_box->i_pos + i_extoffset );
1909 stream_Seek( p_stream, p_box->i_pos + p_box->i_size );
1913 p_box->data.p_sample_soun->i_sample_per_packet = 0;
1914 p_box->data.p_sample_soun->i_bytes_per_packet = 0;
1915 p_box->data.p_sample_soun->i_bytes_per_frame = 0;
1916 p_box->data.p_sample_soun->i_bytes_per_sample = 0;
1919 msg_Dbg( p_stream, "read box: \"soun\" mp4 or qt1/2 (rest=%"PRId64")",
1922 stream_Seek( p_stream, p_box->i_pos +
1923 mp4_box_headersize( p_box ) + 28 );
1926 if( p_box->i_type == ATOM_drms )
1928 msg_Warn( p_stream, "DRM protected streams are not supported." );
1929 MP4_READBOX_EXIT( 0 );
1932 if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
1934 /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
1935 p_box->data.p_sample_soun->i_channelcount = 1;
1938 /* Loads extensions */
1939 MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds/wave/... */
1942 msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
1943 "sample size %d sample rate %f",
1944 p_box->data.p_sample_soun->i_channelcount,
1945 p_box->data.p_sample_soun->i_samplesize,
1946 (float)p_box->data.p_sample_soun->i_sampleratehi +
1947 (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
1950 MP4_READBOX_EXIT( 1 );
1954 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
1956 FREENULL( p_box->data.p_sample_soun->p_qt_description );
1960 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
1962 p_box->i_handler = ATOM_vide;
1963 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );
1965 for( unsigned i = 0; i < 6 ; i++ )
1967 MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
1970 MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
1973 * XXX hack -> produce a copy of the nearly complete chunk
1977 p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
1978 if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
1979 MP4_READBOX_EXIT( 0 );
1980 p_box->data.p_sample_vide->i_qt_image_description = i_read;
1981 memcpy( p_box->data.p_sample_vide->p_qt_image_description,
1986 p_box->data.p_sample_vide->i_qt_image_description = 0;
1987 p_box->data.p_sample_vide->p_qt_image_description = NULL;
1990 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
1991 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
1992 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
1994 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
1995 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
1997 MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
1998 MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
2000 MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
2001 MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
2003 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
2004 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
2007 MP4_READBOX_EXIT( 0 );
2008 memcpy( &p_box->data.p_sample_vide->i_compressorname, p_peek, 32 );
2009 p_peek += 32; i_read -= 32;
2011 MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
2012 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
2014 stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 78);
2016 if( p_box->i_type == ATOM_drmi )
2018 msg_Warn( p_stream, "DRM protected streams are not supported." );
2019 MP4_READBOX_EXIT( 0 );
2022 MP4_ReadBoxContainerRaw( p_stream, p_box );
2025 msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d",
2026 p_box->data.p_sample_vide->i_width,
2027 p_box->data.p_sample_vide->i_height,
2028 p_box->data.p_sample_vide->i_depth );
2031 MP4_READBOX_EXIT( 1 );
2035 void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
2037 FREENULL( p_box->data.p_sample_vide->p_qt_image_description );
2040 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
2042 stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
2043 MP4_ReadBoxContainerRaw( p_stream, p_box );
2047 static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
2051 p_box->i_handler = ATOM_text;
2052 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t );
2054 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2055 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2057 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2059 MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2064 /* FIXME search right signification */
2066 p_box->data.p_sample_text->i_justification_horizontal = 1;
2067 p_box->data.p_sample_text->i_justification_vertical = 1;
2069 case -1: // Flush Right
2070 p_box->data.p_sample_text->i_justification_horizontal = -1;
2071 p_box->data.p_sample_text->i_justification_vertical = -1;
2073 case -2: // Flush Left
2074 p_box->data.p_sample_text->i_justification_horizontal = 0;
2075 p_box->data.p_sample_text->i_justification_vertical = 0;
2077 case 0: // Flush Default
2079 p_box->data.p_sample_text->i_justification_horizontal = 1;
2080 p_box->data.p_sample_text->i_justification_vertical = -1;
2084 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[0] );
2085 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[1] );
2086 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[2] );
2087 p_box->data.p_sample_text->i_background_color[3] = 0xFF;
2089 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2090 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2091 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2092 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2095 msg_Dbg( p_stream, "read box: \"text\" in stsd text" );
2097 MP4_READBOX_EXIT( 1 );
2100 static int MP4_ReadBox_sample_tx3g( stream_t *p_stream, MP4_Box_t *p_box )
2102 p_box->i_handler = ATOM_text;
2103 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t );
2105 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2106 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2108 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2110 MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2112 MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_horizontal );
2113 MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_vertical );
2115 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[0] );
2116 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[1] );
2117 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[2] );
2118 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[3] );
2120 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2121 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2122 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2123 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2125 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved3 );
2127 MP4_GET2BYTES( p_box->data.p_sample_text->i_font_id );
2128 MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_face );
2129 MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_size );
2130 MP4_GET4BYTES( p_box->data.p_sample_text->i_font_color );
2133 msg_Dbg( p_stream, "read box: \"tx3g\" in stsd text" );
2135 MP4_READBOX_EXIT( 1 );
2140 /* We can't easily call it, and anyway ~ 20 bytes lost isn't a real problem */
2141 static void MP4_FreeBox_sample_text( MP4_Box_t *p_box )
2143 FREENULL( p_box->data.p_sample_text->psz_text_name );
2148 static int MP4_ReadBox_stsd( stream_t *p_stream, MP4_Box_t *p_box )
2151 MP4_READBOX_ENTER( MP4_Box_data_stsd_t );
2153 MP4_GETVERSIONFLAGS( p_box->data.p_stsd );
2155 MP4_GET4BYTES( p_box->data.p_stsd->i_entry_count );
2157 stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
2159 MP4_ReadBoxContainerRaw( p_stream, p_box );
2162 msg_Dbg( p_stream, "read box: \"stsd\" entry-count %d",
2163 p_box->data.p_stsd->i_entry_count );
2166 MP4_READBOX_EXIT( 1 );
2170 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
2172 MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
2174 MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
2176 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
2177 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
2179 if( p_box->data.p_stsz->i_sample_size == 0 )
2181 p_box->data.p_stsz->i_entry_size =
2182 calloc( p_box->data.p_stsz->i_sample_count, sizeof(uint32_t) );
2183 if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
2184 MP4_READBOX_EXIT( 0 );
2186 for( unsigned int i = 0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
2188 MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
2192 p_box->data.p_stsz->i_entry_size = NULL;
2195 msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
2196 p_box->data.p_stsz->i_sample_size,
2197 p_box->data.p_stsz->i_sample_count );
2200 MP4_READBOX_EXIT( 1 );
2203 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
2205 FREENULL( p_box->data.p_stsz->i_entry_size );
2208 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
2210 FREENULL( p_box->data.p_stsc->i_first_chunk );
2211 FREENULL( p_box->data.p_stsc->i_samples_per_chunk );
2212 FREENULL( p_box->data.p_stsc->i_sample_description_index );
2215 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
2217 MP4_READBOX_ENTER( MP4_Box_data_stsc_t );
2219 MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
2221 MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
2223 p_box->data.p_stsc->i_first_chunk =
2224 calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2225 p_box->data.p_stsc->i_samples_per_chunk =
2226 calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2227 p_box->data.p_stsc->i_sample_description_index =
2228 calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2229 if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
2230 || p_box->data.p_stsc->i_samples_per_chunk == NULL
2231 || p_box->data.p_stsc->i_sample_description_index == NULL ) )
2233 MP4_READBOX_EXIT( 0 );
2236 for( unsigned int i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
2238 MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
2239 MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
2240 MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
2244 msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
2245 p_box->data.p_stsc->i_entry_count );
2248 MP4_READBOX_EXIT( 1 );
2251 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
2253 MP4_READBOX_ENTER( MP4_Box_data_co64_t );
2255 MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
2257 MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
2259 p_box->data.p_co64->i_chunk_offset =
2260 calloc( p_box->data.p_co64->i_entry_count, sizeof(uint64_t) );
2261 if( p_box->data.p_co64->i_chunk_offset == NULL )
2262 MP4_READBOX_EXIT( 0 );
2264 for( unsigned int i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
2266 if( p_box->i_type == ATOM_stco )
2272 MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2280 MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2285 msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
2286 p_box->data.p_co64->i_entry_count );
2289 MP4_READBOX_EXIT( 1 );
2292 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
2294 FREENULL( p_box->data.p_co64->i_chunk_offset );
2297 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
2299 MP4_READBOX_ENTER( MP4_Box_data_stss_t );
2301 MP4_GETVERSIONFLAGS( p_box->data.p_stss );
2303 MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
2305 p_box->data.p_stss->i_sample_number =
2306 calloc( p_box->data.p_stss->i_entry_count, sizeof(uint32_t) );
2307 if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
2308 MP4_READBOX_EXIT( 0 );
2311 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
2314 MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
2315 /* XXX in libmp4 sample begin at 0 */
2316 p_box->data.p_stss->i_sample_number[i]--;
2318 if ( i < p_box->data.p_stss->i_entry_count )
2319 p_box->data.p_stss->i_entry_count = i;
2322 msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
2323 p_box->data.p_stss->i_entry_count );
2326 MP4_READBOX_EXIT( 1 );
2329 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
2331 FREENULL( p_box->data.p_stss->i_sample_number );
2334 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
2336 FREENULL( p_box->data.p_stsh->i_shadowed_sample_number );
2337 FREENULL( p_box->data.p_stsh->i_sync_sample_number );
2340 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
2342 MP4_READBOX_ENTER( MP4_Box_data_stsh_t );
2344 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
2347 MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
2349 p_box->data.p_stsh->i_shadowed_sample_number =
2350 calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2351 p_box->data.p_stsh->i_sync_sample_number =
2352 calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2354 if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
2355 || p_box->data.p_stsh->i_sync_sample_number == NULL )
2357 MP4_READBOX_EXIT( 0 );
2361 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
2363 MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
2364 MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
2366 if ( i < p_box->data.p_stss->i_entry_count )
2367 p_box->data.p_stss->i_entry_count = i;
2370 msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
2371 p_box->data.p_stsh->i_entry_count );
2373 MP4_READBOX_EXIT( 1 );
2377 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
2379 MP4_READBOX_ENTER( MP4_Box_data_stdp_t );
2381 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
2383 p_box->data.p_stdp->i_priority =
2384 calloc( i_read / 2, sizeof(uint16_t) );
2386 if( unlikely( !p_box->data.p_stdp->i_priority ) )
2387 MP4_READBOX_EXIT( 0 );
2389 for( unsigned i = 0; i < i_read / 2 ; i++ )
2391 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
2395 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
2399 MP4_READBOX_EXIT( 1 );
2402 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
2404 FREENULL( p_box->data.p_stdp->i_priority );
2407 static void MP4_FreeBox_padb( MP4_Box_t *p_box )
2409 FREENULL( p_box->data.p_padb->i_reserved1 );
2410 FREENULL( p_box->data.p_padb->i_pad2 );
2411 FREENULL( p_box->data.p_padb->i_reserved2 );
2412 FREENULL( p_box->data.p_padb->i_pad1 );
2415 static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
2419 MP4_READBOX_ENTER( MP4_Box_data_padb_t );
2421 MP4_GETVERSIONFLAGS( p_box->data.p_padb );
2423 MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
2424 count = (p_box->data.p_padb->i_sample_count + 1) / 2;
2426 p_box->data.p_padb->i_reserved1 = calloc( count, sizeof(uint16_t) );
2427 p_box->data.p_padb->i_pad2 = calloc( count, sizeof(uint16_t) );
2428 p_box->data.p_padb->i_reserved2 = calloc( count, sizeof(uint16_t) );
2429 p_box->data.p_padb->i_pad1 = calloc( count, sizeof(uint16_t) );
2430 if( p_box->data.p_padb->i_reserved1 == NULL
2431 || p_box->data.p_padb->i_pad2 == NULL
2432 || p_box->data.p_padb->i_reserved2 == NULL
2433 || p_box->data.p_padb->i_pad1 == NULL )
2435 MP4_READBOX_EXIT( 0 );
2438 for( unsigned int i = 0; i < i_read / 2 ; i++ )
2442 MP4_READBOX_EXIT( 0 );
2444 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
2445 p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
2446 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
2447 p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;
2449 p_peek += 1; i_read -= 1;
2453 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
2457 MP4_READBOX_EXIT( 1 );
2460 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
2462 FREENULL( p_box->data.p_elst->i_segment_duration );
2463 FREENULL( p_box->data.p_elst->i_media_time );
2464 FREENULL( p_box->data.p_elst->i_media_rate_integer );
2465 FREENULL( p_box->data.p_elst->i_media_rate_fraction );
2468 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
2470 MP4_READBOX_ENTER( MP4_Box_data_elst_t );
2472 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
2475 MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
2477 p_box->data.p_elst->i_segment_duration =
2478 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
2479 p_box->data.p_elst->i_media_time =
2480 calloc( p_box->data.p_elst->i_entry_count, sizeof(int64_t) );
2481 p_box->data.p_elst->i_media_rate_integer =
2482 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
2483 p_box->data.p_elst->i_media_rate_fraction =
2484 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
2485 if( p_box->data.p_elst->i_segment_duration == NULL
2486 || p_box->data.p_elst->i_media_time == NULL
2487 || p_box->data.p_elst->i_media_rate_integer == NULL
2488 || p_box->data.p_elst->i_media_rate_fraction == NULL )
2490 MP4_READBOX_EXIT( 0 );
2494 for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
2496 if( p_box->data.p_elst->i_version == 1 )
2500 MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
2502 MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
2508 MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
2510 MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
2511 p_box->data.p_elst->i_media_time[i] = (int32_t)p_box->data.p_elst->i_media_time[i];
2514 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
2515 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
2517 if ( i < p_box->data.p_elst->i_entry_count )
2518 p_box->data.p_elst->i_entry_count = i;
2520 msg_Dbg( p_stream, "read box: \"elst\" entry-count %lu",
2521 (unsigned long)p_box->data.p_elst->i_entry_count );
2523 MP4_READBOX_EXIT( 1 );
2526 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
2528 uint16_t i_language;
2531 MP4_READBOX_ENTER( MP4_Box_data_cprt_t );
2533 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
2535 MP4_GET2BYTES( i_language );
2536 decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
2538 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
2541 msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
2542 p_box->data.p_cprt->rgs_language,
2543 p_box->data.p_cprt->psz_notice );
2546 MP4_READBOX_EXIT( 1 );
2549 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
2551 FREENULL( p_box->data.p_cprt->psz_notice );
2555 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
2557 MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
2559 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
2562 "read box: \"dcom\" compression algorithm : %4.4s",
2563 (char*)&p_box->data.p_dcom->i_algorithm );
2565 MP4_READBOX_EXIT( 1 );
2568 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
2570 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );
2572 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
2574 p_box->data.p_cmvd->i_compressed_size = i_read;
2576 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
2577 MP4_READBOX_EXIT( 0 );
2579 /* now copy compressed data */
2580 memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
2582 p_box->data.p_cmvd->b_compressed = 1;
2585 msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
2586 p_box->data.p_cmvd->i_compressed_size );
2589 MP4_READBOX_EXIT( 1 );
2591 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
2593 FREENULL( p_box->data.p_cmvd->p_data );
2597 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
2603 stream_t *p_stream_memory;
2609 if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
2612 if( !p_box->p_father ||
2613 ( p_box->p_father->i_type != ATOM_moov &&
2614 p_box->p_father->i_type != ATOM_foov ) )
2616 msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
2620 if( !MP4_ReadBoxContainer( p_stream, p_box ) )
2625 if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
2626 ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
2627 p_cmvd->data.p_cmvd->p_data == NULL )
2629 msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
2633 if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
2635 msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
2636 "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
2641 msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
2645 /* decompress data */
2646 /* allocate a new buffer */
2647 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
2649 /* init default structures */
2650 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
2651 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
2652 z_data.next_out = p_data;
2653 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
2654 z_data.zalloc = (alloc_func)Z_NULL;
2655 z_data.zfree = (free_func)Z_NULL;
2656 z_data.opaque = (voidpf)Z_NULL;
2659 if( inflateInit( &z_data ) != Z_OK )
2661 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
2667 i_result = inflate( &z_data, Z_NO_FLUSH );
2668 if( i_result != Z_OK && i_result != Z_STREAM_END )
2670 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
2675 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
2677 msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
2680 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
2683 if( inflateEnd( &z_data ) != Z_OK )
2685 msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
2689 free( p_cmvd->data.p_cmvd->p_data );
2690 p_cmvd->data.p_cmvd->p_data = p_data;
2691 p_cmvd->data.p_cmvd->b_compressed = 0;
2693 msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
2695 /* now create a memory stream */
2697 stream_MemoryNew( VLC_OBJECT(p_stream), p_cmvd->data.p_cmvd->p_data,
2698 p_cmvd->data.p_cmvd->i_uncompressed_size, true );
2700 /* and read uncompressd moov */
2701 p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
2703 stream_Delete( p_stream_memory );
2706 msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
2709 return p_box->data.p_cmov->p_moov ? 1 : 0;
2710 #endif /* HAVE_ZLIB_H */
2713 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
2716 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t );
2718 MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
2719 MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
2720 MP4_GET4BYTES( i_len );
2725 p_box->data.p_rdrf->psz_ref = malloc( i_len );
2726 if( p_box->data.p_rdrf->psz_ref == NULL )
2727 MP4_READBOX_EXIT( 0 );
2730 for( unsigned i = 0; i < i_len; i++ )
2732 MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
2734 p_box->data.p_rdrf->psz_ref[i_len] = '\0';
2738 p_box->data.p_rdrf->psz_ref = NULL;
2743 "read box: \"rdrf\" type:%4.4s ref %s",
2744 (char*)&p_box->data.p_rdrf->i_ref_type,
2745 p_box->data.p_rdrf->psz_ref );
2747 MP4_READBOX_EXIT( 1 );
2750 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
2752 FREENULL( p_box->data.p_rdrf->psz_ref );
2756 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
2758 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t );
2760 MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
2762 MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
2766 "read box: \"rmdr\" rate:%d",
2767 p_box->data.p_rmdr->i_rate );
2769 MP4_READBOX_EXIT( 1 );
2772 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
2774 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t );
2776 MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
2780 "read box: \"rmqu\" quality:%d",
2781 p_box->data.p_rmqu->i_quality );
2783 MP4_READBOX_EXIT( 1 );
2786 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
2788 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t );
2789 MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
2791 MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
2792 MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
2793 MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
2794 MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
2798 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
2799 (char*)&p_box->data.p_rmvc->i_gestaltType,
2800 p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
2801 p_box->data.p_rmvc->i_checkType );
2804 MP4_READBOX_EXIT( 1 );
2807 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
2809 MP4_READBOX_ENTER( MP4_Box_data_frma_t );
2811 MP4_GETFOURCC( p_box->data.p_frma->i_type );
2814 msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
2815 (char *)&p_box->data.p_frma->i_type );
2818 MP4_READBOX_EXIT( 1 );
2821 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
2823 MP4_READBOX_ENTER( MP4_Box_data_skcr_t );
2825 MP4_GET4BYTES( p_box->data.p_skcr->i_init );
2826 MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
2827 MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
2830 msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
2831 p_box->data.p_skcr->i_init,
2832 p_box->data.p_skcr->i_encr,
2833 p_box->data.p_skcr->i_decr );
2836 MP4_READBOX_EXIT( 1 );
2839 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
2842 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
2843 * so unless data decrypt itself by magic, there will be no playback,
2844 * but we never know... */
2845 msg_Warn( p_stream, "DRM protected streams are not supported." );
2849 static int MP4_ReadBox_String( stream_t *p_stream, MP4_Box_t *p_box )
2851 MP4_READBOX_ENTER( MP4_Box_data_string_t );
2853 p_box->data.p_string->psz_text = malloc( p_box->i_size + 1 - 8 ); /* +\0, -name, -size */
2854 if( p_box->data.p_string->psz_text == NULL )
2855 MP4_READBOX_EXIT( 0 );
2857 memcpy( p_box->data.p_string->psz_text, p_peek, p_box->i_size - 8 );
2858 p_box->data.p_string->psz_text[p_box->i_size - 8] = '\0';
2861 msg_Dbg( p_stream, "read box: \"%4.4s\" text=`%s'", (char *) & p_box->i_type,
2862 p_box->data.p_string->psz_text );
2864 MP4_READBOX_EXIT( 1 );
2867 static void MP4_FreeBox_String( MP4_Box_t *p_box )
2869 FREENULL( p_box->data.p_string->psz_text );
2872 static int MP4_ReadBox_Binary( stream_t *p_stream, MP4_Box_t *p_box )
2874 MP4_READBOX_ENTER( MP4_Box_data_binary_t );
2875 i_read = __MIN( i_read, UINT32_MAX );
2878 p_box->data.p_binary->p_blob = malloc( i_read );
2879 if ( p_box->data.p_binary->p_blob )
2881 memcpy( p_box->data.p_binary->p_blob, p_peek, i_read );
2882 p_box->data.p_binary->i_blob = i_read;
2885 MP4_READBOX_EXIT( 1 );
2888 static void MP4_FreeBox_Binary( MP4_Box_t *p_box )
2890 FREENULL( p_box->data.p_binary->p_blob );
2891 p_box->data.p_binary->i_blob = 0;
2894 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
2896 MP4_READBOX_ENTER( MP4_Box_data_data_t );
2897 MP4_Box_data_data_t *p_data = p_box->data.p_data;
2899 if ( i_read < 8 || i_read - 8 > UINT32_MAX )
2900 MP4_READBOX_EXIT( 0 );
2903 MP4_GET1BYTE( i_type );
2907 msg_Dbg( p_stream, "skipping unknown 'data' atom with type %"PRIu8, i_type );
2909 MP4_READBOX_EXIT( 0 );
2912 MP4_GET3BYTES( p_data->e_wellknowntype );
2913 MP4_GET2BYTES( p_data->locale.i_country );
2914 MP4_GET2BYTES( p_data->locale.i_language );
2916 msg_Dbg( p_stream, "read 'data' atom: knowntype=%"PRIu32", country=%"PRIu16" lang=%"PRIu16
2917 ", size %"PRId64" bytes", p_data->e_wellknowntype,
2918 p_data->locale.i_country, p_data->locale.i_language, i_read );
2920 p_box->data.p_data->p_blob = malloc( i_read );
2921 if ( !p_box->data.p_data->p_blob )
2922 MP4_READBOX_EXIT( 0 );
2924 p_box->data.p_data->i_blob = i_read;
2925 memcpy( p_box->data.p_data->p_blob, p_peek, i_read);
2927 MP4_READBOX_EXIT( 1 );
2930 static void MP4_FreeBox_data( MP4_Box_t *p_box )
2932 free( p_box->data.p_data->p_blob );
2935 static int MP4_ReadBox_Metadata( stream_t *p_stream, MP4_Box_t *p_box )
2937 const uint8_t *p_peek;
2938 if ( stream_Peek( p_stream, &p_peek, 16 ) < 16 )
2940 if ( stream_Read( p_stream, NULL, 8 ) < 8 )
2942 return MP4_ReadBoxContainerChildren( p_stream, p_box, ATOM_data );
2945 /* Chapter support */
2946 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
2948 MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
2949 for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
2950 free( p_chpl->chapter[i].psz_name );
2953 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
2955 MP4_Box_data_chpl_t *p_chpl;
2957 VLC_UNUSED(i_dummy);
2959 MP4_READBOX_ENTER( MP4_Box_data_chpl_t );
2961 p_chpl = p_box->data.p_chpl;
2963 MP4_GETVERSIONFLAGS( p_chpl );
2965 if ( i_read < 5 || p_chpl->i_version != 0x1 )
2966 MP4_READBOX_EXIT( 0 );
2968 MP4_GET4BYTES( i_dummy );
2970 MP4_GET1BYTE( p_chpl->i_chapter );
2972 for( i = 0; i < p_chpl->i_chapter; i++ )
2979 MP4_GET8BYTES( i_start );
2980 MP4_GET1BYTE( i_len );
2982 p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
2983 if( !p_chpl->chapter[i].psz_name )
2984 MP4_READBOX_EXIT( 0 );
2986 i_copy = __MIN( i_len, i_read );
2988 memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
2989 p_chpl->chapter[i].psz_name[i_copy] = '\0';
2990 p_chpl->chapter[i].i_start = i_start;
2996 if ( i != p_chpl->i_chapter )
2997 p_chpl->i_chapter = i;
2999 /* Bubble sort by increasing start date */
3002 for( i = 0; i < p_chpl->i_chapter - 1; i++ )
3004 if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
3006 char *psz = p_chpl->chapter[i+1].psz_name;
3007 int64_t i64 = p_chpl->chapter[i+1].i_start;
3009 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
3010 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
3012 p_chpl->chapter[i].psz_name = psz;
3013 p_chpl->chapter[i].i_start = i64;
3022 msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
3023 p_chpl->i_chapter );
3025 MP4_READBOX_EXIT( 1 );
3028 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
3030 MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t );
3032 p_box->data.p_tref_generic->i_track_ID = NULL;
3033 p_box->data.p_tref_generic->i_entry_count = i_read / sizeof(uint32_t);
3034 if( p_box->data.p_tref_generic->i_entry_count > 0 )
3035 p_box->data.p_tref_generic->i_track_ID = calloc( p_box->data.p_tref_generic->i_entry_count, sizeof(uint32_t) );
3036 if( p_box->data.p_tref_generic->i_track_ID == NULL )
3037 MP4_READBOX_EXIT( 0 );
3039 for( unsigned i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
3041 MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
3044 msg_Dbg( p_stream, "read box: \"chap\" %d references",
3045 p_box->data.p_tref_generic->i_entry_count );
3048 MP4_READBOX_EXIT( 1 );
3050 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
3052 FREENULL( p_box->data.p_tref_generic->i_track_ID );
3055 static int MP4_ReadBox_keys( stream_t *p_stream, MP4_Box_t *p_box )
3057 MP4_READBOX_ENTER( MP4_Box_data_keys_t );
3060 MP4_READBOX_EXIT( 0 );
3063 MP4_GET4BYTES( i_count ); /* reserved + flags */
3065 MP4_READBOX_EXIT( 0 );
3067 MP4_GET4BYTES( i_count );
3068 p_box->data.p_keys->p_entries = calloc( i_count, sizeof(*p_box->data.p_keys->p_entries) );
3069 if ( !p_box->data.p_keys->p_entries )
3070 MP4_READBOX_EXIT( 0 );
3071 p_box->data.p_keys->i_entry_count = i_count;
3074 for( ; i < i_count; i++ )
3079 MP4_GET4BYTES( i_keysize );
3080 if ( (i_keysize < 8) || (i_keysize - 4 > i_read) )
3082 MP4_GETFOURCC( p_box->data.p_keys->p_entries[i].i_namespace );
3084 p_box->data.p_keys->p_entries[i].psz_value = malloc( i_keysize + 1 );
3085 if ( !p_box->data.p_keys->p_entries[i].psz_value )
3087 memcpy( p_box->data.p_keys->p_entries[i].psz_value, p_peek, i_keysize );
3088 p_box->data.p_keys->p_entries[i].psz_value[i_keysize] = 0;
3089 p_peek += i_keysize;
3090 i_read -= i_keysize;
3091 #ifdef MP4_ULTRA_VERBOSE
3092 msg_Dbg( p_stream, "read box: \"keys\": %u '%s'", i + 1,
3093 p_box->data.p_keys->p_entries[i].p_value );
3097 p_box->data.p_keys->i_entry_count = i;
3099 MP4_READBOX_EXIT( 1 );
3102 static void MP4_FreeBox_keys( MP4_Box_t *p_box )
3104 for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
3105 free( p_box->data.p_keys->p_entries[i].psz_value );
3106 free( p_box->data.p_keys->p_entries );
3109 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
3111 uint8_t meta_data[8];
3112 int i_actually_read;
3114 // skip over box header
3115 i_actually_read = stream_Read( p_stream, meta_data, 8 );
3116 if( i_actually_read < 8 )
3119 if ( p_box->p_father && p_box->p_father->i_type == ATOM_udta ) /* itunes udta/meta */
3121 /* meta content starts with a 4 byte version/flags value (should be 0) */
3122 i_actually_read = stream_Read( p_stream, meta_data, 4 );
3123 if( i_actually_read < 4 || memcmp( meta_data, "\0\0\0", 4 ) )
3127 if ( !MP4_ReadBoxContainerChildren( p_stream, p_box, ATOM_hdlr ) )
3131 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box, "hdlr" );
3132 if ( !p_hdlr || !BOXDATA(p_hdlr) ||
3133 ( BOXDATA(p_hdlr)->i_handler_type != HANDLER_mdta &&
3134 BOXDATA(p_hdlr)->i_handler_type != HANDLER_mdir ) ||
3135 BOXDATA(p_hdlr)->i_version != 0 )
3138 /* then it behaves like a container */
3139 return MP4_ReadBoxContainerRaw( p_stream, p_box );
3142 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3145 VLC_UNUSED(i_unused);
3147 MP4_READBOX_ENTER( MP4_Box_data_iods_t );
3148 MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3150 MP4_GET1BYTE( i_unused ); /* tag */
3151 MP4_GET1BYTE( i_unused ); /* length */
3153 MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3154 are used for other flags */
3155 MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3156 MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3157 MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3158 MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3159 MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3163 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3164 "visual: %i, graphics: %i",
3165 p_box->data.p_iods->i_object_descriptor >> 6,
3166 p_box->data.p_iods->i_OD_profile_level,
3167 p_box->data.p_iods->i_scene_profile_level,
3168 p_box->data.p_iods->i_audio_profile_level,
3169 p_box->data.p_iods->i_visual_profile_level,
3170 p_box->data.p_iods->i_graphics_profile_level );
3173 MP4_READBOX_EXIT( 1 );
3176 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3178 MP4_READBOX_ENTER( MP4_Box_data_pasp_t );
3180 MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
3181 MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
3185 "read box: \"paps\" %dx%d",
3186 p_box->data.p_pasp->i_horizontal_spacing,
3187 p_box->data.p_pasp->i_vertical_spacing);
3190 MP4_READBOX_EXIT( 1 );
3193 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
3195 MP4_READBOX_ENTER( MP4_Box_data_mehd_t );
3197 MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
3198 if( p_box->data.p_mehd->i_version == 1 )
3199 MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
3200 else /* version == 0 */
3201 MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
3205 "read box: \"mehd\" frag dur. %"PRIu64"",
3206 p_box->data.p_mehd->i_fragment_duration );
3209 MP4_READBOX_EXIT( 1 );
3212 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
3214 MP4_READBOX_ENTER( MP4_Box_data_trex_t );
3215 MP4_GETVERSIONFLAGS( p_box->data.p_trex );
3217 MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
3218 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
3219 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
3220 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
3221 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
3225 "read box: \"trex\" trackID: %"PRIu32"",
3226 p_box->data.p_trex->i_track_ID );
3229 MP4_READBOX_EXIT( 1 );
3232 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
3234 uint32_t i_sample_count;
3235 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t );
3236 MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
3237 MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
3238 i_sample_count = i_read;
3240 p_sdtp->p_sample_table = calloc( i_sample_count, 1 );
3242 if( !p_sdtp->p_sample_table )
3243 MP4_READBOX_EXIT( 0 );
3245 for( uint32_t i = 0; i < i_sample_count; i++ )
3246 MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
3249 msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
3250 if ( i_sample_count > 3 )
3252 "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
3253 p_sdtp->p_sample_table[0],
3254 p_sdtp->p_sample_table[1],
3255 p_sdtp->p_sample_table[2],
3256 p_sdtp->p_sample_table[3] );
3259 MP4_READBOX_EXIT( 1 );
3262 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
3264 FREENULL( p_box->data.p_sdtp->p_sample_table );
3267 static int MP4_ReadBox_tsel( stream_t *p_stream, MP4_Box_t *p_box )
3269 MP4_READBOX_ENTER( MP4_Box_data_tsel_t );
3271 MP4_GET4BYTES( i_version );
3272 if ( i_version != 0 || i_read < 4 )
3273 MP4_READBOX_EXIT( 0 );
3274 MP4_GET4BYTES( p_box->data.p_tsel->i_switch_group );
3275 /* ignore list of attributes as es are present before switch */
3276 MP4_READBOX_EXIT( 1 );
3279 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
3281 MP4_READBOX_ENTER( MP4_Box_data_mfro_t );
3283 MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
3284 MP4_GET4BYTES( p_box->data.p_mfro->i_size );
3288 "read box: \"mfro\" size: %"PRIu32"",
3289 p_box->data.p_mfro->i_size);
3292 MP4_READBOX_EXIT( 1 );
3295 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
3297 uint32_t i_number_of_entries;
3298 MP4_READBOX_ENTER( MP4_Box_data_tfra_t );
3299 MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
3300 MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
3302 MP4_GET4BYTES( p_tfra->i_track_ID );
3303 uint32_t i_lengths = 0;
3304 MP4_GET4BYTES( i_lengths );
3305 MP4_GET4BYTES( p_tfra->i_number_of_entries );
3306 i_number_of_entries = p_tfra->i_number_of_entries;
3307 p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
3308 p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
3309 p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
3311 size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
3312 p_tfra->p_time = calloc( i_number_of_entries, size );
3313 p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
3315 size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
3316 p_tfra->p_traf_number = calloc( i_number_of_entries, size );
3317 size = 1 + p_tfra->i_length_size_of_trun_num;
3318 p_tfra->p_trun_number = calloc( i_number_of_entries, size );
3319 size = 1 + p_tfra->i_length_size_of_sample_num;
3320 p_tfra->p_sample_number = calloc( i_number_of_entries, size );
3322 if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
3323 || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
3326 int i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
3327 + p_tfra->i_length_size_of_trun_num
3328 + p_tfra->i_length_size_of_sample_num;
3331 for( i = 0; i < i_number_of_entries; i++ )
3334 if( p_tfra->i_version == 1 )
3336 if ( i_read < i_fields_length + 16 )
3338 MP4_GET8BYTES( p_tfra->p_time[i*2] );
3339 MP4_GET8BYTES( p_tfra->p_moof_offset[i*2] );
3343 if ( i_read < i_fields_length + 8 )
3345 MP4_GET4BYTES( p_tfra->p_time[i] );
3346 MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
3348 switch (p_tfra->i_length_size_of_traf_num)
3351 MP4_GET1BYTE( p_tfra->p_traf_number[i] );
3354 MP4_GET2BYTES( p_tfra->p_traf_number[i*2] );
3357 MP4_GET3BYTES( p_tfra->p_traf_number[i*3] );
3360 MP4_GET4BYTES( p_tfra->p_traf_number[i*4] );
3366 switch (p_tfra->i_length_size_of_trun_num)
3369 MP4_GET1BYTE( p_tfra->p_trun_number[i] );
3372 MP4_GET2BYTES( p_tfra->p_trun_number[i*2] );
3375 MP4_GET3BYTES( p_tfra->p_trun_number[i*3] );
3378 MP4_GET4BYTES( p_tfra->p_trun_number[i*4] );
3384 switch (p_tfra->i_length_size_of_sample_num)
3387 MP4_GET1BYTE( p_tfra->p_sample_number[i] );
3390 MP4_GET2BYTES( p_tfra->p_sample_number[i*2] );
3393 MP4_GET3BYTES( p_tfra->p_sample_number[i*3] );
3396 MP4_GET4BYTES( p_tfra->p_sample_number[i*4] );
3402 if ( i < i_number_of_entries )
3403 i_number_of_entries = i;
3406 if( p_tfra->i_version == 0 )
3408 msg_Dbg( p_stream, "time[0]: %"PRIu32", moof_offset[0]: %"PRIx32"",
3409 p_tfra->p_time[0], p_tfra->p_moof_offset[0] );
3411 msg_Dbg( p_stream, "time[1]: %"PRIu32", moof_offset[1]: %"PRIx32"",
3412 p_tfra->p_time[1], p_tfra->p_moof_offset[1] );
3416 msg_Dbg( p_stream, "time[0]: %"PRIu64", moof_offset[0]: %"PRIx64"",
3417 ((uint64_t *)(p_tfra->p_time))[0],
3418 ((uint64_t *)(p_tfra->p_moof_offset))[0] );
3420 msg_Dbg( p_stream, "time[1]: %"PRIu64", moof_offset[1]: %"PRIx64"",
3421 ((uint64_t *)(p_tfra->p_time))[1],
3422 ((uint64_t *)(p_tfra->p_moof_offset))[1] );
3425 msg_Dbg( p_stream, "number_of_entries is %"PRIu32"", i_number_of_entries );
3426 msg_Dbg( p_stream, "track ID is: %"PRIu32"", p_tfra->i_track_ID );
3429 MP4_READBOX_EXIT( 1 );
3431 MP4_READBOX_EXIT( 0 );
3434 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
3436 FREENULL( p_box->data.p_tfra->p_time );
3437 FREENULL( p_box->data.p_tfra->p_moof_offset );
3438 FREENULL( p_box->data.p_tfra->p_traf_number );
3439 FREENULL( p_box->data.p_tfra->p_trun_number );
3440 FREENULL( p_box->data.p_tfra->p_sample_number );
3443 static int MP4_ReadBox_pnot( stream_t *p_stream, MP4_Box_t *p_box )
3445 if ( p_box->i_size != 20 )
3447 MP4_READBOX_ENTER( MP4_Box_data_pnot_t );
3448 MP4_GET4BYTES( p_box->data.p_pnot->i_date );
3450 MP4_GET2BYTES( i_version );
3451 if ( i_version != 0 )
3452 MP4_READBOX_EXIT( 0 );
3453 MP4_GETFOURCC( p_box->data.p_pnot->i_type );
3454 MP4_GET2BYTES( p_box->data.p_pnot->i_index );
3455 MP4_READBOX_EXIT( 1 );
3459 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
3461 if( !p_box->p_father )
3465 if( p_box->p_father->i_type == ATOM_stsd )
3467 MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
3470 if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
3471 (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
3475 switch( p_hdlr->data.p_hdlr->i_handler_type )
3478 return MP4_ReadBox_sample_soun( p_stream, p_box );
3480 return MP4_ReadBox_sample_vide( p_stream, p_box );
3482 return MP4_ReadBox_sample_text( p_stream, p_box );
3485 return MP4_ReadBox_sample_tx3g( p_stream, p_box );
3488 "unknown handler type in stsd (incompletely loaded)" );
3494 if MP4_BOX_TYPE_ASCII()
3496 "unknown box type %4.4s (incompletely loaded)",
3497 (char*)&p_box->i_type );
3500 "unknown box type c%3.3s (incompletely loaded)",
3501 (char*)&p_box->i_type+1 );
3502 p_box->e_flags |= BOX_FLAG_INCOMPLETE;
3507 /**** ------------------------------------------------------------------- ****/
3508 /**** "Higher level" Functions ****/
3509 /**** ------------------------------------------------------------------- ****/
3514 int (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
3515 void (*MP4_FreeBox_function )( MP4_Box_t *p_box );
3516 uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
3517 } MP4_Box_Function [] =
3520 { ATOM_moov, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3521 { ATOM_foov, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3522 { ATOM_trak, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_moov },
3523 { ATOM_trak, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_foov },
3524 { ATOM_mdia, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_trak },
3525 { ATOM_moof, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3526 { ATOM_minf, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_mdia },
3527 { ATOM_stbl, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_minf },
3528 { ATOM_dinf, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_minf },
3529 { ATOM_dinf, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_meta },
3530 { ATOM_edts, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_trak },
3531 { ATOM_udta, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3532 { ATOM_nmhd, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_minf },
3533 { ATOM_hnti, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_udta },
3534 { ATOM_rmra, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_moov },
3535 { ATOM_rmda, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_rmra },
3536 { ATOM_tref, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_trak },
3537 { ATOM_gmhd, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_minf },
3538 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_stsd },
3539 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
3540 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_WMA2 }, /* flip4mac */
3541 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_in24 },
3542 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_in32 },
3543 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_fl32 },
3544 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_fl64 },
3545 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_QDMC },
3546 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_QDM2 },
3547 { ATOM_ilst, MP4_ReadBox_ilst, MP4_FreeBox_Common, ATOM_meta },
3548 { ATOM_mvex, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_moov },
3549 { ATOM_mvex, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_ftyp },
3552 { ATOM_ftyp, MP4_ReadBox_ftyp, MP4_FreeBox_ftyp, 0 },
3553 { ATOM_cmov, MP4_ReadBox_cmov, MP4_FreeBox_Common, 0 },
3554 { ATOM_mvhd, MP4_ReadBox_mvhd, MP4_FreeBox_Common, ATOM_moov },
3555 { ATOM_mvhd, MP4_ReadBox_mvhd, MP4_FreeBox_Common, ATOM_foov },
3556 { ATOM_tkhd, MP4_ReadBox_tkhd, MP4_FreeBox_Common, ATOM_trak },
3557 { ATOM_load, MP4_ReadBox_load, MP4_FreeBox_Common, ATOM_trak },
3558 { ATOM_mdhd, MP4_ReadBox_mdhd, MP4_FreeBox_Common, ATOM_mdia },
3559 { ATOM_hdlr, MP4_ReadBox_hdlr, MP4_FreeBox_hdlr, ATOM_mdia },
3560 { ATOM_hdlr, MP4_ReadBox_hdlr, MP4_FreeBox_hdlr, ATOM_meta },
3561 { ATOM_hdlr, MP4_ReadBox_hdlr, MP4_FreeBox_hdlr, ATOM_minf },
3562 { ATOM_vmhd, MP4_ReadBox_vmhd, MP4_FreeBox_Common, ATOM_minf },
3563 { ATOM_smhd, MP4_ReadBox_smhd, MP4_FreeBox_Common, ATOM_minf },
3564 { ATOM_hmhd, MP4_ReadBox_hmhd, MP4_FreeBox_Common, ATOM_minf },
3565 { ATOM_alis, MP4_ReadBoxSkip, MP4_FreeBox_Common, ATOM_dref },
3566 { ATOM_url, MP4_ReadBox_url, MP4_FreeBox_url, 0 },
3567 { ATOM_urn, MP4_ReadBox_urn, MP4_FreeBox_urn, 0 },
3568 { ATOM_dref, MP4_ReadBox_dref, MP4_FreeBox_Common, 0 },
3569 { ATOM_stts, MP4_ReadBox_stts, MP4_FreeBox_stts, ATOM_stbl },
3570 { ATOM_ctts, MP4_ReadBox_ctts, MP4_FreeBox_ctts, ATOM_stbl },
3571 { ATOM_stsd, MP4_ReadBox_stsd, MP4_FreeBox_Common, ATOM_stbl },
3572 { ATOM_stsz, MP4_ReadBox_stsz, MP4_FreeBox_stsz, ATOM_stbl },
3573 { ATOM_stsc, MP4_ReadBox_stsc, MP4_FreeBox_stsc, ATOM_stbl },
3574 { ATOM_stco, MP4_ReadBox_stco_co64, MP4_FreeBox_stco_co64, ATOM_stbl },
3575 { ATOM_co64, MP4_ReadBox_stco_co64, MP4_FreeBox_stco_co64, ATOM_stbl },
3576 { ATOM_stss, MP4_ReadBox_stss, MP4_FreeBox_stss, ATOM_stbl },
3577 { ATOM_stsh, MP4_ReadBox_stsh, MP4_FreeBox_stsh, ATOM_stbl },
3578 { ATOM_stdp, MP4_ReadBox_stdp, MP4_FreeBox_stdp, 0 },
3579 { ATOM_padb, MP4_ReadBox_padb, MP4_FreeBox_padb, 0 },
3580 { ATOM_elst, MP4_ReadBox_elst, MP4_FreeBox_elst, ATOM_edts },
3581 { ATOM_cprt, MP4_ReadBox_cprt, MP4_FreeBox_cprt, 0 },
3582 { ATOM_esds, MP4_ReadBox_esds, MP4_FreeBox_esds, ATOM_wave }, /* mp4a in wave chunk */
3583 { ATOM_esds, MP4_ReadBox_esds, MP4_FreeBox_esds, ATOM_mp4a },
3584 { ATOM_esds, MP4_ReadBox_esds, MP4_FreeBox_esds, ATOM_mp4v },
3585 { ATOM_esds, MP4_ReadBox_esds, MP4_FreeBox_esds, ATOM_mp4s },
3586 { ATOM_dcom, MP4_ReadBox_dcom, MP4_FreeBox_Common, 0 },
3587 { ATOM_cmvd, MP4_ReadBox_cmvd, MP4_FreeBox_cmvd, 0 },
3588 { ATOM_avcC, MP4_ReadBox_avcC, MP4_FreeBox_avcC, ATOM_avc1 },
3589 { ATOM_hvcC, MP4_ReadBox_hvcC, MP4_FreeBox_hvcC, 0 },
3590 { ATOM_dac3, MP4_ReadBox_dac3, MP4_FreeBox_Common, 0 },
3591 { ATOM_dec3, MP4_ReadBox_dec3, MP4_FreeBox_Common, 0 },
3592 { ATOM_dvc1, MP4_ReadBox_dvc1, MP4_FreeBox_Common, 0 },
3593 { ATOM_enda, MP4_ReadBox_enda, MP4_FreeBox_Common, 0 },
3594 { ATOM_iods, MP4_ReadBox_iods, MP4_FreeBox_Common, 0 },
3595 { ATOM_pasp, MP4_ReadBox_pasp, MP4_FreeBox_Common, 0 },
3596 { ATOM_keys, MP4_ReadBox_keys, MP4_FreeBox_keys, ATOM_meta },
3598 /* Quicktime preview atoms, all at root */
3599 { ATOM_pnot, MP4_ReadBox_pnot, MP4_FreeBox_Common, 0 },
3600 { ATOM_pict, MP4_ReadBox_Binary, MP4_FreeBox_Binary, 0 },
3601 { ATOM_PICT, MP4_ReadBox_Binary, MP4_FreeBox_Binary, 0 },
3603 /* Nothing to do with this box */
3604 { ATOM_mdat, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3605 { ATOM_skip, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3606 { ATOM_free, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3607 { ATOM_wide, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3608 { ATOM_binm, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3611 { ATOM_tx3g, MP4_ReadBox_sample_tx3g, MP4_FreeBox_Common, 0 },
3612 //{ ATOM_text, MP4_ReadBox_sample_text, MP4_FreeBox_Common, 0 },
3615 { ATOM_soun, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3616 { ATOM_ac3, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3617 { ATOM_eac3, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3618 { ATOM_lpcm, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3619 { ATOM_ms02, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3620 { ATOM_ms11, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3621 { ATOM_ms55, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3622 { ATOM__mp3, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3623 { ATOM_mp4a, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3624 { ATOM_twos, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3625 { ATOM_sowt, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3626 { ATOM_QDMC, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3627 { ATOM_QDM2, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3628 { ATOM_ima4, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3629 { ATOM_IMA4, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3630 { ATOM_dvi, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3631 { ATOM_alaw, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3632 { ATOM_ulaw, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3633 { ATOM_raw, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3634 { ATOM_MAC3, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3635 { ATOM_MAC6, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3636 { ATOM_Qclp, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3637 { ATOM_samr, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3638 { ATOM_sawb, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3639 { ATOM_OggS, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3640 { ATOM_alac, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3641 { ATOM_WMA2, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd }, /* flip4mac */
3642 /* Sound extensions */
3643 { ATOM_chan, MP4_ReadBox_stsdext_chan, MP4_FreeBox_stsdext_chan, 0 },
3644 { ATOM_WMA2, MP4_ReadBox_WMA2, MP4_FreeBox_WMA2, ATOM_wave }, /* flip4mac */
3646 { ATOM_drmi, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3647 { ATOM_vide, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3648 { ATOM_mp4v, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3649 { ATOM_SVQ1, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3650 { ATOM_SVQ3, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3651 { ATOM_ZyGo, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3652 { ATOM_DIVX, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3653 { ATOM_XVID, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3654 { ATOM_h263, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3655 { ATOM_s263, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3656 { ATOM_cvid, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3657 { ATOM_3IV1, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3658 { ATOM_3iv1, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3659 { ATOM_3IV2, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3660 { ATOM_3iv2, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3661 { ATOM_3IVD, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3662 { ATOM_3ivd, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3663 { ATOM_3VID, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3664 { ATOM_3vid, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3665 { ATOM_mjpa, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3666 { ATOM_mjpb, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3667 { ATOM_qdrw, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3668 { ATOM_mp2v, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3669 { ATOM_hdv2, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3670 { ATOM_WMV3, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3672 { ATOM_mjqt, MP4_ReadBox_default, NULL, 0 }, /* found in mjpa/b */
3673 { ATOM_mjht, MP4_ReadBox_default, NULL, 0 },
3675 { ATOM_dvc, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3676 { ATOM_dvp, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3677 { ATOM_dv5n, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3678 { ATOM_dv5p, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3679 { ATOM_VP31, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3680 { ATOM_vp31, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3681 { ATOM_h264, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3683 { ATOM_jpeg, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3684 { ATOM_avc1, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3686 { ATOM_yv12, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, 0 },
3687 { ATOM_yuv2, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, 0 },
3689 { ATOM_strf, MP4_ReadBox_strf, MP4_FreeBox_strf, ATOM_WMV3 }, /* flip4mac */
3690 { ATOM_ASF , MP4_ReadBox_ASF, MP4_FreeBox_Common, ATOM_WMV3 }, /* flip4mac */
3691 { ATOM_ASF , MP4_ReadBox_ASF, MP4_FreeBox_Common, ATOM_wave }, /* flip4mac */
3693 { ATOM_mp4s, MP4_ReadBox_sample_mp4s, MP4_FreeBox_Common, ATOM_stsd },
3695 /* XXX there is 2 box where we could find this entry stbl and tref*/
3696 { ATOM_hint, MP4_ReadBox_default, MP4_FreeBox_Common, 0 },
3698 /* found in tref box */
3699 { ATOM_dpnd, MP4_ReadBox_default, NULL, 0 },
3700 { ATOM_ipir, MP4_ReadBox_default, NULL, 0 },
3701 { ATOM_mpod, MP4_ReadBox_default, NULL, 0 },
3702 { ATOM_chap, MP4_ReadBox_tref_generic, MP4_FreeBox_tref_generic, 0 },
3705 { ATOM_rtp, MP4_ReadBox_default, NULL, 0 },
3707 /* found in rmra/rmda */
3708 { ATOM_rdrf, MP4_ReadBox_rdrf, MP4_FreeBox_rdrf , ATOM_rmda },
3709 { ATOM_rmdr, MP4_ReadBox_rmdr, MP4_FreeBox_Common, ATOM_rmda },
3710 { ATOM_rmqu, MP4_ReadBox_rmqu, MP4_FreeBox_Common, ATOM_rmda },
3711 { ATOM_rmvc, MP4_ReadBox_rmvc, MP4_FreeBox_Common, ATOM_rmda },
3713 { ATOM_drms, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, 0 },
3714 { ATOM_sinf, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3715 { ATOM_schi, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3716 { ATOM_user, MP4_ReadBox_drms, MP4_FreeBox_Common, 0 },
3717 { ATOM_key, MP4_ReadBox_drms, MP4_FreeBox_Common, 0 },
3718 { ATOM_iviv, MP4_ReadBox_drms, MP4_FreeBox_Common, 0 },
3719 { ATOM_priv, MP4_ReadBox_drms, MP4_FreeBox_Common, 0 },
3720 { ATOM_frma, MP4_ReadBox_frma, MP4_FreeBox_Common, ATOM_sinf }, /* and rinf */
3721 { ATOM_frma, MP4_ReadBox_frma, MP4_FreeBox_Common, ATOM_wave }, /* flip4mac */
3722 { ATOM_skcr, MP4_ReadBox_skcr, MP4_FreeBox_Common, 0 },
3724 /* ilst meta tags */
3725 { ATOM_0xa9ART, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3726 { ATOM_0xa9alb, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3727 { ATOM_0xa9cmt, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3728 { ATOM_0xa9com, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3729 { ATOM_0xa9day, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3730 { ATOM_0xa9des, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3731 { ATOM_0xa9enc, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3732 { ATOM_0xa9gen, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3733 { ATOM_0xa9grp, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3734 { ATOM_0xa9lyr, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3735 { ATOM_0xa9nam, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3736 { ATOM_0xa9too, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3737 { ATOM_0xa9trk, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3738 { ATOM_0xa9wrt, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3739 { ATOM_aART, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3740 { ATOM_atID, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst }, /* iTunes */
3741 { ATOM_cnID, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst }, /* iTunes */
3742 { ATOM_covr, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_ilst },
3743 { ATOM_disk, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3744 { ATOM_flvr, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3745 { ATOM_gnre, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3746 { ATOM_rtng, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3747 { ATOM_trkn, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3748 { ATOM_xid_, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3751 { ATOM_0xa9ART, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3752 { ATOM_0xa9alb, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3753 { ATOM_0xa9ard, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3754 { ATOM_0xa9arg, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3755 { ATOM_0xa9aut, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3756 { ATOM_0xa9cak, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3757 { ATOM_0xa9cmt, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3758 { ATOM_0xa9con, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3759 { ATOM_0xa9com, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3760 { ATOM_0xa9cpy, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3761 { ATOM_0xa9day, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3762 { ATOM_0xa9des, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3763 { ATOM_0xa9dir, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3764 { ATOM_0xa9dis, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3765 { ATOM_0xa9dsa, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3766 { ATOM_0xa9fmt, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3767 { ATOM_0xa9gen, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3768 { ATOM_0xa9grp, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3769 { ATOM_0xa9hst, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3770 { ATOM_0xa9inf, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3771 { ATOM_0xa9isr, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3772 { ATOM_0xa9lab, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3773 { ATOM_0xa9lal, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3774 { ATOM_0xa9lnt, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3775 { ATOM_0xa9lyr, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3776 { ATOM_0xa9mak, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3777 { ATOM_0xa9mal, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3778 { ATOM_0xa9mod, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3779 { ATOM_0xa9nam, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3780 { ATOM_0xa9ope, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3781 { ATOM_0xa9phg, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3782 { ATOM_0xa9PRD, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3783 { ATOM_0xa9prd, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3784 { ATOM_0xa9prf, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3785 { ATOM_0xa9pub, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3786 { ATOM_0xa9req, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3787 { ATOM_0xa9sne, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3788 { ATOM_0xa9snm, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3789 { ATOM_0xa9sol, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3790 { ATOM_0xa9src, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3791 { ATOM_0xa9st3, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3792 { ATOM_0xa9swr, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3793 { ATOM_0xa9thx, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3794 { ATOM_0xa9too, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3795 { ATOM_0xa9trk, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3796 { ATOM_0xa9url, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3797 { ATOM_0xa9wrn, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3798 { ATOM_0xa9xpd, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3799 { ATOM_0xa9xyz, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3800 { ATOM_chpl, MP4_ReadBox_chpl, MP4_FreeBox_chpl, ATOM_udta }, /* nero unlabeled chapters list */
3801 { ATOM_name, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3802 { ATOM_vndr, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3803 { ATOM_SDLN, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3805 /* udta, non meta */
3806 { ATOM_tsel, MP4_ReadBox_tsel, MP4_FreeBox_Common, ATOM_udta },
3808 /* iTunes/Quicktime meta info */
3809 { ATOM_meta, MP4_ReadBox_meta, MP4_FreeBox_Common, 0 },
3810 { ATOM_data, MP4_ReadBox_data, MP4_FreeBox_data, 0 },
3812 /* found in smoothstreaming */
3813 { ATOM_traf, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_moof },
3814 { ATOM_mfra, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3815 { ATOM_mfhd, MP4_ReadBox_mfhd, MP4_FreeBox_Common, ATOM_moof },
3816 { ATOM_sidx, MP4_ReadBox_sidx, MP4_FreeBox_sidx, 0 },
3817 { ATOM_tfhd, MP4_ReadBox_tfhd, MP4_FreeBox_Common, ATOM_traf },
3818 { ATOM_trun, MP4_ReadBox_trun, MP4_FreeBox_trun, ATOM_traf },
3819 { ATOM_trex, MP4_ReadBox_trex, MP4_FreeBox_Common, ATOM_mvex },
3820 { ATOM_mehd, MP4_ReadBox_mehd, MP4_FreeBox_Common, ATOM_mvex },
3821 { ATOM_sdtp, MP4_ReadBox_sdtp, MP4_FreeBox_sdtp, 0 },
3822 { ATOM_tfra, MP4_ReadBox_tfra, MP4_FreeBox_tfra, ATOM_mfra },
3823 { ATOM_mfro, MP4_ReadBox_mfro, MP4_FreeBox_Common, ATOM_mfra },
3824 { ATOM_uuid, MP4_ReadBox_uuid, MP4_FreeBox_uuid, 0 },
3827 { 0, MP4_ReadBox_default, NULL, 0 }
3831 /*****************************************************************************
3832 * MP4_ReadBox : parse the actual box and the children
3833 * XXX : Do not go to the next box
3834 *****************************************************************************/
3835 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
3837 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
3838 unsigned int i_index;
3843 if( !MP4_ReadBoxCommon( p_stream, p_box ) )
3845 msg_Warn( p_stream, "cannot read one box" );
3849 if( !p_box->i_size )
3851 msg_Dbg( p_stream, "found an empty box (null size)" );
3855 p_box->p_father = p_father;
3857 /* Now search function to call */
3858 for( i_index = 0; ; i_index++ )
3860 if ( MP4_Box_Function[i_index].i_parent &&
3862 p_box->p_father->i_type != MP4_Box_Function[i_index].i_parent )
3865 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
3866 ( MP4_Box_Function[i_index].i_type == 0 ) )
3872 if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
3874 MP4_BoxFree( p_stream, p_box );
3878 p_box->pf_free = MP4_Box_Function[i_index].MP4_FreeBox_function;
3883 /*****************************************************************************
3884 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
3886 *****************************************************************************/
3887 void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
3894 for( p_child = p_box->p_first; p_child != NULL; )
3898 p_next = p_child->p_next;
3899 MP4_BoxFree( s, p_child );
3903 /* Now search function to call */
3904 if( p_box->data.p_payload )
3906 if (unlikely( p_box->pf_free == NULL ))
3908 /* Should not happen */
3909 if MP4_BOX_TYPE_ASCII()
3911 "cannot free box %4.4s, type unknown",
3912 (char*)&p_box->i_type );
3915 "cannot free box c%3.3s, type unknown",
3916 (char*)&p_box->i_type+1 );
3920 p_box->pf_free( p_box );
3922 free( p_box->data.p_payload );
3927 /* SmooBox is a very simple MP4 box, VLC specific, used only for the stream_filter to
3928 * send information to the demux. SmooBox is actually a simplified moov box (we wanted
3929 * to avoid the hassle of building a moov box at the stream_filter level) */
3930 MP4_Box_t *MP4_BoxGetSmooBox( stream_t *s )
3932 /* p_chunk is a virtual root container for the smoo box */
3936 p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
3937 if( unlikely( p_chunk == NULL ) )
3940 p_chunk->i_type = ATOM_root;
3941 p_chunk->i_shortsize = 1;
3943 p_smoo = MP4_ReadBox( s, p_chunk );
3944 if( !p_smoo || p_smoo->i_type != ATOM_uuid || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
3946 msg_Warn( s, "no smoo box found!");
3950 p_chunk->p_first = p_smoo;
3951 p_chunk->p_last = p_smoo;
3960 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
3962 /* p_chunk is a virtual root container for the moof and mdat boxes */
3964 MP4_Box_t *p_tmp_box = NULL;
3966 p_tmp_box = calloc( 1, sizeof( MP4_Box_t ) );
3967 if( unlikely( p_tmp_box == NULL ) )
3970 /* We might get a ftyp box or a SmooBox */
3971 MP4_ReadBoxCommon( s, p_tmp_box );
3973 if( (p_tmp_box->i_type == ATOM_uuid && !CmpUUID( &p_tmp_box->i_uuid, &SmooBoxUUID )) )
3976 return MP4_BoxGetSmooBox( s );
3978 else if( p_tmp_box->i_type == ATOM_ftyp )
3981 return MP4_BoxGetRoot( s );
3985 p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
3986 if( unlikely( p_chunk == NULL ) )
3989 p_chunk->i_type = ATOM_root;
3990 p_chunk->i_shortsize = 1;
3992 MP4_ReadBoxContainerChildren( s, p_chunk, ATOM_moof );
3994 p_tmp_box = p_chunk->p_first;
3997 p_chunk->i_size += p_tmp_box->i_size;
3998 p_tmp_box = p_tmp_box->p_next;
4004 /*****************************************************************************
4005 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
4006 *****************************************************************************
4007 * The first box is a virtual box "root" and is the father for all first
4008 * level boxes for the file, a sort of virtual contener
4009 *****************************************************************************/
4010 MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
4016 p_root = malloc( sizeof( MP4_Box_t ) );
4017 if( p_root == NULL )
4021 p_root->i_type = ATOM_root;
4022 p_root->i_shortsize = 1;
4023 /* could be a DASH stream for exemple, 0 means unknown or infinite size */
4025 CreateUUID( &p_root->i_uuid, p_root->i_type );
4027 p_root->data.p_payload = NULL;
4028 p_root->p_father = NULL;
4029 p_root->p_first = NULL;
4030 p_root->p_last = NULL;
4031 p_root->p_next = NULL;
4035 /* First get the moov */
4036 i_result = MP4_ReadBoxContainerChildren( p_stream, p_root, ATOM_moov );
4040 /* If there is a mvex box, it means fragmented MP4, and we're done */
4041 else if( MP4_BoxCount( p_root, "moov/mvex" ) > 0 )
4044 p_root->i_size = stream_Size( s );
4045 if( stream_Tell( s ) + 8 < stream_Size( s ) )
4047 /* Get the rest of the file */
4048 i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
4057 /* check if there is a cmov, if so replace
4058 compressed moov by uncompressed one */
4059 if( ( ( p_moov = MP4_BoxGet( p_root, "moov" ) ) &&
4060 ( p_cmov = MP4_BoxGet( p_root, "moov/cmov" ) ) ) ||
4061 ( ( p_moov = MP4_BoxGet( p_root, "foov" ) ) &&
4062 ( p_cmov = MP4_BoxGet( p_root, "foov/cmov" ) ) ) )
4064 /* rename the compressed moov as a box to skip */
4065 p_moov->i_type = ATOM_skip;
4067 /* get uncompressed p_moov */
4068 p_moov = p_cmov->data.p_cmov->p_moov;
4069 p_cmov->data.p_cmov->p_moov = NULL;
4071 /* make p_root father of this new moov */
4072 p_moov->p_father = p_root;
4074 /* insert this new moov box as first child of p_root */
4075 p_moov->p_next = p_root->p_first;
4076 p_root->p_first = p_moov;
4083 stream_Seek( p_stream, 0 );
4088 static void MP4_BoxDumpStructure_Internal( stream_t *s,
4089 MP4_Box_t *p_box, unsigned int i_level )
4092 uint32_t i_displayedtype = p_box->i_type;
4093 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype)[0] = 'c';
4097 msg_Dbg( s, "dumping root Box \"%4.4s\"",
4098 (char*)&i_displayedtype );
4103 if( i_level >= (sizeof(str) - 1)/4 )
4106 memset( str, ' ', sizeof(str) );
4107 for( unsigned i = 0; i < i_level; i++ )
4112 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
4113 "+ %4.4s size %"PRIu64" offset %ju%s",
4114 (char*)&i_displayedtype, p_box->i_size,
4115 (uintmax_t)p_box->i_pos,
4116 p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
4117 msg_Dbg( s, "%s", str );
4119 p_child = p_box->p_first;
4122 MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
4123 p_child = p_child->p_next;
4127 void MP4_BoxDumpStructure( stream_t *s, MP4_Box_t *p_box )
4129 MP4_BoxDumpStructure_Internal( s, p_box, 0 );
4133 /*****************************************************************************
4134 *****************************************************************************
4136 ** High level methods to acces an MP4 file
4138 *****************************************************************************
4139 *****************************************************************************/
4140 static void get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
4143 if( !*ppsz_path[0] )
4149 i_len = strcspn( *ppsz_path, "/[" );
4150 if( !i_len && **ppsz_path == '/' )
4154 *ppsz_token = strndup( *ppsz_path, i_len );
4155 if( unlikely(!*ppsz_token) )
4158 *ppsz_path += i_len;
4160 if( **ppsz_path == '[' )
4163 *pi_number = strtol( *ppsz_path, NULL, 10 );
4164 while( **ppsz_path && **ppsz_path != ']' )
4168 if( **ppsz_path == ']' )
4177 while( **ppsz_path == '/' )
4183 static void MP4_BoxGet_Internal( MP4_Box_t **pp_result,
4184 MP4_Box_t *p_box, const char *psz_fmt, va_list args)
4196 if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
4199 if( !psz_path || !psz_path[0] )
4206 // fprintf( stderr, "path:'%s'\n", psz_path );
4207 psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
4212 get_token( &psz_path, &psz_token, &i_number );
4213 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
4214 // psz_path,psz_token,i_number );
4222 if( !strcmp( psz_token, "/" ) )
4225 while( p_box && p_box->i_type != ATOM_root )
4227 p_box = p_box->p_father;
4235 if( !strcmp( psz_token, "." ) )
4240 if( !strcmp( psz_token, ".." ) )
4242 p_box = p_box->p_father;
4249 if( strlen( psz_token ) == 4 )
4252 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
4253 psz_token[2], psz_token[3] );
4254 p_box = p_box->p_first;
4261 if( p_box->i_type == i_fourcc )
4269 p_box = p_box->p_next;
4273 if( *psz_token == '\0' )
4275 p_box = p_box->p_first;
4287 p_box = p_box->p_next;
4292 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
4296 FREENULL( psz_token );
4308 /*****************************************************************************
4309 * MP4_BoxGet: find a box given a path relative to p_box
4310 *****************************************************************************
4311 * Path Format: . .. / as usual
4312 * [number] to specifie box number ex: trak[12]
4314 * ex: /moov/trak[12]
4316 *****************************************************************************/
4317 MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, const char *psz_fmt, ... )
4320 MP4_Box_t *p_result;
4322 va_start( args, psz_fmt );
4323 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
4329 /*****************************************************************************
4330 * MP4_BoxCount: count box given a path relative to p_box
4331 *****************************************************************************
4332 * Path Format: . .. / as usual
4333 * [number] to specifie box number ex: trak[12]
4335 * ex: /moov/trak[12]
4337 *****************************************************************************/
4338 int MP4_BoxCount( MP4_Box_t *p_box, const char *psz_fmt, ... )
4342 MP4_Box_t *p_result, *p_next;
4344 va_start( args, psz_fmt );
4345 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
4353 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
4355 if( p_next->i_type == p_result->i_type)