demux: mp4: map more metadata (fix #18807)
[vlc.git] / modules / demux / mp4 / libmp4.c
blob2301d5ee04a607ce0bdd5bd740d3a77dadc6908c
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 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <vlc_common.h>
28 #include <vlc_stream.h> /* vlc_stream_Peek*/
29 #include <vlc_strings.h> /* vlc_ascii_tolower */
31 #ifdef HAVE_ZLIB_H
32 # include <zlib.h> /* for compressed moov */
33 #endif
35 #include "libmp4.h"
36 #include "languages.h"
37 #include <math.h>
38 #include <assert.h>
40 /* Some assumptions:
41 * The input method HAS to be seekable
44 /* convert 16.16 fixed point to floating point */
45 static double conv_fx( int32_t fx ) {
46 double fp = fx;
47 fp /= 65536.;
48 return fp;
51 /* some functions for mp4 encoding of variables */
52 #ifdef MP4_VERBOSE
53 static void MP4_ConvertDate2Str( char *psz, uint64_t i_date, bool b_relative )
55 int i_day;
56 int i_hour;
57 int i_min;
58 int i_sec;
60 /* date begin at 1 jan 1904 */
61 if ( !b_relative )
62 i_date += ((INT64_C(1904) * 365) + 17) * 24 * 60 * 60;
64 i_day = i_date / ( 60*60*24);
65 i_hour = ( i_date /( 60*60 ) ) % 60;
66 i_min = ( i_date / 60 ) % 60;
67 i_sec = i_date % 60;
68 sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds", i_day, i_hour, i_min, i_sec );
70 #endif
72 /*****************************************************************************
73 * Some prototypes.
74 *****************************************************************************/
75 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
76 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father );
77 static void MP4_Box_Clean_Specific( MP4_Box_t *p_box );
78 static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box );
80 int MP4_Seek( stream_t *p_stream, uint64_t i_pos )
82 /* Prevent prefetch breakage */
83 uint64_t i_size = stream_Size( p_stream );
84 if( i_size > 0 && i_pos >= i_size )
85 return VLC_EGENERIC;
87 bool b_canseek = false;
88 if ( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_canseek ) != VLC_SUCCESS ||
89 b_canseek )
91 /* can seek or don't know */
92 return vlc_stream_Seek( p_stream, i_pos );
94 /* obviously can't seek then */
96 int64_t i_current_pos = vlc_stream_Tell( p_stream );
97 if ( i_current_pos < 0 || i_pos < (uint64_t)i_current_pos )
98 return VLC_EGENERIC;
100 size_t i_toread = i_pos - i_current_pos;
101 if( i_toread == 0 )
102 return VLC_SUCCESS;
103 else if( i_toread > (1<<17) )
104 return VLC_EGENERIC;
106 if( vlc_stream_Read( p_stream, NULL, i_toread ) != (ssize_t)i_toread )
107 return VLC_EGENERIC;
108 return VLC_SUCCESS;
111 static void MP4_BoxAddChild( MP4_Box_t *p_parent, MP4_Box_t *p_childbox )
113 if( !p_parent->p_first )
114 p_parent->p_first = p_childbox;
115 else
116 p_parent->p_last->p_next = p_childbox;
117 p_parent->p_last = p_childbox;
118 p_childbox->p_father = p_parent;
121 MP4_Box_t * MP4_BoxExtract( MP4_Box_t **pp_chain, uint32_t i_type )
123 MP4_Box_t *p_box = *pp_chain;
124 while( p_box )
126 if( p_box->i_type == i_type )
128 *pp_chain = p_box->p_next;
129 p_box->p_next = NULL;
130 return p_box;
132 pp_chain = &p_box->p_next;
133 p_box = p_box->p_next;
135 return NULL;
138 /* Don't use vlc_stream_Seek directly */
139 #undef vlc_stream_Seek
140 #define vlc_stream_Seek(a,b) __NO__
142 /*****************************************************************************
143 * MP4_PeekBoxHeader : Load only common parameters for all boxes
144 *****************************************************************************
145 * p_box need to be an already allocated MP4_Box_t, and all data
146 * will only be peek not read
148 * RETURN : 0 if it fail, 1 otherwise
149 *****************************************************************************/
150 static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box )
152 int i_read;
153 const uint8_t *p_peek;
155 if( ( ( i_read = vlc_stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
157 return 0;
159 p_box->i_pos = vlc_stream_Tell( p_stream );
161 p_box->data.p_payload = NULL;
162 p_box->p_father = NULL;
163 p_box->p_first = NULL;
164 p_box->p_last = NULL;
165 p_box->p_next = NULL;
167 MP4_GET4BYTES( p_box->i_shortsize );
168 MP4_GETFOURCC( p_box->i_type );
170 /* Now special case */
172 if( p_box->i_shortsize == 1 )
174 if( i_read < 8 )
175 return 0;
176 /* get the true size on 64 bits */
177 MP4_GET8BYTES( p_box->i_size );
179 else
181 p_box->i_size = p_box->i_shortsize;
182 /* XXX size of 0 means that the box extends to end of file */
185 if( UINT64_MAX - p_box->i_size < p_box->i_pos )
186 return 0;
188 if( p_box->i_type == ATOM_uuid )
190 if( i_read < 16 )
191 return 0;
192 /* get extented type on 16 bytes */
193 GetUUID( &p_box->i_uuid, p_peek );
196 #ifdef MP4_ULTRA_VERBOSE
197 if( p_box->i_size )
199 if MP4_BOX_TYPE_ASCII()
200 msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64" %"PRId64,
201 (char*)&p_box->i_type, p_box->i_size, p_box->i_pos );
202 else
203 msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
204 (char*)&p_box->i_type+1, p_box->i_size );
206 #endif
208 return 1;
211 /*****************************************************************************
212 * MP4_ReadBoxRestricted : Reads box from current position
213 *****************************************************************************
214 * if p_box == NULL, box is invalid or failed, position undefined
215 * on success, position is past read box or EOF
216 *****************************************************************************/
217 static MP4_Box_t *MP4_ReadBoxRestricted( stream_t *p_stream, MP4_Box_t *p_father,
218 const uint32_t onlytypes[], const uint32_t nottypes[],
219 bool *pb_restrictionhit )
221 MP4_Box_t peekbox = { 0 };
222 if ( !MP4_PeekBoxHeader( p_stream, &peekbox ) )
223 return NULL;
225 if( peekbox.i_size < 8 )
227 msg_Warn( p_stream, "found an invalid sized %"PRIu64" box %4.4s @%"PRIu64 ,
228 peekbox.i_size, (char *) &peekbox.i_type, vlc_stream_Tell(p_stream) );
229 return NULL;
232 for( size_t i=0; nottypes && nottypes[i]; i++ )
234 if( nottypes[i] == peekbox.i_type )
236 *pb_restrictionhit = true;
237 return NULL;
241 for( size_t i=0; onlytypes && onlytypes[i]; i++ )
243 if( onlytypes[i] != peekbox.i_type )
245 *pb_restrictionhit = true;
246 return NULL;
250 /* if father's size == 0, it means unknown or infinite size,
251 * and we skip the followong check */
252 if( p_father && p_father->i_size > 0 )
254 const uint64_t i_box_next = peekbox.i_size + peekbox.i_pos;
255 const uint64_t i_father_next = p_father->i_size + p_father->i_pos;
256 /* check if it's within p-father */
257 if( i_box_next > i_father_next )
259 msg_Warn( p_stream, "out of bound child %4.4s", (char*) &peekbox.i_type );
260 return NULL; /* out of bound */
264 /* Everything seems OK */
265 MP4_Box_t *p_box = (MP4_Box_t *) malloc( sizeof(MP4_Box_t) );
266 if( !p_box )
267 return NULL;
268 *p_box = peekbox;
270 const uint64_t i_next = p_box->i_pos + p_box->i_size;
271 p_box->p_father = p_father;
272 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
274 msg_Warn( p_stream, "Failed reading box %4.4s", (char*) &peekbox.i_type );
275 MP4_BoxFree( p_box );
276 p_box = NULL;
279 /* Check is we consumed all data */
280 if( vlc_stream_Tell( p_stream ) < i_next )
282 MP4_Seek( p_stream, i_next - 1 ); /* since past seek can fail when hitting EOF */
283 MP4_Seek( p_stream, i_next );
284 if( vlc_stream_Tell( p_stream ) < i_next - 1 ) /* Truncated box */
286 msg_Warn( p_stream, "truncated box %4.4s discarded", (char*) &peekbox.i_type );
287 MP4_BoxFree( p_box );
288 p_box = NULL;
292 if ( p_box )
293 MP4_BoxAddChild( p_father, p_box );
295 return p_box;
298 /*****************************************************************************
299 * For all known box a loader is given,
300 * you have to be already read container header
301 * without container size, file position on exit is unknown
302 *****************************************************************************/
303 static int MP4_ReadBoxContainerChildrenIndexed( stream_t *p_stream,
304 MP4_Box_t *p_container, const uint32_t stoplist[],
305 const uint32_t excludelist[], bool b_indexed )
307 /* Size of root container is set to 0 when unknown, for exemple
308 * with a DASH stream. In that case, we skip the following check */
309 if( (p_container->i_size || p_container->p_father)
310 && ( vlc_stream_Tell( p_stream ) + ((b_indexed)?16:8) >
311 (uint64_t)(p_container->i_pos + p_container->i_size) )
314 /* there is no box to load */
315 return 0;
318 uint64_t i_last_pos = 0; /* used to detect read failure loops */
319 const uint64_t i_end = p_container->i_pos + p_container->i_size;
320 MP4_Box_t *p_box = NULL;
321 bool b_onexclude = false;
322 bool b_continue;
325 b_continue = false;
326 if ( p_container->i_size )
328 const uint64_t i_tell = vlc_stream_Tell( p_stream );
329 if( i_tell + ((b_indexed)?16:8) >= i_end )
330 break;
333 uint32_t i_index = 0;
334 if ( b_indexed )
336 uint8_t read[8];
337 if ( vlc_stream_Read( p_stream, read, 8 ) < 8 )
338 break;
339 i_index = GetDWBE(&read[4]);
341 b_onexclude = false; /* If stopped due exclude list */
342 if( (p_box = MP4_ReadBoxRestricted( p_stream, p_container, NULL, excludelist, &b_onexclude )) )
344 b_continue = true;
345 p_box->i_index = i_index;
346 for(size_t i=0; stoplist && stoplist[i]; i++)
348 if( p_box->i_type == stoplist[i] )
349 return 1;
353 const uint64_t i_tell = vlc_stream_Tell( p_stream );
354 if ( p_container->i_size && i_tell >= i_end )
356 assert( i_tell == i_end );
357 break;
360 if ( !p_box )
362 /* Continue with next if box fails to load */
363 if( i_last_pos == i_tell )
364 break;
365 i_last_pos = i_tell;
366 b_continue = true;
369 } while( b_continue );
371 /* Always move to end of container */
372 if ( !b_onexclude && p_container->i_size )
374 const uint64_t i_tell = vlc_stream_Tell( p_stream );
375 if ( i_tell != i_end )
376 MP4_Seek( p_stream, i_end );
379 return 1;
382 int MP4_ReadBoxContainerRestricted( stream_t *p_stream, MP4_Box_t *p_container,
383 const uint32_t stoplist[], const uint32_t excludelist[] )
385 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
386 stoplist, excludelist, false );
389 int MP4_ReadBoxContainerChildren( stream_t *p_stream, MP4_Box_t *p_container,
390 const uint32_t stoplist[] )
392 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
393 stoplist, NULL, false );
396 static void MP4_BoxOffsetUp( MP4_Box_t *p_box, uint64_t i_offset )
398 while(p_box)
400 p_box->i_pos += i_offset;
401 MP4_BoxOffsetUp( p_box->p_first, i_offset );
402 p_box = p_box->p_next;
406 /* Reads within an already read/in memory box (containers without having to seek) */
407 static int MP4_ReadBoxContainerRawInBox( stream_t *p_stream, MP4_Box_t *p_container,
408 uint8_t *p_buffer, uint64_t i_size, uint64_t i_offset )
410 if(!p_container)
411 return 0;
412 stream_t *p_substream = vlc_stream_MemoryNew( p_stream, p_buffer, i_size,
413 true );
414 if( !p_substream )
415 return 0;
416 MP4_Box_t *p_last = p_container->p_last;
417 MP4_ReadBoxContainerChildren( p_substream, p_container, NULL );
418 vlc_stream_Delete( p_substream );
419 /* do pos fixup */
420 if( p_container )
422 MP4_Box_t *p_box = p_last ? p_last : p_container->p_first;
423 MP4_BoxOffsetUp(p_box, i_offset);
426 return 1;
429 static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
431 if( p_container->i_size &&
432 ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
434 /* container is empty, 8 stand for the first header in this box */
435 return 1;
438 /* enter box */
439 if ( MP4_Seek( p_stream, p_container->i_pos +
440 mp4_box_headersize( p_container ) ) )
441 return 0;
442 return MP4_ReadBoxContainerChildren( p_stream, p_container, NULL );
445 static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
447 /* XXX sometime moov is hiden in a free box */
448 if( p_box->p_father &&
449 p_box->p_father->i_type == ATOM_root &&
450 p_box->i_type == ATOM_free )
452 const uint8_t *p_peek;
453 int i_read;
454 vlc_fourcc_t i_fcc;
456 i_read = vlc_stream_Peek( p_stream, &p_peek, 44 );
458 p_peek += mp4_box_headersize( p_box ) + 4;
459 i_read -= mp4_box_headersize( p_box ) + 4;
461 if( i_read >= 8 )
463 i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
465 if( i_fcc == ATOM_cmov || i_fcc == ATOM_mvhd )
467 msg_Warn( p_stream, "detected moov hidden in a free box ..." );
469 p_box->i_type = ATOM_foov;
470 return MP4_ReadBoxContainer( p_stream, p_box );
475 /* Nothing to do */
476 #ifdef MP4_ULTRA_VERBOSE
477 if MP4_BOX_TYPE_ASCII()
478 msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
479 else
480 msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
481 #endif
482 return 1;
485 static int MP4_ReadBox_ilst( stream_t *p_stream, MP4_Box_t *p_box )
487 if( p_box->i_size < 8 || vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
488 return 0;
490 /* Find our handler */
491 if ( !p_box->i_handler && p_box->p_father )
493 const MP4_Box_t *p_sibling = p_box->p_father->p_first;
494 while( p_sibling )
496 if ( p_sibling->i_type == ATOM_hdlr && p_sibling->data.p_hdlr )
498 p_box->i_handler = p_sibling->data.p_hdlr->i_handler_type;
499 break;
501 p_sibling = p_sibling->p_next;
505 switch( p_box->i_handler )
507 case 0:
508 msg_Warn( p_stream, "no handler for ilst atom" );
509 return 0;
510 case HANDLER_mdta:
511 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_box, NULL, NULL, true );
512 case HANDLER_mdir:
513 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
514 default:
515 msg_Warn( p_stream, "Unknown ilst handler type '%4.4s'", (char*)&p_box->i_handler );
516 return 0;
520 static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
522 FREENULL( p_box->data.p_ftyp->i_compatible_brands );
525 static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
527 MP4_READBOX_ENTER( MP4_Box_data_ftyp_t, MP4_FreeBox_ftyp );
529 MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
530 MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
532 if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
534 uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
535 calloc( p_box->data.p_ftyp->i_compatible_brands_count,
536 sizeof(uint32_t));
538 if( unlikely( tab == NULL ) )
539 MP4_READBOX_EXIT( 0 );
541 for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
543 MP4_GETFOURCC( tab[i] );
546 else
548 p_box->data.p_ftyp->i_compatible_brands = NULL;
551 MP4_READBOX_EXIT( 1 );
555 static int MP4_ReadBox_mvhd( stream_t *p_stream, MP4_Box_t *p_box )
557 #ifdef MP4_VERBOSE
558 char s_creation_time[128];
559 char s_modification_time[128];
560 char s_duration[128];
561 #endif
562 MP4_READBOX_ENTER( MP4_Box_data_mvhd_t, NULL );
564 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
566 if( p_box->data.p_mvhd->i_version )
568 MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
569 MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
570 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
571 MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
573 else
575 MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
576 MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
577 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
578 MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
580 MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
581 MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
582 MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
585 for( unsigned i = 0; i < 2; i++ )
587 MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
589 for( unsigned i = 0; i < 9; i++ )
591 MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
593 for( unsigned i = 0; i < 6; i++ )
595 MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
598 MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
601 #ifdef MP4_VERBOSE
602 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
603 MP4_ConvertDate2Str( s_modification_time,
604 p_box->data.p_mvhd->i_modification_time, false );
605 if( p_box->data.p_mvhd->i_rate && p_box->data.p_mvhd->i_timescale )
607 MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_timescale, true );
609 else
611 s_duration[0] = 0;
613 msg_Dbg( p_stream, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
614 s_creation_time,
615 s_modification_time,
616 (uint32_t)p_box->data.p_mvhd->i_timescale,
617 s_duration,
618 (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
619 (float)p_box->data.p_mvhd->i_volume / 256 ,
620 (uint32_t)p_box->data.p_mvhd->i_next_track_id );
621 #endif
622 MP4_READBOX_EXIT( 1 );
625 static int MP4_ReadBox_mfhd( stream_t *p_stream, MP4_Box_t *p_box )
627 MP4_READBOX_ENTER( MP4_Box_data_mfhd_t, NULL );
629 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
631 MP4_GET4BYTES( p_box->data.p_mfhd->i_sequence_number );
633 #ifdef MP4_VERBOSE
634 msg_Dbg( p_stream, "read box: \"mfhd\" sequence number %d",
635 p_box->data.p_mfhd->i_sequence_number );
636 #endif
637 MP4_READBOX_EXIT( 1 );
640 static int MP4_ReadBox_tfxd( stream_t *p_stream, MP4_Box_t *p_box )
642 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, NULL );
644 MP4_Box_data_tfxd_t *p_tfxd_data = p_box->data.p_tfxd;
645 MP4_GETVERSIONFLAGS( p_tfxd_data );
647 if( p_tfxd_data->i_version == 0 )
649 MP4_GET4BYTES( p_tfxd_data->i_fragment_abs_time );
650 MP4_GET4BYTES( p_tfxd_data->i_fragment_duration );
652 else
654 MP4_GET8BYTES( p_tfxd_data->i_fragment_abs_time );
655 MP4_GET8BYTES( p_tfxd_data->i_fragment_duration );
658 #ifdef MP4_VERBOSE
659 msg_Dbg( p_stream, "read box: \"tfxd\" version %d, flags 0x%x, "\
660 "fragment duration %"PRIu64", fragment abs time %"PRIu64,
661 p_tfxd_data->i_version,
662 p_tfxd_data->i_flags,
663 p_tfxd_data->i_fragment_duration,
664 p_tfxd_data->i_fragment_abs_time
666 #endif
668 MP4_READBOX_EXIT( 1 );
671 static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
673 FREENULL( p_box->data.p_tfrf->p_tfrf_data_fields );
676 static int MP4_ReadBox_tfrf( stream_t *p_stream, MP4_Box_t *p_box )
678 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, MP4_FreeBox_tfrf );
680 MP4_Box_data_tfrf_t *p_tfrf_data = p_box->data.p_tfrf;
681 MP4_GETVERSIONFLAGS( p_tfrf_data );
683 MP4_GET1BYTE( p_tfrf_data->i_fragment_count );
685 p_tfrf_data->p_tfrf_data_fields = calloc( p_tfrf_data->i_fragment_count,
686 sizeof( TfrfBoxDataFields_t ) );
687 if( !p_tfrf_data->p_tfrf_data_fields )
688 MP4_READBOX_EXIT( 0 );
690 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
692 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
693 if( p_tfrf_data->i_version == 0 )
695 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_abs_time );
696 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_duration );
698 else
700 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_abs_time );
701 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_duration );
705 #ifdef MP4_VERBOSE
706 msg_Dbg( p_stream, "read box: \"tfrf\" version %d, flags 0x%x, "\
707 "fragment count %"PRIu8, p_tfrf_data->i_version,
708 p_tfrf_data->i_flags, p_tfrf_data->i_fragment_count );
710 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
712 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
713 msg_Dbg( p_stream, "\"tfrf\" fragment duration %"PRIu64", "\
714 "fragment abs time %"PRIu64,
715 TfrfBoxDataField->i_fragment_duration,
716 TfrfBoxDataField->i_fragment_abs_time );
719 #endif
721 MP4_READBOX_EXIT( 1 );
724 static int MP4_ReadBox_XML360( stream_t *p_stream, MP4_Box_t *p_box )
726 MP4_READBOX_ENTER( MP4_Box_data_360_t, NULL );
728 MP4_Box_data_360_t *p_360_data = p_box->data.p_360;
730 /* Copy the string for pattern matching as it does not end
731 with a '\0' in the stream. */
732 char *psz_rdf = strndup((char *)p_peek, i_read);
734 if ( unlikely( !psz_rdf ) )
735 MP4_READBOX_EXIT( 0 );
737 /* Try to find the string "GSpherical:Spherical" because the v1
738 spherical video spec says the tag must be there. */
740 if ( strcasestr( psz_rdf, "Gspherical:Spherical" ) )
741 p_360_data->i_projection_mode = PROJECTION_MODE_EQUIRECTANGULAR;
743 /* Try to find the stero mode. */
744 if ( strcasestr( psz_rdf, "left-right" ) )
746 msg_Dbg( p_stream, "Left-right stereo mode" );
747 p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_LEFT_RIGHT;
750 if ( strcasestr( psz_rdf, "top-bottom" ) )
752 msg_Dbg( p_stream, "Top-bottom stereo mode" );
753 p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_TOP_BOTTOM;
756 free( psz_rdf );
758 MP4_READBOX_EXIT( 1 );
761 static int MP4_ReadBox_st3d( stream_t *p_stream, MP4_Box_t *p_box )
763 MP4_READBOX_ENTER( MP4_Box_data_st3d_t, NULL );
765 uint8_t i_version;
766 MP4_GET1BYTE( i_version );
767 if ( i_version != 0 )
768 MP4_READBOX_EXIT( 0 );
770 uint32_t i_flags;
771 VLC_UNUSED( i_flags );
772 MP4_GET3BYTES( i_flags );
774 MP4_Box_data_st3d_t *p_data = p_box->data.p_st3d;
775 MP4_GET1BYTE( p_data->i_stereo_mode );
777 MP4_READBOX_EXIT( 1 );
780 static int MP4_ReadBox_prhd( stream_t *p_stream, MP4_Box_t *p_box )
782 MP4_READBOX_ENTER( MP4_Box_data_prhd_t, NULL );
784 uint8_t i_version;
785 MP4_GET1BYTE( i_version );
786 if (i_version != 0)
787 MP4_READBOX_EXIT( 0 );
789 uint32_t i_flags;
790 VLC_UNUSED( i_flags );
791 MP4_GET3BYTES( i_flags );
793 MP4_Box_data_prhd_t *p_data = p_box->data.p_prhd;
794 int32_t fixed16_16;
795 MP4_GET4BYTES( fixed16_16 );
796 p_data->f_pose_yaw_degrees = (float) fixed16_16 / 65536.0f;
798 MP4_GET4BYTES( fixed16_16 );
799 p_data->f_pose_pitch_degrees = (float) fixed16_16 / 65536.0f;
801 MP4_GET4BYTES( fixed16_16 );
802 p_data->f_pose_roll_degrees = (float) fixed16_16 / 65536.0f;
804 MP4_READBOX_EXIT( 1 );
807 static int MP4_ReadBox_equi( stream_t *p_stream, MP4_Box_t *p_box )
809 MP4_READBOX_ENTER( MP4_Box_data_equi_t, NULL );
811 uint8_t i_version;
812 MP4_GET1BYTE( i_version );
813 if (i_version != 0)
814 MP4_READBOX_EXIT( 0 );
816 uint32_t i_flags;
817 VLC_UNUSED( i_flags );
818 MP4_GET3BYTES( i_flags );
820 MP4_Box_data_equi_t *p_data = p_box->data.p_equi;
821 MP4_GET4BYTES( p_data->i_projection_bounds_top );
822 MP4_GET4BYTES( p_data->i_projection_bounds_bottom );
823 MP4_GET4BYTES( p_data->i_projection_bounds_left );
824 MP4_GET4BYTES( p_data->i_projection_bounds_right );
826 MP4_READBOX_EXIT( 1 );
829 static int MP4_ReadBox_cbmp( stream_t *p_stream, MP4_Box_t *p_box )
831 MP4_READBOX_ENTER( MP4_Box_data_cbmp_t, NULL );
833 uint8_t i_version;
834 MP4_GET1BYTE( i_version );
835 if (i_version != 0)
836 MP4_READBOX_EXIT( 0 );
838 uint32_t i_flags;
839 VLC_UNUSED( i_flags );
840 MP4_GET3BYTES( i_flags );
842 MP4_Box_data_cbmp_t *p_data = p_box->data.p_cbmp;
843 MP4_GET4BYTES( p_data->i_layout );
844 MP4_GET4BYTES( p_data->i_padding );
846 MP4_READBOX_EXIT( 1 );
849 static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
851 FREENULL( p_box->data.p_sidx->p_items );
854 static int MP4_ReadBox_sidx( stream_t *p_stream, MP4_Box_t *p_box )
856 MP4_READBOX_ENTER( MP4_Box_data_sidx_t, MP4_FreeBox_sidx );
858 MP4_Box_data_sidx_t *p_sidx_data = p_box->data.p_sidx;
859 MP4_GETVERSIONFLAGS( p_sidx_data );
861 MP4_GET4BYTES( p_sidx_data->i_reference_ID );
862 MP4_GET4BYTES( p_sidx_data->i_timescale );
864 if( p_sidx_data->i_version == 0 )
866 MP4_GET4BYTES( p_sidx_data->i_earliest_presentation_time );
867 MP4_GET4BYTES( p_sidx_data->i_first_offset );
869 else
871 MP4_GET8BYTES( p_sidx_data->i_earliest_presentation_time );
872 MP4_GET8BYTES( p_sidx_data->i_first_offset );
875 uint16_t i_reserved;
876 VLC_UNUSED(i_reserved);
877 MP4_GET2BYTES( i_reserved );
878 MP4_GET2BYTES( p_sidx_data->i_reference_count );
879 uint16_t i_count = p_sidx_data->i_reference_count;
881 p_sidx_data->p_items = calloc( i_count, sizeof( MP4_Box_sidx_item_t ) );
882 uint32_t tmp;
883 for( unsigned i = 0; i < i_count; i++ )
885 MP4_GET4BYTES( tmp );
886 p_sidx_data->p_items[i].b_reference_type = (bool)((tmp & 0x80000000)>>24);
887 p_sidx_data->p_items[i].i_referenced_size = tmp & 0x7fffffff;
888 MP4_GET4BYTES( p_sidx_data->p_items[i].i_subsegment_duration );
890 MP4_GET4BYTES( tmp );
891 p_sidx_data->p_items[i].b_starts_with_SAP = (bool)((tmp & 0x80000000)>>24);
892 p_sidx_data->p_items[i].i_SAP_type = (tmp & 0x70000000)>>24;
893 p_sidx_data->p_items[i].i_SAP_delta_time = tmp & 0xfffffff;
896 #ifdef MP4_VERBOSE
897 msg_Dbg( p_stream, "read box: \"sidx\" version %d, flags 0x%x, "\
898 "ref_ID %"PRIu32", timescale %"PRIu32", ref_count %"PRIu16", "\
899 "first subsegmt duration %"PRIu32,
900 p_sidx_data->i_version,
901 p_sidx_data->i_flags,
902 p_sidx_data->i_reference_ID,
903 p_sidx_data->i_timescale,
904 p_sidx_data->i_reference_count,
905 p_sidx_data->p_items[0].i_subsegment_duration
907 #endif
909 MP4_READBOX_EXIT( 1 );
912 static int MP4_ReadBox_tfhd( stream_t *p_stream, MP4_Box_t *p_box )
914 MP4_READBOX_ENTER( MP4_Box_data_tfhd_t, NULL );
916 MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );
918 if( p_box->data.p_tfhd->i_version != 0 )
920 msg_Warn( p_stream, "'tfhd' box with version != 0. "\
921 " Don't know what to do with that, please patch" );
922 MP4_READBOX_EXIT( 0 );
925 MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );
927 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
929 msg_Dbg( p_stream, "'duration-is-empty' flag is present "\
930 "=> no samples for this time interval." );
931 p_box->data.p_tfhd->b_empty = true;
933 else
934 p_box->data.p_tfhd->b_empty = false;
936 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
937 MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
938 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
939 MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
940 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
941 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
942 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
943 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
944 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
945 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
947 #ifdef MP4_VERBOSE
948 char psz_base[128] = "\0";
949 char psz_desc[128] = "\0";
950 char psz_dura[128] = "\0";
951 char psz_size[128] = "\0";
952 char psz_flag[128] = "\0";
953 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
954 snprintf(psz_base, sizeof(psz_base), "base offset %"PRId64, p_box->data.p_tfhd->i_base_data_offset);
955 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
956 snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
957 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
958 snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
959 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
960 snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
961 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
962 snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);
964 msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
965 p_box->data.p_tfhd->i_version,
966 p_box->data.p_tfhd->i_flags,
967 p_box->data.p_tfhd->i_track_ID,
968 psz_base, psz_desc, psz_dura, psz_size, psz_flag );
969 #endif
971 MP4_READBOX_EXIT( 1 );
974 static void MP4_FreeBox_trun( MP4_Box_t *p_box )
976 FREENULL( p_box->data.p_trun->p_samples );
979 static int MP4_ReadBox_trun( stream_t *p_stream, MP4_Box_t *p_box )
981 MP4_READBOX_ENTER( MP4_Box_data_trun_t, MP4_FreeBox_trun );
983 MP4_GETVERSIONFLAGS( p_box->data.p_trun );
985 MP4_GET4BYTES( p_box->data.p_trun->i_sample_count );
987 if( p_box->data.p_trun->i_flags & MP4_TRUN_DATA_OFFSET )
988 MP4_GET4BYTES( p_box->data.p_trun->i_data_offset );
989 if( p_box->data.p_trun->i_flags & MP4_TRUN_FIRST_FLAGS )
990 MP4_GET4BYTES( p_box->data.p_trun->i_first_sample_flags );
992 p_box->data.p_trun->p_samples =
993 calloc( p_box->data.p_trun->i_sample_count, sizeof(MP4_descriptor_trun_sample_t) );
994 if ( p_box->data.p_trun->p_samples == NULL )
995 MP4_READBOX_EXIT( 0 );
997 for( unsigned int i = 0; i<p_box->data.p_trun->i_sample_count; i++ )
999 MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
1000 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
1001 MP4_GET4BYTES( p_sample->i_duration );
1002 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
1003 MP4_GET4BYTES( p_sample->i_size );
1004 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS )
1005 MP4_GET4BYTES( p_sample->i_flags );
1006 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
1007 MP4_GET4BYTES( p_sample->i_composition_time_offset.v0 );
1010 #ifdef MP4_ULTRA_VERBOSE
1011 msg_Dbg( p_stream, "read box: \"trun\" version %u flags 0x%x sample count %u",
1012 p_box->data.p_trun->i_version,
1013 p_box->data.p_trun->i_flags,
1014 p_box->data.p_trun->i_sample_count );
1016 for( unsigned int i = 0; i<p_box->data.p_trun->i_sample_count; i++ )
1018 MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
1019 msg_Dbg( p_stream, "read box: \"trun\" sample %4.4u flags 0x%x "\
1020 "duration %"PRIu32" size %"PRIu32" composition time offset %"PRIu32,
1021 i, p_sample->i_flags, p_sample->i_duration,
1022 p_sample->i_size, p_sample->i_composition_time_offset );
1024 #endif
1026 MP4_READBOX_EXIT( 1 );
1029 static int MP4_ReadBox_tfdt( stream_t *p_stream, MP4_Box_t *p_box )
1031 MP4_READBOX_ENTER( MP4_Box_data_tfdt_t, NULL );
1032 if( i_read < 8 )
1033 MP4_READBOX_EXIT( 0 );
1035 MP4_GETVERSIONFLAGS( p_box->data.p_tfdt );
1037 if( p_box->data.p_tfdt->i_version == 0 )
1038 MP4_GET4BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1039 else if( p_box->data.p_tfdt->i_version == 1 )
1040 MP4_GET8BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1041 else
1042 MP4_READBOX_EXIT( 0 );
1044 MP4_READBOX_EXIT( 1 );
1047 static int MP4_ReadBox_tkhd( stream_t *p_stream, MP4_Box_t *p_box )
1049 #ifdef MP4_VERBOSE
1050 char s_creation_time[128];
1051 char s_modification_time[128];
1052 char s_duration[128];
1053 #endif
1054 MP4_READBOX_ENTER( MP4_Box_data_tkhd_t, NULL );
1056 MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
1058 if( p_box->data.p_tkhd->i_version )
1060 MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
1061 MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
1062 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1063 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1064 MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
1066 else
1068 MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
1069 MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
1070 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1071 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1072 MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
1075 for( unsigned i = 0; i < 2; i++ )
1077 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
1079 MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
1080 MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
1081 MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
1082 MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
1084 for( unsigned i = 0; i < 9; i++ )
1086 MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
1088 MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
1089 MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
1091 double rotation = 0;//angle in degrees to be rotated clockwise
1092 double scale[2]; // scale factor; sx = scale[0] , sy = scale[1]
1093 int32_t *matrix = p_box->data.p_tkhd->i_matrix;
1095 scale[0] = sqrt(conv_fx(matrix[0]) * conv_fx(matrix[0]) +
1096 conv_fx(matrix[3]) * conv_fx(matrix[3]));
1097 scale[1] = sqrt(conv_fx(matrix[1]) * conv_fx(matrix[1]) +
1098 conv_fx(matrix[4]) * conv_fx(matrix[4]));
1100 if( likely(scale[0] > 0 && scale[1] > 0) )
1102 rotation = atan2(conv_fx(matrix[1]) / scale[1],
1103 conv_fx(matrix[0]) / scale[0]) * 180 / M_PI;
1104 if (rotation < 0)
1105 rotation += 360.;
1108 p_box->data.p_tkhd->f_rotation = rotation;
1110 #ifdef MP4_VERBOSE
1111 double translate[2];// amount to translate; tx = translate[0] , ty = translate[1]
1113 translate[0] = conv_fx(matrix[6]);
1114 translate[1] = conv_fx(matrix[7]);
1116 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
1117 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time, false );
1118 MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration, true );
1120 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. "
1121 "Matrix: %i %i %i %i %i %i %i %i %i",
1122 s_creation_time,
1123 s_modification_time,
1124 s_duration,
1125 p_box->data.p_tkhd->i_track_ID,
1126 p_box->data.p_tkhd->i_layer,
1127 (float)p_box->data.p_tkhd->i_volume / 256 ,
1128 rotation,
1129 scale[0],
1130 scale[1],
1131 translate[0],
1132 translate[1],
1133 (float)p_box->data.p_tkhd->i_width / BLOCK16x16,
1134 (float)p_box->data.p_tkhd->i_height / BLOCK16x16,
1135 p_box->data.p_tkhd->i_matrix[0],
1136 p_box->data.p_tkhd->i_matrix[1],
1137 p_box->data.p_tkhd->i_matrix[2],
1138 p_box->data.p_tkhd->i_matrix[3],
1139 p_box->data.p_tkhd->i_matrix[4],
1140 p_box->data.p_tkhd->i_matrix[5],
1141 p_box->data.p_tkhd->i_matrix[6],
1142 p_box->data.p_tkhd->i_matrix[7],
1143 p_box->data.p_tkhd->i_matrix[8] );
1144 #endif
1145 MP4_READBOX_EXIT( 1 );
1148 static int MP4_ReadBox_load( stream_t *p_stream, MP4_Box_t *p_box )
1150 if ( p_box->i_size != 24 )
1151 return 0;
1152 MP4_READBOX_ENTER( MP4_Box_data_load_t, NULL );
1153 MP4_GET4BYTES( p_box->data.p_load->i_start_time );
1154 MP4_GET4BYTES( p_box->data.p_load->i_duration );
1155 MP4_GET4BYTES( p_box->data.p_load->i_flags );
1156 MP4_GET4BYTES( p_box->data.p_load->i_hints );
1157 MP4_READBOX_EXIT( 1 );
1160 static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
1162 uint16_t i_language;
1163 #ifdef MP4_VERBOSE
1164 char s_creation_time[128];
1165 char s_modification_time[128];
1166 char s_duration[128];
1167 #endif
1168 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t, NULL );
1170 MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
1172 if( p_box->data.p_mdhd->i_version )
1174 MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
1175 MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
1176 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1177 MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
1179 else
1181 MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
1182 MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
1183 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1184 MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
1187 MP4_GET2BYTES( i_language );
1188 decodeQtLanguageCode( i_language, p_box->data.p_mdhd->rgs_language,
1189 &p_box->data.p_mdhd->b_mac_encoding );
1191 MP4_GET2BYTES( p_box->data.p_mdhd->i_quality );
1193 #ifdef MP4_VERBOSE
1194 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time, false );
1195 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time, false );
1196 MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration, true );
1197 msg_Dbg( p_stream, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %3.3s",
1198 s_creation_time,
1199 s_modification_time,
1200 (uint32_t)p_box->data.p_mdhd->i_timescale,
1201 s_duration,
1202 (char*) &p_box->data.p_mdhd->rgs_language );
1203 #endif
1204 MP4_READBOX_EXIT( 1 );
1207 static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
1209 FREENULL( p_box->data.p_hdlr->psz_name );
1212 static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
1214 int32_t i_reserved;
1215 VLC_UNUSED(i_reserved);
1217 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t, MP4_FreeBox_hdlr );
1219 MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
1221 MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
1222 MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
1224 MP4_GET4BYTES( i_reserved );
1225 MP4_GET4BYTES( i_reserved );
1226 MP4_GET4BYTES( i_reserved );
1227 p_box->data.p_hdlr->psz_name = NULL;
1229 if( i_read > 0 )
1231 uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_read + 1 );
1232 if( unlikely( psz == NULL ) )
1233 MP4_READBOX_EXIT( 0 );
1235 /* Yes, I love .mp4 :( */
1236 if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
1238 uint8_t i_len;
1239 int i_copy;
1241 MP4_GET1BYTE( i_len );
1242 i_copy = __MIN( i_read, i_len );
1244 memcpy( psz, p_peek, i_copy );
1245 p_box->data.p_hdlr->psz_name[i_copy] = '\0';
1247 else
1249 memcpy( psz, p_peek, i_read );
1250 p_box->data.p_hdlr->psz_name[i_read] = '\0';
1254 #ifdef MP4_VERBOSE
1255 msg_Dbg( p_stream, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
1256 (char*)&p_box->data.p_hdlr->i_handler_type,
1257 p_box->data.p_hdlr->psz_name );
1259 #endif
1260 MP4_READBOX_EXIT( 1 );
1263 static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
1265 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t, NULL );
1267 MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
1269 MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
1270 for( unsigned i = 0; i < 3; i++ )
1272 MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
1275 #ifdef MP4_VERBOSE
1276 msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
1277 p_box->data.p_vmhd->i_graphics_mode,
1278 p_box->data.p_vmhd->i_opcolor[0],
1279 p_box->data.p_vmhd->i_opcolor[1],
1280 p_box->data.p_vmhd->i_opcolor[2] );
1281 #endif
1282 MP4_READBOX_EXIT( 1 );
1285 static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
1287 MP4_READBOX_ENTER( MP4_Box_data_smhd_t, NULL );
1289 MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
1293 MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
1295 MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
1297 #ifdef MP4_VERBOSE
1298 msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
1299 (float)p_box->data.p_smhd->i_balance / 256 );
1300 #endif
1301 MP4_READBOX_EXIT( 1 );
1305 static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
1307 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t, NULL );
1309 MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
1311 MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
1312 MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
1314 MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
1315 MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
1317 MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
1319 #ifdef MP4_VERBOSE
1320 msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1321 p_box->data.p_hmhd->i_max_PDU_size,
1322 p_box->data.p_hmhd->i_avg_PDU_size,
1323 p_box->data.p_hmhd->i_max_bitrate,
1324 p_box->data.p_hmhd->i_avg_bitrate );
1325 #endif
1326 MP4_READBOX_EXIT( 1 );
1329 static void MP4_FreeBox_url( MP4_Box_t *p_box )
1331 FREENULL( p_box->data.p_url->psz_location );
1334 static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
1336 MP4_READBOX_ENTER( MP4_Box_data_url_t, MP4_FreeBox_url );
1338 MP4_GETVERSIONFLAGS( p_box->data.p_url );
1339 MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
1341 #ifdef MP4_VERBOSE
1342 msg_Dbg( p_stream, "read box: \"url\" url: %s",
1343 p_box->data.p_url->psz_location );
1345 #endif
1346 MP4_READBOX_EXIT( 1 );
1349 static void MP4_FreeBox_urn( MP4_Box_t *p_box )
1351 FREENULL( p_box->data.p_urn->psz_name );
1352 FREENULL( p_box->data.p_urn->psz_location );
1355 static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
1357 MP4_READBOX_ENTER( MP4_Box_data_urn_t, MP4_FreeBox_urn );
1359 MP4_GETVERSIONFLAGS( p_box->data.p_urn );
1361 MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
1362 MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
1364 #ifdef MP4_VERBOSE
1365 msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
1366 p_box->data.p_urn->psz_name,
1367 p_box->data.p_urn->psz_location );
1368 #endif
1369 MP4_READBOX_EXIT( 1 );
1372 static int MP4_ReadBox_LtdContainer( stream_t *p_stream, MP4_Box_t *p_box )
1374 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_lcont_t, 16, NULL );
1375 if( i_read < 8 )
1376 MP4_READBOX_EXIT( 0 );
1378 MP4_GETVERSIONFLAGS( p_box->data.p_lcont );
1379 if( p_box->data.p_lcont->i_version != 0 )
1380 MP4_READBOX_EXIT( 0 );
1381 MP4_GET4BYTES( p_box->data.p_lcont->i_entry_count );
1383 uint32_t i_entry = 0;
1384 i_read = p_box->i_size - 16;
1385 while (i_read > 8 && i_entry < p_box->data.p_lcont->i_entry_count )
1387 MP4_Box_t *p_childbox = MP4_ReadBox( p_stream, p_box );
1388 if( !p_childbox )
1389 break;
1390 MP4_BoxAddChild( p_box, p_childbox );
1391 i_entry++;
1392 i_read -= p_childbox->i_size;
1395 if (i_entry != p_box->data.p_lcont->i_entry_count)
1396 p_box->data.p_lcont->i_entry_count = i_entry;
1398 #ifdef MP4_VERBOSE
1399 msg_Dbg( p_stream, "read box: \"%4.4s\" entry-count %d", (char *)&p_box->i_type,
1400 p_box->data.p_lcont->i_entry_count );
1402 #endif
1404 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
1405 MP4_READBOX_EXIT( 0 );
1407 MP4_READBOX_EXIT( 1 );
1410 static void MP4_FreeBox_stts( MP4_Box_t *p_box )
1412 FREENULL( p_box->data.p_stts->pi_sample_count );
1413 FREENULL( p_box->data.p_stts->pi_sample_delta );
1416 static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
1418 MP4_READBOX_ENTER( MP4_Box_data_stts_t, MP4_FreeBox_stts );
1420 MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1421 MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );
1423 p_box->data.p_stts->pi_sample_count =
1424 calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
1425 p_box->data.p_stts->pi_sample_delta =
1426 calloc( p_box->data.p_stts->i_entry_count, sizeof(int32_t) );
1427 if( p_box->data.p_stts->pi_sample_count == NULL
1428 || p_box->data.p_stts->pi_sample_delta == NULL )
1430 MP4_READBOX_EXIT( 0 );
1433 uint32_t i = 0;
1434 for( ; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
1436 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_count[i] );
1437 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_delta[i] );
1440 if ( i < p_box->data.p_stts->i_entry_count )
1441 p_box->data.p_stts->i_entry_count = i;
1443 #ifdef MP4_VERBOSE
1444 msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
1445 p_box->data.p_stts->i_entry_count );
1447 #endif
1448 MP4_READBOX_EXIT( 1 );
1452 static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
1454 FREENULL( p_box->data.p_ctts->pi_sample_count );
1455 FREENULL( p_box->data.p_ctts->pi_sample_offset );
1458 static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
1460 MP4_READBOX_ENTER( MP4_Box_data_ctts_t, MP4_FreeBox_ctts );
1462 MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1464 MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );
1466 p_box->data.p_ctts->pi_sample_count =
1467 calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
1468 p_box->data.p_ctts->pi_sample_offset =
1469 calloc( p_box->data.p_ctts->i_entry_count, sizeof(int32_t) );
1470 if( ( p_box->data.p_ctts->pi_sample_count == NULL )
1471 || ( p_box->data.p_ctts->pi_sample_offset == NULL ) )
1473 MP4_READBOX_EXIT( 0 );
1476 uint32_t i = 0;
1477 for( ; (i < p_box->data.p_ctts->i_entry_count )&&( i_read >=8 ); i++ )
1479 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_count[i] );
1480 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_offset[i] );
1482 if ( i < p_box->data.p_ctts->i_entry_count )
1483 p_box->data.p_ctts->i_entry_count = i;
1485 #ifdef MP4_VERBOSE
1486 msg_Dbg( p_stream, "read box: \"ctts\" entry-count %d",
1487 p_box->data.p_ctts->i_entry_count );
1489 #endif
1490 MP4_READBOX_EXIT( 1 );
1493 static int MP4_ReadBox_cslg( stream_t *p_stream, MP4_Box_t *p_box )
1495 MP4_READBOX_ENTER( MP4_Box_data_cslg_t, NULL );
1497 unsigned i_version, i_flags;
1498 MP4_GET1BYTE( i_version );
1499 MP4_GET3BYTES( i_flags );
1500 VLC_UNUSED(i_flags);
1502 if( i_version > 1 )
1503 MP4_READBOX_EXIT( 0 );
1505 #define READ_CSLG(readbytes) {\
1506 readbytes( p_box->data.p_cslg->ct_to_dts_shift );\
1507 readbytes( p_box->data.p_cslg->i_least_delta );\
1508 readbytes( p_box->data.p_cslg->i_max_delta );\
1509 readbytes( p_box->data.p_cslg->i_composition_starttime );\
1510 readbytes( p_box->data.p_cslg->i_composition_endtime ); }
1512 if( i_version == 0 )
1513 READ_CSLG(MP4_GET4BYTES)
1514 else
1515 READ_CSLG(MP4_GET8BYTES)
1517 MP4_READBOX_EXIT( 1 );
1520 static int MP4_ReadLengthDescriptor( uint8_t **pp_peek, int64_t *i_read )
1522 unsigned int i_b;
1523 unsigned int i_len = 0;
1526 i_b = **pp_peek;
1528 (*pp_peek)++;
1529 (*i_read)--;
1530 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1531 } while( i_b&0x80 );
1532 return( i_len );
1536 static void MP4_FreeBox_esds( MP4_Box_t *p_box )
1538 FREENULL( p_box->data.p_esds->es_descriptor.psz_URL );
1539 if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1541 FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1542 FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1546 static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
1548 #define es_descriptor p_box->data.p_esds->es_descriptor
1549 unsigned int i_len;
1550 unsigned int i_flags;
1551 unsigned int i_type;
1553 MP4_READBOX_ENTER( MP4_Box_data_esds_t, MP4_FreeBox_esds );
1555 MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1558 MP4_GET1BYTE( i_type );
1559 if( i_type == 0x03 ) /* MP4ESDescrTag ISO/IEC 14496-1 8.3.3 */
1561 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1563 #ifdef MP4_VERBOSE
1564 msg_Dbg( p_stream, "found esds MPEG4ESDescr (%dBytes)",
1565 i_len );
1566 #endif
1568 MP4_GET2BYTES( es_descriptor.i_ES_ID );
1569 MP4_GET1BYTE( i_flags );
1570 es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1571 es_descriptor.b_url = ( (i_flags&0x40) != 0);
1572 es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1574 es_descriptor.i_stream_priority = i_flags&0x1f;
1575 if( es_descriptor.b_stream_dependence )
1577 MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1579 if( es_descriptor.b_url && i_read > 0 )
1581 uint8_t i_url;
1583 MP4_GET1BYTE( i_url );
1584 if( i_url > i_read )
1585 MP4_READBOX_EXIT( 1 );
1586 es_descriptor.psz_URL = malloc( (unsigned) i_url + 1 );
1587 if( es_descriptor.psz_URL )
1589 memcpy( es_descriptor.psz_URL, p_peek, i_url );
1590 es_descriptor.psz_URL[i_url] = 0;
1592 p_peek += i_url;
1593 i_read -= i_url;
1595 else
1597 es_descriptor.psz_URL = NULL;
1599 if( es_descriptor.b_OCRstream )
1601 MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1603 MP4_GET1BYTE( i_type ); /* get next type */
1606 if( i_type != 0x04)/* MP4DecConfigDescrTag ISO/IEC 14496-1 8.3.4 */
1608 es_descriptor.p_decConfigDescr = NULL;
1609 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1612 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1614 #ifdef MP4_VERBOSE
1615 msg_Dbg( p_stream, "found esds MP4DecConfigDescr (%dBytes)",
1616 i_len );
1617 #endif
1619 es_descriptor.p_decConfigDescr =
1620 calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
1621 if( unlikely( es_descriptor.p_decConfigDescr == NULL ) )
1622 MP4_READBOX_EXIT( 0 );
1624 MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectProfileIndication );
1625 MP4_GET1BYTE( i_flags );
1626 es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1627 es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1628 MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1629 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1630 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1631 MP4_GET1BYTE( i_type );
1632 if( i_type != 0x05 )/* MP4DecSpecificDescrTag ISO/IEC 14496-1 8.3.5 */
1634 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1635 es_descriptor.p_decConfigDescr->p_decoder_specific_info = NULL;
1636 MP4_READBOX_EXIT( 1 );
1639 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1641 #ifdef MP4_VERBOSE
1642 msg_Dbg( p_stream, "found esds MP4DecSpecificDescr (%dBytes)",
1643 i_len );
1644 #endif
1645 if( i_len > i_read )
1646 MP4_READBOX_EXIT( 0 );
1648 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1649 es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1650 if( unlikely( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL ) )
1651 MP4_READBOX_EXIT( 0 );
1653 memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1654 p_peek, i_len );
1656 MP4_READBOX_EXIT( 1 );
1657 #undef es_descriptor
1660 static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
1662 MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1663 int i;
1665 if( p_avcC->i_avcC > 0 ) FREENULL( p_avcC->p_avcC );
1667 if( p_avcC->sps )
1669 for( i = 0; i < p_avcC->i_sps; i++ )
1670 FREENULL( p_avcC->sps[i] );
1672 if( p_avcC->pps )
1674 for( i = 0; i < p_avcC->i_pps; i++ )
1675 FREENULL( p_avcC->pps[i] );
1677 if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->sps );
1678 if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->i_sps_length );
1679 if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->pps );
1680 if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->i_pps_length );
1683 static int MP4_ReadBox_avcC( stream_t *p_stream, MP4_Box_t *p_box )
1685 MP4_Box_data_avcC_t *p_avcC;
1686 int i;
1688 MP4_READBOX_ENTER( MP4_Box_data_avcC_t, MP4_FreeBox_avcC );
1689 p_avcC = p_box->data.p_avcC;
1691 p_avcC->i_avcC = i_read;
1692 if( p_avcC->i_avcC > 0 )
1694 uint8_t * p = p_avcC->p_avcC = malloc( p_avcC->i_avcC );
1695 if( p )
1696 memcpy( p, p_peek, i_read );
1699 MP4_GET1BYTE( p_avcC->i_version );
1700 MP4_GET1BYTE( p_avcC->i_profile );
1701 MP4_GET1BYTE( p_avcC->i_profile_compatibility );
1702 MP4_GET1BYTE( p_avcC->i_level );
1703 MP4_GET1BYTE( p_avcC->i_reserved1 );
1704 p_avcC->i_length_size = (p_avcC->i_reserved1&0x03) + 1;
1705 p_avcC->i_reserved1 >>= 2;
1707 MP4_GET1BYTE( p_avcC->i_reserved2 );
1708 p_avcC->i_sps = p_avcC->i_reserved2&0x1f;
1709 p_avcC->i_reserved2 >>= 5;
1711 if( p_avcC->i_sps > 0 )
1713 p_avcC->i_sps_length = calloc( p_avcC->i_sps, sizeof( uint16_t ) );
1714 p_avcC->sps = calloc( p_avcC->i_sps, sizeof( uint8_t* ) );
1716 if( !p_avcC->i_sps_length || !p_avcC->sps )
1717 goto error;
1719 for( i = 0; i < p_avcC->i_sps && i_read > 2; i++ )
1721 MP4_GET2BYTES( p_avcC->i_sps_length[i] );
1722 if ( p_avcC->i_sps_length[i] > i_read )
1723 goto error;
1724 p_avcC->sps[i] = malloc( p_avcC->i_sps_length[i] );
1725 if( p_avcC->sps[i] )
1726 memcpy( p_avcC->sps[i], p_peek, p_avcC->i_sps_length[i] );
1728 p_peek += p_avcC->i_sps_length[i];
1729 i_read -= p_avcC->i_sps_length[i];
1731 if ( i != p_avcC->i_sps )
1732 goto error;
1735 MP4_GET1BYTE( p_avcC->i_pps );
1736 if( p_avcC->i_pps > 0 )
1738 p_avcC->i_pps_length = calloc( p_avcC->i_pps, sizeof( uint16_t ) );
1739 p_avcC->pps = calloc( p_avcC->i_pps, sizeof( uint8_t* ) );
1741 if( !p_avcC->i_pps_length || !p_avcC->pps )
1742 goto error;
1744 for( i = 0; i < p_avcC->i_pps && i_read > 2; i++ )
1746 MP4_GET2BYTES( p_avcC->i_pps_length[i] );
1747 if( p_avcC->i_pps_length[i] > i_read )
1748 goto error;
1749 p_avcC->pps[i] = malloc( p_avcC->i_pps_length[i] );
1750 if( p_avcC->pps[i] )
1751 memcpy( p_avcC->pps[i], p_peek, p_avcC->i_pps_length[i] );
1753 p_peek += p_avcC->i_pps_length[i];
1754 i_read -= p_avcC->i_pps_length[i];
1756 if ( i != p_avcC->i_pps )
1757 goto error;
1759 #ifdef MP4_VERBOSE
1760 msg_Dbg( p_stream,
1761 "read box: \"avcC\" version=%d profile=0x%x level=0x%x length size=%d sps=%d pps=%d",
1762 p_avcC->i_version, p_avcC->i_profile, p_avcC->i_level,
1763 p_avcC->i_length_size,
1764 p_avcC->i_sps, p_avcC->i_pps );
1765 for( i = 0; i < p_avcC->i_sps; i++ )
1767 msg_Dbg( p_stream, " - sps[%d] length=%d",
1768 i, p_avcC->i_sps_length[i] );
1770 for( i = 0; i < p_avcC->i_pps; i++ )
1772 msg_Dbg( p_stream, " - pps[%d] length=%d",
1773 i, p_avcC->i_pps_length[i] );
1776 #endif
1777 MP4_READBOX_EXIT( 1 );
1779 error:
1780 MP4_FreeBox_avcC( p_box );
1781 MP4_READBOX_EXIT( 0 );
1784 static void MP4_FreeBox_vpcC( MP4_Box_t *p_box )
1786 free( p_box->data.p_vpcC->p_codec_init_data );
1789 static int MP4_ReadBox_vpcC( stream_t *p_stream, MP4_Box_t *p_box )
1791 MP4_READBOX_ENTER( MP4_Box_data_vpcC_t, MP4_FreeBox_vpcC );
1792 MP4_Box_data_vpcC_t *p_vpcC = p_box->data.p_vpcC;
1794 if( p_box->i_size < 6 )
1795 MP4_READBOX_EXIT( 0 );
1797 uint8_t i_version;
1798 MP4_GET1BYTE( i_version );
1799 if( i_version != 0 )
1800 MP4_READBOX_EXIT( 0 );
1802 MP4_GET1BYTE( p_vpcC->i_profile );
1803 MP4_GET1BYTE( p_vpcC->i_level );
1804 MP4_GET1BYTE( p_vpcC->i_bit_depth );
1805 p_vpcC->i_color_space = p_vpcC->i_bit_depth & 0x0F;
1806 p_vpcC->i_bit_depth >>= 4;
1807 MP4_GET1BYTE( p_vpcC->i_chroma_subsampling );
1808 p_vpcC->i_xfer_function = ( p_vpcC->i_chroma_subsampling & 0x0F ) >> 1;
1809 p_vpcC->i_fullrange = p_vpcC->i_chroma_subsampling & 0x01;
1810 p_vpcC->i_chroma_subsampling >>= 4;
1811 MP4_GET2BYTES( p_vpcC->i_codec_init_datasize );
1812 if( p_vpcC->i_codec_init_datasize > i_read )
1813 p_vpcC->i_codec_init_datasize = i_read;
1815 if( p_vpcC->i_codec_init_datasize )
1817 p_vpcC->p_codec_init_data = malloc( i_read );
1818 if( !p_vpcC->p_codec_init_data )
1819 MP4_READBOX_EXIT( 0 );
1820 memcpy( p_vpcC->p_codec_init_data, p_peek, i_read );
1823 MP4_READBOX_EXIT( 1 );
1826 static void MP4_FreeBox_WMA2( MP4_Box_t *p_box )
1828 FREENULL( p_box->data.p_WMA2->p_extra );
1831 static int MP4_ReadBox_WMA2( stream_t *p_stream, MP4_Box_t *p_box )
1833 MP4_READBOX_ENTER( MP4_Box_data_WMA2_t, MP4_FreeBox_WMA2 );
1835 MP4_Box_data_WMA2_t *p_WMA2 = p_box->data.p_WMA2;
1837 MP4_GET2BYTESLE( p_WMA2->Format.wFormatTag );
1838 MP4_GET2BYTESLE( p_WMA2->Format.nChannels );
1839 MP4_GET4BYTESLE( p_WMA2->Format.nSamplesPerSec );
1840 MP4_GET4BYTESLE( p_WMA2->Format.nAvgBytesPerSec );
1841 MP4_GET2BYTESLE( p_WMA2->Format.nBlockAlign );
1842 MP4_GET2BYTESLE( p_WMA2->Format.wBitsPerSample );
1844 uint16_t i_cbSize;
1845 MP4_GET2BYTESLE( i_cbSize );
1847 if ( i_read < 0 || i_cbSize > i_read )
1848 goto error;
1850 p_WMA2->i_extra = i_cbSize;
1851 if ( p_WMA2->i_extra )
1853 p_WMA2->p_extra = malloc( p_WMA2->i_extra );
1854 if ( ! p_WMA2->p_extra )
1855 goto error;
1856 memcpy( p_WMA2->p_extra, p_peek, p_WMA2->i_extra );
1859 MP4_READBOX_EXIT( 1 );
1861 error:
1862 MP4_READBOX_EXIT( 0 );
1865 static void MP4_FreeBox_strf( MP4_Box_t *p_box )
1867 FREENULL( p_box->data.p_strf->p_extra );
1870 static int MP4_ReadBox_strf( stream_t *p_stream, MP4_Box_t *p_box )
1872 MP4_READBOX_ENTER( MP4_Box_data_strf_t, MP4_FreeBox_strf );
1874 MP4_Box_data_strf_t *p_strf = p_box->data.p_strf;
1876 MP4_GET4BYTESLE( p_strf->bmiHeader.biSize );
1877 MP4_GET4BYTESLE( p_strf->bmiHeader.biWidth );
1878 MP4_GET4BYTESLE( p_strf->bmiHeader.biHeight );
1879 MP4_GET2BYTESLE( p_strf->bmiHeader.biPlanes );
1880 MP4_GET2BYTESLE( p_strf->bmiHeader.biBitCount );
1881 MP4_GETFOURCC( p_strf->bmiHeader.biCompression );
1882 MP4_GET4BYTESLE( p_strf->bmiHeader.biSizeImage );
1883 MP4_GET4BYTESLE( p_strf->bmiHeader.biXPelsPerMeter );
1884 MP4_GET4BYTESLE( p_strf->bmiHeader.biYPelsPerMeter );
1885 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrUsed );
1886 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrImportant );
1888 if ( i_read < 0 )
1889 goto error;
1891 p_strf->i_extra = i_read;
1892 if ( p_strf->i_extra )
1894 p_strf->p_extra = malloc( p_strf->i_extra );
1895 if ( ! p_strf->p_extra )
1896 goto error;
1897 memcpy( p_strf->p_extra, p_peek, i_read );
1900 MP4_READBOX_EXIT( 1 );
1902 error:
1903 MP4_READBOX_EXIT( 0 );
1906 static int MP4_ReadBox_ASF( stream_t *p_stream, MP4_Box_t *p_box )
1908 MP4_READBOX_ENTER( MP4_Box_data_ASF_t, NULL );
1910 MP4_Box_data_ASF_t *p_asf = p_box->data.p_asf;
1912 if (i_read != 8)
1913 MP4_READBOX_EXIT( 0 );
1915 MP4_GET1BYTE( p_asf->i_stream_number );
1916 /* remaining is unknown */
1918 MP4_READBOX_EXIT( 1 );
1921 static void MP4_FreeBox_sbgp( MP4_Box_t *p_box )
1923 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
1924 free( p_sbgp->entries.pi_sample_count );
1925 free( p_sbgp->entries.pi_group_description_index );
1928 static int MP4_ReadBox_sbgp( stream_t *p_stream, MP4_Box_t *p_box )
1930 MP4_READBOX_ENTER( MP4_Box_data_sbgp_t, MP4_FreeBox_sbgp );
1931 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
1932 uint32_t i_flags;
1934 if ( i_read < 12 )
1935 MP4_READBOX_EXIT( 0 );
1937 MP4_GET1BYTE( p_sbgp->i_version );
1938 MP4_GET3BYTES( i_flags );
1939 if( i_flags != 0 )
1940 MP4_READBOX_EXIT( 0 );
1942 MP4_GETFOURCC( p_sbgp->i_grouping_type );
1944 if( p_sbgp->i_version == 1 )
1946 if( i_read < 8 )
1947 MP4_READBOX_EXIT( 0 );
1948 MP4_GET4BYTES( p_sbgp->i_grouping_type_parameter );
1951 MP4_GET4BYTES( p_sbgp->i_entry_count );
1952 if( p_sbgp->i_entry_count > i_read / (4 + 4) )
1953 p_sbgp->i_entry_count = i_read / (4 + 4);
1955 p_sbgp->entries.pi_sample_count = malloc( p_sbgp->i_entry_count * sizeof(uint32_t) );
1956 p_sbgp->entries.pi_group_description_index = malloc( p_sbgp->i_entry_count * sizeof(uint32_t) );
1958 if( !p_sbgp->entries.pi_sample_count || !p_sbgp->entries.pi_group_description_index )
1960 MP4_FreeBox_sbgp( p_box );
1961 MP4_READBOX_EXIT( 0 );
1964 for( uint32_t i=0; i<p_sbgp->i_entry_count; i++ )
1966 MP4_GET4BYTES( p_sbgp->entries.pi_sample_count[i] );
1967 MP4_GET4BYTES( p_sbgp->entries.pi_group_description_index[i] );
1970 #ifdef MP4_VERBOSE
1971 msg_Dbg( p_stream,
1972 "read box: \"sbgp\" grouping type %4.4s", (char*) &p_sbgp->i_grouping_type );
1973 #ifdef MP4_ULTRA_VERBOSE
1974 for (uint32_t i = 0; i < p_sbgp->i_entry_count; i++)
1975 msg_Dbg( p_stream, "\t samples %" PRIu32 " group %" PRIu32,
1976 p_sbgp->entries.pi_sample_count[i],
1977 p_sbgp->entries.pi_group_description_index[i] );
1978 #endif
1979 #endif
1981 MP4_READBOX_EXIT( 1 );
1984 static void MP4_FreeBox_sgpd( MP4_Box_t *p_box )
1986 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
1987 free( p_sgpd->p_entries );
1990 static int MP4_ReadBox_sgpd( stream_t *p_stream, MP4_Box_t *p_box )
1992 MP4_READBOX_ENTER( MP4_Box_data_sgpd_t, MP4_FreeBox_sgpd );
1993 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
1994 uint32_t i_flags;
1995 uint32_t i_default_length = 0;
1997 if ( i_read < 8 )
1998 MP4_READBOX_EXIT( 0 );
2000 MP4_GET1BYTE( p_sgpd->i_version );
2001 MP4_GET3BYTES( i_flags );
2002 if( i_flags != 0 )
2003 MP4_READBOX_EXIT( 0 );
2005 MP4_GETFOURCC( p_sgpd->i_grouping_type );
2007 switch( p_sgpd->i_grouping_type )
2009 case SAMPLEGROUP_rap:
2010 break;
2012 default:
2013 #ifdef MP4_VERBOSE
2014 msg_Dbg( p_stream,
2015 "read box: \"sgpd\" grouping type %4.4s (unimplemented)", (char*) &p_sgpd->i_grouping_type );
2016 #endif
2017 MP4_READBOX_EXIT( 1 );
2020 if( p_sgpd->i_version == 1 )
2022 if( i_read < 8 )
2023 MP4_READBOX_EXIT( 0 );
2024 MP4_GET4BYTES( i_default_length );
2026 else if( p_sgpd->i_version >= 2 )
2028 if( i_read < 8 )
2029 MP4_READBOX_EXIT( 0 );
2030 MP4_GET4BYTES( p_sgpd->i_default_sample_description_index );
2033 MP4_GET4BYTES( p_sgpd->i_entry_count );
2035 p_sgpd->p_entries = malloc( p_sgpd->i_entry_count * sizeof(*p_sgpd->p_entries) );
2036 if( !p_sgpd->p_entries )
2037 MP4_READBOX_EXIT( 0 );
2039 uint32_t i = 0;
2040 for( ; i<p_sgpd->i_entry_count; i++ )
2042 uint32_t i_description_length = i_default_length;
2043 if( p_sgpd->i_version == 1 && i_default_length == 0 )
2045 if( i_read < 4 )
2046 break;
2047 MP4_GET4BYTES( i_description_length );
2050 if( p_sgpd->i_version == 1 && i_read < i_description_length )
2051 break;
2053 switch( p_sgpd->i_grouping_type )
2055 case SAMPLEGROUP_rap:
2057 if( i_read < 1 )
2059 p_sgpd->i_entry_count = 0;
2060 MP4_FreeBox_sgpd( p_box );
2061 MP4_READBOX_EXIT( 0 );
2063 uint8_t i_data;
2064 MP4_GET1BYTE( i_data );
2065 p_sgpd->p_entries[i].rap.i_num_leading_samples_known = i_data & 0x80;
2066 p_sgpd->p_entries[i].rap.i_num_leading_samples = i_data & 0x7F;
2068 break;
2070 default:
2071 assert(0);
2075 if( i != p_sgpd->i_entry_count )
2076 p_sgpd->i_entry_count = i;
2078 #ifdef MP4_VERBOSE
2079 msg_Dbg( p_stream,
2080 "read box: \"sgpd\" grouping type %4.4s", (char*) &p_sgpd->i_grouping_type );
2081 #endif
2083 MP4_READBOX_EXIT( 1 );
2086 static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
2088 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2089 free( p_chan->layout.p_descriptions );
2092 static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
2094 MP4_READBOX_ENTER( MP4_Box_data_chan_t, MP4_FreeBox_stsdext_chan );
2095 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2097 if ( i_read < 16 )
2098 MP4_READBOX_EXIT( 0 );
2100 MP4_GET1BYTE( p_chan->i_version );
2101 MP4_GET3BYTES( p_chan->i_channels_flags );
2102 MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
2103 MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
2104 MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
2106 size_t i_descsize = 8 + 3 * sizeof(float);
2107 if ( (size_t)i_read < p_chan->layout.i_channels_description_count * i_descsize )
2108 MP4_READBOX_EXIT( 0 );
2110 p_chan->layout.p_descriptions =
2111 malloc( p_chan->layout.i_channels_description_count * i_descsize );
2113 if ( !p_chan->layout.p_descriptions )
2114 MP4_READBOX_EXIT( 0 );
2116 uint32_t i;
2117 for( i=0; i<p_chan->layout.i_channels_description_count; i++ )
2119 if ( i_read < 20 )
2120 break;
2121 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
2122 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
2123 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
2124 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
2125 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
2127 if ( i<p_chan->layout.i_channels_description_count )
2128 p_chan->layout.i_channels_description_count = i;
2130 #ifdef MP4_VERBOSE
2131 msg_Dbg( p_stream,
2132 "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
2133 p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
2134 p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
2135 #endif
2136 MP4_READBOX_EXIT( 1 );
2139 static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
2141 MP4_READBOX_ENTER( MP4_Box_data_dec3_t, NULL );
2143 MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
2145 unsigned i_header;
2146 MP4_GET2BYTES( i_header );
2148 p_dec3->i_data_rate = i_header >> 3;
2149 p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
2150 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
2151 MP4_GET3BYTES( i_header );
2152 p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
2153 p_dec3->stream[i].i_bsid = ( i_header >> 17 ) & 0x01f;
2154 p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
2155 p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
2156 p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
2157 p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
2158 if (p_dec3->stream[i].i_num_dep_sub) {
2159 MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
2160 p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
2161 } else
2162 p_dec3->stream[i].i_chan_loc = 0;
2165 #ifdef MP4_VERBOSE
2166 msg_Dbg( p_stream,
2167 "read box: \"dec3\" bitrate %dkbps %d independent substreams",
2168 p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
2170 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
2171 msg_Dbg( p_stream,
2172 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
2173 "num dependent subs=%d chan_loc=0x%x",
2174 i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
2175 p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
2176 #endif
2177 MP4_READBOX_EXIT( 1 );
2180 static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
2182 MP4_Box_data_dac3_t *p_dac3;
2183 MP4_READBOX_ENTER( MP4_Box_data_dac3_t, NULL );
2185 p_dac3 = p_box->data.p_dac3;
2187 unsigned i_header;
2188 MP4_GET3BYTES( i_header );
2190 p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
2191 p_dac3->i_bsid = ( i_header >> 17 ) & 0x01f;
2192 p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
2193 p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
2194 p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
2195 p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
2197 #ifdef MP4_VERBOSE
2198 msg_Dbg( p_stream,
2199 "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
2200 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 );
2201 #endif
2202 MP4_READBOX_EXIT( 1 );
2205 static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
2207 MP4_READBOX_ENTER( MP4_Box_data_dvc1_t, NULL );
2208 if( i_read < 7 )
2209 MP4_READBOX_EXIT( 0 );
2211 MP4_Box_data_dvc1_t *p_dvc1 = p_box->data.p_dvc1;
2212 MP4_GET1BYTE( p_dvc1->i_profile_level );
2213 p_dvc1->i_vc1 = i_read; /* Header + profile_level */
2214 if( p_dvc1->i_vc1 > 0 && (p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 )) )
2215 memcpy( p_dvc1->p_vc1, p_peek, i_read );
2217 #ifdef MP4_VERBOSE
2218 msg_Dbg( p_stream,
2219 "read box: \"dvc1\" profile=%"PRIu8, (p_dvc1->i_profile_level & 0xf0) >> 4 );
2220 #endif
2222 MP4_READBOX_EXIT( 1 );
2225 static int MP4_ReadBox_fiel( stream_t *p_stream, MP4_Box_t *p_box )
2227 MP4_Box_data_fiel_t *p_fiel;
2228 MP4_READBOX_ENTER( MP4_Box_data_fiel_t, NULL );
2229 p_fiel = p_box->data.p_fiel;
2230 if(i_read < 2)
2231 MP4_READBOX_EXIT( 0 );
2232 if(p_peek[0] == 1)
2234 p_fiel->i_flags = BLOCK_FLAG_SINGLE_FIELD;
2236 else if(p_peek[0] == 2) /* Interlaced */
2239 * 0 – There is only one field.
2240 * 1 – T is displayed earliest, T is stored first in the file.
2241 * 6 – B is displayed earliest, B is stored first in the file.
2242 * 9 – B is displayed earliest, T is stored first in the file.
2243 * 14 – T is displayed earliest, B is stored first in the file.
2245 if(p_peek[1] == 1 || p_peek[1] == 9)
2246 p_fiel->i_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
2247 else if(p_peek[1] == 6 || p_peek[1] == 14)
2248 p_fiel->i_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
2250 MP4_READBOX_EXIT( 1 );
2253 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
2255 MP4_Box_data_enda_t *p_enda;
2256 MP4_READBOX_ENTER( MP4_Box_data_enda_t, NULL );
2258 p_enda = p_box->data.p_enda;
2260 MP4_GET2BYTES( p_enda->i_little_endian );
2262 #ifdef MP4_VERBOSE
2263 msg_Dbg( p_stream,
2264 "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
2265 #endif
2266 MP4_READBOX_EXIT( 1 );
2269 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
2271 FREENULL( p_box->data.p_sample_soun->p_qt_description );
2274 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
2276 p_box->i_handler = ATOM_soun;
2277 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t, MP4_FreeBox_sample_soun );
2278 p_box->data.p_sample_soun->p_qt_description = NULL;
2280 /* Sanity check needed because the "wave" box does also contain an
2281 * "mp4a" box that we don't understand. */
2282 if( i_read < 28 )
2284 i_read -= 30;
2285 MP4_READBOX_EXIT( 1 );
2288 for( unsigned i = 0; i < 6 ; i++ )
2290 MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
2293 MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
2296 * XXX hack -> produce a copy of the nearly complete chunk
2298 p_box->data.p_sample_soun->i_qt_description = 0;
2299 p_box->data.p_sample_soun->p_qt_description = NULL;
2300 if( i_read > 0 )
2302 p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
2303 if( p_box->data.p_sample_soun->p_qt_description )
2305 p_box->data.p_sample_soun->i_qt_description = i_read;
2306 memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
2310 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
2311 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
2312 MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
2314 MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
2315 MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
2316 MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
2317 MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
2318 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
2319 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
2321 #ifdef MP4_VERBOSE
2322 msg_Dbg( p_stream,
2323 "read box: \"soun\" stsd qt_version %"PRIu16" compid=%"PRIx16,
2324 p_box->data.p_sample_soun->i_qt_version,
2325 p_box->data.p_sample_soun->i_compressionid );
2326 #endif
2327 /* @36 bytes */
2328 if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
2330 /* SoundDescriptionV1 */
2331 MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
2332 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
2333 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
2334 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
2336 #ifdef MP4_VERBOSE
2337 msg_Dbg( p_stream,
2338 "read box: \"soun\" V1 sample/packet=%d bytes/packet=%d "
2339 "bytes/frame=%d bytes/sample=%d",
2340 p_box->data.p_sample_soun->i_sample_per_packet,
2341 p_box->data.p_sample_soun->i_bytes_per_packet,
2342 p_box->data.p_sample_soun->i_bytes_per_frame,
2343 p_box->data.p_sample_soun->i_bytes_per_sample );
2344 #endif
2345 /* @52 bytes */
2347 else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
2349 /* SoundDescriptionV2 */
2350 double f_sample_rate;
2351 int64_t i_dummy64;
2352 uint32_t i_channel, i_extoffset, i_dummy32;
2354 /* Checks */
2355 if ( p_box->data.p_sample_soun->i_channelcount != 0x3 ||
2356 p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
2357 p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
2358 p_box->data.p_sample_soun->i_reserved3 != 0x0 ||
2359 p_box->data.p_sample_soun->i_sampleratehi != 0x1 ||//65536
2360 p_box->data.p_sample_soun->i_sampleratelo != 0x0 ) //remainder
2362 msg_Err( p_stream, "invalid stsd V2 box defaults" );
2363 MP4_READBOX_EXIT( 0 );
2365 /* !Checks */
2367 MP4_GET4BYTES( i_extoffset ); /* offset to stsd extentions */
2368 MP4_GET8BYTES( i_dummy64 );
2369 memcpy( &f_sample_rate, &i_dummy64, 8 );
2370 msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
2371 /* Rounding error with lo, but we don't care since we do not support fractional audio rate */
2372 p_box->data.p_sample_soun->i_sampleratehi = (uint32_t)f_sample_rate;
2373 p_box->data.p_sample_soun->i_sampleratelo = (f_sample_rate - p_box->data.p_sample_soun->i_sampleratehi);
2375 MP4_GET4BYTES( i_channel );
2376 p_box->data.p_sample_soun->i_channelcount = i_channel;
2378 MP4_GET4BYTES( i_dummy32 );
2379 if ( i_dummy32 != 0x7F000000 )
2381 msg_Err( p_stream, "invalid stsd V2 box" );
2382 MP4_READBOX_EXIT( 0 );
2385 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
2386 MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
2387 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
2388 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2390 #ifdef MP4_VERBOSE
2391 msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
2392 "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
2393 f_sample_rate,
2394 p_box->data.p_sample_soun->i_constbitsperchannel,
2395 p_box->data.p_sample_soun->i_formatflags,
2396 p_box->data.p_sample_soun->i_constbytesperaudiopacket,
2397 p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2398 #endif
2399 /* @72 bytes + */
2400 if( i_extoffset > i_actually_read )
2401 i_extoffset = i_actually_read;
2402 p_peek = &p_buff[i_extoffset];
2403 i_read = i_actually_read - i_extoffset;
2405 else
2407 p_box->data.p_sample_soun->i_sample_per_packet = 0;
2408 p_box->data.p_sample_soun->i_bytes_per_packet = 0;
2409 p_box->data.p_sample_soun->i_bytes_per_frame = 0;
2410 p_box->data.p_sample_soun->i_bytes_per_sample = 0;
2412 #ifdef MP4_VERBOSE
2413 msg_Dbg( p_stream, "read box: \"soun\" V0 or qt1/2 (rest=%"PRId64")",
2414 i_read );
2415 #endif
2416 /* @36 bytes */
2419 if( p_box->i_type == ATOM_drms )
2421 msg_Warn( p_stream, "DRM protected streams are not supported." );
2422 MP4_READBOX_EXIT( 0 );
2425 if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
2427 /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
2428 p_box->data.p_sample_soun->i_channelcount = 1;
2431 /* Loads extensions */
2432 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2433 p_box->i_pos + p_peek - p_buff ); /* esds/wave/... */
2435 #ifdef MP4_VERBOSE
2436 msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
2437 "sample size %d sample rate %f",
2438 p_box->data.p_sample_soun->i_channelcount,
2439 p_box->data.p_sample_soun->i_samplesize,
2440 (float)p_box->data.p_sample_soun->i_sampleratehi +
2441 (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
2443 #endif
2444 MP4_READBOX_EXIT( 1 );
2447 static void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
2449 FREENULL( p_box->data.p_sample_vide->p_qt_image_description );
2452 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
2454 p_box->i_handler = ATOM_vide;
2455 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t, MP4_FreeBox_sample_vide );
2457 for( unsigned i = 0; i < 6 ; i++ )
2459 MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
2462 MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
2465 * XXX hack -> produce a copy of the nearly complete chunk
2467 if( i_read > 0 )
2469 p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
2470 if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
2471 MP4_READBOX_EXIT( 0 );
2472 p_box->data.p_sample_vide->i_qt_image_description = i_read;
2473 memcpy( p_box->data.p_sample_vide->p_qt_image_description,
2474 p_peek, i_read );
2476 else
2478 p_box->data.p_sample_vide->i_qt_image_description = 0;
2479 p_box->data.p_sample_vide->p_qt_image_description = NULL;
2482 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
2483 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
2484 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
2486 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
2487 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
2489 MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
2490 MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
2492 MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
2493 MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
2495 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
2496 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
2498 if ( i_read < 32 )
2499 MP4_READBOX_EXIT( 0 );
2500 if( p_peek[0] <= 31 ) // Must be Pascal String
2502 memcpy( &p_box->data.p_sample_vide->sz_compressorname, &p_peek[1], p_peek[0] );
2503 p_box->data.p_sample_vide->sz_compressorname[p_peek[0]] = 0;
2505 p_peek += 32; i_read -= 32;
2507 MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
2508 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
2510 if( p_box->i_type == ATOM_drmi )
2512 msg_Warn( p_stream, "DRM protected streams are not supported." );
2513 MP4_READBOX_EXIT( 0 );
2516 if( i_actually_read > 78 && p_peek - p_buff > 78 )
2518 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2519 p_box->i_pos + p_peek - p_buff );
2522 #ifdef MP4_VERBOSE
2523 msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d (%s)",
2524 p_box->data.p_sample_vide->i_width,
2525 p_box->data.p_sample_vide->i_height,
2526 p_box->data.p_sample_vide->i_depth,
2527 p_box->data.p_sample_vide->sz_compressorname );
2529 #endif
2530 MP4_READBOX_EXIT( 1 );
2533 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
2535 p_box->i_handler = ATOM_text;
2536 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_text_t, 16, NULL );
2537 if( i_read < 8 )
2538 MP4_READBOX_EXIT( 0 );
2540 MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
2542 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2543 MP4_READBOX_EXIT( 0 );
2545 MP4_READBOX_EXIT( 1 );
2548 static void MP4_FreeBox_sample_hint( MP4_Box_t *p_box )
2550 FREENULL( p_box->data.p_sample_hint->p_data );
2553 static int MP4_ReadBox_sample_hint8( stream_t *p_stream, MP4_Box_t *p_box )
2555 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_hint_t, 24, MP4_FreeBox_sample_hint );
2557 for( unsigned i = 0; i < 6 ; i++ )
2559 MP4_GET1BYTE( p_box->data.p_sample_hint->i_reserved1[i] );
2562 MP4_GET2BYTES( p_box->data.p_sample_hint->i_data_reference_index );
2564 if( !(p_box->data.p_sample_hint->p_data = malloc(8)) )
2565 MP4_READBOX_EXIT( 0 );
2567 MP4_GET8BYTES( *(p_box->data.p_sample_hint->p_data) );
2569 MP4_ReadBoxContainerChildren(p_stream, p_box, NULL);
2571 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2572 MP4_READBOX_EXIT( 0 );
2574 MP4_READBOX_EXIT( 1 );
2577 static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
2579 int32_t t;
2581 p_box->i_handler = ATOM_text;
2582 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t, NULL );
2584 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2585 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2587 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2589 MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2591 MP4_GET4BYTES( t );
2592 switch( t )
2594 /* FIXME search right signification */
2595 case 1: // Center
2596 p_box->data.p_sample_text->i_justification_horizontal = 1;
2597 p_box->data.p_sample_text->i_justification_vertical = 1;
2598 break;
2599 case -1: // Flush Right
2600 p_box->data.p_sample_text->i_justification_horizontal = -1;
2601 p_box->data.p_sample_text->i_justification_vertical = -1;
2602 break;
2603 case -2: // Flush Left
2604 p_box->data.p_sample_text->i_justification_horizontal = 0;
2605 p_box->data.p_sample_text->i_justification_vertical = 0;
2606 break;
2607 case 0: // Flush Default
2608 default:
2609 p_box->data.p_sample_text->i_justification_horizontal = 1;
2610 p_box->data.p_sample_text->i_justification_vertical = -1;
2611 break;
2614 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[0] );
2615 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[1] );
2616 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[2] );
2617 p_box->data.p_sample_text->i_background_color[3] = 0xFF;
2619 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2620 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2621 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2622 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2624 #ifdef MP4_VERBOSE
2625 msg_Dbg( p_stream, "read box: \"text\" in stsd text" );
2626 #endif
2627 MP4_READBOX_EXIT( 1 );
2630 static int MP4_ReadBox_sample_clcp( stream_t *p_stream, MP4_Box_t *p_box )
2632 p_box->i_handler = ATOM_clcp;
2633 MP4_READBOX_ENTER( MP4_Box_data_sample_clcp_t, NULL );
2635 if( i_read < 8 )
2636 MP4_READBOX_EXIT( 0 );
2638 for( int i=0; i<6; i++ )
2639 MP4_GET1BYTE( p_box->data.p_sample_clcp->i_reserved1[i] );
2640 MP4_GET2BYTES( p_box->data.p_sample_clcp->i_data_reference_index );
2642 #ifdef MP4_VERBOSE
2643 msg_Dbg( p_stream, "read box: \"clcp\" in stsd" );
2644 #endif
2645 MP4_READBOX_EXIT( 1 );
2648 static int MP4_ReadBox_sample_tx3g( stream_t *p_stream, MP4_Box_t *p_box )
2650 p_box->i_handler = ATOM_text;
2651 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t, NULL );
2653 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2654 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2656 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2658 MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2660 MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_horizontal );
2661 MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_vertical );
2663 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[0] );
2664 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[1] );
2665 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[2] );
2666 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[3] );
2668 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2669 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2670 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2671 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2673 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved3 );
2675 MP4_GET2BYTES( p_box->data.p_sample_text->i_font_id );
2676 MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_face );
2677 MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_size );
2678 MP4_GET4BYTES( p_box->data.p_sample_text->i_font_color );
2680 #ifdef MP4_VERBOSE
2681 msg_Dbg( p_stream, "read box: \"tx3g\" in stsd text" );
2682 #endif
2683 MP4_READBOX_EXIT( 1 );
2687 #if 0
2688 /* We can't easily call it, and anyway ~ 20 bytes lost isn't a real problem */
2689 static void MP4_FreeBox_sample_text( MP4_Box_t *p_box )
2691 FREENULL( p_box->data.p_sample_text->psz_text_name );
2693 #endif
2695 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
2697 FREENULL( p_box->data.p_stsz->i_entry_size );
2700 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
2702 MP4_READBOX_ENTER( MP4_Box_data_stsz_t, MP4_FreeBox_stsz );
2704 MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
2706 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
2707 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_count );
2709 if( p_box->data.p_stsz->i_sample_size == 0 )
2711 p_box->data.p_stsz->i_entry_size =
2712 calloc( p_box->data.p_stsz->i_sample_count, sizeof(uint32_t) );
2713 if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
2714 MP4_READBOX_EXIT( 0 );
2716 for( unsigned int i = 0; (i<p_box->data.p_stsz->i_sample_count)&&(i_read >= 4 ); i++ )
2718 MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
2721 else
2722 p_box->data.p_stsz->i_entry_size = NULL;
2724 #ifdef MP4_VERBOSE
2725 msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
2726 p_box->data.p_stsz->i_sample_size,
2727 p_box->data.p_stsz->i_sample_count );
2729 #endif
2730 MP4_READBOX_EXIT( 1 );
2733 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
2735 FREENULL( p_box->data.p_stsc->i_first_chunk );
2736 FREENULL( p_box->data.p_stsc->i_samples_per_chunk );
2737 FREENULL( p_box->data.p_stsc->i_sample_description_index );
2740 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
2742 MP4_READBOX_ENTER( MP4_Box_data_stsc_t, MP4_FreeBox_stsc );
2744 MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
2746 MP4_GET4BYTES( p_box->data.p_stsc->i_entry_count );
2748 p_box->data.p_stsc->i_first_chunk =
2749 calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2750 p_box->data.p_stsc->i_samples_per_chunk =
2751 calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2752 p_box->data.p_stsc->i_sample_description_index =
2753 calloc( p_box->data.p_stsc->i_entry_count, sizeof(uint32_t) );
2754 if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
2755 || p_box->data.p_stsc->i_samples_per_chunk == NULL
2756 || p_box->data.p_stsc->i_sample_description_index == NULL ) )
2758 MP4_READBOX_EXIT( 0 );
2761 for( unsigned int i = 0; (i < p_box->data.p_stsc->i_entry_count )&&( i_read >= 12 );i++ )
2763 MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
2764 MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
2765 MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
2768 #ifdef MP4_VERBOSE
2769 msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
2770 p_box->data.p_stsc->i_entry_count );
2772 #endif
2773 MP4_READBOX_EXIT( 1 );
2776 static void MP4_FreeBox_sdp( MP4_Box_t *p_box )
2778 FREENULL( p_box->data.p_sdp->psz_text );
2781 static int MP4_ReadBox_sdp( stream_t *p_stream, MP4_Box_t *p_box )
2783 MP4_READBOX_ENTER( MP4_Box_data_sdp_t, MP4_FreeBox_sdp );
2785 MP4_GETSTRINGZ( p_box->data.p_sdp->psz_text );
2787 MP4_READBOX_EXIT( 1 );
2790 static void MP4_FreeBox_rtp( MP4_Box_t *p_box )
2792 FREENULL( p_box->data.p_moviehintinformation_rtp->psz_text );
2795 static int MP4_ReadBox_rtp( stream_t *p_stream, MP4_Box_t *p_box )
2797 MP4_READBOX_ENTER( MP4_Box_data_moviehintinformation_rtp_t, MP4_FreeBox_rtp );
2799 MP4_GET4BYTES( p_box->data.p_moviehintinformation_rtp->i_description_format );
2801 MP4_GETSTRINGZ( p_box->data.p_moviehintinformation_rtp->psz_text );
2803 MP4_READBOX_EXIT( 1 );
2806 static int MP4_ReadBox_tims( stream_t *p_stream, MP4_Box_t *p_box )
2808 MP4_READBOX_ENTER( MP4_Box_data_tims_t, NULL );
2810 MP4_GET4BYTES( p_box->data.p_tims->i_timescale );
2812 MP4_READBOX_EXIT( 1 );
2815 static int MP4_ReadBox_tsro( stream_t *p_stream, MP4_Box_t *p_box )
2817 MP4_READBOX_ENTER( MP4_Box_data_tsro_t, NULL );
2819 MP4_GET4BYTES( p_box->data.p_tsro->i_offset );
2821 MP4_READBOX_EXIT( 1 );
2824 static int MP4_ReadBox_tssy( stream_t *p_stream, MP4_Box_t *p_box )
2826 MP4_READBOX_ENTER( MP4_Box_data_tssy_t, NULL );
2828 MP4_GET1BYTE( p_box->data.p_tssy->i_reserved_timestamp_sync );
2830 MP4_READBOX_EXIT( 1 );
2833 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
2835 FREENULL( p_box->data.p_co64->i_chunk_offset );
2838 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
2840 MP4_READBOX_ENTER( MP4_Box_data_co64_t, MP4_FreeBox_stco_co64 );
2842 MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
2844 MP4_GET4BYTES( p_box->data.p_co64->i_entry_count );
2846 p_box->data.p_co64->i_chunk_offset =
2847 calloc( p_box->data.p_co64->i_entry_count, sizeof(uint64_t) );
2848 if( p_box->data.p_co64->i_chunk_offset == NULL )
2849 MP4_READBOX_EXIT( 0 );
2851 for( unsigned int i = 0; i < p_box->data.p_co64->i_entry_count; i++ )
2853 if( p_box->i_type == ATOM_stco )
2855 if( i_read < 4 )
2857 break;
2859 MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2861 else
2863 if( i_read < 8 )
2865 break;
2867 MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2871 #ifdef MP4_VERBOSE
2872 msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
2873 p_box->data.p_co64->i_entry_count );
2875 #endif
2876 MP4_READBOX_EXIT( 1 );
2879 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
2881 FREENULL( p_box->data.p_stss->i_sample_number );
2884 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
2886 MP4_READBOX_ENTER( MP4_Box_data_stss_t, MP4_FreeBox_stss );
2888 MP4_GETVERSIONFLAGS( p_box->data.p_stss );
2890 MP4_GET4BYTES( p_box->data.p_stss->i_entry_count );
2892 p_box->data.p_stss->i_sample_number =
2893 calloc( p_box->data.p_stss->i_entry_count, sizeof(uint32_t) );
2894 if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
2895 MP4_READBOX_EXIT( 0 );
2897 unsigned int i;
2898 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 4 ); i++ )
2901 MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
2902 /* XXX in libmp4 sample begin at 0 */
2903 p_box->data.p_stss->i_sample_number[i]--;
2905 if ( i < p_box->data.p_stss->i_entry_count )
2906 p_box->data.p_stss->i_entry_count = i;
2908 #ifdef MP4_VERBOSE
2909 msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
2910 p_box->data.p_stss->i_entry_count );
2912 #endif
2913 MP4_READBOX_EXIT( 1 );
2916 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
2918 FREENULL( p_box->data.p_stsh->i_shadowed_sample_number );
2919 FREENULL( p_box->data.p_stsh->i_sync_sample_number );
2922 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
2924 MP4_READBOX_ENTER( MP4_Box_data_stsh_t, MP4_FreeBox_stsh );
2926 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
2929 MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
2931 p_box->data.p_stsh->i_shadowed_sample_number =
2932 calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2933 p_box->data.p_stsh->i_sync_sample_number =
2934 calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
2936 if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
2937 || p_box->data.p_stsh->i_sync_sample_number == NULL )
2939 MP4_READBOX_EXIT( 0 );
2942 unsigned i;
2943 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); i++ )
2945 MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
2946 MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
2948 if ( i < p_box->data.p_stss->i_entry_count )
2949 p_box->data.p_stss->i_entry_count = i;
2951 #ifdef MP4_VERBOSE
2952 msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
2953 p_box->data.p_stsh->i_entry_count );
2954 #endif
2955 MP4_READBOX_EXIT( 1 );
2958 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
2960 FREENULL( p_box->data.p_stdp->i_priority );
2963 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
2965 MP4_READBOX_ENTER( MP4_Box_data_stdp_t, MP4_FreeBox_stdp );
2967 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
2969 p_box->data.p_stdp->i_priority =
2970 calloc( i_read / 2, sizeof(uint16_t) );
2972 if( unlikely( !p_box->data.p_stdp->i_priority ) )
2973 MP4_READBOX_EXIT( 0 );
2975 for( unsigned i = 0; i < i_read / 2 ; i++ )
2977 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
2980 #ifdef MP4_VERBOSE
2981 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
2982 i_read / 2 );
2984 #endif
2985 MP4_READBOX_EXIT( 1 );
2988 static void MP4_FreeBox_padb( MP4_Box_t *p_box )
2990 FREENULL( p_box->data.p_padb->i_reserved1 );
2991 FREENULL( p_box->data.p_padb->i_pad2 );
2992 FREENULL( p_box->data.p_padb->i_reserved2 );
2993 FREENULL( p_box->data.p_padb->i_pad1 );
2996 static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
2998 uint32_t count;
3000 MP4_READBOX_ENTER( MP4_Box_data_padb_t, MP4_FreeBox_padb );
3002 MP4_GETVERSIONFLAGS( p_box->data.p_padb );
3004 MP4_GET4BYTES( p_box->data.p_padb->i_sample_count );
3005 count = (p_box->data.p_padb->i_sample_count + 1) / 2;
3007 p_box->data.p_padb->i_reserved1 = calloc( count, sizeof(uint16_t) );
3008 p_box->data.p_padb->i_pad2 = calloc( count, sizeof(uint16_t) );
3009 p_box->data.p_padb->i_reserved2 = calloc( count, sizeof(uint16_t) );
3010 p_box->data.p_padb->i_pad1 = calloc( count, sizeof(uint16_t) );
3011 if( p_box->data.p_padb->i_reserved1 == NULL
3012 || p_box->data.p_padb->i_pad2 == NULL
3013 || p_box->data.p_padb->i_reserved2 == NULL
3014 || p_box->data.p_padb->i_pad1 == NULL )
3016 MP4_READBOX_EXIT( 0 );
3019 for( unsigned int i = 0; i < i_read / 2 ; i++ )
3021 if( i >= count )
3023 MP4_READBOX_EXIT( 0 );
3025 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 7 )&0x01;
3026 p_box->data.p_padb->i_pad2[i] = ( (*p_peek) >> 4 )&0x07;
3027 p_box->data.p_padb->i_reserved1[i] = ( (*p_peek) >> 3 )&0x01;
3028 p_box->data.p_padb->i_pad1[i] = ( (*p_peek) )&0x07;
3030 p_peek += 1; i_read -= 1;
3033 #ifdef MP4_VERBOSE
3034 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
3035 i_read / 2 );
3037 #endif
3038 MP4_READBOX_EXIT( 1 );
3041 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
3043 FREENULL( p_box->data.p_elst->i_segment_duration );
3044 FREENULL( p_box->data.p_elst->i_media_time );
3045 FREENULL( p_box->data.p_elst->i_media_rate_integer );
3046 FREENULL( p_box->data.p_elst->i_media_rate_fraction );
3049 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
3051 MP4_READBOX_ENTER( MP4_Box_data_elst_t, MP4_FreeBox_elst );
3053 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
3056 MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
3058 p_box->data.p_elst->i_segment_duration =
3059 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
3060 p_box->data.p_elst->i_media_time =
3061 calloc( p_box->data.p_elst->i_entry_count, sizeof(int64_t) );
3062 p_box->data.p_elst->i_media_rate_integer =
3063 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
3064 p_box->data.p_elst->i_media_rate_fraction =
3065 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
3066 if( p_box->data.p_elst->i_segment_duration == NULL
3067 || p_box->data.p_elst->i_media_time == NULL
3068 || p_box->data.p_elst->i_media_rate_integer == NULL
3069 || p_box->data.p_elst->i_media_rate_fraction == NULL )
3071 MP4_READBOX_EXIT( 0 );
3074 unsigned i;
3075 for( i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
3077 if( p_box->data.p_elst->i_version == 1 )
3079 if ( i_read < 20 )
3080 break;
3081 MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
3083 MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
3085 else
3087 if ( i_read < 12 )
3088 break;
3089 MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
3091 MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
3092 p_box->data.p_elst->i_media_time[i] = (int32_t)p_box->data.p_elst->i_media_time[i];
3095 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
3096 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
3098 if ( i < p_box->data.p_elst->i_entry_count )
3099 p_box->data.p_elst->i_entry_count = i;
3100 #ifdef MP4_VERBOSE
3101 msg_Dbg( p_stream, "read box: \"elst\" entry-count %lu",
3102 (unsigned long)p_box->data.p_elst->i_entry_count );
3103 #endif
3104 MP4_READBOX_EXIT( 1 );
3107 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
3109 FREENULL( p_box->data.p_cprt->psz_notice );
3112 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
3114 uint16_t i_language;
3115 bool b_mac;
3117 MP4_READBOX_ENTER( MP4_Box_data_cprt_t, MP4_FreeBox_cprt );
3119 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
3121 MP4_GET2BYTES( i_language );
3122 decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
3124 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
3126 #ifdef MP4_VERBOSE
3127 msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
3128 p_box->data.p_cprt->rgs_language,
3129 p_box->data.p_cprt->psz_notice );
3131 #endif
3132 MP4_READBOX_EXIT( 1 );
3135 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
3137 MP4_READBOX_ENTER( MP4_Box_data_dcom_t, NULL );
3139 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
3140 #ifdef MP4_VERBOSE
3141 msg_Dbg( p_stream,
3142 "read box: \"dcom\" compression algorithm : %4.4s",
3143 (char*)&p_box->data.p_dcom->i_algorithm );
3144 #endif
3145 MP4_READBOX_EXIT( 1 );
3148 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
3150 FREENULL( p_box->data.p_cmvd->p_data );
3153 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
3155 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t, MP4_FreeBox_cmvd );
3157 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
3159 p_box->data.p_cmvd->i_compressed_size = i_read;
3161 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
3162 MP4_READBOX_EXIT( 0 );
3164 /* now copy compressed data */
3165 memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
3167 p_box->data.p_cmvd->b_compressed = 1;
3169 #ifdef MP4_VERBOSE
3170 msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
3171 p_box->data.p_cmvd->i_compressed_size );
3172 #endif
3174 MP4_READBOX_EXIT( 1 );
3177 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
3179 MP4_Box_t *p_dcom;
3180 MP4_Box_t *p_cmvd;
3182 #ifdef HAVE_ZLIB_H
3183 stream_t *p_stream_memory;
3184 z_stream z_data;
3185 uint8_t *p_data;
3186 int i_result;
3187 #endif
3189 if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
3190 return 0;
3192 if( !p_box->p_father ||
3193 ( p_box->p_father->i_type != ATOM_moov &&
3194 p_box->p_father->i_type != ATOM_foov ) )
3196 msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
3197 return 1;
3200 if( !MP4_ReadBoxContainer( p_stream, p_box ) )
3202 return 0;
3205 if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
3206 ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
3207 p_cmvd->data.p_cmvd->p_data == NULL )
3209 msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
3210 return 0;
3213 if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
3215 msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
3216 "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
3217 return 0;
3220 #ifndef HAVE_ZLIB_H
3221 msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
3222 return 0;
3224 #else
3225 /* decompress data */
3226 /* allocate a new buffer */
3227 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
3228 return 0;
3229 /* init default structures */
3230 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
3231 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
3232 z_data.next_out = p_data;
3233 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
3234 z_data.zalloc = (alloc_func)Z_NULL;
3235 z_data.zfree = (free_func)Z_NULL;
3236 z_data.opaque = (voidpf)Z_NULL;
3238 /* init zlib */
3239 if( inflateInit( &z_data ) != Z_OK )
3241 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3242 free( p_data );
3243 return 0;
3246 /* uncompress */
3247 i_result = inflate( &z_data, Z_NO_FLUSH );
3248 if( i_result != Z_OK && i_result != Z_STREAM_END )
3250 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3251 free( p_data );
3252 return 0;
3255 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
3257 msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
3258 "mismatch" );
3260 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
3262 /* close zlib */
3263 if( inflateEnd( &z_data ) != Z_OK )
3265 msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
3266 "data (ignored)" );
3269 free( p_cmvd->data.p_cmvd->p_data );
3270 p_cmvd->data.p_cmvd->p_data = p_data;
3271 p_cmvd->data.p_cmvd->b_compressed = 0;
3273 msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
3275 /* now create a memory stream */
3276 p_stream_memory =
3277 vlc_stream_MemoryNew( VLC_OBJECT(p_stream),
3278 p_cmvd->data.p_cmvd->p_data,
3279 p_cmvd->data.p_cmvd->i_uncompressed_size, true );
3281 /* and read uncompressd moov */
3282 p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
3284 vlc_stream_Delete( p_stream_memory );
3286 #ifdef MP4_VERBOSE
3287 msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
3288 #endif
3290 return p_box->data.p_cmov->p_moov ? 1 : 0;
3291 #endif /* HAVE_ZLIB_H */
3294 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
3296 FREENULL( p_box->data.p_rdrf->psz_ref );
3299 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
3301 uint32_t i_len;
3302 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t, MP4_FreeBox_rdrf );
3304 MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
3305 MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
3306 MP4_GET4BYTES( i_len );
3307 i_len++;
3309 if( i_len > 0 )
3311 p_box->data.p_rdrf->psz_ref = malloc( i_len );
3312 if( p_box->data.p_rdrf->psz_ref == NULL )
3313 MP4_READBOX_EXIT( 0 );
3314 i_len--;
3316 for( unsigned i = 0; i < i_len; i++ )
3318 MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
3320 p_box->data.p_rdrf->psz_ref[i_len] = '\0';
3322 else
3324 p_box->data.p_rdrf->psz_ref = NULL;
3327 #ifdef MP4_VERBOSE
3328 msg_Dbg( p_stream,
3329 "read box: \"rdrf\" type:%4.4s ref %s",
3330 (char*)&p_box->data.p_rdrf->i_ref_type,
3331 p_box->data.p_rdrf->psz_ref );
3332 #endif
3333 MP4_READBOX_EXIT( 1 );
3338 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
3340 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t, NULL );
3342 MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
3344 MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
3346 #ifdef MP4_VERBOSE
3347 msg_Dbg( p_stream,
3348 "read box: \"rmdr\" rate:%d",
3349 p_box->data.p_rmdr->i_rate );
3350 #endif
3351 MP4_READBOX_EXIT( 1 );
3354 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
3356 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t, NULL );
3358 MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
3360 #ifdef MP4_VERBOSE
3361 msg_Dbg( p_stream,
3362 "read box: \"rmqu\" quality:%d",
3363 p_box->data.p_rmqu->i_quality );
3364 #endif
3365 MP4_READBOX_EXIT( 1 );
3368 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
3370 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t, NULL );
3371 MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
3373 MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
3374 MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
3375 MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
3376 MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
3378 #ifdef MP4_VERBOSE
3379 msg_Dbg( p_stream,
3380 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
3381 (char*)&p_box->data.p_rmvc->i_gestaltType,
3382 p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
3383 p_box->data.p_rmvc->i_checkType );
3384 #endif
3386 MP4_READBOX_EXIT( 1 );
3389 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
3391 MP4_READBOX_ENTER( MP4_Box_data_frma_t, NULL );
3393 MP4_GETFOURCC( p_box->data.p_frma->i_type );
3395 #ifdef MP4_VERBOSE
3396 msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
3397 (char *)&p_box->data.p_frma->i_type );
3398 #endif
3400 MP4_READBOX_EXIT( 1 );
3403 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
3405 MP4_READBOX_ENTER( MP4_Box_data_skcr_t, NULL );
3407 MP4_GET4BYTES( p_box->data.p_skcr->i_init );
3408 MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
3409 MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
3411 #ifdef MP4_VERBOSE
3412 msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
3413 p_box->data.p_skcr->i_init,
3414 p_box->data.p_skcr->i_encr,
3415 p_box->data.p_skcr->i_decr );
3416 #endif
3418 MP4_READBOX_EXIT( 1 );
3421 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
3423 VLC_UNUSED(p_box);
3424 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
3425 * so unless data decrypt itself by magic, there will be no playback,
3426 * but we never know... */
3427 msg_Warn( p_stream, "DRM protected streams are not supported." );
3428 return 1;
3431 static void MP4_FreeBox_Binary( MP4_Box_t *p_box )
3433 FREENULL( p_box->data.p_binary->p_blob );
3434 p_box->data.p_binary->i_blob = 0;
3437 static int MP4_ReadBox_Binary( stream_t *p_stream, MP4_Box_t *p_box )
3439 MP4_READBOX_ENTER( MP4_Box_data_binary_t, MP4_FreeBox_Binary );
3440 i_read = __MIN( i_read, UINT32_MAX );
3441 if ( i_read > 0 )
3443 p_box->data.p_binary->p_blob = malloc( i_read );
3444 if ( p_box->data.p_binary->p_blob )
3446 memcpy( p_box->data.p_binary->p_blob, p_peek, i_read );
3447 p_box->data.p_binary->i_blob = i_read;
3450 MP4_READBOX_EXIT( 1 );
3453 static void MP4_FreeBox_data( MP4_Box_t *p_box )
3455 free( p_box->data.p_data->p_blob );
3458 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
3460 MP4_READBOX_ENTER( MP4_Box_data_data_t, MP4_FreeBox_data );
3461 MP4_Box_data_data_t *p_data = p_box->data.p_data;
3463 if ( i_read < 8 || i_read - 8 > UINT32_MAX )
3464 MP4_READBOX_EXIT( 0 );
3466 uint8_t i_type;
3467 MP4_GET1BYTE( i_type );
3468 if ( i_type != 0 )
3470 #ifdef MP4_VERBOSE
3471 msg_Dbg( p_stream, "skipping unknown 'data' atom with type %"PRIu8, i_type );
3472 #endif
3473 MP4_READBOX_EXIT( 0 );
3476 MP4_GET3BYTES( p_data->e_wellknowntype );
3477 MP4_GET2BYTES( p_data->locale.i_country );
3478 MP4_GET2BYTES( p_data->locale.i_language );
3479 #ifdef MP4_VERBOSE
3480 msg_Dbg( p_stream, "read 'data' atom: knowntype=%"PRIu32", country=%"PRIu16" lang=%"PRIu16
3481 ", size %"PRId64" bytes", p_data->e_wellknowntype,
3482 p_data->locale.i_country, p_data->locale.i_language, i_read );
3483 #endif
3484 p_box->data.p_data->p_blob = malloc( i_read );
3485 if ( !p_box->data.p_data->p_blob )
3486 MP4_READBOX_EXIT( 0 );
3488 p_box->data.p_data->i_blob = i_read;
3489 memcpy( p_box->data.p_data->p_blob, p_peek, i_read);
3491 MP4_READBOX_EXIT( 1 );
3494 static int MP4_ReadBox_Metadata( stream_t *p_stream, MP4_Box_t *p_box )
3496 const uint8_t *p_peek;
3497 if ( vlc_stream_Peek( p_stream, &p_peek, 16 ) < 16 )
3498 return 0;
3499 if ( vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
3500 return 0;
3501 const uint32_t stoplist[] = { ATOM_data, 0 };
3502 return MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist );
3505 /* Chapter support */
3506 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
3508 MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
3509 for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
3510 free( p_chpl->chapter[i].psz_name );
3513 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
3515 MP4_Box_data_chpl_t *p_chpl;
3516 uint32_t i_dummy;
3517 VLC_UNUSED(i_dummy);
3518 int i;
3519 MP4_READBOX_ENTER( MP4_Box_data_chpl_t, MP4_FreeBox_chpl );
3521 p_chpl = p_box->data.p_chpl;
3523 MP4_GETVERSIONFLAGS( p_chpl );
3525 if ( i_read < 5 || p_chpl->i_version != 0x1 )
3526 MP4_READBOX_EXIT( 0 );
3528 MP4_GET4BYTES( i_dummy );
3530 MP4_GET1BYTE( p_chpl->i_chapter );
3532 for( i = 0; i < p_chpl->i_chapter; i++ )
3534 uint64_t i_start;
3535 uint8_t i_len;
3536 int i_copy;
3537 if ( i_read < 9 )
3538 break;
3539 MP4_GET8BYTES( i_start );
3540 MP4_GET1BYTE( i_len );
3542 p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
3543 if( !p_chpl->chapter[i].psz_name )
3544 MP4_READBOX_EXIT( 0 );
3546 i_copy = __MIN( i_len, i_read );
3547 if( i_copy > 0 )
3548 memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
3549 p_chpl->chapter[i].psz_name[i_copy] = '\0';
3550 p_chpl->chapter[i].i_start = i_start;
3552 p_peek += i_copy;
3553 i_read -= i_copy;
3556 if ( i != p_chpl->i_chapter )
3557 p_chpl->i_chapter = i;
3559 /* Bubble sort by increasing start date */
3562 for( i = 0; i < p_chpl->i_chapter - 1; i++ )
3564 if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
3566 char *psz = p_chpl->chapter[i+1].psz_name;
3567 int64_t i64 = p_chpl->chapter[i+1].i_start;
3569 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
3570 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
3572 p_chpl->chapter[i].psz_name = psz;
3573 p_chpl->chapter[i].i_start = i64;
3575 i = -1;
3576 break;
3579 } while( i == -1 );
3581 #ifdef MP4_VERBOSE
3582 msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
3583 p_chpl->i_chapter );
3584 #endif
3585 MP4_READBOX_EXIT( 1 );
3588 /* GoPro HiLight tags support */
3589 static void MP4_FreeBox_HMMT( MP4_Box_t *p_box )
3591 FREENULL( p_box->data.p_hmmt->pi_chapter_start );
3594 static int MP4_ReadBox_HMMT( stream_t *p_stream, MP4_Box_t *p_box )
3596 #define MAX_CHAPTER_COUNT 100
3598 MP4_Box_data_HMMT_t *p_hmmt;
3599 MP4_READBOX_ENTER( MP4_Box_data_HMMT_t, MP4_FreeBox_HMMT );
3601 if( i_read < 4 )
3602 MP4_READBOX_EXIT( 0 );
3604 p_hmmt = p_box->data.p_hmmt;
3606 MP4_GET4BYTES( p_hmmt->i_chapter_count );
3608 if( p_hmmt->i_chapter_count <= 0 )
3610 p_hmmt->pi_chapter_start = NULL;
3611 MP4_READBOX_EXIT( 1 );
3614 if( ( i_read / sizeof(uint32_t) ) < p_hmmt->i_chapter_count )
3615 MP4_READBOX_EXIT( 0 );
3617 /* Cameras are allowing a maximum of 100 tags */
3618 if( p_hmmt->i_chapter_count > MAX_CHAPTER_COUNT )
3619 p_hmmt->i_chapter_count = MAX_CHAPTER_COUNT;
3621 p_hmmt->pi_chapter_start = malloc( p_hmmt->i_chapter_count * sizeof(uint32_t) );
3622 if( p_hmmt->pi_chapter_start == NULL )
3623 MP4_READBOX_EXIT( 0 );
3625 for( uint32_t i = 0; i < p_hmmt->i_chapter_count; i++ )
3627 MP4_GET4BYTES( p_hmmt->pi_chapter_start[i] );
3630 #ifdef MP4_VERBOSE
3631 msg_Dbg( p_stream, "read box: \"HMMT\" %d HiLight tags", p_hmmt->i_chapter_count );
3632 #endif
3634 MP4_READBOX_EXIT( 1 );
3637 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
3639 FREENULL( p_box->data.p_tref_generic->i_track_ID );
3642 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
3644 MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t, MP4_FreeBox_tref_generic );
3646 p_box->data.p_tref_generic->i_track_ID = NULL;
3647 p_box->data.p_tref_generic->i_entry_count = i_read / sizeof(uint32_t);
3648 if( p_box->data.p_tref_generic->i_entry_count > 0 )
3649 p_box->data.p_tref_generic->i_track_ID = calloc( p_box->data.p_tref_generic->i_entry_count, sizeof(uint32_t) );
3650 if( p_box->data.p_tref_generic->i_track_ID == NULL )
3651 MP4_READBOX_EXIT( 0 );
3653 for( unsigned i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
3655 MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
3657 #ifdef MP4_VERBOSE
3658 msg_Dbg( p_stream, "read box: \"chap\" %d references",
3659 p_box->data.p_tref_generic->i_entry_count );
3660 #endif
3662 MP4_READBOX_EXIT( 1 );
3665 static void MP4_FreeBox_keys( MP4_Box_t *p_box )
3667 for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
3668 free( p_box->data.p_keys->p_entries[i].psz_value );
3669 free( p_box->data.p_keys->p_entries );
3672 static int MP4_ReadBox_keys( stream_t *p_stream, MP4_Box_t *p_box )
3674 MP4_READBOX_ENTER( MP4_Box_data_keys_t, MP4_FreeBox_keys );
3676 if ( i_read < 8 )
3677 MP4_READBOX_EXIT( 0 );
3679 uint32_t i_count;
3680 MP4_GET4BYTES( i_count ); /* reserved + flags */
3681 if ( i_count != 0 )
3682 MP4_READBOX_EXIT( 0 );
3684 MP4_GET4BYTES( i_count );
3685 p_box->data.p_keys->p_entries = calloc( i_count, sizeof(*p_box->data.p_keys->p_entries) );
3686 if ( !p_box->data.p_keys->p_entries )
3687 MP4_READBOX_EXIT( 0 );
3688 p_box->data.p_keys->i_entry_count = i_count;
3690 uint32_t i=0;
3691 for( ; i < i_count; i++ )
3693 if ( i_read < 8 )
3694 break;
3695 uint32_t i_keysize;
3696 MP4_GET4BYTES( i_keysize );
3697 if ( (i_keysize < 8) || (i_keysize - 4 > i_read) )
3698 break;
3699 MP4_GETFOURCC( p_box->data.p_keys->p_entries[i].i_namespace );
3700 i_keysize -= 8;
3701 p_box->data.p_keys->p_entries[i].psz_value = malloc( i_keysize + 1 );
3702 if ( !p_box->data.p_keys->p_entries[i].psz_value )
3703 break;
3704 memcpy( p_box->data.p_keys->p_entries[i].psz_value, p_peek, i_keysize );
3705 p_box->data.p_keys->p_entries[i].psz_value[i_keysize] = 0;
3706 p_peek += i_keysize;
3707 i_read -= i_keysize;
3708 #ifdef MP4_ULTRA_VERBOSE
3709 msg_Dbg( p_stream, "read box: \"keys\": %u '%s'", i + 1,
3710 p_box->data.p_keys->p_entries[i].psz_value );
3711 #endif
3713 if ( i < i_count )
3714 p_box->data.p_keys->i_entry_count = i;
3716 MP4_READBOX_EXIT( 1 );
3719 static int MP4_ReadBox_colr( stream_t *p_stream, MP4_Box_t *p_box )
3721 MP4_READBOX_ENTER( MP4_Box_data_colr_t, NULL );
3722 MP4_GETFOURCC( p_box->data.p_colr->i_type );
3723 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'c' ) ||
3724 p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3726 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_primary_idx );
3727 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_transfer_function_idx );
3728 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_matrix_idx );
3729 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3730 MP4_GET1BYTE( p_box->data.p_colr->nclc.i_full_range );
3732 else
3734 #ifdef MP4_VERBOSE
3735 msg_Warn( p_stream, "Unhandled colr type: %4.4s", (char*)&p_box->data.p_colr->i_type );
3736 #endif
3738 MP4_READBOX_EXIT( 1 );
3741 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
3743 const uint8_t *p_peek;
3744 const size_t i_headersize = mp4_box_headersize( p_box );
3746 if( p_box->i_size < 16 || p_box->i_size - i_headersize < 8 )
3747 return 0;
3749 /* skip over box header */
3750 if( vlc_stream_Read( p_stream, NULL, i_headersize ) < (ssize_t) i_headersize )
3751 return 0;
3753 /* meta content starts with a 4 byte version/flags value (should be 0) */
3754 if( vlc_stream_Peek( p_stream, &p_peek, 8 ) < 8 )
3755 return 0;
3757 if( !memcmp( p_peek, "\0\0\0", 4 ) ) /* correct header case */
3759 if( vlc_stream_Read( p_stream, NULL, 4 ) < 4 )
3760 return 0;
3762 else if( memcmp( &p_peek[4], "hdlr", 4 ) ) /* Broken, headerless ones */
3764 return 0;
3767 /* load child atoms up to the handler (which should be next anyway) */
3768 const uint32_t stoplist[] = { ATOM_hdlr, 0 };
3769 if ( !MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist ) )
3770 return 0;
3772 /* Mandatory */
3773 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box, "hdlr" );
3774 if ( p_hdlr && BOXDATA(p_hdlr) && BOXDATA(p_hdlr)->i_version == 0 )
3776 p_box->i_handler = BOXDATA(p_hdlr)->i_handler_type;
3777 switch( p_box->i_handler )
3779 case HANDLER_mdta:
3780 case HANDLER_mdir:
3781 /* then it behaves like a container */
3782 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
3783 default:
3784 /* skip parsing, will be seen as empty container */
3785 break;
3789 return 1;
3792 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3794 char i_unused;
3795 VLC_UNUSED(i_unused);
3797 MP4_READBOX_ENTER( MP4_Box_data_iods_t, NULL );
3798 MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3800 MP4_GET1BYTE( i_unused ); /* tag */
3801 MP4_GET1BYTE( i_unused ); /* length */
3803 MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3804 are used for other flags */
3805 MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3806 MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3807 MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3808 MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3809 MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3811 #ifdef MP4_VERBOSE
3812 msg_Dbg( p_stream,
3813 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3814 "visual: %i, graphics: %i",
3815 p_box->data.p_iods->i_object_descriptor >> 6,
3816 p_box->data.p_iods->i_OD_profile_level,
3817 p_box->data.p_iods->i_scene_profile_level,
3818 p_box->data.p_iods->i_audio_profile_level,
3819 p_box->data.p_iods->i_visual_profile_level,
3820 p_box->data.p_iods->i_graphics_profile_level );
3821 #endif
3823 MP4_READBOX_EXIT( 1 );
3826 static int MP4_ReadBox_btrt( stream_t *p_stream, MP4_Box_t *p_box )
3828 MP4_READBOX_ENTER( MP4_Box_data_btrt_t, NULL );
3830 if(i_read != 12)
3831 MP4_READBOX_EXIT( 0 );
3833 MP4_GET4BYTES( p_box->data.p_btrt->i_buffer_size );
3834 MP4_GET4BYTES( p_box->data.p_btrt->i_max_bitrate );
3835 MP4_GET4BYTES( p_box->data.p_btrt->i_avg_bitrate );
3837 MP4_READBOX_EXIT( 1 );
3840 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3842 MP4_READBOX_ENTER( MP4_Box_data_pasp_t, NULL );
3844 MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
3845 MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
3847 #ifdef MP4_VERBOSE
3848 msg_Dbg( p_stream,
3849 "read box: \"paps\" %dx%d",
3850 p_box->data.p_pasp->i_horizontal_spacing,
3851 p_box->data.p_pasp->i_vertical_spacing);
3852 #endif
3854 MP4_READBOX_EXIT( 1 );
3857 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
3859 MP4_READBOX_ENTER( MP4_Box_data_mehd_t, NULL );
3861 MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
3862 if( p_box->data.p_mehd->i_version == 1 )
3863 MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
3864 else /* version == 0 */
3865 MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
3867 #ifdef MP4_VERBOSE
3868 msg_Dbg( p_stream,
3869 "read box: \"mehd\" frag dur. %"PRIu64"",
3870 p_box->data.p_mehd->i_fragment_duration );
3871 #endif
3873 MP4_READBOX_EXIT( 1 );
3876 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
3878 MP4_READBOX_ENTER( MP4_Box_data_trex_t, NULL );
3879 MP4_GETVERSIONFLAGS( p_box->data.p_trex );
3881 MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
3882 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
3883 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
3884 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
3885 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
3887 #ifdef MP4_VERBOSE
3888 msg_Dbg( p_stream,
3889 "read box: \"trex\" trackID: %"PRIu32"",
3890 p_box->data.p_trex->i_track_ID );
3891 #endif
3893 MP4_READBOX_EXIT( 1 );
3896 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
3898 FREENULL( p_box->data.p_sdtp->p_sample_table );
3901 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
3903 uint32_t i_sample_count;
3904 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t, MP4_FreeBox_sdtp );
3905 MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
3906 MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
3907 i_sample_count = i_read;
3909 p_sdtp->p_sample_table = calloc( i_sample_count, 1 );
3911 if( !p_sdtp->p_sample_table )
3912 MP4_READBOX_EXIT( 0 );
3914 for( uint32_t i = 0; i < i_sample_count; i++ )
3915 MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
3917 #ifdef MP4_VERBOSE
3918 msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
3919 if ( i_sample_count > 3 )
3920 msg_Dbg( p_stream,
3921 "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
3922 p_sdtp->p_sample_table[0],
3923 p_sdtp->p_sample_table[1],
3924 p_sdtp->p_sample_table[2],
3925 p_sdtp->p_sample_table[3] );
3926 #endif
3928 MP4_READBOX_EXIT( 1 );
3931 static int MP4_ReadBox_tsel( stream_t *p_stream, MP4_Box_t *p_box )
3933 MP4_READBOX_ENTER( MP4_Box_data_tsel_t, NULL );
3934 uint32_t i_version;
3935 MP4_GET4BYTES( i_version );
3936 if ( i_version != 0 || i_read < 4 )
3937 MP4_READBOX_EXIT( 0 );
3938 MP4_GET4BYTES( p_box->data.p_tsel->i_switch_group );
3939 /* ignore list of attributes as es are present before switch */
3940 MP4_READBOX_EXIT( 1 );
3943 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
3945 MP4_READBOX_ENTER( MP4_Box_data_mfro_t, NULL );
3947 MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
3948 MP4_GET4BYTES( p_box->data.p_mfro->i_size );
3950 #ifdef MP4_VERBOSE
3951 msg_Dbg( p_stream,
3952 "read box: \"mfro\" size: %"PRIu32"",
3953 p_box->data.p_mfro->i_size);
3954 #endif
3956 MP4_READBOX_EXIT( 1 );
3959 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
3961 FREENULL( p_box->data.p_tfra->p_time );
3962 FREENULL( p_box->data.p_tfra->p_moof_offset );
3963 FREENULL( p_box->data.p_tfra->p_traf_number );
3964 FREENULL( p_box->data.p_tfra->p_trun_number );
3965 FREENULL( p_box->data.p_tfra->p_sample_number );
3968 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
3970 #define READ_VARIABLE_LENGTH(lengthvar, p_array) switch (lengthvar)\
3972 case 0:\
3973 MP4_GET1BYTE( p_array[i] );\
3974 break;\
3975 case 1:\
3976 MP4_GET2BYTES( *((uint16_t *)&p_array[i*2]) );\
3977 break;\
3978 case 2:\
3979 MP4_GET3BYTES( *((uint32_t *)&p_array[i*4]) );\
3980 break;\
3981 case 3:\
3982 MP4_GET4BYTES( *((uint32_t *)&p_array[i*4]) );\
3983 break;\
3984 default:\
3985 goto error;\
3987 #define FIX_VARIABLE_LENGTH(lengthvar) if ( lengthvar == 3 ) lengthvar = 4
3989 uint32_t i_number_of_entries;
3990 MP4_READBOX_ENTER( MP4_Box_data_tfra_t, MP4_FreeBox_tfra );
3991 MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
3992 MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
3993 if ( p_tfra->i_version > 1 )
3994 MP4_READBOX_EXIT( 0 );
3995 MP4_GET4BYTES( p_tfra->i_track_ID );
3996 uint32_t i_lengths = 0;
3997 MP4_GET4BYTES( i_lengths );
3998 MP4_GET4BYTES( p_tfra->i_number_of_entries );
3999 i_number_of_entries = p_tfra->i_number_of_entries;
4000 p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
4001 p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
4002 p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
4004 size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
4005 p_tfra->p_time = calloc( i_number_of_entries, size );
4006 p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
4008 size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
4009 if ( size == 3 ) size++;
4010 p_tfra->p_traf_number = calloc( i_number_of_entries, size );
4011 size = 1 + p_tfra->i_length_size_of_trun_num;
4012 if ( size == 3 ) size++;
4013 p_tfra->p_trun_number = calloc( i_number_of_entries, size );
4014 size = 1 + p_tfra->i_length_size_of_sample_num;
4015 if ( size == 3 ) size++;
4016 p_tfra->p_sample_number = calloc( i_number_of_entries, size );
4018 if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
4019 || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
4020 goto error;
4022 int i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
4023 + p_tfra->i_length_size_of_trun_num
4024 + p_tfra->i_length_size_of_sample_num;
4026 uint32_t i;
4027 for( i = 0; i < i_number_of_entries; i++ )
4030 if( p_tfra->i_version == 1 )
4032 if ( i_read < i_fields_length + 16 )
4033 break;
4034 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_time[i*2]) );
4035 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_moof_offset[i*2]) );
4037 else
4039 if ( i_read < i_fields_length + 8 )
4040 break;
4041 MP4_GET4BYTES( p_tfra->p_time[i] );
4042 MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
4045 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num, p_tfra->p_traf_number);
4046 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num, p_tfra->p_trun_number);
4047 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num, p_tfra->p_sample_number);
4049 if ( i < i_number_of_entries )
4050 i_number_of_entries = i;
4052 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num);
4053 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num);
4054 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num);
4056 #ifdef MP4_ULTRA_VERBOSE
4057 for( i = 0; i < i_number_of_entries; i++ )
4059 if( p_tfra->i_version == 0 )
4061 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu32", "
4062 "moof_offset[%"PRIu32"]: %"PRIu32"",
4063 p_tfra->i_track_ID,
4064 i, p_tfra->p_time[i],
4065 i, p_tfra->p_moof_offset[i] );
4067 else
4069 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu64", "
4070 "moof_offset[%"PRIu32"]: %"PRIu64"",
4071 p_tfra->i_track_ID,
4072 i, ((uint64_t *)(p_tfra->p_time))[i],
4073 i, ((uint64_t *)(p_tfra->p_moof_offset))[i] );
4076 #endif
4077 #ifdef MP4_VERBOSE
4078 msg_Dbg( p_stream, "tfra[%"PRIu32"] %"PRIu32" entries",
4079 p_tfra->i_track_ID, i_number_of_entries );
4080 #endif
4082 MP4_READBOX_EXIT( 1 );
4083 error:
4084 MP4_READBOX_EXIT( 0 );
4086 #undef READ_VARIABLE_LENGTH
4087 #undef FIX_VARIABLE_LENGTH
4090 static int MP4_ReadBox_pnot( stream_t *p_stream, MP4_Box_t *p_box )
4092 if ( p_box->i_size != 20 )
4093 return 0;
4094 MP4_READBOX_ENTER( MP4_Box_data_pnot_t, NULL );
4095 MP4_GET4BYTES( p_box->data.p_pnot->i_date );
4096 uint16_t i_version;
4097 MP4_GET2BYTES( i_version );
4098 if ( i_version != 0 )
4099 MP4_READBOX_EXIT( 0 );
4100 MP4_GETFOURCC( p_box->data.p_pnot->i_type );
4101 MP4_GET2BYTES( p_box->data.p_pnot->i_index );
4102 MP4_READBOX_EXIT( 1 );
4105 static int MP4_ReadBox_SA3D( stream_t *p_stream, MP4_Box_t *p_box )
4107 MP4_READBOX_ENTER( MP4_Box_data_SA3D_t, NULL );
4109 uint8_t i_version;
4110 MP4_GET1BYTE( i_version );
4111 if ( i_version != 0 )
4112 MP4_READBOX_EXIT( 0 );
4114 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_type );
4115 MP4_GET4BYTES( p_box->data.p_SA3D->i_ambisonic_order );
4116 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_channel_ordering );
4117 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_normalization );
4118 MP4_GET4BYTES( p_box->data.p_SA3D->i_num_channels );
4119 MP4_READBOX_EXIT( 1 );
4122 /* For generic */
4123 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
4125 if( !p_box->p_father )
4127 goto unknown;
4129 if( p_box->p_father->i_type == ATOM_stsd )
4131 MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
4132 MP4_Box_t *p_hdlr;
4134 if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
4135 (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
4137 goto unknown;
4139 switch( p_hdlr->data.p_hdlr->i_handler_type )
4141 case ATOM_soun:
4142 return MP4_ReadBox_sample_soun( p_stream, p_box );
4143 case ATOM_vide:
4144 return MP4_ReadBox_sample_vide( p_stream, p_box );
4145 case ATOM_hint:
4146 return MP4_ReadBox_sample_hint8( p_stream, p_box );
4147 case ATOM_text:
4148 return MP4_ReadBox_sample_text( p_stream, p_box );
4149 case ATOM_tx3g:
4150 case ATOM_sbtl:
4151 return MP4_ReadBox_sample_tx3g( p_stream, p_box );
4152 default:
4153 msg_Warn( p_stream,
4154 "unknown handler type in stsd (incompletely loaded)" );
4155 return 1;
4159 unknown:
4160 if MP4_BOX_TYPE_ASCII()
4161 msg_Warn( p_stream,
4162 "unknown box type %4.4s (incompletely loaded)",
4163 (char*)&p_box->i_type );
4164 else
4165 msg_Warn( p_stream,
4166 "unknown box type c%3.3s (incompletely loaded)",
4167 (char*)&p_box->i_type+1 );
4168 p_box->e_flags |= BOX_FLAG_INCOMPLETE;
4170 return 1;
4173 /**** ------------------------------------------------------------------- ****/
4175 static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
4177 if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
4178 return MP4_ReadBox_tfrf( p_stream, p_box );
4179 if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
4180 return MP4_ReadBox_tfxd( p_stream, p_box );
4181 if( !CmpUUID( &p_box->i_uuid, &XML360BoxUUID ) )
4182 return MP4_ReadBox_XML360( p_stream, p_box );
4183 if( !CmpUUID( &p_box->i_uuid, &PS3DDSBoxUUID ) && p_box->i_size == 28 )
4184 return MP4_ReadBox_Binary( p_stream, p_box );
4186 #ifdef MP4_VERBOSE
4187 msg_Warn( p_stream, "Unknown uuid type box: "
4188 "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-"
4189 "%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
4190 p_box->i_uuid.b[0], p_box->i_uuid.b[1], p_box->i_uuid.b[2], p_box->i_uuid.b[3],
4191 p_box->i_uuid.b[4], p_box->i_uuid.b[5], p_box->i_uuid.b[6], p_box->i_uuid.b[7],
4192 p_box->i_uuid.b[8], p_box->i_uuid.b[9], p_box->i_uuid.b[10], p_box->i_uuid.b[11],
4193 p_box->i_uuid.b[12], p_box->i_uuid.b[13], p_box->i_uuid.b[14], p_box->i_uuid.b[15] );
4194 #else
4195 msg_Warn( p_stream, "Unknown uuid type box" );
4196 #endif
4197 return 1;
4200 /**** ------------------------------------------------------------------- ****/
4201 /**** "Higher level" Functions ****/
4202 /**** ------------------------------------------------------------------- ****/
4204 static const struct
4206 uint32_t i_type;
4207 int (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
4208 uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
4209 } MP4_Box_Function [] =
4211 /* Containers */
4212 { ATOM_moov, MP4_ReadBoxContainer, 0 },
4213 { ATOM_foov, MP4_ReadBoxContainer, 0 },
4214 { ATOM_trak, MP4_ReadBoxContainer, ATOM_moov },
4215 { ATOM_trak, MP4_ReadBoxContainer, ATOM_foov },
4216 { ATOM_mdia, MP4_ReadBoxContainer, ATOM_trak },
4217 { ATOM_moof, MP4_ReadBoxContainer, 0 },
4218 { ATOM_minf, MP4_ReadBoxContainer, ATOM_mdia },
4219 { ATOM_stbl, MP4_ReadBoxContainer, ATOM_minf },
4220 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_minf },
4221 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_meta },
4222 { ATOM_edts, MP4_ReadBoxContainer, ATOM_trak },
4223 { ATOM_udta, MP4_ReadBoxContainer, 0 },
4224 { ATOM_nmhd, MP4_ReadBoxContainer, ATOM_minf },
4225 { ATOM_hnti, MP4_ReadBoxContainer, ATOM_udta },
4226 { ATOM_rmra, MP4_ReadBoxContainer, ATOM_moov },
4227 { ATOM_rmda, MP4_ReadBoxContainer, ATOM_rmra },
4228 { ATOM_tref, MP4_ReadBoxContainer, ATOM_trak },
4229 { ATOM_gmhd, MP4_ReadBoxContainer, ATOM_minf },
4230 { ATOM_wave, MP4_ReadBoxContainer, ATOM_stsd },
4231 { ATOM_wave, MP4_ReadBoxContainer, ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
4232 { ATOM_wave, MP4_ReadBoxContainer, ATOM_WMA2 }, /* flip4mac */
4233 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in24 },
4234 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in32 },
4235 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl32 },
4236 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl64 },
4237 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDMC },
4238 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDM2 },
4239 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiFL }, /* XiphQT */
4240 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiVs }, /* XiphQT */
4241 { ATOM_ilst, MP4_ReadBox_ilst, ATOM_meta },
4242 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_moov },
4243 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_ftyp },
4245 /* specific box */
4246 { ATOM_ftyp, MP4_ReadBox_ftyp, 0 },
4247 { ATOM_styp, MP4_ReadBox_ftyp, 0 },
4248 { ATOM_cmov, MP4_ReadBox_cmov, 0 },
4249 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_moov },
4250 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_foov },
4251 { ATOM_tkhd, MP4_ReadBox_tkhd, ATOM_trak },
4252 { ATOM_load, MP4_ReadBox_load, ATOM_trak },
4253 { ATOM_mdhd, MP4_ReadBox_mdhd, ATOM_mdia },
4254 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_mdia },
4255 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_meta },
4256 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_minf },
4257 { ATOM_vmhd, MP4_ReadBox_vmhd, ATOM_minf },
4258 { ATOM_smhd, MP4_ReadBox_smhd, ATOM_minf },
4259 { ATOM_hmhd, MP4_ReadBox_hmhd, ATOM_minf },
4260 { ATOM_alis, MP4_ReadBoxSkip, ATOM_dref },
4261 { ATOM_url, MP4_ReadBox_url, 0 },
4262 { ATOM_urn, MP4_ReadBox_urn, 0 },
4263 { ATOM_dref, MP4_ReadBox_LtdContainer, 0 },
4264 { ATOM_stts, MP4_ReadBox_stts, ATOM_stbl },
4265 { ATOM_ctts, MP4_ReadBox_ctts, ATOM_stbl },
4266 { ATOM_cslg, MP4_ReadBox_cslg, ATOM_stbl },
4267 { ATOM_stsd, MP4_ReadBox_LtdContainer, ATOM_stbl },
4268 { ATOM_stsz, MP4_ReadBox_stsz, ATOM_stbl },
4269 { ATOM_stsc, MP4_ReadBox_stsc, ATOM_stbl },
4270 { ATOM_stco, MP4_ReadBox_stco_co64, ATOM_stbl },
4271 { ATOM_co64, MP4_ReadBox_stco_co64, ATOM_stbl },
4272 { ATOM_stss, MP4_ReadBox_stss, ATOM_stbl },
4273 { ATOM_stsh, MP4_ReadBox_stsh, ATOM_stbl },
4274 { ATOM_stdp, MP4_ReadBox_stdp, 0 },
4275 { ATOM_padb, MP4_ReadBox_padb, 0 },
4276 { ATOM_elst, MP4_ReadBox_elst, ATOM_edts },
4277 { ATOM_cprt, MP4_ReadBox_cprt, 0 },
4278 { ATOM_esds, MP4_ReadBox_esds, ATOM_wave }, /* mp4a in wave chunk */
4279 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4a },
4280 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4v },
4281 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4s },
4282 { ATOM_dcom, MP4_ReadBox_dcom, 0 },
4283 { ATOM_dfLa, MP4_ReadBox_Binary, ATOM_fLaC },
4284 { ATOM_cmvd, MP4_ReadBox_cmvd, 0 },
4285 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc1 },
4286 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc3 },
4287 { ATOM_hvcC, MP4_ReadBox_Binary, 0 },
4288 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp08 },
4289 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp09 },
4290 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp10 },
4291 { ATOM_dac3, MP4_ReadBox_dac3, 0 },
4292 { ATOM_dec3, MP4_ReadBox_dec3, 0 },
4293 { ATOM_dvc1, MP4_ReadBox_dvc1, ATOM_vc1 },
4294 { ATOM_fiel, MP4_ReadBox_fiel, 0 },
4295 { ATOM_glbl, MP4_ReadBox_Binary, ATOM_FFV1 },
4296 { ATOM_enda, MP4_ReadBox_enda, 0 },
4297 { ATOM_iods, MP4_ReadBox_iods, 0 },
4298 { ATOM_pasp, MP4_ReadBox_pasp, 0 },
4299 { ATOM_btrt, MP4_ReadBox_btrt, 0 }, /* codecs bitrate stsd/????/btrt */
4300 { ATOM_keys, MP4_ReadBox_keys, ATOM_meta },
4301 { ATOM_colr, MP4_ReadBox_colr, 0 },
4303 /* XiphQT */
4304 { ATOM_vCtH, MP4_ReadBox_Binary, ATOM_wave },
4305 { ATOM_vCtC, MP4_ReadBox_Binary, ATOM_wave },
4306 { ATOM_vCtd, MP4_ReadBox_Binary, ATOM_wave },
4307 { ATOM_fCtS, MP4_ReadBox_Binary, ATOM_wave },
4309 /* Samples groups specific information */
4310 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_stbl },
4311 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_traf },
4312 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_stbl },
4313 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_traf },
4315 /* Quicktime preview atoms, all at root */
4316 { ATOM_pnot, MP4_ReadBox_pnot, 0 },
4317 { ATOM_pict, MP4_ReadBox_Binary, 0 },
4318 { ATOM_PICT, MP4_ReadBox_Binary, 0 },
4320 /* Nothing to do with this box */
4321 { ATOM_mdat, MP4_ReadBoxSkip, 0 },
4322 { ATOM_skip, MP4_ReadBoxSkip, 0 },
4323 { ATOM_free, MP4_ReadBoxSkip, 0 },
4324 { ATOM_wide, MP4_ReadBoxSkip, 0 },
4325 { ATOM_binm, MP4_ReadBoxSkip, 0 },
4327 /* Subtitles */
4328 { ATOM_tx3g, MP4_ReadBox_sample_tx3g, 0 },
4329 { ATOM_c608, MP4_ReadBox_sample_clcp, ATOM_stsd },
4330 //{ ATOM_text, MP4_ReadBox_sample_text, 0 },
4331 /* In sample WebVTT subtitle atoms. No ATOM_wvtt in normal parsing */
4332 { ATOM_vttc, MP4_ReadBoxContainer, ATOM_wvtt },
4333 { ATOM_payl, MP4_ReadBox_Binary, ATOM_vttc },
4335 /* for codecs */
4336 { ATOM_soun, MP4_ReadBox_sample_soun, ATOM_stsd },
4337 { ATOM_agsm, MP4_ReadBox_sample_soun, ATOM_stsd },
4338 { ATOM_ac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4339 { ATOM_eac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4340 { ATOM_fLaC, MP4_ReadBox_sample_soun, ATOM_stsd },
4341 { ATOM_lpcm, MP4_ReadBox_sample_soun, ATOM_stsd },
4342 { ATOM_ms02, MP4_ReadBox_sample_soun, ATOM_stsd },
4343 { ATOM_ms11, MP4_ReadBox_sample_soun, ATOM_stsd },
4344 { ATOM_ms55, MP4_ReadBox_sample_soun, ATOM_stsd },
4345 { ATOM__mp3, MP4_ReadBox_sample_soun, ATOM_stsd },
4346 { ATOM_mp4a, MP4_ReadBox_sample_soun, ATOM_stsd },
4347 { ATOM_twos, MP4_ReadBox_sample_soun, ATOM_stsd },
4348 { ATOM_sowt, MP4_ReadBox_sample_soun, ATOM_stsd },
4349 { ATOM_QDMC, MP4_ReadBox_sample_soun, ATOM_stsd },
4350 { ATOM_QDM2, MP4_ReadBox_sample_soun, ATOM_stsd },
4351 { ATOM_ima4, MP4_ReadBox_sample_soun, ATOM_stsd },
4352 { ATOM_IMA4, MP4_ReadBox_sample_soun, ATOM_stsd },
4353 { ATOM_dvi, MP4_ReadBox_sample_soun, ATOM_stsd },
4354 { ATOM_alaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4355 { ATOM_ulaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4356 { ATOM_MAC3, MP4_ReadBox_sample_soun, ATOM_stsd },
4357 { ATOM_MAC6, MP4_ReadBox_sample_soun, ATOM_stsd },
4358 { ATOM_Qclp, MP4_ReadBox_sample_soun, ATOM_stsd },
4359 { ATOM_samr, MP4_ReadBox_sample_soun, ATOM_stsd },
4360 { ATOM_sawb, MP4_ReadBox_sample_soun, ATOM_stsd },
4361 { ATOM_OggS, MP4_ReadBox_sample_soun, ATOM_stsd },
4362 { ATOM_alac, MP4_ReadBox_sample_soun, ATOM_stsd },
4363 { ATOM_WMA2, MP4_ReadBox_sample_soun, ATOM_stsd }, /* flip4mac */
4364 { ATOM_wma, MP4_ReadBox_sample_soun, ATOM_stsd }, /* ismv wmapro */
4365 { ATOM_Opus, MP4_ReadBox_sample_soun, ATOM_stsd },
4366 /* Sound extensions */
4367 { ATOM_chan, MP4_ReadBox_stsdext_chan, 0 },
4368 { ATOM_WMA2, MP4_ReadBox_WMA2, ATOM_wave }, /* flip4mac */
4369 { ATOM_dOps, MP4_ReadBox_Binary, ATOM_Opus },
4370 { ATOM_wfex, MP4_ReadBox_WMA2, ATOM_wma }, /* ismv formatex */
4372 /* Both uncompressed sound and video */
4373 { ATOM_raw, MP4_ReadBox_default, ATOM_stsd },
4375 { ATOM_drmi, MP4_ReadBox_sample_vide, ATOM_stsd },
4376 { ATOM_vide, MP4_ReadBox_sample_vide, ATOM_stsd },
4377 { ATOM_mp4v, MP4_ReadBox_sample_vide, ATOM_stsd },
4378 { ATOM_SVQ1, MP4_ReadBox_sample_vide, ATOM_stsd },
4379 { ATOM_SVQ3, MP4_ReadBox_sample_vide, ATOM_stsd },
4380 { ATOM_ZyGo, MP4_ReadBox_sample_vide, ATOM_stsd },
4381 { ATOM_DIVX, MP4_ReadBox_sample_vide, ATOM_stsd },
4382 { ATOM_XVID, MP4_ReadBox_sample_vide, ATOM_stsd },
4383 { ATOM_h263, MP4_ReadBox_sample_vide, ATOM_stsd },
4384 { ATOM_s263, MP4_ReadBox_sample_vide, ATOM_stsd },
4385 { ATOM_cvid, MP4_ReadBox_sample_vide, ATOM_stsd },
4386 { ATOM_3IV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4387 { ATOM_3iv1, MP4_ReadBox_sample_vide, ATOM_stsd },
4388 { ATOM_3IV2, MP4_ReadBox_sample_vide, ATOM_stsd },
4389 { ATOM_3iv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4390 { ATOM_3IVD, MP4_ReadBox_sample_vide, ATOM_stsd },
4391 { ATOM_3ivd, MP4_ReadBox_sample_vide, ATOM_stsd },
4392 { ATOM_3VID, MP4_ReadBox_sample_vide, ATOM_stsd },
4393 { ATOM_3vid, MP4_ReadBox_sample_vide, ATOM_stsd },
4394 { ATOM_FFV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4395 { ATOM_mjpa, MP4_ReadBox_sample_vide, ATOM_stsd },
4396 { ATOM_mjpb, MP4_ReadBox_sample_vide, ATOM_stsd },
4397 { ATOM_qdrw, MP4_ReadBox_sample_vide, ATOM_stsd },
4398 { ATOM_mp2v, MP4_ReadBox_sample_vide, ATOM_stsd },
4399 { ATOM_hdv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4400 { ATOM_WMV3, MP4_ReadBox_sample_vide, ATOM_stsd },
4402 { ATOM_mjqt, MP4_ReadBox_default, 0 }, /* found in mjpa/b */
4403 { ATOM_mjht, MP4_ReadBox_default, 0 },
4405 { ATOM_dvc, MP4_ReadBox_sample_vide, ATOM_stsd },
4406 { ATOM_dvp, MP4_ReadBox_sample_vide, ATOM_stsd },
4407 { ATOM_dv5n, MP4_ReadBox_sample_vide, ATOM_stsd },
4408 { ATOM_dv5p, MP4_ReadBox_sample_vide, ATOM_stsd },
4409 { ATOM_VP31, MP4_ReadBox_sample_vide, ATOM_stsd },
4410 { ATOM_vp31, MP4_ReadBox_sample_vide, ATOM_stsd },
4411 { ATOM_h264, MP4_ReadBox_sample_vide, ATOM_stsd },
4413 { ATOM_jpeg, MP4_ReadBox_sample_vide, ATOM_stsd },
4414 { ATOM_vc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4415 { ATOM_avc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4416 { ATOM_avc3, MP4_ReadBox_sample_vide, ATOM_stsd },
4418 { ATOM_rrtp, MP4_ReadBox_sample_hint8, ATOM_stsd },
4420 { ATOM_yv12, MP4_ReadBox_sample_vide, 0 },
4421 { ATOM_yuv2, MP4_ReadBox_sample_vide, 0 },
4423 { ATOM_strf, MP4_ReadBox_strf, ATOM_WMV3 }, /* flip4mac */
4424 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_WMV3 }, /* flip4mac */
4425 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_wave }, /* flip4mac */
4427 { ATOM_mp4s, MP4_ReadBox_sample_mp4s, ATOM_stsd },
4429 /* XXX there is 2 box where we could find this entry stbl and tref*/
4430 { ATOM_hint, MP4_ReadBox_default, 0 },
4432 /* found in tref box */
4433 { ATOM_dpnd, MP4_ReadBox_default, 0 },
4434 { ATOM_ipir, MP4_ReadBox_default, 0 },
4435 { ATOM_mpod, MP4_ReadBox_default, 0 },
4436 { ATOM_chap, MP4_ReadBox_tref_generic, 0 },
4438 /* found in hnti */
4439 { ATOM_rtp, MP4_ReadBox_rtp, ATOM_hnti },
4440 { ATOM_sdp, MP4_ReadBox_sdp, ATOM_hnti },
4442 /* found in rrtp sample description */
4443 { ATOM_tims, MP4_ReadBox_tims, 0 },
4444 { ATOM_tsro, MP4_ReadBox_tsro, 0 },
4445 { ATOM_tssy, MP4_ReadBox_tssy, 0 },
4447 /* found in rmra/rmda */
4448 { ATOM_rdrf, MP4_ReadBox_rdrf, ATOM_rmda },
4449 { ATOM_rmdr, MP4_ReadBox_rmdr, ATOM_rmda },
4450 { ATOM_rmqu, MP4_ReadBox_rmqu, ATOM_rmda },
4451 { ATOM_rmvc, MP4_ReadBox_rmvc, ATOM_rmda },
4453 { ATOM_drms, MP4_ReadBox_sample_soun, 0 },
4454 { ATOM_sinf, MP4_ReadBoxContainer, 0 },
4455 { ATOM_schi, MP4_ReadBoxContainer, 0 },
4456 { ATOM_user, MP4_ReadBox_drms, 0 },
4457 { ATOM_key, MP4_ReadBox_drms, 0 },
4458 { ATOM_iviv, MP4_ReadBox_drms, 0 },
4459 { ATOM_priv, MP4_ReadBox_drms, 0 },
4460 { ATOM_frma, MP4_ReadBox_frma, ATOM_sinf }, /* and rinf */
4461 { ATOM_frma, MP4_ReadBox_frma, ATOM_wave }, /* flip4mac */
4462 { ATOM_skcr, MP4_ReadBox_skcr, 0 },
4464 /* ilst meta tags */
4465 { ATOM_0xa9ART, MP4_ReadBox_Metadata, ATOM_ilst },
4466 { ATOM_0xa9alb, MP4_ReadBox_Metadata, ATOM_ilst },
4467 { ATOM_0xa9cmt, MP4_ReadBox_Metadata, ATOM_ilst },
4468 { ATOM_0xa9com, MP4_ReadBox_Metadata, ATOM_ilst },
4469 { ATOM_0xa9cpy, MP4_ReadBox_Metadata, ATOM_ilst },
4470 { ATOM_0xa9day, MP4_ReadBox_Metadata, ATOM_ilst },
4471 { ATOM_0xa9des, MP4_ReadBox_Metadata, ATOM_ilst },
4472 { ATOM_0xa9enc, MP4_ReadBox_Metadata, ATOM_ilst },
4473 { ATOM_0xa9gen, MP4_ReadBox_Metadata, ATOM_ilst },
4474 { ATOM_0xa9grp, MP4_ReadBox_Metadata, ATOM_ilst },
4475 { ATOM_0xa9lyr, MP4_ReadBox_Metadata, ATOM_ilst },
4476 { ATOM_0xa9nam, MP4_ReadBox_Metadata, ATOM_ilst },
4477 { ATOM_0xa9too, MP4_ReadBox_Metadata, ATOM_ilst },
4478 { ATOM_0xa9trk, MP4_ReadBox_Metadata, ATOM_ilst },
4479 { ATOM_0xa9wrt, MP4_ReadBox_Metadata, ATOM_ilst },
4480 { ATOM_aART, MP4_ReadBox_Metadata, ATOM_ilst },
4481 { ATOM_atID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4482 { ATOM_cnID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4483 { ATOM_covr, MP4_ReadBoxContainer, ATOM_ilst },
4484 { ATOM_desc, MP4_ReadBox_Metadata, ATOM_ilst },
4485 { ATOM_disk, MP4_ReadBox_Metadata, ATOM_ilst },
4486 { ATOM_flvr, MP4_ReadBox_Metadata, ATOM_ilst },
4487 { ATOM_gnre, MP4_ReadBox_Metadata, ATOM_ilst },
4488 { ATOM_rtng, MP4_ReadBox_Metadata, ATOM_ilst },
4489 { ATOM_trkn, MP4_ReadBox_Metadata, ATOM_ilst },
4490 { ATOM_xid_, MP4_ReadBox_Metadata, ATOM_ilst },
4491 { ATOM_gshh, MP4_ReadBox_Metadata, ATOM_ilst }, /* YouTube gs?? */
4492 { ATOM_gspm, MP4_ReadBox_Metadata, ATOM_ilst },
4493 { ATOM_gspu, MP4_ReadBox_Metadata, ATOM_ilst },
4494 { ATOM_gssd, MP4_ReadBox_Metadata, ATOM_ilst },
4495 { ATOM_gsst, MP4_ReadBox_Metadata, ATOM_ilst },
4496 { ATOM_gstd, MP4_ReadBox_Metadata, ATOM_ilst },
4497 { ATOM_ITUN, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunesInfo */
4499 /* udta */
4500 { ATOM_0x40PRM, MP4_ReadBox_Binary, ATOM_udta },
4501 { ATOM_0x40PRQ, MP4_ReadBox_Binary, ATOM_udta },
4502 { ATOM_0xa9ART, MP4_ReadBox_Binary, ATOM_udta },
4503 { ATOM_0xa9alb, MP4_ReadBox_Binary, ATOM_udta },
4504 { ATOM_0xa9ard, MP4_ReadBox_Binary, ATOM_udta },
4505 { ATOM_0xa9arg, MP4_ReadBox_Binary, ATOM_udta },
4506 { ATOM_0xa9aut, MP4_ReadBox_Binary, ATOM_udta },
4507 { ATOM_0xa9cak, MP4_ReadBox_Binary, ATOM_udta },
4508 { ATOM_0xa9cmt, MP4_ReadBox_Binary, ATOM_udta },
4509 { ATOM_0xa9con, MP4_ReadBox_Binary, ATOM_udta },
4510 { ATOM_0xa9com, MP4_ReadBox_Binary, ATOM_udta },
4511 { ATOM_0xa9cpy, MP4_ReadBox_Binary, ATOM_udta },
4512 { ATOM_0xa9day, MP4_ReadBox_Binary, ATOM_udta },
4513 { ATOM_0xa9des, MP4_ReadBox_Binary, ATOM_udta },
4514 { ATOM_0xa9dir, MP4_ReadBox_Binary, ATOM_udta },
4515 { ATOM_0xa9dis, MP4_ReadBox_Binary, ATOM_udta },
4516 { ATOM_0xa9dsa, MP4_ReadBox_Binary, ATOM_udta },
4517 { ATOM_0xa9fmt, MP4_ReadBox_Binary, ATOM_udta },
4518 { ATOM_0xa9gen, MP4_ReadBox_Binary, ATOM_udta },
4519 { ATOM_0xa9grp, MP4_ReadBox_Binary, ATOM_udta },
4520 { ATOM_0xa9hst, MP4_ReadBox_Binary, ATOM_udta },
4521 { ATOM_0xa9inf, MP4_ReadBox_Binary, ATOM_udta },
4522 { ATOM_0xa9isr, MP4_ReadBox_Binary, ATOM_udta },
4523 { ATOM_0xa9lab, MP4_ReadBox_Binary, ATOM_udta },
4524 { ATOM_0xa9lal, MP4_ReadBox_Binary, ATOM_udta },
4525 { ATOM_0xa9lnt, MP4_ReadBox_Binary, ATOM_udta },
4526 { ATOM_0xa9lyr, MP4_ReadBox_Binary, ATOM_udta },
4527 { ATOM_0xa9mak, MP4_ReadBox_Binary, ATOM_udta },
4528 { ATOM_0xa9mal, MP4_ReadBox_Binary, ATOM_udta },
4529 { ATOM_0xa9mod, MP4_ReadBox_Binary, ATOM_udta },
4530 { ATOM_0xa9nam, MP4_ReadBox_Binary, ATOM_udta },
4531 { ATOM_0xa9ope, MP4_ReadBox_Binary, ATOM_udta },
4532 { ATOM_0xa9phg, MP4_ReadBox_Binary, ATOM_udta },
4533 { ATOM_0xa9PRD, MP4_ReadBox_Binary, ATOM_udta },
4534 { ATOM_0xa9prd, MP4_ReadBox_Binary, ATOM_udta },
4535 { ATOM_0xa9prf, MP4_ReadBox_Binary, ATOM_udta },
4536 { ATOM_0xa9pub, MP4_ReadBox_Binary, ATOM_udta },
4537 { ATOM_0xa9req, MP4_ReadBox_Binary, ATOM_udta },
4538 { ATOM_0xa9sne, MP4_ReadBox_Binary, ATOM_udta },
4539 { ATOM_0xa9snm, MP4_ReadBox_Binary, ATOM_udta },
4540 { ATOM_0xa9sol, MP4_ReadBox_Binary, ATOM_udta },
4541 { ATOM_0xa9src, MP4_ReadBox_Binary, ATOM_udta },
4542 { ATOM_0xa9st3, MP4_ReadBox_Binary, ATOM_udta },
4543 { ATOM_0xa9swr, MP4_ReadBox_Binary, ATOM_udta },
4544 { ATOM_0xa9thx, MP4_ReadBox_Binary, ATOM_udta },
4545 { ATOM_0xa9too, MP4_ReadBox_Binary, ATOM_udta },
4546 { ATOM_0xa9trk, MP4_ReadBox_Binary, ATOM_udta },
4547 { ATOM_0xa9url, MP4_ReadBox_Binary, ATOM_udta },
4548 { ATOM_0xa9wrn, MP4_ReadBox_Binary, ATOM_udta },
4549 { ATOM_0xa9xpd, MP4_ReadBox_Binary, ATOM_udta },
4550 { ATOM_0xa9xyz, MP4_ReadBox_Binary, ATOM_udta },
4551 { ATOM_chpl, MP4_ReadBox_chpl, ATOM_udta }, /* nero unlabeled chapters list */
4552 { ATOM_MCPS, MP4_ReadBox_Binary, ATOM_udta },
4553 { ATOM_name, MP4_ReadBox_Binary, ATOM_udta },
4554 { ATOM_vndr, MP4_ReadBox_Binary, ATOM_udta },
4555 { ATOM_SDLN, MP4_ReadBox_Binary, ATOM_udta },
4556 { ATOM_HMMT, MP4_ReadBox_HMMT, ATOM_udta }, /* GoPro HiLight tags */
4558 /* udta, non meta */
4559 { ATOM_tsel, MP4_ReadBox_tsel, ATOM_udta },
4561 /* iTunes/Quicktime meta info */
4562 { ATOM_meta, MP4_ReadBox_meta, 0 },
4563 { ATOM_ID32, MP4_ReadBox_Binary, ATOM_meta }, /* ID3v2 in 3GPP / ETSI TS 126 244 8.3 */
4564 { ATOM_data, MP4_ReadBox_data, 0 }, /* ilst/@too and others, ITUN/data */
4565 { ATOM_mean, MP4_ReadBox_Binary, ATOM_ITUN },
4566 { ATOM_name, MP4_ReadBox_Binary, ATOM_ITUN },
4568 /* found in smoothstreaming */
4569 { ATOM_traf, MP4_ReadBoxContainer, ATOM_moof },
4570 { ATOM_mfra, MP4_ReadBoxContainer, 0 },
4571 { ATOM_mfhd, MP4_ReadBox_mfhd, ATOM_moof },
4572 { ATOM_sidx, MP4_ReadBox_sidx, 0 },
4573 { ATOM_tfhd, MP4_ReadBox_tfhd, ATOM_traf },
4574 { ATOM_trun, MP4_ReadBox_trun, ATOM_traf },
4575 { ATOM_tfdt, MP4_ReadBox_tfdt, ATOM_traf },
4576 { ATOM_trex, MP4_ReadBox_trex, ATOM_mvex },
4577 { ATOM_mehd, MP4_ReadBox_mehd, ATOM_mvex },
4578 { ATOM_sdtp, MP4_ReadBox_sdtp, 0 },
4579 { ATOM_tfra, MP4_ReadBox_tfra, ATOM_mfra },
4580 { ATOM_mfro, MP4_ReadBox_mfro, ATOM_mfra },
4581 { ATOM_uuid, MP4_ReadBox_uuid, 0 },
4583 /* spatial/360°/VR */
4584 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_avc1 },
4585 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_mp4v },
4586 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_avc1 },
4587 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_mp4v },
4588 { ATOM_proj, MP4_ReadBoxContainer, ATOM_sv3d },
4589 { ATOM_prhd, MP4_ReadBox_prhd, ATOM_proj },
4590 { ATOM_equi, MP4_ReadBox_equi, ATOM_proj },
4591 { ATOM_cbmp, MP4_ReadBox_cbmp, ATOM_proj },
4593 /* Ambisonics */
4594 { ATOM_SA3D, MP4_ReadBox_SA3D, 0 },
4596 /* Last entry */
4597 { 0, MP4_ReadBox_default, 0 }
4600 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
4602 int i_index;
4604 for( i_index = 0; ; i_index++ )
4606 if ( MP4_Box_Function[i_index].i_parent &&
4607 p_father && p_father->i_type != MP4_Box_Function[i_index].i_parent )
4608 continue;
4610 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
4611 ( MP4_Box_Function[i_index].i_type == 0 ) )
4613 break;
4617 if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
4619 return VLC_EGENERIC;
4622 return VLC_SUCCESS;
4625 static void MP4_Box_Clean_Specific( MP4_Box_t *p_box )
4627 if( p_box->pf_free )
4628 p_box->pf_free( p_box );
4631 /*****************************************************************************
4632 * MP4_ReadBox : parse the actual box and the children
4633 * XXX : Do not go to the next box
4634 *****************************************************************************/
4635 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
4637 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
4638 if( p_box == NULL )
4639 return NULL;
4641 if( !MP4_PeekBoxHeader( p_stream, p_box ) )
4643 msg_Warn( p_stream, "cannot read one box" );
4644 free( p_box );
4645 return NULL;
4648 if( p_father && p_father->i_size > 0 &&
4649 p_father->i_pos + p_father->i_size < p_box->i_pos + p_box->i_size )
4651 msg_Dbg( p_stream, "out of bound child" );
4652 free( p_box );
4653 return NULL;
4656 if( !p_box->i_size )
4658 msg_Dbg( p_stream, "found an empty box (null size)" );
4659 free( p_box );
4660 return NULL;
4662 p_box->p_father = p_father;
4664 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
4666 uint64_t i_end = p_box->i_pos + p_box->i_size;
4667 MP4_BoxFree( p_box );
4668 MP4_Seek( p_stream, i_end ); /* Skip the failed box */
4669 return NULL;
4672 return p_box;
4675 /*****************************************************************************
4676 * MP4_BoxNew : creates and initializes an arbitrary box
4677 *****************************************************************************/
4678 MP4_Box_t * MP4_BoxNew( uint32_t i_type )
4680 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) );
4681 if( likely( p_box != NULL ) )
4683 p_box->i_type = i_type;
4685 return p_box;
4688 /*****************************************************************************
4689 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
4690 * the children
4691 *****************************************************************************/
4692 void MP4_BoxFree( MP4_Box_t *p_box )
4694 MP4_Box_t *p_child;
4696 if( !p_box )
4697 return; /* hehe */
4699 for( p_child = p_box->p_first; p_child != NULL; )
4701 MP4_Box_t *p_next;
4703 p_next = p_child->p_next;
4704 MP4_BoxFree( p_child );
4705 p_child = p_next;
4708 MP4_Box_Clean_Specific( p_box );
4710 if( p_box->data.p_payload )
4711 free( p_box->data.p_payload );
4713 free( p_box );
4716 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
4718 /* p_chunk is a virtual root container for the moof and mdat boxes */
4719 MP4_Box_t *p_fakeroot;
4720 MP4_Box_t *p_tmp_box;
4722 p_fakeroot = MP4_BoxNew( ATOM_root );
4723 if( unlikely( p_fakeroot == NULL ) )
4724 return NULL;
4725 p_fakeroot->i_shortsize = 1;
4727 const uint32_t stoplist[] = { ATOM_moov, ATOM_moof, 0 };
4728 MP4_ReadBoxContainerChildren( s, p_fakeroot, stoplist );
4730 p_tmp_box = p_fakeroot->p_first;
4731 if( p_tmp_box == NULL )
4733 MP4_BoxFree( p_fakeroot );
4734 return NULL;
4736 else while( p_tmp_box )
4738 p_fakeroot->i_size += p_tmp_box->i_size;
4739 p_tmp_box = p_tmp_box->p_next;
4742 return p_fakeroot;
4745 /*****************************************************************************
4746 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
4747 *****************************************************************************
4748 * The first box is a virtual box "root" and is the father for all first
4749 * level boxes for the file, a sort of virtual contener
4750 *****************************************************************************/
4751 MP4_Box_t *MP4_BoxGetRoot( stream_t *p_stream )
4753 int i_result;
4755 MP4_Box_t *p_vroot = MP4_BoxNew( ATOM_root );
4756 if( p_vroot == NULL )
4757 return NULL;
4759 p_vroot->i_shortsize = 1;
4760 int64_t i_size = stream_Size( p_stream );
4761 if( i_size > 0 )
4762 p_vroot->i_size = i_size;
4764 /* First get the moov */
4765 const uint32_t stoplist[] = { ATOM_moov, ATOM_mdat, 0 };
4766 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
4768 /* mdat appeared first */
4769 if( i_result && !MP4_BoxGet( p_vroot, "moov" ) )
4771 bool b_seekable;
4772 if( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_seekable ) != VLC_SUCCESS || !b_seekable )
4774 msg_Err( p_stream, "no moov before mdat and the stream is not seekable" );
4775 goto error;
4778 /* continue loading up to moov */
4779 const uint32_t stoplist[] = { ATOM_moov, 0 };
4780 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
4783 if( !i_result )
4784 goto error;
4786 /* If there is a mvex box, it means fragmented MP4, and we're done */
4787 if( MP4_BoxCount( p_vroot, "moov/mvex" ) > 0 )
4789 /* Read a bit more atoms as we might have an index between moov and moof */
4790 const uint32_t stoplist[] = { ATOM_sidx, 0 };
4791 const uint32_t excludelist[] = { ATOM_moof, ATOM_mdat, 0 };
4792 MP4_ReadBoxContainerChildrenIndexed( p_stream, p_vroot, stoplist, excludelist, false );
4793 return p_vroot;
4796 if( vlc_stream_Tell( p_stream ) + 8 < (uint64_t) stream_Size( p_stream ) )
4798 /* Get the rest of the file */
4799 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, NULL );
4801 if( !i_result )
4802 goto error;
4805 MP4_Box_t *p_moov;
4806 MP4_Box_t *p_cmov;
4808 /* check if there is a cmov, if so replace
4809 compressed moov by uncompressed one */
4810 if( ( ( p_moov = MP4_BoxGet( p_vroot, "moov" ) ) &&
4811 ( p_cmov = MP4_BoxGet( p_vroot, "moov/cmov" ) ) ) ||
4812 ( ( p_moov = MP4_BoxGet( p_vroot, "foov" ) ) &&
4813 ( p_cmov = MP4_BoxGet( p_vroot, "foov/cmov" ) ) ) )
4815 /* rename the compressed moov as a box to skip */
4816 p_moov->i_type = ATOM_skip;
4818 /* get uncompressed p_moov */
4819 p_moov = p_cmov->data.p_cmov->p_moov;
4820 p_cmov->data.p_cmov->p_moov = NULL;
4822 /* make p_root father of this new moov */
4823 p_moov->p_father = p_vroot;
4825 /* insert this new moov box as first child of p_root */
4826 p_moov->p_next = p_vroot->p_first;
4827 p_vroot->p_first = p_moov;
4830 return p_vroot;
4832 error:
4833 MP4_BoxFree( p_vroot );
4834 MP4_Seek( p_stream, 0 );
4835 return NULL;
4839 static void MP4_BoxDumpStructure_Internal( stream_t *s, const MP4_Box_t *p_box,
4840 unsigned int i_level )
4842 const MP4_Box_t *p_child;
4843 uint32_t i_displayedtype = p_box->i_type;
4844 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype)[0] = 'c';
4846 if( !i_level )
4848 msg_Dbg( s, "dumping root Box \"%4.4s\"",
4849 (char*)&i_displayedtype );
4851 else
4853 char str[512];
4854 if( i_level >= (sizeof(str) - 1)/4 )
4855 return;
4857 memset( str, ' ', sizeof(str) );
4858 for( unsigned i = 0; i < i_level; i++ )
4860 str[i*4] = '|';
4863 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
4864 "+ %4.4s size %"PRIu64" offset %" PRIuMAX "%s",
4865 (char*)&i_displayedtype, p_box->i_size,
4866 (uintmax_t)p_box->i_pos,
4867 p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
4868 msg_Dbg( s, "%s", str );
4870 p_child = p_box->p_first;
4871 while( p_child )
4873 MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
4874 p_child = p_child->p_next;
4878 void MP4_BoxDumpStructure( stream_t *s, const MP4_Box_t *p_box )
4880 MP4_BoxDumpStructure_Internal( s, p_box, 0 );
4884 /*****************************************************************************
4885 *****************************************************************************
4887 ** High level methods to acces an MP4 file
4889 *****************************************************************************
4890 *****************************************************************************/
4891 static bool get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
4893 size_t i_len ;
4894 if( !*ppsz_path[0] )
4896 *ppsz_token = NULL;
4897 *pi_number = 0;
4898 return true;
4900 i_len = strcspn( *ppsz_path, "/[" );
4901 if( !i_len && **ppsz_path == '/' )
4903 i_len = 1;
4905 *ppsz_token = strndup( *ppsz_path, i_len );
4906 if( unlikely(!*ppsz_token) )
4907 return false;
4909 *ppsz_path += i_len;
4911 /* Parse the token number token[n] */
4912 if( **ppsz_path == '[' )
4914 (*ppsz_path)++;
4915 *pi_number = strtol( *ppsz_path, NULL, 10 );
4916 while( **ppsz_path && **ppsz_path != ']' )
4918 (*ppsz_path)++;
4920 if( **ppsz_path == ']' )
4922 (*ppsz_path)++;
4925 else
4927 *pi_number = 0;
4930 /* Forward to start of next token */
4931 while( **ppsz_path == '/' )
4933 (*ppsz_path)++;
4936 return true;
4939 static void MP4_BoxGet_Internal( const MP4_Box_t **pp_result, const MP4_Box_t *p_box,
4940 const char *psz_fmt, va_list args)
4942 char *psz_dup;
4943 char *psz_path;
4944 char *psz_token = NULL;
4946 if( !p_box )
4948 *pp_result = NULL;
4949 return;
4952 if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
4953 psz_path = NULL;
4955 if( !psz_path || !psz_path[0] )
4957 free( psz_path );
4958 *pp_result = NULL;
4959 return;
4962 // fprintf( stderr, "path:'%s'\n", psz_path );
4963 psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
4964 for( ; ; )
4966 int i_number;
4968 if( !get_token( &psz_path, &psz_token, &i_number ) )
4969 goto error_box;
4970 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
4971 // psz_path,psz_token,i_number );
4972 if( !psz_token )
4974 free( psz_dup );
4975 *pp_result = p_box;
4976 return;
4978 else
4979 if( !strcmp( psz_token, "/" ) )
4981 /* Find root box */
4982 while( p_box && p_box->i_type != ATOM_root )
4984 p_box = p_box->p_father;
4986 if( !p_box )
4988 goto error_box;
4991 else
4992 if( !strcmp( psz_token, "." ) )
4994 /* Do nothing */
4996 else
4997 if( !strcmp( psz_token, ".." ) )
4999 p_box = p_box->p_father;
5000 if( !p_box )
5002 goto error_box;
5005 else
5006 if( strlen( psz_token ) == 4 )
5008 uint32_t i_fourcc;
5009 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
5010 psz_token[2], psz_token[3] );
5011 p_box = p_box->p_first;
5012 for( ; ; )
5014 if( !p_box )
5016 goto error_box;
5018 if( p_box->i_type == i_fourcc )
5020 if( !i_number )
5022 break;
5024 i_number--;
5026 p_box = p_box->p_next;
5029 else
5030 if( *psz_token == '\0' )
5032 p_box = p_box->p_first;
5033 for( ; ; )
5035 if( !p_box )
5037 goto error_box;
5039 if( !i_number )
5041 break;
5043 i_number--;
5044 p_box = p_box->p_next;
5047 else
5049 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
5050 goto error_box;
5053 FREENULL( psz_token );
5056 return;
5058 error_box:
5059 free( psz_token );
5060 free( psz_dup );
5061 *pp_result = NULL;
5062 return;
5065 /*****************************************************************************
5066 * MP4_BoxGet: find a box given a path relative to p_box
5067 *****************************************************************************
5068 * Path Format: . .. / as usual
5069 * [number] to specifie box number ex: trak[12]
5071 * ex: /moov/trak[12]
5072 * ../mdia
5073 *****************************************************************************/
5074 MP4_Box_t *MP4_BoxGet( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5076 va_list args;
5077 const MP4_Box_t *p_result;
5079 va_start( args, psz_fmt );
5080 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5081 va_end( args );
5083 return( (MP4_Box_t *) p_result );
5086 /*****************************************************************************
5087 * MP4_BoxCount: count box given a path relative to p_box
5088 *****************************************************************************
5089 * Path Format: . .. / as usual
5090 * [number] to specifie box number ex: trak[12]
5092 * ex: /moov/trak[12]
5093 * ../mdia
5094 *****************************************************************************/
5095 unsigned MP4_BoxCount( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5097 va_list args;
5098 unsigned i_count;
5099 const MP4_Box_t *p_result, *p_next;
5101 va_start( args, psz_fmt );
5102 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5103 va_end( args );
5104 if( !p_result )
5106 return( 0 );
5109 i_count = 1;
5110 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
5112 if( p_next->i_type == p_result->i_type)
5114 i_count++;
5117 return( i_count );