mp4: fix ELST signed overflow
[vlc.git] / modules / demux / mp4 / libmp4.c
blobdbe3598c629dac061e8fa384314f28814a8cd1f4
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_GET1BYTE( dst ) MP4_GETX_PRIVATE( dst, *p_peek, 1 )
73 #define MP4_GET3BYTES( dst ) MP4_GETX_PRIVATE( dst, Get24bBE(p_peek), 3 )
74 #define MP4_GET4BYTES( dst ) MP4_GETX_PRIVATE( dst, GetDWBE(p_peek), 4 )
75 #define MP4_GET8BYTES( dst ) MP4_GETX_PRIVATE( dst, GetQWBE(p_peek), 8 )
76 #define MP4_GETFOURCC( dst ) MP4_GETX_PRIVATE( dst, \
77 VLC_FOURCC(p_peek[0],p_peek[1],p_peek[2],p_peek[3]), 4)
79 #define MP4_GET2BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetWLE(p_peek), 2 )
80 #define MP4_GET4BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetDWLE(p_peek), 4 )
81 #define MP4_GET8BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetQWLE(p_peek), 8 )
83 #define MP4_GETVERSIONFLAGS( p_void ) \
84 MP4_GET1BYTE( p_void->i_version ); \
85 MP4_GET3BYTES( p_void->i_flags )
87 static char *mp4_getstringz( uint8_t **restrict in, uint64_t *restrict size )
89 assert( *size <= SSIZE_MAX );
91 size_t len = strnlen( (const char *)*in, *size );
92 if( len == 0 )
93 return NULL;
95 char *ret = malloc( len + 1 );
96 if( likely(ret != NULL) )
98 memcpy( ret, *in, len );
99 ret[len] = '\0';
101 len++;
102 *in += len;
103 *size -= len;
104 return ret;
107 #define MP4_GETSTRINGZ( p_str ) \
108 do \
109 (p_str) = mp4_getstringz( &p_peek, &i_read ); \
110 while(0)
112 static uint8_t *mp4_readbox_enter_common( stream_t *s, MP4_Box_t *box,
113 size_t typesize,
114 void (*release)( MP4_Box_t * ),
115 uint64_t readsize )
117 const size_t headersize = mp4_box_headersize( box );
119 if( unlikely(readsize < headersize) || unlikely(readsize > SSIZE_MAX) )
120 return NULL;
122 uint8_t *buf = malloc( readsize );
123 if( unlikely(buf == NULL) )
124 return NULL;
126 ssize_t val = vlc_stream_Read( s, buf, readsize );
127 if( (size_t)val != readsize )
129 msg_Warn( s, "mp4: wanted %"PRIu64" bytes, got %zd", readsize, val );
130 goto error;
133 box->data.p_payload = malloc( typesize );
134 if( unlikely(box->data.p_payload == NULL) )
135 goto error;
137 memset( box->data.p_payload, 0, typesize );
138 box->pf_free = release;
139 return buf;
140 error:
141 free( buf );
142 return NULL;
145 static uint8_t *mp4_readbox_enter_partial( stream_t *s, MP4_Box_t *box,
146 size_t typesize,
147 void (*release)( MP4_Box_t * ),
148 uint64_t *restrict readsize )
150 if( (uint64_t)*readsize > box->i_size )
151 *readsize = box->i_size;
153 return mp4_readbox_enter_common( s, box, typesize, release, *readsize );
156 static uint8_t *mp4_readbox_enter( stream_t *s, MP4_Box_t *box,
157 size_t typesize,
158 void (*release)( MP4_Box_t * ) )
160 uint64_t readsize = box->i_size;
161 return mp4_readbox_enter_common( s, box, typesize, release, readsize );
165 #define MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_TYPE_t, maxread, release ) \
166 uint64_t i_read = (maxread); \
167 uint8_t *p_buff = mp4_readbox_enter_partial( p_stream, p_box, \
168 sizeof( MP4_Box_data_TYPE_t ), release, &i_read ); \
169 if( unlikely(p_buff == NULL) ) \
170 return 0; \
171 const size_t header_size = mp4_box_headersize( p_box ); \
172 uint8_t *p_peek = p_buff + header_size; \
173 i_read -= header_size
175 #define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t, release ) \
176 uint8_t *p_buff = mp4_readbox_enter( p_stream, p_box, \
177 sizeof(MP4_Box_data_TYPE_t), release ); \
178 if( unlikely(p_buff == NULL) ) \
179 return 0; \
180 uint64_t i_read = p_box->i_size; \
181 const size_t header_size = mp4_box_headersize( p_box ); \
182 uint8_t *p_peek = p_buff + header_size; \
183 i_read -= header_size
185 #define MP4_READBOX_EXIT( i_code ) \
186 do \
188 free( p_buff ); \
189 return( i_code ); \
190 } while (0)
192 /*****************************************************************************
193 * Some prototypes.
194 *****************************************************************************/
195 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
196 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father );
197 static void MP4_Box_Clean_Specific( MP4_Box_t *p_box );
198 static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box );
200 int MP4_Seek( stream_t *p_stream, uint64_t i_pos )
202 /* Prevent prefetch breakage */
203 uint64_t i_size = stream_Size( p_stream );
204 if( i_size > 0 && i_pos >= i_size )
205 return VLC_EGENERIC;
207 bool b_canseek = false;
208 if ( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_canseek ) != VLC_SUCCESS ||
209 b_canseek )
211 /* can seek or don't know */
212 return vlc_stream_Seek( p_stream, i_pos );
214 /* obviously can't seek then */
216 int64_t i_current_pos = vlc_stream_Tell( p_stream );
217 if ( i_current_pos < 0 || i_pos < (uint64_t)i_current_pos )
218 return VLC_EGENERIC;
220 size_t i_toread = i_pos - i_current_pos;
221 if( i_toread == 0 )
222 return VLC_SUCCESS;
223 else if( i_toread > (1<<17) )
224 return VLC_EGENERIC;
226 if( vlc_stream_Read( p_stream, NULL, i_toread ) != (ssize_t)i_toread )
227 return VLC_EGENERIC;
228 return VLC_SUCCESS;
231 static void MP4_BoxAddChild( MP4_Box_t *p_parent, MP4_Box_t *p_childbox )
233 if( !p_parent->p_first )
234 p_parent->p_first = p_childbox;
235 else
236 p_parent->p_last->p_next = p_childbox;
237 p_parent->p_last = p_childbox;
238 p_childbox->p_father = p_parent;
241 MP4_Box_t * MP4_BoxExtract( MP4_Box_t **pp_chain, uint32_t i_type )
243 MP4_Box_t *p_box = *pp_chain;
244 while( p_box )
246 if( p_box->i_type == i_type )
248 *pp_chain = p_box->p_next;
249 p_box->p_next = NULL;
250 return p_box;
252 pp_chain = &p_box->p_next;
253 p_box = p_box->p_next;
255 return NULL;
258 /* Don't use vlc_stream_Seek directly */
259 #undef vlc_stream_Seek
260 #define vlc_stream_Seek(a,b) __NO__
262 /*****************************************************************************
263 * MP4_PeekBoxHeader : Load only common parameters for all boxes
264 *****************************************************************************
265 * p_box need to be an already allocated MP4_Box_t, and all data
266 * will only be peek not read
268 * RETURN : 0 if it fail, 1 otherwise
269 *****************************************************************************/
270 static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box )
272 int i_read;
273 const uint8_t *p_peek;
275 if( ( ( i_read = vlc_stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
277 return 0;
279 p_box->i_pos = vlc_stream_Tell( p_stream );
281 p_box->data.p_payload = NULL;
282 p_box->p_father = NULL;
283 p_box->p_first = NULL;
284 p_box->p_last = NULL;
285 p_box->p_next = NULL;
287 MP4_GET4BYTES( p_box->i_shortsize );
288 MP4_GETFOURCC( p_box->i_type );
290 /* Now special case */
292 if( p_box->i_shortsize == 1 )
294 if( i_read < 8 )
295 return 0;
296 /* get the true size on 64 bits */
297 MP4_GET8BYTES( p_box->i_size );
299 else
301 p_box->i_size = p_box->i_shortsize;
302 /* XXX size of 0 means that the box extends to end of file */
305 if( UINT64_MAX - p_box->i_size < p_box->i_pos )
306 return 0;
308 if( p_box->i_type == ATOM_uuid )
310 if( i_read < 16 )
311 return 0;
312 /* get extented type on 16 bytes */
313 GetUUID( &p_box->i_uuid, p_peek );
316 #ifdef MP4_ULTRA_VERBOSE
317 if( p_box->i_size )
319 if MP4_BOX_TYPE_ASCII()
320 msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64" %"PRId64,
321 (char*)&p_box->i_type, p_box->i_size, p_box->i_pos );
322 else
323 msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
324 (char*)&p_box->i_type+1, p_box->i_size );
326 #endif
328 return 1;
331 /*****************************************************************************
332 * MP4_ReadBoxRestricted : Reads box from current position
333 *****************************************************************************
334 * if p_box == NULL, box is invalid or failed, position undefined
335 * on success, position is past read box or EOF
336 *****************************************************************************/
337 static MP4_Box_t *MP4_ReadBoxRestricted( stream_t *p_stream, MP4_Box_t *p_father,
338 const uint32_t onlytypes[], const uint32_t nottypes[],
339 bool *pb_restrictionhit )
341 MP4_Box_t peekbox = { 0 };
342 if ( !MP4_PeekBoxHeader( p_stream, &peekbox ) )
343 return NULL;
345 if( peekbox.i_size < 8 )
347 msg_Warn( p_stream, "found an invalid sized %"PRIu64" box %4.4s @%"PRIu64 ,
348 peekbox.i_size, (char *) &peekbox.i_type, vlc_stream_Tell(p_stream) );
349 return NULL;
352 for( size_t i=0; nottypes && nottypes[i]; i++ )
354 if( nottypes[i] == peekbox.i_type )
356 *pb_restrictionhit = true;
357 return NULL;
361 for( size_t i=0; onlytypes && onlytypes[i]; i++ )
363 if( onlytypes[i] != peekbox.i_type )
365 *pb_restrictionhit = true;
366 return NULL;
370 /* if father's size == 0, it means unknown or infinite size,
371 * and we skip the followong check */
372 if( p_father && p_father->i_size > 0 )
374 const uint64_t i_box_next = peekbox.i_size + peekbox.i_pos;
375 const uint64_t i_father_next = p_father->i_size + p_father->i_pos;
376 /* check if it's within p-father */
377 if( i_box_next > i_father_next )
379 msg_Warn( p_stream, "out of bound child %4.4s", (char*) &peekbox.i_type );
380 return NULL; /* out of bound */
384 /* Everything seems OK */
385 MP4_Box_t *p_box = (MP4_Box_t *) malloc( sizeof(MP4_Box_t) );
386 if( !p_box )
387 return NULL;
388 *p_box = peekbox;
390 const uint64_t i_next = p_box->i_pos + p_box->i_size;
391 p_box->p_father = p_father;
392 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
394 msg_Warn( p_stream, "Failed reading box %4.4s", (char*) &peekbox.i_type );
395 MP4_BoxFree( p_box );
396 p_box = NULL;
399 /* Check is we consumed all data */
400 if( vlc_stream_Tell( p_stream ) < i_next )
402 MP4_Seek( p_stream, i_next - 1 ); /* since past seek can fail when hitting EOF */
403 MP4_Seek( p_stream, i_next );
404 if( vlc_stream_Tell( p_stream ) < i_next - 1 ) /* Truncated box */
406 msg_Warn( p_stream, "truncated box %4.4s discarded", (char*) &peekbox.i_type );
407 MP4_BoxFree( p_box );
408 p_box = NULL;
412 if ( p_box )
413 MP4_BoxAddChild( p_father, p_box );
415 return p_box;
418 /*****************************************************************************
419 * For all known box a loader is given,
420 * you have to be already read container header
421 * without container size, file position on exit is unknown
422 *****************************************************************************/
423 static int MP4_ReadBoxContainerChildrenIndexed( stream_t *p_stream,
424 MP4_Box_t *p_container, const uint32_t stoplist[],
425 const uint32_t excludelist[], bool b_indexed )
427 /* Size of root container is set to 0 when unknown, for exemple
428 * with a DASH stream. In that case, we skip the following check */
429 if( (p_container->i_size || p_container->p_father)
430 && ( vlc_stream_Tell( p_stream ) + ((b_indexed)?16:8) >
431 (uint64_t)(p_container->i_pos + p_container->i_size) )
434 /* there is no box to load */
435 return 0;
438 uint64_t i_last_pos = 0; /* used to detect read failure loops */
439 const uint64_t i_end = p_container->i_pos + p_container->i_size;
440 MP4_Box_t *p_box = NULL;
441 bool b_onexclude = false;
442 bool b_continue;
445 b_continue = false;
446 if ( p_container->i_size )
448 const uint64_t i_tell = vlc_stream_Tell( p_stream );
449 if( i_tell + ((b_indexed)?16:8) >= i_end )
450 break;
453 uint32_t i_index = 0;
454 if ( b_indexed )
456 uint8_t read[8];
457 if ( vlc_stream_Read( p_stream, read, 8 ) < 8 )
458 break;
459 i_index = GetDWBE(&read[4]);
461 b_onexclude = false; /* If stopped due exclude list */
462 if( (p_box = MP4_ReadBoxRestricted( p_stream, p_container, NULL, excludelist, &b_onexclude )) )
464 b_continue = true;
465 p_box->i_index = i_index;
466 for(size_t i=0; stoplist && stoplist[i]; i++)
468 if( p_box->i_type == stoplist[i] )
469 return 1;
473 const uint64_t i_tell = vlc_stream_Tell( p_stream );
474 if ( p_container->i_size && i_tell >= i_end )
476 assert( i_tell == i_end );
477 break;
480 if ( !p_box )
482 /* Continue with next if box fails to load */
483 if( i_last_pos == i_tell )
484 break;
485 i_last_pos = i_tell;
486 b_continue = true;
489 } while( b_continue );
491 /* Always move to end of container */
492 if ( !b_onexclude && p_container->i_size )
494 const uint64_t i_tell = vlc_stream_Tell( p_stream );
495 if ( i_tell != i_end )
496 MP4_Seek( p_stream, i_end );
499 return 1;
502 int MP4_ReadBoxContainerRestricted( stream_t *p_stream, MP4_Box_t *p_container,
503 const uint32_t stoplist[], const uint32_t excludelist[] )
505 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
506 stoplist, excludelist, false );
509 int MP4_ReadBoxContainerChildren( stream_t *p_stream, MP4_Box_t *p_container,
510 const uint32_t stoplist[] )
512 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
513 stoplist, NULL, false );
516 static void MP4_BoxOffsetUp( MP4_Box_t *p_box, uint64_t i_offset )
518 while(p_box)
520 p_box->i_pos += i_offset;
521 MP4_BoxOffsetUp( p_box->p_first, i_offset );
522 p_box = p_box->p_next;
526 /* Reads within an already read/in memory box (containers without having to seek) */
527 static int MP4_ReadBoxContainerRawInBox( stream_t *p_stream, MP4_Box_t *p_container,
528 uint8_t *p_buffer, uint64_t i_size, uint64_t i_offset )
530 if(!p_container)
531 return 0;
532 stream_t *p_substream = vlc_stream_MemoryNew( p_stream, p_buffer, i_size,
533 true );
534 if( !p_substream )
535 return 0;
536 MP4_Box_t *p_last = p_container->p_last;
537 MP4_ReadBoxContainerChildren( p_substream, p_container, NULL );
538 vlc_stream_Delete( p_substream );
539 /* do pos fixup */
540 if( p_container )
542 MP4_Box_t *p_box = p_last ? p_last : p_container->p_first;
543 MP4_BoxOffsetUp(p_box, i_offset);
546 return 1;
549 static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
551 if( p_container->i_size &&
552 ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
554 /* container is empty, 8 stand for the first header in this box */
555 return 1;
558 /* enter box */
559 if ( MP4_Seek( p_stream, p_container->i_pos +
560 mp4_box_headersize( p_container ) ) )
561 return 0;
562 return MP4_ReadBoxContainerChildren( p_stream, p_container, NULL );
565 static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
567 /* XXX sometime moov is hidden in a free box */
568 if( p_box->p_father &&
569 p_box->p_father->i_type == ATOM_root &&
570 p_box->i_type == ATOM_free )
572 const uint8_t *p_peek;
573 size_t header_size = mp4_box_headersize( p_box ) + 4;
574 vlc_fourcc_t i_fcc;
576 ssize_t i_read = vlc_stream_Peek( p_stream, &p_peek, 44 );
577 if( unlikely(i_read < (ssize_t)header_size) )
578 return 0;
580 p_peek += header_size;
581 i_read -= header_size;
583 if( i_read >= 8 )
585 i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
587 if( i_fcc == ATOM_cmov || i_fcc == ATOM_mvhd )
589 msg_Warn( p_stream, "detected moov hidden in a free box ..." );
591 p_box->i_type = ATOM_foov;
592 return MP4_ReadBoxContainer( p_stream, p_box );
597 /* Nothing to do */
598 #ifdef MP4_ULTRA_VERBOSE
599 if MP4_BOX_TYPE_ASCII()
600 msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
601 else
602 msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
603 #endif
604 return 1;
607 static int MP4_ReadBox_ilst( stream_t *p_stream, MP4_Box_t *p_box )
609 if( p_box->i_size < 8 || vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
610 return 0;
612 /* Find our handler */
613 if ( !p_box->i_handler && p_box->p_father )
615 const MP4_Box_t *p_sibling = p_box->p_father->p_first;
616 while( p_sibling )
618 if ( p_sibling->i_type == ATOM_hdlr && p_sibling->data.p_hdlr )
620 p_box->i_handler = p_sibling->data.p_hdlr->i_handler_type;
621 break;
623 p_sibling = p_sibling->p_next;
627 switch( p_box->i_handler )
629 case 0:
630 msg_Warn( p_stream, "no handler for ilst atom" );
631 return 0;
632 case HANDLER_mdta:
633 return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_box, NULL, NULL, true );
634 case HANDLER_mdir:
635 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
636 default:
637 msg_Warn( p_stream, "Unknown ilst handler type '%4.4s'", (char*)&p_box->i_handler );
638 return 0;
642 static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
644 FREENULL( p_box->data.p_ftyp->i_compatible_brands );
647 static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
649 MP4_READBOX_ENTER( MP4_Box_data_ftyp_t, MP4_FreeBox_ftyp );
651 MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
652 MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
654 p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4;
655 if( p_box->data.p_ftyp->i_compatible_brands_count > 0 )
657 uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
658 vlc_alloc( p_box->data.p_ftyp->i_compatible_brands_count,
659 sizeof(uint32_t) );
661 if( unlikely( tab == NULL ) )
662 MP4_READBOX_EXIT( 0 );
664 for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
666 MP4_GETFOURCC( tab[i] );
669 else
671 p_box->data.p_ftyp->i_compatible_brands = NULL;
674 MP4_READBOX_EXIT( 1 );
678 static int MP4_ReadBox_mvhd( stream_t *p_stream, MP4_Box_t *p_box )
680 #ifdef MP4_VERBOSE
681 char s_creation_time[128];
682 char s_modification_time[128];
683 char s_duration[128];
684 #endif
685 MP4_READBOX_ENTER( MP4_Box_data_mvhd_t, NULL );
687 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
689 if( p_box->data.p_mvhd->i_version )
691 MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
692 MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
693 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
694 MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
696 else
698 MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
699 MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
700 MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
701 MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
703 MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
704 MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
705 MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
708 for( unsigned i = 0; i < 2; i++ )
710 MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
712 for( unsigned i = 0; i < 9; i++ )
714 MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
716 for( unsigned i = 0; i < 6; i++ )
718 MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
721 MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
724 #ifdef MP4_VERBOSE
725 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
726 MP4_ConvertDate2Str( s_modification_time,
727 p_box->data.p_mvhd->i_modification_time, false );
728 if( p_box->data.p_mvhd->i_rate && p_box->data.p_mvhd->i_timescale )
730 MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_timescale, true );
732 else
734 s_duration[0] = 0;
736 msg_Dbg( p_stream, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
737 s_creation_time,
738 s_modification_time,
739 (uint32_t)p_box->data.p_mvhd->i_timescale,
740 s_duration,
741 (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
742 (float)p_box->data.p_mvhd->i_volume / 256 ,
743 (uint32_t)p_box->data.p_mvhd->i_next_track_id );
744 #endif
745 MP4_READBOX_EXIT( 1 );
748 static int MP4_ReadBox_mfhd( stream_t *p_stream, MP4_Box_t *p_box )
750 MP4_READBOX_ENTER( MP4_Box_data_mfhd_t, NULL );
752 MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
754 MP4_GET4BYTES( p_box->data.p_mfhd->i_sequence_number );
756 #ifdef MP4_VERBOSE
757 msg_Dbg( p_stream, "read box: \"mfhd\" sequence number %d",
758 p_box->data.p_mfhd->i_sequence_number );
759 #endif
760 MP4_READBOX_EXIT( 1 );
763 static int MP4_ReadBox_tfxd( stream_t *p_stream, MP4_Box_t *p_box )
765 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, NULL );
767 MP4_Box_data_tfxd_t *p_tfxd_data = p_box->data.p_tfxd;
768 MP4_GETVERSIONFLAGS( p_tfxd_data );
770 if( p_tfxd_data->i_version == 0 )
772 MP4_GET4BYTES( p_tfxd_data->i_fragment_abs_time );
773 MP4_GET4BYTES( p_tfxd_data->i_fragment_duration );
775 else
777 MP4_GET8BYTES( p_tfxd_data->i_fragment_abs_time );
778 MP4_GET8BYTES( p_tfxd_data->i_fragment_duration );
781 #ifdef MP4_VERBOSE
782 msg_Dbg( p_stream, "read box: \"tfxd\" version %d, flags 0x%x, "\
783 "fragment duration %"PRIu64", fragment abs time %"PRIu64,
784 p_tfxd_data->i_version,
785 p_tfxd_data->i_flags,
786 p_tfxd_data->i_fragment_duration,
787 p_tfxd_data->i_fragment_abs_time
789 #endif
791 MP4_READBOX_EXIT( 1 );
794 static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
796 FREENULL( p_box->data.p_tfrf->p_tfrf_data_fields );
799 static int MP4_ReadBox_tfrf( stream_t *p_stream, MP4_Box_t *p_box )
801 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, MP4_FreeBox_tfrf );
803 MP4_Box_data_tfrf_t *p_tfrf_data = p_box->data.p_tfrf;
804 MP4_GETVERSIONFLAGS( p_tfrf_data );
806 MP4_GET1BYTE( p_tfrf_data->i_fragment_count );
808 p_tfrf_data->p_tfrf_data_fields = calloc( p_tfrf_data->i_fragment_count,
809 sizeof( TfrfBoxDataFields_t ) );
810 if( !p_tfrf_data->p_tfrf_data_fields )
811 MP4_READBOX_EXIT( 0 );
813 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
815 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
816 if( p_tfrf_data->i_version == 0 )
818 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_abs_time );
819 MP4_GET4BYTES( TfrfBoxDataField->i_fragment_duration );
821 else
823 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_abs_time );
824 MP4_GET8BYTES( TfrfBoxDataField->i_fragment_duration );
828 #ifdef MP4_VERBOSE
829 msg_Dbg( p_stream, "read box: \"tfrf\" version %d, flags 0x%x, "\
830 "fragment count %"PRIu8, p_tfrf_data->i_version,
831 p_tfrf_data->i_flags, p_tfrf_data->i_fragment_count );
833 for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
835 TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
836 msg_Dbg( p_stream, "\"tfrf\" fragment duration %"PRIu64", "\
837 "fragment abs time %"PRIu64,
838 TfrfBoxDataField->i_fragment_duration,
839 TfrfBoxDataField->i_fragment_abs_time );
842 #endif
844 MP4_READBOX_EXIT( 1 );
847 static int MP4_ReadBox_XML360( stream_t *p_stream, MP4_Box_t *p_box )
849 MP4_READBOX_ENTER( MP4_Box_data_360_t, NULL );
851 MP4_Box_data_360_t *p_360_data = p_box->data.p_360;
853 /* Copy the string for pattern matching as it does not end
854 with a '\0' in the stream. */
855 char *psz_rdf = strndup((char *)p_peek, i_read);
857 if ( unlikely( !psz_rdf ) )
858 MP4_READBOX_EXIT( 0 );
860 /* Try to find the string "GSpherical:Spherical" because the v1
861 spherical video spec says the tag must be there. */
863 if ( strcasestr( psz_rdf, "Gspherical:Spherical" ) )
864 p_360_data->i_projection_mode = PROJECTION_MODE_EQUIRECTANGULAR;
866 /* Try to find the stero mode. */
867 if ( strcasestr( psz_rdf, "left-right" ) )
869 msg_Dbg( p_stream, "Left-right stereo mode" );
870 p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_LEFT_RIGHT;
873 if ( strcasestr( psz_rdf, "top-bottom" ) )
875 msg_Dbg( p_stream, "Top-bottom stereo mode" );
876 p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_TOP_BOTTOM;
879 free( psz_rdf );
881 MP4_READBOX_EXIT( 1 );
884 static int MP4_ReadBox_st3d( stream_t *p_stream, MP4_Box_t *p_box )
886 MP4_READBOX_ENTER( MP4_Box_data_st3d_t, NULL );
888 uint8_t i_version;
889 MP4_GET1BYTE( i_version );
890 if ( i_version != 0 )
891 MP4_READBOX_EXIT( 0 );
893 uint32_t i_flags;
894 VLC_UNUSED( i_flags );
895 MP4_GET3BYTES( i_flags );
897 MP4_Box_data_st3d_t *p_data = p_box->data.p_st3d;
898 MP4_GET1BYTE( p_data->i_stereo_mode );
900 MP4_READBOX_EXIT( 1 );
903 static int MP4_ReadBox_prhd( stream_t *p_stream, MP4_Box_t *p_box )
905 MP4_READBOX_ENTER( MP4_Box_data_prhd_t, NULL );
907 uint8_t i_version;
908 MP4_GET1BYTE( i_version );
909 if (i_version != 0)
910 MP4_READBOX_EXIT( 0 );
912 uint32_t i_flags;
913 VLC_UNUSED( i_flags );
914 MP4_GET3BYTES( i_flags );
916 MP4_Box_data_prhd_t *p_data = p_box->data.p_prhd;
917 int32_t fixed16_16;
918 MP4_GET4BYTES( fixed16_16 );
919 p_data->f_pose_yaw_degrees = (float) fixed16_16 / 65536.0f;
921 MP4_GET4BYTES( fixed16_16 );
922 p_data->f_pose_pitch_degrees = (float) fixed16_16 / 65536.0f;
924 MP4_GET4BYTES( fixed16_16 );
925 p_data->f_pose_roll_degrees = (float) fixed16_16 / 65536.0f;
927 MP4_READBOX_EXIT( 1 );
930 static int MP4_ReadBox_equi( stream_t *p_stream, MP4_Box_t *p_box )
932 MP4_READBOX_ENTER( MP4_Box_data_equi_t, NULL );
934 uint8_t i_version;
935 MP4_GET1BYTE( i_version );
936 if (i_version != 0)
937 MP4_READBOX_EXIT( 0 );
939 uint32_t i_flags;
940 VLC_UNUSED( i_flags );
941 MP4_GET3BYTES( i_flags );
943 MP4_Box_data_equi_t *p_data = p_box->data.p_equi;
944 MP4_GET4BYTES( p_data->i_projection_bounds_top );
945 MP4_GET4BYTES( p_data->i_projection_bounds_bottom );
946 MP4_GET4BYTES( p_data->i_projection_bounds_left );
947 MP4_GET4BYTES( p_data->i_projection_bounds_right );
949 MP4_READBOX_EXIT( 1 );
952 static int MP4_ReadBox_cbmp( stream_t *p_stream, MP4_Box_t *p_box )
954 MP4_READBOX_ENTER( MP4_Box_data_cbmp_t, NULL );
956 uint8_t i_version;
957 MP4_GET1BYTE( i_version );
958 if (i_version != 0)
959 MP4_READBOX_EXIT( 0 );
961 uint32_t i_flags;
962 VLC_UNUSED( i_flags );
963 MP4_GET3BYTES( i_flags );
965 MP4_Box_data_cbmp_t *p_data = p_box->data.p_cbmp;
966 MP4_GET4BYTES( p_data->i_layout );
967 MP4_GET4BYTES( p_data->i_padding );
969 MP4_READBOX_EXIT( 1 );
972 static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
974 FREENULL( p_box->data.p_sidx->p_items );
977 static int MP4_ReadBox_sidx( stream_t *p_stream, MP4_Box_t *p_box )
979 MP4_READBOX_ENTER( MP4_Box_data_sidx_t, MP4_FreeBox_sidx );
981 MP4_Box_data_sidx_t *p_sidx_data = p_box->data.p_sidx;
982 MP4_GETVERSIONFLAGS( p_sidx_data );
984 MP4_GET4BYTES( p_sidx_data->i_reference_ID );
985 MP4_GET4BYTES( p_sidx_data->i_timescale );
987 if( p_sidx_data->i_version == 0 )
989 MP4_GET4BYTES( p_sidx_data->i_earliest_presentation_time );
990 MP4_GET4BYTES( p_sidx_data->i_first_offset );
992 else
994 MP4_GET8BYTES( p_sidx_data->i_earliest_presentation_time );
995 MP4_GET8BYTES( p_sidx_data->i_first_offset );
998 uint16_t i_reserved, i_count;
1000 VLC_UNUSED(i_reserved);
1001 MP4_GET2BYTES( i_reserved );
1002 MP4_GET2BYTES( i_count );
1004 p_sidx_data->i_reference_count = i_count;
1005 p_sidx_data->p_items = vlc_alloc( i_count, sizeof( MP4_Box_sidx_item_t ) );
1006 if( unlikely(p_sidx_data->p_items == NULL) )
1007 MP4_READBOX_EXIT( 0 );
1009 for( unsigned i = 0; i < i_count; i++ )
1011 MP4_Box_sidx_item_t *item = p_sidx_data->p_items + i;
1012 uint32_t tmp;
1014 MP4_GET4BYTES( tmp );
1015 item->b_reference_type = tmp >> 31;
1016 item->i_referenced_size = tmp & 0x7fffffff;
1017 MP4_GET4BYTES( item->i_subsegment_duration );
1019 MP4_GET4BYTES( tmp );
1020 item->b_starts_with_SAP = tmp >> 31;
1021 item->i_SAP_type = (tmp >> 24) & 0x70;
1022 item->i_SAP_delta_time = tmp & 0xfffffff;
1025 #ifdef MP4_VERBOSE
1026 msg_Dbg( p_stream, "read box: \"sidx\" version %d, flags 0x%x, "\
1027 "ref_ID %"PRIu32", timescale %"PRIu32", ref_count %"PRIu16", "\
1028 "first subsegmt duration %"PRIu32,
1029 p_sidx_data->i_version,
1030 p_sidx_data->i_flags,
1031 p_sidx_data->i_reference_ID,
1032 p_sidx_data->i_timescale,
1033 p_sidx_data->i_reference_count,
1034 p_sidx_data->p_items[0].i_subsegment_duration
1036 #endif
1038 MP4_READBOX_EXIT( 1 );
1041 static int MP4_ReadBox_tfhd( stream_t *p_stream, MP4_Box_t *p_box )
1043 MP4_READBOX_ENTER( MP4_Box_data_tfhd_t, NULL );
1045 MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );
1047 if( p_box->data.p_tfhd->i_version != 0 )
1049 msg_Warn( p_stream, "'tfhd' box with version != 0. "\
1050 " Don't know what to do with that, please patch" );
1051 MP4_READBOX_EXIT( 0 );
1054 MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );
1056 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
1058 msg_Dbg( p_stream, "'duration-is-empty' flag is present "\
1059 "=> no samples for this time interval." );
1060 p_box->data.p_tfhd->b_empty = true;
1062 else
1063 p_box->data.p_tfhd->b_empty = false;
1065 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
1066 MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
1067 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
1068 MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
1069 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
1070 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
1071 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
1072 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
1073 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
1074 MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
1076 #ifdef MP4_VERBOSE
1077 char psz_base[128] = "\0";
1078 char psz_desc[128] = "\0";
1079 char psz_dura[128] = "\0";
1080 char psz_size[128] = "\0";
1081 char psz_flag[128] = "\0";
1082 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
1083 snprintf(psz_base, sizeof(psz_base), "base offset %"PRId64, p_box->data.p_tfhd->i_base_data_offset);
1084 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
1085 snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
1086 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
1087 snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
1088 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
1089 snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
1090 if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
1091 snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);
1093 msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
1094 p_box->data.p_tfhd->i_version,
1095 p_box->data.p_tfhd->i_flags,
1096 p_box->data.p_tfhd->i_track_ID,
1097 psz_base, psz_desc, psz_dura, psz_size, psz_flag );
1098 #endif
1100 MP4_READBOX_EXIT( 1 );
1103 static void MP4_FreeBox_trun( MP4_Box_t *p_box )
1105 FREENULL( p_box->data.p_trun->p_samples );
1108 static int MP4_ReadBox_trun( stream_t *p_stream, MP4_Box_t *p_box )
1110 uint32_t count;
1112 MP4_READBOX_ENTER( MP4_Box_data_trun_t, MP4_FreeBox_trun );
1114 MP4_GETVERSIONFLAGS( p_box->data.p_trun );
1115 MP4_GET4BYTES( count );
1117 if( p_box->data.p_trun->i_flags & MP4_TRUN_DATA_OFFSET )
1118 MP4_GET4BYTES( p_box->data.p_trun->i_data_offset );
1119 if( p_box->data.p_trun->i_flags & MP4_TRUN_FIRST_FLAGS )
1120 MP4_GET4BYTES( p_box->data.p_trun->i_first_sample_flags );
1122 if( UINT64_C(16) * count > i_read )
1123 MP4_READBOX_EXIT( 0 );
1125 p_box->data.p_trun->p_samples = vlc_alloc( count,
1126 sizeof(MP4_descriptor_trun_sample_t) );
1127 if ( p_box->data.p_trun->p_samples == NULL )
1128 MP4_READBOX_EXIT( 0 );
1129 p_box->data.p_trun->i_sample_count = count;
1131 for( unsigned int i = 0; i < count; i++ )
1133 MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
1134 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
1135 MP4_GET4BYTES( p_sample->i_duration );
1136 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
1137 MP4_GET4BYTES( p_sample->i_size );
1138 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS )
1139 MP4_GET4BYTES( p_sample->i_flags );
1140 if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
1141 MP4_GET4BYTES( p_sample->i_composition_time_offset.v0 );
1144 #ifdef MP4_ULTRA_VERBOSE
1145 msg_Dbg( p_stream, "read box: \"trun\" version %u flags 0x%x sample count %"PRIu32,
1146 p_box->data.p_trun->i_version,
1147 p_box->data.p_trun->i_flags,
1148 p_box->data.p_trun->i_sample_count );
1150 for( unsigned int i = 0; i < count; i++ )
1152 MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
1153 msg_Dbg( p_stream, "read box: \"trun\" sample %4.4u flags 0x%x "\
1154 "duration %"PRIu32" size %"PRIu32" composition time offset %"PRIu32,
1155 i, p_sample->i_flags, p_sample->i_duration,
1156 p_sample->i_size, p_sample->i_composition_time_offset );
1158 #endif
1160 MP4_READBOX_EXIT( 1 );
1163 static int MP4_ReadBox_tfdt( stream_t *p_stream, MP4_Box_t *p_box )
1165 MP4_READBOX_ENTER( MP4_Box_data_tfdt_t, NULL );
1166 if( i_read < 8 )
1167 MP4_READBOX_EXIT( 0 );
1169 MP4_GETVERSIONFLAGS( p_box->data.p_tfdt );
1171 if( p_box->data.p_tfdt->i_version == 0 )
1172 MP4_GET4BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1173 else if( p_box->data.p_tfdt->i_version == 1 )
1174 MP4_GET8BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1175 else
1176 MP4_READBOX_EXIT( 0 );
1178 MP4_READBOX_EXIT( 1 );
1181 static int MP4_ReadBox_tkhd( stream_t *p_stream, MP4_Box_t *p_box )
1183 #ifdef MP4_VERBOSE
1184 char s_creation_time[128];
1185 char s_modification_time[128];
1186 char s_duration[128];
1187 #endif
1188 MP4_READBOX_ENTER( MP4_Box_data_tkhd_t, NULL );
1190 MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
1192 if( p_box->data.p_tkhd->i_version )
1194 MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
1195 MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
1196 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1197 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1198 MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
1200 else
1202 MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
1203 MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
1204 MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1205 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1206 MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
1209 for( unsigned i = 0; i < 2; i++ )
1211 MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
1213 MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
1214 MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
1215 MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
1216 MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
1218 for( unsigned i = 0; i < 9; i++ )
1220 MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
1222 MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
1223 MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
1225 double rotation = 0;//angle in degrees to be rotated clockwise
1226 double scale[2]; // scale factor; sx = scale[0] , sy = scale[1]
1227 int32_t *matrix = p_box->data.p_tkhd->i_matrix;
1229 scale[0] = sqrt(conv_fx(matrix[0]) * conv_fx(matrix[0]) +
1230 conv_fx(matrix[3]) * conv_fx(matrix[3]));
1231 scale[1] = sqrt(conv_fx(matrix[1]) * conv_fx(matrix[1]) +
1232 conv_fx(matrix[4]) * conv_fx(matrix[4]));
1234 if( likely(scale[0] > 0 && scale[1] > 0) )
1236 rotation = atan2(conv_fx(matrix[1]) / scale[1],
1237 conv_fx(matrix[0]) / scale[0]) * 180 / M_PI;
1238 if (rotation < 0)
1239 rotation += 360.;
1242 p_box->data.p_tkhd->f_rotation = rotation;
1244 #ifdef MP4_VERBOSE
1245 double translate[2];// amount to translate; tx = translate[0] , ty = translate[1]
1247 translate[0] = conv_fx(matrix[6]);
1248 translate[1] = conv_fx(matrix[7]);
1250 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
1251 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time, false );
1252 MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration, true );
1254 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. "
1255 "Matrix: %i %i %i %i %i %i %i %i %i",
1256 s_creation_time,
1257 s_modification_time,
1258 s_duration,
1259 p_box->data.p_tkhd->i_track_ID,
1260 p_box->data.p_tkhd->i_layer,
1261 (float)p_box->data.p_tkhd->i_volume / 256 ,
1262 rotation,
1263 scale[0],
1264 scale[1],
1265 translate[0],
1266 translate[1],
1267 (float)p_box->data.p_tkhd->i_width / BLOCK16x16,
1268 (float)p_box->data.p_tkhd->i_height / BLOCK16x16,
1269 p_box->data.p_tkhd->i_matrix[0],
1270 p_box->data.p_tkhd->i_matrix[1],
1271 p_box->data.p_tkhd->i_matrix[2],
1272 p_box->data.p_tkhd->i_matrix[3],
1273 p_box->data.p_tkhd->i_matrix[4],
1274 p_box->data.p_tkhd->i_matrix[5],
1275 p_box->data.p_tkhd->i_matrix[6],
1276 p_box->data.p_tkhd->i_matrix[7],
1277 p_box->data.p_tkhd->i_matrix[8] );
1278 #endif
1279 MP4_READBOX_EXIT( 1 );
1282 static int MP4_ReadBox_load( stream_t *p_stream, MP4_Box_t *p_box )
1284 if ( p_box->i_size != 24 )
1285 return 0;
1286 MP4_READBOX_ENTER( MP4_Box_data_load_t, NULL );
1287 MP4_GET4BYTES( p_box->data.p_load->i_start_time );
1288 MP4_GET4BYTES( p_box->data.p_load->i_duration );
1289 MP4_GET4BYTES( p_box->data.p_load->i_flags );
1290 MP4_GET4BYTES( p_box->data.p_load->i_hints );
1291 MP4_READBOX_EXIT( 1 );
1294 static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
1296 uint16_t i_language;
1297 #ifdef MP4_VERBOSE
1298 char s_creation_time[128];
1299 char s_modification_time[128];
1300 char s_duration[128];
1301 #endif
1302 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t, NULL );
1304 MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
1306 if( p_box->data.p_mdhd->i_version )
1308 MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
1309 MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
1310 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1311 MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
1313 else
1315 MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
1316 MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
1317 MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1318 MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
1321 MP4_GET2BYTES( i_language );
1322 decodeQtLanguageCode( i_language, p_box->data.p_mdhd->rgs_language,
1323 &p_box->data.p_mdhd->b_mac_encoding );
1325 MP4_GET2BYTES( p_box->data.p_mdhd->i_quality );
1327 #ifdef MP4_VERBOSE
1328 MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time, false );
1329 MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time, false );
1330 MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration, true );
1331 msg_Dbg( p_stream, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %3.3s",
1332 s_creation_time,
1333 s_modification_time,
1334 (uint32_t)p_box->data.p_mdhd->i_timescale,
1335 s_duration,
1336 (char*) &p_box->data.p_mdhd->rgs_language );
1337 #endif
1338 MP4_READBOX_EXIT( 1 );
1341 static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
1343 FREENULL( p_box->data.p_hdlr->psz_name );
1346 static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
1348 int32_t i_reserved;
1349 VLC_UNUSED(i_reserved);
1351 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t, MP4_FreeBox_hdlr );
1353 MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
1355 MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
1356 MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
1358 MP4_GET4BYTES( i_reserved );
1359 MP4_GET4BYTES( i_reserved );
1360 MP4_GET4BYTES( i_reserved );
1361 p_box->data.p_hdlr->psz_name = NULL;
1363 if( i_read > 0 )
1365 uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_read + 1 );
1366 if( unlikely( psz == NULL ) )
1367 MP4_READBOX_EXIT( 0 );
1369 /* Yes, I love .mp4 :( */
1370 if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
1372 uint8_t i_len;
1373 int i_copy;
1375 MP4_GET1BYTE( i_len );
1376 i_copy = __MIN( i_read, i_len );
1378 memcpy( psz, p_peek, i_copy );
1379 p_box->data.p_hdlr->psz_name[i_copy] = '\0';
1381 else
1383 memcpy( psz, p_peek, i_read );
1384 p_box->data.p_hdlr->psz_name[i_read] = '\0';
1388 #ifdef MP4_VERBOSE
1389 msg_Dbg( p_stream, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
1390 (char*)&p_box->data.p_hdlr->i_handler_type,
1391 p_box->data.p_hdlr->psz_name );
1393 #endif
1394 MP4_READBOX_EXIT( 1 );
1397 static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
1399 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t, NULL );
1401 MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
1403 MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
1404 for( unsigned i = 0; i < 3; i++ )
1406 MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
1409 #ifdef MP4_VERBOSE
1410 msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
1411 p_box->data.p_vmhd->i_graphics_mode,
1412 p_box->data.p_vmhd->i_opcolor[0],
1413 p_box->data.p_vmhd->i_opcolor[1],
1414 p_box->data.p_vmhd->i_opcolor[2] );
1415 #endif
1416 MP4_READBOX_EXIT( 1 );
1419 static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
1421 MP4_READBOX_ENTER( MP4_Box_data_smhd_t, NULL );
1423 MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
1427 MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
1429 MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
1431 #ifdef MP4_VERBOSE
1432 msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
1433 (float)p_box->data.p_smhd->i_balance / 256 );
1434 #endif
1435 MP4_READBOX_EXIT( 1 );
1439 static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
1441 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t, NULL );
1443 MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
1445 MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
1446 MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
1448 MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
1449 MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
1451 MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
1453 #ifdef MP4_VERBOSE
1454 msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1455 p_box->data.p_hmhd->i_max_PDU_size,
1456 p_box->data.p_hmhd->i_avg_PDU_size,
1457 p_box->data.p_hmhd->i_max_bitrate,
1458 p_box->data.p_hmhd->i_avg_bitrate );
1459 #endif
1460 MP4_READBOX_EXIT( 1 );
1463 static void MP4_FreeBox_url( MP4_Box_t *p_box )
1465 FREENULL( p_box->data.p_url->psz_location );
1468 static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
1470 MP4_READBOX_ENTER( MP4_Box_data_url_t, MP4_FreeBox_url );
1472 MP4_GETVERSIONFLAGS( p_box->data.p_url );
1473 MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
1475 #ifdef MP4_VERBOSE
1476 msg_Dbg( p_stream, "read box: \"url\" url: %s",
1477 p_box->data.p_url->psz_location );
1479 #endif
1480 MP4_READBOX_EXIT( 1 );
1483 static void MP4_FreeBox_urn( MP4_Box_t *p_box )
1485 FREENULL( p_box->data.p_urn->psz_name );
1486 FREENULL( p_box->data.p_urn->psz_location );
1489 static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
1491 MP4_READBOX_ENTER( MP4_Box_data_urn_t, MP4_FreeBox_urn );
1493 MP4_GETVERSIONFLAGS( p_box->data.p_urn );
1495 MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
1496 MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
1498 #ifdef MP4_VERBOSE
1499 msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
1500 p_box->data.p_urn->psz_name,
1501 p_box->data.p_urn->psz_location );
1502 #endif
1503 MP4_READBOX_EXIT( 1 );
1506 static int MP4_ReadBox_LtdContainer( stream_t *p_stream, MP4_Box_t *p_box )
1508 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_lcont_t, 16, NULL );
1509 if( i_read < 8 )
1510 MP4_READBOX_EXIT( 0 );
1512 MP4_GETVERSIONFLAGS( p_box->data.p_lcont );
1513 if( p_box->data.p_lcont->i_version != 0 )
1514 MP4_READBOX_EXIT( 0 );
1515 MP4_GET4BYTES( p_box->data.p_lcont->i_entry_count );
1517 uint32_t i_entry = 0;
1518 i_read = p_box->i_size - 16;
1519 while (i_read > 8 && i_entry < p_box->data.p_lcont->i_entry_count )
1521 MP4_Box_t *p_childbox = MP4_ReadBox( p_stream, p_box );
1522 if( !p_childbox )
1523 break;
1524 MP4_BoxAddChild( p_box, p_childbox );
1525 i_entry++;
1526 i_read -= p_childbox->i_size;
1529 if (i_entry != p_box->data.p_lcont->i_entry_count)
1530 p_box->data.p_lcont->i_entry_count = i_entry;
1532 #ifdef MP4_VERBOSE
1533 msg_Dbg( p_stream, "read box: \"%4.4s\" entry-count %d", (char *)&p_box->i_type,
1534 p_box->data.p_lcont->i_entry_count );
1536 #endif
1538 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
1539 MP4_READBOX_EXIT( 0 );
1541 MP4_READBOX_EXIT( 1 );
1544 static void MP4_FreeBox_stts( MP4_Box_t *p_box )
1546 FREENULL( p_box->data.p_stts->pi_sample_count );
1547 FREENULL( p_box->data.p_stts->pi_sample_delta );
1550 static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
1552 uint32_t count;
1554 MP4_READBOX_ENTER( MP4_Box_data_stts_t, MP4_FreeBox_stts );
1556 MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1557 MP4_GET4BYTES( count );
1559 if( UINT64_C(8) * count > i_read )
1561 /*count = i_read / 8;*/
1562 MP4_READBOX_EXIT( 0 );
1565 p_box->data.p_stts->pi_sample_count = vlc_alloc( count, sizeof(uint32_t) );
1566 p_box->data.p_stts->pi_sample_delta = vlc_alloc( count, sizeof(int32_t) );
1567 p_box->data.p_stts->i_entry_count = count;
1569 if( p_box->data.p_stts->pi_sample_count == NULL
1570 || p_box->data.p_stts->pi_sample_delta == NULL )
1572 MP4_READBOX_EXIT( 0 );
1575 for( uint32_t i = 0; i < count; i++ )
1577 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_count[i] );
1578 MP4_GET4BYTES( p_box->data.p_stts->pi_sample_delta[i] );
1581 #ifdef MP4_VERBOSE
1582 msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
1583 p_box->data.p_stts->i_entry_count );
1585 #endif
1586 MP4_READBOX_EXIT( 1 );
1590 static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
1592 FREENULL( p_box->data.p_ctts->pi_sample_count );
1593 FREENULL( p_box->data.p_ctts->pi_sample_offset );
1596 static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
1598 uint32_t count;
1600 MP4_READBOX_ENTER( MP4_Box_data_ctts_t, MP4_FreeBox_ctts );
1602 MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1603 MP4_GET4BYTES( count );
1605 if( UINT64_C(8) * count > i_read )
1606 MP4_READBOX_EXIT( 0 );
1608 p_box->data.p_ctts->pi_sample_count = vlc_alloc( count, sizeof(uint32_t) );
1609 p_box->data.p_ctts->pi_sample_offset = vlc_alloc( count, sizeof(int32_t) );
1610 if( unlikely(p_box->data.p_ctts->pi_sample_count == NULL
1611 || p_box->data.p_ctts->pi_sample_offset == NULL) )
1612 MP4_READBOX_EXIT( 0 );
1613 p_box->data.p_ctts->i_entry_count = count;
1615 for( uint32_t i = 0; i < count; i++ )
1617 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_count[i] );
1618 MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_offset[i] );
1621 #ifdef MP4_VERBOSE
1622 msg_Dbg( p_stream, "read box: \"ctts\" entry-count %"PRIu32, count );
1624 #endif
1625 MP4_READBOX_EXIT( 1 );
1628 static int MP4_ReadBox_cslg( stream_t *p_stream, MP4_Box_t *p_box )
1630 MP4_READBOX_ENTER( MP4_Box_data_cslg_t, NULL );
1632 unsigned i_version, i_flags;
1633 MP4_GET1BYTE( i_version );
1634 MP4_GET3BYTES( i_flags );
1635 VLC_UNUSED(i_flags);
1637 if( i_version > 1 )
1638 MP4_READBOX_EXIT( 0 );
1640 #define READ_CSLG(readbytes) {\
1641 readbytes( p_box->data.p_cslg->ct_to_dts_shift );\
1642 readbytes( p_box->data.p_cslg->i_least_delta );\
1643 readbytes( p_box->data.p_cslg->i_max_delta );\
1644 readbytes( p_box->data.p_cslg->i_composition_starttime );\
1645 readbytes( p_box->data.p_cslg->i_composition_endtime ); }
1647 if( i_version == 0 )
1648 READ_CSLG(MP4_GET4BYTES)
1649 else
1650 READ_CSLG(MP4_GET8BYTES)
1652 MP4_READBOX_EXIT( 1 );
1655 static uint64_t MP4_ReadLengthDescriptor( uint8_t **restrict bufp,
1656 uint64_t *restrict lenp )
1658 unsigned char *buf = *bufp;
1659 uint64_t len = *lenp;
1660 unsigned char b;
1661 uint64_t value = 0;
1665 if (unlikely(len == 0))
1666 return -1; /* end of bit stream */
1667 if (unlikely(value > (UINT64_MAX >> 7)))
1668 return -1; /* integer overflow */
1670 b = *(buf++);
1671 len--;
1672 value = (value << 7) + (b & 0x7f);
1674 while (b & 0x80);
1676 *bufp = buf;
1677 *lenp = len;
1678 return value;
1682 static void MP4_FreeBox_esds( MP4_Box_t *p_box )
1684 FREENULL( p_box->data.p_esds->es_descriptor.psz_URL );
1685 if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1687 FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1688 FREENULL( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1692 static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
1694 #define es_descriptor p_box->data.p_esds->es_descriptor
1695 uint64_t i_len;
1696 unsigned int i_flags;
1697 unsigned int i_type;
1699 MP4_READBOX_ENTER( MP4_Box_data_esds_t, MP4_FreeBox_esds );
1701 MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1704 MP4_GET1BYTE( i_type );
1705 if( i_type == 0x03 ) /* MP4ESDescrTag ISO/IEC 14496-1 8.3.3 */
1707 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1708 if( unlikely(i_len == UINT64_C(-1)) )
1709 MP4_READBOX_EXIT( 0 );
1711 #ifdef MP4_VERBOSE
1712 msg_Dbg( p_stream, "found esds MPEG4ESDescr (%"PRIu64" bytes)",
1713 i_len );
1714 #endif
1716 MP4_GET2BYTES( es_descriptor.i_ES_ID );
1717 MP4_GET1BYTE( i_flags );
1718 es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1719 es_descriptor.b_url = ( (i_flags&0x40) != 0);
1720 es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1722 es_descriptor.i_stream_priority = i_flags&0x1f;
1723 if( es_descriptor.b_stream_dependence )
1725 MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1727 if( es_descriptor.b_url && i_read > 0 )
1729 uint8_t i_url;
1731 MP4_GET1BYTE( i_url );
1732 if( i_url > i_read )
1733 MP4_READBOX_EXIT( 1 );
1734 es_descriptor.psz_URL = malloc( (unsigned) i_url + 1 );
1735 if( es_descriptor.psz_URL )
1737 memcpy( es_descriptor.psz_URL, p_peek, i_url );
1738 es_descriptor.psz_URL[i_url] = 0;
1740 p_peek += i_url;
1741 i_read -= i_url;
1743 else
1745 es_descriptor.psz_URL = NULL;
1747 if( es_descriptor.b_OCRstream )
1749 MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1751 MP4_GET1BYTE( i_type ); /* get next type */
1754 if( i_type != 0x04)/* MP4DecConfigDescrTag ISO/IEC 14496-1 8.3.4 */
1756 es_descriptor.p_decConfigDescr = NULL;
1757 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1760 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1761 if( unlikely(i_len == UINT64_C(-1)) )
1762 MP4_READBOX_EXIT( 0 );
1763 #ifdef MP4_VERBOSE
1764 msg_Dbg( p_stream, "found esds MP4DecConfigDescr (%"PRIu64" bytes)",
1765 i_len );
1766 #endif
1768 es_descriptor.p_decConfigDescr =
1769 calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
1770 if( unlikely( es_descriptor.p_decConfigDescr == NULL ) )
1771 MP4_READBOX_EXIT( 0 );
1773 MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectProfileIndication );
1774 MP4_GET1BYTE( i_flags );
1775 es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1776 es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1777 MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1778 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1779 MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1780 MP4_GET1BYTE( i_type );
1781 if( i_type != 0x05 )/* MP4DecSpecificDescrTag ISO/IEC 14496-1 8.3.5 */
1783 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1784 es_descriptor.p_decConfigDescr->p_decoder_specific_info = NULL;
1785 MP4_READBOX_EXIT( 1 );
1788 i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1789 if( unlikely(i_len == UINT64_C(-1)) )
1790 MP4_READBOX_EXIT( 0 );
1791 #ifdef MP4_VERBOSE
1792 msg_Dbg( p_stream, "found esds MP4DecSpecificDescr (%"PRIu64" bytes)",
1793 i_len );
1794 #endif
1795 if( i_len > i_read )
1796 MP4_READBOX_EXIT( 0 );
1798 es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1799 es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1800 if( unlikely( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL ) )
1801 MP4_READBOX_EXIT( 0 );
1803 memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1804 p_peek, i_len );
1806 MP4_READBOX_EXIT( 1 );
1807 #undef es_descriptor
1810 static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
1812 MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1813 int i;
1815 if( p_avcC->i_avcC > 0 ) FREENULL( p_avcC->p_avcC );
1817 if( p_avcC->sps )
1819 for( i = 0; i < p_avcC->i_sps; i++ )
1820 FREENULL( p_avcC->sps[i] );
1822 if( p_avcC->pps )
1824 for( i = 0; i < p_avcC->i_pps; i++ )
1825 FREENULL( p_avcC->pps[i] );
1827 if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->sps );
1828 if( p_avcC->i_sps > 0 ) FREENULL( p_avcC->i_sps_length );
1829 if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->pps );
1830 if( p_avcC->i_pps > 0 ) FREENULL( p_avcC->i_pps_length );
1833 static int MP4_ReadBox_avcC( stream_t *p_stream, MP4_Box_t *p_box )
1835 MP4_Box_data_avcC_t *p_avcC;
1836 int i;
1838 MP4_READBOX_ENTER( MP4_Box_data_avcC_t, MP4_FreeBox_avcC );
1839 p_avcC = p_box->data.p_avcC;
1841 p_avcC->i_avcC = i_read;
1842 if( p_avcC->i_avcC > 0 )
1844 uint8_t * p = p_avcC->p_avcC = malloc( p_avcC->i_avcC );
1845 if( p )
1846 memcpy( p, p_peek, i_read );
1849 MP4_GET1BYTE( p_avcC->i_version );
1850 MP4_GET1BYTE( p_avcC->i_profile );
1851 MP4_GET1BYTE( p_avcC->i_profile_compatibility );
1852 MP4_GET1BYTE( p_avcC->i_level );
1853 MP4_GET1BYTE( p_avcC->i_reserved1 );
1854 p_avcC->i_length_size = (p_avcC->i_reserved1&0x03) + 1;
1855 p_avcC->i_reserved1 >>= 2;
1857 MP4_GET1BYTE( p_avcC->i_reserved2 );
1858 p_avcC->i_sps = p_avcC->i_reserved2&0x1f;
1859 p_avcC->i_reserved2 >>= 5;
1861 if( p_avcC->i_sps > 0 )
1863 p_avcC->i_sps_length = calloc( p_avcC->i_sps, sizeof( uint16_t ) );
1864 p_avcC->sps = calloc( p_avcC->i_sps, sizeof( uint8_t* ) );
1866 if( !p_avcC->i_sps_length || !p_avcC->sps )
1867 goto error;
1869 for( i = 0; i < p_avcC->i_sps && i_read > 2; i++ )
1871 MP4_GET2BYTES( p_avcC->i_sps_length[i] );
1872 if ( p_avcC->i_sps_length[i] > i_read )
1873 goto error;
1874 p_avcC->sps[i] = malloc( p_avcC->i_sps_length[i] );
1875 if( p_avcC->sps[i] )
1876 memcpy( p_avcC->sps[i], p_peek, p_avcC->i_sps_length[i] );
1878 p_peek += p_avcC->i_sps_length[i];
1879 i_read -= p_avcC->i_sps_length[i];
1881 if ( i != p_avcC->i_sps )
1882 goto error;
1885 MP4_GET1BYTE( p_avcC->i_pps );
1886 if( p_avcC->i_pps > 0 )
1888 p_avcC->i_pps_length = calloc( p_avcC->i_pps, sizeof( uint16_t ) );
1889 p_avcC->pps = calloc( p_avcC->i_pps, sizeof( uint8_t* ) );
1891 if( !p_avcC->i_pps_length || !p_avcC->pps )
1892 goto error;
1894 for( i = 0; i < p_avcC->i_pps && i_read > 2; i++ )
1896 MP4_GET2BYTES( p_avcC->i_pps_length[i] );
1897 if( p_avcC->i_pps_length[i] > i_read )
1898 goto error;
1899 p_avcC->pps[i] = malloc( p_avcC->i_pps_length[i] );
1900 if( p_avcC->pps[i] )
1901 memcpy( p_avcC->pps[i], p_peek, p_avcC->i_pps_length[i] );
1903 p_peek += p_avcC->i_pps_length[i];
1904 i_read -= p_avcC->i_pps_length[i];
1906 if ( i != p_avcC->i_pps )
1907 goto error;
1909 #ifdef MP4_VERBOSE
1910 msg_Dbg( p_stream,
1911 "read box: \"avcC\" version=%d profile=0x%x level=0x%x length size=%d sps=%d pps=%d",
1912 p_avcC->i_version, p_avcC->i_profile, p_avcC->i_level,
1913 p_avcC->i_length_size,
1914 p_avcC->i_sps, p_avcC->i_pps );
1915 for( i = 0; i < p_avcC->i_sps; i++ )
1917 msg_Dbg( p_stream, " - sps[%d] length=%d",
1918 i, p_avcC->i_sps_length[i] );
1920 for( i = 0; i < p_avcC->i_pps; i++ )
1922 msg_Dbg( p_stream, " - pps[%d] length=%d",
1923 i, p_avcC->i_pps_length[i] );
1926 #endif
1927 MP4_READBOX_EXIT( 1 );
1929 error:
1930 MP4_FreeBox_avcC( p_box );
1931 MP4_READBOX_EXIT( 0 );
1934 static void MP4_FreeBox_vpcC( MP4_Box_t *p_box )
1936 free( p_box->data.p_vpcC->p_codec_init_data );
1939 static int MP4_ReadBox_vpcC( stream_t *p_stream, MP4_Box_t *p_box )
1941 MP4_READBOX_ENTER( MP4_Box_data_vpcC_t, MP4_FreeBox_vpcC );
1942 MP4_Box_data_vpcC_t *p_vpcC = p_box->data.p_vpcC;
1944 if( p_box->i_size < 6 )
1945 MP4_READBOX_EXIT( 0 );
1947 uint8_t i_version;
1948 MP4_GET1BYTE( i_version );
1949 if( i_version != 0 )
1950 MP4_READBOX_EXIT( 0 );
1952 MP4_GET1BYTE( p_vpcC->i_profile );
1953 MP4_GET1BYTE( p_vpcC->i_level );
1954 MP4_GET1BYTE( p_vpcC->i_bit_depth );
1955 p_vpcC->i_color_space = p_vpcC->i_bit_depth & 0x0F;
1956 p_vpcC->i_bit_depth >>= 4;
1957 MP4_GET1BYTE( p_vpcC->i_chroma_subsampling );
1958 p_vpcC->i_xfer_function = ( p_vpcC->i_chroma_subsampling & 0x0F ) >> 1;
1959 p_vpcC->i_fullrange = p_vpcC->i_chroma_subsampling & 0x01;
1960 p_vpcC->i_chroma_subsampling >>= 4;
1961 MP4_GET2BYTES( p_vpcC->i_codec_init_datasize );
1962 if( p_vpcC->i_codec_init_datasize > i_read )
1963 p_vpcC->i_codec_init_datasize = i_read;
1965 if( p_vpcC->i_codec_init_datasize )
1967 p_vpcC->p_codec_init_data = malloc( i_read );
1968 if( !p_vpcC->p_codec_init_data )
1969 MP4_READBOX_EXIT( 0 );
1970 memcpy( p_vpcC->p_codec_init_data, p_peek, i_read );
1973 MP4_READBOX_EXIT( 1 );
1976 static void MP4_FreeBox_WMA2( MP4_Box_t *p_box )
1978 FREENULL( p_box->data.p_WMA2->p_extra );
1981 static int MP4_ReadBox_WMA2( stream_t *p_stream, MP4_Box_t *p_box )
1983 MP4_READBOX_ENTER( MP4_Box_data_WMA2_t, MP4_FreeBox_WMA2 );
1985 MP4_Box_data_WMA2_t *p_WMA2 = p_box->data.p_WMA2;
1987 MP4_GET2BYTESLE( p_WMA2->Format.wFormatTag );
1988 MP4_GET2BYTESLE( p_WMA2->Format.nChannels );
1989 MP4_GET4BYTESLE( p_WMA2->Format.nSamplesPerSec );
1990 MP4_GET4BYTESLE( p_WMA2->Format.nAvgBytesPerSec );
1991 MP4_GET2BYTESLE( p_WMA2->Format.nBlockAlign );
1992 MP4_GET2BYTESLE( p_WMA2->Format.wBitsPerSample );
1994 uint16_t i_cbSize;
1995 MP4_GET2BYTESLE( i_cbSize );
1997 if( i_cbSize > i_read )
1998 goto error;
2000 p_WMA2->i_extra = i_cbSize;
2001 if ( p_WMA2->i_extra )
2003 p_WMA2->p_extra = malloc( p_WMA2->i_extra );
2004 if ( ! p_WMA2->p_extra )
2005 goto error;
2006 memcpy( p_WMA2->p_extra, p_peek, p_WMA2->i_extra );
2009 MP4_READBOX_EXIT( 1 );
2011 error:
2012 MP4_READBOX_EXIT( 0 );
2015 static void MP4_FreeBox_strf( MP4_Box_t *p_box )
2017 FREENULL( p_box->data.p_strf->p_extra );
2020 static int MP4_ReadBox_strf( stream_t *p_stream, MP4_Box_t *p_box )
2022 MP4_READBOX_ENTER( MP4_Box_data_strf_t, MP4_FreeBox_strf );
2024 MP4_Box_data_strf_t *p_strf = p_box->data.p_strf;
2026 if( i_read < 40 )
2027 goto error;
2029 MP4_GET4BYTESLE( p_strf->bmiHeader.biSize );
2030 MP4_GET4BYTESLE( p_strf->bmiHeader.biWidth );
2031 MP4_GET4BYTESLE( p_strf->bmiHeader.biHeight );
2032 MP4_GET2BYTESLE( p_strf->bmiHeader.biPlanes );
2033 MP4_GET2BYTESLE( p_strf->bmiHeader.biBitCount );
2034 MP4_GETFOURCC( p_strf->bmiHeader.biCompression );
2035 MP4_GET4BYTESLE( p_strf->bmiHeader.biSizeImage );
2036 MP4_GET4BYTESLE( p_strf->bmiHeader.biXPelsPerMeter );
2037 MP4_GET4BYTESLE( p_strf->bmiHeader.biYPelsPerMeter );
2038 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrUsed );
2039 MP4_GET4BYTESLE( p_strf->bmiHeader.biClrImportant );
2041 p_strf->i_extra = i_read;
2042 if ( p_strf->i_extra )
2044 p_strf->p_extra = malloc( p_strf->i_extra );
2045 if ( ! p_strf->p_extra )
2046 goto error;
2047 memcpy( p_strf->p_extra, p_peek, i_read );
2050 MP4_READBOX_EXIT( 1 );
2052 error:
2053 MP4_READBOX_EXIT( 0 );
2056 static int MP4_ReadBox_ASF( stream_t *p_stream, MP4_Box_t *p_box )
2058 MP4_READBOX_ENTER( MP4_Box_data_ASF_t, NULL );
2060 MP4_Box_data_ASF_t *p_asf = p_box->data.p_asf;
2062 if (i_read != 8)
2063 MP4_READBOX_EXIT( 0 );
2065 MP4_GET1BYTE( p_asf->i_stream_number );
2066 /* remaining is unknown */
2068 MP4_READBOX_EXIT( 1 );
2071 static void MP4_FreeBox_sbgp( MP4_Box_t *p_box )
2073 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2074 free( p_sbgp->entries.pi_sample_count );
2075 free( p_sbgp->entries.pi_group_description_index );
2078 static int MP4_ReadBox_sbgp( stream_t *p_stream, MP4_Box_t *p_box )
2080 MP4_READBOX_ENTER( MP4_Box_data_sbgp_t, MP4_FreeBox_sbgp );
2081 MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2082 uint32_t i_flags;
2084 if ( i_read < 12 )
2085 MP4_READBOX_EXIT( 0 );
2087 MP4_GET1BYTE( p_sbgp->i_version );
2088 MP4_GET3BYTES( i_flags );
2089 if( i_flags != 0 )
2090 MP4_READBOX_EXIT( 0 );
2092 MP4_GETFOURCC( p_sbgp->i_grouping_type );
2094 if( p_sbgp->i_version == 1 )
2096 if( i_read < 8 )
2097 MP4_READBOX_EXIT( 0 );
2098 MP4_GET4BYTES( p_sbgp->i_grouping_type_parameter );
2101 MP4_GET4BYTES( p_sbgp->i_entry_count );
2102 if( p_sbgp->i_entry_count > i_read / (4 + 4) )
2103 p_sbgp->i_entry_count = i_read / (4 + 4);
2105 p_sbgp->entries.pi_sample_count = vlc_alloc( p_sbgp->i_entry_count, sizeof(uint32_t) );
2106 p_sbgp->entries.pi_group_description_index = vlc_alloc( p_sbgp->i_entry_count, sizeof(uint32_t) );
2108 if( !p_sbgp->entries.pi_sample_count || !p_sbgp->entries.pi_group_description_index )
2110 MP4_FreeBox_sbgp( p_box );
2111 MP4_READBOX_EXIT( 0 );
2114 for( uint32_t i=0; i<p_sbgp->i_entry_count; i++ )
2116 MP4_GET4BYTES( p_sbgp->entries.pi_sample_count[i] );
2117 MP4_GET4BYTES( p_sbgp->entries.pi_group_description_index[i] );
2120 #ifdef MP4_VERBOSE
2121 msg_Dbg( p_stream,
2122 "read box: \"sbgp\" grouping type %4.4s", (char*) &p_sbgp->i_grouping_type );
2123 #ifdef MP4_ULTRA_VERBOSE
2124 for (uint32_t i = 0; i < p_sbgp->i_entry_count; i++)
2125 msg_Dbg( p_stream, "\t samples %" PRIu32 " group %" PRIu32,
2126 p_sbgp->entries.pi_sample_count[i],
2127 p_sbgp->entries.pi_group_description_index[i] );
2128 #endif
2129 #endif
2131 MP4_READBOX_EXIT( 1 );
2134 static void MP4_FreeBox_sgpd( MP4_Box_t *p_box )
2136 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2137 free( p_sgpd->p_entries );
2140 static int MP4_ReadBox_sgpd( stream_t *p_stream, MP4_Box_t *p_box )
2142 MP4_READBOX_ENTER( MP4_Box_data_sgpd_t, MP4_FreeBox_sgpd );
2143 MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2144 uint32_t i_flags;
2145 uint32_t i_default_length = 0;
2147 if ( i_read < 8 )
2148 MP4_READBOX_EXIT( 0 );
2150 MP4_GET1BYTE( p_sgpd->i_version );
2151 MP4_GET3BYTES( i_flags );
2152 if( i_flags != 0 )
2153 MP4_READBOX_EXIT( 0 );
2155 MP4_GETFOURCC( p_sgpd->i_grouping_type );
2157 switch( p_sgpd->i_grouping_type )
2159 case SAMPLEGROUP_rap:
2160 break;
2162 default:
2163 #ifdef MP4_VERBOSE
2164 msg_Dbg( p_stream,
2165 "read box: \"sgpd\" grouping type %4.4s (unimplemented)", (char*) &p_sgpd->i_grouping_type );
2166 #endif
2167 MP4_READBOX_EXIT( 1 );
2170 if( p_sgpd->i_version == 1 )
2172 if( i_read < 8 )
2173 MP4_READBOX_EXIT( 0 );
2174 MP4_GET4BYTES( i_default_length );
2176 else if( p_sgpd->i_version >= 2 )
2178 if( i_read < 8 )
2179 MP4_READBOX_EXIT( 0 );
2180 MP4_GET4BYTES( p_sgpd->i_default_sample_description_index );
2183 MP4_GET4BYTES( p_sgpd->i_entry_count );
2185 p_sgpd->p_entries = vlc_alloc( p_sgpd->i_entry_count, sizeof(*p_sgpd->p_entries) );
2186 if( !p_sgpd->p_entries )
2187 MP4_READBOX_EXIT( 0 );
2189 uint32_t i = 0;
2190 for( ; i<p_sgpd->i_entry_count; i++ )
2192 uint32_t i_description_length = i_default_length;
2193 if( p_sgpd->i_version == 1 && i_default_length == 0 )
2195 if( i_read < 4 )
2196 break;
2197 MP4_GET4BYTES( i_description_length );
2200 if( p_sgpd->i_version == 1 && i_read < i_description_length )
2201 break;
2203 switch( p_sgpd->i_grouping_type )
2205 case SAMPLEGROUP_rap:
2207 if( i_read < 1 )
2209 p_sgpd->i_entry_count = 0;
2210 MP4_FreeBox_sgpd( p_box );
2211 MP4_READBOX_EXIT( 0 );
2213 uint8_t i_data;
2214 MP4_GET1BYTE( i_data );
2215 p_sgpd->p_entries[i].rap.i_num_leading_samples_known = i_data & 0x80;
2216 p_sgpd->p_entries[i].rap.i_num_leading_samples = i_data & 0x7F;
2218 break;
2220 default:
2221 assert(0);
2225 if( i != p_sgpd->i_entry_count )
2226 p_sgpd->i_entry_count = i;
2228 #ifdef MP4_VERBOSE
2229 msg_Dbg( p_stream,
2230 "read box: \"sgpd\" grouping type %4.4s", (char*) &p_sgpd->i_grouping_type );
2231 #endif
2233 MP4_READBOX_EXIT( 1 );
2236 static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
2238 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2239 free( p_chan->layout.p_descriptions );
2242 static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
2244 MP4_READBOX_ENTER( MP4_Box_data_chan_t, MP4_FreeBox_stsdext_chan );
2245 MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2247 if ( i_read < 16 )
2248 MP4_READBOX_EXIT( 0 );
2250 MP4_GET1BYTE( p_chan->i_version );
2251 MP4_GET3BYTES( p_chan->i_channels_flags );
2252 MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
2253 MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
2254 MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
2256 size_t i_descsize = 8 + 3 * sizeof(float);
2257 if ( i_read < p_chan->layout.i_channels_description_count * i_descsize )
2258 MP4_READBOX_EXIT( 0 );
2260 p_chan->layout.p_descriptions =
2261 vlc_alloc( p_chan->layout.i_channels_description_count, i_descsize );
2263 if ( !p_chan->layout.p_descriptions )
2264 MP4_READBOX_EXIT( 0 );
2266 uint32_t i;
2267 for( i=0; i<p_chan->layout.i_channels_description_count; i++ )
2269 if ( i_read < 20 )
2270 break;
2271 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
2272 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
2273 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
2274 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
2275 MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
2277 if ( i<p_chan->layout.i_channels_description_count )
2278 p_chan->layout.i_channels_description_count = i;
2280 #ifdef MP4_VERBOSE
2281 msg_Dbg( p_stream,
2282 "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
2283 p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
2284 p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
2285 #endif
2286 MP4_READBOX_EXIT( 1 );
2289 static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
2291 MP4_READBOX_ENTER( MP4_Box_data_dec3_t, NULL );
2293 MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
2295 unsigned i_header;
2296 MP4_GET2BYTES( i_header );
2298 p_dec3->i_data_rate = i_header >> 3;
2299 p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
2300 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
2301 MP4_GET3BYTES( i_header );
2302 p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
2303 p_dec3->stream[i].i_bsid = ( i_header >> 17 ) & 0x01f;
2304 p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
2305 p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
2306 p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
2307 p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
2308 if (p_dec3->stream[i].i_num_dep_sub) {
2309 MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
2310 p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
2311 } else
2312 p_dec3->stream[i].i_chan_loc = 0;
2315 #ifdef MP4_VERBOSE
2316 msg_Dbg( p_stream,
2317 "read box: \"dec3\" bitrate %dkbps %d independent substreams",
2318 p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
2320 for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
2321 msg_Dbg( p_stream,
2322 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
2323 "num dependent subs=%d chan_loc=0x%x",
2324 i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
2325 p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
2326 #endif
2327 MP4_READBOX_EXIT( 1 );
2330 static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
2332 MP4_Box_data_dac3_t *p_dac3;
2333 MP4_READBOX_ENTER( MP4_Box_data_dac3_t, NULL );
2335 p_dac3 = p_box->data.p_dac3;
2337 unsigned i_header;
2338 MP4_GET3BYTES( i_header );
2340 p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
2341 p_dac3->i_bsid = ( i_header >> 17 ) & 0x01f;
2342 p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
2343 p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
2344 p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
2345 p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
2347 #ifdef MP4_VERBOSE
2348 msg_Dbg( p_stream,
2349 "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
2350 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 );
2351 #endif
2352 MP4_READBOX_EXIT( 1 );
2355 static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
2357 MP4_READBOX_ENTER( MP4_Box_data_dvc1_t, NULL );
2358 if( i_read < 7 )
2359 MP4_READBOX_EXIT( 0 );
2361 MP4_Box_data_dvc1_t *p_dvc1 = p_box->data.p_dvc1;
2362 MP4_GET1BYTE( p_dvc1->i_profile_level );
2363 p_dvc1->i_vc1 = i_read; /* Header + profile_level */
2364 if( p_dvc1->i_vc1 > 0 && (p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 )) )
2365 memcpy( p_dvc1->p_vc1, p_peek, i_read );
2367 #ifdef MP4_VERBOSE
2368 msg_Dbg( p_stream,
2369 "read box: \"dvc1\" profile=%"PRIu8, (p_dvc1->i_profile_level & 0xf0) >> 4 );
2370 #endif
2372 MP4_READBOX_EXIT( 1 );
2375 static int MP4_ReadBox_fiel( stream_t *p_stream, MP4_Box_t *p_box )
2377 MP4_Box_data_fiel_t *p_fiel;
2378 MP4_READBOX_ENTER( MP4_Box_data_fiel_t, NULL );
2379 p_fiel = p_box->data.p_fiel;
2380 if(i_read < 2)
2381 MP4_READBOX_EXIT( 0 );
2382 if(p_peek[0] == 1)
2384 p_fiel->i_flags = BLOCK_FLAG_SINGLE_FIELD;
2386 else if(p_peek[0] == 2) /* Interlaced */
2389 * 0 – There is only one field.
2390 * 1 – T is displayed earliest, T is stored first in the file.
2391 * 6 – B is displayed earliest, B is stored first in the file.
2392 * 9 – B is displayed earliest, T is stored first in the file.
2393 * 14 – T is displayed earliest, B is stored first in the file.
2395 if(p_peek[1] == 1 || p_peek[1] == 9)
2396 p_fiel->i_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
2397 else if(p_peek[1] == 6 || p_peek[1] == 14)
2398 p_fiel->i_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
2400 MP4_READBOX_EXIT( 1 );
2403 static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
2405 MP4_Box_data_enda_t *p_enda;
2406 MP4_READBOX_ENTER( MP4_Box_data_enda_t, NULL );
2408 p_enda = p_box->data.p_enda;
2410 MP4_GET2BYTES( p_enda->i_little_endian );
2412 #ifdef MP4_VERBOSE
2413 msg_Dbg( p_stream,
2414 "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
2415 #endif
2416 MP4_READBOX_EXIT( 1 );
2419 static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
2421 FREENULL( p_box->data.p_sample_soun->p_qt_description );
2424 static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
2426 p_box->i_handler = ATOM_soun;
2427 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t, MP4_FreeBox_sample_soun );
2428 p_box->data.p_sample_soun->p_qt_description = NULL;
2430 size_t i_actually_read = i_read + header_size;
2432 /* Sanity check needed because the "wave" box does also contain an
2433 * "mp4a" box that we don't understand. */
2434 if( i_read < 28 )
2436 MP4_READBOX_EXIT( 1 );
2439 for( unsigned i = 0; i < 6 ; i++ )
2441 MP4_GET1BYTE( p_box->data.p_sample_soun->i_reserved1[i] );
2444 MP4_GET2BYTES( p_box->data.p_sample_soun->i_data_reference_index );
2447 * XXX hack -> produce a copy of the nearly complete chunk
2449 p_box->data.p_sample_soun->i_qt_description = 0;
2450 p_box->data.p_sample_soun->p_qt_description = NULL;
2451 if( i_read > 0 )
2453 p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
2454 if( p_box->data.p_sample_soun->p_qt_description )
2456 p_box->data.p_sample_soun->i_qt_description = i_read;
2457 memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
2461 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
2462 MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
2463 MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
2465 MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
2466 MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
2467 MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
2468 MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
2469 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
2470 MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
2472 #ifdef MP4_VERBOSE
2473 msg_Dbg( p_stream,
2474 "read box: \"soun\" stsd qt_version %"PRIu16" compid=%"PRIx16,
2475 p_box->data.p_sample_soun->i_qt_version,
2476 p_box->data.p_sample_soun->i_compressionid );
2477 #endif
2478 /* @36 bytes */
2479 if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
2481 /* SoundDescriptionV1 */
2482 MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
2483 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
2484 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
2485 MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
2487 #ifdef MP4_VERBOSE
2488 msg_Dbg( p_stream,
2489 "read box: \"soun\" V1 sample/packet=%d bytes/packet=%d "
2490 "bytes/frame=%d bytes/sample=%d",
2491 p_box->data.p_sample_soun->i_sample_per_packet,
2492 p_box->data.p_sample_soun->i_bytes_per_packet,
2493 p_box->data.p_sample_soun->i_bytes_per_frame,
2494 p_box->data.p_sample_soun->i_bytes_per_sample );
2495 #endif
2496 /* @52 bytes */
2498 else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
2500 /* SoundDescriptionV2 */
2501 double f_sample_rate;
2502 int64_t i_dummy64;
2503 uint32_t i_channel, i_extoffset, i_dummy32;
2505 /* Checks */
2506 if ( p_box->data.p_sample_soun->i_channelcount != 0x3 ||
2507 p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
2508 p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
2509 p_box->data.p_sample_soun->i_reserved3 != 0x0 ||
2510 p_box->data.p_sample_soun->i_sampleratehi != 0x1 ||//65536
2511 p_box->data.p_sample_soun->i_sampleratelo != 0x0 ) //remainder
2513 msg_Err( p_stream, "invalid stsd V2 box defaults" );
2514 MP4_READBOX_EXIT( 0 );
2516 /* !Checks */
2518 MP4_GET4BYTES( i_extoffset ); /* offset to stsd extentions */
2519 MP4_GET8BYTES( i_dummy64 );
2520 memcpy( &f_sample_rate, &i_dummy64, 8 );
2521 msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
2522 /* Rounding error with lo, but we don't care since we do not support fractional audio rate */
2523 p_box->data.p_sample_soun->i_sampleratehi = (uint32_t)f_sample_rate;
2524 p_box->data.p_sample_soun->i_sampleratelo = (f_sample_rate - p_box->data.p_sample_soun->i_sampleratehi);
2526 MP4_GET4BYTES( i_channel );
2527 p_box->data.p_sample_soun->i_channelcount = i_channel;
2529 MP4_GET4BYTES( i_dummy32 );
2530 if ( i_dummy32 != 0x7F000000 )
2532 msg_Err( p_stream, "invalid stsd V2 box" );
2533 MP4_READBOX_EXIT( 0 );
2536 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
2537 MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
2538 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
2539 MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2541 #ifdef MP4_VERBOSE
2542 msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
2543 "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
2544 f_sample_rate,
2545 p_box->data.p_sample_soun->i_constbitsperchannel,
2546 p_box->data.p_sample_soun->i_formatflags,
2547 p_box->data.p_sample_soun->i_constbytesperaudiopacket,
2548 p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2549 #endif
2550 /* @72 bytes + */
2551 if( i_extoffset > i_actually_read )
2552 i_extoffset = i_actually_read;
2553 p_peek = &p_buff[i_extoffset];
2554 i_read = i_actually_read - i_extoffset;
2556 else
2558 p_box->data.p_sample_soun->i_sample_per_packet = 0;
2559 p_box->data.p_sample_soun->i_bytes_per_packet = 0;
2560 p_box->data.p_sample_soun->i_bytes_per_frame = 0;
2561 p_box->data.p_sample_soun->i_bytes_per_sample = 0;
2563 #ifdef MP4_VERBOSE
2564 msg_Dbg( p_stream, "read box: \"soun\" V0 or qt1/2 (rest=%"PRIu64")",
2565 i_read );
2566 #endif
2567 /* @36 bytes */
2570 if( p_box->i_type == ATOM_drms )
2572 msg_Warn( p_stream, "DRM protected streams are not supported." );
2573 MP4_READBOX_EXIT( 0 );
2576 if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
2578 /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
2579 p_box->data.p_sample_soun->i_channelcount = 1;
2582 /* Loads extensions */
2583 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2584 p_box->i_pos + p_peek - p_buff ); /* esds/wave/... */
2586 #ifdef MP4_VERBOSE
2587 msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
2588 "sample size %d sample rate %f",
2589 p_box->data.p_sample_soun->i_channelcount,
2590 p_box->data.p_sample_soun->i_samplesize,
2591 (float)p_box->data.p_sample_soun->i_sampleratehi +
2592 (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
2594 #endif
2595 MP4_READBOX_EXIT( 1 );
2598 static void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
2600 FREENULL( p_box->data.p_sample_vide->p_qt_image_description );
2603 int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
2605 p_box->i_handler = ATOM_vide;
2606 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t, MP4_FreeBox_sample_vide );
2608 size_t i_actually_read = i_read + header_size;
2610 for( unsigned i = 0; i < 6 ; i++ )
2612 MP4_GET1BYTE( p_box->data.p_sample_vide->i_reserved1[i] );
2615 MP4_GET2BYTES( p_box->data.p_sample_vide->i_data_reference_index );
2618 * XXX hack -> produce a copy of the nearly complete chunk
2620 if( i_read > 0 )
2622 p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
2623 if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
2624 MP4_READBOX_EXIT( 0 );
2625 p_box->data.p_sample_vide->i_qt_image_description = i_read;
2626 memcpy( p_box->data.p_sample_vide->p_qt_image_description,
2627 p_peek, i_read );
2629 else
2631 p_box->data.p_sample_vide->i_qt_image_description = 0;
2632 p_box->data.p_sample_vide->p_qt_image_description = NULL;
2635 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
2636 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
2637 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
2639 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
2640 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
2642 MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
2643 MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
2645 MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
2646 MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
2648 MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_data_size );
2649 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
2651 if ( i_read < 32 )
2652 MP4_READBOX_EXIT( 0 );
2653 if( p_peek[0] <= 31 ) // Must be Pascal String
2655 memcpy( &p_box->data.p_sample_vide->sz_compressorname, &p_peek[1], p_peek[0] );
2656 p_box->data.p_sample_vide->sz_compressorname[p_peek[0]] = 0;
2658 p_peek += 32; i_read -= 32;
2660 MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
2661 MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
2663 if( p_box->i_type == ATOM_drmi )
2665 msg_Warn( p_stream, "DRM protected streams are not supported." );
2666 MP4_READBOX_EXIT( 0 );
2669 if( i_actually_read > 78 && p_peek - p_buff > 78 )
2671 MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2672 p_box->i_pos + p_peek - p_buff );
2675 #ifdef MP4_VERBOSE
2676 msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d (%s)",
2677 p_box->data.p_sample_vide->i_width,
2678 p_box->data.p_sample_vide->i_height,
2679 p_box->data.p_sample_vide->i_depth,
2680 p_box->data.p_sample_vide->sz_compressorname );
2682 #endif
2683 MP4_READBOX_EXIT( 1 );
2686 static int MP4_ReadBox_sample_mp4s( stream_t *p_stream, MP4_Box_t *p_box )
2688 p_box->i_handler = ATOM_text;
2689 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_text_t, 16, NULL );
2690 (void) p_peek;
2691 if( i_read < 8 )
2692 MP4_READBOX_EXIT( 0 );
2694 MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
2696 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2697 MP4_READBOX_EXIT( 0 );
2699 MP4_READBOX_EXIT( 1 );
2702 static void MP4_FreeBox_sample_hint( MP4_Box_t *p_box )
2704 FREENULL( p_box->data.p_sample_hint->p_data );
2707 static int MP4_ReadBox_sample_hint8( stream_t *p_stream, MP4_Box_t *p_box )
2709 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_hint_t, 24, MP4_FreeBox_sample_hint );
2711 for( unsigned i = 0; i < 6 ; i++ )
2713 MP4_GET1BYTE( p_box->data.p_sample_hint->i_reserved1[i] );
2716 MP4_GET2BYTES( p_box->data.p_sample_hint->i_data_reference_index );
2718 if( !(p_box->data.p_sample_hint->p_data = malloc(8)) )
2719 MP4_READBOX_EXIT( 0 );
2721 MP4_GET8BYTES( *(p_box->data.p_sample_hint->p_data) );
2723 MP4_ReadBoxContainerChildren(p_stream, p_box, NULL);
2725 if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2726 MP4_READBOX_EXIT( 0 );
2728 MP4_READBOX_EXIT( 1 );
2731 static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
2733 int32_t t;
2735 p_box->i_handler = ATOM_text;
2736 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t, NULL );
2738 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2739 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2741 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2743 MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2745 MP4_GET4BYTES( t );
2746 switch( t )
2748 /* FIXME search right signification */
2749 case 1: // Center
2750 p_box->data.p_sample_text->i_justification_horizontal = 1;
2751 p_box->data.p_sample_text->i_justification_vertical = 1;
2752 break;
2753 case -1: // Flush Right
2754 p_box->data.p_sample_text->i_justification_horizontal = -1;
2755 p_box->data.p_sample_text->i_justification_vertical = -1;
2756 break;
2757 case -2: // Flush Left
2758 p_box->data.p_sample_text->i_justification_horizontal = 0;
2759 p_box->data.p_sample_text->i_justification_vertical = 0;
2760 break;
2761 case 0: // Flush Default
2762 default:
2763 p_box->data.p_sample_text->i_justification_horizontal = 1;
2764 p_box->data.p_sample_text->i_justification_vertical = -1;
2765 break;
2768 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[0] );
2769 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[1] );
2770 MP4_GET2BYTES( p_box->data.p_sample_text->i_background_color[2] );
2771 p_box->data.p_sample_text->i_background_color[3] = 0xFF;
2773 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2774 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2775 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2776 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2778 #ifdef MP4_VERBOSE
2779 msg_Dbg( p_stream, "read box: \"text\" in stsd text" );
2780 #endif
2781 MP4_READBOX_EXIT( 1 );
2784 static int MP4_ReadBox_sample_clcp( stream_t *p_stream, MP4_Box_t *p_box )
2786 p_box->i_handler = ATOM_clcp;
2787 MP4_READBOX_ENTER( MP4_Box_data_sample_clcp_t, NULL );
2789 if( i_read < 8 )
2790 MP4_READBOX_EXIT( 0 );
2792 for( int i=0; i<6; i++ )
2793 MP4_GET1BYTE( p_box->data.p_sample_clcp->i_reserved1[i] );
2794 MP4_GET2BYTES( p_box->data.p_sample_clcp->i_data_reference_index );
2796 #ifdef MP4_VERBOSE
2797 msg_Dbg( p_stream, "read box: \"clcp\" in stsd" );
2798 #endif
2799 MP4_READBOX_EXIT( 1 );
2802 static int MP4_ReadBox_sample_tx3g( stream_t *p_stream, MP4_Box_t *p_box )
2804 p_box->i_handler = ATOM_text;
2805 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t, NULL );
2807 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved1 );
2808 MP4_GET2BYTES( p_box->data.p_sample_text->i_reserved2 );
2810 MP4_GET2BYTES( p_box->data.p_sample_text->i_data_reference_index );
2812 MP4_GET4BYTES( p_box->data.p_sample_text->i_display_flags );
2814 MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_horizontal );
2815 MP4_GET1BYTE ( p_box->data.p_sample_text->i_justification_vertical );
2817 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[0] );
2818 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[1] );
2819 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[2] );
2820 MP4_GET1BYTE ( p_box->data.p_sample_text->i_background_color[3] );
2822 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_top );
2823 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_left );
2824 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_bottom );
2825 MP4_GET2BYTES( p_box->data.p_sample_text->i_text_box_right );
2827 MP4_GET4BYTES( p_box->data.p_sample_text->i_reserved3 );
2829 MP4_GET2BYTES( p_box->data.p_sample_text->i_font_id );
2830 MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_face );
2831 MP4_GET1BYTE ( p_box->data.p_sample_text->i_font_size );
2832 MP4_GET4BYTES( p_box->data.p_sample_text->i_font_color );
2834 #ifdef MP4_VERBOSE
2835 msg_Dbg( p_stream, "read box: \"tx3g\" in stsd text" );
2836 #endif
2837 MP4_READBOX_EXIT( 1 );
2841 #if 0
2842 /* We can't easily call it, and anyway ~ 20 bytes lost isn't a real problem */
2843 static void MP4_FreeBox_sample_text( MP4_Box_t *p_box )
2845 FREENULL( p_box->data.p_sample_text->psz_text_name );
2847 #endif
2849 static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
2851 FREENULL( p_box->data.p_stsz->i_entry_size );
2854 static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
2856 uint32_t count;
2858 MP4_READBOX_ENTER( MP4_Box_data_stsz_t, MP4_FreeBox_stsz );
2860 MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
2862 MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
2863 MP4_GET4BYTES( count );
2864 p_box->data.p_stsz->i_sample_count = count;
2866 if( p_box->data.p_stsz->i_sample_size == 0 )
2868 if( UINT64_C(4) * count > i_read )
2869 MP4_READBOX_EXIT( 0 );
2871 p_box->data.p_stsz->i_entry_size =
2872 vlc_alloc( count, sizeof(uint32_t) );
2873 if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
2874 MP4_READBOX_EXIT( 0 );
2876 for( uint32_t i = 0; i < count; i++ )
2878 MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
2881 else
2882 p_box->data.p_stsz->i_entry_size = NULL;
2884 #ifdef MP4_VERBOSE
2885 msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
2886 p_box->data.p_stsz->i_sample_size,
2887 p_box->data.p_stsz->i_sample_count );
2889 #endif
2890 MP4_READBOX_EXIT( 1 );
2893 static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
2895 FREENULL( p_box->data.p_stsc->i_first_chunk );
2896 FREENULL( p_box->data.p_stsc->i_samples_per_chunk );
2897 FREENULL( p_box->data.p_stsc->i_sample_description_index );
2900 static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
2902 uint32_t count;
2904 MP4_READBOX_ENTER( MP4_Box_data_stsc_t, MP4_FreeBox_stsc );
2906 MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
2907 MP4_GET4BYTES( count );
2909 if( UINT64_C(12) * count > i_read )
2910 MP4_READBOX_EXIT( 0 );
2912 p_box->data.p_stsc->i_first_chunk = vlc_alloc( count, sizeof(uint32_t) );
2913 p_box->data.p_stsc->i_samples_per_chunk = vlc_alloc( count,
2914 sizeof(uint32_t) );
2915 p_box->data.p_stsc->i_sample_description_index = vlc_alloc( count,
2916 sizeof(uint32_t) );
2917 if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
2918 || p_box->data.p_stsc->i_samples_per_chunk == NULL
2919 || p_box->data.p_stsc->i_sample_description_index == NULL ) )
2921 MP4_READBOX_EXIT( 0 );
2923 p_box->data.p_stsc->i_entry_count = count;
2925 for( uint32_t i = 0; i < count;i++ )
2927 MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
2928 MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
2929 MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
2932 #ifdef MP4_VERBOSE
2933 msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
2934 p_box->data.p_stsc->i_entry_count );
2936 #endif
2937 MP4_READBOX_EXIT( 1 );
2940 static void MP4_FreeBox_sdp( MP4_Box_t *p_box )
2942 FREENULL( p_box->data.p_sdp->psz_text );
2945 static int MP4_ReadBox_sdp( stream_t *p_stream, MP4_Box_t *p_box )
2947 MP4_READBOX_ENTER( MP4_Box_data_sdp_t, MP4_FreeBox_sdp );
2949 MP4_GETSTRINGZ( p_box->data.p_sdp->psz_text );
2951 MP4_READBOX_EXIT( 1 );
2954 static void MP4_FreeBox_rtp( MP4_Box_t *p_box )
2956 FREENULL( p_box->data.p_moviehintinformation_rtp->psz_text );
2959 static int MP4_ReadBox_rtp( stream_t *p_stream, MP4_Box_t *p_box )
2961 MP4_READBOX_ENTER( MP4_Box_data_moviehintinformation_rtp_t, MP4_FreeBox_rtp );
2963 MP4_GET4BYTES( p_box->data.p_moviehintinformation_rtp->i_description_format );
2965 MP4_GETSTRINGZ( p_box->data.p_moviehintinformation_rtp->psz_text );
2967 MP4_READBOX_EXIT( 1 );
2970 static int MP4_ReadBox_tims( stream_t *p_stream, MP4_Box_t *p_box )
2972 MP4_READBOX_ENTER( MP4_Box_data_tims_t, NULL );
2974 MP4_GET4BYTES( p_box->data.p_tims->i_timescale );
2976 MP4_READBOX_EXIT( 1 );
2979 static int MP4_ReadBox_tsro( stream_t *p_stream, MP4_Box_t *p_box )
2981 MP4_READBOX_ENTER( MP4_Box_data_tsro_t, NULL );
2983 MP4_GET4BYTES( p_box->data.p_tsro->i_offset );
2985 MP4_READBOX_EXIT( 1 );
2988 static int MP4_ReadBox_tssy( stream_t *p_stream, MP4_Box_t *p_box )
2990 MP4_READBOX_ENTER( MP4_Box_data_tssy_t, NULL );
2992 MP4_GET1BYTE( p_box->data.p_tssy->i_reserved_timestamp_sync );
2994 MP4_READBOX_EXIT( 1 );
2997 static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
2999 FREENULL( p_box->data.p_co64->i_chunk_offset );
3002 static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
3004 const bool sixtyfour = p_box->i_type != ATOM_stco;
3005 uint32_t count;
3007 MP4_READBOX_ENTER( MP4_Box_data_co64_t, MP4_FreeBox_stco_co64 );
3009 MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
3010 MP4_GET4BYTES( count );
3012 if( (sixtyfour ? UINT64_C(8) : UINT64_C(4)) * count > i_read )
3013 MP4_READBOX_EXIT( 0 );
3015 p_box->data.p_co64->i_chunk_offset = vlc_alloc( count, sizeof(uint64_t) );
3016 if( unlikely(p_box->data.p_co64->i_chunk_offset == NULL) )
3017 MP4_READBOX_EXIT( 0 );
3018 p_box->data.p_co64->i_entry_count = count;
3020 for( uint32_t i = 0; i < count; i++ )
3022 if( sixtyfour )
3023 MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
3024 else
3025 MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
3028 #ifdef MP4_VERBOSE
3029 msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
3030 p_box->data.p_co64->i_entry_count );
3032 #endif
3033 MP4_READBOX_EXIT( 1 );
3036 static void MP4_FreeBox_stss( MP4_Box_t *p_box )
3038 FREENULL( p_box->data.p_stss->i_sample_number );
3041 static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
3043 uint32_t count;
3045 MP4_READBOX_ENTER( MP4_Box_data_stss_t, MP4_FreeBox_stss );
3047 MP4_GETVERSIONFLAGS( p_box->data.p_stss );
3048 MP4_GET4BYTES( count );
3050 if( UINT64_C(4) * count > i_read )
3051 MP4_READBOX_EXIT( 0 );
3053 p_box->data.p_stss->i_sample_number = vlc_alloc( count, sizeof(uint32_t) );
3054 if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
3055 MP4_READBOX_EXIT( 0 );
3056 p_box->data.p_stss->i_entry_count = count;
3058 for( uint32_t i = 0; i < count; i++ )
3060 MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
3061 /* XXX in libmp4 sample begin at 0 */
3062 p_box->data.p_stss->i_sample_number[i]--;
3065 #ifdef MP4_VERBOSE
3066 msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
3067 p_box->data.p_stss->i_entry_count );
3069 #endif
3070 MP4_READBOX_EXIT( 1 );
3073 static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
3075 FREENULL( p_box->data.p_stsh->i_shadowed_sample_number );
3076 FREENULL( p_box->data.p_stsh->i_sync_sample_number );
3079 static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
3081 uint32_t count;
3083 MP4_READBOX_ENTER( MP4_Box_data_stsh_t, MP4_FreeBox_stsh );
3085 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
3086 MP4_GET4BYTES( count );
3088 if( UINT64_C(8) * count > i_read )
3089 MP4_READBOX_EXIT( 0 );
3091 p_box->data.p_stsh->i_shadowed_sample_number = vlc_alloc( count,
3092 sizeof(uint32_t) );
3093 p_box->data.p_stsh->i_sync_sample_number = vlc_alloc( count,
3094 sizeof(uint32_t) );
3095 if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
3096 || p_box->data.p_stsh->i_sync_sample_number == NULL )
3097 MP4_READBOX_EXIT( 0 );
3098 p_box->data.p_stsh->i_entry_count = count;
3100 for( uint32_t i = 0; i < p_box->data.p_stss->i_entry_count; i++ )
3102 MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
3103 MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
3106 #ifdef MP4_VERBOSE
3107 msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
3108 p_box->data.p_stsh->i_entry_count );
3109 #endif
3110 MP4_READBOX_EXIT( 1 );
3113 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
3115 FREENULL( p_box->data.p_stdp->i_priority );
3118 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
3120 MP4_READBOX_ENTER( MP4_Box_data_stdp_t, MP4_FreeBox_stdp );
3122 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
3124 p_box->data.p_stdp->i_priority =
3125 calloc( i_read / 2, sizeof(uint16_t) );
3127 if( unlikely( !p_box->data.p_stdp->i_priority ) )
3128 MP4_READBOX_EXIT( 0 );
3130 for( unsigned i = 0; i < i_read / 2 ; i++ )
3132 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
3135 #ifdef MP4_VERBOSE
3136 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
3137 i_read / 2 );
3139 #endif
3140 MP4_READBOX_EXIT( 1 );
3143 static void MP4_FreeBox_padb( MP4_Box_t *p_box )
3145 FREENULL( p_box->data.p_padb->i_reserved );
3146 FREENULL( p_box->data.p_padb->i_pad );
3149 static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
3151 uint32_t count;
3153 MP4_READBOX_ENTER( MP4_Box_data_padb_t, MP4_FreeBox_padb );
3155 MP4_GETVERSIONFLAGS( p_box->data.p_padb );
3156 MP4_GET4BYTES( count );
3158 if( ((count / 2) + (count & 1)) > i_read )
3160 MP4_READBOX_EXIT( 0 );
3163 p_box->data.p_padb->i_reserved = malloc( count );
3164 p_box->data.p_padb->i_pad = malloc( count );
3165 p_box->data.p_padb->i_sample_count = count;
3167 if( unlikely(p_box->data.p_padb->i_reserved == NULL
3168 || p_box->data.p_padb->i_pad == NULL) )
3170 MP4_READBOX_EXIT( 0 );
3173 for( size_t i = 0; i < count; i += 2 )
3175 p_box->data.p_padb->i_reserved[i] = ( (*p_peek) >> 7 )&0x01;
3176 p_box->data.p_padb->i_pad[i + 1] = ( (*p_peek) >> 4 )&0x07;
3177 p_box->data.p_padb->i_reserved[i + 1] = ( (*p_peek) >> 3 )&0x01;
3178 p_box->data.p_padb->i_pad[i] = ( (*p_peek) )&0x07;
3180 p_peek++;
3181 i_read--;
3184 #ifdef MP4_VERBOSE
3185 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRIu64,
3186 i_read / 2 );
3188 #endif
3189 MP4_READBOX_EXIT( 1 );
3192 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
3194 FREENULL( p_box->data.p_elst->i_segment_duration );
3195 FREENULL( p_box->data.p_elst->i_media_time );
3196 FREENULL( p_box->data.p_elst->i_media_rate_integer );
3197 FREENULL( p_box->data.p_elst->i_media_rate_fraction );
3200 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
3202 MP4_READBOX_ENTER( MP4_Box_data_elst_t, MP4_FreeBox_elst );
3204 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
3207 MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
3209 uint32_t i_entries_max = i_read / ((p_box->data.p_elst->i_version == 1) ? 20 : 12);
3210 if( p_box->data.p_elst->i_entry_count > i_entries_max )
3211 p_box->data.p_elst->i_entry_count = i_entries_max;
3213 p_box->data.p_elst->i_segment_duration =
3214 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
3215 p_box->data.p_elst->i_media_time =
3216 calloc( p_box->data.p_elst->i_entry_count, sizeof(int64_t) );
3217 p_box->data.p_elst->i_media_rate_integer =
3218 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
3219 p_box->data.p_elst->i_media_rate_fraction =
3220 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
3221 if( p_box->data.p_elst->i_segment_duration == NULL
3222 || p_box->data.p_elst->i_media_time == NULL
3223 || p_box->data.p_elst->i_media_rate_integer == NULL
3224 || p_box->data.p_elst->i_media_rate_fraction == NULL )
3226 MP4_READBOX_EXIT( 0 );
3229 for( uint32_t i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
3231 uint64_t segment_duration;
3232 int64_t media_time;
3234 if( p_box->data.p_elst->i_version == 1 )
3236 union { int64_t s; uint64_t u; } u;
3238 MP4_GET8BYTES( segment_duration );
3239 MP4_GET8BYTES( u.u );
3240 media_time = u.s;
3242 else
3244 union { int32_t s; uint32_t u; } u;
3246 MP4_GET4BYTES( segment_duration );
3247 MP4_GET4BYTES( u.u );
3248 media_time = u.s;
3251 p_box->data.p_elst->i_segment_duration[i] = segment_duration;
3252 p_box->data.p_elst->i_media_time[i] = media_time;
3253 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
3254 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
3257 #ifdef MP4_VERBOSE
3258 msg_Dbg( p_stream, "read box: \"elst\" entry-count %" PRIu32,
3259 p_box->data.p_elst->i_entry_count );
3260 #endif
3261 MP4_READBOX_EXIT( 1 );
3264 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
3266 FREENULL( p_box->data.p_cprt->psz_notice );
3269 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
3271 uint16_t i_language;
3272 bool b_mac;
3274 MP4_READBOX_ENTER( MP4_Box_data_cprt_t, MP4_FreeBox_cprt );
3276 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
3278 MP4_GET2BYTES( i_language );
3279 decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
3281 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
3283 #ifdef MP4_VERBOSE
3284 msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
3285 p_box->data.p_cprt->rgs_language,
3286 p_box->data.p_cprt->psz_notice );
3288 #endif
3289 MP4_READBOX_EXIT( 1 );
3292 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
3294 MP4_READBOX_ENTER( MP4_Box_data_dcom_t, NULL );
3296 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
3297 #ifdef MP4_VERBOSE
3298 msg_Dbg( p_stream,
3299 "read box: \"dcom\" compression algorithm : %4.4s",
3300 (char*)&p_box->data.p_dcom->i_algorithm );
3301 #endif
3302 MP4_READBOX_EXIT( 1 );
3305 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
3307 FREENULL( p_box->data.p_cmvd->p_data );
3310 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
3312 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t, MP4_FreeBox_cmvd );
3314 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
3316 p_box->data.p_cmvd->i_compressed_size = i_read;
3318 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
3319 MP4_READBOX_EXIT( 0 );
3321 /* now copy compressed data */
3322 memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
3324 p_box->data.p_cmvd->b_compressed = 1;
3326 #ifdef MP4_VERBOSE
3327 msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
3328 p_box->data.p_cmvd->i_compressed_size );
3329 #endif
3331 MP4_READBOX_EXIT( 1 );
3334 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
3336 MP4_Box_t *p_dcom;
3337 MP4_Box_t *p_cmvd;
3339 #ifdef HAVE_ZLIB_H
3340 stream_t *p_stream_memory;
3341 z_stream z_data;
3342 uint8_t *p_data;
3343 int i_result;
3344 #endif
3346 if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
3347 return 0;
3349 if( !p_box->p_father ||
3350 ( p_box->p_father->i_type != ATOM_moov &&
3351 p_box->p_father->i_type != ATOM_foov ) )
3353 msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
3354 return 1;
3357 if( !MP4_ReadBoxContainer( p_stream, p_box ) )
3359 return 0;
3362 if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
3363 ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
3364 p_cmvd->data.p_cmvd->p_data == NULL )
3366 msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
3367 return 0;
3370 if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
3372 msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
3373 "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
3374 return 0;
3377 #ifndef HAVE_ZLIB_H
3378 msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
3379 return 0;
3381 #else
3382 /* decompress data */
3383 /* allocate a new buffer */
3384 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
3385 return 0;
3386 /* init default structures */
3387 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
3388 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
3389 z_data.next_out = p_data;
3390 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
3391 z_data.zalloc = (alloc_func)Z_NULL;
3392 z_data.zfree = (free_func)Z_NULL;
3393 z_data.opaque = (voidpf)Z_NULL;
3395 /* init zlib */
3396 if( inflateInit( &z_data ) != Z_OK )
3398 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3399 free( p_data );
3400 return 0;
3403 /* uncompress */
3404 i_result = inflate( &z_data, Z_NO_FLUSH );
3405 if( i_result != Z_OK && i_result != Z_STREAM_END )
3407 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3408 free( p_data );
3409 return 0;
3412 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
3414 msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
3415 "mismatch" );
3417 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
3419 /* close zlib */
3420 if( inflateEnd( &z_data ) != Z_OK )
3422 msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
3423 "data (ignored)" );
3426 free( p_cmvd->data.p_cmvd->p_data );
3427 p_cmvd->data.p_cmvd->p_data = p_data;
3428 p_cmvd->data.p_cmvd->b_compressed = 0;
3430 msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
3432 /* now create a memory stream */
3433 p_stream_memory =
3434 vlc_stream_MemoryNew( VLC_OBJECT(p_stream),
3435 p_cmvd->data.p_cmvd->p_data,
3436 p_cmvd->data.p_cmvd->i_uncompressed_size, true );
3438 /* and read uncompressd moov */
3439 p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
3441 vlc_stream_Delete( p_stream_memory );
3443 #ifdef MP4_VERBOSE
3444 msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
3445 #endif
3447 return p_box->data.p_cmov->p_moov ? 1 : 0;
3448 #endif /* HAVE_ZLIB_H */
3451 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
3453 FREENULL( p_box->data.p_rdrf->psz_ref );
3456 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
3458 uint32_t i_len;
3459 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t, MP4_FreeBox_rdrf );
3461 MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
3462 MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
3463 MP4_GET4BYTES( i_len );
3464 i_len++;
3466 if( i_len > 0 )
3468 p_box->data.p_rdrf->psz_ref = malloc( i_len );
3469 if( p_box->data.p_rdrf->psz_ref == NULL )
3470 MP4_READBOX_EXIT( 0 );
3471 i_len--;
3473 for( unsigned i = 0; i < i_len; i++ )
3475 MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
3477 p_box->data.p_rdrf->psz_ref[i_len] = '\0';
3479 else
3481 p_box->data.p_rdrf->psz_ref = NULL;
3484 #ifdef MP4_VERBOSE
3485 msg_Dbg( p_stream,
3486 "read box: \"rdrf\" type:%4.4s ref %s",
3487 (char*)&p_box->data.p_rdrf->i_ref_type,
3488 p_box->data.p_rdrf->psz_ref );
3489 #endif
3490 MP4_READBOX_EXIT( 1 );
3495 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
3497 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t, NULL );
3499 MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
3501 MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
3503 #ifdef MP4_VERBOSE
3504 msg_Dbg( p_stream,
3505 "read box: \"rmdr\" rate:%d",
3506 p_box->data.p_rmdr->i_rate );
3507 #endif
3508 MP4_READBOX_EXIT( 1 );
3511 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
3513 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t, NULL );
3515 MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
3517 #ifdef MP4_VERBOSE
3518 msg_Dbg( p_stream,
3519 "read box: \"rmqu\" quality:%d",
3520 p_box->data.p_rmqu->i_quality );
3521 #endif
3522 MP4_READBOX_EXIT( 1 );
3525 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
3527 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t, NULL );
3528 MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
3530 MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
3531 MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
3532 MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
3533 MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
3535 #ifdef MP4_VERBOSE
3536 msg_Dbg( p_stream,
3537 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
3538 (char*)&p_box->data.p_rmvc->i_gestaltType,
3539 p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
3540 p_box->data.p_rmvc->i_checkType );
3541 #endif
3543 MP4_READBOX_EXIT( 1 );
3546 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
3548 MP4_READBOX_ENTER( MP4_Box_data_frma_t, NULL );
3550 MP4_GETFOURCC( p_box->data.p_frma->i_type );
3552 #ifdef MP4_VERBOSE
3553 msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
3554 (char *)&p_box->data.p_frma->i_type );
3555 #endif
3557 MP4_READBOX_EXIT( 1 );
3560 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
3562 MP4_READBOX_ENTER( MP4_Box_data_skcr_t, NULL );
3564 MP4_GET4BYTES( p_box->data.p_skcr->i_init );
3565 MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
3566 MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
3568 #ifdef MP4_VERBOSE
3569 msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
3570 p_box->data.p_skcr->i_init,
3571 p_box->data.p_skcr->i_encr,
3572 p_box->data.p_skcr->i_decr );
3573 #endif
3575 MP4_READBOX_EXIT( 1 );
3578 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
3580 VLC_UNUSED(p_box);
3581 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
3582 * so unless data decrypt itself by magic, there will be no playback,
3583 * but we never know... */
3584 msg_Warn( p_stream, "DRM protected streams are not supported." );
3585 return 1;
3588 static void MP4_FreeBox_Binary( MP4_Box_t *p_box )
3590 FREENULL( p_box->data.p_binary->p_blob );
3591 p_box->data.p_binary->i_blob = 0;
3594 static int MP4_ReadBox_Binary( stream_t *p_stream, MP4_Box_t *p_box )
3596 MP4_READBOX_ENTER( MP4_Box_data_binary_t, MP4_FreeBox_Binary );
3597 i_read = __MIN( i_read, UINT32_MAX );
3598 if ( i_read > 0 )
3600 p_box->data.p_binary->p_blob = malloc( i_read );
3601 if ( p_box->data.p_binary->p_blob )
3603 memcpy( p_box->data.p_binary->p_blob, p_peek, i_read );
3604 p_box->data.p_binary->i_blob = i_read;
3607 MP4_READBOX_EXIT( 1 );
3610 static void MP4_FreeBox_data( MP4_Box_t *p_box )
3612 free( p_box->data.p_data->p_blob );
3615 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
3617 MP4_READBOX_ENTER( MP4_Box_data_data_t, MP4_FreeBox_data );
3618 MP4_Box_data_data_t *p_data = p_box->data.p_data;
3620 if ( i_read < 8 || i_read - 8 > UINT32_MAX )
3621 MP4_READBOX_EXIT( 0 );
3623 uint8_t i_type;
3624 MP4_GET1BYTE( i_type );
3625 if ( i_type != 0 )
3627 #ifdef MP4_VERBOSE
3628 msg_Dbg( p_stream, "skipping unknown 'data' atom with type %"PRIu8, i_type );
3629 #endif
3630 MP4_READBOX_EXIT( 0 );
3633 MP4_GET3BYTES( p_data->e_wellknowntype );
3634 MP4_GET2BYTES( p_data->locale.i_country );
3635 MP4_GET2BYTES( p_data->locale.i_language );
3636 #ifdef MP4_VERBOSE
3637 msg_Dbg( p_stream, "read 'data' atom: knowntype=%"PRIu32", country=%"PRIu16" lang=%"PRIu16
3638 ", size %"PRIu64" bytes", p_data->e_wellknowntype,
3639 p_data->locale.i_country, p_data->locale.i_language, i_read );
3640 #endif
3641 p_box->data.p_data->p_blob = malloc( i_read );
3642 if ( !p_box->data.p_data->p_blob )
3643 MP4_READBOX_EXIT( 0 );
3645 p_box->data.p_data->i_blob = i_read;
3646 memcpy( p_box->data.p_data->p_blob, p_peek, i_read);
3648 MP4_READBOX_EXIT( 1 );
3651 static int MP4_ReadBox_Metadata( stream_t *p_stream, MP4_Box_t *p_box )
3653 const uint8_t *p_peek;
3654 if ( vlc_stream_Peek( p_stream, &p_peek, 16 ) < 16 )
3655 return 0;
3656 if ( vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
3657 return 0;
3658 const uint32_t stoplist[] = { ATOM_data, 0 };
3659 return MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist );
3662 /* Chapter support */
3663 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
3665 MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
3666 for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
3667 free( p_chpl->chapter[i].psz_name );
3670 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
3672 MP4_Box_data_chpl_t *p_chpl;
3673 uint32_t i_dummy;
3674 VLC_UNUSED(i_dummy);
3675 int i;
3676 MP4_READBOX_ENTER( MP4_Box_data_chpl_t, MP4_FreeBox_chpl );
3678 p_chpl = p_box->data.p_chpl;
3680 MP4_GETVERSIONFLAGS( p_chpl );
3682 if ( i_read < 5 || p_chpl->i_version != 0x1 )
3683 MP4_READBOX_EXIT( 0 );
3685 MP4_GET4BYTES( i_dummy );
3687 MP4_GET1BYTE( p_chpl->i_chapter );
3689 for( i = 0; i < p_chpl->i_chapter; i++ )
3691 uint64_t i_start;
3692 uint8_t i_len;
3693 int i_copy;
3694 if ( i_read < 9 )
3695 break;
3696 MP4_GET8BYTES( i_start );
3697 MP4_GET1BYTE( i_len );
3699 p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
3700 if( !p_chpl->chapter[i].psz_name )
3701 MP4_READBOX_EXIT( 0 );
3703 i_copy = __MIN( i_len, i_read );
3704 if( i_copy > 0 )
3705 memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
3706 p_chpl->chapter[i].psz_name[i_copy] = '\0';
3707 p_chpl->chapter[i].i_start = i_start;
3709 p_peek += i_copy;
3710 i_read -= i_copy;
3713 if ( i != p_chpl->i_chapter )
3714 p_chpl->i_chapter = i;
3716 /* Bubble sort by increasing start date */
3719 for( i = 0; i < p_chpl->i_chapter - 1; i++ )
3721 if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
3723 char *psz = p_chpl->chapter[i+1].psz_name;
3724 int64_t i64 = p_chpl->chapter[i+1].i_start;
3726 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
3727 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
3729 p_chpl->chapter[i].psz_name = psz;
3730 p_chpl->chapter[i].i_start = i64;
3732 i = -1;
3733 break;
3736 } while( i == -1 );
3738 #ifdef MP4_VERBOSE
3739 msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
3740 p_chpl->i_chapter );
3741 #endif
3742 MP4_READBOX_EXIT( 1 );
3745 /* GoPro HiLight tags support */
3746 static void MP4_FreeBox_HMMT( MP4_Box_t *p_box )
3748 FREENULL( p_box->data.p_hmmt->pi_chapter_start );
3751 static int MP4_ReadBox_HMMT( stream_t *p_stream, MP4_Box_t *p_box )
3753 #define MAX_CHAPTER_COUNT 100
3755 MP4_Box_data_HMMT_t *p_hmmt;
3756 MP4_READBOX_ENTER( MP4_Box_data_HMMT_t, MP4_FreeBox_HMMT );
3758 if( i_read < 4 )
3759 MP4_READBOX_EXIT( 0 );
3761 p_hmmt = p_box->data.p_hmmt;
3763 MP4_GET4BYTES( p_hmmt->i_chapter_count );
3765 if( p_hmmt->i_chapter_count <= 0 )
3767 p_hmmt->pi_chapter_start = NULL;
3768 MP4_READBOX_EXIT( 1 );
3771 if( ( i_read / sizeof(uint32_t) ) < p_hmmt->i_chapter_count )
3772 MP4_READBOX_EXIT( 0 );
3774 /* Cameras are allowing a maximum of 100 tags */
3775 if( p_hmmt->i_chapter_count > MAX_CHAPTER_COUNT )
3776 p_hmmt->i_chapter_count = MAX_CHAPTER_COUNT;
3778 p_hmmt->pi_chapter_start = vlc_alloc( p_hmmt->i_chapter_count, sizeof(uint32_t) );
3779 if( p_hmmt->pi_chapter_start == NULL )
3780 MP4_READBOX_EXIT( 0 );
3782 for( uint32_t i = 0; i < p_hmmt->i_chapter_count; i++ )
3784 MP4_GET4BYTES( p_hmmt->pi_chapter_start[i] );
3787 #ifdef MP4_VERBOSE
3788 msg_Dbg( p_stream, "read box: \"HMMT\" %d HiLight tags", p_hmmt->i_chapter_count );
3789 #endif
3791 MP4_READBOX_EXIT( 1 );
3794 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
3796 FREENULL( p_box->data.p_tref_generic->i_track_ID );
3799 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
3801 MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t, MP4_FreeBox_tref_generic );
3803 p_box->data.p_tref_generic->i_track_ID = NULL;
3804 p_box->data.p_tref_generic->i_entry_count = i_read / sizeof(uint32_t);
3805 if( p_box->data.p_tref_generic->i_entry_count > 0 )
3806 p_box->data.p_tref_generic->i_track_ID = calloc( p_box->data.p_tref_generic->i_entry_count, sizeof(uint32_t) );
3807 if( p_box->data.p_tref_generic->i_track_ID == NULL )
3808 MP4_READBOX_EXIT( 0 );
3810 for( unsigned i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
3812 MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
3814 #ifdef MP4_VERBOSE
3815 msg_Dbg( p_stream, "read box: \"chap\" %d references",
3816 p_box->data.p_tref_generic->i_entry_count );
3817 #endif
3819 MP4_READBOX_EXIT( 1 );
3822 static void MP4_FreeBox_keys( MP4_Box_t *p_box )
3824 for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
3825 free( p_box->data.p_keys->p_entries[i].psz_value );
3826 free( p_box->data.p_keys->p_entries );
3829 static int MP4_ReadBox_keys( stream_t *p_stream, MP4_Box_t *p_box )
3831 MP4_READBOX_ENTER( MP4_Box_data_keys_t, MP4_FreeBox_keys );
3833 if ( i_read < 8 )
3834 MP4_READBOX_EXIT( 0 );
3836 uint32_t i_count;
3837 MP4_GET4BYTES( i_count ); /* reserved + flags */
3838 if ( i_count != 0 )
3839 MP4_READBOX_EXIT( 0 );
3841 MP4_GET4BYTES( i_count );
3842 p_box->data.p_keys->p_entries = calloc( i_count, sizeof(*p_box->data.p_keys->p_entries) );
3843 if ( !p_box->data.p_keys->p_entries )
3844 MP4_READBOX_EXIT( 0 );
3845 p_box->data.p_keys->i_entry_count = i_count;
3847 uint32_t i=0;
3848 for( ; i < i_count; i++ )
3850 if ( i_read < 8 )
3851 break;
3852 uint32_t i_keysize;
3853 MP4_GET4BYTES( i_keysize );
3854 if ( (i_keysize < 8) || (i_keysize - 4 > i_read) )
3855 break;
3856 MP4_GETFOURCC( p_box->data.p_keys->p_entries[i].i_namespace );
3857 i_keysize -= 8;
3858 p_box->data.p_keys->p_entries[i].psz_value = malloc( i_keysize + 1 );
3859 if ( !p_box->data.p_keys->p_entries[i].psz_value )
3860 break;
3861 memcpy( p_box->data.p_keys->p_entries[i].psz_value, p_peek, i_keysize );
3862 p_box->data.p_keys->p_entries[i].psz_value[i_keysize] = 0;
3863 p_peek += i_keysize;
3864 i_read -= i_keysize;
3865 #ifdef MP4_ULTRA_VERBOSE
3866 msg_Dbg( p_stream, "read box: \"keys\": %u '%s'", i + 1,
3867 p_box->data.p_keys->p_entries[i].psz_value );
3868 #endif
3870 if ( i < i_count )
3871 p_box->data.p_keys->i_entry_count = i;
3873 MP4_READBOX_EXIT( 1 );
3876 static int MP4_ReadBox_colr( stream_t *p_stream, MP4_Box_t *p_box )
3878 MP4_READBOX_ENTER( MP4_Box_data_colr_t, NULL );
3879 MP4_GETFOURCC( p_box->data.p_colr->i_type );
3880 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'c' ) ||
3881 p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3883 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_primary_idx );
3884 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_transfer_function_idx );
3885 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_matrix_idx );
3886 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3887 MP4_GET1BYTE( p_box->data.p_colr->nclc.i_full_range );
3889 else
3891 #ifdef MP4_VERBOSE
3892 msg_Warn( p_stream, "Unhandled colr type: %4.4s", (char*)&p_box->data.p_colr->i_type );
3893 #endif
3895 MP4_READBOX_EXIT( 1 );
3898 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
3900 const uint8_t *p_peek;
3901 const size_t i_headersize = mp4_box_headersize( p_box );
3903 if( p_box->i_size < 16 || p_box->i_size - i_headersize < 8 )
3904 return 0;
3906 /* skip over box header */
3907 if( vlc_stream_Read( p_stream, NULL, i_headersize ) < (ssize_t) i_headersize )
3908 return 0;
3910 /* meta content starts with a 4 byte version/flags value (should be 0) */
3911 if( vlc_stream_Peek( p_stream, &p_peek, 8 ) < 8 )
3912 return 0;
3914 if( !memcmp( p_peek, "\0\0\0", 4 ) ) /* correct header case */
3916 if( vlc_stream_Read( p_stream, NULL, 4 ) < 4 )
3917 return 0;
3919 else if( memcmp( &p_peek[4], "hdlr", 4 ) ) /* Broken, headerless ones */
3921 return 0;
3924 /* load child atoms up to the handler (which should be next anyway) */
3925 const uint32_t stoplist[] = { ATOM_hdlr, 0 };
3926 if ( !MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist ) )
3927 return 0;
3929 /* Mandatory */
3930 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box, "hdlr" );
3931 if ( p_hdlr && BOXDATA(p_hdlr) && BOXDATA(p_hdlr)->i_version == 0 )
3933 p_box->i_handler = BOXDATA(p_hdlr)->i_handler_type;
3934 switch( p_box->i_handler )
3936 case HANDLER_mdta:
3937 case HANDLER_mdir:
3938 /* then it behaves like a container */
3939 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
3940 default:
3941 /* skip parsing, will be seen as empty container */
3942 break;
3946 return 1;
3949 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3951 char i_unused;
3952 VLC_UNUSED(i_unused);
3954 MP4_READBOX_ENTER( MP4_Box_data_iods_t, NULL );
3955 MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3957 MP4_GET1BYTE( i_unused ); /* tag */
3958 MP4_GET1BYTE( i_unused ); /* length */
3960 MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3961 are used for other flags */
3962 MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3963 MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3964 MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3965 MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3966 MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3968 #ifdef MP4_VERBOSE
3969 msg_Dbg( p_stream,
3970 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3971 "visual: %i, graphics: %i",
3972 p_box->data.p_iods->i_object_descriptor >> 6,
3973 p_box->data.p_iods->i_OD_profile_level,
3974 p_box->data.p_iods->i_scene_profile_level,
3975 p_box->data.p_iods->i_audio_profile_level,
3976 p_box->data.p_iods->i_visual_profile_level,
3977 p_box->data.p_iods->i_graphics_profile_level );
3978 #endif
3980 MP4_READBOX_EXIT( 1 );
3983 static int MP4_ReadBox_btrt( stream_t *p_stream, MP4_Box_t *p_box )
3985 MP4_READBOX_ENTER( MP4_Box_data_btrt_t, NULL );
3987 if(i_read != 12)
3988 MP4_READBOX_EXIT( 0 );
3990 MP4_GET4BYTES( p_box->data.p_btrt->i_buffer_size );
3991 MP4_GET4BYTES( p_box->data.p_btrt->i_max_bitrate );
3992 MP4_GET4BYTES( p_box->data.p_btrt->i_avg_bitrate );
3994 MP4_READBOX_EXIT( 1 );
3997 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3999 MP4_READBOX_ENTER( MP4_Box_data_pasp_t, NULL );
4001 MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
4002 MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
4004 #ifdef MP4_VERBOSE
4005 msg_Dbg( p_stream,
4006 "read box: \"paps\" %dx%d",
4007 p_box->data.p_pasp->i_horizontal_spacing,
4008 p_box->data.p_pasp->i_vertical_spacing);
4009 #endif
4011 MP4_READBOX_EXIT( 1 );
4014 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
4016 MP4_READBOX_ENTER( MP4_Box_data_mehd_t, NULL );
4018 MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
4019 if( p_box->data.p_mehd->i_version == 1 )
4020 MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
4021 else /* version == 0 */
4022 MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
4024 #ifdef MP4_VERBOSE
4025 msg_Dbg( p_stream,
4026 "read box: \"mehd\" frag dur. %"PRIu64"",
4027 p_box->data.p_mehd->i_fragment_duration );
4028 #endif
4030 MP4_READBOX_EXIT( 1 );
4033 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
4035 MP4_READBOX_ENTER( MP4_Box_data_trex_t, NULL );
4036 MP4_GETVERSIONFLAGS( p_box->data.p_trex );
4038 MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
4039 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
4040 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
4041 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
4042 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
4044 #ifdef MP4_VERBOSE
4045 msg_Dbg( p_stream,
4046 "read box: \"trex\" trackID: %"PRIu32"",
4047 p_box->data.p_trex->i_track_ID );
4048 #endif
4050 MP4_READBOX_EXIT( 1 );
4053 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
4055 FREENULL( p_box->data.p_sdtp->p_sample_table );
4058 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
4060 uint32_t i_sample_count;
4061 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t, MP4_FreeBox_sdtp );
4062 MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
4063 MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
4064 i_sample_count = i_read;
4066 p_sdtp->p_sample_table = calloc( i_sample_count, 1 );
4068 if( !p_sdtp->p_sample_table )
4069 MP4_READBOX_EXIT( 0 );
4071 for( uint32_t i = 0; i < i_sample_count; i++ )
4072 MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
4074 #ifdef MP4_VERBOSE
4075 msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
4076 if ( i_sample_count > 3 )
4077 msg_Dbg( p_stream,
4078 "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
4079 p_sdtp->p_sample_table[0],
4080 p_sdtp->p_sample_table[1],
4081 p_sdtp->p_sample_table[2],
4082 p_sdtp->p_sample_table[3] );
4083 #endif
4085 MP4_READBOX_EXIT( 1 );
4088 static int MP4_ReadBox_tsel( stream_t *p_stream, MP4_Box_t *p_box )
4090 MP4_READBOX_ENTER( MP4_Box_data_tsel_t, NULL );
4091 uint32_t i_version;
4092 MP4_GET4BYTES( i_version );
4093 if ( i_version != 0 || i_read < 4 )
4094 MP4_READBOX_EXIT( 0 );
4095 MP4_GET4BYTES( p_box->data.p_tsel->i_switch_group );
4096 /* ignore list of attributes as es are present before switch */
4097 MP4_READBOX_EXIT( 1 );
4100 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
4102 MP4_READBOX_ENTER( MP4_Box_data_mfro_t, NULL );
4104 MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
4105 MP4_GET4BYTES( p_box->data.p_mfro->i_size );
4107 #ifdef MP4_VERBOSE
4108 msg_Dbg( p_stream,
4109 "read box: \"mfro\" size: %"PRIu32"",
4110 p_box->data.p_mfro->i_size);
4111 #endif
4113 MP4_READBOX_EXIT( 1 );
4116 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
4118 FREENULL( p_box->data.p_tfra->p_time );
4119 FREENULL( p_box->data.p_tfra->p_moof_offset );
4120 FREENULL( p_box->data.p_tfra->p_traf_number );
4121 FREENULL( p_box->data.p_tfra->p_trun_number );
4122 FREENULL( p_box->data.p_tfra->p_sample_number );
4125 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
4127 #define READ_VARIABLE_LENGTH(lengthvar, p_array) switch (lengthvar)\
4129 case 0:\
4130 MP4_GET1BYTE( p_array[i] );\
4131 break;\
4132 case 1:\
4133 MP4_GET2BYTES( *((uint16_t *)&p_array[i*2]) );\
4134 break;\
4135 case 2:\
4136 MP4_GET3BYTES( *((uint32_t *)&p_array[i*4]) );\
4137 break;\
4138 case 3:\
4139 MP4_GET4BYTES( *((uint32_t *)&p_array[i*4]) );\
4140 break;\
4141 default:\
4142 goto error;\
4144 #define FIX_VARIABLE_LENGTH(lengthvar) if ( lengthvar == 3 ) lengthvar = 4
4146 uint32_t i_number_of_entries;
4147 MP4_READBOX_ENTER( MP4_Box_data_tfra_t, MP4_FreeBox_tfra );
4148 MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
4149 MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
4150 if ( p_tfra->i_version > 1 )
4151 MP4_READBOX_EXIT( 0 );
4152 MP4_GET4BYTES( p_tfra->i_track_ID );
4153 uint32_t i_lengths = 0;
4154 MP4_GET4BYTES( i_lengths );
4155 MP4_GET4BYTES( p_tfra->i_number_of_entries );
4156 i_number_of_entries = p_tfra->i_number_of_entries;
4157 p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
4158 p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
4159 p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
4161 size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
4162 p_tfra->p_time = calloc( i_number_of_entries, size );
4163 p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
4165 size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
4166 if ( size == 3 ) size++;
4167 p_tfra->p_traf_number = calloc( i_number_of_entries, size );
4168 size = 1 + p_tfra->i_length_size_of_trun_num;
4169 if ( size == 3 ) size++;
4170 p_tfra->p_trun_number = calloc( i_number_of_entries, size );
4171 size = 1 + p_tfra->i_length_size_of_sample_num;
4172 if ( size == 3 ) size++;
4173 p_tfra->p_sample_number = calloc( i_number_of_entries, size );
4175 if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
4176 || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
4177 goto error;
4179 unsigned i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
4180 + p_tfra->i_length_size_of_trun_num
4181 + p_tfra->i_length_size_of_sample_num;
4183 uint32_t i;
4184 for( i = 0; i < i_number_of_entries; i++ )
4187 if( p_tfra->i_version == 1 )
4189 if ( i_read < i_fields_length + 16 )
4190 break;
4191 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_time[i*2]) );
4192 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_moof_offset[i*2]) );
4194 else
4196 if ( i_read < i_fields_length + 8 )
4197 break;
4198 MP4_GET4BYTES( p_tfra->p_time[i] );
4199 MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
4202 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num, p_tfra->p_traf_number);
4203 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num, p_tfra->p_trun_number);
4204 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num, p_tfra->p_sample_number);
4206 if ( i < i_number_of_entries )
4207 i_number_of_entries = i;
4209 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num);
4210 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num);
4211 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num);
4213 #ifdef MP4_ULTRA_VERBOSE
4214 for( i = 0; i < i_number_of_entries; i++ )
4216 if( p_tfra->i_version == 0 )
4218 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu32", "
4219 "moof_offset[%"PRIu32"]: %"PRIu32"",
4220 p_tfra->i_track_ID,
4221 i, p_tfra->p_time[i],
4222 i, p_tfra->p_moof_offset[i] );
4224 else
4226 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu64", "
4227 "moof_offset[%"PRIu32"]: %"PRIu64"",
4228 p_tfra->i_track_ID,
4229 i, ((uint64_t *)(p_tfra->p_time))[i],
4230 i, ((uint64_t *)(p_tfra->p_moof_offset))[i] );
4233 #endif
4234 #ifdef MP4_VERBOSE
4235 msg_Dbg( p_stream, "tfra[%"PRIu32"] %"PRIu32" entries",
4236 p_tfra->i_track_ID, i_number_of_entries );
4237 #endif
4239 MP4_READBOX_EXIT( 1 );
4240 error:
4241 MP4_READBOX_EXIT( 0 );
4243 #undef READ_VARIABLE_LENGTH
4244 #undef FIX_VARIABLE_LENGTH
4247 static int MP4_ReadBox_pnot( stream_t *p_stream, MP4_Box_t *p_box )
4249 if ( p_box->i_size != 20 )
4250 return 0;
4251 MP4_READBOX_ENTER( MP4_Box_data_pnot_t, NULL );
4252 MP4_GET4BYTES( p_box->data.p_pnot->i_date );
4253 uint16_t i_version;
4254 MP4_GET2BYTES( i_version );
4255 if ( i_version != 0 )
4256 MP4_READBOX_EXIT( 0 );
4257 MP4_GETFOURCC( p_box->data.p_pnot->i_type );
4258 MP4_GET2BYTES( p_box->data.p_pnot->i_index );
4259 MP4_READBOX_EXIT( 1 );
4262 static int MP4_ReadBox_SA3D( stream_t *p_stream, MP4_Box_t *p_box )
4264 MP4_READBOX_ENTER( MP4_Box_data_SA3D_t, NULL );
4266 uint8_t i_version;
4267 MP4_GET1BYTE( i_version );
4268 if ( i_version != 0 )
4269 MP4_READBOX_EXIT( 0 );
4271 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_type );
4272 MP4_GET4BYTES( p_box->data.p_SA3D->i_ambisonic_order );
4273 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_channel_ordering );
4274 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_normalization );
4275 MP4_GET4BYTES( p_box->data.p_SA3D->i_num_channels );
4276 MP4_READBOX_EXIT( 1 );
4279 /* For generic */
4280 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
4282 if( !p_box->p_father )
4284 goto unknown;
4286 if( p_box->p_father->i_type == ATOM_stsd )
4288 MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
4289 MP4_Box_t *p_hdlr;
4291 if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
4292 (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
4294 goto unknown;
4296 switch( p_hdlr->data.p_hdlr->i_handler_type )
4298 case ATOM_soun:
4299 return MP4_ReadBox_sample_soun( p_stream, p_box );
4300 case ATOM_vide:
4301 return MP4_ReadBox_sample_vide( p_stream, p_box );
4302 case ATOM_hint:
4303 return MP4_ReadBox_sample_hint8( p_stream, p_box );
4304 case ATOM_text:
4305 return MP4_ReadBox_sample_text( p_stream, p_box );
4306 case ATOM_tx3g:
4307 case ATOM_sbtl:
4308 return MP4_ReadBox_sample_tx3g( p_stream, p_box );
4309 default:
4310 msg_Warn( p_stream,
4311 "unknown handler type in stsd (incompletely loaded)" );
4312 return 1;
4316 unknown:
4317 if MP4_BOX_TYPE_ASCII()
4318 msg_Warn( p_stream,
4319 "unknown box type %4.4s (incompletely loaded)",
4320 (char*)&p_box->i_type );
4321 else
4322 msg_Warn( p_stream,
4323 "unknown box type c%3.3s (incompletely loaded)",
4324 (char*)&p_box->i_type+1 );
4325 p_box->e_flags |= BOX_FLAG_INCOMPLETE;
4327 return 1;
4330 /**** ------------------------------------------------------------------- ****/
4332 static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
4334 if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
4335 return MP4_ReadBox_tfrf( p_stream, p_box );
4336 if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
4337 return MP4_ReadBox_tfxd( p_stream, p_box );
4338 if( !CmpUUID( &p_box->i_uuid, &XML360BoxUUID ) )
4339 return MP4_ReadBox_XML360( p_stream, p_box );
4340 if( !CmpUUID( &p_box->i_uuid, &PS3DDSBoxUUID ) && p_box->i_size == 28 )
4341 return MP4_ReadBox_Binary( p_stream, p_box );
4343 #ifdef MP4_VERBOSE
4344 msg_Warn( p_stream, "Unknown uuid type box: "
4345 "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-"
4346 "%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
4347 p_box->i_uuid.b[0], p_box->i_uuid.b[1], p_box->i_uuid.b[2], p_box->i_uuid.b[3],
4348 p_box->i_uuid.b[4], p_box->i_uuid.b[5], p_box->i_uuid.b[6], p_box->i_uuid.b[7],
4349 p_box->i_uuid.b[8], p_box->i_uuid.b[9], p_box->i_uuid.b[10], p_box->i_uuid.b[11],
4350 p_box->i_uuid.b[12], p_box->i_uuid.b[13], p_box->i_uuid.b[14], p_box->i_uuid.b[15] );
4351 #else
4352 msg_Warn( p_stream, "Unknown uuid type box" );
4353 #endif
4354 return 1;
4357 /**** ------------------------------------------------------------------- ****/
4358 /**** "Higher level" Functions ****/
4359 /**** ------------------------------------------------------------------- ****/
4361 static const struct
4363 uint32_t i_type;
4364 int (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
4365 uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
4366 } MP4_Box_Function [] =
4368 /* Containers */
4369 { ATOM_moov, MP4_ReadBoxContainer, 0 },
4370 { ATOM_foov, MP4_ReadBoxContainer, 0 },
4371 { ATOM_trak, MP4_ReadBoxContainer, ATOM_moov },
4372 { ATOM_trak, MP4_ReadBoxContainer, ATOM_foov },
4373 { ATOM_mdia, MP4_ReadBoxContainer, ATOM_trak },
4374 { ATOM_moof, MP4_ReadBoxContainer, 0 },
4375 { ATOM_minf, MP4_ReadBoxContainer, ATOM_mdia },
4376 { ATOM_stbl, MP4_ReadBoxContainer, ATOM_minf },
4377 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_minf },
4378 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_meta },
4379 { ATOM_edts, MP4_ReadBoxContainer, ATOM_trak },
4380 { ATOM_udta, MP4_ReadBoxContainer, 0 },
4381 { ATOM_nmhd, MP4_ReadBoxContainer, ATOM_minf },
4382 { ATOM_hnti, MP4_ReadBoxContainer, ATOM_udta },
4383 { ATOM_rmra, MP4_ReadBoxContainer, ATOM_moov },
4384 { ATOM_rmda, MP4_ReadBoxContainer, ATOM_rmra },
4385 { ATOM_tref, MP4_ReadBoxContainer, ATOM_trak },
4386 { ATOM_gmhd, MP4_ReadBoxContainer, ATOM_minf },
4387 { ATOM_wave, MP4_ReadBoxContainer, ATOM_stsd },
4388 { ATOM_wave, MP4_ReadBoxContainer, ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
4389 { ATOM_wave, MP4_ReadBoxContainer, ATOM_WMA2 }, /* flip4mac */
4390 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in24 },
4391 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in32 },
4392 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl32 },
4393 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl64 },
4394 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDMC },
4395 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDM2 },
4396 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiFL }, /* XiphQT */
4397 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiVs }, /* XiphQT */
4398 { ATOM_ilst, MP4_ReadBox_ilst, ATOM_meta },
4399 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_moov },
4400 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_ftyp },
4402 /* specific box */
4403 { ATOM_ftyp, MP4_ReadBox_ftyp, 0 },
4404 { ATOM_styp, MP4_ReadBox_ftyp, 0 },
4405 { ATOM_cmov, MP4_ReadBox_cmov, 0 },
4406 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_moov },
4407 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_foov },
4408 { ATOM_tkhd, MP4_ReadBox_tkhd, ATOM_trak },
4409 { ATOM_load, MP4_ReadBox_load, ATOM_trak },
4410 { ATOM_mdhd, MP4_ReadBox_mdhd, ATOM_mdia },
4411 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_mdia },
4412 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_meta },
4413 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_minf },
4414 { ATOM_vmhd, MP4_ReadBox_vmhd, ATOM_minf },
4415 { ATOM_smhd, MP4_ReadBox_smhd, ATOM_minf },
4416 { ATOM_hmhd, MP4_ReadBox_hmhd, ATOM_minf },
4417 { ATOM_alis, MP4_ReadBoxSkip, ATOM_dref },
4418 { ATOM_url, MP4_ReadBox_url, 0 },
4419 { ATOM_urn, MP4_ReadBox_urn, 0 },
4420 { ATOM_dref, MP4_ReadBox_LtdContainer, 0 },
4421 { ATOM_stts, MP4_ReadBox_stts, ATOM_stbl },
4422 { ATOM_ctts, MP4_ReadBox_ctts, ATOM_stbl },
4423 { ATOM_cslg, MP4_ReadBox_cslg, ATOM_stbl },
4424 { ATOM_stsd, MP4_ReadBox_LtdContainer, ATOM_stbl },
4425 { ATOM_stsz, MP4_ReadBox_stsz, ATOM_stbl },
4426 { ATOM_stsc, MP4_ReadBox_stsc, ATOM_stbl },
4427 { ATOM_stco, MP4_ReadBox_stco_co64, ATOM_stbl },
4428 { ATOM_co64, MP4_ReadBox_stco_co64, ATOM_stbl },
4429 { ATOM_stss, MP4_ReadBox_stss, ATOM_stbl },
4430 { ATOM_stsh, MP4_ReadBox_stsh, ATOM_stbl },
4431 { ATOM_stdp, MP4_ReadBox_stdp, 0 },
4432 { ATOM_padb, MP4_ReadBox_padb, 0 },
4433 { ATOM_elst, MP4_ReadBox_elst, ATOM_edts },
4434 { ATOM_cprt, MP4_ReadBox_cprt, 0 },
4435 { ATOM_esds, MP4_ReadBox_esds, ATOM_wave }, /* mp4a in wave chunk */
4436 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4a },
4437 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4v },
4438 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4s },
4439 { ATOM_dcom, MP4_ReadBox_dcom, 0 },
4440 { ATOM_dfLa, MP4_ReadBox_Binary, ATOM_fLaC },
4441 { ATOM_cmvd, MP4_ReadBox_cmvd, 0 },
4442 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc1 },
4443 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc3 },
4444 { ATOM_hvcC, MP4_ReadBox_Binary, 0 },
4445 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp08 },
4446 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp09 },
4447 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp10 },
4448 { ATOM_dac3, MP4_ReadBox_dac3, 0 },
4449 { ATOM_dec3, MP4_ReadBox_dec3, 0 },
4450 { ATOM_dvc1, MP4_ReadBox_dvc1, ATOM_vc1 },
4451 { ATOM_fiel, MP4_ReadBox_fiel, 0 },
4452 { ATOM_glbl, MP4_ReadBox_Binary, ATOM_FFV1 },
4453 { ATOM_enda, MP4_ReadBox_enda, 0 },
4454 { ATOM_iods, MP4_ReadBox_iods, 0 },
4455 { ATOM_pasp, MP4_ReadBox_pasp, 0 },
4456 { ATOM_btrt, MP4_ReadBox_btrt, 0 }, /* codecs bitrate stsd/????/btrt */
4457 { ATOM_keys, MP4_ReadBox_keys, ATOM_meta },
4458 { ATOM_colr, MP4_ReadBox_colr, 0 },
4460 /* XiphQT */
4461 { ATOM_vCtH, MP4_ReadBox_Binary, ATOM_wave },
4462 { ATOM_vCtC, MP4_ReadBox_Binary, ATOM_wave },
4463 { ATOM_vCtd, MP4_ReadBox_Binary, ATOM_wave },
4464 { ATOM_fCtS, MP4_ReadBox_Binary, ATOM_wave },
4466 /* Samples groups specific information */
4467 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_stbl },
4468 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_traf },
4469 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_stbl },
4470 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_traf },
4472 /* Quicktime preview atoms, all at root */
4473 { ATOM_pnot, MP4_ReadBox_pnot, 0 },
4474 { ATOM_pict, MP4_ReadBox_Binary, 0 },
4475 { ATOM_PICT, MP4_ReadBox_Binary, 0 },
4477 /* Nothing to do with this box */
4478 { ATOM_mdat, MP4_ReadBoxSkip, 0 },
4479 { ATOM_skip, MP4_ReadBoxSkip, 0 },
4480 { ATOM_free, MP4_ReadBoxSkip, 0 },
4481 { ATOM_wide, MP4_ReadBoxSkip, 0 },
4482 { ATOM_binm, MP4_ReadBoxSkip, 0 },
4484 /* Subtitles */
4485 { ATOM_tx3g, MP4_ReadBox_sample_tx3g, 0 },
4486 { ATOM_c608, MP4_ReadBox_sample_clcp, ATOM_stsd },
4487 //{ ATOM_text, MP4_ReadBox_sample_text, 0 },
4488 /* In sample WebVTT subtitle atoms. No ATOM_wvtt in normal parsing */
4489 { ATOM_vttc, MP4_ReadBoxContainer, ATOM_wvtt },
4490 { ATOM_payl, MP4_ReadBox_Binary, ATOM_vttc },
4492 /* for codecs */
4493 { ATOM_soun, MP4_ReadBox_sample_soun, ATOM_stsd },
4494 { ATOM_agsm, MP4_ReadBox_sample_soun, ATOM_stsd },
4495 { ATOM_ac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4496 { ATOM_eac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4497 { ATOM_fLaC, MP4_ReadBox_sample_soun, ATOM_stsd },
4498 { ATOM_lpcm, MP4_ReadBox_sample_soun, ATOM_stsd },
4499 { ATOM_ms02, MP4_ReadBox_sample_soun, ATOM_stsd },
4500 { ATOM_ms11, MP4_ReadBox_sample_soun, ATOM_stsd },
4501 { ATOM_ms55, MP4_ReadBox_sample_soun, ATOM_stsd },
4502 { ATOM__mp3, MP4_ReadBox_sample_soun, ATOM_stsd },
4503 { ATOM_mp4a, MP4_ReadBox_sample_soun, ATOM_stsd },
4504 { ATOM_twos, MP4_ReadBox_sample_soun, ATOM_stsd },
4505 { ATOM_sowt, MP4_ReadBox_sample_soun, ATOM_stsd },
4506 { ATOM_QDMC, MP4_ReadBox_sample_soun, ATOM_stsd },
4507 { ATOM_QDM2, MP4_ReadBox_sample_soun, ATOM_stsd },
4508 { ATOM_ima4, MP4_ReadBox_sample_soun, ATOM_stsd },
4509 { ATOM_IMA4, MP4_ReadBox_sample_soun, ATOM_stsd },
4510 { ATOM_dvi, MP4_ReadBox_sample_soun, ATOM_stsd },
4511 { ATOM_alaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4512 { ATOM_ulaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4513 { ATOM_MAC3, MP4_ReadBox_sample_soun, ATOM_stsd },
4514 { ATOM_MAC6, MP4_ReadBox_sample_soun, ATOM_stsd },
4515 { ATOM_Qclp, MP4_ReadBox_sample_soun, ATOM_stsd },
4516 { ATOM_samr, MP4_ReadBox_sample_soun, ATOM_stsd },
4517 { ATOM_sawb, MP4_ReadBox_sample_soun, ATOM_stsd },
4518 { ATOM_OggS, MP4_ReadBox_sample_soun, ATOM_stsd },
4519 { ATOM_alac, MP4_ReadBox_sample_soun, ATOM_stsd },
4520 { ATOM_WMA2, MP4_ReadBox_sample_soun, ATOM_stsd }, /* flip4mac */
4521 { ATOM_wma, MP4_ReadBox_sample_soun, ATOM_stsd }, /* ismv wmapro */
4522 { ATOM_Opus, MP4_ReadBox_sample_soun, ATOM_stsd },
4523 /* Sound extensions */
4524 { ATOM_chan, MP4_ReadBox_stsdext_chan, 0 },
4525 { ATOM_WMA2, MP4_ReadBox_WMA2, ATOM_wave }, /* flip4mac */
4526 { ATOM_dOps, MP4_ReadBox_Binary, ATOM_Opus },
4527 { ATOM_wfex, MP4_ReadBox_WMA2, ATOM_wma }, /* ismv formatex */
4529 /* Both uncompressed sound and video */
4530 { ATOM_raw, MP4_ReadBox_default, ATOM_stsd },
4532 { ATOM_drmi, MP4_ReadBox_sample_vide, ATOM_stsd },
4533 { ATOM_vide, MP4_ReadBox_sample_vide, ATOM_stsd },
4534 { ATOM_mp4v, MP4_ReadBox_sample_vide, ATOM_stsd },
4535 { ATOM_SVQ1, MP4_ReadBox_sample_vide, ATOM_stsd },
4536 { ATOM_SVQ3, MP4_ReadBox_sample_vide, ATOM_stsd },
4537 { ATOM_ZyGo, MP4_ReadBox_sample_vide, ATOM_stsd },
4538 { ATOM_DIVX, MP4_ReadBox_sample_vide, ATOM_stsd },
4539 { ATOM_XVID, MP4_ReadBox_sample_vide, ATOM_stsd },
4540 { ATOM_h263, MP4_ReadBox_sample_vide, ATOM_stsd },
4541 { ATOM_s263, MP4_ReadBox_sample_vide, ATOM_stsd },
4542 { ATOM_cvid, MP4_ReadBox_sample_vide, ATOM_stsd },
4543 { ATOM_3IV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4544 { ATOM_3iv1, MP4_ReadBox_sample_vide, ATOM_stsd },
4545 { ATOM_3IV2, MP4_ReadBox_sample_vide, ATOM_stsd },
4546 { ATOM_3iv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4547 { ATOM_3IVD, MP4_ReadBox_sample_vide, ATOM_stsd },
4548 { ATOM_3ivd, MP4_ReadBox_sample_vide, ATOM_stsd },
4549 { ATOM_3VID, MP4_ReadBox_sample_vide, ATOM_stsd },
4550 { ATOM_3vid, MP4_ReadBox_sample_vide, ATOM_stsd },
4551 { ATOM_FFV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4552 { ATOM_mjpa, MP4_ReadBox_sample_vide, ATOM_stsd },
4553 { ATOM_mjpb, MP4_ReadBox_sample_vide, ATOM_stsd },
4554 { ATOM_qdrw, MP4_ReadBox_sample_vide, ATOM_stsd },
4555 { ATOM_mp2v, MP4_ReadBox_sample_vide, ATOM_stsd },
4556 { ATOM_hdv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4557 { ATOM_WMV3, MP4_ReadBox_sample_vide, ATOM_stsd },
4559 { ATOM_mjqt, MP4_ReadBox_default, 0 }, /* found in mjpa/b */
4560 { ATOM_mjht, MP4_ReadBox_default, 0 },
4562 { ATOM_dvc, MP4_ReadBox_sample_vide, ATOM_stsd },
4563 { ATOM_dvp, MP4_ReadBox_sample_vide, ATOM_stsd },
4564 { ATOM_dv5n, MP4_ReadBox_sample_vide, ATOM_stsd },
4565 { ATOM_dv5p, MP4_ReadBox_sample_vide, ATOM_stsd },
4566 { ATOM_VP31, MP4_ReadBox_sample_vide, ATOM_stsd },
4567 { ATOM_vp31, MP4_ReadBox_sample_vide, ATOM_stsd },
4568 { ATOM_h264, MP4_ReadBox_sample_vide, ATOM_stsd },
4570 { ATOM_jpeg, MP4_ReadBox_sample_vide, ATOM_stsd },
4571 { ATOM_vc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4572 { ATOM_avc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4573 { ATOM_avc3, MP4_ReadBox_sample_vide, ATOM_stsd },
4575 { ATOM_rrtp, MP4_ReadBox_sample_hint8, ATOM_stsd },
4577 { ATOM_yv12, MP4_ReadBox_sample_vide, 0 },
4578 { ATOM_yuv2, MP4_ReadBox_sample_vide, 0 },
4580 { ATOM_strf, MP4_ReadBox_strf, ATOM_WMV3 }, /* flip4mac */
4581 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_WMV3 }, /* flip4mac */
4582 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_wave }, /* flip4mac */
4584 { ATOM_mp4s, MP4_ReadBox_sample_mp4s, ATOM_stsd },
4586 /* XXX there is 2 box where we could find this entry stbl and tref*/
4587 { ATOM_hint, MP4_ReadBox_default, 0 },
4589 /* found in tref box */
4590 { ATOM_dpnd, MP4_ReadBox_default, 0 },
4591 { ATOM_ipir, MP4_ReadBox_default, 0 },
4592 { ATOM_mpod, MP4_ReadBox_default, 0 },
4593 { ATOM_chap, MP4_ReadBox_tref_generic, 0 },
4595 /* found in hnti */
4596 { ATOM_rtp, MP4_ReadBox_rtp, ATOM_hnti },
4597 { ATOM_sdp, MP4_ReadBox_sdp, ATOM_hnti },
4599 /* found in rrtp sample description */
4600 { ATOM_tims, MP4_ReadBox_tims, 0 },
4601 { ATOM_tsro, MP4_ReadBox_tsro, 0 },
4602 { ATOM_tssy, MP4_ReadBox_tssy, 0 },
4604 /* found in rmra/rmda */
4605 { ATOM_rdrf, MP4_ReadBox_rdrf, ATOM_rmda },
4606 { ATOM_rmdr, MP4_ReadBox_rmdr, ATOM_rmda },
4607 { ATOM_rmqu, MP4_ReadBox_rmqu, ATOM_rmda },
4608 { ATOM_rmvc, MP4_ReadBox_rmvc, ATOM_rmda },
4610 { ATOM_drms, MP4_ReadBox_sample_soun, 0 },
4611 { ATOM_sinf, MP4_ReadBoxContainer, 0 },
4612 { ATOM_schi, MP4_ReadBoxContainer, 0 },
4613 { ATOM_user, MP4_ReadBox_drms, 0 },
4614 { ATOM_key, MP4_ReadBox_drms, 0 },
4615 { ATOM_iviv, MP4_ReadBox_drms, 0 },
4616 { ATOM_priv, MP4_ReadBox_drms, 0 },
4617 { ATOM_frma, MP4_ReadBox_frma, ATOM_sinf }, /* and rinf */
4618 { ATOM_frma, MP4_ReadBox_frma, ATOM_wave }, /* flip4mac */
4619 { ATOM_skcr, MP4_ReadBox_skcr, 0 },
4621 /* ilst meta tags */
4622 { ATOM_0xa9ART, MP4_ReadBox_Metadata, ATOM_ilst },
4623 { ATOM_0xa9alb, MP4_ReadBox_Metadata, ATOM_ilst },
4624 { ATOM_0xa9cmt, MP4_ReadBox_Metadata, ATOM_ilst },
4625 { ATOM_0xa9com, MP4_ReadBox_Metadata, ATOM_ilst },
4626 { ATOM_0xa9cpy, MP4_ReadBox_Metadata, ATOM_ilst },
4627 { ATOM_0xa9day, MP4_ReadBox_Metadata, ATOM_ilst },
4628 { ATOM_0xa9des, MP4_ReadBox_Metadata, ATOM_ilst },
4629 { ATOM_0xa9enc, MP4_ReadBox_Metadata, ATOM_ilst },
4630 { ATOM_0xa9gen, MP4_ReadBox_Metadata, ATOM_ilst },
4631 { ATOM_0xa9grp, MP4_ReadBox_Metadata, ATOM_ilst },
4632 { ATOM_0xa9lyr, MP4_ReadBox_Metadata, ATOM_ilst },
4633 { ATOM_0xa9nam, MP4_ReadBox_Metadata, ATOM_ilst },
4634 { ATOM_0xa9too, MP4_ReadBox_Metadata, ATOM_ilst },
4635 { ATOM_0xa9trk, MP4_ReadBox_Metadata, ATOM_ilst },
4636 { ATOM_0xa9wrt, MP4_ReadBox_Metadata, ATOM_ilst },
4637 { ATOM_aART, MP4_ReadBox_Metadata, ATOM_ilst },
4638 { ATOM_atID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4639 { ATOM_cnID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4640 { ATOM_covr, MP4_ReadBoxContainer, ATOM_ilst },
4641 { ATOM_desc, MP4_ReadBox_Metadata, ATOM_ilst },
4642 { ATOM_disk, MP4_ReadBox_Metadata, ATOM_ilst },
4643 { ATOM_flvr, MP4_ReadBox_Metadata, ATOM_ilst },
4644 { ATOM_gnre, MP4_ReadBox_Metadata, ATOM_ilst },
4645 { ATOM_rtng, MP4_ReadBox_Metadata, ATOM_ilst },
4646 { ATOM_trkn, MP4_ReadBox_Metadata, ATOM_ilst },
4647 { ATOM_xid_, MP4_ReadBox_Metadata, ATOM_ilst },
4648 { ATOM_gshh, MP4_ReadBox_Metadata, ATOM_ilst }, /* YouTube gs?? */
4649 { ATOM_gspm, MP4_ReadBox_Metadata, ATOM_ilst },
4650 { ATOM_gspu, MP4_ReadBox_Metadata, ATOM_ilst },
4651 { ATOM_gssd, MP4_ReadBox_Metadata, ATOM_ilst },
4652 { ATOM_gsst, MP4_ReadBox_Metadata, ATOM_ilst },
4653 { ATOM_gstd, MP4_ReadBox_Metadata, ATOM_ilst },
4654 { ATOM_ITUN, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunesInfo */
4656 /* udta */
4657 { ATOM_0x40PRM, MP4_ReadBox_Binary, ATOM_udta },
4658 { ATOM_0x40PRQ, MP4_ReadBox_Binary, ATOM_udta },
4659 { ATOM_0xa9ART, MP4_ReadBox_Binary, ATOM_udta },
4660 { ATOM_0xa9alb, MP4_ReadBox_Binary, ATOM_udta },
4661 { ATOM_0xa9ard, MP4_ReadBox_Binary, ATOM_udta },
4662 { ATOM_0xa9arg, MP4_ReadBox_Binary, ATOM_udta },
4663 { ATOM_0xa9aut, MP4_ReadBox_Binary, ATOM_udta },
4664 { ATOM_0xa9cak, MP4_ReadBox_Binary, ATOM_udta },
4665 { ATOM_0xa9cmt, MP4_ReadBox_Binary, ATOM_udta },
4666 { ATOM_0xa9con, MP4_ReadBox_Binary, ATOM_udta },
4667 { ATOM_0xa9com, MP4_ReadBox_Binary, ATOM_udta },
4668 { ATOM_0xa9cpy, MP4_ReadBox_Binary, ATOM_udta },
4669 { ATOM_0xa9day, MP4_ReadBox_Binary, ATOM_udta },
4670 { ATOM_0xa9des, MP4_ReadBox_Binary, ATOM_udta },
4671 { ATOM_0xa9dir, MP4_ReadBox_Binary, ATOM_udta },
4672 { ATOM_0xa9dis, MP4_ReadBox_Binary, ATOM_udta },
4673 { ATOM_0xa9dsa, MP4_ReadBox_Binary, ATOM_udta },
4674 { ATOM_0xa9fmt, MP4_ReadBox_Binary, ATOM_udta },
4675 { ATOM_0xa9gen, MP4_ReadBox_Binary, ATOM_udta },
4676 { ATOM_0xa9grp, MP4_ReadBox_Binary, ATOM_udta },
4677 { ATOM_0xa9hst, MP4_ReadBox_Binary, ATOM_udta },
4678 { ATOM_0xa9inf, MP4_ReadBox_Binary, ATOM_udta },
4679 { ATOM_0xa9isr, MP4_ReadBox_Binary, ATOM_udta },
4680 { ATOM_0xa9lab, MP4_ReadBox_Binary, ATOM_udta },
4681 { ATOM_0xa9lal, MP4_ReadBox_Binary, ATOM_udta },
4682 { ATOM_0xa9lnt, MP4_ReadBox_Binary, ATOM_udta },
4683 { ATOM_0xa9lyr, MP4_ReadBox_Binary, ATOM_udta },
4684 { ATOM_0xa9mak, MP4_ReadBox_Binary, ATOM_udta },
4685 { ATOM_0xa9mal, MP4_ReadBox_Binary, ATOM_udta },
4686 { ATOM_0xa9mod, MP4_ReadBox_Binary, ATOM_udta },
4687 { ATOM_0xa9nam, MP4_ReadBox_Binary, ATOM_udta },
4688 { ATOM_0xa9ope, MP4_ReadBox_Binary, ATOM_udta },
4689 { ATOM_0xa9phg, MP4_ReadBox_Binary, ATOM_udta },
4690 { ATOM_0xa9PRD, MP4_ReadBox_Binary, ATOM_udta },
4691 { ATOM_0xa9prd, MP4_ReadBox_Binary, ATOM_udta },
4692 { ATOM_0xa9prf, MP4_ReadBox_Binary, ATOM_udta },
4693 { ATOM_0xa9pub, MP4_ReadBox_Binary, ATOM_udta },
4694 { ATOM_0xa9req, MP4_ReadBox_Binary, ATOM_udta },
4695 { ATOM_0xa9sne, MP4_ReadBox_Binary, ATOM_udta },
4696 { ATOM_0xa9snm, MP4_ReadBox_Binary, ATOM_udta },
4697 { ATOM_0xa9sol, MP4_ReadBox_Binary, ATOM_udta },
4698 { ATOM_0xa9src, MP4_ReadBox_Binary, ATOM_udta },
4699 { ATOM_0xa9st3, MP4_ReadBox_Binary, ATOM_udta },
4700 { ATOM_0xa9swr, MP4_ReadBox_Binary, ATOM_udta },
4701 { ATOM_0xa9thx, MP4_ReadBox_Binary, ATOM_udta },
4702 { ATOM_0xa9too, MP4_ReadBox_Binary, ATOM_udta },
4703 { ATOM_0xa9trk, MP4_ReadBox_Binary, ATOM_udta },
4704 { ATOM_0xa9url, MP4_ReadBox_Binary, ATOM_udta },
4705 { ATOM_0xa9wrn, MP4_ReadBox_Binary, ATOM_udta },
4706 { ATOM_0xa9xpd, MP4_ReadBox_Binary, ATOM_udta },
4707 { ATOM_0xa9xyz, MP4_ReadBox_Binary, ATOM_udta },
4708 { ATOM_chpl, MP4_ReadBox_chpl, ATOM_udta }, /* nero unlabeled chapters list */
4709 { ATOM_MCPS, MP4_ReadBox_Binary, ATOM_udta },
4710 { ATOM_name, MP4_ReadBox_Binary, ATOM_udta },
4711 { ATOM_vndr, MP4_ReadBox_Binary, ATOM_udta },
4712 { ATOM_SDLN, MP4_ReadBox_Binary, ATOM_udta },
4713 { ATOM_HMMT, MP4_ReadBox_HMMT, ATOM_udta }, /* GoPro HiLight tags */
4715 /* udta, non meta */
4716 { ATOM_tsel, MP4_ReadBox_tsel, ATOM_udta },
4718 /* iTunes/Quicktime meta info */
4719 { ATOM_meta, MP4_ReadBox_meta, 0 },
4720 { ATOM_ID32, MP4_ReadBox_Binary, ATOM_meta }, /* ID3v2 in 3GPP / ETSI TS 126 244 8.3 */
4721 { ATOM_data, MP4_ReadBox_data, 0 }, /* ilst/@too and others, ITUN/data */
4722 { ATOM_mean, MP4_ReadBox_Binary, ATOM_ITUN },
4723 { ATOM_name, MP4_ReadBox_Binary, ATOM_ITUN },
4725 /* found in smoothstreaming */
4726 { ATOM_traf, MP4_ReadBoxContainer, ATOM_moof },
4727 { ATOM_mfra, MP4_ReadBoxContainer, 0 },
4728 { ATOM_mfhd, MP4_ReadBox_mfhd, ATOM_moof },
4729 { ATOM_sidx, MP4_ReadBox_sidx, 0 },
4730 { ATOM_tfhd, MP4_ReadBox_tfhd, ATOM_traf },
4731 { ATOM_trun, MP4_ReadBox_trun, ATOM_traf },
4732 { ATOM_tfdt, MP4_ReadBox_tfdt, ATOM_traf },
4733 { ATOM_trex, MP4_ReadBox_trex, ATOM_mvex },
4734 { ATOM_mehd, MP4_ReadBox_mehd, ATOM_mvex },
4735 { ATOM_sdtp, MP4_ReadBox_sdtp, 0 },
4736 { ATOM_tfra, MP4_ReadBox_tfra, ATOM_mfra },
4737 { ATOM_mfro, MP4_ReadBox_mfro, ATOM_mfra },
4738 { ATOM_uuid, MP4_ReadBox_uuid, 0 },
4740 /* spatial/360°/VR */
4741 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_avc1 },
4742 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_mp4v },
4743 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_avc1 },
4744 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_mp4v },
4745 { ATOM_proj, MP4_ReadBoxContainer, ATOM_sv3d },
4746 { ATOM_prhd, MP4_ReadBox_prhd, ATOM_proj },
4747 { ATOM_equi, MP4_ReadBox_equi, ATOM_proj },
4748 { ATOM_cbmp, MP4_ReadBox_cbmp, ATOM_proj },
4750 /* Ambisonics */
4751 { ATOM_SA3D, MP4_ReadBox_SA3D, 0 },
4753 /* Last entry */
4754 { 0, MP4_ReadBox_default, 0 }
4757 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
4759 int i_index;
4761 for( i_index = 0; ; i_index++ )
4763 if ( MP4_Box_Function[i_index].i_parent &&
4764 p_father && p_father->i_type != MP4_Box_Function[i_index].i_parent )
4765 continue;
4767 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
4768 ( MP4_Box_Function[i_index].i_type == 0 ) )
4770 break;
4774 if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
4776 return VLC_EGENERIC;
4779 return VLC_SUCCESS;
4782 static void MP4_Box_Clean_Specific( MP4_Box_t *p_box )
4784 if( p_box->pf_free )
4785 p_box->pf_free( p_box );
4788 /*****************************************************************************
4789 * MP4_ReadBox : parse the actual box and the children
4790 * XXX : Do not go to the next box
4791 *****************************************************************************/
4792 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
4794 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
4795 if( p_box == NULL )
4796 return NULL;
4798 if( !MP4_PeekBoxHeader( p_stream, p_box ) )
4800 msg_Warn( p_stream, "cannot read one box" );
4801 free( p_box );
4802 return NULL;
4805 if( p_father && p_father->i_size > 0 &&
4806 p_father->i_pos + p_father->i_size < p_box->i_pos + p_box->i_size )
4808 msg_Dbg( p_stream, "out of bound child" );
4809 free( p_box );
4810 return NULL;
4813 if( !p_box->i_size )
4815 msg_Dbg( p_stream, "found an empty box (null size)" );
4816 free( p_box );
4817 return NULL;
4819 p_box->p_father = p_father;
4821 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
4823 uint64_t i_end = p_box->i_pos + p_box->i_size;
4824 MP4_BoxFree( p_box );
4825 MP4_Seek( p_stream, i_end ); /* Skip the failed box */
4826 return NULL;
4829 return p_box;
4832 /*****************************************************************************
4833 * MP4_BoxNew : creates and initializes an arbitrary box
4834 *****************************************************************************/
4835 MP4_Box_t * MP4_BoxNew( uint32_t i_type )
4837 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) );
4838 if( likely( p_box != NULL ) )
4840 p_box->i_type = i_type;
4842 return p_box;
4845 /*****************************************************************************
4846 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
4847 * the children
4848 *****************************************************************************/
4849 void MP4_BoxFree( MP4_Box_t *p_box )
4851 MP4_Box_t *p_child;
4853 if( !p_box )
4854 return; /* hehe */
4856 for( p_child = p_box->p_first; p_child != NULL; )
4858 MP4_Box_t *p_next;
4860 p_next = p_child->p_next;
4861 MP4_BoxFree( p_child );
4862 p_child = p_next;
4865 MP4_Box_Clean_Specific( p_box );
4867 if( p_box->data.p_payload )
4868 free( p_box->data.p_payload );
4870 free( p_box );
4873 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
4875 /* p_chunk is a virtual root container for the moof and mdat boxes */
4876 MP4_Box_t *p_fakeroot;
4877 MP4_Box_t *p_tmp_box;
4879 p_fakeroot = MP4_BoxNew( ATOM_root );
4880 if( unlikely( p_fakeroot == NULL ) )
4881 return NULL;
4882 p_fakeroot->i_shortsize = 1;
4884 const uint32_t stoplist[] = { ATOM_moov, ATOM_moof, 0 };
4885 MP4_ReadBoxContainerChildren( s, p_fakeroot, stoplist );
4887 p_tmp_box = p_fakeroot->p_first;
4888 if( p_tmp_box == NULL )
4890 MP4_BoxFree( p_fakeroot );
4891 return NULL;
4893 else while( p_tmp_box )
4895 p_fakeroot->i_size += p_tmp_box->i_size;
4896 p_tmp_box = p_tmp_box->p_next;
4899 return p_fakeroot;
4902 /*****************************************************************************
4903 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
4904 *****************************************************************************
4905 * The first box is a virtual box "root" and is the father for all first
4906 * level boxes for the file, a sort of virtual contener
4907 *****************************************************************************/
4908 MP4_Box_t *MP4_BoxGetRoot( stream_t *p_stream )
4910 int i_result;
4912 MP4_Box_t *p_vroot = MP4_BoxNew( ATOM_root );
4913 if( p_vroot == NULL )
4914 return NULL;
4916 p_vroot->i_shortsize = 1;
4917 int64_t i_size = stream_Size( p_stream );
4918 if( i_size > 0 )
4919 p_vroot->i_size = i_size;
4921 /* First get the moov */
4922 const uint32_t stoplist[] = { ATOM_moov, ATOM_mdat, 0 };
4923 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
4925 /* mdat appeared first */
4926 if( i_result && !MP4_BoxGet( p_vroot, "moov" ) )
4928 bool b_seekable;
4929 if( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_seekable ) != VLC_SUCCESS || !b_seekable )
4931 msg_Err( p_stream, "no moov before mdat and the stream is not seekable" );
4932 goto error;
4935 /* continue loading up to moov */
4936 const uint32_t stoplist[] = { ATOM_moov, 0 };
4937 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
4940 if( !i_result )
4941 goto error;
4943 /* If there is a mvex box, it means fragmented MP4, and we're done */
4944 if( MP4_BoxCount( p_vroot, "moov/mvex" ) > 0 )
4946 /* Read a bit more atoms as we might have an index between moov and moof */
4947 const uint32_t stoplist[] = { ATOM_sidx, 0 };
4948 const uint32_t excludelist[] = { ATOM_moof, ATOM_mdat, 0 };
4949 MP4_ReadBoxContainerChildrenIndexed( p_stream, p_vroot, stoplist, excludelist, false );
4950 return p_vroot;
4953 if( vlc_stream_Tell( p_stream ) + 8 < (uint64_t) stream_Size( p_stream ) )
4955 /* Get the rest of the file */
4956 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, NULL );
4958 if( !i_result )
4959 goto error;
4962 MP4_Box_t *p_moov;
4963 MP4_Box_t *p_cmov;
4965 /* check if there is a cmov, if so replace
4966 compressed moov by uncompressed one */
4967 if( ( ( p_moov = MP4_BoxGet( p_vroot, "moov" ) ) &&
4968 ( p_cmov = MP4_BoxGet( p_vroot, "moov/cmov" ) ) ) ||
4969 ( ( p_moov = MP4_BoxGet( p_vroot, "foov" ) ) &&
4970 ( p_cmov = MP4_BoxGet( p_vroot, "foov/cmov" ) ) ) )
4972 /* rename the compressed moov as a box to skip */
4973 p_moov->i_type = ATOM_skip;
4975 /* get uncompressed p_moov */
4976 p_moov = p_cmov->data.p_cmov->p_moov;
4977 p_cmov->data.p_cmov->p_moov = NULL;
4979 /* make p_root father of this new moov */
4980 p_moov->p_father = p_vroot;
4982 /* insert this new moov box as first child of p_root */
4983 p_moov->p_next = p_vroot->p_first;
4984 p_vroot->p_first = p_moov;
4987 return p_vroot;
4989 error:
4990 MP4_BoxFree( p_vroot );
4991 MP4_Seek( p_stream, 0 );
4992 return NULL;
4996 static void MP4_BoxDumpStructure_Internal( stream_t *s, const MP4_Box_t *p_box,
4997 unsigned int i_level )
4999 const MP4_Box_t *p_child;
5000 uint32_t i_displayedtype = p_box->i_type;
5001 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype)[0] = 'c';
5003 if( !i_level )
5005 msg_Dbg( s, "dumping root Box \"%4.4s\"",
5006 (char*)&i_displayedtype );
5008 else
5010 char str[512];
5011 if( i_level >= (sizeof(str) - 1)/4 )
5012 return;
5014 memset( str, ' ', sizeof(str) );
5015 for( unsigned i = 0; i < i_level; i++ )
5017 str[i*4] = '|';
5020 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
5021 "+ %4.4s size %"PRIu64" offset %" PRIuMAX "%s",
5022 (char*)&i_displayedtype, p_box->i_size,
5023 (uintmax_t)p_box->i_pos,
5024 p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
5025 msg_Dbg( s, "%s", str );
5027 p_child = p_box->p_first;
5028 while( p_child )
5030 MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
5031 p_child = p_child->p_next;
5035 void MP4_BoxDumpStructure( stream_t *s, const MP4_Box_t *p_box )
5037 MP4_BoxDumpStructure_Internal( s, p_box, 0 );
5041 /*****************************************************************************
5042 *****************************************************************************
5044 ** High level methods to acces an MP4 file
5046 *****************************************************************************
5047 *****************************************************************************/
5048 static bool get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
5050 size_t i_len ;
5051 if( !*ppsz_path[0] )
5053 *ppsz_token = NULL;
5054 *pi_number = 0;
5055 return true;
5057 i_len = strcspn( *ppsz_path, "/[" );
5058 if( !i_len && **ppsz_path == '/' )
5060 i_len = 1;
5062 *ppsz_token = strndup( *ppsz_path, i_len );
5063 if( unlikely(!*ppsz_token) )
5064 return false;
5066 *ppsz_path += i_len;
5068 /* Parse the token number token[n] */
5069 if( **ppsz_path == '[' )
5071 (*ppsz_path)++;
5072 *pi_number = strtol( *ppsz_path, NULL, 10 );
5073 while( **ppsz_path && **ppsz_path != ']' )
5075 (*ppsz_path)++;
5077 if( **ppsz_path == ']' )
5079 (*ppsz_path)++;
5082 else
5084 *pi_number = 0;
5087 /* Forward to start of next token */
5088 while( **ppsz_path == '/' )
5090 (*ppsz_path)++;
5093 return true;
5096 static void MP4_BoxGet_Internal( const MP4_Box_t **pp_result, const MP4_Box_t *p_box,
5097 const char *psz_fmt, va_list args)
5099 char *psz_dup;
5100 char *psz_path;
5101 char *psz_token = NULL;
5103 if( !p_box )
5105 *pp_result = NULL;
5106 return;
5109 if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
5110 psz_path = NULL;
5112 if( !psz_path || !psz_path[0] )
5114 free( psz_path );
5115 *pp_result = NULL;
5116 return;
5119 // fprintf( stderr, "path:'%s'\n", psz_path );
5120 psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
5121 for( ; ; )
5123 int i_number;
5125 if( !get_token( &psz_path, &psz_token, &i_number ) )
5126 goto error_box;
5127 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
5128 // psz_path,psz_token,i_number );
5129 if( !psz_token )
5131 free( psz_dup );
5132 *pp_result = p_box;
5133 return;
5135 else
5136 if( !strcmp( psz_token, "/" ) )
5138 /* Find root box */
5139 while( p_box && p_box->i_type != ATOM_root )
5141 p_box = p_box->p_father;
5143 if( !p_box )
5145 goto error_box;
5148 else
5149 if( !strcmp( psz_token, "." ) )
5151 /* Do nothing */
5153 else
5154 if( !strcmp( psz_token, ".." ) )
5156 p_box = p_box->p_father;
5157 if( !p_box )
5159 goto error_box;
5162 else
5163 if( strlen( psz_token ) == 4 )
5165 uint32_t i_fourcc;
5166 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
5167 psz_token[2], psz_token[3] );
5168 p_box = p_box->p_first;
5169 for( ; ; )
5171 if( !p_box )
5173 goto error_box;
5175 if( p_box->i_type == i_fourcc )
5177 if( !i_number )
5179 break;
5181 i_number--;
5183 p_box = p_box->p_next;
5186 else
5187 if( *psz_token == '\0' )
5189 p_box = p_box->p_first;
5190 for( ; ; )
5192 if( !p_box )
5194 goto error_box;
5196 if( !i_number )
5198 break;
5200 i_number--;
5201 p_box = p_box->p_next;
5204 else
5206 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
5207 goto error_box;
5210 FREENULL( psz_token );
5213 return;
5215 error_box:
5216 free( psz_token );
5217 free( psz_dup );
5218 *pp_result = NULL;
5219 return;
5222 /*****************************************************************************
5223 * MP4_BoxGet: find a box given a path relative to p_box
5224 *****************************************************************************
5225 * Path Format: . .. / as usual
5226 * [number] to specifie box number ex: trak[12]
5228 * ex: /moov/trak[12]
5229 * ../mdia
5230 *****************************************************************************/
5231 MP4_Box_t *MP4_BoxGet( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5233 va_list args;
5234 const MP4_Box_t *p_result;
5236 va_start( args, psz_fmt );
5237 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5238 va_end( args );
5240 return( (MP4_Box_t *) p_result );
5243 /*****************************************************************************
5244 * MP4_BoxCount: count box given a path relative to p_box
5245 *****************************************************************************
5246 * Path Format: . .. / as usual
5247 * [number] to specifie box number ex: trak[12]
5249 * ex: /moov/trak[12]
5250 * ../mdia
5251 *****************************************************************************/
5252 unsigned MP4_BoxCount( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5254 va_list args;
5255 unsigned i_count;
5256 const MP4_Box_t *p_result, *p_next;
5258 va_start( args, psz_fmt );
5259 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5260 va_end( args );
5261 if( !p_result )
5263 return( 0 );
5266 i_count = 1;
5267 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
5269 if( p_next->i_type == p_result->i_type)
5271 i_count++;
5274 return( i_count );