demux: libmp4: fix iloc unitialized base offset
[vlc.git] / modules / demux / mp4 / libmp4.c
blob7e4ca4b093b9063738ccf1e442a773876d684880
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>
39 #include <limits.h>
41 /* Some assumptions:
42 * The input method HAS to be seekable
45 /* convert 16.16 fixed point to floating point */
46 static double conv_fx( int32_t fx ) {
47 double fp = fx;
48 fp /= 65536.;
49 return fp;
52 /* some functions for mp4 encoding of variables */
53 #ifdef MP4_VERBOSE
54 static char * MP4_Time2Str( stime_t i_duration, uint32_t i_scale )
56 uint64_t i_time = (i_scale > 0) ? i_duration / i_scale : 0;
57 unsigned h = ( i_time /( 60*60 ) ) % 60;
58 unsigned m = ( i_time / 60 ) % 60;
59 unsigned s = i_time % 60;
60 unsigned ms = (i_scale) ? (1000*i_duration / i_scale) % 1000 : 0;
62 char *out;
63 if( asprintf( &out, "%u:%.2u:%.2u:%.3u", h, m, s, ms ) < 0 )
64 return NULL;
65 return out;
67 #endif
69 #define MP4_GETX_PRIVATE(dst, code, size) \
70 do \
71 { \
72 if( (i_read) >= (size) ) \
73 { \
74 dst = (code); \
75 p_peek += (size); \
76 i_read -= (size); \
77 } \
78 else \
79 { \
80 dst = 0; \
81 i_read = 0; \
82 } \
83 } while(0)
85 #define MP4_GET1BYTE( dst ) MP4_GETX_PRIVATE( dst, *p_peek, 1 )
86 #define MP4_GET2BYTES( dst ) MP4_GETX_PRIVATE( dst, GetWBE(p_peek), 2 )
87 #define MP4_GET3BYTES( dst ) MP4_GETX_PRIVATE( dst, Get24bBE(p_peek), 3 )
88 #define MP4_GET4BYTES( dst ) MP4_GETX_PRIVATE( dst, GetDWBE(p_peek), 4 )
89 #define MP4_GET8BYTES( dst ) MP4_GETX_PRIVATE( dst, GetQWBE(p_peek), 8 )
91 #define MP4_GET2BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetWLE(p_peek), 2 )
92 #define MP4_GET4BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetDWLE(p_peek), 4 )
93 #define MP4_GET8BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetQWLE(p_peek), 8 )
94 #define MP4_GETFOURCC( dst ) MP4_GET4BYTESLE( dst )
96 #define MP4_GETVERSIONFLAGS( p_void ) \
97 MP4_GET1BYTE( p_void->i_version ); \
98 MP4_GET3BYTES( p_void->i_flags )
100 static char *mp4_getstringz( uint8_t **restrict in, uint64_t *restrict size )
102 assert( *size <= SSIZE_MAX );
104 if( *size == 0 )
105 return NULL;
107 if( *in == 0 ) /* Null string stored */
109 *in += 1;
110 *size -= 1;
111 return NULL;
114 size_t len = strnlen( (const char *)*in, *size );
115 if( len == 0 || len >= *size )
116 return NULL;
118 len++;
120 char *ret = malloc( len );
121 if( likely(ret != NULL) )
122 memcpy( ret, *in, len );
123 *in += len;
124 *size -= len;
125 return ret;
128 #define MP4_GETSTRINGZ( p_str ) \
129 do \
130 (p_str) = mp4_getstringz( &p_peek, &i_read ); \
131 while(0)
133 /* This macro is used when we want to printf the box type
134 * APPLE annotation box is :
135 * either 0xA9 + 24-bit ASCII text string (and 0xA9 isn't printable)
136 * either 32-bit ASCII text string
138 #define MP4_BOX_TYPE_ASCII() ( ((char*)&p_box->i_type)[0] != (char)0xA9 )
140 static inline uint32_t Get24bBE( const uint8_t *p )
142 return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
145 static inline void GetUUID( UUID_t *p_uuid, const uint8_t *p_buff )
147 memcpy( p_uuid, p_buff, 16 );
150 static uint8_t *mp4_readbox_enter_common( stream_t *s, MP4_Box_t *box,
151 size_t typesize,
152 void (*release)( MP4_Box_t * ),
153 uint64_t readsize )
155 const size_t headersize = mp4_box_headersize( box );
157 if( unlikely(readsize < headersize) || unlikely(readsize > SSIZE_MAX) )
158 return NULL;
160 uint8_t *buf = malloc( readsize );
161 if( unlikely(buf == NULL) )
162 return NULL;
164 ssize_t val = vlc_stream_Read( s, buf, readsize );
165 if( (size_t)val != readsize )
167 msg_Warn( s, "mp4: wanted %"PRIu64" bytes, got %zd", readsize, val );
168 goto error;
171 box->data.p_payload = malloc( typesize );
172 if( unlikely(box->data.p_payload == NULL) )
173 goto error;
175 memset( box->data.p_payload, 0, typesize );
176 box->pf_free = release;
177 return buf;
178 error:
179 free( buf );
180 return NULL;
183 static uint8_t *mp4_readbox_enter_partial( stream_t *s, MP4_Box_t *box,
184 size_t typesize,
185 void (*release)( MP4_Box_t * ),
186 uint64_t *restrict readsize )
188 if( (uint64_t)*readsize > box->i_size )
189 *readsize = box->i_size;
191 return mp4_readbox_enter_common( s, box, typesize, release, *readsize );
194 static uint8_t *mp4_readbox_enter( stream_t *s, MP4_Box_t *box,
195 size_t typesize,
196 void (*release)( MP4_Box_t * ) )
198 uint64_t readsize = box->i_size;
199 return mp4_readbox_enter_common( s, box, typesize, release, readsize );
203 #define MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_TYPE_t, maxread, release ) \
204 uint64_t i_read = (maxread); \
205 uint8_t *p_buff = mp4_readbox_enter_partial( p_stream, p_box, \
206 sizeof( MP4_Box_data_TYPE_t ), release, &i_read ); \
207 if( unlikely(p_buff == NULL) ) \
208 return 0; \
209 const size_t header_size = mp4_box_headersize( p_box ); \
210 uint8_t *p_peek = p_buff + header_size; \
211 i_read -= header_size
213 #define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t, release ) \
214 uint8_t *p_buff = mp4_readbox_enter( p_stream, p_box, \
215 sizeof(MP4_Box_data_TYPE_t), release ); \
216 if( unlikely(p_buff == NULL) ) \
217 return 0; \
218 uint64_t i_read = p_box->i_size; \
219 const size_t header_size = mp4_box_headersize( p_box ); \
220 uint8_t *p_peek = p_buff + header_size; \
221 i_read -= header_size
223 #define MP4_READBOX_EXIT( i_code ) \
224 do \
226 free( p_buff ); \
227 return( i_code ); \
228 } while (0)
230 /*****************************************************************************
231 * Some prototypes.
232 *****************************************************************************/
233 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
234 static MP4_Box_t *MP4_ReadBoxUsing( stream_t *p_stream, MP4_Box_t *p_father,
235 int(*MP4_ReadBox_function)(stream_t *, MP4_Box_t *) );
236 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father );
237 static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box );
239 int MP4_Seek( stream_t *p_stream, uint64_t i_pos )
241 bool b_canseek = false;
242 if ( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_canseek ) != VLC_SUCCESS ||
243 b_canseek )
245 /* can seek or don't know */
246 return vlc_stream_Seek( p_stream, i_pos );
248 /* obviously can't seek then */
250 int64_t i_current_pos = vlc_stream_Tell( p_stream );
251 if ( i_current_pos < 0 || i_pos < (uint64_t)i_current_pos )
252 return VLC_EGENERIC;
254 size_t i_toread = i_pos - i_current_pos;
255 if( i_toread == 0 )
256 return VLC_SUCCESS;
257 else if( i_toread > (1<<17) )
258 return VLC_EGENERIC;
260 if( vlc_stream_Read( p_stream, NULL, i_toread ) != (ssize_t)i_toread )
261 return VLC_EGENERIC;
262 return VLC_SUCCESS;
265 static void MP4_BoxAddChild( MP4_Box_t *p_parent, MP4_Box_t *p_childbox )
267 if( !p_parent->p_first )
268 p_parent->p_first = p_childbox;
269 else
270 p_parent->p_last->p_next = p_childbox;
271 p_parent->p_last = p_childbox;
272 p_childbox->p_father = p_parent;
275 MP4_Box_t * MP4_BoxExtract( MP4_Box_t **pp_chain, uint32_t i_type )
277 MP4_Box_t *p_box = *pp_chain;
278 while( p_box )
280 if( p_box->i_type == i_type )
282 *pp_chain = p_box->p_next;
283 p_box->p_next = NULL;
284 return p_box;
286 pp_chain = &p_box->p_next;
287 p_box = p_box->p_next;
289 return NULL;
292 /* Don't use vlc_stream_Seek directly */
293 #undef vlc_stream_Seek
294 #define vlc_stream_Seek(a,b) __NO__
296 /*****************************************************************************
297 * MP4_PeekBoxHeader : Load only common parameters for all boxes
298 *****************************************************************************
299 * p_box need to be an already allocated MP4_Box_t, and all data
300 * will only be peek not read
302 * RETURN : 0 if it fail, 1 otherwise
303 *****************************************************************************/
304 static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box )
306 int i_read;
307 const uint8_t *p_peek;
309 if( ( ( i_read = vlc_stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
311 return 0;
313 p_box->i_pos = vlc_stream_Tell( p_stream );
315 p_box->data.p_payload = NULL;
316 p_box->p_father = NULL;
317 p_box->p_first = NULL;
318 p_box->p_last = NULL;
319 p_box->p_next = NULL;
321 MP4_GET4BYTES( p_box->i_shortsize );
322 MP4_GETFOURCC( p_box->i_type );
324 /* Now special case */
326 if( p_box->i_shortsize == 1 )
328 if( i_read < 8 )
329 return 0;
330 /* get the true size on 64 bits */
331 MP4_GET8BYTES( p_box->i_size );
333 else
335 p_box->i_size = p_box->i_shortsize;
336 /* XXX size of 0 means that the box extends to end of file */
339 if( UINT64_MAX - p_box->i_size < p_box->i_pos )
340 return 0;
342 if( p_box->i_type == ATOM_uuid )
344 if( i_read < 16 )
345 return 0;
346 /* get extented type on 16 bytes */
347 GetUUID( &p_box->i_uuid, p_peek );
350 #ifdef MP4_ULTRA_VERBOSE
351 if( p_box->i_size )
353 if MP4_BOX_TYPE_ASCII()
354 msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64" %"PRId64,
355 (char*)&p_box->i_type, p_box->i_size, p_box->i_pos );
356 else
357 msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
358 (char*)&p_box->i_type+1, p_box->i_size );
360 #endif
362 return 1;
365 /*****************************************************************************
366 * MP4_ReadBoxRestricted : Reads box from current position
367 *****************************************************************************
368 * if p_box == NULL, box is invalid or failed, position undefined
369 * on success, position is past read box or EOF
370 *****************************************************************************/
371 static MP4_Box_t *MP4_ReadBoxRestricted( stream_t *p_stream, MP4_Box_t *p_father,
372 const uint32_t onlytypes[], const uint32_t nottypes[],
373 bool *pb_restrictionhit )
375 MP4_Box_t peekbox = { 0 };
376 if ( !MP4_PeekBoxHeader( p_stream, &peekbox ) )
377 return NULL;
379 if( peekbox.i_size < 8 )
381 msg_Warn( p_stream, "found an invalid sized %"PRIu64" box %4.4s @%"PRIu64 ,
382 peekbox.i_size, (char *) &peekbox.i_type, vlc_stream_Tell(p_stream) );
383 return NULL;
386 for( size_t i=0; nottypes && nottypes[i]; i++ )
388 if( nottypes[i] == peekbox.i_type )
390 *pb_restrictionhit = true;
391 return NULL;
395 for( size_t i=0; onlytypes && onlytypes[i]; i++ )
397 if( onlytypes[i] != peekbox.i_type )
399 *pb_restrictionhit = true;
400 return NULL;
404 /* if father's size == 0, it means unknown or infinite size,
405 * and we skip the followong check */
406 if( p_father && p_father->i_size > 0 )
408 const uint64_t i_box_next = peekbox.i_size + peekbox.i_pos;
409 const uint64_t i_father_next = p_father->i_size + p_father->i_pos;
410 /* check if it's within p-father */
411 if( i_box_next > i_father_next )
413 msg_Warn( p_stream, "out of bound child %4.4s", (char*) &peekbox.i_type );
414 return NULL; /* out of bound */
418 /* Everything seems OK */
419 MP4_Box_t *p_box = (MP4_Box_t *) malloc( sizeof(MP4_Box_t) );
420 if( !p_box )
421 return NULL;
422 *p_box = peekbox;
424 const uint64_t i_next = p_box->i_pos + p_box->i_size;
425 p_box->p_father = p_father;
426 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
428 msg_Warn( p_stream, "Failed reading box %4.4s", (char*) &peekbox.i_type );
429 MP4_BoxFree( p_box );
430 p_box = NULL;
433 /* Check is we consumed all data */
434 if( vlc_stream_Tell( p_stream ) < i_next )
436 MP4_Seek( p_stream, i_next - 1 ); /* since past seek can fail when hitting EOF */
437 MP4_Seek( p_stream, i_next );
438 if( vlc_stream_Tell( p_stream ) < i_next - 1 ) /* Truncated box */
440 msg_Warn( p_stream, "truncated box %4.4s discarded", (char*) &peekbox.i_type );
441 MP4_BoxFree( p_box );
442 p_box = NULL;
446 if ( p_box )
447 MP4_BoxAddChild( p_father, p_box );
449 return p_box;
452 /*****************************************************************************
453 * For all known box a loader is given,
454 * you have to be already read container header
455 * without container size, file position on exit is unknown
456 *****************************************************************************/
457 static int MP4_ReadBoxContainerChildrenIndexed( stream_t *p_stream,
458 MP4_Box_t *p_container, const uint32_t stoplist[],
459 const uint32_t excludelist[], bool b_indexed )
461 /* Size of root container is set to 0 when unknown, for exemple
462 * with a DASH stream. In that case, we skip the following check */
463 if( (p_container->i_size || p_container->p_father)
464 && ( vlc_stream_Tell( p_stream ) + ((b_indexed)?16:8) >
465 (uint64_t)(p_container->i_pos + p_container->i_size) )
468 /* there is no box to load */
469 return 0;
472 uint64_t i_last_pos = 0; /* used to detect read failure loops */
473 const uint64_t i_end = p_container->i_pos + p_container->i_size;
474 MP4_Box_t *p_box = NULL;
475 bool b_onexclude = false;
476 bool b_continue;
479 b_continue = false;
480 if ( p_container->i_size )
482 const uint64_t i_tell = vlc_stream_Tell( p_stream );
483 if( i_tell + ((b_indexed)?16:8) >= i_end )
484 break;
487 uint32_t i_index = 0;
488 if ( b_indexed )
490 uint8_t read[8];
491 if ( vlc_stream_Read( p_stream, read, 8 ) < 8 )
492 break;
493 i_index = GetDWBE(&read[4]);
495 b_onexclude = false; /* If stopped due exclude list */
496 if( (p_box = MP4_ReadBoxRestricted( p_stream, p_container, NULL, excludelist, &b_onexclude )) )
498 b_continue = true;
499 p_box->i_index = i_index;
500 for(size_t i=0; stoplist && stoplist[i]; i++)
502 if( p_box->i_type == stoplist[i] )
503 return 1;
507 const uint64_t i_tell = vlc_stream_Tell( p_stream );
508 if ( p_container->i_size && i_tell >= i_end )
510 assert( i_tell == i_end );
511 break;
514 if ( !p_box )
516 /* Continue with next if box fails to load */
517 if( i_last_pos == i_tell )
518 break;
519 i_last_pos = i_tell;
520 b_continue = true;
523 } while( b_continue );
525 /* Always move to end of container */
526 if ( !b_onexclude && p_container->i_size )
528 const uint64_t i_tell = vlc_stream_Tell( p_stream );
529 if ( i_tell != i_end )
530 MP4_Seek( p_stream, i_end );
533 return 1;
536 int MP4_ReadBoxContainerRestricted( stream_t *p_stream, MP4_Box_t *p_container,
537 const uint32_t stoplist[], const uint32_t excludelist[] )
539 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
540 stoplist, excludelist, false );
543 int MP4_ReadBoxContainerChildren( stream_t *p_stream, MP4_Box_t *p_container,
544 const uint32_t stoplist[] )
546 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
547 stoplist, NULL, false );
550 static void MP4_BoxOffsetUp( MP4_Box_t *p_box, uint64_t i_offset )
552 while(p_box)
554 p_box->i_pos += i_offset;
555 MP4_BoxOffsetUp( p_box->p_first, i_offset );
556 p_box = p_box->p_next;
560 /* Reads within an already read/in memory box (containers without having to seek) */
561 static int MP4_ReadBoxContainerRawInBox( stream_t *p_stream, MP4_Box_t *p_container,
562 uint8_t *p_buffer, uint64_t i_size, uint64_t i_offset )
564 if(!p_container)
565 return 0;
566 stream_t *p_substream = vlc_stream_MemoryNew( p_stream, p_buffer, i_size,
567 true );
568 if( !p_substream )
569 return 0;
570 MP4_Box_t *p_last = p_container->p_last;
571 MP4_ReadBoxContainerChildren( p_substream, p_container, NULL );
572 vlc_stream_Delete( p_substream );
573 /* do pos fixup */
574 if( p_container )
576 MP4_Box_t *p_box = p_last ? p_last : p_container->p_first;
577 MP4_BoxOffsetUp(p_box, i_offset);
580 return 1;
583 static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
585 if( p_container->i_size &&
586 ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
588 /* container is empty, 8 stand for the first header in this box */
589 return 1;
592 /* enter box */
593 if ( MP4_Seek( p_stream, p_container->i_pos +
594 mp4_box_headersize( p_container ) ) )
595 return 0;
596 return MP4_ReadBoxContainerChildren( p_stream, p_container, NULL );
599 static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
601 /* XXX sometime moov is hidden in a free box */
602 if( p_box->p_father &&
603 p_box->p_father->i_type == ATOM_root &&
604 p_box->i_type == ATOM_free )
606 const uint8_t *p_peek;
607 size_t header_size = mp4_box_headersize( p_box ) + 4;
608 vlc_fourcc_t i_fcc;
610 ssize_t i_read = vlc_stream_Peek( p_stream, &p_peek, 44 );
611 if( unlikely(i_read < (ssize_t)header_size) )
612 return 0;
614 p_peek += header_size;
615 i_read -= header_size;
617 if( i_read >= 8 )
619 i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
621 if( i_fcc == ATOM_cmov || i_fcc == ATOM_mvhd )
623 msg_Warn( p_stream, "detected moov hidden in a free box ..." );
625 p_box->i_type = ATOM_foov;
626 return MP4_ReadBoxContainer( p_stream, p_box );
631 /* Nothing to do */
632 #ifdef MP4_ULTRA_VERBOSE
633 if MP4_BOX_TYPE_ASCII()
634 msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
635 else
636 msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
637 #endif
638 return 1;
641 static int MP4_ReadBox_ilst( stream_t *p_stream, MP4_Box_t *p_box )
643 if( p_box->i_size < 8 || vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
644 return 0;
646 /* Find our handler */
647 if ( !p_box->i_handler && p_box->p_father )
649 const MP4_Box_t *p_sibling = p_box->p_father->p_first;
650 while( p_sibling )
652 if ( p_sibling->i_type == ATOM_hdlr && p_sibling->data.p_hdlr )
654 p_box->i_handler = p_sibling->data.p_hdlr->i_handler_type;
655 break;
657 p_sibling = p_sibling->p_next;
661 switch( p_box->i_handler )
663 case 0:
664 msg_Warn( p_stream, "no handler for ilst atom" );
665 return 0;
666 case HANDLER_mdta:
667 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_box, NULL, NULL, true );
668 case HANDLER_mdir:
669 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
670 default:
671 msg_Warn( p_stream, "Unknown ilst handler type '%4.4s'", (char*)&p_box->i_handler );
672 return 0;
676 static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
678 free( p_box->data.p_ftyp->i_compatible_brands );
681 static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
683 MP4_READBOX_ENTER( MP4_Box_data_ftyp_t, MP4_FreeBox_ftyp );
685 MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
686 MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
688 p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4;
689 if( p_box->data.p_ftyp->i_compatible_brands_count > 0 )
691 uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
692 vlc_alloc( p_box->data.p_ftyp->i_compatible_brands_count,
693 sizeof(uint32_t) );
695 if( unlikely( tab == NULL ) )
696 MP4_READBOX_EXIT( 0 );
698 for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
700 MP4_GETFOURCC( tab[i] );
703 else
705 p_box->data.p_ftyp->i_compatible_brands = NULL;
708 MP4_READBOX_EXIT( 1 );
712 static int MP4_ReadBox_mvhd( stream_t *p_stream, MP4_Box_t *p_box )
714 MP4_READBOX_ENTER( MP4_Box_data_mvhd_t, NULL );
716 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
718 if( p_box->data.p_mvhd->i_version )
720 MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
721 MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
722 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
723 MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
725 else
727 MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
728 MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
729 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
730 MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
732 MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
733 MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
734 MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
737 for( unsigned i = 0; i < 2; i++ )
739 MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
741 for( unsigned i = 0; i < 9; i++ )
743 MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
745 for( unsigned i = 0; i < 6; i++ )
747 MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
750 MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
753 #ifdef MP4_VERBOSE
754 char *psz_duration = MP4_Time2Str( p_box->data.p_mvhd->i_duration, p_box->data.p_mvhd->i_timescale );
755 msg_Dbg( p_stream, "read box: \"mvhd\" timescale %"PRIu32" duration %"PRIu64" (%s) rate %.2f volume %.2f",
756 p_box->data.p_mvhd->i_timescale,
757 p_box->data.p_mvhd->i_duration,
758 psz_duration,
759 (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
760 (float)p_box->data.p_mvhd->i_volume / 256 );
761 free( psz_duration );
762 #endif
763 MP4_READBOX_EXIT( 1 );
766 static int MP4_ReadBox_mfhd( stream_t *p_stream, MP4_Box_t *p_box )
768 MP4_READBOX_ENTER( MP4_Box_data_mfhd_t, NULL );
770 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
772 MP4_GET4BYTES( p_box->data.p_mfhd->i_sequence_number );
774 #ifdef MP4_VERBOSE
775 msg_Dbg( p_stream, "read box: \"mfhd\" sequence number %d",
776 p_box->data.p_mfhd->i_sequence_number );
777 #endif
778 MP4_READBOX_EXIT( 1 );
781 static int MP4_ReadBox_tfxd( stream_t *p_stream, MP4_Box_t *p_box )
783 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, NULL );
785 MP4_Box_data_tfxd_t *p_tfxd_data = p_box->data.p_tfxd;
786 MP4_GETVERSIONFLAGS( p_tfxd_data );
788 if( p_tfxd_data->i_version == 0 )
790 MP4_GET4BYTES( p_tfxd_data->i_fragment_abs_time );
791 MP4_GET4BYTES( p_tfxd_data->i_fragment_duration );
793 else
795 MP4_GET8BYTES( p_tfxd_data->i_fragment_abs_time );
796 MP4_GET8BYTES( p_tfxd_data->i_fragment_duration );
799 #ifdef MP4_VERBOSE
800 msg_Dbg( p_stream, "read box: \"tfxd\" version %d, flags 0x%x, "\
801 "fragment duration %"PRIu64", fragment abs time %"PRIu64,
802 p_tfxd_data->i_version,
803 p_tfxd_data->i_flags,
804 p_tfxd_data->i_fragment_duration,
805 p_tfxd_data->i_fragment_abs_time
807 #endif
809 MP4_READBOX_EXIT( 1 );
812 static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
814 free( p_box->data.p_tfrf->p_tfrf_data_fields );
817 static int MP4_ReadBox_tfrf( stream_t *p_stream, MP4_Box_t *p_box )
819 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, MP4_FreeBox_tfrf );
821 MP4_Box_data_tfrf_t *p_tfrf_data = p_box->data.p_tfrf;
822 MP4_GETVERSIONFLAGS( p_tfrf_data );
824 MP4_GET1BYTE( p_tfrf_data->i_fragment_count );
826 p_tfrf_data->p_tfrf_data_fields = calloc( p_tfrf_data->i_fragment_count,
827 sizeof( TfrfBoxDataFields_t ) );
828 if( !p_tfrf_data->p_tfrf_data_fields )
829 MP4_READBOX_EXIT( 0 );
831 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
833 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
834 if( p_tfrf_data->i_version == 0 )
836 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_abs_time );
837 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_duration );
839 else
841 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_abs_time );
842 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_duration );
846 #ifdef MP4_VERBOSE
847 msg_Dbg( p_stream, "read box: \"tfrf\" version %d, flags 0x%x, "\
848 "fragment count %"PRIu8, p_tfrf_data->i_version,
849 p_tfrf_data->i_flags, p_tfrf_data->i_fragment_count );
851 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
853 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
854 msg_Dbg( p_stream, "\"tfrf\" fragment duration %"PRIu64", "\
855 "fragment abs time %"PRIu64,
856 TfrfBoxDataField->i_fragment_duration,
857 TfrfBoxDataField->i_fragment_abs_time );
860 #endif
862 MP4_READBOX_EXIT( 1 );
865 static int MP4_ReadBox_XML360( stream_t *p_stream, MP4_Box_t *p_box )
867 MP4_READBOX_ENTER( MP4_Box_data_360_t, NULL );
869 MP4_Box_data_360_t *p_360_data = p_box->data.p_360;
871 /* Copy the string for pattern matching as it does not end
872 with a '\0' in the stream. */
873 char *psz_rdf = strndup((char *)p_peek, i_read);
875 if ( unlikely( !psz_rdf ) )
876 MP4_READBOX_EXIT( 0 );
878 /* Try to find the string "GSpherical:Spherical" because the v1
879 spherical video spec says the tag must be there. */
881 if ( strcasestr( psz_rdf, "Gspherical:Spherical" ) )
882 p_360_data->i_projection_mode = PROJECTION_MODE_EQUIRECTANGULAR;
884 /* Try to find the stero mode. */
885 if ( strcasestr( psz_rdf, "left-right" ) )
887 msg_Dbg( p_stream, "Left-right stereo mode" );
888 p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_LEFT_RIGHT;
891 if ( strcasestr( psz_rdf, "top-bottom" ) )
893 msg_Dbg( p_stream, "Top-bottom stereo mode" );
894 p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_TOP_BOTTOM;
897 free( psz_rdf );
899 MP4_READBOX_EXIT( 1 );
902 static int MP4_ReadBox_st3d( stream_t *p_stream, MP4_Box_t *p_box )
904 MP4_READBOX_ENTER( MP4_Box_data_st3d_t, NULL );
906 uint8_t i_version;
907 MP4_GET1BYTE( i_version );
908 if ( i_version != 0 )
909 MP4_READBOX_EXIT( 0 );
911 uint32_t i_flags;
912 VLC_UNUSED( i_flags );
913 MP4_GET3BYTES( i_flags );
915 MP4_Box_data_st3d_t *p_data = p_box->data.p_st3d;
916 MP4_GET1BYTE( p_data->i_stereo_mode );
918 MP4_READBOX_EXIT( 1 );
921 static int MP4_ReadBox_prhd( stream_t *p_stream, MP4_Box_t *p_box )
923 MP4_READBOX_ENTER( MP4_Box_data_prhd_t, NULL );
925 uint8_t i_version;
926 MP4_GET1BYTE( i_version );
927 if (i_version != 0)
928 MP4_READBOX_EXIT( 0 );
930 uint32_t i_flags;
931 VLC_UNUSED( i_flags );
932 MP4_GET3BYTES( i_flags );
934 MP4_Box_data_prhd_t *p_data = p_box->data.p_prhd;
935 int32_t fixed16_16;
936 MP4_GET4BYTES( fixed16_16 );
937 p_data->f_pose_yaw_degrees = (float) fixed16_16 / 65536.0f;
939 MP4_GET4BYTES( fixed16_16 );
940 p_data->f_pose_pitch_degrees = (float) fixed16_16 / 65536.0f;
942 MP4_GET4BYTES( fixed16_16 );
943 p_data->f_pose_roll_degrees = (float) fixed16_16 / 65536.0f;
945 MP4_READBOX_EXIT( 1 );
948 static int MP4_ReadBox_equi( stream_t *p_stream, MP4_Box_t *p_box )
950 MP4_READBOX_ENTER( MP4_Box_data_equi_t, NULL );
952 uint8_t i_version;
953 MP4_GET1BYTE( i_version );
954 if (i_version != 0)
955 MP4_READBOX_EXIT( 0 );
957 uint32_t i_flags;
958 VLC_UNUSED( i_flags );
959 MP4_GET3BYTES( i_flags );
961 MP4_Box_data_equi_t *p_data = p_box->data.p_equi;
962 MP4_GET4BYTES( p_data->i_projection_bounds_top );
963 MP4_GET4BYTES( p_data->i_projection_bounds_bottom );
964 MP4_GET4BYTES( p_data->i_projection_bounds_left );
965 MP4_GET4BYTES( p_data->i_projection_bounds_right );
967 MP4_READBOX_EXIT( 1 );
970 static int MP4_ReadBox_cbmp( stream_t *p_stream, MP4_Box_t *p_box )
972 MP4_READBOX_ENTER( MP4_Box_data_cbmp_t, NULL );
974 uint8_t i_version;
975 MP4_GET1BYTE( i_version );
976 if (i_version != 0)
977 MP4_READBOX_EXIT( 0 );
979 uint32_t i_flags;
980 VLC_UNUSED( i_flags );
981 MP4_GET3BYTES( i_flags );
983 MP4_Box_data_cbmp_t *p_data = p_box->data.p_cbmp;
984 MP4_GET4BYTES( p_data->i_layout );
985 MP4_GET4BYTES( p_data->i_padding );
987 MP4_READBOX_EXIT( 1 );
990 static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
992 free( p_box->data.p_sidx->p_items );
995 static int MP4_ReadBox_sidx( stream_t *p_stream, MP4_Box_t *p_box )
997 MP4_READBOX_ENTER( MP4_Box_data_sidx_t, MP4_FreeBox_sidx );
999 MP4_Box_data_sidx_t *p_sidx_data = p_box->data.p_sidx;
1000 MP4_GETVERSIONFLAGS( p_sidx_data );
1002 MP4_GET4BYTES( p_sidx_data->i_reference_ID );
1003 MP4_GET4BYTES( p_sidx_data->i_timescale );
1005 if( p_sidx_data->i_version == 0 )
1007 MP4_GET4BYTES( p_sidx_data->i_earliest_presentation_time );
1008 MP4_GET4BYTES( p_sidx_data->i_first_offset );
1010 else
1012 MP4_GET8BYTES( p_sidx_data->i_earliest_presentation_time );
1013 MP4_GET8BYTES( p_sidx_data->i_first_offset );
1016 uint16_t i_reserved, i_count;
1018 VLC_UNUSED(i_reserved);
1019 MP4_GET2BYTES( i_reserved );
1020 MP4_GET2BYTES( i_count );
1021 if( i_count == 0 )
1022 MP4_READBOX_EXIT( 1 );
1024 p_sidx_data->i_reference_count = i_count;
1025 p_sidx_data->p_items = vlc_alloc( i_count, sizeof( MP4_Box_sidx_item_t ) );
1026 if( unlikely(p_sidx_data->p_items == NULL) )
1027 MP4_READBOX_EXIT( 0 );
1029 for( unsigned i = 0; i < i_count; i++ )
1031 MP4_Box_sidx_item_t *item = p_sidx_data->p_items + i;
1032 uint32_t tmp;
1034 MP4_GET4BYTES( tmp );
1035 item->b_reference_type = tmp >> 31;
1036 item->i_referenced_size = tmp & 0x7fffffff;
1037 MP4_GET4BYTES( item->i_subsegment_duration );
1039 MP4_GET4BYTES( tmp );
1040 item->b_starts_with_SAP = tmp >> 31;
1041 item->i_SAP_type = (tmp >> 24) & 0x70;
1042 item->i_SAP_delta_time = tmp & 0xfffffff;
1045 #ifdef MP4_VERBOSE
1046 msg_Dbg( p_stream, "read box: \"sidx\" version %d, flags 0x%x, "\
1047 "ref_ID %"PRIu32", timescale %"PRIu32", ref_count %"PRIu16", "\
1048 "first subsegmt duration %"PRIu32,
1049 p_sidx_data->i_version,
1050 p_sidx_data->i_flags,
1051 p_sidx_data->i_reference_ID,
1052 p_sidx_data->i_timescale,
1053 p_sidx_data->i_reference_count,
1054 p_sidx_data->p_items[0].i_subsegment_duration
1056 #endif
1058 MP4_READBOX_EXIT( 1 );
1061 static int MP4_ReadBox_tfhd( stream_t *p_stream, MP4_Box_t *p_box )
1063 MP4_READBOX_ENTER( MP4_Box_data_tfhd_t, NULL );
1065 MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );
1067 if( p_box->data.p_tfhd->i_version != 0 )
1069 msg_Warn( p_stream, "'tfhd' box with version != 0. "\
1070 " Don't know what to do with that, please patch" );
1071 MP4_READBOX_EXIT( 0 );
1074 MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );
1076 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
1078 msg_Dbg( p_stream, "'duration-is-empty' flag is present "\
1079 "=> no samples for this time interval." );
1080 p_box->data.p_tfhd->b_empty = true;
1082 else
1083 p_box->data.p_tfhd->b_empty = false;
1085 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
1086 MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
1087 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
1088 MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
1089 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
1090 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
1091 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
1092 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
1093 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
1094 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
1096 #ifdef MP4_VERBOSE
1097 char psz_base[128] = "\0";
1098 char psz_desc[128] = "\0";
1099 char psz_dura[128] = "\0";
1100 char psz_size[128] = "\0";
1101 char psz_flag[128] = "\0";
1102 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
1103 snprintf(psz_base, sizeof(psz_base), "base offset %"PRId64, p_box->data.p_tfhd->i_base_data_offset);
1104 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
1105 snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
1106 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
1107 snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
1108 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
1109 snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
1110 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
1111 snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);
1113 msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
1114 p_box->data.p_tfhd->i_version,
1115 p_box->data.p_tfhd->i_flags,
1116 p_box->data.p_tfhd->i_track_ID,
1117 psz_base, psz_desc, psz_dura, psz_size, psz_flag );
1118 #endif
1120 MP4_READBOX_EXIT( 1 );
1123 static void MP4_FreeBox_trun( MP4_Box_t *p_box )
1125 free( p_box->data.p_trun->p_samples );
1128 static int MP4_ReadBox_trun( stream_t *p_stream, MP4_Box_t *p_box )
1130 uint32_t count;
1132 MP4_READBOX_ENTER( MP4_Box_data_trun_t, MP4_FreeBox_trun );
1133 MP4_Box_data_trun_t *p_trun = p_box->data.p_trun;
1134 MP4_GETVERSIONFLAGS( p_trun );
1135 MP4_GET4BYTES( count );
1137 if( p_trun->i_flags & MP4_TRUN_DATA_OFFSET )
1138 MP4_GET4BYTES( p_trun->i_data_offset );
1139 if( p_trun->i_flags & MP4_TRUN_FIRST_FLAGS )
1140 MP4_GET4BYTES( p_trun->i_first_sample_flags );
1142 uint64_t i_entry_size =
1143 !!(p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION) +
1144 !!(p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE) +
1145 !!(p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS) +
1146 !!(p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET);
1148 if( i_entry_size * 4 * count > i_read )
1149 MP4_READBOX_EXIT( 0 );
1151 p_trun->p_samples = vlc_alloc( count, sizeof(MP4_descriptor_trun_sample_t) );
1152 if ( p_trun->p_samples == NULL )
1153 MP4_READBOX_EXIT( 0 );
1154 p_trun->i_sample_count = count;
1156 for( unsigned int i = 0; i < count; i++ )
1158 MP4_descriptor_trun_sample_t *p_sample = &p_trun->p_samples[i];
1159 if( p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
1160 MP4_GET4BYTES( p_sample->i_duration );
1161 if( p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
1162 MP4_GET4BYTES( p_sample->i_size );
1163 if( p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS )
1164 MP4_GET4BYTES( p_sample->i_flags );
1165 if( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
1166 MP4_GET4BYTES( p_sample->i_composition_time_offset.v0 );
1169 #ifdef MP4_ULTRA_VERBOSE
1170 msg_Dbg( p_stream, "read box: \"trun\" version %u flags 0x%x sample count %"PRIu32,
1171 p_trun->i_version,
1172 p_trun->i_flags,
1173 p_trun->i_sample_count );
1175 for( unsigned int i = 0; i < count; i++ )
1177 MP4_descriptor_trun_sample_t *p_sample = &p_trun->p_samples[i];
1178 msg_Dbg( p_stream, "read box: \"trun\" sample %4.4u flags 0x%x "\
1179 "duration %"PRIu32" size %"PRIu32" composition time offset %"PRIu32,
1180 i, p_sample->i_flags, p_sample->i_duration,
1181 p_sample->i_size, p_sample->i_composition_time_offset );
1183 #endif
1185 MP4_READBOX_EXIT( 1 );
1188 static int MP4_ReadBox_tfdt( stream_t *p_stream, MP4_Box_t *p_box )
1190 MP4_READBOX_ENTER( MP4_Box_data_tfdt_t, NULL );
1191 if( i_read < 8 )
1192 MP4_READBOX_EXIT( 0 );
1194 MP4_GETVERSIONFLAGS( p_box->data.p_tfdt );
1196 if( p_box->data.p_tfdt->i_version == 0 )
1197 MP4_GET4BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1198 else if( p_box->data.p_tfdt->i_version == 1 )
1199 MP4_GET8BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1200 else
1201 MP4_READBOX_EXIT( 0 );
1203 MP4_READBOX_EXIT( 1 );
1206 static int MP4_ReadBox_tkhd( stream_t *p_stream, MP4_Box_t *p_box )
1208 MP4_READBOX_ENTER( MP4_Box_data_tkhd_t, NULL );
1210 MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
1212 if( p_box->data.p_tkhd->i_version )
1214 MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
1215 MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
1216 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1217 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1218 MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
1220 else
1222 MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
1223 MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
1224 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1225 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1226 MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
1229 for( unsigned i = 0; i < 2; i++ )
1231 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
1233 MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
1234 MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
1235 MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
1236 MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
1238 for( unsigned i = 0; i < 9; i++ )
1240 MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
1242 MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
1243 MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
1245 double rotation = 0;//angle in degrees to be rotated clockwise
1246 double scale[2]; // scale factor; sx = scale[0] , sy = scale[1]
1247 int32_t *matrix = p_box->data.p_tkhd->i_matrix;
1249 scale[0] = sqrt(conv_fx(matrix[0]) * conv_fx(matrix[0]) +
1250 conv_fx(matrix[3]) * conv_fx(matrix[3]));
1251 scale[1] = sqrt(conv_fx(matrix[1]) * conv_fx(matrix[1]) +
1252 conv_fx(matrix[4]) * conv_fx(matrix[4]));
1254 if( likely(scale[0] > 0 && scale[1] > 0) )
1256 rotation = atan2(conv_fx(matrix[1]) / scale[1],
1257 conv_fx(matrix[0]) / scale[0]) * 180 / M_PI;
1258 if (rotation < 0)
1259 rotation += 360.;
1262 p_box->data.p_tkhd->f_rotation = rotation;
1264 #ifdef MP4_VERBOSE
1265 double translate[2];// amount to translate; tx = translate[0] , ty = translate[1]
1267 translate[0] = conv_fx(matrix[6]);
1268 translate[1] = conv_fx(matrix[7]);
1270 msg_Dbg( p_stream, "read box: \"tkhd\" track #%"PRIu32" duration %"PRIu64" layer %d "
1271 "volume %3.1f rotation %3.1f scale %.2fx%.2f translate +%.2f+%.2f size %ux%u. "
1272 "Matrix: %i %i %i %i %i %i %i %i %i",
1273 p_box->data.p_tkhd->i_track_ID,
1274 p_box->data.p_tkhd->i_duration,
1275 p_box->data.p_tkhd->i_layer,
1276 (float)p_box->data.p_tkhd->i_volume / 256 ,
1277 rotation,
1278 scale[0],
1279 scale[1],
1280 translate[0],
1281 translate[1],
1282 (unsigned)p_box->data.p_tkhd->i_width / BLOCK16x16,
1283 (unsigned)p_box->data.p_tkhd->i_height / BLOCK16x16,
1284 p_box->data.p_tkhd->i_matrix[0],
1285 p_box->data.p_tkhd->i_matrix[1],
1286 p_box->data.p_tkhd->i_matrix[2],
1287 p_box->data.p_tkhd->i_matrix[3],
1288 p_box->data.p_tkhd->i_matrix[4],
1289 p_box->data.p_tkhd->i_matrix[5],
1290 p_box->data.p_tkhd->i_matrix[6],
1291 p_box->data.p_tkhd->i_matrix[7],
1292 p_box->data.p_tkhd->i_matrix[8] );
1293 #endif
1294 MP4_READBOX_EXIT( 1 );
1297 static int MP4_ReadBox_load( stream_t *p_stream, MP4_Box_t *p_box )
1299 if ( p_box->i_size != 24 )
1300 return 0;
1301 MP4_READBOX_ENTER( MP4_Box_data_load_t, NULL );
1302 MP4_GET4BYTES( p_box->data.p_load->i_start_time );
1303 MP4_GET4BYTES( p_box->data.p_load->i_duration );
1304 MP4_GET4BYTES( p_box->data.p_load->i_flags );
1305 MP4_GET4BYTES( p_box->data.p_load->i_hints );
1306 MP4_READBOX_EXIT( 1 );
1309 static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
1311 uint16_t i_language;
1312 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t, NULL );
1314 MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
1316 if( p_box->data.p_mdhd->i_version )
1318 MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
1319 MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
1320 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1321 MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
1323 else
1325 MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
1326 MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
1327 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1328 MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
1331 MP4_GET2BYTES( i_language );
1332 decodeQtLanguageCode( i_language, p_box->data.p_mdhd->rgs_language,
1333 &p_box->data.p_mdhd->b_mac_encoding );
1335 MP4_GET2BYTES( p_box->data.p_mdhd->i_quality );
1337 #ifdef MP4_VERBOSE
1338 char *psz_duration = MP4_Time2Str( p_box->data.p_mdhd->i_duration, p_box->data.p_mdhd->i_timescale );
1339 msg_Dbg( p_stream, "read box: \"mdhd\" timescale %"PRIu32" duration %"PRIu64" (%s) language %3.3s",
1340 p_box->data.p_mdhd->i_timescale,
1341 p_box->data.p_mdhd->i_duration,
1342 psz_duration,
1343 (char*) &p_box->data.p_mdhd->rgs_language );
1344 free( psz_duration );
1345 #endif
1346 MP4_READBOX_EXIT( 1 );
1349 static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
1351 free( p_box->data.p_hdlr->psz_name );
1354 static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
1356 int32_t i_reserved;
1357 VLC_UNUSED(i_reserved);
1359 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t, MP4_FreeBox_hdlr );
1361 MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
1363 MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
1364 MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
1366 MP4_GET4BYTES( i_reserved );
1367 MP4_GET4BYTES( i_reserved );
1368 MP4_GET4BYTES( i_reserved );
1369 p_box->data.p_hdlr->psz_name = NULL;
1371 if( i_read >= SSIZE_MAX )
1372 MP4_READBOX_EXIT( 0 );
1374 if( i_read > 0 )
1376 size_t i_copy;
1378 /* Yes, I love .mp4 :( */
1379 if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
1381 uint8_t i_len;
1383 MP4_GET1BYTE( i_len );
1384 i_copy = (i_len <= i_read) ? i_len : i_read;
1386 else
1387 i_copy = i_read;
1389 uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_copy + 1 );
1390 if( unlikely( psz == NULL ) )
1391 MP4_READBOX_EXIT( 0 );
1393 memcpy( psz, p_peek, i_copy );
1394 p_box->data.p_hdlr->psz_name[i_copy] = '\0';
1397 #ifdef MP4_VERBOSE
1398 msg_Dbg( p_stream, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
1399 (char*)&p_box->data.p_hdlr->i_handler_type,
1400 p_box->data.p_hdlr->psz_name );
1402 #endif
1403 MP4_READBOX_EXIT( 1 );
1406 static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
1408 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t, NULL );
1410 MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
1412 MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
1413 for( unsigned i = 0; i < 3; i++ )
1415 MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
1418 #ifdef MP4_VERBOSE
1419 msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
1420 p_box->data.p_vmhd->i_graphics_mode,
1421 p_box->data.p_vmhd->i_opcolor[0],
1422 p_box->data.p_vmhd->i_opcolor[1],
1423 p_box->data.p_vmhd->i_opcolor[2] );
1424 #endif
1425 MP4_READBOX_EXIT( 1 );
1428 static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
1430 MP4_READBOX_ENTER( MP4_Box_data_smhd_t, NULL );
1432 MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
1436 MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
1438 MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
1440 #ifdef MP4_VERBOSE
1441 msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
1442 (float)p_box->data.p_smhd->i_balance / 256 );
1443 #endif
1444 MP4_READBOX_EXIT( 1 );
1448 static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
1450 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t, NULL );
1452 MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
1454 MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
1455 MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
1457 MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
1458 MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
1460 MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
1462 #ifdef MP4_VERBOSE
1463 msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1464 p_box->data.p_hmhd->i_max_PDU_size,
1465 p_box->data.p_hmhd->i_avg_PDU_size,
1466 p_box->data.p_hmhd->i_max_bitrate,
1467 p_box->data.p_hmhd->i_avg_bitrate );
1468 #endif
1469 MP4_READBOX_EXIT( 1 );
1472 static void MP4_FreeBox_url( MP4_Box_t *p_box )
1474 free( p_box->data.p_url->psz_location );
1477 static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
1479 MP4_READBOX_ENTER( MP4_Box_data_url_t, MP4_FreeBox_url );
1481 MP4_GETVERSIONFLAGS( p_box->data.p_url );
1482 MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
1484 #ifdef MP4_VERBOSE
1485 msg_Dbg( p_stream, "read box: \"url\" url: %s",
1486 p_box->data.p_url->psz_location );
1488 #endif
1489 MP4_READBOX_EXIT( 1 );
1492 static void MP4_FreeBox_urn( MP4_Box_t *p_box )
1494 free( p_box->data.p_urn->psz_name );
1495 free( p_box->data.p_urn->psz_location );
1498 static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
1500 MP4_READBOX_ENTER( MP4_Box_data_urn_t, MP4_FreeBox_urn );
1502 MP4_GETVERSIONFLAGS( p_box->data.p_urn );
1504 MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
1505 MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
1507 #ifdef MP4_VERBOSE
1508 msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
1509 p_box->data.p_urn->psz_name,
1510 p_box->data.p_urn->psz_location );
1511 #endif
1512 MP4_READBOX_EXIT( 1 );
1515 static int MP4_ReadBox_LtdContainer( stream_t *p_stream, MP4_Box_t *p_box )
1517 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_lcont_t, 16, NULL );
1518 if( i_read < 8 )
1519 MP4_READBOX_EXIT( 0 );
1521 MP4_GETVERSIONFLAGS( p_box->data.p_lcont );
1522 if( p_box->data.p_lcont->i_version != 0 )
1523 MP4_READBOX_EXIT( 0 );
1524 MP4_GET4BYTES( p_box->data.p_lcont->i_entry_count );
1526 uint32_t i_entry = 0;
1527 i_read = p_box->i_size - 16;
1528 while (i_read > 8 && i_entry < p_box->data.p_lcont->i_entry_count )
1530 MP4_Box_t *p_childbox = MP4_ReadBox( p_stream, p_box );
1531 if( !p_childbox )
1532 break;
1533 MP4_BoxAddChild( p_box, p_childbox );
1534 i_entry++;
1536 if( i_read < p_childbox->i_size )
1537 MP4_READBOX_EXIT( 0 );
1539 i_read -= p_childbox->i_size;
1542 if (i_entry != p_box->data.p_lcont->i_entry_count)
1543 p_box->data.p_lcont->i_entry_count = i_entry;
1545 #ifdef MP4_VERBOSE
1546 msg_Dbg( p_stream, "read box: \"%4.4s\" entry-count %d", (char *)&p_box->i_type,
1547 p_box->data.p_lcont->i_entry_count );
1549 #endif
1551 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
1552 MP4_READBOX_EXIT( 0 );
1554 MP4_READBOX_EXIT( 1 );
1557 static void MP4_FreeBox_stts( MP4_Box_t *p_box )
1559 free( p_box->data.p_stts->pi_sample_count );
1560 free( p_box->data.p_stts->pi_sample_delta );
1563 static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
1565 uint32_t count;
1567 MP4_READBOX_ENTER( MP4_Box_data_stts_t, MP4_FreeBox_stts );
1569 MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1570 MP4_GET4BYTES( count );
1572 if( UINT64_C(8) * count > i_read )
1574 /*count = i_read / 8;*/
1575 MP4_READBOX_EXIT( 0 );
1578 p_box->data.p_stts->pi_sample_count = vlc_alloc( count, sizeof(uint32_t) );
1579 p_box->data.p_stts->pi_sample_delta = vlc_alloc( count, sizeof(int32_t) );
1580 p_box->data.p_stts->i_entry_count = count;
1582 if( p_box->data.p_stts->pi_sample_count == NULL
1583 || p_box->data.p_stts->pi_sample_delta == NULL )
1585 MP4_READBOX_EXIT( 0 );
1588 for( uint32_t i = 0; i < count; i++ )
1590 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_count[i] );
1591 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_delta[i] );
1594 #ifdef MP4_VERBOSE
1595 msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
1596 p_box->data.p_stts->i_entry_count );
1598 #endif
1599 MP4_READBOX_EXIT( 1 );
1603 static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
1605 free( p_box->data.p_ctts->pi_sample_count );
1606 free( p_box->data.p_ctts->pi_sample_offset );
1609 static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
1611 uint32_t count;
1613 MP4_READBOX_ENTER( MP4_Box_data_ctts_t, MP4_FreeBox_ctts );
1615 MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1616 MP4_GET4BYTES( count );
1618 if( UINT64_C(8) * count > i_read )
1619 MP4_READBOX_EXIT( 0 );
1621 p_box->data.p_ctts->pi_sample_count = vlc_alloc( count, sizeof(uint32_t) );
1622 p_box->data.p_ctts->pi_sample_offset = vlc_alloc( count, sizeof(int32_t) );
1623 if( unlikely(p_box->data.p_ctts->pi_sample_count == NULL
1624 || p_box->data.p_ctts->pi_sample_offset == NULL) )
1625 MP4_READBOX_EXIT( 0 );
1626 p_box->data.p_ctts->i_entry_count = count;
1628 for( uint32_t i = 0; i < count; i++ )
1630 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_count[i] );
1631 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_offset[i] );
1634 #ifdef MP4_VERBOSE
1635 msg_Dbg( p_stream, "read box: \"ctts\" entry-count %"PRIu32, count );
1637 #endif
1638 MP4_READBOX_EXIT( 1 );
1641 static int MP4_ReadBox_cslg( stream_t *p_stream, MP4_Box_t *p_box )
1643 MP4_READBOX_ENTER( MP4_Box_data_cslg_t, NULL );
1645 unsigned i_version, i_flags;
1646 MP4_GET1BYTE( i_version );
1647 MP4_GET3BYTES( i_flags );
1648 VLC_UNUSED(i_flags);
1650 if( i_version > 1 )
1651 MP4_READBOX_EXIT( 0 );
1653 #define READ_CSLG(readbytes) {\
1654 readbytes( p_box->data.p_cslg->ct_to_dts_shift );\
1655 readbytes( p_box->data.p_cslg->i_least_delta );\
1656 readbytes( p_box->data.p_cslg->i_max_delta );\
1657 readbytes( p_box->data.p_cslg->i_composition_starttime );\
1658 readbytes( p_box->data.p_cslg->i_composition_endtime ); }
1660 if( i_version == 0 )
1661 READ_CSLG(MP4_GET4BYTES)
1662 else
1663 READ_CSLG(MP4_GET8BYTES)
1665 MP4_READBOX_EXIT( 1 );
1668 static uint64_t MP4_ReadLengthDescriptor( uint8_t **restrict bufp,
1669 uint64_t *restrict lenp )
1671 unsigned char *buf = *bufp;
1672 uint64_t len = *lenp;
1673 unsigned char b;
1674 uint64_t value = 0;
1678 if (unlikely(len == 0))
1679 return -1; /* end of bit stream */
1680 if (unlikely(value > (UINT64_MAX >> 7)))
1681 return -1; /* integer overflow */
1683 b = *(buf++);
1684 len--;
1685 value = (value << 7) + (b & 0x7f);
1687 while (b & 0x80);
1689 *bufp = buf;
1690 *lenp = len;
1691 return value;
1695 static void MP4_FreeBox_esds( MP4_Box_t *p_box )
1697 free( p_box->data.p_esds->es_descriptor.psz_URL );
1698 if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1700 free( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1701 free( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1705 static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
1707 #define es_descriptor p_box->data.p_esds->es_descriptor
1708 uint64_t i_len;
1709 unsigned int i_flags;
1710 unsigned int i_type;
1712 MP4_READBOX_ENTER( MP4_Box_data_esds_t, MP4_FreeBox_esds );
1714 MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1717 MP4_GET1BYTE( i_type );
1718 if( i_type == 0x03 ) /* MP4ESDescrTag ISO/IEC 14496-1 8.3.3 */
1720 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1721 if( unlikely(i_len == UINT64_C(-1)) )
1722 MP4_READBOX_EXIT( 0 );
1724 #ifdef MP4_VERBOSE
1725 msg_Dbg( p_stream, "found esds MPEG4ESDescr (%"PRIu64" bytes)",
1726 i_len );
1727 #endif
1729 MP4_GET2BYTES( es_descriptor.i_ES_ID );
1730 MP4_GET1BYTE( i_flags );
1731 es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1732 es_descriptor.b_url = ( (i_flags&0x40) != 0);
1733 es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1735 es_descriptor.i_stream_priority = i_flags&0x1f;
1736 if( es_descriptor.b_stream_dependence )
1738 MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1740 if( es_descriptor.b_url && i_read > 0 )
1742 uint8_t i_url;
1744 MP4_GET1BYTE( i_url );
1745 if( i_url > i_read )
1746 MP4_READBOX_EXIT( 1 );
1747 es_descriptor.psz_URL = malloc( (unsigned) i_url + 1 );
1748 if( es_descriptor.psz_URL )
1750 memcpy( es_descriptor.psz_URL, p_peek, i_url );
1751 es_descriptor.psz_URL[i_url] = 0;
1753 p_peek += i_url;
1754 i_read -= i_url;
1756 else
1758 es_descriptor.psz_URL = NULL;
1760 if( es_descriptor.b_OCRstream )
1762 MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1764 MP4_GET1BYTE( i_type ); /* get next type */
1767 if( i_type != 0x04)/* MP4DecConfigDescrTag ISO/IEC 14496-1 8.3.4 */
1769 es_descriptor.p_decConfigDescr = NULL;
1770 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1773 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1774 if( unlikely(i_len == UINT64_C(-1)) )
1775 MP4_READBOX_EXIT( 0 );
1776 #ifdef MP4_VERBOSE
1777 msg_Dbg( p_stream, "found esds MP4DecConfigDescr (%"PRIu64" bytes)",
1778 i_len );
1779 #endif
1781 es_descriptor.p_decConfigDescr =
1782 calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
1783 if( unlikely( es_descriptor.p_decConfigDescr == NULL ) )
1784 MP4_READBOX_EXIT( 0 );
1786 MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectProfileIndication );
1787 MP4_GET1BYTE( i_flags );
1788 es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1789 es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1790 MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1791 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1792 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1793 MP4_GET1BYTE( i_type );
1794 if( i_type != 0x05 )/* MP4DecSpecificDescrTag ISO/IEC 14496-1 8.3.5 */
1796 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1797 es_descriptor.p_decConfigDescr->p_decoder_specific_info = NULL;
1798 MP4_READBOX_EXIT( 1 );
1801 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1802 if( unlikely(i_len == UINT64_C(-1)) )
1803 MP4_READBOX_EXIT( 0 );
1804 #ifdef MP4_VERBOSE
1805 msg_Dbg( p_stream, "found esds MP4DecSpecificDescr (%"PRIu64" bytes)",
1806 i_len );
1807 #endif
1808 if( i_len > i_read )
1809 MP4_READBOX_EXIT( 0 );
1811 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1812 es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1813 if( unlikely( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL ) )
1814 MP4_READBOX_EXIT( 0 );
1816 memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1817 p_peek, i_len );
1819 MP4_READBOX_EXIT( 1 );
1820 #undef es_descriptor
1823 static void MP4_FreeBox_av1C( MP4_Box_t *p_box )
1825 MP4_Box_data_av1C_t *p_av1C = p_box->data.p_av1C;
1826 free( p_av1C->p_av1C );
1829 static int MP4_ReadBox_av1C( stream_t *p_stream, MP4_Box_t *p_box )
1831 MP4_Box_data_av1C_t *p_av1C;
1833 MP4_READBOX_ENTER( MP4_Box_data_av1C_t, MP4_FreeBox_av1C );
1834 p_av1C = p_box->data.p_av1C;
1836 if( i_read < 4 ||
1837 p_peek[0] != 0x81 ) /* marker / version */
1838 MP4_READBOX_EXIT( 0 );
1840 p_av1C->p_av1C = malloc( i_read );
1841 if( p_av1C->p_av1C )
1843 memcpy( p_av1C->p_av1C, p_peek, i_read );
1844 p_av1C->i_av1C = i_read;
1847 uint8_t i_8b;
1848 MP4_GET1BYTE( i_8b ); /* marker / version */
1850 MP4_GET1BYTE( i_8b );
1851 p_av1C->i_profile = i_8b >> 5;
1852 p_av1C->i_level = i_8b & 0x1F;
1854 MP4_GET1BYTE( i_8b );
1855 MP4_GET1BYTE( i_8b );
1857 if( i_8b & 0x10 ) /* delay flag */
1858 p_av1C->i_presentation_delay = 1 + (i_8b & 0x0F);
1859 else
1860 p_av1C->i_presentation_delay = 0;
1862 MP4_READBOX_EXIT( 1 );
1865 static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
1867 MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1868 free( p_avcC->p_avcC );
1871 static int MP4_ReadBox_avcC( stream_t *p_stream, MP4_Box_t *p_box )
1873 MP4_Box_data_avcC_t *p_avcC;
1875 MP4_READBOX_ENTER( MP4_Box_data_avcC_t, MP4_FreeBox_avcC );
1876 p_avcC = p_box->data.p_avcC;
1878 if( i_read > 0 )
1880 p_avcC->p_avcC = malloc( i_read );
1881 if( p_avcC->p_avcC )
1883 memcpy( p_avcC->p_avcC, p_peek, i_read );
1884 p_avcC->i_avcC = i_read;
1888 MP4_GET1BYTE( p_avcC->i_version );
1889 MP4_GET1BYTE( p_avcC->i_profile );
1890 MP4_GET1BYTE( p_avcC->i_profile_compatibility );
1891 MP4_GET1BYTE( p_avcC->i_level );
1892 #ifdef MP4_VERBOSE
1893 msg_Dbg( p_stream,
1894 "read box: \"avcC\" version=%d profile=0x%x level=0x%x",
1895 p_avcC->i_version, p_avcC->i_profile, p_avcC->i_level );
1896 #endif
1897 MP4_READBOX_EXIT( 1 );
1900 static void MP4_FreeBox_vpcC( MP4_Box_t *p_box )
1902 free( p_box->data.p_vpcC->p_codec_init_data );
1905 static int MP4_ReadBox_vpcC( stream_t *p_stream, MP4_Box_t *p_box )
1907 MP4_READBOX_ENTER( MP4_Box_data_vpcC_t, MP4_FreeBox_vpcC );
1908 MP4_Box_data_vpcC_t *p_vpcC = p_box->data.p_vpcC;
1910 if( p_box->i_size < 6 )
1911 MP4_READBOX_EXIT( 0 );
1913 MP4_GET1BYTE( p_vpcC->i_version );
1914 if( p_vpcC->i_version > 1 )
1915 MP4_READBOX_EXIT( 0 );
1917 MP4_GET1BYTE( p_vpcC->i_profile );
1918 MP4_GET1BYTE( p_vpcC->i_level );
1919 MP4_GET1BYTE( p_vpcC->i_bit_depth );
1921 /* Deprecated one
1922 https://github.com/webmproject/vp9-dash/blob/master/archive/VPCodecISOMediaFileFormatBinding-v0.docx */
1923 if( p_vpcC->i_version == 0 )
1925 p_vpcC->i_color_primaries = p_vpcC->i_bit_depth & 0x0F;
1926 p_vpcC->i_bit_depth >>= 4;
1927 MP4_GET1BYTE( p_vpcC->i_chroma_subsampling );
1928 p_vpcC->i_xfer_function = ( p_vpcC->i_chroma_subsampling & 0x0F ) >> 1;
1929 p_vpcC->i_fullrange = p_vpcC->i_chroma_subsampling & 0x01;
1930 p_vpcC->i_chroma_subsampling >>= 4;
1932 else
1934 p_vpcC->i_chroma_subsampling = ( p_vpcC->i_bit_depth & 0x0F ) >> 1;
1935 p_vpcC->i_fullrange = p_vpcC->i_bit_depth & 0x01;
1936 p_vpcC->i_bit_depth >>= 4;
1937 MP4_GET1BYTE( p_vpcC->i_color_primaries );
1938 MP4_GET1BYTE( p_vpcC->i_xfer_function );
1939 MP4_GET1BYTE( p_vpcC->i_matrix_coeffs );
1942 MP4_GET2BYTES( p_vpcC->i_codec_init_datasize );
1943 if( p_vpcC->i_codec_init_datasize > i_read )
1944 p_vpcC->i_codec_init_datasize = i_read;
1946 if( p_vpcC->i_codec_init_datasize )
1948 p_vpcC->p_codec_init_data = malloc( i_read );
1949 if( !p_vpcC->p_codec_init_data )
1950 MP4_READBOX_EXIT( 0 );
1951 memcpy( p_vpcC->p_codec_init_data, p_peek, i_read );
1954 MP4_READBOX_EXIT( 1 );
1957 static int MP4_ReadBox_SmDm( stream_t *p_stream, MP4_Box_t *p_box )
1959 MP4_READBOX_ENTER( MP4_Box_data_SmDm_t, NULL );
1960 MP4_Box_data_SmDm_t *p_SmDm = p_box->data.p_SmDm;
1962 /* SmDm: version/flags RGB */
1963 /* mdcv: version/flags GBR or not */
1964 if( p_box->i_type != ATOM_mdcv )
1966 uint8_t i_version;
1967 uint32_t i_flags;
1968 MP4_GET1BYTE( i_version );
1969 MP4_GET3BYTES( i_flags );
1970 VLC_UNUSED(i_flags);
1971 if( i_version != 0 )
1972 MP4_READBOX_EXIT( 0 );
1975 const uint8_t RGB2GBR[3] = {2,0,1};
1976 for(int i=0; i<6; i++)
1978 int index = (p_box->i_type != ATOM_mdcv) ? RGB2GBR[i/2] + i%2 : i;
1979 MP4_GET2BYTES( p_SmDm->primaries[index] );
1981 /* convert from fixed point to 0.00002 resolution */
1982 if(p_box->i_type != ATOM_mdcv)
1983 p_SmDm->primaries[index] = 50000 *
1984 (double)p_SmDm->primaries[index] / (double)(1<<16);
1986 for(int i=0; i<2; i++)
1988 MP4_GET2BYTES( p_SmDm->white_point[i] );
1989 if(p_box->i_type != ATOM_mdcv)
1990 p_SmDm->white_point[i] = 50000 *
1991 (double)p_SmDm->white_point[i] / (double)(1<<16);
1994 MP4_GET4BYTES( p_SmDm->i_luminanceMax );
1995 MP4_GET4BYTES( p_SmDm->i_luminanceMin );
1996 if(p_box->i_type != ATOM_mdcv)
1998 p_SmDm->i_luminanceMax = 10000 *
1999 (double)p_SmDm->i_luminanceMax / (double) (1<<8);
2000 p_SmDm->i_luminanceMin = 10000 *
2001 (double)p_SmDm->i_luminanceMin / (double) (1<<14);
2004 MP4_READBOX_EXIT( 1 );
2007 static int MP4_ReadBox_CoLL( stream_t *p_stream, MP4_Box_t *p_box )
2009 MP4_READBOX_ENTER( MP4_Box_data_CoLL_t, NULL );
2010 MP4_Box_data_CoLL_t *p_CoLL = p_box->data.p_CoLL;
2012 if( p_box->i_type != ATOM_clli )
2014 uint8_t i_version;
2015 uint32_t i_flags;
2016 MP4_GET1BYTE( i_version );
2017 MP4_GET3BYTES( i_flags );
2018 VLC_UNUSED(i_flags);
2019 if( i_version != 0 )
2020 MP4_READBOX_EXIT( 0 );
2023 MP4_GET2BYTES( p_CoLL->i_maxCLL );
2024 MP4_GET2BYTES( p_CoLL->i_maxFALL );
2025 MP4_READBOX_EXIT( 1 );
2028 static void MP4_FreeBox_WMA2( MP4_Box_t *p_box )
2030 free( p_box->data.p_WMA2->p_extra );
2033 static int MP4_ReadBox_WMA2( stream_t *p_stream, MP4_Box_t *p_box )
2035 MP4_READBOX_ENTER( MP4_Box_data_WMA2_t, MP4_FreeBox_WMA2 );
2037 MP4_Box_data_WMA2_t *p_WMA2 = p_box->data.p_WMA2;
2039 MP4_GET2BYTESLE( p_WMA2->Format.wFormatTag );
2040 MP4_GET2BYTESLE( p_WMA2->Format.nChannels );
2041 MP4_GET4BYTESLE( p_WMA2->Format.nSamplesPerSec );
2042 MP4_GET4BYTESLE( p_WMA2->Format.nAvgBytesPerSec );
2043 MP4_GET2BYTESLE( p_WMA2->Format.nBlockAlign );
2044 MP4_GET2BYTESLE( p_WMA2->Format.wBitsPerSample );
2046 uint16_t i_cbSize;
2047 MP4_GET2BYTESLE( i_cbSize );
2049 if( i_cbSize > i_read )
2050 goto error;
2052 p_WMA2->i_extra = i_cbSize;
2053 if ( p_WMA2->i_extra )
2055 p_WMA2->p_extra = malloc( p_WMA2->i_extra );
2056 if ( ! p_WMA2->p_extra )
2057 goto error;
2058 memcpy( p_WMA2->p_extra, p_peek, p_WMA2->i_extra );
2061 MP4_READBOX_EXIT( 1 );
2063 error:
2064 MP4_READBOX_EXIT( 0 );
2067 static void MP4_FreeBox_strf( MP4_Box_t *p_box )
2069 free( p_box->data.p_strf->p_extra );
2072 static int MP4_ReadBox_strf( stream_t *p_stream, MP4_Box_t *p_box )
2074 MP4_READBOX_ENTER( MP4_Box_data_strf_t, MP4_FreeBox_strf );
2076 MP4_Box_data_strf_t *p_strf = p_box->data.p_strf;
2078 if( i_read < 40 )
2079 goto error;
2081 MP4_GET4BYTESLE( p_strf->bmiHeader.biSize );
2082 MP4_GET4BYTESLE( p_strf->bmiHeader.biWidth );
2083 MP4_GET4BYTESLE( p_strf->bmiHeader.biHeight );
2084 MP4_GET2BYTESLE( p_strf->bmiHeader.biPlanes );
2085 MP4_GET2BYTESLE( p_strf->bmiHeader.biBitCount );
2086 MP4_GETFOURCC( p_strf->bmiHeader.biCompression );
2087 MP4_GET4BYTESLE( p_strf->bmiHeader.biSizeImage );
2088 MP4_GET4BYTESLE( p_strf->bmiHeader.biXPelsPerMeter );
2089 MP4_GET4BYTESLE( p_strf->bmiHeader.biYPelsPerMeter );
2090 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrUsed );
2091 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrImportant );
2093 p_strf->i_extra = i_read;
2094 if ( p_strf->i_extra )
2096 p_strf->p_extra = malloc( p_strf->i_extra );
2097 if ( ! p_strf->p_extra )
2098 goto error;
2099 memcpy( p_strf->p_extra, p_peek, i_read );
2102 MP4_READBOX_EXIT( 1 );
2104 error:
2105 MP4_READBOX_EXIT( 0 );
2108 static int MP4_ReadBox_ASF( stream_t *p_stream, MP4_Box_t *p_box )
2110 MP4_READBOX_ENTER( MP4_Box_data_ASF_t, NULL );
2112 MP4_Box_data_ASF_t *p_asf = p_box->data.p_asf;
2114 if (i_read != 8)
2115 MP4_READBOX_EXIT( 0 );
2117 MP4_GET1BYTE( p_asf->i_stream_number );
2118 /* remaining is unknown */
2120 MP4_READBOX_EXIT( 1 );
2123 static void MP4_FreeBox_sbgp( MP4_Box_t *p_box )
2125 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2126 free( p_sbgp->entries.pi_sample_count );
2127 free( p_sbgp->entries.pi_group_description_index );
2130 static int MP4_ReadBox_sbgp( stream_t *p_stream, MP4_Box_t *p_box )
2132 MP4_READBOX_ENTER( MP4_Box_data_sbgp_t, MP4_FreeBox_sbgp );
2133 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2134 uint32_t i_flags;
2136 if ( i_read < 12 )
2137 MP4_READBOX_EXIT( 0 );
2139 MP4_GET1BYTE( p_sbgp->i_version );
2140 MP4_GET3BYTES( i_flags );
2141 if( i_flags != 0 )
2142 MP4_READBOX_EXIT( 0 );
2144 MP4_GETFOURCC( p_sbgp->i_grouping_type );
2146 if( p_sbgp->i_version == 1 )
2148 if( i_read < 8 )
2149 MP4_READBOX_EXIT( 0 );
2150 MP4_GET4BYTES( p_sbgp->i_grouping_type_parameter );
2153 MP4_GET4BYTES( p_sbgp->i_entry_count );
2154 if( p_sbgp->i_entry_count > i_read / (4 + 4) )
2155 p_sbgp->i_entry_count = i_read / (4 + 4);
2157 p_sbgp->entries.pi_sample_count = vlc_alloc( p_sbgp->i_entry_count, sizeof(uint32_t) );
2158 p_sbgp->entries.pi_group_description_index = vlc_alloc( p_sbgp->i_entry_count, sizeof(uint32_t) );
2160 if( !p_sbgp->entries.pi_sample_count || !p_sbgp->entries.pi_group_description_index )
2162 MP4_FreeBox_sbgp( p_box );
2163 MP4_READBOX_EXIT( 0 );
2166 for( uint32_t i=0; i<p_sbgp->i_entry_count; i++ )
2168 MP4_GET4BYTES( p_sbgp->entries.pi_sample_count[i] );
2169 MP4_GET4BYTES( p_sbgp->entries.pi_group_description_index[i] );
2172 #ifdef MP4_VERBOSE
2173 msg_Dbg( p_stream,
2174 "read box: \"sbgp\" grouping type %4.4s", (char*) &p_sbgp->i_grouping_type );
2175 #ifdef MP4_ULTRA_VERBOSE
2176 for (uint32_t i = 0; i < p_sbgp->i_entry_count; i++)
2177 msg_Dbg( p_stream, "\t samples %" PRIu32 " group %" PRIu32,
2178 p_sbgp->entries.pi_sample_count[i],
2179 p_sbgp->entries.pi_group_description_index[i] );
2180 #endif
2181 #endif
2183 MP4_READBOX_EXIT( 1 );
2186 static void MP4_FreeBox_sgpd( MP4_Box_t *p_box )
2188 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2189 free( p_sgpd->p_entries );
2192 static int MP4_ReadBox_sgpd( stream_t *p_stream, MP4_Box_t *p_box )
2194 MP4_READBOX_ENTER( MP4_Box_data_sgpd_t, MP4_FreeBox_sgpd );
2195 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2196 uint32_t i_flags;
2197 uint32_t i_default_length = 0;
2199 if ( i_read < 8 )
2200 MP4_READBOX_EXIT( 0 );
2202 MP4_GET1BYTE( p_sgpd->i_version );
2203 MP4_GET3BYTES( i_flags );
2204 if( i_flags != 0 )
2205 MP4_READBOX_EXIT( 0 );
2207 MP4_GETFOURCC( p_sgpd->i_grouping_type );
2209 switch( p_sgpd->i_grouping_type )
2211 case SAMPLEGROUP_rap:
2212 break;
2214 default:
2215 #ifdef MP4_VERBOSE
2216 msg_Dbg( p_stream,
2217 "read box: \"sgpd\" grouping type %4.4s (unimplemented)", (char*) &p_sgpd->i_grouping_type );
2218 #endif
2219 MP4_READBOX_EXIT( 1 );
2222 if( p_sgpd->i_version == 1 )
2224 if( i_read < 8 )
2225 MP4_READBOX_EXIT( 0 );
2226 MP4_GET4BYTES( i_default_length );
2228 else if( p_sgpd->i_version >= 2 )
2230 if( i_read < 8 )
2231 MP4_READBOX_EXIT( 0 );
2232 MP4_GET4BYTES( p_sgpd->i_default_sample_description_index );
2235 MP4_GET4BYTES( p_sgpd->i_entry_count );
2237 p_sgpd->p_entries = vlc_alloc( p_sgpd->i_entry_count, sizeof(*p_sgpd->p_entries) );
2238 if( !p_sgpd->p_entries )
2239 MP4_READBOX_EXIT( 0 );
2241 uint32_t i = 0;
2242 for( ; i<p_sgpd->i_entry_count; i++ )
2244 uint32_t i_description_length = i_default_length;
2245 if( p_sgpd->i_version == 1 && i_default_length == 0 )
2247 if( i_read < 4 )
2248 break;
2249 MP4_GET4BYTES( i_description_length );
2252 if( p_sgpd->i_version == 1 && i_read < i_description_length )
2253 break;
2255 switch( p_sgpd->i_grouping_type )
2257 case SAMPLEGROUP_rap:
2259 if( i_read < 1 )
2261 p_sgpd->i_entry_count = 0;
2262 MP4_FreeBox_sgpd( p_box );
2263 MP4_READBOX_EXIT( 0 );
2265 uint8_t i_data;
2266 MP4_GET1BYTE( i_data );
2267 p_sgpd->p_entries[i].rap.i_num_leading_samples_known = i_data & 0x80;
2268 p_sgpd->p_entries[i].rap.i_num_leading_samples = i_data & 0x7F;
2270 break;
2272 default:
2273 assert(0);
2277 if( i != p_sgpd->i_entry_count )
2278 p_sgpd->i_entry_count = i;
2280 #ifdef MP4_VERBOSE
2281 msg_Dbg( p_stream,
2282 "read box: \"sgpd\" grouping type %4.4s", (char*) &p_sgpd->i_grouping_type );
2283 #endif
2285 MP4_READBOX_EXIT( 1 );
2288 static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
2290 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2291 free( p_chan->layout.p_descriptions );
2294 static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
2296 MP4_READBOX_ENTER( MP4_Box_data_chan_t, MP4_FreeBox_stsdext_chan );
2297 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2299 if ( i_read < 16 )
2300 MP4_READBOX_EXIT( 0 );
2302 MP4_GET1BYTE( p_chan->i_version );
2303 MP4_GET3BYTES( p_chan->i_channels_flags );
2304 MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
2305 MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
2306 MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
2308 size_t i_descsize = 8 + 3 * sizeof(float);
2309 if ( i_read < p_chan->layout.i_channels_description_count * i_descsize )
2310 MP4_READBOX_EXIT( 0 );
2312 p_chan->layout.p_descriptions =
2313 vlc_alloc( p_chan->layout.i_channels_description_count, i_descsize );
2315 if ( !p_chan->layout.p_descriptions )
2316 MP4_READBOX_EXIT( 0 );
2318 uint32_t i;
2319 for( i=0; i<p_chan->layout.i_channels_description_count; i++ )
2321 if ( i_read < 20 )
2322 break;
2323 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
2324 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
2325 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
2326 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
2327 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
2329 if ( i<p_chan->layout.i_channels_description_count )
2330 p_chan->layout.i_channels_description_count = i;
2332 #ifdef MP4_VERBOSE
2333 msg_Dbg( p_stream,
2334 "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
2335 p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
2336 p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
2337 #endif
2338 MP4_READBOX_EXIT( 1 );
2341 static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
2343 MP4_READBOX_ENTER( MP4_Box_data_dec3_t, NULL );
2345 MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
2347 unsigned i_header;
2348 MP4_GET2BYTES( i_header );
2350 p_dec3->i_data_rate = i_header >> 3;
2351 p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
2352 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
2353 MP4_GET3BYTES( i_header );
2354 p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
2355 p_dec3->stream[i].i_bsid = ( i_header >> 17 ) & 0x01f;
2356 p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
2357 p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
2358 p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
2359 p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
2360 if (p_dec3->stream[i].i_num_dep_sub) {
2361 MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
2362 p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
2363 } else
2364 p_dec3->stream[i].i_chan_loc = 0;
2367 #ifdef MP4_VERBOSE
2368 msg_Dbg( p_stream,
2369 "read box: \"dec3\" bitrate %dkbps %d independent substreams",
2370 p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
2372 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
2373 msg_Dbg( p_stream,
2374 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
2375 "num dependent subs=%d chan_loc=0x%x",
2376 i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
2377 p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
2378 #endif
2379 MP4_READBOX_EXIT( 1 );
2382 static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
2384 MP4_Box_data_dac3_t *p_dac3;
2385 MP4_READBOX_ENTER( MP4_Box_data_dac3_t, NULL );
2387 p_dac3 = p_box->data.p_dac3;
2389 unsigned i_header;
2390 MP4_GET3BYTES( i_header );
2392 p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
2393 p_dac3->i_bsid = ( i_header >> 17 ) & 0x01f;
2394 p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
2395 p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
2396 p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
2397 p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
2399 #ifdef MP4_VERBOSE
2400 msg_Dbg( p_stream,
2401 "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
2402 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 );
2403 #endif
2404 MP4_READBOX_EXIT( 1 );
2407 static void MP4_FreeBox_dvc1( MP4_Box_t *p_box )
2409 free( p_box->data.p_dvc1->p_vc1 );
2412 static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
2414 MP4_READBOX_ENTER( MP4_Box_data_dvc1_t, MP4_FreeBox_dvc1 );
2415 if( i_read < 7 )
2416 MP4_READBOX_EXIT( 0 );
2418 MP4_Box_data_dvc1_t *p_dvc1 = p_box->data.p_dvc1;
2419 MP4_GET1BYTE( p_dvc1->i_profile_level );
2420 p_dvc1->i_vc1 = i_read; /* Header + profile_level */
2421 if( p_dvc1->i_vc1 > 0 && (p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 )) )
2422 memcpy( p_dvc1->p_vc1, p_peek, i_read );
2424 #ifdef MP4_VERBOSE
2425 msg_Dbg( p_stream,
2426 "read box: \"dvc1\" profile=%"PRIu8, (p_dvc1->i_profile_level & 0xf0) >> 4 );
2427 #endif
2429 MP4_READBOX_EXIT( 1 );
2432 static int MP4_ReadBox_fiel( stream_t *p_stream, MP4_Box_t *p_box )
2434 MP4_Box_data_fiel_t *p_fiel;
2435 MP4_READBOX_ENTER( MP4_Box_data_fiel_t, NULL );
2436 p_fiel = p_box->data.p_fiel;
2437 if(i_read < 2)
2438 MP4_READBOX_EXIT( 0 );
2439 if(p_peek[0] == 1)
2441 p_fiel->i_flags = BLOCK_FLAG_SINGLE_FIELD;
2443 else if(p_peek[0] == 2) /* Interlaced */
2446 * 0 – There is only one field.
2447 * 1 – T is displayed earliest, T is stored first in the file.
2448 * 6 – B is displayed earliest, B is stored first in the file.
2449 * 9 – B is displayed earliest, T is stored first in the file.
2450 * 14 – T is displayed earliest, B is stored first in the file.
2452 if(p_peek[1] == 1 || p_peek[1] == 9)
2453 p_fiel->i_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
2454 else if(p_peek[1] == 6 || p_peek[1] == 14)
2455 p_fiel->i_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
2457 MP4_READBOX_EXIT( 1 );
2460 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
2462 MP4_Box_data_enda_t *p_enda;
2463 MP4_READBOX_ENTER( MP4_Box_data_enda_t, NULL );
2465 p_enda = p_box->data.p_enda;
2467 MP4_GET2BYTES( p_enda->i_little_endian );
2469 #ifdef MP4_VERBOSE
2470 msg_Dbg( p_stream,
2471 "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
2472 #endif
2473 MP4_READBOX_EXIT( 1 );
2476 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
2478 free( p_box->data.p_sample_soun->p_qt_description );
2481 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
2483 p_box->i_handler = ATOM_soun;
2484 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t, MP4_FreeBox_sample_soun );
2485 p_box->data.p_sample_soun->p_qt_description = NULL;
2487 size_t i_actually_read = i_read + header_size;
2489 /* Sanity check needed because the "wave" box does also contain an
2490 * "mp4a" box that we don't understand. */
2491 if( i_read < 28 )
2493 MP4_READBOX_EXIT( 1 );
2496 for( unsigned i = 0; i < 6 ; i++ )
2498 MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
2501 MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
2504 * XXX hack -> produce a copy of the nearly complete chunk
2506 p_box->data.p_sample_soun->i_qt_description = 0;
2507 p_box->data.p_sample_soun->p_qt_description = NULL;
2508 if( i_read > 0 )
2510 p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
2511 if( p_box->data.p_sample_soun->p_qt_description )
2513 p_box->data.p_sample_soun->i_qt_description = i_read;
2514 memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
2518 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
2519 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
2520 MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
2522 MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
2523 MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
2524 MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
2525 MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
2526 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
2527 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
2529 #ifdef MP4_VERBOSE
2530 msg_Dbg( p_stream,
2531 "read box: \"soun\" stsd qt_version %"PRIu16" compid=%"PRIx16,
2532 p_box->data.p_sample_soun->i_qt_version,
2533 p_box->data.p_sample_soun->i_compressionid );
2534 #endif
2535 /* @36 bytes */
2536 if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
2538 /* SoundDescriptionV1 */
2539 MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
2540 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
2541 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
2542 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
2544 #ifdef MP4_VERBOSE
2545 msg_Dbg( p_stream,
2546 "read box: \"soun\" V1 sample/packet=%d bytes/packet=%d "
2547 "bytes/frame=%d bytes/sample=%d",
2548 p_box->data.p_sample_soun->i_sample_per_packet,
2549 p_box->data.p_sample_soun->i_bytes_per_packet,
2550 p_box->data.p_sample_soun->i_bytes_per_frame,
2551 p_box->data.p_sample_soun->i_bytes_per_sample );
2552 #endif
2553 /* @52 bytes */
2555 else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
2557 /* SoundDescriptionV2 */
2558 double f_sample_rate;
2559 int64_t i_dummy64;
2560 uint32_t i_channel, i_extoffset, i_dummy32;
2562 /* Checks */
2563 if ( p_box->data.p_sample_soun->i_channelcount != 0x3 ||
2564 p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
2565 p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
2566 p_box->data.p_sample_soun->i_reserved3 != 0x0 ||
2567 p_box->data.p_sample_soun->i_sampleratehi != 0x1 ||//65536
2568 p_box->data.p_sample_soun->i_sampleratelo != 0x0 ) //remainder
2570 msg_Err( p_stream, "invalid stsd V2 box defaults" );
2571 MP4_READBOX_EXIT( 0 );
2573 /* !Checks */
2575 MP4_GET4BYTES( i_extoffset ); /* offset to stsd extentions */
2576 MP4_GET8BYTES( i_dummy64 );
2577 memcpy( &f_sample_rate, &i_dummy64, 8 );
2578 msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
2579 /* Rounding error with lo, but we don't care since we do not support fractional audio rate */
2580 p_box->data.p_sample_soun->i_sampleratehi = (uint32_t)f_sample_rate;
2581 p_box->data.p_sample_soun->i_sampleratelo = (f_sample_rate - p_box->data.p_sample_soun->i_sampleratehi);
2583 MP4_GET4BYTES( i_channel );
2584 p_box->data.p_sample_soun->i_channelcount = i_channel;
2586 MP4_GET4BYTES( i_dummy32 );
2587 if ( i_dummy32 != 0x7F000000 )
2589 msg_Err( p_stream, "invalid stsd V2 box" );
2590 MP4_READBOX_EXIT( 0 );
2593 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
2594 MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
2595 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
2596 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2598 #ifdef MP4_VERBOSE
2599 msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
2600 "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
2601 f_sample_rate,
2602 p_box->data.p_sample_soun->i_constbitsperchannel,
2603 p_box->data.p_sample_soun->i_formatflags,
2604 p_box->data.p_sample_soun->i_constbytesperaudiopacket,
2605 p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2606 #endif
2607 /* @72 bytes + */
2608 if( i_extoffset > i_actually_read )
2609 i_extoffset = i_actually_read;
2610 p_peek = &p_buff[i_extoffset];
2611 i_read = i_actually_read - i_extoffset;
2613 else
2615 p_box->data.p_sample_soun->i_sample_per_packet = 0;
2616 p_box->data.p_sample_soun->i_bytes_per_packet = 0;
2617 p_box->data.p_sample_soun->i_bytes_per_frame = 0;
2618 p_box->data.p_sample_soun->i_bytes_per_sample = 0;
2620 #ifdef MP4_VERBOSE
2621 msg_Dbg( p_stream, "read box: \"soun\" V0 or qt1/2 (rest=%"PRIu64")",
2622 i_read );
2623 #endif
2624 /* @36 bytes */
2627 if( p_box->i_type == ATOM_drms )
2629 msg_Warn( p_stream, "DRM protected streams are not supported." );
2630 MP4_READBOX_EXIT( 0 );
2633 if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
2635 /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
2636 p_box->data.p_sample_soun->i_channelcount = 1;
2639 /* Loads extensions */
2640 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2641 p_box->i_pos + p_peek - p_buff ); /* esds/wave/... */
2643 #ifdef MP4_VERBOSE
2644 msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
2645 "sample size %d sample rate %f",
2646 p_box->data.p_sample_soun->i_channelcount,
2647 p_box->data.p_sample_soun->i_samplesize,
2648 (float)p_box->data.p_sample_soun->i_sampleratehi +
2649 (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
2651 #endif
2652 MP4_READBOX_EXIT( 1 );
2655 static void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
2657 free( p_box->data.p_sample_vide->p_qt_image_description );
2660 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
2662 p_box->i_handler = ATOM_vide;
2663 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t, MP4_FreeBox_sample_vide );
2665 size_t i_actually_read = i_read + header_size;
2667 for( unsigned i = 0; i < 6 ; i++ )
2669 MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
2672 MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
2675 * XXX hack -> produce a copy of the nearly complete chunk
2677 if( i_read > 0 )
2679 p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
2680 if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
2681 MP4_READBOX_EXIT( 0 );
2682 p_box->data.p_sample_vide->i_qt_image_description = i_read;
2683 memcpy( p_box->data.p_sample_vide->p_qt_image_description,
2684 p_peek, i_read );
2686 else
2688 p_box->data.p_sample_vide->i_qt_image_description = 0;
2689 p_box->data.p_sample_vide->p_qt_image_description = NULL;
2692 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
2693 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
2694 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
2696 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
2697 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
2699 MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
2700 MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
2702 MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
2703 MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
2705 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
2706 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
2708 if ( i_read < 32 )
2709 MP4_READBOX_EXIT( 0 );
2710 if( p_peek[0] <= 31 ) // Must be Pascal String
2712 memcpy( &p_box->data.p_sample_vide->sz_compressorname, &p_peek[1], p_peek[0] );
2713 p_box->data.p_sample_vide->sz_compressorname[p_peek[0]] = 0;
2715 p_peek += 32; i_read -= 32;
2717 MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
2718 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
2720 if( p_box->i_type == ATOM_drmi )
2722 msg_Warn( p_stream, "DRM protected streams are not supported." );
2723 MP4_READBOX_EXIT( 0 );
2726 if( i_actually_read > 78 && p_peek - p_buff > 78 )
2728 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2729 p_box->i_pos + p_peek - p_buff );
2732 #ifdef MP4_VERBOSE
2733 msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d (%s)",
2734 p_box->data.p_sample_vide->i_width,
2735 p_box->data.p_sample_vide->i_height,
2736 p_box->data.p_sample_vide->i_depth,
2737 p_box->data.p_sample_vide->sz_compressorname );
2739 #endif
2740 MP4_READBOX_EXIT( 1 );
2743 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
2745 p_box->i_handler = ATOM_text;
2746 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_text_t, 16, NULL );
2747 (void) p_peek;
2748 if( i_read < 8 )
2749 MP4_READBOX_EXIT( 0 );
2751 MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
2753 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2754 MP4_READBOX_EXIT( 0 );
2756 MP4_READBOX_EXIT( 1 );
2759 static void MP4_FreeBox_sample_hint( MP4_Box_t *p_box )
2761 free( p_box->data.p_sample_hint->p_data );
2764 static int MP4_ReadBox_sample_hint8( stream_t *p_stream, MP4_Box_t *p_box )
2766 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_hint_t, 24, MP4_FreeBox_sample_hint );
2768 for( unsigned i = 0; i < 6 ; i++ )
2770 MP4_GET1BYTE( p_box->data.p_sample_hint->i_reserved1[i] );
2773 MP4_GET2BYTES( p_box->data.p_sample_hint->i_data_reference_index );
2775 if( !(p_box->data.p_sample_hint->p_data = malloc(8)) )
2776 MP4_READBOX_EXIT( 0 );
2778 MP4_GET8BYTES( *(p_box->data.p_sample_hint->p_data) );
2780 MP4_ReadBoxContainerChildren(p_stream, p_box, NULL);
2782 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2783 MP4_READBOX_EXIT( 0 );
2785 MP4_READBOX_EXIT( 1 );
2788 static void MP4_FreeBox_sample_text( MP4_Box_t *p_box )
2790 free( p_box->data.p_sample_text->p_data );
2793 static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
2795 p_box->i_handler = ATOM_text;
2796 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t, MP4_FreeBox_sample_text );
2798 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2799 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2801 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2803 if( i_read )
2805 p_box->data.p_sample_text->p_data = malloc( i_read );
2806 if( !p_box->data.p_sample_text->p_data )
2807 MP4_READBOX_EXIT( 0 );
2808 memcpy( p_box->data.p_sample_text->p_data, p_peek, i_read );
2810 p_box->data.p_sample_text->i_data = i_read;
2812 #ifdef MP4_VERBOSE
2813 msg_Dbg( p_stream, "read box: \"text\" in stsd text" );
2814 #endif
2815 MP4_READBOX_EXIT( 1 );
2818 static int MP4_ReadBox_sample_clcp( stream_t *p_stream, MP4_Box_t *p_box )
2820 p_box->i_handler = ATOM_clcp;
2821 MP4_READBOX_ENTER( MP4_Box_data_sample_clcp_t, NULL );
2823 if( i_read < 8 )
2824 MP4_READBOX_EXIT( 0 );
2826 for( int i=0; i<6; i++ )
2827 MP4_GET1BYTE( p_box->data.p_sample_clcp->i_reserved1[i] );
2828 MP4_GET2BYTES( p_box->data.p_sample_clcp->i_data_reference_index );
2830 #ifdef MP4_VERBOSE
2831 msg_Dbg( p_stream, "read box: \"clcp\" in stsd" );
2832 #endif
2833 MP4_READBOX_EXIT( 1 );
2836 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
2838 free( p_box->data.p_stsz->i_entry_size );
2841 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
2843 uint32_t count;
2845 MP4_READBOX_ENTER( MP4_Box_data_stsz_t, MP4_FreeBox_stsz );
2847 MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
2849 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
2850 MP4_GET4BYTES( count );
2851 p_box->data.p_stsz->i_sample_count = count;
2853 if( p_box->data.p_stsz->i_sample_size == 0 )
2855 if( UINT64_C(4) * count > i_read )
2856 MP4_READBOX_EXIT( 0 );
2858 p_box->data.p_stsz->i_entry_size =
2859 vlc_alloc( count, sizeof(uint32_t) );
2860 if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
2861 MP4_READBOX_EXIT( 0 );
2863 for( uint32_t i = 0; i < count; i++ )
2865 MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
2868 else
2869 p_box->data.p_stsz->i_entry_size = NULL;
2871 #ifdef MP4_VERBOSE
2872 msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
2873 p_box->data.p_stsz->i_sample_size,
2874 p_box->data.p_stsz->i_sample_count );
2876 #endif
2877 MP4_READBOX_EXIT( 1 );
2880 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
2882 free( p_box->data.p_stsc->i_first_chunk );
2883 free( p_box->data.p_stsc->i_samples_per_chunk );
2884 free( p_box->data.p_stsc->i_sample_description_index );
2887 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
2889 uint32_t count;
2891 MP4_READBOX_ENTER( MP4_Box_data_stsc_t, MP4_FreeBox_stsc );
2893 MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
2894 MP4_GET4BYTES( count );
2896 if( UINT64_C(12) * count > i_read )
2897 MP4_READBOX_EXIT( 0 );
2899 p_box->data.p_stsc->i_first_chunk = vlc_alloc( count, sizeof(uint32_t) );
2900 p_box->data.p_stsc->i_samples_per_chunk = vlc_alloc( count,
2901 sizeof(uint32_t) );
2902 p_box->data.p_stsc->i_sample_description_index = vlc_alloc( count,
2903 sizeof(uint32_t) );
2904 if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
2905 || p_box->data.p_stsc->i_samples_per_chunk == NULL
2906 || p_box->data.p_stsc->i_sample_description_index == NULL ) )
2908 MP4_READBOX_EXIT( 0 );
2910 p_box->data.p_stsc->i_entry_count = count;
2912 for( uint32_t i = 0; i < count;i++ )
2914 MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
2915 MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
2916 MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
2919 #ifdef MP4_VERBOSE
2920 msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
2921 p_box->data.p_stsc->i_entry_count );
2923 #endif
2924 MP4_READBOX_EXIT( 1 );
2927 static void MP4_FreeBox_sdp( MP4_Box_t *p_box )
2929 free( p_box->data.p_sdp->psz_text );
2932 static int MP4_ReadBox_sdp( stream_t *p_stream, MP4_Box_t *p_box )
2934 MP4_READBOX_ENTER( MP4_Box_data_sdp_t, MP4_FreeBox_sdp );
2936 MP4_GETSTRINGZ( p_box->data.p_sdp->psz_text );
2938 MP4_READBOX_EXIT( 1 );
2941 static void MP4_FreeBox_rtp( MP4_Box_t *p_box )
2943 free( p_box->data.p_moviehintinformation_rtp->psz_text );
2946 static int MP4_ReadBox_rtp( stream_t *p_stream, MP4_Box_t *p_box )
2948 MP4_READBOX_ENTER( MP4_Box_data_moviehintinformation_rtp_t, MP4_FreeBox_rtp );
2950 MP4_GET4BYTES( p_box->data.p_moviehintinformation_rtp->i_description_format );
2952 MP4_GETSTRINGZ( p_box->data.p_moviehintinformation_rtp->psz_text );
2954 MP4_READBOX_EXIT( 1 );
2957 static int MP4_ReadBox_tims( stream_t *p_stream, MP4_Box_t *p_box )
2959 MP4_READBOX_ENTER( MP4_Box_data_tims_t, NULL );
2961 MP4_GET4BYTES( p_box->data.p_tims->i_timescale );
2963 MP4_READBOX_EXIT( 1 );
2966 static int MP4_ReadBox_tsro( stream_t *p_stream, MP4_Box_t *p_box )
2968 MP4_READBOX_ENTER( MP4_Box_data_tsro_t, NULL );
2970 MP4_GET4BYTES( p_box->data.p_tsro->i_offset );
2972 MP4_READBOX_EXIT( 1 );
2975 static int MP4_ReadBox_tssy( stream_t *p_stream, MP4_Box_t *p_box )
2977 MP4_READBOX_ENTER( MP4_Box_data_tssy_t, NULL );
2979 MP4_GET1BYTE( p_box->data.p_tssy->i_reserved_timestamp_sync );
2981 MP4_READBOX_EXIT( 1 );
2984 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
2986 free( p_box->data.p_co64->i_chunk_offset );
2989 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
2991 const bool sixtyfour = p_box->i_type != ATOM_stco;
2992 uint32_t count;
2994 MP4_READBOX_ENTER( MP4_Box_data_co64_t, MP4_FreeBox_stco_co64 );
2996 MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
2997 MP4_GET4BYTES( count );
2999 if( (sixtyfour ? UINT64_C(8) : UINT64_C(4)) * count > i_read )
3000 MP4_READBOX_EXIT( 0 );
3002 p_box->data.p_co64->i_chunk_offset = vlc_alloc( count, sizeof(uint64_t) );
3003 if( unlikely(p_box->data.p_co64->i_chunk_offset == NULL) )
3004 MP4_READBOX_EXIT( 0 );
3005 p_box->data.p_co64->i_entry_count = count;
3007 for( uint32_t i = 0; i < count; i++ )
3009 if( sixtyfour )
3010 MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
3011 else
3012 MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
3015 #ifdef MP4_VERBOSE
3016 msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
3017 p_box->data.p_co64->i_entry_count );
3019 #endif
3020 MP4_READBOX_EXIT( 1 );
3023 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
3025 free( p_box->data.p_stss->i_sample_number );
3028 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
3030 uint32_t count;
3032 MP4_READBOX_ENTER( MP4_Box_data_stss_t, MP4_FreeBox_stss );
3034 MP4_GETVERSIONFLAGS( p_box->data.p_stss );
3035 MP4_GET4BYTES( count );
3037 if( UINT64_C(4) * count > i_read )
3038 MP4_READBOX_EXIT( 0 );
3040 p_box->data.p_stss->i_sample_number = vlc_alloc( count, sizeof(uint32_t) );
3041 if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
3042 MP4_READBOX_EXIT( 0 );
3043 p_box->data.p_stss->i_entry_count = count;
3045 for( uint32_t i = 0; i < count; i++ )
3047 MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
3048 /* XXX in libmp4 sample begin at 0 */
3049 p_box->data.p_stss->i_sample_number[i]--;
3052 #ifdef MP4_VERBOSE
3053 msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
3054 p_box->data.p_stss->i_entry_count );
3056 #endif
3057 MP4_READBOX_EXIT( 1 );
3060 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
3062 free( p_box->data.p_stsh->i_shadowed_sample_number );
3063 free( p_box->data.p_stsh->i_sync_sample_number );
3066 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
3068 uint32_t count;
3070 MP4_READBOX_ENTER( MP4_Box_data_stsh_t, MP4_FreeBox_stsh );
3072 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
3073 MP4_GET4BYTES( count );
3075 if( UINT64_C(8) * count > i_read )
3076 MP4_READBOX_EXIT( 0 );
3078 p_box->data.p_stsh->i_shadowed_sample_number = vlc_alloc( count,
3079 sizeof(uint32_t) );
3080 p_box->data.p_stsh->i_sync_sample_number = vlc_alloc( count,
3081 sizeof(uint32_t) );
3082 if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
3083 || p_box->data.p_stsh->i_sync_sample_number == NULL )
3084 MP4_READBOX_EXIT( 0 );
3085 p_box->data.p_stsh->i_entry_count = count;
3087 for( uint32_t i = 0; i < p_box->data.p_stss->i_entry_count; i++ )
3089 MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
3090 MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
3093 #ifdef MP4_VERBOSE
3094 msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
3095 p_box->data.p_stsh->i_entry_count );
3096 #endif
3097 MP4_READBOX_EXIT( 1 );
3100 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
3102 free( p_box->data.p_stdp->i_priority );
3105 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
3107 MP4_READBOX_ENTER( MP4_Box_data_stdp_t, MP4_FreeBox_stdp );
3109 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
3111 p_box->data.p_stdp->i_priority =
3112 calloc( i_read / 2, sizeof(uint16_t) );
3114 if( unlikely( !p_box->data.p_stdp->i_priority ) )
3115 MP4_READBOX_EXIT( 0 );
3117 for( unsigned i = 0; i < i_read / 2 ; i++ )
3119 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
3122 #ifdef MP4_VERBOSE
3123 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
3124 i_read / 2 );
3126 #endif
3127 MP4_READBOX_EXIT( 1 );
3130 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
3132 free( p_box->data.p_elst->i_segment_duration );
3133 free( p_box->data.p_elst->i_media_time );
3134 free( p_box->data.p_elst->i_media_rate_integer );
3135 free( p_box->data.p_elst->i_media_rate_fraction );
3138 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
3140 uint32_t count;
3142 MP4_READBOX_ENTER( MP4_Box_data_elst_t, MP4_FreeBox_elst );
3144 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
3145 MP4_GET4BYTES( count );
3147 if( count == 0 )
3148 MP4_READBOX_EXIT( 1 );
3150 uint32_t i_entries_max = i_read / ((p_box->data.p_elst->i_version == 1) ? 20 : 12);
3151 if( count > i_entries_max )
3152 count = i_entries_max;
3154 p_box->data.p_elst->i_segment_duration = vlc_alloc( count,
3155 sizeof(uint64_t) );
3156 p_box->data.p_elst->i_media_time = vlc_alloc( count, sizeof(int64_t) );
3157 p_box->data.p_elst->i_media_rate_integer = vlc_alloc( count,
3158 sizeof(uint16_t) );
3159 p_box->data.p_elst->i_media_rate_fraction = vlc_alloc( count,
3160 sizeof(uint16_t) );
3161 if( p_box->data.p_elst->i_segment_duration == NULL
3162 || p_box->data.p_elst->i_media_time == NULL
3163 || p_box->data.p_elst->i_media_rate_integer == NULL
3164 || p_box->data.p_elst->i_media_rate_fraction == NULL )
3166 MP4_READBOX_EXIT( 0 );
3168 p_box->data.p_elst->i_entry_count = count;
3170 for( uint32_t i = 0; i < count; i++ )
3172 uint64_t segment_duration;
3173 int64_t media_time;
3175 if( p_box->data.p_elst->i_version == 1 )
3177 union { int64_t s; uint64_t u; } u;
3179 MP4_GET8BYTES( segment_duration );
3180 MP4_GET8BYTES( u.u );
3181 media_time = u.s;
3183 else
3185 union { int32_t s; uint32_t u; } u;
3187 MP4_GET4BYTES( segment_duration );
3188 MP4_GET4BYTES( u.u );
3189 media_time = u.s;
3192 p_box->data.p_elst->i_segment_duration[i] = segment_duration;
3193 p_box->data.p_elst->i_media_time[i] = media_time;
3194 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
3195 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
3198 #ifdef MP4_VERBOSE
3199 msg_Dbg( p_stream, "read box: \"elst\" entry-count %" PRIu32,
3200 p_box->data.p_elst->i_entry_count );
3201 #endif
3202 MP4_READBOX_EXIT( 1 );
3205 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
3207 free( p_box->data.p_cprt->psz_notice );
3210 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
3212 uint16_t i_language;
3213 bool b_mac;
3215 MP4_READBOX_ENTER( MP4_Box_data_cprt_t, MP4_FreeBox_cprt );
3217 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
3219 MP4_GET2BYTES( i_language );
3220 decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
3222 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
3224 #ifdef MP4_VERBOSE
3225 msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
3226 p_box->data.p_cprt->rgs_language,
3227 p_box->data.p_cprt->psz_notice );
3229 #endif
3230 MP4_READBOX_EXIT( 1 );
3233 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
3235 MP4_READBOX_ENTER( MP4_Box_data_dcom_t, NULL );
3237 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
3238 #ifdef MP4_VERBOSE
3239 msg_Dbg( p_stream,
3240 "read box: \"dcom\" compression algorithm : %4.4s",
3241 (char*)&p_box->data.p_dcom->i_algorithm );
3242 #endif
3243 MP4_READBOX_EXIT( 1 );
3246 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
3248 free( p_box->data.p_cmvd->p_data );
3251 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
3253 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t, MP4_FreeBox_cmvd );
3255 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
3257 p_box->data.p_cmvd->i_compressed_size = i_read;
3259 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
3260 MP4_READBOX_EXIT( 0 );
3262 /* now copy compressed data */
3263 memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
3265 p_box->data.p_cmvd->b_compressed = 1;
3267 #ifdef MP4_VERBOSE
3268 msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
3269 p_box->data.p_cmvd->i_compressed_size );
3270 #endif
3272 MP4_READBOX_EXIT( 1 );
3275 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
3277 MP4_Box_t *p_dcom;
3278 MP4_Box_t *p_cmvd;
3280 #ifdef HAVE_ZLIB_H
3281 stream_t *p_stream_memory;
3282 z_stream z_data;
3283 uint8_t *p_data;
3284 int i_result;
3285 #endif
3287 if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
3288 return 0;
3290 if( !p_box->p_father ||
3291 ( p_box->p_father->i_type != ATOM_moov &&
3292 p_box->p_father->i_type != ATOM_foov ) )
3294 msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
3295 return 1;
3298 if( !MP4_ReadBoxContainer( p_stream, p_box ) )
3300 return 0;
3303 if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
3304 ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
3305 p_cmvd->data.p_cmvd->p_data == NULL )
3307 msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
3308 return 0;
3311 if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
3313 msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
3314 "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
3315 return 0;
3318 #ifndef HAVE_ZLIB_H
3319 msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
3320 return 0;
3322 #else
3323 /* decompress data */
3324 /* allocate a new buffer */
3325 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
3326 return 0;
3327 /* init default structures */
3328 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
3329 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
3330 z_data.next_out = p_data;
3331 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
3332 z_data.zalloc = (alloc_func)Z_NULL;
3333 z_data.zfree = (free_func)Z_NULL;
3334 z_data.opaque = (voidpf)Z_NULL;
3336 /* init zlib */
3337 if( inflateInit( &z_data ) != Z_OK )
3339 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3340 free( p_data );
3341 return 0;
3344 /* uncompress */
3345 i_result = inflate( &z_data, Z_NO_FLUSH );
3346 if( i_result != Z_OK && i_result != Z_STREAM_END )
3348 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3349 free( p_data );
3350 return 0;
3353 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
3355 msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
3356 "mismatch" );
3358 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
3360 /* close zlib */
3361 if( inflateEnd( &z_data ) != Z_OK )
3363 msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
3364 "data (ignored)" );
3367 free( p_cmvd->data.p_cmvd->p_data );
3368 p_cmvd->data.p_cmvd->p_data = p_data;
3369 p_cmvd->data.p_cmvd->b_compressed = 0;
3371 msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
3373 /* now create a memory stream */
3374 p_stream_memory =
3375 vlc_stream_MemoryNew( VLC_OBJECT(p_stream),
3376 p_cmvd->data.p_cmvd->p_data,
3377 p_cmvd->data.p_cmvd->i_uncompressed_size, true );
3379 /* and read uncompressd moov */
3380 p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
3382 vlc_stream_Delete( p_stream_memory );
3384 #ifdef MP4_VERBOSE
3385 msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
3386 #endif
3388 return p_box->data.p_cmov->p_moov ? 1 : 0;
3389 #endif /* HAVE_ZLIB_H */
3392 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
3394 free( p_box->data.p_rdrf->psz_ref );
3397 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
3399 uint32_t i_len;
3400 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t, MP4_FreeBox_rdrf );
3402 MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
3403 MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
3404 MP4_GET4BYTES( i_len );
3405 i_len++;
3407 if( i_len > 0 )
3409 p_box->data.p_rdrf->psz_ref = malloc( i_len );
3410 if( p_box->data.p_rdrf->psz_ref == NULL )
3411 MP4_READBOX_EXIT( 0 );
3412 i_len--;
3414 for( unsigned i = 0; i < i_len; i++ )
3416 MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
3418 p_box->data.p_rdrf->psz_ref[i_len] = '\0';
3420 else
3422 p_box->data.p_rdrf->psz_ref = NULL;
3425 #ifdef MP4_VERBOSE
3426 msg_Dbg( p_stream,
3427 "read box: \"rdrf\" type:%4.4s ref %s",
3428 (char*)&p_box->data.p_rdrf->i_ref_type,
3429 p_box->data.p_rdrf->psz_ref );
3430 #endif
3431 MP4_READBOX_EXIT( 1 );
3436 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
3438 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t, NULL );
3440 MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
3442 MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
3444 #ifdef MP4_VERBOSE
3445 msg_Dbg( p_stream,
3446 "read box: \"rmdr\" rate:%d",
3447 p_box->data.p_rmdr->i_rate );
3448 #endif
3449 MP4_READBOX_EXIT( 1 );
3452 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
3454 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t, NULL );
3456 MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
3458 #ifdef MP4_VERBOSE
3459 msg_Dbg( p_stream,
3460 "read box: \"rmqu\" quality:%d",
3461 p_box->data.p_rmqu->i_quality );
3462 #endif
3463 MP4_READBOX_EXIT( 1 );
3466 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
3468 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t, NULL );
3469 MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
3471 MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
3472 MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
3473 MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
3474 MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
3476 #ifdef MP4_VERBOSE
3477 msg_Dbg( p_stream,
3478 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
3479 (char*)&p_box->data.p_rmvc->i_gestaltType,
3480 p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
3481 p_box->data.p_rmvc->i_checkType );
3482 #endif
3484 MP4_READBOX_EXIT( 1 );
3487 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
3489 MP4_READBOX_ENTER( MP4_Box_data_frma_t, NULL );
3491 MP4_GETFOURCC( p_box->data.p_frma->i_type );
3493 #ifdef MP4_VERBOSE
3494 msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
3495 (char *)&p_box->data.p_frma->i_type );
3496 #endif
3498 MP4_READBOX_EXIT( 1 );
3501 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
3503 MP4_READBOX_ENTER( MP4_Box_data_skcr_t, NULL );
3505 MP4_GET4BYTES( p_box->data.p_skcr->i_init );
3506 MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
3507 MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
3509 #ifdef MP4_VERBOSE
3510 msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
3511 p_box->data.p_skcr->i_init,
3512 p_box->data.p_skcr->i_encr,
3513 p_box->data.p_skcr->i_decr );
3514 #endif
3516 MP4_READBOX_EXIT( 1 );
3519 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
3521 VLC_UNUSED(p_box);
3522 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
3523 * so unless data decrypt itself by magic, there will be no playback,
3524 * but we never know... */
3525 msg_Warn( p_stream, "DRM protected streams are not supported." );
3526 return 1;
3529 static void MP4_FreeBox_Binary( MP4_Box_t *p_box )
3531 free( p_box->data.p_binary->p_blob );
3534 static int MP4_ReadBox_Binary( stream_t *p_stream, MP4_Box_t *p_box )
3536 MP4_READBOX_ENTER( MP4_Box_data_binary_t, MP4_FreeBox_Binary );
3537 i_read = __MIN( i_read, UINT32_MAX );
3538 if ( i_read > 0 )
3540 p_box->data.p_binary->p_blob = malloc( i_read );
3541 if ( p_box->data.p_binary->p_blob )
3543 memcpy( p_box->data.p_binary->p_blob, p_peek, i_read );
3544 p_box->data.p_binary->i_blob = i_read;
3547 MP4_READBOX_EXIT( 1 );
3550 static void MP4_FreeBox_data( MP4_Box_t *p_box )
3552 free( p_box->data.p_data->p_blob );
3555 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
3557 MP4_READBOX_ENTER( MP4_Box_data_data_t, MP4_FreeBox_data );
3558 MP4_Box_data_data_t *p_data = p_box->data.p_data;
3560 if ( i_read < 8 || i_read - 8 > UINT32_MAX )
3561 MP4_READBOX_EXIT( 0 );
3563 uint8_t i_type;
3564 MP4_GET1BYTE( i_type );
3565 if ( i_type != 0 )
3567 #ifdef MP4_VERBOSE
3568 msg_Dbg( p_stream, "skipping unknown 'data' atom with type %"PRIu8, i_type );
3569 #endif
3570 MP4_READBOX_EXIT( 0 );
3573 MP4_GET3BYTES( p_data->e_wellknowntype );
3574 MP4_GET2BYTES( p_data->locale.i_country );
3575 MP4_GET2BYTES( p_data->locale.i_language );
3576 #ifdef MP4_VERBOSE
3577 msg_Dbg( p_stream, "read 'data' atom: knowntype=%"PRIu32", country=%"PRIu16" lang=%"PRIu16
3578 ", size %"PRIu64" bytes", p_data->e_wellknowntype,
3579 p_data->locale.i_country, p_data->locale.i_language, i_read );
3580 #endif
3581 p_box->data.p_data->p_blob = malloc( i_read );
3582 if ( !p_box->data.p_data->p_blob )
3583 MP4_READBOX_EXIT( 0 );
3585 p_box->data.p_data->i_blob = i_read;
3586 memcpy( p_box->data.p_data->p_blob, p_peek, i_read);
3588 MP4_READBOX_EXIT( 1 );
3591 static int MP4_ReadBox_Metadata( stream_t *p_stream, MP4_Box_t *p_box )
3593 const uint8_t *p_peek;
3594 if ( vlc_stream_Peek( p_stream, &p_peek, 16 ) < 16 )
3595 return 0;
3596 if ( vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
3597 return 0;
3598 const uint32_t stoplist[] = { ATOM_data, 0 };
3599 return MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist );
3602 /* Chapter support */
3603 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
3605 MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
3606 for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
3607 free( p_chpl->chapter[i].psz_name );
3610 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
3612 MP4_Box_data_chpl_t *p_chpl;
3613 uint32_t i_dummy;
3614 VLC_UNUSED(i_dummy);
3615 int i;
3616 MP4_READBOX_ENTER( MP4_Box_data_chpl_t, MP4_FreeBox_chpl );
3618 p_chpl = p_box->data.p_chpl;
3620 MP4_GETVERSIONFLAGS( p_chpl );
3622 if ( i_read < 5 || p_chpl->i_version != 0x1 )
3623 MP4_READBOX_EXIT( 0 );
3625 MP4_GET4BYTES( i_dummy );
3627 MP4_GET1BYTE( p_chpl->i_chapter );
3629 for( i = 0; i < p_chpl->i_chapter; i++ )
3631 uint64_t i_start;
3632 uint8_t i_len;
3633 int i_copy;
3634 if ( i_read < 9 )
3635 break;
3636 MP4_GET8BYTES( i_start );
3637 MP4_GET1BYTE( i_len );
3639 p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
3640 if( !p_chpl->chapter[i].psz_name )
3641 MP4_READBOX_EXIT( 0 );
3643 i_copy = __MIN( i_len, i_read );
3644 if( i_copy > 0 )
3645 memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
3646 p_chpl->chapter[i].psz_name[i_copy] = '\0';
3647 p_chpl->chapter[i].i_start = i_start;
3649 p_peek += i_copy;
3650 i_read -= i_copy;
3653 if ( i != p_chpl->i_chapter )
3654 p_chpl->i_chapter = i;
3656 /* Bubble sort by increasing start date */
3659 for( i = 0; i < p_chpl->i_chapter - 1; i++ )
3661 if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
3663 char *psz = p_chpl->chapter[i+1].psz_name;
3664 int64_t i64 = p_chpl->chapter[i+1].i_start;
3666 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
3667 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
3669 p_chpl->chapter[i].psz_name = psz;
3670 p_chpl->chapter[i].i_start = i64;
3672 i = -1;
3673 break;
3676 } while( i == -1 );
3678 #ifdef MP4_VERBOSE
3679 msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
3680 p_chpl->i_chapter );
3681 #endif
3682 MP4_READBOX_EXIT( 1 );
3685 /* GoPro HiLight tags support */
3686 static void MP4_FreeBox_HMMT( MP4_Box_t *p_box )
3688 free( p_box->data.p_hmmt->pi_chapter_start );
3691 static int MP4_ReadBox_HMMT( stream_t *p_stream, MP4_Box_t *p_box )
3693 #define MAX_CHAPTER_COUNT 100
3695 MP4_Box_data_HMMT_t *p_hmmt;
3696 MP4_READBOX_ENTER( MP4_Box_data_HMMT_t, MP4_FreeBox_HMMT );
3698 if( i_read < 4 )
3699 MP4_READBOX_EXIT( 0 );
3701 p_hmmt = p_box->data.p_hmmt;
3703 MP4_GET4BYTES( p_hmmt->i_chapter_count );
3705 if( p_hmmt->i_chapter_count <= 0 )
3707 p_hmmt->pi_chapter_start = NULL;
3708 MP4_READBOX_EXIT( 1 );
3711 if( ( i_read / sizeof(uint32_t) ) < p_hmmt->i_chapter_count )
3712 MP4_READBOX_EXIT( 0 );
3714 /* Cameras are allowing a maximum of 100 tags */
3715 if( p_hmmt->i_chapter_count > MAX_CHAPTER_COUNT )
3716 p_hmmt->i_chapter_count = MAX_CHAPTER_COUNT;
3718 p_hmmt->pi_chapter_start = vlc_alloc( p_hmmt->i_chapter_count, sizeof(uint32_t) );
3719 if( p_hmmt->pi_chapter_start == NULL )
3720 MP4_READBOX_EXIT( 0 );
3722 for( uint32_t i = 0; i < p_hmmt->i_chapter_count; i++ )
3724 MP4_GET4BYTES( p_hmmt->pi_chapter_start[i] );
3727 #ifdef MP4_VERBOSE
3728 msg_Dbg( p_stream, "read box: \"HMMT\" %d HiLight tags", p_hmmt->i_chapter_count );
3729 #endif
3731 MP4_READBOX_EXIT( 1 );
3734 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
3736 free( p_box->data.p_tref_generic->i_track_ID );
3739 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
3741 uint32_t count;
3743 MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t, MP4_FreeBox_tref_generic );
3745 p_box->data.p_tref_generic->i_track_ID = NULL;
3746 count = i_read / sizeof(uint32_t);
3747 p_box->data.p_tref_generic->i_entry_count = count;
3748 p_box->data.p_tref_generic->i_track_ID = vlc_alloc( count,
3749 sizeof(uint32_t) );
3750 if( p_box->data.p_tref_generic->i_track_ID == NULL )
3751 MP4_READBOX_EXIT( 0 );
3753 for( unsigned i = 0; i < count; i++ )
3755 MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
3757 #ifdef MP4_VERBOSE
3758 msg_Dbg( p_stream, "read box: \"chap\" %d references",
3759 p_box->data.p_tref_generic->i_entry_count );
3760 #endif
3762 MP4_READBOX_EXIT( 1 );
3765 static void MP4_FreeBox_keys( MP4_Box_t *p_box )
3767 for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
3768 free( p_box->data.p_keys->p_entries[i].psz_value );
3769 free( p_box->data.p_keys->p_entries );
3772 static int MP4_ReadBox_keys( stream_t *p_stream, MP4_Box_t *p_box )
3774 MP4_READBOX_ENTER( MP4_Box_data_keys_t, MP4_FreeBox_keys );
3776 if ( i_read < 8 )
3777 MP4_READBOX_EXIT( 0 );
3779 uint32_t i_count;
3780 MP4_GET4BYTES( i_count ); /* reserved + flags */
3781 if ( i_count != 0 )
3782 MP4_READBOX_EXIT( 0 );
3784 MP4_GET4BYTES( i_count );
3785 p_box->data.p_keys->p_entries = calloc( i_count, sizeof(*p_box->data.p_keys->p_entries) );
3786 if ( !p_box->data.p_keys->p_entries )
3787 MP4_READBOX_EXIT( 0 );
3788 p_box->data.p_keys->i_entry_count = i_count;
3790 uint32_t i=0;
3791 for( ; i < i_count; i++ )
3793 if ( i_read < 8 )
3794 break;
3795 uint32_t i_keysize;
3796 MP4_GET4BYTES( i_keysize );
3797 if ( (i_keysize < 8) || (i_keysize - 4 > i_read) )
3798 break;
3799 MP4_GETFOURCC( p_box->data.p_keys->p_entries[i].i_namespace );
3800 i_keysize -= 8;
3801 p_box->data.p_keys->p_entries[i].psz_value = malloc( i_keysize + 1 );
3802 if ( !p_box->data.p_keys->p_entries[i].psz_value )
3803 break;
3804 memcpy( p_box->data.p_keys->p_entries[i].psz_value, p_peek, i_keysize );
3805 p_box->data.p_keys->p_entries[i].psz_value[i_keysize] = 0;
3806 p_peek += i_keysize;
3807 i_read -= i_keysize;
3808 #ifdef MP4_ULTRA_VERBOSE
3809 msg_Dbg( p_stream, "read box: \"keys\": %u '%s'", i + 1,
3810 p_box->data.p_keys->p_entries[i].psz_value );
3811 #endif
3813 if ( i < i_count )
3814 p_box->data.p_keys->i_entry_count = i;
3816 MP4_READBOX_EXIT( 1 );
3819 static int MP4_ReadBox_colr( stream_t *p_stream, MP4_Box_t *p_box )
3821 MP4_READBOX_ENTER( MP4_Box_data_colr_t, NULL );
3822 MP4_GETFOURCC( p_box->data.p_colr->i_type );
3823 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'c' ) ||
3824 p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3826 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_primary_idx );
3827 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_transfer_function_idx );
3828 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_matrix_idx );
3829 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3830 MP4_GET1BYTE( p_box->data.p_colr->nclc.i_full_range );
3832 else
3834 #ifdef MP4_VERBOSE
3835 msg_Warn( p_stream, "Unhandled colr type: %4.4s", (char*)&p_box->data.p_colr->i_type );
3836 #endif
3838 MP4_READBOX_EXIT( 1 );
3841 static int MP4_ReadBox_irot( stream_t *p_stream, MP4_Box_t *p_box )
3843 MP4_READBOX_ENTER( MP4_Box_data_irot_t, NULL );
3844 MP4_GET1BYTE( p_box->data.p_irot->i_ccw_degrees );
3845 p_box->data.p_irot->i_ccw_degrees &= 0x03;
3846 p_box->data.p_irot->i_ccw_degrees *= 90;
3847 MP4_READBOX_EXIT( 1 );
3850 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
3852 const uint8_t *p_peek;
3853 const size_t i_headersize = mp4_box_headersize( p_box );
3855 if( p_box->i_size < 16 || p_box->i_size - i_headersize < 8 )
3856 return 0;
3858 /* skip over box header */
3859 if( vlc_stream_Read( p_stream, NULL, i_headersize ) < (ssize_t) i_headersize )
3860 return 0;
3862 /* meta content starts with a 4 byte version/flags value (should be 0) */
3863 if( vlc_stream_Peek( p_stream, &p_peek, 8 ) < 8 )
3864 return 0;
3866 if( !memcmp( p_peek, "\0\0\0", 4 ) ) /* correct header case */
3868 if( vlc_stream_Read( p_stream, NULL, 4 ) < 4 )
3869 return 0;
3871 else if( memcmp( &p_peek[4], "hdlr", 4 ) ) /* Broken, headerless ones */
3873 return 0;
3876 /* load child atoms up to the handler (which should be next anyway) */
3877 const uint32_t stoplist[] = { ATOM_hdlr, 0 };
3878 if ( !MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist ) )
3879 return 0;
3881 /* Mandatory */
3882 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box, "hdlr" );
3883 if ( p_hdlr && BOXDATA(p_hdlr) && BOXDATA(p_hdlr)->i_version == 0 )
3885 p_box->i_handler = BOXDATA(p_hdlr)->i_handler_type;
3886 switch( p_box->i_handler )
3888 case HANDLER_pict:
3889 case HANDLER_mdta:
3890 case HANDLER_mdir:
3891 /* then it behaves like a container */
3892 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
3893 default:
3894 /* skip parsing, will be seen as empty container */
3895 break;
3899 return 1;
3902 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3904 char i_unused;
3905 VLC_UNUSED(i_unused);
3907 MP4_READBOX_ENTER( MP4_Box_data_iods_t, NULL );
3908 MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3910 MP4_GET1BYTE( i_unused ); /* tag */
3911 MP4_GET1BYTE( i_unused ); /* length */
3913 MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3914 are used for other flags */
3915 MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3916 MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3917 MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3918 MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3919 MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3921 #ifdef MP4_VERBOSE
3922 msg_Dbg( p_stream,
3923 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3924 "visual: %i, graphics: %i",
3925 p_box->data.p_iods->i_object_descriptor >> 6,
3926 p_box->data.p_iods->i_OD_profile_level,
3927 p_box->data.p_iods->i_scene_profile_level,
3928 p_box->data.p_iods->i_audio_profile_level,
3929 p_box->data.p_iods->i_visual_profile_level,
3930 p_box->data.p_iods->i_graphics_profile_level );
3931 #endif
3933 MP4_READBOX_EXIT( 1 );
3936 static int MP4_ReadBox_btrt( stream_t *p_stream, MP4_Box_t *p_box )
3938 MP4_READBOX_ENTER( MP4_Box_data_btrt_t, NULL );
3940 if(i_read != 12)
3941 MP4_READBOX_EXIT( 0 );
3943 MP4_GET4BYTES( p_box->data.p_btrt->i_buffer_size );
3944 MP4_GET4BYTES( p_box->data.p_btrt->i_max_bitrate );
3945 MP4_GET4BYTES( p_box->data.p_btrt->i_avg_bitrate );
3947 MP4_READBOX_EXIT( 1 );
3950 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3952 MP4_READBOX_ENTER( MP4_Box_data_pasp_t, NULL );
3954 MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
3955 MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
3957 #ifdef MP4_VERBOSE
3958 msg_Dbg( p_stream,
3959 "read box: \"paps\" %dx%d",
3960 p_box->data.p_pasp->i_horizontal_spacing,
3961 p_box->data.p_pasp->i_vertical_spacing);
3962 #endif
3964 MP4_READBOX_EXIT( 1 );
3967 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
3969 MP4_READBOX_ENTER( MP4_Box_data_mehd_t, NULL );
3971 MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
3972 if( p_box->data.p_mehd->i_version == 1 )
3973 MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
3974 else /* version == 0 */
3975 MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
3977 #ifdef MP4_VERBOSE
3978 msg_Dbg( p_stream,
3979 "read box: \"mehd\" frag dur. %"PRIu64"",
3980 p_box->data.p_mehd->i_fragment_duration );
3981 #endif
3983 MP4_READBOX_EXIT( 1 );
3986 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
3988 MP4_READBOX_ENTER( MP4_Box_data_trex_t, NULL );
3989 MP4_GETVERSIONFLAGS( p_box->data.p_trex );
3991 MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
3992 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
3993 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
3994 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
3995 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
3997 #ifdef MP4_VERBOSE
3998 msg_Dbg( p_stream,
3999 "read box: \"trex\" trackID: %"PRIu32"",
4000 p_box->data.p_trex->i_track_ID );
4001 #endif
4003 MP4_READBOX_EXIT( 1 );
4006 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
4008 free( p_box->data.p_sdtp->p_sample_table );
4011 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
4013 uint32_t i_sample_count;
4014 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t, MP4_FreeBox_sdtp );
4015 MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
4016 MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
4017 i_sample_count = i_read;
4019 p_sdtp->p_sample_table = malloc( i_sample_count );
4020 if( unlikely(p_sdtp->p_sample_table == NULL) )
4021 MP4_READBOX_EXIT( 0 );
4023 for( uint32_t i = 0; i < i_sample_count; i++ )
4024 MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
4026 #ifdef MP4_VERBOSE
4027 msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
4028 if ( i_sample_count > 3 )
4029 msg_Dbg( p_stream,
4030 "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
4031 p_sdtp->p_sample_table[0],
4032 p_sdtp->p_sample_table[1],
4033 p_sdtp->p_sample_table[2],
4034 p_sdtp->p_sample_table[3] );
4035 #endif
4037 MP4_READBOX_EXIT( 1 );
4040 static int MP4_ReadBox_tsel( stream_t *p_stream, MP4_Box_t *p_box )
4042 MP4_READBOX_ENTER( MP4_Box_data_tsel_t, NULL );
4043 uint32_t i_version;
4044 MP4_GET4BYTES( i_version );
4045 if ( i_version != 0 || i_read < 4 )
4046 MP4_READBOX_EXIT( 0 );
4047 MP4_GET4BYTES( p_box->data.p_tsel->i_switch_group );
4048 /* ignore list of attributes as es are present before switch */
4049 MP4_READBOX_EXIT( 1 );
4052 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
4054 MP4_READBOX_ENTER( MP4_Box_data_mfro_t, NULL );
4056 MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
4057 MP4_GET4BYTES( p_box->data.p_mfro->i_size );
4059 #ifdef MP4_VERBOSE
4060 msg_Dbg( p_stream,
4061 "read box: \"mfro\" size: %"PRIu32"",
4062 p_box->data.p_mfro->i_size);
4063 #endif
4065 MP4_READBOX_EXIT( 1 );
4068 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
4070 free( p_box->data.p_tfra->p_time );
4071 free( p_box->data.p_tfra->p_moof_offset );
4072 free( p_box->data.p_tfra->p_traf_number );
4073 free( p_box->data.p_tfra->p_trun_number );
4074 free( p_box->data.p_tfra->p_sample_number );
4077 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
4079 #define READ_VARIABLE_LENGTH(lengthvar, p_array) switch (lengthvar)\
4081 case 0:\
4082 MP4_GET1BYTE( p_array[i] );\
4083 break;\
4084 case 1:\
4085 MP4_GET2BYTES( *((uint16_t *)&p_array[i*2]) );\
4086 break;\
4087 case 2:\
4088 MP4_GET3BYTES( *((uint32_t *)&p_array[i*4]) );\
4089 break;\
4090 case 3:\
4091 MP4_GET4BYTES( *((uint32_t *)&p_array[i*4]) );\
4092 break;\
4093 default:\
4094 goto error;\
4096 #define FIX_VARIABLE_LENGTH(lengthvar) if ( lengthvar == 3 ) lengthvar = 4
4098 uint32_t i_number_of_entries;
4099 MP4_READBOX_ENTER( MP4_Box_data_tfra_t, MP4_FreeBox_tfra );
4100 MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
4101 MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
4102 if ( p_tfra->i_version > 1 )
4103 MP4_READBOX_EXIT( 0 );
4104 MP4_GET4BYTES( p_tfra->i_track_ID );
4105 uint32_t i_lengths = 0;
4106 MP4_GET4BYTES( i_lengths );
4107 MP4_GET4BYTES( p_tfra->i_number_of_entries );
4108 i_number_of_entries = p_tfra->i_number_of_entries;
4109 p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
4110 p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
4111 p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
4113 size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
4114 p_tfra->p_time = calloc( i_number_of_entries, size );
4115 p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
4117 size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
4118 if ( size == 3 ) size++;
4119 p_tfra->p_traf_number = calloc( i_number_of_entries, size );
4120 size = 1 + p_tfra->i_length_size_of_trun_num;
4121 if ( size == 3 ) size++;
4122 p_tfra->p_trun_number = calloc( i_number_of_entries, size );
4123 size = 1 + p_tfra->i_length_size_of_sample_num;
4124 if ( size == 3 ) size++;
4125 p_tfra->p_sample_number = calloc( i_number_of_entries, size );
4127 if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
4128 || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
4129 goto error;
4131 unsigned i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
4132 + p_tfra->i_length_size_of_trun_num
4133 + p_tfra->i_length_size_of_sample_num;
4135 uint32_t i;
4136 for( i = 0; i < i_number_of_entries; i++ )
4139 if( p_tfra->i_version == 1 )
4141 if ( i_read < i_fields_length + 16 )
4142 break;
4143 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_time[i*2]) );
4144 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_moof_offset[i*2]) );
4146 else
4148 if ( i_read < i_fields_length + 8 )
4149 break;
4150 MP4_GET4BYTES( p_tfra->p_time[i] );
4151 MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
4154 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num, p_tfra->p_traf_number);
4155 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num, p_tfra->p_trun_number);
4156 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num, p_tfra->p_sample_number);
4158 if ( i < i_number_of_entries )
4159 i_number_of_entries = i;
4161 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num);
4162 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num);
4163 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num);
4165 #ifdef MP4_ULTRA_VERBOSE
4166 for( i = 0; i < i_number_of_entries; i++ )
4168 if( p_tfra->i_version == 0 )
4170 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu32", "
4171 "moof_offset[%"PRIu32"]: %"PRIu32"",
4172 p_tfra->i_track_ID,
4173 i, p_tfra->p_time[i],
4174 i, p_tfra->p_moof_offset[i] );
4176 else
4178 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu64", "
4179 "moof_offset[%"PRIu32"]: %"PRIu64"",
4180 p_tfra->i_track_ID,
4181 i, ((uint64_t *)(p_tfra->p_time))[i],
4182 i, ((uint64_t *)(p_tfra->p_moof_offset))[i] );
4185 #endif
4186 #ifdef MP4_VERBOSE
4187 msg_Dbg( p_stream, "tfra[%"PRIu32"] %"PRIu32" entries",
4188 p_tfra->i_track_ID, i_number_of_entries );
4189 #endif
4191 MP4_READBOX_EXIT( 1 );
4192 error:
4193 MP4_READBOX_EXIT( 0 );
4195 #undef READ_VARIABLE_LENGTH
4196 #undef FIX_VARIABLE_LENGTH
4199 static int MP4_ReadBox_pnot( stream_t *p_stream, MP4_Box_t *p_box )
4201 if ( p_box->i_size != 20 )
4202 return 0;
4203 MP4_READBOX_ENTER( MP4_Box_data_pnot_t, NULL );
4204 MP4_GET4BYTES( p_box->data.p_pnot->i_date );
4205 uint16_t i_version;
4206 MP4_GET2BYTES( i_version );
4207 if ( i_version != 0 )
4208 MP4_READBOX_EXIT( 0 );
4209 MP4_GETFOURCC( p_box->data.p_pnot->i_type );
4210 MP4_GET2BYTES( p_box->data.p_pnot->i_index );
4211 MP4_READBOX_EXIT( 1 );
4214 static int MP4_ReadBox_SA3D( stream_t *p_stream, MP4_Box_t *p_box )
4216 MP4_READBOX_ENTER( MP4_Box_data_SA3D_t, NULL );
4218 uint8_t i_version;
4219 MP4_GET1BYTE( i_version );
4220 if ( i_version != 0 )
4221 MP4_READBOX_EXIT( 0 );
4223 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_type );
4224 MP4_GET4BYTES( p_box->data.p_SA3D->i_ambisonic_order );
4225 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_channel_ordering );
4226 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_normalization );
4227 MP4_GET4BYTES( p_box->data.p_SA3D->i_num_channels );
4228 MP4_READBOX_EXIT( 1 );
4231 static void MP4_FreeBox_Reference( MP4_Box_t *p_box )
4233 MP4_Box_data_refbox_t *p_data = p_box->data.p_refbox;
4234 free( p_data->p_references );
4237 static int MP4_ReadBox_Reference( stream_t *p_stream, MP4_Box_t *p_box )
4239 MP4_READBOX_ENTER( MP4_Box_data_refbox_t, MP4_FreeBox_Reference );
4240 MP4_Box_data_refbox_t *p_data = p_box->data.p_refbox;
4242 if( p_box->p_father->data.p_iref->i_flags == 0 )
4243 MP4_GET2BYTES( p_data->i_from_item_id );
4244 else
4245 MP4_GET4BYTES( p_data->i_from_item_id );
4246 MP4_GET2BYTES( p_data->i_reference_count );
4247 if( i_read / ((p_box->p_father->data.p_iref->i_flags == 0 ) ? 2 : 4) <
4248 p_data->i_reference_count )
4249 MP4_READBOX_EXIT( 0 );
4251 p_data->p_references = malloc( sizeof(*p_data->p_references) *
4252 p_data->i_reference_count );
4253 if( !p_data->p_references )
4254 MP4_READBOX_EXIT( 0 );
4255 for( uint16_t i=0; i<p_data->i_reference_count; i++ )
4257 if( p_box->p_father->data.p_iref->i_flags == 0 )
4258 MP4_GET2BYTES( p_data->p_references[i].i_to_item_id );
4259 else
4260 MP4_GET4BYTES( p_data->p_references[i].i_to_item_id );
4263 MP4_READBOX_EXIT( 1 );
4266 static int MP4_ReadBox_iref( stream_t *p_stream, MP4_Box_t *p_box )
4268 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_iref_t, 12, NULL );
4269 MP4_Box_data_iref_t *p_data = p_box->data.p_iref;
4270 if( i_read < 4 )
4271 MP4_READBOX_EXIT( 0 );
4273 MP4_GET1BYTE( p_data->i_version );
4274 MP4_GET3BYTES( p_data->i_flags );
4275 if( p_data->i_version > 0 )
4276 MP4_READBOX_EXIT( 0 );
4278 assert( i_read == 0 );
4280 uint32_t i = 0;
4281 uint64_t i_remain = p_box->i_size - 12;
4282 while ( i_remain > 8 )
4284 MP4_Box_t *p_childbox = MP4_ReadBoxUsing( p_stream, p_box,
4285 MP4_ReadBox_Reference );
4286 if( !p_childbox || i_remain < p_childbox->i_size )
4288 MP4_BoxFree( p_childbox );
4289 break;
4292 MP4_BoxAddChild( p_box, p_childbox );
4293 i_remain -= p_childbox->i_size;
4294 i++;
4297 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
4298 MP4_READBOX_EXIT( 0 );
4300 MP4_READBOX_EXIT( 1 );
4303 static void MP4_FreeBox_iloc( MP4_Box_t *p_box )
4305 MP4_Box_data_iloc_t *p_data = p_box->data.p_iloc;
4306 for( uint32_t i=0; i<p_data->i_item_count; i++ )
4307 free( p_data->p_items[i].p_extents );
4308 free( p_data->p_items );
4311 static int MP4_ReadBox_iloc( stream_t *p_stream, MP4_Box_t *p_box )
4313 MP4_READBOX_ENTER( MP4_Box_data_iloc_t, MP4_FreeBox_iloc );
4314 MP4_Box_data_iloc_t *p_data = p_box->data.p_iloc;
4316 uint16_t i_foo;
4318 uint8_t i_version;
4319 uint32_t i_flags;
4320 MP4_GET1BYTE( i_version );
4321 MP4_GET3BYTES( i_flags );
4322 VLC_UNUSED(i_flags);
4324 MP4_GET1BYTE( p_data->i_offset_size );
4325 p_data->i_length_size = p_data->i_offset_size & 0x0F;
4326 p_data->i_offset_size >>= 4;
4327 MP4_GET1BYTE( p_data->i_base_offset_size );
4328 if( i_version == 0 )
4329 p_data->i_index_size = 0;
4330 else
4331 p_data->i_index_size = p_data->i_base_offset_size & 0x0F;
4332 p_data->i_base_offset_size >>= 4;
4334 /* Only accept 0,4,8 */
4335 if( (p_data->i_offset_size & 0xF3) || p_data->i_offset_size > 8 ||
4336 (p_data->i_length_size & 0xF3) || p_data->i_length_size > 8 ||
4337 (p_data->i_base_offset_size & 0xF3) || p_data->i_base_offset_size > 8 ||
4338 (p_data->i_index_size & 0xF3) || p_data->i_index_size > 8 )
4339 MP4_READBOX_EXIT( 0 );
4341 if( i_version < 2 )
4342 MP4_GET2BYTES( p_data->i_item_count );
4343 else if( i_version == 2 )
4344 MP4_GET4BYTES( p_data->i_item_count );
4345 else
4346 MP4_READBOX_EXIT( 0 );
4348 if( i_read / 6 < p_data->i_item_count )
4349 MP4_READBOX_EXIT( 0 );
4351 p_data->p_items = malloc( p_data->i_item_count * sizeof(p_data->p_items[0]) );
4352 if( !p_data->p_items )
4353 MP4_READBOX_EXIT( 0 );
4355 for( uint32_t i=0; i<p_data->i_item_count; i++ )
4357 if( i_version < 2 )
4358 MP4_GET2BYTES( p_data->p_items[i].i_item_id );
4359 else
4360 MP4_GET4BYTES( p_data->p_items[i].i_item_id );
4362 if( i_version > 0 )
4364 MP4_GET2BYTES( i_foo );
4365 p_data->p_items[i].i_construction_method = i_foo & 0x0F;
4368 MP4_GET2BYTES( p_data->p_items[i].i_data_reference_index );
4370 switch( p_data->i_base_offset_size )
4372 case 4: MP4_GET4BYTES( p_data->p_items[i].i_base_offset ); break;
4373 case 8: MP4_GET8BYTES( p_data->p_items[i].i_base_offset ); break;
4374 default: p_data->p_items[i].i_base_offset = 0; break;
4377 MP4_GET2BYTES( p_data->p_items[i].i_extent_count );
4379 uint64_t i_entrysize = (( i_version > 0 ) ? p_data->i_index_size : 0) +
4380 p_data->i_offset_size + p_data->i_length_size;
4381 if( i_read / i_entrysize < p_data->p_items[i].i_extent_count )
4383 p_data->i_item_count = i;
4384 MP4_READBOX_EXIT( 0 );
4387 p_data->p_items[i].p_extents = malloc( p_data->p_items[i].i_extent_count *
4388 sizeof(p_data->p_items[i].p_extents[0]) );
4389 for( uint16_t j=0; j<p_data->p_items[i].i_extent_count; j++ )
4391 if( i_version > 0 )
4393 switch( p_data->i_index_size )
4395 case 4: MP4_GET4BYTES( p_data->p_items[i].p_extents[j].i_extent_index ); break;
4396 case 8: MP4_GET8BYTES( p_data->p_items[i].p_extents[j].i_extent_index ); break;
4397 default: break;
4400 switch( p_data->i_offset_size )
4402 case 4: MP4_GET4BYTES( p_data->p_items[i].p_extents[j].i_extent_offset ); break;
4403 case 8: MP4_GET8BYTES( p_data->p_items[i].p_extents[j].i_extent_offset ); break;
4404 default: break;
4406 switch( p_data->i_length_size )
4408 case 4: MP4_GET4BYTES( p_data->p_items[i].p_extents[j].i_extent_length ); break;
4409 case 8: MP4_GET8BYTES( p_data->p_items[i].p_extents[j].i_extent_length ); break;
4410 default: break;
4415 MP4_READBOX_EXIT( 1 );
4418 static int MP4_ReadBox_iinf( stream_t *p_stream, MP4_Box_t *p_box )
4420 const uint8_t *p_versionpeek;
4421 size_t i_peek = vlc_stream_Peek( p_stream, &p_versionpeek, 9 );
4422 if( i_peek < 9 )
4423 return 0;
4425 size_t i_header = 12 + (( p_versionpeek[8] == 0 ) ? 2 : 4);
4426 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_iinf_t, i_header, NULL );
4427 if( i_read + 8 < i_header )
4428 MP4_READBOX_EXIT( 0 );
4430 uint8_t i_version;
4431 uint32_t i_flags;
4432 MP4_GET1BYTE( i_version );
4433 MP4_GET3BYTES( i_flags ); VLC_UNUSED(i_flags);
4434 if( i_version > 2 )
4435 MP4_READBOX_EXIT( 0 );
4437 if( i_version == 0 )
4438 MP4_GET2BYTES( p_box->data.p_iinf->i_entry_count );
4439 else
4440 MP4_GET4BYTES( p_box->data.p_iinf->i_entry_count );
4442 assert( i_read == 0 );
4444 uint32_t i = 0;
4445 uint64_t i_remain = p_box->i_size - 16;
4446 while ( i_remain > 8 && i < p_box->data.p_iinf->i_entry_count )
4448 MP4_Box_t *p_childbox = MP4_ReadBox( p_stream, p_box );
4449 if( !p_childbox || i_remain < p_childbox->i_size )
4451 MP4_BoxFree( p_childbox );
4452 p_box->data.p_iinf->i_entry_count = i;
4453 break;
4456 MP4_BoxAddChild( p_box, p_childbox );
4457 i_remain -= p_childbox->i_size;
4458 i++;
4461 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
4462 MP4_READBOX_EXIT( 0 );
4464 MP4_READBOX_EXIT( 1 );
4467 static void MP4_FreeBox_infe( MP4_Box_t *p_box )
4469 MP4_Box_data_infe_t *p_data = p_box->data.p_infe;
4470 free( p_data->psz_content_encoding );
4471 free( p_data->psz_content_type );
4472 free( p_data->psz_item_name );
4473 free( p_data->psz_item_uri_type );
4476 static int MP4_ReadBox_infe( stream_t *p_stream, MP4_Box_t *p_box )
4478 MP4_READBOX_ENTER( MP4_Box_data_infe_t, MP4_FreeBox_infe );
4479 MP4_Box_data_infe_t *p_data = p_box->data.p_infe;
4481 uint8_t i_version;
4482 MP4_GET1BYTE( i_version );
4483 MP4_GET3BYTES( p_data->i_flags );
4484 if( i_version > 3 )
4485 MP4_READBOX_EXIT( 0 );
4487 if( i_version < 2 )
4489 MP4_GET2BYTES( p_data->i_item_id );
4490 MP4_GET2BYTES( p_data->i_item_protection_index );
4491 p_data->psz_item_name = mp4_getstringz( &p_peek, &i_read );
4492 if( i_read > 0 )
4494 p_data->psz_content_type = mp4_getstringz( &p_peek, &i_read );
4495 if( i_read > 0 )
4496 p_data->psz_content_encoding = mp4_getstringz( &p_peek, &i_read );
4499 //if( i_version == 1 )
4501 /* extensions, we do not care */
4504 else
4506 if( i_version == 2 )
4507 MP4_GET2BYTES( p_data->i_item_id );
4508 else
4509 MP4_GET4BYTES( p_data->i_item_id );
4510 MP4_GET2BYTES( p_data->i_item_protection_index );
4511 MP4_GETFOURCC( p_data->item_type );
4512 p_data->psz_item_name = mp4_getstringz( &p_peek, &i_read );
4513 if( p_data->item_type == VLC_FOURCC('m','i','m','e') )
4515 p_data->psz_content_type = mp4_getstringz( &p_peek, &i_read );
4516 if( i_read > 0 )
4517 p_data->psz_content_encoding = mp4_getstringz( &p_peek, &i_read );
4519 else if( p_data->item_type == VLC_FOURCC('u','r','i',' ') )
4521 p_data->psz_item_uri_type = mp4_getstringz( &p_peek, &i_read );
4525 MP4_READBOX_EXIT( 1 );
4528 static int MP4_ReadBox_pitm( stream_t *p_stream, MP4_Box_t *p_box )
4530 MP4_READBOX_ENTER( MP4_Box_data_pitm_t, NULL );
4531 MP4_Box_data_pitm_t *p_data = p_box->data.p_pitm;
4533 uint8_t i_version;
4534 uint32_t i_flags;
4535 MP4_GET1BYTE( i_version );
4536 MP4_GET3BYTES( i_flags ); VLC_UNUSED(i_flags);
4538 if( i_version == 0 )
4539 MP4_GET2BYTES( p_data->i_item_id );
4540 else
4541 MP4_GET4BYTES( p_data->i_item_id );
4543 MP4_READBOX_EXIT( 1 );
4546 static int MP4_ReadBox_ispe( stream_t *p_stream, MP4_Box_t *p_box )
4548 MP4_READBOX_ENTER( MP4_Box_data_ispe_t, NULL );
4549 MP4_Box_data_ispe_t *p_data = p_box->data.p_ispe;
4551 uint8_t i_version;
4552 uint32_t i_flags;
4553 MP4_GET1BYTE( i_version );
4554 MP4_GET3BYTES( i_flags ); VLC_UNUSED(i_flags);
4555 if( i_version > 0 )
4556 MP4_READBOX_EXIT( 0 );
4558 MP4_GET4BYTES( p_data->i_width );
4559 MP4_GET4BYTES( p_data->i_height );
4561 MP4_READBOX_EXIT( 1 );
4564 static void MP4_FreeBox_ipma( MP4_Box_t *p_box )
4566 MP4_Box_data_ipma_t *p_data = p_box->data.p_ipma;
4567 for( uint32_t i=0; i<p_data->i_entry_count; i++ )
4568 free( p_data->p_entries[i].p_assocs );
4569 free( p_data->p_entries );
4572 static int MP4_ReadBox_ipma( stream_t *p_stream, MP4_Box_t *p_box )
4574 MP4_READBOX_ENTER( MP4_Box_data_ipma_t, MP4_FreeBox_ipma );
4575 MP4_Box_data_ipma_t *p_data = p_box->data.p_ipma;
4577 uint8_t i_version;
4578 uint32_t i_flags;
4579 MP4_GET1BYTE( i_version );
4580 MP4_GET3BYTES( i_flags );
4582 MP4_GET4BYTES( p_data->i_entry_count );
4583 if( (i_read / ((i_version < 1) ? 3 : 5) < p_data->i_entry_count) )
4585 p_data->i_entry_count = 0;
4586 MP4_READBOX_EXIT( 0 );
4589 p_data->p_entries = malloc( sizeof(p_data->p_entries[0]) * p_data->i_entry_count );
4590 if( !p_data->p_entries )
4592 p_data->i_entry_count = 0;
4593 MP4_READBOX_EXIT( 0 );
4596 for( uint32_t i=0; i<p_data->i_entry_count; i++ )
4598 if( i_read < ((i_version < 1) ? 3 : 5) )
4600 p_data->i_entry_count = i;
4601 MP4_READBOX_EXIT( 0 );
4603 if( i_version < 1 )
4604 MP4_GET2BYTES( p_data->p_entries[i].i_item_id );
4605 else
4606 MP4_GET4BYTES( p_data->p_entries[i].i_item_id );
4607 MP4_GET1BYTE( p_data->p_entries[i].i_association_count );
4609 if( i_read / ((i_flags & 0x01) ? 2 : 1) <
4610 p_data->p_entries[i].i_association_count )
4612 p_data->i_entry_count = i;
4613 MP4_READBOX_EXIT( 0 );
4616 p_data->p_entries[i].p_assocs =
4617 malloc( sizeof(p_data->p_entries[i].p_assocs[0]) *
4618 p_data->p_entries[i].i_association_count );
4619 if( !p_data->p_entries[i].p_assocs )
4621 p_data->p_entries[i].i_association_count = 0;
4622 p_data->i_entry_count = i;
4623 MP4_READBOX_EXIT( 0 );
4626 for( uint8_t j=0; j<p_data->p_entries[i].i_association_count; j++ )
4628 MP4_GET1BYTE( p_data->p_entries[i].p_assocs[j].i_property_index );
4629 p_data->p_entries[i].p_assocs[j].b_essential =
4630 p_data->p_entries[i].p_assocs[j].i_property_index & 0x80;
4631 p_data->p_entries[i].p_assocs[j].i_property_index &= 0x7F;
4632 if( i_flags & 0x01 )
4634 p_data->p_entries[i].p_assocs[j].i_property_index <<= 8;
4635 uint8_t i_low;
4636 MP4_GET1BYTE( i_low );
4637 p_data->p_entries[i].p_assocs[j].i_property_index |= i_low;
4642 MP4_READBOX_EXIT( 1 );
4645 /* For generic */
4646 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
4648 if( !p_box->p_father )
4650 goto unknown;
4652 if( p_box->p_father->i_type == ATOM_stsd )
4654 MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
4655 MP4_Box_t *p_hdlr;
4657 if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
4658 (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
4660 goto unknown;
4662 switch( p_hdlr->data.p_hdlr->i_handler_type )
4664 case ATOM_soun:
4665 return MP4_ReadBox_sample_soun( p_stream, p_box );
4666 case ATOM_vide:
4667 case ATOM_pict: /* heif */
4668 return MP4_ReadBox_sample_vide( p_stream, p_box );
4669 case ATOM_hint:
4670 return MP4_ReadBox_sample_hint8( p_stream, p_box );
4671 case ATOM_text:
4672 case ATOM_subt:
4673 case ATOM_tx3g:
4674 case ATOM_sbtl:
4675 return MP4_ReadBox_sample_text( p_stream, p_box );
4676 default:
4677 msg_Warn( p_stream,
4678 "unknown handler type in stsd (incompletely loaded)" );
4679 return 1;
4683 unknown:
4684 if MP4_BOX_TYPE_ASCII()
4685 msg_Warn( p_stream,
4686 "unknown box type %4.4s (incompletely loaded)",
4687 (char*)&p_box->i_type );
4688 else
4689 msg_Warn( p_stream,
4690 "unknown box type c%3.3s (incompletely loaded)",
4691 (char*)&p_box->i_type+1 );
4692 p_box->e_flags |= BOX_FLAG_INCOMPLETE;
4694 return 1;
4697 /**** ------------------------------------------------------------------- ****/
4699 static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
4701 if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
4702 return MP4_ReadBox_tfrf( p_stream, p_box );
4703 if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
4704 return MP4_ReadBox_tfxd( p_stream, p_box );
4705 if( !CmpUUID( &p_box->i_uuid, &XML360BoxUUID ) )
4706 return MP4_ReadBox_XML360( p_stream, p_box );
4707 if( !CmpUUID( &p_box->i_uuid, &PS3DDSBoxUUID ) && p_box->i_size == 28 )
4708 return MP4_ReadBox_Binary( p_stream, p_box );
4710 #ifdef MP4_VERBOSE
4711 msg_Warn( p_stream, "Unknown uuid type box: "
4712 "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-"
4713 "%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
4714 p_box->i_uuid.b[0], p_box->i_uuid.b[1], p_box->i_uuid.b[2], p_box->i_uuid.b[3],
4715 p_box->i_uuid.b[4], p_box->i_uuid.b[5], p_box->i_uuid.b[6], p_box->i_uuid.b[7],
4716 p_box->i_uuid.b[8], p_box->i_uuid.b[9], p_box->i_uuid.b[10], p_box->i_uuid.b[11],
4717 p_box->i_uuid.b[12], p_box->i_uuid.b[13], p_box->i_uuid.b[14], p_box->i_uuid.b[15] );
4718 #else
4719 msg_Warn( p_stream, "Unknown uuid type box" );
4720 #endif
4721 return 1;
4724 /**** ------------------------------------------------------------------- ****/
4725 /**** "Higher level" Functions ****/
4726 /**** ------------------------------------------------------------------- ****/
4728 static const struct
4730 uint32_t i_type;
4731 int (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
4732 uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
4733 } MP4_Box_Function [] =
4735 /* Containers */
4736 { ATOM_moov, MP4_ReadBoxContainer, 0 },
4737 { ATOM_foov, MP4_ReadBoxContainer, 0 },
4738 { ATOM_trak, MP4_ReadBoxContainer, ATOM_moov },
4739 { ATOM_trak, MP4_ReadBoxContainer, ATOM_foov },
4740 { ATOM_mdia, MP4_ReadBoxContainer, ATOM_trak },
4741 { ATOM_moof, MP4_ReadBoxContainer, 0 },
4742 { ATOM_minf, MP4_ReadBoxContainer, ATOM_mdia },
4743 { ATOM_stbl, MP4_ReadBoxContainer, ATOM_minf },
4744 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_minf },
4745 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_meta },
4746 { ATOM_edts, MP4_ReadBoxContainer, ATOM_trak },
4747 { ATOM_udta, MP4_ReadBoxContainer, 0 },
4748 { ATOM_nmhd, MP4_ReadBoxContainer, ATOM_minf },
4749 { ATOM_hnti, MP4_ReadBoxContainer, ATOM_udta },
4750 { ATOM_rmra, MP4_ReadBoxContainer, ATOM_moov },
4751 { ATOM_rmda, MP4_ReadBoxContainer, ATOM_rmra },
4752 { ATOM_tref, MP4_ReadBoxContainer, ATOM_trak },
4753 { ATOM_gmhd, MP4_ReadBoxContainer, ATOM_minf },
4754 { ATOM_wave, MP4_ReadBoxContainer, ATOM_stsd },
4755 { ATOM_wave, MP4_ReadBoxContainer, ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
4756 { ATOM_wave, MP4_ReadBoxContainer, ATOM_WMA2 }, /* flip4mac */
4757 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in24 },
4758 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in32 },
4759 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl32 },
4760 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl64 },
4761 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDMC },
4762 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDM2 },
4763 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiFL }, /* XiphQT */
4764 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiVs }, /* XiphQT */
4765 { ATOM_ilst, MP4_ReadBox_ilst, ATOM_meta },
4766 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_moov },
4767 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_ftyp },
4769 /* specific box */
4770 { ATOM_ftyp, MP4_ReadBox_ftyp, 0 },
4771 { ATOM_styp, MP4_ReadBox_ftyp, 0 },
4772 { ATOM_cmov, MP4_ReadBox_cmov, 0 },
4773 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_moov },
4774 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_foov },
4775 { ATOM_tkhd, MP4_ReadBox_tkhd, ATOM_trak },
4776 { ATOM_load, MP4_ReadBox_load, ATOM_trak },
4777 { ATOM_mdhd, MP4_ReadBox_mdhd, ATOM_mdia },
4778 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_mdia },
4779 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_meta },
4780 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_minf },
4781 { ATOM_vmhd, MP4_ReadBox_vmhd, ATOM_minf },
4782 { ATOM_smhd, MP4_ReadBox_smhd, ATOM_minf },
4783 { ATOM_hmhd, MP4_ReadBox_hmhd, ATOM_minf },
4784 { ATOM_alis, MP4_ReadBoxSkip, ATOM_dref },
4785 { ATOM_url, MP4_ReadBox_url, 0 },
4786 { ATOM_urn, MP4_ReadBox_urn, 0 },
4787 { ATOM_dref, MP4_ReadBox_LtdContainer, 0 },
4788 { ATOM_stts, MP4_ReadBox_stts, ATOM_stbl },
4789 { ATOM_ctts, MP4_ReadBox_ctts, ATOM_stbl },
4790 { ATOM_cslg, MP4_ReadBox_cslg, ATOM_stbl },
4791 { ATOM_stsd, MP4_ReadBox_LtdContainer, ATOM_stbl },
4792 { ATOM_stsz, MP4_ReadBox_stsz, ATOM_stbl },
4793 { ATOM_stsc, MP4_ReadBox_stsc, ATOM_stbl },
4794 { ATOM_stco, MP4_ReadBox_stco_co64, ATOM_stbl },
4795 { ATOM_co64, MP4_ReadBox_stco_co64, ATOM_stbl },
4796 { ATOM_stss, MP4_ReadBox_stss, ATOM_stbl },
4797 { ATOM_stsh, MP4_ReadBox_stsh, ATOM_stbl },
4798 { ATOM_stdp, MP4_ReadBox_stdp, 0 },
4799 { ATOM_elst, MP4_ReadBox_elst, ATOM_edts },
4800 { ATOM_cprt, MP4_ReadBox_cprt, 0 },
4801 { ATOM_esds, MP4_ReadBox_esds, ATOM_wave }, /* mp4a in wave chunk */
4802 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4a },
4803 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4v },
4804 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4s },
4805 { ATOM_dcom, MP4_ReadBox_dcom, 0 },
4806 { ATOM_dfLa, MP4_ReadBox_Binary, ATOM_fLaC },
4807 { ATOM_cmvd, MP4_ReadBox_cmvd, 0 },
4808 { ATOM_av1C, MP4_ReadBox_av1C, ATOM_av01 },
4809 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc1 },
4810 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc3 },
4811 { ATOM_hvcC, MP4_ReadBox_Binary, 0 },
4812 { ATOM_jpeC, MP4_ReadBox_Binary, 0 }, /* heif */
4813 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp08 },
4814 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp09 },
4815 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp10 },
4816 { ATOM_SmDm, MP4_ReadBox_SmDm, ATOM_vpcC }, /* SMPTE2086 mastering display */
4817 { ATOM_mdcv, MP4_ReadBox_SmDm, 0 }, /* */
4818 { ATOM_CoLL, MP4_ReadBox_CoLL, ATOM_vpcC }, /* CEA861-3 light level */
4819 { ATOM_clli, MP4_ReadBox_CoLL, 0 }, /* */
4820 { ATOM_dac3, MP4_ReadBox_dac3, 0 },
4821 { ATOM_dec3, MP4_ReadBox_dec3, 0 },
4822 { ATOM_dvc1, MP4_ReadBox_dvc1, ATOM_vc1 },
4823 { ATOM_fiel, MP4_ReadBox_fiel, 0 },
4824 { ATOM_glbl, MP4_ReadBox_Binary, ATOM_FFV1 },
4825 { ATOM_enda, MP4_ReadBox_enda, 0 },
4826 { ATOM_iods, MP4_ReadBox_iods, 0 },
4827 { ATOM_pasp, MP4_ReadBox_pasp, 0 },
4828 { ATOM_btrt, MP4_ReadBox_btrt, 0 }, /* codecs bitrate stsd/????/btrt */
4829 { ATOM_keys, MP4_ReadBox_keys, ATOM_meta },
4830 { ATOM_colr, MP4_ReadBox_colr, 0 },
4831 { ATOM_irot, MP4_ReadBox_irot, 0 }, /* heif */
4833 /* XiphQT */
4834 { ATOM_vCtH, MP4_ReadBox_Binary, ATOM_wave },
4835 { ATOM_vCtC, MP4_ReadBox_Binary, ATOM_wave },
4836 { ATOM_vCtd, MP4_ReadBox_Binary, ATOM_wave },
4837 { ATOM_fCtS, MP4_ReadBox_Binary, ATOM_wave },
4839 /* Samples groups specific information */
4840 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_stbl },
4841 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_traf },
4842 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_stbl },
4843 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_traf },
4845 /* Quicktime preview atoms, all at root */
4846 { ATOM_pnot, MP4_ReadBox_pnot, 0 },
4847 { ATOM_pict, MP4_ReadBox_Binary, 0 },
4848 { ATOM_PICT, MP4_ReadBox_Binary, 0 },
4850 /* Nothing to do with this box */
4851 { ATOM_mdat, MP4_ReadBoxSkip, 0 },
4852 { ATOM_skip, MP4_ReadBoxSkip, 0 },
4853 { ATOM_free, MP4_ReadBoxSkip, 0 },
4854 { ATOM_wide, MP4_ReadBoxSkip, 0 },
4855 { ATOM_binm, MP4_ReadBoxSkip, 0 },
4857 /* Subtitles */
4858 { ATOM_tx3g, MP4_ReadBox_sample_text, ATOM_sbtl },
4859 { ATOM_tx3g, MP4_ReadBox_sample_text, ATOM_text },
4860 { ATOM_c608, MP4_ReadBox_sample_clcp, ATOM_stsd },
4861 //{ ATOM_text, MP4_ReadBox_sample_text, 0 },
4862 /* In sample WebVTT subtitle atoms. No ATOM_wvtt in normal parsing */
4863 { ATOM_vttc, MP4_ReadBoxContainer, ATOM_wvtt },
4864 { ATOM_payl, MP4_ReadBox_Binary, ATOM_vttc },
4866 /* for codecs */
4867 { ATOM_soun, MP4_ReadBox_sample_soun, ATOM_stsd },
4868 { ATOM_agsm, MP4_ReadBox_sample_soun, ATOM_stsd },
4869 { ATOM_ac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4870 { ATOM_AC3, MP4_ReadBox_sample_soun, ATOM_stsd },
4871 { ATOM_eac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4872 { ATOM_fLaC, MP4_ReadBox_sample_soun, ATOM_stsd },
4873 { ATOM_lpcm, MP4_ReadBox_sample_soun, ATOM_stsd },
4874 { ATOM_ms02, MP4_ReadBox_sample_soun, ATOM_stsd },
4875 { ATOM_ms11, MP4_ReadBox_sample_soun, ATOM_stsd },
4876 { ATOM_ms55, MP4_ReadBox_sample_soun, ATOM_stsd },
4877 { ATOM__mp3, MP4_ReadBox_sample_soun, ATOM_stsd },
4878 { ATOM_mp4a, MP4_ReadBox_sample_soun, ATOM_stsd },
4879 { ATOM_twos, MP4_ReadBox_sample_soun, ATOM_stsd },
4880 { ATOM_sowt, MP4_ReadBox_sample_soun, ATOM_stsd },
4881 { ATOM_QDMC, MP4_ReadBox_sample_soun, ATOM_stsd },
4882 { ATOM_QDM2, MP4_ReadBox_sample_soun, ATOM_stsd },
4883 { ATOM_ima4, MP4_ReadBox_sample_soun, ATOM_stsd },
4884 { ATOM_IMA4, MP4_ReadBox_sample_soun, ATOM_stsd },
4885 { ATOM_dvi, MP4_ReadBox_sample_soun, ATOM_stsd },
4886 { ATOM_alaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4887 { ATOM_ulaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4888 { ATOM_MAC3, MP4_ReadBox_sample_soun, ATOM_stsd },
4889 { ATOM_MAC6, MP4_ReadBox_sample_soun, ATOM_stsd },
4890 { ATOM_Qclp, MP4_ReadBox_sample_soun, ATOM_stsd },
4891 { ATOM_samr, MP4_ReadBox_sample_soun, ATOM_stsd },
4892 { ATOM_sawb, MP4_ReadBox_sample_soun, ATOM_stsd },
4893 { ATOM_OggS, MP4_ReadBox_sample_soun, ATOM_stsd },
4894 { ATOM_alac, MP4_ReadBox_sample_soun, ATOM_stsd },
4895 { ATOM_WMA2, MP4_ReadBox_sample_soun, ATOM_stsd }, /* flip4mac */
4896 { ATOM_wma, MP4_ReadBox_sample_soun, ATOM_stsd }, /* ismv wmapro */
4897 { ATOM_Opus, MP4_ReadBox_sample_soun, ATOM_stsd },
4898 /* Sound extensions */
4899 { ATOM_chan, MP4_ReadBox_stsdext_chan, 0 },
4900 { ATOM_WMA2, MP4_ReadBox_WMA2, ATOM_wave }, /* flip4mac */
4901 { ATOM_dOps, MP4_ReadBox_Binary, ATOM_Opus },
4902 { ATOM_wfex, MP4_ReadBox_WMA2, ATOM_wma }, /* ismv formatex */
4904 /* Both uncompressed sound and video */
4905 { ATOM_raw, MP4_ReadBox_default, ATOM_stsd },
4907 { ATOM_drmi, MP4_ReadBox_sample_vide, ATOM_stsd },
4908 { ATOM_vide, MP4_ReadBox_sample_vide, ATOM_stsd },
4909 { ATOM_mp4v, MP4_ReadBox_sample_vide, ATOM_stsd },
4910 { ATOM_SVQ1, MP4_ReadBox_sample_vide, ATOM_stsd },
4911 { ATOM_SVQ3, MP4_ReadBox_sample_vide, ATOM_stsd },
4912 { ATOM_ZyGo, MP4_ReadBox_sample_vide, ATOM_stsd },
4913 { ATOM_DIVX, MP4_ReadBox_sample_vide, ATOM_stsd },
4914 { ATOM_XVID, MP4_ReadBox_sample_vide, ATOM_stsd },
4915 { ATOM_h263, MP4_ReadBox_sample_vide, ATOM_stsd },
4916 { ATOM_s263, MP4_ReadBox_sample_vide, ATOM_stsd },
4917 { ATOM_cvid, MP4_ReadBox_sample_vide, ATOM_stsd },
4918 { ATOM_3IV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4919 { ATOM_3iv1, MP4_ReadBox_sample_vide, ATOM_stsd },
4920 { ATOM_3IV2, MP4_ReadBox_sample_vide, ATOM_stsd },
4921 { ATOM_3iv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4922 { ATOM_3IVD, MP4_ReadBox_sample_vide, ATOM_stsd },
4923 { ATOM_3ivd, MP4_ReadBox_sample_vide, ATOM_stsd },
4924 { ATOM_3VID, MP4_ReadBox_sample_vide, ATOM_stsd },
4925 { ATOM_3vid, MP4_ReadBox_sample_vide, ATOM_stsd },
4926 { ATOM_FFV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4927 { ATOM_mjpa, MP4_ReadBox_sample_vide, ATOM_stsd },
4928 { ATOM_mjpb, MP4_ReadBox_sample_vide, ATOM_stsd },
4929 { ATOM_qdrw, MP4_ReadBox_sample_vide, ATOM_stsd },
4930 { ATOM_mp2v, MP4_ReadBox_sample_vide, ATOM_stsd },
4931 { ATOM_hdv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4932 { ATOM_WMV3, MP4_ReadBox_sample_vide, ATOM_stsd },
4934 { ATOM_mjqt, MP4_ReadBox_default, 0 }, /* found in mjpa/b */
4935 { ATOM_mjht, MP4_ReadBox_default, 0 },
4937 { ATOM_dvc, MP4_ReadBox_sample_vide, ATOM_stsd },
4938 { ATOM_dvp, MP4_ReadBox_sample_vide, ATOM_stsd },
4939 { ATOM_dv5n, MP4_ReadBox_sample_vide, ATOM_stsd },
4940 { ATOM_dv5p, MP4_ReadBox_sample_vide, ATOM_stsd },
4941 { ATOM_VP31, MP4_ReadBox_sample_vide, ATOM_stsd },
4942 { ATOM_vp31, MP4_ReadBox_sample_vide, ATOM_stsd },
4943 { ATOM_h264, MP4_ReadBox_sample_vide, ATOM_stsd },
4945 { ATOM_jpeg, MP4_ReadBox_sample_vide, ATOM_stsd },
4946 { ATOM_vc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4947 { ATOM_av01, MP4_ReadBox_sample_vide, ATOM_stsd },
4948 { ATOM_avc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4949 { ATOM_avc3, MP4_ReadBox_sample_vide, ATOM_stsd },
4951 { ATOM_rrtp, MP4_ReadBox_sample_hint8, ATOM_stsd },
4953 { ATOM_yv12, MP4_ReadBox_sample_vide, 0 },
4954 { ATOM_yuv2, MP4_ReadBox_sample_vide, 0 },
4956 { ATOM_strf, MP4_ReadBox_strf, ATOM_WVC1 }, /* MS smooth */
4957 { ATOM_strf, MP4_ReadBox_strf, ATOM_H264 }, /* MS smooth */
4959 { ATOM_strf, MP4_ReadBox_strf, ATOM_WMV3 }, /* flip4mac */
4960 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_WMV3 }, /* flip4mac */
4961 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_wave }, /* flip4mac */
4963 { ATOM_mp4s, MP4_ReadBox_sample_mp4s, ATOM_stsd },
4965 /* XXX there is 2 box where we could find this entry stbl and tref*/
4966 { ATOM_hint, MP4_ReadBox_default, ATOM_tref },
4967 { ATOM_hint, MP4_ReadBox_default, ATOM_stbl },
4969 /* found in tref box */
4970 { ATOM_dpnd, MP4_ReadBox_default, ATOM_tref },
4971 { ATOM_ipir, MP4_ReadBox_default, ATOM_tref },
4972 { ATOM_mpod, MP4_ReadBox_default, ATOM_tref },
4973 { ATOM_chap, MP4_ReadBox_tref_generic, ATOM_tref },
4975 /* found in hnti */
4976 { ATOM_rtp, MP4_ReadBox_rtp, ATOM_hnti },
4977 { ATOM_sdp, MP4_ReadBox_sdp, ATOM_hnti },
4979 /* found in rrtp sample description */
4980 { ATOM_tims, MP4_ReadBox_tims, 0 },
4981 { ATOM_tsro, MP4_ReadBox_tsro, 0 },
4982 { ATOM_tssy, MP4_ReadBox_tssy, 0 },
4984 /* found in rmra/rmda */
4985 { ATOM_rdrf, MP4_ReadBox_rdrf, ATOM_rmda },
4986 { ATOM_rmdr, MP4_ReadBox_rmdr, ATOM_rmda },
4987 { ATOM_rmqu, MP4_ReadBox_rmqu, ATOM_rmda },
4988 { ATOM_rmvc, MP4_ReadBox_rmvc, ATOM_rmda },
4990 { ATOM_drms, MP4_ReadBox_sample_soun, 0 },
4991 { ATOM_sinf, MP4_ReadBoxContainer, 0 },
4992 { ATOM_schi, MP4_ReadBoxContainer, 0 },
4993 { ATOM_user, MP4_ReadBox_drms, 0 },
4994 { ATOM_key, MP4_ReadBox_drms, 0 },
4995 { ATOM_iviv, MP4_ReadBox_drms, 0 },
4996 { ATOM_priv, MP4_ReadBox_drms, 0 },
4997 { ATOM_frma, MP4_ReadBox_frma, ATOM_sinf }, /* and rinf */
4998 { ATOM_frma, MP4_ReadBox_frma, ATOM_wave }, /* flip4mac */
4999 { ATOM_skcr, MP4_ReadBox_skcr, 0 },
5001 /* ilst meta tags */
5002 { ATOM_0xa9ART, MP4_ReadBox_Metadata, ATOM_ilst },
5003 { ATOM_0xa9alb, MP4_ReadBox_Metadata, ATOM_ilst },
5004 { ATOM_0xa9cmt, MP4_ReadBox_Metadata, ATOM_ilst },
5005 { ATOM_0xa9com, MP4_ReadBox_Metadata, ATOM_ilst },
5006 { ATOM_0xa9cpy, MP4_ReadBox_Metadata, ATOM_ilst },
5007 { ATOM_0xa9day, MP4_ReadBox_Metadata, ATOM_ilst },
5008 { ATOM_0xa9des, MP4_ReadBox_Metadata, ATOM_ilst },
5009 { ATOM_0xa9enc, MP4_ReadBox_Metadata, ATOM_ilst },
5010 { ATOM_0xa9gen, MP4_ReadBox_Metadata, ATOM_ilst },
5011 { ATOM_0xa9grp, MP4_ReadBox_Metadata, ATOM_ilst },
5012 { ATOM_0xa9lyr, MP4_ReadBox_Metadata, ATOM_ilst },
5013 { ATOM_0xa9nam, MP4_ReadBox_Metadata, ATOM_ilst },
5014 { ATOM_0xa9too, MP4_ReadBox_Metadata, ATOM_ilst },
5015 { ATOM_0xa9trk, MP4_ReadBox_Metadata, ATOM_ilst },
5016 { ATOM_0xa9wrt, MP4_ReadBox_Metadata, ATOM_ilst },
5017 { ATOM_aART, MP4_ReadBox_Metadata, ATOM_ilst },
5018 { ATOM_atID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
5019 { ATOM_cnID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
5020 { ATOM_covr, MP4_ReadBoxContainer, ATOM_ilst },
5021 { ATOM_desc, MP4_ReadBox_Metadata, ATOM_ilst },
5022 { ATOM_disk, MP4_ReadBox_Metadata, ATOM_ilst },
5023 { ATOM_flvr, MP4_ReadBox_Metadata, ATOM_ilst },
5024 { ATOM_gnre, MP4_ReadBox_Metadata, ATOM_ilst },
5025 { ATOM_rtng, MP4_ReadBox_Metadata, ATOM_ilst },
5026 { ATOM_trkn, MP4_ReadBox_Metadata, ATOM_ilst },
5027 { ATOM_xid_, MP4_ReadBox_Metadata, ATOM_ilst },
5028 { ATOM_gshh, MP4_ReadBox_Metadata, ATOM_ilst }, /* YouTube gs?? */
5029 { ATOM_gspm, MP4_ReadBox_Metadata, ATOM_ilst },
5030 { ATOM_gspu, MP4_ReadBox_Metadata, ATOM_ilst },
5031 { ATOM_gssd, MP4_ReadBox_Metadata, ATOM_ilst },
5032 { ATOM_gsst, MP4_ReadBox_Metadata, ATOM_ilst },
5033 { ATOM_gstd, MP4_ReadBox_Metadata, ATOM_ilst },
5034 { ATOM_ITUN, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunesInfo */
5036 /* udta */
5037 { ATOM_0x40PRM, MP4_ReadBox_Binary, ATOM_udta },
5038 { ATOM_0x40PRQ, MP4_ReadBox_Binary, ATOM_udta },
5039 { ATOM_0xa9ART, MP4_ReadBox_Binary, ATOM_udta },
5040 { ATOM_0xa9alb, MP4_ReadBox_Binary, ATOM_udta },
5041 { ATOM_0xa9ard, MP4_ReadBox_Binary, ATOM_udta },
5042 { ATOM_0xa9arg, MP4_ReadBox_Binary, ATOM_udta },
5043 { ATOM_0xa9aut, MP4_ReadBox_Binary, ATOM_udta },
5044 { ATOM_0xa9cak, MP4_ReadBox_Binary, ATOM_udta },
5045 { ATOM_0xa9cmt, MP4_ReadBox_Binary, ATOM_udta },
5046 { ATOM_0xa9con, MP4_ReadBox_Binary, ATOM_udta },
5047 { ATOM_0xa9com, MP4_ReadBox_Binary, ATOM_udta },
5048 { ATOM_0xa9cpy, MP4_ReadBox_Binary, ATOM_udta },
5049 { ATOM_0xa9day, MP4_ReadBox_Binary, ATOM_udta },
5050 { ATOM_0xa9des, MP4_ReadBox_Binary, ATOM_udta },
5051 { ATOM_0xa9dir, MP4_ReadBox_Binary, ATOM_udta },
5052 { ATOM_0xa9dis, MP4_ReadBox_Binary, ATOM_udta },
5053 { ATOM_0xa9dsa, MP4_ReadBox_Binary, ATOM_udta },
5054 { ATOM_0xa9fmt, MP4_ReadBox_Binary, ATOM_udta },
5055 { ATOM_0xa9gen, MP4_ReadBox_Binary, ATOM_udta },
5056 { ATOM_0xa9grp, MP4_ReadBox_Binary, ATOM_udta },
5057 { ATOM_0xa9hst, MP4_ReadBox_Binary, ATOM_udta },
5058 { ATOM_0xa9inf, MP4_ReadBox_Binary, ATOM_udta },
5059 { ATOM_0xa9isr, MP4_ReadBox_Binary, ATOM_udta },
5060 { ATOM_0xa9lab, MP4_ReadBox_Binary, ATOM_udta },
5061 { ATOM_0xa9lal, MP4_ReadBox_Binary, ATOM_udta },
5062 { ATOM_0xa9lnt, MP4_ReadBox_Binary, ATOM_udta },
5063 { ATOM_0xa9lyr, MP4_ReadBox_Binary, ATOM_udta },
5064 { ATOM_0xa9mak, MP4_ReadBox_Binary, ATOM_udta },
5065 { ATOM_0xa9mal, MP4_ReadBox_Binary, ATOM_udta },
5066 { ATOM_0xa9mod, MP4_ReadBox_Binary, ATOM_udta },
5067 { ATOM_0xa9nam, MP4_ReadBox_Binary, ATOM_udta },
5068 { ATOM_0xa9ope, MP4_ReadBox_Binary, ATOM_udta },
5069 { ATOM_0xa9phg, MP4_ReadBox_Binary, ATOM_udta },
5070 { ATOM_0xa9PRD, MP4_ReadBox_Binary, ATOM_udta },
5071 { ATOM_0xa9prd, MP4_ReadBox_Binary, ATOM_udta },
5072 { ATOM_0xa9prf, MP4_ReadBox_Binary, ATOM_udta },
5073 { ATOM_0xa9pub, MP4_ReadBox_Binary, ATOM_udta },
5074 { ATOM_0xa9req, MP4_ReadBox_Binary, ATOM_udta },
5075 { ATOM_0xa9sne, MP4_ReadBox_Binary, ATOM_udta },
5076 { ATOM_0xa9snm, MP4_ReadBox_Binary, ATOM_udta },
5077 { ATOM_0xa9sol, MP4_ReadBox_Binary, ATOM_udta },
5078 { ATOM_0xa9src, MP4_ReadBox_Binary, ATOM_udta },
5079 { ATOM_0xa9st3, MP4_ReadBox_Binary, ATOM_udta },
5080 { ATOM_0xa9swr, MP4_ReadBox_Binary, ATOM_udta },
5081 { ATOM_0xa9thx, MP4_ReadBox_Binary, ATOM_udta },
5082 { ATOM_0xa9too, MP4_ReadBox_Binary, ATOM_udta },
5083 { ATOM_0xa9trk, MP4_ReadBox_Binary, ATOM_udta },
5084 { ATOM_0xa9url, MP4_ReadBox_Binary, ATOM_udta },
5085 { ATOM_0xa9wrn, MP4_ReadBox_Binary, ATOM_udta },
5086 { ATOM_0xa9xpd, MP4_ReadBox_Binary, ATOM_udta },
5087 { ATOM_0xa9xyz, MP4_ReadBox_Binary, ATOM_udta },
5088 { ATOM_chpl, MP4_ReadBox_chpl, ATOM_udta }, /* nero unlabeled chapters list */
5089 { ATOM_MCPS, MP4_ReadBox_Binary, ATOM_udta },
5090 { ATOM_name, MP4_ReadBox_Binary, ATOM_udta },
5091 { ATOM_vndr, MP4_ReadBox_Binary, ATOM_udta },
5092 { ATOM_SDLN, MP4_ReadBox_Binary, ATOM_udta },
5093 { ATOM_HMMT, MP4_ReadBox_HMMT, ATOM_udta }, /* GoPro HiLight tags */
5095 /* udta, non meta */
5096 { ATOM_tsel, MP4_ReadBox_tsel, ATOM_udta },
5098 /* iTunes/Quicktime meta info */
5099 { ATOM_meta, MP4_ReadBox_meta, 0 },
5100 { ATOM_ID32, MP4_ReadBox_Binary, ATOM_meta }, /* ID3v2 in 3GPP / ETSI TS 126 244 8.3 */
5101 { ATOM_data, MP4_ReadBox_data, 0 }, /* ilst/@too and others, ITUN/data */
5102 { ATOM_mean, MP4_ReadBox_Binary, ATOM_ITUN },
5103 { ATOM_name, MP4_ReadBox_Binary, ATOM_ITUN },
5105 /* found in smoothstreaming */
5106 { ATOM_traf, MP4_ReadBoxContainer, ATOM_moof },
5107 { ATOM_mfra, MP4_ReadBoxContainer, 0 },
5108 { ATOM_mfhd, MP4_ReadBox_mfhd, ATOM_moof },
5109 { ATOM_sidx, MP4_ReadBox_sidx, 0 },
5110 { ATOM_tfhd, MP4_ReadBox_tfhd, ATOM_traf },
5111 { ATOM_trun, MP4_ReadBox_trun, ATOM_traf },
5112 { ATOM_tfdt, MP4_ReadBox_tfdt, ATOM_traf },
5113 { ATOM_trex, MP4_ReadBox_trex, ATOM_mvex },
5114 { ATOM_mehd, MP4_ReadBox_mehd, ATOM_mvex },
5115 { ATOM_sdtp, MP4_ReadBox_sdtp, 0 },
5116 { ATOM_tfra, MP4_ReadBox_tfra, ATOM_mfra },
5117 { ATOM_mfro, MP4_ReadBox_mfro, ATOM_mfra },
5118 { ATOM_uuid, MP4_ReadBox_uuid, 0 },
5120 /* spatial/360°/VR */
5121 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_avc1 },
5122 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_mp4v },
5123 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_avc1 },
5124 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_mp4v },
5125 { ATOM_proj, MP4_ReadBoxContainer, ATOM_sv3d },
5126 { ATOM_prhd, MP4_ReadBox_prhd, ATOM_proj },
5127 { ATOM_equi, MP4_ReadBox_equi, ATOM_proj },
5128 { ATOM_cbmp, MP4_ReadBox_cbmp, ATOM_proj },
5130 /* Ambisonics */
5131 { ATOM_SA3D, MP4_ReadBox_SA3D, 0 },
5133 /* iso4 brand meta references */
5134 { ATOM_iloc, MP4_ReadBox_iloc, ATOM_meta },
5135 { ATOM_iinf, MP4_ReadBox_iinf, ATOM_meta },
5136 { ATOM_infe, MP4_ReadBox_infe, ATOM_iinf },
5137 { ATOM_iref, MP4_ReadBox_iref, ATOM_meta },
5138 { ATOM_pitm, MP4_ReadBox_pitm, ATOM_meta },
5140 /* HEIF specific meta references */
5141 { ATOM_iprp, MP4_ReadBoxContainer, ATOM_meta },
5142 { ATOM_ipco, MP4_ReadBoxContainer, ATOM_iprp },
5143 { ATOM_ispe, MP4_ReadBox_ispe, ATOM_ipco },
5144 { ATOM_ipma, MP4_ReadBox_ipma, ATOM_iprp },
5146 /* Last entry */
5147 { 0, MP4_ReadBox_default, 0 }
5150 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
5152 int i_index;
5154 for( i_index = 0; ; i_index++ )
5156 if ( MP4_Box_Function[i_index].i_parent &&
5157 p_father && p_father->i_type != MP4_Box_Function[i_index].i_parent )
5158 continue;
5160 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
5161 ( MP4_Box_Function[i_index].i_type == 0 ) )
5163 break;
5167 if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
5169 return VLC_EGENERIC;
5172 return VLC_SUCCESS;
5175 static MP4_Box_t *MP4_ReadBoxAllocateCheck( stream_t *p_stream, MP4_Box_t *p_father )
5177 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
5178 if( p_box == NULL )
5179 return NULL;
5181 if( !MP4_PeekBoxHeader( p_stream, p_box ) )
5183 msg_Warn( p_stream, "cannot read one box" );
5184 free( p_box );
5185 return NULL;
5188 if( p_father && p_father->i_size > 0 &&
5189 p_father->i_pos + p_father->i_size < p_box->i_pos + p_box->i_size )
5191 msg_Dbg( p_stream, "out of bound child" );
5192 free( p_box );
5193 return NULL;
5196 if( !p_box->i_size )
5198 msg_Dbg( p_stream, "found an empty box (null size)" );
5199 free( p_box );
5200 return NULL;
5202 p_box->p_father = p_father;
5204 return p_box;
5207 /*****************************************************************************
5208 * MP4_ReadBoxUsing : parse the actual box and the children using handler
5209 *****************************************************************************/
5210 static MP4_Box_t *MP4_ReadBoxUsing( stream_t *p_stream, MP4_Box_t *p_father,
5211 int(*MP4_ReadBox_function)(stream_t *, MP4_Box_t *) )
5213 MP4_Box_t *p_box = MP4_ReadBoxAllocateCheck( p_stream, p_father );
5214 if( !p_box )
5215 return NULL;
5217 if( MP4_ReadBox_function( p_stream, p_box ) != 1 )
5219 uint64_t i_end = p_box->i_pos + p_box->i_size;
5220 MP4_BoxFree( p_box );
5221 MP4_Seek( p_stream, i_end ); /* Skip the failed box */
5222 return NULL;
5224 return p_box;
5227 /*****************************************************************************
5228 * MP4_ReadBox : parse the actual box and the children
5229 * XXX : Do not go to the next box
5230 *****************************************************************************/
5231 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
5233 MP4_Box_t *p_box = MP4_ReadBoxAllocateCheck( p_stream, p_father );
5234 if( !p_box )
5235 return NULL;
5237 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
5239 uint64_t i_end = p_box->i_pos + p_box->i_size;
5240 MP4_BoxFree( p_box );
5241 MP4_Seek( p_stream, i_end ); /* Skip the failed box */
5242 return NULL;
5244 return p_box;
5247 /*****************************************************************************
5248 * MP4_BoxNew : creates and initializes an arbitrary box
5249 *****************************************************************************/
5250 MP4_Box_t * MP4_BoxNew( uint32_t i_type )
5252 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) );
5253 if( likely( p_box != NULL ) )
5255 p_box->i_type = i_type;
5257 return p_box;
5260 /*****************************************************************************
5261 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
5262 * the children
5263 *****************************************************************************/
5264 void MP4_BoxFree( MP4_Box_t *p_box )
5266 MP4_Box_t *p_child;
5268 if( !p_box )
5269 return; /* hehe */
5271 for( p_child = p_box->p_first; p_child != NULL; )
5273 MP4_Box_t *p_next;
5275 p_next = p_child->p_next;
5276 MP4_BoxFree( p_child );
5277 p_child = p_next;
5280 if( p_box->pf_free )
5281 p_box->pf_free( p_box );
5283 free( p_box->data.p_payload );
5284 free( p_box );
5287 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
5289 /* p_chunk is a virtual root container for the moof and mdat boxes */
5290 MP4_Box_t *p_fakeroot;
5291 MP4_Box_t *p_tmp_box;
5293 p_fakeroot = MP4_BoxNew( ATOM_root );
5294 if( unlikely( p_fakeroot == NULL ) )
5295 return NULL;
5296 p_fakeroot->i_shortsize = 1;
5298 const uint32_t stoplist[] = { ATOM_moov, ATOM_moof, 0 };
5299 MP4_ReadBoxContainerChildren( s, p_fakeroot, stoplist );
5301 p_tmp_box = p_fakeroot->p_first;
5302 if( p_tmp_box == NULL )
5304 MP4_BoxFree( p_fakeroot );
5305 return NULL;
5307 else while( p_tmp_box )
5309 p_fakeroot->i_size += p_tmp_box->i_size;
5310 p_tmp_box = p_tmp_box->p_next;
5313 return p_fakeroot;
5316 /*****************************************************************************
5317 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
5318 *****************************************************************************
5319 * The first box is a virtual box "root" and is the father for all first
5320 * level boxes for the file, a sort of virtual contener
5321 *****************************************************************************/
5322 MP4_Box_t *MP4_BoxGetRoot( stream_t *p_stream )
5324 int i_result;
5326 MP4_Box_t *p_vroot = MP4_BoxNew( ATOM_root );
5327 if( p_vroot == NULL )
5328 return NULL;
5330 p_vroot->i_shortsize = 1;
5331 uint64_t i_size;
5332 if( vlc_stream_GetSize( p_stream, &i_size ) == 0 )
5333 p_vroot->i_size = i_size;
5335 /* First get the moov */
5337 const uint32_t stoplist[] = { ATOM_moov, ATOM_mdat, 0 };
5338 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
5341 /* mdat appeared first */
5342 if( i_result && !MP4_BoxGet( p_vroot, "moov" ) )
5344 bool b_seekable;
5345 if( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_seekable ) != VLC_SUCCESS || !b_seekable )
5347 msg_Err( p_stream, "no moov before mdat and the stream is not seekable" );
5348 goto error;
5351 /* continue loading up to moov */
5352 const uint32_t stoplist[] = { ATOM_moov, 0 };
5353 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
5356 if( !i_result )
5357 return p_vroot;
5359 /* If there is a mvex box, it means fragmented MP4, and we're done */
5360 if( MP4_BoxCount( p_vroot, "moov/mvex" ) > 0 )
5362 /* Read a bit more atoms as we might have an index between moov and moof */
5363 const uint32_t stoplist[] = { ATOM_sidx, 0 };
5364 const uint32_t excludelist[] = { ATOM_moof, ATOM_mdat, 0 };
5365 MP4_ReadBoxContainerChildrenIndexed( p_stream, p_vroot, stoplist, excludelist, false );
5366 return p_vroot;
5369 if( vlc_stream_Tell( p_stream ) + 8 < (uint64_t) stream_Size( p_stream ) )
5371 /* Get the rest of the file */
5372 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, NULL );
5374 if( !i_result )
5375 goto error;
5378 MP4_Box_t *p_moov;
5379 MP4_Box_t *p_cmov;
5381 /* check if there is a cmov, if so replace
5382 compressed moov by uncompressed one */
5383 if( ( ( p_moov = MP4_BoxGet( p_vroot, "moov" ) ) &&
5384 ( p_cmov = MP4_BoxGet( p_vroot, "moov/cmov" ) ) ) ||
5385 ( ( p_moov = MP4_BoxGet( p_vroot, "foov" ) ) &&
5386 ( p_cmov = MP4_BoxGet( p_vroot, "foov/cmov" ) ) ) )
5388 /* rename the compressed moov as a box to skip */
5389 p_moov->i_type = ATOM_skip;
5391 /* get uncompressed p_moov */
5392 p_moov = p_cmov->data.p_cmov->p_moov;
5393 p_cmov->data.p_cmov->p_moov = NULL;
5395 /* make p_root father of this new moov */
5396 p_moov->p_father = p_vroot;
5398 /* insert this new moov box as first child of p_root */
5399 p_moov->p_next = p_vroot->p_first;
5400 p_vroot->p_first = p_moov;
5403 return p_vroot;
5405 error:
5406 MP4_BoxFree( p_vroot );
5407 MP4_Seek( p_stream, 0 );
5408 return NULL;
5412 static void MP4_BoxDumpStructure_Internal( stream_t *s, const MP4_Box_t *p_box,
5413 unsigned int i_level )
5415 const MP4_Box_t *p_child;
5416 uint32_t i_displayedtype = p_box->i_type;
5417 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype)[0] = 'c';
5419 if( !i_level )
5421 msg_Dbg( s, "dumping root Box \"%4.4s\"",
5422 (char*)&i_displayedtype );
5424 else
5426 char str[512];
5427 if( i_level >= (sizeof(str) - 1)/4 )
5428 return;
5430 memset( str, ' ', sizeof(str) );
5431 for( unsigned i = 0; i < i_level; i++ )
5433 str[i*4] = '|';
5436 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
5437 "+ %4.4s size %"PRIu64" offset %" PRIuMAX "%s",
5438 (char*)&i_displayedtype, p_box->i_size,
5439 (uintmax_t)p_box->i_pos,
5440 p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
5441 msg_Dbg( s, "%s", str );
5443 p_child = p_box->p_first;
5444 while( p_child )
5446 MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
5447 p_child = p_child->p_next;
5451 void MP4_BoxDumpStructure( stream_t *s, const MP4_Box_t *p_box )
5453 MP4_BoxDumpStructure_Internal( s, p_box, 0 );
5457 /*****************************************************************************
5458 *****************************************************************************
5460 ** High level methods to acces an MP4 file
5462 *****************************************************************************
5463 *****************************************************************************/
5464 static bool get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
5466 size_t i_len ;
5467 if( !*ppsz_path[0] )
5469 *ppsz_token = NULL;
5470 *pi_number = 0;
5471 return true;
5473 i_len = strcspn( *ppsz_path, "/[" );
5474 if( !i_len && **ppsz_path == '/' )
5476 i_len = 1;
5478 *ppsz_token = strndup( *ppsz_path, i_len );
5479 if( unlikely(!*ppsz_token) )
5480 return false;
5482 *ppsz_path += i_len;
5484 /* Parse the token number token[n] */
5485 if( **ppsz_path == '[' )
5487 (*ppsz_path)++;
5488 *pi_number = strtol( *ppsz_path, NULL, 10 );
5489 while( **ppsz_path && **ppsz_path != ']' )
5491 (*ppsz_path)++;
5493 if( **ppsz_path == ']' )
5495 (*ppsz_path)++;
5498 else
5500 *pi_number = 0;
5503 /* Forward to start of next token */
5504 while( **ppsz_path == '/' )
5506 (*ppsz_path)++;
5509 return true;
5512 static void MP4_BoxGet_Internal( const MP4_Box_t **pp_result, const MP4_Box_t *p_box,
5513 const char *psz_fmt, va_list args)
5515 char *psz_dup;
5516 char *psz_path;
5517 char *psz_token = NULL;
5519 if( !p_box )
5521 *pp_result = NULL;
5522 return;
5525 if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
5526 psz_path = NULL;
5528 if( !psz_path || !psz_path[0] )
5530 free( psz_path );
5531 *pp_result = NULL;
5532 return;
5535 // fprintf( stderr, "path:'%s'\n", psz_path );
5536 psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
5537 for( ; ; )
5539 int i_number;
5541 if( !get_token( &psz_path, &psz_token, &i_number ) )
5542 goto error_box;
5543 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
5544 // psz_path,psz_token,i_number );
5545 if( !psz_token )
5547 free( psz_dup );
5548 *pp_result = p_box;
5549 return;
5551 else
5552 if( !strcmp( psz_token, "/" ) )
5554 /* Find root box */
5555 while( p_box && p_box->i_type != ATOM_root )
5557 p_box = p_box->p_father;
5559 if( !p_box )
5561 goto error_box;
5564 else
5565 if( !strcmp( psz_token, "." ) )
5567 /* Do nothing */
5569 else
5570 if( !strcmp( psz_token, ".." ) )
5572 p_box = p_box->p_father;
5573 if( !p_box )
5575 goto error_box;
5578 else
5579 if( strlen( psz_token ) == 4 )
5581 uint32_t i_fourcc;
5582 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
5583 psz_token[2], psz_token[3] );
5584 p_box = p_box->p_first;
5585 for( ; ; )
5587 if( !p_box )
5589 goto error_box;
5591 if( p_box->i_type == i_fourcc )
5593 if( !i_number )
5595 break;
5597 i_number--;
5599 p_box = p_box->p_next;
5602 else
5603 if( *psz_token == '\0' )
5605 p_box = p_box->p_first;
5606 for( ; ; )
5608 if( !p_box )
5610 goto error_box;
5612 if( !i_number )
5614 break;
5616 i_number--;
5617 p_box = p_box->p_next;
5620 else
5622 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
5623 goto error_box;
5626 free( psz_token );
5629 return;
5631 error_box:
5632 free( psz_token );
5633 free( psz_dup );
5634 *pp_result = NULL;
5635 return;
5638 /*****************************************************************************
5639 * MP4_BoxGet: find a box given a path relative to p_box
5640 *****************************************************************************
5641 * Path Format: . .. / as usual
5642 * [number] to specifie box number ex: trak[12]
5644 * ex: /moov/trak[12]
5645 * ../mdia
5646 *****************************************************************************/
5647 VLC_FORMAT(2, 3)
5648 MP4_Box_t *MP4_BoxGet( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5650 va_list args;
5651 const MP4_Box_t *p_result;
5653 va_start( args, psz_fmt );
5654 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5655 va_end( args );
5657 return( (MP4_Box_t *) p_result );
5660 /*****************************************************************************
5661 * MP4_BoxCount: count box given a path relative to p_box
5662 *****************************************************************************
5663 * Path Format: . .. / as usual
5664 * [number] to specifie box number ex: trak[12]
5666 * ex: /moov/trak[12]
5667 * ../mdia
5668 *****************************************************************************/
5669 VLC_FORMAT(2, 3)
5670 unsigned MP4_BoxCount( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5672 va_list args;
5673 unsigned i_count;
5674 const MP4_Box_t *p_result, *p_next;
5676 va_start( args, psz_fmt );
5677 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5678 va_end( args );
5679 if( !p_result )
5681 return( 0 );
5684 i_count = 1;
5685 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
5687 if( p_next->i_type == p_result->i_type)
5689 i_count++;
5692 return( i_count );