demux: libmp4: SmDm is fixed point
[vlc.git] / modules / demux / mp4 / libmp4.c
blobf3c7b58676a7d537e1c08f0c432e85fabdf33594
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_avcC( MP4_Box_t *p_box )
1825 MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1826 free( p_avcC->p_avcC );
1829 static int MP4_ReadBox_avcC( stream_t *p_stream, MP4_Box_t *p_box )
1831 MP4_Box_data_avcC_t *p_avcC;
1833 MP4_READBOX_ENTER( MP4_Box_data_avcC_t, MP4_FreeBox_avcC );
1834 p_avcC = p_box->data.p_avcC;
1836 p_avcC->i_avcC = i_read;
1837 if( p_avcC->i_avcC > 0 )
1839 uint8_t * p = p_avcC->p_avcC = malloc( p_avcC->i_avcC );
1840 if( p )
1841 memcpy( p, p_peek, i_read );
1844 MP4_GET1BYTE( p_avcC->i_version );
1845 MP4_GET1BYTE( p_avcC->i_profile );
1846 MP4_GET1BYTE( p_avcC->i_profile_compatibility );
1847 MP4_GET1BYTE( p_avcC->i_level );
1848 #ifdef MP4_VERBOSE
1849 msg_Dbg( p_stream,
1850 "read box: \"avcC\" version=%d profile=0x%x level=0x%x",
1851 p_avcC->i_version, p_avcC->i_profile, p_avcC->i_level );
1852 #endif
1853 MP4_READBOX_EXIT( 1 );
1856 static void MP4_FreeBox_vpcC( MP4_Box_t *p_box )
1858 free( p_box->data.p_vpcC->p_codec_init_data );
1861 static int MP4_ReadBox_vpcC( stream_t *p_stream, MP4_Box_t *p_box )
1863 MP4_READBOX_ENTER( MP4_Box_data_vpcC_t, MP4_FreeBox_vpcC );
1864 MP4_Box_data_vpcC_t *p_vpcC = p_box->data.p_vpcC;
1866 if( p_box->i_size < 6 )
1867 MP4_READBOX_EXIT( 0 );
1869 MP4_GET1BYTE( p_vpcC->i_version );
1870 if( p_vpcC->i_version > 1 )
1871 MP4_READBOX_EXIT( 0 );
1873 MP4_GET1BYTE( p_vpcC->i_profile );
1874 MP4_GET1BYTE( p_vpcC->i_level );
1875 MP4_GET1BYTE( p_vpcC->i_bit_depth );
1877 /* Deprecated one
1878 https://github.com/webmproject/vp9-dash/blob/master/archive/VPCodecISOMediaFileFormatBinding-v0.docx */
1879 if( p_vpcC->i_version == 0 )
1881 p_vpcC->i_color_primaries = p_vpcC->i_bit_depth & 0x0F;
1882 p_vpcC->i_bit_depth >>= 4;
1883 MP4_GET1BYTE( p_vpcC->i_chroma_subsampling );
1884 p_vpcC->i_xfer_function = ( p_vpcC->i_chroma_subsampling & 0x0F ) >> 1;
1885 p_vpcC->i_fullrange = p_vpcC->i_chroma_subsampling & 0x01;
1886 p_vpcC->i_chroma_subsampling >>= 4;
1888 else
1890 p_vpcC->i_chroma_subsampling = ( p_vpcC->i_bit_depth & 0x0F ) >> 1;
1891 p_vpcC->i_fullrange = p_vpcC->i_bit_depth & 0x01;
1892 p_vpcC->i_bit_depth >>= 4;
1893 MP4_GET1BYTE( p_vpcC->i_color_primaries );
1894 MP4_GET1BYTE( p_vpcC->i_xfer_function );
1895 MP4_GET1BYTE( p_vpcC->i_matrix_coeffs );
1898 MP4_GET2BYTES( p_vpcC->i_codec_init_datasize );
1899 if( p_vpcC->i_codec_init_datasize > i_read )
1900 p_vpcC->i_codec_init_datasize = i_read;
1902 if( p_vpcC->i_codec_init_datasize )
1904 p_vpcC->p_codec_init_data = malloc( i_read );
1905 if( !p_vpcC->p_codec_init_data )
1906 MP4_READBOX_EXIT( 0 );
1907 memcpy( p_vpcC->p_codec_init_data, p_peek, i_read );
1910 MP4_READBOX_EXIT( 1 );
1913 static int MP4_ReadBox_SmDm( stream_t *p_stream, MP4_Box_t *p_box )
1915 MP4_READBOX_ENTER( MP4_Box_data_SmDm_t, NULL );
1916 MP4_Box_data_SmDm_t *p_SmDm = p_box->data.p_SmDm;
1918 /* SmDm: version/flags RGB */
1919 /* mdcv: version/flags GBR or not */
1920 if( p_box->i_type != ATOM_mdcv )
1922 uint8_t i_version;
1923 uint32_t i_flags;
1924 MP4_GET1BYTE( i_version );
1925 MP4_GET3BYTES( i_flags );
1926 VLC_UNUSED(i_flags);
1927 if( i_version != 0 )
1928 MP4_READBOX_EXIT( 0 );
1931 const uint8_t RGB2GBR[3] = {2,0,1};
1932 for(int i=0; i<6; i++)
1934 int index = (p_box->i_type != ATOM_mdcv) ? RGB2GBR[i/2] + i%2 : i;
1935 MP4_GET2BYTES( p_SmDm->primaries[index] );
1937 /* convert from fixed point to 0.00002 resolution */
1938 if(p_box->i_type != ATOM_mdcv)
1939 p_SmDm->primaries[index] = 50000 *
1940 (double)p_SmDm->primaries[index] / (double)(1<<16);
1942 for(int i=0; i<2; i++)
1944 MP4_GET2BYTES( p_SmDm->white_point[i] );
1945 if(p_box->i_type != ATOM_mdcv)
1946 p_SmDm->white_point[i] = 50000 *
1947 (double)p_SmDm->white_point[i] / (double)(1<<16);
1950 MP4_GET4BYTES( p_SmDm->i_luminanceMax );
1951 MP4_GET4BYTES( p_SmDm->i_luminanceMin );
1952 if(p_box->i_type != ATOM_mdcv)
1954 p_SmDm->i_luminanceMax = 10000 *
1955 (double)p_SmDm->i_luminanceMax / (double) (1<<8);
1956 p_SmDm->i_luminanceMin = 10000 *
1957 (double)p_SmDm->i_luminanceMin / (double) (1<<14);
1960 MP4_READBOX_EXIT( 1 );
1963 static int MP4_ReadBox_CoLL( stream_t *p_stream, MP4_Box_t *p_box )
1965 MP4_READBOX_ENTER( MP4_Box_data_CoLL_t, NULL );
1966 MP4_Box_data_CoLL_t *p_CoLL = p_box->data.p_CoLL;
1968 if( p_box->i_type != ATOM_clli )
1970 uint8_t i_version;
1971 uint32_t i_flags;
1972 MP4_GET1BYTE( i_version );
1973 MP4_GET3BYTES( i_flags );
1974 VLC_UNUSED(i_flags);
1975 if( i_version != 0 )
1976 MP4_READBOX_EXIT( 0 );
1979 MP4_GET2BYTES( p_CoLL->i_maxCLL );
1980 MP4_GET2BYTES( p_CoLL->i_maxFALL );
1981 MP4_READBOX_EXIT( 1 );
1984 static void MP4_FreeBox_WMA2( MP4_Box_t *p_box )
1986 free( p_box->data.p_WMA2->p_extra );
1989 static int MP4_ReadBox_WMA2( stream_t *p_stream, MP4_Box_t *p_box )
1991 MP4_READBOX_ENTER( MP4_Box_data_WMA2_t, MP4_FreeBox_WMA2 );
1993 MP4_Box_data_WMA2_t *p_WMA2 = p_box->data.p_WMA2;
1995 MP4_GET2BYTESLE( p_WMA2->Format.wFormatTag );
1996 MP4_GET2BYTESLE( p_WMA2->Format.nChannels );
1997 MP4_GET4BYTESLE( p_WMA2->Format.nSamplesPerSec );
1998 MP4_GET4BYTESLE( p_WMA2->Format.nAvgBytesPerSec );
1999 MP4_GET2BYTESLE( p_WMA2->Format.nBlockAlign );
2000 MP4_GET2BYTESLE( p_WMA2->Format.wBitsPerSample );
2002 uint16_t i_cbSize;
2003 MP4_GET2BYTESLE( i_cbSize );
2005 if( i_cbSize > i_read )
2006 goto error;
2008 p_WMA2->i_extra = i_cbSize;
2009 if ( p_WMA2->i_extra )
2011 p_WMA2->p_extra = malloc( p_WMA2->i_extra );
2012 if ( ! p_WMA2->p_extra )
2013 goto error;
2014 memcpy( p_WMA2->p_extra, p_peek, p_WMA2->i_extra );
2017 MP4_READBOX_EXIT( 1 );
2019 error:
2020 MP4_READBOX_EXIT( 0 );
2023 static void MP4_FreeBox_strf( MP4_Box_t *p_box )
2025 free( p_box->data.p_strf->p_extra );
2028 static int MP4_ReadBox_strf( stream_t *p_stream, MP4_Box_t *p_box )
2030 MP4_READBOX_ENTER( MP4_Box_data_strf_t, MP4_FreeBox_strf );
2032 MP4_Box_data_strf_t *p_strf = p_box->data.p_strf;
2034 if( i_read < 40 )
2035 goto error;
2037 MP4_GET4BYTESLE( p_strf->bmiHeader.biSize );
2038 MP4_GET4BYTESLE( p_strf->bmiHeader.biWidth );
2039 MP4_GET4BYTESLE( p_strf->bmiHeader.biHeight );
2040 MP4_GET2BYTESLE( p_strf->bmiHeader.biPlanes );
2041 MP4_GET2BYTESLE( p_strf->bmiHeader.biBitCount );
2042 MP4_GETFOURCC( p_strf->bmiHeader.biCompression );
2043 MP4_GET4BYTESLE( p_strf->bmiHeader.biSizeImage );
2044 MP4_GET4BYTESLE( p_strf->bmiHeader.biXPelsPerMeter );
2045 MP4_GET4BYTESLE( p_strf->bmiHeader.biYPelsPerMeter );
2046 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrUsed );
2047 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrImportant );
2049 p_strf->i_extra = i_read;
2050 if ( p_strf->i_extra )
2052 p_strf->p_extra = malloc( p_strf->i_extra );
2053 if ( ! p_strf->p_extra )
2054 goto error;
2055 memcpy( p_strf->p_extra, p_peek, i_read );
2058 MP4_READBOX_EXIT( 1 );
2060 error:
2061 MP4_READBOX_EXIT( 0 );
2064 static int MP4_ReadBox_ASF( stream_t *p_stream, MP4_Box_t *p_box )
2066 MP4_READBOX_ENTER( MP4_Box_data_ASF_t, NULL );
2068 MP4_Box_data_ASF_t *p_asf = p_box->data.p_asf;
2070 if (i_read != 8)
2071 MP4_READBOX_EXIT( 0 );
2073 MP4_GET1BYTE( p_asf->i_stream_number );
2074 /* remaining is unknown */
2076 MP4_READBOX_EXIT( 1 );
2079 static void MP4_FreeBox_sbgp( MP4_Box_t *p_box )
2081 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2082 free( p_sbgp->entries.pi_sample_count );
2083 free( p_sbgp->entries.pi_group_description_index );
2086 static int MP4_ReadBox_sbgp( stream_t *p_stream, MP4_Box_t *p_box )
2088 MP4_READBOX_ENTER( MP4_Box_data_sbgp_t, MP4_FreeBox_sbgp );
2089 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2090 uint32_t i_flags;
2092 if ( i_read < 12 )
2093 MP4_READBOX_EXIT( 0 );
2095 MP4_GET1BYTE( p_sbgp->i_version );
2096 MP4_GET3BYTES( i_flags );
2097 if( i_flags != 0 )
2098 MP4_READBOX_EXIT( 0 );
2100 MP4_GETFOURCC( p_sbgp->i_grouping_type );
2102 if( p_sbgp->i_version == 1 )
2104 if( i_read < 8 )
2105 MP4_READBOX_EXIT( 0 );
2106 MP4_GET4BYTES( p_sbgp->i_grouping_type_parameter );
2109 MP4_GET4BYTES( p_sbgp->i_entry_count );
2110 if( p_sbgp->i_entry_count > i_read / (4 + 4) )
2111 p_sbgp->i_entry_count = i_read / (4 + 4);
2113 p_sbgp->entries.pi_sample_count = vlc_alloc( p_sbgp->i_entry_count, sizeof(uint32_t) );
2114 p_sbgp->entries.pi_group_description_index = vlc_alloc( p_sbgp->i_entry_count, sizeof(uint32_t) );
2116 if( !p_sbgp->entries.pi_sample_count || !p_sbgp->entries.pi_group_description_index )
2118 MP4_FreeBox_sbgp( p_box );
2119 MP4_READBOX_EXIT( 0 );
2122 for( uint32_t i=0; i<p_sbgp->i_entry_count; i++ )
2124 MP4_GET4BYTES( p_sbgp->entries.pi_sample_count[i] );
2125 MP4_GET4BYTES( p_sbgp->entries.pi_group_description_index[i] );
2128 #ifdef MP4_VERBOSE
2129 msg_Dbg( p_stream,
2130 "read box: \"sbgp\" grouping type %4.4s", (char*) &p_sbgp->i_grouping_type );
2131 #ifdef MP4_ULTRA_VERBOSE
2132 for (uint32_t i = 0; i < p_sbgp->i_entry_count; i++)
2133 msg_Dbg( p_stream, "\t samples %" PRIu32 " group %" PRIu32,
2134 p_sbgp->entries.pi_sample_count[i],
2135 p_sbgp->entries.pi_group_description_index[i] );
2136 #endif
2137 #endif
2139 MP4_READBOX_EXIT( 1 );
2142 static void MP4_FreeBox_sgpd( MP4_Box_t *p_box )
2144 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2145 free( p_sgpd->p_entries );
2148 static int MP4_ReadBox_sgpd( stream_t *p_stream, MP4_Box_t *p_box )
2150 MP4_READBOX_ENTER( MP4_Box_data_sgpd_t, MP4_FreeBox_sgpd );
2151 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2152 uint32_t i_flags;
2153 uint32_t i_default_length = 0;
2155 if ( i_read < 8 )
2156 MP4_READBOX_EXIT( 0 );
2158 MP4_GET1BYTE( p_sgpd->i_version );
2159 MP4_GET3BYTES( i_flags );
2160 if( i_flags != 0 )
2161 MP4_READBOX_EXIT( 0 );
2163 MP4_GETFOURCC( p_sgpd->i_grouping_type );
2165 switch( p_sgpd->i_grouping_type )
2167 case SAMPLEGROUP_rap:
2168 break;
2170 default:
2171 #ifdef MP4_VERBOSE
2172 msg_Dbg( p_stream,
2173 "read box: \"sgpd\" grouping type %4.4s (unimplemented)", (char*) &p_sgpd->i_grouping_type );
2174 #endif
2175 MP4_READBOX_EXIT( 1 );
2178 if( p_sgpd->i_version == 1 )
2180 if( i_read < 8 )
2181 MP4_READBOX_EXIT( 0 );
2182 MP4_GET4BYTES( i_default_length );
2184 else if( p_sgpd->i_version >= 2 )
2186 if( i_read < 8 )
2187 MP4_READBOX_EXIT( 0 );
2188 MP4_GET4BYTES( p_sgpd->i_default_sample_description_index );
2191 MP4_GET4BYTES( p_sgpd->i_entry_count );
2193 p_sgpd->p_entries = vlc_alloc( p_sgpd->i_entry_count, sizeof(*p_sgpd->p_entries) );
2194 if( !p_sgpd->p_entries )
2195 MP4_READBOX_EXIT( 0 );
2197 uint32_t i = 0;
2198 for( ; i<p_sgpd->i_entry_count; i++ )
2200 uint32_t i_description_length = i_default_length;
2201 if( p_sgpd->i_version == 1 && i_default_length == 0 )
2203 if( i_read < 4 )
2204 break;
2205 MP4_GET4BYTES( i_description_length );
2208 if( p_sgpd->i_version == 1 && i_read < i_description_length )
2209 break;
2211 switch( p_sgpd->i_grouping_type )
2213 case SAMPLEGROUP_rap:
2215 if( i_read < 1 )
2217 p_sgpd->i_entry_count = 0;
2218 MP4_FreeBox_sgpd( p_box );
2219 MP4_READBOX_EXIT( 0 );
2221 uint8_t i_data;
2222 MP4_GET1BYTE( i_data );
2223 p_sgpd->p_entries[i].rap.i_num_leading_samples_known = i_data & 0x80;
2224 p_sgpd->p_entries[i].rap.i_num_leading_samples = i_data & 0x7F;
2226 break;
2228 default:
2229 assert(0);
2233 if( i != p_sgpd->i_entry_count )
2234 p_sgpd->i_entry_count = i;
2236 #ifdef MP4_VERBOSE
2237 msg_Dbg( p_stream,
2238 "read box: \"sgpd\" grouping type %4.4s", (char*) &p_sgpd->i_grouping_type );
2239 #endif
2241 MP4_READBOX_EXIT( 1 );
2244 static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
2246 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2247 free( p_chan->layout.p_descriptions );
2250 static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
2252 MP4_READBOX_ENTER( MP4_Box_data_chan_t, MP4_FreeBox_stsdext_chan );
2253 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2255 if ( i_read < 16 )
2256 MP4_READBOX_EXIT( 0 );
2258 MP4_GET1BYTE( p_chan->i_version );
2259 MP4_GET3BYTES( p_chan->i_channels_flags );
2260 MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
2261 MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
2262 MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
2264 size_t i_descsize = 8 + 3 * sizeof(float);
2265 if ( i_read < p_chan->layout.i_channels_description_count * i_descsize )
2266 MP4_READBOX_EXIT( 0 );
2268 p_chan->layout.p_descriptions =
2269 vlc_alloc( p_chan->layout.i_channels_description_count, i_descsize );
2271 if ( !p_chan->layout.p_descriptions )
2272 MP4_READBOX_EXIT( 0 );
2274 uint32_t i;
2275 for( i=0; i<p_chan->layout.i_channels_description_count; i++ )
2277 if ( i_read < 20 )
2278 break;
2279 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
2280 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
2281 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
2282 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
2283 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
2285 if ( i<p_chan->layout.i_channels_description_count )
2286 p_chan->layout.i_channels_description_count = i;
2288 #ifdef MP4_VERBOSE
2289 msg_Dbg( p_stream,
2290 "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
2291 p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
2292 p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
2293 #endif
2294 MP4_READBOX_EXIT( 1 );
2297 static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
2299 MP4_READBOX_ENTER( MP4_Box_data_dec3_t, NULL );
2301 MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
2303 unsigned i_header;
2304 MP4_GET2BYTES( i_header );
2306 p_dec3->i_data_rate = i_header >> 3;
2307 p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
2308 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
2309 MP4_GET3BYTES( i_header );
2310 p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
2311 p_dec3->stream[i].i_bsid = ( i_header >> 17 ) & 0x01f;
2312 p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
2313 p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
2314 p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
2315 p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
2316 if (p_dec3->stream[i].i_num_dep_sub) {
2317 MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
2318 p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
2319 } else
2320 p_dec3->stream[i].i_chan_loc = 0;
2323 #ifdef MP4_VERBOSE
2324 msg_Dbg( p_stream,
2325 "read box: \"dec3\" bitrate %dkbps %d independent substreams",
2326 p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
2328 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
2329 msg_Dbg( p_stream,
2330 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
2331 "num dependent subs=%d chan_loc=0x%x",
2332 i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
2333 p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
2334 #endif
2335 MP4_READBOX_EXIT( 1 );
2338 static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
2340 MP4_Box_data_dac3_t *p_dac3;
2341 MP4_READBOX_ENTER( MP4_Box_data_dac3_t, NULL );
2343 p_dac3 = p_box->data.p_dac3;
2345 unsigned i_header;
2346 MP4_GET3BYTES( i_header );
2348 p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
2349 p_dac3->i_bsid = ( i_header >> 17 ) & 0x01f;
2350 p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
2351 p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
2352 p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
2353 p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
2355 #ifdef MP4_VERBOSE
2356 msg_Dbg( p_stream,
2357 "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
2358 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 );
2359 #endif
2360 MP4_READBOX_EXIT( 1 );
2363 static void MP4_FreeBox_dvc1( MP4_Box_t *p_box )
2365 free( p_box->data.p_dvc1->p_vc1 );
2368 static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
2370 MP4_READBOX_ENTER( MP4_Box_data_dvc1_t, MP4_FreeBox_dvc1 );
2371 if( i_read < 7 )
2372 MP4_READBOX_EXIT( 0 );
2374 MP4_Box_data_dvc1_t *p_dvc1 = p_box->data.p_dvc1;
2375 MP4_GET1BYTE( p_dvc1->i_profile_level );
2376 p_dvc1->i_vc1 = i_read; /* Header + profile_level */
2377 if( p_dvc1->i_vc1 > 0 && (p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 )) )
2378 memcpy( p_dvc1->p_vc1, p_peek, i_read );
2380 #ifdef MP4_VERBOSE
2381 msg_Dbg( p_stream,
2382 "read box: \"dvc1\" profile=%"PRIu8, (p_dvc1->i_profile_level & 0xf0) >> 4 );
2383 #endif
2385 MP4_READBOX_EXIT( 1 );
2388 static int MP4_ReadBox_fiel( stream_t *p_stream, MP4_Box_t *p_box )
2390 MP4_Box_data_fiel_t *p_fiel;
2391 MP4_READBOX_ENTER( MP4_Box_data_fiel_t, NULL );
2392 p_fiel = p_box->data.p_fiel;
2393 if(i_read < 2)
2394 MP4_READBOX_EXIT( 0 );
2395 if(p_peek[0] == 1)
2397 p_fiel->i_flags = BLOCK_FLAG_SINGLE_FIELD;
2399 else if(p_peek[0] == 2) /* Interlaced */
2402 * 0 – There is only one field.
2403 * 1 – T is displayed earliest, T is stored first in the file.
2404 * 6 – B is displayed earliest, B is stored first in the file.
2405 * 9 – B is displayed earliest, T is stored first in the file.
2406 * 14 – T is displayed earliest, B is stored first in the file.
2408 if(p_peek[1] == 1 || p_peek[1] == 9)
2409 p_fiel->i_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
2410 else if(p_peek[1] == 6 || p_peek[1] == 14)
2411 p_fiel->i_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
2413 MP4_READBOX_EXIT( 1 );
2416 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
2418 MP4_Box_data_enda_t *p_enda;
2419 MP4_READBOX_ENTER( MP4_Box_data_enda_t, NULL );
2421 p_enda = p_box->data.p_enda;
2423 MP4_GET2BYTES( p_enda->i_little_endian );
2425 #ifdef MP4_VERBOSE
2426 msg_Dbg( p_stream,
2427 "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
2428 #endif
2429 MP4_READBOX_EXIT( 1 );
2432 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
2434 free( p_box->data.p_sample_soun->p_qt_description );
2437 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
2439 p_box->i_handler = ATOM_soun;
2440 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t, MP4_FreeBox_sample_soun );
2441 p_box->data.p_sample_soun->p_qt_description = NULL;
2443 size_t i_actually_read = i_read + header_size;
2445 /* Sanity check needed because the "wave" box does also contain an
2446 * "mp4a" box that we don't understand. */
2447 if( i_read < 28 )
2449 MP4_READBOX_EXIT( 1 );
2452 for( unsigned i = 0; i < 6 ; i++ )
2454 MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
2457 MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
2460 * XXX hack -> produce a copy of the nearly complete chunk
2462 p_box->data.p_sample_soun->i_qt_description = 0;
2463 p_box->data.p_sample_soun->p_qt_description = NULL;
2464 if( i_read > 0 )
2466 p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
2467 if( p_box->data.p_sample_soun->p_qt_description )
2469 p_box->data.p_sample_soun->i_qt_description = i_read;
2470 memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
2474 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
2475 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
2476 MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
2478 MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
2479 MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
2480 MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
2481 MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
2482 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
2483 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
2485 #ifdef MP4_VERBOSE
2486 msg_Dbg( p_stream,
2487 "read box: \"soun\" stsd qt_version %"PRIu16" compid=%"PRIx16,
2488 p_box->data.p_sample_soun->i_qt_version,
2489 p_box->data.p_sample_soun->i_compressionid );
2490 #endif
2491 /* @36 bytes */
2492 if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
2494 /* SoundDescriptionV1 */
2495 MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
2496 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
2497 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
2498 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
2500 #ifdef MP4_VERBOSE
2501 msg_Dbg( p_stream,
2502 "read box: \"soun\" V1 sample/packet=%d bytes/packet=%d "
2503 "bytes/frame=%d bytes/sample=%d",
2504 p_box->data.p_sample_soun->i_sample_per_packet,
2505 p_box->data.p_sample_soun->i_bytes_per_packet,
2506 p_box->data.p_sample_soun->i_bytes_per_frame,
2507 p_box->data.p_sample_soun->i_bytes_per_sample );
2508 #endif
2509 /* @52 bytes */
2511 else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
2513 /* SoundDescriptionV2 */
2514 double f_sample_rate;
2515 int64_t i_dummy64;
2516 uint32_t i_channel, i_extoffset, i_dummy32;
2518 /* Checks */
2519 if ( p_box->data.p_sample_soun->i_channelcount != 0x3 ||
2520 p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
2521 p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
2522 p_box->data.p_sample_soun->i_reserved3 != 0x0 ||
2523 p_box->data.p_sample_soun->i_sampleratehi != 0x1 ||//65536
2524 p_box->data.p_sample_soun->i_sampleratelo != 0x0 ) //remainder
2526 msg_Err( p_stream, "invalid stsd V2 box defaults" );
2527 MP4_READBOX_EXIT( 0 );
2529 /* !Checks */
2531 MP4_GET4BYTES( i_extoffset ); /* offset to stsd extentions */
2532 MP4_GET8BYTES( i_dummy64 );
2533 memcpy( &f_sample_rate, &i_dummy64, 8 );
2534 msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
2535 /* Rounding error with lo, but we don't care since we do not support fractional audio rate */
2536 p_box->data.p_sample_soun->i_sampleratehi = (uint32_t)f_sample_rate;
2537 p_box->data.p_sample_soun->i_sampleratelo = (f_sample_rate - p_box->data.p_sample_soun->i_sampleratehi);
2539 MP4_GET4BYTES( i_channel );
2540 p_box->data.p_sample_soun->i_channelcount = i_channel;
2542 MP4_GET4BYTES( i_dummy32 );
2543 if ( i_dummy32 != 0x7F000000 )
2545 msg_Err( p_stream, "invalid stsd V2 box" );
2546 MP4_READBOX_EXIT( 0 );
2549 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
2550 MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
2551 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
2552 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2554 #ifdef MP4_VERBOSE
2555 msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
2556 "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
2557 f_sample_rate,
2558 p_box->data.p_sample_soun->i_constbitsperchannel,
2559 p_box->data.p_sample_soun->i_formatflags,
2560 p_box->data.p_sample_soun->i_constbytesperaudiopacket,
2561 p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2562 #endif
2563 /* @72 bytes + */
2564 if( i_extoffset > i_actually_read )
2565 i_extoffset = i_actually_read;
2566 p_peek = &p_buff[i_extoffset];
2567 i_read = i_actually_read - i_extoffset;
2569 else
2571 p_box->data.p_sample_soun->i_sample_per_packet = 0;
2572 p_box->data.p_sample_soun->i_bytes_per_packet = 0;
2573 p_box->data.p_sample_soun->i_bytes_per_frame = 0;
2574 p_box->data.p_sample_soun->i_bytes_per_sample = 0;
2576 #ifdef MP4_VERBOSE
2577 msg_Dbg( p_stream, "read box: \"soun\" V0 or qt1/2 (rest=%"PRIu64")",
2578 i_read );
2579 #endif
2580 /* @36 bytes */
2583 if( p_box->i_type == ATOM_drms )
2585 msg_Warn( p_stream, "DRM protected streams are not supported." );
2586 MP4_READBOX_EXIT( 0 );
2589 if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
2591 /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
2592 p_box->data.p_sample_soun->i_channelcount = 1;
2595 /* Loads extensions */
2596 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2597 p_box->i_pos + p_peek - p_buff ); /* esds/wave/... */
2599 #ifdef MP4_VERBOSE
2600 msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
2601 "sample size %d sample rate %f",
2602 p_box->data.p_sample_soun->i_channelcount,
2603 p_box->data.p_sample_soun->i_samplesize,
2604 (float)p_box->data.p_sample_soun->i_sampleratehi +
2605 (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
2607 #endif
2608 MP4_READBOX_EXIT( 1 );
2611 static void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
2613 free( p_box->data.p_sample_vide->p_qt_image_description );
2616 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
2618 p_box->i_handler = ATOM_vide;
2619 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t, MP4_FreeBox_sample_vide );
2621 size_t i_actually_read = i_read + header_size;
2623 for( unsigned i = 0; i < 6 ; i++ )
2625 MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
2628 MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
2631 * XXX hack -> produce a copy of the nearly complete chunk
2633 if( i_read > 0 )
2635 p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
2636 if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
2637 MP4_READBOX_EXIT( 0 );
2638 p_box->data.p_sample_vide->i_qt_image_description = i_read;
2639 memcpy( p_box->data.p_sample_vide->p_qt_image_description,
2640 p_peek, i_read );
2642 else
2644 p_box->data.p_sample_vide->i_qt_image_description = 0;
2645 p_box->data.p_sample_vide->p_qt_image_description = NULL;
2648 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
2649 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
2650 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
2652 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
2653 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
2655 MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
2656 MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
2658 MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
2659 MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
2661 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
2662 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
2664 if ( i_read < 32 )
2665 MP4_READBOX_EXIT( 0 );
2666 if( p_peek[0] <= 31 ) // Must be Pascal String
2668 memcpy( &p_box->data.p_sample_vide->sz_compressorname, &p_peek[1], p_peek[0] );
2669 p_box->data.p_sample_vide->sz_compressorname[p_peek[0]] = 0;
2671 p_peek += 32; i_read -= 32;
2673 MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
2674 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
2676 if( p_box->i_type == ATOM_drmi )
2678 msg_Warn( p_stream, "DRM protected streams are not supported." );
2679 MP4_READBOX_EXIT( 0 );
2682 if( i_actually_read > 78 && p_peek - p_buff > 78 )
2684 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2685 p_box->i_pos + p_peek - p_buff );
2688 #ifdef MP4_VERBOSE
2689 msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d (%s)",
2690 p_box->data.p_sample_vide->i_width,
2691 p_box->data.p_sample_vide->i_height,
2692 p_box->data.p_sample_vide->i_depth,
2693 p_box->data.p_sample_vide->sz_compressorname );
2695 #endif
2696 MP4_READBOX_EXIT( 1 );
2699 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
2701 p_box->i_handler = ATOM_text;
2702 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_text_t, 16, NULL );
2703 (void) p_peek;
2704 if( i_read < 8 )
2705 MP4_READBOX_EXIT( 0 );
2707 MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
2709 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2710 MP4_READBOX_EXIT( 0 );
2712 MP4_READBOX_EXIT( 1 );
2715 static void MP4_FreeBox_sample_hint( MP4_Box_t *p_box )
2717 free( p_box->data.p_sample_hint->p_data );
2720 static int MP4_ReadBox_sample_hint8( stream_t *p_stream, MP4_Box_t *p_box )
2722 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_hint_t, 24, MP4_FreeBox_sample_hint );
2724 for( unsigned i = 0; i < 6 ; i++ )
2726 MP4_GET1BYTE( p_box->data.p_sample_hint->i_reserved1[i] );
2729 MP4_GET2BYTES( p_box->data.p_sample_hint->i_data_reference_index );
2731 if( !(p_box->data.p_sample_hint->p_data = malloc(8)) )
2732 MP4_READBOX_EXIT( 0 );
2734 MP4_GET8BYTES( *(p_box->data.p_sample_hint->p_data) );
2736 MP4_ReadBoxContainerChildren(p_stream, p_box, NULL);
2738 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2739 MP4_READBOX_EXIT( 0 );
2741 MP4_READBOX_EXIT( 1 );
2744 static void MP4_FreeBox_sample_text( MP4_Box_t *p_box )
2746 free( p_box->data.p_sample_text->p_data );
2749 static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
2751 p_box->i_handler = ATOM_text;
2752 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t, MP4_FreeBox_sample_text );
2754 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2755 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2757 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2759 if( i_read )
2761 p_box->data.p_sample_text->p_data = malloc( i_read );
2762 if( !p_box->data.p_sample_text->p_data )
2763 MP4_READBOX_EXIT( 0 );
2764 memcpy( p_box->data.p_sample_text->p_data, p_peek, i_read );
2766 p_box->data.p_sample_text->i_data = i_read;
2768 #ifdef MP4_VERBOSE
2769 msg_Dbg( p_stream, "read box: \"text\" in stsd text" );
2770 #endif
2771 MP4_READBOX_EXIT( 1 );
2774 static int MP4_ReadBox_sample_clcp( stream_t *p_stream, MP4_Box_t *p_box )
2776 p_box->i_handler = ATOM_clcp;
2777 MP4_READBOX_ENTER( MP4_Box_data_sample_clcp_t, NULL );
2779 if( i_read < 8 )
2780 MP4_READBOX_EXIT( 0 );
2782 for( int i=0; i<6; i++ )
2783 MP4_GET1BYTE( p_box->data.p_sample_clcp->i_reserved1[i] );
2784 MP4_GET2BYTES( p_box->data.p_sample_clcp->i_data_reference_index );
2786 #ifdef MP4_VERBOSE
2787 msg_Dbg( p_stream, "read box: \"clcp\" in stsd" );
2788 #endif
2789 MP4_READBOX_EXIT( 1 );
2792 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
2794 free( p_box->data.p_stsz->i_entry_size );
2797 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
2799 uint32_t count;
2801 MP4_READBOX_ENTER( MP4_Box_data_stsz_t, MP4_FreeBox_stsz );
2803 MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
2805 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
2806 MP4_GET4BYTES( count );
2807 p_box->data.p_stsz->i_sample_count = count;
2809 if( p_box->data.p_stsz->i_sample_size == 0 )
2811 if( UINT64_C(4) * count > i_read )
2812 MP4_READBOX_EXIT( 0 );
2814 p_box->data.p_stsz->i_entry_size =
2815 vlc_alloc( count, sizeof(uint32_t) );
2816 if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
2817 MP4_READBOX_EXIT( 0 );
2819 for( uint32_t i = 0; i < count; i++ )
2821 MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
2824 else
2825 p_box->data.p_stsz->i_entry_size = NULL;
2827 #ifdef MP4_VERBOSE
2828 msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
2829 p_box->data.p_stsz->i_sample_size,
2830 p_box->data.p_stsz->i_sample_count );
2832 #endif
2833 MP4_READBOX_EXIT( 1 );
2836 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
2838 free( p_box->data.p_stsc->i_first_chunk );
2839 free( p_box->data.p_stsc->i_samples_per_chunk );
2840 free( p_box->data.p_stsc->i_sample_description_index );
2843 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
2845 uint32_t count;
2847 MP4_READBOX_ENTER( MP4_Box_data_stsc_t, MP4_FreeBox_stsc );
2849 MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
2850 MP4_GET4BYTES( count );
2852 if( UINT64_C(12) * count > i_read )
2853 MP4_READBOX_EXIT( 0 );
2855 p_box->data.p_stsc->i_first_chunk = vlc_alloc( count, sizeof(uint32_t) );
2856 p_box->data.p_stsc->i_samples_per_chunk = vlc_alloc( count,
2857 sizeof(uint32_t) );
2858 p_box->data.p_stsc->i_sample_description_index = vlc_alloc( count,
2859 sizeof(uint32_t) );
2860 if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
2861 || p_box->data.p_stsc->i_samples_per_chunk == NULL
2862 || p_box->data.p_stsc->i_sample_description_index == NULL ) )
2864 MP4_READBOX_EXIT( 0 );
2866 p_box->data.p_stsc->i_entry_count = count;
2868 for( uint32_t i = 0; i < count;i++ )
2870 MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
2871 MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
2872 MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
2875 #ifdef MP4_VERBOSE
2876 msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
2877 p_box->data.p_stsc->i_entry_count );
2879 #endif
2880 MP4_READBOX_EXIT( 1 );
2883 static void MP4_FreeBox_sdp( MP4_Box_t *p_box )
2885 free( p_box->data.p_sdp->psz_text );
2888 static int MP4_ReadBox_sdp( stream_t *p_stream, MP4_Box_t *p_box )
2890 MP4_READBOX_ENTER( MP4_Box_data_sdp_t, MP4_FreeBox_sdp );
2892 MP4_GETSTRINGZ( p_box->data.p_sdp->psz_text );
2894 MP4_READBOX_EXIT( 1 );
2897 static void MP4_FreeBox_rtp( MP4_Box_t *p_box )
2899 free( p_box->data.p_moviehintinformation_rtp->psz_text );
2902 static int MP4_ReadBox_rtp( stream_t *p_stream, MP4_Box_t *p_box )
2904 MP4_READBOX_ENTER( MP4_Box_data_moviehintinformation_rtp_t, MP4_FreeBox_rtp );
2906 MP4_GET4BYTES( p_box->data.p_moviehintinformation_rtp->i_description_format );
2908 MP4_GETSTRINGZ( p_box->data.p_moviehintinformation_rtp->psz_text );
2910 MP4_READBOX_EXIT( 1 );
2913 static int MP4_ReadBox_tims( stream_t *p_stream, MP4_Box_t *p_box )
2915 MP4_READBOX_ENTER( MP4_Box_data_tims_t, NULL );
2917 MP4_GET4BYTES( p_box->data.p_tims->i_timescale );
2919 MP4_READBOX_EXIT( 1 );
2922 static int MP4_ReadBox_tsro( stream_t *p_stream, MP4_Box_t *p_box )
2924 MP4_READBOX_ENTER( MP4_Box_data_tsro_t, NULL );
2926 MP4_GET4BYTES( p_box->data.p_tsro->i_offset );
2928 MP4_READBOX_EXIT( 1 );
2931 static int MP4_ReadBox_tssy( stream_t *p_stream, MP4_Box_t *p_box )
2933 MP4_READBOX_ENTER( MP4_Box_data_tssy_t, NULL );
2935 MP4_GET1BYTE( p_box->data.p_tssy->i_reserved_timestamp_sync );
2937 MP4_READBOX_EXIT( 1 );
2940 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
2942 free( p_box->data.p_co64->i_chunk_offset );
2945 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
2947 const bool sixtyfour = p_box->i_type != ATOM_stco;
2948 uint32_t count;
2950 MP4_READBOX_ENTER( MP4_Box_data_co64_t, MP4_FreeBox_stco_co64 );
2952 MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
2953 MP4_GET4BYTES( count );
2955 if( (sixtyfour ? UINT64_C(8) : UINT64_C(4)) * count > i_read )
2956 MP4_READBOX_EXIT( 0 );
2958 p_box->data.p_co64->i_chunk_offset = vlc_alloc( count, sizeof(uint64_t) );
2959 if( unlikely(p_box->data.p_co64->i_chunk_offset == NULL) )
2960 MP4_READBOX_EXIT( 0 );
2961 p_box->data.p_co64->i_entry_count = count;
2963 for( uint32_t i = 0; i < count; i++ )
2965 if( sixtyfour )
2966 MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2967 else
2968 MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2971 #ifdef MP4_VERBOSE
2972 msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
2973 p_box->data.p_co64->i_entry_count );
2975 #endif
2976 MP4_READBOX_EXIT( 1 );
2979 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
2981 free( p_box->data.p_stss->i_sample_number );
2984 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
2986 uint32_t count;
2988 MP4_READBOX_ENTER( MP4_Box_data_stss_t, MP4_FreeBox_stss );
2990 MP4_GETVERSIONFLAGS( p_box->data.p_stss );
2991 MP4_GET4BYTES( count );
2993 if( UINT64_C(4) * count > i_read )
2994 MP4_READBOX_EXIT( 0 );
2996 p_box->data.p_stss->i_sample_number = vlc_alloc( count, sizeof(uint32_t) );
2997 if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
2998 MP4_READBOX_EXIT( 0 );
2999 p_box->data.p_stss->i_entry_count = count;
3001 for( uint32_t i = 0; i < count; i++ )
3003 MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
3004 /* XXX in libmp4 sample begin at 0 */
3005 p_box->data.p_stss->i_sample_number[i]--;
3008 #ifdef MP4_VERBOSE
3009 msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
3010 p_box->data.p_stss->i_entry_count );
3012 #endif
3013 MP4_READBOX_EXIT( 1 );
3016 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
3018 free( p_box->data.p_stsh->i_shadowed_sample_number );
3019 free( p_box->data.p_stsh->i_sync_sample_number );
3022 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
3024 uint32_t count;
3026 MP4_READBOX_ENTER( MP4_Box_data_stsh_t, MP4_FreeBox_stsh );
3028 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
3029 MP4_GET4BYTES( count );
3031 if( UINT64_C(8) * count > i_read )
3032 MP4_READBOX_EXIT( 0 );
3034 p_box->data.p_stsh->i_shadowed_sample_number = vlc_alloc( count,
3035 sizeof(uint32_t) );
3036 p_box->data.p_stsh->i_sync_sample_number = vlc_alloc( count,
3037 sizeof(uint32_t) );
3038 if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
3039 || p_box->data.p_stsh->i_sync_sample_number == NULL )
3040 MP4_READBOX_EXIT( 0 );
3041 p_box->data.p_stsh->i_entry_count = count;
3043 for( uint32_t i = 0; i < p_box->data.p_stss->i_entry_count; i++ )
3045 MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
3046 MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
3049 #ifdef MP4_VERBOSE
3050 msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
3051 p_box->data.p_stsh->i_entry_count );
3052 #endif
3053 MP4_READBOX_EXIT( 1 );
3056 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
3058 free( p_box->data.p_stdp->i_priority );
3061 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
3063 MP4_READBOX_ENTER( MP4_Box_data_stdp_t, MP4_FreeBox_stdp );
3065 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
3067 p_box->data.p_stdp->i_priority =
3068 calloc( i_read / 2, sizeof(uint16_t) );
3070 if( unlikely( !p_box->data.p_stdp->i_priority ) )
3071 MP4_READBOX_EXIT( 0 );
3073 for( unsigned i = 0; i < i_read / 2 ; i++ )
3075 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
3078 #ifdef MP4_VERBOSE
3079 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
3080 i_read / 2 );
3082 #endif
3083 MP4_READBOX_EXIT( 1 );
3086 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
3088 free( p_box->data.p_elst->i_segment_duration );
3089 free( p_box->data.p_elst->i_media_time );
3090 free( p_box->data.p_elst->i_media_rate_integer );
3091 free( p_box->data.p_elst->i_media_rate_fraction );
3094 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
3096 uint32_t count;
3098 MP4_READBOX_ENTER( MP4_Box_data_elst_t, MP4_FreeBox_elst );
3100 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
3101 MP4_GET4BYTES( count );
3103 if( count == 0 )
3104 MP4_READBOX_EXIT( 1 );
3106 uint32_t i_entries_max = i_read / ((p_box->data.p_elst->i_version == 1) ? 20 : 12);
3107 if( count > i_entries_max )
3108 count = i_entries_max;
3110 p_box->data.p_elst->i_segment_duration = vlc_alloc( count,
3111 sizeof(uint64_t) );
3112 p_box->data.p_elst->i_media_time = vlc_alloc( count, sizeof(int64_t) );
3113 p_box->data.p_elst->i_media_rate_integer = vlc_alloc( count,
3114 sizeof(uint16_t) );
3115 p_box->data.p_elst->i_media_rate_fraction = vlc_alloc( count,
3116 sizeof(uint16_t) );
3117 if( p_box->data.p_elst->i_segment_duration == NULL
3118 || p_box->data.p_elst->i_media_time == NULL
3119 || p_box->data.p_elst->i_media_rate_integer == NULL
3120 || p_box->data.p_elst->i_media_rate_fraction == NULL )
3122 MP4_READBOX_EXIT( 0 );
3124 p_box->data.p_elst->i_entry_count = count;
3126 for( uint32_t i = 0; i < count; i++ )
3128 uint64_t segment_duration;
3129 int64_t media_time;
3131 if( p_box->data.p_elst->i_version == 1 )
3133 union { int64_t s; uint64_t u; } u;
3135 MP4_GET8BYTES( segment_duration );
3136 MP4_GET8BYTES( u.u );
3137 media_time = u.s;
3139 else
3141 union { int32_t s; uint32_t u; } u;
3143 MP4_GET4BYTES( segment_duration );
3144 MP4_GET4BYTES( u.u );
3145 media_time = u.s;
3148 p_box->data.p_elst->i_segment_duration[i] = segment_duration;
3149 p_box->data.p_elst->i_media_time[i] = media_time;
3150 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
3151 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
3154 #ifdef MP4_VERBOSE
3155 msg_Dbg( p_stream, "read box: \"elst\" entry-count %" PRIu32,
3156 p_box->data.p_elst->i_entry_count );
3157 #endif
3158 MP4_READBOX_EXIT( 1 );
3161 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
3163 free( p_box->data.p_cprt->psz_notice );
3166 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
3168 uint16_t i_language;
3169 bool b_mac;
3171 MP4_READBOX_ENTER( MP4_Box_data_cprt_t, MP4_FreeBox_cprt );
3173 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
3175 MP4_GET2BYTES( i_language );
3176 decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
3178 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
3180 #ifdef MP4_VERBOSE
3181 msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
3182 p_box->data.p_cprt->rgs_language,
3183 p_box->data.p_cprt->psz_notice );
3185 #endif
3186 MP4_READBOX_EXIT( 1 );
3189 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
3191 MP4_READBOX_ENTER( MP4_Box_data_dcom_t, NULL );
3193 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
3194 #ifdef MP4_VERBOSE
3195 msg_Dbg( p_stream,
3196 "read box: \"dcom\" compression algorithm : %4.4s",
3197 (char*)&p_box->data.p_dcom->i_algorithm );
3198 #endif
3199 MP4_READBOX_EXIT( 1 );
3202 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
3204 free( p_box->data.p_cmvd->p_data );
3207 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
3209 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t, MP4_FreeBox_cmvd );
3211 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
3213 p_box->data.p_cmvd->i_compressed_size = i_read;
3215 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
3216 MP4_READBOX_EXIT( 0 );
3218 /* now copy compressed data */
3219 memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
3221 p_box->data.p_cmvd->b_compressed = 1;
3223 #ifdef MP4_VERBOSE
3224 msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
3225 p_box->data.p_cmvd->i_compressed_size );
3226 #endif
3228 MP4_READBOX_EXIT( 1 );
3231 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
3233 MP4_Box_t *p_dcom;
3234 MP4_Box_t *p_cmvd;
3236 #ifdef HAVE_ZLIB_H
3237 stream_t *p_stream_memory;
3238 z_stream z_data;
3239 uint8_t *p_data;
3240 int i_result;
3241 #endif
3243 if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
3244 return 0;
3246 if( !p_box->p_father ||
3247 ( p_box->p_father->i_type != ATOM_moov &&
3248 p_box->p_father->i_type != ATOM_foov ) )
3250 msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
3251 return 1;
3254 if( !MP4_ReadBoxContainer( p_stream, p_box ) )
3256 return 0;
3259 if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
3260 ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
3261 p_cmvd->data.p_cmvd->p_data == NULL )
3263 msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
3264 return 0;
3267 if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
3269 msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
3270 "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
3271 return 0;
3274 #ifndef HAVE_ZLIB_H
3275 msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
3276 return 0;
3278 #else
3279 /* decompress data */
3280 /* allocate a new buffer */
3281 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
3282 return 0;
3283 /* init default structures */
3284 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
3285 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
3286 z_data.next_out = p_data;
3287 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
3288 z_data.zalloc = (alloc_func)Z_NULL;
3289 z_data.zfree = (free_func)Z_NULL;
3290 z_data.opaque = (voidpf)Z_NULL;
3292 /* init zlib */
3293 if( inflateInit( &z_data ) != Z_OK )
3295 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3296 free( p_data );
3297 return 0;
3300 /* uncompress */
3301 i_result = inflate( &z_data, Z_NO_FLUSH );
3302 if( i_result != Z_OK && i_result != Z_STREAM_END )
3304 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3305 free( p_data );
3306 return 0;
3309 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
3311 msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
3312 "mismatch" );
3314 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
3316 /* close zlib */
3317 if( inflateEnd( &z_data ) != Z_OK )
3319 msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
3320 "data (ignored)" );
3323 free( p_cmvd->data.p_cmvd->p_data );
3324 p_cmvd->data.p_cmvd->p_data = p_data;
3325 p_cmvd->data.p_cmvd->b_compressed = 0;
3327 msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
3329 /* now create a memory stream */
3330 p_stream_memory =
3331 vlc_stream_MemoryNew( VLC_OBJECT(p_stream),
3332 p_cmvd->data.p_cmvd->p_data,
3333 p_cmvd->data.p_cmvd->i_uncompressed_size, true );
3335 /* and read uncompressd moov */
3336 p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
3338 vlc_stream_Delete( p_stream_memory );
3340 #ifdef MP4_VERBOSE
3341 msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
3342 #endif
3344 return p_box->data.p_cmov->p_moov ? 1 : 0;
3345 #endif /* HAVE_ZLIB_H */
3348 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
3350 free( p_box->data.p_rdrf->psz_ref );
3353 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
3355 uint32_t i_len;
3356 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t, MP4_FreeBox_rdrf );
3358 MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
3359 MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
3360 MP4_GET4BYTES( i_len );
3361 i_len++;
3363 if( i_len > 0 )
3365 p_box->data.p_rdrf->psz_ref = malloc( i_len );
3366 if( p_box->data.p_rdrf->psz_ref == NULL )
3367 MP4_READBOX_EXIT( 0 );
3368 i_len--;
3370 for( unsigned i = 0; i < i_len; i++ )
3372 MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
3374 p_box->data.p_rdrf->psz_ref[i_len] = '\0';
3376 else
3378 p_box->data.p_rdrf->psz_ref = NULL;
3381 #ifdef MP4_VERBOSE
3382 msg_Dbg( p_stream,
3383 "read box: \"rdrf\" type:%4.4s ref %s",
3384 (char*)&p_box->data.p_rdrf->i_ref_type,
3385 p_box->data.p_rdrf->psz_ref );
3386 #endif
3387 MP4_READBOX_EXIT( 1 );
3392 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
3394 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t, NULL );
3396 MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
3398 MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
3400 #ifdef MP4_VERBOSE
3401 msg_Dbg( p_stream,
3402 "read box: \"rmdr\" rate:%d",
3403 p_box->data.p_rmdr->i_rate );
3404 #endif
3405 MP4_READBOX_EXIT( 1 );
3408 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
3410 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t, NULL );
3412 MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
3414 #ifdef MP4_VERBOSE
3415 msg_Dbg( p_stream,
3416 "read box: \"rmqu\" quality:%d",
3417 p_box->data.p_rmqu->i_quality );
3418 #endif
3419 MP4_READBOX_EXIT( 1 );
3422 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
3424 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t, NULL );
3425 MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
3427 MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
3428 MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
3429 MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
3430 MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
3432 #ifdef MP4_VERBOSE
3433 msg_Dbg( p_stream,
3434 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
3435 (char*)&p_box->data.p_rmvc->i_gestaltType,
3436 p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
3437 p_box->data.p_rmvc->i_checkType );
3438 #endif
3440 MP4_READBOX_EXIT( 1 );
3443 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
3445 MP4_READBOX_ENTER( MP4_Box_data_frma_t, NULL );
3447 MP4_GETFOURCC( p_box->data.p_frma->i_type );
3449 #ifdef MP4_VERBOSE
3450 msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
3451 (char *)&p_box->data.p_frma->i_type );
3452 #endif
3454 MP4_READBOX_EXIT( 1 );
3457 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
3459 MP4_READBOX_ENTER( MP4_Box_data_skcr_t, NULL );
3461 MP4_GET4BYTES( p_box->data.p_skcr->i_init );
3462 MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
3463 MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
3465 #ifdef MP4_VERBOSE
3466 msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
3467 p_box->data.p_skcr->i_init,
3468 p_box->data.p_skcr->i_encr,
3469 p_box->data.p_skcr->i_decr );
3470 #endif
3472 MP4_READBOX_EXIT( 1 );
3475 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
3477 VLC_UNUSED(p_box);
3478 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
3479 * so unless data decrypt itself by magic, there will be no playback,
3480 * but we never know... */
3481 msg_Warn( p_stream, "DRM protected streams are not supported." );
3482 return 1;
3485 static void MP4_FreeBox_Binary( MP4_Box_t *p_box )
3487 free( p_box->data.p_binary->p_blob );
3490 static int MP4_ReadBox_Binary( stream_t *p_stream, MP4_Box_t *p_box )
3492 MP4_READBOX_ENTER( MP4_Box_data_binary_t, MP4_FreeBox_Binary );
3493 i_read = __MIN( i_read, UINT32_MAX );
3494 if ( i_read > 0 )
3496 p_box->data.p_binary->p_blob = malloc( i_read );
3497 if ( p_box->data.p_binary->p_blob )
3499 memcpy( p_box->data.p_binary->p_blob, p_peek, i_read );
3500 p_box->data.p_binary->i_blob = i_read;
3503 MP4_READBOX_EXIT( 1 );
3506 static void MP4_FreeBox_data( MP4_Box_t *p_box )
3508 free( p_box->data.p_data->p_blob );
3511 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
3513 MP4_READBOX_ENTER( MP4_Box_data_data_t, MP4_FreeBox_data );
3514 MP4_Box_data_data_t *p_data = p_box->data.p_data;
3516 if ( i_read < 8 || i_read - 8 > UINT32_MAX )
3517 MP4_READBOX_EXIT( 0 );
3519 uint8_t i_type;
3520 MP4_GET1BYTE( i_type );
3521 if ( i_type != 0 )
3523 #ifdef MP4_VERBOSE
3524 msg_Dbg( p_stream, "skipping unknown 'data' atom with type %"PRIu8, i_type );
3525 #endif
3526 MP4_READBOX_EXIT( 0 );
3529 MP4_GET3BYTES( p_data->e_wellknowntype );
3530 MP4_GET2BYTES( p_data->locale.i_country );
3531 MP4_GET2BYTES( p_data->locale.i_language );
3532 #ifdef MP4_VERBOSE
3533 msg_Dbg( p_stream, "read 'data' atom: knowntype=%"PRIu32", country=%"PRIu16" lang=%"PRIu16
3534 ", size %"PRIu64" bytes", p_data->e_wellknowntype,
3535 p_data->locale.i_country, p_data->locale.i_language, i_read );
3536 #endif
3537 p_box->data.p_data->p_blob = malloc( i_read );
3538 if ( !p_box->data.p_data->p_blob )
3539 MP4_READBOX_EXIT( 0 );
3541 p_box->data.p_data->i_blob = i_read;
3542 memcpy( p_box->data.p_data->p_blob, p_peek, i_read);
3544 MP4_READBOX_EXIT( 1 );
3547 static int MP4_ReadBox_Metadata( stream_t *p_stream, MP4_Box_t *p_box )
3549 const uint8_t *p_peek;
3550 if ( vlc_stream_Peek( p_stream, &p_peek, 16 ) < 16 )
3551 return 0;
3552 if ( vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
3553 return 0;
3554 const uint32_t stoplist[] = { ATOM_data, 0 };
3555 return MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist );
3558 /* Chapter support */
3559 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
3561 MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
3562 for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
3563 free( p_chpl->chapter[i].psz_name );
3566 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
3568 MP4_Box_data_chpl_t *p_chpl;
3569 uint32_t i_dummy;
3570 VLC_UNUSED(i_dummy);
3571 int i;
3572 MP4_READBOX_ENTER( MP4_Box_data_chpl_t, MP4_FreeBox_chpl );
3574 p_chpl = p_box->data.p_chpl;
3576 MP4_GETVERSIONFLAGS( p_chpl );
3578 if ( i_read < 5 || p_chpl->i_version != 0x1 )
3579 MP4_READBOX_EXIT( 0 );
3581 MP4_GET4BYTES( i_dummy );
3583 MP4_GET1BYTE( p_chpl->i_chapter );
3585 for( i = 0; i < p_chpl->i_chapter; i++ )
3587 uint64_t i_start;
3588 uint8_t i_len;
3589 int i_copy;
3590 if ( i_read < 9 )
3591 break;
3592 MP4_GET8BYTES( i_start );
3593 MP4_GET1BYTE( i_len );
3595 p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
3596 if( !p_chpl->chapter[i].psz_name )
3597 MP4_READBOX_EXIT( 0 );
3599 i_copy = __MIN( i_len, i_read );
3600 if( i_copy > 0 )
3601 memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
3602 p_chpl->chapter[i].psz_name[i_copy] = '\0';
3603 p_chpl->chapter[i].i_start = i_start;
3605 p_peek += i_copy;
3606 i_read -= i_copy;
3609 if ( i != p_chpl->i_chapter )
3610 p_chpl->i_chapter = i;
3612 /* Bubble sort by increasing start date */
3615 for( i = 0; i < p_chpl->i_chapter - 1; i++ )
3617 if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
3619 char *psz = p_chpl->chapter[i+1].psz_name;
3620 int64_t i64 = p_chpl->chapter[i+1].i_start;
3622 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
3623 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
3625 p_chpl->chapter[i].psz_name = psz;
3626 p_chpl->chapter[i].i_start = i64;
3628 i = -1;
3629 break;
3632 } while( i == -1 );
3634 #ifdef MP4_VERBOSE
3635 msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
3636 p_chpl->i_chapter );
3637 #endif
3638 MP4_READBOX_EXIT( 1 );
3641 /* GoPro HiLight tags support */
3642 static void MP4_FreeBox_HMMT( MP4_Box_t *p_box )
3644 free( p_box->data.p_hmmt->pi_chapter_start );
3647 static int MP4_ReadBox_HMMT( stream_t *p_stream, MP4_Box_t *p_box )
3649 #define MAX_CHAPTER_COUNT 100
3651 MP4_Box_data_HMMT_t *p_hmmt;
3652 MP4_READBOX_ENTER( MP4_Box_data_HMMT_t, MP4_FreeBox_HMMT );
3654 if( i_read < 4 )
3655 MP4_READBOX_EXIT( 0 );
3657 p_hmmt = p_box->data.p_hmmt;
3659 MP4_GET4BYTES( p_hmmt->i_chapter_count );
3661 if( p_hmmt->i_chapter_count <= 0 )
3663 p_hmmt->pi_chapter_start = NULL;
3664 MP4_READBOX_EXIT( 1 );
3667 if( ( i_read / sizeof(uint32_t) ) < p_hmmt->i_chapter_count )
3668 MP4_READBOX_EXIT( 0 );
3670 /* Cameras are allowing a maximum of 100 tags */
3671 if( p_hmmt->i_chapter_count > MAX_CHAPTER_COUNT )
3672 p_hmmt->i_chapter_count = MAX_CHAPTER_COUNT;
3674 p_hmmt->pi_chapter_start = vlc_alloc( p_hmmt->i_chapter_count, sizeof(uint32_t) );
3675 if( p_hmmt->pi_chapter_start == NULL )
3676 MP4_READBOX_EXIT( 0 );
3678 for( uint32_t i = 0; i < p_hmmt->i_chapter_count; i++ )
3680 MP4_GET4BYTES( p_hmmt->pi_chapter_start[i] );
3683 #ifdef MP4_VERBOSE
3684 msg_Dbg( p_stream, "read box: \"HMMT\" %d HiLight tags", p_hmmt->i_chapter_count );
3685 #endif
3687 MP4_READBOX_EXIT( 1 );
3690 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
3692 free( p_box->data.p_tref_generic->i_track_ID );
3695 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
3697 uint32_t count;
3699 MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t, MP4_FreeBox_tref_generic );
3701 p_box->data.p_tref_generic->i_track_ID = NULL;
3702 count = i_read / sizeof(uint32_t);
3703 p_box->data.p_tref_generic->i_entry_count = count;
3704 p_box->data.p_tref_generic->i_track_ID = vlc_alloc( count,
3705 sizeof(uint32_t) );
3706 if( p_box->data.p_tref_generic->i_track_ID == NULL )
3707 MP4_READBOX_EXIT( 0 );
3709 for( unsigned i = 0; i < count; i++ )
3711 MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
3713 #ifdef MP4_VERBOSE
3714 msg_Dbg( p_stream, "read box: \"chap\" %d references",
3715 p_box->data.p_tref_generic->i_entry_count );
3716 #endif
3718 MP4_READBOX_EXIT( 1 );
3721 static void MP4_FreeBox_keys( MP4_Box_t *p_box )
3723 for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
3724 free( p_box->data.p_keys->p_entries[i].psz_value );
3725 free( p_box->data.p_keys->p_entries );
3728 static int MP4_ReadBox_keys( stream_t *p_stream, MP4_Box_t *p_box )
3730 MP4_READBOX_ENTER( MP4_Box_data_keys_t, MP4_FreeBox_keys );
3732 if ( i_read < 8 )
3733 MP4_READBOX_EXIT( 0 );
3735 uint32_t i_count;
3736 MP4_GET4BYTES( i_count ); /* reserved + flags */
3737 if ( i_count != 0 )
3738 MP4_READBOX_EXIT( 0 );
3740 MP4_GET4BYTES( i_count );
3741 p_box->data.p_keys->p_entries = calloc( i_count, sizeof(*p_box->data.p_keys->p_entries) );
3742 if ( !p_box->data.p_keys->p_entries )
3743 MP4_READBOX_EXIT( 0 );
3744 p_box->data.p_keys->i_entry_count = i_count;
3746 uint32_t i=0;
3747 for( ; i < i_count; i++ )
3749 if ( i_read < 8 )
3750 break;
3751 uint32_t i_keysize;
3752 MP4_GET4BYTES( i_keysize );
3753 if ( (i_keysize < 8) || (i_keysize - 4 > i_read) )
3754 break;
3755 MP4_GETFOURCC( p_box->data.p_keys->p_entries[i].i_namespace );
3756 i_keysize -= 8;
3757 p_box->data.p_keys->p_entries[i].psz_value = malloc( i_keysize + 1 );
3758 if ( !p_box->data.p_keys->p_entries[i].psz_value )
3759 break;
3760 memcpy( p_box->data.p_keys->p_entries[i].psz_value, p_peek, i_keysize );
3761 p_box->data.p_keys->p_entries[i].psz_value[i_keysize] = 0;
3762 p_peek += i_keysize;
3763 i_read -= i_keysize;
3764 #ifdef MP4_ULTRA_VERBOSE
3765 msg_Dbg( p_stream, "read box: \"keys\": %u '%s'", i + 1,
3766 p_box->data.p_keys->p_entries[i].psz_value );
3767 #endif
3769 if ( i < i_count )
3770 p_box->data.p_keys->i_entry_count = i;
3772 MP4_READBOX_EXIT( 1 );
3775 static int MP4_ReadBox_colr( stream_t *p_stream, MP4_Box_t *p_box )
3777 MP4_READBOX_ENTER( MP4_Box_data_colr_t, NULL );
3778 MP4_GETFOURCC( p_box->data.p_colr->i_type );
3779 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'c' ) ||
3780 p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3782 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_primary_idx );
3783 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_transfer_function_idx );
3784 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_matrix_idx );
3785 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3786 MP4_GET1BYTE( p_box->data.p_colr->nclc.i_full_range );
3788 else
3790 #ifdef MP4_VERBOSE
3791 msg_Warn( p_stream, "Unhandled colr type: %4.4s", (char*)&p_box->data.p_colr->i_type );
3792 #endif
3794 MP4_READBOX_EXIT( 1 );
3797 static int MP4_ReadBox_irot( stream_t *p_stream, MP4_Box_t *p_box )
3799 MP4_READBOX_ENTER( MP4_Box_data_irot_t, NULL );
3800 MP4_GET1BYTE( p_box->data.p_irot->i_ccw_degrees );
3801 p_box->data.p_irot->i_ccw_degrees &= 0x03;
3802 p_box->data.p_irot->i_ccw_degrees *= 90;
3803 MP4_READBOX_EXIT( 1 );
3806 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
3808 const uint8_t *p_peek;
3809 const size_t i_headersize = mp4_box_headersize( p_box );
3811 if( p_box->i_size < 16 || p_box->i_size - i_headersize < 8 )
3812 return 0;
3814 /* skip over box header */
3815 if( vlc_stream_Read( p_stream, NULL, i_headersize ) < (ssize_t) i_headersize )
3816 return 0;
3818 /* meta content starts with a 4 byte version/flags value (should be 0) */
3819 if( vlc_stream_Peek( p_stream, &p_peek, 8 ) < 8 )
3820 return 0;
3822 if( !memcmp( p_peek, "\0\0\0", 4 ) ) /* correct header case */
3824 if( vlc_stream_Read( p_stream, NULL, 4 ) < 4 )
3825 return 0;
3827 else if( memcmp( &p_peek[4], "hdlr", 4 ) ) /* Broken, headerless ones */
3829 return 0;
3832 /* load child atoms up to the handler (which should be next anyway) */
3833 const uint32_t stoplist[] = { ATOM_hdlr, 0 };
3834 if ( !MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist ) )
3835 return 0;
3837 /* Mandatory */
3838 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box, "hdlr" );
3839 if ( p_hdlr && BOXDATA(p_hdlr) && BOXDATA(p_hdlr)->i_version == 0 )
3841 p_box->i_handler = BOXDATA(p_hdlr)->i_handler_type;
3842 switch( p_box->i_handler )
3844 case HANDLER_pict:
3845 case HANDLER_mdta:
3846 case HANDLER_mdir:
3847 /* then it behaves like a container */
3848 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
3849 default:
3850 /* skip parsing, will be seen as empty container */
3851 break;
3855 return 1;
3858 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3860 char i_unused;
3861 VLC_UNUSED(i_unused);
3863 MP4_READBOX_ENTER( MP4_Box_data_iods_t, NULL );
3864 MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3866 MP4_GET1BYTE( i_unused ); /* tag */
3867 MP4_GET1BYTE( i_unused ); /* length */
3869 MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3870 are used for other flags */
3871 MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3872 MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3873 MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3874 MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3875 MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3877 #ifdef MP4_VERBOSE
3878 msg_Dbg( p_stream,
3879 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3880 "visual: %i, graphics: %i",
3881 p_box->data.p_iods->i_object_descriptor >> 6,
3882 p_box->data.p_iods->i_OD_profile_level,
3883 p_box->data.p_iods->i_scene_profile_level,
3884 p_box->data.p_iods->i_audio_profile_level,
3885 p_box->data.p_iods->i_visual_profile_level,
3886 p_box->data.p_iods->i_graphics_profile_level );
3887 #endif
3889 MP4_READBOX_EXIT( 1 );
3892 static int MP4_ReadBox_btrt( stream_t *p_stream, MP4_Box_t *p_box )
3894 MP4_READBOX_ENTER( MP4_Box_data_btrt_t, NULL );
3896 if(i_read != 12)
3897 MP4_READBOX_EXIT( 0 );
3899 MP4_GET4BYTES( p_box->data.p_btrt->i_buffer_size );
3900 MP4_GET4BYTES( p_box->data.p_btrt->i_max_bitrate );
3901 MP4_GET4BYTES( p_box->data.p_btrt->i_avg_bitrate );
3903 MP4_READBOX_EXIT( 1 );
3906 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3908 MP4_READBOX_ENTER( MP4_Box_data_pasp_t, NULL );
3910 MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
3911 MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
3913 #ifdef MP4_VERBOSE
3914 msg_Dbg( p_stream,
3915 "read box: \"paps\" %dx%d",
3916 p_box->data.p_pasp->i_horizontal_spacing,
3917 p_box->data.p_pasp->i_vertical_spacing);
3918 #endif
3920 MP4_READBOX_EXIT( 1 );
3923 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
3925 MP4_READBOX_ENTER( MP4_Box_data_mehd_t, NULL );
3927 MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
3928 if( p_box->data.p_mehd->i_version == 1 )
3929 MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
3930 else /* version == 0 */
3931 MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
3933 #ifdef MP4_VERBOSE
3934 msg_Dbg( p_stream,
3935 "read box: \"mehd\" frag dur. %"PRIu64"",
3936 p_box->data.p_mehd->i_fragment_duration );
3937 #endif
3939 MP4_READBOX_EXIT( 1 );
3942 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
3944 MP4_READBOX_ENTER( MP4_Box_data_trex_t, NULL );
3945 MP4_GETVERSIONFLAGS( p_box->data.p_trex );
3947 MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
3948 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
3949 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
3950 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
3951 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
3953 #ifdef MP4_VERBOSE
3954 msg_Dbg( p_stream,
3955 "read box: \"trex\" trackID: %"PRIu32"",
3956 p_box->data.p_trex->i_track_ID );
3957 #endif
3959 MP4_READBOX_EXIT( 1 );
3962 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
3964 free( p_box->data.p_sdtp->p_sample_table );
3967 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
3969 uint32_t i_sample_count;
3970 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t, MP4_FreeBox_sdtp );
3971 MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
3972 MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
3973 i_sample_count = i_read;
3975 p_sdtp->p_sample_table = malloc( i_sample_count );
3976 if( unlikely(p_sdtp->p_sample_table == NULL) )
3977 MP4_READBOX_EXIT( 0 );
3979 for( uint32_t i = 0; i < i_sample_count; i++ )
3980 MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
3982 #ifdef MP4_VERBOSE
3983 msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
3984 if ( i_sample_count > 3 )
3985 msg_Dbg( p_stream,
3986 "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
3987 p_sdtp->p_sample_table[0],
3988 p_sdtp->p_sample_table[1],
3989 p_sdtp->p_sample_table[2],
3990 p_sdtp->p_sample_table[3] );
3991 #endif
3993 MP4_READBOX_EXIT( 1 );
3996 static int MP4_ReadBox_tsel( stream_t *p_stream, MP4_Box_t *p_box )
3998 MP4_READBOX_ENTER( MP4_Box_data_tsel_t, NULL );
3999 uint32_t i_version;
4000 MP4_GET4BYTES( i_version );
4001 if ( i_version != 0 || i_read < 4 )
4002 MP4_READBOX_EXIT( 0 );
4003 MP4_GET4BYTES( p_box->data.p_tsel->i_switch_group );
4004 /* ignore list of attributes as es are present before switch */
4005 MP4_READBOX_EXIT( 1 );
4008 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
4010 MP4_READBOX_ENTER( MP4_Box_data_mfro_t, NULL );
4012 MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
4013 MP4_GET4BYTES( p_box->data.p_mfro->i_size );
4015 #ifdef MP4_VERBOSE
4016 msg_Dbg( p_stream,
4017 "read box: \"mfro\" size: %"PRIu32"",
4018 p_box->data.p_mfro->i_size);
4019 #endif
4021 MP4_READBOX_EXIT( 1 );
4024 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
4026 free( p_box->data.p_tfra->p_time );
4027 free( p_box->data.p_tfra->p_moof_offset );
4028 free( p_box->data.p_tfra->p_traf_number );
4029 free( p_box->data.p_tfra->p_trun_number );
4030 free( p_box->data.p_tfra->p_sample_number );
4033 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
4035 #define READ_VARIABLE_LENGTH(lengthvar, p_array) switch (lengthvar)\
4037 case 0:\
4038 MP4_GET1BYTE( p_array[i] );\
4039 break;\
4040 case 1:\
4041 MP4_GET2BYTES( *((uint16_t *)&p_array[i*2]) );\
4042 break;\
4043 case 2:\
4044 MP4_GET3BYTES( *((uint32_t *)&p_array[i*4]) );\
4045 break;\
4046 case 3:\
4047 MP4_GET4BYTES( *((uint32_t *)&p_array[i*4]) );\
4048 break;\
4049 default:\
4050 goto error;\
4052 #define FIX_VARIABLE_LENGTH(lengthvar) if ( lengthvar == 3 ) lengthvar = 4
4054 uint32_t i_number_of_entries;
4055 MP4_READBOX_ENTER( MP4_Box_data_tfra_t, MP4_FreeBox_tfra );
4056 MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
4057 MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
4058 if ( p_tfra->i_version > 1 )
4059 MP4_READBOX_EXIT( 0 );
4060 MP4_GET4BYTES( p_tfra->i_track_ID );
4061 uint32_t i_lengths = 0;
4062 MP4_GET4BYTES( i_lengths );
4063 MP4_GET4BYTES( p_tfra->i_number_of_entries );
4064 i_number_of_entries = p_tfra->i_number_of_entries;
4065 p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
4066 p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
4067 p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
4069 size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
4070 p_tfra->p_time = calloc( i_number_of_entries, size );
4071 p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
4073 size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
4074 if ( size == 3 ) size++;
4075 p_tfra->p_traf_number = calloc( i_number_of_entries, size );
4076 size = 1 + p_tfra->i_length_size_of_trun_num;
4077 if ( size == 3 ) size++;
4078 p_tfra->p_trun_number = calloc( i_number_of_entries, size );
4079 size = 1 + p_tfra->i_length_size_of_sample_num;
4080 if ( size == 3 ) size++;
4081 p_tfra->p_sample_number = calloc( i_number_of_entries, size );
4083 if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
4084 || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
4085 goto error;
4087 unsigned i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
4088 + p_tfra->i_length_size_of_trun_num
4089 + p_tfra->i_length_size_of_sample_num;
4091 uint32_t i;
4092 for( i = 0; i < i_number_of_entries; i++ )
4095 if( p_tfra->i_version == 1 )
4097 if ( i_read < i_fields_length + 16 )
4098 break;
4099 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_time[i*2]) );
4100 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_moof_offset[i*2]) );
4102 else
4104 if ( i_read < i_fields_length + 8 )
4105 break;
4106 MP4_GET4BYTES( p_tfra->p_time[i] );
4107 MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
4110 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num, p_tfra->p_traf_number);
4111 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num, p_tfra->p_trun_number);
4112 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num, p_tfra->p_sample_number);
4114 if ( i < i_number_of_entries )
4115 i_number_of_entries = i;
4117 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num);
4118 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num);
4119 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num);
4121 #ifdef MP4_ULTRA_VERBOSE
4122 for( i = 0; i < i_number_of_entries; i++ )
4124 if( p_tfra->i_version == 0 )
4126 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu32", "
4127 "moof_offset[%"PRIu32"]: %"PRIu32"",
4128 p_tfra->i_track_ID,
4129 i, p_tfra->p_time[i],
4130 i, p_tfra->p_moof_offset[i] );
4132 else
4134 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu64", "
4135 "moof_offset[%"PRIu32"]: %"PRIu64"",
4136 p_tfra->i_track_ID,
4137 i, ((uint64_t *)(p_tfra->p_time))[i],
4138 i, ((uint64_t *)(p_tfra->p_moof_offset))[i] );
4141 #endif
4142 #ifdef MP4_VERBOSE
4143 msg_Dbg( p_stream, "tfra[%"PRIu32"] %"PRIu32" entries",
4144 p_tfra->i_track_ID, i_number_of_entries );
4145 #endif
4147 MP4_READBOX_EXIT( 1 );
4148 error:
4149 MP4_READBOX_EXIT( 0 );
4151 #undef READ_VARIABLE_LENGTH
4152 #undef FIX_VARIABLE_LENGTH
4155 static int MP4_ReadBox_pnot( stream_t *p_stream, MP4_Box_t *p_box )
4157 if ( p_box->i_size != 20 )
4158 return 0;
4159 MP4_READBOX_ENTER( MP4_Box_data_pnot_t, NULL );
4160 MP4_GET4BYTES( p_box->data.p_pnot->i_date );
4161 uint16_t i_version;
4162 MP4_GET2BYTES( i_version );
4163 if ( i_version != 0 )
4164 MP4_READBOX_EXIT( 0 );
4165 MP4_GETFOURCC( p_box->data.p_pnot->i_type );
4166 MP4_GET2BYTES( p_box->data.p_pnot->i_index );
4167 MP4_READBOX_EXIT( 1 );
4170 static int MP4_ReadBox_SA3D( stream_t *p_stream, MP4_Box_t *p_box )
4172 MP4_READBOX_ENTER( MP4_Box_data_SA3D_t, NULL );
4174 uint8_t i_version;
4175 MP4_GET1BYTE( i_version );
4176 if ( i_version != 0 )
4177 MP4_READBOX_EXIT( 0 );
4179 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_type );
4180 MP4_GET4BYTES( p_box->data.p_SA3D->i_ambisonic_order );
4181 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_channel_ordering );
4182 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_normalization );
4183 MP4_GET4BYTES( p_box->data.p_SA3D->i_num_channels );
4184 MP4_READBOX_EXIT( 1 );
4187 static void MP4_FreeBox_Reference( MP4_Box_t *p_box )
4189 MP4_Box_data_refbox_t *p_data = p_box->data.p_refbox;
4190 free( p_data->p_references );
4193 static int MP4_ReadBox_Reference( stream_t *p_stream, MP4_Box_t *p_box )
4195 MP4_READBOX_ENTER( MP4_Box_data_refbox_t, MP4_FreeBox_Reference );
4196 MP4_Box_data_refbox_t *p_data = p_box->data.p_refbox;
4198 if( p_box->p_father->data.p_iref->i_flags == 0 )
4199 MP4_GET2BYTES( p_data->i_from_item_id );
4200 else
4201 MP4_GET4BYTES( p_data->i_from_item_id );
4202 MP4_GET2BYTES( p_data->i_reference_count );
4204 if( i_read / sizeof(*p_data->p_references) < p_data->i_reference_count )
4205 MP4_READBOX_EXIT( 0 );
4207 p_data->p_references = malloc( sizeof(*p_data->p_references) *
4208 p_data->i_reference_count );
4209 if( !p_data->p_references )
4210 MP4_READBOX_EXIT( 0 );
4211 for( uint16_t i=0; i<p_data->i_reference_count; i++ )
4213 if( p_box->p_father->data.p_iref == 0 )
4214 MP4_GET2BYTES( p_data->p_references[i].i_to_item_id );
4215 else
4216 MP4_GET4BYTES( p_data->p_references[i].i_to_item_id );
4219 MP4_READBOX_EXIT( 1 );
4222 static int MP4_ReadBox_iref( stream_t *p_stream, MP4_Box_t *p_box )
4224 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_iref_t, 12, NULL );
4225 MP4_Box_data_iref_t *p_data = p_box->data.p_iref;
4226 if( i_read < 4 )
4227 MP4_READBOX_EXIT( 0 );
4229 MP4_GET1BYTE( p_data->i_version );
4230 MP4_GET3BYTES( p_data->i_flags );
4231 if( p_data->i_version > 0 )
4232 MP4_READBOX_EXIT( 0 );
4234 assert( i_read == 0 );
4236 uint32_t i = 0;
4237 uint64_t i_remain = p_box->i_size - 12;
4238 while ( i_remain > 8 )
4240 MP4_Box_t *p_childbox = MP4_ReadBoxUsing( p_stream, p_box,
4241 MP4_ReadBox_Reference );
4242 if( !p_childbox || i_remain < p_childbox->i_size )
4244 MP4_BoxFree( p_childbox );
4245 break;
4248 MP4_BoxAddChild( p_box, p_childbox );
4249 i_remain -= p_childbox->i_size;
4250 i++;
4253 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
4254 MP4_READBOX_EXIT( 0 );
4256 MP4_READBOX_EXIT( 1 );
4259 static void MP4_FreeBox_iloc( MP4_Box_t *p_box )
4261 MP4_Box_data_iloc_t *p_data = p_box->data.p_iloc;
4262 for( uint32_t i=0; i<p_data->i_item_count; i++ )
4263 free( p_data->p_items[i].p_extents );
4264 free( p_data->p_items );
4267 static int MP4_ReadBox_iloc( stream_t *p_stream, MP4_Box_t *p_box )
4269 MP4_READBOX_ENTER( MP4_Box_data_iloc_t, MP4_FreeBox_iloc );
4270 MP4_Box_data_iloc_t *p_data = p_box->data.p_iloc;
4272 uint16_t i_foo;
4274 uint8_t i_version;
4275 uint32_t i_flags;
4276 MP4_GET1BYTE( i_version );
4277 MP4_GET3BYTES( i_flags );
4278 VLC_UNUSED(i_flags);
4280 MP4_GET1BYTE( p_data->i_offset_size );
4281 p_data->i_length_size = p_data->i_offset_size & 0x0F;
4282 p_data->i_offset_size >>= 4;
4283 MP4_GET1BYTE( p_data->i_base_offset_size );
4284 if( i_version == 0 )
4285 p_data->i_index_size = 0;
4286 else
4287 p_data->i_index_size = p_data->i_base_offset_size & 0x0F;
4288 p_data->i_base_offset_size >>= 4;
4290 /* Only accept 0,4,8 */
4291 if( (p_data->i_offset_size & 0xF3) || p_data->i_offset_size > 8 ||
4292 (p_data->i_length_size & 0xF3) || p_data->i_length_size > 8 ||
4293 (p_data->i_base_offset_size & 0xF3) || p_data->i_base_offset_size > 8 ||
4294 (p_data->i_index_size & 0xF3) || p_data->i_index_size > 8 )
4295 MP4_READBOX_EXIT( 0 );
4297 if( i_version < 2 )
4298 MP4_GET2BYTES( p_data->i_item_count );
4299 else if( i_version == 2 )
4300 MP4_GET4BYTES( p_data->i_item_count );
4301 else
4302 MP4_READBOX_EXIT( 0 );
4304 if( i_read / 6 < p_data->i_item_count )
4305 MP4_READBOX_EXIT( 0 );
4307 p_data->p_items = malloc( p_data->i_item_count * sizeof(p_data->p_items[0]) );
4308 if( !p_data->p_items )
4309 MP4_READBOX_EXIT( 0 );
4311 for( uint32_t i=0; i<p_data->i_item_count; i++ )
4313 if( i_version < 2 )
4314 MP4_GET2BYTES( p_data->p_items[i].i_item_id );
4315 else
4316 MP4_GET4BYTES( p_data->p_items[i].i_item_id );
4318 if( i_version > 0 )
4320 MP4_GET2BYTES( i_foo );
4321 p_data->p_items[i].i_construction_method = i_foo & 0x0F;
4324 MP4_GET2BYTES( p_data->p_items[i].i_data_reference_index );
4326 switch( p_data->i_base_offset_size )
4328 case 4: MP4_GET4BYTES( p_data->p_items[i].i_base_offset ); break;
4329 case 8: MP4_GET8BYTES( p_data->p_items[i].i_base_offset ); break;
4330 default: break;
4333 MP4_GET2BYTES( p_data->p_items[i].i_extent_count );
4335 uint64_t i_entrysize = (( i_version > 0 ) ? p_data->i_index_size : 0) +
4336 p_data->i_offset_size + p_data->i_length_size;
4337 if( i_read / i_entrysize < p_data->p_items[i].i_extent_count )
4339 p_data->i_item_count = i;
4340 MP4_READBOX_EXIT( 0 );
4343 p_data->p_items[i].p_extents = malloc( p_data->p_items[i].i_extent_count *
4344 sizeof(p_data->p_items[i].p_extents[0]) );
4345 for( uint16_t j=0; j<p_data->p_items[i].i_extent_count; j++ )
4347 if( i_version > 0 )
4349 switch( p_data->i_index_size )
4351 case 4: MP4_GET4BYTES( p_data->p_items[i].p_extents[j].i_extent_index ); break;
4352 case 8: MP4_GET8BYTES( p_data->p_items[i].p_extents[j].i_extent_index ); break;
4353 default: break;
4356 switch( p_data->i_offset_size )
4358 case 4: MP4_GET4BYTES( p_data->p_items[i].p_extents[j].i_extent_offset ); break;
4359 case 8: MP4_GET8BYTES( p_data->p_items[i].p_extents[j].i_extent_offset ); break;
4360 default: break;
4362 switch( p_data->i_length_size )
4364 case 4: MP4_GET4BYTES( p_data->p_items[i].p_extents[j].i_extent_length ); break;
4365 case 8: MP4_GET8BYTES( p_data->p_items[i].p_extents[j].i_extent_length ); break;
4366 default: break;
4371 MP4_READBOX_EXIT( 1 );
4374 static int MP4_ReadBox_iinf( stream_t *p_stream, MP4_Box_t *p_box )
4376 const uint8_t *p_versionpeek;
4377 size_t i_peek = vlc_stream_Peek( p_stream, &p_versionpeek, 9 );
4378 if( i_peek < 9 )
4379 return 0;
4381 size_t i_header = 12 + (( p_versionpeek[8] == 0 ) ? 2 : 4);
4382 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_iinf_t, i_header, NULL );
4383 if( i_read + 8 < i_header )
4384 MP4_READBOX_EXIT( 0 );
4386 uint8_t i_version;
4387 uint32_t i_flags;
4388 MP4_GET1BYTE( i_version );
4389 MP4_GET3BYTES( i_flags ); VLC_UNUSED(i_flags);
4390 if( i_version > 2 )
4391 MP4_READBOX_EXIT( 0 );
4393 if( i_version == 0 )
4394 MP4_GET2BYTES( p_box->data.p_iinf->i_entry_count );
4395 else
4396 MP4_GET4BYTES( p_box->data.p_iinf->i_entry_count );
4398 assert( i_read == 0 );
4400 uint32_t i = 0;
4401 uint64_t i_remain = p_box->i_size - 16;
4402 while ( i_remain > 8 && i < p_box->data.p_iinf->i_entry_count )
4404 MP4_Box_t *p_childbox = MP4_ReadBox( p_stream, p_box );
4405 if( !p_childbox || i_remain < p_childbox->i_size )
4407 MP4_BoxFree( p_childbox );
4408 p_box->data.p_iinf->i_entry_count = i;
4409 break;
4412 MP4_BoxAddChild( p_box, p_childbox );
4413 i_remain -= p_childbox->i_size;
4414 i++;
4417 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
4418 MP4_READBOX_EXIT( 0 );
4420 MP4_READBOX_EXIT( 1 );
4423 static void MP4_FreeBox_infe( MP4_Box_t *p_box )
4425 MP4_Box_data_infe_t *p_data = p_box->data.p_infe;
4426 free( p_data->psz_content_encoding );
4427 free( p_data->psz_content_type );
4428 free( p_data->psz_item_name );
4429 free( p_data->psz_item_uri_type );
4432 static int MP4_ReadBox_infe( stream_t *p_stream, MP4_Box_t *p_box )
4434 MP4_READBOX_ENTER( MP4_Box_data_infe_t, MP4_FreeBox_infe );
4435 MP4_Box_data_infe_t *p_data = p_box->data.p_infe;
4437 uint8_t i_version;
4438 MP4_GET1BYTE( i_version );
4439 MP4_GET3BYTES( p_data->i_flags );
4440 if( i_version > 3 )
4441 MP4_READBOX_EXIT( 0 );
4443 if( i_version < 2 )
4445 MP4_GET2BYTES( p_data->i_item_id );
4446 MP4_GET2BYTES( p_data->i_item_protection_index );
4447 p_data->psz_item_name = mp4_getstringz( &p_peek, &i_read );
4448 if( i_read > 0 )
4450 p_data->psz_content_type = mp4_getstringz( &p_peek, &i_read );
4451 if( i_read > 0 )
4452 p_data->psz_content_encoding = mp4_getstringz( &p_peek, &i_read );
4455 //if( i_version == 1 )
4457 /* extensions, we do not care */
4460 else
4462 if( i_version == 2 )
4463 MP4_GET2BYTES( p_data->i_item_id );
4464 else
4465 MP4_GET4BYTES( p_data->i_item_id );
4466 MP4_GET2BYTES( p_data->i_item_protection_index );
4467 MP4_GETFOURCC( p_data->item_type );
4468 p_data->psz_item_name = mp4_getstringz( &p_peek, &i_read );
4469 if( p_data->item_type == VLC_FOURCC('m','i','m','e') )
4471 p_data->psz_content_type = mp4_getstringz( &p_peek, &i_read );
4472 if( i_read > 0 )
4473 p_data->psz_content_encoding = mp4_getstringz( &p_peek, &i_read );
4475 else if( p_data->item_type == VLC_FOURCC('u','r','i',' ') )
4477 p_data->psz_item_uri_type = mp4_getstringz( &p_peek, &i_read );
4481 MP4_READBOX_EXIT( 1 );
4484 static int MP4_ReadBox_pitm( stream_t *p_stream, MP4_Box_t *p_box )
4486 MP4_READBOX_ENTER( MP4_Box_data_pitm_t, NULL );
4487 MP4_Box_data_pitm_t *p_data = p_box->data.p_pitm;
4489 uint8_t i_version;
4490 uint32_t i_flags;
4491 MP4_GET1BYTE( i_version );
4492 MP4_GET3BYTES( i_flags ); VLC_UNUSED(i_flags);
4494 if( i_version == 0 )
4495 MP4_GET2BYTES( p_data->i_item_id );
4496 else
4497 MP4_GET4BYTES( p_data->i_item_id );
4499 MP4_READBOX_EXIT( 1 );
4502 static int MP4_ReadBox_ispe( stream_t *p_stream, MP4_Box_t *p_box )
4504 MP4_READBOX_ENTER( MP4_Box_data_ispe_t, NULL );
4505 MP4_Box_data_ispe_t *p_data = p_box->data.p_ispe;
4507 uint8_t i_version;
4508 uint32_t i_flags;
4509 MP4_GET1BYTE( i_version );
4510 MP4_GET3BYTES( i_flags ); VLC_UNUSED(i_flags);
4511 if( i_version > 0 )
4512 MP4_READBOX_EXIT( 0 );
4514 MP4_GET4BYTES( p_data->i_width );
4515 MP4_GET4BYTES( p_data->i_height );
4517 MP4_READBOX_EXIT( 1 );
4520 static void MP4_FreeBox_ipma( MP4_Box_t *p_box )
4522 MP4_Box_data_ipma_t *p_data = p_box->data.p_ipma;
4523 for( uint32_t i=0; i<p_data->i_entry_count; i++ )
4524 free( p_data->p_entries[i].p_assocs );
4525 free( p_data->p_entries );
4528 static int MP4_ReadBox_ipma( stream_t *p_stream, MP4_Box_t *p_box )
4530 MP4_READBOX_ENTER( MP4_Box_data_ipma_t, MP4_FreeBox_ipma );
4531 MP4_Box_data_ipma_t *p_data = p_box->data.p_ipma;
4533 uint8_t i_version;
4534 uint32_t i_flags;
4535 MP4_GET1BYTE( i_version );
4536 MP4_GET3BYTES( i_flags );
4538 MP4_GET4BYTES( p_data->i_entry_count );
4539 if( (i_read / ((i_version < 1) ? 3 : 5) < p_data->i_entry_count) )
4541 p_data->i_entry_count = 0;
4542 MP4_READBOX_EXIT( 0 );
4545 p_data->p_entries = malloc( sizeof(p_data->p_entries[0]) * p_data->i_entry_count );
4546 if( !p_data->p_entries )
4548 p_data->i_entry_count = 0;
4549 MP4_READBOX_EXIT( 0 );
4552 for( uint32_t i=0; i<p_data->i_entry_count; i++ )
4554 if( i_read < ((i_version < 1) ? 3 : 5) )
4556 p_data->i_entry_count = i;
4557 MP4_READBOX_EXIT( 0 );
4559 if( i_version < 1 )
4560 MP4_GET2BYTES( p_data->p_entries[i].i_item_id );
4561 else
4562 MP4_GET4BYTES( p_data->p_entries[i].i_item_id );
4563 MP4_GET1BYTE( p_data->p_entries[i].i_association_count );
4565 if( i_read / ((i_flags & 0x01) ? 2 : 1) <
4566 p_data->p_entries[i].i_association_count )
4568 p_data->i_entry_count = i;
4569 MP4_READBOX_EXIT( 0 );
4572 p_data->p_entries[i].p_assocs =
4573 malloc( sizeof(p_data->p_entries[i].p_assocs[0]) *
4574 p_data->p_entries[i].i_association_count );
4575 if( !p_data->p_entries[i].p_assocs )
4577 p_data->p_entries[i].i_association_count = 0;
4578 p_data->i_entry_count = i;
4579 MP4_READBOX_EXIT( 0 );
4582 for( uint8_t j=0; j<p_data->p_entries[i].i_association_count; j++ )
4584 MP4_GET1BYTE( p_data->p_entries[i].p_assocs[j].i_property_index );
4585 p_data->p_entries[i].p_assocs[j].b_essential =
4586 p_data->p_entries[i].p_assocs[j].i_property_index & 0x80;
4587 p_data->p_entries[i].p_assocs[j].i_property_index &= 0x7F;
4588 if( i_flags & 0x01 )
4590 p_data->p_entries[i].p_assocs[j].i_property_index <<= 8;
4591 uint8_t i_low;
4592 MP4_GET1BYTE( i_low );
4593 p_data->p_entries[i].p_assocs[j].i_property_index |= i_low;
4598 MP4_READBOX_EXIT( 1 );
4601 /* For generic */
4602 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
4604 if( !p_box->p_father )
4606 goto unknown;
4608 if( p_box->p_father->i_type == ATOM_stsd )
4610 MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
4611 MP4_Box_t *p_hdlr;
4613 if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
4614 (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
4616 goto unknown;
4618 switch( p_hdlr->data.p_hdlr->i_handler_type )
4620 case ATOM_soun:
4621 return MP4_ReadBox_sample_soun( p_stream, p_box );
4622 case ATOM_vide:
4623 case ATOM_pict: /* heif */
4624 return MP4_ReadBox_sample_vide( p_stream, p_box );
4625 case ATOM_hint:
4626 return MP4_ReadBox_sample_hint8( p_stream, p_box );
4627 case ATOM_text:
4628 case ATOM_subt:
4629 case ATOM_tx3g:
4630 case ATOM_sbtl:
4631 return MP4_ReadBox_sample_text( p_stream, p_box );
4632 default:
4633 msg_Warn( p_stream,
4634 "unknown handler type in stsd (incompletely loaded)" );
4635 return 1;
4639 unknown:
4640 if MP4_BOX_TYPE_ASCII()
4641 msg_Warn( p_stream,
4642 "unknown box type %4.4s (incompletely loaded)",
4643 (char*)&p_box->i_type );
4644 else
4645 msg_Warn( p_stream,
4646 "unknown box type c%3.3s (incompletely loaded)",
4647 (char*)&p_box->i_type+1 );
4648 p_box->e_flags |= BOX_FLAG_INCOMPLETE;
4650 return 1;
4653 /**** ------------------------------------------------------------------- ****/
4655 static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
4657 if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
4658 return MP4_ReadBox_tfrf( p_stream, p_box );
4659 if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
4660 return MP4_ReadBox_tfxd( p_stream, p_box );
4661 if( !CmpUUID( &p_box->i_uuid, &XML360BoxUUID ) )
4662 return MP4_ReadBox_XML360( p_stream, p_box );
4663 if( !CmpUUID( &p_box->i_uuid, &PS3DDSBoxUUID ) && p_box->i_size == 28 )
4664 return MP4_ReadBox_Binary( p_stream, p_box );
4666 #ifdef MP4_VERBOSE
4667 msg_Warn( p_stream, "Unknown uuid type box: "
4668 "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-"
4669 "%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
4670 p_box->i_uuid.b[0], p_box->i_uuid.b[1], p_box->i_uuid.b[2], p_box->i_uuid.b[3],
4671 p_box->i_uuid.b[4], p_box->i_uuid.b[5], p_box->i_uuid.b[6], p_box->i_uuid.b[7],
4672 p_box->i_uuid.b[8], p_box->i_uuid.b[9], p_box->i_uuid.b[10], p_box->i_uuid.b[11],
4673 p_box->i_uuid.b[12], p_box->i_uuid.b[13], p_box->i_uuid.b[14], p_box->i_uuid.b[15] );
4674 #else
4675 msg_Warn( p_stream, "Unknown uuid type box" );
4676 #endif
4677 return 1;
4680 /**** ------------------------------------------------------------------- ****/
4681 /**** "Higher level" Functions ****/
4682 /**** ------------------------------------------------------------------- ****/
4684 static const struct
4686 uint32_t i_type;
4687 int (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
4688 uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
4689 } MP4_Box_Function [] =
4691 /* Containers */
4692 { ATOM_moov, MP4_ReadBoxContainer, 0 },
4693 { ATOM_foov, MP4_ReadBoxContainer, 0 },
4694 { ATOM_trak, MP4_ReadBoxContainer, ATOM_moov },
4695 { ATOM_trak, MP4_ReadBoxContainer, ATOM_foov },
4696 { ATOM_mdia, MP4_ReadBoxContainer, ATOM_trak },
4697 { ATOM_moof, MP4_ReadBoxContainer, 0 },
4698 { ATOM_minf, MP4_ReadBoxContainer, ATOM_mdia },
4699 { ATOM_stbl, MP4_ReadBoxContainer, ATOM_minf },
4700 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_minf },
4701 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_meta },
4702 { ATOM_edts, MP4_ReadBoxContainer, ATOM_trak },
4703 { ATOM_udta, MP4_ReadBoxContainer, 0 },
4704 { ATOM_nmhd, MP4_ReadBoxContainer, ATOM_minf },
4705 { ATOM_hnti, MP4_ReadBoxContainer, ATOM_udta },
4706 { ATOM_rmra, MP4_ReadBoxContainer, ATOM_moov },
4707 { ATOM_rmda, MP4_ReadBoxContainer, ATOM_rmra },
4708 { ATOM_tref, MP4_ReadBoxContainer, ATOM_trak },
4709 { ATOM_gmhd, MP4_ReadBoxContainer, ATOM_minf },
4710 { ATOM_wave, MP4_ReadBoxContainer, ATOM_stsd },
4711 { ATOM_wave, MP4_ReadBoxContainer, ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
4712 { ATOM_wave, MP4_ReadBoxContainer, ATOM_WMA2 }, /* flip4mac */
4713 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in24 },
4714 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in32 },
4715 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl32 },
4716 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl64 },
4717 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDMC },
4718 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDM2 },
4719 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiFL }, /* XiphQT */
4720 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiVs }, /* XiphQT */
4721 { ATOM_ilst, MP4_ReadBox_ilst, ATOM_meta },
4722 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_moov },
4723 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_ftyp },
4725 /* specific box */
4726 { ATOM_ftyp, MP4_ReadBox_ftyp, 0 },
4727 { ATOM_styp, MP4_ReadBox_ftyp, 0 },
4728 { ATOM_cmov, MP4_ReadBox_cmov, 0 },
4729 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_moov },
4730 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_foov },
4731 { ATOM_tkhd, MP4_ReadBox_tkhd, ATOM_trak },
4732 { ATOM_load, MP4_ReadBox_load, ATOM_trak },
4733 { ATOM_mdhd, MP4_ReadBox_mdhd, ATOM_mdia },
4734 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_mdia },
4735 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_meta },
4736 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_minf },
4737 { ATOM_vmhd, MP4_ReadBox_vmhd, ATOM_minf },
4738 { ATOM_smhd, MP4_ReadBox_smhd, ATOM_minf },
4739 { ATOM_hmhd, MP4_ReadBox_hmhd, ATOM_minf },
4740 { ATOM_alis, MP4_ReadBoxSkip, ATOM_dref },
4741 { ATOM_url, MP4_ReadBox_url, 0 },
4742 { ATOM_urn, MP4_ReadBox_urn, 0 },
4743 { ATOM_dref, MP4_ReadBox_LtdContainer, 0 },
4744 { ATOM_stts, MP4_ReadBox_stts, ATOM_stbl },
4745 { ATOM_ctts, MP4_ReadBox_ctts, ATOM_stbl },
4746 { ATOM_cslg, MP4_ReadBox_cslg, ATOM_stbl },
4747 { ATOM_stsd, MP4_ReadBox_LtdContainer, ATOM_stbl },
4748 { ATOM_stsz, MP4_ReadBox_stsz, ATOM_stbl },
4749 { ATOM_stsc, MP4_ReadBox_stsc, ATOM_stbl },
4750 { ATOM_stco, MP4_ReadBox_stco_co64, ATOM_stbl },
4751 { ATOM_co64, MP4_ReadBox_stco_co64, ATOM_stbl },
4752 { ATOM_stss, MP4_ReadBox_stss, ATOM_stbl },
4753 { ATOM_stsh, MP4_ReadBox_stsh, ATOM_stbl },
4754 { ATOM_stdp, MP4_ReadBox_stdp, 0 },
4755 { ATOM_elst, MP4_ReadBox_elst, ATOM_edts },
4756 { ATOM_cprt, MP4_ReadBox_cprt, 0 },
4757 { ATOM_esds, MP4_ReadBox_esds, ATOM_wave }, /* mp4a in wave chunk */
4758 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4a },
4759 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4v },
4760 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4s },
4761 { ATOM_dcom, MP4_ReadBox_dcom, 0 },
4762 { ATOM_dfLa, MP4_ReadBox_Binary, ATOM_fLaC },
4763 { ATOM_cmvd, MP4_ReadBox_cmvd, 0 },
4764 { ATOM_av1C, MP4_ReadBox_Binary, ATOM_av01 },
4765 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc1 },
4766 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc3 },
4767 { ATOM_hvcC, MP4_ReadBox_Binary, 0 },
4768 { ATOM_jpeC, MP4_ReadBox_Binary, 0 }, /* heif */
4769 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp08 },
4770 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp09 },
4771 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp10 },
4772 { ATOM_SmDm, MP4_ReadBox_SmDm, ATOM_vpcC }, /* SMPTE2086 mastering display */
4773 { ATOM_mdcv, MP4_ReadBox_SmDm, 0 }, /* */
4774 { ATOM_CoLL, MP4_ReadBox_CoLL, ATOM_vpcC }, /* CEA861-3 light level */
4775 { ATOM_clli, MP4_ReadBox_CoLL, 0 }, /* */
4776 { ATOM_dac3, MP4_ReadBox_dac3, 0 },
4777 { ATOM_dec3, MP4_ReadBox_dec3, 0 },
4778 { ATOM_dvc1, MP4_ReadBox_dvc1, ATOM_vc1 },
4779 { ATOM_fiel, MP4_ReadBox_fiel, 0 },
4780 { ATOM_glbl, MP4_ReadBox_Binary, ATOM_FFV1 },
4781 { ATOM_enda, MP4_ReadBox_enda, 0 },
4782 { ATOM_iods, MP4_ReadBox_iods, 0 },
4783 { ATOM_pasp, MP4_ReadBox_pasp, 0 },
4784 { ATOM_btrt, MP4_ReadBox_btrt, 0 }, /* codecs bitrate stsd/????/btrt */
4785 { ATOM_keys, MP4_ReadBox_keys, ATOM_meta },
4786 { ATOM_colr, MP4_ReadBox_colr, 0 },
4787 { ATOM_irot, MP4_ReadBox_irot, 0 }, /* heif */
4789 /* XiphQT */
4790 { ATOM_vCtH, MP4_ReadBox_Binary, ATOM_wave },
4791 { ATOM_vCtC, MP4_ReadBox_Binary, ATOM_wave },
4792 { ATOM_vCtd, MP4_ReadBox_Binary, ATOM_wave },
4793 { ATOM_fCtS, MP4_ReadBox_Binary, ATOM_wave },
4795 /* Samples groups specific information */
4796 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_stbl },
4797 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_traf },
4798 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_stbl },
4799 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_traf },
4801 /* Quicktime preview atoms, all at root */
4802 { ATOM_pnot, MP4_ReadBox_pnot, 0 },
4803 { ATOM_pict, MP4_ReadBox_Binary, 0 },
4804 { ATOM_PICT, MP4_ReadBox_Binary, 0 },
4806 /* Nothing to do with this box */
4807 { ATOM_mdat, MP4_ReadBoxSkip, 0 },
4808 { ATOM_skip, MP4_ReadBoxSkip, 0 },
4809 { ATOM_free, MP4_ReadBoxSkip, 0 },
4810 { ATOM_wide, MP4_ReadBoxSkip, 0 },
4811 { ATOM_binm, MP4_ReadBoxSkip, 0 },
4813 /* Subtitles */
4814 { ATOM_tx3g, MP4_ReadBox_sample_text, ATOM_sbtl },
4815 { ATOM_tx3g, MP4_ReadBox_sample_text, ATOM_text },
4816 { ATOM_c608, MP4_ReadBox_sample_clcp, ATOM_stsd },
4817 //{ ATOM_text, MP4_ReadBox_sample_text, 0 },
4818 /* In sample WebVTT subtitle atoms. No ATOM_wvtt in normal parsing */
4819 { ATOM_vttc, MP4_ReadBoxContainer, ATOM_wvtt },
4820 { ATOM_payl, MP4_ReadBox_Binary, ATOM_vttc },
4822 /* for codecs */
4823 { ATOM_soun, MP4_ReadBox_sample_soun, ATOM_stsd },
4824 { ATOM_agsm, MP4_ReadBox_sample_soun, ATOM_stsd },
4825 { ATOM_ac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4826 { ATOM_AC3, MP4_ReadBox_sample_soun, ATOM_stsd },
4827 { ATOM_eac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4828 { ATOM_fLaC, MP4_ReadBox_sample_soun, ATOM_stsd },
4829 { ATOM_lpcm, MP4_ReadBox_sample_soun, ATOM_stsd },
4830 { ATOM_ms02, MP4_ReadBox_sample_soun, ATOM_stsd },
4831 { ATOM_ms11, MP4_ReadBox_sample_soun, ATOM_stsd },
4832 { ATOM_ms55, MP4_ReadBox_sample_soun, ATOM_stsd },
4833 { ATOM__mp3, MP4_ReadBox_sample_soun, ATOM_stsd },
4834 { ATOM_mp4a, MP4_ReadBox_sample_soun, ATOM_stsd },
4835 { ATOM_twos, MP4_ReadBox_sample_soun, ATOM_stsd },
4836 { ATOM_sowt, MP4_ReadBox_sample_soun, ATOM_stsd },
4837 { ATOM_QDMC, MP4_ReadBox_sample_soun, ATOM_stsd },
4838 { ATOM_QDM2, MP4_ReadBox_sample_soun, ATOM_stsd },
4839 { ATOM_ima4, MP4_ReadBox_sample_soun, ATOM_stsd },
4840 { ATOM_IMA4, MP4_ReadBox_sample_soun, ATOM_stsd },
4841 { ATOM_dvi, MP4_ReadBox_sample_soun, ATOM_stsd },
4842 { ATOM_alaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4843 { ATOM_ulaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4844 { ATOM_MAC3, MP4_ReadBox_sample_soun, ATOM_stsd },
4845 { ATOM_MAC6, MP4_ReadBox_sample_soun, ATOM_stsd },
4846 { ATOM_Qclp, MP4_ReadBox_sample_soun, ATOM_stsd },
4847 { ATOM_samr, MP4_ReadBox_sample_soun, ATOM_stsd },
4848 { ATOM_sawb, MP4_ReadBox_sample_soun, ATOM_stsd },
4849 { ATOM_OggS, MP4_ReadBox_sample_soun, ATOM_stsd },
4850 { ATOM_alac, MP4_ReadBox_sample_soun, ATOM_stsd },
4851 { ATOM_WMA2, MP4_ReadBox_sample_soun, ATOM_stsd }, /* flip4mac */
4852 { ATOM_wma, MP4_ReadBox_sample_soun, ATOM_stsd }, /* ismv wmapro */
4853 { ATOM_Opus, MP4_ReadBox_sample_soun, ATOM_stsd },
4854 /* Sound extensions */
4855 { ATOM_chan, MP4_ReadBox_stsdext_chan, 0 },
4856 { ATOM_WMA2, MP4_ReadBox_WMA2, ATOM_wave }, /* flip4mac */
4857 { ATOM_dOps, MP4_ReadBox_Binary, ATOM_Opus },
4858 { ATOM_wfex, MP4_ReadBox_WMA2, ATOM_wma }, /* ismv formatex */
4860 /* Both uncompressed sound and video */
4861 { ATOM_raw, MP4_ReadBox_default, ATOM_stsd },
4863 { ATOM_drmi, MP4_ReadBox_sample_vide, ATOM_stsd },
4864 { ATOM_vide, MP4_ReadBox_sample_vide, ATOM_stsd },
4865 { ATOM_mp4v, MP4_ReadBox_sample_vide, ATOM_stsd },
4866 { ATOM_SVQ1, MP4_ReadBox_sample_vide, ATOM_stsd },
4867 { ATOM_SVQ3, MP4_ReadBox_sample_vide, ATOM_stsd },
4868 { ATOM_ZyGo, MP4_ReadBox_sample_vide, ATOM_stsd },
4869 { ATOM_DIVX, MP4_ReadBox_sample_vide, ATOM_stsd },
4870 { ATOM_XVID, MP4_ReadBox_sample_vide, ATOM_stsd },
4871 { ATOM_h263, MP4_ReadBox_sample_vide, ATOM_stsd },
4872 { ATOM_s263, MP4_ReadBox_sample_vide, ATOM_stsd },
4873 { ATOM_cvid, MP4_ReadBox_sample_vide, ATOM_stsd },
4874 { ATOM_3IV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4875 { ATOM_3iv1, MP4_ReadBox_sample_vide, ATOM_stsd },
4876 { ATOM_3IV2, MP4_ReadBox_sample_vide, ATOM_stsd },
4877 { ATOM_3iv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4878 { ATOM_3IVD, MP4_ReadBox_sample_vide, ATOM_stsd },
4879 { ATOM_3ivd, MP4_ReadBox_sample_vide, ATOM_stsd },
4880 { ATOM_3VID, MP4_ReadBox_sample_vide, ATOM_stsd },
4881 { ATOM_3vid, MP4_ReadBox_sample_vide, ATOM_stsd },
4882 { ATOM_FFV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4883 { ATOM_mjpa, MP4_ReadBox_sample_vide, ATOM_stsd },
4884 { ATOM_mjpb, MP4_ReadBox_sample_vide, ATOM_stsd },
4885 { ATOM_qdrw, MP4_ReadBox_sample_vide, ATOM_stsd },
4886 { ATOM_mp2v, MP4_ReadBox_sample_vide, ATOM_stsd },
4887 { ATOM_hdv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4888 { ATOM_WMV3, MP4_ReadBox_sample_vide, ATOM_stsd },
4890 { ATOM_mjqt, MP4_ReadBox_default, 0 }, /* found in mjpa/b */
4891 { ATOM_mjht, MP4_ReadBox_default, 0 },
4893 { ATOM_dvc, MP4_ReadBox_sample_vide, ATOM_stsd },
4894 { ATOM_dvp, MP4_ReadBox_sample_vide, ATOM_stsd },
4895 { ATOM_dv5n, MP4_ReadBox_sample_vide, ATOM_stsd },
4896 { ATOM_dv5p, MP4_ReadBox_sample_vide, ATOM_stsd },
4897 { ATOM_VP31, MP4_ReadBox_sample_vide, ATOM_stsd },
4898 { ATOM_vp31, MP4_ReadBox_sample_vide, ATOM_stsd },
4899 { ATOM_h264, MP4_ReadBox_sample_vide, ATOM_stsd },
4901 { ATOM_jpeg, MP4_ReadBox_sample_vide, ATOM_stsd },
4902 { ATOM_vc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4903 { ATOM_av01, MP4_ReadBox_sample_vide, ATOM_stsd },
4904 { ATOM_avc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4905 { ATOM_avc3, MP4_ReadBox_sample_vide, ATOM_stsd },
4907 { ATOM_rrtp, MP4_ReadBox_sample_hint8, ATOM_stsd },
4909 { ATOM_yv12, MP4_ReadBox_sample_vide, 0 },
4910 { ATOM_yuv2, MP4_ReadBox_sample_vide, 0 },
4912 { ATOM_strf, MP4_ReadBox_strf, ATOM_WVC1 }, /* MS smooth */
4913 { ATOM_strf, MP4_ReadBox_strf, ATOM_H264 }, /* MS smooth */
4915 { ATOM_strf, MP4_ReadBox_strf, ATOM_WMV3 }, /* flip4mac */
4916 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_WMV3 }, /* flip4mac */
4917 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_wave }, /* flip4mac */
4919 { ATOM_mp4s, MP4_ReadBox_sample_mp4s, ATOM_stsd },
4921 /* XXX there is 2 box where we could find this entry stbl and tref*/
4922 { ATOM_hint, MP4_ReadBox_default, ATOM_tref },
4923 { ATOM_hint, MP4_ReadBox_default, ATOM_stbl },
4925 /* found in tref box */
4926 { ATOM_dpnd, MP4_ReadBox_default, ATOM_tref },
4927 { ATOM_ipir, MP4_ReadBox_default, ATOM_tref },
4928 { ATOM_mpod, MP4_ReadBox_default, ATOM_tref },
4929 { ATOM_chap, MP4_ReadBox_tref_generic, ATOM_tref },
4931 /* found in hnti */
4932 { ATOM_rtp, MP4_ReadBox_rtp, ATOM_hnti },
4933 { ATOM_sdp, MP4_ReadBox_sdp, ATOM_hnti },
4935 /* found in rrtp sample description */
4936 { ATOM_tims, MP4_ReadBox_tims, 0 },
4937 { ATOM_tsro, MP4_ReadBox_tsro, 0 },
4938 { ATOM_tssy, MP4_ReadBox_tssy, 0 },
4940 /* found in rmra/rmda */
4941 { ATOM_rdrf, MP4_ReadBox_rdrf, ATOM_rmda },
4942 { ATOM_rmdr, MP4_ReadBox_rmdr, ATOM_rmda },
4943 { ATOM_rmqu, MP4_ReadBox_rmqu, ATOM_rmda },
4944 { ATOM_rmvc, MP4_ReadBox_rmvc, ATOM_rmda },
4946 { ATOM_drms, MP4_ReadBox_sample_soun, 0 },
4947 { ATOM_sinf, MP4_ReadBoxContainer, 0 },
4948 { ATOM_schi, MP4_ReadBoxContainer, 0 },
4949 { ATOM_user, MP4_ReadBox_drms, 0 },
4950 { ATOM_key, MP4_ReadBox_drms, 0 },
4951 { ATOM_iviv, MP4_ReadBox_drms, 0 },
4952 { ATOM_priv, MP4_ReadBox_drms, 0 },
4953 { ATOM_frma, MP4_ReadBox_frma, ATOM_sinf }, /* and rinf */
4954 { ATOM_frma, MP4_ReadBox_frma, ATOM_wave }, /* flip4mac */
4955 { ATOM_skcr, MP4_ReadBox_skcr, 0 },
4957 /* ilst meta tags */
4958 { ATOM_0xa9ART, MP4_ReadBox_Metadata, ATOM_ilst },
4959 { ATOM_0xa9alb, MP4_ReadBox_Metadata, ATOM_ilst },
4960 { ATOM_0xa9cmt, MP4_ReadBox_Metadata, ATOM_ilst },
4961 { ATOM_0xa9com, MP4_ReadBox_Metadata, ATOM_ilst },
4962 { ATOM_0xa9cpy, MP4_ReadBox_Metadata, ATOM_ilst },
4963 { ATOM_0xa9day, MP4_ReadBox_Metadata, ATOM_ilst },
4964 { ATOM_0xa9des, MP4_ReadBox_Metadata, ATOM_ilst },
4965 { ATOM_0xa9enc, MP4_ReadBox_Metadata, ATOM_ilst },
4966 { ATOM_0xa9gen, MP4_ReadBox_Metadata, ATOM_ilst },
4967 { ATOM_0xa9grp, MP4_ReadBox_Metadata, ATOM_ilst },
4968 { ATOM_0xa9lyr, MP4_ReadBox_Metadata, ATOM_ilst },
4969 { ATOM_0xa9nam, MP4_ReadBox_Metadata, ATOM_ilst },
4970 { ATOM_0xa9too, MP4_ReadBox_Metadata, ATOM_ilst },
4971 { ATOM_0xa9trk, MP4_ReadBox_Metadata, ATOM_ilst },
4972 { ATOM_0xa9wrt, MP4_ReadBox_Metadata, ATOM_ilst },
4973 { ATOM_aART, MP4_ReadBox_Metadata, ATOM_ilst },
4974 { ATOM_atID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4975 { ATOM_cnID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4976 { ATOM_covr, MP4_ReadBoxContainer, ATOM_ilst },
4977 { ATOM_desc, MP4_ReadBox_Metadata, ATOM_ilst },
4978 { ATOM_disk, MP4_ReadBox_Metadata, ATOM_ilst },
4979 { ATOM_flvr, MP4_ReadBox_Metadata, ATOM_ilst },
4980 { ATOM_gnre, MP4_ReadBox_Metadata, ATOM_ilst },
4981 { ATOM_rtng, MP4_ReadBox_Metadata, ATOM_ilst },
4982 { ATOM_trkn, MP4_ReadBox_Metadata, ATOM_ilst },
4983 { ATOM_xid_, MP4_ReadBox_Metadata, ATOM_ilst },
4984 { ATOM_gshh, MP4_ReadBox_Metadata, ATOM_ilst }, /* YouTube gs?? */
4985 { ATOM_gspm, MP4_ReadBox_Metadata, ATOM_ilst },
4986 { ATOM_gspu, MP4_ReadBox_Metadata, ATOM_ilst },
4987 { ATOM_gssd, MP4_ReadBox_Metadata, ATOM_ilst },
4988 { ATOM_gsst, MP4_ReadBox_Metadata, ATOM_ilst },
4989 { ATOM_gstd, MP4_ReadBox_Metadata, ATOM_ilst },
4990 { ATOM_ITUN, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunesInfo */
4992 /* udta */
4993 { ATOM_0x40PRM, MP4_ReadBox_Binary, ATOM_udta },
4994 { ATOM_0x40PRQ, MP4_ReadBox_Binary, ATOM_udta },
4995 { ATOM_0xa9ART, MP4_ReadBox_Binary, ATOM_udta },
4996 { ATOM_0xa9alb, MP4_ReadBox_Binary, ATOM_udta },
4997 { ATOM_0xa9ard, MP4_ReadBox_Binary, ATOM_udta },
4998 { ATOM_0xa9arg, MP4_ReadBox_Binary, ATOM_udta },
4999 { ATOM_0xa9aut, MP4_ReadBox_Binary, ATOM_udta },
5000 { ATOM_0xa9cak, MP4_ReadBox_Binary, ATOM_udta },
5001 { ATOM_0xa9cmt, MP4_ReadBox_Binary, ATOM_udta },
5002 { ATOM_0xa9con, MP4_ReadBox_Binary, ATOM_udta },
5003 { ATOM_0xa9com, MP4_ReadBox_Binary, ATOM_udta },
5004 { ATOM_0xa9cpy, MP4_ReadBox_Binary, ATOM_udta },
5005 { ATOM_0xa9day, MP4_ReadBox_Binary, ATOM_udta },
5006 { ATOM_0xa9des, MP4_ReadBox_Binary, ATOM_udta },
5007 { ATOM_0xa9dir, MP4_ReadBox_Binary, ATOM_udta },
5008 { ATOM_0xa9dis, MP4_ReadBox_Binary, ATOM_udta },
5009 { ATOM_0xa9dsa, MP4_ReadBox_Binary, ATOM_udta },
5010 { ATOM_0xa9fmt, MP4_ReadBox_Binary, ATOM_udta },
5011 { ATOM_0xa9gen, MP4_ReadBox_Binary, ATOM_udta },
5012 { ATOM_0xa9grp, MP4_ReadBox_Binary, ATOM_udta },
5013 { ATOM_0xa9hst, MP4_ReadBox_Binary, ATOM_udta },
5014 { ATOM_0xa9inf, MP4_ReadBox_Binary, ATOM_udta },
5015 { ATOM_0xa9isr, MP4_ReadBox_Binary, ATOM_udta },
5016 { ATOM_0xa9lab, MP4_ReadBox_Binary, ATOM_udta },
5017 { ATOM_0xa9lal, MP4_ReadBox_Binary, ATOM_udta },
5018 { ATOM_0xa9lnt, MP4_ReadBox_Binary, ATOM_udta },
5019 { ATOM_0xa9lyr, MP4_ReadBox_Binary, ATOM_udta },
5020 { ATOM_0xa9mak, MP4_ReadBox_Binary, ATOM_udta },
5021 { ATOM_0xa9mal, MP4_ReadBox_Binary, ATOM_udta },
5022 { ATOM_0xa9mod, MP4_ReadBox_Binary, ATOM_udta },
5023 { ATOM_0xa9nam, MP4_ReadBox_Binary, ATOM_udta },
5024 { ATOM_0xa9ope, MP4_ReadBox_Binary, ATOM_udta },
5025 { ATOM_0xa9phg, MP4_ReadBox_Binary, ATOM_udta },
5026 { ATOM_0xa9PRD, MP4_ReadBox_Binary, ATOM_udta },
5027 { ATOM_0xa9prd, MP4_ReadBox_Binary, ATOM_udta },
5028 { ATOM_0xa9prf, MP4_ReadBox_Binary, ATOM_udta },
5029 { ATOM_0xa9pub, MP4_ReadBox_Binary, ATOM_udta },
5030 { ATOM_0xa9req, MP4_ReadBox_Binary, ATOM_udta },
5031 { ATOM_0xa9sne, MP4_ReadBox_Binary, ATOM_udta },
5032 { ATOM_0xa9snm, MP4_ReadBox_Binary, ATOM_udta },
5033 { ATOM_0xa9sol, MP4_ReadBox_Binary, ATOM_udta },
5034 { ATOM_0xa9src, MP4_ReadBox_Binary, ATOM_udta },
5035 { ATOM_0xa9st3, MP4_ReadBox_Binary, ATOM_udta },
5036 { ATOM_0xa9swr, MP4_ReadBox_Binary, ATOM_udta },
5037 { ATOM_0xa9thx, MP4_ReadBox_Binary, ATOM_udta },
5038 { ATOM_0xa9too, MP4_ReadBox_Binary, ATOM_udta },
5039 { ATOM_0xa9trk, MP4_ReadBox_Binary, ATOM_udta },
5040 { ATOM_0xa9url, MP4_ReadBox_Binary, ATOM_udta },
5041 { ATOM_0xa9wrn, MP4_ReadBox_Binary, ATOM_udta },
5042 { ATOM_0xa9xpd, MP4_ReadBox_Binary, ATOM_udta },
5043 { ATOM_0xa9xyz, MP4_ReadBox_Binary, ATOM_udta },
5044 { ATOM_chpl, MP4_ReadBox_chpl, ATOM_udta }, /* nero unlabeled chapters list */
5045 { ATOM_MCPS, MP4_ReadBox_Binary, ATOM_udta },
5046 { ATOM_name, MP4_ReadBox_Binary, ATOM_udta },
5047 { ATOM_vndr, MP4_ReadBox_Binary, ATOM_udta },
5048 { ATOM_SDLN, MP4_ReadBox_Binary, ATOM_udta },
5049 { ATOM_HMMT, MP4_ReadBox_HMMT, ATOM_udta }, /* GoPro HiLight tags */
5051 /* udta, non meta */
5052 { ATOM_tsel, MP4_ReadBox_tsel, ATOM_udta },
5054 /* iTunes/Quicktime meta info */
5055 { ATOM_meta, MP4_ReadBox_meta, 0 },
5056 { ATOM_ID32, MP4_ReadBox_Binary, ATOM_meta }, /* ID3v2 in 3GPP / ETSI TS 126 244 8.3 */
5057 { ATOM_data, MP4_ReadBox_data, 0 }, /* ilst/@too and others, ITUN/data */
5058 { ATOM_mean, MP4_ReadBox_Binary, ATOM_ITUN },
5059 { ATOM_name, MP4_ReadBox_Binary, ATOM_ITUN },
5061 /* found in smoothstreaming */
5062 { ATOM_traf, MP4_ReadBoxContainer, ATOM_moof },
5063 { ATOM_mfra, MP4_ReadBoxContainer, 0 },
5064 { ATOM_mfhd, MP4_ReadBox_mfhd, ATOM_moof },
5065 { ATOM_sidx, MP4_ReadBox_sidx, 0 },
5066 { ATOM_tfhd, MP4_ReadBox_tfhd, ATOM_traf },
5067 { ATOM_trun, MP4_ReadBox_trun, ATOM_traf },
5068 { ATOM_tfdt, MP4_ReadBox_tfdt, ATOM_traf },
5069 { ATOM_trex, MP4_ReadBox_trex, ATOM_mvex },
5070 { ATOM_mehd, MP4_ReadBox_mehd, ATOM_mvex },
5071 { ATOM_sdtp, MP4_ReadBox_sdtp, 0 },
5072 { ATOM_tfra, MP4_ReadBox_tfra, ATOM_mfra },
5073 { ATOM_mfro, MP4_ReadBox_mfro, ATOM_mfra },
5074 { ATOM_uuid, MP4_ReadBox_uuid, 0 },
5076 /* spatial/360°/VR */
5077 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_avc1 },
5078 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_mp4v },
5079 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_avc1 },
5080 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_mp4v },
5081 { ATOM_proj, MP4_ReadBoxContainer, ATOM_sv3d },
5082 { ATOM_prhd, MP4_ReadBox_prhd, ATOM_proj },
5083 { ATOM_equi, MP4_ReadBox_equi, ATOM_proj },
5084 { ATOM_cbmp, MP4_ReadBox_cbmp, ATOM_proj },
5086 /* Ambisonics */
5087 { ATOM_SA3D, MP4_ReadBox_SA3D, 0 },
5089 /* iso4 brand meta references */
5090 { ATOM_iloc, MP4_ReadBox_iloc, ATOM_meta },
5091 { ATOM_iinf, MP4_ReadBox_iinf, ATOM_meta },
5092 { ATOM_infe, MP4_ReadBox_infe, ATOM_iinf },
5093 { ATOM_iref, MP4_ReadBox_iref, ATOM_meta },
5094 { ATOM_pitm, MP4_ReadBox_pitm, ATOM_meta },
5096 /* HEIF specific meta references */
5097 { ATOM_iprp, MP4_ReadBoxContainer, ATOM_meta },
5098 { ATOM_ipco, MP4_ReadBoxContainer, ATOM_iprp },
5099 { ATOM_ispe, MP4_ReadBox_ispe, ATOM_ipco },
5100 { ATOM_ipma, MP4_ReadBox_ipma, ATOM_iprp },
5102 /* Last entry */
5103 { 0, MP4_ReadBox_default, 0 }
5106 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
5108 int i_index;
5110 for( i_index = 0; ; i_index++ )
5112 if ( MP4_Box_Function[i_index].i_parent &&
5113 p_father && p_father->i_type != MP4_Box_Function[i_index].i_parent )
5114 continue;
5116 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
5117 ( MP4_Box_Function[i_index].i_type == 0 ) )
5119 break;
5123 if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
5125 return VLC_EGENERIC;
5128 return VLC_SUCCESS;
5131 static MP4_Box_t *MP4_ReadBoxAllocateCheck( stream_t *p_stream, MP4_Box_t *p_father )
5133 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
5134 if( p_box == NULL )
5135 return NULL;
5137 if( !MP4_PeekBoxHeader( p_stream, p_box ) )
5139 msg_Warn( p_stream, "cannot read one box" );
5140 free( p_box );
5141 return NULL;
5144 if( p_father && p_father->i_size > 0 &&
5145 p_father->i_pos + p_father->i_size < p_box->i_pos + p_box->i_size )
5147 msg_Dbg( p_stream, "out of bound child" );
5148 free( p_box );
5149 return NULL;
5152 if( !p_box->i_size )
5154 msg_Dbg( p_stream, "found an empty box (null size)" );
5155 free( p_box );
5156 return NULL;
5158 p_box->p_father = p_father;
5160 return p_box;
5163 /*****************************************************************************
5164 * MP4_ReadBoxUsing : parse the actual box and the children using handler
5165 *****************************************************************************/
5166 static MP4_Box_t *MP4_ReadBoxUsing( stream_t *p_stream, MP4_Box_t *p_father,
5167 int(*MP4_ReadBox_function)(stream_t *, MP4_Box_t *) )
5169 MP4_Box_t *p_box = MP4_ReadBoxAllocateCheck( p_stream, p_father );
5170 if( !p_box )
5171 return NULL;
5173 if( MP4_ReadBox_function( p_stream, p_box ) != VLC_SUCCESS )
5175 uint64_t i_end = p_box->i_pos + p_box->i_size;
5176 MP4_BoxFree( p_box );
5177 MP4_Seek( p_stream, i_end ); /* Skip the failed box */
5178 return NULL;
5180 return p_box;
5183 /*****************************************************************************
5184 * MP4_ReadBox : parse the actual box and the children
5185 * XXX : Do not go to the next box
5186 *****************************************************************************/
5187 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
5189 MP4_Box_t *p_box = MP4_ReadBoxAllocateCheck( p_stream, p_father );
5190 if( !p_box )
5191 return NULL;
5193 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
5195 uint64_t i_end = p_box->i_pos + p_box->i_size;
5196 MP4_BoxFree( p_box );
5197 MP4_Seek( p_stream, i_end ); /* Skip the failed box */
5198 return NULL;
5200 return p_box;
5203 /*****************************************************************************
5204 * MP4_BoxNew : creates and initializes an arbitrary box
5205 *****************************************************************************/
5206 MP4_Box_t * MP4_BoxNew( uint32_t i_type )
5208 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) );
5209 if( likely( p_box != NULL ) )
5211 p_box->i_type = i_type;
5213 return p_box;
5216 /*****************************************************************************
5217 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
5218 * the children
5219 *****************************************************************************/
5220 void MP4_BoxFree( MP4_Box_t *p_box )
5222 MP4_Box_t *p_child;
5224 if( !p_box )
5225 return; /* hehe */
5227 for( p_child = p_box->p_first; p_child != NULL; )
5229 MP4_Box_t *p_next;
5231 p_next = p_child->p_next;
5232 MP4_BoxFree( p_child );
5233 p_child = p_next;
5236 if( p_box->pf_free )
5237 p_box->pf_free( p_box );
5239 free( p_box->data.p_payload );
5240 free( p_box );
5243 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
5245 /* p_chunk is a virtual root container for the moof and mdat boxes */
5246 MP4_Box_t *p_fakeroot;
5247 MP4_Box_t *p_tmp_box;
5249 p_fakeroot = MP4_BoxNew( ATOM_root );
5250 if( unlikely( p_fakeroot == NULL ) )
5251 return NULL;
5252 p_fakeroot->i_shortsize = 1;
5254 const uint32_t stoplist[] = { ATOM_moov, ATOM_moof, 0 };
5255 MP4_ReadBoxContainerChildren( s, p_fakeroot, stoplist );
5257 p_tmp_box = p_fakeroot->p_first;
5258 if( p_tmp_box == NULL )
5260 MP4_BoxFree( p_fakeroot );
5261 return NULL;
5263 else while( p_tmp_box )
5265 p_fakeroot->i_size += p_tmp_box->i_size;
5266 p_tmp_box = p_tmp_box->p_next;
5269 return p_fakeroot;
5272 /*****************************************************************************
5273 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
5274 *****************************************************************************
5275 * The first box is a virtual box "root" and is the father for all first
5276 * level boxes for the file, a sort of virtual contener
5277 *****************************************************************************/
5278 MP4_Box_t *MP4_BoxGetRoot( stream_t *p_stream )
5280 int i_result;
5282 MP4_Box_t *p_vroot = MP4_BoxNew( ATOM_root );
5283 if( p_vroot == NULL )
5284 return NULL;
5286 p_vroot->i_shortsize = 1;
5287 uint64_t i_size;
5288 if( vlc_stream_GetSize( p_stream, &i_size ) == 0 )
5289 p_vroot->i_size = i_size;
5291 /* First get the moov */
5293 const uint32_t stoplist[] = { ATOM_moov, ATOM_mdat, 0 };
5294 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
5297 /* mdat appeared first */
5298 if( i_result && !MP4_BoxGet( p_vroot, "moov" ) )
5300 bool b_seekable;
5301 if( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_seekable ) != VLC_SUCCESS || !b_seekable )
5303 msg_Err( p_stream, "no moov before mdat and the stream is not seekable" );
5304 goto error;
5307 /* continue loading up to moov */
5308 const uint32_t stoplist[] = { ATOM_moov, 0 };
5309 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
5312 if( !i_result )
5313 goto error;
5315 /* If there is a mvex box, it means fragmented MP4, and we're done */
5316 if( MP4_BoxCount( p_vroot, "moov/mvex" ) > 0 )
5318 /* Read a bit more atoms as we might have an index between moov and moof */
5319 const uint32_t stoplist[] = { ATOM_sidx, 0 };
5320 const uint32_t excludelist[] = { ATOM_moof, ATOM_mdat, 0 };
5321 MP4_ReadBoxContainerChildrenIndexed( p_stream, p_vroot, stoplist, excludelist, false );
5322 return p_vroot;
5325 if( vlc_stream_Tell( p_stream ) + 8 < (uint64_t) stream_Size( p_stream ) )
5327 /* Get the rest of the file */
5328 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, NULL );
5330 if( !i_result )
5331 goto error;
5334 MP4_Box_t *p_moov;
5335 MP4_Box_t *p_cmov;
5337 /* check if there is a cmov, if so replace
5338 compressed moov by uncompressed one */
5339 if( ( ( p_moov = MP4_BoxGet( p_vroot, "moov" ) ) &&
5340 ( p_cmov = MP4_BoxGet( p_vroot, "moov/cmov" ) ) ) ||
5341 ( ( p_moov = MP4_BoxGet( p_vroot, "foov" ) ) &&
5342 ( p_cmov = MP4_BoxGet( p_vroot, "foov/cmov" ) ) ) )
5344 /* rename the compressed moov as a box to skip */
5345 p_moov->i_type = ATOM_skip;
5347 /* get uncompressed p_moov */
5348 p_moov = p_cmov->data.p_cmov->p_moov;
5349 p_cmov->data.p_cmov->p_moov = NULL;
5351 /* make p_root father of this new moov */
5352 p_moov->p_father = p_vroot;
5354 /* insert this new moov box as first child of p_root */
5355 p_moov->p_next = p_vroot->p_first;
5356 p_vroot->p_first = p_moov;
5359 return p_vroot;
5361 error:
5362 MP4_BoxFree( p_vroot );
5363 MP4_Seek( p_stream, 0 );
5364 return NULL;
5368 static void MP4_BoxDumpStructure_Internal( stream_t *s, const MP4_Box_t *p_box,
5369 unsigned int i_level )
5371 const MP4_Box_t *p_child;
5372 uint32_t i_displayedtype = p_box->i_type;
5373 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype)[0] = 'c';
5375 if( !i_level )
5377 msg_Dbg( s, "dumping root Box \"%4.4s\"",
5378 (char*)&i_displayedtype );
5380 else
5382 char str[512];
5383 if( i_level >= (sizeof(str) - 1)/4 )
5384 return;
5386 memset( str, ' ', sizeof(str) );
5387 for( unsigned i = 0; i < i_level; i++ )
5389 str[i*4] = '|';
5392 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
5393 "+ %4.4s size %"PRIu64" offset %" PRIuMAX "%s",
5394 (char*)&i_displayedtype, p_box->i_size,
5395 (uintmax_t)p_box->i_pos,
5396 p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
5397 msg_Dbg( s, "%s", str );
5399 p_child = p_box->p_first;
5400 while( p_child )
5402 MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
5403 p_child = p_child->p_next;
5407 void MP4_BoxDumpStructure( stream_t *s, const MP4_Box_t *p_box )
5409 MP4_BoxDumpStructure_Internal( s, p_box, 0 );
5413 /*****************************************************************************
5414 *****************************************************************************
5416 ** High level methods to acces an MP4 file
5418 *****************************************************************************
5419 *****************************************************************************/
5420 static bool get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
5422 size_t i_len ;
5423 if( !*ppsz_path[0] )
5425 *ppsz_token = NULL;
5426 *pi_number = 0;
5427 return true;
5429 i_len = strcspn( *ppsz_path, "/[" );
5430 if( !i_len && **ppsz_path == '/' )
5432 i_len = 1;
5434 *ppsz_token = strndup( *ppsz_path, i_len );
5435 if( unlikely(!*ppsz_token) )
5436 return false;
5438 *ppsz_path += i_len;
5440 /* Parse the token number token[n] */
5441 if( **ppsz_path == '[' )
5443 (*ppsz_path)++;
5444 *pi_number = strtol( *ppsz_path, NULL, 10 );
5445 while( **ppsz_path && **ppsz_path != ']' )
5447 (*ppsz_path)++;
5449 if( **ppsz_path == ']' )
5451 (*ppsz_path)++;
5454 else
5456 *pi_number = 0;
5459 /* Forward to start of next token */
5460 while( **ppsz_path == '/' )
5462 (*ppsz_path)++;
5465 return true;
5468 static void MP4_BoxGet_Internal( const MP4_Box_t **pp_result, const MP4_Box_t *p_box,
5469 const char *psz_fmt, va_list args)
5471 char *psz_dup;
5472 char *psz_path;
5473 char *psz_token = NULL;
5475 if( !p_box )
5477 *pp_result = NULL;
5478 return;
5481 if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
5482 psz_path = NULL;
5484 if( !psz_path || !psz_path[0] )
5486 free( psz_path );
5487 *pp_result = NULL;
5488 return;
5491 // fprintf( stderr, "path:'%s'\n", psz_path );
5492 psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
5493 for( ; ; )
5495 int i_number;
5497 if( !get_token( &psz_path, &psz_token, &i_number ) )
5498 goto error_box;
5499 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
5500 // psz_path,psz_token,i_number );
5501 if( !psz_token )
5503 free( psz_dup );
5504 *pp_result = p_box;
5505 return;
5507 else
5508 if( !strcmp( psz_token, "/" ) )
5510 /* Find root box */
5511 while( p_box && p_box->i_type != ATOM_root )
5513 p_box = p_box->p_father;
5515 if( !p_box )
5517 goto error_box;
5520 else
5521 if( !strcmp( psz_token, "." ) )
5523 /* Do nothing */
5525 else
5526 if( !strcmp( psz_token, ".." ) )
5528 p_box = p_box->p_father;
5529 if( !p_box )
5531 goto error_box;
5534 else
5535 if( strlen( psz_token ) == 4 )
5537 uint32_t i_fourcc;
5538 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
5539 psz_token[2], psz_token[3] );
5540 p_box = p_box->p_first;
5541 for( ; ; )
5543 if( !p_box )
5545 goto error_box;
5547 if( p_box->i_type == i_fourcc )
5549 if( !i_number )
5551 break;
5553 i_number--;
5555 p_box = p_box->p_next;
5558 else
5559 if( *psz_token == '\0' )
5561 p_box = p_box->p_first;
5562 for( ; ; )
5564 if( !p_box )
5566 goto error_box;
5568 if( !i_number )
5570 break;
5572 i_number--;
5573 p_box = p_box->p_next;
5576 else
5578 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
5579 goto error_box;
5582 free( psz_token );
5585 return;
5587 error_box:
5588 free( psz_token );
5589 free( psz_dup );
5590 *pp_result = NULL;
5591 return;
5594 /*****************************************************************************
5595 * MP4_BoxGet: find a box given a path relative to p_box
5596 *****************************************************************************
5597 * Path Format: . .. / as usual
5598 * [number] to specifie box number ex: trak[12]
5600 * ex: /moov/trak[12]
5601 * ../mdia
5602 *****************************************************************************/
5603 VLC_FORMAT(2, 3)
5604 MP4_Box_t *MP4_BoxGet( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5606 va_list args;
5607 const MP4_Box_t *p_result;
5609 va_start( args, psz_fmt );
5610 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5611 va_end( args );
5613 return( (MP4_Box_t *) p_result );
5616 /*****************************************************************************
5617 * MP4_BoxCount: count box given a path relative to p_box
5618 *****************************************************************************
5619 * Path Format: . .. / as usual
5620 * [number] to specifie box number ex: trak[12]
5622 * ex: /moov/trak[12]
5623 * ../mdia
5624 *****************************************************************************/
5625 VLC_FORMAT(2, 3)
5626 unsigned MP4_BoxCount( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5628 va_list args;
5629 unsigned i_count;
5630 const MP4_Box_t *p_result, *p_next;
5632 va_start( args, psz_fmt );
5633 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5634 va_end( args );
5635 if( !p_result )
5637 return( 0 );
5640 i_count = 1;
5641 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
5643 if( p_next->i_type == p_result->i_type)
5645 i_count++;
5648 return( i_count );