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 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_stream.h> /* vlc_stream_Peek*/
29 #include <vlc_strings.h> /* vlc_ascii_tolower */
32 # include <zlib.h> /* for compressed moov */
36 #include "languages.h"
42 * The input method HAS to be seekable
45 /* convert 16.16 fixed point to floating point */
46 static double conv_fx( int32_t fx
) {
52 /* some functions for mp4 encoding of variables */
54 static char * MP4_Time2Str( stime_t i_duration
, uint32_t i_scale
)
56 uint64_t i_time
= (i_scale
> 0) ? i_duration
/ i_scale
: 0;
57 unsigned h
= ( i_time
/( 60*60 ) ) % 60;
58 unsigned m
= ( i_time
/ 60 ) % 60;
59 unsigned s
= i_time
% 60;
60 unsigned ms
= (i_scale
) ? (1000*i_duration
/ i_scale
) % 1000 : 0;
63 if( asprintf( &out
, "%u:%.2u:%.2u:%.3u", h
, m
, s
, ms
) < 0 )
69 #define MP4_GETX_PRIVATE(dst, code, size) \
72 if( (i_read) >= (size) ) \
85 #define MP4_GET1BYTE( dst ) MP4_GETX_PRIVATE( dst, *p_peek, 1 )
86 #define MP4_GET2BYTES( dst ) MP4_GETX_PRIVATE( dst, GetWBE(p_peek), 2 )
87 #define MP4_GET3BYTES( dst ) MP4_GETX_PRIVATE( dst, Get24bBE(p_peek), 3 )
88 #define MP4_GET4BYTES( dst ) MP4_GETX_PRIVATE( dst, GetDWBE(p_peek), 4 )
89 #define MP4_GET8BYTES( dst ) MP4_GETX_PRIVATE( dst, GetQWBE(p_peek), 8 )
91 #define MP4_GET2BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetWLE(p_peek), 2 )
92 #define MP4_GET4BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetDWLE(p_peek), 4 )
93 #define MP4_GET8BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetQWLE(p_peek), 8 )
94 #define MP4_GETFOURCC( dst ) MP4_GET4BYTESLE( dst )
96 #define MP4_GETVERSIONFLAGS( p_void ) \
97 MP4_GET1BYTE( p_void->i_version ); \
98 MP4_GET3BYTES( p_void->i_flags )
100 static char *mp4_getstringz( uint8_t **restrict in
, uint64_t *restrict size
)
102 assert( *size
<= SSIZE_MAX
);
107 if( *in
== 0 ) /* Null string stored */
114 size_t len
= strnlen( (const char *)*in
, *size
);
115 if( len
== 0 || len
>= *size
)
120 char *ret
= malloc( len
);
121 if( likely(ret
!= NULL
) )
122 memcpy( ret
, *in
, len
);
128 #define MP4_GETSTRINGZ( p_str ) \
130 (p_str) = mp4_getstringz( &p_peek, &i_read ); \
133 /* This macro is used when we want to printf the box type
134 * APPLE annotation box is :
135 * either 0xA9 + 24-bit ASCII text string (and 0xA9 isn't printable)
136 * either 32-bit ASCII text string
138 #define MP4_BOX_TYPE_ASCII() ( ((char*)&p_box->i_type)[0] != (char)0xA9 )
140 static inline uint32_t Get24bBE( const uint8_t *p
)
142 return( ( p
[0] <<16 ) + ( p
[1] <<8 ) + p
[2] );
145 static inline void GetUUID( UUID_t
*p_uuid
, const uint8_t *p_buff
)
147 memcpy( p_uuid
, p_buff
, 16 );
150 static uint8_t *mp4_readbox_enter_common( stream_t
*s
, MP4_Box_t
*box
,
152 void (*release
)( MP4_Box_t
* ),
155 const size_t headersize
= mp4_box_headersize( box
);
157 if( unlikely(readsize
< headersize
) || unlikely(readsize
> SSIZE_MAX
) )
160 uint8_t *buf
= malloc( readsize
);
161 if( unlikely(buf
== NULL
) )
164 ssize_t val
= vlc_stream_Read( s
, buf
, readsize
);
165 if( (size_t)val
!= readsize
)
167 msg_Warn( s
, "mp4: wanted %"PRIu64
" bytes, got %zd", readsize
, val
);
171 box
->data
.p_payload
= malloc( typesize
);
172 if( unlikely(box
->data
.p_payload
== NULL
) )
175 memset( box
->data
.p_payload
, 0, typesize
);
176 box
->pf_free
= release
;
183 static uint8_t *mp4_readbox_enter_partial( stream_t
*s
, MP4_Box_t
*box
,
185 void (*release
)( MP4_Box_t
* ),
186 uint64_t *restrict readsize
)
188 if( (uint64_t)*readsize
> box
->i_size
)
189 *readsize
= box
->i_size
;
191 return mp4_readbox_enter_common( s
, box
, typesize
, release
, *readsize
);
194 static uint8_t *mp4_readbox_enter( stream_t
*s
, MP4_Box_t
*box
,
196 void (*release
)( MP4_Box_t
* ) )
198 uint64_t readsize
= box
->i_size
;
199 return mp4_readbox_enter_common( s
, box
, typesize
, release
, readsize
);
203 #define MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_TYPE_t, maxread, release ) \
204 uint64_t i_read = (maxread); \
205 uint8_t *p_buff = mp4_readbox_enter_partial( p_stream, p_box, \
206 sizeof( MP4_Box_data_TYPE_t ), release, &i_read ); \
207 if( unlikely(p_buff == NULL) ) \
209 const size_t header_size = mp4_box_headersize( p_box ); \
210 uint8_t *p_peek = p_buff + header_size; \
211 i_read -= header_size
213 #define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t, release ) \
214 uint8_t *p_buff = mp4_readbox_enter( p_stream, p_box, \
215 sizeof(MP4_Box_data_TYPE_t), release ); \
216 if( unlikely(p_buff == NULL) ) \
218 uint64_t i_read = p_box->i_size; \
219 const size_t header_size = mp4_box_headersize( p_box ); \
220 uint8_t *p_peek = p_buff + header_size; \
221 i_read -= header_size
223 #define MP4_READBOX_EXIT( i_code ) \
230 /*****************************************************************************
232 *****************************************************************************/
233 static MP4_Box_t
*MP4_ReadBox( stream_t
*p_stream
, MP4_Box_t
*p_father
);
234 static MP4_Box_t
*MP4_ReadBoxUsing( stream_t
*p_stream
, MP4_Box_t
*p_father
,
235 int(*MP4_ReadBox_function
)(stream_t
*, MP4_Box_t
*) );
236 static int MP4_Box_Read_Specific( stream_t
*p_stream
, MP4_Box_t
*p_box
, MP4_Box_t
*p_father
);
237 static int MP4_PeekBoxHeader( stream_t
*p_stream
, MP4_Box_t
*p_box
);
239 int MP4_Seek( stream_t
*p_stream
, uint64_t i_pos
)
241 bool b_canseek
= false;
242 if ( vlc_stream_Control( p_stream
, STREAM_CAN_SEEK
, &b_canseek
) != VLC_SUCCESS
||
245 /* can seek or don't know */
246 return vlc_stream_Seek( p_stream
, i_pos
);
248 /* obviously can't seek then */
250 int64_t i_current_pos
= vlc_stream_Tell( p_stream
);
251 if ( i_current_pos
< 0 || i_pos
< (uint64_t)i_current_pos
)
254 size_t i_toread
= i_pos
- i_current_pos
;
257 else if( i_toread
> (1<<17) )
260 if( vlc_stream_Read( p_stream
, NULL
, i_toread
) != (ssize_t
)i_toread
)
265 static void MP4_BoxAddChild( MP4_Box_t
*p_parent
, MP4_Box_t
*p_childbox
)
267 if( !p_parent
->p_first
)
268 p_parent
->p_first
= p_childbox
;
270 p_parent
->p_last
->p_next
= p_childbox
;
271 p_parent
->p_last
= p_childbox
;
272 p_childbox
->p_father
= p_parent
;
275 MP4_Box_t
* MP4_BoxExtract( MP4_Box_t
**pp_chain
, uint32_t i_type
)
277 MP4_Box_t
*p_box
= *pp_chain
;
280 if( p_box
->i_type
== i_type
)
282 *pp_chain
= p_box
->p_next
;
283 p_box
->p_next
= NULL
;
286 pp_chain
= &p_box
->p_next
;
287 p_box
= p_box
->p_next
;
292 /* Don't use vlc_stream_Seek directly */
293 #undef vlc_stream_Seek
294 #define vlc_stream_Seek(a,b) __NO__
296 /*****************************************************************************
297 * MP4_PeekBoxHeader : Load only common parameters for all boxes
298 *****************************************************************************
299 * p_box need to be an already allocated MP4_Box_t, and all data
300 * will only be peek not read
302 * RETURN : 0 if it fail, 1 otherwise
303 *****************************************************************************/
304 static int MP4_PeekBoxHeader( stream_t
*p_stream
, MP4_Box_t
*p_box
)
307 const uint8_t *p_peek
;
309 if( ( ( i_read
= vlc_stream_Peek( p_stream
, &p_peek
, 32 ) ) < 8 ) )
313 p_box
->i_pos
= vlc_stream_Tell( p_stream
);
315 p_box
->data
.p_payload
= NULL
;
316 p_box
->p_father
= NULL
;
317 p_box
->p_first
= NULL
;
318 p_box
->p_last
= NULL
;
319 p_box
->p_next
= NULL
;
321 MP4_GET4BYTES( p_box
->i_shortsize
);
322 MP4_GETFOURCC( p_box
->i_type
);
324 /* Now special case */
326 if( p_box
->i_shortsize
== 1 )
330 /* get the true size on 64 bits */
331 MP4_GET8BYTES( p_box
->i_size
);
335 p_box
->i_size
= p_box
->i_shortsize
;
336 /* XXX size of 0 means that the box extends to end of file */
339 if( UINT64_MAX
- p_box
->i_size
< p_box
->i_pos
)
342 if( p_box
->i_type
== ATOM_uuid
)
346 /* get extented type on 16 bytes */
347 GetUUID( &p_box
->i_uuid
, p_peek
);
350 #ifdef MP4_ULTRA_VERBOSE
353 if MP4_BOX_TYPE_ASCII()
354 msg_Dbg( p_stream
, "found Box: %4.4s size %"PRId64
" %"PRId64
,
355 (char*)&p_box
->i_type
, p_box
->i_size
, p_box
->i_pos
);
357 msg_Dbg( p_stream
, "found Box: c%3.3s size %"PRId64
,
358 (char*)&p_box
->i_type
+1, p_box
->i_size
);
365 /*****************************************************************************
366 * MP4_ReadBoxRestricted : Reads box from current position
367 *****************************************************************************
368 * if p_box == NULL, box is invalid or failed, position undefined
369 * on success, position is past read box or EOF
370 *****************************************************************************/
371 static MP4_Box_t
*MP4_ReadBoxRestricted( stream_t
*p_stream
, MP4_Box_t
*p_father
,
372 const uint32_t stopbefore
[], bool *pb_restrictionhit
)
374 MP4_Box_t peekbox
= { 0 };
375 if ( !MP4_PeekBoxHeader( p_stream
, &peekbox
) )
378 if( peekbox
.i_size
< 8 )
380 msg_Warn( p_stream
, "found an invalid sized %"PRIu64
" box %4.4s @%"PRIu64
,
381 peekbox
.i_size
, (char *) &peekbox
.i_type
, vlc_stream_Tell(p_stream
) );
385 for( size_t i
=0; stopbefore
&& stopbefore
[i
]; i
++ )
387 if( stopbefore
[i
] == peekbox
.i_type
)
389 *pb_restrictionhit
= true;
394 /* if father's size == 0, it means unknown or infinite size,
395 * and we skip the followong check */
396 if( p_father
&& p_father
->i_size
> 0 )
398 const uint64_t i_box_next
= peekbox
.i_size
+ peekbox
.i_pos
;
399 const uint64_t i_father_next
= p_father
->i_size
+ p_father
->i_pos
;
400 /* check if it's within p-father */
401 if( i_box_next
> i_father_next
)
403 msg_Warn( p_stream
, "out of bound child %4.4s", (char*) &peekbox
.i_type
);
404 return NULL
; /* out of bound */
408 /* Everything seems OK */
409 MP4_Box_t
*p_box
= (MP4_Box_t
*) malloc( sizeof(MP4_Box_t
) );
414 const uint64_t i_next
= p_box
->i_pos
+ p_box
->i_size
;
415 p_box
->p_father
= p_father
;
416 if( MP4_Box_Read_Specific( p_stream
, p_box
, p_father
) != VLC_SUCCESS
)
418 msg_Warn( p_stream
, "Failed reading box %4.4s", (char*) &peekbox
.i_type
);
419 MP4_BoxFree( p_box
);
423 /* Check is we consumed all data */
424 if( vlc_stream_Tell( p_stream
) < i_next
)
426 MP4_Seek( p_stream
, i_next
- 1 ); /* since past seek can fail when hitting EOF */
427 MP4_Seek( p_stream
, i_next
);
428 if( vlc_stream_Tell( p_stream
) < i_next
- 1 ) /* Truncated box */
430 msg_Warn( p_stream
, "truncated box %4.4s discarded", (char*) &peekbox
.i_type
);
431 MP4_BoxFree( p_box
);
437 MP4_BoxAddChild( p_father
, p_box
);
442 /*****************************************************************************
443 * For all known box a loader is given,
444 * you have to be already read container header
445 * without container size, file position on exit is unknown
446 *****************************************************************************/
447 static int MP4_ReadBoxContainerChildrenIndexed( stream_t
*p_stream
,
448 MP4_Box_t
*p_container
, const uint32_t stoplist
[],
449 const uint32_t excludelist
[], bool b_indexed
)
451 /* Size of root container is set to 0 when unknown, for exemple
452 * with a DASH stream. In that case, we skip the following check */
453 if( (p_container
->i_size
|| p_container
->p_father
)
454 && ( vlc_stream_Tell( p_stream
) + ((b_indexed
)?16:8) >
455 (uint64_t)(p_container
->i_pos
+ p_container
->i_size
) )
458 /* there is no box to load */
462 uint64_t i_last_pos
= 0; /* used to detect read failure loops */
463 const uint64_t i_end
= p_container
->i_pos
+ p_container
->i_size
;
464 MP4_Box_t
*p_box
= NULL
;
465 bool b_onexclude
= false;
470 if ( p_container
->i_size
)
472 const uint64_t i_tell
= vlc_stream_Tell( p_stream
);
473 if( i_tell
+ ((b_indexed
)?16:8) >= i_end
)
477 uint32_t i_index
= 0;
481 if ( vlc_stream_Read( p_stream
, read
, 8 ) < 8 )
483 i_index
= GetDWBE(&read
[4]);
485 b_onexclude
= false; /* If stopped due exclude list */
486 if( (p_box
= MP4_ReadBoxRestricted( p_stream
, p_container
, excludelist
, &b_onexclude
)) )
489 p_box
->i_index
= i_index
;
490 for(size_t i
=0; stoplist
&& stoplist
[i
]; i
++)
492 if( p_box
->i_type
== stoplist
[i
] )
497 const uint64_t i_tell
= vlc_stream_Tell( p_stream
);
498 if ( p_container
->i_size
&& i_tell
>= i_end
)
500 assert( i_tell
== i_end
);
506 /* Continue with next if box fails to load */
507 if( i_last_pos
== i_tell
)
513 } while( b_continue
);
515 /* Always move to end of container */
516 if ( !b_onexclude
&& p_container
->i_size
)
518 const uint64_t i_tell
= vlc_stream_Tell( p_stream
);
519 if ( i_tell
!= i_end
)
520 MP4_Seek( p_stream
, i_end
);
526 int MP4_ReadBoxContainerRestricted( stream_t
*p_stream
, MP4_Box_t
*p_container
,
527 const uint32_t stoplist
[], const uint32_t excludelist
[] )
529 return MP4_ReadBoxContainerChildrenIndexed( p_stream
, p_container
,
530 stoplist
, excludelist
, false );
533 int MP4_ReadBoxContainerChildren( stream_t
*p_stream
, MP4_Box_t
*p_container
,
534 const uint32_t stoplist
[] )
536 return MP4_ReadBoxContainerChildrenIndexed( p_stream
, p_container
,
537 stoplist
, NULL
, false );
540 static void MP4_BoxOffsetUp( MP4_Box_t
*p_box
, uint64_t i_offset
)
544 p_box
->i_pos
+= i_offset
;
545 MP4_BoxOffsetUp( p_box
->p_first
, i_offset
);
546 p_box
= p_box
->p_next
;
550 /* Reads within an already read/in memory box (containers without having to seek) */
551 static int MP4_ReadBoxContainerRawInBox( stream_t
*p_stream
, MP4_Box_t
*p_container
,
552 uint8_t *p_buffer
, uint64_t i_size
, uint64_t i_offset
)
556 stream_t
*p_substream
= vlc_stream_MemoryNew( p_stream
, p_buffer
, i_size
,
560 MP4_Box_t
*p_last
= p_container
->p_last
;
561 MP4_ReadBoxContainerChildren( p_substream
, p_container
, NULL
);
562 vlc_stream_Delete( p_substream
);
566 MP4_Box_t
*p_box
= p_last
? p_last
: p_container
->p_first
;
567 MP4_BoxOffsetUp(p_box
, i_offset
);
573 static int MP4_ReadBoxContainer( stream_t
*p_stream
, MP4_Box_t
*p_container
)
575 if( p_container
->i_size
&&
576 ( p_container
->i_size
<= (size_t)mp4_box_headersize(p_container
) + 8 ) )
578 /* container is empty, 8 stand for the first header in this box */
583 if ( MP4_Seek( p_stream
, p_container
->i_pos
+
584 mp4_box_headersize( p_container
) ) )
586 return MP4_ReadBoxContainerChildren( p_stream
, p_container
, NULL
);
589 static int MP4_ReadBoxSkip( stream_t
*p_stream
, MP4_Box_t
*p_box
)
591 /* XXX sometime moov is hidden in a free box */
592 if( p_box
->p_father
&&
593 p_box
->p_father
->i_type
== ATOM_root
&&
594 p_box
->i_type
== ATOM_free
)
596 const uint8_t *p_peek
;
597 size_t header_size
= mp4_box_headersize( p_box
) + 4;
600 ssize_t i_read
= vlc_stream_Peek( p_stream
, &p_peek
, 44 );
601 if( unlikely(i_read
< (ssize_t
)header_size
) )
604 p_peek
+= header_size
;
605 i_read
-= header_size
;
609 i_fcc
= VLC_FOURCC( p_peek
[0], p_peek
[1], p_peek
[2], p_peek
[3] );
611 if( i_fcc
== ATOM_cmov
|| i_fcc
== ATOM_mvhd
)
613 msg_Warn( p_stream
, "detected moov hidden in a free box ..." );
615 p_box
->i_type
= ATOM_foov
;
616 return MP4_ReadBoxContainer( p_stream
, p_box
);
622 #ifdef MP4_ULTRA_VERBOSE
623 if MP4_BOX_TYPE_ASCII()
624 msg_Dbg( p_stream
, "skip box: \"%4.4s\"", (char*)&p_box
->i_type
);
626 msg_Dbg( p_stream
, "skip box: \"c%3.3s\"", (char*)&p_box
->i_type
+1 );
631 static int MP4_ReadBox_ilst( stream_t
*p_stream
, MP4_Box_t
*p_box
)
633 if( p_box
->i_size
< 8 || vlc_stream_Read( p_stream
, NULL
, 8 ) < 8 )
636 /* Find our handler */
637 if ( !p_box
->i_handler
&& p_box
->p_father
)
639 const MP4_Box_t
*p_sibling
= p_box
->p_father
->p_first
;
642 if ( p_sibling
->i_type
== ATOM_hdlr
&& p_sibling
->data
.p_hdlr
)
644 p_box
->i_handler
= p_sibling
->data
.p_hdlr
->i_handler_type
;
647 p_sibling
= p_sibling
->p_next
;
651 switch( p_box
->i_handler
)
654 msg_Warn( p_stream
, "no handler for ilst atom" );
657 return MP4_ReadBoxContainerChildrenIndexed( p_stream
, p_box
, NULL
, NULL
, true );
659 return MP4_ReadBoxContainerChildren( p_stream
, p_box
, NULL
);
661 msg_Warn( p_stream
, "Unknown ilst handler type '%4.4s'", (char*)&p_box
->i_handler
);
666 static void MP4_FreeBox_ftyp( MP4_Box_t
*p_box
)
668 free( p_box
->data
.p_ftyp
->i_compatible_brands
);
671 static int MP4_ReadBox_ftyp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
673 MP4_READBOX_ENTER( MP4_Box_data_ftyp_t
, MP4_FreeBox_ftyp
);
675 MP4_GETFOURCC( p_box
->data
.p_ftyp
->i_major_brand
);
676 MP4_GET4BYTES( p_box
->data
.p_ftyp
->i_minor_version
);
678 p_box
->data
.p_ftyp
->i_compatible_brands_count
= i_read
/ 4;
679 if( p_box
->data
.p_ftyp
->i_compatible_brands_count
> 0 )
681 uint32_t *tab
= p_box
->data
.p_ftyp
->i_compatible_brands
=
682 vlc_alloc( p_box
->data
.p_ftyp
->i_compatible_brands_count
,
685 if( unlikely( tab
== NULL
) )
686 MP4_READBOX_EXIT( 0 );
688 for( unsigned i
= 0; i
< p_box
->data
.p_ftyp
->i_compatible_brands_count
; i
++ )
690 MP4_GETFOURCC( tab
[i
] );
695 p_box
->data
.p_ftyp
->i_compatible_brands
= NULL
;
698 MP4_READBOX_EXIT( 1 );
702 static int MP4_ReadBox_mvhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
704 MP4_READBOX_ENTER( MP4_Box_data_mvhd_t
, NULL
);
706 MP4_GETVERSIONFLAGS( p_box
->data
.p_mvhd
);
708 if( p_box
->data
.p_mvhd
->i_version
)
710 MP4_GET8BYTES( p_box
->data
.p_mvhd
->i_creation_time
);
711 MP4_GET8BYTES( p_box
->data
.p_mvhd
->i_modification_time
);
712 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_timescale
);
713 MP4_GET8BYTES( p_box
->data
.p_mvhd
->i_duration
);
717 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_creation_time
);
718 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_modification_time
);
719 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_timescale
);
720 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_duration
);
722 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_rate
);
723 MP4_GET2BYTES( p_box
->data
.p_mvhd
->i_volume
);
724 MP4_GET2BYTES( p_box
->data
.p_mvhd
->i_reserved1
);
727 for( unsigned i
= 0; i
< 2; i
++ )
729 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_reserved2
[i
] );
731 for( unsigned i
= 0; i
< 9; i
++ )
733 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_matrix
[i
] );
735 for( unsigned i
= 0; i
< 6; i
++ )
737 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_predefined
[i
] );
740 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_next_track_id
);
744 char *psz_duration
= MP4_Time2Str( p_box
->data
.p_mvhd
->i_duration
, p_box
->data
.p_mvhd
->i_timescale
);
745 msg_Dbg( p_stream
, "read box: \"mvhd\" timescale %"PRIu32
" duration %"PRIu64
" (%s) rate %.2f volume %.2f",
746 p_box
->data
.p_mvhd
->i_timescale
,
747 p_box
->data
.p_mvhd
->i_duration
,
749 (float)p_box
->data
.p_mvhd
->i_rate
/ (1<<16 ),
750 (float)p_box
->data
.p_mvhd
->i_volume
/ 256 );
751 free( psz_duration
);
753 MP4_READBOX_EXIT( 1 );
756 static int MP4_ReadBox_mfhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
758 MP4_READBOX_ENTER( MP4_Box_data_mfhd_t
, NULL
);
760 MP4_GETVERSIONFLAGS( p_box
->data
.p_mvhd
);
762 MP4_GET4BYTES( p_box
->data
.p_mfhd
->i_sequence_number
);
765 msg_Dbg( p_stream
, "read box: \"mfhd\" sequence number %d",
766 p_box
->data
.p_mfhd
->i_sequence_number
);
768 MP4_READBOX_EXIT( 1 );
771 static int MP4_ReadBox_tfxd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
773 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t
, NULL
);
775 MP4_Box_data_tfxd_t
*p_tfxd_data
= p_box
->data
.p_tfxd
;
776 MP4_GETVERSIONFLAGS( p_tfxd_data
);
778 if( p_tfxd_data
->i_version
== 0 )
780 MP4_GET4BYTES( p_tfxd_data
->i_fragment_abs_time
);
781 MP4_GET4BYTES( p_tfxd_data
->i_fragment_duration
);
785 MP4_GET8BYTES( p_tfxd_data
->i_fragment_abs_time
);
786 MP4_GET8BYTES( p_tfxd_data
->i_fragment_duration
);
790 msg_Dbg( p_stream
, "read box: \"tfxd\" version %d, flags 0x%x, "\
791 "fragment duration %"PRIu64
", fragment abs time %"PRIu64
,
792 p_tfxd_data
->i_version
,
793 p_tfxd_data
->i_flags
,
794 p_tfxd_data
->i_fragment_duration
,
795 p_tfxd_data
->i_fragment_abs_time
799 MP4_READBOX_EXIT( 1 );
802 static void MP4_FreeBox_tfrf( MP4_Box_t
*p_box
)
804 free( p_box
->data
.p_tfrf
->p_tfrf_data_fields
);
807 static int MP4_ReadBox_tfrf( stream_t
*p_stream
, MP4_Box_t
*p_box
)
809 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t
, MP4_FreeBox_tfrf
);
811 MP4_Box_data_tfrf_t
*p_tfrf_data
= p_box
->data
.p_tfrf
;
812 MP4_GETVERSIONFLAGS( p_tfrf_data
);
814 MP4_GET1BYTE( p_tfrf_data
->i_fragment_count
);
816 p_tfrf_data
->p_tfrf_data_fields
= calloc( p_tfrf_data
->i_fragment_count
,
817 sizeof( TfrfBoxDataFields_t
) );
818 if( !p_tfrf_data
->p_tfrf_data_fields
)
819 MP4_READBOX_EXIT( 0 );
821 for( uint8_t i
= 0; i
< p_tfrf_data
->i_fragment_count
; i
++ )
823 TfrfBoxDataFields_t
*TfrfBoxDataField
= &p_tfrf_data
->p_tfrf_data_fields
[i
];
824 if( p_tfrf_data
->i_version
== 0 )
826 MP4_GET4BYTES( TfrfBoxDataField
->i_fragment_abs_time
);
827 MP4_GET4BYTES( TfrfBoxDataField
->i_fragment_duration
);
831 MP4_GET8BYTES( TfrfBoxDataField
->i_fragment_abs_time
);
832 MP4_GET8BYTES( TfrfBoxDataField
->i_fragment_duration
);
837 msg_Dbg( p_stream
, "read box: \"tfrf\" version %d, flags 0x%x, "\
838 "fragment count %"PRIu8
, p_tfrf_data
->i_version
,
839 p_tfrf_data
->i_flags
, p_tfrf_data
->i_fragment_count
);
841 for( uint8_t i
= 0; i
< p_tfrf_data
->i_fragment_count
; i
++ )
843 TfrfBoxDataFields_t
*TfrfBoxDataField
= &p_tfrf_data
->p_tfrf_data_fields
[i
];
844 msg_Dbg( p_stream
, "\"tfrf\" fragment duration %"PRIu64
", "\
845 "fragment abs time %"PRIu64
,
846 TfrfBoxDataField
->i_fragment_duration
,
847 TfrfBoxDataField
->i_fragment_abs_time
);
852 MP4_READBOX_EXIT( 1 );
855 static int MP4_ReadBox_XML360( stream_t
*p_stream
, MP4_Box_t
*p_box
)
857 MP4_READBOX_ENTER( MP4_Box_data_360_t
, NULL
);
859 MP4_Box_data_360_t
*p_360_data
= p_box
->data
.p_360
;
861 /* Copy the string for pattern matching as it does not end
862 with a '\0' in the stream. */
863 char *psz_rdf
= strndup((char *)p_peek
, i_read
);
865 if ( unlikely( !psz_rdf
) )
866 MP4_READBOX_EXIT( 0 );
868 /* Try to find the string "GSpherical:Spherical" because the v1
869 spherical video spec says the tag must be there. */
871 if ( strcasestr( psz_rdf
, "Gspherical:Spherical" ) )
872 p_360_data
->i_projection_mode
= PROJECTION_MODE_EQUIRECTANGULAR
;
874 /* Try to find the stero mode. */
875 if ( strcasestr( psz_rdf
, "left-right" ) )
877 msg_Dbg( p_stream
, "Left-right stereo mode" );
878 p_360_data
->e_stereo_mode
= XML360_STEREOSCOPIC_LEFT_RIGHT
;
881 if ( strcasestr( psz_rdf
, "top-bottom" ) )
883 msg_Dbg( p_stream
, "Top-bottom stereo mode" );
884 p_360_data
->e_stereo_mode
= XML360_STEREOSCOPIC_TOP_BOTTOM
;
889 MP4_READBOX_EXIT( 1 );
892 static int MP4_ReadBox_st3d( stream_t
*p_stream
, MP4_Box_t
*p_box
)
894 MP4_READBOX_ENTER( MP4_Box_data_st3d_t
, NULL
);
897 MP4_GET1BYTE( i_version
);
898 if ( i_version
!= 0 )
899 MP4_READBOX_EXIT( 0 );
902 VLC_UNUSED( i_flags
);
903 MP4_GET3BYTES( i_flags
);
905 MP4_Box_data_st3d_t
*p_data
= p_box
->data
.p_st3d
;
906 MP4_GET1BYTE( p_data
->i_stereo_mode
);
908 MP4_READBOX_EXIT( 1 );
911 static int MP4_ReadBox_prhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
913 MP4_READBOX_ENTER( MP4_Box_data_prhd_t
, NULL
);
916 MP4_GET1BYTE( i_version
);
918 MP4_READBOX_EXIT( 0 );
921 VLC_UNUSED( i_flags
);
922 MP4_GET3BYTES( i_flags
);
924 MP4_Box_data_prhd_t
*p_data
= p_box
->data
.p_prhd
;
926 MP4_GET4BYTES( fixed16_16
);
927 p_data
->f_pose_yaw_degrees
= (float) fixed16_16
/ 65536.0f
;
929 MP4_GET4BYTES( fixed16_16
);
930 p_data
->f_pose_pitch_degrees
= (float) fixed16_16
/ 65536.0f
;
932 MP4_GET4BYTES( fixed16_16
);
933 p_data
->f_pose_roll_degrees
= (float) fixed16_16
/ 65536.0f
;
935 MP4_READBOX_EXIT( 1 );
938 static int MP4_ReadBox_equi( stream_t
*p_stream
, MP4_Box_t
*p_box
)
940 MP4_READBOX_ENTER( MP4_Box_data_equi_t
, NULL
);
943 MP4_GET1BYTE( i_version
);
945 MP4_READBOX_EXIT( 0 );
948 VLC_UNUSED( i_flags
);
949 MP4_GET3BYTES( i_flags
);
951 MP4_Box_data_equi_t
*p_data
= p_box
->data
.p_equi
;
952 MP4_GET4BYTES( p_data
->i_projection_bounds_top
);
953 MP4_GET4BYTES( p_data
->i_projection_bounds_bottom
);
954 MP4_GET4BYTES( p_data
->i_projection_bounds_left
);
955 MP4_GET4BYTES( p_data
->i_projection_bounds_right
);
957 MP4_READBOX_EXIT( 1 );
960 static int MP4_ReadBox_cbmp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
962 MP4_READBOX_ENTER( MP4_Box_data_cbmp_t
, NULL
);
965 MP4_GET1BYTE( i_version
);
967 MP4_READBOX_EXIT( 0 );
970 VLC_UNUSED( i_flags
);
971 MP4_GET3BYTES( i_flags
);
973 MP4_Box_data_cbmp_t
*p_data
= p_box
->data
.p_cbmp
;
974 MP4_GET4BYTES( p_data
->i_layout
);
975 MP4_GET4BYTES( p_data
->i_padding
);
977 MP4_READBOX_EXIT( 1 );
980 static void MP4_FreeBox_sidx( MP4_Box_t
*p_box
)
982 free( p_box
->data
.p_sidx
->p_items
);
985 static int MP4_ReadBox_sidx( stream_t
*p_stream
, MP4_Box_t
*p_box
)
987 MP4_READBOX_ENTER( MP4_Box_data_sidx_t
, MP4_FreeBox_sidx
);
989 MP4_Box_data_sidx_t
*p_sidx_data
= p_box
->data
.p_sidx
;
990 MP4_GETVERSIONFLAGS( p_sidx_data
);
992 MP4_GET4BYTES( p_sidx_data
->i_reference_ID
);
993 MP4_GET4BYTES( p_sidx_data
->i_timescale
);
995 if( p_sidx_data
->i_version
== 0 )
997 MP4_GET4BYTES( p_sidx_data
->i_earliest_presentation_time
);
998 MP4_GET4BYTES( p_sidx_data
->i_first_offset
);
1002 MP4_GET8BYTES( p_sidx_data
->i_earliest_presentation_time
);
1003 MP4_GET8BYTES( p_sidx_data
->i_first_offset
);
1006 uint16_t i_reserved
, i_count
;
1008 VLC_UNUSED(i_reserved
);
1009 MP4_GET2BYTES( i_reserved
);
1010 MP4_GET2BYTES( i_count
);
1012 MP4_READBOX_EXIT( 1 );
1014 p_sidx_data
->i_reference_count
= i_count
;
1015 p_sidx_data
->p_items
= vlc_alloc( i_count
, sizeof( MP4_Box_sidx_item_t
) );
1016 if( unlikely(p_sidx_data
->p_items
== NULL
) )
1017 MP4_READBOX_EXIT( 0 );
1019 for( unsigned i
= 0; i
< i_count
; i
++ )
1021 MP4_Box_sidx_item_t
*item
= p_sidx_data
->p_items
+ i
;
1024 MP4_GET4BYTES( tmp
);
1025 item
->b_reference_type
= tmp
>> 31;
1026 item
->i_referenced_size
= tmp
& 0x7fffffff;
1027 MP4_GET4BYTES( item
->i_subsegment_duration
);
1029 MP4_GET4BYTES( tmp
);
1030 item
->b_starts_with_SAP
= tmp
>> 31;
1031 item
->i_SAP_type
= (tmp
>> 24) & 0x70;
1032 item
->i_SAP_delta_time
= tmp
& 0xfffffff;
1036 msg_Dbg( p_stream
, "read box: \"sidx\" version %d, flags 0x%x, "\
1037 "ref_ID %"PRIu32
", timescale %"PRIu32
", ref_count %"PRIu16
", "\
1038 "first subsegmt duration %"PRIu32
,
1039 p_sidx_data
->i_version
,
1040 p_sidx_data
->i_flags
,
1041 p_sidx_data
->i_reference_ID
,
1042 p_sidx_data
->i_timescale
,
1043 p_sidx_data
->i_reference_count
,
1044 p_sidx_data
->p_items
[0].i_subsegment_duration
1048 MP4_READBOX_EXIT( 1 );
1051 static int MP4_ReadBox_tfhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1053 MP4_READBOX_ENTER( MP4_Box_data_tfhd_t
, NULL
);
1055 MP4_GETVERSIONFLAGS( p_box
->data
.p_tfhd
);
1057 if( p_box
->data
.p_tfhd
->i_version
!= 0 )
1059 msg_Warn( p_stream
, "'tfhd' box with version != 0. "\
1060 " Don't know what to do with that, please patch" );
1061 MP4_READBOX_EXIT( 0 );
1064 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_track_ID
);
1066 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DURATION_IS_EMPTY
)
1068 msg_Dbg( p_stream
, "'duration-is-empty' flag is present "\
1069 "=> no samples for this time interval." );
1070 p_box
->data
.p_tfhd
->b_empty
= true;
1073 p_box
->data
.p_tfhd
->b_empty
= false;
1075 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
)
1076 MP4_GET8BYTES( p_box
->data
.p_tfhd
->i_base_data_offset
);
1077 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_SAMPLE_DESC_INDEX
)
1078 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_sample_description_index
);
1079 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
1080 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_default_sample_duration
);
1081 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_SIZE
)
1082 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_default_sample_size
);
1083 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_FLAGS
)
1084 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_default_sample_flags
);
1087 char psz_base
[128] = "\0";
1088 char psz_desc
[128] = "\0";
1089 char psz_dura
[128] = "\0";
1090 char psz_size
[128] = "\0";
1091 char psz_flag
[128] = "\0";
1092 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
)
1093 snprintf(psz_base
, sizeof(psz_base
), "base offset %"PRId64
, p_box
->data
.p_tfhd
->i_base_data_offset
);
1094 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_SAMPLE_DESC_INDEX
)
1095 snprintf(psz_desc
, sizeof(psz_desc
), "sample description index %d", p_box
->data
.p_tfhd
->i_sample_description_index
);
1096 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
1097 snprintf(psz_dura
, sizeof(psz_dura
), "sample duration %d", p_box
->data
.p_tfhd
->i_default_sample_duration
);
1098 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_SIZE
)
1099 snprintf(psz_size
, sizeof(psz_size
), "sample size %d", p_box
->data
.p_tfhd
->i_default_sample_size
);
1100 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_FLAGS
)
1101 snprintf(psz_flag
, sizeof(psz_flag
), "sample flags 0x%x", p_box
->data
.p_tfhd
->i_default_sample_flags
);
1103 msg_Dbg( p_stream
, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
1104 p_box
->data
.p_tfhd
->i_version
,
1105 p_box
->data
.p_tfhd
->i_flags
,
1106 p_box
->data
.p_tfhd
->i_track_ID
,
1107 psz_base
, psz_desc
, psz_dura
, psz_size
, psz_flag
);
1110 MP4_READBOX_EXIT( 1 );
1113 static void MP4_FreeBox_trun( MP4_Box_t
*p_box
)
1115 free( p_box
->data
.p_trun
->p_samples
);
1118 static int MP4_ReadBox_trun( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1122 MP4_READBOX_ENTER( MP4_Box_data_trun_t
, MP4_FreeBox_trun
);
1123 MP4_Box_data_trun_t
*p_trun
= p_box
->data
.p_trun
;
1124 MP4_GETVERSIONFLAGS( p_trun
);
1125 MP4_GET4BYTES( count
);
1127 if( p_trun
->i_flags
& MP4_TRUN_DATA_OFFSET
)
1128 MP4_GET4BYTES( p_trun
->i_data_offset
);
1129 if( p_trun
->i_flags
& MP4_TRUN_FIRST_FLAGS
)
1130 MP4_GET4BYTES( p_trun
->i_first_sample_flags
);
1132 uint64_t i_entry_size
=
1133 !!(p_trun
->i_flags
& MP4_TRUN_SAMPLE_DURATION
) +
1134 !!(p_trun
->i_flags
& MP4_TRUN_SAMPLE_SIZE
) +
1135 !!(p_trun
->i_flags
& MP4_TRUN_SAMPLE_FLAGS
) +
1136 !!(p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
);
1138 if( i_entry_size
* 4 * count
> i_read
)
1139 MP4_READBOX_EXIT( 0 );
1141 p_trun
->p_samples
= vlc_alloc( count
, sizeof(MP4_descriptor_trun_sample_t
) );
1142 if ( p_trun
->p_samples
== NULL
)
1143 MP4_READBOX_EXIT( 0 );
1144 p_trun
->i_sample_count
= count
;
1146 for( unsigned int i
= 0; i
< count
; i
++ )
1148 MP4_descriptor_trun_sample_t
*p_sample
= &p_trun
->p_samples
[i
];
1149 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
1150 MP4_GET4BYTES( p_sample
->i_duration
);
1151 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
1152 MP4_GET4BYTES( p_sample
->i_size
);
1153 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_FLAGS
)
1154 MP4_GET4BYTES( p_sample
->i_flags
);
1155 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
)
1156 MP4_GET4BYTES( p_sample
->i_composition_time_offset
.v0
);
1159 #ifdef MP4_ULTRA_VERBOSE
1160 msg_Dbg( p_stream
, "read box: \"trun\" version %u flags 0x%x sample count %"PRIu32
,
1163 p_trun
->i_sample_count
);
1165 for( unsigned int i
= 0; i
< count
; i
++ )
1167 MP4_descriptor_trun_sample_t
*p_sample
= &p_trun
->p_samples
[i
];
1168 msg_Dbg( p_stream
, "read box: \"trun\" sample %4.4u flags 0x%x "\
1169 "duration %"PRIu32
" size %"PRIu32
" composition time offset %"PRIu32
,
1170 i
, p_sample
->i_flags
, p_sample
->i_duration
,
1171 p_sample
->i_size
, p_sample
->i_composition_time_offset
);
1175 MP4_READBOX_EXIT( 1 );
1178 static int MP4_ReadBox_tfdt( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1180 MP4_READBOX_ENTER( MP4_Box_data_tfdt_t
, NULL
);
1182 MP4_READBOX_EXIT( 0 );
1184 MP4_GETVERSIONFLAGS( p_box
->data
.p_tfdt
);
1186 if( p_box
->data
.p_tfdt
->i_version
== 0 )
1187 MP4_GET4BYTES( p_box
->data
.p_tfdt
->i_base_media_decode_time
);
1188 else if( p_box
->data
.p_tfdt
->i_version
== 1 )
1189 MP4_GET8BYTES( p_box
->data
.p_tfdt
->i_base_media_decode_time
);
1191 MP4_READBOX_EXIT( 0 );
1193 MP4_READBOX_EXIT( 1 );
1196 static int MP4_ReadBox_tkhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1198 MP4_READBOX_ENTER( MP4_Box_data_tkhd_t
, NULL
);
1200 MP4_GETVERSIONFLAGS( p_box
->data
.p_tkhd
);
1202 if( p_box
->data
.p_tkhd
->i_version
)
1204 MP4_GET8BYTES( p_box
->data
.p_tkhd
->i_creation_time
);
1205 MP4_GET8BYTES( p_box
->data
.p_tkhd
->i_modification_time
);
1206 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_track_ID
);
1207 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_reserved
);
1208 MP4_GET8BYTES( p_box
->data
.p_tkhd
->i_duration
);
1212 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_creation_time
);
1213 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_modification_time
);
1214 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_track_ID
);
1215 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_reserved
);
1216 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_duration
);
1219 for( unsigned i
= 0; i
< 2; i
++ )
1221 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_reserved2
[i
] );
1223 MP4_GET2BYTES( p_box
->data
.p_tkhd
->i_layer
);
1224 MP4_GET2BYTES( p_box
->data
.p_tkhd
->i_predefined
);
1225 MP4_GET2BYTES( p_box
->data
.p_tkhd
->i_volume
);
1226 MP4_GET2BYTES( p_box
->data
.p_tkhd
->i_reserved3
);
1228 for( unsigned i
= 0; i
< 9; i
++ )
1230 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_matrix
[i
] );
1232 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_width
);
1233 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_height
);
1235 double rotation
= 0;//angle in degrees to be rotated clockwise
1236 double scale
[2]; // scale factor; sx = scale[0] , sy = scale[1]
1237 int32_t *matrix
= p_box
->data
.p_tkhd
->i_matrix
;
1239 int64_t det
= (int64_t)matrix
[0] * matrix
[4] - (int64_t)matrix
[1] * matrix
[3];
1241 /* If determinant is negative copy the matrix and flip it horizontally. */
1242 const int flip
[] = { -1, 1, 1 };
1243 for (int j
= 0; j
< 9; j
++)
1244 matrix
[j
] *= flip
[j
% 3];
1245 p_box
->data
.p_tkhd
->i_flip
= 1;
1248 scale
[0] = sqrt(conv_fx(matrix
[0]) * conv_fx(matrix
[0]) +
1249 conv_fx(matrix
[3]) * conv_fx(matrix
[3]));
1250 scale
[1] = sqrt(conv_fx(matrix
[1]) * conv_fx(matrix
[1]) +
1251 conv_fx(matrix
[4]) * conv_fx(matrix
[4]));
1253 if( likely(scale
[0] > 0 && scale
[1] > 0) )
1255 rotation
= atan2(conv_fx(matrix
[1]) / scale
[1],
1256 conv_fx(matrix
[0]) / scale
[0]) * 180 / M_PI
;
1261 p_box
->data
.p_tkhd
->f_rotation
= rotation
;
1264 double translate
[2];// amount to translate; tx = translate[0] , ty = translate[1]
1266 translate
[0] = conv_fx(matrix
[6]);
1267 translate
[1] = conv_fx(matrix
[7]);
1269 msg_Dbg( p_stream
, "read box: \"tkhd\" track #%"PRIu32
" duration %"PRIu64
" layer %d "
1270 "volume %3.1f rotation %3.1f scale %.2fx%.2f translate +%.2f+%.2f size %ux%u. "
1271 "Matrix: %i %i %i %i %i %i %i %i %i",
1272 p_box
->data
.p_tkhd
->i_track_ID
,
1273 p_box
->data
.p_tkhd
->i_duration
,
1274 p_box
->data
.p_tkhd
->i_layer
,
1275 (float)p_box
->data
.p_tkhd
->i_volume
/ 256 ,
1281 (unsigned)p_box
->data
.p_tkhd
->i_width
/ BLOCK16x16
,
1282 (unsigned)p_box
->data
.p_tkhd
->i_height
/ BLOCK16x16
,
1283 p_box
->data
.p_tkhd
->i_matrix
[0],
1284 p_box
->data
.p_tkhd
->i_matrix
[1],
1285 p_box
->data
.p_tkhd
->i_matrix
[2],
1286 p_box
->data
.p_tkhd
->i_matrix
[3],
1287 p_box
->data
.p_tkhd
->i_matrix
[4],
1288 p_box
->data
.p_tkhd
->i_matrix
[5],
1289 p_box
->data
.p_tkhd
->i_matrix
[6],
1290 p_box
->data
.p_tkhd
->i_matrix
[7],
1291 p_box
->data
.p_tkhd
->i_matrix
[8] );
1293 MP4_READBOX_EXIT( 1 );
1296 static int MP4_ReadBox_load( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1298 if ( p_box
->i_size
!= 24 )
1300 MP4_READBOX_ENTER( MP4_Box_data_load_t
, NULL
);
1301 MP4_GET4BYTES( p_box
->data
.p_load
->i_start_time
);
1302 MP4_GET4BYTES( p_box
->data
.p_load
->i_duration
);
1303 MP4_GET4BYTES( p_box
->data
.p_load
->i_flags
);
1304 MP4_GET4BYTES( p_box
->data
.p_load
->i_hints
);
1305 MP4_READBOX_EXIT( 1 );
1308 static int MP4_ReadBox_mdhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1310 uint16_t i_language
;
1311 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t
, NULL
);
1313 MP4_GETVERSIONFLAGS( p_box
->data
.p_mdhd
);
1315 if( p_box
->data
.p_mdhd
->i_version
)
1317 MP4_GET8BYTES( p_box
->data
.p_mdhd
->i_creation_time
);
1318 MP4_GET8BYTES( p_box
->data
.p_mdhd
->i_modification_time
);
1319 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_timescale
);
1320 MP4_GET8BYTES( p_box
->data
.p_mdhd
->i_duration
);
1324 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_creation_time
);
1325 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_modification_time
);
1326 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_timescale
);
1327 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_duration
);
1330 MP4_GET2BYTES( i_language
);
1331 decodeQtLanguageCode( i_language
, p_box
->data
.p_mdhd
->rgs_language
,
1332 &p_box
->data
.p_mdhd
->b_mac_encoding
);
1334 MP4_GET2BYTES( p_box
->data
.p_mdhd
->i_quality
);
1337 char *psz_duration
= MP4_Time2Str( p_box
->data
.p_mdhd
->i_duration
, p_box
->data
.p_mdhd
->i_timescale
);
1338 msg_Dbg( p_stream
, "read box: \"mdhd\" timescale %"PRIu32
" duration %"PRIu64
" (%s) language %3.3s",
1339 p_box
->data
.p_mdhd
->i_timescale
,
1340 p_box
->data
.p_mdhd
->i_duration
,
1342 (char*) &p_box
->data
.p_mdhd
->rgs_language
);
1343 free( psz_duration
);
1345 MP4_READBOX_EXIT( 1 );
1348 static void MP4_FreeBox_hdlr( MP4_Box_t
*p_box
)
1350 free( p_box
->data
.p_hdlr
->psz_name
);
1353 static int MP4_ReadBox_hdlr( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1356 VLC_UNUSED(i_reserved
);
1358 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t
, MP4_FreeBox_hdlr
);
1360 MP4_GETVERSIONFLAGS( p_box
->data
.p_hdlr
);
1362 MP4_GETFOURCC( p_box
->data
.p_hdlr
->i_predefined
);
1363 MP4_GETFOURCC( p_box
->data
.p_hdlr
->i_handler_type
);
1365 MP4_GET4BYTES( i_reserved
);
1366 MP4_GET4BYTES( i_reserved
);
1367 MP4_GET4BYTES( i_reserved
);
1368 p_box
->data
.p_hdlr
->psz_name
= NULL
;
1370 if( i_read
>= SSIZE_MAX
)
1371 MP4_READBOX_EXIT( 0 );
1377 /* Yes, I love .mp4 :( */
1378 if( p_box
->data
.p_hdlr
->i_predefined
== VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
1382 MP4_GET1BYTE( i_len
);
1383 i_copy
= (i_len
<= i_read
) ? i_len
: i_read
;
1388 uint8_t *psz
= p_box
->data
.p_hdlr
->psz_name
= malloc( i_copy
+ 1 );
1389 if( unlikely( psz
== NULL
) )
1390 MP4_READBOX_EXIT( 0 );
1392 memcpy( psz
, p_peek
, i_copy
);
1393 p_box
->data
.p_hdlr
->psz_name
[i_copy
] = '\0';
1397 msg_Dbg( p_stream
, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
1398 (char*)&p_box
->data
.p_hdlr
->i_handler_type
,
1399 p_box
->data
.p_hdlr
->psz_name
);
1402 MP4_READBOX_EXIT( 1 );
1405 static int MP4_ReadBox_vmhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1407 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t
, NULL
);
1409 MP4_GETVERSIONFLAGS( p_box
->data
.p_vmhd
);
1411 MP4_GET2BYTES( p_box
->data
.p_vmhd
->i_graphics_mode
);
1412 for( unsigned i
= 0; i
< 3; i
++ )
1414 MP4_GET2BYTES( p_box
->data
.p_vmhd
->i_opcolor
[i
] );
1418 msg_Dbg( p_stream
, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
1419 p_box
->data
.p_vmhd
->i_graphics_mode
,
1420 p_box
->data
.p_vmhd
->i_opcolor
[0],
1421 p_box
->data
.p_vmhd
->i_opcolor
[1],
1422 p_box
->data
.p_vmhd
->i_opcolor
[2] );
1424 MP4_READBOX_EXIT( 1 );
1427 static int MP4_ReadBox_smhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1429 MP4_READBOX_ENTER( MP4_Box_data_smhd_t
, NULL
);
1431 MP4_GETVERSIONFLAGS( p_box
->data
.p_smhd
);
1435 MP4_GET2BYTES( p_box
->data
.p_smhd
->i_balance
);
1437 MP4_GET2BYTES( p_box
->data
.p_smhd
->i_reserved
);
1440 msg_Dbg( p_stream
, "read box: \"smhd\" balance %f",
1441 (float)p_box
->data
.p_smhd
->i_balance
/ 256 );
1443 MP4_READBOX_EXIT( 1 );
1447 static int MP4_ReadBox_hmhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1449 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t
, NULL
);
1451 MP4_GETVERSIONFLAGS( p_box
->data
.p_hmhd
);
1453 MP4_GET2BYTES( p_box
->data
.p_hmhd
->i_max_PDU_size
);
1454 MP4_GET2BYTES( p_box
->data
.p_hmhd
->i_avg_PDU_size
);
1456 MP4_GET4BYTES( p_box
->data
.p_hmhd
->i_max_bitrate
);
1457 MP4_GET4BYTES( p_box
->data
.p_hmhd
->i_avg_bitrate
);
1459 MP4_GET4BYTES( p_box
->data
.p_hmhd
->i_reserved
);
1462 msg_Dbg( p_stream
, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1463 p_box
->data
.p_hmhd
->i_max_PDU_size
,
1464 p_box
->data
.p_hmhd
->i_avg_PDU_size
,
1465 p_box
->data
.p_hmhd
->i_max_bitrate
,
1466 p_box
->data
.p_hmhd
->i_avg_bitrate
);
1468 MP4_READBOX_EXIT( 1 );
1471 static void MP4_FreeBox_url( MP4_Box_t
*p_box
)
1473 free( p_box
->data
.p_url
->psz_location
);
1476 static int MP4_ReadBox_url( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1478 MP4_READBOX_ENTER( MP4_Box_data_url_t
, MP4_FreeBox_url
);
1480 MP4_GETVERSIONFLAGS( p_box
->data
.p_url
);
1481 MP4_GETSTRINGZ( p_box
->data
.p_url
->psz_location
);
1484 msg_Dbg( p_stream
, "read box: \"url\" url: %s",
1485 p_box
->data
.p_url
->psz_location
);
1488 MP4_READBOX_EXIT( 1 );
1491 static void MP4_FreeBox_urn( MP4_Box_t
*p_box
)
1493 free( p_box
->data
.p_urn
->psz_name
);
1494 free( p_box
->data
.p_urn
->psz_location
);
1497 static int MP4_ReadBox_urn( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1499 MP4_READBOX_ENTER( MP4_Box_data_urn_t
, MP4_FreeBox_urn
);
1501 MP4_GETVERSIONFLAGS( p_box
->data
.p_urn
);
1503 MP4_GETSTRINGZ( p_box
->data
.p_urn
->psz_name
);
1504 MP4_GETSTRINGZ( p_box
->data
.p_urn
->psz_location
);
1507 msg_Dbg( p_stream
, "read box: \"urn\" name %s location %s",
1508 p_box
->data
.p_urn
->psz_name
,
1509 p_box
->data
.p_urn
->psz_location
);
1511 MP4_READBOX_EXIT( 1 );
1514 static int MP4_ReadBox_LtdContainer( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1516 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_lcont_t
, 16, NULL
);
1518 MP4_READBOX_EXIT( 0 );
1520 MP4_GETVERSIONFLAGS( p_box
->data
.p_lcont
);
1521 if( p_box
->data
.p_lcont
->i_version
!= 0 )
1522 MP4_READBOX_EXIT( 0 );
1523 MP4_GET4BYTES( p_box
->data
.p_lcont
->i_entry_count
);
1525 uint32_t i_entry
= 0;
1526 i_read
= p_box
->i_size
- 16;
1527 while (i_read
> 8 && i_entry
< p_box
->data
.p_lcont
->i_entry_count
)
1529 MP4_Box_t
*p_childbox
= MP4_ReadBox( p_stream
, p_box
);
1532 MP4_BoxAddChild( p_box
, p_childbox
);
1535 if( i_read
< p_childbox
->i_size
)
1536 MP4_READBOX_EXIT( 0 );
1538 i_read
-= p_childbox
->i_size
;
1541 if (i_entry
!= p_box
->data
.p_lcont
->i_entry_count
)
1542 p_box
->data
.p_lcont
->i_entry_count
= i_entry
;
1545 msg_Dbg( p_stream
, "read box: \"%4.4s\" entry-count %d", (char *)&p_box
->i_type
,
1546 p_box
->data
.p_lcont
->i_entry_count
);
1550 if ( MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) )
1551 MP4_READBOX_EXIT( 0 );
1553 MP4_READBOX_EXIT( 1 );
1556 static void MP4_FreeBox_stts( MP4_Box_t
*p_box
)
1558 free( p_box
->data
.p_stts
->pi_sample_count
);
1559 free( p_box
->data
.p_stts
->pi_sample_delta
);
1562 static int MP4_ReadBox_stts( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1566 MP4_READBOX_ENTER( MP4_Box_data_stts_t
, MP4_FreeBox_stts
);
1568 MP4_GETVERSIONFLAGS( p_box
->data
.p_stts
);
1569 MP4_GET4BYTES( count
);
1571 if( UINT64_C(8) * count
> i_read
)
1573 /*count = i_read / 8;*/
1574 MP4_READBOX_EXIT( 0 );
1577 p_box
->data
.p_stts
->pi_sample_count
= vlc_alloc( count
, sizeof(uint32_t) );
1578 p_box
->data
.p_stts
->pi_sample_delta
= vlc_alloc( count
, sizeof(int32_t) );
1579 p_box
->data
.p_stts
->i_entry_count
= count
;
1581 if( p_box
->data
.p_stts
->pi_sample_count
== NULL
1582 || p_box
->data
.p_stts
->pi_sample_delta
== NULL
)
1584 MP4_READBOX_EXIT( 0 );
1587 for( uint32_t i
= 0; i
< count
; i
++ )
1589 MP4_GET4BYTES( p_box
->data
.p_stts
->pi_sample_count
[i
] );
1590 MP4_GET4BYTES( p_box
->data
.p_stts
->pi_sample_delta
[i
] );
1594 msg_Dbg( p_stream
, "read box: \"stts\" entry-count %d",
1595 p_box
->data
.p_stts
->i_entry_count
);
1598 MP4_READBOX_EXIT( 1 );
1602 static void MP4_FreeBox_ctts( MP4_Box_t
*p_box
)
1604 free( p_box
->data
.p_ctts
->pi_sample_count
);
1605 free( p_box
->data
.p_ctts
->pi_sample_offset
);
1608 static int MP4_ReadBox_ctts( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1612 MP4_READBOX_ENTER( MP4_Box_data_ctts_t
, MP4_FreeBox_ctts
);
1614 MP4_GETVERSIONFLAGS( p_box
->data
.p_ctts
);
1615 MP4_GET4BYTES( count
);
1617 if( UINT64_C(8) * count
> i_read
)
1618 MP4_READBOX_EXIT( 0 );
1620 p_box
->data
.p_ctts
->pi_sample_count
= vlc_alloc( count
, sizeof(uint32_t) );
1621 p_box
->data
.p_ctts
->pi_sample_offset
= vlc_alloc( count
, sizeof(int32_t) );
1622 if( unlikely(p_box
->data
.p_ctts
->pi_sample_count
== NULL
1623 || p_box
->data
.p_ctts
->pi_sample_offset
== NULL
) )
1624 MP4_READBOX_EXIT( 0 );
1625 p_box
->data
.p_ctts
->i_entry_count
= count
;
1627 for( uint32_t i
= 0; i
< count
; i
++ )
1629 MP4_GET4BYTES( p_box
->data
.p_ctts
->pi_sample_count
[i
] );
1630 MP4_GET4BYTES( p_box
->data
.p_ctts
->pi_sample_offset
[i
] );
1634 msg_Dbg( p_stream
, "read box: \"ctts\" entry-count %"PRIu32
, count
);
1637 MP4_READBOX_EXIT( 1 );
1640 static int MP4_ReadBox_cslg( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1642 MP4_READBOX_ENTER( MP4_Box_data_cslg_t
, NULL
);
1644 unsigned i_version
, i_flags
;
1645 MP4_GET1BYTE( i_version
);
1646 MP4_GET3BYTES( i_flags
);
1647 VLC_UNUSED(i_flags
);
1650 MP4_READBOX_EXIT( 0 );
1652 union { int32_t s
; uint32_t u
; } u32
;
1653 union { int64_t s
; uint64_t u
; } u64
;
1654 #define DOREAD_CSLG( readbytes, temp, member ) \
1655 { readbytes( temp.u ); member = temp.s; }
1657 #define READ_CSLG( readbytes, temp ) {\
1658 DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->ct_to_dts_shift );\
1659 DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->i_least_delta );\
1660 DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->i_max_delta );\
1661 DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->i_composition_starttime );\
1662 DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->i_composition_endtime ); }
1664 if( i_version
== 0 )
1665 READ_CSLG( MP4_GET4BYTES
, u32
)
1667 READ_CSLG( MP4_GET8BYTES
, u64
)
1669 MP4_READBOX_EXIT( 1 );
1672 static uint64_t MP4_ReadLengthDescriptor( uint8_t **restrict bufp
,
1673 uint64_t *restrict lenp
)
1675 unsigned char *buf
= *bufp
;
1676 uint64_t len
= *lenp
;
1682 if (unlikely(len
== 0))
1683 return -1; /* end of bit stream */
1684 if (unlikely(value
> (UINT64_MAX
>> 7)))
1685 return -1; /* integer overflow */
1689 value
= (value
<< 7) + (b
& 0x7f);
1699 static void MP4_FreeBox_esds( MP4_Box_t
*p_box
)
1701 free( p_box
->data
.p_esds
->es_descriptor
.psz_URL
);
1702 if( p_box
->data
.p_esds
->es_descriptor
.p_decConfigDescr
)
1704 free( p_box
->data
.p_esds
->es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
);
1705 free( p_box
->data
.p_esds
->es_descriptor
.p_decConfigDescr
);
1709 static int MP4_ReadBox_esds( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1711 #define es_descriptor p_box->data.p_esds->es_descriptor
1713 unsigned int i_flags
;
1714 unsigned int i_type
;
1716 MP4_READBOX_ENTER( MP4_Box_data_esds_t
, MP4_FreeBox_esds
);
1718 MP4_GETVERSIONFLAGS( p_box
->data
.p_esds
);
1721 MP4_GET1BYTE( i_type
);
1722 if( i_type
== 0x03 ) /* MP4ESDescrTag ISO/IEC 14496-1 */
1724 i_len
= MP4_ReadLengthDescriptor( &p_peek
, &i_read
);
1725 if( unlikely(i_len
== UINT64_C(-1)) )
1726 MP4_READBOX_EXIT( 0 );
1729 msg_Dbg( p_stream
, "found esds MPEG4ESDescr (%"PRIu64
" bytes)",
1733 MP4_GET2BYTES( es_descriptor
.i_ES_ID
);
1734 MP4_GET1BYTE( i_flags
);
1735 es_descriptor
.b_stream_dependence
= ( (i_flags
&0x80) != 0);
1736 es_descriptor
.b_url
= ( (i_flags
&0x40) != 0);
1737 es_descriptor
.b_OCRstream
= ( (i_flags
&0x20) != 0);
1739 es_descriptor
.i_stream_priority
= i_flags
&0x1f;
1740 if( es_descriptor
.b_stream_dependence
)
1742 MP4_GET2BYTES( es_descriptor
.i_depend_on_ES_ID
);
1744 if( es_descriptor
.b_url
&& i_read
> 0 )
1748 MP4_GET1BYTE( i_url
);
1749 if( i_url
> i_read
)
1750 MP4_READBOX_EXIT( 1 );
1751 es_descriptor
.psz_URL
= malloc( (unsigned) i_url
+ 1 );
1752 if( es_descriptor
.psz_URL
)
1754 memcpy( es_descriptor
.psz_URL
, p_peek
, i_url
);
1755 es_descriptor
.psz_URL
[i_url
] = 0;
1762 es_descriptor
.psz_URL
= NULL
;
1764 if( es_descriptor
.b_OCRstream
)
1766 MP4_GET2BYTES( es_descriptor
.i_OCR_ES_ID
);
1768 MP4_GET1BYTE( i_type
); /* get next type */
1771 if( i_type
!= 0x04)/* MP4DecConfigDescrTag ISO/IEC 14496-1 8.3.4 */
1773 es_descriptor
.p_decConfigDescr
= NULL
;
1774 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1777 i_len
= MP4_ReadLengthDescriptor( &p_peek
, &i_read
);
1778 if( unlikely(i_len
== UINT64_C(-1)) )
1779 MP4_READBOX_EXIT( 0 );
1781 msg_Dbg( p_stream
, "found esds MP4DecConfigDescr (%"PRIu64
" bytes)",
1785 es_descriptor
.p_decConfigDescr
=
1786 calloc( 1, sizeof( MP4_descriptor_decoder_config_t
));
1787 if( unlikely( es_descriptor
.p_decConfigDescr
== NULL
) )
1788 MP4_READBOX_EXIT( 0 );
1790 MP4_GET1BYTE( es_descriptor
.p_decConfigDescr
->i_objectProfileIndication
);
1791 MP4_GET1BYTE( i_flags
);
1792 es_descriptor
.p_decConfigDescr
->i_streamType
= i_flags
>> 2;
1793 es_descriptor
.p_decConfigDescr
->b_upStream
= ( i_flags
>> 1 )&0x01;
1794 MP4_GET3BYTES( es_descriptor
.p_decConfigDescr
->i_buffer_sizeDB
);
1795 MP4_GET4BYTES( es_descriptor
.p_decConfigDescr
->i_max_bitrate
);
1796 MP4_GET4BYTES( es_descriptor
.p_decConfigDescr
->i_avg_bitrate
);
1797 MP4_GET1BYTE( i_type
);
1798 if( i_type
!= 0x05 )/* MP4DecSpecificDescrTag ISO/IEC 14496-1 8.3.5 */
1800 es_descriptor
.p_decConfigDescr
->i_decoder_specific_info_len
= 0;
1801 es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
= NULL
;
1802 MP4_READBOX_EXIT( 1 );
1805 i_len
= MP4_ReadLengthDescriptor( &p_peek
, &i_read
);
1806 if( unlikely(i_len
== UINT64_C(-1)) )
1807 MP4_READBOX_EXIT( 0 );
1809 msg_Dbg( p_stream
, "found esds MP4DecSpecificDescr (%"PRIu64
" bytes)",
1812 if( i_len
> i_read
)
1813 MP4_READBOX_EXIT( 0 );
1815 es_descriptor
.p_decConfigDescr
->i_decoder_specific_info_len
= i_len
;
1816 es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
= malloc( i_len
);
1817 if( unlikely( es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
== NULL
) )
1818 MP4_READBOX_EXIT( 0 );
1820 memcpy( es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
,
1823 MP4_READBOX_EXIT( 1 );
1824 #undef es_descriptor
1827 static void MP4_FreeBox_av1C( MP4_Box_t
*p_box
)
1829 MP4_Box_data_av1C_t
*p_av1C
= p_box
->data
.p_av1C
;
1830 free( p_av1C
->p_av1C
);
1833 static int MP4_ReadBox_av1C( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1835 MP4_Box_data_av1C_t
*p_av1C
;
1837 MP4_READBOX_ENTER( MP4_Box_data_av1C_t
, MP4_FreeBox_av1C
);
1838 p_av1C
= p_box
->data
.p_av1C
;
1841 p_peek
[0] != 0x81 ) /* marker / version */
1842 MP4_READBOX_EXIT( 0 );
1844 p_av1C
->p_av1C
= malloc( i_read
);
1845 if( p_av1C
->p_av1C
)
1847 memcpy( p_av1C
->p_av1C
, p_peek
, i_read
);
1848 p_av1C
->i_av1C
= i_read
;
1852 MP4_GET1BYTE( i_8b
); /* marker / version */
1854 MP4_GET1BYTE( i_8b
);
1855 p_av1C
->i_profile
= i_8b
>> 5;
1856 p_av1C
->i_level
= i_8b
& 0x1F;
1858 MP4_GET1BYTE( i_8b
);
1859 MP4_GET1BYTE( i_8b
);
1861 if( i_8b
& 0x10 ) /* delay flag */
1862 p_av1C
->i_presentation_delay
= 1 + (i_8b
& 0x0F);
1864 p_av1C
->i_presentation_delay
= 0;
1866 MP4_READBOX_EXIT( 1 );
1869 static void MP4_FreeBox_avcC( MP4_Box_t
*p_box
)
1871 MP4_Box_data_avcC_t
*p_avcC
= p_box
->data
.p_avcC
;
1872 free( p_avcC
->p_avcC
);
1875 static int MP4_ReadBox_avcC( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1877 MP4_Box_data_avcC_t
*p_avcC
;
1879 MP4_READBOX_ENTER( MP4_Box_data_avcC_t
, MP4_FreeBox_avcC
);
1880 p_avcC
= p_box
->data
.p_avcC
;
1884 p_avcC
->p_avcC
= malloc( i_read
);
1885 if( p_avcC
->p_avcC
)
1887 memcpy( p_avcC
->p_avcC
, p_peek
, i_read
);
1888 p_avcC
->i_avcC
= i_read
;
1892 MP4_GET1BYTE( p_avcC
->i_version
);
1893 MP4_GET1BYTE( p_avcC
->i_profile
);
1894 MP4_GET1BYTE( p_avcC
->i_profile_compatibility
);
1895 MP4_GET1BYTE( p_avcC
->i_level
);
1898 "read box: \"avcC\" version=%d profile=0x%x level=0x%x",
1899 p_avcC
->i_version
, p_avcC
->i_profile
, p_avcC
->i_level
);
1901 MP4_READBOX_EXIT( 1 );
1904 static void MP4_FreeBox_vpcC( MP4_Box_t
*p_box
)
1906 free( p_box
->data
.p_vpcC
->p_codec_init_data
);
1909 static int MP4_ReadBox_vpcC( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1911 MP4_READBOX_ENTER( MP4_Box_data_vpcC_t
, MP4_FreeBox_vpcC
);
1912 MP4_Box_data_vpcC_t
*p_vpcC
= p_box
->data
.p_vpcC
;
1914 if( p_box
->i_size
< 6 )
1915 MP4_READBOX_EXIT( 0 );
1917 MP4_GET1BYTE( p_vpcC
->i_version
);
1918 if( p_vpcC
->i_version
> 1 )
1919 MP4_READBOX_EXIT( 0 );
1921 MP4_GET1BYTE( p_vpcC
->i_profile
);
1922 MP4_GET1BYTE( p_vpcC
->i_level
);
1923 MP4_GET1BYTE( p_vpcC
->i_bit_depth
);
1926 https://github.com/webmproject/vp9-dash/blob/master/archive/VPCodecISOMediaFileFormatBinding-v0.docx */
1927 if( p_vpcC
->i_version
== 0 )
1929 p_vpcC
->i_color_primaries
= p_vpcC
->i_bit_depth
& 0x0F;
1930 p_vpcC
->i_bit_depth
>>= 4;
1931 MP4_GET1BYTE( p_vpcC
->i_chroma_subsampling
);
1932 p_vpcC
->i_xfer_function
= ( p_vpcC
->i_chroma_subsampling
& 0x0F ) >> 1;
1933 p_vpcC
->i_fullrange
= p_vpcC
->i_chroma_subsampling
& 0x01;
1934 p_vpcC
->i_chroma_subsampling
>>= 4;
1938 p_vpcC
->i_chroma_subsampling
= ( p_vpcC
->i_bit_depth
& 0x0F ) >> 1;
1939 p_vpcC
->i_fullrange
= p_vpcC
->i_bit_depth
& 0x01;
1940 p_vpcC
->i_bit_depth
>>= 4;
1941 MP4_GET1BYTE( p_vpcC
->i_color_primaries
);
1942 MP4_GET1BYTE( p_vpcC
->i_xfer_function
);
1943 MP4_GET1BYTE( p_vpcC
->i_matrix_coeffs
);
1946 MP4_GET2BYTES( p_vpcC
->i_codec_init_datasize
);
1947 if( p_vpcC
->i_codec_init_datasize
> i_read
)
1948 p_vpcC
->i_codec_init_datasize
= i_read
;
1950 if( p_vpcC
->i_codec_init_datasize
)
1952 p_vpcC
->p_codec_init_data
= malloc( i_read
);
1953 if( !p_vpcC
->p_codec_init_data
)
1954 MP4_READBOX_EXIT( 0 );
1955 memcpy( p_vpcC
->p_codec_init_data
, p_peek
, i_read
);
1958 MP4_READBOX_EXIT( 1 );
1961 static int MP4_ReadBox_SmDm( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1963 MP4_READBOX_ENTER( MP4_Box_data_SmDm_t
, NULL
);
1964 MP4_Box_data_SmDm_t
*p_SmDm
= p_box
->data
.p_SmDm
;
1966 /* SmDm: version/flags RGB */
1967 /* mdcv: version/flags GBR or not */
1968 if( p_box
->i_type
!= ATOM_mdcv
)
1972 MP4_GET1BYTE( i_version
);
1973 MP4_GET3BYTES( i_flags
);
1974 VLC_UNUSED(i_flags
);
1975 if( i_version
!= 0 )
1976 MP4_READBOX_EXIT( 0 );
1979 const uint8_t RGB2GBR
[3] = {2,0,1};
1980 for(int i
=0; i
<6; i
++)
1982 int index
= (p_box
->i_type
!= ATOM_mdcv
) ? RGB2GBR
[i
/2] + i
%2 : i
;
1983 MP4_GET2BYTES( p_SmDm
->primaries
[index
] );
1985 /* convert from fixed point to 0.00002 resolution */
1986 if(p_box
->i_type
!= ATOM_mdcv
)
1987 p_SmDm
->primaries
[index
] = 50000 *
1988 (double)p_SmDm
->primaries
[index
] / (double)(1<<16);
1990 for(int i
=0; i
<2; i
++)
1992 MP4_GET2BYTES( p_SmDm
->white_point
[i
] );
1993 if(p_box
->i_type
!= ATOM_mdcv
)
1994 p_SmDm
->white_point
[i
] = 50000 *
1995 (double)p_SmDm
->white_point
[i
] / (double)(1<<16);
1998 MP4_GET4BYTES( p_SmDm
->i_luminanceMax
);
1999 MP4_GET4BYTES( p_SmDm
->i_luminanceMin
);
2000 if(p_box
->i_type
!= ATOM_mdcv
)
2002 p_SmDm
->i_luminanceMax
= 10000 *
2003 (double)p_SmDm
->i_luminanceMax
/ (double) (1<<8);
2004 p_SmDm
->i_luminanceMin
= 10000 *
2005 (double)p_SmDm
->i_luminanceMin
/ (double) (1<<14);
2008 MP4_READBOX_EXIT( 1 );
2011 static int MP4_ReadBox_CoLL( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2013 MP4_READBOX_ENTER( MP4_Box_data_CoLL_t
, NULL
);
2014 MP4_Box_data_CoLL_t
*p_CoLL
= p_box
->data
.p_CoLL
;
2016 if( p_box
->i_type
!= ATOM_clli
)
2020 MP4_GET1BYTE( i_version
);
2021 MP4_GET3BYTES( i_flags
);
2022 VLC_UNUSED(i_flags
);
2023 if( i_version
!= 0 )
2024 MP4_READBOX_EXIT( 0 );
2027 MP4_GET2BYTES( p_CoLL
->i_maxCLL
);
2028 MP4_GET2BYTES( p_CoLL
->i_maxFALL
);
2029 MP4_READBOX_EXIT( 1 );
2032 static void MP4_FreeBox_WMA2( MP4_Box_t
*p_box
)
2034 free( p_box
->data
.p_WMA2
->p_extra
);
2037 static int MP4_ReadBox_WMA2( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2039 MP4_READBOX_ENTER( MP4_Box_data_WMA2_t
, MP4_FreeBox_WMA2
);
2041 MP4_Box_data_WMA2_t
*p_WMA2
= p_box
->data
.p_WMA2
;
2043 MP4_GET2BYTESLE( p_WMA2
->Format
.wFormatTag
);
2044 MP4_GET2BYTESLE( p_WMA2
->Format
.nChannels
);
2045 MP4_GET4BYTESLE( p_WMA2
->Format
.nSamplesPerSec
);
2046 MP4_GET4BYTESLE( p_WMA2
->Format
.nAvgBytesPerSec
);
2047 MP4_GET2BYTESLE( p_WMA2
->Format
.nBlockAlign
);
2048 MP4_GET2BYTESLE( p_WMA2
->Format
.wBitsPerSample
);
2051 MP4_GET2BYTESLE( i_cbSize
);
2053 if( i_cbSize
> i_read
)
2056 p_WMA2
->i_extra
= i_cbSize
;
2057 if ( p_WMA2
->i_extra
)
2059 p_WMA2
->p_extra
= malloc( p_WMA2
->i_extra
);
2060 if ( ! p_WMA2
->p_extra
)
2062 memcpy( p_WMA2
->p_extra
, p_peek
, p_WMA2
->i_extra
);
2065 MP4_READBOX_EXIT( 1 );
2068 MP4_READBOX_EXIT( 0 );
2071 static void MP4_FreeBox_strf( MP4_Box_t
*p_box
)
2073 free( p_box
->data
.p_strf
->p_extra
);
2076 static int MP4_ReadBox_strf( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2078 MP4_READBOX_ENTER( MP4_Box_data_strf_t
, MP4_FreeBox_strf
);
2080 MP4_Box_data_strf_t
*p_strf
= p_box
->data
.p_strf
;
2085 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biSize
);
2086 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biWidth
);
2087 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biHeight
);
2088 MP4_GET2BYTESLE( p_strf
->bmiHeader
.biPlanes
);
2089 MP4_GET2BYTESLE( p_strf
->bmiHeader
.biBitCount
);
2090 MP4_GETFOURCC( p_strf
->bmiHeader
.biCompression
);
2091 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biSizeImage
);
2092 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biXPelsPerMeter
);
2093 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biYPelsPerMeter
);
2094 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biClrUsed
);
2095 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biClrImportant
);
2097 p_strf
->i_extra
= i_read
;
2098 if ( p_strf
->i_extra
)
2100 p_strf
->p_extra
= malloc( p_strf
->i_extra
);
2101 if ( ! p_strf
->p_extra
)
2103 memcpy( p_strf
->p_extra
, p_peek
, i_read
);
2106 MP4_READBOX_EXIT( 1 );
2109 MP4_READBOX_EXIT( 0 );
2112 static int MP4_ReadBox_ASF( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2114 MP4_READBOX_ENTER( MP4_Box_data_ASF_t
, NULL
);
2116 MP4_Box_data_ASF_t
*p_asf
= p_box
->data
.p_asf
;
2119 MP4_READBOX_EXIT( 0 );
2121 MP4_GET1BYTE( p_asf
->i_stream_number
);
2122 /* remaining is unknown */
2124 MP4_READBOX_EXIT( 1 );
2127 static void MP4_FreeBox_sbgp( MP4_Box_t
*p_box
)
2129 MP4_Box_data_sbgp_t
*p_sbgp
= p_box
->data
.p_sbgp
;
2130 free( p_sbgp
->entries
.pi_sample_count
);
2131 free( p_sbgp
->entries
.pi_group_description_index
);
2134 static int MP4_ReadBox_sbgp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2136 MP4_READBOX_ENTER( MP4_Box_data_sbgp_t
, MP4_FreeBox_sbgp
);
2137 MP4_Box_data_sbgp_t
*p_sbgp
= p_box
->data
.p_sbgp
;
2141 MP4_READBOX_EXIT( 0 );
2143 MP4_GET1BYTE( p_sbgp
->i_version
);
2144 MP4_GET3BYTES( i_flags
);
2146 MP4_READBOX_EXIT( 0 );
2148 MP4_GETFOURCC( p_sbgp
->i_grouping_type
);
2150 if( p_sbgp
->i_version
== 1 )
2153 MP4_READBOX_EXIT( 0 );
2154 MP4_GET4BYTES( p_sbgp
->i_grouping_type_parameter
);
2157 MP4_GET4BYTES( p_sbgp
->i_entry_count
);
2158 if( p_sbgp
->i_entry_count
> i_read
/ (4 + 4) )
2159 p_sbgp
->i_entry_count
= i_read
/ (4 + 4);
2161 p_sbgp
->entries
.pi_sample_count
= vlc_alloc( p_sbgp
->i_entry_count
, sizeof(uint32_t) );
2162 p_sbgp
->entries
.pi_group_description_index
= vlc_alloc( p_sbgp
->i_entry_count
, sizeof(uint32_t) );
2164 if( !p_sbgp
->entries
.pi_sample_count
|| !p_sbgp
->entries
.pi_group_description_index
)
2166 MP4_FreeBox_sbgp( p_box
);
2167 MP4_READBOX_EXIT( 0 );
2170 for( uint32_t i
=0; i
<p_sbgp
->i_entry_count
; i
++ )
2172 MP4_GET4BYTES( p_sbgp
->entries
.pi_sample_count
[i
] );
2173 MP4_GET4BYTES( p_sbgp
->entries
.pi_group_description_index
[i
] );
2178 "read box: \"sbgp\" grouping type %4.4s", (char*) &p_sbgp
->i_grouping_type
);
2179 #ifdef MP4_ULTRA_VERBOSE
2180 for (uint32_t i
= 0; i
< p_sbgp
->i_entry_count
; i
++)
2181 msg_Dbg( p_stream
, "\t samples %" PRIu32
" group %" PRIu32
,
2182 p_sbgp
->entries
.pi_sample_count
[i
],
2183 p_sbgp
->entries
.pi_group_description_index
[i
] );
2187 MP4_READBOX_EXIT( 1 );
2190 static void MP4_FreeBox_sgpd( MP4_Box_t
*p_box
)
2192 MP4_Box_data_sgpd_t
*p_sgpd
= p_box
->data
.p_sgpd
;
2193 free( p_sgpd
->p_entries
);
2196 static int MP4_ReadBox_sgpd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2198 MP4_READBOX_ENTER( MP4_Box_data_sgpd_t
, MP4_FreeBox_sgpd
);
2199 MP4_Box_data_sgpd_t
*p_sgpd
= p_box
->data
.p_sgpd
;
2201 uint32_t i_default_length
= 0;
2204 MP4_READBOX_EXIT( 0 );
2206 MP4_GET1BYTE( p_sgpd
->i_version
);
2207 MP4_GET3BYTES( i_flags
);
2209 MP4_READBOX_EXIT( 0 );
2211 MP4_GETFOURCC( p_sgpd
->i_grouping_type
);
2213 switch( p_sgpd
->i_grouping_type
)
2215 case SAMPLEGROUP_rap
:
2221 "read box: \"sgpd\" grouping type %4.4s (unimplemented)", (char*) &p_sgpd
->i_grouping_type
);
2223 MP4_READBOX_EXIT( 1 );
2226 if( p_sgpd
->i_version
== 1 )
2229 MP4_READBOX_EXIT( 0 );
2230 MP4_GET4BYTES( i_default_length
);
2232 else if( p_sgpd
->i_version
>= 2 )
2235 MP4_READBOX_EXIT( 0 );
2236 MP4_GET4BYTES( p_sgpd
->i_default_sample_description_index
);
2239 MP4_GET4BYTES( p_sgpd
->i_entry_count
);
2241 p_sgpd
->p_entries
= vlc_alloc( p_sgpd
->i_entry_count
, sizeof(*p_sgpd
->p_entries
) );
2242 if( !p_sgpd
->p_entries
)
2243 MP4_READBOX_EXIT( 0 );
2246 for( ; i
<p_sgpd
->i_entry_count
; i
++ )
2248 uint32_t i_description_length
= i_default_length
;
2249 if( p_sgpd
->i_version
== 1 && i_default_length
== 0 )
2253 MP4_GET4BYTES( i_description_length
);
2256 if( p_sgpd
->i_version
== 1 && i_read
< i_description_length
)
2259 switch( p_sgpd
->i_grouping_type
)
2261 case SAMPLEGROUP_rap
:
2265 free( p_sgpd
->p_entries
);
2266 p_sgpd
->i_entry_count
= 0;
2267 p_sgpd
->p_entries
= NULL
;
2268 MP4_READBOX_EXIT( 0 );
2271 MP4_GET1BYTE( i_data
);
2272 p_sgpd
->p_entries
[i
].rap
.i_num_leading_samples_known
= i_data
& 0x80;
2273 p_sgpd
->p_entries
[i
].rap
.i_num_leading_samples
= i_data
& 0x7F;
2282 if( i
!= p_sgpd
->i_entry_count
)
2283 p_sgpd
->i_entry_count
= i
;
2287 "read box: \"sgpd\" grouping type %4.4s", (char*) &p_sgpd
->i_grouping_type
);
2290 MP4_READBOX_EXIT( 1 );
2293 static void MP4_FreeBox_stsdext_chan( MP4_Box_t
*p_box
)
2295 MP4_Box_data_chan_t
*p_chan
= p_box
->data
.p_chan
;
2296 CoreAudio_Layout_Clean( &p_chan
->layout
);
2299 static int MP4_ReadBox_stsdext_chan( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2301 MP4_READBOX_ENTER( MP4_Box_data_chan_t
, MP4_FreeBox_stsdext_chan
);
2302 MP4_Box_data_chan_t
*p_chan
= p_box
->data
.p_chan
;
2305 MP4_READBOX_EXIT( 0 );
2307 MP4_GET1BYTE( p_chan
->i_version
);
2308 MP4_GET3BYTES( p_chan
->i_channels_flags
);
2309 MP4_GET4BYTES( p_chan
->layout
.i_channels_layout_tag
);
2310 MP4_GET4BYTES( p_chan
->layout
.i_channels_bitmap
);
2311 MP4_GET4BYTES( p_chan
->layout
.i_channels_description_count
);
2313 size_t i_descsize
= 8 + 3 * sizeof(float);
2314 if ( i_read
< p_chan
->layout
.i_channels_description_count
* i_descsize
)
2315 MP4_READBOX_EXIT( 0 );
2317 p_chan
->layout
.p_descriptions
=
2318 vlc_alloc( p_chan
->layout
.i_channels_description_count
, i_descsize
);
2320 if ( !p_chan
->layout
.p_descriptions
)
2321 MP4_READBOX_EXIT( 0 );
2324 for( i
=0; i
<p_chan
->layout
.i_channels_description_count
; i
++ )
2328 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].i_channel_label
);
2329 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].i_channel_flags
);
2330 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].f_coordinates
[0] );
2331 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].f_coordinates
[1] );
2332 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].f_coordinates
[2] );
2334 if ( i
<p_chan
->layout
.i_channels_description_count
)
2335 p_chan
->layout
.i_channels_description_count
= i
;
2339 "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
2340 p_chan
->i_channels_flags
, p_chan
->layout
.i_channels_layout_tag
,
2341 p_chan
->layout
.i_channels_bitmap
, p_chan
->layout
.i_channels_description_count
);
2343 MP4_READBOX_EXIT( 1 );
2346 static int MP4_ReadBox_stsdext_srat( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2348 MP4_READBOX_ENTER( MP4_Box_data_srat_t
, NULL
);
2349 MP4_Box_data_srat_t
*p_srat
= p_box
->data
.p_srat
;
2351 MP4_READBOX_EXIT( 0 );
2354 VLC_UNUSED( i_dummy
);
2355 MP4_GET4BYTES( i_dummy
); /* version flags */
2356 MP4_GET4BYTES( p_srat
->i_sample_rate
);
2358 MP4_READBOX_EXIT( 1 );
2361 static int MP4_ReadBox_dec3( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2363 MP4_READBOX_ENTER( MP4_Box_data_dec3_t
, NULL
);
2365 MP4_Box_data_dec3_t
*p_dec3
= p_box
->data
.p_dec3
;
2368 MP4_GET2BYTES( i_header
);
2370 p_dec3
->i_data_rate
= i_header
>> 3;
2371 p_dec3
->i_num_ind_sub
= (i_header
& 0x7) + 1;
2372 for (uint8_t i
= 0; i
< p_dec3
->i_num_ind_sub
; i
++) {
2373 MP4_GET3BYTES( i_header
);
2374 p_dec3
->stream
[i
].i_fscod
= ( i_header
>> 22 ) & 0x03;
2375 p_dec3
->stream
[i
].i_bsid
= ( i_header
>> 17 ) & 0x01f;
2376 p_dec3
->stream
[i
].i_bsmod
= ( i_header
>> 12 ) & 0x01f;
2377 p_dec3
->stream
[i
].i_acmod
= ( i_header
>> 9 ) & 0x07;
2378 p_dec3
->stream
[i
].i_lfeon
= ( i_header
>> 8 ) & 0x01;
2379 p_dec3
->stream
[i
].i_num_dep_sub
= (i_header
>> 1) & 0x0f;
2380 if (p_dec3
->stream
[i
].i_num_dep_sub
) {
2381 MP4_GET1BYTE( p_dec3
->stream
[i
].i_chan_loc
);
2382 p_dec3
->stream
[i
].i_chan_loc
|= (i_header
& 1) << 8;
2384 p_dec3
->stream
[i
].i_chan_loc
= 0;
2389 "read box: \"dec3\" bitrate %dkbps %d independent substreams",
2390 p_dec3
->i_data_rate
, p_dec3
->i_num_ind_sub
);
2392 for (uint8_t i
= 0; i
< p_dec3
->i_num_ind_sub
; i
++)
2394 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
2395 "num dependent subs=%d chan_loc=0x%x",
2396 i
, p_dec3
->stream
[i
].i_bsid
, p_dec3
->stream
[i
].i_bsmod
, p_dec3
->stream
[i
].i_acmod
,
2397 p_dec3
->stream
[i
].i_lfeon
, p_dec3
->stream
[i
].i_num_dep_sub
, p_dec3
->stream
[i
].i_chan_loc
);
2399 MP4_READBOX_EXIT( 1 );
2402 static int MP4_ReadBox_dac3( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2404 MP4_Box_data_dac3_t
*p_dac3
;
2405 MP4_READBOX_ENTER( MP4_Box_data_dac3_t
, NULL
);
2407 p_dac3
= p_box
->data
.p_dac3
;
2410 MP4_GET3BYTES( i_header
);
2412 p_dac3
->i_fscod
= ( i_header
>> 22 ) & 0x03;
2413 p_dac3
->i_bsid
= ( i_header
>> 17 ) & 0x01f;
2414 p_dac3
->i_bsmod
= ( i_header
>> 14 ) & 0x07;
2415 p_dac3
->i_acmod
= ( i_header
>> 11 ) & 0x07;
2416 p_dac3
->i_lfeon
= ( i_header
>> 10 ) & 0x01;
2417 p_dac3
->i_bitrate_code
= ( i_header
>> 5) & 0x1f;
2421 "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
2422 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
);
2424 MP4_READBOX_EXIT( 1 );
2427 static void MP4_FreeBox_dvc1( MP4_Box_t
*p_box
)
2429 free( p_box
->data
.p_dvc1
->p_vc1
);
2432 static int MP4_ReadBox_dvc1( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2434 MP4_READBOX_ENTER( MP4_Box_data_dvc1_t
, MP4_FreeBox_dvc1
);
2436 MP4_READBOX_EXIT( 0 );
2438 MP4_Box_data_dvc1_t
*p_dvc1
= p_box
->data
.p_dvc1
;
2439 MP4_GET1BYTE( p_dvc1
->i_profile_level
);
2440 p_dvc1
->i_vc1
= i_read
; /* Header + profile_level */
2441 if( p_dvc1
->i_vc1
> 0 && (p_dvc1
->p_vc1
= malloc( p_dvc1
->i_vc1
)) )
2442 memcpy( p_dvc1
->p_vc1
, p_peek
, i_read
);
2445 uint8_t i_profile
= (p_dvc1
->i_profile_level
& 0xf0) >> 4;
2446 msg_Dbg( p_stream
, "read box: \"dvc1\" profile=%"PRIu8
, i_profile
);
2449 MP4_READBOX_EXIT( 1 );
2452 static int MP4_ReadBox_fiel( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2454 MP4_Box_data_fiel_t
*p_fiel
;
2455 MP4_READBOX_ENTER( MP4_Box_data_fiel_t
, NULL
);
2456 p_fiel
= p_box
->data
.p_fiel
;
2458 MP4_READBOX_EXIT( 0 );
2459 if(p_peek
[0] == 2) /* Interlaced */
2462 * 0 – There is only one field.
2463 * 1 – T is displayed earliest, T is stored first in the file.
2464 * 6 – B is displayed earliest, B is stored first in the file.
2465 * 9 – B is displayed earliest, T is stored first in the file.
2466 * 14 – T is displayed earliest, B is stored first in the file.
2469 p_fiel
->i_flags
= BLOCK_FLAG_SINGLE_FIELD
;
2470 else if(p_peek
[1] == 1 || p_peek
[1] == 9)
2471 p_fiel
->i_flags
= BLOCK_FLAG_TOP_FIELD_FIRST
;
2472 else if(p_peek
[1] == 6 || p_peek
[1] == 14)
2473 p_fiel
->i_flags
= BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
2475 MP4_READBOX_EXIT( 1 );
2478 static int MP4_ReadBox_enda( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2480 MP4_Box_data_enda_t
*p_enda
;
2481 MP4_READBOX_ENTER( MP4_Box_data_enda_t
, NULL
);
2483 p_enda
= p_box
->data
.p_enda
;
2485 MP4_GET2BYTES( p_enda
->i_little_endian
);
2489 "read box: \"enda\" little_endian=%d", p_enda
->i_little_endian
);
2491 MP4_READBOX_EXIT( 1 );
2494 static void MP4_FreeBox_sample_soun( MP4_Box_t
*p_box
)
2496 free( p_box
->data
.p_sample_soun
->p_qt_description
);
2499 static int MP4_ReadBox_sample_soun( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2501 p_box
->i_handler
= ATOM_soun
;
2502 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t
, MP4_FreeBox_sample_soun
);
2503 p_box
->data
.p_sample_soun
->p_qt_description
= NULL
;
2505 size_t i_actually_read
= i_read
+ header_size
;
2507 /* Sanity check needed because the "wave" box does also contain an
2508 * "mp4a" box that we don't understand. */
2511 MP4_READBOX_EXIT( 1 );
2514 READ_SAMPLE_DESC_COMMON_8BYTES_HEADER
;
2517 * XXX hack -> produce a copy of the nearly complete chunk
2519 p_box
->data
.p_sample_soun
->i_qt_description
= 0;
2520 p_box
->data
.p_sample_soun
->p_qt_description
= NULL
;
2523 p_box
->data
.p_sample_soun
->p_qt_description
= malloc( i_read
);
2524 if( p_box
->data
.p_sample_soun
->p_qt_description
)
2526 p_box
->data
.p_sample_soun
->i_qt_description
= i_read
;
2527 memcpy( p_box
->data
.p_sample_soun
->p_qt_description
, p_peek
, i_read
);
2531 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_qt_version
);
2532 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_qt_revision_level
);
2533 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_qt_vendor
);
2535 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_channelcount
);
2536 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_samplesize
);
2537 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_compressionid
);
2538 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_reserved3
);
2539 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_sampleratehi
);
2540 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_sampleratelo
);
2544 "read box: \"soun\" stsd qt_version %"PRIu16
" compid=%"PRIx16
,
2545 p_box
->data
.p_sample_soun
->i_qt_version
,
2546 p_box
->data
.p_sample_soun
->i_compressionid
);
2549 if( p_box
->data
.p_sample_soun
->i_qt_version
== 1 && i_read
>= 16 )
2551 /* SoundDescriptionV1 */
2552 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_sample_per_packet
);
2553 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_bytes_per_packet
);
2554 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_bytes_per_frame
);
2555 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_bytes_per_sample
);
2559 "read box: \"soun\" V1 sample/packet=%d bytes/packet=%d "
2560 "bytes/frame=%d bytes/sample=%d",
2561 p_box
->data
.p_sample_soun
->i_sample_per_packet
,
2562 p_box
->data
.p_sample_soun
->i_bytes_per_packet
,
2563 p_box
->data
.p_sample_soun
->i_bytes_per_frame
,
2564 p_box
->data
.p_sample_soun
->i_bytes_per_sample
);
2568 else if( p_box
->data
.p_sample_soun
->i_qt_version
== 2 && i_read
>= 36 )
2570 /* SoundDescriptionV2 */
2571 double f_sample_rate
;
2573 uint32_t i_channel
, i_extoffset
, i_dummy32
;
2576 if ( p_box
->data
.p_sample_soun
->i_channelcount
!= 0x3 ||
2577 p_box
->data
.p_sample_soun
->i_samplesize
!= 0x0010 ||
2578 p_box
->data
.p_sample_soun
->i_compressionid
!= 0xFFFE ||
2579 p_box
->data
.p_sample_soun
->i_reserved3
!= 0x0 ||
2580 p_box
->data
.p_sample_soun
->i_sampleratehi
!= 0x1 ||//65536
2581 p_box
->data
.p_sample_soun
->i_sampleratelo
!= 0x0 ) //remainder
2583 msg_Err( p_stream
, "invalid stsd V2 box defaults" );
2584 MP4_READBOX_EXIT( 0 );
2588 MP4_GET4BYTES( i_extoffset
); /* offset to stsd extentions */
2589 MP4_GET8BYTES( i_dummy64
);
2590 memcpy( &f_sample_rate
, &i_dummy64
, 8 );
2591 msg_Dbg( p_stream
, "read box: %f Hz", f_sample_rate
);
2592 /* Rounding error with lo, but we don't care since we do not support fractional audio rate */
2593 p_box
->data
.p_sample_soun
->i_sampleratehi
= (uint32_t)f_sample_rate
;
2594 p_box
->data
.p_sample_soun
->i_sampleratelo
= (f_sample_rate
- p_box
->data
.p_sample_soun
->i_sampleratehi
);
2596 MP4_GET4BYTES( i_channel
);
2597 p_box
->data
.p_sample_soun
->i_channelcount
= i_channel
;
2599 MP4_GET4BYTES( i_dummy32
);
2600 if ( i_dummy32
!= 0x7F000000 )
2602 msg_Err( p_stream
, "invalid stsd V2 box" );
2603 MP4_READBOX_EXIT( 0 );
2606 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_constbitsperchannel
);
2607 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_formatflags
);
2608 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_constbytesperaudiopacket
);
2609 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_constLPCMframesperaudiopacket
);
2612 msg_Dbg( p_stream
, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
2613 "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
2615 p_box
->data
.p_sample_soun
->i_constbitsperchannel
,
2616 p_box
->data
.p_sample_soun
->i_formatflags
,
2617 p_box
->data
.p_sample_soun
->i_constbytesperaudiopacket
,
2618 p_box
->data
.p_sample_soun
->i_constLPCMframesperaudiopacket
);
2621 if( i_extoffset
> i_actually_read
)
2622 i_extoffset
= i_actually_read
;
2623 p_peek
= &p_buff
[i_extoffset
];
2624 i_read
= i_actually_read
- i_extoffset
;
2628 p_box
->data
.p_sample_soun
->i_sample_per_packet
= 0;
2629 p_box
->data
.p_sample_soun
->i_bytes_per_packet
= 0;
2630 p_box
->data
.p_sample_soun
->i_bytes_per_frame
= 0;
2631 p_box
->data
.p_sample_soun
->i_bytes_per_sample
= 0;
2634 msg_Dbg( p_stream
, "read box: \"soun\" V0 or qt1/2 (rest=%"PRIu64
")",
2640 if( p_box
->i_type
== ATOM_drms
)
2642 msg_Warn( p_stream
, "DRM protected streams are not supported." );
2643 MP4_READBOX_EXIT( 0 );
2646 if( p_box
->i_type
== ATOM_samr
|| p_box
->i_type
== ATOM_sawb
)
2648 /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
2649 p_box
->data
.p_sample_soun
->i_channelcount
= 1;
2652 /* Loads extensions */
2653 MP4_ReadBoxContainerRawInBox( p_stream
, p_box
, p_peek
, i_read
,
2654 p_box
->i_pos
+ p_peek
- p_buff
); /* esds/wave/... */
2657 msg_Dbg( p_stream
, "read box: \"soun\" in stsd channel %d "
2658 "sample size %d sample rate %f",
2659 p_box
->data
.p_sample_soun
->i_channelcount
,
2660 p_box
->data
.p_sample_soun
->i_samplesize
,
2661 (float)p_box
->data
.p_sample_soun
->i_sampleratehi
+
2662 (float)p_box
->data
.p_sample_soun
->i_sampleratelo
/ BLOCK16x16
);
2665 MP4_READBOX_EXIT( 1 );
2668 static void MP4_FreeBox_sample_vide( MP4_Box_t
*p_box
)
2670 free( p_box
->data
.p_sample_vide
->p_qt_image_description
);
2673 int MP4_ReadBox_sample_vide( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2675 p_box
->i_handler
= ATOM_vide
;
2676 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t
, MP4_FreeBox_sample_vide
);
2678 size_t i_actually_read
= i_read
+ header_size
;
2680 READ_SAMPLE_DESC_COMMON_8BYTES_HEADER
;
2683 * XXX hack -> produce a copy of the nearly complete chunk
2687 p_box
->data
.p_sample_vide
->p_qt_image_description
= malloc( i_read
);
2688 if( unlikely( p_box
->data
.p_sample_vide
->p_qt_image_description
== NULL
) )
2689 MP4_READBOX_EXIT( 0 );
2690 p_box
->data
.p_sample_vide
->i_qt_image_description
= i_read
;
2691 memcpy( p_box
->data
.p_sample_vide
->p_qt_image_description
,
2696 p_box
->data
.p_sample_vide
->i_qt_image_description
= 0;
2697 p_box
->data
.p_sample_vide
->p_qt_image_description
= NULL
;
2700 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_qt_version
);
2701 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_qt_revision_level
);
2702 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_qt_vendor
);
2704 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_qt_temporal_quality
);
2705 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_qt_spatial_quality
);
2707 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_width
);
2708 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_height
);
2710 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_horizresolution
);
2711 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_vertresolution
);
2713 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_qt_data_size
);
2714 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_qt_frame_count
);
2717 MP4_READBOX_EXIT( 0 );
2718 if( p_peek
[0] <= 31 ) // Must be Pascal String
2720 memcpy( &p_box
->data
.p_sample_vide
->sz_compressorname
, &p_peek
[1], p_peek
[0] );
2721 p_box
->data
.p_sample_vide
->sz_compressorname
[p_peek
[0]] = 0;
2723 p_peek
+= 32; i_read
-= 32;
2725 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_depth
);
2726 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_qt_color_table
);
2728 if( p_box
->i_type
== ATOM_drmi
)
2730 msg_Warn( p_stream
, "DRM protected streams are not supported." );
2731 MP4_READBOX_EXIT( 0 );
2734 if( i_actually_read
> 78 && p_peek
- p_buff
> 78 )
2736 MP4_ReadBoxContainerRawInBox( p_stream
, p_box
, p_peek
, i_read
,
2737 p_box
->i_pos
+ p_peek
- p_buff
);
2741 msg_Dbg( p_stream
, "read box: \"vide\" in stsd %dx%d depth %d (%s)",
2742 p_box
->data
.p_sample_vide
->i_width
,
2743 p_box
->data
.p_sample_vide
->i_height
,
2744 p_box
->data
.p_sample_vide
->i_depth
,
2745 p_box
->data
.p_sample_vide
->sz_compressorname
);
2748 MP4_READBOX_EXIT( 1 );
2751 static void MP4_FreeBox_sample_generic( MP4_Box_t
*p_box
)
2753 free( p_box
->data
.p_sample_gen
->p_data
);
2756 static int MP4_ReadBox_sample_generic( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2758 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_generic_t
, 16, NULL
);
2760 READ_SAMPLE_DESC_COMMON_8BYTES_HEADER
;
2762 switch( p_box
->i_type
)
2765 p_box
->i_handler
= ATOM_text
;
2768 msg_Warn( p_stream
, "Unknown mapping for %4.4s with generic handler",
2769 (const char *)&p_box
->i_type
);
2773 MP4_ReadBoxContainerChildren( p_stream
, p_box
, NULL
);
2775 if ( MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) )
2776 MP4_READBOX_EXIT( 0 );
2778 MP4_READBOX_EXIT( 1 );
2781 static int MP4_ReadBox_sample_hint8( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2783 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_generic_t
, 24, MP4_FreeBox_sample_generic
);
2785 READ_SAMPLE_DESC_COMMON_8BYTES_HEADER
;
2787 if( !(p_box
->data
.p_sample_gen
->p_data
= malloc(8)) )
2788 MP4_READBOX_EXIT( 0 );
2790 MP4_GET8BYTES( *(p_box
->data
.p_sample_gen
->p_data
) );
2791 p_box
->data
.p_sample_gen
->i_data
= 8;
2793 MP4_ReadBoxContainerChildren(p_stream
, p_box
, NULL
);
2795 if ( MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) )
2796 MP4_READBOX_EXIT( 0 );
2798 MP4_READBOX_EXIT( 1 );
2801 static int MP4_ReadBox_sample_text( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2803 p_box
->i_handler
= ATOM_text
;
2804 MP4_READBOX_ENTER( MP4_Box_data_sample_generic_t
, MP4_FreeBox_sample_generic
);
2806 READ_SAMPLE_DESC_COMMON_8BYTES_HEADER
;
2810 p_box
->data
.p_sample_gen
->p_data
= malloc( i_read
);
2811 if( !p_box
->data
.p_sample_gen
->p_data
)
2812 MP4_READBOX_EXIT( 0 );
2813 memcpy( p_box
->data
.p_sample_gen
->p_data
, p_peek
, i_read
);
2815 p_box
->data
.p_sample_gen
->i_data
= i_read
;
2818 msg_Dbg( p_stream
, "read box: \"%4.4s\" in stsd", (const char*) &p_box
->i_type
);
2820 MP4_READBOX_EXIT( 1 );
2823 static int MP4_ReadBox_stsd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2825 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_lcont_t
, 16, NULL
);
2827 MP4_READBOX_EXIT( 0 );
2829 MP4_GETVERSIONFLAGS( p_box
->data
.p_lcont
);
2830 if( p_box
->data
.p_lcont
->i_version
> 1 )
2831 MP4_READBOX_EXIT( 0 );
2832 MP4_GET4BYTES( p_box
->data
.p_lcont
->i_entry_count
);
2834 const MP4_Box_t
*p_mdia
= MP4_BoxGet( p_box
, "../../../" );
2835 const MP4_Box_t
*p_hdlr
;
2836 if( p_mdia
== NULL
|| p_mdia
->i_type
!= ATOM_mdia
||
2837 (p_hdlr
= MP4_BoxGet( p_mdia
, "hdlr" )) == NULL
)
2839 MP4_READBOX_EXIT( 0 );
2842 /* Tag stsd with handler type that children can just read from */
2843 p_box
->i_handler
= BOXDATA(p_hdlr
)->i_handler_type
;
2845 uint32_t i_entry
= 0;
2846 i_read
= p_box
->i_size
- 16;
2847 while (i_read
> 8 && i_entry
< p_box
->data
.p_lcont
->i_entry_count
)
2849 int(*pf_read
)(stream_t
*, MP4_Box_t
*);
2850 switch( BOXDATA(p_hdlr
)->i_handler_type
)
2853 pf_read
= MP4_ReadBox_sample_soun
;
2856 case ATOM_pict
: /* heif */
2857 pf_read
= MP4_ReadBox_sample_vide
;
2860 pf_read
= MP4_ReadBox_sample_hint8
;
2867 pf_read
= MP4_ReadBox_sample_text
;
2869 case ATOM_subp
: /* see #13464 */
2870 pf_read
= MP4_ReadBox_sample_generic
;
2874 msg_Warn( p_stream
, "unknown handler type %4.4s in stsd",
2875 (const char *)& BOXDATA(p_hdlr
)->i_handler_type
);
2882 MP4_Box_t
*p_sample
= MP4_ReadBoxUsing( p_stream
, p_box
, pf_read
);
2886 /* write back stsd handler in case of final handler set by child */
2887 p_box
->i_handler
= p_sample
->i_handler
;
2889 MP4_BoxAddChild( p_box
, p_sample
);
2892 if( i_read
< p_sample
->i_size
)
2893 MP4_READBOX_EXIT( 0 );
2895 i_read
-= p_sample
->i_size
;
2898 if (i_entry
!= p_box
->data
.p_lcont
->i_entry_count
)
2899 p_box
->data
.p_lcont
->i_entry_count
= i_entry
;
2902 msg_Dbg( p_stream
, "read box: \"%4.4s\" entry-count %d", (char *)&p_box
->i_type
,
2903 p_box
->data
.p_lcont
->i_entry_count
);
2907 if ( MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) )
2908 MP4_READBOX_EXIT( 0 );
2910 MP4_READBOX_EXIT( 1 );
2913 static void MP4_FreeBox_stsz( MP4_Box_t
*p_box
)
2915 free( p_box
->data
.p_stsz
->i_entry_size
);
2918 static int MP4_ReadBox_stsz( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2922 MP4_READBOX_ENTER( MP4_Box_data_stsz_t
, MP4_FreeBox_stsz
);
2924 MP4_GETVERSIONFLAGS( p_box
->data
.p_stsz
);
2926 MP4_GET4BYTES( p_box
->data
.p_stsz
->i_sample_size
);
2927 MP4_GET4BYTES( count
);
2928 p_box
->data
.p_stsz
->i_sample_count
= count
;
2930 if( p_box
->data
.p_stsz
->i_sample_size
== 0 )
2932 if( UINT64_C(4) * count
> i_read
)
2933 MP4_READBOX_EXIT( 0 );
2935 p_box
->data
.p_stsz
->i_entry_size
=
2936 vlc_alloc( count
, sizeof(uint32_t) );
2937 if( unlikely( !p_box
->data
.p_stsz
->i_entry_size
) )
2938 MP4_READBOX_EXIT( 0 );
2940 for( uint32_t i
= 0; i
< count
; i
++ )
2942 MP4_GET4BYTES( p_box
->data
.p_stsz
->i_entry_size
[i
] );
2946 p_box
->data
.p_stsz
->i_entry_size
= NULL
;
2949 msg_Dbg( p_stream
, "read box: \"stsz\" sample-size %d sample-count %d",
2950 p_box
->data
.p_stsz
->i_sample_size
,
2951 p_box
->data
.p_stsz
->i_sample_count
);
2954 MP4_READBOX_EXIT( 1 );
2957 static void MP4_FreeBox_stsc( MP4_Box_t
*p_box
)
2959 free( p_box
->data
.p_stsc
->i_first_chunk
);
2960 free( p_box
->data
.p_stsc
->i_samples_per_chunk
);
2961 free( p_box
->data
.p_stsc
->i_sample_description_index
);
2964 static int MP4_ReadBox_stsc( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2968 MP4_READBOX_ENTER( MP4_Box_data_stsc_t
, MP4_FreeBox_stsc
);
2970 MP4_GETVERSIONFLAGS( p_box
->data
.p_stsc
);
2971 MP4_GET4BYTES( count
);
2973 if( UINT64_C(12) * count
> i_read
)
2974 MP4_READBOX_EXIT( 0 );
2976 p_box
->data
.p_stsc
->i_first_chunk
= vlc_alloc( count
, sizeof(uint32_t) );
2977 p_box
->data
.p_stsc
->i_samples_per_chunk
= vlc_alloc( count
,
2979 p_box
->data
.p_stsc
->i_sample_description_index
= vlc_alloc( count
,
2981 if( unlikely( p_box
->data
.p_stsc
->i_first_chunk
== NULL
2982 || p_box
->data
.p_stsc
->i_samples_per_chunk
== NULL
2983 || p_box
->data
.p_stsc
->i_sample_description_index
== NULL
) )
2985 MP4_READBOX_EXIT( 0 );
2987 p_box
->data
.p_stsc
->i_entry_count
= count
;
2989 for( uint32_t i
= 0; i
< count
;i
++ )
2991 MP4_GET4BYTES( p_box
->data
.p_stsc
->i_first_chunk
[i
] );
2992 MP4_GET4BYTES( p_box
->data
.p_stsc
->i_samples_per_chunk
[i
] );
2993 MP4_GET4BYTES( p_box
->data
.p_stsc
->i_sample_description_index
[i
] );
2997 msg_Dbg( p_stream
, "read box: \"stsc\" entry-count %d",
2998 p_box
->data
.p_stsc
->i_entry_count
);
3001 MP4_READBOX_EXIT( 1 );
3004 static void MP4_FreeBox_sdp( MP4_Box_t
*p_box
)
3006 free( p_box
->data
.p_sdp
->psz_text
);
3009 static int MP4_ReadBox_sdp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3011 MP4_READBOX_ENTER( MP4_Box_data_sdp_t
, MP4_FreeBox_sdp
);
3013 MP4_GETSTRINGZ( p_box
->data
.p_sdp
->psz_text
);
3015 MP4_READBOX_EXIT( 1 );
3018 static void MP4_FreeBox_rtp( MP4_Box_t
*p_box
)
3020 free( p_box
->data
.p_moviehintinformation_rtp
->psz_text
);
3023 static int MP4_ReadBox_rtp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3025 MP4_READBOX_ENTER( MP4_Box_data_moviehintinformation_rtp_t
, MP4_FreeBox_rtp
);
3027 MP4_GET4BYTES( p_box
->data
.p_moviehintinformation_rtp
->i_description_format
);
3029 MP4_GETSTRINGZ( p_box
->data
.p_moviehintinformation_rtp
->psz_text
);
3031 MP4_READBOX_EXIT( 1 );
3034 static int MP4_ReadBox_tims( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3036 MP4_READBOX_ENTER( MP4_Box_data_tims_t
, NULL
);
3038 MP4_GET4BYTES( p_box
->data
.p_tims
->i_timescale
);
3040 MP4_READBOX_EXIT( 1 );
3043 static int MP4_ReadBox_tsro( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3045 MP4_READBOX_ENTER( MP4_Box_data_tsro_t
, NULL
);
3047 MP4_GET4BYTES( p_box
->data
.p_tsro
->i_offset
);
3049 MP4_READBOX_EXIT( 1 );
3052 static int MP4_ReadBox_tssy( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3054 MP4_READBOX_ENTER( MP4_Box_data_tssy_t
, NULL
);
3056 MP4_GET1BYTE( p_box
->data
.p_tssy
->i_reserved_timestamp_sync
);
3058 MP4_READBOX_EXIT( 1 );
3061 static void MP4_FreeBox_stco_co64( MP4_Box_t
*p_box
)
3063 free( p_box
->data
.p_co64
->i_chunk_offset
);
3066 static int MP4_ReadBox_stco_co64( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3068 const bool sixtyfour
= p_box
->i_type
!= ATOM_stco
;
3071 MP4_READBOX_ENTER( MP4_Box_data_co64_t
, MP4_FreeBox_stco_co64
);
3073 MP4_GETVERSIONFLAGS( p_box
->data
.p_co64
);
3074 MP4_GET4BYTES( count
);
3076 if( (sixtyfour
? UINT64_C(8) : UINT64_C(4)) * count
> i_read
)
3077 MP4_READBOX_EXIT( 0 );
3079 p_box
->data
.p_co64
->i_chunk_offset
= vlc_alloc( count
, sizeof(uint64_t) );
3080 if( unlikely(p_box
->data
.p_co64
->i_chunk_offset
== NULL
) )
3081 MP4_READBOX_EXIT( 0 );
3082 p_box
->data
.p_co64
->i_entry_count
= count
;
3084 for( uint32_t i
= 0; i
< count
; i
++ )
3087 MP4_GET8BYTES( p_box
->data
.p_co64
->i_chunk_offset
[i
] );
3089 MP4_GET4BYTES( p_box
->data
.p_co64
->i_chunk_offset
[i
] );
3093 msg_Dbg( p_stream
, "read box: \"co64\" entry-count %d",
3094 p_box
->data
.p_co64
->i_entry_count
);
3097 MP4_READBOX_EXIT( 1 );
3100 static void MP4_FreeBox_stss( MP4_Box_t
*p_box
)
3102 free( p_box
->data
.p_stss
->i_sample_number
);
3105 static int MP4_ReadBox_stss( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3109 MP4_READBOX_ENTER( MP4_Box_data_stss_t
, MP4_FreeBox_stss
);
3111 MP4_GETVERSIONFLAGS( p_box
->data
.p_stss
);
3112 MP4_GET4BYTES( count
);
3114 if( UINT64_C(4) * count
> i_read
)
3115 MP4_READBOX_EXIT( 0 );
3117 p_box
->data
.p_stss
->i_sample_number
= vlc_alloc( count
, sizeof(uint32_t) );
3118 if( unlikely( p_box
->data
.p_stss
->i_sample_number
== NULL
) )
3119 MP4_READBOX_EXIT( 0 );
3120 p_box
->data
.p_stss
->i_entry_count
= count
;
3122 for( uint32_t i
= 0; i
< count
; i
++ )
3124 MP4_GET4BYTES( p_box
->data
.p_stss
->i_sample_number
[i
] );
3125 /* XXX in libmp4 sample begin at 0 */
3126 p_box
->data
.p_stss
->i_sample_number
[i
]--;
3130 msg_Dbg( p_stream
, "read box: \"stss\" entry-count %d",
3131 p_box
->data
.p_stss
->i_entry_count
);
3134 MP4_READBOX_EXIT( 1 );
3137 static void MP4_FreeBox_stsh( MP4_Box_t
*p_box
)
3139 free( p_box
->data
.p_stsh
->i_shadowed_sample_number
);
3140 free( p_box
->data
.p_stsh
->i_sync_sample_number
);
3143 static int MP4_ReadBox_stsh( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3147 MP4_READBOX_ENTER( MP4_Box_data_stsh_t
, MP4_FreeBox_stsh
);
3149 MP4_GETVERSIONFLAGS( p_box
->data
.p_stsh
);
3150 MP4_GET4BYTES( count
);
3152 if( UINT64_C(8) * count
> i_read
)
3153 MP4_READBOX_EXIT( 0 );
3155 p_box
->data
.p_stsh
->i_shadowed_sample_number
= vlc_alloc( count
,
3157 p_box
->data
.p_stsh
->i_sync_sample_number
= vlc_alloc( count
,
3159 if( p_box
->data
.p_stsh
->i_shadowed_sample_number
== NULL
3160 || p_box
->data
.p_stsh
->i_sync_sample_number
== NULL
)
3161 MP4_READBOX_EXIT( 0 );
3162 p_box
->data
.p_stsh
->i_entry_count
= count
;
3164 for( uint32_t i
= 0; i
< p_box
->data
.p_stss
->i_entry_count
; i
++ )
3166 MP4_GET4BYTES( p_box
->data
.p_stsh
->i_shadowed_sample_number
[i
] );
3167 MP4_GET4BYTES( p_box
->data
.p_stsh
->i_sync_sample_number
[i
] );
3171 msg_Dbg( p_stream
, "read box: \"stsh\" entry-count %d",
3172 p_box
->data
.p_stsh
->i_entry_count
);
3174 MP4_READBOX_EXIT( 1 );
3177 static void MP4_FreeBox_stdp( MP4_Box_t
*p_box
)
3179 free( p_box
->data
.p_stdp
->i_priority
);
3182 static int MP4_ReadBox_stdp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3184 MP4_READBOX_ENTER( MP4_Box_data_stdp_t
, MP4_FreeBox_stdp
);
3186 MP4_GETVERSIONFLAGS( p_box
->data
.p_stdp
);
3188 p_box
->data
.p_stdp
->i_priority
=
3189 calloc( i_read
/ 2, sizeof(uint16_t) );
3191 if( unlikely( !p_box
->data
.p_stdp
->i_priority
) )
3192 MP4_READBOX_EXIT( 0 );
3194 for( unsigned i
= 0; i
< i_read
/ 2 ; i
++ )
3196 MP4_GET2BYTES( p_box
->data
.p_stdp
->i_priority
[i
] );
3200 msg_Dbg( p_stream
, "read box: \"stdp\" entry-count %"PRId64
,
3204 MP4_READBOX_EXIT( 1 );
3207 static void MP4_FreeBox_elst( MP4_Box_t
*p_box
)
3209 free( p_box
->data
.p_elst
->i_segment_duration
);
3210 free( p_box
->data
.p_elst
->i_media_time
);
3211 free( p_box
->data
.p_elst
->i_media_rate_integer
);
3212 free( p_box
->data
.p_elst
->i_media_rate_fraction
);
3215 static int MP4_ReadBox_elst( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3219 MP4_READBOX_ENTER( MP4_Box_data_elst_t
, MP4_FreeBox_elst
);
3221 MP4_GETVERSIONFLAGS( p_box
->data
.p_elst
);
3222 MP4_GET4BYTES( count
);
3225 MP4_READBOX_EXIT( 1 );
3227 uint32_t i_entries_max
= i_read
/ ((p_box
->data
.p_elst
->i_version
== 1) ? 20 : 12);
3228 if( count
> i_entries_max
)
3229 count
= i_entries_max
;
3231 p_box
->data
.p_elst
->i_segment_duration
= vlc_alloc( count
,
3233 p_box
->data
.p_elst
->i_media_time
= vlc_alloc( count
, sizeof(int64_t) );
3234 p_box
->data
.p_elst
->i_media_rate_integer
= vlc_alloc( count
,
3236 p_box
->data
.p_elst
->i_media_rate_fraction
= vlc_alloc( count
,
3238 if( p_box
->data
.p_elst
->i_segment_duration
== NULL
3239 || p_box
->data
.p_elst
->i_media_time
== NULL
3240 || p_box
->data
.p_elst
->i_media_rate_integer
== NULL
3241 || p_box
->data
.p_elst
->i_media_rate_fraction
== NULL
)
3243 MP4_READBOX_EXIT( 0 );
3245 p_box
->data
.p_elst
->i_entry_count
= count
;
3247 for( uint32_t i
= 0; i
< count
; i
++ )
3249 uint64_t segment_duration
;
3252 if( p_box
->data
.p_elst
->i_version
== 1 )
3254 union { int64_t s
; uint64_t u
; } u
;
3256 MP4_GET8BYTES( segment_duration
);
3257 MP4_GET8BYTES( u
.u
);
3262 union { int32_t s
; uint32_t u
; } u
;
3264 MP4_GET4BYTES( segment_duration
);
3265 MP4_GET4BYTES( u
.u
);
3269 p_box
->data
.p_elst
->i_segment_duration
[i
] = segment_duration
;
3270 p_box
->data
.p_elst
->i_media_time
[i
] = media_time
;
3271 MP4_GET2BYTES( p_box
->data
.p_elst
->i_media_rate_integer
[i
] );
3272 MP4_GET2BYTES( p_box
->data
.p_elst
->i_media_rate_fraction
[i
] );
3276 msg_Dbg( p_stream
, "read box: \"elst\" entry-count %" PRIu32
,
3277 p_box
->data
.p_elst
->i_entry_count
);
3279 MP4_READBOX_EXIT( 1 );
3282 static void MP4_FreeBox_cprt( MP4_Box_t
*p_box
)
3284 free( p_box
->data
.p_cprt
->psz_notice
);
3287 static int MP4_ReadBox_cprt( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3289 uint16_t i_language
;
3292 MP4_READBOX_ENTER( MP4_Box_data_cprt_t
, MP4_FreeBox_cprt
);
3294 MP4_GETVERSIONFLAGS( p_box
->data
.p_cprt
);
3296 MP4_GET2BYTES( i_language
);
3297 decodeQtLanguageCode( i_language
, p_box
->data
.p_cprt
->rgs_language
, &b_mac
);
3299 MP4_GETSTRINGZ( p_box
->data
.p_cprt
->psz_notice
);
3302 msg_Dbg( p_stream
, "read box: \"cprt\" language %3.3s notice %s",
3303 p_box
->data
.p_cprt
->rgs_language
,
3304 p_box
->data
.p_cprt
->psz_notice
);
3307 MP4_READBOX_EXIT( 1 );
3310 static int MP4_ReadBox_dcom( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3312 MP4_READBOX_ENTER( MP4_Box_data_dcom_t
, NULL
);
3314 MP4_GETFOURCC( p_box
->data
.p_dcom
->i_algorithm
);
3317 "read box: \"dcom\" compression algorithm : %4.4s",
3318 (char*)&p_box
->data
.p_dcom
->i_algorithm
);
3320 MP4_READBOX_EXIT( 1 );
3323 static void MP4_FreeBox_cmvd( MP4_Box_t
*p_box
)
3325 free( p_box
->data
.p_cmvd
->p_data
);
3328 static int MP4_ReadBox_cmvd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3330 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t
, MP4_FreeBox_cmvd
);
3332 MP4_GET4BYTES( p_box
->data
.p_cmvd
->i_uncompressed_size
);
3334 p_box
->data
.p_cmvd
->i_compressed_size
= i_read
;
3336 if( !( p_box
->data
.p_cmvd
->p_data
= malloc( i_read
) ) )
3337 MP4_READBOX_EXIT( 0 );
3339 /* now copy compressed data */
3340 memcpy( p_box
->data
.p_cmvd
->p_data
, p_peek
,i_read
);
3342 p_box
->data
.p_cmvd
->b_compressed
= 1;
3345 msg_Dbg( p_stream
, "read box: \"cmvd\" compressed data size %d",
3346 p_box
->data
.p_cmvd
->i_compressed_size
);
3349 MP4_READBOX_EXIT( 1 );
3352 static int MP4_ReadBox_cmov( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3358 stream_t
*p_stream_memory
;
3364 if( !( p_box
->data
.p_cmov
= calloc(1, sizeof( MP4_Box_data_cmov_t
) ) ) )
3367 if( !p_box
->p_father
||
3368 ( p_box
->p_father
->i_type
!= ATOM_moov
&&
3369 p_box
->p_father
->i_type
!= ATOM_foov
) )
3371 msg_Warn( p_stream
, "Read box: \"cmov\" box alone" );
3375 if( !MP4_ReadBoxContainer( p_stream
, p_box
) )
3380 if( ( p_dcom
= MP4_BoxGet( p_box
, "dcom" ) ) == NULL
||
3381 ( p_cmvd
= MP4_BoxGet( p_box
, "cmvd" ) ) == NULL
||
3382 p_cmvd
->data
.p_cmvd
->p_data
== NULL
)
3384 msg_Warn( p_stream
, "read box: \"cmov\" incomplete" );
3388 if( p_dcom
->data
.p_dcom
->i_algorithm
!= ATOM_zlib
)
3390 msg_Dbg( p_stream
, "read box: \"cmov\" compression algorithm : %4.4s "
3391 "not supported", (char*)&p_dcom
->data
.p_dcom
->i_algorithm
);
3396 msg_Dbg( p_stream
, "read box: \"cmov\" zlib unsupported" );
3400 /* decompress data */
3401 /* allocate a new buffer */
3402 if( !( p_data
= malloc( p_cmvd
->data
.p_cmvd
->i_uncompressed_size
) ) )
3404 /* init default structures */
3405 z_data
.next_in
= p_cmvd
->data
.p_cmvd
->p_data
;
3406 z_data
.avail_in
= p_cmvd
->data
.p_cmvd
->i_compressed_size
;
3407 z_data
.next_out
= p_data
;
3408 z_data
.avail_out
= p_cmvd
->data
.p_cmvd
->i_uncompressed_size
;
3409 z_data
.zalloc
= (alloc_func
)Z_NULL
;
3410 z_data
.zfree
= (free_func
)Z_NULL
;
3411 z_data
.opaque
= (voidpf
)Z_NULL
;
3414 if( inflateInit( &z_data
) != Z_OK
)
3416 msg_Err( p_stream
, "read box: \"cmov\" error while uncompressing" );
3422 i_result
= inflate( &z_data
, Z_NO_FLUSH
);
3423 if( i_result
!= Z_OK
&& i_result
!= Z_STREAM_END
)
3425 msg_Err( p_stream
, "read box: \"cmov\" error while uncompressing" );
3430 if( p_cmvd
->data
.p_cmvd
->i_uncompressed_size
!= z_data
.total_out
)
3432 msg_Warn( p_stream
, "read box: \"cmov\" uncompressing data size "
3435 p_cmvd
->data
.p_cmvd
->i_uncompressed_size
= z_data
.total_out
;
3438 if( inflateEnd( &z_data
) != Z_OK
)
3440 msg_Warn( p_stream
, "read box: \"cmov\" error while uncompressing "
3444 free( p_cmvd
->data
.p_cmvd
->p_data
);
3445 p_cmvd
->data
.p_cmvd
->p_data
= p_data
;
3446 p_cmvd
->data
.p_cmvd
->b_compressed
= 0;
3448 msg_Dbg( p_stream
, "read box: \"cmov\" box successfully uncompressed" );
3450 /* now create a memory stream */
3452 vlc_stream_MemoryNew( VLC_OBJECT(p_stream
),
3453 p_cmvd
->data
.p_cmvd
->p_data
,
3454 p_cmvd
->data
.p_cmvd
->i_uncompressed_size
, true );
3456 /* and read uncompressd moov */
3457 p_box
->data
.p_cmov
->p_moov
= MP4_ReadBox( p_stream_memory
, NULL
);
3459 vlc_stream_Delete( p_stream_memory
);
3462 msg_Dbg( p_stream
, "read box: \"cmov\" compressed movie header completed");
3465 return p_box
->data
.p_cmov
->p_moov
? 1 : 0;
3466 #endif /* HAVE_ZLIB_H */
3469 static void MP4_FreeBox_rdrf( MP4_Box_t
*p_box
)
3471 free( p_box
->data
.p_rdrf
->psz_ref
);
3474 static int MP4_ReadBox_rdrf( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3477 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t
, MP4_FreeBox_rdrf
);
3479 MP4_GETVERSIONFLAGS( p_box
->data
.p_rdrf
);
3480 MP4_GETFOURCC( p_box
->data
.p_rdrf
->i_ref_type
);
3481 MP4_GET4BYTES( i_len
);
3486 p_box
->data
.p_rdrf
->psz_ref
= malloc( i_len
);
3487 if( p_box
->data
.p_rdrf
->psz_ref
== NULL
)
3488 MP4_READBOX_EXIT( 0 );
3491 for( unsigned i
= 0; i
< i_len
; i
++ )
3493 MP4_GET1BYTE( p_box
->data
.p_rdrf
->psz_ref
[i
] );
3495 p_box
->data
.p_rdrf
->psz_ref
[i_len
] = '\0';
3499 p_box
->data
.p_rdrf
->psz_ref
= NULL
;
3504 "read box: \"rdrf\" type:%4.4s ref %s",
3505 (char*)&p_box
->data
.p_rdrf
->i_ref_type
,
3506 p_box
->data
.p_rdrf
->psz_ref
);
3508 MP4_READBOX_EXIT( 1 );
3513 static int MP4_ReadBox_rmdr( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3515 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t
, NULL
);
3517 MP4_GETVERSIONFLAGS( p_box
->data
.p_rmdr
);
3519 MP4_GET4BYTES( p_box
->data
.p_rmdr
->i_rate
);
3523 "read box: \"rmdr\" rate:%d",
3524 p_box
->data
.p_rmdr
->i_rate
);
3526 MP4_READBOX_EXIT( 1 );
3529 static int MP4_ReadBox_rmqu( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3531 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t
, NULL
);
3533 MP4_GET4BYTES( p_box
->data
.p_rmqu
->i_quality
);
3537 "read box: \"rmqu\" quality:%d",
3538 p_box
->data
.p_rmqu
->i_quality
);
3540 MP4_READBOX_EXIT( 1 );
3543 static int MP4_ReadBox_rmvc( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3545 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t
, NULL
);
3546 MP4_GETVERSIONFLAGS( p_box
->data
.p_rmvc
);
3548 MP4_GETFOURCC( p_box
->data
.p_rmvc
->i_gestaltType
);
3549 MP4_GET4BYTES( p_box
->data
.p_rmvc
->i_val1
);
3550 MP4_GET4BYTES( p_box
->data
.p_rmvc
->i_val2
);
3551 MP4_GET2BYTES( p_box
->data
.p_rmvc
->i_checkType
);
3555 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
3556 (char*)&p_box
->data
.p_rmvc
->i_gestaltType
,
3557 p_box
->data
.p_rmvc
->i_val1
,p_box
->data
.p_rmvc
->i_val2
,
3558 p_box
->data
.p_rmvc
->i_checkType
);
3561 MP4_READBOX_EXIT( 1 );
3564 static int MP4_ReadBox_frma( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3566 MP4_READBOX_ENTER( MP4_Box_data_frma_t
, NULL
);
3568 MP4_GETFOURCC( p_box
->data
.p_frma
->i_type
);
3571 msg_Dbg( p_stream
, "read box: \"frma\" i_type:%4.4s",
3572 (char *)&p_box
->data
.p_frma
->i_type
);
3575 MP4_READBOX_EXIT( 1 );
3578 static int MP4_ReadBox_skcr( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3580 MP4_READBOX_ENTER( MP4_Box_data_skcr_t
, NULL
);
3582 MP4_GET4BYTES( p_box
->data
.p_skcr
->i_init
);
3583 MP4_GET4BYTES( p_box
->data
.p_skcr
->i_encr
);
3584 MP4_GET4BYTES( p_box
->data
.p_skcr
->i_decr
);
3587 msg_Dbg( p_stream
, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
3588 p_box
->data
.p_skcr
->i_init
,
3589 p_box
->data
.p_skcr
->i_encr
,
3590 p_box
->data
.p_skcr
->i_decr
);
3593 MP4_READBOX_EXIT( 1 );
3596 static int MP4_ReadBox_drms( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3599 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
3600 * so unless data decrypt itself by magic, there will be no playback,
3601 * but we never know... */
3602 msg_Warn( p_stream
, "DRM protected streams are not supported." );
3606 static void MP4_FreeBox_Binary( MP4_Box_t
*p_box
)
3608 free( p_box
->data
.p_binary
->p_blob
);
3611 static int MP4_ReadBox_Binary( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3613 MP4_READBOX_ENTER( MP4_Box_data_binary_t
, MP4_FreeBox_Binary
);
3614 i_read
= __MIN( i_read
, UINT32_MAX
);
3617 p_box
->data
.p_binary
->p_blob
= malloc( i_read
);
3618 if ( p_box
->data
.p_binary
->p_blob
)
3620 memcpy( p_box
->data
.p_binary
->p_blob
, p_peek
, i_read
);
3621 p_box
->data
.p_binary
->i_blob
= i_read
;
3624 MP4_READBOX_EXIT( 1 );
3627 static void MP4_FreeBox_data( MP4_Box_t
*p_box
)
3629 free( p_box
->data
.p_data
->p_blob
);
3632 static int MP4_ReadBox_data( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3634 MP4_READBOX_ENTER( MP4_Box_data_data_t
, MP4_FreeBox_data
);
3635 MP4_Box_data_data_t
*p_data
= p_box
->data
.p_data
;
3637 if ( i_read
< 8 || i_read
- 8 > UINT32_MAX
)
3638 MP4_READBOX_EXIT( 0 );
3641 MP4_GET1BYTE( i_type
);
3645 msg_Dbg( p_stream
, "skipping unknown 'data' atom with type %"PRIu8
, i_type
);
3647 MP4_READBOX_EXIT( 0 );
3650 MP4_GET3BYTES( p_data
->e_wellknowntype
);
3651 MP4_GET2BYTES( p_data
->locale
.i_country
);
3652 MP4_GET2BYTES( p_data
->locale
.i_language
);
3654 msg_Dbg( p_stream
, "read 'data' atom: knowntype=%"PRIu32
", country=%"PRIu16
" lang=%"PRIu16
3655 ", size %"PRIu64
" bytes", p_data
->e_wellknowntype
,
3656 p_data
->locale
.i_country
, p_data
->locale
.i_language
, i_read
);
3658 p_box
->data
.p_data
->p_blob
= malloc( i_read
);
3659 if ( !p_box
->data
.p_data
->p_blob
)
3660 MP4_READBOX_EXIT( 0 );
3662 p_box
->data
.p_data
->i_blob
= i_read
;
3663 memcpy( p_box
->data
.p_data
->p_blob
, p_peek
, i_read
);
3665 MP4_READBOX_EXIT( 1 );
3668 static int MP4_ReadBox_Metadata( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3670 const uint8_t *p_peek
;
3671 if ( vlc_stream_Peek( p_stream
, &p_peek
, 16 ) < 16 )
3673 if ( vlc_stream_Read( p_stream
, NULL
, 8 ) < 8 )
3675 const uint32_t stoplist
[] = { ATOM_data
, 0 };
3676 return MP4_ReadBoxContainerChildren( p_stream
, p_box
, stoplist
);
3679 /* Chapter support */
3680 static void MP4_FreeBox_chpl( MP4_Box_t
*p_box
)
3682 MP4_Box_data_chpl_t
*p_chpl
= p_box
->data
.p_chpl
;
3683 for( unsigned i
= 0; i
< p_chpl
->i_chapter
; i
++ )
3684 free( p_chpl
->chapter
[i
].psz_name
);
3687 static int MP4_ReadBox_chpl( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3689 MP4_Box_data_chpl_t
*p_chpl
;
3691 VLC_UNUSED(i_dummy
);
3693 MP4_READBOX_ENTER( MP4_Box_data_chpl_t
, MP4_FreeBox_chpl
);
3695 p_chpl
= p_box
->data
.p_chpl
;
3697 MP4_GETVERSIONFLAGS( p_chpl
);
3699 if ( i_read
< 5 || p_chpl
->i_version
!= 0x1 )
3700 MP4_READBOX_EXIT( 0 );
3702 MP4_GET4BYTES( i_dummy
);
3704 MP4_GET1BYTE( p_chpl
->i_chapter
);
3706 for( i
= 0; i
< p_chpl
->i_chapter
; i
++ )
3713 MP4_GET8BYTES( i_start
);
3714 MP4_GET1BYTE( i_len
);
3716 p_chpl
->chapter
[i
].psz_name
= malloc( i_len
+ 1 );
3717 if( !p_chpl
->chapter
[i
].psz_name
)
3718 MP4_READBOX_EXIT( 0 );
3720 i_copy
= __MIN( i_len
, i_read
);
3722 memcpy( p_chpl
->chapter
[i
].psz_name
, p_peek
, i_copy
);
3723 p_chpl
->chapter
[i
].psz_name
[i_copy
] = '\0';
3724 p_chpl
->chapter
[i
].i_start
= i_start
;
3730 if ( i
!= p_chpl
->i_chapter
)
3731 p_chpl
->i_chapter
= i
;
3733 /* Bubble sort by increasing start date */
3736 for( i
= 0; i
< p_chpl
->i_chapter
- 1; i
++ )
3738 if( p_chpl
->chapter
[i
].i_start
> p_chpl
->chapter
[i
+1].i_start
)
3740 char *psz
= p_chpl
->chapter
[i
+1].psz_name
;
3741 int64_t i64
= p_chpl
->chapter
[i
+1].i_start
;
3743 p_chpl
->chapter
[i
+1].psz_name
= p_chpl
->chapter
[i
].psz_name
;
3744 p_chpl
->chapter
[i
+1].i_start
= p_chpl
->chapter
[i
].i_start
;
3746 p_chpl
->chapter
[i
].psz_name
= psz
;
3747 p_chpl
->chapter
[i
].i_start
= i64
;
3756 msg_Dbg( p_stream
, "read box: \"chpl\" %d chapters",
3757 p_chpl
->i_chapter
);
3759 MP4_READBOX_EXIT( 1 );
3762 /* GoPro HiLight tags support */
3763 static void MP4_FreeBox_HMMT( MP4_Box_t
*p_box
)
3765 free( p_box
->data
.p_hmmt
->pi_chapter_start
);
3768 static int MP4_ReadBox_HMMT( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3770 #define MAX_CHAPTER_COUNT 100
3772 MP4_Box_data_HMMT_t
*p_hmmt
;
3773 MP4_READBOX_ENTER( MP4_Box_data_HMMT_t
, MP4_FreeBox_HMMT
);
3776 MP4_READBOX_EXIT( 0 );
3778 p_hmmt
= p_box
->data
.p_hmmt
;
3780 MP4_GET4BYTES( p_hmmt
->i_chapter_count
);
3782 if( p_hmmt
->i_chapter_count
<= 0 )
3784 p_hmmt
->pi_chapter_start
= NULL
;
3785 MP4_READBOX_EXIT( 1 );
3788 if( ( i_read
/ sizeof(uint32_t) ) < p_hmmt
->i_chapter_count
)
3789 MP4_READBOX_EXIT( 0 );
3791 /* Cameras are allowing a maximum of 100 tags */
3792 if( p_hmmt
->i_chapter_count
> MAX_CHAPTER_COUNT
)
3793 p_hmmt
->i_chapter_count
= MAX_CHAPTER_COUNT
;
3795 p_hmmt
->pi_chapter_start
= vlc_alloc( p_hmmt
->i_chapter_count
, sizeof(uint32_t) );
3796 if( p_hmmt
->pi_chapter_start
== NULL
)
3797 MP4_READBOX_EXIT( 0 );
3799 for( uint32_t i
= 0; i
< p_hmmt
->i_chapter_count
; i
++ )
3801 MP4_GET4BYTES( p_hmmt
->pi_chapter_start
[i
] );
3805 msg_Dbg( p_stream
, "read box: \"HMMT\" %d HiLight tags", p_hmmt
->i_chapter_count
);
3808 MP4_READBOX_EXIT( 1 );
3811 static void MP4_FreeBox_TrackReference( MP4_Box_t
*p_box
)
3813 free( p_box
->data
.p_track_reference
->i_track_ID
);
3816 static int MP4_ReadBox_TrackReference( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3820 MP4_READBOX_ENTER( MP4_Box_data_trak_reference_t
, MP4_FreeBox_TrackReference
);
3822 p_box
->data
.p_track_reference
->i_track_ID
= NULL
;
3823 count
= i_read
/ sizeof(uint32_t);
3824 p_box
->data
.p_track_reference
->i_entry_count
= count
;
3825 p_box
->data
.p_track_reference
->i_track_ID
= vlc_alloc( count
,
3827 if( p_box
->data
.p_track_reference
->i_track_ID
== NULL
)
3828 MP4_READBOX_EXIT( 0 );
3830 for( unsigned i
= 0; i
< count
; i
++ )
3832 MP4_GET4BYTES( p_box
->data
.p_track_reference
->i_track_ID
[i
] );
3835 msg_Dbg( p_stream
, "read box: \"chap\" %d references",
3836 p_box
->data
.p_track_reference
->i_entry_count
);
3839 MP4_READBOX_EXIT( 1 );
3842 static int MP4_ReadBox_tref( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3845 ssize_t i_header
= mp4_box_headersize( p_box
);
3846 if( vlc_stream_Read( p_stream
, NULL
, i_header
) != i_header
)
3848 /* read each reference atom with forced handler */
3849 uint64_t i_remain
= p_box
->i_size
- 8;
3850 while ( i_remain
> 8 )
3852 MP4_Box_t
*p_childbox
= MP4_ReadBoxUsing( p_stream
, p_box
,
3853 MP4_ReadBox_TrackReference
);
3854 if( !p_childbox
|| i_remain
< p_childbox
->i_size
)
3856 MP4_BoxFree( p_childbox
);
3860 MP4_BoxAddChild( p_box
, p_childbox
);
3861 i_remain
-= p_childbox
->i_size
;
3864 return MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) ? 0 : 1;
3867 static void MP4_FreeBox_keys( MP4_Box_t
*p_box
)
3869 for( uint32_t i
=0; i
<p_box
->data
.p_keys
->i_entry_count
; i
++ )
3870 free( p_box
->data
.p_keys
->p_entries
[i
].psz_value
);
3871 free( p_box
->data
.p_keys
->p_entries
);
3874 static int MP4_ReadBox_keys( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3876 MP4_READBOX_ENTER( MP4_Box_data_keys_t
, MP4_FreeBox_keys
);
3879 MP4_READBOX_EXIT( 0 );
3882 MP4_GET4BYTES( i_count
); /* reserved + flags */
3884 MP4_READBOX_EXIT( 0 );
3886 MP4_GET4BYTES( i_count
);
3887 p_box
->data
.p_keys
->p_entries
= calloc( i_count
, sizeof(*p_box
->data
.p_keys
->p_entries
) );
3888 if ( !p_box
->data
.p_keys
->p_entries
)
3889 MP4_READBOX_EXIT( 0 );
3890 p_box
->data
.p_keys
->i_entry_count
= i_count
;
3893 for( ; i
< i_count
; i
++ )
3898 MP4_GET4BYTES( i_keysize
);
3899 if ( (i_keysize
< 8) || (i_keysize
- 4 > i_read
) )
3901 MP4_GETFOURCC( p_box
->data
.p_keys
->p_entries
[i
].i_namespace
);
3903 p_box
->data
.p_keys
->p_entries
[i
].psz_value
= malloc( i_keysize
+ 1 );
3904 if ( !p_box
->data
.p_keys
->p_entries
[i
].psz_value
)
3906 memcpy( p_box
->data
.p_keys
->p_entries
[i
].psz_value
, p_peek
, i_keysize
);
3907 p_box
->data
.p_keys
->p_entries
[i
].psz_value
[i_keysize
] = 0;
3908 p_peek
+= i_keysize
;
3909 i_read
-= i_keysize
;
3910 #ifdef MP4_ULTRA_VERBOSE
3911 msg_Dbg( p_stream
, "read box: \"keys\": %u '%s'", i
+ 1,
3912 p_box
->data
.p_keys
->p_entries
[i
].psz_value
);
3916 p_box
->data
.p_keys
->i_entry_count
= i
;
3918 MP4_READBOX_EXIT( 1 );
3921 static int MP4_ReadBox_colr( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3923 MP4_READBOX_ENTER( MP4_Box_data_colr_t
, NULL
);
3924 MP4_GETFOURCC( p_box
->data
.p_colr
->i_type
);
3925 if ( p_box
->data
.p_colr
->i_type
== VLC_FOURCC( 'n', 'c', 'l', 'c' ) ||
3926 p_box
->data
.p_colr
->i_type
== VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3928 MP4_GET2BYTES( p_box
->data
.p_colr
->nclc
.i_primary_idx
);
3929 MP4_GET2BYTES( p_box
->data
.p_colr
->nclc
.i_transfer_function_idx
);
3930 MP4_GET2BYTES( p_box
->data
.p_colr
->nclc
.i_matrix_idx
);
3931 if ( p_box
->data
.p_colr
->i_type
== VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3932 MP4_GET1BYTE( p_box
->data
.p_colr
->nclc
.i_full_range
);
3937 msg_Warn( p_stream
, "Unhandled colr type: %4.4s", (char*)&p_box
->data
.p_colr
->i_type
);
3940 MP4_READBOX_EXIT( 1 );
3943 static int MP4_ReadBox_irot( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3945 MP4_READBOX_ENTER( MP4_Box_data_irot_t
, NULL
);
3946 MP4_GET1BYTE( p_box
->data
.p_irot
->i_ccw_degrees
);
3947 p_box
->data
.p_irot
->i_ccw_degrees
&= 0x03;
3948 p_box
->data
.p_irot
->i_ccw_degrees
*= 90;
3949 MP4_READBOX_EXIT( 1 );
3952 static int MP4_ReadBox_meta( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3954 const uint8_t *p_peek
;
3955 const size_t i_headersize
= mp4_box_headersize( p_box
);
3957 if( p_box
->i_size
< 16 || p_box
->i_size
- i_headersize
< 8 )
3960 /* skip over box header */
3961 if( vlc_stream_Read( p_stream
, NULL
, i_headersize
) < (ssize_t
) i_headersize
)
3964 /* meta content starts with a 4 byte version/flags value (should be 0) */
3965 if( vlc_stream_Peek( p_stream
, &p_peek
, 8 ) < 8 )
3968 if( !memcmp( p_peek
, "\0\0\0", 4 ) ) /* correct header case */
3970 if( vlc_stream_Read( p_stream
, NULL
, 4 ) < 4 )
3973 else if( memcmp( &p_peek
[4], "hdlr", 4 ) ) /* Broken, headerless ones */
3978 /* load child atoms up to the handler (which should be next anyway) */
3979 const uint32_t stoplist
[] = { ATOM_hdlr
, 0 };
3980 if ( !MP4_ReadBoxContainerChildren( p_stream
, p_box
, stoplist
) )
3984 const MP4_Box_t
*p_hdlr
= MP4_BoxGet( p_box
, "hdlr" );
3985 if ( p_hdlr
&& BOXDATA(p_hdlr
) && BOXDATA(p_hdlr
)->i_version
== 0 )
3987 p_box
->i_handler
= BOXDATA(p_hdlr
)->i_handler_type
;
3988 switch( p_box
->i_handler
)
3993 /* then it behaves like a container */
3994 return MP4_ReadBoxContainerChildren( p_stream
, p_box
, NULL
);
3996 /* skip parsing, will be seen as empty container */
4004 static int MP4_ReadBox_iods( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4007 VLC_UNUSED(i_unused
);
4009 MP4_READBOX_ENTER( MP4_Box_data_iods_t
, NULL
);
4010 MP4_GETVERSIONFLAGS( p_box
->data
.p_iods
);
4012 MP4_GET1BYTE( i_unused
); /* tag */
4013 MP4_GET1BYTE( i_unused
); /* length */
4015 MP4_GET2BYTES( p_box
->data
.p_iods
->i_object_descriptor
); /* 10bits, 6 other bits
4016 are used for other flags */
4017 MP4_GET1BYTE( p_box
->data
.p_iods
->i_OD_profile_level
);
4018 MP4_GET1BYTE( p_box
->data
.p_iods
->i_scene_profile_level
);
4019 MP4_GET1BYTE( p_box
->data
.p_iods
->i_audio_profile_level
);
4020 MP4_GET1BYTE( p_box
->data
.p_iods
->i_visual_profile_level
);
4021 MP4_GET1BYTE( p_box
->data
.p_iods
->i_graphics_profile_level
);
4025 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
4026 "visual: %i, graphics: %i",
4027 p_box
->data
.p_iods
->i_object_descriptor
>> 6,
4028 p_box
->data
.p_iods
->i_OD_profile_level
,
4029 p_box
->data
.p_iods
->i_scene_profile_level
,
4030 p_box
->data
.p_iods
->i_audio_profile_level
,
4031 p_box
->data
.p_iods
->i_visual_profile_level
,
4032 p_box
->data
.p_iods
->i_graphics_profile_level
);
4035 MP4_READBOX_EXIT( 1 );
4038 static int MP4_ReadBox_btrt( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4040 MP4_READBOX_ENTER( MP4_Box_data_btrt_t
, NULL
);
4043 MP4_READBOX_EXIT( 0 );
4045 MP4_GET4BYTES( p_box
->data
.p_btrt
->i_buffer_size
);
4046 MP4_GET4BYTES( p_box
->data
.p_btrt
->i_max_bitrate
);
4047 MP4_GET4BYTES( p_box
->data
.p_btrt
->i_avg_bitrate
);
4049 MP4_READBOX_EXIT( 1 );
4052 static int MP4_ReadBox_pasp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4054 MP4_READBOX_ENTER( MP4_Box_data_pasp_t
, NULL
);
4056 MP4_GET4BYTES( p_box
->data
.p_pasp
->i_horizontal_spacing
);
4057 MP4_GET4BYTES( p_box
->data
.p_pasp
->i_vertical_spacing
);
4061 "read box: \"paps\" %dx%d",
4062 p_box
->data
.p_pasp
->i_horizontal_spacing
,
4063 p_box
->data
.p_pasp
->i_vertical_spacing
);
4066 MP4_READBOX_EXIT( 1 );
4069 static int MP4_ReadBox_mehd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4071 MP4_READBOX_ENTER( MP4_Box_data_mehd_t
, NULL
);
4073 MP4_GETVERSIONFLAGS( p_box
->data
.p_mehd
);
4074 if( p_box
->data
.p_mehd
->i_version
== 1 )
4075 MP4_GET8BYTES( p_box
->data
.p_mehd
->i_fragment_duration
);
4076 else /* version == 0 */
4077 MP4_GET4BYTES( p_box
->data
.p_mehd
->i_fragment_duration
);
4081 "read box: \"mehd\" frag dur. %"PRIu64
"",
4082 p_box
->data
.p_mehd
->i_fragment_duration
);
4085 MP4_READBOX_EXIT( 1 );
4088 static int MP4_ReadBox_trex( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4090 MP4_READBOX_ENTER( MP4_Box_data_trex_t
, NULL
);
4091 MP4_GETVERSIONFLAGS( p_box
->data
.p_trex
);
4093 MP4_GET4BYTES( p_box
->data
.p_trex
->i_track_ID
);
4094 MP4_GET4BYTES( p_box
->data
.p_trex
->i_default_sample_description_index
);
4095 MP4_GET4BYTES( p_box
->data
.p_trex
->i_default_sample_duration
);
4096 MP4_GET4BYTES( p_box
->data
.p_trex
->i_default_sample_size
);
4097 MP4_GET4BYTES( p_box
->data
.p_trex
->i_default_sample_flags
);
4101 "read box: \"trex\" trackID: %"PRIu32
"",
4102 p_box
->data
.p_trex
->i_track_ID
);
4105 MP4_READBOX_EXIT( 1 );
4108 static void MP4_FreeBox_sdtp( MP4_Box_t
*p_box
)
4110 free( p_box
->data
.p_sdtp
->p_sample_table
);
4113 static int MP4_ReadBox_sdtp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4115 uint32_t i_sample_count
;
4116 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t
, MP4_FreeBox_sdtp
);
4117 MP4_Box_data_sdtp_t
*p_sdtp
= p_box
->data
.p_sdtp
;
4118 MP4_GETVERSIONFLAGS( p_box
->data
.p_sdtp
);
4119 i_sample_count
= i_read
;
4121 p_sdtp
->p_sample_table
= malloc( i_sample_count
);
4122 if( unlikely(p_sdtp
->p_sample_table
== NULL
) )
4123 MP4_READBOX_EXIT( 0 );
4125 for( uint32_t i
= 0; i
< i_sample_count
; i
++ )
4126 MP4_GET1BYTE( p_sdtp
->p_sample_table
[i
] );
4129 msg_Dbg( p_stream
, "i_sample_count is %"PRIu32
"", i_sample_count
);
4130 if ( i_sample_count
> 3 )
4132 "read box: \"sdtp\" head: %"PRIx8
" %"PRIx8
" %"PRIx8
" %"PRIx8
"",
4133 p_sdtp
->p_sample_table
[0],
4134 p_sdtp
->p_sample_table
[1],
4135 p_sdtp
->p_sample_table
[2],
4136 p_sdtp
->p_sample_table
[3] );
4139 MP4_READBOX_EXIT( 1 );
4142 static int MP4_ReadBox_tsel( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4144 MP4_READBOX_ENTER( MP4_Box_data_tsel_t
, NULL
);
4146 MP4_GET4BYTES( i_version
);
4147 if ( i_version
!= 0 || i_read
< 4 )
4148 MP4_READBOX_EXIT( 0 );
4149 MP4_GET4BYTES( p_box
->data
.p_tsel
->i_switch_group
);
4150 /* ignore list of attributes as es are present before switch */
4151 MP4_READBOX_EXIT( 1 );
4154 static int MP4_ReadBox_mfro( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4156 MP4_READBOX_ENTER( MP4_Box_data_mfro_t
, NULL
);
4158 MP4_GETVERSIONFLAGS( p_box
->data
.p_mfro
);
4159 MP4_GET4BYTES( p_box
->data
.p_mfro
->i_size
);
4163 "read box: \"mfro\" size: %"PRIu32
"",
4164 p_box
->data
.p_mfro
->i_size
);
4167 MP4_READBOX_EXIT( 1 );
4170 static void MP4_FreeBox_tfra( MP4_Box_t
*p_box
)
4172 free( p_box
->data
.p_tfra
->p_time
);
4173 free( p_box
->data
.p_tfra
->p_moof_offset
);
4174 free( p_box
->data
.p_tfra
->p_traf_number
);
4175 free( p_box
->data
.p_tfra
->p_trun_number
);
4176 free( p_box
->data
.p_tfra
->p_sample_number
);
4179 static int MP4_ReadBox_tfra( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4181 #define READ_VARIABLE_LENGTH(lengthvar, p_array) switch (lengthvar)\
4184 MP4_GET1BYTE( p_array[i] );\
4187 MP4_GET2BYTES( *((uint16_t *)&p_array[i*2]) );\
4190 MP4_GET3BYTES( *((uint32_t *)&p_array[i*4]) );\
4193 MP4_GET4BYTES( *((uint32_t *)&p_array[i*4]) );\
4198 #define FIX_VARIABLE_LENGTH(lengthvar) if ( lengthvar == 3 ) lengthvar = 4
4200 uint32_t i_number_of_entries
;
4201 MP4_READBOX_ENTER( MP4_Box_data_tfra_t
, MP4_FreeBox_tfra
);
4202 MP4_Box_data_tfra_t
*p_tfra
= p_box
->data
.p_tfra
;
4203 MP4_GETVERSIONFLAGS( p_box
->data
.p_tfra
);
4204 if ( p_tfra
->i_version
> 1 )
4205 MP4_READBOX_EXIT( 0 );
4206 MP4_GET4BYTES( p_tfra
->i_track_ID
);
4207 uint32_t i_lengths
= 0;
4208 MP4_GET4BYTES( i_lengths
);
4209 MP4_GET4BYTES( p_tfra
->i_number_of_entries
);
4210 i_number_of_entries
= p_tfra
->i_number_of_entries
;
4211 p_tfra
->i_length_size_of_traf_num
= i_lengths
>> 4;
4212 p_tfra
->i_length_size_of_trun_num
= ( i_lengths
& 0x0c ) >> 2;
4213 p_tfra
->i_length_size_of_sample_num
= i_lengths
& 0x03;
4215 size_t size
= 4 + 4*p_tfra
->i_version
; /* size in {4, 8} */
4216 p_tfra
->p_time
= calloc( i_number_of_entries
, size
);
4217 p_tfra
->p_moof_offset
= calloc( i_number_of_entries
, size
);
4219 size
= 1 + p_tfra
->i_length_size_of_traf_num
; /* size in [|1, 4|] */
4220 if ( size
== 3 ) size
++;
4221 p_tfra
->p_traf_number
= calloc( i_number_of_entries
, size
);
4222 size
= 1 + p_tfra
->i_length_size_of_trun_num
;
4223 if ( size
== 3 ) size
++;
4224 p_tfra
->p_trun_number
= calloc( i_number_of_entries
, size
);
4225 size
= 1 + p_tfra
->i_length_size_of_sample_num
;
4226 if ( size
== 3 ) size
++;
4227 p_tfra
->p_sample_number
= calloc( i_number_of_entries
, size
);
4229 if( !p_tfra
->p_time
|| !p_tfra
->p_moof_offset
|| !p_tfra
->p_traf_number
4230 || !p_tfra
->p_trun_number
|| !p_tfra
->p_sample_number
)
4233 unsigned i_fields_length
= 3 + p_tfra
->i_length_size_of_traf_num
4234 + p_tfra
->i_length_size_of_trun_num
4235 + p_tfra
->i_length_size_of_sample_num
;
4238 for( i
= 0; i
< i_number_of_entries
; i
++ )
4241 if( p_tfra
->i_version
== 1 )
4243 if ( i_read
< i_fields_length
+ 16 )
4245 MP4_GET8BYTES( *((uint64_t *)&p_tfra
->p_time
[i
*2]) );
4246 MP4_GET8BYTES( *((uint64_t *)&p_tfra
->p_moof_offset
[i
*2]) );
4250 if ( i_read
< i_fields_length
+ 8 )
4252 MP4_GET4BYTES( p_tfra
->p_time
[i
] );
4253 MP4_GET4BYTES( p_tfra
->p_moof_offset
[i
] );
4256 READ_VARIABLE_LENGTH(p_tfra
->i_length_size_of_traf_num
, p_tfra
->p_traf_number
);
4257 READ_VARIABLE_LENGTH(p_tfra
->i_length_size_of_trun_num
, p_tfra
->p_trun_number
);
4258 READ_VARIABLE_LENGTH(p_tfra
->i_length_size_of_sample_num
, p_tfra
->p_sample_number
);
4260 if ( i
< i_number_of_entries
)
4261 i_number_of_entries
= i
;
4263 FIX_VARIABLE_LENGTH(p_tfra
->i_length_size_of_traf_num
);
4264 FIX_VARIABLE_LENGTH(p_tfra
->i_length_size_of_trun_num
);
4265 FIX_VARIABLE_LENGTH(p_tfra
->i_length_size_of_sample_num
);
4267 #ifdef MP4_ULTRA_VERBOSE
4268 for( i
= 0; i
< i_number_of_entries
; i
++ )
4270 if( p_tfra
->i_version
== 0 )
4272 msg_Dbg( p_stream
, "tfra[%"PRIu32
"] time[%"PRIu32
"]: %"PRIu32
", "
4273 "moof_offset[%"PRIu32
"]: %"PRIu32
"",
4275 i
, p_tfra
->p_time
[i
],
4276 i
, p_tfra
->p_moof_offset
[i
] );
4280 msg_Dbg( p_stream
, "tfra[%"PRIu32
"] time[%"PRIu32
"]: %"PRIu64
", "
4281 "moof_offset[%"PRIu32
"]: %"PRIu64
"",
4283 i
, ((uint64_t *)(p_tfra
->p_time
))[i
],
4284 i
, ((uint64_t *)(p_tfra
->p_moof_offset
))[i
] );
4289 msg_Dbg( p_stream
, "tfra[%"PRIu32
"] %"PRIu32
" entries",
4290 p_tfra
->i_track_ID
, i_number_of_entries
);
4293 MP4_READBOX_EXIT( 1 );
4295 MP4_READBOX_EXIT( 0 );
4297 #undef READ_VARIABLE_LENGTH
4298 #undef FIX_VARIABLE_LENGTH
4301 static int MP4_ReadBox_pnot( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4303 if ( p_box
->i_size
!= 20 )
4305 MP4_READBOX_ENTER( MP4_Box_data_pnot_t
, NULL
);
4306 MP4_GET4BYTES( p_box
->data
.p_pnot
->i_date
);
4308 MP4_GET2BYTES( i_version
);
4309 if ( i_version
!= 0 )
4310 MP4_READBOX_EXIT( 0 );
4311 MP4_GETFOURCC( p_box
->data
.p_pnot
->i_type
);
4312 MP4_GET2BYTES( p_box
->data
.p_pnot
->i_index
);
4313 MP4_READBOX_EXIT( 1 );
4316 static int MP4_ReadBox_SA3D( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4318 MP4_READBOX_ENTER( MP4_Box_data_SA3D_t
, NULL
);
4321 MP4_GET1BYTE( i_version
);
4322 if ( i_version
!= 0 )
4323 MP4_READBOX_EXIT( 0 );
4325 MP4_GET1BYTE( p_box
->data
.p_SA3D
->i_ambisonic_type
);
4326 MP4_GET4BYTES( p_box
->data
.p_SA3D
->i_ambisonic_order
);
4327 MP4_GET1BYTE( p_box
->data
.p_SA3D
->i_ambisonic_channel_ordering
);
4328 MP4_GET1BYTE( p_box
->data
.p_SA3D
->i_ambisonic_normalization
);
4329 MP4_GET4BYTES( p_box
->data
.p_SA3D
->i_num_channels
);
4330 MP4_READBOX_EXIT( 1 );
4333 static void MP4_FreeBox_Reference( MP4_Box_t
*p_box
)
4335 MP4_Box_data_refbox_t
*p_data
= p_box
->data
.p_refbox
;
4336 free( p_data
->p_references
);
4339 static int MP4_ReadBox_Reference( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4341 MP4_READBOX_ENTER( MP4_Box_data_refbox_t
, MP4_FreeBox_Reference
);
4342 MP4_Box_data_refbox_t
*p_data
= p_box
->data
.p_refbox
;
4344 if( p_box
->p_father
->data
.p_iref
->i_flags
== 0 )
4345 MP4_GET2BYTES( p_data
->i_from_item_id
);
4347 MP4_GET4BYTES( p_data
->i_from_item_id
);
4348 MP4_GET2BYTES( p_data
->i_reference_count
);
4349 if( i_read
/ ((p_box
->p_father
->data
.p_iref
->i_flags
== 0 ) ? 2 : 4) <
4350 p_data
->i_reference_count
)
4351 MP4_READBOX_EXIT( 0 );
4353 p_data
->p_references
= malloc( sizeof(*p_data
->p_references
) *
4354 p_data
->i_reference_count
);
4355 if( !p_data
->p_references
)
4356 MP4_READBOX_EXIT( 0 );
4357 for( uint16_t i
=0; i
<p_data
->i_reference_count
; i
++ )
4359 if( p_box
->p_father
->data
.p_iref
->i_flags
== 0 )
4360 MP4_GET2BYTES( p_data
->p_references
[i
].i_to_item_id
);
4362 MP4_GET4BYTES( p_data
->p_references
[i
].i_to_item_id
);
4365 MP4_READBOX_EXIT( 1 );
4368 static int MP4_ReadBox_iref( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4370 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_iref_t
, 12, NULL
);
4371 MP4_Box_data_iref_t
*p_data
= p_box
->data
.p_iref
;
4373 MP4_READBOX_EXIT( 0 );
4375 MP4_GET1BYTE( p_data
->i_version
);
4376 MP4_GET3BYTES( p_data
->i_flags
);
4377 if( p_data
->i_version
> 0 )
4378 MP4_READBOX_EXIT( 0 );
4380 assert( i_read
== 0 );
4383 uint64_t i_remain
= p_box
->i_size
- 12;
4384 while ( i_remain
> 8 )
4386 MP4_Box_t
*p_childbox
= MP4_ReadBoxUsing( p_stream
, p_box
,
4387 MP4_ReadBox_Reference
);
4388 if( !p_childbox
|| i_remain
< p_childbox
->i_size
)
4390 MP4_BoxFree( p_childbox
);
4394 MP4_BoxAddChild( p_box
, p_childbox
);
4395 i_remain
-= p_childbox
->i_size
;
4399 if ( MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) )
4400 MP4_READBOX_EXIT( 0 );
4402 MP4_READBOX_EXIT( 1 );
4405 static void MP4_FreeBox_iloc( MP4_Box_t
*p_box
)
4407 MP4_Box_data_iloc_t
*p_data
= p_box
->data
.p_iloc
;
4408 if( p_data
->p_items
)
4410 for( uint32_t i
=0; i
<p_data
->i_item_count
; i
++ )
4411 free( p_data
->p_items
[i
].p_extents
);
4412 free( p_data
->p_items
);
4416 static int MP4_ReadBox_iloc( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4418 MP4_READBOX_ENTER( MP4_Box_data_iloc_t
, MP4_FreeBox_iloc
);
4419 MP4_Box_data_iloc_t
*p_data
= p_box
->data
.p_iloc
;
4425 MP4_GET1BYTE( i_version
);
4426 MP4_GET3BYTES( i_flags
);
4427 VLC_UNUSED(i_flags
);
4429 MP4_GET1BYTE( p_data
->i_offset_size
);
4430 p_data
->i_length_size
= p_data
->i_offset_size
& 0x0F;
4431 p_data
->i_offset_size
>>= 4;
4432 MP4_GET1BYTE( p_data
->i_base_offset_size
);
4433 if( i_version
== 0 )
4434 p_data
->i_index_size
= 0;
4436 p_data
->i_index_size
= p_data
->i_base_offset_size
& 0x0F;
4437 p_data
->i_base_offset_size
>>= 4;
4439 /* Only accept 0,4,8 */
4440 if( (p_data
->i_offset_size
& 0xF3) || p_data
->i_offset_size
> 8 ||
4441 (p_data
->i_length_size
& 0xF3) || p_data
->i_length_size
> 8 ||
4442 (p_data
->i_base_offset_size
& 0xF3) || p_data
->i_base_offset_size
> 8 ||
4443 (p_data
->i_index_size
& 0xF3) || p_data
->i_index_size
> 8 )
4444 MP4_READBOX_EXIT( 0 );
4447 MP4_GET2BYTES( p_data
->i_item_count
);
4448 else if( i_version
== 2 )
4449 MP4_GET4BYTES( p_data
->i_item_count
);
4451 MP4_READBOX_EXIT( 0 );
4453 if( i_read
/ 6 < p_data
->i_item_count
)
4454 MP4_READBOX_EXIT( 0 );
4456 p_data
->p_items
= malloc( p_data
->i_item_count
* sizeof(p_data
->p_items
[0]) );
4457 if( !p_data
->p_items
)
4458 MP4_READBOX_EXIT( 0 );
4460 for( uint32_t i
=0; i
<p_data
->i_item_count
; i
++ )
4463 MP4_GET2BYTES( p_data
->p_items
[i
].i_item_id
);
4465 MP4_GET4BYTES( p_data
->p_items
[i
].i_item_id
);
4469 MP4_GET2BYTES( i_foo
);
4470 p_data
->p_items
[i
].i_construction_method
= i_foo
& 0x0F;
4472 else p_data
->p_items
[i
].i_construction_method
= 0;
4474 MP4_GET2BYTES( p_data
->p_items
[i
].i_data_reference_index
);
4476 switch( p_data
->i_base_offset_size
)
4478 case 4: MP4_GET4BYTES( p_data
->p_items
[i
].i_base_offset
); break;
4479 case 8: MP4_GET8BYTES( p_data
->p_items
[i
].i_base_offset
); break;
4480 default: p_data
->p_items
[i
].i_base_offset
= 0; break;
4483 MP4_GET2BYTES( p_data
->p_items
[i
].i_extent_count
);
4485 uint64_t i_entrysize
= (( i_version
> 0 ) ? p_data
->i_index_size
: 0) +
4486 p_data
->i_offset_size
+ p_data
->i_length_size
;
4487 if( i_read
/ i_entrysize
< p_data
->p_items
[i
].i_extent_count
)
4489 p_data
->i_item_count
= i
;
4490 MP4_READBOX_EXIT( 0 );
4493 p_data
->p_items
[i
].p_extents
= malloc( p_data
->p_items
[i
].i_extent_count
*
4494 sizeof(p_data
->p_items
[i
].p_extents
[0]) );
4495 for( uint16_t j
=0; j
<p_data
->p_items
[i
].i_extent_count
; j
++ )
4499 switch( p_data
->i_index_size
)
4501 case 4: MP4_GET4BYTES( p_data
->p_items
[i
].p_extents
[j
].i_extent_index
); break;
4502 case 8: MP4_GET8BYTES( p_data
->p_items
[i
].p_extents
[j
].i_extent_index
); break;
4503 default: p_data
->p_items
[i
].p_extents
[j
].i_extent_index
= 0 ; break;
4506 switch( p_data
->i_offset_size
)
4508 case 4: MP4_GET4BYTES( p_data
->p_items
[i
].p_extents
[j
].i_extent_offset
); break;
4509 case 8: MP4_GET8BYTES( p_data
->p_items
[i
].p_extents
[j
].i_extent_offset
); break;
4510 default: p_data
->p_items
[i
].p_extents
[j
].i_extent_offset
= 0; break;
4512 switch( p_data
->i_length_size
)
4514 case 4: MP4_GET4BYTES( p_data
->p_items
[i
].p_extents
[j
].i_extent_length
); break;
4515 case 8: MP4_GET8BYTES( p_data
->p_items
[i
].p_extents
[j
].i_extent_length
); break;
4516 default: p_data
->p_items
[i
].p_extents
[j
].i_extent_length
= 0; break;
4521 MP4_READBOX_EXIT( 1 );
4524 static int MP4_ReadBox_iinf( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4526 const uint8_t *p_versionpeek
;
4527 size_t i_peek
= vlc_stream_Peek( p_stream
, &p_versionpeek
, 9 );
4531 size_t i_header
= 12 + (( p_versionpeek
[8] == 0 ) ? 2 : 4);
4532 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_iinf_t
, i_header
, NULL
);
4533 if( i_read
+ 8 < i_header
)
4534 MP4_READBOX_EXIT( 0 );
4538 MP4_GET1BYTE( i_version
);
4539 MP4_GET3BYTES( i_flags
); VLC_UNUSED(i_flags
);
4541 MP4_READBOX_EXIT( 0 );
4543 if( i_version
== 0 )
4544 MP4_GET2BYTES( p_box
->data
.p_iinf
->i_entry_count
);
4546 MP4_GET4BYTES( p_box
->data
.p_iinf
->i_entry_count
);
4548 assert( i_read
== 0 );
4551 uint64_t i_remain
= p_box
->i_size
- i_header
;
4552 while ( i_remain
> 8 && i
< p_box
->data
.p_iinf
->i_entry_count
)
4554 MP4_Box_t
*p_childbox
= MP4_ReadBox( p_stream
, p_box
);
4555 if( !p_childbox
|| i_remain
< p_childbox
->i_size
)
4557 MP4_BoxFree( p_childbox
);
4558 p_box
->data
.p_iinf
->i_entry_count
= i
;
4562 MP4_BoxAddChild( p_box
, p_childbox
);
4563 i_remain
-= p_childbox
->i_size
;
4567 if ( MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) )
4568 MP4_READBOX_EXIT( 0 );
4570 MP4_READBOX_EXIT( 1 );
4573 static void MP4_FreeBox_infe( MP4_Box_t
*p_box
)
4575 MP4_Box_data_infe_t
*p_data
= p_box
->data
.p_infe
;
4576 free( p_data
->psz_content_encoding
);
4577 free( p_data
->psz_content_type
);
4578 free( p_data
->psz_item_name
);
4579 free( p_data
->psz_item_uri_type
);
4582 static int MP4_ReadBox_infe( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4584 MP4_READBOX_ENTER( MP4_Box_data_infe_t
, MP4_FreeBox_infe
);
4585 MP4_Box_data_infe_t
*p_data
= p_box
->data
.p_infe
;
4588 MP4_GET1BYTE( i_version
);
4589 MP4_GET3BYTES( p_data
->i_flags
);
4591 MP4_READBOX_EXIT( 0 );
4595 MP4_GET2BYTES( p_data
->i_item_id
);
4596 MP4_GET2BYTES( p_data
->i_item_protection_index
);
4597 p_data
->psz_item_name
= mp4_getstringz( &p_peek
, &i_read
);
4600 p_data
->psz_content_type
= mp4_getstringz( &p_peek
, &i_read
);
4602 p_data
->psz_content_encoding
= mp4_getstringz( &p_peek
, &i_read
);
4605 //if( i_version == 1 )
4607 /* extensions, we do not care */
4612 if( i_version
== 2 )
4613 MP4_GET2BYTES( p_data
->i_item_id
);
4615 MP4_GET4BYTES( p_data
->i_item_id
);
4616 MP4_GET2BYTES( p_data
->i_item_protection_index
);
4617 MP4_GETFOURCC( p_data
->item_type
);
4618 p_data
->psz_item_name
= mp4_getstringz( &p_peek
, &i_read
);
4619 if( p_data
->item_type
== VLC_FOURCC('m','i','m','e') )
4621 p_data
->psz_content_type
= mp4_getstringz( &p_peek
, &i_read
);
4623 p_data
->psz_content_encoding
= mp4_getstringz( &p_peek
, &i_read
);
4625 else if( p_data
->item_type
== VLC_FOURCC('u','r','i',' ') )
4627 p_data
->psz_item_uri_type
= mp4_getstringz( &p_peek
, &i_read
);
4631 MP4_READBOX_EXIT( 1 );
4634 static int MP4_ReadBox_pitm( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4636 MP4_READBOX_ENTER( MP4_Box_data_pitm_t
, NULL
);
4637 MP4_Box_data_pitm_t
*p_data
= p_box
->data
.p_pitm
;
4641 MP4_GET1BYTE( i_version
);
4642 MP4_GET3BYTES( i_flags
); VLC_UNUSED(i_flags
);
4644 if( i_version
== 0 )
4645 MP4_GET2BYTES( p_data
->i_item_id
);
4647 MP4_GET4BYTES( p_data
->i_item_id
);
4649 MP4_READBOX_EXIT( 1 );
4652 static int MP4_ReadBox_ispe( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4654 MP4_READBOX_ENTER( MP4_Box_data_ispe_t
, NULL
);
4655 MP4_Box_data_ispe_t
*p_data
= p_box
->data
.p_ispe
;
4659 MP4_GET1BYTE( i_version
);
4660 MP4_GET3BYTES( i_flags
); VLC_UNUSED(i_flags
);
4662 MP4_READBOX_EXIT( 0 );
4664 MP4_GET4BYTES( p_data
->i_width
);
4665 MP4_GET4BYTES( p_data
->i_height
);
4667 MP4_READBOX_EXIT( 1 );
4670 static void MP4_FreeBox_ipma( MP4_Box_t
*p_box
)
4672 MP4_Box_data_ipma_t
*p_data
= p_box
->data
.p_ipma
;
4673 for( uint32_t i
=0; i
<p_data
->i_entry_count
; i
++ )
4674 free( p_data
->p_entries
[i
].p_assocs
);
4675 free( p_data
->p_entries
);
4678 static int MP4_ReadBox_ipma( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4680 MP4_READBOX_ENTER( MP4_Box_data_ipma_t
, MP4_FreeBox_ipma
);
4681 MP4_Box_data_ipma_t
*p_data
= p_box
->data
.p_ipma
;
4685 MP4_GET1BYTE( i_version
);
4686 MP4_GET3BYTES( i_flags
);
4688 MP4_GET4BYTES( p_data
->i_entry_count
);
4689 if( (i_read
/ ((i_version
< 1) ? 3 : 5) < p_data
->i_entry_count
) )
4691 p_data
->i_entry_count
= 0;
4692 MP4_READBOX_EXIT( 0 );
4695 p_data
->p_entries
= malloc( sizeof(p_data
->p_entries
[0]) * p_data
->i_entry_count
);
4696 if( !p_data
->p_entries
)
4698 p_data
->i_entry_count
= 0;
4699 MP4_READBOX_EXIT( 0 );
4702 for( uint32_t i
=0; i
<p_data
->i_entry_count
; i
++ )
4704 if( i_read
< ((i_version
< 1) ? 3 : 5) )
4706 p_data
->i_entry_count
= i
;
4707 MP4_READBOX_EXIT( 0 );
4710 MP4_GET2BYTES( p_data
->p_entries
[i
].i_item_id
);
4712 MP4_GET4BYTES( p_data
->p_entries
[i
].i_item_id
);
4713 MP4_GET1BYTE( p_data
->p_entries
[i
].i_association_count
);
4715 if( i_read
/ ((i_flags
& 0x01) ? 2 : 1) <
4716 p_data
->p_entries
[i
].i_association_count
)
4718 p_data
->i_entry_count
= i
;
4719 MP4_READBOX_EXIT( 0 );
4722 p_data
->p_entries
[i
].p_assocs
=
4723 malloc( sizeof(p_data
->p_entries
[i
].p_assocs
[0]) *
4724 p_data
->p_entries
[i
].i_association_count
);
4725 if( !p_data
->p_entries
[i
].p_assocs
)
4727 p_data
->p_entries
[i
].i_association_count
= 0;
4728 p_data
->i_entry_count
= i
;
4729 MP4_READBOX_EXIT( 0 );
4732 for( uint8_t j
=0; j
<p_data
->p_entries
[i
].i_association_count
; j
++ )
4734 MP4_GET1BYTE( p_data
->p_entries
[i
].p_assocs
[j
].i_property_index
);
4735 p_data
->p_entries
[i
].p_assocs
[j
].b_essential
=
4736 p_data
->p_entries
[i
].p_assocs
[j
].i_property_index
& 0x80;
4737 p_data
->p_entries
[i
].p_assocs
[j
].i_property_index
&= 0x7F;
4738 if( i_flags
& 0x01 )
4740 p_data
->p_entries
[i
].p_assocs
[j
].i_property_index
<<= 8;
4742 MP4_GET1BYTE( i_low
);
4743 p_data
->p_entries
[i
].p_assocs
[j
].i_property_index
|= i_low
;
4748 MP4_READBOX_EXIT( 1 );
4752 static int MP4_ReadBox_default( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4754 if( !p_box
->p_father
)
4760 if MP4_BOX_TYPE_ASCII()
4762 "unknown box type %4.4s (incompletely loaded)",
4763 (char*)&p_box
->i_type
);
4766 "unknown box type c%3.3s (incompletely loaded)",
4767 (char*)&p_box
->i_type
+1 );
4768 p_box
->e_flags
|= BOX_FLAG_INCOMPLETE
;
4773 /**** ------------------------------------------------------------------- ****/
4775 static int MP4_ReadBox_uuid( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4777 if( !CmpUUID( &p_box
->i_uuid
, &TfrfBoxUUID
) )
4778 return MP4_ReadBox_tfrf( p_stream
, p_box
);
4779 if( !CmpUUID( &p_box
->i_uuid
, &TfxdBoxUUID
) )
4780 return MP4_ReadBox_tfxd( p_stream
, p_box
);
4781 if( !CmpUUID( &p_box
->i_uuid
, &XML360BoxUUID
) )
4782 return MP4_ReadBox_XML360( p_stream
, p_box
);
4783 if( !CmpUUID( &p_box
->i_uuid
, &PS3DDSBoxUUID
) && p_box
->i_size
== 28 )
4784 return MP4_ReadBox_Binary( p_stream
, p_box
);
4787 msg_Warn( p_stream
, "Unknown uuid type box: "
4788 "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-"
4789 "%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
4790 p_box
->i_uuid
.b
[0], p_box
->i_uuid
.b
[1], p_box
->i_uuid
.b
[2], p_box
->i_uuid
.b
[3],
4791 p_box
->i_uuid
.b
[4], p_box
->i_uuid
.b
[5], p_box
->i_uuid
.b
[6], p_box
->i_uuid
.b
[7],
4792 p_box
->i_uuid
.b
[8], p_box
->i_uuid
.b
[9], p_box
->i_uuid
.b
[10], p_box
->i_uuid
.b
[11],
4793 p_box
->i_uuid
.b
[12], p_box
->i_uuid
.b
[13], p_box
->i_uuid
.b
[14], p_box
->i_uuid
.b
[15] );
4795 msg_Warn( p_stream
, "Unknown uuid type box" );
4800 /**** ------------------------------------------------------------------- ****/
4801 /**** "Higher level" Functions ****/
4802 /**** ------------------------------------------------------------------- ****/
4807 int (*MP4_ReadBox_function
)( stream_t
*p_stream
, MP4_Box_t
*p_box
);
4808 uint32_t i_parent
; /* set parent to restrict, duplicating if needed; 0 for any */
4809 } MP4_Box_Function
[] =
4812 { ATOM_moov
, MP4_ReadBoxContainer
, 0 },
4813 { ATOM_foov
, MP4_ReadBoxContainer
, 0 },
4814 { ATOM_trak
, MP4_ReadBoxContainer
, ATOM_moov
},
4815 { ATOM_trak
, MP4_ReadBoxContainer
, ATOM_foov
},
4816 { ATOM_mdia
, MP4_ReadBoxContainer
, ATOM_trak
},
4817 { ATOM_moof
, MP4_ReadBoxContainer
, 0 },
4818 { ATOM_minf
, MP4_ReadBoxContainer
, ATOM_mdia
},
4819 { ATOM_stbl
, MP4_ReadBoxContainer
, ATOM_minf
},
4820 { ATOM_dinf
, MP4_ReadBoxContainer
, ATOM_minf
},
4821 { ATOM_dinf
, MP4_ReadBoxContainer
, ATOM_meta
},
4822 { ATOM_edts
, MP4_ReadBoxContainer
, ATOM_trak
},
4823 { ATOM_udta
, MP4_ReadBoxContainer
, 0 },
4824 { ATOM_nmhd
, MP4_ReadBoxContainer
, ATOM_minf
},
4825 { ATOM_hnti
, MP4_ReadBoxContainer
, ATOM_udta
},
4826 { ATOM_rmra
, MP4_ReadBoxContainer
, ATOM_moov
},
4827 { ATOM_rmda
, MP4_ReadBoxContainer
, ATOM_rmra
},
4828 { ATOM_tref
, MP4_ReadBox_tref
, ATOM_trak
},
4829 { ATOM_gmhd
, MP4_ReadBoxContainer
, ATOM_minf
},
4830 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_stsd
},
4831 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_mp4a
}, /* some quicktime mp4a/wave/mp4a.. */
4832 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_WMA2
}, /* flip4mac */
4833 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_in24
},
4834 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_in32
},
4835 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_fl32
},
4836 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_fl64
},
4837 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_QDMC
},
4838 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_QDM2
},
4839 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_XiFL
}, /* XiphQT */
4840 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_XiVs
}, /* XiphQT */
4841 { ATOM_ilst
, MP4_ReadBox_ilst
, ATOM_meta
},
4842 { ATOM_mvex
, MP4_ReadBoxContainer
, ATOM_moov
},
4843 { ATOM_mvex
, MP4_ReadBoxContainer
, ATOM_ftyp
},
4846 { ATOM_ftyp
, MP4_ReadBox_ftyp
, 0 },
4847 { ATOM_styp
, MP4_ReadBox_ftyp
, 0 },
4848 { ATOM_cmov
, MP4_ReadBox_cmov
, 0 },
4849 { ATOM_mvhd
, MP4_ReadBox_mvhd
, ATOM_moov
},
4850 { ATOM_mvhd
, MP4_ReadBox_mvhd
, ATOM_foov
},
4851 { ATOM_tkhd
, MP4_ReadBox_tkhd
, ATOM_trak
},
4852 { ATOM_load
, MP4_ReadBox_load
, ATOM_trak
},
4853 { ATOM_mdhd
, MP4_ReadBox_mdhd
, ATOM_mdia
},
4854 { ATOM_hdlr
, MP4_ReadBox_hdlr
, ATOM_mdia
},
4855 { ATOM_hdlr
, MP4_ReadBox_hdlr
, ATOM_meta
},
4856 { ATOM_hdlr
, MP4_ReadBox_hdlr
, ATOM_minf
},
4857 { ATOM_vmhd
, MP4_ReadBox_vmhd
, ATOM_minf
},
4858 { ATOM_smhd
, MP4_ReadBox_smhd
, ATOM_minf
},
4859 { ATOM_hmhd
, MP4_ReadBox_hmhd
, ATOM_minf
},
4860 { ATOM_alis
, MP4_ReadBoxSkip
, ATOM_dref
},
4861 { ATOM_url
, MP4_ReadBox_url
, 0 },
4862 { ATOM_urn
, MP4_ReadBox_urn
, 0 },
4863 { ATOM_dref
, MP4_ReadBox_LtdContainer
, 0 },
4864 { ATOM_stts
, MP4_ReadBox_stts
, ATOM_stbl
},
4865 { ATOM_ctts
, MP4_ReadBox_ctts
, ATOM_stbl
},
4866 { ATOM_cslg
, MP4_ReadBox_cslg
, ATOM_stbl
},
4867 { ATOM_stsd
, MP4_ReadBox_stsd
, ATOM_stbl
},
4868 { ATOM_stsz
, MP4_ReadBox_stsz
, ATOM_stbl
},
4869 { ATOM_stsc
, MP4_ReadBox_stsc
, ATOM_stbl
},
4870 { ATOM_stco
, MP4_ReadBox_stco_co64
, ATOM_stbl
},
4871 { ATOM_co64
, MP4_ReadBox_stco_co64
, ATOM_stbl
},
4872 { ATOM_stss
, MP4_ReadBox_stss
, ATOM_stbl
},
4873 { ATOM_stsh
, MP4_ReadBox_stsh
, ATOM_stbl
},
4874 { ATOM_stdp
, MP4_ReadBox_stdp
, 0 },
4875 { ATOM_elst
, MP4_ReadBox_elst
, ATOM_edts
},
4876 { ATOM_cprt
, MP4_ReadBox_cprt
, 0 },
4877 { ATOM_esds
, MP4_ReadBox_esds
, ATOM_wave
}, /* mp4a in wave chunk */
4878 { ATOM_esds
, MP4_ReadBox_esds
, ATOM_mp4a
},
4879 { ATOM_esds
, MP4_ReadBox_esds
, ATOM_mp4v
},
4880 { ATOM_esds
, MP4_ReadBox_esds
, ATOM_mp4s
},
4881 { ATOM_dcom
, MP4_ReadBox_dcom
, 0 },
4882 { ATOM_dfLa
, MP4_ReadBox_Binary
, ATOM_fLaC
},
4883 { ATOM_cmvd
, MP4_ReadBox_cmvd
, 0 },
4884 { ATOM_av1C
, MP4_ReadBox_av1C
, ATOM_av01
},
4885 { ATOM_avcC
, MP4_ReadBox_avcC
, ATOM_avc1
},
4886 { ATOM_avcC
, MP4_ReadBox_avcC
, ATOM_avc3
},
4887 { ATOM_hvcC
, MP4_ReadBox_Binary
, 0 },
4888 { ATOM_jpeC
, MP4_ReadBox_Binary
, 0 }, /* heif */
4889 { ATOM_av1C
, MP4_ReadBox_av1C
, ATOM_ipco
}, /* heif */
4890 { ATOM_vpcC
, MP4_ReadBox_vpcC
, ATOM_vp08
},
4891 { ATOM_vpcC
, MP4_ReadBox_vpcC
, ATOM_vp09
},
4892 { ATOM_vpcC
, MP4_ReadBox_vpcC
, ATOM_vp10
},
4893 { ATOM_SmDm
, MP4_ReadBox_SmDm
, ATOM_vpcC
}, /* SMPTE2086 mastering display */
4894 { ATOM_mdcv
, MP4_ReadBox_SmDm
, 0 }, /* */
4895 { ATOM_CoLL
, MP4_ReadBox_CoLL
, ATOM_vpcC
}, /* CEA861-3 light level */
4896 { ATOM_clli
, MP4_ReadBox_CoLL
, 0 }, /* */
4897 { ATOM_dac3
, MP4_ReadBox_dac3
, 0 },
4898 { ATOM_dec3
, MP4_ReadBox_dec3
, 0 },
4899 { ATOM_dvc1
, MP4_ReadBox_dvc1
, ATOM_vc1
},
4900 { ATOM_fiel
, MP4_ReadBox_fiel
, 0 },
4901 { ATOM_glbl
, MP4_ReadBox_Binary
, 0 },
4902 { ATOM_enda
, MP4_ReadBox_enda
, 0 },
4903 { ATOM_iods
, MP4_ReadBox_iods
, 0 },
4904 { ATOM_pasp
, MP4_ReadBox_pasp
, 0 },
4905 { ATOM_btrt
, MP4_ReadBox_btrt
, 0 }, /* codecs bitrate stsd/????/btrt */
4906 { ATOM_keys
, MP4_ReadBox_keys
, ATOM_meta
},
4907 { ATOM_colr
, MP4_ReadBox_colr
, 0 },
4908 { ATOM_irot
, MP4_ReadBox_irot
, 0 }, /* heif */
4911 { ATOM_vCtH
, MP4_ReadBox_Binary
, ATOM_wave
},
4912 { ATOM_vCtC
, MP4_ReadBox_Binary
, ATOM_wave
},
4913 { ATOM_vCtd
, MP4_ReadBox_Binary
, ATOM_wave
},
4914 { ATOM_fCtS
, MP4_ReadBox_Binary
, ATOM_wave
},
4916 /* Samples groups specific information */
4917 { ATOM_sbgp
, MP4_ReadBox_sbgp
, ATOM_stbl
},
4918 { ATOM_sbgp
, MP4_ReadBox_sbgp
, ATOM_traf
},
4919 { ATOM_sgpd
, MP4_ReadBox_sgpd
, ATOM_stbl
},
4920 { ATOM_sgpd
, MP4_ReadBox_sgpd
, ATOM_traf
},
4922 /* Quicktime preview atoms, all at root */
4923 { ATOM_pnot
, MP4_ReadBox_pnot
, 0 },
4924 { ATOM_pict
, MP4_ReadBox_Binary
, 0 },
4925 { ATOM_PICT
, MP4_ReadBox_Binary
, 0 },
4926 /* Other preview atoms */
4927 { ATOM_thum
, MP4_ReadBox_Binary
, 0 },
4929 /* Nothing to do with this box */
4930 { ATOM_mdat
, MP4_ReadBoxSkip
, 0 },
4931 { ATOM_skip
, MP4_ReadBoxSkip
, 0 },
4932 { ATOM_free
, MP4_ReadBoxSkip
, 0 },
4933 { ATOM_wide
, MP4_ReadBoxSkip
, 0 },
4934 { ATOM_binm
, MP4_ReadBoxSkip
, 0 },
4936 /* In sample WebVTT subtitle atoms. No ATOM_wvtt in normal parsing */
4937 { ATOM_vttc
, MP4_ReadBoxContainer
, ATOM_wvtt
},
4938 { ATOM_payl
, MP4_ReadBox_Binary
, ATOM_vttc
},
4940 /* Sound extensions */
4941 { ATOM_chan
, MP4_ReadBox_stsdext_chan
, 0 },
4942 { ATOM_srat
, MP4_ReadBox_stsdext_srat
, 0 },
4943 { ATOM_WMA2
, MP4_ReadBox_WMA2
, ATOM_wave
}, /* flip4mac */
4944 { ATOM_dOps
, MP4_ReadBox_Binary
, ATOM_Opus
},
4945 { ATOM_wfex
, MP4_ReadBox_WMA2
, ATOM_wma
}, /* ismv formatex */
4947 { ATOM_mjqt
, MP4_ReadBox_default
, 0 }, /* found in mjpa/b */
4948 { ATOM_mjht
, MP4_ReadBox_default
, 0 },
4950 { ATOM_strf
, MP4_ReadBox_strf
, ATOM_WVC1
}, /* MS smooth */
4951 { ATOM_strf
, MP4_ReadBox_strf
, ATOM_H264
}, /* MS smooth */
4953 { ATOM_strf
, MP4_ReadBox_strf
, ATOM_WMV3
}, /* flip4mac */
4954 { ATOM_ASF
, MP4_ReadBox_ASF
, ATOM_WMV3
}, /* flip4mac */
4955 { ATOM_ASF
, MP4_ReadBox_ASF
, ATOM_wave
}, /* flip4mac */
4957 { ATOM_hint
, MP4_ReadBox_default
, ATOM_stbl
},
4960 { ATOM_rtp
, MP4_ReadBox_rtp
, ATOM_hnti
},
4961 { ATOM_sdp
, MP4_ReadBox_sdp
, ATOM_hnti
},
4963 /* found in rrtp sample description */
4964 { ATOM_tims
, MP4_ReadBox_tims
, 0 },
4965 { ATOM_tsro
, MP4_ReadBox_tsro
, 0 },
4966 { ATOM_tssy
, MP4_ReadBox_tssy
, 0 },
4968 /* found in rmra/rmda */
4969 { ATOM_rdrf
, MP4_ReadBox_rdrf
, ATOM_rmda
},
4970 { ATOM_rmdr
, MP4_ReadBox_rmdr
, ATOM_rmda
},
4971 { ATOM_rmqu
, MP4_ReadBox_rmqu
, ATOM_rmda
},
4972 { ATOM_rmvc
, MP4_ReadBox_rmvc
, ATOM_rmda
},
4974 { ATOM_sinf
, MP4_ReadBoxContainer
, 0 },
4975 { ATOM_schi
, MP4_ReadBoxContainer
, 0 },
4976 { ATOM_user
, MP4_ReadBox_drms
, 0 },
4977 { ATOM_key
, MP4_ReadBox_drms
, 0 },
4978 { ATOM_iviv
, MP4_ReadBox_drms
, 0 },
4979 { ATOM_priv
, MP4_ReadBox_drms
, 0 },
4980 { ATOM_frma
, MP4_ReadBox_frma
, ATOM_sinf
}, /* and rinf */
4981 { ATOM_frma
, MP4_ReadBox_frma
, ATOM_wave
}, /* flip4mac */
4982 { ATOM_skcr
, MP4_ReadBox_skcr
, 0 },
4984 /* ilst meta tags */
4985 { ATOM_0xa9ART
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4986 { ATOM_0xa9alb
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4987 { ATOM_0xa9cmt
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4988 { ATOM_0xa9com
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4989 { ATOM_0xa9cpy
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4990 { ATOM_0xa9day
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4991 { ATOM_0xa9des
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4992 { ATOM_0xa9enc
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4993 { ATOM_0xa9gen
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4994 { ATOM_0xa9grp
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4995 { ATOM_0xa9lyr
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4996 { ATOM_0xa9nam
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4997 { ATOM_0xa9too
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4998 { ATOM_0xa9trk
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4999 { ATOM_0xa9wrt
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5000 { ATOM_aART
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5001 { ATOM_atID
, MP4_ReadBox_Metadata
, ATOM_ilst
}, /* iTunes */
5002 { ATOM_cnID
, MP4_ReadBox_Metadata
, ATOM_ilst
}, /* iTunes */
5003 { ATOM_covr
, MP4_ReadBoxContainer
, ATOM_ilst
},
5004 { ATOM_desc
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5005 { ATOM_disk
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5006 { ATOM_flvr
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5007 { ATOM_gnre
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5008 { ATOM_rtng
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5009 { ATOM_trkn
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5010 { ATOM_xid_
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5011 { ATOM_gshh
, MP4_ReadBox_Metadata
, ATOM_ilst
}, /* YouTube gs?? */
5012 { ATOM_gspm
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5013 { ATOM_gspu
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5014 { ATOM_gssd
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5015 { ATOM_gsst
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5016 { ATOM_gstd
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5017 { ATOM_ITUN
, MP4_ReadBox_Metadata
, ATOM_ilst
}, /* iTunesInfo */
5018 { ATOM_purl
, MP4_ReadBox_Metadata
, ATOM_ilst
},
5021 { ATOM_0x40PRM
, MP4_ReadBox_Binary
, ATOM_udta
},
5022 { ATOM_0x40PRQ
, MP4_ReadBox_Binary
, ATOM_udta
},
5023 { ATOM_0xa9ART
, MP4_ReadBox_Binary
, ATOM_udta
},
5024 { ATOM_0xa9alb
, MP4_ReadBox_Binary
, ATOM_udta
},
5025 { ATOM_0xa9ard
, MP4_ReadBox_Binary
, ATOM_udta
},
5026 { ATOM_0xa9arg
, MP4_ReadBox_Binary
, ATOM_udta
},
5027 { ATOM_0xa9aut
, MP4_ReadBox_Binary
, ATOM_udta
},
5028 { ATOM_0xa9cak
, MP4_ReadBox_Binary
, ATOM_udta
},
5029 { ATOM_0xa9cmt
, MP4_ReadBox_Binary
, ATOM_udta
},
5030 { ATOM_0xa9con
, MP4_ReadBox_Binary
, ATOM_udta
},
5031 { ATOM_0xa9com
, MP4_ReadBox_Binary
, ATOM_udta
},
5032 { ATOM_0xa9cpy
, MP4_ReadBox_Binary
, ATOM_udta
},
5033 { ATOM_0xa9day
, MP4_ReadBox_Binary
, ATOM_udta
},
5034 { ATOM_0xa9des
, MP4_ReadBox_Binary
, ATOM_udta
},
5035 { ATOM_0xa9dir
, MP4_ReadBox_Binary
, ATOM_udta
},
5036 { ATOM_0xa9dis
, MP4_ReadBox_Binary
, ATOM_udta
},
5037 { ATOM_0xa9dsa
, MP4_ReadBox_Binary
, ATOM_udta
},
5038 { ATOM_0xa9fmt
, MP4_ReadBox_Binary
, ATOM_udta
},
5039 { ATOM_0xa9gen
, MP4_ReadBox_Binary
, ATOM_udta
},
5040 { ATOM_0xa9grp
, MP4_ReadBox_Binary
, ATOM_udta
},
5041 { ATOM_0xa9hst
, MP4_ReadBox_Binary
, ATOM_udta
},
5042 { ATOM_0xa9inf
, MP4_ReadBox_Binary
, ATOM_udta
},
5043 { ATOM_0xa9isr
, MP4_ReadBox_Binary
, ATOM_udta
},
5044 { ATOM_0xa9lab
, MP4_ReadBox_Binary
, ATOM_udta
},
5045 { ATOM_0xa9lal
, MP4_ReadBox_Binary
, ATOM_udta
},
5046 { ATOM_0xa9lnt
, MP4_ReadBox_Binary
, ATOM_udta
},
5047 { ATOM_0xa9lyr
, MP4_ReadBox_Binary
, ATOM_udta
},
5048 { ATOM_0xa9mak
, MP4_ReadBox_Binary
, ATOM_udta
},
5049 { ATOM_0xa9mal
, MP4_ReadBox_Binary
, ATOM_udta
},
5050 { ATOM_0xa9mod
, MP4_ReadBox_Binary
, ATOM_udta
},
5051 { ATOM_0xa9nam
, MP4_ReadBox_Binary
, ATOM_udta
},
5052 { ATOM_0xa9ope
, MP4_ReadBox_Binary
, ATOM_udta
},
5053 { ATOM_0xa9phg
, MP4_ReadBox_Binary
, ATOM_udta
},
5054 { ATOM_0xa9PRD
, MP4_ReadBox_Binary
, ATOM_udta
},
5055 { ATOM_0xa9prd
, MP4_ReadBox_Binary
, ATOM_udta
},
5056 { ATOM_0xa9prf
, MP4_ReadBox_Binary
, ATOM_udta
},
5057 { ATOM_0xa9pub
, MP4_ReadBox_Binary
, ATOM_udta
},
5058 { ATOM_0xa9req
, MP4_ReadBox_Binary
, ATOM_udta
},
5059 { ATOM_0xa9sne
, MP4_ReadBox_Binary
, ATOM_udta
},
5060 { ATOM_0xa9snm
, MP4_ReadBox_Binary
, ATOM_udta
},
5061 { ATOM_0xa9sol
, MP4_ReadBox_Binary
, ATOM_udta
},
5062 { ATOM_0xa9src
, MP4_ReadBox_Binary
, ATOM_udta
},
5063 { ATOM_0xa9st3
, MP4_ReadBox_Binary
, ATOM_udta
},
5064 { ATOM_0xa9swr
, MP4_ReadBox_Binary
, ATOM_udta
},
5065 { ATOM_0xa9thx
, MP4_ReadBox_Binary
, ATOM_udta
},
5066 { ATOM_0xa9too
, MP4_ReadBox_Binary
, ATOM_udta
},
5067 { ATOM_0xa9trk
, MP4_ReadBox_Binary
, ATOM_udta
},
5068 { ATOM_0xa9url
, MP4_ReadBox_Binary
, ATOM_udta
},
5069 { ATOM_0xa9wrn
, MP4_ReadBox_Binary
, ATOM_udta
},
5070 { ATOM_0xa9xpd
, MP4_ReadBox_Binary
, ATOM_udta
},
5071 { ATOM_0xa9xyz
, MP4_ReadBox_Binary
, ATOM_udta
},
5072 { ATOM_chpl
, MP4_ReadBox_chpl
, ATOM_udta
}, /* nero unlabeled chapters list */
5073 { ATOM_MCPS
, MP4_ReadBox_Binary
, ATOM_udta
},
5074 { ATOM_name
, MP4_ReadBox_Binary
, ATOM_udta
},
5075 { ATOM_vndr
, MP4_ReadBox_Binary
, ATOM_udta
},
5076 { ATOM_SDLN
, MP4_ReadBox_Binary
, ATOM_udta
},
5077 { ATOM_HMMT
, MP4_ReadBox_HMMT
, ATOM_udta
}, /* GoPro HiLight tags */
5079 /* udta, non meta */
5080 { ATOM_tsel
, MP4_ReadBox_tsel
, ATOM_udta
},
5082 /* iTunes/Quicktime meta info */
5083 { ATOM_meta
, MP4_ReadBox_meta
, 0 },
5084 { ATOM_ID32
, MP4_ReadBox_Binary
, ATOM_meta
}, /* ID3v2 in 3GPP / ETSI TS 126 244 8.3 */
5085 { ATOM_data
, MP4_ReadBox_data
, 0 }, /* ilst/@too and others, ITUN/data */
5086 { ATOM_mean
, MP4_ReadBox_Binary
, ATOM_ITUN
},
5087 { ATOM_name
, MP4_ReadBox_Binary
, ATOM_ITUN
},
5089 /* found in smoothstreaming */
5090 { ATOM_traf
, MP4_ReadBoxContainer
, ATOM_moof
},
5091 { ATOM_mfra
, MP4_ReadBoxContainer
, 0 },
5092 { ATOM_mfhd
, MP4_ReadBox_mfhd
, ATOM_moof
},
5093 { ATOM_sidx
, MP4_ReadBox_sidx
, 0 },
5094 { ATOM_tfhd
, MP4_ReadBox_tfhd
, ATOM_traf
},
5095 { ATOM_trun
, MP4_ReadBox_trun
, ATOM_traf
},
5096 { ATOM_tfdt
, MP4_ReadBox_tfdt
, ATOM_traf
},
5097 { ATOM_trex
, MP4_ReadBox_trex
, ATOM_mvex
},
5098 { ATOM_mehd
, MP4_ReadBox_mehd
, ATOM_mvex
},
5099 { ATOM_sdtp
, MP4_ReadBox_sdtp
, 0 },
5100 { ATOM_tfra
, MP4_ReadBox_tfra
, ATOM_mfra
},
5101 { ATOM_mfro
, MP4_ReadBox_mfro
, ATOM_mfra
},
5102 { ATOM_uuid
, MP4_ReadBox_uuid
, 0 },
5104 /* spatial/360°/VR */
5105 { ATOM_st3d
, MP4_ReadBox_st3d
, ATOM_avc1
},
5106 { ATOM_st3d
, MP4_ReadBox_st3d
, ATOM_mp4v
},
5107 { ATOM_sv3d
, MP4_ReadBoxContainer
, ATOM_avc1
},
5108 { ATOM_sv3d
, MP4_ReadBoxContainer
, ATOM_mp4v
},
5109 { ATOM_proj
, MP4_ReadBoxContainer
, ATOM_sv3d
},
5110 { ATOM_prhd
, MP4_ReadBox_prhd
, ATOM_proj
},
5111 { ATOM_equi
, MP4_ReadBox_equi
, ATOM_proj
},
5112 { ATOM_cbmp
, MP4_ReadBox_cbmp
, ATOM_proj
},
5115 { ATOM_SA3D
, MP4_ReadBox_SA3D
, 0 },
5117 /* iso4 brand meta references */
5118 { ATOM_idat
, MP4_ReadBoxSkip
, ATOM_meta
},
5119 { ATOM_iloc
, MP4_ReadBox_iloc
, ATOM_meta
},
5120 { ATOM_iinf
, MP4_ReadBox_iinf
, ATOM_meta
},
5121 { ATOM_infe
, MP4_ReadBox_infe
, ATOM_iinf
},
5122 { ATOM_iref
, MP4_ReadBox_iref
, ATOM_meta
},
5123 { ATOM_pitm
, MP4_ReadBox_pitm
, ATOM_meta
},
5125 /* HEIF specific meta references */
5126 { ATOM_iprp
, MP4_ReadBoxContainer
, ATOM_meta
},
5127 { ATOM_ipco
, MP4_ReadBoxContainer
, ATOM_iprp
},
5128 { ATOM_ispe
, MP4_ReadBox_ispe
, ATOM_ipco
},
5129 { ATOM_ipma
, MP4_ReadBox_ipma
, ATOM_iprp
},
5132 { 0, MP4_ReadBox_default
, 0 }
5135 static int MP4_Box_Read_Specific( stream_t
*p_stream
, MP4_Box_t
*p_box
, MP4_Box_t
*p_father
)
5139 for( i_index
= 0; ; i_index
++ )
5141 if ( MP4_Box_Function
[i_index
].i_parent
&&
5142 p_father
&& p_father
->i_type
!= MP4_Box_Function
[i_index
].i_parent
)
5145 if( ( MP4_Box_Function
[i_index
].i_type
== p_box
->i_type
)||
5146 ( MP4_Box_Function
[i_index
].i_type
== 0 ) )
5152 if( !(MP4_Box_Function
[i_index
].MP4_ReadBox_function
)( p_stream
, p_box
) )
5154 return VLC_EGENERIC
;
5160 static MP4_Box_t
*MP4_ReadBoxAllocateCheck( stream_t
*p_stream
, MP4_Box_t
*p_father
)
5162 MP4_Box_t
*p_box
= calloc( 1, sizeof( MP4_Box_t
) ); /* Needed to ensure simple on error handler */
5166 if( !MP4_PeekBoxHeader( p_stream
, p_box
) )
5168 msg_Warn( p_stream
, "cannot read one box" );
5173 if( p_father
&& p_father
->i_size
> 0 &&
5174 p_father
->i_pos
+ p_father
->i_size
< p_box
->i_pos
+ p_box
->i_size
)
5176 msg_Dbg( p_stream
, "out of bound child" );
5181 if( !p_box
->i_size
)
5183 msg_Dbg( p_stream
, "found an empty box (null size)" );
5187 p_box
->p_father
= p_father
;
5192 /*****************************************************************************
5193 * MP4_ReadBoxUsing : parse the actual box and the children using handler
5194 *****************************************************************************/
5195 static MP4_Box_t
*MP4_ReadBoxUsing( stream_t
*p_stream
, MP4_Box_t
*p_father
,
5196 int(*MP4_ReadBox_function
)(stream_t
*, MP4_Box_t
*) )
5198 MP4_Box_t
*p_box
= MP4_ReadBoxAllocateCheck( p_stream
, p_father
);
5202 if( MP4_ReadBox_function( p_stream
, p_box
) != 1 )
5204 uint64_t i_end
= p_box
->i_pos
+ p_box
->i_size
;
5205 MP4_BoxFree( p_box
);
5206 MP4_Seek( p_stream
, i_end
); /* Skip the failed box */
5212 /*****************************************************************************
5213 * MP4_ReadBox : parse the actual box and the children
5214 * XXX : Do not go to the next box
5215 *****************************************************************************/
5216 static MP4_Box_t
*MP4_ReadBox( stream_t
*p_stream
, MP4_Box_t
*p_father
)
5218 MP4_Box_t
*p_box
= MP4_ReadBoxAllocateCheck( p_stream
, p_father
);
5222 if( MP4_Box_Read_Specific( p_stream
, p_box
, p_father
) != VLC_SUCCESS
)
5224 uint64_t i_end
= p_box
->i_pos
+ p_box
->i_size
;
5225 MP4_BoxFree( p_box
);
5226 MP4_Seek( p_stream
, i_end
); /* Skip the failed box */
5232 /*****************************************************************************
5233 * MP4_BoxNew : creates and initializes an arbitrary box
5234 *****************************************************************************/
5235 MP4_Box_t
* MP4_BoxNew( uint32_t i_type
)
5237 MP4_Box_t
*p_box
= calloc( 1, sizeof( MP4_Box_t
) );
5238 if( likely( p_box
!= NULL
) )
5240 p_box
->i_type
= i_type
;
5245 /*****************************************************************************
5246 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
5248 *****************************************************************************/
5249 void MP4_BoxFree( MP4_Box_t
*p_box
)
5256 for( p_child
= p_box
->p_first
; p_child
!= NULL
; )
5260 p_next
= p_child
->p_next
;
5261 MP4_BoxFree( p_child
);
5265 if( p_box
->pf_free
)
5266 p_box
->pf_free( p_box
);
5268 free( p_box
->data
.p_payload
);
5272 MP4_Box_t
*MP4_BoxGetNextChunk( stream_t
*s
)
5274 /* p_chunk is a virtual root container for the moof and mdat boxes */
5275 MP4_Box_t
*p_fakeroot
;
5276 MP4_Box_t
*p_tmp_box
;
5278 p_fakeroot
= MP4_BoxNew( ATOM_root
);
5279 if( unlikely( p_fakeroot
== NULL
) )
5281 p_fakeroot
->i_shortsize
= 1;
5283 const uint32_t stoplist
[] = { ATOM_moov
, ATOM_moof
, 0 };
5284 MP4_ReadBoxContainerChildren( s
, p_fakeroot
, stoplist
);
5286 p_tmp_box
= p_fakeroot
->p_first
;
5287 if( p_tmp_box
== NULL
)
5289 MP4_BoxFree( p_fakeroot
);
5292 else while( p_tmp_box
)
5294 p_fakeroot
->i_size
+= p_tmp_box
->i_size
;
5295 p_tmp_box
= p_tmp_box
->p_next
;
5301 /*****************************************************************************
5302 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
5303 *****************************************************************************
5304 * The first box is a virtual box "root" and is the father for all first
5305 * level boxes for the file, a sort of virtual contener
5306 *****************************************************************************/
5307 MP4_Box_t
*MP4_BoxGetRoot( stream_t
*p_stream
)
5311 MP4_Box_t
*p_vroot
= MP4_BoxNew( ATOM_root
);
5312 if( p_vroot
== NULL
)
5315 p_vroot
->i_shortsize
= 1;
5317 if( vlc_stream_GetSize( p_stream
, &i_size
) == 0 )
5318 p_vroot
->i_size
= i_size
;
5320 /* First get the moov */
5322 const uint32_t stoplist
[] = { ATOM_moov
, ATOM_mdat
, 0 };
5323 i_result
= MP4_ReadBoxContainerChildren( p_stream
, p_vroot
, stoplist
);
5326 /* mdat appeared first */
5327 if( i_result
&& !MP4_BoxGet( p_vroot
, "moov" ) )
5330 if( vlc_stream_Control( p_stream
, STREAM_CAN_SEEK
, &b_seekable
) != VLC_SUCCESS
|| !b_seekable
)
5332 msg_Err( p_stream
, "no moov before mdat and the stream is not seekable" );
5336 /* continue loading up to moov */
5337 const uint32_t stoplist
[] = { ATOM_moov
, 0 };
5338 i_result
= MP4_ReadBoxContainerChildren( p_stream
, p_vroot
, stoplist
);
5344 /* If there is a mvex box, it means fragmented MP4, and we're done */
5345 if( MP4_BoxCount( p_vroot
, "moov/mvex" ) > 0 )
5347 /* Read a bit more atoms as we might have an index between moov and moof */
5348 const uint32_t stoplist
[] = { ATOM_sidx
, 0 };
5349 const uint32_t excludelist
[] = { ATOM_moof
, ATOM_mdat
, 0 };
5350 MP4_ReadBoxContainerChildrenIndexed( p_stream
, p_vroot
, stoplist
, excludelist
, false );
5354 if( vlc_stream_Tell( p_stream
) + 8 < (uint64_t) stream_Size( p_stream
) )
5356 /* Get the rest of the file */
5357 i_result
= MP4_ReadBoxContainerChildren( p_stream
, p_vroot
, NULL
);
5366 /* check if there is a cmov, if so replace
5367 compressed moov by uncompressed one */
5368 if( ( ( p_moov
= MP4_BoxGet( p_vroot
, "moov" ) ) &&
5369 ( p_cmov
= MP4_BoxGet( p_vroot
, "moov/cmov" ) ) ) ||
5370 ( ( p_moov
= MP4_BoxGet( p_vroot
, "foov" ) ) &&
5371 ( p_cmov
= MP4_BoxGet( p_vroot
, "foov/cmov" ) ) ) )
5373 /* rename the compressed moov as a box to skip */
5374 p_moov
->i_type
= ATOM_skip
;
5376 /* get uncompressed p_moov */
5377 p_moov
= p_cmov
->data
.p_cmov
->p_moov
;
5378 p_cmov
->data
.p_cmov
->p_moov
= NULL
;
5380 /* make p_root father of this new moov */
5381 p_moov
->p_father
= p_vroot
;
5383 /* insert this new moov box as first child of p_root */
5384 p_moov
->p_next
= p_vroot
->p_first
;
5385 p_vroot
->p_first
= p_moov
;
5391 MP4_BoxFree( p_vroot
);
5392 MP4_Seek( p_stream
, 0 );
5397 static void MP4_BoxDumpStructure_Internal( stream_t
*s
, const MP4_Box_t
*p_box
,
5398 unsigned int i_level
)
5400 const MP4_Box_t
*p_child
;
5401 uint32_t i_displayedtype
= p_box
->i_type
;
5402 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype
)[0] = 'c';
5406 msg_Dbg( s
, "dumping root Box \"%4.4s\"",
5407 (char*)&i_displayedtype
);
5412 if( i_level
>= (sizeof(str
) - 1)/4 )
5415 memset( str
, ' ', sizeof(str
) );
5416 for( unsigned i
= 0; i
< i_level
; i
++ )
5421 snprintf( &str
[i_level
* 4], sizeof(str
) - 4*i_level
,
5422 "+ %4.4s size %"PRIu64
" offset %"PRIu64
"%s",
5423 (char *)&i_displayedtype
, p_box
->i_size
, p_box
->i_pos
,
5424 p_box
->e_flags
& BOX_FLAG_INCOMPLETE
? " (\?\?\?\?)" : "" );
5425 msg_Dbg( s
, "%s", str
);
5427 p_child
= p_box
->p_first
;
5430 MP4_BoxDumpStructure_Internal( s
, p_child
, i_level
+ 1 );
5431 p_child
= p_child
->p_next
;
5435 void MP4_BoxDumpStructure( stream_t
*s
, const MP4_Box_t
*p_box
)
5437 MP4_BoxDumpStructure_Internal( s
, p_box
, 0 );
5441 /*****************************************************************************
5442 *****************************************************************************
5444 ** High level methods to acces an MP4 file
5446 *****************************************************************************
5447 *****************************************************************************/
5448 static bool get_token( char **ppsz_path
, char **ppsz_token
, int *pi_number
)
5451 if( !*ppsz_path
[0] )
5457 i_len
= strcspn( *ppsz_path
, "/[" );
5458 if( !i_len
&& **ppsz_path
== '/' )
5462 *ppsz_token
= strndup( *ppsz_path
, i_len
);
5463 if( unlikely(!*ppsz_token
) )
5466 *ppsz_path
+= i_len
;
5468 /* Parse the token number token[n] */
5469 if( **ppsz_path
== '[' )
5472 *pi_number
= strtol( *ppsz_path
, NULL
, 10 );
5473 while( **ppsz_path
&& **ppsz_path
!= ']' )
5477 if( **ppsz_path
== ']' )
5487 /* Forward to start of next token */
5488 while( **ppsz_path
== '/' )
5496 static void MP4_BoxGet_Internal( const MP4_Box_t
**pp_result
, const MP4_Box_t
*p_box
,
5497 const char *psz_fmt
, va_list args
)
5501 char *psz_token
= NULL
;
5509 if( vasprintf( &psz_path
, psz_fmt
, args
) == -1 )
5512 if( !psz_path
|| !psz_path
[0] )
5519 // fprintf( stderr, "path:'%s'\n", psz_path );
5520 psz_dup
= psz_path
; /* keep this pointer, as it need to be unallocated */
5525 if( !get_token( &psz_path
, &psz_token
, &i_number
) )
5527 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
5528 // psz_path,psz_token,i_number );
5536 if( !strcmp( psz_token
, "/" ) )
5539 while( p_box
&& p_box
->i_type
!= ATOM_root
)
5541 p_box
= p_box
->p_father
;
5549 if( !strcmp( psz_token
, "." ) )
5554 if( !strcmp( psz_token
, ".." ) )
5556 p_box
= p_box
->p_father
;
5563 if( strlen( psz_token
) == 4 )
5566 i_fourcc
= VLC_FOURCC( psz_token
[0], psz_token
[1],
5567 psz_token
[2], psz_token
[3] );
5568 p_box
= p_box
->p_first
;
5575 if( p_box
->i_type
== i_fourcc
)
5583 p_box
= p_box
->p_next
;
5587 if( *psz_token
== '\0' )
5589 p_box
= p_box
->p_first
;
5601 p_box
= p_box
->p_next
;
5606 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
5622 /*****************************************************************************
5623 * MP4_BoxGet: find a box given a path relative to p_box
5624 *****************************************************************************
5625 * Path Format: . .. / as usual
5626 * [number] to specifie box number ex: trak[12]
5628 * ex: /moov/trak[12]
5630 *****************************************************************************/
5632 MP4_Box_t
*MP4_BoxGet( const MP4_Box_t
*p_box
, const char *psz_fmt
, ... )
5635 const MP4_Box_t
*p_result
;
5637 va_start( args
, psz_fmt
);
5638 MP4_BoxGet_Internal( &p_result
, p_box
, psz_fmt
, args
);
5641 return( (MP4_Box_t
*) p_result
);
5644 /*****************************************************************************
5645 * MP4_BoxCount: count box given a path relative to p_box
5646 *****************************************************************************
5647 * Path Format: . .. / as usual
5648 * [number] to specifie box number ex: trak[12]
5650 * ex: /moov/trak[12]
5652 *****************************************************************************/
5654 unsigned MP4_BoxCount( const MP4_Box_t
*p_box
, const char *psz_fmt
, ... )
5658 const MP4_Box_t
*p_result
, *p_next
;
5660 va_start( args
, psz_fmt
);
5661 MP4_BoxGet_Internal( &p_result
, p_box
, psz_fmt
, args
);
5669 for( p_next
= p_result
->p_next
; p_next
!= NULL
; p_next
= p_next
->p_next
)
5671 if( p_next
->i_type
== p_result
->i_type
)