mp4: check STSS size before allocation
[vlc.git] / modules / demux / mp4 / libmp4.c
blobb6f2ad6eb7fe8f29e92ded8d940d4875d97fb8a9
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 MP4_READBOX_ENTER( MP4_Box_data_stsh_t, MP4_FreeBox_stsh );
3083 MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
3086 MP4_GET4BYTES( p_box->data.p_stsh->i_entry_count );
3088 p_box->data.p_stsh->i_shadowed_sample_number =
3089 calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
3090 p_box->data.p_stsh->i_sync_sample_number =
3091 calloc( p_box->data.p_stsh->i_entry_count, sizeof(uint32_t) );
3093 if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
3094 || p_box->data.p_stsh->i_sync_sample_number == NULL )
3096 MP4_READBOX_EXIT( 0 );
3099 unsigned i;
3100 for( i = 0; (i < p_box->data.p_stss->i_entry_count )&&( i_read >= 8 ); 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] );
3105 if ( i < p_box->data.p_stss->i_entry_count )
3106 p_box->data.p_stss->i_entry_count = i;
3108 #ifdef MP4_VERBOSE
3109 msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
3110 p_box->data.p_stsh->i_entry_count );
3111 #endif
3112 MP4_READBOX_EXIT( 1 );
3115 static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
3117 FREENULL( p_box->data.p_stdp->i_priority );
3120 static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
3122 MP4_READBOX_ENTER( MP4_Box_data_stdp_t, MP4_FreeBox_stdp );
3124 MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
3126 p_box->data.p_stdp->i_priority =
3127 calloc( i_read / 2, sizeof(uint16_t) );
3129 if( unlikely( !p_box->data.p_stdp->i_priority ) )
3130 MP4_READBOX_EXIT( 0 );
3132 for( unsigned i = 0; i < i_read / 2 ; i++ )
3134 MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
3137 #ifdef MP4_VERBOSE
3138 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
3139 i_read / 2 );
3141 #endif
3142 MP4_READBOX_EXIT( 1 );
3145 static void MP4_FreeBox_padb( MP4_Box_t *p_box )
3147 FREENULL( p_box->data.p_padb->i_reserved );
3148 FREENULL( p_box->data.p_padb->i_pad );
3151 static int MP4_ReadBox_padb( stream_t *p_stream, MP4_Box_t *p_box )
3153 uint32_t count;
3155 MP4_READBOX_ENTER( MP4_Box_data_padb_t, MP4_FreeBox_padb );
3157 MP4_GETVERSIONFLAGS( p_box->data.p_padb );
3158 MP4_GET4BYTES( count );
3160 if( ((count / 2) + (count & 1)) > i_read )
3162 MP4_READBOX_EXIT( 0 );
3165 p_box->data.p_padb->i_reserved = malloc( count );
3166 p_box->data.p_padb->i_pad = malloc( count );
3167 p_box->data.p_padb->i_sample_count = count;
3169 if( unlikely(p_box->data.p_padb->i_reserved == NULL
3170 || p_box->data.p_padb->i_pad == NULL) )
3172 MP4_READBOX_EXIT( 0 );
3175 for( size_t i = 0; i < count; i += 2 )
3177 p_box->data.p_padb->i_reserved[i] = ( (*p_peek) >> 7 )&0x01;
3178 p_box->data.p_padb->i_pad[i + 1] = ( (*p_peek) >> 4 )&0x07;
3179 p_box->data.p_padb->i_reserved[i + 1] = ( (*p_peek) >> 3 )&0x01;
3180 p_box->data.p_padb->i_pad[i] = ( (*p_peek) )&0x07;
3182 p_peek++;
3183 i_read--;
3186 #ifdef MP4_VERBOSE
3187 msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRIu64,
3188 i_read / 2 );
3190 #endif
3191 MP4_READBOX_EXIT( 1 );
3194 static void MP4_FreeBox_elst( MP4_Box_t *p_box )
3196 FREENULL( p_box->data.p_elst->i_segment_duration );
3197 FREENULL( p_box->data.p_elst->i_media_time );
3198 FREENULL( p_box->data.p_elst->i_media_rate_integer );
3199 FREENULL( p_box->data.p_elst->i_media_rate_fraction );
3202 static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
3204 MP4_READBOX_ENTER( MP4_Box_data_elst_t, MP4_FreeBox_elst );
3206 MP4_GETVERSIONFLAGS( p_box->data.p_elst );
3209 MP4_GET4BYTES( p_box->data.p_elst->i_entry_count );
3211 uint32_t i_entries_max = i_read / ((p_box->data.p_elst->i_version == 1) ? 20 : 12);
3212 if( p_box->data.p_elst->i_entry_count > i_entries_max )
3213 p_box->data.p_elst->i_entry_count = i_entries_max;
3215 p_box->data.p_elst->i_segment_duration =
3216 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint64_t) );
3217 p_box->data.p_elst->i_media_time =
3218 calloc( p_box->data.p_elst->i_entry_count, sizeof(int64_t) );
3219 p_box->data.p_elst->i_media_rate_integer =
3220 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
3221 p_box->data.p_elst->i_media_rate_fraction =
3222 calloc( p_box->data.p_elst->i_entry_count, sizeof(uint16_t) );
3223 if( p_box->data.p_elst->i_segment_duration == NULL
3224 || p_box->data.p_elst->i_media_time == NULL
3225 || p_box->data.p_elst->i_media_rate_integer == NULL
3226 || p_box->data.p_elst->i_media_rate_fraction == NULL )
3228 MP4_READBOX_EXIT( 0 );
3231 for( uint32_t i = 0; i < p_box->data.p_elst->i_entry_count; i++ )
3233 if( p_box->data.p_elst->i_version == 1 )
3235 MP4_GET8BYTES( p_box->data.p_elst->i_segment_duration[i] );
3236 MP4_GET8BYTES( p_box->data.p_elst->i_media_time[i] );
3238 else
3240 MP4_GET4BYTES( p_box->data.p_elst->i_segment_duration[i] );
3241 MP4_GET4BYTES( p_box->data.p_elst->i_media_time[i] );
3242 p_box->data.p_elst->i_media_time[i] = (int32_t)p_box->data.p_elst->i_media_time[i];
3245 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_integer[i] );
3246 MP4_GET2BYTES( p_box->data.p_elst->i_media_rate_fraction[i] );
3249 #ifdef MP4_VERBOSE
3250 msg_Dbg( p_stream, "read box: \"elst\" entry-count %" PRIu32,
3251 p_box->data.p_elst->i_entry_count );
3252 #endif
3253 MP4_READBOX_EXIT( 1 );
3256 static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
3258 FREENULL( p_box->data.p_cprt->psz_notice );
3261 static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
3263 uint16_t i_language;
3264 bool b_mac;
3266 MP4_READBOX_ENTER( MP4_Box_data_cprt_t, MP4_FreeBox_cprt );
3268 MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
3270 MP4_GET2BYTES( i_language );
3271 decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
3273 MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
3275 #ifdef MP4_VERBOSE
3276 msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
3277 p_box->data.p_cprt->rgs_language,
3278 p_box->data.p_cprt->psz_notice );
3280 #endif
3281 MP4_READBOX_EXIT( 1 );
3284 static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
3286 MP4_READBOX_ENTER( MP4_Box_data_dcom_t, NULL );
3288 MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
3289 #ifdef MP4_VERBOSE
3290 msg_Dbg( p_stream,
3291 "read box: \"dcom\" compression algorithm : %4.4s",
3292 (char*)&p_box->data.p_dcom->i_algorithm );
3293 #endif
3294 MP4_READBOX_EXIT( 1 );
3297 static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
3299 FREENULL( p_box->data.p_cmvd->p_data );
3302 static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
3304 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t, MP4_FreeBox_cmvd );
3306 MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
3308 p_box->data.p_cmvd->i_compressed_size = i_read;
3310 if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
3311 MP4_READBOX_EXIT( 0 );
3313 /* now copy compressed data */
3314 memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
3316 p_box->data.p_cmvd->b_compressed = 1;
3318 #ifdef MP4_VERBOSE
3319 msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
3320 p_box->data.p_cmvd->i_compressed_size );
3321 #endif
3323 MP4_READBOX_EXIT( 1 );
3326 static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
3328 MP4_Box_t *p_dcom;
3329 MP4_Box_t *p_cmvd;
3331 #ifdef HAVE_ZLIB_H
3332 stream_t *p_stream_memory;
3333 z_stream z_data;
3334 uint8_t *p_data;
3335 int i_result;
3336 #endif
3338 if( !( p_box->data.p_cmov = calloc(1, sizeof( MP4_Box_data_cmov_t ) ) ) )
3339 return 0;
3341 if( !p_box->p_father ||
3342 ( p_box->p_father->i_type != ATOM_moov &&
3343 p_box->p_father->i_type != ATOM_foov ) )
3345 msg_Warn( p_stream, "Read box: \"cmov\" box alone" );
3346 return 1;
3349 if( !MP4_ReadBoxContainer( p_stream, p_box ) )
3351 return 0;
3354 if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
3355 ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
3356 p_cmvd->data.p_cmvd->p_data == NULL )
3358 msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
3359 return 0;
3362 if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
3364 msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
3365 "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
3366 return 0;
3369 #ifndef HAVE_ZLIB_H
3370 msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
3371 return 0;
3373 #else
3374 /* decompress data */
3375 /* allocate a new buffer */
3376 if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
3377 return 0;
3378 /* init default structures */
3379 z_data.next_in = p_cmvd->data.p_cmvd->p_data;
3380 z_data.avail_in = p_cmvd->data.p_cmvd->i_compressed_size;
3381 z_data.next_out = p_data;
3382 z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
3383 z_data.zalloc = (alloc_func)Z_NULL;
3384 z_data.zfree = (free_func)Z_NULL;
3385 z_data.opaque = (voidpf)Z_NULL;
3387 /* init zlib */
3388 if( inflateInit( &z_data ) != Z_OK )
3390 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3391 free( p_data );
3392 return 0;
3395 /* uncompress */
3396 i_result = inflate( &z_data, Z_NO_FLUSH );
3397 if( i_result != Z_OK && i_result != Z_STREAM_END )
3399 msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3400 free( p_data );
3401 return 0;
3404 if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
3406 msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
3407 "mismatch" );
3409 p_cmvd->data.p_cmvd->i_uncompressed_size = z_data.total_out;
3411 /* close zlib */
3412 if( inflateEnd( &z_data ) != Z_OK )
3414 msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
3415 "data (ignored)" );
3418 free( p_cmvd->data.p_cmvd->p_data );
3419 p_cmvd->data.p_cmvd->p_data = p_data;
3420 p_cmvd->data.p_cmvd->b_compressed = 0;
3422 msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
3424 /* now create a memory stream */
3425 p_stream_memory =
3426 vlc_stream_MemoryNew( VLC_OBJECT(p_stream),
3427 p_cmvd->data.p_cmvd->p_data,
3428 p_cmvd->data.p_cmvd->i_uncompressed_size, true );
3430 /* and read uncompressd moov */
3431 p_box->data.p_cmov->p_moov = MP4_ReadBox( p_stream_memory, NULL );
3433 vlc_stream_Delete( p_stream_memory );
3435 #ifdef MP4_VERBOSE
3436 msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
3437 #endif
3439 return p_box->data.p_cmov->p_moov ? 1 : 0;
3440 #endif /* HAVE_ZLIB_H */
3443 static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
3445 FREENULL( p_box->data.p_rdrf->psz_ref );
3448 static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
3450 uint32_t i_len;
3451 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t, MP4_FreeBox_rdrf );
3453 MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
3454 MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
3455 MP4_GET4BYTES( i_len );
3456 i_len++;
3458 if( i_len > 0 )
3460 p_box->data.p_rdrf->psz_ref = malloc( i_len );
3461 if( p_box->data.p_rdrf->psz_ref == NULL )
3462 MP4_READBOX_EXIT( 0 );
3463 i_len--;
3465 for( unsigned i = 0; i < i_len; i++ )
3467 MP4_GET1BYTE( p_box->data.p_rdrf->psz_ref[i] );
3469 p_box->data.p_rdrf->psz_ref[i_len] = '\0';
3471 else
3473 p_box->data.p_rdrf->psz_ref = NULL;
3476 #ifdef MP4_VERBOSE
3477 msg_Dbg( p_stream,
3478 "read box: \"rdrf\" type:%4.4s ref %s",
3479 (char*)&p_box->data.p_rdrf->i_ref_type,
3480 p_box->data.p_rdrf->psz_ref );
3481 #endif
3482 MP4_READBOX_EXIT( 1 );
3487 static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
3489 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t, NULL );
3491 MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
3493 MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
3495 #ifdef MP4_VERBOSE
3496 msg_Dbg( p_stream,
3497 "read box: \"rmdr\" rate:%d",
3498 p_box->data.p_rmdr->i_rate );
3499 #endif
3500 MP4_READBOX_EXIT( 1 );
3503 static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
3505 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t, NULL );
3507 MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
3509 #ifdef MP4_VERBOSE
3510 msg_Dbg( p_stream,
3511 "read box: \"rmqu\" quality:%d",
3512 p_box->data.p_rmqu->i_quality );
3513 #endif
3514 MP4_READBOX_EXIT( 1 );
3517 static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
3519 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t, NULL );
3520 MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
3522 MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
3523 MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
3524 MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
3525 MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
3527 #ifdef MP4_VERBOSE
3528 msg_Dbg( p_stream,
3529 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
3530 (char*)&p_box->data.p_rmvc->i_gestaltType,
3531 p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
3532 p_box->data.p_rmvc->i_checkType );
3533 #endif
3535 MP4_READBOX_EXIT( 1 );
3538 static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
3540 MP4_READBOX_ENTER( MP4_Box_data_frma_t, NULL );
3542 MP4_GETFOURCC( p_box->data.p_frma->i_type );
3544 #ifdef MP4_VERBOSE
3545 msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
3546 (char *)&p_box->data.p_frma->i_type );
3547 #endif
3549 MP4_READBOX_EXIT( 1 );
3552 static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
3554 MP4_READBOX_ENTER( MP4_Box_data_skcr_t, NULL );
3556 MP4_GET4BYTES( p_box->data.p_skcr->i_init );
3557 MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
3558 MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
3560 #ifdef MP4_VERBOSE
3561 msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
3562 p_box->data.p_skcr->i_init,
3563 p_box->data.p_skcr->i_encr,
3564 p_box->data.p_skcr->i_decr );
3565 #endif
3567 MP4_READBOX_EXIT( 1 );
3570 static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
3572 VLC_UNUSED(p_box);
3573 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
3574 * so unless data decrypt itself by magic, there will be no playback,
3575 * but we never know... */
3576 msg_Warn( p_stream, "DRM protected streams are not supported." );
3577 return 1;
3580 static void MP4_FreeBox_Binary( MP4_Box_t *p_box )
3582 FREENULL( p_box->data.p_binary->p_blob );
3583 p_box->data.p_binary->i_blob = 0;
3586 static int MP4_ReadBox_Binary( stream_t *p_stream, MP4_Box_t *p_box )
3588 MP4_READBOX_ENTER( MP4_Box_data_binary_t, MP4_FreeBox_Binary );
3589 i_read = __MIN( i_read, UINT32_MAX );
3590 if ( i_read > 0 )
3592 p_box->data.p_binary->p_blob = malloc( i_read );
3593 if ( p_box->data.p_binary->p_blob )
3595 memcpy( p_box->data.p_binary->p_blob, p_peek, i_read );
3596 p_box->data.p_binary->i_blob = i_read;
3599 MP4_READBOX_EXIT( 1 );
3602 static void MP4_FreeBox_data( MP4_Box_t *p_box )
3604 free( p_box->data.p_data->p_blob );
3607 static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
3609 MP4_READBOX_ENTER( MP4_Box_data_data_t, MP4_FreeBox_data );
3610 MP4_Box_data_data_t *p_data = p_box->data.p_data;
3612 if ( i_read < 8 || i_read - 8 > UINT32_MAX )
3613 MP4_READBOX_EXIT( 0 );
3615 uint8_t i_type;
3616 MP4_GET1BYTE( i_type );
3617 if ( i_type != 0 )
3619 #ifdef MP4_VERBOSE
3620 msg_Dbg( p_stream, "skipping unknown 'data' atom with type %"PRIu8, i_type );
3621 #endif
3622 MP4_READBOX_EXIT( 0 );
3625 MP4_GET3BYTES( p_data->e_wellknowntype );
3626 MP4_GET2BYTES( p_data->locale.i_country );
3627 MP4_GET2BYTES( p_data->locale.i_language );
3628 #ifdef MP4_VERBOSE
3629 msg_Dbg( p_stream, "read 'data' atom: knowntype=%"PRIu32", country=%"PRIu16" lang=%"PRIu16
3630 ", size %"PRIu64" bytes", p_data->e_wellknowntype,
3631 p_data->locale.i_country, p_data->locale.i_language, i_read );
3632 #endif
3633 p_box->data.p_data->p_blob = malloc( i_read );
3634 if ( !p_box->data.p_data->p_blob )
3635 MP4_READBOX_EXIT( 0 );
3637 p_box->data.p_data->i_blob = i_read;
3638 memcpy( p_box->data.p_data->p_blob, p_peek, i_read);
3640 MP4_READBOX_EXIT( 1 );
3643 static int MP4_ReadBox_Metadata( stream_t *p_stream, MP4_Box_t *p_box )
3645 const uint8_t *p_peek;
3646 if ( vlc_stream_Peek( p_stream, &p_peek, 16 ) < 16 )
3647 return 0;
3648 if ( vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
3649 return 0;
3650 const uint32_t stoplist[] = { ATOM_data, 0 };
3651 return MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist );
3654 /* Chapter support */
3655 static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
3657 MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
3658 for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
3659 free( p_chpl->chapter[i].psz_name );
3662 static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
3664 MP4_Box_data_chpl_t *p_chpl;
3665 uint32_t i_dummy;
3666 VLC_UNUSED(i_dummy);
3667 int i;
3668 MP4_READBOX_ENTER( MP4_Box_data_chpl_t, MP4_FreeBox_chpl );
3670 p_chpl = p_box->data.p_chpl;
3672 MP4_GETVERSIONFLAGS( p_chpl );
3674 if ( i_read < 5 || p_chpl->i_version != 0x1 )
3675 MP4_READBOX_EXIT( 0 );
3677 MP4_GET4BYTES( i_dummy );
3679 MP4_GET1BYTE( p_chpl->i_chapter );
3681 for( i = 0; i < p_chpl->i_chapter; i++ )
3683 uint64_t i_start;
3684 uint8_t i_len;
3685 int i_copy;
3686 if ( i_read < 9 )
3687 break;
3688 MP4_GET8BYTES( i_start );
3689 MP4_GET1BYTE( i_len );
3691 p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
3692 if( !p_chpl->chapter[i].psz_name )
3693 MP4_READBOX_EXIT( 0 );
3695 i_copy = __MIN( i_len, i_read );
3696 if( i_copy > 0 )
3697 memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
3698 p_chpl->chapter[i].psz_name[i_copy] = '\0';
3699 p_chpl->chapter[i].i_start = i_start;
3701 p_peek += i_copy;
3702 i_read -= i_copy;
3705 if ( i != p_chpl->i_chapter )
3706 p_chpl->i_chapter = i;
3708 /* Bubble sort by increasing start date */
3711 for( i = 0; i < p_chpl->i_chapter - 1; i++ )
3713 if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
3715 char *psz = p_chpl->chapter[i+1].psz_name;
3716 int64_t i64 = p_chpl->chapter[i+1].i_start;
3718 p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
3719 p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
3721 p_chpl->chapter[i].psz_name = psz;
3722 p_chpl->chapter[i].i_start = i64;
3724 i = -1;
3725 break;
3728 } while( i == -1 );
3730 #ifdef MP4_VERBOSE
3731 msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
3732 p_chpl->i_chapter );
3733 #endif
3734 MP4_READBOX_EXIT( 1 );
3737 /* GoPro HiLight tags support */
3738 static void MP4_FreeBox_HMMT( MP4_Box_t *p_box )
3740 FREENULL( p_box->data.p_hmmt->pi_chapter_start );
3743 static int MP4_ReadBox_HMMT( stream_t *p_stream, MP4_Box_t *p_box )
3745 #define MAX_CHAPTER_COUNT 100
3747 MP4_Box_data_HMMT_t *p_hmmt;
3748 MP4_READBOX_ENTER( MP4_Box_data_HMMT_t, MP4_FreeBox_HMMT );
3750 if( i_read < 4 )
3751 MP4_READBOX_EXIT( 0 );
3753 p_hmmt = p_box->data.p_hmmt;
3755 MP4_GET4BYTES( p_hmmt->i_chapter_count );
3757 if( p_hmmt->i_chapter_count <= 0 )
3759 p_hmmt->pi_chapter_start = NULL;
3760 MP4_READBOX_EXIT( 1 );
3763 if( ( i_read / sizeof(uint32_t) ) < p_hmmt->i_chapter_count )
3764 MP4_READBOX_EXIT( 0 );
3766 /* Cameras are allowing a maximum of 100 tags */
3767 if( p_hmmt->i_chapter_count > MAX_CHAPTER_COUNT )
3768 p_hmmt->i_chapter_count = MAX_CHAPTER_COUNT;
3770 p_hmmt->pi_chapter_start = vlc_alloc( p_hmmt->i_chapter_count, sizeof(uint32_t) );
3771 if( p_hmmt->pi_chapter_start == NULL )
3772 MP4_READBOX_EXIT( 0 );
3774 for( uint32_t i = 0; i < p_hmmt->i_chapter_count; i++ )
3776 MP4_GET4BYTES( p_hmmt->pi_chapter_start[i] );
3779 #ifdef MP4_VERBOSE
3780 msg_Dbg( p_stream, "read box: \"HMMT\" %d HiLight tags", p_hmmt->i_chapter_count );
3781 #endif
3783 MP4_READBOX_EXIT( 1 );
3786 static void MP4_FreeBox_tref_generic( MP4_Box_t *p_box )
3788 FREENULL( p_box->data.p_tref_generic->i_track_ID );
3791 static int MP4_ReadBox_tref_generic( stream_t *p_stream, MP4_Box_t *p_box )
3793 MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t, MP4_FreeBox_tref_generic );
3795 p_box->data.p_tref_generic->i_track_ID = NULL;
3796 p_box->data.p_tref_generic->i_entry_count = i_read / sizeof(uint32_t);
3797 if( p_box->data.p_tref_generic->i_entry_count > 0 )
3798 p_box->data.p_tref_generic->i_track_ID = calloc( p_box->data.p_tref_generic->i_entry_count, sizeof(uint32_t) );
3799 if( p_box->data.p_tref_generic->i_track_ID == NULL )
3800 MP4_READBOX_EXIT( 0 );
3802 for( unsigned i = 0; i < p_box->data.p_tref_generic->i_entry_count; i++ )
3804 MP4_GET4BYTES( p_box->data.p_tref_generic->i_track_ID[i] );
3806 #ifdef MP4_VERBOSE
3807 msg_Dbg( p_stream, "read box: \"chap\" %d references",
3808 p_box->data.p_tref_generic->i_entry_count );
3809 #endif
3811 MP4_READBOX_EXIT( 1 );
3814 static void MP4_FreeBox_keys( MP4_Box_t *p_box )
3816 for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
3817 free( p_box->data.p_keys->p_entries[i].psz_value );
3818 free( p_box->data.p_keys->p_entries );
3821 static int MP4_ReadBox_keys( stream_t *p_stream, MP4_Box_t *p_box )
3823 MP4_READBOX_ENTER( MP4_Box_data_keys_t, MP4_FreeBox_keys );
3825 if ( i_read < 8 )
3826 MP4_READBOX_EXIT( 0 );
3828 uint32_t i_count;
3829 MP4_GET4BYTES( i_count ); /* reserved + flags */
3830 if ( i_count != 0 )
3831 MP4_READBOX_EXIT( 0 );
3833 MP4_GET4BYTES( i_count );
3834 p_box->data.p_keys->p_entries = calloc( i_count, sizeof(*p_box->data.p_keys->p_entries) );
3835 if ( !p_box->data.p_keys->p_entries )
3836 MP4_READBOX_EXIT( 0 );
3837 p_box->data.p_keys->i_entry_count = i_count;
3839 uint32_t i=0;
3840 for( ; i < i_count; i++ )
3842 if ( i_read < 8 )
3843 break;
3844 uint32_t i_keysize;
3845 MP4_GET4BYTES( i_keysize );
3846 if ( (i_keysize < 8) || (i_keysize - 4 > i_read) )
3847 break;
3848 MP4_GETFOURCC( p_box->data.p_keys->p_entries[i].i_namespace );
3849 i_keysize -= 8;
3850 p_box->data.p_keys->p_entries[i].psz_value = malloc( i_keysize + 1 );
3851 if ( !p_box->data.p_keys->p_entries[i].psz_value )
3852 break;
3853 memcpy( p_box->data.p_keys->p_entries[i].psz_value, p_peek, i_keysize );
3854 p_box->data.p_keys->p_entries[i].psz_value[i_keysize] = 0;
3855 p_peek += i_keysize;
3856 i_read -= i_keysize;
3857 #ifdef MP4_ULTRA_VERBOSE
3858 msg_Dbg( p_stream, "read box: \"keys\": %u '%s'", i + 1,
3859 p_box->data.p_keys->p_entries[i].psz_value );
3860 #endif
3862 if ( i < i_count )
3863 p_box->data.p_keys->i_entry_count = i;
3865 MP4_READBOX_EXIT( 1 );
3868 static int MP4_ReadBox_colr( stream_t *p_stream, MP4_Box_t *p_box )
3870 MP4_READBOX_ENTER( MP4_Box_data_colr_t, NULL );
3871 MP4_GETFOURCC( p_box->data.p_colr->i_type );
3872 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'c' ) ||
3873 p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3875 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_primary_idx );
3876 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_transfer_function_idx );
3877 MP4_GET2BYTES( p_box->data.p_colr->nclc.i_matrix_idx );
3878 if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3879 MP4_GET1BYTE( p_box->data.p_colr->nclc.i_full_range );
3881 else
3883 #ifdef MP4_VERBOSE
3884 msg_Warn( p_stream, "Unhandled colr type: %4.4s", (char*)&p_box->data.p_colr->i_type );
3885 #endif
3887 MP4_READBOX_EXIT( 1 );
3890 static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
3892 const uint8_t *p_peek;
3893 const size_t i_headersize = mp4_box_headersize( p_box );
3895 if( p_box->i_size < 16 || p_box->i_size - i_headersize < 8 )
3896 return 0;
3898 /* skip over box header */
3899 if( vlc_stream_Read( p_stream, NULL, i_headersize ) < (ssize_t) i_headersize )
3900 return 0;
3902 /* meta content starts with a 4 byte version/flags value (should be 0) */
3903 if( vlc_stream_Peek( p_stream, &p_peek, 8 ) < 8 )
3904 return 0;
3906 if( !memcmp( p_peek, "\0\0\0", 4 ) ) /* correct header case */
3908 if( vlc_stream_Read( p_stream, NULL, 4 ) < 4 )
3909 return 0;
3911 else if( memcmp( &p_peek[4], "hdlr", 4 ) ) /* Broken, headerless ones */
3913 return 0;
3916 /* load child atoms up to the handler (which should be next anyway) */
3917 const uint32_t stoplist[] = { ATOM_hdlr, 0 };
3918 if ( !MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist ) )
3919 return 0;
3921 /* Mandatory */
3922 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box, "hdlr" );
3923 if ( p_hdlr && BOXDATA(p_hdlr) && BOXDATA(p_hdlr)->i_version == 0 )
3925 p_box->i_handler = BOXDATA(p_hdlr)->i_handler_type;
3926 switch( p_box->i_handler )
3928 case HANDLER_mdta:
3929 case HANDLER_mdir:
3930 /* then it behaves like a container */
3931 return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
3932 default:
3933 /* skip parsing, will be seen as empty container */
3934 break;
3938 return 1;
3941 static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
3943 char i_unused;
3944 VLC_UNUSED(i_unused);
3946 MP4_READBOX_ENTER( MP4_Box_data_iods_t, NULL );
3947 MP4_GETVERSIONFLAGS( p_box->data.p_iods );
3949 MP4_GET1BYTE( i_unused ); /* tag */
3950 MP4_GET1BYTE( i_unused ); /* length */
3952 MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
3953 are used for other flags */
3954 MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
3955 MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
3956 MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
3957 MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
3958 MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
3960 #ifdef MP4_VERBOSE
3961 msg_Dbg( p_stream,
3962 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3963 "visual: %i, graphics: %i",
3964 p_box->data.p_iods->i_object_descriptor >> 6,
3965 p_box->data.p_iods->i_OD_profile_level,
3966 p_box->data.p_iods->i_scene_profile_level,
3967 p_box->data.p_iods->i_audio_profile_level,
3968 p_box->data.p_iods->i_visual_profile_level,
3969 p_box->data.p_iods->i_graphics_profile_level );
3970 #endif
3972 MP4_READBOX_EXIT( 1 );
3975 static int MP4_ReadBox_btrt( stream_t *p_stream, MP4_Box_t *p_box )
3977 MP4_READBOX_ENTER( MP4_Box_data_btrt_t, NULL );
3979 if(i_read != 12)
3980 MP4_READBOX_EXIT( 0 );
3982 MP4_GET4BYTES( p_box->data.p_btrt->i_buffer_size );
3983 MP4_GET4BYTES( p_box->data.p_btrt->i_max_bitrate );
3984 MP4_GET4BYTES( p_box->data.p_btrt->i_avg_bitrate );
3986 MP4_READBOX_EXIT( 1 );
3989 static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
3991 MP4_READBOX_ENTER( MP4_Box_data_pasp_t, NULL );
3993 MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
3994 MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
3996 #ifdef MP4_VERBOSE
3997 msg_Dbg( p_stream,
3998 "read box: \"paps\" %dx%d",
3999 p_box->data.p_pasp->i_horizontal_spacing,
4000 p_box->data.p_pasp->i_vertical_spacing);
4001 #endif
4003 MP4_READBOX_EXIT( 1 );
4006 static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
4008 MP4_READBOX_ENTER( MP4_Box_data_mehd_t, NULL );
4010 MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
4011 if( p_box->data.p_mehd->i_version == 1 )
4012 MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
4013 else /* version == 0 */
4014 MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
4016 #ifdef MP4_VERBOSE
4017 msg_Dbg( p_stream,
4018 "read box: \"mehd\" frag dur. %"PRIu64"",
4019 p_box->data.p_mehd->i_fragment_duration );
4020 #endif
4022 MP4_READBOX_EXIT( 1 );
4025 static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
4027 MP4_READBOX_ENTER( MP4_Box_data_trex_t, NULL );
4028 MP4_GETVERSIONFLAGS( p_box->data.p_trex );
4030 MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
4031 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
4032 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
4033 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
4034 MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
4036 #ifdef MP4_VERBOSE
4037 msg_Dbg( p_stream,
4038 "read box: \"trex\" trackID: %"PRIu32"",
4039 p_box->data.p_trex->i_track_ID );
4040 #endif
4042 MP4_READBOX_EXIT( 1 );
4045 static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
4047 FREENULL( p_box->data.p_sdtp->p_sample_table );
4050 static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
4052 uint32_t i_sample_count;
4053 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t, MP4_FreeBox_sdtp );
4054 MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
4055 MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
4056 i_sample_count = i_read;
4058 p_sdtp->p_sample_table = calloc( i_sample_count, 1 );
4060 if( !p_sdtp->p_sample_table )
4061 MP4_READBOX_EXIT( 0 );
4063 for( uint32_t i = 0; i < i_sample_count; i++ )
4064 MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
4066 #ifdef MP4_VERBOSE
4067 msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
4068 if ( i_sample_count > 3 )
4069 msg_Dbg( p_stream,
4070 "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
4071 p_sdtp->p_sample_table[0],
4072 p_sdtp->p_sample_table[1],
4073 p_sdtp->p_sample_table[2],
4074 p_sdtp->p_sample_table[3] );
4075 #endif
4077 MP4_READBOX_EXIT( 1 );
4080 static int MP4_ReadBox_tsel( stream_t *p_stream, MP4_Box_t *p_box )
4082 MP4_READBOX_ENTER( MP4_Box_data_tsel_t, NULL );
4083 uint32_t i_version;
4084 MP4_GET4BYTES( i_version );
4085 if ( i_version != 0 || i_read < 4 )
4086 MP4_READBOX_EXIT( 0 );
4087 MP4_GET4BYTES( p_box->data.p_tsel->i_switch_group );
4088 /* ignore list of attributes as es are present before switch */
4089 MP4_READBOX_EXIT( 1 );
4092 static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
4094 MP4_READBOX_ENTER( MP4_Box_data_mfro_t, NULL );
4096 MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
4097 MP4_GET4BYTES( p_box->data.p_mfro->i_size );
4099 #ifdef MP4_VERBOSE
4100 msg_Dbg( p_stream,
4101 "read box: \"mfro\" size: %"PRIu32"",
4102 p_box->data.p_mfro->i_size);
4103 #endif
4105 MP4_READBOX_EXIT( 1 );
4108 static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
4110 FREENULL( p_box->data.p_tfra->p_time );
4111 FREENULL( p_box->data.p_tfra->p_moof_offset );
4112 FREENULL( p_box->data.p_tfra->p_traf_number );
4113 FREENULL( p_box->data.p_tfra->p_trun_number );
4114 FREENULL( p_box->data.p_tfra->p_sample_number );
4117 static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
4119 #define READ_VARIABLE_LENGTH(lengthvar, p_array) switch (lengthvar)\
4121 case 0:\
4122 MP4_GET1BYTE( p_array[i] );\
4123 break;\
4124 case 1:\
4125 MP4_GET2BYTES( *((uint16_t *)&p_array[i*2]) );\
4126 break;\
4127 case 2:\
4128 MP4_GET3BYTES( *((uint32_t *)&p_array[i*4]) );\
4129 break;\
4130 case 3:\
4131 MP4_GET4BYTES( *((uint32_t *)&p_array[i*4]) );\
4132 break;\
4133 default:\
4134 goto error;\
4136 #define FIX_VARIABLE_LENGTH(lengthvar) if ( lengthvar == 3 ) lengthvar = 4
4138 uint32_t i_number_of_entries;
4139 MP4_READBOX_ENTER( MP4_Box_data_tfra_t, MP4_FreeBox_tfra );
4140 MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
4141 MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
4142 if ( p_tfra->i_version > 1 )
4143 MP4_READBOX_EXIT( 0 );
4144 MP4_GET4BYTES( p_tfra->i_track_ID );
4145 uint32_t i_lengths = 0;
4146 MP4_GET4BYTES( i_lengths );
4147 MP4_GET4BYTES( p_tfra->i_number_of_entries );
4148 i_number_of_entries = p_tfra->i_number_of_entries;
4149 p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
4150 p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
4151 p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
4153 size_t size = 4 + 4*p_tfra->i_version; /* size in {4, 8} */
4154 p_tfra->p_time = calloc( i_number_of_entries, size );
4155 p_tfra->p_moof_offset = calloc( i_number_of_entries, size );
4157 size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
4158 if ( size == 3 ) size++;
4159 p_tfra->p_traf_number = calloc( i_number_of_entries, size );
4160 size = 1 + p_tfra->i_length_size_of_trun_num;
4161 if ( size == 3 ) size++;
4162 p_tfra->p_trun_number = calloc( i_number_of_entries, size );
4163 size = 1 + p_tfra->i_length_size_of_sample_num;
4164 if ( size == 3 ) size++;
4165 p_tfra->p_sample_number = calloc( i_number_of_entries, size );
4167 if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
4168 || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
4169 goto error;
4171 unsigned i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
4172 + p_tfra->i_length_size_of_trun_num
4173 + p_tfra->i_length_size_of_sample_num;
4175 uint32_t i;
4176 for( i = 0; i < i_number_of_entries; i++ )
4179 if( p_tfra->i_version == 1 )
4181 if ( i_read < i_fields_length + 16 )
4182 break;
4183 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_time[i*2]) );
4184 MP4_GET8BYTES( *((uint64_t *)&p_tfra->p_moof_offset[i*2]) );
4186 else
4188 if ( i_read < i_fields_length + 8 )
4189 break;
4190 MP4_GET4BYTES( p_tfra->p_time[i] );
4191 MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
4194 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num, p_tfra->p_traf_number);
4195 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num, p_tfra->p_trun_number);
4196 READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num, p_tfra->p_sample_number);
4198 if ( i < i_number_of_entries )
4199 i_number_of_entries = i;
4201 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num);
4202 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num);
4203 FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num);
4205 #ifdef MP4_ULTRA_VERBOSE
4206 for( i = 0; i < i_number_of_entries; i++ )
4208 if( p_tfra->i_version == 0 )
4210 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu32", "
4211 "moof_offset[%"PRIu32"]: %"PRIu32"",
4212 p_tfra->i_track_ID,
4213 i, p_tfra->p_time[i],
4214 i, p_tfra->p_moof_offset[i] );
4216 else
4218 msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu64", "
4219 "moof_offset[%"PRIu32"]: %"PRIu64"",
4220 p_tfra->i_track_ID,
4221 i, ((uint64_t *)(p_tfra->p_time))[i],
4222 i, ((uint64_t *)(p_tfra->p_moof_offset))[i] );
4225 #endif
4226 #ifdef MP4_VERBOSE
4227 msg_Dbg( p_stream, "tfra[%"PRIu32"] %"PRIu32" entries",
4228 p_tfra->i_track_ID, i_number_of_entries );
4229 #endif
4231 MP4_READBOX_EXIT( 1 );
4232 error:
4233 MP4_READBOX_EXIT( 0 );
4235 #undef READ_VARIABLE_LENGTH
4236 #undef FIX_VARIABLE_LENGTH
4239 static int MP4_ReadBox_pnot( stream_t *p_stream, MP4_Box_t *p_box )
4241 if ( p_box->i_size != 20 )
4242 return 0;
4243 MP4_READBOX_ENTER( MP4_Box_data_pnot_t, NULL );
4244 MP4_GET4BYTES( p_box->data.p_pnot->i_date );
4245 uint16_t i_version;
4246 MP4_GET2BYTES( i_version );
4247 if ( i_version != 0 )
4248 MP4_READBOX_EXIT( 0 );
4249 MP4_GETFOURCC( p_box->data.p_pnot->i_type );
4250 MP4_GET2BYTES( p_box->data.p_pnot->i_index );
4251 MP4_READBOX_EXIT( 1 );
4254 static int MP4_ReadBox_SA3D( stream_t *p_stream, MP4_Box_t *p_box )
4256 MP4_READBOX_ENTER( MP4_Box_data_SA3D_t, NULL );
4258 uint8_t i_version;
4259 MP4_GET1BYTE( i_version );
4260 if ( i_version != 0 )
4261 MP4_READBOX_EXIT( 0 );
4263 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_type );
4264 MP4_GET4BYTES( p_box->data.p_SA3D->i_ambisonic_order );
4265 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_channel_ordering );
4266 MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_normalization );
4267 MP4_GET4BYTES( p_box->data.p_SA3D->i_num_channels );
4268 MP4_READBOX_EXIT( 1 );
4271 /* For generic */
4272 static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
4274 if( !p_box->p_father )
4276 goto unknown;
4278 if( p_box->p_father->i_type == ATOM_stsd )
4280 MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../.." );
4281 MP4_Box_t *p_hdlr;
4283 if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
4284 (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
4286 goto unknown;
4288 switch( p_hdlr->data.p_hdlr->i_handler_type )
4290 case ATOM_soun:
4291 return MP4_ReadBox_sample_soun( p_stream, p_box );
4292 case ATOM_vide:
4293 return MP4_ReadBox_sample_vide( p_stream, p_box );
4294 case ATOM_hint:
4295 return MP4_ReadBox_sample_hint8( p_stream, p_box );
4296 case ATOM_text:
4297 return MP4_ReadBox_sample_text( p_stream, p_box );
4298 case ATOM_tx3g:
4299 case ATOM_sbtl:
4300 return MP4_ReadBox_sample_tx3g( p_stream, p_box );
4301 default:
4302 msg_Warn( p_stream,
4303 "unknown handler type in stsd (incompletely loaded)" );
4304 return 1;
4308 unknown:
4309 if MP4_BOX_TYPE_ASCII()
4310 msg_Warn( p_stream,
4311 "unknown box type %4.4s (incompletely loaded)",
4312 (char*)&p_box->i_type );
4313 else
4314 msg_Warn( p_stream,
4315 "unknown box type c%3.3s (incompletely loaded)",
4316 (char*)&p_box->i_type+1 );
4317 p_box->e_flags |= BOX_FLAG_INCOMPLETE;
4319 return 1;
4322 /**** ------------------------------------------------------------------- ****/
4324 static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
4326 if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
4327 return MP4_ReadBox_tfrf( p_stream, p_box );
4328 if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
4329 return MP4_ReadBox_tfxd( p_stream, p_box );
4330 if( !CmpUUID( &p_box->i_uuid, &XML360BoxUUID ) )
4331 return MP4_ReadBox_XML360( p_stream, p_box );
4332 if( !CmpUUID( &p_box->i_uuid, &PS3DDSBoxUUID ) && p_box->i_size == 28 )
4333 return MP4_ReadBox_Binary( p_stream, p_box );
4335 #ifdef MP4_VERBOSE
4336 msg_Warn( p_stream, "Unknown uuid type box: "
4337 "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-"
4338 "%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
4339 p_box->i_uuid.b[0], p_box->i_uuid.b[1], p_box->i_uuid.b[2], p_box->i_uuid.b[3],
4340 p_box->i_uuid.b[4], p_box->i_uuid.b[5], p_box->i_uuid.b[6], p_box->i_uuid.b[7],
4341 p_box->i_uuid.b[8], p_box->i_uuid.b[9], p_box->i_uuid.b[10], p_box->i_uuid.b[11],
4342 p_box->i_uuid.b[12], p_box->i_uuid.b[13], p_box->i_uuid.b[14], p_box->i_uuid.b[15] );
4343 #else
4344 msg_Warn( p_stream, "Unknown uuid type box" );
4345 #endif
4346 return 1;
4349 /**** ------------------------------------------------------------------- ****/
4350 /**** "Higher level" Functions ****/
4351 /**** ------------------------------------------------------------------- ****/
4353 static const struct
4355 uint32_t i_type;
4356 int (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
4357 uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
4358 } MP4_Box_Function [] =
4360 /* Containers */
4361 { ATOM_moov, MP4_ReadBoxContainer, 0 },
4362 { ATOM_foov, MP4_ReadBoxContainer, 0 },
4363 { ATOM_trak, MP4_ReadBoxContainer, ATOM_moov },
4364 { ATOM_trak, MP4_ReadBoxContainer, ATOM_foov },
4365 { ATOM_mdia, MP4_ReadBoxContainer, ATOM_trak },
4366 { ATOM_moof, MP4_ReadBoxContainer, 0 },
4367 { ATOM_minf, MP4_ReadBoxContainer, ATOM_mdia },
4368 { ATOM_stbl, MP4_ReadBoxContainer, ATOM_minf },
4369 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_minf },
4370 { ATOM_dinf, MP4_ReadBoxContainer, ATOM_meta },
4371 { ATOM_edts, MP4_ReadBoxContainer, ATOM_trak },
4372 { ATOM_udta, MP4_ReadBoxContainer, 0 },
4373 { ATOM_nmhd, MP4_ReadBoxContainer, ATOM_minf },
4374 { ATOM_hnti, MP4_ReadBoxContainer, ATOM_udta },
4375 { ATOM_rmra, MP4_ReadBoxContainer, ATOM_moov },
4376 { ATOM_rmda, MP4_ReadBoxContainer, ATOM_rmra },
4377 { ATOM_tref, MP4_ReadBoxContainer, ATOM_trak },
4378 { ATOM_gmhd, MP4_ReadBoxContainer, ATOM_minf },
4379 { ATOM_wave, MP4_ReadBoxContainer, ATOM_stsd },
4380 { ATOM_wave, MP4_ReadBoxContainer, ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
4381 { ATOM_wave, MP4_ReadBoxContainer, ATOM_WMA2 }, /* flip4mac */
4382 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in24 },
4383 { ATOM_wave, MP4_ReadBoxContainer, ATOM_in32 },
4384 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl32 },
4385 { ATOM_wave, MP4_ReadBoxContainer, ATOM_fl64 },
4386 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDMC },
4387 { ATOM_wave, MP4_ReadBoxContainer, ATOM_QDM2 },
4388 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiFL }, /* XiphQT */
4389 { ATOM_wave, MP4_ReadBoxContainer, ATOM_XiVs }, /* XiphQT */
4390 { ATOM_ilst, MP4_ReadBox_ilst, ATOM_meta },
4391 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_moov },
4392 { ATOM_mvex, MP4_ReadBoxContainer, ATOM_ftyp },
4394 /* specific box */
4395 { ATOM_ftyp, MP4_ReadBox_ftyp, 0 },
4396 { ATOM_styp, MP4_ReadBox_ftyp, 0 },
4397 { ATOM_cmov, MP4_ReadBox_cmov, 0 },
4398 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_moov },
4399 { ATOM_mvhd, MP4_ReadBox_mvhd, ATOM_foov },
4400 { ATOM_tkhd, MP4_ReadBox_tkhd, ATOM_trak },
4401 { ATOM_load, MP4_ReadBox_load, ATOM_trak },
4402 { ATOM_mdhd, MP4_ReadBox_mdhd, ATOM_mdia },
4403 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_mdia },
4404 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_meta },
4405 { ATOM_hdlr, MP4_ReadBox_hdlr, ATOM_minf },
4406 { ATOM_vmhd, MP4_ReadBox_vmhd, ATOM_minf },
4407 { ATOM_smhd, MP4_ReadBox_smhd, ATOM_minf },
4408 { ATOM_hmhd, MP4_ReadBox_hmhd, ATOM_minf },
4409 { ATOM_alis, MP4_ReadBoxSkip, ATOM_dref },
4410 { ATOM_url, MP4_ReadBox_url, 0 },
4411 { ATOM_urn, MP4_ReadBox_urn, 0 },
4412 { ATOM_dref, MP4_ReadBox_LtdContainer, 0 },
4413 { ATOM_stts, MP4_ReadBox_stts, ATOM_stbl },
4414 { ATOM_ctts, MP4_ReadBox_ctts, ATOM_stbl },
4415 { ATOM_cslg, MP4_ReadBox_cslg, ATOM_stbl },
4416 { ATOM_stsd, MP4_ReadBox_LtdContainer, ATOM_stbl },
4417 { ATOM_stsz, MP4_ReadBox_stsz, ATOM_stbl },
4418 { ATOM_stsc, MP4_ReadBox_stsc, ATOM_stbl },
4419 { ATOM_stco, MP4_ReadBox_stco_co64, ATOM_stbl },
4420 { ATOM_co64, MP4_ReadBox_stco_co64, ATOM_stbl },
4421 { ATOM_stss, MP4_ReadBox_stss, ATOM_stbl },
4422 { ATOM_stsh, MP4_ReadBox_stsh, ATOM_stbl },
4423 { ATOM_stdp, MP4_ReadBox_stdp, 0 },
4424 { ATOM_padb, MP4_ReadBox_padb, 0 },
4425 { ATOM_elst, MP4_ReadBox_elst, ATOM_edts },
4426 { ATOM_cprt, MP4_ReadBox_cprt, 0 },
4427 { ATOM_esds, MP4_ReadBox_esds, ATOM_wave }, /* mp4a in wave chunk */
4428 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4a },
4429 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4v },
4430 { ATOM_esds, MP4_ReadBox_esds, ATOM_mp4s },
4431 { ATOM_dcom, MP4_ReadBox_dcom, 0 },
4432 { ATOM_dfLa, MP4_ReadBox_Binary, ATOM_fLaC },
4433 { ATOM_cmvd, MP4_ReadBox_cmvd, 0 },
4434 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc1 },
4435 { ATOM_avcC, MP4_ReadBox_avcC, ATOM_avc3 },
4436 { ATOM_hvcC, MP4_ReadBox_Binary, 0 },
4437 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp08 },
4438 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp09 },
4439 { ATOM_vpcC, MP4_ReadBox_vpcC, ATOM_vp10 },
4440 { ATOM_dac3, MP4_ReadBox_dac3, 0 },
4441 { ATOM_dec3, MP4_ReadBox_dec3, 0 },
4442 { ATOM_dvc1, MP4_ReadBox_dvc1, ATOM_vc1 },
4443 { ATOM_fiel, MP4_ReadBox_fiel, 0 },
4444 { ATOM_glbl, MP4_ReadBox_Binary, ATOM_FFV1 },
4445 { ATOM_enda, MP4_ReadBox_enda, 0 },
4446 { ATOM_iods, MP4_ReadBox_iods, 0 },
4447 { ATOM_pasp, MP4_ReadBox_pasp, 0 },
4448 { ATOM_btrt, MP4_ReadBox_btrt, 0 }, /* codecs bitrate stsd/????/btrt */
4449 { ATOM_keys, MP4_ReadBox_keys, ATOM_meta },
4450 { ATOM_colr, MP4_ReadBox_colr, 0 },
4452 /* XiphQT */
4453 { ATOM_vCtH, MP4_ReadBox_Binary, ATOM_wave },
4454 { ATOM_vCtC, MP4_ReadBox_Binary, ATOM_wave },
4455 { ATOM_vCtd, MP4_ReadBox_Binary, ATOM_wave },
4456 { ATOM_fCtS, MP4_ReadBox_Binary, ATOM_wave },
4458 /* Samples groups specific information */
4459 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_stbl },
4460 { ATOM_sbgp, MP4_ReadBox_sbgp, ATOM_traf },
4461 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_stbl },
4462 { ATOM_sgpd, MP4_ReadBox_sgpd, ATOM_traf },
4464 /* Quicktime preview atoms, all at root */
4465 { ATOM_pnot, MP4_ReadBox_pnot, 0 },
4466 { ATOM_pict, MP4_ReadBox_Binary, 0 },
4467 { ATOM_PICT, MP4_ReadBox_Binary, 0 },
4469 /* Nothing to do with this box */
4470 { ATOM_mdat, MP4_ReadBoxSkip, 0 },
4471 { ATOM_skip, MP4_ReadBoxSkip, 0 },
4472 { ATOM_free, MP4_ReadBoxSkip, 0 },
4473 { ATOM_wide, MP4_ReadBoxSkip, 0 },
4474 { ATOM_binm, MP4_ReadBoxSkip, 0 },
4476 /* Subtitles */
4477 { ATOM_tx3g, MP4_ReadBox_sample_tx3g, 0 },
4478 { ATOM_c608, MP4_ReadBox_sample_clcp, ATOM_stsd },
4479 //{ ATOM_text, MP4_ReadBox_sample_text, 0 },
4480 /* In sample WebVTT subtitle atoms. No ATOM_wvtt in normal parsing */
4481 { ATOM_vttc, MP4_ReadBoxContainer, ATOM_wvtt },
4482 { ATOM_payl, MP4_ReadBox_Binary, ATOM_vttc },
4484 /* for codecs */
4485 { ATOM_soun, MP4_ReadBox_sample_soun, ATOM_stsd },
4486 { ATOM_agsm, MP4_ReadBox_sample_soun, ATOM_stsd },
4487 { ATOM_ac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4488 { ATOM_eac3, MP4_ReadBox_sample_soun, ATOM_stsd },
4489 { ATOM_fLaC, MP4_ReadBox_sample_soun, ATOM_stsd },
4490 { ATOM_lpcm, MP4_ReadBox_sample_soun, ATOM_stsd },
4491 { ATOM_ms02, MP4_ReadBox_sample_soun, ATOM_stsd },
4492 { ATOM_ms11, MP4_ReadBox_sample_soun, ATOM_stsd },
4493 { ATOM_ms55, MP4_ReadBox_sample_soun, ATOM_stsd },
4494 { ATOM__mp3, MP4_ReadBox_sample_soun, ATOM_stsd },
4495 { ATOM_mp4a, MP4_ReadBox_sample_soun, ATOM_stsd },
4496 { ATOM_twos, MP4_ReadBox_sample_soun, ATOM_stsd },
4497 { ATOM_sowt, MP4_ReadBox_sample_soun, ATOM_stsd },
4498 { ATOM_QDMC, MP4_ReadBox_sample_soun, ATOM_stsd },
4499 { ATOM_QDM2, MP4_ReadBox_sample_soun, ATOM_stsd },
4500 { ATOM_ima4, MP4_ReadBox_sample_soun, ATOM_stsd },
4501 { ATOM_IMA4, MP4_ReadBox_sample_soun, ATOM_stsd },
4502 { ATOM_dvi, MP4_ReadBox_sample_soun, ATOM_stsd },
4503 { ATOM_alaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4504 { ATOM_ulaw, MP4_ReadBox_sample_soun, ATOM_stsd },
4505 { ATOM_MAC3, MP4_ReadBox_sample_soun, ATOM_stsd },
4506 { ATOM_MAC6, MP4_ReadBox_sample_soun, ATOM_stsd },
4507 { ATOM_Qclp, MP4_ReadBox_sample_soun, ATOM_stsd },
4508 { ATOM_samr, MP4_ReadBox_sample_soun, ATOM_stsd },
4509 { ATOM_sawb, MP4_ReadBox_sample_soun, ATOM_stsd },
4510 { ATOM_OggS, MP4_ReadBox_sample_soun, ATOM_stsd },
4511 { ATOM_alac, MP4_ReadBox_sample_soun, ATOM_stsd },
4512 { ATOM_WMA2, MP4_ReadBox_sample_soun, ATOM_stsd }, /* flip4mac */
4513 { ATOM_wma, MP4_ReadBox_sample_soun, ATOM_stsd }, /* ismv wmapro */
4514 { ATOM_Opus, MP4_ReadBox_sample_soun, ATOM_stsd },
4515 /* Sound extensions */
4516 { ATOM_chan, MP4_ReadBox_stsdext_chan, 0 },
4517 { ATOM_WMA2, MP4_ReadBox_WMA2, ATOM_wave }, /* flip4mac */
4518 { ATOM_dOps, MP4_ReadBox_Binary, ATOM_Opus },
4519 { ATOM_wfex, MP4_ReadBox_WMA2, ATOM_wma }, /* ismv formatex */
4521 /* Both uncompressed sound and video */
4522 { ATOM_raw, MP4_ReadBox_default, ATOM_stsd },
4524 { ATOM_drmi, MP4_ReadBox_sample_vide, ATOM_stsd },
4525 { ATOM_vide, MP4_ReadBox_sample_vide, ATOM_stsd },
4526 { ATOM_mp4v, MP4_ReadBox_sample_vide, ATOM_stsd },
4527 { ATOM_SVQ1, MP4_ReadBox_sample_vide, ATOM_stsd },
4528 { ATOM_SVQ3, MP4_ReadBox_sample_vide, ATOM_stsd },
4529 { ATOM_ZyGo, MP4_ReadBox_sample_vide, ATOM_stsd },
4530 { ATOM_DIVX, MP4_ReadBox_sample_vide, ATOM_stsd },
4531 { ATOM_XVID, MP4_ReadBox_sample_vide, ATOM_stsd },
4532 { ATOM_h263, MP4_ReadBox_sample_vide, ATOM_stsd },
4533 { ATOM_s263, MP4_ReadBox_sample_vide, ATOM_stsd },
4534 { ATOM_cvid, MP4_ReadBox_sample_vide, ATOM_stsd },
4535 { ATOM_3IV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4536 { ATOM_3iv1, MP4_ReadBox_sample_vide, ATOM_stsd },
4537 { ATOM_3IV2, MP4_ReadBox_sample_vide, ATOM_stsd },
4538 { ATOM_3iv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4539 { ATOM_3IVD, MP4_ReadBox_sample_vide, ATOM_stsd },
4540 { ATOM_3ivd, MP4_ReadBox_sample_vide, ATOM_stsd },
4541 { ATOM_3VID, MP4_ReadBox_sample_vide, ATOM_stsd },
4542 { ATOM_3vid, MP4_ReadBox_sample_vide, ATOM_stsd },
4543 { ATOM_FFV1, MP4_ReadBox_sample_vide, ATOM_stsd },
4544 { ATOM_mjpa, MP4_ReadBox_sample_vide, ATOM_stsd },
4545 { ATOM_mjpb, MP4_ReadBox_sample_vide, ATOM_stsd },
4546 { ATOM_qdrw, MP4_ReadBox_sample_vide, ATOM_stsd },
4547 { ATOM_mp2v, MP4_ReadBox_sample_vide, ATOM_stsd },
4548 { ATOM_hdv2, MP4_ReadBox_sample_vide, ATOM_stsd },
4549 { ATOM_WMV3, MP4_ReadBox_sample_vide, ATOM_stsd },
4551 { ATOM_mjqt, MP4_ReadBox_default, 0 }, /* found in mjpa/b */
4552 { ATOM_mjht, MP4_ReadBox_default, 0 },
4554 { ATOM_dvc, MP4_ReadBox_sample_vide, ATOM_stsd },
4555 { ATOM_dvp, MP4_ReadBox_sample_vide, ATOM_stsd },
4556 { ATOM_dv5n, MP4_ReadBox_sample_vide, ATOM_stsd },
4557 { ATOM_dv5p, MP4_ReadBox_sample_vide, ATOM_stsd },
4558 { ATOM_VP31, MP4_ReadBox_sample_vide, ATOM_stsd },
4559 { ATOM_vp31, MP4_ReadBox_sample_vide, ATOM_stsd },
4560 { ATOM_h264, MP4_ReadBox_sample_vide, ATOM_stsd },
4562 { ATOM_jpeg, MP4_ReadBox_sample_vide, ATOM_stsd },
4563 { ATOM_vc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4564 { ATOM_avc1, MP4_ReadBox_sample_vide, ATOM_stsd },
4565 { ATOM_avc3, MP4_ReadBox_sample_vide, ATOM_stsd },
4567 { ATOM_rrtp, MP4_ReadBox_sample_hint8, ATOM_stsd },
4569 { ATOM_yv12, MP4_ReadBox_sample_vide, 0 },
4570 { ATOM_yuv2, MP4_ReadBox_sample_vide, 0 },
4572 { ATOM_strf, MP4_ReadBox_strf, ATOM_WMV3 }, /* flip4mac */
4573 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_WMV3 }, /* flip4mac */
4574 { ATOM_ASF , MP4_ReadBox_ASF, ATOM_wave }, /* flip4mac */
4576 { ATOM_mp4s, MP4_ReadBox_sample_mp4s, ATOM_stsd },
4578 /* XXX there is 2 box where we could find this entry stbl and tref*/
4579 { ATOM_hint, MP4_ReadBox_default, 0 },
4581 /* found in tref box */
4582 { ATOM_dpnd, MP4_ReadBox_default, 0 },
4583 { ATOM_ipir, MP4_ReadBox_default, 0 },
4584 { ATOM_mpod, MP4_ReadBox_default, 0 },
4585 { ATOM_chap, MP4_ReadBox_tref_generic, 0 },
4587 /* found in hnti */
4588 { ATOM_rtp, MP4_ReadBox_rtp, ATOM_hnti },
4589 { ATOM_sdp, MP4_ReadBox_sdp, ATOM_hnti },
4591 /* found in rrtp sample description */
4592 { ATOM_tims, MP4_ReadBox_tims, 0 },
4593 { ATOM_tsro, MP4_ReadBox_tsro, 0 },
4594 { ATOM_tssy, MP4_ReadBox_tssy, 0 },
4596 /* found in rmra/rmda */
4597 { ATOM_rdrf, MP4_ReadBox_rdrf, ATOM_rmda },
4598 { ATOM_rmdr, MP4_ReadBox_rmdr, ATOM_rmda },
4599 { ATOM_rmqu, MP4_ReadBox_rmqu, ATOM_rmda },
4600 { ATOM_rmvc, MP4_ReadBox_rmvc, ATOM_rmda },
4602 { ATOM_drms, MP4_ReadBox_sample_soun, 0 },
4603 { ATOM_sinf, MP4_ReadBoxContainer, 0 },
4604 { ATOM_schi, MP4_ReadBoxContainer, 0 },
4605 { ATOM_user, MP4_ReadBox_drms, 0 },
4606 { ATOM_key, MP4_ReadBox_drms, 0 },
4607 { ATOM_iviv, MP4_ReadBox_drms, 0 },
4608 { ATOM_priv, MP4_ReadBox_drms, 0 },
4609 { ATOM_frma, MP4_ReadBox_frma, ATOM_sinf }, /* and rinf */
4610 { ATOM_frma, MP4_ReadBox_frma, ATOM_wave }, /* flip4mac */
4611 { ATOM_skcr, MP4_ReadBox_skcr, 0 },
4613 /* ilst meta tags */
4614 { ATOM_0xa9ART, MP4_ReadBox_Metadata, ATOM_ilst },
4615 { ATOM_0xa9alb, MP4_ReadBox_Metadata, ATOM_ilst },
4616 { ATOM_0xa9cmt, MP4_ReadBox_Metadata, ATOM_ilst },
4617 { ATOM_0xa9com, MP4_ReadBox_Metadata, ATOM_ilst },
4618 { ATOM_0xa9cpy, MP4_ReadBox_Metadata, ATOM_ilst },
4619 { ATOM_0xa9day, MP4_ReadBox_Metadata, ATOM_ilst },
4620 { ATOM_0xa9des, MP4_ReadBox_Metadata, ATOM_ilst },
4621 { ATOM_0xa9enc, MP4_ReadBox_Metadata, ATOM_ilst },
4622 { ATOM_0xa9gen, MP4_ReadBox_Metadata, ATOM_ilst },
4623 { ATOM_0xa9grp, MP4_ReadBox_Metadata, ATOM_ilst },
4624 { ATOM_0xa9lyr, MP4_ReadBox_Metadata, ATOM_ilst },
4625 { ATOM_0xa9nam, MP4_ReadBox_Metadata, ATOM_ilst },
4626 { ATOM_0xa9too, MP4_ReadBox_Metadata, ATOM_ilst },
4627 { ATOM_0xa9trk, MP4_ReadBox_Metadata, ATOM_ilst },
4628 { ATOM_0xa9wrt, MP4_ReadBox_Metadata, ATOM_ilst },
4629 { ATOM_aART, MP4_ReadBox_Metadata, ATOM_ilst },
4630 { ATOM_atID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4631 { ATOM_cnID, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunes */
4632 { ATOM_covr, MP4_ReadBoxContainer, ATOM_ilst },
4633 { ATOM_desc, MP4_ReadBox_Metadata, ATOM_ilst },
4634 { ATOM_disk, MP4_ReadBox_Metadata, ATOM_ilst },
4635 { ATOM_flvr, MP4_ReadBox_Metadata, ATOM_ilst },
4636 { ATOM_gnre, MP4_ReadBox_Metadata, ATOM_ilst },
4637 { ATOM_rtng, MP4_ReadBox_Metadata, ATOM_ilst },
4638 { ATOM_trkn, MP4_ReadBox_Metadata, ATOM_ilst },
4639 { ATOM_xid_, MP4_ReadBox_Metadata, ATOM_ilst },
4640 { ATOM_gshh, MP4_ReadBox_Metadata, ATOM_ilst }, /* YouTube gs?? */
4641 { ATOM_gspm, MP4_ReadBox_Metadata, ATOM_ilst },
4642 { ATOM_gspu, MP4_ReadBox_Metadata, ATOM_ilst },
4643 { ATOM_gssd, MP4_ReadBox_Metadata, ATOM_ilst },
4644 { ATOM_gsst, MP4_ReadBox_Metadata, ATOM_ilst },
4645 { ATOM_gstd, MP4_ReadBox_Metadata, ATOM_ilst },
4646 { ATOM_ITUN, MP4_ReadBox_Metadata, ATOM_ilst }, /* iTunesInfo */
4648 /* udta */
4649 { ATOM_0x40PRM, MP4_ReadBox_Binary, ATOM_udta },
4650 { ATOM_0x40PRQ, MP4_ReadBox_Binary, ATOM_udta },
4651 { ATOM_0xa9ART, MP4_ReadBox_Binary, ATOM_udta },
4652 { ATOM_0xa9alb, MP4_ReadBox_Binary, ATOM_udta },
4653 { ATOM_0xa9ard, MP4_ReadBox_Binary, ATOM_udta },
4654 { ATOM_0xa9arg, MP4_ReadBox_Binary, ATOM_udta },
4655 { ATOM_0xa9aut, MP4_ReadBox_Binary, ATOM_udta },
4656 { ATOM_0xa9cak, MP4_ReadBox_Binary, ATOM_udta },
4657 { ATOM_0xa9cmt, MP4_ReadBox_Binary, ATOM_udta },
4658 { ATOM_0xa9con, MP4_ReadBox_Binary, ATOM_udta },
4659 { ATOM_0xa9com, MP4_ReadBox_Binary, ATOM_udta },
4660 { ATOM_0xa9cpy, MP4_ReadBox_Binary, ATOM_udta },
4661 { ATOM_0xa9day, MP4_ReadBox_Binary, ATOM_udta },
4662 { ATOM_0xa9des, MP4_ReadBox_Binary, ATOM_udta },
4663 { ATOM_0xa9dir, MP4_ReadBox_Binary, ATOM_udta },
4664 { ATOM_0xa9dis, MP4_ReadBox_Binary, ATOM_udta },
4665 { ATOM_0xa9dsa, MP4_ReadBox_Binary, ATOM_udta },
4666 { ATOM_0xa9fmt, MP4_ReadBox_Binary, ATOM_udta },
4667 { ATOM_0xa9gen, MP4_ReadBox_Binary, ATOM_udta },
4668 { ATOM_0xa9grp, MP4_ReadBox_Binary, ATOM_udta },
4669 { ATOM_0xa9hst, MP4_ReadBox_Binary, ATOM_udta },
4670 { ATOM_0xa9inf, MP4_ReadBox_Binary, ATOM_udta },
4671 { ATOM_0xa9isr, MP4_ReadBox_Binary, ATOM_udta },
4672 { ATOM_0xa9lab, MP4_ReadBox_Binary, ATOM_udta },
4673 { ATOM_0xa9lal, MP4_ReadBox_Binary, ATOM_udta },
4674 { ATOM_0xa9lnt, MP4_ReadBox_Binary, ATOM_udta },
4675 { ATOM_0xa9lyr, MP4_ReadBox_Binary, ATOM_udta },
4676 { ATOM_0xa9mak, MP4_ReadBox_Binary, ATOM_udta },
4677 { ATOM_0xa9mal, MP4_ReadBox_Binary, ATOM_udta },
4678 { ATOM_0xa9mod, MP4_ReadBox_Binary, ATOM_udta },
4679 { ATOM_0xa9nam, MP4_ReadBox_Binary, ATOM_udta },
4680 { ATOM_0xa9ope, MP4_ReadBox_Binary, ATOM_udta },
4681 { ATOM_0xa9phg, MP4_ReadBox_Binary, ATOM_udta },
4682 { ATOM_0xa9PRD, MP4_ReadBox_Binary, ATOM_udta },
4683 { ATOM_0xa9prd, MP4_ReadBox_Binary, ATOM_udta },
4684 { ATOM_0xa9prf, MP4_ReadBox_Binary, ATOM_udta },
4685 { ATOM_0xa9pub, MP4_ReadBox_Binary, ATOM_udta },
4686 { ATOM_0xa9req, MP4_ReadBox_Binary, ATOM_udta },
4687 { ATOM_0xa9sne, MP4_ReadBox_Binary, ATOM_udta },
4688 { ATOM_0xa9snm, MP4_ReadBox_Binary, ATOM_udta },
4689 { ATOM_0xa9sol, MP4_ReadBox_Binary, ATOM_udta },
4690 { ATOM_0xa9src, MP4_ReadBox_Binary, ATOM_udta },
4691 { ATOM_0xa9st3, MP4_ReadBox_Binary, ATOM_udta },
4692 { ATOM_0xa9swr, MP4_ReadBox_Binary, ATOM_udta },
4693 { ATOM_0xa9thx, MP4_ReadBox_Binary, ATOM_udta },
4694 { ATOM_0xa9too, MP4_ReadBox_Binary, ATOM_udta },
4695 { ATOM_0xa9trk, MP4_ReadBox_Binary, ATOM_udta },
4696 { ATOM_0xa9url, MP4_ReadBox_Binary, ATOM_udta },
4697 { ATOM_0xa9wrn, MP4_ReadBox_Binary, ATOM_udta },
4698 { ATOM_0xa9xpd, MP4_ReadBox_Binary, ATOM_udta },
4699 { ATOM_0xa9xyz, MP4_ReadBox_Binary, ATOM_udta },
4700 { ATOM_chpl, MP4_ReadBox_chpl, ATOM_udta }, /* nero unlabeled chapters list */
4701 { ATOM_MCPS, MP4_ReadBox_Binary, ATOM_udta },
4702 { ATOM_name, MP4_ReadBox_Binary, ATOM_udta },
4703 { ATOM_vndr, MP4_ReadBox_Binary, ATOM_udta },
4704 { ATOM_SDLN, MP4_ReadBox_Binary, ATOM_udta },
4705 { ATOM_HMMT, MP4_ReadBox_HMMT, ATOM_udta }, /* GoPro HiLight tags */
4707 /* udta, non meta */
4708 { ATOM_tsel, MP4_ReadBox_tsel, ATOM_udta },
4710 /* iTunes/Quicktime meta info */
4711 { ATOM_meta, MP4_ReadBox_meta, 0 },
4712 { ATOM_ID32, MP4_ReadBox_Binary, ATOM_meta }, /* ID3v2 in 3GPP / ETSI TS 126 244 8.3 */
4713 { ATOM_data, MP4_ReadBox_data, 0 }, /* ilst/@too and others, ITUN/data */
4714 { ATOM_mean, MP4_ReadBox_Binary, ATOM_ITUN },
4715 { ATOM_name, MP4_ReadBox_Binary, ATOM_ITUN },
4717 /* found in smoothstreaming */
4718 { ATOM_traf, MP4_ReadBoxContainer, ATOM_moof },
4719 { ATOM_mfra, MP4_ReadBoxContainer, 0 },
4720 { ATOM_mfhd, MP4_ReadBox_mfhd, ATOM_moof },
4721 { ATOM_sidx, MP4_ReadBox_sidx, 0 },
4722 { ATOM_tfhd, MP4_ReadBox_tfhd, ATOM_traf },
4723 { ATOM_trun, MP4_ReadBox_trun, ATOM_traf },
4724 { ATOM_tfdt, MP4_ReadBox_tfdt, ATOM_traf },
4725 { ATOM_trex, MP4_ReadBox_trex, ATOM_mvex },
4726 { ATOM_mehd, MP4_ReadBox_mehd, ATOM_mvex },
4727 { ATOM_sdtp, MP4_ReadBox_sdtp, 0 },
4728 { ATOM_tfra, MP4_ReadBox_tfra, ATOM_mfra },
4729 { ATOM_mfro, MP4_ReadBox_mfro, ATOM_mfra },
4730 { ATOM_uuid, MP4_ReadBox_uuid, 0 },
4732 /* spatial/360°/VR */
4733 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_avc1 },
4734 { ATOM_st3d, MP4_ReadBox_st3d, ATOM_mp4v },
4735 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_avc1 },
4736 { ATOM_sv3d, MP4_ReadBoxContainer, ATOM_mp4v },
4737 { ATOM_proj, MP4_ReadBoxContainer, ATOM_sv3d },
4738 { ATOM_prhd, MP4_ReadBox_prhd, ATOM_proj },
4739 { ATOM_equi, MP4_ReadBox_equi, ATOM_proj },
4740 { ATOM_cbmp, MP4_ReadBox_cbmp, ATOM_proj },
4742 /* Ambisonics */
4743 { ATOM_SA3D, MP4_ReadBox_SA3D, 0 },
4745 /* Last entry */
4746 { 0, MP4_ReadBox_default, 0 }
4749 static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
4751 int i_index;
4753 for( i_index = 0; ; i_index++ )
4755 if ( MP4_Box_Function[i_index].i_parent &&
4756 p_father && p_father->i_type != MP4_Box_Function[i_index].i_parent )
4757 continue;
4759 if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
4760 ( MP4_Box_Function[i_index].i_type == 0 ) )
4762 break;
4766 if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
4768 return VLC_EGENERIC;
4771 return VLC_SUCCESS;
4774 static void MP4_Box_Clean_Specific( MP4_Box_t *p_box )
4776 if( p_box->pf_free )
4777 p_box->pf_free( p_box );
4780 /*****************************************************************************
4781 * MP4_ReadBox : parse the actual box and the children
4782 * XXX : Do not go to the next box
4783 *****************************************************************************/
4784 static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
4786 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
4787 if( p_box == NULL )
4788 return NULL;
4790 if( !MP4_PeekBoxHeader( p_stream, p_box ) )
4792 msg_Warn( p_stream, "cannot read one box" );
4793 free( p_box );
4794 return NULL;
4797 if( p_father && p_father->i_size > 0 &&
4798 p_father->i_pos + p_father->i_size < p_box->i_pos + p_box->i_size )
4800 msg_Dbg( p_stream, "out of bound child" );
4801 free( p_box );
4802 return NULL;
4805 if( !p_box->i_size )
4807 msg_Dbg( p_stream, "found an empty box (null size)" );
4808 free( p_box );
4809 return NULL;
4811 p_box->p_father = p_father;
4813 if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
4815 uint64_t i_end = p_box->i_pos + p_box->i_size;
4816 MP4_BoxFree( p_box );
4817 MP4_Seek( p_stream, i_end ); /* Skip the failed box */
4818 return NULL;
4821 return p_box;
4824 /*****************************************************************************
4825 * MP4_BoxNew : creates and initializes an arbitrary box
4826 *****************************************************************************/
4827 MP4_Box_t * MP4_BoxNew( uint32_t i_type )
4829 MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) );
4830 if( likely( p_box != NULL ) )
4832 p_box->i_type = i_type;
4834 return p_box;
4837 /*****************************************************************************
4838 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
4839 * the children
4840 *****************************************************************************/
4841 void MP4_BoxFree( MP4_Box_t *p_box )
4843 MP4_Box_t *p_child;
4845 if( !p_box )
4846 return; /* hehe */
4848 for( p_child = p_box->p_first; p_child != NULL; )
4850 MP4_Box_t *p_next;
4852 p_next = p_child->p_next;
4853 MP4_BoxFree( p_child );
4854 p_child = p_next;
4857 MP4_Box_Clean_Specific( p_box );
4859 if( p_box->data.p_payload )
4860 free( p_box->data.p_payload );
4862 free( p_box );
4865 MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
4867 /* p_chunk is a virtual root container for the moof and mdat boxes */
4868 MP4_Box_t *p_fakeroot;
4869 MP4_Box_t *p_tmp_box;
4871 p_fakeroot = MP4_BoxNew( ATOM_root );
4872 if( unlikely( p_fakeroot == NULL ) )
4873 return NULL;
4874 p_fakeroot->i_shortsize = 1;
4876 const uint32_t stoplist[] = { ATOM_moov, ATOM_moof, 0 };
4877 MP4_ReadBoxContainerChildren( s, p_fakeroot, stoplist );
4879 p_tmp_box = p_fakeroot->p_first;
4880 if( p_tmp_box == NULL )
4882 MP4_BoxFree( p_fakeroot );
4883 return NULL;
4885 else while( p_tmp_box )
4887 p_fakeroot->i_size += p_tmp_box->i_size;
4888 p_tmp_box = p_tmp_box->p_next;
4891 return p_fakeroot;
4894 /*****************************************************************************
4895 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
4896 *****************************************************************************
4897 * The first box is a virtual box "root" and is the father for all first
4898 * level boxes for the file, a sort of virtual contener
4899 *****************************************************************************/
4900 MP4_Box_t *MP4_BoxGetRoot( stream_t *p_stream )
4902 int i_result;
4904 MP4_Box_t *p_vroot = MP4_BoxNew( ATOM_root );
4905 if( p_vroot == NULL )
4906 return NULL;
4908 p_vroot->i_shortsize = 1;
4909 int64_t i_size = stream_Size( p_stream );
4910 if( i_size > 0 )
4911 p_vroot->i_size = i_size;
4913 /* First get the moov */
4914 const uint32_t stoplist[] = { ATOM_moov, ATOM_mdat, 0 };
4915 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
4917 /* mdat appeared first */
4918 if( i_result && !MP4_BoxGet( p_vroot, "moov" ) )
4920 bool b_seekable;
4921 if( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_seekable ) != VLC_SUCCESS || !b_seekable )
4923 msg_Err( p_stream, "no moov before mdat and the stream is not seekable" );
4924 goto error;
4927 /* continue loading up to moov */
4928 const uint32_t stoplist[] = { ATOM_moov, 0 };
4929 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
4932 if( !i_result )
4933 goto error;
4935 /* If there is a mvex box, it means fragmented MP4, and we're done */
4936 if( MP4_BoxCount( p_vroot, "moov/mvex" ) > 0 )
4938 /* Read a bit more atoms as we might have an index between moov and moof */
4939 const uint32_t stoplist[] = { ATOM_sidx, 0 };
4940 const uint32_t excludelist[] = { ATOM_moof, ATOM_mdat, 0 };
4941 MP4_ReadBoxContainerChildrenIndexed( p_stream, p_vroot, stoplist, excludelist, false );
4942 return p_vroot;
4945 if( vlc_stream_Tell( p_stream ) + 8 < (uint64_t) stream_Size( p_stream ) )
4947 /* Get the rest of the file */
4948 i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, NULL );
4950 if( !i_result )
4951 goto error;
4954 MP4_Box_t *p_moov;
4955 MP4_Box_t *p_cmov;
4957 /* check if there is a cmov, if so replace
4958 compressed moov by uncompressed one */
4959 if( ( ( p_moov = MP4_BoxGet( p_vroot, "moov" ) ) &&
4960 ( p_cmov = MP4_BoxGet( p_vroot, "moov/cmov" ) ) ) ||
4961 ( ( p_moov = MP4_BoxGet( p_vroot, "foov" ) ) &&
4962 ( p_cmov = MP4_BoxGet( p_vroot, "foov/cmov" ) ) ) )
4964 /* rename the compressed moov as a box to skip */
4965 p_moov->i_type = ATOM_skip;
4967 /* get uncompressed p_moov */
4968 p_moov = p_cmov->data.p_cmov->p_moov;
4969 p_cmov->data.p_cmov->p_moov = NULL;
4971 /* make p_root father of this new moov */
4972 p_moov->p_father = p_vroot;
4974 /* insert this new moov box as first child of p_root */
4975 p_moov->p_next = p_vroot->p_first;
4976 p_vroot->p_first = p_moov;
4979 return p_vroot;
4981 error:
4982 MP4_BoxFree( p_vroot );
4983 MP4_Seek( p_stream, 0 );
4984 return NULL;
4988 static void MP4_BoxDumpStructure_Internal( stream_t *s, const MP4_Box_t *p_box,
4989 unsigned int i_level )
4991 const MP4_Box_t *p_child;
4992 uint32_t i_displayedtype = p_box->i_type;
4993 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype)[0] = 'c';
4995 if( !i_level )
4997 msg_Dbg( s, "dumping root Box \"%4.4s\"",
4998 (char*)&i_displayedtype );
5000 else
5002 char str[512];
5003 if( i_level >= (sizeof(str) - 1)/4 )
5004 return;
5006 memset( str, ' ', sizeof(str) );
5007 for( unsigned i = 0; i < i_level; i++ )
5009 str[i*4] = '|';
5012 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
5013 "+ %4.4s size %"PRIu64" offset %" PRIuMAX "%s",
5014 (char*)&i_displayedtype, p_box->i_size,
5015 (uintmax_t)p_box->i_pos,
5016 p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
5017 msg_Dbg( s, "%s", str );
5019 p_child = p_box->p_first;
5020 while( p_child )
5022 MP4_BoxDumpStructure_Internal( s, p_child, i_level + 1 );
5023 p_child = p_child->p_next;
5027 void MP4_BoxDumpStructure( stream_t *s, const MP4_Box_t *p_box )
5029 MP4_BoxDumpStructure_Internal( s, p_box, 0 );
5033 /*****************************************************************************
5034 *****************************************************************************
5036 ** High level methods to acces an MP4 file
5038 *****************************************************************************
5039 *****************************************************************************/
5040 static bool get_token( char **ppsz_path, char **ppsz_token, int *pi_number )
5042 size_t i_len ;
5043 if( !*ppsz_path[0] )
5045 *ppsz_token = NULL;
5046 *pi_number = 0;
5047 return true;
5049 i_len = strcspn( *ppsz_path, "/[" );
5050 if( !i_len && **ppsz_path == '/' )
5052 i_len = 1;
5054 *ppsz_token = strndup( *ppsz_path, i_len );
5055 if( unlikely(!*ppsz_token) )
5056 return false;
5058 *ppsz_path += i_len;
5060 /* Parse the token number token[n] */
5061 if( **ppsz_path == '[' )
5063 (*ppsz_path)++;
5064 *pi_number = strtol( *ppsz_path, NULL, 10 );
5065 while( **ppsz_path && **ppsz_path != ']' )
5067 (*ppsz_path)++;
5069 if( **ppsz_path == ']' )
5071 (*ppsz_path)++;
5074 else
5076 *pi_number = 0;
5079 /* Forward to start of next token */
5080 while( **ppsz_path == '/' )
5082 (*ppsz_path)++;
5085 return true;
5088 static void MP4_BoxGet_Internal( const MP4_Box_t **pp_result, const MP4_Box_t *p_box,
5089 const char *psz_fmt, va_list args)
5091 char *psz_dup;
5092 char *psz_path;
5093 char *psz_token = NULL;
5095 if( !p_box )
5097 *pp_result = NULL;
5098 return;
5101 if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
5102 psz_path = NULL;
5104 if( !psz_path || !psz_path[0] )
5106 free( psz_path );
5107 *pp_result = NULL;
5108 return;
5111 // fprintf( stderr, "path:'%s'\n", psz_path );
5112 psz_dup = psz_path; /* keep this pointer, as it need to be unallocated */
5113 for( ; ; )
5115 int i_number;
5117 if( !get_token( &psz_path, &psz_token, &i_number ) )
5118 goto error_box;
5119 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
5120 // psz_path,psz_token,i_number );
5121 if( !psz_token )
5123 free( psz_dup );
5124 *pp_result = p_box;
5125 return;
5127 else
5128 if( !strcmp( psz_token, "/" ) )
5130 /* Find root box */
5131 while( p_box && p_box->i_type != ATOM_root )
5133 p_box = p_box->p_father;
5135 if( !p_box )
5137 goto error_box;
5140 else
5141 if( !strcmp( psz_token, "." ) )
5143 /* Do nothing */
5145 else
5146 if( !strcmp( psz_token, ".." ) )
5148 p_box = p_box->p_father;
5149 if( !p_box )
5151 goto error_box;
5154 else
5155 if( strlen( psz_token ) == 4 )
5157 uint32_t i_fourcc;
5158 i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
5159 psz_token[2], psz_token[3] );
5160 p_box = p_box->p_first;
5161 for( ; ; )
5163 if( !p_box )
5165 goto error_box;
5167 if( p_box->i_type == i_fourcc )
5169 if( !i_number )
5171 break;
5173 i_number--;
5175 p_box = p_box->p_next;
5178 else
5179 if( *psz_token == '\0' )
5181 p_box = p_box->p_first;
5182 for( ; ; )
5184 if( !p_box )
5186 goto error_box;
5188 if( !i_number )
5190 break;
5192 i_number--;
5193 p_box = p_box->p_next;
5196 else
5198 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
5199 goto error_box;
5202 FREENULL( psz_token );
5205 return;
5207 error_box:
5208 free( psz_token );
5209 free( psz_dup );
5210 *pp_result = NULL;
5211 return;
5214 /*****************************************************************************
5215 * MP4_BoxGet: find a box given a path relative to p_box
5216 *****************************************************************************
5217 * Path Format: . .. / as usual
5218 * [number] to specifie box number ex: trak[12]
5220 * ex: /moov/trak[12]
5221 * ../mdia
5222 *****************************************************************************/
5223 MP4_Box_t *MP4_BoxGet( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5225 va_list args;
5226 const MP4_Box_t *p_result;
5228 va_start( args, psz_fmt );
5229 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5230 va_end( args );
5232 return( (MP4_Box_t *) p_result );
5235 /*****************************************************************************
5236 * MP4_BoxCount: count box given a path relative to p_box
5237 *****************************************************************************
5238 * Path Format: . .. / as usual
5239 * [number] to specifie box number ex: trak[12]
5241 * ex: /moov/trak[12]
5242 * ../mdia
5243 *****************************************************************************/
5244 unsigned MP4_BoxCount( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5246 va_list args;
5247 unsigned i_count;
5248 const MP4_Box_t *p_result, *p_next;
5250 va_start( args, psz_fmt );
5251 MP4_BoxGet_Internal( &p_result, p_box, psz_fmt, args );
5252 va_end( args );
5253 if( !p_result )
5255 return( 0 );
5258 i_count = 1;
5259 for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
5261 if( p_next->i_type == p_result->i_type)
5263 i_count++;
5266 return( i_count );