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 );
910 static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
914 char s_creation_time[128];
915 char s_modification_time[128];
916 char s_duration[128];
918 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t );
920 MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
922 if( p_box->data.p_mdhd->i_version )
924 MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
925 MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
926 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
927 MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
931 MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
932 MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
933 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
934 MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
937 MP4_GET2BYTES( i_language );
938 decodeQtLanguageCode( i_language, p_box->data.p_mdhd->rgs_language,
939 &p_box->data.p_mdhd->b_mac_encoding );
941 MP4_GET2BYTES( p_box->data.p_mdhd->i_quality );
944 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time, false );
945 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time, false );
946 MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration, true );
947 msg_Dbg( p_stream, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %3.3s",
950 (uint32_t)p_box->data.p_mdhd->i_timescale,
952 (char*) &p_box->data.p_mdhd->rgs_language );
954 MP4_READBOX_EXIT( 1 );
958 static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
961 VLC_UNUSED(i_reserved);
963 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
965 MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
967 MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
968 MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
970 MP4_GET4BYTES( i_reserved );
971 MP4_GET4BYTES( i_reserved );
972 MP4_GET4BYTES( i_reserved );
973 p_box->data.p_hdlr->psz_name = NULL;
977 uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_read + 1 );
978 if( unlikely( psz == NULL ) )
979 MP4_READBOX_EXIT( 0 );
981 /* Yes, I love .mp4 :( */
982 if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
987 MP4_GET1BYTE( i_len );
988 i_copy = __MIN( i_read, i_len );
990 memcpy( psz, p_peek, i_copy );
991 p_box->data.p_hdlr->psz_name[i_copy] = '\0';
995 memcpy( psz, p_peek, i_read );
996 p_box->data.p_hdlr->psz_name[i_read] = '\0';
1001 msg_Dbg( p_stream, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
1002 (char*)&p_box->data.p_hdlr->i_handler_type,
1003 p_box->data.p_hdlr->psz_name );
1006 MP4_READBOX_EXIT( 1 );
1009 static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
1011 FREENULL( p_box->data.p_hdlr->psz_name );
1014 static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
1016 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t );
1018 MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
1020 MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
1021 for( unsigned i = 0; i < 3; i++ )
1023 MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
1027 msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
1028 p_box->data.p_vmhd->i_graphics_mode,
1029 p_box->data.p_vmhd->i_opcolor[0],
1030 p_box->data.p_vmhd->i_opcolor[1],
1031 p_box->data.p_vmhd->i_opcolor[2] );
1033 MP4_READBOX_EXIT( 1 );
1036 static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
1038 MP4_READBOX_ENTER( MP4_Box_data_smhd_t );
1040 MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
1044 MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
1046 MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
1049 msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
1050 (float)p_box->data.p_smhd->i_balance / 256 );
1052 MP4_READBOX_EXIT( 1 );
1056 static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
1058 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t );
1060 MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
1062 MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
1063 MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
1065 MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
1066 MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
1068 MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
1071 msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1072 p_box->data.p_hmhd->i_max_PDU_size,
1073 p_box->data.p_hmhd->i_avg_PDU_size,
1074 p_box->data.p_hmhd->i_max_bitrate,
1075 p_box->data.p_hmhd->i_avg_bitrate );
1077 MP4_READBOX_EXIT( 1 );
1080 static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
1082 MP4_READBOX_ENTER( MP4_Box_data_url_t );
1084 MP4_GETVERSIONFLAGS( p_box->data.p_url );
1085 MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
1088 msg_Dbg( p_stream, "read box: \"url\" url: %s",
1089 p_box->data.p_url->psz_location );
1092 MP4_READBOX_EXIT( 1 );
1096 static void MP4_FreeBox_url( MP4_Box_t *p_box )
1098 FREENULL( p_box->data.p_url->psz_location );
1101 static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
1103 MP4_READBOX_ENTER( MP4_Box_data_urn_t );
1105 MP4_GETVERSIONFLAGS( p_box->data.p_urn );
1107 MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
1108 MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
1111 msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
1112 p_box->data.p_urn->psz_name,
1113 p_box->data.p_urn->psz_location );
1115 MP4_READBOX_EXIT( 1 );
1117 static void MP4_FreeBox_urn( MP4_Box_t *p_box )
1119 FREENULL( p_box->data.p_urn->psz_name );
1120 FREENULL( p_box->data.p_urn->psz_location );
1124 static int MP4_ReadBox_dref( stream_t *p_stream, MP4_Box_t *p_box )
1126 MP4_READBOX_ENTER( MP4_Box_data_dref_t );
1128 MP4_GETVERSIONFLAGS( p_box->data.p_dref );
1130 MP4_GET4BYTES( p_box->data.p_dref->i_entry_count );
1132 stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
1133 MP4_ReadBoxContainerRaw( p_stream, p_box );
1136 msg_Dbg( p_stream, "read box: \"dref\" entry-count %d",
1137 p_box->data.p_dref->i_entry_count );
1140 MP4_READBOX_EXIT( 1 );
1143 static void MP4_FreeBox_stts( MP4_Box_t *p_box )
1145 FREENULL( p_box->data.p_stts->pi_sample_count );
1146 FREENULL( p_box->data.p_stts->pi_sample_delta );
1149 static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
1151 MP4_READBOX_ENTER( MP4_Box_data_stts_t );
1153 MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1154 MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );
1156 p_box->data.p_stts->pi_sample_count =
1157 calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
1158 p_box->data.p_stts->pi_sample_delta =
1159 calloc( p_box->data.p_stts->i_entry_count, sizeof(int32_t) );
1160 if( p_box->data.p_stts->pi_sample_count == NULL
1161 || p_box->data.p_stts->pi_sample_delta == NULL )
1163 MP4_READBOX_EXIT( 0 );
1167 for( ; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
1169 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_count[i] );
1170 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_delta[i] );
1173 if ( i < p_box->data.p_stts->i_entry_count )
1174 p_box->data.p_stts->i_entry_count = i;
1177 msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
1178 p_box->data.p_stts->i_entry_count );
1181 MP4_READBOX_EXIT( 1 );
1185 static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
1187 FREENULL( p_box->data.p_ctts->pi_sample_count );
1188 FREENULL( p_box->data.p_ctts->pi_sample_offset );
1191 static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
1193 MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
1195 MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1197 MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );
1199 p_box->data.p_ctts->pi_sample_count =
1200 calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
1201 p_box->data.p_ctts->pi_sample_offset =
1202 calloc( p_box->data.p_ctts->i_entry_count, sizeof(int32_t) );
1203 if( ( p_box->data.p_ctts->pi_sample_count == NULL )
1204 || ( p_box->data.p_ctts->pi_sample_offset == NULL ) )
1206 MP4_READBOX_EXIT( 0 );
1210 for( ; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
1212 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_count[i] );
1213 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_offset[i] );
1215 if ( i < p_box->data.p_ctts->i_entry_count )
1216 p_box->data.p_ctts->i_entry_count = i;
1219 msg_Dbg( p_stream, "read box: \"ctts\" entry-count %d",
1220 p_box->data.p_ctts->i_entry_count );
1223 MP4_READBOX_EXIT( 1 );
1227 static int MP4_ReadLengthDescriptor( uint8_t **pp_peek, int64_t *i_read )
1230 unsigned int i_len = 0;
1237 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1238 } while( i_b&0x80 );
1243 static void MP4_FreeBox_esds( MP4_Box_t *p_box )
1245 FREENULL( p_box->data.p_esds->es_descriptor.psz_URL );
1246 if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1248 FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1249 FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1253 static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
1255 #define es_descriptor p_box->data.p_esds->es_descriptor
1257 unsigned int i_flags;
1258 unsigned int i_type;
1260 MP4_READBOX_ENTER( MP4_Box_data_esds_t );
1262 MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1265 MP4_GET1BYTE( i_type );
1266 if( i_type == 0x03 ) /* MP4ESDescrTag ISO/IEC 14496-1 8.3.3 */
1268 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1271 msg_Dbg( p_stream, "found esds MPEG4ESDescr (%dBytes)",
1275 MP4_GET2BYTES( es_descriptor.i_ES_ID );
1276 MP4_GET1BYTE( i_flags );
1277 es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1278 es_descriptor.b_url = ( (i_flags&0x40) != 0);
1279 es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1281 es_descriptor.i_stream_priority = i_flags&0x1f;
1282 if( es_descriptor.b_stream_dependence )
1284 MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1286 if( es_descriptor.b_url )
1290 MP4_GET1BYTE( i_len );
1291 i_len = __MIN(i_read, i_len);
1292 es_descriptor.psz_URL = malloc( i_len + 1 );
1293 if( es_descriptor.psz_URL )
1295 memcpy( es_descriptor.psz_URL, p_peek, i_len );
1296 es_descriptor.psz_URL[i_len] = 0;
1303 es_descriptor.psz_URL = NULL;
1305 if( es_descriptor.b_OCRstream )
1307 MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1309 MP4_GET1BYTE( i_type ); /* get next type */
1312 if( i_type != 0x04)/* MP4DecConfigDescrTag ISO/IEC 14496-1 8.3.4 */
1314 es_descriptor.p_decConfigDescr = NULL;
1315 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1318 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1321 msg_Dbg( p_stream, "found esds MP4DecConfigDescr (%dBytes)",
1325 es_descriptor.p_decConfigDescr =
1326 calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
1327 if( unlikely( es_descriptor.p_decConfigDescr == NULL ) )
1328 MP4_READBOX_EXIT( 0 );
1330 MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectProfileIndication );
1331 MP4_GET1BYTE( i_flags );
1332 es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1333 es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1334 MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1335 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1336 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1337 MP4_GET1BYTE( i_type );
1338 if( i_type != 0x05 )/* MP4DecSpecificDescrTag ISO/IEC 14496-1 8.3.5 */
1340 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1341 es_descriptor.p_decConfigDescr->p_decoder_specific_info = NULL;
1342 MP4_READBOX_EXIT( 1 );
1345 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1348 msg_Dbg( p_stream, "found esds MP4DecSpecificDescr (%dBytes)",
1351 if( i_len > i_read )
1352 MP4_READBOX_EXIT( 0 );
1354 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1355 es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1356 if( unlikely( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL ) )
1357 MP4_READBOX_EXIT( 0 );
1359 memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1362 MP4_READBOX_EXIT( 1 );
1363 #undef es_descriptor
1366 static void MP4_FreeBox_hvcC(MP4_Box_t *p_box )
1368 MP4_Box_data_hvcC_t *p_hvcC = p_box->data.p_hvcC;
1369 if( p_hvcC->i_hvcC > 0 ) FREENULL( p_hvcC->p_hvcC) ;
1372 static int MP4_ReadBox_hvcC( stream_t *p_stream, MP4_Box_t *p_box )
1374 MP4_Box_data_hvcC_t *p_hvcC;
1376 MP4_READBOX_ENTER( MP4_Box_data_hvcC_t );
1377 p_hvcC = p_box->data.p_hvcC;
1379 p_hvcC->i_hvcC = i_read;
1380 if( p_hvcC->i_hvcC > 0 )
1382 uint8_t * p = p_hvcC->p_hvcC = malloc( p_hvcC->i_hvcC );
1384 memcpy( p, p_peek, i_read );
1386 MP4_READBOX_EXIT( 1 );
1389 static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
1391 MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1394 if( p_avcC->i_avcC > 0 ) FREENULL( p_avcC->p_avcC );
1398 for( i = 0; i < p_avcC->i_sps; i++ )
1399 FREENULL( p_avcC->sps[i] );
1403 for( i = 0; i < p_avcC->i_pps; i++ )
1404 FREENULL( p_avcC->pps[i] );
1406 if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->sps );
1407 if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->i_sps_length );
1408 if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->pps );
1409 if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->i_pps_length );
1412 static int MP4_ReadBox_avcC( stream_t *p_stream, MP4_Box_t *p_box )
1414 MP4_Box_data_avcC_t *p_avcC;
1417 MP4_READBOX_ENTER( MP4_Box_data_avcC_t );
1418 p_avcC = p_box->data.p_avcC;
1420 p_avcC->i_avcC = i_read;
1421 if( p_avcC->i_avcC > 0 )
1423 uint8_t * p = p_avcC->p_avcC = malloc( p_avcC->i_avcC );
1425 memcpy( p, p_peek, i_read );
1428 MP4_GET1BYTE( p_avcC->i_version );
1429 MP4_GET1BYTE( p_avcC->i_profile );
1430 MP4_GET1BYTE( p_avcC->i_profile_compatibility );
1431 MP4_GET1BYTE( p_avcC->i_level );
1432 MP4_GET1BYTE( p_avcC->i_reserved1 );
1433 p_avcC->i_length_size = (p_avcC->i_reserved1&0x03) + 1;
1434 p_avcC->i_reserved1 >>= 2;
1436 MP4_GET1BYTE( p_avcC->i_reserved2 );
1437 p_avcC->i_sps = p_avcC->i_reserved2&0x1f;
1438 p_avcC->i_reserved2 >>= 5;
1440 if( p_avcC->i_sps > 0 )
1442 p_avcC->i_sps_length = calloc( p_avcC->i_sps, sizeof( uint16_t ) );
1443 p_avcC->sps = calloc( p_avcC->i_sps, sizeof( uint8_t* ) );
1445 if( !p_avcC->i_sps_length || !p_avcC->sps )
1448 for( i = 0; i < p_avcC->i_sps && i_read > 2; i++ )
1450 MP4_GET2BYTES( p_avcC->i_sps_length[i] );
1451 if ( p_avcC->i_sps_length[i] > i_read )
1453 p_avcC->sps[i] = malloc( p_avcC->i_sps_length[i] );
1454 if( p_avcC->sps[i] )
1455 memcpy( p_avcC->sps[i], p_peek, p_avcC->i_sps_length[i] );
1457 p_peek += p_avcC->i_sps_length[i];
1458 i_read -= p_avcC->i_sps_length[i];
1460 if ( i != p_avcC->i_sps )
1464 MP4_GET1BYTE( p_avcC->i_pps );
1465 if( p_avcC->i_pps > 0 )
1467 p_avcC->i_pps_length = calloc( p_avcC->i_pps, sizeof( uint16_t ) );
1468 p_avcC->pps = calloc( p_avcC->i_pps, sizeof( uint8_t* ) );
1470 if( !p_avcC->i_pps_length || !p_avcC->pps )
1473 for( i = 0; i < p_avcC->i_pps && i_read > 2; i++ )
1475 MP4_GET2BYTES( p_avcC->i_pps_length[i] );
1476 if( p_avcC->i_pps_length[i] > i_read )
1478 p_avcC->pps[i] = malloc( p_avcC->i_pps_length[i] );
1479 if( p_avcC->pps[i] )
1480 memcpy( p_avcC->pps[i], p_peek, p_avcC->i_pps_length[i] );
1482 p_peek += p_avcC->i_pps_length[i];
1483 i_read -= p_avcC->i_pps_length[i];
1485 if ( i != p_avcC->i_pps )
1490 "read box: \"avcC\" version=%d profile=0x%x level=0x%x length size=%d sps=%d pps=%d",
1491 p_avcC->i_version, p_avcC->i_profile, p_avcC->i_level,
1492 p_avcC->i_length_size,
1493 p_avcC->i_sps, p_avcC->i_pps );
1494 for( i = 0; i < p_avcC->i_sps; i++ )
1496 msg_Dbg( p_stream, " - sps[%d] length=%d",
1497 i, p_avcC->i_sps_length[i] );
1499 for( i = 0; i < p_avcC->i_pps; i++ )
1501 msg_Dbg( p_stream, " - pps[%d] length=%d",
1502 i, p_avcC->i_pps_length[i] );
1506 MP4_READBOX_EXIT( 1 );
1509 MP4_FreeBox_avcC( p_box );
1510 MP4_READBOX_EXIT( 0 );
1513 static int MP4_ReadBox_WMA2( stream_t *p_stream, MP4_Box_t *p_box )
1515 MP4_READBOX_ENTER( MP4_Box_data_WMA2_t );
1517 MP4_Box_data_WMA2_t *p_WMA2 = p_box->data.p_WMA2;
1519 MP4_GET2BYTESLE( p_WMA2->Format.wFormatTag );
1520 MP4_GET2BYTESLE( p_WMA2->Format.nChannels );
1521 MP4_GET4BYTESLE( p_WMA2->Format.nSamplesPerSec );
1522 MP4_GET4BYTESLE( p_WMA2->Format.nAvgBytesPerSec );
1523 MP4_GET2BYTESLE( p_WMA2->Format.nBlockAlign );
1524 MP4_GET2BYTESLE( p_WMA2->Format.wBitsPerSample );
1527 MP4_GET2BYTESLE( i_cbSize );
1529 if ( i_read < 0 || i_cbSize > i_read )
1532 p_WMA2->i_extra = i_cbSize;
1533 if ( p_WMA2->i_extra )
1535 p_WMA2->p_extra = malloc( p_WMA2->i_extra );
1536 if ( ! p_WMA2->p_extra )
1538 memcpy( p_WMA2->p_extra, p_peek, p_WMA2->i_extra );
1541 MP4_READBOX_EXIT( 1 );
1544 MP4_READBOX_EXIT( 0 );
1547 static void MP4_FreeBox_WMA2( MP4_Box_t *p_box )
1549 FREENULL( p_box->data.p_WMA2->p_extra );
1552 static int MP4_ReadBox_strf( stream_t *p_stream, MP4_Box_t *p_box )
1554 MP4_READBOX_ENTER( MP4_Box_data_strf_t );
1556 MP4_Box_data_strf_t *p_strf = p_box->data.p_strf;
1558 MP4_GET4BYTESLE( p_strf->bmiHeader.biSize );
1559 MP4_GET4BYTESLE( p_strf->bmiHeader.biWidth );
1560 MP4_GET4BYTESLE( p_strf->bmiHeader.biHeight );
1561 MP4_GET2BYTESLE( p_strf->bmiHeader.biPlanes );
1562 MP4_GET2BYTESLE( p_strf->bmiHeader.biBitCount );
1563 MP4_GETFOURCC( p_strf->bmiHeader.biCompression );
1564 MP4_GET4BYTESLE( p_strf->bmiHeader.biSizeImage );
1565 MP4_GET4BYTESLE( p_strf->bmiHeader.biXPelsPerMeter );
1566 MP4_GET4BYTESLE( p_strf->bmiHeader.biYPelsPerMeter );
1567 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrUsed );
1568 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrImportant );
1573 p_strf->i_extra = i_read;
1574 if ( p_strf->i_extra )
1576 p_strf->p_extra = malloc( p_strf->i_extra );
1577 if ( ! p_strf->p_extra )
1579 memcpy( p_strf->p_extra, p_peek, i_read );
1582 MP4_READBOX_EXIT( 1 );
1585 MP4_READBOX_EXIT( 0 );
1588 static void MP4_FreeBox_strf( MP4_Box_t *p_box )
1590 FREENULL( p_box->data.p_strf->p_extra );
1593 static int MP4_ReadBox_ASF( stream_t *p_stream, MP4_Box_t *p_box )
1595 MP4_READBOX_ENTER( MP4_Box_data_ASF_t );
1597 MP4_Box_data_ASF_t *p_asf = p_box->data.p_asf;
1600 MP4_READBOX_EXIT( 0 );
1602 MP4_GET1BYTE( p_asf->i_stream_number );
1603 /* remaining is unknown */
1605 MP4_READBOX_EXIT( 1 );
1608 static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
1610 MP4_READBOX_ENTER( MP4_Box_data_chan_t );
1611 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
1614 MP4_READBOX_EXIT( 0 );
1616 MP4_GET1BYTE( p_chan->i_version );
1617 MP4_GET3BYTES( p_chan->i_channels_flags );
1618 MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
1619 MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
1620 MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
1622 size_t i_descsize = 8 + 3 * sizeof(float);
1623 if ( (size_t)i_read < p_chan->layout.i_channels_description_count * i_descsize )
1624 MP4_READBOX_EXIT( 0 );
1626 p_chan->layout.p_descriptions =
1627 malloc( p_chan->layout.i_channels_description_count * i_descsize );
1629 if ( !p_chan->layout.p_descriptions )
1630 MP4_READBOX_EXIT( 0 );
1633 for( i=0; i<p_chan->layout.i_channels_description_count; i++ )
1637 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
1638 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
1639 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
1640 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
1641 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
1643 if ( i<p_chan->layout.i_channels_description_count )
1644 p_chan->layout.i_channels_description_count = i;
1648 "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
1649 p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
1650 p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
1652 MP4_READBOX_EXIT( 1 );
1655 static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
1657 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
1658 free( p_chan->layout.p_descriptions );
1661 static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
1663 MP4_READBOX_ENTER( MP4_Box_data_dec3_t );
1665 MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
1668 MP4_GET2BYTES( i_header );
1670 p_dec3->i_data_rate = i_header >> 3;
1671 p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
1672 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
1673 MP4_GET3BYTES( i_header );
1674 p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
1675 p_dec3->stream[i].i_bsid = ( i_header >> 17 ) & 0x01f;
1676 p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
1677 p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
1678 p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
1679 p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
1680 if (p_dec3->stream[i].i_num_dep_sub) {
1681 MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
1682 p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
1684 p_dec3->stream[i].i_chan_loc = 0;
1689 "read box: \"dec3\" bitrate %dkbps %d independant substreams",
1690 p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
1692 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
1694 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
1695 "num dependant subs=%d chan_loc=0x%x",
1696 i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
1697 p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
1699 MP4_READBOX_EXIT( 1 );
1702 static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
1704 MP4_Box_data_dac3_t *p_dac3;
1705 MP4_READBOX_ENTER( MP4_Box_data_dac3_t );
1707 p_dac3 = p_box->data.p_dac3;
1710 MP4_GET3BYTES( i_header );
1712 p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
1713 p_dac3->i_bsid = ( i_header >> 17 ) & 0x01f;
1714 p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
1715 p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
1716 p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
1717 p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
1721 "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
1722 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 );
1724 MP4_READBOX_EXIT( 1 );
1727 static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
1729 MP4_Box_data_dvc1_t *p_dvc1;
1731 MP4_READBOX_ENTER( MP4_Box_data_dvc1_t );
1732 p_dvc1 = p_box->data.p_dvc1;
1734 MP4_GET1BYTE( p_dvc1->i_profile_level ); /* profile is on 4bits, level 3bits */
1735 uint8_t i_profile = (p_dvc1->i_profile_level & 0xf0) >> 4;
1736 if( i_profile != 0x06 && i_profile != 0x0c )
1738 msg_Warn( p_stream, "unsupported VC-1 profile (%"PRIu8"), please report", i_profile );
1739 MP4_READBOX_EXIT( 0 );
1743 p_dvc1->i_vc1 = p_box->i_size - 7; /* Header + profile_level */
1745 if( p_dvc1->i_vc1 > 0 )
1747 uint8_t *p = p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 );
1749 memcpy( p, p_peek, i_read );
1754 "read box: \"dvc1\" profile=%"PRIu8" level=%i",
1755 i_profile, p_dvc1->i_profile_level & 0x0e >> 1 );
1758 MP4_READBOX_EXIT( 1 );
1761 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
1763 MP4_Box_data_enda_t *p_enda;
1764 MP4_READBOX_ENTER( MP4_Box_data_enda_t );
1766 p_enda = p_box->data.p_enda;
1768 MP4_GET2BYTES( p_enda->i_little_endian );
1772 "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
1774 MP4_READBOX_EXIT( 1 );
1777 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
1779 p_box->i_handler = ATOM_soun;
1780 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t );
1781 p_box->data.p_sample_soun->p_qt_description = NULL;
1783 /* Sanity check needed because the "wave" box does also contain an
1784 * "mp4a" box that we don't understand. */
1788 MP4_READBOX_EXIT( 1 );
1791 for( unsigned i = 0; i < 6 ; i++ )
1793 MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
1796 MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
1799 * XXX hack -> produce a copy of the nearly complete chunk
1801 p_box->data.p_sample_soun->i_qt_description = 0;
1802 p_box->data.p_sample_soun->p_qt_description = NULL;
1805 p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
1806 if( p_box->data.p_sample_soun->p_qt_description )
1808 p_box->data.p_sample_soun->i_qt_description = i_read;
1809 memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
1813 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
1814 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
1815 MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
1817 MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
1818 MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
1819 MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
1820 MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
1821 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
1822 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
1824 if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
1826 /* SoundDescriptionV1 */
1827 MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
1828 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
1829 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
1830 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
1834 "read box: \"soun\" qt3+ sample/packet=%d bytes/packet=%d "
1835 "bytes/frame=%d bytes/sample=%d",
1836 p_box->data.p_sample_soun->i_sample_per_packet,
1837 p_box->data.p_sample_soun->i_bytes_per_packet,
1838 p_box->data.p_sample_soun->i_bytes_per_frame,
1839 p_box->data.p_sample_soun->i_bytes_per_sample );
1841 stream_Seek( p_stream, p_box->i_pos +
1842 mp4_box_headersize( p_box ) + 44 );
1844 else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
1846 /* SoundDescriptionV2 */
1847 double f_sample_rate;
1849 uint32_t i_channel, i_extoffset, i_dummy32;
1852 if ( p_box->data.p_sample_soun->i_channelcount != 0x3 ||
1853 p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
1854 p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
1855 p_box->data.p_sample_soun->i_reserved3 != 0x0 ||
1856 p_box->data.p_sample_soun->i_sampleratehi != 0x1 ||//65536
1857 p_box->data.p_sample_soun->i_sampleratelo != 0x0 ) //remainder
1859 msg_Err( p_stream, "invalid stsd V2 box defaults" );
1860 MP4_READBOX_EXIT( 0 );
1864 MP4_GET4BYTES( i_extoffset ); /* offset to stsd extentions */
1865 MP4_GET8BYTES( i_dummy64 );
1866 memcpy( &f_sample_rate, &i_dummy64, 8 );
1867 msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
1868 p_box->data.p_sample_soun->i_sampleratehi = (int)f_sample_rate % BLOCK16x16;
1869 p_box->data.p_sample_soun->i_sampleratelo = f_sample_rate / BLOCK16x16;
1871 MP4_GET4BYTES( i_channel );
1872 p_box->data.p_sample_soun->i_channelcount = i_channel;
1874 MP4_GET4BYTES( i_dummy32 );
1875 if ( i_dummy32 != 0x7F000000 )
1877 msg_Err( p_stream, "invalid stsd V2 box" );
1878 MP4_READBOX_EXIT( 0 );
1881 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
1882 MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
1883 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
1884 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
1887 msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
1888 "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
1890 p_box->data.p_sample_soun->i_constbitsperchannel,
1891 p_box->data.p_sample_soun->i_formatflags,
1892 p_box->data.p_sample_soun->i_constbytesperaudiopacket,
1893 p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
1895 if ( i_extoffset < p_box->i_size )
1896 stream_Seek( p_stream, p_box->i_pos + i_extoffset );
1898 stream_Seek( p_stream, p_box->i_pos + p_box->i_size );
1902 p_box->data.p_sample_soun->i_sample_per_packet = 0;
1903 p_box->data.p_sample_soun->i_bytes_per_packet = 0;
1904 p_box->data.p_sample_soun->i_bytes_per_frame = 0;
1905 p_box->data.p_sample_soun->i_bytes_per_sample = 0;
1908 msg_Dbg( p_stream, "read box: \"soun\" mp4 or qt1/2 (rest=%"PRId64")",
1911 stream_Seek( p_stream, p_box->i_pos +
1912 mp4_box_headersize( p_box ) + 28 );
1915 if( p_box->i_type == ATOM_drms )
1917 msg_Warn( p_stream, "DRM protected streams are not supported." );
1918 MP4_READBOX_EXIT( 0 );
1921 if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
1923 /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
1924 p_box->data.p_sample_soun->i_channelcount = 1;
1927 /* Loads extensions */
1928 MP4_ReadBoxContainerRaw( p_stream, p_box ); /* esds/wave/... */
1931 msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
1932 "sample size %d sample rate %f",
1933 p_box->data.p_sample_soun->i_channelcount,
1934 p_box->data.p_sample_soun->i_samplesize,
1935 (float)p_box->data.p_sample_soun->i_sampleratehi +
1936 (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
1939 MP4_READBOX_EXIT( 1 );
1943 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
1945 FREENULL( p_box->data.p_sample_soun->p_qt_description );
1949 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
1951 p_box->i_handler = ATOM_vide;
1952 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t );
1954 for( unsigned i = 0; i < 6 ; i++ )
1956 MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
1959 MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
1962 * XXX hack -> produce a copy of the nearly complete chunk
1966 p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
1967 if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
1968 MP4_READBOX_EXIT( 0 );
1969 p_box->data.p_sample_vide->i_qt_image_description = i_read;
1970 memcpy( p_box->data.p_sample_vide->p_qt_image_description,
1975 p_box->data.p_sample_vide->i_qt_image_description = 0;
1976 p_box->data.p_sample_vide->p_qt_image_description = NULL;
1979 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
1980 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
1981 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
1983 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
1984 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
1986 MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
1987 MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
1989 MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
1990 MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
1992 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
1993 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
1996 MP4_READBOX_EXIT( 0 );
1997 memcpy( &p_box->data.p_sample_vide->i_compressorname, p_peek, 32 );
1998 p_peek += 32; i_read -= 32;
2000 MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
2001 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
2003 stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 78);
2005 if( p_box->i_type == ATOM_drmi )
2007 msg_Warn( p_stream, "DRM protected streams are not supported." );
2008 MP4_READBOX_EXIT( 0 );
2011 MP4_ReadBoxContainerRaw( p_stream, p_box );
2014 msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d",
2015 p_box->data.p_sample_vide->i_width,
2016 p_box->data.p_sample_vide->i_height,
2017 p_box->data.p_sample_vide->i_depth );
2020 MP4_READBOX_EXIT( 1 );
2024 void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
2026 FREENULL( p_box->data.p_sample_vide->p_qt_image_description );
2029 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
2031 stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
2032 MP4_ReadBoxContainerRaw( p_stream, p_box );
2036 static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
2040 p_box->i_handler = ATOM_text;
2041 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t );
2043 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2044 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2046 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2048 MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2053 /* FIXME search right signification */
2055 p_box->data.p_sample_text->i_justification_horizontal = 1;
2056 p_box->data.p_sample_text->i_justification_vertical = 1;
2058 case -1: // Flush Right
2059 p_box->data.p_sample_text->i_justification_horizontal = -1;
2060 p_box->data.p_sample_text->i_justification_vertical = -1;
2062 case -2: // Flush Left
2063 p_box->data.p_sample_text->i_justification_horizontal = 0;
2064 p_box->data.p_sample_text->i_justification_vertical = 0;
2066 case 0: // Flush Default
2068 p_box->data.p_sample_text->i_justification_horizontal = 1;
2069 p_box->data.p_sample_text->i_justification_vertical = -1;
2073 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[0] );
2074 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[1] );
2075 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[2] );
2076 p_box->data.p_sample_text->i_background_color[3] = 0xFF;
2078 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2079 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2080 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2081 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2084 msg_Dbg( p_stream, "read box: \"text\" in stsd text" );
2086 MP4_READBOX_EXIT( 1 );
2089 static int MP4_ReadBox_sample_tx3g( stream_t *p_stream, MP4_Box_t *p_box )
2091 p_box->i_handler = ATOM_text;
2092 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t );
2094 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2095 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2097 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2099 MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2101 MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_horizontal );
2102 MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_vertical );
2104 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[0] );
2105 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[1] );
2106 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[2] );
2107 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[3] );
2109 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2110 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2111 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2112 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2114 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved3 );
2116 MP4_GET2BYTES( p_box->data.p_sample_text->i_font_id );
2117 MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_face );
2118 MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_size );
2119 MP4_GET4BYTES( p_box->data.p_sample_text->i_font_color );
2122 msg_Dbg( p_stream, "read box: \"tx3g\" in stsd text" );
2124 MP4_READBOX_EXIT( 1 );
2129 /* We can't easily call it, and anyway ~ 20 bytes lost isn't a real problem */
2130 static void MP4_FreeBox_sample_text( MP4_Box_t *p_box )
2132 FREENULL( p_box->data.p_sample_text->psz_text_name );
2137 static int MP4_ReadBox_stsd( stream_t *p_stream, MP4_Box_t *p_box )
2140 MP4_READBOX_ENTER( MP4_Box_data_stsd_t );
2142 MP4_GETVERSIONFLAGS( p_box->data.p_stsd );
2144 MP4_GET4BYTES( p_box->data.p_stsd->i_entry_count );
2146 stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
2148 MP4_ReadBoxContainerRaw( p_stream, p_box );
2151 msg_Dbg( p_stream, "read box: \"stsd\" entry-count %d",
2152 p_box->data.p_stsd->i_entry_count );
2155 MP4_READBOX_EXIT( 1 );
2159 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
2161 MP4_READBOX_ENTER( MP4_Box_data_stsz_t );
2163 MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
2165 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
2166 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
2168 if( p_box->data.p_stsz->i_sample_size == 0 )
2170 p_box->data.p_stsz->i_entry_size =
2171 calloc( p_box->data.p_stsz->i_sample_count, sizeof(uint32_t) );
2172 if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
2173 MP4_READBOX_EXIT( 0 );
2175 for( unsigned int i = 0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
2177 MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
2181 p_box->data.p_stsz->i_entry_size = NULL;
2184 msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
2185 p_box->data.p_stsz->i_sample_size,
2186 p_box->data.p_stsz->i_sample_count );
2189 MP4_READBOX_EXIT( 1 );
2192 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
2194 FREENULL( p_box->data.p_stsz->i_entry_size );
2197 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
2199 FREENULL( p_box->data.p_stsc->i_first_chunk );
2200 FREENULL( p_box->data.p_stsc->i_samples_per_chunk );
2201 FREENULL( p_box->data.p_stsc->i_sample_description_index );
2204 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
2206 MP4_READBOX_ENTER( MP4_Box_data_stsc_t );
2208 MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
2210 MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
2212 p_box->data.p_stsc->i_first_chunk =
2213 calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2214 p_box->data.p_stsc->i_samples_per_chunk =
2215 calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2216 p_box->data.p_stsc->i_sample_description_index =
2217 calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2218 if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
2219 || p_box->data.p_stsc->i_samples_per_chunk == NULL
2220 || p_box->data.p_stsc->i_sample_description_index == NULL ) )
2222 MP4_READBOX_EXIT( 0 );
2225 for( unsigned int i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
2227 MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
2228 MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
2229 MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
2233 msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
2234 p_box->data.p_stsc->i_entry_count );
2237 MP4_READBOX_EXIT( 1 );
2240 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
2242 MP4_READBOX_ENTER( MP4_Box_data_co64_t );
2244 MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
2246 MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
2248 p_box->data.p_co64->i_chunk_offset =
2249 calloc( p_box->data.p_co64->i_entry_count, sizeof(uint64_t) );
2250 if( p_box->data.p_co64->i_chunk_offset == NULL )
2251 MP4_READBOX_EXIT( 0 );
2253 for( unsigned int i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
2255 if( p_box->i_type == ATOM_stco )
2261 MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2269 MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2274 msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
2275 p_box->data.p_co64->i_entry_count );
2278 MP4_READBOX_EXIT( 1 );
2281 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
2283 FREENULL( p_box->data.p_co64->i_chunk_offset );
2286 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
2288 MP4_READBOX_ENTER( MP4_Box_data_stss_t );
2290 MP4_GETVERSIONFLAGS( p_box->data.p_stss );
2292 MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
2294 p_box->data.p_stss->i_sample_number =
2295 calloc( p_box->data.p_stss->i_entry_count, sizeof(uint32_t) );
2296 if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
2297 MP4_READBOX_EXIT( 0 );
2300 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
2303 MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
2304 /* XXX in libmp4 sample begin at 0 */
2305 p_box->data.p_stss->i_sample_number[i]--;
2307 if ( i < p_box->data.p_stss->i_entry_count )
2308 p_box->data.p_stss->i_entry_count = i;
2311 msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
2312 p_box->data.p_stss->i_entry_count );
2315 MP4_READBOX_EXIT( 1 );
2318 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
2320 FREENULL( p_box->data.p_stss->i_sample_number );
2323 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
2325 FREENULL( p_box->data.p_stsh->i_shadowed_sample_number );
2326 FREENULL( p_box->data.p_stsh->i_sync_sample_number );
2329 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
2331 MP4_READBOX_ENTER( MP4_Box_data_stsh_t );
2333 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
2336 MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
2338 p_box->data.p_stsh->i_shadowed_sample_number =
2339 calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2340 p_box->data.p_stsh->i_sync_sample_number =
2341 calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2343 if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
2344 || p_box->data.p_stsh->i_sync_sample_number == NULL )
2346 MP4_READBOX_EXIT( 0 );
2350 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
2352 MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
2353 MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
2355 if ( i < p_box->data.p_stss->i_entry_count )
2356 p_box->data.p_stss->i_entry_count = i;
2359 msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
2360 p_box->data.p_stsh->i_entry_count );
2362 MP4_READBOX_EXIT( 1 );
2366 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
2368 MP4_READBOX_ENTER( MP4_Box_data_stdp_t );
2370 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
2372 p_box->data.p_stdp->i_priority =
2373 calloc( i_read / 2, sizeof(uint16_t) );
2375 if( unlikely( !p_box->data.p_stdp->i_priority ) )
2376 MP4_READBOX_EXIT( 0 );
2378 for( unsigned i = 0; i < i_read / 2 ; i++ )
2380 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
2384 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
2388 MP4_READBOX_EXIT( 1 );
2391 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
2393 FREENULL( p_box->data.p_stdp->i_priority );
2396 static void MP4_FreeBox_padb( MP4_Box_t *p_box )
2398 FREENULL( p_box->data.p_padb->i_reserved1 );
2399 FREENULL( p_box->data.p_padb->i_pad2 );
2400 FREENULL( p_box->data.p_padb->i_reserved2 );
2401 FREENULL( p_box->data.p_padb->i_pad1 );
2404 static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
2408 MP4_READBOX_ENTER( MP4_Box_data_padb_t );
2410 MP4_GETVERSIONFLAGS( p_box->data.p_padb );
2412 MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
2413 count = (p_box->data.p_padb->i_sample_count + 1) / 2;
2415 p_box->data.p_padb->i_reserved1 = calloc( count, sizeof(uint16_t) );
2416 p_box->data.p_padb->i_pad2 = calloc( count, sizeof(uint16_t) );
2417 p_box->data.p_padb->i_reserved2 = calloc( count, sizeof(uint16_t) );
2418 p_box->data.p_padb->i_pad1 = calloc( count, sizeof(uint16_t) );
2419 if( p_box->data.p_padb->i_reserved1 == NULL
2420 || p_box->data.p_padb->i_pad2 == NULL
2421 || p_box->data.p_padb->i_reserved2 == NULL
2422 || p_box->data.p_padb->i_pad1 == NULL )
2424 MP4_READBOX_EXIT( 0 );
2427 for( unsigned int i = 0; i < i_read / 2 ; i++ )
2431 MP4_READBOX_EXIT( 0 );
2433 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
2434 p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
2435 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
2436 p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;
2438 p_peek += 1; i_read -= 1;
2442 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
2446 MP4_READBOX_EXIT( 1 );
2449 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
2451 FREENULL( p_box->data.p_elst->i_segment_duration );
2452 FREENULL( p_box->data.p_elst->i_media_time );
2453 FREENULL( p_box->data.p_elst->i_media_rate_integer );
2454 FREENULL( p_box->data.p_elst->i_media_rate_fraction );
2457 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
2459 MP4_READBOX_ENTER( MP4_Box_data_elst_t );
2461 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
2464 MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
2466 p_box->data.p_elst->i_segment_duration =
2467 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
2468 p_box->data.p_elst->i_media_time =
2469 calloc( p_box->data.p_elst->i_entry_count, sizeof(int64_t) );
2470 p_box->data.p_elst->i_media_rate_integer =
2471 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
2472 p_box->data.p_elst->i_media_rate_fraction =
2473 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
2474 if( p_box->data.p_elst->i_segment_duration == NULL
2475 || p_box->data.p_elst->i_media_time == NULL
2476 || p_box->data.p_elst->i_media_rate_integer == NULL
2477 || p_box->data.p_elst->i_media_rate_fraction == NULL )
2479 MP4_READBOX_EXIT( 0 );
2483 for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
2485 if( p_box->data.p_elst->i_version == 1 )
2489 MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
2491 MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
2497 MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
2499 MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
2500 p_box->data.p_elst->i_media_time[i] = (int32_t)p_box->data.p_elst->i_media_time[i];
2503 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
2504 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
2506 if ( i < p_box->data.p_elst->i_entry_count )
2507 p_box->data.p_elst->i_entry_count = i;
2509 msg_Dbg( p_stream, "read box: \"elst\" entry-count %lu",
2510 (unsigned long)p_box->data.p_elst->i_entry_count );
2512 MP4_READBOX_EXIT( 1 );
2515 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
2517 uint16_t i_language;
2520 MP4_READBOX_ENTER( MP4_Box_data_cprt_t );
2522 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
2524 MP4_GET2BYTES( i_language );
2525 decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
2527 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
2530 msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
2531 p_box->data.p_cprt->rgs_language,
2532 p_box->data.p_cprt->psz_notice );
2535 MP4_READBOX_EXIT( 1 );
2538 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
2540 FREENULL( p_box->data.p_cprt->psz_notice );
2544 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
2546 MP4_READBOX_ENTER( MP4_Box_data_dcom_t );
2548 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
2551 "read box: \"dcom\" compression algorithm : %4.4s",
2552 (char*)&p_box->data.p_dcom->i_algorithm );
2554 MP4_READBOX_EXIT( 1 );
2557 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
2559 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t );
2561 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
2563 p_box->data.p_cmvd->i_compressed_size = i_read;
2565 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
2566 MP4_READBOX_EXIT( 0 );
2568 /* now copy compressed data */
2569 memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
2571 p_box->data.p_cmvd->b_compressed = 1;
2574 msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
2575 p_box->data.p_cmvd->i_compressed_size );
2578 MP4_READBOX_EXIT( 1 );
2580 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
2582 FREENULL( p_box->data.p_cmvd->p_data );
2586 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
2592 stream_t *p_stream_memory;
2598 if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
2601 if( !p_box->p_father ||
2602 ( p_box->p_father->i_type != ATOM_moov &&
2603 p_box->p_father->i_type != ATOM_foov ) )
2605 msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
2609 if( !MP4_ReadBoxContainer( p_stream, p_box ) )
2614 if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
2615 ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
2616 p_cmvd->data.p_cmvd->p_data == NULL )
2618 msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
2622 if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
2624 msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
2625 "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
2630 msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
2634 /* decompress data */
2635 /* allocate a new buffer */
2636 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
2638 /* init default structures */
2639 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
2640 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
2641 z_data.next_out = p_data;
2642 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
2643 z_data.zalloc = (alloc_func)Z_NULL;
2644 z_data.zfree = (free_func)Z_NULL;
2645 z_data.opaque = (voidpf)Z_NULL;
2648 if( inflateInit( &z_data ) != Z_OK )
2650 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
2656 i_result = inflate( &z_data, Z_NO_FLUSH );
2657 if( i_result != Z_OK && i_result != Z_STREAM_END )
2659 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
2664 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
2666 msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
2669 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
2672 if( inflateEnd( &z_data ) != Z_OK )
2674 msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
2678 free( p_cmvd->data.p_cmvd->p_data );
2679 p_cmvd->data.p_cmvd->p_data = p_data;
2680 p_cmvd->data.p_cmvd->b_compressed = 0;
2682 msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
2684 /* now create a memory stream */
2686 stream_MemoryNew( VLC_OBJECT(p_stream), p_cmvd->data.p_cmvd->p_data,
2687 p_cmvd->data.p_cmvd->i_uncompressed_size, true );
2689 /* and read uncompressd moov */
2690 p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
2692 stream_Delete( p_stream_memory );
2695 msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
2698 return p_box->data.p_cmov->p_moov ? 1 : 0;
2699 #endif /* HAVE_ZLIB_H */
2702 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
2705 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t );
2707 MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
2708 MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
2709 MP4_GET4BYTES( i_len );
2714 p_box->data.p_rdrf->psz_ref = malloc( i_len );
2715 if( p_box->data.p_rdrf->psz_ref == NULL )
2716 MP4_READBOX_EXIT( 0 );
2719 for( unsigned i = 0; i < i_len; i++ )
2721 MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
2723 p_box->data.p_rdrf->psz_ref[i_len] = '\0';
2727 p_box->data.p_rdrf->psz_ref = NULL;
2732 "read box: \"rdrf\" type:%4.4s ref %s",
2733 (char*)&p_box->data.p_rdrf->i_ref_type,
2734 p_box->data.p_rdrf->psz_ref );
2736 MP4_READBOX_EXIT( 1 );
2739 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
2741 FREENULL( p_box->data.p_rdrf->psz_ref );
2745 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
2747 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t );
2749 MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
2751 MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
2755 "read box: \"rmdr\" rate:%d",
2756 p_box->data.p_rmdr->i_rate );
2758 MP4_READBOX_EXIT( 1 );
2761 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
2763 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t );
2765 MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
2769 "read box: \"rmqu\" quality:%d",
2770 p_box->data.p_rmqu->i_quality );
2772 MP4_READBOX_EXIT( 1 );
2775 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
2777 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t );
2778 MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
2780 MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
2781 MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
2782 MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
2783 MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
2787 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
2788 (char*)&p_box->data.p_rmvc->i_gestaltType,
2789 p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
2790 p_box->data.p_rmvc->i_checkType );
2793 MP4_READBOX_EXIT( 1 );
2796 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
2798 MP4_READBOX_ENTER( MP4_Box_data_frma_t );
2800 MP4_GETFOURCC( p_box->data.p_frma->i_type );
2803 msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
2804 (char *)&p_box->data.p_frma->i_type );
2807 MP4_READBOX_EXIT( 1 );
2810 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
2812 MP4_READBOX_ENTER( MP4_Box_data_skcr_t );
2814 MP4_GET4BYTES( p_box->data.p_skcr->i_init );
2815 MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
2816 MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
2819 msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
2820 p_box->data.p_skcr->i_init,
2821 p_box->data.p_skcr->i_encr,
2822 p_box->data.p_skcr->i_decr );
2825 MP4_READBOX_EXIT( 1 );
2828 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
2831 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
2832 * so unless data decrypt itself by magic, there will be no playback,
2833 * but we never know... */
2834 msg_Warn( p_stream, "DRM protected streams are not supported." );
2838 static int MP4_ReadBox_String( stream_t *p_stream, MP4_Box_t *p_box )
2840 MP4_READBOX_ENTER( MP4_Box_data_string_t );
2842 p_box->data.p_string->psz_text = malloc( p_box->i_size + 1 - 8 ); /* +\0, -name, -size */
2843 if( p_box->data.p_string->psz_text == NULL )
2844 MP4_READBOX_EXIT( 0 );
2846 memcpy( p_box->data.p_string->psz_text, p_peek, p_box->i_size - 8 );
2847 p_box->data.p_string->psz_text[p_box->i_size - 8] = '\0';
2850 msg_Dbg( p_stream, "read box: \"%4.4s\" text=`%s'", (char *) & p_box->i_type,
2851 p_box->data.p_string->psz_text );
2853 MP4_READBOX_EXIT( 1 );
2856 static void MP4_FreeBox_String( MP4_Box_t *p_box )
2858 FREENULL( p_box->data.p_string->psz_text );
2861 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
2863 MP4_READBOX_ENTER( MP4_Box_data_data_t );
2864 MP4_Box_data_data_t *p_data = p_box->data.p_data;
2866 if ( i_read < 8 || i_read - 8 > UINT32_MAX )
2867 MP4_READBOX_EXIT( 0 );
2870 MP4_GET1BYTE( i_type );
2874 msg_Dbg( p_stream, "skipping unknown 'data' atom with type %"PRIu8, i_type );
2876 MP4_READBOX_EXIT( 0 );
2879 MP4_GET3BYTES( p_data->e_wellknowntype );
2880 MP4_GET2BYTES( p_data->locale.i_country );
2881 MP4_GET2BYTES( p_data->locale.i_language );
2883 msg_Dbg( p_stream, "read 'data' atom: knowntype=%"PRIu32", country=%"PRIu16" lang=%"PRIu16
2884 ", size %"PRId64" bytes", p_data->e_wellknowntype,
2885 p_data->locale.i_country, p_data->locale.i_language, i_read );
2887 p_box->data.p_data->p_blob = malloc( i_read );
2888 if ( !p_box->data.p_data->p_blob )
2889 MP4_READBOX_EXIT( 0 );
2891 p_box->data.p_data->i_blob = i_read;
2892 memcpy( p_box->data.p_data->p_blob, p_peek, i_read);
2894 MP4_READBOX_EXIT( 1 );
2897 static void MP4_FreeBox_data( MP4_Box_t *p_box )
2899 free( p_box->data.p_data->p_blob );
2902 static int MP4_ReadBox_Metadata( stream_t *p_stream, MP4_Box_t *p_box )
2904 const uint8_t *p_peek;
2905 if ( stream_Peek( p_stream, &p_peek, 16 ) < 16 )
2907 if ( stream_Read( p_stream, NULL, 8 ) < 8 )
2909 return MP4_ReadBoxContainerChildren( p_stream, p_box, ATOM_data );
2912 /* Chapter support */
2913 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
2915 MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
2916 for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
2917 free( p_chpl->chapter[i].psz_name );
2920 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
2922 MP4_Box_data_chpl_t *p_chpl;
2924 VLC_UNUSED(i_dummy);
2926 MP4_READBOX_ENTER( MP4_Box_data_chpl_t );
2928 p_chpl = p_box->data.p_chpl;
2930 MP4_GETVERSIONFLAGS( p_chpl );
2932 MP4_GET4BYTES( i_dummy );
2934 MP4_GET1BYTE( p_chpl->i_chapter );
2936 for( i = 0; i < p_chpl->i_chapter; i++ )
2943 MP4_GET8BYTES( i_start );
2944 MP4_GET1BYTE( i_len );
2946 p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
2947 if( !p_chpl->chapter[i].psz_name )
2948 MP4_READBOX_EXIT( 0 );
2950 i_copy = __MIN( i_len, i_read );
2952 memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
2953 p_chpl->chapter[i].psz_name[i_copy] = '\0';
2954 p_chpl->chapter[i].i_start = i_start;
2960 if ( i != p_chpl->i_chapter )
2961 p_chpl->i_chapter = i;
2963 /* Bubble sort by increasing start date */
2966 for( i = 0; i < p_chpl->i_chapter - 1; i++ )
2968 if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
2970 char *psz = p_chpl->chapter[i+1].psz_name;
2971 int64_t i64 = p_chpl->chapter[i+1].i_start;
2973 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
2974 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
2976 p_chpl->chapter[i].psz_name = psz;
2977 p_chpl->chapter[i].i_start = i64;
2986 msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
2987 p_chpl->i_chapter );
2989 MP4_READBOX_EXIT( 1 );
2992 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
2994 MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t );
2996 p_box->data.p_tref_generic->i_track_ID = NULL;
2997 p_box->data.p_tref_generic->i_entry_count = i_read / sizeof(uint32_t);
2998 if( p_box->data.p_tref_generic->i_entry_count > 0 )
2999 p_box->data.p_tref_generic->i_track_ID = calloc( p_box->data.p_tref_generic->i_entry_count, sizeof(uint32_t) );
3000 if( p_box->data.p_tref_generic->i_track_ID == NULL )
3001 MP4_READBOX_EXIT( 0 );
3003 for( unsigned i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
3005 MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
3008 msg_Dbg( p_stream, "read box: \"chap\" %d references",
3009 p_box->data.p_tref_generic->i_entry_count );
3012 MP4_READBOX_EXIT( 1 );
3014 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
3016 FREENULL( p_box->data.p_tref_generic->i_track_ID );
3019 static int MP4_ReadBox_keys( stream_t *p_stream, MP4_Box_t *p_box )
3021 MP4_READBOX_ENTER( MP4_Box_data_keys_t );
3024 MP4_READBOX_EXIT( 0 );
3027 MP4_GET4BYTES( i_count ); /* reserved + flags */
3029 MP4_READBOX_EXIT( 0 );
3031 MP4_GET4BYTES( i_count );
3032 p_box->data.p_keys->p_entries = calloc( i_count, sizeof(*p_box->data.p_keys->p_entries) );
3033 if ( !p_box->data.p_keys->p_entries )
3034 MP4_READBOX_EXIT( 0 );
3035 p_box->data.p_keys->i_entry_count = i_count;
3038 for( ; i < i_count; i++ )
3043 MP4_GET4BYTES( i_keysize );
3044 if ( (i_keysize < 8) || (i_keysize - 4 > i_read) )
3046 MP4_GETFOURCC( p_box->data.p_keys->p_entries[i].i_namespace );
3048 p_box->data.p_keys->p_entries[i].psz_value = malloc( i_keysize + 1 );
3049 if ( !p_box->data.p_keys->p_entries[i].psz_value )
3051 memcpy( p_box->data.p_keys->p_entries[i].psz_value, p_peek, i_keysize );
3052 p_box->data.p_keys->p_entries[i].psz_value[i_keysize] = 0;
3053 p_peek += i_keysize;
3054 i_read -= i_keysize;
3055 #ifdef MP4_ULTRA_VERBOSE
3056 msg_Dbg( p_stream, "read box: \"keys\": %u '%s'", i + 1,
3057 p_box->data.p_keys->p_entries[i].p_value );
3061 p_box->data.p_keys->i_entry_count = i;
3063 MP4_READBOX_EXIT( 1 );
3066 static void MP4_FreeBox_keys( MP4_Box_t *p_box )
3068 for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
3069 free( p_box->data.p_keys->p_entries[i].psz_value );
3070 free( p_box->data.p_keys->p_entries );
3073 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
3075 uint8_t meta_data[8];
3076 int i_actually_read;
3078 // skip over box header
3079 i_actually_read = stream_Read( p_stream, meta_data, 8 );
3080 if( i_actually_read < 8 )
3083 if ( p_box->p_father && p_box->p_father->i_type == ATOM_udta ) /* itunes udta/meta */
3085 /* meta content starts with a 4 byte version/flags value (should be 0) */
3086 i_actually_read = stream_Read( p_stream, meta_data, 4 );
3087 if( i_actually_read < 4 || memcmp( meta_data, "\0\0\0", 4 ) )
3091 if ( !MP4_ReadBoxContainerChildren( p_stream, p_box, ATOM_hdlr ) )
3095 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box, "hdlr" );
3096 if ( !p_hdlr || !BOXDATA(p_hdlr) ||
3097 ( BOXDATA(p_hdlr)->i_handler_type != HANDLER_mdta &&
3098 BOXDATA(p_hdlr)->i_handler_type != HANDLER_mdir ) ||
3099 BOXDATA(p_hdlr)->i_version != 0 )
3102 /* then it behaves like a container */
3103 return MP4_ReadBoxContainerRaw( p_stream, p_box );
3106 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3109 VLC_UNUSED(i_unused);
3111 MP4_READBOX_ENTER( MP4_Box_data_iods_t );
3112 MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3114 MP4_GET1BYTE( i_unused ); /* tag */
3115 MP4_GET1BYTE( i_unused ); /* length */
3117 MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3118 are used for other flags */
3119 MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3120 MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3121 MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3122 MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3123 MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3127 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3128 "visual: %i, graphics: %i",
3129 p_box->data.p_iods->i_object_descriptor >> 6,
3130 p_box->data.p_iods->i_OD_profile_level,
3131 p_box->data.p_iods->i_scene_profile_level,
3132 p_box->data.p_iods->i_audio_profile_level,
3133 p_box->data.p_iods->i_visual_profile_level,
3134 p_box->data.p_iods->i_graphics_profile_level );
3137 MP4_READBOX_EXIT( 1 );
3140 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3142 MP4_READBOX_ENTER( MP4_Box_data_pasp_t );
3144 MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
3145 MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
3149 "read box: \"paps\" %dx%d",
3150 p_box->data.p_pasp->i_horizontal_spacing,
3151 p_box->data.p_pasp->i_vertical_spacing);
3154 MP4_READBOX_EXIT( 1 );
3157 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
3159 MP4_READBOX_ENTER( MP4_Box_data_mehd_t );
3161 MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
3162 if( p_box->data.p_mehd->i_version == 1 )
3163 MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
3164 else /* version == 0 */
3165 MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
3169 "read box: \"mehd\" frag dur. %"PRIu64"",
3170 p_box->data.p_mehd->i_fragment_duration );
3173 MP4_READBOX_EXIT( 1 );
3176 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
3178 MP4_READBOX_ENTER( MP4_Box_data_trex_t );
3179 MP4_GETVERSIONFLAGS( p_box->data.p_trex );
3181 MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
3182 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
3183 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
3184 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
3185 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
3189 "read box: \"trex\" trackID: %"PRIu32"",
3190 p_box->data.p_trex->i_track_ID );
3193 MP4_READBOX_EXIT( 1 );
3196 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
3198 uint32_t i_sample_count;
3199 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t );
3200 MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
3201 MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
3202 i_sample_count = i_read;
3204 p_sdtp->p_sample_table = calloc( i_sample_count, 1 );
3206 if( !p_sdtp->p_sample_table )
3207 MP4_READBOX_EXIT( 0 );
3209 for( uint32_t i = 0; i < i_sample_count; i++ )
3210 MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
3213 msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
3214 if ( i_sample_count > 3 )
3216 "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
3217 p_sdtp->p_sample_table[0],
3218 p_sdtp->p_sample_table[1],
3219 p_sdtp->p_sample_table[2],
3220 p_sdtp->p_sample_table[3] );
3223 MP4_READBOX_EXIT( 1 );
3226 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
3228 FREENULL( p_box->data.p_sdtp->p_sample_table );
3231 static int MP4_ReadBox_tsel( stream_t *p_stream, MP4_Box_t *p_box )
3233 MP4_READBOX_ENTER( MP4_Box_data_tsel_t );
3235 MP4_GET4BYTES( i_version );
3236 if ( i_version != 0 || i_read < 4 )
3237 MP4_READBOX_EXIT( 0 );
3238 MP4_GET4BYTES( p_box->data.p_tsel->i_switch_group );
3239 /* ignore list of attributes as es are present before switch */
3240 MP4_READBOX_EXIT( 1 );
3243 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
3245 MP4_READBOX_ENTER( MP4_Box_data_mfro_t );
3247 MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
3248 MP4_GET4BYTES( p_box->data.p_mfro->i_size );
3252 "read box: \"mfro\" size: %"PRIu32"",
3253 p_box->data.p_mfro->i_size);
3256 MP4_READBOX_EXIT( 1 );
3259 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
3261 uint32_t i_number_of_entries;
3262 MP4_READBOX_ENTER( MP4_Box_data_tfra_t );
3263 MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
3264 MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
3266 MP4_GET4BYTES( p_tfra->i_track_ID );
3267 uint32_t i_lengths = 0;
3268 MP4_GET4BYTES( i_lengths );
3269 MP4_GET4BYTES( p_tfra->i_number_of_entries );
3270 i_number_of_entries = p_tfra->i_number_of_entries;
3271 p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
3272 p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
3273 p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
3275 size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
3276 p_tfra->p_time = calloc( i_number_of_entries, size );
3277 p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
3279 size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
3280 p_tfra->p_traf_number = calloc( i_number_of_entries, size );
3281 size = 1 + p_tfra->i_length_size_of_trun_num;
3282 p_tfra->p_trun_number = calloc( i_number_of_entries, size );
3283 size = 1 + p_tfra->i_length_size_of_sample_num;
3284 p_tfra->p_sample_number = calloc( i_number_of_entries, size );
3286 if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
3287 || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
3290 int i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
3291 + p_tfra->i_length_size_of_trun_num
3292 + p_tfra->i_length_size_of_sample_num;
3295 for( i = 0; i < i_number_of_entries; i++ )
3298 if( p_tfra->i_version == 1 )
3300 if ( i_read < i_fields_length + 16 )
3302 MP4_GET8BYTES( p_tfra->p_time[i*2] );
3303 MP4_GET8BYTES( p_tfra->p_moof_offset[i*2] );
3307 if ( i_read < i_fields_length + 8 )
3309 MP4_GET4BYTES( p_tfra->p_time[i] );
3310 MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
3312 switch (p_tfra->i_length_size_of_traf_num)
3315 MP4_GET1BYTE( p_tfra->p_traf_number[i] );
3318 MP4_GET2BYTES( p_tfra->p_traf_number[i*2] );
3321 MP4_GET3BYTES( p_tfra->p_traf_number[i*3] );
3324 MP4_GET4BYTES( p_tfra->p_traf_number[i*4] );
3330 switch (p_tfra->i_length_size_of_trun_num)
3333 MP4_GET1BYTE( p_tfra->p_trun_number[i] );
3336 MP4_GET2BYTES( p_tfra->p_trun_number[i*2] );
3339 MP4_GET3BYTES( p_tfra->p_trun_number[i*3] );
3342 MP4_GET4BYTES( p_tfra->p_trun_number[i*4] );
3348 switch (p_tfra->i_length_size_of_sample_num)
3351 MP4_GET1BYTE( p_tfra->p_sample_number[i] );
3354 MP4_GET2BYTES( p_tfra->p_sample_number[i*2] );
3357 MP4_GET3BYTES( p_tfra->p_sample_number[i*3] );
3360 MP4_GET4BYTES( p_tfra->p_sample_number[i*4] );
3366 if ( i < i_number_of_entries )
3367 i_number_of_entries = i;
3370 if( p_tfra->i_version == 0 )
3372 msg_Dbg( p_stream, "time[0]: %"PRIu32", moof_offset[0]: %"PRIx32"",
3373 p_tfra->p_time[0], p_tfra->p_moof_offset[0] );
3375 msg_Dbg( p_stream, "time[1]: %"PRIu32", moof_offset[1]: %"PRIx32"",
3376 p_tfra->p_time[1], p_tfra->p_moof_offset[1] );
3380 msg_Dbg( p_stream, "time[0]: %"PRIu64", moof_offset[0]: %"PRIx64"",
3381 ((uint64_t *)(p_tfra->p_time))[0],
3382 ((uint64_t *)(p_tfra->p_moof_offset))[0] );
3384 msg_Dbg( p_stream, "time[1]: %"PRIu64", moof_offset[1]: %"PRIx64"",
3385 ((uint64_t *)(p_tfra->p_time))[1],
3386 ((uint64_t *)(p_tfra->p_moof_offset))[1] );
3389 msg_Dbg( p_stream, "number_of_entries is %"PRIu32"", i_number_of_entries );
3390 msg_Dbg( p_stream, "track ID is: %"PRIu32"", p_tfra->i_track_ID );
3393 MP4_READBOX_EXIT( 1 );
3395 MP4_READBOX_EXIT( 0 );
3398 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
3400 FREENULL( p_box->data.p_tfra->p_time );
3401 FREENULL( p_box->data.p_tfra->p_moof_offset );
3402 FREENULL( p_box->data.p_tfra->p_traf_number );
3403 FREENULL( p_box->data.p_tfra->p_trun_number );
3404 FREENULL( p_box->data.p_tfra->p_sample_number );
3409 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
3411 if( !p_box->p_father )
3415 if( p_box->p_father->i_type == ATOM_stsd )
3417 MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
3420 if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
3421 (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
3425 switch( p_hdlr->data.p_hdlr->i_handler_type )
3428 return MP4_ReadBox_sample_soun( p_stream, p_box );
3430 return MP4_ReadBox_sample_vide( p_stream, p_box );
3432 return MP4_ReadBox_sample_text( p_stream, p_box );
3435 return MP4_ReadBox_sample_tx3g( p_stream, p_box );
3438 "unknown handler type in stsd (incompletely loaded)" );
3444 if MP4_BOX_TYPE_ASCII()
3446 "unknown box type %4.4s (incompletely loaded)",
3447 (char*)&p_box->i_type );
3450 "unknown box type c%3.3s (incompletely loaded)",
3451 (char*)&p_box->i_type+1 );
3452 p_box->e_flags |= BOX_FLAG_INCOMPLETE;
3457 /**** ------------------------------------------------------------------- ****/
3458 /**** "Higher level" Functions ****/
3459 /**** ------------------------------------------------------------------- ****/
3464 int (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
3465 void (*MP4_FreeBox_function )( MP4_Box_t *p_box );
3466 uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
3467 } MP4_Box_Function [] =
3470 { ATOM_moov, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3471 { ATOM_foov, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3472 { ATOM_trak, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_moov },
3473 { ATOM_trak, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_foov },
3474 { ATOM_mdia, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_trak },
3475 { ATOM_moof, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3476 { ATOM_minf, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_mdia },
3477 { ATOM_stbl, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_minf },
3478 { ATOM_dinf, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_minf },
3479 { ATOM_dinf, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_meta },
3480 { ATOM_edts, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_trak },
3481 { ATOM_udta, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3482 { ATOM_nmhd, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_minf },
3483 { ATOM_hnti, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_udta },
3484 { ATOM_rmra, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_moov },
3485 { ATOM_rmda, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_rmra },
3486 { ATOM_tref, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_trak },
3487 { ATOM_gmhd, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_minf },
3488 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_stsd },
3489 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
3490 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_WMA2 }, /* flip4mac */
3491 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_in24 },
3492 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_in32 },
3493 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_fl32 },
3494 { ATOM_wave, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_fl64 },
3495 { ATOM_ilst, MP4_ReadBox_ilst, MP4_FreeBox_Common, ATOM_meta },
3496 { ATOM_mvex, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_moov },
3497 { ATOM_mvex, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_ftyp },
3500 { ATOM_ftyp, MP4_ReadBox_ftyp, MP4_FreeBox_ftyp, 0 },
3501 { ATOM_cmov, MP4_ReadBox_cmov, MP4_FreeBox_Common, 0 },
3502 { ATOM_mvhd, MP4_ReadBox_mvhd, MP4_FreeBox_Common, ATOM_moov },
3503 { ATOM_mvhd, MP4_ReadBox_mvhd, MP4_FreeBox_Common, ATOM_foov },
3504 { ATOM_tkhd, MP4_ReadBox_tkhd, MP4_FreeBox_Common, ATOM_trak },
3505 { ATOM_mdhd, MP4_ReadBox_mdhd, MP4_FreeBox_Common, ATOM_mdia },
3506 { ATOM_hdlr, MP4_ReadBox_hdlr, MP4_FreeBox_hdlr, ATOM_mdia },
3507 { ATOM_hdlr, MP4_ReadBox_hdlr, MP4_FreeBox_hdlr, ATOM_meta },
3508 { ATOM_hdlr, MP4_ReadBox_hdlr, MP4_FreeBox_hdlr, ATOM_minf },
3509 { ATOM_vmhd, MP4_ReadBox_vmhd, MP4_FreeBox_Common, ATOM_minf },
3510 { ATOM_smhd, MP4_ReadBox_smhd, MP4_FreeBox_Common, ATOM_minf },
3511 { ATOM_hmhd, MP4_ReadBox_hmhd, MP4_FreeBox_Common, ATOM_minf },
3512 { ATOM_alis, MP4_ReadBoxSkip, MP4_FreeBox_Common, ATOM_dref },
3513 { ATOM_url, MP4_ReadBox_url, MP4_FreeBox_url, 0 },
3514 { ATOM_urn, MP4_ReadBox_urn, MP4_FreeBox_urn, 0 },
3515 { ATOM_dref, MP4_ReadBox_dref, MP4_FreeBox_Common, 0 },
3516 { ATOM_stts, MP4_ReadBox_stts, MP4_FreeBox_stts, ATOM_stbl },
3517 { ATOM_ctts, MP4_ReadBox_ctts, MP4_FreeBox_ctts, ATOM_stbl },
3518 { ATOM_stsd, MP4_ReadBox_stsd, MP4_FreeBox_Common, ATOM_stbl },
3519 { ATOM_stsz, MP4_ReadBox_stsz, MP4_FreeBox_stsz, ATOM_stbl },
3520 { ATOM_stsc, MP4_ReadBox_stsc, MP4_FreeBox_stsc, ATOM_stbl },
3521 { ATOM_stco, MP4_ReadBox_stco_co64, MP4_FreeBox_stco_co64, ATOM_stbl },
3522 { ATOM_co64, MP4_ReadBox_stco_co64, MP4_FreeBox_stco_co64, ATOM_stbl },
3523 { ATOM_stss, MP4_ReadBox_stss, MP4_FreeBox_stss, ATOM_stbl },
3524 { ATOM_stsh, MP4_ReadBox_stsh, MP4_FreeBox_stsh, ATOM_stbl },
3525 { ATOM_stdp, MP4_ReadBox_stdp, MP4_FreeBox_stdp, 0 },
3526 { ATOM_padb, MP4_ReadBox_padb, MP4_FreeBox_padb, 0 },
3527 { ATOM_elst, MP4_ReadBox_elst, MP4_FreeBox_elst, ATOM_edts },
3528 { ATOM_cprt, MP4_ReadBox_cprt, MP4_FreeBox_cprt, 0 },
3529 { ATOM_esds, MP4_ReadBox_esds, MP4_FreeBox_esds, ATOM_wave }, /* mp4a in wave chunk */
3530 { ATOM_esds, MP4_ReadBox_esds, MP4_FreeBox_esds, ATOM_mp4a },
3531 { ATOM_esds, MP4_ReadBox_esds, MP4_FreeBox_esds, ATOM_mp4v },
3532 { ATOM_esds, MP4_ReadBox_esds, MP4_FreeBox_esds, ATOM_mp4s },
3533 { ATOM_dcom, MP4_ReadBox_dcom, MP4_FreeBox_Common, 0 },
3534 { ATOM_cmvd, MP4_ReadBox_cmvd, MP4_FreeBox_cmvd, 0 },
3535 { ATOM_avcC, MP4_ReadBox_avcC, MP4_FreeBox_avcC, ATOM_avc1 },
3536 { ATOM_hvcC, MP4_ReadBox_hvcC, MP4_FreeBox_hvcC, 0 },
3537 { ATOM_dac3, MP4_ReadBox_dac3, MP4_FreeBox_Common, 0 },
3538 { ATOM_dec3, MP4_ReadBox_dec3, MP4_FreeBox_Common, 0 },
3539 { ATOM_dvc1, MP4_ReadBox_dvc1, MP4_FreeBox_Common, 0 },
3540 { ATOM_enda, MP4_ReadBox_enda, MP4_FreeBox_Common, 0 },
3541 { ATOM_iods, MP4_ReadBox_iods, MP4_FreeBox_Common, 0 },
3542 { ATOM_pasp, MP4_ReadBox_pasp, MP4_FreeBox_Common, 0 },
3543 { ATOM_keys, MP4_ReadBox_keys, MP4_FreeBox_keys, ATOM_meta },
3545 /* Nothing to do with this box */
3546 { ATOM_mdat, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3547 { ATOM_skip, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3548 { ATOM_free, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3549 { ATOM_wide, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3550 { ATOM_binm, MP4_ReadBoxSkip, MP4_FreeBox_Common, 0 },
3553 { ATOM_tx3g, MP4_ReadBox_sample_tx3g, MP4_FreeBox_Common, 0 },
3554 //{ ATOM_text, MP4_ReadBox_sample_text, MP4_FreeBox_Common, 0 },
3557 { ATOM_soun, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3558 { ATOM_ac3, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3559 { ATOM_eac3, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3560 { ATOM_lpcm, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3561 { ATOM_ms02, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3562 { ATOM_ms11, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3563 { ATOM_ms55, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3564 { ATOM__mp3, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3565 { ATOM_mp4a, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3566 { ATOM_twos, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3567 { ATOM_sowt, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3568 { ATOM_QDMC, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3569 { ATOM_QDM2, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3570 { ATOM_ima4, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3571 { ATOM_IMA4, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3572 { ATOM_dvi, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3573 { ATOM_alaw, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3574 { ATOM_ulaw, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3575 { ATOM_raw, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3576 { ATOM_MAC3, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3577 { ATOM_MAC6, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3578 { ATOM_Qclp, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3579 { ATOM_samr, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3580 { ATOM_sawb, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3581 { ATOM_OggS, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3582 { ATOM_alac, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd },
3583 { ATOM_WMA2, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, ATOM_stsd }, /* flip4mac */
3584 /* Sound extensions */
3585 { ATOM_chan, MP4_ReadBox_stsdext_chan, MP4_FreeBox_stsdext_chan, 0 },
3586 { ATOM_WMA2, MP4_ReadBox_WMA2, MP4_FreeBox_WMA2, ATOM_wave }, /* flip4mac */
3588 { ATOM_drmi, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3589 { ATOM_vide, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3590 { ATOM_mp4v, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3591 { ATOM_SVQ1, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3592 { ATOM_SVQ3, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3593 { ATOM_ZyGo, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3594 { ATOM_DIVX, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3595 { ATOM_XVID, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3596 { ATOM_h263, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3597 { ATOM_s263, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3598 { ATOM_cvid, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3599 { ATOM_3IV1, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3600 { ATOM_3iv1, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3601 { ATOM_3IV2, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3602 { ATOM_3iv2, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3603 { ATOM_3IVD, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3604 { ATOM_3ivd, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3605 { ATOM_3VID, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3606 { ATOM_3vid, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3607 { ATOM_mjpa, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3608 { ATOM_mjpb, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3609 { ATOM_qdrw, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3610 { ATOM_mp2v, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3611 { ATOM_hdv2, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3612 { ATOM_WMV3, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3614 { ATOM_mjqt, MP4_ReadBox_default, NULL, 0 }, /* found in mjpa/b */
3615 { ATOM_mjht, MP4_ReadBox_default, NULL, 0 },
3617 { ATOM_dvc, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3618 { ATOM_dvp, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3619 { ATOM_dv5n, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3620 { ATOM_dv5p, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3621 { ATOM_VP31, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3622 { ATOM_vp31, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3623 { ATOM_h264, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3625 { ATOM_jpeg, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3626 { ATOM_avc1, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, ATOM_stsd },
3628 { ATOM_yv12, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, 0 },
3629 { ATOM_yuv2, MP4_ReadBox_sample_vide, MP4_FreeBox_sample_vide, 0 },
3631 { ATOM_strf, MP4_ReadBox_strf, MP4_FreeBox_strf, ATOM_WMV3 }, /* flip4mac */
3632 { ATOM_ASF , MP4_ReadBox_ASF, MP4_FreeBox_Common, ATOM_WMV3 }, /* flip4mac */
3633 { ATOM_ASF , MP4_ReadBox_ASF, MP4_FreeBox_Common, ATOM_wave }, /* flip4mac */
3635 { ATOM_mp4s, MP4_ReadBox_sample_mp4s, MP4_FreeBox_Common, ATOM_stsd },
3637 /* XXX there is 2 box where we could find this entry stbl and tref*/
3638 { ATOM_hint, MP4_ReadBox_default, MP4_FreeBox_Common, 0 },
3640 /* found in tref box */
3641 { ATOM_dpnd, MP4_ReadBox_default, NULL, 0 },
3642 { ATOM_ipir, MP4_ReadBox_default, NULL, 0 },
3643 { ATOM_mpod, MP4_ReadBox_default, NULL, 0 },
3644 { ATOM_chap, MP4_ReadBox_tref_generic, MP4_FreeBox_tref_generic, 0 },
3647 { ATOM_rtp, MP4_ReadBox_default, NULL, 0 },
3649 /* found in rmra/rmda */
3650 { ATOM_rdrf, MP4_ReadBox_rdrf, MP4_FreeBox_rdrf , ATOM_rmda },
3651 { ATOM_rmdr, MP4_ReadBox_rmdr, MP4_FreeBox_Common, ATOM_rmda },
3652 { ATOM_rmqu, MP4_ReadBox_rmqu, MP4_FreeBox_Common, ATOM_rmda },
3653 { ATOM_rmvc, MP4_ReadBox_rmvc, MP4_FreeBox_Common, ATOM_rmda },
3655 { ATOM_drms, MP4_ReadBox_sample_soun, MP4_FreeBox_sample_soun, 0 },
3656 { ATOM_sinf, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3657 { ATOM_schi, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3658 { ATOM_user, MP4_ReadBox_drms, MP4_FreeBox_Common, 0 },
3659 { ATOM_key, MP4_ReadBox_drms, MP4_FreeBox_Common, 0 },
3660 { ATOM_iviv, MP4_ReadBox_drms, MP4_FreeBox_Common, 0 },
3661 { ATOM_priv, MP4_ReadBox_drms, MP4_FreeBox_Common, 0 },
3662 { ATOM_frma, MP4_ReadBox_frma, MP4_FreeBox_Common, ATOM_sinf }, /* and rinf */
3663 { ATOM_frma, MP4_ReadBox_frma, MP4_FreeBox_Common, ATOM_wave }, /* flip4mac */
3664 { ATOM_skcr, MP4_ReadBox_skcr, MP4_FreeBox_Common, 0 },
3666 /* ilst meta tags */
3667 { ATOM_0xa9ART, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3668 { ATOM_0xa9alb, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3669 { ATOM_0xa9cmt, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3670 { ATOM_0xa9com, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3671 { ATOM_0xa9day, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3672 { ATOM_0xa9des, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3673 { ATOM_0xa9enc, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3674 { ATOM_0xa9gen, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3675 { ATOM_0xa9grp, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3676 { ATOM_0xa9lyr, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3677 { ATOM_0xa9nam, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3678 { ATOM_0xa9too, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3679 { ATOM_0xa9trk, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3680 { ATOM_0xa9wrt, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3681 { ATOM_aART, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3682 { ATOM_atID, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst }, /* iTunes */
3683 { ATOM_chpl, MP4_ReadBox_chpl, MP4_FreeBox_chpl, ATOM_ilst },
3684 { ATOM_cnID, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst }, /* iTunes */
3685 { ATOM_covr, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_ilst },
3686 { ATOM_disk, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3687 { ATOM_flvr, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3688 { ATOM_gnre, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3689 { ATOM_rtng, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3690 { ATOM_trkn, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3691 { ATOM_xid_, MP4_ReadBox_Metadata, MP4_FreeBox_Common, ATOM_ilst },
3694 { ATOM_0xa9ART, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3695 { ATOM_0xa9alb, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3696 { ATOM_0xa9ard, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3697 { ATOM_0xa9arg, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3698 { ATOM_0xa9aut, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3699 { ATOM_0xa9cak, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3700 { ATOM_0xa9cmt, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3701 { ATOM_0xa9con, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3702 { ATOM_0xa9com, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3703 { ATOM_0xa9cpy, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3704 { ATOM_0xa9day, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3705 { ATOM_0xa9des, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3706 { ATOM_0xa9dir, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3707 { ATOM_0xa9dis, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3708 { ATOM_0xa9dsa, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3709 { ATOM_0xa9fmt, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3710 { ATOM_0xa9gen, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3711 { ATOM_0xa9grp, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3712 { ATOM_0xa9hst, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3713 { ATOM_0xa9inf, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3714 { ATOM_0xa9isr, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3715 { ATOM_0xa9lab, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3716 { ATOM_0xa9lal, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3717 { ATOM_0xa9lnt, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3718 { ATOM_0xa9lyr, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3719 { ATOM_0xa9mak, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3720 { ATOM_0xa9mal, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3721 { ATOM_0xa9mod, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3722 { ATOM_0xa9nam, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3723 { ATOM_0xa9ope, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3724 { ATOM_0xa9phg, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3725 { ATOM_0xa9PRD, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3726 { ATOM_0xa9prd, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3727 { ATOM_0xa9prf, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3728 { ATOM_0xa9pub, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3729 { ATOM_0xa9req, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3730 { ATOM_0xa9sne, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3731 { ATOM_0xa9snm, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3732 { ATOM_0xa9sol, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3733 { ATOM_0xa9src, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3734 { ATOM_0xa9st3, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3735 { ATOM_0xa9swr, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3736 { ATOM_0xa9thx, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3737 { ATOM_0xa9too, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3738 { ATOM_0xa9trk, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3739 { ATOM_0xa9url, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3740 { ATOM_0xa9wrn, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3741 { ATOM_0xa9xpd, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3742 { ATOM_0xa9xyz, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3743 { ATOM_name, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3744 { ATOM_vndr, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3745 { ATOM_SDLN, MP4_ReadBox_String, MP4_FreeBox_String, ATOM_udta },
3747 /* udta, non meta */
3748 { ATOM_tsel, MP4_ReadBox_tsel, MP4_FreeBox_Common, ATOM_udta },
3750 /* iTunes/Quicktime meta info */
3751 { ATOM_meta, MP4_ReadBox_meta, MP4_FreeBox_Common, 0 },
3752 { ATOM_data, MP4_ReadBox_data, MP4_FreeBox_data, 0 },
3754 /* found in smoothstreaming */
3755 { ATOM_traf, MP4_ReadBoxContainer, MP4_FreeBox_Common, ATOM_moof },
3756 { ATOM_mfra, MP4_ReadBoxContainer, MP4_FreeBox_Common, 0 },
3757 { ATOM_mfhd, MP4_ReadBox_mfhd, MP4_FreeBox_Common, ATOM_moof },
3758 { ATOM_sidx, MP4_ReadBox_sidx, MP4_FreeBox_sidx, 0 },
3759 { ATOM_tfhd, MP4_ReadBox_tfhd, MP4_FreeBox_Common, ATOM_traf },
3760 { ATOM_trun, MP4_ReadBox_trun, MP4_FreeBox_trun, ATOM_traf },
3761 { ATOM_trex, MP4_ReadBox_trex, MP4_FreeBox_Common, ATOM_mvex },
3762 { ATOM_mehd, MP4_ReadBox_mehd, MP4_FreeBox_Common, ATOM_mvex },
3763 { ATOM_sdtp, MP4_ReadBox_sdtp, MP4_FreeBox_sdtp, 0 },
3764 { ATOM_tfra, MP4_ReadBox_tfra, MP4_FreeBox_tfra, ATOM_mfra },
3765 { ATOM_mfro, MP4_ReadBox_mfro, MP4_FreeBox_Common, ATOM_mfra },
3766 { ATOM_uuid, MP4_ReadBox_uuid, MP4_FreeBox_uuid, 0 },
3769 { 0, MP4_ReadBox_default, NULL, 0 }
3773 /*****************************************************************************
3774 * MP4_ReadBox : parse the actual box and the children
3775 * XXX : Do not go to the next box
3776 *****************************************************************************/
3777 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
3779 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
3780 unsigned int i_index;
3785 if( !MP4_ReadBoxCommon( p_stream, p_box ) )
3787 msg_Warn( p_stream, "cannot read one box" );
3791 if( !p_box->i_size )
3793 msg_Dbg( p_stream, "found an empty box (null size)" );
3797 p_box->p_father = p_father;
3799 /* Now search function to call */
3800 for( i_index = 0; ; i_index++ )
3802 if ( MP4_Box_Function[i_index].i_parent &&
3804 p_box->p_father->i_type != MP4_Box_Function[i_index].i_parent )
3807 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
3808 ( MP4_Box_Function[i_index].i_type == 0 ) )
3814 if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
3816 MP4_BoxFree( p_stream, p_box );
3820 p_box->pf_free = MP4_Box_Function[i_index].MP4_FreeBox_function;
3825 /*****************************************************************************
3826 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
3828 *****************************************************************************/
3829 void MP4_BoxFree( stream_t *s, MP4_Box_t *p_box )
3836 for( p_child = p_box->p_first; p_child != NULL; )
3840 p_next = p_child->p_next;
3841 MP4_BoxFree( s, p_child );
3845 /* Now search function to call */
3846 if( p_box->data.p_payload )
3848 if (unlikely( p_box->pf_free == NULL ))
3850 /* Should not happen */
3851 if MP4_BOX_TYPE_ASCII()
3853 "cannot free box %4.4s, type unknown",
3854 (char*)&p_box->i_type );
3857 "cannot free box c%3.3s, type unknown",
3858 (char*)&p_box->i_type+1 );
3862 p_box->pf_free( p_box );
3864 free( p_box->data.p_payload );
3869 /* SmooBox is a very simple MP4 box, VLC specific, used only for the stream_filter to
3870 * send information to the demux. SmooBox is actually a simplified moov box (we wanted
3871 * to avoid the hassle of building a moov box at the stream_filter level) */
3872 MP4_Box_t *MP4_BoxGetSmooBox( stream_t *s )
3874 /* p_chunk is a virtual root container for the smoo box */
3878 p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
3879 if( unlikely( p_chunk == NULL ) )
3882 p_chunk->i_type = ATOM_root;
3883 p_chunk->i_shortsize = 1;
3885 p_smoo = MP4_ReadBox( s, p_chunk );
3886 if( !p_smoo || p_smoo->i_type != ATOM_uuid || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
3888 msg_Warn( s, "no smoo box found!");
3892 p_chunk->p_first = p_smoo;
3893 p_chunk->p_last = p_smoo;
3902 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
3904 /* p_chunk is a virtual root container for the moof and mdat boxes */
3906 MP4_Box_t *p_tmp_box = NULL;
3908 p_tmp_box = calloc( 1, sizeof( MP4_Box_t ) );
3909 if( unlikely( p_tmp_box == NULL ) )
3912 /* We might get a ftyp box or a SmooBox */
3913 MP4_ReadBoxCommon( s, p_tmp_box );
3915 if( (p_tmp_box->i_type == ATOM_uuid && !CmpUUID( &p_tmp_box->i_uuid, &SmooBoxUUID )) )
3918 return MP4_BoxGetSmooBox( s );
3920 else if( p_tmp_box->i_type == ATOM_ftyp )
3923 return MP4_BoxGetRoot( s );
3927 p_chunk = calloc( 1, sizeof( MP4_Box_t ) );
3928 if( unlikely( p_chunk == NULL ) )
3931 p_chunk->i_type = ATOM_root;
3932 p_chunk->i_shortsize = 1;
3934 MP4_ReadBoxContainerChildren( s, p_chunk, ATOM_moof );
3936 p_tmp_box = p_chunk->p_first;
3939 p_chunk->i_size += p_tmp_box->i_size;
3940 p_tmp_box = p_tmp_box->p_next;
3946 /*****************************************************************************
3947 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
3948 *****************************************************************************
3949 * The first box is a virtual box "root" and is the father for all first
3950 * level boxes for the file, a sort of virtual contener
3951 *****************************************************************************/
3952 MP4_Box_t *MP4_BoxGetRoot( stream_t *s )
3958 p_root = malloc( sizeof( MP4_Box_t ) );
3959 if( p_root == NULL )
3963 p_root->i_type = ATOM_root;
3964 p_root->i_shortsize = 1;
3965 /* could be a DASH stream for exemple, 0 means unknown or infinite size */
3967 CreateUUID( &p_root->i_uuid, p_root->i_type );
3969 p_root->data.p_payload = NULL;
3970 p_root->p_father = NULL;
3971 p_root->p_first = NULL;
3972 p_root->p_last = NULL;
3973 p_root->p_next = NULL;
3977 /* First get the moov */
3978 i_result = MP4_ReadBoxContainerChildren( p_stream, p_root, ATOM_moov );
3982 /* If there is a mvex box, it means fragmented MP4, and we're done */
3983 else if( MP4_BoxCount( p_root, "moov/mvex" ) > 0 )
3986 p_root->i_size = stream_Size( s );
3987 if( stream_Tell( s ) + 8 < stream_Size( s ) )
3989 /* Get the rest of the file */
3990 i_result = MP4_ReadBoxContainerRaw( p_stream, p_root );
3999 /* check if there is a cmov, if so replace
4000 compressed moov by uncompressed one */
4001 if( ( ( p_moov = MP4_BoxGet( p_root, "moov" ) ) &&
4002 ( p_cmov = MP4_BoxGet( p_root, "moov/cmov" ) ) ) ||
4003 ( ( p_moov = MP4_BoxGet( p_root, "foov" ) ) &&
4004 ( p_cmov = MP4_BoxGet( p_root, "foov/cmov" ) ) ) )
4006 /* rename the compressed moov as a box to skip */
4007 p_moov->i_type = ATOM_skip;
4009 /* get uncompressed p_moov */
4010 p_moov = p_cmov->data.p_cmov->p_moov;
4011 p_cmov->data.p_cmov->p_moov = NULL;
4013 /* make p_root father of this new moov */
4014 p_moov->p_father = p_root;
4016 /* insert this new moov box as first child of p_root */
4017 p_moov->p_next = p_root->p_first;
4018 p_root->p_first = p_moov;
4025 stream_Seek( p_stream, 0 );
4030 static void MP4_BoxDumpStructure_Internal( stream_t *s,
4031 MP4_Box_t *p_box, unsigned int i_level )
4034 uint32_t i_displayedtype = p_box->i_type;
4035 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype)[0] = 'c';
4039 msg_Dbg( s, "dumping root Box \"%4.4s\"",
4040 (char*)&i_displayedtype );
4045 if( i_level >= (sizeof(str) - 1)/4 )
4048 memset( str, ' ', sizeof(str) );
4049 for( unsigned i = 0; i < i_level; i++ )
4054 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
4055 "+ %4.4s size %"PRIu64" offset %ju%s",
4056 (char*)&i_displayedtype, p_box->i_size,
4057 (uintmax_t)p_box->i_pos,
4058 p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
4059 msg_Dbg( s, "%s", str );
4061 p_child = p_box->p_first;
4064 MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
4065 p_child = p_child->p_next;
4069 void MP4_BoxDumpStructure( stream_t *s, MP4_Box_t *p_box )
4071 MP4_BoxDumpStructure_Internal( s, p_box, 0 );
4075 /*****************************************************************************
4076 *****************************************************************************
4078 ** High level methods to acces an MP4 file
4080 *****************************************************************************
4081 *****************************************************************************/
4082 static void get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
4085 if( !*ppsz_path[0] )
4091 i_len = strcspn( *ppsz_path, "/[" );
4092 if( !i_len && **ppsz_path == '/' )
4096 *ppsz_token = strndup( *ppsz_path, i_len );
4097 if( unlikely(!*ppsz_token) )
4100 *ppsz_path += i_len;
4102 if( **ppsz_path == '[' )
4105 *pi_number = strtol( *ppsz_path, NULL, 10 );
4106 while( **ppsz_path && **ppsz_path != ']' )
4110 if( **ppsz_path == ']' )
4119 while( **ppsz_path == '/' )
4125 static void MP4_BoxGet_Internal( MP4_Box_t **pp_result,
4126 MP4_Box_t *p_box, const char *psz_fmt, va_list args)
4138 if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
4141 if( !psz_path || !psz_path[0] )
4148 // fprintf( stderr, "path:'%s'\n", psz_path );
4149 psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
4154 get_token( &psz_path, &psz_token, &i_number );
4155 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
4156 // psz_path,psz_token,i_number );
4164 if( !strcmp( psz_token, "/" ) )
4167 while( p_box && p_box->i_type != ATOM_root )
4169 p_box = p_box->p_father;
4177 if( !strcmp( psz_token, "." ) )
4182 if( !strcmp( psz_token, ".." ) )
4184 p_box = p_box->p_father;
4191 if( strlen( psz_token ) == 4 )
4194 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
4195 psz_token[2], psz_token[3] );
4196 p_box = p_box->p_first;
4203 if( p_box->i_type == i_fourcc )
4211 p_box = p_box->p_next;
4215 if( *psz_token == '\0' )
4217 p_box = p_box->p_first;
4229 p_box = p_box->p_next;
4234 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
4238 FREENULL( psz_token );
4250 /*****************************************************************************
4251 * MP4_BoxGet: find a box given a path relative to p_box
4252 *****************************************************************************
4253 * Path Format: . .. / as usual
4254 * [number] to specifie box number ex: trak[12]
4256 * ex: /moov/trak[12]
4258 *****************************************************************************/
4259 MP4_Box_t *MP4_BoxGet( MP4_Box_t *p_box, const char *psz_fmt, ... )
4262 MP4_Box_t *p_result;
4264 va_start( args, psz_fmt );
4265 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
4271 /*****************************************************************************
4272 * MP4_BoxCount: count box given a path relative to p_box
4273 *****************************************************************************
4274 * Path Format: . .. / as usual
4275 * [number] to specifie box number ex: trak[12]
4277 * ex: /moov/trak[12]
4279 *****************************************************************************/
4280 int MP4_BoxCount( MP4_Box_t *p_box, const char *psz_fmt, ... )
4284 MP4_Box_t *p_result, *p_next;
4286 va_start( args, psz_fmt );
4287 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
4295 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
4297 if( p_next->i_type == p_result->i_type)