demux: mp4: optionally store sample desc as extradata
[vlc.git] / modules / demux / mp4 / libmp4.c
blobe3e43b64b87e75197270879618e75bc74bfbad6d
1 /*****************************************************************************
2 * libmp4.c : LibMP4 library for mp4 module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
6 * Author: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <vlc_common.h>
28 #include <vlc_stream.h> /* vlc_stream_Peek*/
29 #include <vlc_strings.h> /* vlc_ascii_tolower */
31 #ifdef HAVE_ZLIB_H
32 # include <zlib.h> /* for compressed moov */
33 #endif
35 #include "libmp4.h"
36 #include "languages.h"
37 #include <math.h>
38 #include <assert.h>
40 /* Some assumptions:
41 * The input method HAS to be seekable
44 /* convert 16.16 fixed point to floating point */
45 static double conv_fx( int32_t fx ) {
46 double fp = fx;
47 fp /= 65536.;
48 return fp;
51 /* some functions for mp4 encoding of variables */
52 #ifdef MP4_VERBOSE
53 static void MP4_ConvertDate2Str( char *psz, uint64_t i_date, bool b_relative )
55 int i_day;
56 int i_hour;
57 int i_min;
58 int i_sec;
60 /* date begin at 1 jan 1904 */
61 if ( !b_relative )
62 i_date += ((INT64_C(1904) * 365) + 17) * 24 * 60 * 60;
64 i_day = i_date / ( 60*60*24);
65 i_hour = ( i_date /( 60*60 ) ) % 60;
66 i_min = ( i_date / 60 ) % 60;
67 i_sec = i_date % 60;
68 sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds", i_day, i_hour, i_min, i_sec );
70 #endif
72 #define MP4_GETX_PRIVATE(dst, code, size) \
73 do \
74 { \
75 if( (i_read) >= (size) ) \
76 { \
77 dst = (code); \
78 p_peek += (size); \
79 i_read -= (size); \
80 } \
81 else \
82 { \
83 dst = 0; \
84 i_read = 0; \
85 } \
86 } while(0)
88 #define MP4_GET1BYTE( dst ) MP4_GETX_PRIVATE( dst, *p_peek, 1 )
89 #define MP4_GET2BYTES( dst ) MP4_GETX_PRIVATE( dst, GetWBE(p_peek), 2 )
90 #define MP4_GET3BYTES( dst ) MP4_GETX_PRIVATE( dst, Get24bBE(p_peek), 3 )
91 #define MP4_GET4BYTES( dst ) MP4_GETX_PRIVATE( dst, GetDWBE(p_peek), 4 )
92 #define MP4_GET8BYTES( dst ) MP4_GETX_PRIVATE( dst, GetQWBE(p_peek), 8 )
94 #define MP4_GET2BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetWLE(p_peek), 2 )
95 #define MP4_GET4BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetDWLE(p_peek), 4 )
96 #define MP4_GET8BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetQWLE(p_peek), 8 )
97 #define MP4_GETFOURCC( dst ) MP4_GET4BYTESLE( dst )
99 #define MP4_GETVERSIONFLAGS( p_void ) \
100 MP4_GET1BYTE( p_void->i_version ); \
101 MP4_GET3BYTES( p_void->i_flags )
103 static char *mp4_getstringz( uint8_t **restrict in, uint64_t *restrict size )
105 assert( *size <= SSIZE_MAX );
107 size_t len = strnlen( (const char *)*in, *size );
108 if( len == 0 || len >= *size )
109 return NULL;
111 len++;
113 char *ret = malloc( len );
114 if( likely(ret != NULL) )
115 memcpy( ret, *in, len );
116 *in += len;
117 *size -= len;
118 return ret;
121 #define MP4_GETSTRINGZ( p_str ) \
122 do \
123 (p_str) = mp4_getstringz( &p_peek, &i_read ); \
124 while(0)
126 /* This macro is used when we want to printf the box type
127 * APPLE annotation box is :
128 * either 0xA9 + 24-bit ASCII text string (and 0xA9 isn't printable)
129 * either 32-bit ASCII text string
131 #define MP4_BOX_TYPE_ASCII() ( ((char*)&p_box->i_type)[0] != (char)0xA9 )
133 static inline uint32_t Get24bBE( const uint8_t *p )
135 return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
138 static inline void GetUUID( UUID_t *p_uuid, const uint8_t *p_buff )
140 memcpy( p_uuid, p_buff, 16 );
143 static uint8_t *mp4_readbox_enter_common( stream_t *s, MP4_Box_t *box,
144 size_t typesize,
145 void (*release)( MP4_Box_t * ),
146 uint64_t readsize )
148 const size_t headersize = mp4_box_headersize( box );
150 if( unlikely(readsize < headersize) || unlikely(readsize > SSIZE_MAX) )
151 return NULL;
153 uint8_t *buf = malloc( readsize );
154 if( unlikely(buf == NULL) )
155 return NULL;
157 ssize_t val = vlc_stream_Read( s, buf, readsize );
158 if( (size_t)val != readsize )
160 msg_Warn( s, "mp4: wanted %"PRIu64" bytes, got %zd", readsize, val );
161 goto error;
164 box->data.p_payload = malloc( typesize );
165 if( unlikely(box->data.p_payload == NULL) )
166 goto error;
168 memset( box->data.p_payload, 0, typesize );
169 box->pf_free = release;
170 return buf;
171 error:
172 free( buf );
173 return NULL;
176 static uint8_t *mp4_readbox_enter_partial( stream_t *s, MP4_Box_t *box,
177 size_t typesize,
178 void (*release)( MP4_Box_t * ),
179 uint64_t *restrict readsize )
181 if( (uint64_t)*readsize > box->i_size )
182 *readsize = box->i_size;
184 return mp4_readbox_enter_common( s, box, typesize, release, *readsize );
187 static uint8_t *mp4_readbox_enter( stream_t *s, MP4_Box_t *box,
188 size_t typesize,
189 void (*release)( MP4_Box_t * ) )
191 uint64_t readsize = box->i_size;
192 return mp4_readbox_enter_common( s, box, typesize, release, readsize );
196 #define MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_TYPE_t, maxread, release ) \
197 uint64_t i_read = (maxread); \
198 uint8_t *p_buff = mp4_readbox_enter_partial( p_stream, p_box, \
199 sizeof( MP4_Box_data_TYPE_t ), release, &i_read ); \
200 if( unlikely(p_buff == NULL) ) \
201 return 0; \
202 const size_t header_size = mp4_box_headersize( p_box ); \
203 uint8_t *p_peek = p_buff + header_size; \
204 i_read -= header_size
206 #define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t, release ) \
207 uint8_t *p_buff = mp4_readbox_enter( p_stream, p_box, \
208 sizeof(MP4_Box_data_TYPE_t), release ); \
209 if( unlikely(p_buff == NULL) ) \
210 return 0; \
211 uint64_t i_read = p_box->i_size; \
212 const size_t header_size = mp4_box_headersize( p_box ); \
213 uint8_t *p_peek = p_buff + header_size; \
214 i_read -= header_size
216 #define MP4_READBOX_EXIT( i_code ) \
217 do \
219 free( p_buff ); \
220 return( i_code ); \
221 } while (0)
223 /*****************************************************************************
224 * Some prototypes.
225 *****************************************************************************/
226 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
227 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father );
228 static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box );
230 int MP4_Seek( stream_t *p_stream, uint64_t i_pos )
232 bool b_canseek = false;
233 if ( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_canseek ) != VLC_SUCCESS ||
234 b_canseek )
236 /* can seek or don't know */
237 return vlc_stream_Seek( p_stream, i_pos );
239 /* obviously can't seek then */
241 int64_t i_current_pos = vlc_stream_Tell( p_stream );
242 if ( i_current_pos < 0 || i_pos < (uint64_t)i_current_pos )
243 return VLC_EGENERIC;
245 size_t i_toread = i_pos - i_current_pos;
246 if( i_toread == 0 )
247 return VLC_SUCCESS;
248 else if( i_toread > (1<<17) )
249 return VLC_EGENERIC;
251 if( vlc_stream_Read( p_stream, NULL, i_toread ) != (ssize_t)i_toread )
252 return VLC_EGENERIC;
253 return VLC_SUCCESS;
256 static void MP4_BoxAddChild( MP4_Box_t *p_parent, MP4_Box_t *p_childbox )
258 if( !p_parent->p_first )
259 p_parent->p_first = p_childbox;
260 else
261 p_parent->p_last->p_next = p_childbox;
262 p_parent->p_last = p_childbox;
263 p_childbox->p_father = p_parent;
266 MP4_Box_t * MP4_BoxExtract( MP4_Box_t **pp_chain, uint32_t i_type )
268 MP4_Box_t *p_box = *pp_chain;
269 while( p_box )
271 if( p_box->i_type == i_type )
273 *pp_chain = p_box->p_next;
274 p_box->p_next = NULL;
275 return p_box;
277 pp_chain = &p_box->p_next;
278 p_box = p_box->p_next;
280 return NULL;
283 /* Don't use vlc_stream_Seek directly */
284 #undef vlc_stream_Seek
285 #define vlc_stream_Seek(a,b) __NO__
287 /*****************************************************************************
288 * MP4_PeekBoxHeader : Load only common parameters for all boxes
289 *****************************************************************************
290 * p_box need to be an already allocated MP4_Box_t, and all data
291 * will only be peek not read
293 * RETURN : 0 if it fail, 1 otherwise
294 *****************************************************************************/
295 static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box )
297 int i_read;
298 const uint8_t *p_peek;
300 if( ( ( i_read = vlc_stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
302 return 0;
304 p_box->i_pos = vlc_stream_Tell( p_stream );
306 p_box->data.p_payload = NULL;
307 p_box->p_father = NULL;
308 p_box->p_first = NULL;
309 p_box->p_last = NULL;
310 p_box->p_next = NULL;
312 MP4_GET4BYTES( p_box->i_shortsize );
313 MP4_GETFOURCC( p_box->i_type );
315 /* Now special case */
317 if( p_box->i_shortsize == 1 )
319 if( i_read < 8 )
320 return 0;
321 /* get the true size on 64 bits */
322 MP4_GET8BYTES( p_box->i_size );
324 else
326 p_box->i_size = p_box->i_shortsize;
327 /* XXX size of 0 means that the box extends to end of file */
330 if( UINT64_MAX - p_box->i_size < p_box->i_pos )
331 return 0;
333 if( p_box->i_type == ATOM_uuid )
335 if( i_read < 16 )
336 return 0;
337 /* get extented type on 16 bytes */
338 GetUUID( &p_box->i_uuid, p_peek );
341 #ifdef MP4_ULTRA_VERBOSE
342 if( p_box->i_size )
344 if MP4_BOX_TYPE_ASCII()
345 msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64" %"PRId64,
346 (char*)&p_box->i_type, p_box->i_size, p_box->i_pos );
347 else
348 msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
349 (char*)&p_box->i_type+1, p_box->i_size );
351 #endif
353 return 1;
356 /*****************************************************************************
357 * MP4_ReadBoxRestricted : Reads box from current position
358 *****************************************************************************
359 * if p_box == NULL, box is invalid or failed, position undefined
360 * on success, position is past read box or EOF
361 *****************************************************************************/
362 static MP4_Box_t *MP4_ReadBoxRestricted( stream_t *p_stream, MP4_Box_t *p_father,
363 const uint32_t onlytypes[], const uint32_t nottypes[],
364 bool *pb_restrictionhit )
366 MP4_Box_t peekbox = { 0 };
367 if ( !MP4_PeekBoxHeader( p_stream, &peekbox ) )
368 return NULL;
370 if( peekbox.i_size < 8 )
372 msg_Warn( p_stream, "found an invalid sized %"PRIu64" box %4.4s @%"PRIu64 ,
373 peekbox.i_size, (char *) &peekbox.i_type, vlc_stream_Tell(p_stream) );
374 return NULL;
377 for( size_t i=0; nottypes && nottypes[i]; i++ )
379 if( nottypes[i] == peekbox.i_type )
381 *pb_restrictionhit = true;
382 return NULL;
386 for( size_t i=0; onlytypes && onlytypes[i]; i++ )
388 if( onlytypes[i] != peekbox.i_type )
390 *pb_restrictionhit = true;
391 return NULL;
395 /* if father's size == 0, it means unknown or infinite size,
396 * and we skip the followong check */
397 if( p_father && p_father->i_size > 0 )
399 const uint64_t i_box_next = peekbox.i_size + peekbox.i_pos;
400 const uint64_t i_father_next = p_father->i_size + p_father->i_pos;
401 /* check if it's within p-father */
402 if( i_box_next > i_father_next )
404 msg_Warn( p_stream, "out of bound child %4.4s", (char*) &peekbox.i_type );
405 return NULL; /* out of bound */
409 /* Everything seems OK */
410 MP4_Box_t *p_box = (MP4_Box_t *) malloc( sizeof(MP4_Box_t) );
411 if( !p_box )
412 return NULL;
413 *p_box = peekbox;
415 const uint64_t i_next = p_box->i_pos + p_box->i_size;
416 p_box->p_father = p_father;
417 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
419 msg_Warn( p_stream, "Failed reading box %4.4s", (char*) &peekbox.i_type );
420 MP4_BoxFree( p_box );
421 p_box = NULL;
424 /* Check is we consumed all data */
425 if( vlc_stream_Tell( p_stream ) < i_next )
427 MP4_Seek( p_stream, i_next - 1 ); /* since past seek can fail when hitting EOF */
428 MP4_Seek( p_stream, i_next );
429 if( vlc_stream_Tell( p_stream ) < i_next - 1 ) /* Truncated box */
431 msg_Warn( p_stream, "truncated box %4.4s discarded", (char*) &peekbox.i_type );
432 MP4_BoxFree( p_box );
433 p_box = NULL;
437 if ( p_box )
438 MP4_BoxAddChild( p_father, p_box );
440 return p_box;
443 /*****************************************************************************
444 * For all known box a loader is given,
445 * you have to be already read container header
446 * without container size, file position on exit is unknown
447 *****************************************************************************/
448 static int MP4_ReadBoxContainerChildrenIndexed( stream_t *p_stream,
449 MP4_Box_t *p_container, const uint32_t stoplist[],
450 const uint32_t excludelist[], bool b_indexed )
452 /* Size of root container is set to 0 when unknown, for exemple
453 * with a DASH stream. In that case, we skip the following check */
454 if( (p_container->i_size || p_container->p_father)
455 && ( vlc_stream_Tell( p_stream ) + ((b_indexed)?16:8) >
456 (uint64_t)(p_container->i_pos + p_container->i_size) )
459 /* there is no box to load */
460 return 0;
463 uint64_t i_last_pos = 0; /* used to detect read failure loops */
464 const uint64_t i_end = p_container->i_pos + p_container->i_size;
465 MP4_Box_t *p_box = NULL;
466 bool b_onexclude = false;
467 bool b_continue;
470 b_continue = false;
471 if ( p_container->i_size )
473 const uint64_t i_tell = vlc_stream_Tell( p_stream );
474 if( i_tell + ((b_indexed)?16:8) >= i_end )
475 break;
478 uint32_t i_index = 0;
479 if ( b_indexed )
481 uint8_t read[8];
482 if ( vlc_stream_Read( p_stream, read, 8 ) < 8 )
483 break;
484 i_index = GetDWBE(&read[4]);
486 b_onexclude = false; /* If stopped due exclude list */
487 if( (p_box = MP4_ReadBoxRestricted( p_stream, p_container, NULL, excludelist, &b_onexclude )) )
489 b_continue = true;
490 p_box->i_index = i_index;
491 for(size_t i=0; stoplist && stoplist[i]; i++)
493 if( p_box->i_type == stoplist[i] )
494 return 1;
498 const uint64_t i_tell = vlc_stream_Tell( p_stream );
499 if ( p_container->i_size && i_tell >= i_end )
501 assert( i_tell == i_end );
502 break;
505 if ( !p_box )
507 /* Continue with next if box fails to load */
508 if( i_last_pos == i_tell )
509 break;
510 i_last_pos = i_tell;
511 b_continue = true;
514 } while( b_continue );
516 /* Always move to end of container */
517 if ( !b_onexclude && p_container->i_size )
519 const uint64_t i_tell = vlc_stream_Tell( p_stream );
520 if ( i_tell != i_end )
521 MP4_Seek( p_stream, i_end );
524 return 1;
527 int MP4_ReadBoxContainerRestricted( stream_t *p_stream, MP4_Box_t *p_container,
528 const uint32_t stoplist[], const uint32_t excludelist[] )
530 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
531 stoplist, excludelist, false );
534 int MP4_ReadBoxContainerChildren( stream_t *p_stream, MP4_Box_t *p_container,
535 const uint32_t stoplist[] )
537 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
538 stoplist, NULL, false );
541 static void MP4_BoxOffsetUp( MP4_Box_t *p_box, uint64_t i_offset )
543 while(p_box)
545 p_box->i_pos += i_offset;
546 MP4_BoxOffsetUp( p_box->p_first, i_offset );
547 p_box = p_box->p_next;
551 /* Reads within an already read/in memory box (containers without having to seek) */
552 static int MP4_ReadBoxContainerRawInBox( stream_t *p_stream, MP4_Box_t *p_container,
553 uint8_t *p_buffer, uint64_t i_size, uint64_t i_offset )
555 if(!p_container)
556 return 0;
557 stream_t *p_substream = vlc_stream_MemoryNew( p_stream, p_buffer, i_size,
558 true );
559 if( !p_substream )
560 return 0;
561 MP4_Box_t *p_last = p_container->p_last;
562 MP4_ReadBoxContainerChildren( p_substream, p_container, NULL );
563 vlc_stream_Delete( p_substream );
564 /* do pos fixup */
565 if( p_container )
567 MP4_Box_t *p_box = p_last ? p_last : p_container->p_first;
568 MP4_BoxOffsetUp(p_box, i_offset);
571 return 1;
574 static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
576 if( p_container->i_size &&
577 ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
579 /* container is empty, 8 stand for the first header in this box */
580 return 1;
583 /* enter box */
584 if ( MP4_Seek( p_stream, p_container->i_pos +
585 mp4_box_headersize( p_container ) ) )
586 return 0;
587 return MP4_ReadBoxContainerChildren( p_stream, p_container, NULL );
590 static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
592 /* XXX sometime moov is hidden in a free box */
593 if( p_box->p_father &&
594 p_box->p_father->i_type == ATOM_root &&
595 p_box->i_type == ATOM_free )
597 const uint8_t *p_peek;
598 size_t header_size = mp4_box_headersize( p_box ) + 4;
599 vlc_fourcc_t i_fcc;
601 ssize_t i_read = vlc_stream_Peek( p_stream, &p_peek, 44 );
602 if( unlikely(i_read < (ssize_t)header_size) )
603 return 0;
605 p_peek += header_size;
606 i_read -= header_size;
608 if( i_read >= 8 )
610 i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
612 if( i_fcc == ATOM_cmov || i_fcc == ATOM_mvhd )
614 msg_Warn( p_stream, "detected moov hidden in a free box ..." );
616 p_box->i_type = ATOM_foov;
617 return MP4_ReadBoxContainer( p_stream, p_box );
622 /* Nothing to do */
623 #ifdef MP4_ULTRA_VERBOSE
624 if MP4_BOX_TYPE_ASCII()
625 msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
626 else
627 msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
628 #endif
629 return 1;
632 static int MP4_ReadBox_ilst( stream_t *p_stream, MP4_Box_t *p_box )
634 if( p_box->i_size < 8 || vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
635 return 0;
637 /* Find our handler */
638 if ( !p_box->i_handler && p_box->p_father )
640 const MP4_Box_t *p_sibling = p_box->p_father->p_first;
641 while( p_sibling )
643 if ( p_sibling->i_type == ATOM_hdlr && p_sibling->data.p_hdlr )
645 p_box->i_handler = p_sibling->data.p_hdlr->i_handler_type;
646 break;
648 p_sibling = p_sibling->p_next;
652 switch( p_box->i_handler )
654 case 0:
655 msg_Warn( p_stream, "no handler for ilst atom" );
656 return 0;
657 case HANDLER_mdta:
658 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_box, NULL, NULL, true );
659 case HANDLER_mdir:
660 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
661 default:
662 msg_Warn( p_stream, "Unknown ilst handler type '%4.4s'", (char*)&p_box->i_handler );
663 return 0;
667 static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
669 free( p_box->data.p_ftyp->i_compatible_brands );
672 static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
674 MP4_READBOX_ENTER( MP4_Box_data_ftyp_t, MP4_FreeBox_ftyp );
676 MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
677 MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
679 p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4;
680 if( p_box->data.p_ftyp->i_compatible_brands_count > 0 )
682 uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
683 vlc_alloc( p_box->data.p_ftyp->i_compatible_brands_count,
684 sizeof(uint32_t) );
686 if( unlikely( tab == NULL ) )
687 MP4_READBOX_EXIT( 0 );
689 for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
691 MP4_GETFOURCC( tab[i] );
694 else
696 p_box->data.p_ftyp->i_compatible_brands = NULL;
699 MP4_READBOX_EXIT( 1 );
703 static int MP4_ReadBox_mvhd( stream_t *p_stream, MP4_Box_t *p_box )
705 #ifdef MP4_VERBOSE
706 char s_creation_time[128];
707 char s_modification_time[128];
708 char s_duration[128];
709 #endif
710 MP4_READBOX_ENTER( MP4_Box_data_mvhd_t, NULL );
712 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
714 if( p_box->data.p_mvhd->i_version )
716 MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
717 MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
718 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
719 MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
721 else
723 MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
724 MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
725 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
726 MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
728 MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
729 MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
730 MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
733 for( unsigned i = 0; i < 2; i++ )
735 MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
737 for( unsigned i = 0; i < 9; i++ )
739 MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
741 for( unsigned i = 0; i < 6; i++ )
743 MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
746 MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
749 #ifdef MP4_VERBOSE
750 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
751 MP4_ConvertDate2Str( s_modification_time,
752 p_box->data.p_mvhd->i_modification_time, false );
753 if( p_box->data.p_mvhd->i_rate && p_box->data.p_mvhd->i_timescale )
755 MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_timescale, true );
757 else
759 s_duration[0] = 0;
761 msg_Dbg( p_stream, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
762 s_creation_time,
763 s_modification_time,
764 (uint32_t)p_box->data.p_mvhd->i_timescale,
765 s_duration,
766 (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
767 (float)p_box->data.p_mvhd->i_volume / 256 ,
768 (uint32_t)p_box->data.p_mvhd->i_next_track_id );
769 #endif
770 MP4_READBOX_EXIT( 1 );
773 static int MP4_ReadBox_mfhd( stream_t *p_stream, MP4_Box_t *p_box )
775 MP4_READBOX_ENTER( MP4_Box_data_mfhd_t, NULL );
777 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
779 MP4_GET4BYTES( p_box->data.p_mfhd->i_sequence_number );
781 #ifdef MP4_VERBOSE
782 msg_Dbg( p_stream, "read box: \"mfhd\" sequence number %d",
783 p_box->data.p_mfhd->i_sequence_number );
784 #endif
785 MP4_READBOX_EXIT( 1 );
788 static int MP4_ReadBox_tfxd( stream_t *p_stream, MP4_Box_t *p_box )
790 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, NULL );
792 MP4_Box_data_tfxd_t *p_tfxd_data = p_box->data.p_tfxd;
793 MP4_GETVERSIONFLAGS( p_tfxd_data );
795 if( p_tfxd_data->i_version == 0 )
797 MP4_GET4BYTES( p_tfxd_data->i_fragment_abs_time );
798 MP4_GET4BYTES( p_tfxd_data->i_fragment_duration );
800 else
802 MP4_GET8BYTES( p_tfxd_data->i_fragment_abs_time );
803 MP4_GET8BYTES( p_tfxd_data->i_fragment_duration );
806 #ifdef MP4_VERBOSE
807 msg_Dbg( p_stream, "read box: \"tfxd\" version %d, flags 0x%x, "\
808 "fragment duration %"PRIu64", fragment abs time %"PRIu64,
809 p_tfxd_data->i_version,
810 p_tfxd_data->i_flags,
811 p_tfxd_data->i_fragment_duration,
812 p_tfxd_data->i_fragment_abs_time
814 #endif
816 MP4_READBOX_EXIT( 1 );
819 static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
821 free( p_box->data.p_tfrf->p_tfrf_data_fields );
824 static int MP4_ReadBox_tfrf( stream_t *p_stream, MP4_Box_t *p_box )
826 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, MP4_FreeBox_tfrf );
828 MP4_Box_data_tfrf_t *p_tfrf_data = p_box->data.p_tfrf;
829 MP4_GETVERSIONFLAGS( p_tfrf_data );
831 MP4_GET1BYTE( p_tfrf_data->i_fragment_count );
833 p_tfrf_data->p_tfrf_data_fields = calloc( p_tfrf_data->i_fragment_count,
834 sizeof( TfrfBoxDataFields_t ) );
835 if( !p_tfrf_data->p_tfrf_data_fields )
836 MP4_READBOX_EXIT( 0 );
838 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
840 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
841 if( p_tfrf_data->i_version == 0 )
843 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_abs_time );
844 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_duration );
846 else
848 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_abs_time );
849 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_duration );
853 #ifdef MP4_VERBOSE
854 msg_Dbg( p_stream, "read box: \"tfrf\" version %d, flags 0x%x, "\
855 "fragment count %"PRIu8, p_tfrf_data->i_version,
856 p_tfrf_data->i_flags, p_tfrf_data->i_fragment_count );
858 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
860 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
861 msg_Dbg( p_stream, "\"tfrf\" fragment duration %"PRIu64", "\
862 "fragment abs time %"PRIu64,
863 TfrfBoxDataField->i_fragment_duration,
864 TfrfBoxDataField->i_fragment_abs_time );
867 #endif
869 MP4_READBOX_EXIT( 1 );
872 static int MP4_ReadBox_XML360( stream_t *p_stream, MP4_Box_t *p_box )
874 MP4_READBOX_ENTER( MP4_Box_data_360_t, NULL );
876 MP4_Box_data_360_t *p_360_data = p_box->data.p_360;
878 /* Copy the string for pattern matching as it does not end
879 with a '\0' in the stream. */
880 char *psz_rdf = strndup((char *)p_peek, i_read);
882 if ( unlikely( !psz_rdf ) )
883 MP4_READBOX_EXIT( 0 );
885 /* Try to find the string "GSpherical:Spherical" because the v1
886 spherical video spec says the tag must be there. */
888 if ( strcasestr( psz_rdf, "Gspherical:Spherical" ) )
889 p_360_data->i_projection_mode = PROJECTION_MODE_EQUIRECTANGULAR;
891 /* Try to find the stero mode. */
892 if ( strcasestr( psz_rdf, "left-right" ) )
894 msg_Dbg( p_stream, "Left-right stereo mode" );
895 p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_LEFT_RIGHT;
898 if ( strcasestr( psz_rdf, "top-bottom" ) )
900 msg_Dbg( p_stream, "Top-bottom stereo mode" );
901 p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_TOP_BOTTOM;
904 free( psz_rdf );
906 MP4_READBOX_EXIT( 1 );
909 static int MP4_ReadBox_st3d( stream_t *p_stream, MP4_Box_t *p_box )
911 MP4_READBOX_ENTER( MP4_Box_data_st3d_t, NULL );
913 uint8_t i_version;
914 MP4_GET1BYTE( i_version );
915 if ( i_version != 0 )
916 MP4_READBOX_EXIT( 0 );
918 uint32_t i_flags;
919 VLC_UNUSED( i_flags );
920 MP4_GET3BYTES( i_flags );
922 MP4_Box_data_st3d_t *p_data = p_box->data.p_st3d;
923 MP4_GET1BYTE( p_data->i_stereo_mode );
925 MP4_READBOX_EXIT( 1 );
928 static int MP4_ReadBox_prhd( stream_t *p_stream, MP4_Box_t *p_box )
930 MP4_READBOX_ENTER( MP4_Box_data_prhd_t, NULL );
932 uint8_t i_version;
933 MP4_GET1BYTE( i_version );
934 if (i_version != 0)
935 MP4_READBOX_EXIT( 0 );
937 uint32_t i_flags;
938 VLC_UNUSED( i_flags );
939 MP4_GET3BYTES( i_flags );
941 MP4_Box_data_prhd_t *p_data = p_box->data.p_prhd;
942 int32_t fixed16_16;
943 MP4_GET4BYTES( fixed16_16 );
944 p_data->f_pose_yaw_degrees = (float) fixed16_16 / 65536.0f;
946 MP4_GET4BYTES( fixed16_16 );
947 p_data->f_pose_pitch_degrees = (float) fixed16_16 / 65536.0f;
949 MP4_GET4BYTES( fixed16_16 );
950 p_data->f_pose_roll_degrees = (float) fixed16_16 / 65536.0f;
952 MP4_READBOX_EXIT( 1 );
955 static int MP4_ReadBox_equi( stream_t *p_stream, MP4_Box_t *p_box )
957 MP4_READBOX_ENTER( MP4_Box_data_equi_t, NULL );
959 uint8_t i_version;
960 MP4_GET1BYTE( i_version );
961 if (i_version != 0)
962 MP4_READBOX_EXIT( 0 );
964 uint32_t i_flags;
965 VLC_UNUSED( i_flags );
966 MP4_GET3BYTES( i_flags );
968 MP4_Box_data_equi_t *p_data = p_box->data.p_equi;
969 MP4_GET4BYTES( p_data->i_projection_bounds_top );
970 MP4_GET4BYTES( p_data->i_projection_bounds_bottom );
971 MP4_GET4BYTES( p_data->i_projection_bounds_left );
972 MP4_GET4BYTES( p_data->i_projection_bounds_right );
974 MP4_READBOX_EXIT( 1 );
977 static int MP4_ReadBox_cbmp( stream_t *p_stream, MP4_Box_t *p_box )
979 MP4_READBOX_ENTER( MP4_Box_data_cbmp_t, NULL );
981 uint8_t i_version;
982 MP4_GET1BYTE( i_version );
983 if (i_version != 0)
984 MP4_READBOX_EXIT( 0 );
986 uint32_t i_flags;
987 VLC_UNUSED( i_flags );
988 MP4_GET3BYTES( i_flags );
990 MP4_Box_data_cbmp_t *p_data = p_box->data.p_cbmp;
991 MP4_GET4BYTES( p_data->i_layout );
992 MP4_GET4BYTES( p_data->i_padding );
994 MP4_READBOX_EXIT( 1 );
997 static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
999 free( p_box->data.p_sidx->p_items );
1002 static int MP4_ReadBox_sidx( stream_t *p_stream, MP4_Box_t *p_box )
1004 MP4_READBOX_ENTER( MP4_Box_data_sidx_t, MP4_FreeBox_sidx );
1006 MP4_Box_data_sidx_t *p_sidx_data = p_box->data.p_sidx;
1007 MP4_GETVERSIONFLAGS( p_sidx_data );
1009 MP4_GET4BYTES( p_sidx_data->i_reference_ID );
1010 MP4_GET4BYTES( p_sidx_data->i_timescale );
1012 if( p_sidx_data->i_version == 0 )
1014 MP4_GET4BYTES( p_sidx_data->i_earliest_presentation_time );
1015 MP4_GET4BYTES( p_sidx_data->i_first_offset );
1017 else
1019 MP4_GET8BYTES( p_sidx_data->i_earliest_presentation_time );
1020 MP4_GET8BYTES( p_sidx_data->i_first_offset );
1023 uint16_t i_reserved, i_count;
1025 VLC_UNUSED(i_reserved);
1026 MP4_GET2BYTES( i_reserved );
1027 MP4_GET2BYTES( i_count );
1028 if( i_count == 0 )
1029 MP4_READBOX_EXIT( 1 );
1031 p_sidx_data->i_reference_count = i_count;
1032 p_sidx_data->p_items = vlc_alloc( i_count, sizeof( MP4_Box_sidx_item_t ) );
1033 if( unlikely(p_sidx_data->p_items == NULL) )
1034 MP4_READBOX_EXIT( 0 );
1036 for( unsigned i = 0; i < i_count; i++ )
1038 MP4_Box_sidx_item_t *item = p_sidx_data->p_items + i;
1039 uint32_t tmp;
1041 MP4_GET4BYTES( tmp );
1042 item->b_reference_type = tmp >> 31;
1043 item->i_referenced_size = tmp & 0x7fffffff;
1044 MP4_GET4BYTES( item->i_subsegment_duration );
1046 MP4_GET4BYTES( tmp );
1047 item->b_starts_with_SAP = tmp >> 31;
1048 item->i_SAP_type = (tmp >> 24) & 0x70;
1049 item->i_SAP_delta_time = tmp & 0xfffffff;
1052 #ifdef MP4_VERBOSE
1053 msg_Dbg( p_stream, "read box: \"sidx\" version %d, flags 0x%x, "\
1054 "ref_ID %"PRIu32", timescale %"PRIu32", ref_count %"PRIu16", "\
1055 "first subsegmt duration %"PRIu32,
1056 p_sidx_data->i_version,
1057 p_sidx_data->i_flags,
1058 p_sidx_data->i_reference_ID,
1059 p_sidx_data->i_timescale,
1060 p_sidx_data->i_reference_count,
1061 p_sidx_data->p_items[0].i_subsegment_duration
1063 #endif
1065 MP4_READBOX_EXIT( 1 );
1068 static int MP4_ReadBox_tfhd( stream_t *p_stream, MP4_Box_t *p_box )
1070 MP4_READBOX_ENTER( MP4_Box_data_tfhd_t, NULL );
1072 MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );
1074 if( p_box->data.p_tfhd->i_version != 0 )
1076 msg_Warn( p_stream, "'tfhd' box with version != 0. "\
1077 " Don't know what to do with that, please patch" );
1078 MP4_READBOX_EXIT( 0 );
1081 MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );
1083 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
1085 msg_Dbg( p_stream, "'duration-is-empty' flag is present "\
1086 "=> no samples for this time interval." );
1087 p_box->data.p_tfhd->b_empty = true;
1089 else
1090 p_box->data.p_tfhd->b_empty = false;
1092 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
1093 MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
1094 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
1095 MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
1096 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
1097 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
1098 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
1099 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
1100 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
1101 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
1103 #ifdef MP4_VERBOSE
1104 char psz_base[128] = "\0";
1105 char psz_desc[128] = "\0";
1106 char psz_dura[128] = "\0";
1107 char psz_size[128] = "\0";
1108 char psz_flag[128] = "\0";
1109 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
1110 snprintf(psz_base, sizeof(psz_base), "base offset %"PRId64, p_box->data.p_tfhd->i_base_data_offset);
1111 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
1112 snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
1113 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
1114 snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
1115 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
1116 snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
1117 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
1118 snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);
1120 msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
1121 p_box->data.p_tfhd->i_version,
1122 p_box->data.p_tfhd->i_flags,
1123 p_box->data.p_tfhd->i_track_ID,
1124 psz_base, psz_desc, psz_dura, psz_size, psz_flag );
1125 #endif
1127 MP4_READBOX_EXIT( 1 );
1130 static void MP4_FreeBox_trun( MP4_Box_t *p_box )
1132 free( p_box->data.p_trun->p_samples );
1135 static int MP4_ReadBox_trun( stream_t *p_stream, MP4_Box_t *p_box )
1137 uint32_t count;
1139 MP4_READBOX_ENTER( MP4_Box_data_trun_t, MP4_FreeBox_trun );
1140 MP4_Box_data_trun_t *p_trun = p_box->data.p_trun;
1141 MP4_GETVERSIONFLAGS( p_trun );
1142 MP4_GET4BYTES( count );
1144 if( p_trun->i_flags & MP4_TRUN_DATA_OFFSET )
1145 MP4_GET4BYTES( p_trun->i_data_offset );
1146 if( p_trun->i_flags & MP4_TRUN_FIRST_FLAGS )
1147 MP4_GET4BYTES( p_trun->i_first_sample_flags );
1149 uint64_t i_entry_size =
1150 !!(p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION) +
1151 !!(p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE) +
1152 !!(p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS) +
1153 !!(p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET);
1155 if( i_entry_size * 4 * count > i_read )
1156 MP4_READBOX_EXIT( 0 );
1158 p_trun->p_samples = vlc_alloc( count, sizeof(MP4_descriptor_trun_sample_t) );
1159 if ( p_trun->p_samples == NULL )
1160 MP4_READBOX_EXIT( 0 );
1161 p_trun->i_sample_count = count;
1163 for( unsigned int i = 0; i < count; i++ )
1165 MP4_descriptor_trun_sample_t *p_sample = &p_trun->p_samples[i];
1166 if( p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
1167 MP4_GET4BYTES( p_sample->i_duration );
1168 if( p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
1169 MP4_GET4BYTES( p_sample->i_size );
1170 if( p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS )
1171 MP4_GET4BYTES( p_sample->i_flags );
1172 if( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
1173 MP4_GET4BYTES( p_sample->i_composition_time_offset.v0 );
1176 #ifdef MP4_ULTRA_VERBOSE
1177 msg_Dbg( p_stream, "read box: \"trun\" version %u flags 0x%x sample count %"PRIu32,
1178 p_trun->i_version,
1179 p_trun->i_flags,
1180 p_trun->i_sample_count );
1182 for( unsigned int i = 0; i < count; i++ )
1184 MP4_descriptor_trun_sample_t *p_sample = &p_trun->p_samples[i];
1185 msg_Dbg( p_stream, "read box: \"trun\" sample %4.4u flags 0x%x "\
1186 "duration %"PRIu32" size %"PRIu32" composition time offset %"PRIu32,
1187 i, p_sample->i_flags, p_sample->i_duration,
1188 p_sample->i_size, p_sample->i_composition_time_offset );
1190 #endif
1192 MP4_READBOX_EXIT( 1 );
1195 static int MP4_ReadBox_tfdt( stream_t *p_stream, MP4_Box_t *p_box )
1197 MP4_READBOX_ENTER( MP4_Box_data_tfdt_t, NULL );
1198 if( i_read < 8 )
1199 MP4_READBOX_EXIT( 0 );
1201 MP4_GETVERSIONFLAGS( p_box->data.p_tfdt );
1203 if( p_box->data.p_tfdt->i_version == 0 )
1204 MP4_GET4BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1205 else if( p_box->data.p_tfdt->i_version == 1 )
1206 MP4_GET8BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1207 else
1208 MP4_READBOX_EXIT( 0 );
1210 MP4_READBOX_EXIT( 1 );
1213 static int MP4_ReadBox_tkhd( stream_t *p_stream, MP4_Box_t *p_box )
1215 #ifdef MP4_VERBOSE
1216 char s_creation_time[128];
1217 char s_modification_time[128];
1218 char s_duration[128];
1219 #endif
1220 MP4_READBOX_ENTER( MP4_Box_data_tkhd_t, NULL );
1222 MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
1224 if( p_box->data.p_tkhd->i_version )
1226 MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
1227 MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
1228 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1229 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1230 MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
1232 else
1234 MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
1235 MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
1236 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1237 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1238 MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
1241 for( unsigned i = 0; i < 2; i++ )
1243 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
1245 MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
1246 MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
1247 MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
1248 MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
1250 for( unsigned i = 0; i < 9; i++ )
1252 MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
1254 MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
1255 MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
1257 double rotation = 0;//angle in degrees to be rotated clockwise
1258 double scale[2]; // scale factor; sx = scale[0] , sy = scale[1]
1259 int32_t *matrix = p_box->data.p_tkhd->i_matrix;
1261 scale[0] = sqrt(conv_fx(matrix[0]) * conv_fx(matrix[0]) +
1262 conv_fx(matrix[3]) * conv_fx(matrix[3]));
1263 scale[1] = sqrt(conv_fx(matrix[1]) * conv_fx(matrix[1]) +
1264 conv_fx(matrix[4]) * conv_fx(matrix[4]));
1266 if( likely(scale[0] > 0 && scale[1] > 0) )
1268 rotation = atan2(conv_fx(matrix[1]) / scale[1],
1269 conv_fx(matrix[0]) / scale[0]) * 180 / M_PI;
1270 if (rotation < 0)
1271 rotation += 360.;
1274 p_box->data.p_tkhd->f_rotation = rotation;
1276 #ifdef MP4_VERBOSE
1277 double translate[2];// amount to translate; tx = translate[0] , ty = translate[1]
1279 translate[0] = conv_fx(matrix[6]);
1280 translate[1] = conv_fx(matrix[7]);
1282 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
1283 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time, false );
1284 MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration, true );
1286 msg_Dbg( p_stream, "read box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f rotation %f scaleX %f scaleY %f translateX %f translateY %f width %f height %f. "
1287 "Matrix: %i %i %i %i %i %i %i %i %i",
1288 s_creation_time,
1289 s_modification_time,
1290 s_duration,
1291 p_box->data.p_tkhd->i_track_ID,
1292 p_box->data.p_tkhd->i_layer,
1293 (float)p_box->data.p_tkhd->i_volume / 256 ,
1294 rotation,
1295 scale[0],
1296 scale[1],
1297 translate[0],
1298 translate[1],
1299 (float)p_box->data.p_tkhd->i_width / BLOCK16x16,
1300 (float)p_box->data.p_tkhd->i_height / BLOCK16x16,
1301 p_box->data.p_tkhd->i_matrix[0],
1302 p_box->data.p_tkhd->i_matrix[1],
1303 p_box->data.p_tkhd->i_matrix[2],
1304 p_box->data.p_tkhd->i_matrix[3],
1305 p_box->data.p_tkhd->i_matrix[4],
1306 p_box->data.p_tkhd->i_matrix[5],
1307 p_box->data.p_tkhd->i_matrix[6],
1308 p_box->data.p_tkhd->i_matrix[7],
1309 p_box->data.p_tkhd->i_matrix[8] );
1310 #endif
1311 MP4_READBOX_EXIT( 1 );
1314 static int MP4_ReadBox_load( stream_t *p_stream, MP4_Box_t *p_box )
1316 if ( p_box->i_size != 24 )
1317 return 0;
1318 MP4_READBOX_ENTER( MP4_Box_data_load_t, NULL );
1319 MP4_GET4BYTES( p_box->data.p_load->i_start_time );
1320 MP4_GET4BYTES( p_box->data.p_load->i_duration );
1321 MP4_GET4BYTES( p_box->data.p_load->i_flags );
1322 MP4_GET4BYTES( p_box->data.p_load->i_hints );
1323 MP4_READBOX_EXIT( 1 );
1326 static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
1328 uint16_t i_language;
1329 #ifdef MP4_VERBOSE
1330 char s_creation_time[128];
1331 char s_modification_time[128];
1332 char s_duration[128];
1333 #endif
1334 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t, NULL );
1336 MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
1338 if( p_box->data.p_mdhd->i_version )
1340 MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
1341 MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
1342 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1343 MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
1345 else
1347 MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
1348 MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
1349 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1350 MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
1353 MP4_GET2BYTES( i_language );
1354 decodeQtLanguageCode( i_language, p_box->data.p_mdhd->rgs_language,
1355 &p_box->data.p_mdhd->b_mac_encoding );
1357 MP4_GET2BYTES( p_box->data.p_mdhd->i_quality );
1359 #ifdef MP4_VERBOSE
1360 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time, false );
1361 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time, false );
1362 MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration, true );
1363 msg_Dbg( p_stream, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %3.3s",
1364 s_creation_time,
1365 s_modification_time,
1366 (uint32_t)p_box->data.p_mdhd->i_timescale,
1367 s_duration,
1368 (char*) &p_box->data.p_mdhd->rgs_language );
1369 #endif
1370 MP4_READBOX_EXIT( 1 );
1373 static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
1375 free( p_box->data.p_hdlr->psz_name );
1378 static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
1380 int32_t i_reserved;
1381 VLC_UNUSED(i_reserved);
1383 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t, MP4_FreeBox_hdlr );
1385 MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
1387 MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
1388 MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
1390 MP4_GET4BYTES( i_reserved );
1391 MP4_GET4BYTES( i_reserved );
1392 MP4_GET4BYTES( i_reserved );
1393 p_box->data.p_hdlr->psz_name = NULL;
1395 if( i_read >= SSIZE_MAX )
1396 MP4_READBOX_EXIT( 0 );
1398 if( i_read > 0 )
1400 size_t i_copy;
1402 /* Yes, I love .mp4 :( */
1403 if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
1405 uint8_t i_len;
1407 MP4_GET1BYTE( i_len );
1408 i_copy = (i_len <= i_read) ? i_len : i_read;
1410 else
1411 i_copy = i_read;
1413 uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_copy + 1 );
1414 if( unlikely( psz == NULL ) )
1415 MP4_READBOX_EXIT( 0 );
1417 memcpy( psz, p_peek, i_copy );
1418 p_box->data.p_hdlr->psz_name[i_copy] = '\0';
1421 #ifdef MP4_VERBOSE
1422 msg_Dbg( p_stream, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
1423 (char*)&p_box->data.p_hdlr->i_handler_type,
1424 p_box->data.p_hdlr->psz_name );
1426 #endif
1427 MP4_READBOX_EXIT( 1 );
1430 static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
1432 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t, NULL );
1434 MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
1436 MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
1437 for( unsigned i = 0; i < 3; i++ )
1439 MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
1442 #ifdef MP4_VERBOSE
1443 msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
1444 p_box->data.p_vmhd->i_graphics_mode,
1445 p_box->data.p_vmhd->i_opcolor[0],
1446 p_box->data.p_vmhd->i_opcolor[1],
1447 p_box->data.p_vmhd->i_opcolor[2] );
1448 #endif
1449 MP4_READBOX_EXIT( 1 );
1452 static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
1454 MP4_READBOX_ENTER( MP4_Box_data_smhd_t, NULL );
1456 MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
1460 MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
1462 MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
1464 #ifdef MP4_VERBOSE
1465 msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
1466 (float)p_box->data.p_smhd->i_balance / 256 );
1467 #endif
1468 MP4_READBOX_EXIT( 1 );
1472 static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
1474 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t, NULL );
1476 MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
1478 MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
1479 MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
1481 MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
1482 MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
1484 MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
1486 #ifdef MP4_VERBOSE
1487 msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1488 p_box->data.p_hmhd->i_max_PDU_size,
1489 p_box->data.p_hmhd->i_avg_PDU_size,
1490 p_box->data.p_hmhd->i_max_bitrate,
1491 p_box->data.p_hmhd->i_avg_bitrate );
1492 #endif
1493 MP4_READBOX_EXIT( 1 );
1496 static void MP4_FreeBox_url( MP4_Box_t *p_box )
1498 free( p_box->data.p_url->psz_location );
1501 static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
1503 MP4_READBOX_ENTER( MP4_Box_data_url_t, MP4_FreeBox_url );
1505 MP4_GETVERSIONFLAGS( p_box->data.p_url );
1506 MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
1508 #ifdef MP4_VERBOSE
1509 msg_Dbg( p_stream, "read box: \"url\" url: %s",
1510 p_box->data.p_url->psz_location );
1512 #endif
1513 MP4_READBOX_EXIT( 1 );
1516 static void MP4_FreeBox_urn( MP4_Box_t *p_box )
1518 free( p_box->data.p_urn->psz_name );
1519 free( p_box->data.p_urn->psz_location );
1522 static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
1524 MP4_READBOX_ENTER( MP4_Box_data_urn_t, MP4_FreeBox_urn );
1526 MP4_GETVERSIONFLAGS( p_box->data.p_urn );
1528 MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
1529 MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
1531 #ifdef MP4_VERBOSE
1532 msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
1533 p_box->data.p_urn->psz_name,
1534 p_box->data.p_urn->psz_location );
1535 #endif
1536 MP4_READBOX_EXIT( 1 );
1539 static int MP4_ReadBox_LtdContainer( stream_t *p_stream, MP4_Box_t *p_box )
1541 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_lcont_t, 16, NULL );
1542 if( i_read < 8 )
1543 MP4_READBOX_EXIT( 0 );
1545 MP4_GETVERSIONFLAGS( p_box->data.p_lcont );
1546 if( p_box->data.p_lcont->i_version != 0 )
1547 MP4_READBOX_EXIT( 0 );
1548 MP4_GET4BYTES( p_box->data.p_lcont->i_entry_count );
1550 uint32_t i_entry = 0;
1551 i_read = p_box->i_size - 16;
1552 while (i_read > 8 && i_entry < p_box->data.p_lcont->i_entry_count )
1554 MP4_Box_t *p_childbox = MP4_ReadBox( p_stream, p_box );
1555 if( !p_childbox )
1556 break;
1557 MP4_BoxAddChild( p_box, p_childbox );
1558 i_entry++;
1560 if( i_read < p_childbox->i_size )
1561 MP4_READBOX_EXIT( 0 );
1563 i_read -= p_childbox->i_size;
1566 if (i_entry != p_box->data.p_lcont->i_entry_count)
1567 p_box->data.p_lcont->i_entry_count = i_entry;
1569 #ifdef MP4_VERBOSE
1570 msg_Dbg( p_stream, "read box: \"%4.4s\" entry-count %d", (char *)&p_box->i_type,
1571 p_box->data.p_lcont->i_entry_count );
1573 #endif
1575 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
1576 MP4_READBOX_EXIT( 0 );
1578 MP4_READBOX_EXIT( 1 );
1581 static void MP4_FreeBox_stts( MP4_Box_t *p_box )
1583 free( p_box->data.p_stts->pi_sample_count );
1584 free( p_box->data.p_stts->pi_sample_delta );
1587 static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
1589 uint32_t count;
1591 MP4_READBOX_ENTER( MP4_Box_data_stts_t, MP4_FreeBox_stts );
1593 MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1594 MP4_GET4BYTES( count );
1596 if( UINT64_C(8) * count > i_read )
1598 /*count = i_read / 8;*/
1599 MP4_READBOX_EXIT( 0 );
1602 p_box->data.p_stts->pi_sample_count = vlc_alloc( count, sizeof(uint32_t) );
1603 p_box->data.p_stts->pi_sample_delta = vlc_alloc( count, sizeof(int32_t) );
1604 p_box->data.p_stts->i_entry_count = count;
1606 if( p_box->data.p_stts->pi_sample_count == NULL
1607 || p_box->data.p_stts->pi_sample_delta == NULL )
1609 MP4_READBOX_EXIT( 0 );
1612 for( uint32_t i = 0; i < count; i++ )
1614 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_count[i] );
1615 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_delta[i] );
1618 #ifdef MP4_VERBOSE
1619 msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
1620 p_box->data.p_stts->i_entry_count );
1622 #endif
1623 MP4_READBOX_EXIT( 1 );
1627 static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
1629 free( p_box->data.p_ctts->pi_sample_count );
1630 free( p_box->data.p_ctts->pi_sample_offset );
1633 static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
1635 uint32_t count;
1637 MP4_READBOX_ENTER( MP4_Box_data_ctts_t, MP4_FreeBox_ctts );
1639 MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1640 MP4_GET4BYTES( count );
1642 if( UINT64_C(8) * count > i_read )
1643 MP4_READBOX_EXIT( 0 );
1645 p_box->data.p_ctts->pi_sample_count = vlc_alloc( count, sizeof(uint32_t) );
1646 p_box->data.p_ctts->pi_sample_offset = vlc_alloc( count, sizeof(int32_t) );
1647 if( unlikely(p_box->data.p_ctts->pi_sample_count == NULL
1648 || p_box->data.p_ctts->pi_sample_offset == NULL) )
1649 MP4_READBOX_EXIT( 0 );
1650 p_box->data.p_ctts->i_entry_count = count;
1652 for( uint32_t i = 0; i < count; i++ )
1654 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_count[i] );
1655 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_offset[i] );
1658 #ifdef MP4_VERBOSE
1659 msg_Dbg( p_stream, "read box: \"ctts\" entry-count %"PRIu32, count );
1661 #endif
1662 MP4_READBOX_EXIT( 1 );
1665 static int MP4_ReadBox_cslg( stream_t *p_stream, MP4_Box_t *p_box )
1667 MP4_READBOX_ENTER( MP4_Box_data_cslg_t, NULL );
1669 unsigned i_version, i_flags;
1670 MP4_GET1BYTE( i_version );
1671 MP4_GET3BYTES( i_flags );
1672 VLC_UNUSED(i_flags);
1674 if( i_version > 1 )
1675 MP4_READBOX_EXIT( 0 );
1677 #define READ_CSLG(readbytes) {\
1678 readbytes( p_box->data.p_cslg->ct_to_dts_shift );\
1679 readbytes( p_box->data.p_cslg->i_least_delta );\
1680 readbytes( p_box->data.p_cslg->i_max_delta );\
1681 readbytes( p_box->data.p_cslg->i_composition_starttime );\
1682 readbytes( p_box->data.p_cslg->i_composition_endtime ); }
1684 if( i_version == 0 )
1685 READ_CSLG(MP4_GET4BYTES)
1686 else
1687 READ_CSLG(MP4_GET8BYTES)
1689 MP4_READBOX_EXIT( 1 );
1692 static uint64_t MP4_ReadLengthDescriptor( uint8_t **restrict bufp,
1693 uint64_t *restrict lenp )
1695 unsigned char *buf = *bufp;
1696 uint64_t len = *lenp;
1697 unsigned char b;
1698 uint64_t value = 0;
1702 if (unlikely(len == 0))
1703 return -1; /* end of bit stream */
1704 if (unlikely(value > (UINT64_MAX >> 7)))
1705 return -1; /* integer overflow */
1707 b = *(buf++);
1708 len--;
1709 value = (value << 7) + (b & 0x7f);
1711 while (b & 0x80);
1713 *bufp = buf;
1714 *lenp = len;
1715 return value;
1719 static void MP4_FreeBox_esds( MP4_Box_t *p_box )
1721 free( p_box->data.p_esds->es_descriptor.psz_URL );
1722 if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1724 free( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1725 free( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1729 static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
1731 #define es_descriptor p_box->data.p_esds->es_descriptor
1732 uint64_t i_len;
1733 unsigned int i_flags;
1734 unsigned int i_type;
1736 MP4_READBOX_ENTER( MP4_Box_data_esds_t, MP4_FreeBox_esds );
1738 MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1741 MP4_GET1BYTE( i_type );
1742 if( i_type == 0x03 ) /* MP4ESDescrTag ISO/IEC 14496-1 8.3.3 */
1744 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1745 if( unlikely(i_len == UINT64_C(-1)) )
1746 MP4_READBOX_EXIT( 0 );
1748 #ifdef MP4_VERBOSE
1749 msg_Dbg( p_stream, "found esds MPEG4ESDescr (%"PRIu64" bytes)",
1750 i_len );
1751 #endif
1753 MP4_GET2BYTES( es_descriptor.i_ES_ID );
1754 MP4_GET1BYTE( i_flags );
1755 es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1756 es_descriptor.b_url = ( (i_flags&0x40) != 0);
1757 es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1759 es_descriptor.i_stream_priority = i_flags&0x1f;
1760 if( es_descriptor.b_stream_dependence )
1762 MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1764 if( es_descriptor.b_url && i_read > 0 )
1766 uint8_t i_url;
1768 MP4_GET1BYTE( i_url );
1769 if( i_url > i_read )
1770 MP4_READBOX_EXIT( 1 );
1771 es_descriptor.psz_URL = malloc( (unsigned) i_url + 1 );
1772 if( es_descriptor.psz_URL )
1774 memcpy( es_descriptor.psz_URL, p_peek, i_url );
1775 es_descriptor.psz_URL[i_url] = 0;
1777 p_peek += i_url;
1778 i_read -= i_url;
1780 else
1782 es_descriptor.psz_URL = NULL;
1784 if( es_descriptor.b_OCRstream )
1786 MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1788 MP4_GET1BYTE( i_type ); /* get next type */
1791 if( i_type != 0x04)/* MP4DecConfigDescrTag ISO/IEC 14496-1 8.3.4 */
1793 es_descriptor.p_decConfigDescr = NULL;
1794 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1797 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1798 if( unlikely(i_len == UINT64_C(-1)) )
1799 MP4_READBOX_EXIT( 0 );
1800 #ifdef MP4_VERBOSE
1801 msg_Dbg( p_stream, "found esds MP4DecConfigDescr (%"PRIu64" bytes)",
1802 i_len );
1803 #endif
1805 es_descriptor.p_decConfigDescr =
1806 calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
1807 if( unlikely( es_descriptor.p_decConfigDescr == NULL ) )
1808 MP4_READBOX_EXIT( 0 );
1810 MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectProfileIndication );
1811 MP4_GET1BYTE( i_flags );
1812 es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1813 es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1814 MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1815 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1816 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1817 MP4_GET1BYTE( i_type );
1818 if( i_type != 0x05 )/* MP4DecSpecificDescrTag ISO/IEC 14496-1 8.3.5 */
1820 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1821 es_descriptor.p_decConfigDescr->p_decoder_specific_info = NULL;
1822 MP4_READBOX_EXIT( 1 );
1825 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1826 if( unlikely(i_len == UINT64_C(-1)) )
1827 MP4_READBOX_EXIT( 0 );
1828 #ifdef MP4_VERBOSE
1829 msg_Dbg( p_stream, "found esds MP4DecSpecificDescr (%"PRIu64" bytes)",
1830 i_len );
1831 #endif
1832 if( i_len > i_read )
1833 MP4_READBOX_EXIT( 0 );
1835 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1836 es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1837 if( unlikely( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL ) )
1838 MP4_READBOX_EXIT( 0 );
1840 memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1841 p_peek, i_len );
1843 MP4_READBOX_EXIT( 1 );
1844 #undef es_descriptor
1847 static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
1849 MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1851 free( p_avcC->p_avcC );
1853 if( p_avcC->sps )
1855 for( int i = 0; i < p_avcC->i_sps; i++ )
1856 free( p_avcC->sps[i] );
1857 free( p_avcC->sps );
1859 free( p_avcC->i_sps_length );
1861 if( p_avcC->pps )
1863 for( int i = 0; i < p_avcC->i_pps; i++ )
1864 free( p_avcC->pps[i] );
1865 free( p_avcC->pps );
1867 free( p_avcC->i_pps_length );
1870 static int MP4_ReadBox_avcC( stream_t *p_stream, MP4_Box_t *p_box )
1872 MP4_Box_data_avcC_t *p_avcC;
1873 int i;
1875 MP4_READBOX_ENTER( MP4_Box_data_avcC_t, MP4_FreeBox_avcC );
1876 p_avcC = p_box->data.p_avcC;
1878 p_avcC->i_avcC = i_read;
1879 if( p_avcC->i_avcC > 0 )
1881 uint8_t * p = p_avcC->p_avcC = malloc( p_avcC->i_avcC );
1882 if( p )
1883 memcpy( p, p_peek, i_read );
1886 MP4_GET1BYTE( p_avcC->i_version );
1887 MP4_GET1BYTE( p_avcC->i_profile );
1888 MP4_GET1BYTE( p_avcC->i_profile_compatibility );
1889 MP4_GET1BYTE( p_avcC->i_level );
1890 MP4_GET1BYTE( p_avcC->i_reserved1 );
1891 p_avcC->i_length_size = (p_avcC->i_reserved1&0x03) + 1;
1892 p_avcC->i_reserved1 >>= 2;
1894 MP4_GET1BYTE( p_avcC->i_reserved2 );
1895 p_avcC->i_sps = p_avcC->i_reserved2&0x1f;
1896 p_avcC->i_reserved2 >>= 5;
1898 if( p_avcC->i_sps > 0 )
1900 p_avcC->i_sps_length = calloc( p_avcC->i_sps, sizeof( uint16_t ) );
1901 p_avcC->sps = calloc( p_avcC->i_sps, sizeof( uint8_t* ) );
1903 if( !p_avcC->i_sps_length || !p_avcC->sps )
1904 goto error;
1906 for( i = 0; i < p_avcC->i_sps && i_read > 2; i++ )
1908 MP4_GET2BYTES( p_avcC->i_sps_length[i] );
1909 if ( p_avcC->i_sps_length[i] > i_read )
1910 goto error;
1911 p_avcC->sps[i] = malloc( p_avcC->i_sps_length[i] );
1912 if( p_avcC->sps[i] )
1913 memcpy( p_avcC->sps[i], p_peek, p_avcC->i_sps_length[i] );
1915 p_peek += p_avcC->i_sps_length[i];
1916 i_read -= p_avcC->i_sps_length[i];
1918 if ( i != p_avcC->i_sps )
1919 goto error;
1922 MP4_GET1BYTE( p_avcC->i_pps );
1923 if( p_avcC->i_pps > 0 )
1925 p_avcC->i_pps_length = calloc( p_avcC->i_pps, sizeof( uint16_t ) );
1926 p_avcC->pps = calloc( p_avcC->i_pps, sizeof( uint8_t* ) );
1928 if( !p_avcC->i_pps_length || !p_avcC->pps )
1929 goto error;
1931 for( i = 0; i < p_avcC->i_pps && i_read > 2; i++ )
1933 MP4_GET2BYTES( p_avcC->i_pps_length[i] );
1934 if( p_avcC->i_pps_length[i] > i_read )
1935 goto error;
1936 p_avcC->pps[i] = malloc( p_avcC->i_pps_length[i] );
1937 if( p_avcC->pps[i] )
1938 memcpy( p_avcC->pps[i], p_peek, p_avcC->i_pps_length[i] );
1940 p_peek += p_avcC->i_pps_length[i];
1941 i_read -= p_avcC->i_pps_length[i];
1943 if ( i != p_avcC->i_pps )
1944 goto error;
1946 #ifdef MP4_VERBOSE
1947 msg_Dbg( p_stream,
1948 "read box: \"avcC\" version=%d profile=0x%x level=0x%x length size=%d sps=%d pps=%d",
1949 p_avcC->i_version, p_avcC->i_profile, p_avcC->i_level,
1950 p_avcC->i_length_size,
1951 p_avcC->i_sps, p_avcC->i_pps );
1952 for( i = 0; i < p_avcC->i_sps; i++ )
1954 msg_Dbg( p_stream, " - sps[%d] length=%d",
1955 i, p_avcC->i_sps_length[i] );
1957 for( i = 0; i < p_avcC->i_pps; i++ )
1959 msg_Dbg( p_stream, " - pps[%d] length=%d",
1960 i, p_avcC->i_pps_length[i] );
1963 #endif
1964 MP4_READBOX_EXIT( 1 );
1966 error:
1967 MP4_READBOX_EXIT( 0 );
1970 static void MP4_FreeBox_vpcC( MP4_Box_t *p_box )
1972 free( p_box->data.p_vpcC->p_codec_init_data );
1975 static int MP4_ReadBox_vpcC( stream_t *p_stream, MP4_Box_t *p_box )
1977 MP4_READBOX_ENTER( MP4_Box_data_vpcC_t, MP4_FreeBox_vpcC );
1978 MP4_Box_data_vpcC_t *p_vpcC = p_box->data.p_vpcC;
1980 if( p_box->i_size < 6 )
1981 MP4_READBOX_EXIT( 0 );
1983 uint8_t i_version;
1984 MP4_GET1BYTE( i_version );
1985 if( i_version != 0 )
1986 MP4_READBOX_EXIT( 0 );
1988 MP4_GET1BYTE( p_vpcC->i_profile );
1989 MP4_GET1BYTE( p_vpcC->i_level );
1990 MP4_GET1BYTE( p_vpcC->i_bit_depth );
1991 p_vpcC->i_color_space = p_vpcC->i_bit_depth & 0x0F;
1992 p_vpcC->i_bit_depth >>= 4;
1993 MP4_GET1BYTE( p_vpcC->i_chroma_subsampling );
1994 p_vpcC->i_xfer_function = ( p_vpcC->i_chroma_subsampling & 0x0F ) >> 1;
1995 p_vpcC->i_fullrange = p_vpcC->i_chroma_subsampling & 0x01;
1996 p_vpcC->i_chroma_subsampling >>= 4;
1997 MP4_GET2BYTES( p_vpcC->i_codec_init_datasize );
1998 if( p_vpcC->i_codec_init_datasize > i_read )
1999 p_vpcC->i_codec_init_datasize = i_read;
2001 if( p_vpcC->i_codec_init_datasize )
2003 p_vpcC->p_codec_init_data = malloc( i_read );
2004 if( !p_vpcC->p_codec_init_data )
2005 MP4_READBOX_EXIT( 0 );
2006 memcpy( p_vpcC->p_codec_init_data, p_peek, i_read );
2009 MP4_READBOX_EXIT( 1 );
2012 static void MP4_FreeBox_WMA2( MP4_Box_t *p_box )
2014 free( p_box->data.p_WMA2->p_extra );
2017 static int MP4_ReadBox_WMA2( stream_t *p_stream, MP4_Box_t *p_box )
2019 MP4_READBOX_ENTER( MP4_Box_data_WMA2_t, MP4_FreeBox_WMA2 );
2021 MP4_Box_data_WMA2_t *p_WMA2 = p_box->data.p_WMA2;
2023 MP4_GET2BYTESLE( p_WMA2->Format.wFormatTag );
2024 MP4_GET2BYTESLE( p_WMA2->Format.nChannels );
2025 MP4_GET4BYTESLE( p_WMA2->Format.nSamplesPerSec );
2026 MP4_GET4BYTESLE( p_WMA2->Format.nAvgBytesPerSec );
2027 MP4_GET2BYTESLE( p_WMA2->Format.nBlockAlign );
2028 MP4_GET2BYTESLE( p_WMA2->Format.wBitsPerSample );
2030 uint16_t i_cbSize;
2031 MP4_GET2BYTESLE( i_cbSize );
2033 if( i_cbSize > i_read )
2034 goto error;
2036 p_WMA2->i_extra = i_cbSize;
2037 if ( p_WMA2->i_extra )
2039 p_WMA2->p_extra = malloc( p_WMA2->i_extra );
2040 if ( ! p_WMA2->p_extra )
2041 goto error;
2042 memcpy( p_WMA2->p_extra, p_peek, p_WMA2->i_extra );
2045 MP4_READBOX_EXIT( 1 );
2047 error:
2048 MP4_READBOX_EXIT( 0 );
2051 static void MP4_FreeBox_strf( MP4_Box_t *p_box )
2053 free( p_box->data.p_strf->p_extra );
2056 static int MP4_ReadBox_strf( stream_t *p_stream, MP4_Box_t *p_box )
2058 MP4_READBOX_ENTER( MP4_Box_data_strf_t, MP4_FreeBox_strf );
2060 MP4_Box_data_strf_t *p_strf = p_box->data.p_strf;
2062 if( i_read < 40 )
2063 goto error;
2065 MP4_GET4BYTESLE( p_strf->bmiHeader.biSize );
2066 MP4_GET4BYTESLE( p_strf->bmiHeader.biWidth );
2067 MP4_GET4BYTESLE( p_strf->bmiHeader.biHeight );
2068 MP4_GET2BYTESLE( p_strf->bmiHeader.biPlanes );
2069 MP4_GET2BYTESLE( p_strf->bmiHeader.biBitCount );
2070 MP4_GETFOURCC( p_strf->bmiHeader.biCompression );
2071 MP4_GET4BYTESLE( p_strf->bmiHeader.biSizeImage );
2072 MP4_GET4BYTESLE( p_strf->bmiHeader.biXPelsPerMeter );
2073 MP4_GET4BYTESLE( p_strf->bmiHeader.biYPelsPerMeter );
2074 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrUsed );
2075 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrImportant );
2077 p_strf->i_extra = i_read;
2078 if ( p_strf->i_extra )
2080 p_strf->p_extra = malloc( p_strf->i_extra );
2081 if ( ! p_strf->p_extra )
2082 goto error;
2083 memcpy( p_strf->p_extra, p_peek, i_read );
2086 MP4_READBOX_EXIT( 1 );
2088 error:
2089 MP4_READBOX_EXIT( 0 );
2092 static int MP4_ReadBox_ASF( stream_t *p_stream, MP4_Box_t *p_box )
2094 MP4_READBOX_ENTER( MP4_Box_data_ASF_t, NULL );
2096 MP4_Box_data_ASF_t *p_asf = p_box->data.p_asf;
2098 if (i_read != 8)
2099 MP4_READBOX_EXIT( 0 );
2101 MP4_GET1BYTE( p_asf->i_stream_number );
2102 /* remaining is unknown */
2104 MP4_READBOX_EXIT( 1 );
2107 static void MP4_FreeBox_sbgp( MP4_Box_t *p_box )
2109 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2110 free( p_sbgp->entries.pi_sample_count );
2111 free( p_sbgp->entries.pi_group_description_index );
2114 static int MP4_ReadBox_sbgp( stream_t *p_stream, MP4_Box_t *p_box )
2116 MP4_READBOX_ENTER( MP4_Box_data_sbgp_t, MP4_FreeBox_sbgp );
2117 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2118 uint32_t i_flags;
2120 if ( i_read < 12 )
2121 MP4_READBOX_EXIT( 0 );
2123 MP4_GET1BYTE( p_sbgp->i_version );
2124 MP4_GET3BYTES( i_flags );
2125 if( i_flags != 0 )
2126 MP4_READBOX_EXIT( 0 );
2128 MP4_GETFOURCC( p_sbgp->i_grouping_type );
2130 if( p_sbgp->i_version == 1 )
2132 if( i_read < 8 )
2133 MP4_READBOX_EXIT( 0 );
2134 MP4_GET4BYTES( p_sbgp->i_grouping_type_parameter );
2137 MP4_GET4BYTES( p_sbgp->i_entry_count );
2138 if( p_sbgp->i_entry_count > i_read / (4 + 4) )
2139 p_sbgp->i_entry_count = i_read / (4 + 4);
2141 p_sbgp->entries.pi_sample_count = vlc_alloc( p_sbgp->i_entry_count, sizeof(uint32_t) );
2142 p_sbgp->entries.pi_group_description_index = vlc_alloc( p_sbgp->i_entry_count, sizeof(uint32_t) );
2144 if( !p_sbgp->entries.pi_sample_count || !p_sbgp->entries.pi_group_description_index )
2146 MP4_FreeBox_sbgp( p_box );
2147 MP4_READBOX_EXIT( 0 );
2150 for( uint32_t i=0; i<p_sbgp->i_entry_count; i++ )
2152 MP4_GET4BYTES( p_sbgp->entries.pi_sample_count[i] );
2153 MP4_GET4BYTES( p_sbgp->entries.pi_group_description_index[i] );
2156 #ifdef MP4_VERBOSE
2157 msg_Dbg( p_stream,
2158 "read box: \"sbgp\" grouping type %4.4s", (char*) &p_sbgp->i_grouping_type );
2159 #ifdef MP4_ULTRA_VERBOSE
2160 for (uint32_t i = 0; i < p_sbgp->i_entry_count; i++)
2161 msg_Dbg( p_stream, "\t samples %" PRIu32 " group %" PRIu32,
2162 p_sbgp->entries.pi_sample_count[i],
2163 p_sbgp->entries.pi_group_description_index[i] );
2164 #endif
2165 #endif
2167 MP4_READBOX_EXIT( 1 );
2170 static void MP4_FreeBox_sgpd( MP4_Box_t *p_box )
2172 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2173 free( p_sgpd->p_entries );
2176 static int MP4_ReadBox_sgpd( stream_t *p_stream, MP4_Box_t *p_box )
2178 MP4_READBOX_ENTER( MP4_Box_data_sgpd_t, MP4_FreeBox_sgpd );
2179 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2180 uint32_t i_flags;
2181 uint32_t i_default_length = 0;
2183 if ( i_read < 8 )
2184 MP4_READBOX_EXIT( 0 );
2186 MP4_GET1BYTE( p_sgpd->i_version );
2187 MP4_GET3BYTES( i_flags );
2188 if( i_flags != 0 )
2189 MP4_READBOX_EXIT( 0 );
2191 MP4_GETFOURCC( p_sgpd->i_grouping_type );
2193 switch( p_sgpd->i_grouping_type )
2195 case SAMPLEGROUP_rap:
2196 break;
2198 default:
2199 #ifdef MP4_VERBOSE
2200 msg_Dbg( p_stream,
2201 "read box: \"sgpd\" grouping type %4.4s (unimplemented)", (char*) &p_sgpd->i_grouping_type );
2202 #endif
2203 MP4_READBOX_EXIT( 1 );
2206 if( p_sgpd->i_version == 1 )
2208 if( i_read < 8 )
2209 MP4_READBOX_EXIT( 0 );
2210 MP4_GET4BYTES( i_default_length );
2212 else if( p_sgpd->i_version >= 2 )
2214 if( i_read < 8 )
2215 MP4_READBOX_EXIT( 0 );
2216 MP4_GET4BYTES( p_sgpd->i_default_sample_description_index );
2219 MP4_GET4BYTES( p_sgpd->i_entry_count );
2221 p_sgpd->p_entries = vlc_alloc( p_sgpd->i_entry_count, sizeof(*p_sgpd->p_entries) );
2222 if( !p_sgpd->p_entries )
2223 MP4_READBOX_EXIT( 0 );
2225 uint32_t i = 0;
2226 for( ; i<p_sgpd->i_entry_count; i++ )
2228 uint32_t i_description_length = i_default_length;
2229 if( p_sgpd->i_version == 1 && i_default_length == 0 )
2231 if( i_read < 4 )
2232 break;
2233 MP4_GET4BYTES( i_description_length );
2236 if( p_sgpd->i_version == 1 && i_read < i_description_length )
2237 break;
2239 switch( p_sgpd->i_grouping_type )
2241 case SAMPLEGROUP_rap:
2243 if( i_read < 1 )
2245 p_sgpd->i_entry_count = 0;
2246 MP4_FreeBox_sgpd( p_box );
2247 MP4_READBOX_EXIT( 0 );
2249 uint8_t i_data;
2250 MP4_GET1BYTE( i_data );
2251 p_sgpd->p_entries[i].rap.i_num_leading_samples_known = i_data & 0x80;
2252 p_sgpd->p_entries[i].rap.i_num_leading_samples = i_data & 0x7F;
2254 break;
2256 default:
2257 assert(0);
2261 if( i != p_sgpd->i_entry_count )
2262 p_sgpd->i_entry_count = i;
2264 #ifdef MP4_VERBOSE
2265 msg_Dbg( p_stream,
2266 "read box: \"sgpd\" grouping type %4.4s", (char*) &p_sgpd->i_grouping_type );
2267 #endif
2269 MP4_READBOX_EXIT( 1 );
2272 static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
2274 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2275 free( p_chan->layout.p_descriptions );
2278 static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
2280 MP4_READBOX_ENTER( MP4_Box_data_chan_t, MP4_FreeBox_stsdext_chan );
2281 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2283 if ( i_read < 16 )
2284 MP4_READBOX_EXIT( 0 );
2286 MP4_GET1BYTE( p_chan->i_version );
2287 MP4_GET3BYTES( p_chan->i_channels_flags );
2288 MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
2289 MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
2290 MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
2292 size_t i_descsize = 8 + 3 * sizeof(float);
2293 if ( i_read < p_chan->layout.i_channels_description_count * i_descsize )
2294 MP4_READBOX_EXIT( 0 );
2296 p_chan->layout.p_descriptions =
2297 vlc_alloc( p_chan->layout.i_channels_description_count, i_descsize );
2299 if ( !p_chan->layout.p_descriptions )
2300 MP4_READBOX_EXIT( 0 );
2302 uint32_t i;
2303 for( i=0; i<p_chan->layout.i_channels_description_count; i++ )
2305 if ( i_read < 20 )
2306 break;
2307 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
2308 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
2309 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
2310 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
2311 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
2313 if ( i<p_chan->layout.i_channels_description_count )
2314 p_chan->layout.i_channels_description_count = i;
2316 #ifdef MP4_VERBOSE
2317 msg_Dbg( p_stream,
2318 "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
2319 p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
2320 p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
2321 #endif
2322 MP4_READBOX_EXIT( 1 );
2325 static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
2327 MP4_READBOX_ENTER( MP4_Box_data_dec3_t, NULL );
2329 MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
2331 unsigned i_header;
2332 MP4_GET2BYTES( i_header );
2334 p_dec3->i_data_rate = i_header >> 3;
2335 p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
2336 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
2337 MP4_GET3BYTES( i_header );
2338 p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
2339 p_dec3->stream[i].i_bsid = ( i_header >> 17 ) & 0x01f;
2340 p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
2341 p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
2342 p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
2343 p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
2344 if (p_dec3->stream[i].i_num_dep_sub) {
2345 MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
2346 p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
2347 } else
2348 p_dec3->stream[i].i_chan_loc = 0;
2351 #ifdef MP4_VERBOSE
2352 msg_Dbg( p_stream,
2353 "read box: \"dec3\" bitrate %dkbps %d independent substreams",
2354 p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
2356 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
2357 msg_Dbg( p_stream,
2358 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
2359 "num dependent subs=%d chan_loc=0x%x",
2360 i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
2361 p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
2362 #endif
2363 MP4_READBOX_EXIT( 1 );
2366 static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
2368 MP4_Box_data_dac3_t *p_dac3;
2369 MP4_READBOX_ENTER( MP4_Box_data_dac3_t, NULL );
2371 p_dac3 = p_box->data.p_dac3;
2373 unsigned i_header;
2374 MP4_GET3BYTES( i_header );
2376 p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
2377 p_dac3->i_bsid = ( i_header >> 17 ) & 0x01f;
2378 p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
2379 p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
2380 p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
2381 p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
2383 #ifdef MP4_VERBOSE
2384 msg_Dbg( p_stream,
2385 "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
2386 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 );
2387 #endif
2388 MP4_READBOX_EXIT( 1 );
2391 static void MP4_FreeBox_dvc1( MP4_Box_t *p_box )
2393 free( p_box->data.p_dvc1->p_vc1 );
2396 static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
2398 MP4_READBOX_ENTER( MP4_Box_data_dvc1_t, MP4_FreeBox_dvc1 );
2399 if( i_read < 7 )
2400 MP4_READBOX_EXIT( 0 );
2402 MP4_Box_data_dvc1_t *p_dvc1 = p_box->data.p_dvc1;
2403 MP4_GET1BYTE( p_dvc1->i_profile_level );
2404 p_dvc1->i_vc1 = i_read; /* Header + profile_level */
2405 if( p_dvc1->i_vc1 > 0 && (p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 )) )
2406 memcpy( p_dvc1->p_vc1, p_peek, i_read );
2408 #ifdef MP4_VERBOSE
2409 msg_Dbg( p_stream,
2410 "read box: \"dvc1\" profile=%"PRIu8, (p_dvc1->i_profile_level & 0xf0) >> 4 );
2411 #endif
2413 MP4_READBOX_EXIT( 1 );
2416 static int MP4_ReadBox_fiel( stream_t *p_stream, MP4_Box_t *p_box )
2418 MP4_Box_data_fiel_t *p_fiel;
2419 MP4_READBOX_ENTER( MP4_Box_data_fiel_t, NULL );
2420 p_fiel = p_box->data.p_fiel;
2421 if(i_read < 2)
2422 MP4_READBOX_EXIT( 0 );
2423 if(p_peek[0] == 1)
2425 p_fiel->i_flags = BLOCK_FLAG_SINGLE_FIELD;
2427 else if(p_peek[0] == 2) /* Interlaced */
2430 * 0 – There is only one field.
2431 * 1 – T is displayed earliest, T is stored first in the file.
2432 * 6 – B is displayed earliest, B is stored first in the file.
2433 * 9 – B is displayed earliest, T is stored first in the file.
2434 * 14 – T is displayed earliest, B is stored first in the file.
2436 if(p_peek[1] == 1 || p_peek[1] == 9)
2437 p_fiel->i_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
2438 else if(p_peek[1] == 6 || p_peek[1] == 14)
2439 p_fiel->i_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
2441 MP4_READBOX_EXIT( 1 );
2444 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
2446 MP4_Box_data_enda_t *p_enda;
2447 MP4_READBOX_ENTER( MP4_Box_data_enda_t, NULL );
2449 p_enda = p_box->data.p_enda;
2451 MP4_GET2BYTES( p_enda->i_little_endian );
2453 #ifdef MP4_VERBOSE
2454 msg_Dbg( p_stream,
2455 "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
2456 #endif
2457 MP4_READBOX_EXIT( 1 );
2460 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
2462 free( p_box->data.p_sample_soun->p_qt_description );
2465 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
2467 p_box->i_handler = ATOM_soun;
2468 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t, MP4_FreeBox_sample_soun );
2469 p_box->data.p_sample_soun->p_qt_description = NULL;
2471 size_t i_actually_read = i_read + header_size;
2473 /* Sanity check needed because the "wave" box does also contain an
2474 * "mp4a" box that we don't understand. */
2475 if( i_read < 28 )
2477 MP4_READBOX_EXIT( 1 );
2480 for( unsigned i = 0; i < 6 ; i++ )
2482 MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
2485 MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
2488 * XXX hack -> produce a copy of the nearly complete chunk
2490 p_box->data.p_sample_soun->i_qt_description = 0;
2491 p_box->data.p_sample_soun->p_qt_description = NULL;
2492 if( i_read > 0 )
2494 p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
2495 if( p_box->data.p_sample_soun->p_qt_description )
2497 p_box->data.p_sample_soun->i_qt_description = i_read;
2498 memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
2502 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
2503 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
2504 MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
2506 MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
2507 MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
2508 MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
2509 MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
2510 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
2511 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
2513 #ifdef MP4_VERBOSE
2514 msg_Dbg( p_stream,
2515 "read box: \"soun\" stsd qt_version %"PRIu16" compid=%"PRIx16,
2516 p_box->data.p_sample_soun->i_qt_version,
2517 p_box->data.p_sample_soun->i_compressionid );
2518 #endif
2519 /* @36 bytes */
2520 if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
2522 /* SoundDescriptionV1 */
2523 MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
2524 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
2525 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
2526 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
2528 #ifdef MP4_VERBOSE
2529 msg_Dbg( p_stream,
2530 "read box: \"soun\" V1 sample/packet=%d bytes/packet=%d "
2531 "bytes/frame=%d bytes/sample=%d",
2532 p_box->data.p_sample_soun->i_sample_per_packet,
2533 p_box->data.p_sample_soun->i_bytes_per_packet,
2534 p_box->data.p_sample_soun->i_bytes_per_frame,
2535 p_box->data.p_sample_soun->i_bytes_per_sample );
2536 #endif
2537 /* @52 bytes */
2539 else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
2541 /* SoundDescriptionV2 */
2542 double f_sample_rate;
2543 int64_t i_dummy64;
2544 uint32_t i_channel, i_extoffset, i_dummy32;
2546 /* Checks */
2547 if ( p_box->data.p_sample_soun->i_channelcount != 0x3 ||
2548 p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
2549 p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
2550 p_box->data.p_sample_soun->i_reserved3 != 0x0 ||
2551 p_box->data.p_sample_soun->i_sampleratehi != 0x1 ||//65536
2552 p_box->data.p_sample_soun->i_sampleratelo != 0x0 ) //remainder
2554 msg_Err( p_stream, "invalid stsd V2 box defaults" );
2555 MP4_READBOX_EXIT( 0 );
2557 /* !Checks */
2559 MP4_GET4BYTES( i_extoffset ); /* offset to stsd extentions */
2560 MP4_GET8BYTES( i_dummy64 );
2561 memcpy( &f_sample_rate, &i_dummy64, 8 );
2562 msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
2563 /* Rounding error with lo, but we don't care since we do not support fractional audio rate */
2564 p_box->data.p_sample_soun->i_sampleratehi = (uint32_t)f_sample_rate;
2565 p_box->data.p_sample_soun->i_sampleratelo = (f_sample_rate - p_box->data.p_sample_soun->i_sampleratehi);
2567 MP4_GET4BYTES( i_channel );
2568 p_box->data.p_sample_soun->i_channelcount = i_channel;
2570 MP4_GET4BYTES( i_dummy32 );
2571 if ( i_dummy32 != 0x7F000000 )
2573 msg_Err( p_stream, "invalid stsd V2 box" );
2574 MP4_READBOX_EXIT( 0 );
2577 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
2578 MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
2579 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
2580 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2582 #ifdef MP4_VERBOSE
2583 msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
2584 "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
2585 f_sample_rate,
2586 p_box->data.p_sample_soun->i_constbitsperchannel,
2587 p_box->data.p_sample_soun->i_formatflags,
2588 p_box->data.p_sample_soun->i_constbytesperaudiopacket,
2589 p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2590 #endif
2591 /* @72 bytes + */
2592 if( i_extoffset > i_actually_read )
2593 i_extoffset = i_actually_read;
2594 p_peek = &p_buff[i_extoffset];
2595 i_read = i_actually_read - i_extoffset;
2597 else
2599 p_box->data.p_sample_soun->i_sample_per_packet = 0;
2600 p_box->data.p_sample_soun->i_bytes_per_packet = 0;
2601 p_box->data.p_sample_soun->i_bytes_per_frame = 0;
2602 p_box->data.p_sample_soun->i_bytes_per_sample = 0;
2604 #ifdef MP4_VERBOSE
2605 msg_Dbg( p_stream, "read box: \"soun\" V0 or qt1/2 (rest=%"PRIu64")",
2606 i_read );
2607 #endif
2608 /* @36 bytes */
2611 if( p_box->i_type == ATOM_drms )
2613 msg_Warn( p_stream, "DRM protected streams are not supported." );
2614 MP4_READBOX_EXIT( 0 );
2617 if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
2619 /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
2620 p_box->data.p_sample_soun->i_channelcount = 1;
2623 /* Loads extensions */
2624 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2625 p_box->i_pos + p_peek - p_buff ); /* esds/wave/... */
2627 #ifdef MP4_VERBOSE
2628 msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
2629 "sample size %d sample rate %f",
2630 p_box->data.p_sample_soun->i_channelcount,
2631 p_box->data.p_sample_soun->i_samplesize,
2632 (float)p_box->data.p_sample_soun->i_sampleratehi +
2633 (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
2635 #endif
2636 MP4_READBOX_EXIT( 1 );
2639 static void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
2641 free( p_box->data.p_sample_vide->p_qt_image_description );
2644 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
2646 p_box->i_handler = ATOM_vide;
2647 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t, MP4_FreeBox_sample_vide );
2649 size_t i_actually_read = i_read + header_size;
2651 for( unsigned i = 0; i < 6 ; i++ )
2653 MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
2656 MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
2659 * XXX hack -> produce a copy of the nearly complete chunk
2661 if( i_read > 0 )
2663 p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
2664 if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
2665 MP4_READBOX_EXIT( 0 );
2666 p_box->data.p_sample_vide->i_qt_image_description = i_read;
2667 memcpy( p_box->data.p_sample_vide->p_qt_image_description,
2668 p_peek, i_read );
2670 else
2672 p_box->data.p_sample_vide->i_qt_image_description = 0;
2673 p_box->data.p_sample_vide->p_qt_image_description = NULL;
2676 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
2677 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
2678 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
2680 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
2681 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
2683 MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
2684 MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
2686 MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
2687 MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
2689 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
2690 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
2692 if ( i_read < 32 )
2693 MP4_READBOX_EXIT( 0 );
2694 if( p_peek[0] <= 31 ) // Must be Pascal String
2696 memcpy( &p_box->data.p_sample_vide->sz_compressorname, &p_peek[1], p_peek[0] );
2697 p_box->data.p_sample_vide->sz_compressorname[p_peek[0]] = 0;
2699 p_peek += 32; i_read -= 32;
2701 MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
2702 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
2704 if( p_box->i_type == ATOM_drmi )
2706 msg_Warn( p_stream, "DRM protected streams are not supported." );
2707 MP4_READBOX_EXIT( 0 );
2710 if( i_actually_read > 78 && p_peek - p_buff > 78 )
2712 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2713 p_box->i_pos + p_peek - p_buff );
2716 #ifdef MP4_VERBOSE
2717 msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d (%s)",
2718 p_box->data.p_sample_vide->i_width,
2719 p_box->data.p_sample_vide->i_height,
2720 p_box->data.p_sample_vide->i_depth,
2721 p_box->data.p_sample_vide->sz_compressorname );
2723 #endif
2724 MP4_READBOX_EXIT( 1 );
2727 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
2729 p_box->i_handler = ATOM_text;
2730 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_text_t, 16, NULL );
2731 (void) p_peek;
2732 if( i_read < 8 )
2733 MP4_READBOX_EXIT( 0 );
2735 MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
2737 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2738 MP4_READBOX_EXIT( 0 );
2740 MP4_READBOX_EXIT( 1 );
2743 static void MP4_FreeBox_sample_hint( MP4_Box_t *p_box )
2745 free( p_box->data.p_sample_hint->p_data );
2748 static int MP4_ReadBox_sample_hint8( stream_t *p_stream, MP4_Box_t *p_box )
2750 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_hint_t, 24, MP4_FreeBox_sample_hint );
2752 for( unsigned i = 0; i < 6 ; i++ )
2754 MP4_GET1BYTE( p_box->data.p_sample_hint->i_reserved1[i] );
2757 MP4_GET2BYTES( p_box->data.p_sample_hint->i_data_reference_index );
2759 if( !(p_box->data.p_sample_hint->p_data = malloc(8)) )
2760 MP4_READBOX_EXIT( 0 );
2762 MP4_GET8BYTES( *(p_box->data.p_sample_hint->p_data) );
2764 MP4_ReadBoxContainerChildren(p_stream, p_box, NULL);
2766 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2767 MP4_READBOX_EXIT( 0 );
2769 MP4_READBOX_EXIT( 1 );
2772 static void MP4_FreeBox_sample_text( MP4_Box_t *p_box )
2774 free( p_box->data.p_sample_text->p_data );
2777 static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
2779 p_box->i_handler = ATOM_text;
2780 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t, MP4_FreeBox_sample_text );
2782 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2783 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2785 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2787 if( i_read )
2789 p_box->data.p_sample_text->p_data = malloc( i_read );
2790 if( !p_box->data.p_sample_text->p_data )
2791 MP4_READBOX_EXIT( 0 );
2792 memcpy( p_box->data.p_sample_text->p_data, p_peek, i_read );
2794 p_box->data.p_sample_text->i_data = i_read;
2796 #ifdef MP4_VERBOSE
2797 msg_Dbg( p_stream, "read box: \"text\" in stsd text" );
2798 #endif
2799 MP4_READBOX_EXIT( 1 );
2802 static int MP4_ReadBox_sample_clcp( stream_t *p_stream, MP4_Box_t *p_box )
2804 p_box->i_handler = ATOM_clcp;
2805 MP4_READBOX_ENTER( MP4_Box_data_sample_clcp_t, NULL );
2807 if( i_read < 8 )
2808 MP4_READBOX_EXIT( 0 );
2810 for( int i=0; i<6; i++ )
2811 MP4_GET1BYTE( p_box->data.p_sample_clcp->i_reserved1[i] );
2812 MP4_GET2BYTES( p_box->data.p_sample_clcp->i_data_reference_index );
2814 #ifdef MP4_VERBOSE
2815 msg_Dbg( p_stream, "read box: \"clcp\" in stsd" );
2816 #endif
2817 MP4_READBOX_EXIT( 1 );
2820 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
2822 free( p_box->data.p_stsz->i_entry_size );
2825 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
2827 uint32_t count;
2829 MP4_READBOX_ENTER( MP4_Box_data_stsz_t, MP4_FreeBox_stsz );
2831 MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
2833 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
2834 MP4_GET4BYTES( count );
2835 p_box->data.p_stsz->i_sample_count = count;
2837 if( p_box->data.p_stsz->i_sample_size == 0 )
2839 if( UINT64_C(4) * count > i_read )
2840 MP4_READBOX_EXIT( 0 );
2842 p_box->data.p_stsz->i_entry_size =
2843 vlc_alloc( count, sizeof(uint32_t) );
2844 if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
2845 MP4_READBOX_EXIT( 0 );
2847 for( uint32_t i = 0; i < count; i++ )
2849 MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
2852 else
2853 p_box->data.p_stsz->i_entry_size = NULL;
2855 #ifdef MP4_VERBOSE
2856 msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
2857 p_box->data.p_stsz->i_sample_size,
2858 p_box->data.p_stsz->i_sample_count );
2860 #endif
2861 MP4_READBOX_EXIT( 1 );
2864 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
2866 free( p_box->data.p_stsc->i_first_chunk );
2867 free( p_box->data.p_stsc->i_samples_per_chunk );
2868 free( p_box->data.p_stsc->i_sample_description_index );
2871 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
2873 uint32_t count;
2875 MP4_READBOX_ENTER( MP4_Box_data_stsc_t, MP4_FreeBox_stsc );
2877 MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
2878 MP4_GET4BYTES( count );
2880 if( UINT64_C(12) * count > i_read )
2881 MP4_READBOX_EXIT( 0 );
2883 p_box->data.p_stsc->i_first_chunk = vlc_alloc( count, sizeof(uint32_t) );
2884 p_box->data.p_stsc->i_samples_per_chunk = vlc_alloc( count,
2885 sizeof(uint32_t) );
2886 p_box->data.p_stsc->i_sample_description_index = vlc_alloc( count,
2887 sizeof(uint32_t) );
2888 if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
2889 || p_box->data.p_stsc->i_samples_per_chunk == NULL
2890 || p_box->data.p_stsc->i_sample_description_index == NULL ) )
2892 MP4_READBOX_EXIT( 0 );
2894 p_box->data.p_stsc->i_entry_count = count;
2896 for( uint32_t i = 0; i < count;i++ )
2898 MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
2899 MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
2900 MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
2903 #ifdef MP4_VERBOSE
2904 msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
2905 p_box->data.p_stsc->i_entry_count );
2907 #endif
2908 MP4_READBOX_EXIT( 1 );
2911 static void MP4_FreeBox_sdp( MP4_Box_t *p_box )
2913 free( p_box->data.p_sdp->psz_text );
2916 static int MP4_ReadBox_sdp( stream_t *p_stream, MP4_Box_t *p_box )
2918 MP4_READBOX_ENTER( MP4_Box_data_sdp_t, MP4_FreeBox_sdp );
2920 MP4_GETSTRINGZ( p_box->data.p_sdp->psz_text );
2922 MP4_READBOX_EXIT( 1 );
2925 static void MP4_FreeBox_rtp( MP4_Box_t *p_box )
2927 free( p_box->data.p_moviehintinformation_rtp->psz_text );
2930 static int MP4_ReadBox_rtp( stream_t *p_stream, MP4_Box_t *p_box )
2932 MP4_READBOX_ENTER( MP4_Box_data_moviehintinformation_rtp_t, MP4_FreeBox_rtp );
2934 MP4_GET4BYTES( p_box->data.p_moviehintinformation_rtp->i_description_format );
2936 MP4_GETSTRINGZ( p_box->data.p_moviehintinformation_rtp->psz_text );
2938 MP4_READBOX_EXIT( 1 );
2941 static int MP4_ReadBox_tims( stream_t *p_stream, MP4_Box_t *p_box )
2943 MP4_READBOX_ENTER( MP4_Box_data_tims_t, NULL );
2945 MP4_GET4BYTES( p_box->data.p_tims->i_timescale );
2947 MP4_READBOX_EXIT( 1 );
2950 static int MP4_ReadBox_tsro( stream_t *p_stream, MP4_Box_t *p_box )
2952 MP4_READBOX_ENTER( MP4_Box_data_tsro_t, NULL );
2954 MP4_GET4BYTES( p_box->data.p_tsro->i_offset );
2956 MP4_READBOX_EXIT( 1 );
2959 static int MP4_ReadBox_tssy( stream_t *p_stream, MP4_Box_t *p_box )
2961 MP4_READBOX_ENTER( MP4_Box_data_tssy_t, NULL );
2963 MP4_GET1BYTE( p_box->data.p_tssy->i_reserved_timestamp_sync );
2965 MP4_READBOX_EXIT( 1 );
2968 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
2970 free( p_box->data.p_co64->i_chunk_offset );
2973 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
2975 const bool sixtyfour = p_box->i_type != ATOM_stco;
2976 uint32_t count;
2978 MP4_READBOX_ENTER( MP4_Box_data_co64_t, MP4_FreeBox_stco_co64 );
2980 MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
2981 MP4_GET4BYTES( count );
2983 if( (sixtyfour ? UINT64_C(8) : UINT64_C(4)) * count > i_read )
2984 MP4_READBOX_EXIT( 0 );
2986 p_box->data.p_co64->i_chunk_offset = vlc_alloc( count, sizeof(uint64_t) );
2987 if( unlikely(p_box->data.p_co64->i_chunk_offset == NULL) )
2988 MP4_READBOX_EXIT( 0 );
2989 p_box->data.p_co64->i_entry_count = count;
2991 for( uint32_t i = 0; i < count; i++ )
2993 if( sixtyfour )
2994 MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2995 else
2996 MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
2999 #ifdef MP4_VERBOSE
3000 msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
3001 p_box->data.p_co64->i_entry_count );
3003 #endif
3004 MP4_READBOX_EXIT( 1 );
3007 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
3009 free( p_box->data.p_stss->i_sample_number );
3012 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
3014 uint32_t count;
3016 MP4_READBOX_ENTER( MP4_Box_data_stss_t, MP4_FreeBox_stss );
3018 MP4_GETVERSIONFLAGS( p_box->data.p_stss );
3019 MP4_GET4BYTES( count );
3021 if( UINT64_C(4) * count > i_read )
3022 MP4_READBOX_EXIT( 0 );
3024 p_box->data.p_stss->i_sample_number = vlc_alloc( count, sizeof(uint32_t) );
3025 if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
3026 MP4_READBOX_EXIT( 0 );
3027 p_box->data.p_stss->i_entry_count = count;
3029 for( uint32_t i = 0; i < count; i++ )
3031 MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
3032 /* XXX in libmp4 sample begin at 0 */
3033 p_box->data.p_stss->i_sample_number[i]--;
3036 #ifdef MP4_VERBOSE
3037 msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
3038 p_box->data.p_stss->i_entry_count );
3040 #endif
3041 MP4_READBOX_EXIT( 1 );
3044 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
3046 free( p_box->data.p_stsh->i_shadowed_sample_number );
3047 free( p_box->data.p_stsh->i_sync_sample_number );
3050 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
3052 uint32_t count;
3054 MP4_READBOX_ENTER( MP4_Box_data_stsh_t, MP4_FreeBox_stsh );
3056 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
3057 MP4_GET4BYTES( count );
3059 if( UINT64_C(8) * count > i_read )
3060 MP4_READBOX_EXIT( 0 );
3062 p_box->data.p_stsh->i_shadowed_sample_number = vlc_alloc( count,
3063 sizeof(uint32_t) );
3064 p_box->data.p_stsh->i_sync_sample_number = vlc_alloc( count,
3065 sizeof(uint32_t) );
3066 if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
3067 || p_box->data.p_stsh->i_sync_sample_number == NULL )
3068 MP4_READBOX_EXIT( 0 );
3069 p_box->data.p_stsh->i_entry_count = count;
3071 for( uint32_t i = 0; i < p_box->data.p_stss->i_entry_count; i++ )
3073 MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
3074 MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
3077 #ifdef MP4_VERBOSE
3078 msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
3079 p_box->data.p_stsh->i_entry_count );
3080 #endif
3081 MP4_READBOX_EXIT( 1 );
3084 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
3086 free( p_box->data.p_stdp->i_priority );
3089 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
3091 MP4_READBOX_ENTER( MP4_Box_data_stdp_t, MP4_FreeBox_stdp );
3093 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
3095 p_box->data.p_stdp->i_priority =
3096 calloc( i_read / 2, sizeof(uint16_t) );
3098 if( unlikely( !p_box->data.p_stdp->i_priority ) )
3099 MP4_READBOX_EXIT( 0 );
3101 for( unsigned i = 0; i < i_read / 2 ; i++ )
3103 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
3106 #ifdef MP4_VERBOSE
3107 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
3108 i_read / 2 );
3110 #endif
3111 MP4_READBOX_EXIT( 1 );
3114 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
3116 free( p_box->data.p_elst->i_segment_duration );
3117 free( p_box->data.p_elst->i_media_time );
3118 free( p_box->data.p_elst->i_media_rate_integer );
3119 free( p_box->data.p_elst->i_media_rate_fraction );
3122 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
3124 uint32_t count;
3126 MP4_READBOX_ENTER( MP4_Box_data_elst_t, MP4_FreeBox_elst );
3128 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
3129 MP4_GET4BYTES( count );
3131 if( count == 0 )
3132 MP4_READBOX_EXIT( 1 );
3134 uint32_t i_entries_max = i_read / ((p_box->data.p_elst->i_version == 1) ? 20 : 12);
3135 if( count > i_entries_max )
3136 count = i_entries_max;
3138 p_box->data.p_elst->i_segment_duration = vlc_alloc( count,
3139 sizeof(uint64_t) );
3140 p_box->data.p_elst->i_media_time = vlc_alloc( count, sizeof(int64_t) );
3141 p_box->data.p_elst->i_media_rate_integer = vlc_alloc( count,
3142 sizeof(uint16_t) );
3143 p_box->data.p_elst->i_media_rate_fraction = vlc_alloc( count,
3144 sizeof(uint16_t) );
3145 if( p_box->data.p_elst->i_segment_duration == NULL
3146 || p_box->data.p_elst->i_media_time == NULL
3147 || p_box->data.p_elst->i_media_rate_integer == NULL
3148 || p_box->data.p_elst->i_media_rate_fraction == NULL )
3150 MP4_READBOX_EXIT( 0 );
3152 p_box->data.p_elst->i_entry_count = count;
3154 for( uint32_t i = 0; i < count; i++ )
3156 uint64_t segment_duration;
3157 int64_t media_time;
3159 if( p_box->data.p_elst->i_version == 1 )
3161 union { int64_t s; uint64_t u; } u;
3163 MP4_GET8BYTES( segment_duration );
3164 MP4_GET8BYTES( u.u );
3165 media_time = u.s;
3167 else
3169 union { int32_t s; uint32_t u; } u;
3171 MP4_GET4BYTES( segment_duration );
3172 MP4_GET4BYTES( u.u );
3173 media_time = u.s;
3176 p_box->data.p_elst->i_segment_duration[i] = segment_duration;
3177 p_box->data.p_elst->i_media_time[i] = media_time;
3178 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
3179 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
3182 #ifdef MP4_VERBOSE
3183 msg_Dbg( p_stream, "read box: \"elst\" entry-count %" PRIu32,
3184 p_box->data.p_elst->i_entry_count );
3185 #endif
3186 MP4_READBOX_EXIT( 1 );
3189 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
3191 free( p_box->data.p_cprt->psz_notice );
3194 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
3196 uint16_t i_language;
3197 bool b_mac;
3199 MP4_READBOX_ENTER( MP4_Box_data_cprt_t, MP4_FreeBox_cprt );
3201 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
3203 MP4_GET2BYTES( i_language );
3204 decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
3206 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
3208 #ifdef MP4_VERBOSE
3209 msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
3210 p_box->data.p_cprt->rgs_language,
3211 p_box->data.p_cprt->psz_notice );
3213 #endif
3214 MP4_READBOX_EXIT( 1 );
3217 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
3219 MP4_READBOX_ENTER( MP4_Box_data_dcom_t, NULL );
3221 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
3222 #ifdef MP4_VERBOSE
3223 msg_Dbg( p_stream,
3224 "read box: \"dcom\" compression algorithm : %4.4s",
3225 (char*)&p_box->data.p_dcom->i_algorithm );
3226 #endif
3227 MP4_READBOX_EXIT( 1 );
3230 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
3232 free( p_box->data.p_cmvd->p_data );
3235 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
3237 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t, MP4_FreeBox_cmvd );
3239 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
3241 p_box->data.p_cmvd->i_compressed_size = i_read;
3243 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
3244 MP4_READBOX_EXIT( 0 );
3246 /* now copy compressed data */
3247 memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
3249 p_box->data.p_cmvd->b_compressed = 1;
3251 #ifdef MP4_VERBOSE
3252 msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
3253 p_box->data.p_cmvd->i_compressed_size );
3254 #endif
3256 MP4_READBOX_EXIT( 1 );
3259 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
3261 MP4_Box_t *p_dcom;
3262 MP4_Box_t *p_cmvd;
3264 #ifdef HAVE_ZLIB_H
3265 stream_t *p_stream_memory;
3266 z_stream z_data;
3267 uint8_t *p_data;
3268 int i_result;
3269 #endif
3271 if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
3272 return 0;
3274 if( !p_box->p_father ||
3275 ( p_box->p_father->i_type != ATOM_moov &&
3276 p_box->p_father->i_type != ATOM_foov ) )
3278 msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
3279 return 1;
3282 if( !MP4_ReadBoxContainer( p_stream, p_box ) )
3284 return 0;
3287 if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
3288 ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
3289 p_cmvd->data.p_cmvd->p_data == NULL )
3291 msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
3292 return 0;
3295 if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
3297 msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
3298 "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
3299 return 0;
3302 #ifndef HAVE_ZLIB_H
3303 msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
3304 return 0;
3306 #else
3307 /* decompress data */
3308 /* allocate a new buffer */
3309 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
3310 return 0;
3311 /* init default structures */
3312 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
3313 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
3314 z_data.next_out = p_data;
3315 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
3316 z_data.zalloc = (alloc_func)Z_NULL;
3317 z_data.zfree = (free_func)Z_NULL;
3318 z_data.opaque = (voidpf)Z_NULL;
3320 /* init zlib */
3321 if( inflateInit( &z_data ) != Z_OK )
3323 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3324 free( p_data );
3325 return 0;
3328 /* uncompress */
3329 i_result = inflate( &z_data, Z_NO_FLUSH );
3330 if( i_result != Z_OK && i_result != Z_STREAM_END )
3332 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3333 free( p_data );
3334 return 0;
3337 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
3339 msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
3340 "mismatch" );
3342 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
3344 /* close zlib */
3345 if( inflateEnd( &z_data ) != Z_OK )
3347 msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
3348 "data (ignored)" );
3351 free( p_cmvd->data.p_cmvd->p_data );
3352 p_cmvd->data.p_cmvd->p_data = p_data;
3353 p_cmvd->data.p_cmvd->b_compressed = 0;
3355 msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
3357 /* now create a memory stream */
3358 p_stream_memory =
3359 vlc_stream_MemoryNew( VLC_OBJECT(p_stream),
3360 p_cmvd->data.p_cmvd->p_data,
3361 p_cmvd->data.p_cmvd->i_uncompressed_size, true );
3363 /* and read uncompressd moov */
3364 p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
3366 vlc_stream_Delete( p_stream_memory );
3368 #ifdef MP4_VERBOSE
3369 msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
3370 #endif
3372 return p_box->data.p_cmov->p_moov ? 1 : 0;
3373 #endif /* HAVE_ZLIB_H */
3376 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
3378 free( p_box->data.p_rdrf->psz_ref );
3381 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
3383 uint32_t i_len;
3384 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t, MP4_FreeBox_rdrf );
3386 MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
3387 MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
3388 MP4_GET4BYTES( i_len );
3389 i_len++;
3391 if( i_len > 0 )
3393 p_box->data.p_rdrf->psz_ref = malloc( i_len );
3394 if( p_box->data.p_rdrf->psz_ref == NULL )
3395 MP4_READBOX_EXIT( 0 );
3396 i_len--;
3398 for( unsigned i = 0; i < i_len; i++ )
3400 MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
3402 p_box->data.p_rdrf->psz_ref[i_len] = '\0';
3404 else
3406 p_box->data.p_rdrf->psz_ref = NULL;
3409 #ifdef MP4_VERBOSE
3410 msg_Dbg( p_stream,
3411 "read box: \"rdrf\" type:%4.4s ref %s",
3412 (char*)&p_box->data.p_rdrf->i_ref_type,
3413 p_box->data.p_rdrf->psz_ref );
3414 #endif
3415 MP4_READBOX_EXIT( 1 );
3420 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
3422 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t, NULL );
3424 MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
3426 MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
3428 #ifdef MP4_VERBOSE
3429 msg_Dbg( p_stream,
3430 "read box: \"rmdr\" rate:%d",
3431 p_box->data.p_rmdr->i_rate );
3432 #endif
3433 MP4_READBOX_EXIT( 1 );
3436 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
3438 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t, NULL );
3440 MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
3442 #ifdef MP4_VERBOSE
3443 msg_Dbg( p_stream,
3444 "read box: \"rmqu\" quality:%d",
3445 p_box->data.p_rmqu->i_quality );
3446 #endif
3447 MP4_READBOX_EXIT( 1 );
3450 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
3452 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t, NULL );
3453 MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
3455 MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
3456 MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
3457 MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
3458 MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
3460 #ifdef MP4_VERBOSE
3461 msg_Dbg( p_stream,
3462 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
3463 (char*)&p_box->data.p_rmvc->i_gestaltType,
3464 p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
3465 p_box->data.p_rmvc->i_checkType );
3466 #endif
3468 MP4_READBOX_EXIT( 1 );
3471 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
3473 MP4_READBOX_ENTER( MP4_Box_data_frma_t, NULL );
3475 MP4_GETFOURCC( p_box->data.p_frma->i_type );
3477 #ifdef MP4_VERBOSE
3478 msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
3479 (char *)&p_box->data.p_frma->i_type );
3480 #endif
3482 MP4_READBOX_EXIT( 1 );
3485 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
3487 MP4_READBOX_ENTER( MP4_Box_data_skcr_t, NULL );
3489 MP4_GET4BYTES( p_box->data.p_skcr->i_init );
3490 MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
3491 MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
3493 #ifdef MP4_VERBOSE
3494 msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
3495 p_box->data.p_skcr->i_init,
3496 p_box->data.p_skcr->i_encr,
3497 p_box->data.p_skcr->i_decr );
3498 #endif
3500 MP4_READBOX_EXIT( 1 );
3503 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
3505 VLC_UNUSED(p_box);
3506 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
3507 * so unless data decrypt itself by magic, there will be no playback,
3508 * but we never know... */
3509 msg_Warn( p_stream, "DRM protected streams are not supported." );
3510 return 1;
3513 static void MP4_FreeBox_Binary( MP4_Box_t *p_box )
3515 free( p_box->data.p_binary->p_blob );
3518 static int MP4_ReadBox_Binary( stream_t *p_stream, MP4_Box_t *p_box )
3520 MP4_READBOX_ENTER( MP4_Box_data_binary_t, MP4_FreeBox_Binary );
3521 i_read = __MIN( i_read, UINT32_MAX );
3522 if ( i_read > 0 )
3524 p_box->data.p_binary->p_blob = malloc( i_read );
3525 if ( p_box->data.p_binary->p_blob )
3527 memcpy( p_box->data.p_binary->p_blob, p_peek, i_read );
3528 p_box->data.p_binary->i_blob = i_read;
3531 MP4_READBOX_EXIT( 1 );
3534 static void MP4_FreeBox_data( MP4_Box_t *p_box )
3536 free( p_box->data.p_data->p_blob );
3539 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
3541 MP4_READBOX_ENTER( MP4_Box_data_data_t, MP4_FreeBox_data );
3542 MP4_Box_data_data_t *p_data = p_box->data.p_data;
3544 if ( i_read < 8 || i_read - 8 > UINT32_MAX )
3545 MP4_READBOX_EXIT( 0 );
3547 uint8_t i_type;
3548 MP4_GET1BYTE( i_type );
3549 if ( i_type != 0 )
3551 #ifdef MP4_VERBOSE
3552 msg_Dbg( p_stream, "skipping unknown 'data' atom with type %"PRIu8, i_type );
3553 #endif
3554 MP4_READBOX_EXIT( 0 );
3557 MP4_GET3BYTES( p_data->e_wellknowntype );
3558 MP4_GET2BYTES( p_data->locale.i_country );
3559 MP4_GET2BYTES( p_data->locale.i_language );
3560 #ifdef MP4_VERBOSE
3561 msg_Dbg( p_stream, "read 'data' atom: knowntype=%"PRIu32", country=%"PRIu16" lang=%"PRIu16
3562 ", size %"PRIu64" bytes", p_data->e_wellknowntype,
3563 p_data->locale.i_country, p_data->locale.i_language, i_read );
3564 #endif
3565 p_box->data.p_data->p_blob = malloc( i_read );
3566 if ( !p_box->data.p_data->p_blob )
3567 MP4_READBOX_EXIT( 0 );
3569 p_box->data.p_data->i_blob = i_read;
3570 memcpy( p_box->data.p_data->p_blob, p_peek, i_read);
3572 MP4_READBOX_EXIT( 1 );
3575 static int MP4_ReadBox_Metadata( stream_t *p_stream, MP4_Box_t *p_box )
3577 const uint8_t *p_peek;
3578 if ( vlc_stream_Peek( p_stream, &p_peek, 16 ) < 16 )
3579 return 0;
3580 if ( vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
3581 return 0;
3582 const uint32_t stoplist[] = { ATOM_data, 0 };
3583 return MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist );
3586 /* Chapter support */
3587 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
3589 MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
3590 for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
3591 free( p_chpl->chapter[i].psz_name );
3594 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
3596 MP4_Box_data_chpl_t *p_chpl;
3597 uint32_t i_dummy;
3598 VLC_UNUSED(i_dummy);
3599 int i;
3600 MP4_READBOX_ENTER( MP4_Box_data_chpl_t, MP4_FreeBox_chpl );
3602 p_chpl = p_box->data.p_chpl;
3604 MP4_GETVERSIONFLAGS( p_chpl );
3606 if ( i_read < 5 || p_chpl->i_version != 0x1 )
3607 MP4_READBOX_EXIT( 0 );
3609 MP4_GET4BYTES( i_dummy );
3611 MP4_GET1BYTE( p_chpl->i_chapter );
3613 for( i = 0; i < p_chpl->i_chapter; i++ )
3615 uint64_t i_start;
3616 uint8_t i_len;
3617 int i_copy;
3618 if ( i_read < 9 )
3619 break;
3620 MP4_GET8BYTES( i_start );
3621 MP4_GET1BYTE( i_len );
3623 p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
3624 if( !p_chpl->chapter[i].psz_name )
3625 MP4_READBOX_EXIT( 0 );
3627 i_copy = __MIN( i_len, i_read );
3628 if( i_copy > 0 )
3629 memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
3630 p_chpl->chapter[i].psz_name[i_copy] = '\0';
3631 p_chpl->chapter[i].i_start = i_start;
3633 p_peek += i_copy;
3634 i_read -= i_copy;
3637 if ( i != p_chpl->i_chapter )
3638 p_chpl->i_chapter = i;
3640 /* Bubble sort by increasing start date */
3643 for( i = 0; i < p_chpl->i_chapter - 1; i++ )
3645 if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
3647 char *psz = p_chpl->chapter[i+1].psz_name;
3648 int64_t i64 = p_chpl->chapter[i+1].i_start;
3650 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
3651 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
3653 p_chpl->chapter[i].psz_name = psz;
3654 p_chpl->chapter[i].i_start = i64;
3656 i = -1;
3657 break;
3660 } while( i == -1 );
3662 #ifdef MP4_VERBOSE
3663 msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
3664 p_chpl->i_chapter );
3665 #endif
3666 MP4_READBOX_EXIT( 1 );
3669 /* GoPro HiLight tags support */
3670 static void MP4_FreeBox_HMMT( MP4_Box_t *p_box )
3672 free( p_box->data.p_hmmt->pi_chapter_start );
3675 static int MP4_ReadBox_HMMT( stream_t *p_stream, MP4_Box_t *p_box )
3677 #define MAX_CHAPTER_COUNT 100
3679 MP4_Box_data_HMMT_t *p_hmmt;
3680 MP4_READBOX_ENTER( MP4_Box_data_HMMT_t, MP4_FreeBox_HMMT );
3682 if( i_read < 4 )
3683 MP4_READBOX_EXIT( 0 );
3685 p_hmmt = p_box->data.p_hmmt;
3687 MP4_GET4BYTES( p_hmmt->i_chapter_count );
3689 if( p_hmmt->i_chapter_count <= 0 )
3691 p_hmmt->pi_chapter_start = NULL;
3692 MP4_READBOX_EXIT( 1 );
3695 if( ( i_read / sizeof(uint32_t) ) < p_hmmt->i_chapter_count )
3696 MP4_READBOX_EXIT( 0 );
3698 /* Cameras are allowing a maximum of 100 tags */
3699 if( p_hmmt->i_chapter_count > MAX_CHAPTER_COUNT )
3700 p_hmmt->i_chapter_count = MAX_CHAPTER_COUNT;
3702 p_hmmt->pi_chapter_start = vlc_alloc( p_hmmt->i_chapter_count, sizeof(uint32_t) );
3703 if( p_hmmt->pi_chapter_start == NULL )
3704 MP4_READBOX_EXIT( 0 );
3706 for( uint32_t i = 0; i < p_hmmt->i_chapter_count; i++ )
3708 MP4_GET4BYTES( p_hmmt->pi_chapter_start[i] );
3711 #ifdef MP4_VERBOSE
3712 msg_Dbg( p_stream, "read box: \"HMMT\" %d HiLight tags", p_hmmt->i_chapter_count );
3713 #endif
3715 MP4_READBOX_EXIT( 1 );
3718 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
3720 free( p_box->data.p_tref_generic->i_track_ID );
3723 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
3725 uint32_t count;
3727 MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t, MP4_FreeBox_tref_generic );
3729 p_box->data.p_tref_generic->i_track_ID = NULL;
3730 count = i_read / sizeof(uint32_t);
3731 p_box->data.p_tref_generic->i_entry_count = count;
3732 p_box->data.p_tref_generic->i_track_ID = vlc_alloc( count,
3733 sizeof(uint32_t) );
3734 if( p_box->data.p_tref_generic->i_track_ID == NULL )
3735 MP4_READBOX_EXIT( 0 );
3737 for( unsigned i = 0; i < count; i++ )
3739 MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
3741 #ifdef MP4_VERBOSE
3742 msg_Dbg( p_stream, "read box: \"chap\" %d references",
3743 p_box->data.p_tref_generic->i_entry_count );
3744 #endif
3746 MP4_READBOX_EXIT( 1 );
3749 static void MP4_FreeBox_keys( MP4_Box_t *p_box )
3751 for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
3752 free( p_box->data.p_keys->p_entries[i].psz_value );
3753 free( p_box->data.p_keys->p_entries );
3756 static int MP4_ReadBox_keys( stream_t *p_stream, MP4_Box_t *p_box )
3758 MP4_READBOX_ENTER( MP4_Box_data_keys_t, MP4_FreeBox_keys );
3760 if ( i_read < 8 )
3761 MP4_READBOX_EXIT( 0 );
3763 uint32_t i_count;
3764 MP4_GET4BYTES( i_count ); /* reserved + flags */
3765 if ( i_count != 0 )
3766 MP4_READBOX_EXIT( 0 );
3768 MP4_GET4BYTES( i_count );
3769 p_box->data.p_keys->p_entries = calloc( i_count, sizeof(*p_box->data.p_keys->p_entries) );
3770 if ( !p_box->data.p_keys->p_entries )
3771 MP4_READBOX_EXIT( 0 );
3772 p_box->data.p_keys->i_entry_count = i_count;
3774 uint32_t i=0;
3775 for( ; i < i_count; i++ )
3777 if ( i_read < 8 )
3778 break;
3779 uint32_t i_keysize;
3780 MP4_GET4BYTES( i_keysize );
3781 if ( (i_keysize < 8) || (i_keysize - 4 > i_read) )
3782 break;
3783 MP4_GETFOURCC( p_box->data.p_keys->p_entries[i].i_namespace );
3784 i_keysize -= 8;
3785 p_box->data.p_keys->p_entries[i].psz_value = malloc( i_keysize + 1 );
3786 if ( !p_box->data.p_keys->p_entries[i].psz_value )
3787 break;
3788 memcpy( p_box->data.p_keys->p_entries[i].psz_value, p_peek, i_keysize );
3789 p_box->data.p_keys->p_entries[i].psz_value[i_keysize] = 0;
3790 p_peek += i_keysize;
3791 i_read -= i_keysize;
3792 #ifdef MP4_ULTRA_VERBOSE
3793 msg_Dbg( p_stream, "read box: \"keys\": %u '%s'", i + 1,
3794 p_box->data.p_keys->p_entries[i].psz_value );
3795 #endif
3797 if ( i < i_count )
3798 p_box->data.p_keys->i_entry_count = i;
3800 MP4_READBOX_EXIT( 1 );
3803 static int MP4_ReadBox_colr( stream_t *p_stream, MP4_Box_t *p_box )
3805 MP4_READBOX_ENTER( MP4_Box_data_colr_t, NULL );
3806 MP4_GETFOURCC( p_box->data.p_colr->i_type );
3807 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'c' ) ||
3808 p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3810 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_primary_idx );
3811 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_transfer_function_idx );
3812 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_matrix_idx );
3813 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3814 MP4_GET1BYTE( p_box->data.p_colr->nclc.i_full_range );
3816 else
3818 #ifdef MP4_VERBOSE
3819 msg_Warn( p_stream, "Unhandled colr type: %4.4s", (char*)&p_box->data.p_colr->i_type );
3820 #endif
3822 MP4_READBOX_EXIT( 1 );
3825 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
3827 const uint8_t *p_peek;
3828 const size_t i_headersize = mp4_box_headersize( p_box );
3830 if( p_box->i_size < 16 || p_box->i_size - i_headersize < 8 )
3831 return 0;
3833 /* skip over box header */
3834 if( vlc_stream_Read( p_stream, NULL, i_headersize ) < (ssize_t) i_headersize )
3835 return 0;
3837 /* meta content starts with a 4 byte version/flags value (should be 0) */
3838 if( vlc_stream_Peek( p_stream, &p_peek, 8 ) < 8 )
3839 return 0;
3841 if( !memcmp( p_peek, "\0\0\0", 4 ) ) /* correct header case */
3843 if( vlc_stream_Read( p_stream, NULL, 4 ) < 4 )
3844 return 0;
3846 else if( memcmp( &p_peek[4], "hdlr", 4 ) ) /* Broken, headerless ones */
3848 return 0;
3851 /* load child atoms up to the handler (which should be next anyway) */
3852 const uint32_t stoplist[] = { ATOM_hdlr, 0 };
3853 if ( !MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist ) )
3854 return 0;
3856 /* Mandatory */
3857 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box, "hdlr" );
3858 if ( p_hdlr && BOXDATA(p_hdlr) && BOXDATA(p_hdlr)->i_version == 0 )
3860 p_box->i_handler = BOXDATA(p_hdlr)->i_handler_type;
3861 switch( p_box->i_handler )
3863 case HANDLER_mdta:
3864 case HANDLER_mdir:
3865 /* then it behaves like a container */
3866 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
3867 default:
3868 /* skip parsing, will be seen as empty container */
3869 break;
3873 return 1;
3876 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3878 char i_unused;
3879 VLC_UNUSED(i_unused);
3881 MP4_READBOX_ENTER( MP4_Box_data_iods_t, NULL );
3882 MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3884 MP4_GET1BYTE( i_unused ); /* tag */
3885 MP4_GET1BYTE( i_unused ); /* length */
3887 MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3888 are used for other flags */
3889 MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3890 MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3891 MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3892 MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3893 MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3895 #ifdef MP4_VERBOSE
3896 msg_Dbg( p_stream,
3897 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3898 "visual: %i, graphics: %i",
3899 p_box->data.p_iods->i_object_descriptor >> 6,
3900 p_box->data.p_iods->i_OD_profile_level,
3901 p_box->data.p_iods->i_scene_profile_level,
3902 p_box->data.p_iods->i_audio_profile_level,
3903 p_box->data.p_iods->i_visual_profile_level,
3904 p_box->data.p_iods->i_graphics_profile_level );
3905 #endif
3907 MP4_READBOX_EXIT( 1 );
3910 static int MP4_ReadBox_btrt( stream_t *p_stream, MP4_Box_t *p_box )
3912 MP4_READBOX_ENTER( MP4_Box_data_btrt_t, NULL );
3914 if(i_read != 12)
3915 MP4_READBOX_EXIT( 0 );
3917 MP4_GET4BYTES( p_box->data.p_btrt->i_buffer_size );
3918 MP4_GET4BYTES( p_box->data.p_btrt->i_max_bitrate );
3919 MP4_GET4BYTES( p_box->data.p_btrt->i_avg_bitrate );
3921 MP4_READBOX_EXIT( 1 );
3924 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3926 MP4_READBOX_ENTER( MP4_Box_data_pasp_t, NULL );
3928 MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
3929 MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
3931 #ifdef MP4_VERBOSE
3932 msg_Dbg( p_stream,
3933 "read box: \"paps\" %dx%d",
3934 p_box->data.p_pasp->i_horizontal_spacing,
3935 p_box->data.p_pasp->i_vertical_spacing);
3936 #endif
3938 MP4_READBOX_EXIT( 1 );
3941 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
3943 MP4_READBOX_ENTER( MP4_Box_data_mehd_t, NULL );
3945 MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
3946 if( p_box->data.p_mehd->i_version == 1 )
3947 MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
3948 else /* version == 0 */
3949 MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
3951 #ifdef MP4_VERBOSE
3952 msg_Dbg( p_stream,
3953 "read box: \"mehd\" frag dur. %"PRIu64"",
3954 p_box->data.p_mehd->i_fragment_duration );
3955 #endif
3957 MP4_READBOX_EXIT( 1 );
3960 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
3962 MP4_READBOX_ENTER( MP4_Box_data_trex_t, NULL );
3963 MP4_GETVERSIONFLAGS( p_box->data.p_trex );
3965 MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
3966 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
3967 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
3968 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
3969 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
3971 #ifdef MP4_VERBOSE
3972 msg_Dbg( p_stream,
3973 "read box: \"trex\" trackID: %"PRIu32"",
3974 p_box->data.p_trex->i_track_ID );
3975 #endif
3977 MP4_READBOX_EXIT( 1 );
3980 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
3982 free( p_box->data.p_sdtp->p_sample_table );
3985 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
3987 uint32_t i_sample_count;
3988 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t, MP4_FreeBox_sdtp );
3989 MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
3990 MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
3991 i_sample_count = i_read;
3993 p_sdtp->p_sample_table = malloc( i_sample_count );
3994 if( unlikely(p_sdtp->p_sample_table == NULL) )
3995 MP4_READBOX_EXIT( 0 );
3997 for( uint32_t i = 0; i < i_sample_count; i++ )
3998 MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
4000 #ifdef MP4_VERBOSE
4001 msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
4002 if ( i_sample_count > 3 )
4003 msg_Dbg( p_stream,
4004 "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
4005 p_sdtp->p_sample_table[0],
4006 p_sdtp->p_sample_table[1],
4007 p_sdtp->p_sample_table[2],
4008 p_sdtp->p_sample_table[3] );
4009 #endif
4011 MP4_READBOX_EXIT( 1 );
4014 static int MP4_ReadBox_tsel( stream_t *p_stream, MP4_Box_t *p_box )
4016 MP4_READBOX_ENTER( MP4_Box_data_tsel_t, NULL );
4017 uint32_t i_version;
4018 MP4_GET4BYTES( i_version );
4019 if ( i_version != 0 || i_read < 4 )
4020 MP4_READBOX_EXIT( 0 );
4021 MP4_GET4BYTES( p_box->data.p_tsel->i_switch_group );
4022 /* ignore list of attributes as es are present before switch */
4023 MP4_READBOX_EXIT( 1 );
4026 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
4028 MP4_READBOX_ENTER( MP4_Box_data_mfro_t, NULL );
4030 MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
4031 MP4_GET4BYTES( p_box->data.p_mfro->i_size );
4033 #ifdef MP4_VERBOSE
4034 msg_Dbg( p_stream,
4035 "read box: \"mfro\" size: %"PRIu32"",
4036 p_box->data.p_mfro->i_size);
4037 #endif
4039 MP4_READBOX_EXIT( 1 );
4042 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
4044 free( p_box->data.p_tfra->p_time );
4045 free( p_box->data.p_tfra->p_moof_offset );
4046 free( p_box->data.p_tfra->p_traf_number );
4047 free( p_box->data.p_tfra->p_trun_number );
4048 free( p_box->data.p_tfra->p_sample_number );
4051 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
4053 #define READ_VARIABLE_LENGTH(lengthvar, p_array) switch (lengthvar)\
4055 case 0:\
4056 MP4_GET1BYTE( p_array[i] );\
4057 break;\
4058 case 1:\
4059 MP4_GET2BYTES( *((uint16_t *)&p_array[i*2]) );\
4060 break;\
4061 case 2:\
4062 MP4_GET3BYTES( *((uint32_t *)&p_array[i*4]) );\
4063 break;\
4064 case 3:\
4065 MP4_GET4BYTES( *((uint32_t *)&p_array[i*4]) );\
4066 break;\
4067 default:\
4068 goto error;\
4070 #define FIX_VARIABLE_LENGTH(lengthvar) if ( lengthvar == 3 ) lengthvar = 4
4072 uint32_t i_number_of_entries;
4073 MP4_READBOX_ENTER( MP4_Box_data_tfra_t, MP4_FreeBox_tfra );
4074 MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
4075 MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
4076 if ( p_tfra->i_version > 1 )
4077 MP4_READBOX_EXIT( 0 );
4078 MP4_GET4BYTES( p_tfra->i_track_ID );
4079 uint32_t i_lengths = 0;
4080 MP4_GET4BYTES( i_lengths );
4081 MP4_GET4BYTES( p_tfra->i_number_of_entries );
4082 i_number_of_entries = p_tfra->i_number_of_entries;
4083 p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
4084 p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
4085 p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
4087 size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
4088 p_tfra->p_time = calloc( i_number_of_entries, size );
4089 p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
4091 size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
4092 if ( size == 3 ) size++;
4093 p_tfra->p_traf_number = calloc( i_number_of_entries, size );
4094 size = 1 + p_tfra->i_length_size_of_trun_num;
4095 if ( size == 3 ) size++;
4096 p_tfra->p_trun_number = calloc( i_number_of_entries, size );
4097 size = 1 + p_tfra->i_length_size_of_sample_num;
4098 if ( size == 3 ) size++;
4099 p_tfra->p_sample_number = calloc( i_number_of_entries, size );
4101 if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
4102 || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
4103 goto error;
4105 unsigned i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
4106 + p_tfra->i_length_size_of_trun_num
4107 + p_tfra->i_length_size_of_sample_num;
4109 uint32_t i;
4110 for( i = 0; i < i_number_of_entries; i++ )
4113 if( p_tfra->i_version == 1 )
4115 if ( i_read < i_fields_length + 16 )
4116 break;
4117 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_time[i*2]) );
4118 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_moof_offset[i*2]) );
4120 else
4122 if ( i_read < i_fields_length + 8 )
4123 break;
4124 MP4_GET4BYTES( p_tfra->p_time[i] );
4125 MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
4128 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num, p_tfra->p_traf_number);
4129 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num, p_tfra->p_trun_number);
4130 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num, p_tfra->p_sample_number);
4132 if ( i < i_number_of_entries )
4133 i_number_of_entries = i;
4135 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num);
4136 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num);
4137 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num);
4139 #ifdef MP4_ULTRA_VERBOSE
4140 for( i = 0; i < i_number_of_entries; i++ )
4142 if( p_tfra->i_version == 0 )
4144 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu32", "
4145 "moof_offset[%"PRIu32"]: %"PRIu32"",
4146 p_tfra->i_track_ID,
4147 i, p_tfra->p_time[i],
4148 i, p_tfra->p_moof_offset[i] );
4150 else
4152 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu64", "
4153 "moof_offset[%"PRIu32"]: %"PRIu64"",
4154 p_tfra->i_track_ID,
4155 i, ((uint64_t *)(p_tfra->p_time))[i],
4156 i, ((uint64_t *)(p_tfra->p_moof_offset))[i] );
4159 #endif
4160 #ifdef MP4_VERBOSE
4161 msg_Dbg( p_stream, "tfra[%"PRIu32"] %"PRIu32" entries",
4162 p_tfra->i_track_ID, i_number_of_entries );
4163 #endif
4165 MP4_READBOX_EXIT( 1 );
4166 error:
4167 MP4_READBOX_EXIT( 0 );
4169 #undef READ_VARIABLE_LENGTH
4170 #undef FIX_VARIABLE_LENGTH
4173 static int MP4_ReadBox_pnot( stream_t *p_stream, MP4_Box_t *p_box )
4175 if ( p_box->i_size != 20 )
4176 return 0;
4177 MP4_READBOX_ENTER( MP4_Box_data_pnot_t, NULL );
4178 MP4_GET4BYTES( p_box->data.p_pnot->i_date );
4179 uint16_t i_version;
4180 MP4_GET2BYTES( i_version );
4181 if ( i_version != 0 )
4182 MP4_READBOX_EXIT( 0 );
4183 MP4_GETFOURCC( p_box->data.p_pnot->i_type );
4184 MP4_GET2BYTES( p_box->data.p_pnot->i_index );
4185 MP4_READBOX_EXIT( 1 );
4188 static int MP4_ReadBox_SA3D( stream_t *p_stream, MP4_Box_t *p_box )
4190 MP4_READBOX_ENTER( MP4_Box_data_SA3D_t, NULL );
4192 uint8_t i_version;
4193 MP4_GET1BYTE( i_version );
4194 if ( i_version != 0 )
4195 MP4_READBOX_EXIT( 0 );
4197 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_type );
4198 MP4_GET4BYTES( p_box->data.p_SA3D->i_ambisonic_order );
4199 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_channel_ordering );
4200 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_normalization );
4201 MP4_GET4BYTES( p_box->data.p_SA3D->i_num_channels );
4202 MP4_READBOX_EXIT( 1 );
4205 /* For generic */
4206 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
4208 if( !p_box->p_father )
4210 goto unknown;
4212 if( p_box->p_father->i_type == ATOM_stsd )
4214 MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
4215 MP4_Box_t *p_hdlr;
4217 if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
4218 (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
4220 goto unknown;
4222 switch( p_hdlr->data.p_hdlr->i_handler_type )
4224 case ATOM_soun:
4225 return MP4_ReadBox_sample_soun( p_stream, p_box );
4226 case ATOM_vide:
4227 return MP4_ReadBox_sample_vide( p_stream, p_box );
4228 case ATOM_hint:
4229 return MP4_ReadBox_sample_hint8( p_stream, p_box );
4230 case ATOM_text:
4231 case ATOM_tx3g:
4232 case ATOM_sbtl:
4233 return MP4_ReadBox_sample_text( p_stream, p_box );
4234 default:
4235 msg_Warn( p_stream,
4236 "unknown handler type in stsd (incompletely loaded)" );
4237 return 1;
4241 unknown:
4242 if MP4_BOX_TYPE_ASCII()
4243 msg_Warn( p_stream,
4244 "unknown box type %4.4s (incompletely loaded)",
4245 (char*)&p_box->i_type );
4246 else
4247 msg_Warn( p_stream,
4248 "unknown box type c%3.3s (incompletely loaded)",
4249 (char*)&p_box->i_type+1 );
4250 p_box->e_flags |= BOX_FLAG_INCOMPLETE;
4252 return 1;
4255 /**** ------------------------------------------------------------------- ****/
4257 static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
4259 if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
4260 return MP4_ReadBox_tfrf( p_stream, p_box );
4261 if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
4262 return MP4_ReadBox_tfxd( p_stream, p_box );
4263 if( !CmpUUID( &p_box->i_uuid, &XML360BoxUUID ) )
4264 return MP4_ReadBox_XML360( p_stream, p_box );
4265 if( !CmpUUID( &p_box->i_uuid, &PS3DDSBoxUUID ) && p_box->i_size == 28 )
4266 return MP4_ReadBox_Binary( p_stream, p_box );
4268 #ifdef MP4_VERBOSE
4269 msg_Warn( p_stream, "Unknown uuid type box: "
4270 "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-"
4271 "%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
4272 p_box->i_uuid.b[0], p_box->i_uuid.b[1], p_box->i_uuid.b[2], p_box->i_uuid.b[3],
4273 p_box->i_uuid.b[4], p_box->i_uuid.b[5], p_box->i_uuid.b[6], p_box->i_uuid.b[7],
4274 p_box->i_uuid.b[8], p_box->i_uuid.b[9], p_box->i_uuid.b[10], p_box->i_uuid.b[11],
4275 p_box->i_uuid.b[12], p_box->i_uuid.b[13], p_box->i_uuid.b[14], p_box->i_uuid.b[15] );
4276 #else
4277 msg_Warn( p_stream, "Unknown uuid type box" );
4278 #endif
4279 return 1;
4282 /**** ------------------------------------------------------------------- ****/
4283 /**** "Higher level" Functions ****/
4284 /**** ------------------------------------------------------------------- ****/
4286 static const struct
4288 uint32_t i_type;
4289 int (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
4290 uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
4291 } MP4_Box_Function [] =
4293 /* Containers */
4294 { ATOM_moov, MP4_ReadBoxContainer, 0 },
4295 { ATOM_foov, MP4_ReadBoxContainer, 0 },
4296 { ATOM_trak, MP4_ReadBoxContainer, ATOM_moov },
4297 { ATOM_trak, MP4_ReadBoxContainer, ATOM_foov },
4298 { ATOM_mdia, MP4_ReadBoxContainer, ATOM_trak },
4299 { ATOM_moof, MP4_ReadBoxContainer, 0 },
4300 { ATOM_minf, MP4_ReadBoxContainer, ATOM_mdia },
4301 { ATOM_stbl, MP4_ReadBoxContainer, ATOM_minf },
4302 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_minf },
4303 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_meta },
4304 { ATOM_edts, MP4_ReadBoxContainer, ATOM_trak },
4305 { ATOM_udta, MP4_ReadBoxContainer, 0 },
4306 { ATOM_nmhd, MP4_ReadBoxContainer, ATOM_minf },
4307 { ATOM_hnti, MP4_ReadBoxContainer, ATOM_udta },
4308 { ATOM_rmra, MP4_ReadBoxContainer, ATOM_moov },
4309 { ATOM_rmda, MP4_ReadBoxContainer, ATOM_rmra },
4310 { ATOM_tref, MP4_ReadBoxContainer, ATOM_trak },
4311 { ATOM_gmhd, MP4_ReadBoxContainer, ATOM_minf },
4312 { ATOM_wave, MP4_ReadBoxContainer, ATOM_stsd },
4313 { ATOM_wave, MP4_ReadBoxContainer, ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
4314 { ATOM_wave, MP4_ReadBoxContainer, ATOM_WMA2 }, /* flip4mac */
4315 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in24 },
4316 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in32 },
4317 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl32 },
4318 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl64 },
4319 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDMC },
4320 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDM2 },
4321 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiFL }, /* XiphQT */
4322 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiVs }, /* XiphQT */
4323 { ATOM_ilst, MP4_ReadBox_ilst, ATOM_meta },
4324 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_moov },
4325 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_ftyp },
4327 /* specific box */
4328 { ATOM_ftyp, MP4_ReadBox_ftyp, 0 },
4329 { ATOM_styp, MP4_ReadBox_ftyp, 0 },
4330 { ATOM_cmov, MP4_ReadBox_cmov, 0 },
4331 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_moov },
4332 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_foov },
4333 { ATOM_tkhd, MP4_ReadBox_tkhd, ATOM_trak },
4334 { ATOM_load, MP4_ReadBox_load, ATOM_trak },
4335 { ATOM_mdhd, MP4_ReadBox_mdhd, ATOM_mdia },
4336 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_mdia },
4337 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_meta },
4338 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_minf },
4339 { ATOM_vmhd, MP4_ReadBox_vmhd, ATOM_minf },
4340 { ATOM_smhd, MP4_ReadBox_smhd, ATOM_minf },
4341 { ATOM_hmhd, MP4_ReadBox_hmhd, ATOM_minf },
4342 { ATOM_alis, MP4_ReadBoxSkip, ATOM_dref },
4343 { ATOM_url, MP4_ReadBox_url, 0 },
4344 { ATOM_urn, MP4_ReadBox_urn, 0 },
4345 { ATOM_dref, MP4_ReadBox_LtdContainer, 0 },
4346 { ATOM_stts, MP4_ReadBox_stts, ATOM_stbl },
4347 { ATOM_ctts, MP4_ReadBox_ctts, ATOM_stbl },
4348 { ATOM_cslg, MP4_ReadBox_cslg, ATOM_stbl },
4349 { ATOM_stsd, MP4_ReadBox_LtdContainer, ATOM_stbl },
4350 { ATOM_stsz, MP4_ReadBox_stsz, ATOM_stbl },
4351 { ATOM_stsc, MP4_ReadBox_stsc, ATOM_stbl },
4352 { ATOM_stco, MP4_ReadBox_stco_co64, ATOM_stbl },
4353 { ATOM_co64, MP4_ReadBox_stco_co64, ATOM_stbl },
4354 { ATOM_stss, MP4_ReadBox_stss, ATOM_stbl },
4355 { ATOM_stsh, MP4_ReadBox_stsh, ATOM_stbl },
4356 { ATOM_stdp, MP4_ReadBox_stdp, 0 },
4357 { ATOM_elst, MP4_ReadBox_elst, ATOM_edts },
4358 { ATOM_cprt, MP4_ReadBox_cprt, 0 },
4359 { ATOM_esds, MP4_ReadBox_esds, ATOM_wave }, /* mp4a in wave chunk */
4360 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4a },
4361 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4v },
4362 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4s },
4363 { ATOM_dcom, MP4_ReadBox_dcom, 0 },
4364 { ATOM_dfLa, MP4_ReadBox_Binary, ATOM_fLaC },
4365 { ATOM_cmvd, MP4_ReadBox_cmvd, 0 },
4366 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc1 },
4367 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc3 },
4368 { ATOM_hvcC, MP4_ReadBox_Binary, 0 },
4369 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp08 },
4370 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp09 },
4371 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp10 },
4372 { ATOM_dac3, MP4_ReadBox_dac3, 0 },
4373 { ATOM_dec3, MP4_ReadBox_dec3, 0 },
4374 { ATOM_dvc1, MP4_ReadBox_dvc1, ATOM_vc1 },
4375 { ATOM_fiel, MP4_ReadBox_fiel, 0 },
4376 { ATOM_glbl, MP4_ReadBox_Binary, ATOM_FFV1 },
4377 { ATOM_enda, MP4_ReadBox_enda, 0 },
4378 { ATOM_iods, MP4_ReadBox_iods, 0 },
4379 { ATOM_pasp, MP4_ReadBox_pasp, 0 },
4380 { ATOM_btrt, MP4_ReadBox_btrt, 0 }, /* codecs bitrate stsd/????/btrt */
4381 { ATOM_keys, MP4_ReadBox_keys, ATOM_meta },
4382 { ATOM_colr, MP4_ReadBox_colr, 0 },
4384 /* XiphQT */
4385 { ATOM_vCtH, MP4_ReadBox_Binary, ATOM_wave },
4386 { ATOM_vCtC, MP4_ReadBox_Binary, ATOM_wave },
4387 { ATOM_vCtd, MP4_ReadBox_Binary, ATOM_wave },
4388 { ATOM_fCtS, MP4_ReadBox_Binary, ATOM_wave },
4390 /* Samples groups specific information */
4391 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_stbl },
4392 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_traf },
4393 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_stbl },
4394 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_traf },
4396 /* Quicktime preview atoms, all at root */
4397 { ATOM_pnot, MP4_ReadBox_pnot, 0 },
4398 { ATOM_pict, MP4_ReadBox_Binary, 0 },
4399 { ATOM_PICT, MP4_ReadBox_Binary, 0 },
4401 /* Nothing to do with this box */
4402 { ATOM_mdat, MP4_ReadBoxSkip, 0 },
4403 { ATOM_skip, MP4_ReadBoxSkip, 0 },
4404 { ATOM_free, MP4_ReadBoxSkip, 0 },
4405 { ATOM_wide, MP4_ReadBoxSkip, 0 },
4406 { ATOM_binm, MP4_ReadBoxSkip, 0 },
4408 /* Subtitles */
4409 { ATOM_tx3g, MP4_ReadBox_sample_text, ATOM_sbtl },
4410 { ATOM_tx3g, MP4_ReadBox_sample_text, ATOM_text },
4411 { ATOM_c608, MP4_ReadBox_sample_clcp, ATOM_stsd },
4412 //{ ATOM_text, MP4_ReadBox_sample_text, 0 },
4413 /* In sample WebVTT subtitle atoms. No ATOM_wvtt in normal parsing */
4414 { ATOM_vttc, MP4_ReadBoxContainer, ATOM_wvtt },
4415 { ATOM_payl, MP4_ReadBox_Binary, ATOM_vttc },
4417 /* for codecs */
4418 { ATOM_soun, MP4_ReadBox_sample_soun, ATOM_stsd },
4419 { ATOM_agsm, MP4_ReadBox_sample_soun, ATOM_stsd },
4420 { ATOM_ac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4421 { ATOM_eac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4422 { ATOM_fLaC, MP4_ReadBox_sample_soun, ATOM_stsd },
4423 { ATOM_lpcm, MP4_ReadBox_sample_soun, ATOM_stsd },
4424 { ATOM_ms02, MP4_ReadBox_sample_soun, ATOM_stsd },
4425 { ATOM_ms11, MP4_ReadBox_sample_soun, ATOM_stsd },
4426 { ATOM_ms55, MP4_ReadBox_sample_soun, ATOM_stsd },
4427 { ATOM__mp3, MP4_ReadBox_sample_soun, ATOM_stsd },
4428 { ATOM_mp4a, MP4_ReadBox_sample_soun, ATOM_stsd },
4429 { ATOM_twos, MP4_ReadBox_sample_soun, ATOM_stsd },
4430 { ATOM_sowt, MP4_ReadBox_sample_soun, ATOM_stsd },
4431 { ATOM_QDMC, MP4_ReadBox_sample_soun, ATOM_stsd },
4432 { ATOM_QDM2, MP4_ReadBox_sample_soun, ATOM_stsd },
4433 { ATOM_ima4, MP4_ReadBox_sample_soun, ATOM_stsd },
4434 { ATOM_IMA4, MP4_ReadBox_sample_soun, ATOM_stsd },
4435 { ATOM_dvi, MP4_ReadBox_sample_soun, ATOM_stsd },
4436 { ATOM_alaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4437 { ATOM_ulaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4438 { ATOM_MAC3, MP4_ReadBox_sample_soun, ATOM_stsd },
4439 { ATOM_MAC6, MP4_ReadBox_sample_soun, ATOM_stsd },
4440 { ATOM_Qclp, MP4_ReadBox_sample_soun, ATOM_stsd },
4441 { ATOM_samr, MP4_ReadBox_sample_soun, ATOM_stsd },
4442 { ATOM_sawb, MP4_ReadBox_sample_soun, ATOM_stsd },
4443 { ATOM_OggS, MP4_ReadBox_sample_soun, ATOM_stsd },
4444 { ATOM_alac, MP4_ReadBox_sample_soun, ATOM_stsd },
4445 { ATOM_WMA2, MP4_ReadBox_sample_soun, ATOM_stsd }, /* flip4mac */
4446 { ATOM_wma, MP4_ReadBox_sample_soun, ATOM_stsd }, /* ismv wmapro */
4447 { ATOM_Opus, MP4_ReadBox_sample_soun, ATOM_stsd },
4448 /* Sound extensions */
4449 { ATOM_chan, MP4_ReadBox_stsdext_chan, 0 },
4450 { ATOM_WMA2, MP4_ReadBox_WMA2, ATOM_wave }, /* flip4mac */
4451 { ATOM_dOps, MP4_ReadBox_Binary, ATOM_Opus },
4452 { ATOM_wfex, MP4_ReadBox_WMA2, ATOM_wma }, /* ismv formatex */
4454 /* Both uncompressed sound and video */
4455 { ATOM_raw, MP4_ReadBox_default, ATOM_stsd },
4457 { ATOM_drmi, MP4_ReadBox_sample_vide, ATOM_stsd },
4458 { ATOM_vide, MP4_ReadBox_sample_vide, ATOM_stsd },
4459 { ATOM_mp4v, MP4_ReadBox_sample_vide, ATOM_stsd },
4460 { ATOM_SVQ1, MP4_ReadBox_sample_vide, ATOM_stsd },
4461 { ATOM_SVQ3, MP4_ReadBox_sample_vide, ATOM_stsd },
4462 { ATOM_ZyGo, MP4_ReadBox_sample_vide, ATOM_stsd },
4463 { ATOM_DIVX, MP4_ReadBox_sample_vide, ATOM_stsd },
4464 { ATOM_XVID, MP4_ReadBox_sample_vide, ATOM_stsd },
4465 { ATOM_h263, MP4_ReadBox_sample_vide, ATOM_stsd },
4466 { ATOM_s263, MP4_ReadBox_sample_vide, ATOM_stsd },
4467 { ATOM_cvid, MP4_ReadBox_sample_vide, ATOM_stsd },
4468 { ATOM_3IV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4469 { ATOM_3iv1, MP4_ReadBox_sample_vide, ATOM_stsd },
4470 { ATOM_3IV2, MP4_ReadBox_sample_vide, ATOM_stsd },
4471 { ATOM_3iv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4472 { ATOM_3IVD, MP4_ReadBox_sample_vide, ATOM_stsd },
4473 { ATOM_3ivd, MP4_ReadBox_sample_vide, ATOM_stsd },
4474 { ATOM_3VID, MP4_ReadBox_sample_vide, ATOM_stsd },
4475 { ATOM_3vid, MP4_ReadBox_sample_vide, ATOM_stsd },
4476 { ATOM_FFV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4477 { ATOM_mjpa, MP4_ReadBox_sample_vide, ATOM_stsd },
4478 { ATOM_mjpb, MP4_ReadBox_sample_vide, ATOM_stsd },
4479 { ATOM_qdrw, MP4_ReadBox_sample_vide, ATOM_stsd },
4480 { ATOM_mp2v, MP4_ReadBox_sample_vide, ATOM_stsd },
4481 { ATOM_hdv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4482 { ATOM_WMV3, MP4_ReadBox_sample_vide, ATOM_stsd },
4484 { ATOM_mjqt, MP4_ReadBox_default, 0 }, /* found in mjpa/b */
4485 { ATOM_mjht, MP4_ReadBox_default, 0 },
4487 { ATOM_dvc, MP4_ReadBox_sample_vide, ATOM_stsd },
4488 { ATOM_dvp, MP4_ReadBox_sample_vide, ATOM_stsd },
4489 { ATOM_dv5n, MP4_ReadBox_sample_vide, ATOM_stsd },
4490 { ATOM_dv5p, MP4_ReadBox_sample_vide, ATOM_stsd },
4491 { ATOM_VP31, MP4_ReadBox_sample_vide, ATOM_stsd },
4492 { ATOM_vp31, MP4_ReadBox_sample_vide, ATOM_stsd },
4493 { ATOM_h264, MP4_ReadBox_sample_vide, ATOM_stsd },
4495 { ATOM_jpeg, MP4_ReadBox_sample_vide, ATOM_stsd },
4496 { ATOM_vc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4497 { ATOM_avc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4498 { ATOM_avc3, MP4_ReadBox_sample_vide, ATOM_stsd },
4500 { ATOM_rrtp, MP4_ReadBox_sample_hint8, ATOM_stsd },
4502 { ATOM_yv12, MP4_ReadBox_sample_vide, 0 },
4503 { ATOM_yuv2, MP4_ReadBox_sample_vide, 0 },
4505 { ATOM_strf, MP4_ReadBox_strf, ATOM_WVC1 }, /* MS smooth */
4506 { ATOM_strf, MP4_ReadBox_strf, ATOM_H264 }, /* MS smooth */
4508 { ATOM_strf, MP4_ReadBox_strf, ATOM_WMV3 }, /* flip4mac */
4509 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_WMV3 }, /* flip4mac */
4510 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_wave }, /* flip4mac */
4512 { ATOM_mp4s, MP4_ReadBox_sample_mp4s, ATOM_stsd },
4514 /* XXX there is 2 box where we could find this entry stbl and tref*/
4515 { ATOM_hint, MP4_ReadBox_default, 0 },
4517 /* found in tref box */
4518 { ATOM_dpnd, MP4_ReadBox_default, 0 },
4519 { ATOM_ipir, MP4_ReadBox_default, 0 },
4520 { ATOM_mpod, MP4_ReadBox_default, 0 },
4521 { ATOM_chap, MP4_ReadBox_tref_generic, 0 },
4523 /* found in hnti */
4524 { ATOM_rtp, MP4_ReadBox_rtp, ATOM_hnti },
4525 { ATOM_sdp, MP4_ReadBox_sdp, ATOM_hnti },
4527 /* found in rrtp sample description */
4528 { ATOM_tims, MP4_ReadBox_tims, 0 },
4529 { ATOM_tsro, MP4_ReadBox_tsro, 0 },
4530 { ATOM_tssy, MP4_ReadBox_tssy, 0 },
4532 /* found in rmra/rmda */
4533 { ATOM_rdrf, MP4_ReadBox_rdrf, ATOM_rmda },
4534 { ATOM_rmdr, MP4_ReadBox_rmdr, ATOM_rmda },
4535 { ATOM_rmqu, MP4_ReadBox_rmqu, ATOM_rmda },
4536 { ATOM_rmvc, MP4_ReadBox_rmvc, ATOM_rmda },
4538 { ATOM_drms, MP4_ReadBox_sample_soun, 0 },
4539 { ATOM_sinf, MP4_ReadBoxContainer, 0 },
4540 { ATOM_schi, MP4_ReadBoxContainer, 0 },
4541 { ATOM_user, MP4_ReadBox_drms, 0 },
4542 { ATOM_key, MP4_ReadBox_drms, 0 },
4543 { ATOM_iviv, MP4_ReadBox_drms, 0 },
4544 { ATOM_priv, MP4_ReadBox_drms, 0 },
4545 { ATOM_frma, MP4_ReadBox_frma, ATOM_sinf }, /* and rinf */
4546 { ATOM_frma, MP4_ReadBox_frma, ATOM_wave }, /* flip4mac */
4547 { ATOM_skcr, MP4_ReadBox_skcr, 0 },
4549 /* ilst meta tags */
4550 { ATOM_0xa9ART, MP4_ReadBox_Metadata, ATOM_ilst },
4551 { ATOM_0xa9alb, MP4_ReadBox_Metadata, ATOM_ilst },
4552 { ATOM_0xa9cmt, MP4_ReadBox_Metadata, ATOM_ilst },
4553 { ATOM_0xa9com, MP4_ReadBox_Metadata, ATOM_ilst },
4554 { ATOM_0xa9cpy, MP4_ReadBox_Metadata, ATOM_ilst },
4555 { ATOM_0xa9day, MP4_ReadBox_Metadata, ATOM_ilst },
4556 { ATOM_0xa9des, MP4_ReadBox_Metadata, ATOM_ilst },
4557 { ATOM_0xa9enc, MP4_ReadBox_Metadata, ATOM_ilst },
4558 { ATOM_0xa9gen, MP4_ReadBox_Metadata, ATOM_ilst },
4559 { ATOM_0xa9grp, MP4_ReadBox_Metadata, ATOM_ilst },
4560 { ATOM_0xa9lyr, MP4_ReadBox_Metadata, ATOM_ilst },
4561 { ATOM_0xa9nam, MP4_ReadBox_Metadata, ATOM_ilst },
4562 { ATOM_0xa9too, MP4_ReadBox_Metadata, ATOM_ilst },
4563 { ATOM_0xa9trk, MP4_ReadBox_Metadata, ATOM_ilst },
4564 { ATOM_0xa9wrt, MP4_ReadBox_Metadata, ATOM_ilst },
4565 { ATOM_aART, MP4_ReadBox_Metadata, ATOM_ilst },
4566 { ATOM_atID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4567 { ATOM_cnID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4568 { ATOM_covr, MP4_ReadBoxContainer, ATOM_ilst },
4569 { ATOM_desc, MP4_ReadBox_Metadata, ATOM_ilst },
4570 { ATOM_disk, MP4_ReadBox_Metadata, ATOM_ilst },
4571 { ATOM_flvr, MP4_ReadBox_Metadata, ATOM_ilst },
4572 { ATOM_gnre, MP4_ReadBox_Metadata, ATOM_ilst },
4573 { ATOM_rtng, MP4_ReadBox_Metadata, ATOM_ilst },
4574 { ATOM_trkn, MP4_ReadBox_Metadata, ATOM_ilst },
4575 { ATOM_xid_, MP4_ReadBox_Metadata, ATOM_ilst },
4576 { ATOM_gshh, MP4_ReadBox_Metadata, ATOM_ilst }, /* YouTube gs?? */
4577 { ATOM_gspm, MP4_ReadBox_Metadata, ATOM_ilst },
4578 { ATOM_gspu, MP4_ReadBox_Metadata, ATOM_ilst },
4579 { ATOM_gssd, MP4_ReadBox_Metadata, ATOM_ilst },
4580 { ATOM_gsst, MP4_ReadBox_Metadata, ATOM_ilst },
4581 { ATOM_gstd, MP4_ReadBox_Metadata, ATOM_ilst },
4582 { ATOM_ITUN, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunesInfo */
4584 /* udta */
4585 { ATOM_0x40PRM, MP4_ReadBox_Binary, ATOM_udta },
4586 { ATOM_0x40PRQ, MP4_ReadBox_Binary, ATOM_udta },
4587 { ATOM_0xa9ART, MP4_ReadBox_Binary, ATOM_udta },
4588 { ATOM_0xa9alb, MP4_ReadBox_Binary, ATOM_udta },
4589 { ATOM_0xa9ard, MP4_ReadBox_Binary, ATOM_udta },
4590 { ATOM_0xa9arg, MP4_ReadBox_Binary, ATOM_udta },
4591 { ATOM_0xa9aut, MP4_ReadBox_Binary, ATOM_udta },
4592 { ATOM_0xa9cak, MP4_ReadBox_Binary, ATOM_udta },
4593 { ATOM_0xa9cmt, MP4_ReadBox_Binary, ATOM_udta },
4594 { ATOM_0xa9con, MP4_ReadBox_Binary, ATOM_udta },
4595 { ATOM_0xa9com, MP4_ReadBox_Binary, ATOM_udta },
4596 { ATOM_0xa9cpy, MP4_ReadBox_Binary, ATOM_udta },
4597 { ATOM_0xa9day, MP4_ReadBox_Binary, ATOM_udta },
4598 { ATOM_0xa9des, MP4_ReadBox_Binary, ATOM_udta },
4599 { ATOM_0xa9dir, MP4_ReadBox_Binary, ATOM_udta },
4600 { ATOM_0xa9dis, MP4_ReadBox_Binary, ATOM_udta },
4601 { ATOM_0xa9dsa, MP4_ReadBox_Binary, ATOM_udta },
4602 { ATOM_0xa9fmt, MP4_ReadBox_Binary, ATOM_udta },
4603 { ATOM_0xa9gen, MP4_ReadBox_Binary, ATOM_udta },
4604 { ATOM_0xa9grp, MP4_ReadBox_Binary, ATOM_udta },
4605 { ATOM_0xa9hst, MP4_ReadBox_Binary, ATOM_udta },
4606 { ATOM_0xa9inf, MP4_ReadBox_Binary, ATOM_udta },
4607 { ATOM_0xa9isr, MP4_ReadBox_Binary, ATOM_udta },
4608 { ATOM_0xa9lab, MP4_ReadBox_Binary, ATOM_udta },
4609 { ATOM_0xa9lal, MP4_ReadBox_Binary, ATOM_udta },
4610 { ATOM_0xa9lnt, MP4_ReadBox_Binary, ATOM_udta },
4611 { ATOM_0xa9lyr, MP4_ReadBox_Binary, ATOM_udta },
4612 { ATOM_0xa9mak, MP4_ReadBox_Binary, ATOM_udta },
4613 { ATOM_0xa9mal, MP4_ReadBox_Binary, ATOM_udta },
4614 { ATOM_0xa9mod, MP4_ReadBox_Binary, ATOM_udta },
4615 { ATOM_0xa9nam, MP4_ReadBox_Binary, ATOM_udta },
4616 { ATOM_0xa9ope, MP4_ReadBox_Binary, ATOM_udta },
4617 { ATOM_0xa9phg, MP4_ReadBox_Binary, ATOM_udta },
4618 { ATOM_0xa9PRD, MP4_ReadBox_Binary, ATOM_udta },
4619 { ATOM_0xa9prd, MP4_ReadBox_Binary, ATOM_udta },
4620 { ATOM_0xa9prf, MP4_ReadBox_Binary, ATOM_udta },
4621 { ATOM_0xa9pub, MP4_ReadBox_Binary, ATOM_udta },
4622 { ATOM_0xa9req, MP4_ReadBox_Binary, ATOM_udta },
4623 { ATOM_0xa9sne, MP4_ReadBox_Binary, ATOM_udta },
4624 { ATOM_0xa9snm, MP4_ReadBox_Binary, ATOM_udta },
4625 { ATOM_0xa9sol, MP4_ReadBox_Binary, ATOM_udta },
4626 { ATOM_0xa9src, MP4_ReadBox_Binary, ATOM_udta },
4627 { ATOM_0xa9st3, MP4_ReadBox_Binary, ATOM_udta },
4628 { ATOM_0xa9swr, MP4_ReadBox_Binary, ATOM_udta },
4629 { ATOM_0xa9thx, MP4_ReadBox_Binary, ATOM_udta },
4630 { ATOM_0xa9too, MP4_ReadBox_Binary, ATOM_udta },
4631 { ATOM_0xa9trk, MP4_ReadBox_Binary, ATOM_udta },
4632 { ATOM_0xa9url, MP4_ReadBox_Binary, ATOM_udta },
4633 { ATOM_0xa9wrn, MP4_ReadBox_Binary, ATOM_udta },
4634 { ATOM_0xa9xpd, MP4_ReadBox_Binary, ATOM_udta },
4635 { ATOM_0xa9xyz, MP4_ReadBox_Binary, ATOM_udta },
4636 { ATOM_chpl, MP4_ReadBox_chpl, ATOM_udta }, /* nero unlabeled chapters list */
4637 { ATOM_MCPS, MP4_ReadBox_Binary, ATOM_udta },
4638 { ATOM_name, MP4_ReadBox_Binary, ATOM_udta },
4639 { ATOM_vndr, MP4_ReadBox_Binary, ATOM_udta },
4640 { ATOM_SDLN, MP4_ReadBox_Binary, ATOM_udta },
4641 { ATOM_HMMT, MP4_ReadBox_HMMT, ATOM_udta }, /* GoPro HiLight tags */
4643 /* udta, non meta */
4644 { ATOM_tsel, MP4_ReadBox_tsel, ATOM_udta },
4646 /* iTunes/Quicktime meta info */
4647 { ATOM_meta, MP4_ReadBox_meta, 0 },
4648 { ATOM_ID32, MP4_ReadBox_Binary, ATOM_meta }, /* ID3v2 in 3GPP / ETSI TS 126 244 8.3 */
4649 { ATOM_data, MP4_ReadBox_data, 0 }, /* ilst/@too and others, ITUN/data */
4650 { ATOM_mean, MP4_ReadBox_Binary, ATOM_ITUN },
4651 { ATOM_name, MP4_ReadBox_Binary, ATOM_ITUN },
4653 /* found in smoothstreaming */
4654 { ATOM_traf, MP4_ReadBoxContainer, ATOM_moof },
4655 { ATOM_mfra, MP4_ReadBoxContainer, 0 },
4656 { ATOM_mfhd, MP4_ReadBox_mfhd, ATOM_moof },
4657 { ATOM_sidx, MP4_ReadBox_sidx, 0 },
4658 { ATOM_tfhd, MP4_ReadBox_tfhd, ATOM_traf },
4659 { ATOM_trun, MP4_ReadBox_trun, ATOM_traf },
4660 { ATOM_tfdt, MP4_ReadBox_tfdt, ATOM_traf },
4661 { ATOM_trex, MP4_ReadBox_trex, ATOM_mvex },
4662 { ATOM_mehd, MP4_ReadBox_mehd, ATOM_mvex },
4663 { ATOM_sdtp, MP4_ReadBox_sdtp, 0 },
4664 { ATOM_tfra, MP4_ReadBox_tfra, ATOM_mfra },
4665 { ATOM_mfro, MP4_ReadBox_mfro, ATOM_mfra },
4666 { ATOM_uuid, MP4_ReadBox_uuid, 0 },
4668 /* spatial/360°/VR */
4669 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_avc1 },
4670 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_mp4v },
4671 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_avc1 },
4672 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_mp4v },
4673 { ATOM_proj, MP4_ReadBoxContainer, ATOM_sv3d },
4674 { ATOM_prhd, MP4_ReadBox_prhd, ATOM_proj },
4675 { ATOM_equi, MP4_ReadBox_equi, ATOM_proj },
4676 { ATOM_cbmp, MP4_ReadBox_cbmp, ATOM_proj },
4678 /* Ambisonics */
4679 { ATOM_SA3D, MP4_ReadBox_SA3D, 0 },
4681 /* Last entry */
4682 { 0, MP4_ReadBox_default, 0 }
4685 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
4687 int i_index;
4689 for( i_index = 0; ; i_index++ )
4691 if ( MP4_Box_Function[i_index].i_parent &&
4692 p_father && p_father->i_type != MP4_Box_Function[i_index].i_parent )
4693 continue;
4695 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
4696 ( MP4_Box_Function[i_index].i_type == 0 ) )
4698 break;
4702 if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
4704 return VLC_EGENERIC;
4707 return VLC_SUCCESS;
4710 /*****************************************************************************
4711 * MP4_ReadBox : parse the actual box and the children
4712 * XXX : Do not go to the next box
4713 *****************************************************************************/
4714 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
4716 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
4717 if( p_box == NULL )
4718 return NULL;
4720 if( !MP4_PeekBoxHeader( p_stream, p_box ) )
4722 msg_Warn( p_stream, "cannot read one box" );
4723 free( p_box );
4724 return NULL;
4727 if( p_father && p_father->i_size > 0 &&
4728 p_father->i_pos + p_father->i_size < p_box->i_pos + p_box->i_size )
4730 msg_Dbg( p_stream, "out of bound child" );
4731 free( p_box );
4732 return NULL;
4735 if( !p_box->i_size )
4737 msg_Dbg( p_stream, "found an empty box (null size)" );
4738 free( p_box );
4739 return NULL;
4741 p_box->p_father = p_father;
4743 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
4745 uint64_t i_end = p_box->i_pos + p_box->i_size;
4746 MP4_BoxFree( p_box );
4747 MP4_Seek( p_stream, i_end ); /* Skip the failed box */
4748 return NULL;
4751 return p_box;
4754 /*****************************************************************************
4755 * MP4_BoxNew : creates and initializes an arbitrary box
4756 *****************************************************************************/
4757 MP4_Box_t * MP4_BoxNew( uint32_t i_type )
4759 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) );
4760 if( likely( p_box != NULL ) )
4762 p_box->i_type = i_type;
4764 return p_box;
4767 /*****************************************************************************
4768 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
4769 * the children
4770 *****************************************************************************/
4771 void MP4_BoxFree( MP4_Box_t *p_box )
4773 MP4_Box_t *p_child;
4775 if( !p_box )
4776 return; /* hehe */
4778 for( p_child = p_box->p_first; p_child != NULL; )
4780 MP4_Box_t *p_next;
4782 p_next = p_child->p_next;
4783 MP4_BoxFree( p_child );
4784 p_child = p_next;
4787 if( p_box->pf_free )
4788 p_box->pf_free( p_box );
4790 free( p_box->data.p_payload );
4791 free( p_box );
4794 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
4796 /* p_chunk is a virtual root container for the moof and mdat boxes */
4797 MP4_Box_t *p_fakeroot;
4798 MP4_Box_t *p_tmp_box;
4800 p_fakeroot = MP4_BoxNew( ATOM_root );
4801 if( unlikely( p_fakeroot == NULL ) )
4802 return NULL;
4803 p_fakeroot->i_shortsize = 1;
4805 const uint32_t stoplist[] = { ATOM_moov, ATOM_moof, 0 };
4806 MP4_ReadBoxContainerChildren( s, p_fakeroot, stoplist );
4808 p_tmp_box = p_fakeroot->p_first;
4809 if( p_tmp_box == NULL )
4811 MP4_BoxFree( p_fakeroot );
4812 return NULL;
4814 else while( p_tmp_box )
4816 p_fakeroot->i_size += p_tmp_box->i_size;
4817 p_tmp_box = p_tmp_box->p_next;
4820 return p_fakeroot;
4823 /*****************************************************************************
4824 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
4825 *****************************************************************************
4826 * The first box is a virtual box "root" and is the father for all first
4827 * level boxes for the file, a sort of virtual contener
4828 *****************************************************************************/
4829 MP4_Box_t *MP4_BoxGetRoot( stream_t *p_stream )
4831 int i_result;
4833 MP4_Box_t *p_vroot = MP4_BoxNew( ATOM_root );
4834 if( p_vroot == NULL )
4835 return NULL;
4837 p_vroot->i_shortsize = 1;
4838 uint64_t i_size;
4839 if( vlc_stream_GetSize( p_stream, &i_size ) == 0 )
4840 p_vroot->i_size = i_size;
4842 /* First get the moov */
4844 const uint32_t stoplist[] = { ATOM_moov, ATOM_mdat, 0 };
4845 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
4848 /* mdat appeared first */
4849 if( i_result && !MP4_BoxGet( p_vroot, "moov" ) )
4851 bool b_seekable;
4852 if( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_seekable ) != VLC_SUCCESS || !b_seekable )
4854 msg_Err( p_stream, "no moov before mdat and the stream is not seekable" );
4855 goto error;
4858 /* continue loading up to moov */
4859 const uint32_t stoplist[] = { ATOM_moov, 0 };
4860 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
4863 if( !i_result )
4864 goto error;
4866 /* If there is a mvex box, it means fragmented MP4, and we're done */
4867 if( MP4_BoxCount( p_vroot, "moov/mvex" ) > 0 )
4869 /* Read a bit more atoms as we might have an index between moov and moof */
4870 const uint32_t stoplist[] = { ATOM_sidx, 0 };
4871 const uint32_t excludelist[] = { ATOM_moof, ATOM_mdat, 0 };
4872 MP4_ReadBoxContainerChildrenIndexed( p_stream, p_vroot, stoplist, excludelist, false );
4873 return p_vroot;
4876 if( vlc_stream_Tell( p_stream ) + 8 < (uint64_t) stream_Size( p_stream ) )
4878 /* Get the rest of the file */
4879 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, NULL );
4881 if( !i_result )
4882 goto error;
4885 MP4_Box_t *p_moov;
4886 MP4_Box_t *p_cmov;
4888 /* check if there is a cmov, if so replace
4889 compressed moov by uncompressed one */
4890 if( ( ( p_moov = MP4_BoxGet( p_vroot, "moov" ) ) &&
4891 ( p_cmov = MP4_BoxGet( p_vroot, "moov/cmov" ) ) ) ||
4892 ( ( p_moov = MP4_BoxGet( p_vroot, "foov" ) ) &&
4893 ( p_cmov = MP4_BoxGet( p_vroot, "foov/cmov" ) ) ) )
4895 /* rename the compressed moov as a box to skip */
4896 p_moov->i_type = ATOM_skip;
4898 /* get uncompressed p_moov */
4899 p_moov = p_cmov->data.p_cmov->p_moov;
4900 p_cmov->data.p_cmov->p_moov = NULL;
4902 /* make p_root father of this new moov */
4903 p_moov->p_father = p_vroot;
4905 /* insert this new moov box as first child of p_root */
4906 p_moov->p_next = p_vroot->p_first;
4907 p_vroot->p_first = p_moov;
4910 return p_vroot;
4912 error:
4913 MP4_BoxFree( p_vroot );
4914 MP4_Seek( p_stream, 0 );
4915 return NULL;
4919 static void MP4_BoxDumpStructure_Internal( stream_t *s, const MP4_Box_t *p_box,
4920 unsigned int i_level )
4922 const MP4_Box_t *p_child;
4923 uint32_t i_displayedtype = p_box->i_type;
4924 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype)[0] = 'c';
4926 if( !i_level )
4928 msg_Dbg( s, "dumping root Box \"%4.4s\"",
4929 (char*)&i_displayedtype );
4931 else
4933 char str[512];
4934 if( i_level >= (sizeof(str) - 1)/4 )
4935 return;
4937 memset( str, ' ', sizeof(str) );
4938 for( unsigned i = 0; i < i_level; i++ )
4940 str[i*4] = '|';
4943 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
4944 "+ %4.4s size %"PRIu64" offset %" PRIuMAX "%s",
4945 (char*)&i_displayedtype, p_box->i_size,
4946 (uintmax_t)p_box->i_pos,
4947 p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
4948 msg_Dbg( s, "%s", str );
4950 p_child = p_box->p_first;
4951 while( p_child )
4953 MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
4954 p_child = p_child->p_next;
4958 void MP4_BoxDumpStructure( stream_t *s, const MP4_Box_t *p_box )
4960 MP4_BoxDumpStructure_Internal( s, p_box, 0 );
4964 /*****************************************************************************
4965 *****************************************************************************
4967 ** High level methods to acces an MP4 file
4969 *****************************************************************************
4970 *****************************************************************************/
4971 static bool get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
4973 size_t i_len ;
4974 if( !*ppsz_path[0] )
4976 *ppsz_token = NULL;
4977 *pi_number = 0;
4978 return true;
4980 i_len = strcspn( *ppsz_path, "/[" );
4981 if( !i_len && **ppsz_path == '/' )
4983 i_len = 1;
4985 *ppsz_token = strndup( *ppsz_path, i_len );
4986 if( unlikely(!*ppsz_token) )
4987 return false;
4989 *ppsz_path += i_len;
4991 /* Parse the token number token[n] */
4992 if( **ppsz_path == '[' )
4994 (*ppsz_path)++;
4995 *pi_number = strtol( *ppsz_path, NULL, 10 );
4996 while( **ppsz_path && **ppsz_path != ']' )
4998 (*ppsz_path)++;
5000 if( **ppsz_path == ']' )
5002 (*ppsz_path)++;
5005 else
5007 *pi_number = 0;
5010 /* Forward to start of next token */
5011 while( **ppsz_path == '/' )
5013 (*ppsz_path)++;
5016 return true;
5019 static void MP4_BoxGet_Internal( const MP4_Box_t **pp_result, const MP4_Box_t *p_box,
5020 const char *psz_fmt, va_list args)
5022 char *psz_dup;
5023 char *psz_path;
5024 char *psz_token = NULL;
5026 if( !p_box )
5028 *pp_result = NULL;
5029 return;
5032 if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
5033 psz_path = NULL;
5035 if( !psz_path || !psz_path[0] )
5037 free( psz_path );
5038 *pp_result = NULL;
5039 return;
5042 // fprintf( stderr, "path:'%s'\n", psz_path );
5043 psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
5044 for( ; ; )
5046 int i_number;
5048 if( !get_token( &psz_path, &psz_token, &i_number ) )
5049 goto error_box;
5050 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
5051 // psz_path,psz_token,i_number );
5052 if( !psz_token )
5054 free( psz_dup );
5055 *pp_result = p_box;
5056 return;
5058 else
5059 if( !strcmp( psz_token, "/" ) )
5061 /* Find root box */
5062 while( p_box && p_box->i_type != ATOM_root )
5064 p_box = p_box->p_father;
5066 if( !p_box )
5068 goto error_box;
5071 else
5072 if( !strcmp( psz_token, "." ) )
5074 /* Do nothing */
5076 else
5077 if( !strcmp( psz_token, ".." ) )
5079 p_box = p_box->p_father;
5080 if( !p_box )
5082 goto error_box;
5085 else
5086 if( strlen( psz_token ) == 4 )
5088 uint32_t i_fourcc;
5089 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
5090 psz_token[2], psz_token[3] );
5091 p_box = p_box->p_first;
5092 for( ; ; )
5094 if( !p_box )
5096 goto error_box;
5098 if( p_box->i_type == i_fourcc )
5100 if( !i_number )
5102 break;
5104 i_number--;
5106 p_box = p_box->p_next;
5109 else
5110 if( *psz_token == '\0' )
5112 p_box = p_box->p_first;
5113 for( ; ; )
5115 if( !p_box )
5117 goto error_box;
5119 if( !i_number )
5121 break;
5123 i_number--;
5124 p_box = p_box->p_next;
5127 else
5129 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
5130 goto error_box;
5133 free( psz_token );
5136 return;
5138 error_box:
5139 free( psz_token );
5140 free( psz_dup );
5141 *pp_result = NULL;
5142 return;
5145 /*****************************************************************************
5146 * MP4_BoxGet: find a box given a path relative to p_box
5147 *****************************************************************************
5148 * Path Format: . .. / as usual
5149 * [number] to specifie box number ex: trak[12]
5151 * ex: /moov/trak[12]
5152 * ../mdia
5153 *****************************************************************************/
5154 MP4_Box_t *MP4_BoxGet( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5156 va_list args;
5157 const MP4_Box_t *p_result;
5159 va_start( args, psz_fmt );
5160 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5161 va_end( args );
5163 return( (MP4_Box_t *) p_result );
5166 /*****************************************************************************
5167 * MP4_BoxCount: count box given a path relative to p_box
5168 *****************************************************************************
5169 * Path Format: . .. / as usual
5170 * [number] to specifie box number ex: trak[12]
5172 * ex: /moov/trak[12]
5173 * ../mdia
5174 *****************************************************************************/
5175 unsigned MP4_BoxCount( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5177 va_list args;
5178 unsigned i_count;
5179 const MP4_Box_t *p_result, *p_next;
5181 va_start( args, psz_fmt );
5182 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5183 va_end( args );
5184 if( !p_result )
5186 return( 0 );
5189 i_count = 1;
5190 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
5192 if( p_next->i_type == p_result->i_type)
5194 i_count++;
5197 return( i_count );