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"
41 * The input method HAS to be seekable
44 /* convert 16.16 fixed point to floating point */
45 static double conv_fx( int32_t fx
) {
51 /* some functions for mp4 encoding of variables */
53 static void MP4_ConvertDate2Str( char *psz
, uint64_t i_date
, bool b_relative
)
60 /* date begin at 1 jan 1904 */
62 i_date
+= ((INT64_C(1904) * 365) + 17) * 24 * 60 * 60;
64 i_day
= i_date
/ ( 60*60*24);
65 i_hour
= ( i_date
/( 60*60 ) ) % 60;
66 i_min
= ( i_date
/ 60 ) % 60;
68 sprintf( psz
, "%dd-%2.2dh:%2.2dm:%2.2ds", i_day
, i_hour
, i_min
, i_sec
);
72 #define MP4_GET1BYTE( dst ) MP4_GETX_PRIVATE( dst, *p_peek, 1 )
73 #define MP4_GET3BYTES( dst ) MP4_GETX_PRIVATE( dst, Get24bBE(p_peek), 3 )
74 #define MP4_GET4BYTES( dst ) MP4_GETX_PRIVATE( dst, GetDWBE(p_peek), 4 )
75 #define MP4_GET8BYTES( dst ) MP4_GETX_PRIVATE( dst, GetQWBE(p_peek), 8 )
76 #define MP4_GETFOURCC( dst ) MP4_GETX_PRIVATE( dst, \
77 VLC_FOURCC(p_peek[0],p_peek[1],p_peek[2],p_peek[3]), 4)
79 #define MP4_GET2BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetWLE(p_peek), 2 )
80 #define MP4_GET4BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetDWLE(p_peek), 4 )
81 #define MP4_GET8BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetQWLE(p_peek), 8 )
83 #define MP4_GETVERSIONFLAGS( p_void ) \
84 MP4_GET1BYTE( p_void->i_version ); \
85 MP4_GET3BYTES( p_void->i_flags )
87 static char *mp4_getstringz( uint8_t **restrict in
, uint64_t *restrict size
)
89 assert( *size
<= SSIZE_MAX
);
91 size_t len
= strnlen( (const char *)*in
, *size
);
95 char *ret
= malloc( len
+ 1 );
96 if( likely(ret
!= NULL
) )
98 memcpy( ret
, *in
, len
);
107 #define MP4_GETSTRINGZ( p_str ) \
109 (p_str) = mp4_getstringz( &p_peek, &i_read ); \
112 static uint8_t *mp4_readbox_enter_common( stream_t
*s
, MP4_Box_t
*box
,
114 void (*release
)( MP4_Box_t
* ),
117 const size_t headersize
= mp4_box_headersize( box
);
119 if( unlikely(readsize
< headersize
) || unlikely(readsize
> SSIZE_MAX
) )
122 uint8_t *buf
= malloc( readsize
);
123 if( unlikely(buf
== NULL
) )
126 ssize_t val
= vlc_stream_Read( s
, buf
, readsize
);
127 if( (size_t)val
!= readsize
)
129 msg_Warn( s
, "mp4: wanted %"PRIu64
" bytes, got %zd", readsize
, val
);
133 box
->data
.p_payload
= malloc( typesize
);
134 if( unlikely(box
->data
.p_payload
== NULL
) )
137 memset( box
->data
.p_payload
, 0, typesize
);
138 box
->pf_free
= release
;
145 static uint8_t *mp4_readbox_enter_partial( stream_t
*s
, MP4_Box_t
*box
,
147 void (*release
)( MP4_Box_t
* ),
148 uint64_t *restrict readsize
)
150 if( (uint64_t)*readsize
> box
->i_size
)
151 *readsize
= box
->i_size
;
153 return mp4_readbox_enter_common( s
, box
, typesize
, release
, *readsize
);
156 static uint8_t *mp4_readbox_enter( stream_t
*s
, MP4_Box_t
*box
,
158 void (*release
)( MP4_Box_t
* ) )
160 uint64_t readsize
= box
->i_size
;
161 return mp4_readbox_enter_common( s
, box
, typesize
, release
, readsize
);
165 #define MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_TYPE_t, maxread, release ) \
166 uint64_t i_read = (maxread); \
167 uint8_t *p_buff = mp4_readbox_enter_partial( p_stream, p_box, \
168 sizeof( MP4_Box_data_TYPE_t ), release, &i_read ); \
169 if( unlikely(p_buff == NULL) ) \
171 const size_t header_size = mp4_box_headersize( p_box ); \
172 uint8_t *p_peek = p_buff + header_size; \
173 i_read -= header_size
175 #define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t, release ) \
176 uint8_t *p_buff = mp4_readbox_enter( p_stream, p_box, \
177 sizeof(MP4_Box_data_TYPE_t), release ); \
178 if( unlikely(p_buff == NULL) ) \
180 uint64_t i_read = p_box->i_size; \
181 const size_t header_size = mp4_box_headersize( p_box ); \
182 uint8_t *p_peek = p_buff + header_size; \
183 i_read -= header_size
185 #define MP4_READBOX_EXIT( i_code ) \
192 /*****************************************************************************
194 *****************************************************************************/
195 static MP4_Box_t
*MP4_ReadBox( stream_t
*p_stream
, MP4_Box_t
*p_father
);
196 static int MP4_Box_Read_Specific( stream_t
*p_stream
, MP4_Box_t
*p_box
, MP4_Box_t
*p_father
);
197 static void MP4_Box_Clean_Specific( MP4_Box_t
*p_box
);
198 static int MP4_PeekBoxHeader( stream_t
*p_stream
, MP4_Box_t
*p_box
);
200 int MP4_Seek( stream_t
*p_stream
, uint64_t i_pos
)
202 /* Prevent prefetch breakage */
203 uint64_t i_size
= stream_Size( p_stream
);
204 if( i_size
> 0 && i_pos
>= i_size
)
207 bool b_canseek
= false;
208 if ( vlc_stream_Control( p_stream
, STREAM_CAN_SEEK
, &b_canseek
) != VLC_SUCCESS
||
211 /* can seek or don't know */
212 return vlc_stream_Seek( p_stream
, i_pos
);
214 /* obviously can't seek then */
216 int64_t i_current_pos
= vlc_stream_Tell( p_stream
);
217 if ( i_current_pos
< 0 || i_pos
< (uint64_t)i_current_pos
)
220 size_t i_toread
= i_pos
- i_current_pos
;
223 else if( i_toread
> (1<<17) )
226 if( vlc_stream_Read( p_stream
, NULL
, i_toread
) != (ssize_t
)i_toread
)
231 static void MP4_BoxAddChild( MP4_Box_t
*p_parent
, MP4_Box_t
*p_childbox
)
233 if( !p_parent
->p_first
)
234 p_parent
->p_first
= p_childbox
;
236 p_parent
->p_last
->p_next
= p_childbox
;
237 p_parent
->p_last
= p_childbox
;
238 p_childbox
->p_father
= p_parent
;
241 MP4_Box_t
* MP4_BoxExtract( MP4_Box_t
**pp_chain
, uint32_t i_type
)
243 MP4_Box_t
*p_box
= *pp_chain
;
246 if( p_box
->i_type
== i_type
)
248 *pp_chain
= p_box
->p_next
;
249 p_box
->p_next
= NULL
;
252 pp_chain
= &p_box
->p_next
;
253 p_box
= p_box
->p_next
;
258 /* Don't use vlc_stream_Seek directly */
259 #undef vlc_stream_Seek
260 #define vlc_stream_Seek(a,b) __NO__
262 /*****************************************************************************
263 * MP4_PeekBoxHeader : Load only common parameters for all boxes
264 *****************************************************************************
265 * p_box need to be an already allocated MP4_Box_t, and all data
266 * will only be peek not read
268 * RETURN : 0 if it fail, 1 otherwise
269 *****************************************************************************/
270 static int MP4_PeekBoxHeader( stream_t
*p_stream
, MP4_Box_t
*p_box
)
273 const uint8_t *p_peek
;
275 if( ( ( i_read
= vlc_stream_Peek( p_stream
, &p_peek
, 32 ) ) < 8 ) )
279 p_box
->i_pos
= vlc_stream_Tell( p_stream
);
281 p_box
->data
.p_payload
= NULL
;
282 p_box
->p_father
= NULL
;
283 p_box
->p_first
= NULL
;
284 p_box
->p_last
= NULL
;
285 p_box
->p_next
= NULL
;
287 MP4_GET4BYTES( p_box
->i_shortsize
);
288 MP4_GETFOURCC( p_box
->i_type
);
290 /* Now special case */
292 if( p_box
->i_shortsize
== 1 )
296 /* get the true size on 64 bits */
297 MP4_GET8BYTES( p_box
->i_size
);
301 p_box
->i_size
= p_box
->i_shortsize
;
302 /* XXX size of 0 means that the box extends to end of file */
305 if( UINT64_MAX
- p_box
->i_size
< p_box
->i_pos
)
308 if( p_box
->i_type
== ATOM_uuid
)
312 /* get extented type on 16 bytes */
313 GetUUID( &p_box
->i_uuid
, p_peek
);
316 #ifdef MP4_ULTRA_VERBOSE
319 if MP4_BOX_TYPE_ASCII()
320 msg_Dbg( p_stream
, "found Box: %4.4s size %"PRId64
" %"PRId64
,
321 (char*)&p_box
->i_type
, p_box
->i_size
, p_box
->i_pos
);
323 msg_Dbg( p_stream
, "found Box: c%3.3s size %"PRId64
,
324 (char*)&p_box
->i_type
+1, p_box
->i_size
);
331 /*****************************************************************************
332 * MP4_ReadBoxRestricted : Reads box from current position
333 *****************************************************************************
334 * if p_box == NULL, box is invalid or failed, position undefined
335 * on success, position is past read box or EOF
336 *****************************************************************************/
337 static MP4_Box_t
*MP4_ReadBoxRestricted( stream_t
*p_stream
, MP4_Box_t
*p_father
,
338 const uint32_t onlytypes
[], const uint32_t nottypes
[],
339 bool *pb_restrictionhit
)
341 MP4_Box_t peekbox
= { 0 };
342 if ( !MP4_PeekBoxHeader( p_stream
, &peekbox
) )
345 if( peekbox
.i_size
< 8 )
347 msg_Warn( p_stream
, "found an invalid sized %"PRIu64
" box %4.4s @%"PRIu64
,
348 peekbox
.i_size
, (char *) &peekbox
.i_type
, vlc_stream_Tell(p_stream
) );
352 for( size_t i
=0; nottypes
&& nottypes
[i
]; i
++ )
354 if( nottypes
[i
] == peekbox
.i_type
)
356 *pb_restrictionhit
= true;
361 for( size_t i
=0; onlytypes
&& onlytypes
[i
]; i
++ )
363 if( onlytypes
[i
] != peekbox
.i_type
)
365 *pb_restrictionhit
= true;
370 /* if father's size == 0, it means unknown or infinite size,
371 * and we skip the followong check */
372 if( p_father
&& p_father
->i_size
> 0 )
374 const uint64_t i_box_next
= peekbox
.i_size
+ peekbox
.i_pos
;
375 const uint64_t i_father_next
= p_father
->i_size
+ p_father
->i_pos
;
376 /* check if it's within p-father */
377 if( i_box_next
> i_father_next
)
379 msg_Warn( p_stream
, "out of bound child %4.4s", (char*) &peekbox
.i_type
);
380 return NULL
; /* out of bound */
384 /* Everything seems OK */
385 MP4_Box_t
*p_box
= (MP4_Box_t
*) malloc( sizeof(MP4_Box_t
) );
390 const uint64_t i_next
= p_box
->i_pos
+ p_box
->i_size
;
391 p_box
->p_father
= p_father
;
392 if( MP4_Box_Read_Specific( p_stream
, p_box
, p_father
) != VLC_SUCCESS
)
394 msg_Warn( p_stream
, "Failed reading box %4.4s", (char*) &peekbox
.i_type
);
395 MP4_BoxFree( p_box
);
399 /* Check is we consumed all data */
400 if( vlc_stream_Tell( p_stream
) < i_next
)
402 MP4_Seek( p_stream
, i_next
- 1 ); /* since past seek can fail when hitting EOF */
403 MP4_Seek( p_stream
, i_next
);
404 if( vlc_stream_Tell( p_stream
) < i_next
- 1 ) /* Truncated box */
406 msg_Warn( p_stream
, "truncated box %4.4s discarded", (char*) &peekbox
.i_type
);
407 MP4_BoxFree( p_box
);
413 MP4_BoxAddChild( p_father
, p_box
);
418 /*****************************************************************************
419 * For all known box a loader is given,
420 * you have to be already read container header
421 * without container size, file position on exit is unknown
422 *****************************************************************************/
423 static int MP4_ReadBoxContainerChildrenIndexed( stream_t
*p_stream
,
424 MP4_Box_t
*p_container
, const uint32_t stoplist
[],
425 const uint32_t excludelist
[], bool b_indexed
)
427 /* Size of root container is set to 0 when unknown, for exemple
428 * with a DASH stream. In that case, we skip the following check */
429 if( (p_container
->i_size
|| p_container
->p_father
)
430 && ( vlc_stream_Tell( p_stream
) + ((b_indexed
)?16:8) >
431 (uint64_t)(p_container
->i_pos
+ p_container
->i_size
) )
434 /* there is no box to load */
438 uint64_t i_last_pos
= 0; /* used to detect read failure loops */
439 const uint64_t i_end
= p_container
->i_pos
+ p_container
->i_size
;
440 MP4_Box_t
*p_box
= NULL
;
441 bool b_onexclude
= false;
446 if ( p_container
->i_size
)
448 const uint64_t i_tell
= vlc_stream_Tell( p_stream
);
449 if( i_tell
+ ((b_indexed
)?16:8) >= i_end
)
453 uint32_t i_index
= 0;
457 if ( vlc_stream_Read( p_stream
, read
, 8 ) < 8 )
459 i_index
= GetDWBE(&read
[4]);
461 b_onexclude
= false; /* If stopped due exclude list */
462 if( (p_box
= MP4_ReadBoxRestricted( p_stream
, p_container
, NULL
, excludelist
, &b_onexclude
)) )
465 p_box
->i_index
= i_index
;
466 for(size_t i
=0; stoplist
&& stoplist
[i
]; i
++)
468 if( p_box
->i_type
== stoplist
[i
] )
473 const uint64_t i_tell
= vlc_stream_Tell( p_stream
);
474 if ( p_container
->i_size
&& i_tell
>= i_end
)
476 assert( i_tell
== i_end
);
482 /* Continue with next if box fails to load */
483 if( i_last_pos
== i_tell
)
489 } while( b_continue
);
491 /* Always move to end of container */
492 if ( !b_onexclude
&& p_container
->i_size
)
494 const uint64_t i_tell
= vlc_stream_Tell( p_stream
);
495 if ( i_tell
!= i_end
)
496 MP4_Seek( p_stream
, i_end
);
502 int MP4_ReadBoxContainerRestricted( stream_t
*p_stream
, MP4_Box_t
*p_container
,
503 const uint32_t stoplist
[], const uint32_t excludelist
[] )
505 return MP4_ReadBoxContainerChildrenIndexed( p_stream
, p_container
,
506 stoplist
, excludelist
, false );
509 int MP4_ReadBoxContainerChildren( stream_t
*p_stream
, MP4_Box_t
*p_container
,
510 const uint32_t stoplist
[] )
512 return MP4_ReadBoxContainerChildrenIndexed( p_stream
, p_container
,
513 stoplist
, NULL
, false );
516 static void MP4_BoxOffsetUp( MP4_Box_t
*p_box
, uint64_t i_offset
)
520 p_box
->i_pos
+= i_offset
;
521 MP4_BoxOffsetUp( p_box
->p_first
, i_offset
);
522 p_box
= p_box
->p_next
;
526 /* Reads within an already read/in memory box (containers without having to seek) */
527 static int MP4_ReadBoxContainerRawInBox( stream_t
*p_stream
, MP4_Box_t
*p_container
,
528 uint8_t *p_buffer
, uint64_t i_size
, uint64_t i_offset
)
532 stream_t
*p_substream
= vlc_stream_MemoryNew( p_stream
, p_buffer
, i_size
,
536 MP4_Box_t
*p_last
= p_container
->p_last
;
537 MP4_ReadBoxContainerChildren( p_substream
, p_container
, NULL
);
538 vlc_stream_Delete( p_substream
);
542 MP4_Box_t
*p_box
= p_last
? p_last
: p_container
->p_first
;
543 MP4_BoxOffsetUp(p_box
, i_offset
);
549 static int MP4_ReadBoxContainer( stream_t
*p_stream
, MP4_Box_t
*p_container
)
551 if( p_container
->i_size
&&
552 ( p_container
->i_size
<= (size_t)mp4_box_headersize(p_container
) + 8 ) )
554 /* container is empty, 8 stand for the first header in this box */
559 if ( MP4_Seek( p_stream
, p_container
->i_pos
+
560 mp4_box_headersize( p_container
) ) )
562 return MP4_ReadBoxContainerChildren( p_stream
, p_container
, NULL
);
565 static int MP4_ReadBoxSkip( stream_t
*p_stream
, MP4_Box_t
*p_box
)
567 /* XXX sometime moov is hidden in a free box */
568 if( p_box
->p_father
&&
569 p_box
->p_father
->i_type
== ATOM_root
&&
570 p_box
->i_type
== ATOM_free
)
572 const uint8_t *p_peek
;
573 size_t header_size
= mp4_box_headersize( p_box
) + 4;
576 ssize_t i_read
= vlc_stream_Peek( p_stream
, &p_peek
, 44 );
577 if( unlikely(i_read
< (ssize_t
)header_size
) )
580 p_peek
+= header_size
;
581 i_read
-= header_size
;
585 i_fcc
= VLC_FOURCC( p_peek
[0], p_peek
[1], p_peek
[2], p_peek
[3] );
587 if( i_fcc
== ATOM_cmov
|| i_fcc
== ATOM_mvhd
)
589 msg_Warn( p_stream
, "detected moov hidden in a free box ..." );
591 p_box
->i_type
= ATOM_foov
;
592 return MP4_ReadBoxContainer( p_stream
, p_box
);
598 #ifdef MP4_ULTRA_VERBOSE
599 if MP4_BOX_TYPE_ASCII()
600 msg_Dbg( p_stream
, "skip box: \"%4.4s\"", (char*)&p_box
->i_type
);
602 msg_Dbg( p_stream
, "skip box: \"c%3.3s\"", (char*)&p_box
->i_type
+1 );
607 static int MP4_ReadBox_ilst( stream_t
*p_stream
, MP4_Box_t
*p_box
)
609 if( p_box
->i_size
< 8 || vlc_stream_Read( p_stream
, NULL
, 8 ) < 8 )
612 /* Find our handler */
613 if ( !p_box
->i_handler
&& p_box
->p_father
)
615 const MP4_Box_t
*p_sibling
= p_box
->p_father
->p_first
;
618 if ( p_sibling
->i_type
== ATOM_hdlr
&& p_sibling
->data
.p_hdlr
)
620 p_box
->i_handler
= p_sibling
->data
.p_hdlr
->i_handler_type
;
623 p_sibling
= p_sibling
->p_next
;
627 switch( p_box
->i_handler
)
630 msg_Warn( p_stream
, "no handler for ilst atom" );
633 return MP4_ReadBoxContainerChildrenIndexed( p_stream
, p_box
, NULL
, NULL
, true );
635 return MP4_ReadBoxContainerChildren( p_stream
, p_box
, NULL
);
637 msg_Warn( p_stream
, "Unknown ilst handler type '%4.4s'", (char*)&p_box
->i_handler
);
642 static void MP4_FreeBox_ftyp( MP4_Box_t
*p_box
)
644 FREENULL( p_box
->data
.p_ftyp
->i_compatible_brands
);
647 static int MP4_ReadBox_ftyp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
649 MP4_READBOX_ENTER( MP4_Box_data_ftyp_t
, MP4_FreeBox_ftyp
);
651 MP4_GETFOURCC( p_box
->data
.p_ftyp
->i_major_brand
);
652 MP4_GET4BYTES( p_box
->data
.p_ftyp
->i_minor_version
);
654 p_box
->data
.p_ftyp
->i_compatible_brands_count
= i_read
/ 4;
655 if( p_box
->data
.p_ftyp
->i_compatible_brands_count
> 0 )
657 uint32_t *tab
= p_box
->data
.p_ftyp
->i_compatible_brands
=
658 vlc_alloc( p_box
->data
.p_ftyp
->i_compatible_brands_count
,
661 if( unlikely( tab
== NULL
) )
662 MP4_READBOX_EXIT( 0 );
664 for( unsigned i
= 0; i
< p_box
->data
.p_ftyp
->i_compatible_brands_count
; i
++ )
666 MP4_GETFOURCC( tab
[i
] );
671 p_box
->data
.p_ftyp
->i_compatible_brands
= NULL
;
674 MP4_READBOX_EXIT( 1 );
678 static int MP4_ReadBox_mvhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
681 char s_creation_time
[128];
682 char s_modification_time
[128];
683 char s_duration
[128];
685 MP4_READBOX_ENTER( MP4_Box_data_mvhd_t
, NULL
);
687 MP4_GETVERSIONFLAGS( p_box
->data
.p_mvhd
);
689 if( p_box
->data
.p_mvhd
->i_version
)
691 MP4_GET8BYTES( p_box
->data
.p_mvhd
->i_creation_time
);
692 MP4_GET8BYTES( p_box
->data
.p_mvhd
->i_modification_time
);
693 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_timescale
);
694 MP4_GET8BYTES( p_box
->data
.p_mvhd
->i_duration
);
698 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_creation_time
);
699 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_modification_time
);
700 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_timescale
);
701 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_duration
);
703 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_rate
);
704 MP4_GET2BYTES( p_box
->data
.p_mvhd
->i_volume
);
705 MP4_GET2BYTES( p_box
->data
.p_mvhd
->i_reserved1
);
708 for( unsigned i
= 0; i
< 2; i
++ )
710 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_reserved2
[i
] );
712 for( unsigned i
= 0; i
< 9; i
++ )
714 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_matrix
[i
] );
716 for( unsigned i
= 0; i
< 6; i
++ )
718 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_predefined
[i
] );
721 MP4_GET4BYTES( p_box
->data
.p_mvhd
->i_next_track_id
);
725 MP4_ConvertDate2Str( s_creation_time
, p_box
->data
.p_mvhd
->i_creation_time
, false );
726 MP4_ConvertDate2Str( s_modification_time
,
727 p_box
->data
.p_mvhd
->i_modification_time
, false );
728 if( p_box
->data
.p_mvhd
->i_rate
&& p_box
->data
.p_mvhd
->i_timescale
)
730 MP4_ConvertDate2Str( s_duration
, p_box
->data
.p_mvhd
->i_duration
/ p_box
->data
.p_mvhd
->i_timescale
, true );
736 msg_Dbg( p_stream
, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
739 (uint32_t)p_box
->data
.p_mvhd
->i_timescale
,
741 (float)p_box
->data
.p_mvhd
->i_rate
/ (1<<16 ),
742 (float)p_box
->data
.p_mvhd
->i_volume
/ 256 ,
743 (uint32_t)p_box
->data
.p_mvhd
->i_next_track_id
);
745 MP4_READBOX_EXIT( 1 );
748 static int MP4_ReadBox_mfhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
750 MP4_READBOX_ENTER( MP4_Box_data_mfhd_t
, NULL
);
752 MP4_GETVERSIONFLAGS( p_box
->data
.p_mvhd
);
754 MP4_GET4BYTES( p_box
->data
.p_mfhd
->i_sequence_number
);
757 msg_Dbg( p_stream
, "read box: \"mfhd\" sequence number %d",
758 p_box
->data
.p_mfhd
->i_sequence_number
);
760 MP4_READBOX_EXIT( 1 );
763 static int MP4_ReadBox_tfxd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
765 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t
, NULL
);
767 MP4_Box_data_tfxd_t
*p_tfxd_data
= p_box
->data
.p_tfxd
;
768 MP4_GETVERSIONFLAGS( p_tfxd_data
);
770 if( p_tfxd_data
->i_version
== 0 )
772 MP4_GET4BYTES( p_tfxd_data
->i_fragment_abs_time
);
773 MP4_GET4BYTES( p_tfxd_data
->i_fragment_duration
);
777 MP4_GET8BYTES( p_tfxd_data
->i_fragment_abs_time
);
778 MP4_GET8BYTES( p_tfxd_data
->i_fragment_duration
);
782 msg_Dbg( p_stream
, "read box: \"tfxd\" version %d, flags 0x%x, "\
783 "fragment duration %"PRIu64
", fragment abs time %"PRIu64
,
784 p_tfxd_data
->i_version
,
785 p_tfxd_data
->i_flags
,
786 p_tfxd_data
->i_fragment_duration
,
787 p_tfxd_data
->i_fragment_abs_time
791 MP4_READBOX_EXIT( 1 );
794 static void MP4_FreeBox_tfrf( MP4_Box_t
*p_box
)
796 FREENULL( p_box
->data
.p_tfrf
->p_tfrf_data_fields
);
799 static int MP4_ReadBox_tfrf( stream_t
*p_stream
, MP4_Box_t
*p_box
)
801 MP4_READBOX_ENTER( MP4_Box_data_tfxd_t
, MP4_FreeBox_tfrf
);
803 MP4_Box_data_tfrf_t
*p_tfrf_data
= p_box
->data
.p_tfrf
;
804 MP4_GETVERSIONFLAGS( p_tfrf_data
);
806 MP4_GET1BYTE( p_tfrf_data
->i_fragment_count
);
808 p_tfrf_data
->p_tfrf_data_fields
= calloc( p_tfrf_data
->i_fragment_count
,
809 sizeof( TfrfBoxDataFields_t
) );
810 if( !p_tfrf_data
->p_tfrf_data_fields
)
811 MP4_READBOX_EXIT( 0 );
813 for( uint8_t i
= 0; i
< p_tfrf_data
->i_fragment_count
; i
++ )
815 TfrfBoxDataFields_t
*TfrfBoxDataField
= &p_tfrf_data
->p_tfrf_data_fields
[i
];
816 if( p_tfrf_data
->i_version
== 0 )
818 MP4_GET4BYTES( TfrfBoxDataField
->i_fragment_abs_time
);
819 MP4_GET4BYTES( TfrfBoxDataField
->i_fragment_duration
);
823 MP4_GET8BYTES( TfrfBoxDataField
->i_fragment_abs_time
);
824 MP4_GET8BYTES( TfrfBoxDataField
->i_fragment_duration
);
829 msg_Dbg( p_stream
, "read box: \"tfrf\" version %d, flags 0x%x, "\
830 "fragment count %"PRIu8
, p_tfrf_data
->i_version
,
831 p_tfrf_data
->i_flags
, p_tfrf_data
->i_fragment_count
);
833 for( uint8_t i
= 0; i
< p_tfrf_data
->i_fragment_count
; i
++ )
835 TfrfBoxDataFields_t
*TfrfBoxDataField
= &p_tfrf_data
->p_tfrf_data_fields
[i
];
836 msg_Dbg( p_stream
, "\"tfrf\" fragment duration %"PRIu64
", "\
837 "fragment abs time %"PRIu64
,
838 TfrfBoxDataField
->i_fragment_duration
,
839 TfrfBoxDataField
->i_fragment_abs_time
);
844 MP4_READBOX_EXIT( 1 );
847 static int MP4_ReadBox_XML360( stream_t
*p_stream
, MP4_Box_t
*p_box
)
849 MP4_READBOX_ENTER( MP4_Box_data_360_t
, NULL
);
851 MP4_Box_data_360_t
*p_360_data
= p_box
->data
.p_360
;
853 /* Copy the string for pattern matching as it does not end
854 with a '\0' in the stream. */
855 char *psz_rdf
= strndup((char *)p_peek
, i_read
);
857 if ( unlikely( !psz_rdf
) )
858 MP4_READBOX_EXIT( 0 );
860 /* Try to find the string "GSpherical:Spherical" because the v1
861 spherical video spec says the tag must be there. */
863 if ( strcasestr( psz_rdf
, "Gspherical:Spherical" ) )
864 p_360_data
->i_projection_mode
= PROJECTION_MODE_EQUIRECTANGULAR
;
866 /* Try to find the stero mode. */
867 if ( strcasestr( psz_rdf
, "left-right" ) )
869 msg_Dbg( p_stream
, "Left-right stereo mode" );
870 p_360_data
->e_stereo_mode
= XML360_STEREOSCOPIC_LEFT_RIGHT
;
873 if ( strcasestr( psz_rdf
, "top-bottom" ) )
875 msg_Dbg( p_stream
, "Top-bottom stereo mode" );
876 p_360_data
->e_stereo_mode
= XML360_STEREOSCOPIC_TOP_BOTTOM
;
881 MP4_READBOX_EXIT( 1 );
884 static int MP4_ReadBox_st3d( stream_t
*p_stream
, MP4_Box_t
*p_box
)
886 MP4_READBOX_ENTER( MP4_Box_data_st3d_t
, NULL
);
889 MP4_GET1BYTE( i_version
);
890 if ( i_version
!= 0 )
891 MP4_READBOX_EXIT( 0 );
894 VLC_UNUSED( i_flags
);
895 MP4_GET3BYTES( i_flags
);
897 MP4_Box_data_st3d_t
*p_data
= p_box
->data
.p_st3d
;
898 MP4_GET1BYTE( p_data
->i_stereo_mode
);
900 MP4_READBOX_EXIT( 1 );
903 static int MP4_ReadBox_prhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
905 MP4_READBOX_ENTER( MP4_Box_data_prhd_t
, NULL
);
908 MP4_GET1BYTE( i_version
);
910 MP4_READBOX_EXIT( 0 );
913 VLC_UNUSED( i_flags
);
914 MP4_GET3BYTES( i_flags
);
916 MP4_Box_data_prhd_t
*p_data
= p_box
->data
.p_prhd
;
918 MP4_GET4BYTES( fixed16_16
);
919 p_data
->f_pose_yaw_degrees
= (float) fixed16_16
/ 65536.0f
;
921 MP4_GET4BYTES( fixed16_16
);
922 p_data
->f_pose_pitch_degrees
= (float) fixed16_16
/ 65536.0f
;
924 MP4_GET4BYTES( fixed16_16
);
925 p_data
->f_pose_roll_degrees
= (float) fixed16_16
/ 65536.0f
;
927 MP4_READBOX_EXIT( 1 );
930 static int MP4_ReadBox_equi( stream_t
*p_stream
, MP4_Box_t
*p_box
)
932 MP4_READBOX_ENTER( MP4_Box_data_equi_t
, NULL
);
935 MP4_GET1BYTE( i_version
);
937 MP4_READBOX_EXIT( 0 );
940 VLC_UNUSED( i_flags
);
941 MP4_GET3BYTES( i_flags
);
943 MP4_Box_data_equi_t
*p_data
= p_box
->data
.p_equi
;
944 MP4_GET4BYTES( p_data
->i_projection_bounds_top
);
945 MP4_GET4BYTES( p_data
->i_projection_bounds_bottom
);
946 MP4_GET4BYTES( p_data
->i_projection_bounds_left
);
947 MP4_GET4BYTES( p_data
->i_projection_bounds_right
);
949 MP4_READBOX_EXIT( 1 );
952 static int MP4_ReadBox_cbmp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
954 MP4_READBOX_ENTER( MP4_Box_data_cbmp_t
, NULL
);
957 MP4_GET1BYTE( i_version
);
959 MP4_READBOX_EXIT( 0 );
962 VLC_UNUSED( i_flags
);
963 MP4_GET3BYTES( i_flags
);
965 MP4_Box_data_cbmp_t
*p_data
= p_box
->data
.p_cbmp
;
966 MP4_GET4BYTES( p_data
->i_layout
);
967 MP4_GET4BYTES( p_data
->i_padding
);
969 MP4_READBOX_EXIT( 1 );
972 static void MP4_FreeBox_sidx( MP4_Box_t
*p_box
)
974 FREENULL( p_box
->data
.p_sidx
->p_items
);
977 static int MP4_ReadBox_sidx( stream_t
*p_stream
, MP4_Box_t
*p_box
)
979 MP4_READBOX_ENTER( MP4_Box_data_sidx_t
, MP4_FreeBox_sidx
);
981 MP4_Box_data_sidx_t
*p_sidx_data
= p_box
->data
.p_sidx
;
982 MP4_GETVERSIONFLAGS( p_sidx_data
);
984 MP4_GET4BYTES( p_sidx_data
->i_reference_ID
);
985 MP4_GET4BYTES( p_sidx_data
->i_timescale
);
987 if( p_sidx_data
->i_version
== 0 )
989 MP4_GET4BYTES( p_sidx_data
->i_earliest_presentation_time
);
990 MP4_GET4BYTES( p_sidx_data
->i_first_offset
);
994 MP4_GET8BYTES( p_sidx_data
->i_earliest_presentation_time
);
995 MP4_GET8BYTES( p_sidx_data
->i_first_offset
);
998 uint16_t i_reserved
, i_count
;
1000 VLC_UNUSED(i_reserved
);
1001 MP4_GET2BYTES( i_reserved
);
1002 MP4_GET2BYTES( i_count
);
1004 p_sidx_data
->i_reference_count
= i_count
;
1005 p_sidx_data
->p_items
= vlc_alloc( i_count
, sizeof( MP4_Box_sidx_item_t
) );
1006 if( unlikely(p_sidx_data
->p_items
== NULL
) )
1007 MP4_READBOX_EXIT( 0 );
1009 for( unsigned i
= 0; i
< i_count
; i
++ )
1011 MP4_Box_sidx_item_t
*item
= p_sidx_data
->p_items
+ i
;
1014 MP4_GET4BYTES( tmp
);
1015 item
->b_reference_type
= tmp
>> 31;
1016 item
->i_referenced_size
= tmp
& 0x7fffffff;
1017 MP4_GET4BYTES( item
->i_subsegment_duration
);
1019 MP4_GET4BYTES( tmp
);
1020 item
->b_starts_with_SAP
= tmp
>> 31;
1021 item
->i_SAP_type
= (tmp
>> 24) & 0x70;
1022 item
->i_SAP_delta_time
= tmp
& 0xfffffff;
1026 msg_Dbg( p_stream
, "read box: \"sidx\" version %d, flags 0x%x, "\
1027 "ref_ID %"PRIu32
", timescale %"PRIu32
", ref_count %"PRIu16
", "\
1028 "first subsegmt duration %"PRIu32
,
1029 p_sidx_data
->i_version
,
1030 p_sidx_data
->i_flags
,
1031 p_sidx_data
->i_reference_ID
,
1032 p_sidx_data
->i_timescale
,
1033 p_sidx_data
->i_reference_count
,
1034 p_sidx_data
->p_items
[0].i_subsegment_duration
1038 MP4_READBOX_EXIT( 1 );
1041 static int MP4_ReadBox_tfhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1043 MP4_READBOX_ENTER( MP4_Box_data_tfhd_t
, NULL
);
1045 MP4_GETVERSIONFLAGS( p_box
->data
.p_tfhd
);
1047 if( p_box
->data
.p_tfhd
->i_version
!= 0 )
1049 msg_Warn( p_stream
, "'tfhd' box with version != 0. "\
1050 " Don't know what to do with that, please patch" );
1051 MP4_READBOX_EXIT( 0 );
1054 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_track_ID
);
1056 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DURATION_IS_EMPTY
)
1058 msg_Dbg( p_stream
, "'duration-is-empty' flag is present "\
1059 "=> no samples for this time interval." );
1060 p_box
->data
.p_tfhd
->b_empty
= true;
1063 p_box
->data
.p_tfhd
->b_empty
= false;
1065 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
)
1066 MP4_GET8BYTES( p_box
->data
.p_tfhd
->i_base_data_offset
);
1067 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_SAMPLE_DESC_INDEX
)
1068 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_sample_description_index
);
1069 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
1070 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_default_sample_duration
);
1071 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_SIZE
)
1072 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_default_sample_size
);
1073 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_FLAGS
)
1074 MP4_GET4BYTES( p_box
->data
.p_tfhd
->i_default_sample_flags
);
1077 char psz_base
[128] = "\0";
1078 char psz_desc
[128] = "\0";
1079 char psz_dura
[128] = "\0";
1080 char psz_size
[128] = "\0";
1081 char psz_flag
[128] = "\0";
1082 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
)
1083 snprintf(psz_base
, sizeof(psz_base
), "base offset %"PRId64
, p_box
->data
.p_tfhd
->i_base_data_offset
);
1084 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_SAMPLE_DESC_INDEX
)
1085 snprintf(psz_desc
, sizeof(psz_desc
), "sample description index %d", p_box
->data
.p_tfhd
->i_sample_description_index
);
1086 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
1087 snprintf(psz_dura
, sizeof(psz_dura
), "sample duration %d", p_box
->data
.p_tfhd
->i_default_sample_duration
);
1088 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_SIZE
)
1089 snprintf(psz_size
, sizeof(psz_size
), "sample size %d", p_box
->data
.p_tfhd
->i_default_sample_size
);
1090 if( p_box
->data
.p_tfhd
->i_flags
& MP4_TFHD_DFLT_SAMPLE_FLAGS
)
1091 snprintf(psz_flag
, sizeof(psz_flag
), "sample flags 0x%x", p_box
->data
.p_tfhd
->i_default_sample_flags
);
1093 msg_Dbg( p_stream
, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
1094 p_box
->data
.p_tfhd
->i_version
,
1095 p_box
->data
.p_tfhd
->i_flags
,
1096 p_box
->data
.p_tfhd
->i_track_ID
,
1097 psz_base
, psz_desc
, psz_dura
, psz_size
, psz_flag
);
1100 MP4_READBOX_EXIT( 1 );
1103 static void MP4_FreeBox_trun( MP4_Box_t
*p_box
)
1105 FREENULL( p_box
->data
.p_trun
->p_samples
);
1108 static int MP4_ReadBox_trun( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1112 MP4_READBOX_ENTER( MP4_Box_data_trun_t
, MP4_FreeBox_trun
);
1113 MP4_Box_data_trun_t
*p_trun
= p_box
->data
.p_trun
;
1114 MP4_GETVERSIONFLAGS( p_trun
);
1115 MP4_GET4BYTES( count
);
1117 if( p_trun
->i_flags
& MP4_TRUN_DATA_OFFSET
)
1118 MP4_GET4BYTES( p_trun
->i_data_offset
);
1119 if( p_trun
->i_flags
& MP4_TRUN_FIRST_FLAGS
)
1120 MP4_GET4BYTES( p_trun
->i_first_sample_flags
);
1122 uint64_t i_entry_size
=
1123 !!(p_trun
->i_flags
& MP4_TRUN_SAMPLE_DURATION
) +
1124 !!(p_trun
->i_flags
& MP4_TRUN_SAMPLE_SIZE
) +
1125 !!(p_trun
->i_flags
& MP4_TRUN_SAMPLE_FLAGS
) +
1126 !!(p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
);
1128 if( i_entry_size
* 4 * count
> i_read
)
1129 MP4_READBOX_EXIT( 0 );
1131 p_trun
->p_samples
= vlc_alloc( count
, sizeof(MP4_descriptor_trun_sample_t
) );
1132 if ( p_trun
->p_samples
== NULL
)
1133 MP4_READBOX_EXIT( 0 );
1134 p_trun
->i_sample_count
= count
;
1136 for( unsigned int i
= 0; i
< count
; i
++ )
1138 MP4_descriptor_trun_sample_t
*p_sample
= &p_trun
->p_samples
[i
];
1139 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
1140 MP4_GET4BYTES( p_sample
->i_duration
);
1141 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
1142 MP4_GET4BYTES( p_sample
->i_size
);
1143 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_FLAGS
)
1144 MP4_GET4BYTES( p_sample
->i_flags
);
1145 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
)
1146 MP4_GET4BYTES( p_sample
->i_composition_time_offset
.v0
);
1149 #ifdef MP4_ULTRA_VERBOSE
1150 msg_Dbg( p_stream
, "read box: \"trun\" version %u flags 0x%x sample count %"PRIu32
,
1153 p_trun
->i_sample_count
);
1155 for( unsigned int i
= 0; i
< count
; i
++ )
1157 MP4_descriptor_trun_sample_t
*p_sample
= &p_trun
->p_samples
[i
];
1158 msg_Dbg( p_stream
, "read box: \"trun\" sample %4.4u flags 0x%x "\
1159 "duration %"PRIu32
" size %"PRIu32
" composition time offset %"PRIu32
,
1160 i
, p_sample
->i_flags
, p_sample
->i_duration
,
1161 p_sample
->i_size
, p_sample
->i_composition_time_offset
);
1165 MP4_READBOX_EXIT( 1 );
1168 static int MP4_ReadBox_tfdt( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1170 MP4_READBOX_ENTER( MP4_Box_data_tfdt_t
, NULL
);
1172 MP4_READBOX_EXIT( 0 );
1174 MP4_GETVERSIONFLAGS( p_box
->data
.p_tfdt
);
1176 if( p_box
->data
.p_tfdt
->i_version
== 0 )
1177 MP4_GET4BYTES( p_box
->data
.p_tfdt
->i_base_media_decode_time
);
1178 else if( p_box
->data
.p_tfdt
->i_version
== 1 )
1179 MP4_GET8BYTES( p_box
->data
.p_tfdt
->i_base_media_decode_time
);
1181 MP4_READBOX_EXIT( 0 );
1183 MP4_READBOX_EXIT( 1 );
1186 static int MP4_ReadBox_tkhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1189 char s_creation_time
[128];
1190 char s_modification_time
[128];
1191 char s_duration
[128];
1193 MP4_READBOX_ENTER( MP4_Box_data_tkhd_t
, NULL
);
1195 MP4_GETVERSIONFLAGS( p_box
->data
.p_tkhd
);
1197 if( p_box
->data
.p_tkhd
->i_version
)
1199 MP4_GET8BYTES( p_box
->data
.p_tkhd
->i_creation_time
);
1200 MP4_GET8BYTES( p_box
->data
.p_tkhd
->i_modification_time
);
1201 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_track_ID
);
1202 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_reserved
);
1203 MP4_GET8BYTES( p_box
->data
.p_tkhd
->i_duration
);
1207 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_creation_time
);
1208 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_modification_time
);
1209 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_track_ID
);
1210 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_reserved
);
1211 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_duration
);
1214 for( unsigned i
= 0; i
< 2; i
++ )
1216 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_reserved2
[i
] );
1218 MP4_GET2BYTES( p_box
->data
.p_tkhd
->i_layer
);
1219 MP4_GET2BYTES( p_box
->data
.p_tkhd
->i_predefined
);
1220 MP4_GET2BYTES( p_box
->data
.p_tkhd
->i_volume
);
1221 MP4_GET2BYTES( p_box
->data
.p_tkhd
->i_reserved3
);
1223 for( unsigned i
= 0; i
< 9; i
++ )
1225 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_matrix
[i
] );
1227 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_width
);
1228 MP4_GET4BYTES( p_box
->data
.p_tkhd
->i_height
);
1230 double rotation
= 0;//angle in degrees to be rotated clockwise
1231 double scale
[2]; // scale factor; sx = scale[0] , sy = scale[1]
1232 int32_t *matrix
= p_box
->data
.p_tkhd
->i_matrix
;
1234 scale
[0] = sqrt(conv_fx(matrix
[0]) * conv_fx(matrix
[0]) +
1235 conv_fx(matrix
[3]) * conv_fx(matrix
[3]));
1236 scale
[1] = sqrt(conv_fx(matrix
[1]) * conv_fx(matrix
[1]) +
1237 conv_fx(matrix
[4]) * conv_fx(matrix
[4]));
1239 if( likely(scale
[0] > 0 && scale
[1] > 0) )
1241 rotation
= atan2(conv_fx(matrix
[1]) / scale
[1],
1242 conv_fx(matrix
[0]) / scale
[0]) * 180 / M_PI
;
1247 p_box
->data
.p_tkhd
->f_rotation
= rotation
;
1250 double translate
[2];// amount to translate; tx = translate[0] , ty = translate[1]
1252 translate
[0] = conv_fx(matrix
[6]);
1253 translate
[1] = conv_fx(matrix
[7]);
1255 MP4_ConvertDate2Str( s_creation_time
, p_box
->data
.p_mvhd
->i_creation_time
, false );
1256 MP4_ConvertDate2Str( s_modification_time
, p_box
->data
.p_mvhd
->i_modification_time
, false );
1257 MP4_ConvertDate2Str( s_duration
, p_box
->data
.p_mvhd
->i_duration
, true );
1259 msg_Dbg( p_stream
, "read box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f rotation %f scaleX %f scaleY %f translateX %f translateY %f width %f height %f. "
1260 "Matrix: %i %i %i %i %i %i %i %i %i",
1262 s_modification_time
,
1264 p_box
->data
.p_tkhd
->i_track_ID
,
1265 p_box
->data
.p_tkhd
->i_layer
,
1266 (float)p_box
->data
.p_tkhd
->i_volume
/ 256 ,
1272 (float)p_box
->data
.p_tkhd
->i_width
/ BLOCK16x16
,
1273 (float)p_box
->data
.p_tkhd
->i_height
/ BLOCK16x16
,
1274 p_box
->data
.p_tkhd
->i_matrix
[0],
1275 p_box
->data
.p_tkhd
->i_matrix
[1],
1276 p_box
->data
.p_tkhd
->i_matrix
[2],
1277 p_box
->data
.p_tkhd
->i_matrix
[3],
1278 p_box
->data
.p_tkhd
->i_matrix
[4],
1279 p_box
->data
.p_tkhd
->i_matrix
[5],
1280 p_box
->data
.p_tkhd
->i_matrix
[6],
1281 p_box
->data
.p_tkhd
->i_matrix
[7],
1282 p_box
->data
.p_tkhd
->i_matrix
[8] );
1284 MP4_READBOX_EXIT( 1 );
1287 static int MP4_ReadBox_load( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1289 if ( p_box
->i_size
!= 24 )
1291 MP4_READBOX_ENTER( MP4_Box_data_load_t
, NULL
);
1292 MP4_GET4BYTES( p_box
->data
.p_load
->i_start_time
);
1293 MP4_GET4BYTES( p_box
->data
.p_load
->i_duration
);
1294 MP4_GET4BYTES( p_box
->data
.p_load
->i_flags
);
1295 MP4_GET4BYTES( p_box
->data
.p_load
->i_hints
);
1296 MP4_READBOX_EXIT( 1 );
1299 static int MP4_ReadBox_mdhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1301 uint16_t i_language
;
1303 char s_creation_time
[128];
1304 char s_modification_time
[128];
1305 char s_duration
[128];
1307 MP4_READBOX_ENTER( MP4_Box_data_mdhd_t
, NULL
);
1309 MP4_GETVERSIONFLAGS( p_box
->data
.p_mdhd
);
1311 if( p_box
->data
.p_mdhd
->i_version
)
1313 MP4_GET8BYTES( p_box
->data
.p_mdhd
->i_creation_time
);
1314 MP4_GET8BYTES( p_box
->data
.p_mdhd
->i_modification_time
);
1315 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_timescale
);
1316 MP4_GET8BYTES( p_box
->data
.p_mdhd
->i_duration
);
1320 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_creation_time
);
1321 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_modification_time
);
1322 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_timescale
);
1323 MP4_GET4BYTES( p_box
->data
.p_mdhd
->i_duration
);
1326 MP4_GET2BYTES( i_language
);
1327 decodeQtLanguageCode( i_language
, p_box
->data
.p_mdhd
->rgs_language
,
1328 &p_box
->data
.p_mdhd
->b_mac_encoding
);
1330 MP4_GET2BYTES( p_box
->data
.p_mdhd
->i_quality
);
1333 MP4_ConvertDate2Str( s_creation_time
, p_box
->data
.p_mdhd
->i_creation_time
, false );
1334 MP4_ConvertDate2Str( s_modification_time
, p_box
->data
.p_mdhd
->i_modification_time
, false );
1335 MP4_ConvertDate2Str( s_duration
, p_box
->data
.p_mdhd
->i_duration
, true );
1336 msg_Dbg( p_stream
, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %3.3s",
1338 s_modification_time
,
1339 (uint32_t)p_box
->data
.p_mdhd
->i_timescale
,
1341 (char*) &p_box
->data
.p_mdhd
->rgs_language
);
1343 MP4_READBOX_EXIT( 1 );
1346 static void MP4_FreeBox_hdlr( MP4_Box_t
*p_box
)
1348 FREENULL( p_box
->data
.p_hdlr
->psz_name
);
1351 static int MP4_ReadBox_hdlr( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1354 VLC_UNUSED(i_reserved
);
1356 MP4_READBOX_ENTER( MP4_Box_data_hdlr_t
, MP4_FreeBox_hdlr
);
1358 MP4_GETVERSIONFLAGS( p_box
->data
.p_hdlr
);
1360 MP4_GETFOURCC( p_box
->data
.p_hdlr
->i_predefined
);
1361 MP4_GETFOURCC( p_box
->data
.p_hdlr
->i_handler_type
);
1363 MP4_GET4BYTES( i_reserved
);
1364 MP4_GET4BYTES( i_reserved
);
1365 MP4_GET4BYTES( i_reserved
);
1366 p_box
->data
.p_hdlr
->psz_name
= NULL
;
1370 uint8_t *psz
= p_box
->data
.p_hdlr
->psz_name
= malloc( i_read
+ 1 );
1371 if( unlikely( psz
== NULL
) )
1372 MP4_READBOX_EXIT( 0 );
1374 /* Yes, I love .mp4 :( */
1375 if( p_box
->data
.p_hdlr
->i_predefined
== VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
1380 MP4_GET1BYTE( i_len
);
1381 i_copy
= __MIN( i_read
, i_len
);
1383 memcpy( psz
, p_peek
, i_copy
);
1384 p_box
->data
.p_hdlr
->psz_name
[i_copy
] = '\0';
1388 memcpy( psz
, p_peek
, i_read
);
1389 p_box
->data
.p_hdlr
->psz_name
[i_read
] = '\0';
1394 msg_Dbg( p_stream
, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
1395 (char*)&p_box
->data
.p_hdlr
->i_handler_type
,
1396 p_box
->data
.p_hdlr
->psz_name
);
1399 MP4_READBOX_EXIT( 1 );
1402 static int MP4_ReadBox_vmhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1404 MP4_READBOX_ENTER( MP4_Box_data_vmhd_t
, NULL
);
1406 MP4_GETVERSIONFLAGS( p_box
->data
.p_vmhd
);
1408 MP4_GET2BYTES( p_box
->data
.p_vmhd
->i_graphics_mode
);
1409 for( unsigned i
= 0; i
< 3; i
++ )
1411 MP4_GET2BYTES( p_box
->data
.p_vmhd
->i_opcolor
[i
] );
1415 msg_Dbg( p_stream
, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
1416 p_box
->data
.p_vmhd
->i_graphics_mode
,
1417 p_box
->data
.p_vmhd
->i_opcolor
[0],
1418 p_box
->data
.p_vmhd
->i_opcolor
[1],
1419 p_box
->data
.p_vmhd
->i_opcolor
[2] );
1421 MP4_READBOX_EXIT( 1 );
1424 static int MP4_ReadBox_smhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1426 MP4_READBOX_ENTER( MP4_Box_data_smhd_t
, NULL
);
1428 MP4_GETVERSIONFLAGS( p_box
->data
.p_smhd
);
1432 MP4_GET2BYTES( p_box
->data
.p_smhd
->i_balance
);
1434 MP4_GET2BYTES( p_box
->data
.p_smhd
->i_reserved
);
1437 msg_Dbg( p_stream
, "read box: \"smhd\" balance %f",
1438 (float)p_box
->data
.p_smhd
->i_balance
/ 256 );
1440 MP4_READBOX_EXIT( 1 );
1444 static int MP4_ReadBox_hmhd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1446 MP4_READBOX_ENTER( MP4_Box_data_hmhd_t
, NULL
);
1448 MP4_GETVERSIONFLAGS( p_box
->data
.p_hmhd
);
1450 MP4_GET2BYTES( p_box
->data
.p_hmhd
->i_max_PDU_size
);
1451 MP4_GET2BYTES( p_box
->data
.p_hmhd
->i_avg_PDU_size
);
1453 MP4_GET4BYTES( p_box
->data
.p_hmhd
->i_max_bitrate
);
1454 MP4_GET4BYTES( p_box
->data
.p_hmhd
->i_avg_bitrate
);
1456 MP4_GET4BYTES( p_box
->data
.p_hmhd
->i_reserved
);
1459 msg_Dbg( p_stream
, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1460 p_box
->data
.p_hmhd
->i_max_PDU_size
,
1461 p_box
->data
.p_hmhd
->i_avg_PDU_size
,
1462 p_box
->data
.p_hmhd
->i_max_bitrate
,
1463 p_box
->data
.p_hmhd
->i_avg_bitrate
);
1465 MP4_READBOX_EXIT( 1 );
1468 static void MP4_FreeBox_url( MP4_Box_t
*p_box
)
1470 FREENULL( p_box
->data
.p_url
->psz_location
);
1473 static int MP4_ReadBox_url( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1475 MP4_READBOX_ENTER( MP4_Box_data_url_t
, MP4_FreeBox_url
);
1477 MP4_GETVERSIONFLAGS( p_box
->data
.p_url
);
1478 MP4_GETSTRINGZ( p_box
->data
.p_url
->psz_location
);
1481 msg_Dbg( p_stream
, "read box: \"url\" url: %s",
1482 p_box
->data
.p_url
->psz_location
);
1485 MP4_READBOX_EXIT( 1 );
1488 static void MP4_FreeBox_urn( MP4_Box_t
*p_box
)
1490 FREENULL( p_box
->data
.p_urn
->psz_name
);
1491 FREENULL( p_box
->data
.p_urn
->psz_location
);
1494 static int MP4_ReadBox_urn( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1496 MP4_READBOX_ENTER( MP4_Box_data_urn_t
, MP4_FreeBox_urn
);
1498 MP4_GETVERSIONFLAGS( p_box
->data
.p_urn
);
1500 MP4_GETSTRINGZ( p_box
->data
.p_urn
->psz_name
);
1501 MP4_GETSTRINGZ( p_box
->data
.p_urn
->psz_location
);
1504 msg_Dbg( p_stream
, "read box: \"urn\" name %s location %s",
1505 p_box
->data
.p_urn
->psz_name
,
1506 p_box
->data
.p_urn
->psz_location
);
1508 MP4_READBOX_EXIT( 1 );
1511 static int MP4_ReadBox_LtdContainer( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1513 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_lcont_t
, 16, NULL
);
1515 MP4_READBOX_EXIT( 0 );
1517 MP4_GETVERSIONFLAGS( p_box
->data
.p_lcont
);
1518 if( p_box
->data
.p_lcont
->i_version
!= 0 )
1519 MP4_READBOX_EXIT( 0 );
1520 MP4_GET4BYTES( p_box
->data
.p_lcont
->i_entry_count
);
1522 uint32_t i_entry
= 0;
1523 i_read
= p_box
->i_size
- 16;
1524 while (i_read
> 8 && i_entry
< p_box
->data
.p_lcont
->i_entry_count
)
1526 MP4_Box_t
*p_childbox
= MP4_ReadBox( p_stream
, p_box
);
1529 MP4_BoxAddChild( p_box
, p_childbox
);
1531 i_read
-= p_childbox
->i_size
;
1534 if (i_entry
!= p_box
->data
.p_lcont
->i_entry_count
)
1535 p_box
->data
.p_lcont
->i_entry_count
= i_entry
;
1538 msg_Dbg( p_stream
, "read box: \"%4.4s\" entry-count %d", (char *)&p_box
->i_type
,
1539 p_box
->data
.p_lcont
->i_entry_count
);
1543 if ( MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) )
1544 MP4_READBOX_EXIT( 0 );
1546 MP4_READBOX_EXIT( 1 );
1549 static void MP4_FreeBox_stts( MP4_Box_t
*p_box
)
1551 FREENULL( p_box
->data
.p_stts
->pi_sample_count
);
1552 FREENULL( p_box
->data
.p_stts
->pi_sample_delta
);
1555 static int MP4_ReadBox_stts( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1559 MP4_READBOX_ENTER( MP4_Box_data_stts_t
, MP4_FreeBox_stts
);
1561 MP4_GETVERSIONFLAGS( p_box
->data
.p_stts
);
1562 MP4_GET4BYTES( count
);
1564 if( UINT64_C(8) * count
> i_read
)
1566 /*count = i_read / 8;*/
1567 MP4_READBOX_EXIT( 0 );
1570 p_box
->data
.p_stts
->pi_sample_count
= vlc_alloc( count
, sizeof(uint32_t) );
1571 p_box
->data
.p_stts
->pi_sample_delta
= vlc_alloc( count
, sizeof(int32_t) );
1572 p_box
->data
.p_stts
->i_entry_count
= count
;
1574 if( p_box
->data
.p_stts
->pi_sample_count
== NULL
1575 || p_box
->data
.p_stts
->pi_sample_delta
== NULL
)
1577 MP4_READBOX_EXIT( 0 );
1580 for( uint32_t i
= 0; i
< count
; i
++ )
1582 MP4_GET4BYTES( p_box
->data
.p_stts
->pi_sample_count
[i
] );
1583 MP4_GET4BYTES( p_box
->data
.p_stts
->pi_sample_delta
[i
] );
1587 msg_Dbg( p_stream
, "read box: \"stts\" entry-count %d",
1588 p_box
->data
.p_stts
->i_entry_count
);
1591 MP4_READBOX_EXIT( 1 );
1595 static void MP4_FreeBox_ctts( MP4_Box_t
*p_box
)
1597 FREENULL( p_box
->data
.p_ctts
->pi_sample_count
);
1598 FREENULL( p_box
->data
.p_ctts
->pi_sample_offset
);
1601 static int MP4_ReadBox_ctts( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1605 MP4_READBOX_ENTER( MP4_Box_data_ctts_t
, MP4_FreeBox_ctts
);
1607 MP4_GETVERSIONFLAGS( p_box
->data
.p_ctts
);
1608 MP4_GET4BYTES( count
);
1610 if( UINT64_C(8) * count
> i_read
)
1611 MP4_READBOX_EXIT( 0 );
1613 p_box
->data
.p_ctts
->pi_sample_count
= vlc_alloc( count
, sizeof(uint32_t) );
1614 p_box
->data
.p_ctts
->pi_sample_offset
= vlc_alloc( count
, sizeof(int32_t) );
1615 if( unlikely(p_box
->data
.p_ctts
->pi_sample_count
== NULL
1616 || p_box
->data
.p_ctts
->pi_sample_offset
== NULL
) )
1617 MP4_READBOX_EXIT( 0 );
1618 p_box
->data
.p_ctts
->i_entry_count
= count
;
1620 for( uint32_t i
= 0; i
< count
; i
++ )
1622 MP4_GET4BYTES( p_box
->data
.p_ctts
->pi_sample_count
[i
] );
1623 MP4_GET4BYTES( p_box
->data
.p_ctts
->pi_sample_offset
[i
] );
1627 msg_Dbg( p_stream
, "read box: \"ctts\" entry-count %"PRIu32
, count
);
1630 MP4_READBOX_EXIT( 1 );
1633 static int MP4_ReadBox_cslg( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1635 MP4_READBOX_ENTER( MP4_Box_data_cslg_t
, NULL
);
1637 unsigned i_version
, i_flags
;
1638 MP4_GET1BYTE( i_version
);
1639 MP4_GET3BYTES( i_flags
);
1640 VLC_UNUSED(i_flags
);
1643 MP4_READBOX_EXIT( 0 );
1645 #define READ_CSLG(readbytes) {\
1646 readbytes( p_box->data.p_cslg->ct_to_dts_shift );\
1647 readbytes( p_box->data.p_cslg->i_least_delta );\
1648 readbytes( p_box->data.p_cslg->i_max_delta );\
1649 readbytes( p_box->data.p_cslg->i_composition_starttime );\
1650 readbytes( p_box->data.p_cslg->i_composition_endtime ); }
1652 if( i_version
== 0 )
1653 READ_CSLG(MP4_GET4BYTES
)
1655 READ_CSLG(MP4_GET8BYTES
)
1657 MP4_READBOX_EXIT( 1 );
1660 static uint64_t MP4_ReadLengthDescriptor( uint8_t **restrict bufp
,
1661 uint64_t *restrict lenp
)
1663 unsigned char *buf
= *bufp
;
1664 uint64_t len
= *lenp
;
1670 if (unlikely(len
== 0))
1671 return -1; /* end of bit stream */
1672 if (unlikely(value
> (UINT64_MAX
>> 7)))
1673 return -1; /* integer overflow */
1677 value
= (value
<< 7) + (b
& 0x7f);
1687 static void MP4_FreeBox_esds( MP4_Box_t
*p_box
)
1689 FREENULL( p_box
->data
.p_esds
->es_descriptor
.psz_URL
);
1690 if( p_box
->data
.p_esds
->es_descriptor
.p_decConfigDescr
)
1692 FREENULL( p_box
->data
.p_esds
->es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
);
1693 FREENULL( p_box
->data
.p_esds
->es_descriptor
.p_decConfigDescr
);
1697 static int MP4_ReadBox_esds( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1699 #define es_descriptor p_box->data.p_esds->es_descriptor
1701 unsigned int i_flags
;
1702 unsigned int i_type
;
1704 MP4_READBOX_ENTER( MP4_Box_data_esds_t
, MP4_FreeBox_esds
);
1706 MP4_GETVERSIONFLAGS( p_box
->data
.p_esds
);
1709 MP4_GET1BYTE( i_type
);
1710 if( i_type
== 0x03 ) /* MP4ESDescrTag ISO/IEC 14496-1 8.3.3 */
1712 i_len
= MP4_ReadLengthDescriptor( &p_peek
, &i_read
);
1713 if( unlikely(i_len
== UINT64_C(-1)) )
1714 MP4_READBOX_EXIT( 0 );
1717 msg_Dbg( p_stream
, "found esds MPEG4ESDescr (%"PRIu64
" bytes)",
1721 MP4_GET2BYTES( es_descriptor
.i_ES_ID
);
1722 MP4_GET1BYTE( i_flags
);
1723 es_descriptor
.b_stream_dependence
= ( (i_flags
&0x80) != 0);
1724 es_descriptor
.b_url
= ( (i_flags
&0x40) != 0);
1725 es_descriptor
.b_OCRstream
= ( (i_flags
&0x20) != 0);
1727 es_descriptor
.i_stream_priority
= i_flags
&0x1f;
1728 if( es_descriptor
.b_stream_dependence
)
1730 MP4_GET2BYTES( es_descriptor
.i_depend_on_ES_ID
);
1732 if( es_descriptor
.b_url
&& i_read
> 0 )
1736 MP4_GET1BYTE( i_url
);
1737 if( i_url
> i_read
)
1738 MP4_READBOX_EXIT( 1 );
1739 es_descriptor
.psz_URL
= malloc( (unsigned) i_url
+ 1 );
1740 if( es_descriptor
.psz_URL
)
1742 memcpy( es_descriptor
.psz_URL
, p_peek
, i_url
);
1743 es_descriptor
.psz_URL
[i_url
] = 0;
1750 es_descriptor
.psz_URL
= NULL
;
1752 if( es_descriptor
.b_OCRstream
)
1754 MP4_GET2BYTES( es_descriptor
.i_OCR_ES_ID
);
1756 MP4_GET1BYTE( i_type
); /* get next type */
1759 if( i_type
!= 0x04)/* MP4DecConfigDescrTag ISO/IEC 14496-1 8.3.4 */
1761 es_descriptor
.p_decConfigDescr
= NULL
;
1762 MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1765 i_len
= MP4_ReadLengthDescriptor( &p_peek
, &i_read
);
1766 if( unlikely(i_len
== UINT64_C(-1)) )
1767 MP4_READBOX_EXIT( 0 );
1769 msg_Dbg( p_stream
, "found esds MP4DecConfigDescr (%"PRIu64
" bytes)",
1773 es_descriptor
.p_decConfigDescr
=
1774 calloc( 1, sizeof( MP4_descriptor_decoder_config_t
));
1775 if( unlikely( es_descriptor
.p_decConfigDescr
== NULL
) )
1776 MP4_READBOX_EXIT( 0 );
1778 MP4_GET1BYTE( es_descriptor
.p_decConfigDescr
->i_objectProfileIndication
);
1779 MP4_GET1BYTE( i_flags
);
1780 es_descriptor
.p_decConfigDescr
->i_streamType
= i_flags
>> 2;
1781 es_descriptor
.p_decConfigDescr
->b_upStream
= ( i_flags
>> 1 )&0x01;
1782 MP4_GET3BYTES( es_descriptor
.p_decConfigDescr
->i_buffer_sizeDB
);
1783 MP4_GET4BYTES( es_descriptor
.p_decConfigDescr
->i_max_bitrate
);
1784 MP4_GET4BYTES( es_descriptor
.p_decConfigDescr
->i_avg_bitrate
);
1785 MP4_GET1BYTE( i_type
);
1786 if( i_type
!= 0x05 )/* MP4DecSpecificDescrTag ISO/IEC 14496-1 8.3.5 */
1788 es_descriptor
.p_decConfigDescr
->i_decoder_specific_info_len
= 0;
1789 es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
= NULL
;
1790 MP4_READBOX_EXIT( 1 );
1793 i_len
= MP4_ReadLengthDescriptor( &p_peek
, &i_read
);
1794 if( unlikely(i_len
== UINT64_C(-1)) )
1795 MP4_READBOX_EXIT( 0 );
1797 msg_Dbg( p_stream
, "found esds MP4DecSpecificDescr (%"PRIu64
" bytes)",
1800 if( i_len
> i_read
)
1801 MP4_READBOX_EXIT( 0 );
1803 es_descriptor
.p_decConfigDescr
->i_decoder_specific_info_len
= i_len
;
1804 es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
= malloc( i_len
);
1805 if( unlikely( es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
== NULL
) )
1806 MP4_READBOX_EXIT( 0 );
1808 memcpy( es_descriptor
.p_decConfigDescr
->p_decoder_specific_info
,
1811 MP4_READBOX_EXIT( 1 );
1812 #undef es_descriptor
1815 static void MP4_FreeBox_avcC( MP4_Box_t
*p_box
)
1817 MP4_Box_data_avcC_t
*p_avcC
= p_box
->data
.p_avcC
;
1820 if( p_avcC
->i_avcC
> 0 ) FREENULL( p_avcC
->p_avcC
);
1824 for( i
= 0; i
< p_avcC
->i_sps
; i
++ )
1825 FREENULL( p_avcC
->sps
[i
] );
1829 for( i
= 0; i
< p_avcC
->i_pps
; i
++ )
1830 FREENULL( p_avcC
->pps
[i
] );
1832 if( p_avcC
->i_sps
> 0 ) FREENULL( p_avcC
->sps
);
1833 if( p_avcC
->i_sps
> 0 ) FREENULL( p_avcC
->i_sps_length
);
1834 if( p_avcC
->i_pps
> 0 ) FREENULL( p_avcC
->pps
);
1835 if( p_avcC
->i_pps
> 0 ) FREENULL( p_avcC
->i_pps_length
);
1838 static int MP4_ReadBox_avcC( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1840 MP4_Box_data_avcC_t
*p_avcC
;
1843 MP4_READBOX_ENTER( MP4_Box_data_avcC_t
, MP4_FreeBox_avcC
);
1844 p_avcC
= p_box
->data
.p_avcC
;
1846 p_avcC
->i_avcC
= i_read
;
1847 if( p_avcC
->i_avcC
> 0 )
1849 uint8_t * p
= p_avcC
->p_avcC
= malloc( p_avcC
->i_avcC
);
1851 memcpy( p
, p_peek
, i_read
);
1854 MP4_GET1BYTE( p_avcC
->i_version
);
1855 MP4_GET1BYTE( p_avcC
->i_profile
);
1856 MP4_GET1BYTE( p_avcC
->i_profile_compatibility
);
1857 MP4_GET1BYTE( p_avcC
->i_level
);
1858 MP4_GET1BYTE( p_avcC
->i_reserved1
);
1859 p_avcC
->i_length_size
= (p_avcC
->i_reserved1
&0x03) + 1;
1860 p_avcC
->i_reserved1
>>= 2;
1862 MP4_GET1BYTE( p_avcC
->i_reserved2
);
1863 p_avcC
->i_sps
= p_avcC
->i_reserved2
&0x1f;
1864 p_avcC
->i_reserved2
>>= 5;
1866 if( p_avcC
->i_sps
> 0 )
1868 p_avcC
->i_sps_length
= calloc( p_avcC
->i_sps
, sizeof( uint16_t ) );
1869 p_avcC
->sps
= calloc( p_avcC
->i_sps
, sizeof( uint8_t* ) );
1871 if( !p_avcC
->i_sps_length
|| !p_avcC
->sps
)
1874 for( i
= 0; i
< p_avcC
->i_sps
&& i_read
> 2; i
++ )
1876 MP4_GET2BYTES( p_avcC
->i_sps_length
[i
] );
1877 if ( p_avcC
->i_sps_length
[i
] > i_read
)
1879 p_avcC
->sps
[i
] = malloc( p_avcC
->i_sps_length
[i
] );
1880 if( p_avcC
->sps
[i
] )
1881 memcpy( p_avcC
->sps
[i
], p_peek
, p_avcC
->i_sps_length
[i
] );
1883 p_peek
+= p_avcC
->i_sps_length
[i
];
1884 i_read
-= p_avcC
->i_sps_length
[i
];
1886 if ( i
!= p_avcC
->i_sps
)
1890 MP4_GET1BYTE( p_avcC
->i_pps
);
1891 if( p_avcC
->i_pps
> 0 )
1893 p_avcC
->i_pps_length
= calloc( p_avcC
->i_pps
, sizeof( uint16_t ) );
1894 p_avcC
->pps
= calloc( p_avcC
->i_pps
, sizeof( uint8_t* ) );
1896 if( !p_avcC
->i_pps_length
|| !p_avcC
->pps
)
1899 for( i
= 0; i
< p_avcC
->i_pps
&& i_read
> 2; i
++ )
1901 MP4_GET2BYTES( p_avcC
->i_pps_length
[i
] );
1902 if( p_avcC
->i_pps_length
[i
] > i_read
)
1904 p_avcC
->pps
[i
] = malloc( p_avcC
->i_pps_length
[i
] );
1905 if( p_avcC
->pps
[i
] )
1906 memcpy( p_avcC
->pps
[i
], p_peek
, p_avcC
->i_pps_length
[i
] );
1908 p_peek
+= p_avcC
->i_pps_length
[i
];
1909 i_read
-= p_avcC
->i_pps_length
[i
];
1911 if ( i
!= p_avcC
->i_pps
)
1916 "read box: \"avcC\" version=%d profile=0x%x level=0x%x length size=%d sps=%d pps=%d",
1917 p_avcC
->i_version
, p_avcC
->i_profile
, p_avcC
->i_level
,
1918 p_avcC
->i_length_size
,
1919 p_avcC
->i_sps
, p_avcC
->i_pps
);
1920 for( i
= 0; i
< p_avcC
->i_sps
; i
++ )
1922 msg_Dbg( p_stream
, " - sps[%d] length=%d",
1923 i
, p_avcC
->i_sps_length
[i
] );
1925 for( i
= 0; i
< p_avcC
->i_pps
; i
++ )
1927 msg_Dbg( p_stream
, " - pps[%d] length=%d",
1928 i
, p_avcC
->i_pps_length
[i
] );
1932 MP4_READBOX_EXIT( 1 );
1935 MP4_FreeBox_avcC( p_box
);
1936 MP4_READBOX_EXIT( 0 );
1939 static void MP4_FreeBox_vpcC( MP4_Box_t
*p_box
)
1941 free( p_box
->data
.p_vpcC
->p_codec_init_data
);
1944 static int MP4_ReadBox_vpcC( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1946 MP4_READBOX_ENTER( MP4_Box_data_vpcC_t
, MP4_FreeBox_vpcC
);
1947 MP4_Box_data_vpcC_t
*p_vpcC
= p_box
->data
.p_vpcC
;
1949 if( p_box
->i_size
< 6 )
1950 MP4_READBOX_EXIT( 0 );
1953 MP4_GET1BYTE( i_version
);
1954 if( i_version
!= 0 )
1955 MP4_READBOX_EXIT( 0 );
1957 MP4_GET1BYTE( p_vpcC
->i_profile
);
1958 MP4_GET1BYTE( p_vpcC
->i_level
);
1959 MP4_GET1BYTE( p_vpcC
->i_bit_depth
);
1960 p_vpcC
->i_color_space
= p_vpcC
->i_bit_depth
& 0x0F;
1961 p_vpcC
->i_bit_depth
>>= 4;
1962 MP4_GET1BYTE( p_vpcC
->i_chroma_subsampling
);
1963 p_vpcC
->i_xfer_function
= ( p_vpcC
->i_chroma_subsampling
& 0x0F ) >> 1;
1964 p_vpcC
->i_fullrange
= p_vpcC
->i_chroma_subsampling
& 0x01;
1965 p_vpcC
->i_chroma_subsampling
>>= 4;
1966 MP4_GET2BYTES( p_vpcC
->i_codec_init_datasize
);
1967 if( p_vpcC
->i_codec_init_datasize
> i_read
)
1968 p_vpcC
->i_codec_init_datasize
= i_read
;
1970 if( p_vpcC
->i_codec_init_datasize
)
1972 p_vpcC
->p_codec_init_data
= malloc( i_read
);
1973 if( !p_vpcC
->p_codec_init_data
)
1974 MP4_READBOX_EXIT( 0 );
1975 memcpy( p_vpcC
->p_codec_init_data
, p_peek
, i_read
);
1978 MP4_READBOX_EXIT( 1 );
1981 static void MP4_FreeBox_WMA2( MP4_Box_t
*p_box
)
1983 FREENULL( p_box
->data
.p_WMA2
->p_extra
);
1986 static int MP4_ReadBox_WMA2( stream_t
*p_stream
, MP4_Box_t
*p_box
)
1988 MP4_READBOX_ENTER( MP4_Box_data_WMA2_t
, MP4_FreeBox_WMA2
);
1990 MP4_Box_data_WMA2_t
*p_WMA2
= p_box
->data
.p_WMA2
;
1992 MP4_GET2BYTESLE( p_WMA2
->Format
.wFormatTag
);
1993 MP4_GET2BYTESLE( p_WMA2
->Format
.nChannels
);
1994 MP4_GET4BYTESLE( p_WMA2
->Format
.nSamplesPerSec
);
1995 MP4_GET4BYTESLE( p_WMA2
->Format
.nAvgBytesPerSec
);
1996 MP4_GET2BYTESLE( p_WMA2
->Format
.nBlockAlign
);
1997 MP4_GET2BYTESLE( p_WMA2
->Format
.wBitsPerSample
);
2000 MP4_GET2BYTESLE( i_cbSize
);
2002 if( i_cbSize
> i_read
)
2005 p_WMA2
->i_extra
= i_cbSize
;
2006 if ( p_WMA2
->i_extra
)
2008 p_WMA2
->p_extra
= malloc( p_WMA2
->i_extra
);
2009 if ( ! p_WMA2
->p_extra
)
2011 memcpy( p_WMA2
->p_extra
, p_peek
, p_WMA2
->i_extra
);
2014 MP4_READBOX_EXIT( 1 );
2017 MP4_READBOX_EXIT( 0 );
2020 static void MP4_FreeBox_strf( MP4_Box_t
*p_box
)
2022 FREENULL( p_box
->data
.p_strf
->p_extra
);
2025 static int MP4_ReadBox_strf( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2027 MP4_READBOX_ENTER( MP4_Box_data_strf_t
, MP4_FreeBox_strf
);
2029 MP4_Box_data_strf_t
*p_strf
= p_box
->data
.p_strf
;
2034 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biSize
);
2035 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biWidth
);
2036 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biHeight
);
2037 MP4_GET2BYTESLE( p_strf
->bmiHeader
.biPlanes
);
2038 MP4_GET2BYTESLE( p_strf
->bmiHeader
.biBitCount
);
2039 MP4_GETFOURCC( p_strf
->bmiHeader
.biCompression
);
2040 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biSizeImage
);
2041 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biXPelsPerMeter
);
2042 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biYPelsPerMeter
);
2043 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biClrUsed
);
2044 MP4_GET4BYTESLE( p_strf
->bmiHeader
.biClrImportant
);
2046 p_strf
->i_extra
= i_read
;
2047 if ( p_strf
->i_extra
)
2049 p_strf
->p_extra
= malloc( p_strf
->i_extra
);
2050 if ( ! p_strf
->p_extra
)
2052 memcpy( p_strf
->p_extra
, p_peek
, i_read
);
2055 MP4_READBOX_EXIT( 1 );
2058 MP4_READBOX_EXIT( 0 );
2061 static int MP4_ReadBox_ASF( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2063 MP4_READBOX_ENTER( MP4_Box_data_ASF_t
, NULL
);
2065 MP4_Box_data_ASF_t
*p_asf
= p_box
->data
.p_asf
;
2068 MP4_READBOX_EXIT( 0 );
2070 MP4_GET1BYTE( p_asf
->i_stream_number
);
2071 /* remaining is unknown */
2073 MP4_READBOX_EXIT( 1 );
2076 static void MP4_FreeBox_sbgp( MP4_Box_t
*p_box
)
2078 MP4_Box_data_sbgp_t
*p_sbgp
= p_box
->data
.p_sbgp
;
2079 free( p_sbgp
->entries
.pi_sample_count
);
2080 free( p_sbgp
->entries
.pi_group_description_index
);
2083 static int MP4_ReadBox_sbgp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2085 MP4_READBOX_ENTER( MP4_Box_data_sbgp_t
, MP4_FreeBox_sbgp
);
2086 MP4_Box_data_sbgp_t
*p_sbgp
= p_box
->data
.p_sbgp
;
2090 MP4_READBOX_EXIT( 0 );
2092 MP4_GET1BYTE( p_sbgp
->i_version
);
2093 MP4_GET3BYTES( i_flags
);
2095 MP4_READBOX_EXIT( 0 );
2097 MP4_GETFOURCC( p_sbgp
->i_grouping_type
);
2099 if( p_sbgp
->i_version
== 1 )
2102 MP4_READBOX_EXIT( 0 );
2103 MP4_GET4BYTES( p_sbgp
->i_grouping_type_parameter
);
2106 MP4_GET4BYTES( p_sbgp
->i_entry_count
);
2107 if( p_sbgp
->i_entry_count
> i_read
/ (4 + 4) )
2108 p_sbgp
->i_entry_count
= i_read
/ (4 + 4);
2110 p_sbgp
->entries
.pi_sample_count
= vlc_alloc( p_sbgp
->i_entry_count
, sizeof(uint32_t) );
2111 p_sbgp
->entries
.pi_group_description_index
= vlc_alloc( p_sbgp
->i_entry_count
, sizeof(uint32_t) );
2113 if( !p_sbgp
->entries
.pi_sample_count
|| !p_sbgp
->entries
.pi_group_description_index
)
2115 MP4_FreeBox_sbgp( p_box
);
2116 MP4_READBOX_EXIT( 0 );
2119 for( uint32_t i
=0; i
<p_sbgp
->i_entry_count
; i
++ )
2121 MP4_GET4BYTES( p_sbgp
->entries
.pi_sample_count
[i
] );
2122 MP4_GET4BYTES( p_sbgp
->entries
.pi_group_description_index
[i
] );
2127 "read box: \"sbgp\" grouping type %4.4s", (char*) &p_sbgp
->i_grouping_type
);
2128 #ifdef MP4_ULTRA_VERBOSE
2129 for (uint32_t i
= 0; i
< p_sbgp
->i_entry_count
; i
++)
2130 msg_Dbg( p_stream
, "\t samples %" PRIu32
" group %" PRIu32
,
2131 p_sbgp
->entries
.pi_sample_count
[i
],
2132 p_sbgp
->entries
.pi_group_description_index
[i
] );
2136 MP4_READBOX_EXIT( 1 );
2139 static void MP4_FreeBox_sgpd( MP4_Box_t
*p_box
)
2141 MP4_Box_data_sgpd_t
*p_sgpd
= p_box
->data
.p_sgpd
;
2142 free( p_sgpd
->p_entries
);
2145 static int MP4_ReadBox_sgpd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2147 MP4_READBOX_ENTER( MP4_Box_data_sgpd_t
, MP4_FreeBox_sgpd
);
2148 MP4_Box_data_sgpd_t
*p_sgpd
= p_box
->data
.p_sgpd
;
2150 uint32_t i_default_length
= 0;
2153 MP4_READBOX_EXIT( 0 );
2155 MP4_GET1BYTE( p_sgpd
->i_version
);
2156 MP4_GET3BYTES( i_flags
);
2158 MP4_READBOX_EXIT( 0 );
2160 MP4_GETFOURCC( p_sgpd
->i_grouping_type
);
2162 switch( p_sgpd
->i_grouping_type
)
2164 case SAMPLEGROUP_rap
:
2170 "read box: \"sgpd\" grouping type %4.4s (unimplemented)", (char*) &p_sgpd
->i_grouping_type
);
2172 MP4_READBOX_EXIT( 1 );
2175 if( p_sgpd
->i_version
== 1 )
2178 MP4_READBOX_EXIT( 0 );
2179 MP4_GET4BYTES( i_default_length
);
2181 else if( p_sgpd
->i_version
>= 2 )
2184 MP4_READBOX_EXIT( 0 );
2185 MP4_GET4BYTES( p_sgpd
->i_default_sample_description_index
);
2188 MP4_GET4BYTES( p_sgpd
->i_entry_count
);
2190 p_sgpd
->p_entries
= vlc_alloc( p_sgpd
->i_entry_count
, sizeof(*p_sgpd
->p_entries
) );
2191 if( !p_sgpd
->p_entries
)
2192 MP4_READBOX_EXIT( 0 );
2195 for( ; i
<p_sgpd
->i_entry_count
; i
++ )
2197 uint32_t i_description_length
= i_default_length
;
2198 if( p_sgpd
->i_version
== 1 && i_default_length
== 0 )
2202 MP4_GET4BYTES( i_description_length
);
2205 if( p_sgpd
->i_version
== 1 && i_read
< i_description_length
)
2208 switch( p_sgpd
->i_grouping_type
)
2210 case SAMPLEGROUP_rap
:
2214 p_sgpd
->i_entry_count
= 0;
2215 MP4_FreeBox_sgpd( p_box
);
2216 MP4_READBOX_EXIT( 0 );
2219 MP4_GET1BYTE( i_data
);
2220 p_sgpd
->p_entries
[i
].rap
.i_num_leading_samples_known
= i_data
& 0x80;
2221 p_sgpd
->p_entries
[i
].rap
.i_num_leading_samples
= i_data
& 0x7F;
2230 if( i
!= p_sgpd
->i_entry_count
)
2231 p_sgpd
->i_entry_count
= i
;
2235 "read box: \"sgpd\" grouping type %4.4s", (char*) &p_sgpd
->i_grouping_type
);
2238 MP4_READBOX_EXIT( 1 );
2241 static void MP4_FreeBox_stsdext_chan( MP4_Box_t
*p_box
)
2243 MP4_Box_data_chan_t
*p_chan
= p_box
->data
.p_chan
;
2244 free( p_chan
->layout
.p_descriptions
);
2247 static int MP4_ReadBox_stsdext_chan( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2249 MP4_READBOX_ENTER( MP4_Box_data_chan_t
, MP4_FreeBox_stsdext_chan
);
2250 MP4_Box_data_chan_t
*p_chan
= p_box
->data
.p_chan
;
2253 MP4_READBOX_EXIT( 0 );
2255 MP4_GET1BYTE( p_chan
->i_version
);
2256 MP4_GET3BYTES( p_chan
->i_channels_flags
);
2257 MP4_GET4BYTES( p_chan
->layout
.i_channels_layout_tag
);
2258 MP4_GET4BYTES( p_chan
->layout
.i_channels_bitmap
);
2259 MP4_GET4BYTES( p_chan
->layout
.i_channels_description_count
);
2261 size_t i_descsize
= 8 + 3 * sizeof(float);
2262 if ( i_read
< p_chan
->layout
.i_channels_description_count
* i_descsize
)
2263 MP4_READBOX_EXIT( 0 );
2265 p_chan
->layout
.p_descriptions
=
2266 vlc_alloc( p_chan
->layout
.i_channels_description_count
, i_descsize
);
2268 if ( !p_chan
->layout
.p_descriptions
)
2269 MP4_READBOX_EXIT( 0 );
2272 for( i
=0; i
<p_chan
->layout
.i_channels_description_count
; i
++ )
2276 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].i_channel_label
);
2277 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].i_channel_flags
);
2278 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].f_coordinates
[0] );
2279 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].f_coordinates
[1] );
2280 MP4_GET4BYTES( p_chan
->layout
.p_descriptions
[i
].f_coordinates
[2] );
2282 if ( i
<p_chan
->layout
.i_channels_description_count
)
2283 p_chan
->layout
.i_channels_description_count
= i
;
2287 "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
2288 p_chan
->i_channels_flags
, p_chan
->layout
.i_channels_layout_tag
,
2289 p_chan
->layout
.i_channels_bitmap
, p_chan
->layout
.i_channels_description_count
);
2291 MP4_READBOX_EXIT( 1 );
2294 static int MP4_ReadBox_dec3( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2296 MP4_READBOX_ENTER( MP4_Box_data_dec3_t
, NULL
);
2298 MP4_Box_data_dec3_t
*p_dec3
= p_box
->data
.p_dec3
;
2301 MP4_GET2BYTES( i_header
);
2303 p_dec3
->i_data_rate
= i_header
>> 3;
2304 p_dec3
->i_num_ind_sub
= (i_header
& 0x7) + 1;
2305 for (uint8_t i
= 0; i
< p_dec3
->i_num_ind_sub
; i
++) {
2306 MP4_GET3BYTES( i_header
);
2307 p_dec3
->stream
[i
].i_fscod
= ( i_header
>> 22 ) & 0x03;
2308 p_dec3
->stream
[i
].i_bsid
= ( i_header
>> 17 ) & 0x01f;
2309 p_dec3
->stream
[i
].i_bsmod
= ( i_header
>> 12 ) & 0x01f;
2310 p_dec3
->stream
[i
].i_acmod
= ( i_header
>> 9 ) & 0x07;
2311 p_dec3
->stream
[i
].i_lfeon
= ( i_header
>> 8 ) & 0x01;
2312 p_dec3
->stream
[i
].i_num_dep_sub
= (i_header
>> 1) & 0x0f;
2313 if (p_dec3
->stream
[i
].i_num_dep_sub
) {
2314 MP4_GET1BYTE( p_dec3
->stream
[i
].i_chan_loc
);
2315 p_dec3
->stream
[i
].i_chan_loc
|= (i_header
& 1) << 8;
2317 p_dec3
->stream
[i
].i_chan_loc
= 0;
2322 "read box: \"dec3\" bitrate %dkbps %d independent substreams",
2323 p_dec3
->i_data_rate
, p_dec3
->i_num_ind_sub
);
2325 for (uint8_t i
= 0; i
< p_dec3
->i_num_ind_sub
; i
++)
2327 "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
2328 "num dependent subs=%d chan_loc=0x%x",
2329 i
, p_dec3
->stream
[i
].i_bsid
, p_dec3
->stream
[i
].i_bsmod
, p_dec3
->stream
[i
].i_acmod
,
2330 p_dec3
->stream
[i
].i_lfeon
, p_dec3
->stream
[i
].i_num_dep_sub
, p_dec3
->stream
[i
].i_chan_loc
);
2332 MP4_READBOX_EXIT( 1 );
2335 static int MP4_ReadBox_dac3( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2337 MP4_Box_data_dac3_t
*p_dac3
;
2338 MP4_READBOX_ENTER( MP4_Box_data_dac3_t
, NULL
);
2340 p_dac3
= p_box
->data
.p_dac3
;
2343 MP4_GET3BYTES( i_header
);
2345 p_dac3
->i_fscod
= ( i_header
>> 22 ) & 0x03;
2346 p_dac3
->i_bsid
= ( i_header
>> 17 ) & 0x01f;
2347 p_dac3
->i_bsmod
= ( i_header
>> 14 ) & 0x07;
2348 p_dac3
->i_acmod
= ( i_header
>> 11 ) & 0x07;
2349 p_dac3
->i_lfeon
= ( i_header
>> 10 ) & 0x01;
2350 p_dac3
->i_bitrate_code
= ( i_header
>> 5) & 0x1f;
2354 "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
2355 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
);
2357 MP4_READBOX_EXIT( 1 );
2360 static int MP4_ReadBox_dvc1( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2362 MP4_READBOX_ENTER( MP4_Box_data_dvc1_t
, NULL
);
2364 MP4_READBOX_EXIT( 0 );
2366 MP4_Box_data_dvc1_t
*p_dvc1
= p_box
->data
.p_dvc1
;
2367 MP4_GET1BYTE( p_dvc1
->i_profile_level
);
2368 p_dvc1
->i_vc1
= i_read
; /* Header + profile_level */
2369 if( p_dvc1
->i_vc1
> 0 && (p_dvc1
->p_vc1
= malloc( p_dvc1
->i_vc1
)) )
2370 memcpy( p_dvc1
->p_vc1
, p_peek
, i_read
);
2374 "read box: \"dvc1\" profile=%"PRIu8
, (p_dvc1
->i_profile_level
& 0xf0) >> 4 );
2377 MP4_READBOX_EXIT( 1 );
2380 static int MP4_ReadBox_fiel( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2382 MP4_Box_data_fiel_t
*p_fiel
;
2383 MP4_READBOX_ENTER( MP4_Box_data_fiel_t
, NULL
);
2384 p_fiel
= p_box
->data
.p_fiel
;
2386 MP4_READBOX_EXIT( 0 );
2389 p_fiel
->i_flags
= BLOCK_FLAG_SINGLE_FIELD
;
2391 else if(p_peek
[0] == 2) /* Interlaced */
2394 * 0 – There is only one field.
2395 * 1 – T is displayed earliest, T is stored first in the file.
2396 * 6 – B is displayed earliest, B is stored first in the file.
2397 * 9 – B is displayed earliest, T is stored first in the file.
2398 * 14 – T is displayed earliest, B is stored first in the file.
2400 if(p_peek
[1] == 1 || p_peek
[1] == 9)
2401 p_fiel
->i_flags
= BLOCK_FLAG_TOP_FIELD_FIRST
;
2402 else if(p_peek
[1] == 6 || p_peek
[1] == 14)
2403 p_fiel
->i_flags
= BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
2405 MP4_READBOX_EXIT( 1 );
2408 static int MP4_ReadBox_enda( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2410 MP4_Box_data_enda_t
*p_enda
;
2411 MP4_READBOX_ENTER( MP4_Box_data_enda_t
, NULL
);
2413 p_enda
= p_box
->data
.p_enda
;
2415 MP4_GET2BYTES( p_enda
->i_little_endian
);
2419 "read box: \"enda\" little_endian=%d", p_enda
->i_little_endian
);
2421 MP4_READBOX_EXIT( 1 );
2424 static void MP4_FreeBox_sample_soun( MP4_Box_t
*p_box
)
2426 FREENULL( p_box
->data
.p_sample_soun
->p_qt_description
);
2429 static int MP4_ReadBox_sample_soun( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2431 p_box
->i_handler
= ATOM_soun
;
2432 MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t
, MP4_FreeBox_sample_soun
);
2433 p_box
->data
.p_sample_soun
->p_qt_description
= NULL
;
2435 size_t i_actually_read
= i_read
+ header_size
;
2437 /* Sanity check needed because the "wave" box does also contain an
2438 * "mp4a" box that we don't understand. */
2441 MP4_READBOX_EXIT( 1 );
2444 for( unsigned i
= 0; i
< 6 ; i
++ )
2446 MP4_GET1BYTE( p_box
->data
.p_sample_soun
->i_reserved1
[i
] );
2449 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_data_reference_index
);
2452 * XXX hack -> produce a copy of the nearly complete chunk
2454 p_box
->data
.p_sample_soun
->i_qt_description
= 0;
2455 p_box
->data
.p_sample_soun
->p_qt_description
= NULL
;
2458 p_box
->data
.p_sample_soun
->p_qt_description
= malloc( i_read
);
2459 if( p_box
->data
.p_sample_soun
->p_qt_description
)
2461 p_box
->data
.p_sample_soun
->i_qt_description
= i_read
;
2462 memcpy( p_box
->data
.p_sample_soun
->p_qt_description
, p_peek
, i_read
);
2466 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_qt_version
);
2467 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_qt_revision_level
);
2468 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_qt_vendor
);
2470 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_channelcount
);
2471 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_samplesize
);
2472 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_compressionid
);
2473 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_reserved3
);
2474 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_sampleratehi
);
2475 MP4_GET2BYTES( p_box
->data
.p_sample_soun
->i_sampleratelo
);
2479 "read box: \"soun\" stsd qt_version %"PRIu16
" compid=%"PRIx16
,
2480 p_box
->data
.p_sample_soun
->i_qt_version
,
2481 p_box
->data
.p_sample_soun
->i_compressionid
);
2484 if( p_box
->data
.p_sample_soun
->i_qt_version
== 1 && i_read
>= 16 )
2486 /* SoundDescriptionV1 */
2487 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_sample_per_packet
);
2488 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_bytes_per_packet
);
2489 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_bytes_per_frame
);
2490 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_bytes_per_sample
);
2494 "read box: \"soun\" V1 sample/packet=%d bytes/packet=%d "
2495 "bytes/frame=%d bytes/sample=%d",
2496 p_box
->data
.p_sample_soun
->i_sample_per_packet
,
2497 p_box
->data
.p_sample_soun
->i_bytes_per_packet
,
2498 p_box
->data
.p_sample_soun
->i_bytes_per_frame
,
2499 p_box
->data
.p_sample_soun
->i_bytes_per_sample
);
2503 else if( p_box
->data
.p_sample_soun
->i_qt_version
== 2 && i_read
>= 36 )
2505 /* SoundDescriptionV2 */
2506 double f_sample_rate
;
2508 uint32_t i_channel
, i_extoffset
, i_dummy32
;
2511 if ( p_box
->data
.p_sample_soun
->i_channelcount
!= 0x3 ||
2512 p_box
->data
.p_sample_soun
->i_samplesize
!= 0x0010 ||
2513 p_box
->data
.p_sample_soun
->i_compressionid
!= 0xFFFE ||
2514 p_box
->data
.p_sample_soun
->i_reserved3
!= 0x0 ||
2515 p_box
->data
.p_sample_soun
->i_sampleratehi
!= 0x1 ||//65536
2516 p_box
->data
.p_sample_soun
->i_sampleratelo
!= 0x0 ) //remainder
2518 msg_Err( p_stream
, "invalid stsd V2 box defaults" );
2519 MP4_READBOX_EXIT( 0 );
2523 MP4_GET4BYTES( i_extoffset
); /* offset to stsd extentions */
2524 MP4_GET8BYTES( i_dummy64
);
2525 memcpy( &f_sample_rate
, &i_dummy64
, 8 );
2526 msg_Dbg( p_stream
, "read box: %f Hz", f_sample_rate
);
2527 /* Rounding error with lo, but we don't care since we do not support fractional audio rate */
2528 p_box
->data
.p_sample_soun
->i_sampleratehi
= (uint32_t)f_sample_rate
;
2529 p_box
->data
.p_sample_soun
->i_sampleratelo
= (f_sample_rate
- p_box
->data
.p_sample_soun
->i_sampleratehi
);
2531 MP4_GET4BYTES( i_channel
);
2532 p_box
->data
.p_sample_soun
->i_channelcount
= i_channel
;
2534 MP4_GET4BYTES( i_dummy32
);
2535 if ( i_dummy32
!= 0x7F000000 )
2537 msg_Err( p_stream
, "invalid stsd V2 box" );
2538 MP4_READBOX_EXIT( 0 );
2541 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_constbitsperchannel
);
2542 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_formatflags
);
2543 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_constbytesperaudiopacket
);
2544 MP4_GET4BYTES( p_box
->data
.p_sample_soun
->i_constLPCMframesperaudiopacket
);
2547 msg_Dbg( p_stream
, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
2548 "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
2550 p_box
->data
.p_sample_soun
->i_constbitsperchannel
,
2551 p_box
->data
.p_sample_soun
->i_formatflags
,
2552 p_box
->data
.p_sample_soun
->i_constbytesperaudiopacket
,
2553 p_box
->data
.p_sample_soun
->i_constLPCMframesperaudiopacket
);
2556 if( i_extoffset
> i_actually_read
)
2557 i_extoffset
= i_actually_read
;
2558 p_peek
= &p_buff
[i_extoffset
];
2559 i_read
= i_actually_read
- i_extoffset
;
2563 p_box
->data
.p_sample_soun
->i_sample_per_packet
= 0;
2564 p_box
->data
.p_sample_soun
->i_bytes_per_packet
= 0;
2565 p_box
->data
.p_sample_soun
->i_bytes_per_frame
= 0;
2566 p_box
->data
.p_sample_soun
->i_bytes_per_sample
= 0;
2569 msg_Dbg( p_stream
, "read box: \"soun\" V0 or qt1/2 (rest=%"PRIu64
")",
2575 if( p_box
->i_type
== ATOM_drms
)
2577 msg_Warn( p_stream
, "DRM protected streams are not supported." );
2578 MP4_READBOX_EXIT( 0 );
2581 if( p_box
->i_type
== ATOM_samr
|| p_box
->i_type
== ATOM_sawb
)
2583 /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
2584 p_box
->data
.p_sample_soun
->i_channelcount
= 1;
2587 /* Loads extensions */
2588 MP4_ReadBoxContainerRawInBox( p_stream
, p_box
, p_peek
, i_read
,
2589 p_box
->i_pos
+ p_peek
- p_buff
); /* esds/wave/... */
2592 msg_Dbg( p_stream
, "read box: \"soun\" in stsd channel %d "
2593 "sample size %d sample rate %f",
2594 p_box
->data
.p_sample_soun
->i_channelcount
,
2595 p_box
->data
.p_sample_soun
->i_samplesize
,
2596 (float)p_box
->data
.p_sample_soun
->i_sampleratehi
+
2597 (float)p_box
->data
.p_sample_soun
->i_sampleratelo
/ BLOCK16x16
);
2600 MP4_READBOX_EXIT( 1 );
2603 static void MP4_FreeBox_sample_vide( MP4_Box_t
*p_box
)
2605 FREENULL( p_box
->data
.p_sample_vide
->p_qt_image_description
);
2608 int MP4_ReadBox_sample_vide( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2610 p_box
->i_handler
= ATOM_vide
;
2611 MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t
, MP4_FreeBox_sample_vide
);
2613 size_t i_actually_read
= i_read
+ header_size
;
2615 for( unsigned i
= 0; i
< 6 ; i
++ )
2617 MP4_GET1BYTE( p_box
->data
.p_sample_vide
->i_reserved1
[i
] );
2620 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_data_reference_index
);
2623 * XXX hack -> produce a copy of the nearly complete chunk
2627 p_box
->data
.p_sample_vide
->p_qt_image_description
= malloc( i_read
);
2628 if( unlikely( p_box
->data
.p_sample_vide
->p_qt_image_description
== NULL
) )
2629 MP4_READBOX_EXIT( 0 );
2630 p_box
->data
.p_sample_vide
->i_qt_image_description
= i_read
;
2631 memcpy( p_box
->data
.p_sample_vide
->p_qt_image_description
,
2636 p_box
->data
.p_sample_vide
->i_qt_image_description
= 0;
2637 p_box
->data
.p_sample_vide
->p_qt_image_description
= NULL
;
2640 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_qt_version
);
2641 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_qt_revision_level
);
2642 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_qt_vendor
);
2644 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_qt_temporal_quality
);
2645 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_qt_spatial_quality
);
2647 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_width
);
2648 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_height
);
2650 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_horizresolution
);
2651 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_vertresolution
);
2653 MP4_GET4BYTES( p_box
->data
.p_sample_vide
->i_qt_data_size
);
2654 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_qt_frame_count
);
2657 MP4_READBOX_EXIT( 0 );
2658 if( p_peek
[0] <= 31 ) // Must be Pascal String
2660 memcpy( &p_box
->data
.p_sample_vide
->sz_compressorname
, &p_peek
[1], p_peek
[0] );
2661 p_box
->data
.p_sample_vide
->sz_compressorname
[p_peek
[0]] = 0;
2663 p_peek
+= 32; i_read
-= 32;
2665 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_depth
);
2666 MP4_GET2BYTES( p_box
->data
.p_sample_vide
->i_qt_color_table
);
2668 if( p_box
->i_type
== ATOM_drmi
)
2670 msg_Warn( p_stream
, "DRM protected streams are not supported." );
2671 MP4_READBOX_EXIT( 0 );
2674 if( i_actually_read
> 78 && p_peek
- p_buff
> 78 )
2676 MP4_ReadBoxContainerRawInBox( p_stream
, p_box
, p_peek
, i_read
,
2677 p_box
->i_pos
+ p_peek
- p_buff
);
2681 msg_Dbg( p_stream
, "read box: \"vide\" in stsd %dx%d depth %d (%s)",
2682 p_box
->data
.p_sample_vide
->i_width
,
2683 p_box
->data
.p_sample_vide
->i_height
,
2684 p_box
->data
.p_sample_vide
->i_depth
,
2685 p_box
->data
.p_sample_vide
->sz_compressorname
);
2688 MP4_READBOX_EXIT( 1 );
2691 static int MP4_ReadBox_sample_mp4s( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2693 p_box
->i_handler
= ATOM_text
;
2694 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_text_t
, 16, NULL
);
2697 MP4_READBOX_EXIT( 0 );
2699 MP4_ReadBoxContainerChildren( p_stream
, p_box
, NULL
);
2701 if ( MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) )
2702 MP4_READBOX_EXIT( 0 );
2704 MP4_READBOX_EXIT( 1 );
2707 static void MP4_FreeBox_sample_hint( MP4_Box_t
*p_box
)
2709 FREENULL( p_box
->data
.p_sample_hint
->p_data
);
2712 static int MP4_ReadBox_sample_hint8( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2714 MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_hint_t
, 24, MP4_FreeBox_sample_hint
);
2716 for( unsigned i
= 0; i
< 6 ; i
++ )
2718 MP4_GET1BYTE( p_box
->data
.p_sample_hint
->i_reserved1
[i
] );
2721 MP4_GET2BYTES( p_box
->data
.p_sample_hint
->i_data_reference_index
);
2723 if( !(p_box
->data
.p_sample_hint
->p_data
= malloc(8)) )
2724 MP4_READBOX_EXIT( 0 );
2726 MP4_GET8BYTES( *(p_box
->data
.p_sample_hint
->p_data
) );
2728 MP4_ReadBoxContainerChildren(p_stream
, p_box
, NULL
);
2730 if ( MP4_Seek( p_stream
, p_box
->i_pos
+ p_box
->i_size
) )
2731 MP4_READBOX_EXIT( 0 );
2733 MP4_READBOX_EXIT( 1 );
2736 static int MP4_ReadBox_sample_text( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2740 p_box
->i_handler
= ATOM_text
;
2741 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t
, NULL
);
2743 MP4_GET4BYTES( p_box
->data
.p_sample_text
->i_reserved1
);
2744 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_reserved2
);
2746 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_data_reference_index
);
2748 MP4_GET4BYTES( p_box
->data
.p_sample_text
->i_display_flags
);
2753 /* FIXME search right signification */
2755 p_box
->data
.p_sample_text
->i_justification_horizontal
= 1;
2756 p_box
->data
.p_sample_text
->i_justification_vertical
= 1;
2758 case -1: // Flush Right
2759 p_box
->data
.p_sample_text
->i_justification_horizontal
= -1;
2760 p_box
->data
.p_sample_text
->i_justification_vertical
= -1;
2762 case -2: // Flush Left
2763 p_box
->data
.p_sample_text
->i_justification_horizontal
= 0;
2764 p_box
->data
.p_sample_text
->i_justification_vertical
= 0;
2766 case 0: // Flush Default
2768 p_box
->data
.p_sample_text
->i_justification_horizontal
= 1;
2769 p_box
->data
.p_sample_text
->i_justification_vertical
= -1;
2773 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_background_color
[0] );
2774 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_background_color
[1] );
2775 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_background_color
[2] );
2776 p_box
->data
.p_sample_text
->i_background_color
[3] = 0xFF;
2778 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_text_box_top
);
2779 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_text_box_left
);
2780 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_text_box_bottom
);
2781 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_text_box_right
);
2784 msg_Dbg( p_stream
, "read box: \"text\" in stsd text" );
2786 MP4_READBOX_EXIT( 1 );
2789 static int MP4_ReadBox_sample_clcp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2791 p_box
->i_handler
= ATOM_clcp
;
2792 MP4_READBOX_ENTER( MP4_Box_data_sample_clcp_t
, NULL
);
2795 MP4_READBOX_EXIT( 0 );
2797 for( int i
=0; i
<6; i
++ )
2798 MP4_GET1BYTE( p_box
->data
.p_sample_clcp
->i_reserved1
[i
] );
2799 MP4_GET2BYTES( p_box
->data
.p_sample_clcp
->i_data_reference_index
);
2802 msg_Dbg( p_stream
, "read box: \"clcp\" in stsd" );
2804 MP4_READBOX_EXIT( 1 );
2807 static int MP4_ReadBox_sample_tx3g( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2809 p_box
->i_handler
= ATOM_text
;
2810 MP4_READBOX_ENTER( MP4_Box_data_sample_text_t
, NULL
);
2812 MP4_GET4BYTES( p_box
->data
.p_sample_text
->i_reserved1
);
2813 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_reserved2
);
2815 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_data_reference_index
);
2817 MP4_GET4BYTES( p_box
->data
.p_sample_text
->i_display_flags
);
2819 MP4_GET1BYTE ( p_box
->data
.p_sample_text
->i_justification_horizontal
);
2820 MP4_GET1BYTE ( p_box
->data
.p_sample_text
->i_justification_vertical
);
2822 MP4_GET1BYTE ( p_box
->data
.p_sample_text
->i_background_color
[0] );
2823 MP4_GET1BYTE ( p_box
->data
.p_sample_text
->i_background_color
[1] );
2824 MP4_GET1BYTE ( p_box
->data
.p_sample_text
->i_background_color
[2] );
2825 MP4_GET1BYTE ( p_box
->data
.p_sample_text
->i_background_color
[3] );
2827 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_text_box_top
);
2828 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_text_box_left
);
2829 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_text_box_bottom
);
2830 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_text_box_right
);
2832 MP4_GET4BYTES( p_box
->data
.p_sample_text
->i_reserved3
);
2834 MP4_GET2BYTES( p_box
->data
.p_sample_text
->i_font_id
);
2835 MP4_GET1BYTE ( p_box
->data
.p_sample_text
->i_font_face
);
2836 MP4_GET1BYTE ( p_box
->data
.p_sample_text
->i_font_size
);
2837 MP4_GET4BYTES( p_box
->data
.p_sample_text
->i_font_color
);
2840 msg_Dbg( p_stream
, "read box: \"tx3g\" in stsd text" );
2842 MP4_READBOX_EXIT( 1 );
2847 /* We can't easily call it, and anyway ~ 20 bytes lost isn't a real problem */
2848 static void MP4_FreeBox_sample_text( MP4_Box_t
*p_box
)
2850 FREENULL( p_box
->data
.p_sample_text
->psz_text_name
);
2854 static void MP4_FreeBox_stsz( MP4_Box_t
*p_box
)
2856 FREENULL( p_box
->data
.p_stsz
->i_entry_size
);
2859 static int MP4_ReadBox_stsz( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2863 MP4_READBOX_ENTER( MP4_Box_data_stsz_t
, MP4_FreeBox_stsz
);
2865 MP4_GETVERSIONFLAGS( p_box
->data
.p_stsz
);
2867 MP4_GET4BYTES( p_box
->data
.p_stsz
->i_sample_size
);
2868 MP4_GET4BYTES( count
);
2869 p_box
->data
.p_stsz
->i_sample_count
= count
;
2871 if( p_box
->data
.p_stsz
->i_sample_size
== 0 )
2873 if( UINT64_C(4) * count
> i_read
)
2874 MP4_READBOX_EXIT( 0 );
2876 p_box
->data
.p_stsz
->i_entry_size
=
2877 vlc_alloc( count
, sizeof(uint32_t) );
2878 if( unlikely( !p_box
->data
.p_stsz
->i_entry_size
) )
2879 MP4_READBOX_EXIT( 0 );
2881 for( uint32_t i
= 0; i
< count
; i
++ )
2883 MP4_GET4BYTES( p_box
->data
.p_stsz
->i_entry_size
[i
] );
2887 p_box
->data
.p_stsz
->i_entry_size
= NULL
;
2890 msg_Dbg( p_stream
, "read box: \"stsz\" sample-size %d sample-count %d",
2891 p_box
->data
.p_stsz
->i_sample_size
,
2892 p_box
->data
.p_stsz
->i_sample_count
);
2895 MP4_READBOX_EXIT( 1 );
2898 static void MP4_FreeBox_stsc( MP4_Box_t
*p_box
)
2900 FREENULL( p_box
->data
.p_stsc
->i_first_chunk
);
2901 FREENULL( p_box
->data
.p_stsc
->i_samples_per_chunk
);
2902 FREENULL( p_box
->data
.p_stsc
->i_sample_description_index
);
2905 static int MP4_ReadBox_stsc( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2909 MP4_READBOX_ENTER( MP4_Box_data_stsc_t
, MP4_FreeBox_stsc
);
2911 MP4_GETVERSIONFLAGS( p_box
->data
.p_stsc
);
2912 MP4_GET4BYTES( count
);
2914 if( UINT64_C(12) * count
> i_read
)
2915 MP4_READBOX_EXIT( 0 );
2917 p_box
->data
.p_stsc
->i_first_chunk
= vlc_alloc( count
, sizeof(uint32_t) );
2918 p_box
->data
.p_stsc
->i_samples_per_chunk
= vlc_alloc( count
,
2920 p_box
->data
.p_stsc
->i_sample_description_index
= vlc_alloc( count
,
2922 if( unlikely( p_box
->data
.p_stsc
->i_first_chunk
== NULL
2923 || p_box
->data
.p_stsc
->i_samples_per_chunk
== NULL
2924 || p_box
->data
.p_stsc
->i_sample_description_index
== NULL
) )
2926 MP4_READBOX_EXIT( 0 );
2928 p_box
->data
.p_stsc
->i_entry_count
= count
;
2930 for( uint32_t i
= 0; i
< count
;i
++ )
2932 MP4_GET4BYTES( p_box
->data
.p_stsc
->i_first_chunk
[i
] );
2933 MP4_GET4BYTES( p_box
->data
.p_stsc
->i_samples_per_chunk
[i
] );
2934 MP4_GET4BYTES( p_box
->data
.p_stsc
->i_sample_description_index
[i
] );
2938 msg_Dbg( p_stream
, "read box: \"stsc\" entry-count %d",
2939 p_box
->data
.p_stsc
->i_entry_count
);
2942 MP4_READBOX_EXIT( 1 );
2945 static void MP4_FreeBox_sdp( MP4_Box_t
*p_box
)
2947 FREENULL( p_box
->data
.p_sdp
->psz_text
);
2950 static int MP4_ReadBox_sdp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2952 MP4_READBOX_ENTER( MP4_Box_data_sdp_t
, MP4_FreeBox_sdp
);
2954 MP4_GETSTRINGZ( p_box
->data
.p_sdp
->psz_text
);
2956 MP4_READBOX_EXIT( 1 );
2959 static void MP4_FreeBox_rtp( MP4_Box_t
*p_box
)
2961 FREENULL( p_box
->data
.p_moviehintinformation_rtp
->psz_text
);
2964 static int MP4_ReadBox_rtp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2966 MP4_READBOX_ENTER( MP4_Box_data_moviehintinformation_rtp_t
, MP4_FreeBox_rtp
);
2968 MP4_GET4BYTES( p_box
->data
.p_moviehintinformation_rtp
->i_description_format
);
2970 MP4_GETSTRINGZ( p_box
->data
.p_moviehintinformation_rtp
->psz_text
);
2972 MP4_READBOX_EXIT( 1 );
2975 static int MP4_ReadBox_tims( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2977 MP4_READBOX_ENTER( MP4_Box_data_tims_t
, NULL
);
2979 MP4_GET4BYTES( p_box
->data
.p_tims
->i_timescale
);
2981 MP4_READBOX_EXIT( 1 );
2984 static int MP4_ReadBox_tsro( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2986 MP4_READBOX_ENTER( MP4_Box_data_tsro_t
, NULL
);
2988 MP4_GET4BYTES( p_box
->data
.p_tsro
->i_offset
);
2990 MP4_READBOX_EXIT( 1 );
2993 static int MP4_ReadBox_tssy( stream_t
*p_stream
, MP4_Box_t
*p_box
)
2995 MP4_READBOX_ENTER( MP4_Box_data_tssy_t
, NULL
);
2997 MP4_GET1BYTE( p_box
->data
.p_tssy
->i_reserved_timestamp_sync
);
2999 MP4_READBOX_EXIT( 1 );
3002 static void MP4_FreeBox_stco_co64( MP4_Box_t
*p_box
)
3004 FREENULL( p_box
->data
.p_co64
->i_chunk_offset
);
3007 static int MP4_ReadBox_stco_co64( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3009 const bool sixtyfour
= p_box
->i_type
!= ATOM_stco
;
3012 MP4_READBOX_ENTER( MP4_Box_data_co64_t
, MP4_FreeBox_stco_co64
);
3014 MP4_GETVERSIONFLAGS( p_box
->data
.p_co64
);
3015 MP4_GET4BYTES( count
);
3017 if( (sixtyfour
? UINT64_C(8) : UINT64_C(4)) * count
> i_read
)
3018 MP4_READBOX_EXIT( 0 );
3020 p_box
->data
.p_co64
->i_chunk_offset
= vlc_alloc( count
, sizeof(uint64_t) );
3021 if( unlikely(p_box
->data
.p_co64
->i_chunk_offset
== NULL
) )
3022 MP4_READBOX_EXIT( 0 );
3023 p_box
->data
.p_co64
->i_entry_count
= count
;
3025 for( uint32_t i
= 0; i
< count
; i
++ )
3028 MP4_GET8BYTES( p_box
->data
.p_co64
->i_chunk_offset
[i
] );
3030 MP4_GET4BYTES( p_box
->data
.p_co64
->i_chunk_offset
[i
] );
3034 msg_Dbg( p_stream
, "read box: \"co64\" entry-count %d",
3035 p_box
->data
.p_co64
->i_entry_count
);
3038 MP4_READBOX_EXIT( 1 );
3041 static void MP4_FreeBox_stss( MP4_Box_t
*p_box
)
3043 FREENULL( p_box
->data
.p_stss
->i_sample_number
);
3046 static int MP4_ReadBox_stss( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3050 MP4_READBOX_ENTER( MP4_Box_data_stss_t
, MP4_FreeBox_stss
);
3052 MP4_GETVERSIONFLAGS( p_box
->data
.p_stss
);
3053 MP4_GET4BYTES( count
);
3055 if( UINT64_C(4) * count
> i_read
)
3056 MP4_READBOX_EXIT( 0 );
3058 p_box
->data
.p_stss
->i_sample_number
= vlc_alloc( count
, sizeof(uint32_t) );
3059 if( unlikely( p_box
->data
.p_stss
->i_sample_number
== NULL
) )
3060 MP4_READBOX_EXIT( 0 );
3061 p_box
->data
.p_stss
->i_entry_count
= count
;
3063 for( uint32_t i
= 0; i
< count
; i
++ )
3065 MP4_GET4BYTES( p_box
->data
.p_stss
->i_sample_number
[i
] );
3066 /* XXX in libmp4 sample begin at 0 */
3067 p_box
->data
.p_stss
->i_sample_number
[i
]--;
3071 msg_Dbg( p_stream
, "read box: \"stss\" entry-count %d",
3072 p_box
->data
.p_stss
->i_entry_count
);
3075 MP4_READBOX_EXIT( 1 );
3078 static void MP4_FreeBox_stsh( MP4_Box_t
*p_box
)
3080 FREENULL( p_box
->data
.p_stsh
->i_shadowed_sample_number
);
3081 FREENULL( p_box
->data
.p_stsh
->i_sync_sample_number
);
3084 static int MP4_ReadBox_stsh( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3088 MP4_READBOX_ENTER( MP4_Box_data_stsh_t
, MP4_FreeBox_stsh
);
3090 MP4_GETVERSIONFLAGS( p_box
->data
.p_stsh
);
3091 MP4_GET4BYTES( count
);
3093 if( UINT64_C(8) * count
> i_read
)
3094 MP4_READBOX_EXIT( 0 );
3096 p_box
->data
.p_stsh
->i_shadowed_sample_number
= vlc_alloc( count
,
3098 p_box
->data
.p_stsh
->i_sync_sample_number
= vlc_alloc( count
,
3100 if( p_box
->data
.p_stsh
->i_shadowed_sample_number
== NULL
3101 || p_box
->data
.p_stsh
->i_sync_sample_number
== NULL
)
3102 MP4_READBOX_EXIT( 0 );
3103 p_box
->data
.p_stsh
->i_entry_count
= count
;
3105 for( uint32_t i
= 0; i
< p_box
->data
.p_stss
->i_entry_count
; i
++ )
3107 MP4_GET4BYTES( p_box
->data
.p_stsh
->i_shadowed_sample_number
[i
] );
3108 MP4_GET4BYTES( p_box
->data
.p_stsh
->i_sync_sample_number
[i
] );
3112 msg_Dbg( p_stream
, "read box: \"stsh\" entry-count %d",
3113 p_box
->data
.p_stsh
->i_entry_count
);
3115 MP4_READBOX_EXIT( 1 );
3118 static void MP4_FreeBox_stdp( MP4_Box_t
*p_box
)
3120 FREENULL( p_box
->data
.p_stdp
->i_priority
);
3123 static int MP4_ReadBox_stdp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3125 MP4_READBOX_ENTER( MP4_Box_data_stdp_t
, MP4_FreeBox_stdp
);
3127 MP4_GETVERSIONFLAGS( p_box
->data
.p_stdp
);
3129 p_box
->data
.p_stdp
->i_priority
=
3130 calloc( i_read
/ 2, sizeof(uint16_t) );
3132 if( unlikely( !p_box
->data
.p_stdp
->i_priority
) )
3133 MP4_READBOX_EXIT( 0 );
3135 for( unsigned i
= 0; i
< i_read
/ 2 ; i
++ )
3137 MP4_GET2BYTES( p_box
->data
.p_stdp
->i_priority
[i
] );
3141 msg_Dbg( p_stream
, "read box: \"stdp\" entry-count %"PRId64
,
3145 MP4_READBOX_EXIT( 1 );
3148 static void MP4_FreeBox_padb( MP4_Box_t
*p_box
)
3150 FREENULL( p_box
->data
.p_padb
->i_reserved
);
3151 FREENULL( p_box
->data
.p_padb
->i_pad
);
3154 static int MP4_ReadBox_padb( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3158 MP4_READBOX_ENTER( MP4_Box_data_padb_t
, MP4_FreeBox_padb
);
3160 MP4_GETVERSIONFLAGS( p_box
->data
.p_padb
);
3161 MP4_GET4BYTES( count
);
3163 if( ((count
/ 2) + (count
& 1)) > i_read
)
3165 MP4_READBOX_EXIT( 0 );
3168 p_box
->data
.p_padb
->i_reserved
= malloc( count
);
3169 p_box
->data
.p_padb
->i_pad
= malloc( count
);
3170 p_box
->data
.p_padb
->i_sample_count
= count
;
3172 if( unlikely(p_box
->data
.p_padb
->i_reserved
== NULL
3173 || p_box
->data
.p_padb
->i_pad
== NULL
) )
3175 MP4_READBOX_EXIT( 0 );
3178 for( size_t i
= 0; i
< count
; i
+= 2 )
3180 p_box
->data
.p_padb
->i_reserved
[i
] = ( (*p_peek
) >> 7 )&0x01;
3181 p_box
->data
.p_padb
->i_pad
[i
+ 1] = ( (*p_peek
) >> 4 )&0x07;
3182 p_box
->data
.p_padb
->i_reserved
[i
+ 1] = ( (*p_peek
) >> 3 )&0x01;
3183 p_box
->data
.p_padb
->i_pad
[i
] = ( (*p_peek
) )&0x07;
3190 msg_Dbg( p_stream
, "read box: \"stdp\" entry-count %"PRIu64
,
3194 MP4_READBOX_EXIT( 1 );
3197 static void MP4_FreeBox_elst( MP4_Box_t
*p_box
)
3199 FREENULL( p_box
->data
.p_elst
->i_segment_duration
);
3200 FREENULL( p_box
->data
.p_elst
->i_media_time
);
3201 FREENULL( p_box
->data
.p_elst
->i_media_rate_integer
);
3202 FREENULL( p_box
->data
.p_elst
->i_media_rate_fraction
);
3205 static int MP4_ReadBox_elst( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3209 MP4_READBOX_ENTER( MP4_Box_data_elst_t
, MP4_FreeBox_elst
);
3211 MP4_GETVERSIONFLAGS( p_box
->data
.p_elst
);
3212 MP4_GET4BYTES( count
);
3214 uint32_t i_entries_max
= i_read
/ ((p_box
->data
.p_elst
->i_version
== 1) ? 20 : 12);
3215 if( count
> i_entries_max
)
3216 count
= i_entries_max
;
3218 p_box
->data
.p_elst
->i_segment_duration
= vlc_alloc( count
,
3220 p_box
->data
.p_elst
->i_media_time
= vlc_alloc( count
, sizeof(int64_t) );
3221 p_box
->data
.p_elst
->i_media_rate_integer
= vlc_alloc( count
,
3223 p_box
->data
.p_elst
->i_media_rate_fraction
= vlc_alloc( count
,
3225 if( p_box
->data
.p_elst
->i_segment_duration
== NULL
3226 || p_box
->data
.p_elst
->i_media_time
== NULL
3227 || p_box
->data
.p_elst
->i_media_rate_integer
== NULL
3228 || p_box
->data
.p_elst
->i_media_rate_fraction
== NULL
)
3230 MP4_READBOX_EXIT( 0 );
3232 p_box
->data
.p_elst
->i_entry_count
= count
;
3234 for( uint32_t i
= 0; i
< count
; i
++ )
3236 uint64_t segment_duration
;
3239 if( p_box
->data
.p_elst
->i_version
== 1 )
3241 union { int64_t s
; uint64_t u
; } u
;
3243 MP4_GET8BYTES( segment_duration
);
3244 MP4_GET8BYTES( u
.u
);
3249 union { int32_t s
; uint32_t u
; } u
;
3251 MP4_GET4BYTES( segment_duration
);
3252 MP4_GET4BYTES( u
.u
);
3256 p_box
->data
.p_elst
->i_segment_duration
[i
] = segment_duration
;
3257 p_box
->data
.p_elst
->i_media_time
[i
] = media_time
;
3258 MP4_GET2BYTES( p_box
->data
.p_elst
->i_media_rate_integer
[i
] );
3259 MP4_GET2BYTES( p_box
->data
.p_elst
->i_media_rate_fraction
[i
] );
3263 msg_Dbg( p_stream
, "read box: \"elst\" entry-count %" PRIu32
,
3264 p_box
->data
.p_elst
->i_entry_count
);
3266 MP4_READBOX_EXIT( 1 );
3269 static void MP4_FreeBox_cprt( MP4_Box_t
*p_box
)
3271 FREENULL( p_box
->data
.p_cprt
->psz_notice
);
3274 static int MP4_ReadBox_cprt( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3276 uint16_t i_language
;
3279 MP4_READBOX_ENTER( MP4_Box_data_cprt_t
, MP4_FreeBox_cprt
);
3281 MP4_GETVERSIONFLAGS( p_box
->data
.p_cprt
);
3283 MP4_GET2BYTES( i_language
);
3284 decodeQtLanguageCode( i_language
, p_box
->data
.p_cprt
->rgs_language
, &b_mac
);
3286 MP4_GETSTRINGZ( p_box
->data
.p_cprt
->psz_notice
);
3289 msg_Dbg( p_stream
, "read box: \"cprt\" language %3.3s notice %s",
3290 p_box
->data
.p_cprt
->rgs_language
,
3291 p_box
->data
.p_cprt
->psz_notice
);
3294 MP4_READBOX_EXIT( 1 );
3297 static int MP4_ReadBox_dcom( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3299 MP4_READBOX_ENTER( MP4_Box_data_dcom_t
, NULL
);
3301 MP4_GETFOURCC( p_box
->data
.p_dcom
->i_algorithm
);
3304 "read box: \"dcom\" compression algorithm : %4.4s",
3305 (char*)&p_box
->data
.p_dcom
->i_algorithm
);
3307 MP4_READBOX_EXIT( 1 );
3310 static void MP4_FreeBox_cmvd( MP4_Box_t
*p_box
)
3312 FREENULL( p_box
->data
.p_cmvd
->p_data
);
3315 static int MP4_ReadBox_cmvd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3317 MP4_READBOX_ENTER( MP4_Box_data_cmvd_t
, MP4_FreeBox_cmvd
);
3319 MP4_GET4BYTES( p_box
->data
.p_cmvd
->i_uncompressed_size
);
3321 p_box
->data
.p_cmvd
->i_compressed_size
= i_read
;
3323 if( !( p_box
->data
.p_cmvd
->p_data
= malloc( i_read
) ) )
3324 MP4_READBOX_EXIT( 0 );
3326 /* now copy compressed data */
3327 memcpy( p_box
->data
.p_cmvd
->p_data
, p_peek
,i_read
);
3329 p_box
->data
.p_cmvd
->b_compressed
= 1;
3332 msg_Dbg( p_stream
, "read box: \"cmvd\" compressed data size %d",
3333 p_box
->data
.p_cmvd
->i_compressed_size
);
3336 MP4_READBOX_EXIT( 1 );
3339 static int MP4_ReadBox_cmov( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3345 stream_t
*p_stream_memory
;
3351 if( !( p_box
->data
.p_cmov
= calloc(1, sizeof( MP4_Box_data_cmov_t
) ) ) )
3354 if( !p_box
->p_father
||
3355 ( p_box
->p_father
->i_type
!= ATOM_moov
&&
3356 p_box
->p_father
->i_type
!= ATOM_foov
) )
3358 msg_Warn( p_stream
, "Read box: \"cmov\" box alone" );
3362 if( !MP4_ReadBoxContainer( p_stream
, p_box
) )
3367 if( ( p_dcom
= MP4_BoxGet( p_box
, "dcom" ) ) == NULL
||
3368 ( p_cmvd
= MP4_BoxGet( p_box
, "cmvd" ) ) == NULL
||
3369 p_cmvd
->data
.p_cmvd
->p_data
== NULL
)
3371 msg_Warn( p_stream
, "read box: \"cmov\" incomplete" );
3375 if( p_dcom
->data
.p_dcom
->i_algorithm
!= ATOM_zlib
)
3377 msg_Dbg( p_stream
, "read box: \"cmov\" compression algorithm : %4.4s "
3378 "not supported", (char*)&p_dcom
->data
.p_dcom
->i_algorithm
);
3383 msg_Dbg( p_stream
, "read box: \"cmov\" zlib unsupported" );
3387 /* decompress data */
3388 /* allocate a new buffer */
3389 if( !( p_data
= malloc( p_cmvd
->data
.p_cmvd
->i_uncompressed_size
) ) )
3391 /* init default structures */
3392 z_data
.next_in
= p_cmvd
->data
.p_cmvd
->p_data
;
3393 z_data
.avail_in
= p_cmvd
->data
.p_cmvd
->i_compressed_size
;
3394 z_data
.next_out
= p_data
;
3395 z_data
.avail_out
= p_cmvd
->data
.p_cmvd
->i_uncompressed_size
;
3396 z_data
.zalloc
= (alloc_func
)Z_NULL
;
3397 z_data
.zfree
= (free_func
)Z_NULL
;
3398 z_data
.opaque
= (voidpf
)Z_NULL
;
3401 if( inflateInit( &z_data
) != Z_OK
)
3403 msg_Err( p_stream
, "read box: \"cmov\" error while uncompressing" );
3409 i_result
= inflate( &z_data
, Z_NO_FLUSH
);
3410 if( i_result
!= Z_OK
&& i_result
!= Z_STREAM_END
)
3412 msg_Err( p_stream
, "read box: \"cmov\" error while uncompressing" );
3417 if( p_cmvd
->data
.p_cmvd
->i_uncompressed_size
!= z_data
.total_out
)
3419 msg_Warn( p_stream
, "read box: \"cmov\" uncompressing data size "
3422 p_cmvd
->data
.p_cmvd
->i_uncompressed_size
= z_data
.total_out
;
3425 if( inflateEnd( &z_data
) != Z_OK
)
3427 msg_Warn( p_stream
, "read box: \"cmov\" error while uncompressing "
3431 free( p_cmvd
->data
.p_cmvd
->p_data
);
3432 p_cmvd
->data
.p_cmvd
->p_data
= p_data
;
3433 p_cmvd
->data
.p_cmvd
->b_compressed
= 0;
3435 msg_Dbg( p_stream
, "read box: \"cmov\" box successfully uncompressed" );
3437 /* now create a memory stream */
3439 vlc_stream_MemoryNew( VLC_OBJECT(p_stream
),
3440 p_cmvd
->data
.p_cmvd
->p_data
,
3441 p_cmvd
->data
.p_cmvd
->i_uncompressed_size
, true );
3443 /* and read uncompressd moov */
3444 p_box
->data
.p_cmov
->p_moov
= MP4_ReadBox( p_stream_memory
, NULL
);
3446 vlc_stream_Delete( p_stream_memory
);
3449 msg_Dbg( p_stream
, "read box: \"cmov\" compressed movie header completed");
3452 return p_box
->data
.p_cmov
->p_moov
? 1 : 0;
3453 #endif /* HAVE_ZLIB_H */
3456 static void MP4_FreeBox_rdrf( MP4_Box_t
*p_box
)
3458 FREENULL( p_box
->data
.p_rdrf
->psz_ref
);
3461 static int MP4_ReadBox_rdrf( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3464 MP4_READBOX_ENTER( MP4_Box_data_rdrf_t
, MP4_FreeBox_rdrf
);
3466 MP4_GETVERSIONFLAGS( p_box
->data
.p_rdrf
);
3467 MP4_GETFOURCC( p_box
->data
.p_rdrf
->i_ref_type
);
3468 MP4_GET4BYTES( i_len
);
3473 p_box
->data
.p_rdrf
->psz_ref
= malloc( i_len
);
3474 if( p_box
->data
.p_rdrf
->psz_ref
== NULL
)
3475 MP4_READBOX_EXIT( 0 );
3478 for( unsigned i
= 0; i
< i_len
; i
++ )
3480 MP4_GET1BYTE( p_box
->data
.p_rdrf
->psz_ref
[i
] );
3482 p_box
->data
.p_rdrf
->psz_ref
[i_len
] = '\0';
3486 p_box
->data
.p_rdrf
->psz_ref
= NULL
;
3491 "read box: \"rdrf\" type:%4.4s ref %s",
3492 (char*)&p_box
->data
.p_rdrf
->i_ref_type
,
3493 p_box
->data
.p_rdrf
->psz_ref
);
3495 MP4_READBOX_EXIT( 1 );
3500 static int MP4_ReadBox_rmdr( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3502 MP4_READBOX_ENTER( MP4_Box_data_rmdr_t
, NULL
);
3504 MP4_GETVERSIONFLAGS( p_box
->data
.p_rmdr
);
3506 MP4_GET4BYTES( p_box
->data
.p_rmdr
->i_rate
);
3510 "read box: \"rmdr\" rate:%d",
3511 p_box
->data
.p_rmdr
->i_rate
);
3513 MP4_READBOX_EXIT( 1 );
3516 static int MP4_ReadBox_rmqu( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3518 MP4_READBOX_ENTER( MP4_Box_data_rmqu_t
, NULL
);
3520 MP4_GET4BYTES( p_box
->data
.p_rmqu
->i_quality
);
3524 "read box: \"rmqu\" quality:%d",
3525 p_box
->data
.p_rmqu
->i_quality
);
3527 MP4_READBOX_EXIT( 1 );
3530 static int MP4_ReadBox_rmvc( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3532 MP4_READBOX_ENTER( MP4_Box_data_rmvc_t
, NULL
);
3533 MP4_GETVERSIONFLAGS( p_box
->data
.p_rmvc
);
3535 MP4_GETFOURCC( p_box
->data
.p_rmvc
->i_gestaltType
);
3536 MP4_GET4BYTES( p_box
->data
.p_rmvc
->i_val1
);
3537 MP4_GET4BYTES( p_box
->data
.p_rmvc
->i_val2
);
3538 MP4_GET2BYTES( p_box
->data
.p_rmvc
->i_checkType
);
3542 "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
3543 (char*)&p_box
->data
.p_rmvc
->i_gestaltType
,
3544 p_box
->data
.p_rmvc
->i_val1
,p_box
->data
.p_rmvc
->i_val2
,
3545 p_box
->data
.p_rmvc
->i_checkType
);
3548 MP4_READBOX_EXIT( 1 );
3551 static int MP4_ReadBox_frma( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3553 MP4_READBOX_ENTER( MP4_Box_data_frma_t
, NULL
);
3555 MP4_GETFOURCC( p_box
->data
.p_frma
->i_type
);
3558 msg_Dbg( p_stream
, "read box: \"frma\" i_type:%4.4s",
3559 (char *)&p_box
->data
.p_frma
->i_type
);
3562 MP4_READBOX_EXIT( 1 );
3565 static int MP4_ReadBox_skcr( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3567 MP4_READBOX_ENTER( MP4_Box_data_skcr_t
, NULL
);
3569 MP4_GET4BYTES( p_box
->data
.p_skcr
->i_init
);
3570 MP4_GET4BYTES( p_box
->data
.p_skcr
->i_encr
);
3571 MP4_GET4BYTES( p_box
->data
.p_skcr
->i_decr
);
3574 msg_Dbg( p_stream
, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
3575 p_box
->data
.p_skcr
->i_init
,
3576 p_box
->data
.p_skcr
->i_encr
,
3577 p_box
->data
.p_skcr
->i_decr
);
3580 MP4_READBOX_EXIT( 1 );
3583 static int MP4_ReadBox_drms( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3586 /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
3587 * so unless data decrypt itself by magic, there will be no playback,
3588 * but we never know... */
3589 msg_Warn( p_stream
, "DRM protected streams are not supported." );
3593 static void MP4_FreeBox_Binary( MP4_Box_t
*p_box
)
3595 FREENULL( p_box
->data
.p_binary
->p_blob
);
3596 p_box
->data
.p_binary
->i_blob
= 0;
3599 static int MP4_ReadBox_Binary( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3601 MP4_READBOX_ENTER( MP4_Box_data_binary_t
, MP4_FreeBox_Binary
);
3602 i_read
= __MIN( i_read
, UINT32_MAX
);
3605 p_box
->data
.p_binary
->p_blob
= malloc( i_read
);
3606 if ( p_box
->data
.p_binary
->p_blob
)
3608 memcpy( p_box
->data
.p_binary
->p_blob
, p_peek
, i_read
);
3609 p_box
->data
.p_binary
->i_blob
= i_read
;
3612 MP4_READBOX_EXIT( 1 );
3615 static void MP4_FreeBox_data( MP4_Box_t
*p_box
)
3617 free( p_box
->data
.p_data
->p_blob
);
3620 static int MP4_ReadBox_data( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3622 MP4_READBOX_ENTER( MP4_Box_data_data_t
, MP4_FreeBox_data
);
3623 MP4_Box_data_data_t
*p_data
= p_box
->data
.p_data
;
3625 if ( i_read
< 8 || i_read
- 8 > UINT32_MAX
)
3626 MP4_READBOX_EXIT( 0 );
3629 MP4_GET1BYTE( i_type
);
3633 msg_Dbg( p_stream
, "skipping unknown 'data' atom with type %"PRIu8
, i_type
);
3635 MP4_READBOX_EXIT( 0 );
3638 MP4_GET3BYTES( p_data
->e_wellknowntype
);
3639 MP4_GET2BYTES( p_data
->locale
.i_country
);
3640 MP4_GET2BYTES( p_data
->locale
.i_language
);
3642 msg_Dbg( p_stream
, "read 'data' atom: knowntype=%"PRIu32
", country=%"PRIu16
" lang=%"PRIu16
3643 ", size %"PRIu64
" bytes", p_data
->e_wellknowntype
,
3644 p_data
->locale
.i_country
, p_data
->locale
.i_language
, i_read
);
3646 p_box
->data
.p_data
->p_blob
= malloc( i_read
);
3647 if ( !p_box
->data
.p_data
->p_blob
)
3648 MP4_READBOX_EXIT( 0 );
3650 p_box
->data
.p_data
->i_blob
= i_read
;
3651 memcpy( p_box
->data
.p_data
->p_blob
, p_peek
, i_read
);
3653 MP4_READBOX_EXIT( 1 );
3656 static int MP4_ReadBox_Metadata( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3658 const uint8_t *p_peek
;
3659 if ( vlc_stream_Peek( p_stream
, &p_peek
, 16 ) < 16 )
3661 if ( vlc_stream_Read( p_stream
, NULL
, 8 ) < 8 )
3663 const uint32_t stoplist
[] = { ATOM_data
, 0 };
3664 return MP4_ReadBoxContainerChildren( p_stream
, p_box
, stoplist
);
3667 /* Chapter support */
3668 static void MP4_FreeBox_chpl( MP4_Box_t
*p_box
)
3670 MP4_Box_data_chpl_t
*p_chpl
= p_box
->data
.p_chpl
;
3671 for( unsigned i
= 0; i
< p_chpl
->i_chapter
; i
++ )
3672 free( p_chpl
->chapter
[i
].psz_name
);
3675 static int MP4_ReadBox_chpl( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3677 MP4_Box_data_chpl_t
*p_chpl
;
3679 VLC_UNUSED(i_dummy
);
3681 MP4_READBOX_ENTER( MP4_Box_data_chpl_t
, MP4_FreeBox_chpl
);
3683 p_chpl
= p_box
->data
.p_chpl
;
3685 MP4_GETVERSIONFLAGS( p_chpl
);
3687 if ( i_read
< 5 || p_chpl
->i_version
!= 0x1 )
3688 MP4_READBOX_EXIT( 0 );
3690 MP4_GET4BYTES( i_dummy
);
3692 MP4_GET1BYTE( p_chpl
->i_chapter
);
3694 for( i
= 0; i
< p_chpl
->i_chapter
; i
++ )
3701 MP4_GET8BYTES( i_start
);
3702 MP4_GET1BYTE( i_len
);
3704 p_chpl
->chapter
[i
].psz_name
= malloc( i_len
+ 1 );
3705 if( !p_chpl
->chapter
[i
].psz_name
)
3706 MP4_READBOX_EXIT( 0 );
3708 i_copy
= __MIN( i_len
, i_read
);
3710 memcpy( p_chpl
->chapter
[i
].psz_name
, p_peek
, i_copy
);
3711 p_chpl
->chapter
[i
].psz_name
[i_copy
] = '\0';
3712 p_chpl
->chapter
[i
].i_start
= i_start
;
3718 if ( i
!= p_chpl
->i_chapter
)
3719 p_chpl
->i_chapter
= i
;
3721 /* Bubble sort by increasing start date */
3724 for( i
= 0; i
< p_chpl
->i_chapter
- 1; i
++ )
3726 if( p_chpl
->chapter
[i
].i_start
> p_chpl
->chapter
[i
+1].i_start
)
3728 char *psz
= p_chpl
->chapter
[i
+1].psz_name
;
3729 int64_t i64
= p_chpl
->chapter
[i
+1].i_start
;
3731 p_chpl
->chapter
[i
+1].psz_name
= p_chpl
->chapter
[i
].psz_name
;
3732 p_chpl
->chapter
[i
+1].i_start
= p_chpl
->chapter
[i
].i_start
;
3734 p_chpl
->chapter
[i
].psz_name
= psz
;
3735 p_chpl
->chapter
[i
].i_start
= i64
;
3744 msg_Dbg( p_stream
, "read box: \"chpl\" %d chapters",
3745 p_chpl
->i_chapter
);
3747 MP4_READBOX_EXIT( 1 );
3750 /* GoPro HiLight tags support */
3751 static void MP4_FreeBox_HMMT( MP4_Box_t
*p_box
)
3753 FREENULL( p_box
->data
.p_hmmt
->pi_chapter_start
);
3756 static int MP4_ReadBox_HMMT( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3758 #define MAX_CHAPTER_COUNT 100
3760 MP4_Box_data_HMMT_t
*p_hmmt
;
3761 MP4_READBOX_ENTER( MP4_Box_data_HMMT_t
, MP4_FreeBox_HMMT
);
3764 MP4_READBOX_EXIT( 0 );
3766 p_hmmt
= p_box
->data
.p_hmmt
;
3768 MP4_GET4BYTES( p_hmmt
->i_chapter_count
);
3770 if( p_hmmt
->i_chapter_count
<= 0 )
3772 p_hmmt
->pi_chapter_start
= NULL
;
3773 MP4_READBOX_EXIT( 1 );
3776 if( ( i_read
/ sizeof(uint32_t) ) < p_hmmt
->i_chapter_count
)
3777 MP4_READBOX_EXIT( 0 );
3779 /* Cameras are allowing a maximum of 100 tags */
3780 if( p_hmmt
->i_chapter_count
> MAX_CHAPTER_COUNT
)
3781 p_hmmt
->i_chapter_count
= MAX_CHAPTER_COUNT
;
3783 p_hmmt
->pi_chapter_start
= vlc_alloc( p_hmmt
->i_chapter_count
, sizeof(uint32_t) );
3784 if( p_hmmt
->pi_chapter_start
== NULL
)
3785 MP4_READBOX_EXIT( 0 );
3787 for( uint32_t i
= 0; i
< p_hmmt
->i_chapter_count
; i
++ )
3789 MP4_GET4BYTES( p_hmmt
->pi_chapter_start
[i
] );
3793 msg_Dbg( p_stream
, "read box: \"HMMT\" %d HiLight tags", p_hmmt
->i_chapter_count
);
3796 MP4_READBOX_EXIT( 1 );
3799 static void MP4_FreeBox_tref_generic( MP4_Box_t
*p_box
)
3801 FREENULL( p_box
->data
.p_tref_generic
->i_track_ID
);
3804 static int MP4_ReadBox_tref_generic( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3808 MP4_READBOX_ENTER( MP4_Box_data_tref_generic_t
, MP4_FreeBox_tref_generic
);
3810 p_box
->data
.p_tref_generic
->i_track_ID
= NULL
;
3811 count
= i_read
/ sizeof(uint32_t);
3812 p_box
->data
.p_tref_generic
->i_entry_count
= count
;
3813 p_box
->data
.p_tref_generic
->i_track_ID
= vlc_alloc( count
,
3815 if( p_box
->data
.p_tref_generic
->i_track_ID
== NULL
)
3816 MP4_READBOX_EXIT( 0 );
3818 for( unsigned i
= 0; i
< count
; i
++ )
3820 MP4_GET4BYTES( p_box
->data
.p_tref_generic
->i_track_ID
[i
] );
3823 msg_Dbg( p_stream
, "read box: \"chap\" %d references",
3824 p_box
->data
.p_tref_generic
->i_entry_count
);
3827 MP4_READBOX_EXIT( 1 );
3830 static void MP4_FreeBox_keys( MP4_Box_t
*p_box
)
3832 for( uint32_t i
=0; i
<p_box
->data
.p_keys
->i_entry_count
; i
++ )
3833 free( p_box
->data
.p_keys
->p_entries
[i
].psz_value
);
3834 free( p_box
->data
.p_keys
->p_entries
);
3837 static int MP4_ReadBox_keys( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3839 MP4_READBOX_ENTER( MP4_Box_data_keys_t
, MP4_FreeBox_keys
);
3842 MP4_READBOX_EXIT( 0 );
3845 MP4_GET4BYTES( i_count
); /* reserved + flags */
3847 MP4_READBOX_EXIT( 0 );
3849 MP4_GET4BYTES( i_count
);
3850 p_box
->data
.p_keys
->p_entries
= calloc( i_count
, sizeof(*p_box
->data
.p_keys
->p_entries
) );
3851 if ( !p_box
->data
.p_keys
->p_entries
)
3852 MP4_READBOX_EXIT( 0 );
3853 p_box
->data
.p_keys
->i_entry_count
= i_count
;
3856 for( ; i
< i_count
; i
++ )
3861 MP4_GET4BYTES( i_keysize
);
3862 if ( (i_keysize
< 8) || (i_keysize
- 4 > i_read
) )
3864 MP4_GETFOURCC( p_box
->data
.p_keys
->p_entries
[i
].i_namespace
);
3866 p_box
->data
.p_keys
->p_entries
[i
].psz_value
= malloc( i_keysize
+ 1 );
3867 if ( !p_box
->data
.p_keys
->p_entries
[i
].psz_value
)
3869 memcpy( p_box
->data
.p_keys
->p_entries
[i
].psz_value
, p_peek
, i_keysize
);
3870 p_box
->data
.p_keys
->p_entries
[i
].psz_value
[i_keysize
] = 0;
3871 p_peek
+= i_keysize
;
3872 i_read
-= i_keysize
;
3873 #ifdef MP4_ULTRA_VERBOSE
3874 msg_Dbg( p_stream
, "read box: \"keys\": %u '%s'", i
+ 1,
3875 p_box
->data
.p_keys
->p_entries
[i
].psz_value
);
3879 p_box
->data
.p_keys
->i_entry_count
= i
;
3881 MP4_READBOX_EXIT( 1 );
3884 static int MP4_ReadBox_colr( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3886 MP4_READBOX_ENTER( MP4_Box_data_colr_t
, NULL
);
3887 MP4_GETFOURCC( p_box
->data
.p_colr
->i_type
);
3888 if ( p_box
->data
.p_colr
->i_type
== VLC_FOURCC( 'n', 'c', 'l', 'c' ) ||
3889 p_box
->data
.p_colr
->i_type
== VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3891 MP4_GET2BYTES( p_box
->data
.p_colr
->nclc
.i_primary_idx
);
3892 MP4_GET2BYTES( p_box
->data
.p_colr
->nclc
.i_transfer_function_idx
);
3893 MP4_GET2BYTES( p_box
->data
.p_colr
->nclc
.i_matrix_idx
);
3894 if ( p_box
->data
.p_colr
->i_type
== VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
3895 MP4_GET1BYTE( p_box
->data
.p_colr
->nclc
.i_full_range
);
3900 msg_Warn( p_stream
, "Unhandled colr type: %4.4s", (char*)&p_box
->data
.p_colr
->i_type
);
3903 MP4_READBOX_EXIT( 1 );
3906 static int MP4_ReadBox_meta( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3908 const uint8_t *p_peek
;
3909 const size_t i_headersize
= mp4_box_headersize( p_box
);
3911 if( p_box
->i_size
< 16 || p_box
->i_size
- i_headersize
< 8 )
3914 /* skip over box header */
3915 if( vlc_stream_Read( p_stream
, NULL
, i_headersize
) < (ssize_t
) i_headersize
)
3918 /* meta content starts with a 4 byte version/flags value (should be 0) */
3919 if( vlc_stream_Peek( p_stream
, &p_peek
, 8 ) < 8 )
3922 if( !memcmp( p_peek
, "\0\0\0", 4 ) ) /* correct header case */
3924 if( vlc_stream_Read( p_stream
, NULL
, 4 ) < 4 )
3927 else if( memcmp( &p_peek
[4], "hdlr", 4 ) ) /* Broken, headerless ones */
3932 /* load child atoms up to the handler (which should be next anyway) */
3933 const uint32_t stoplist
[] = { ATOM_hdlr
, 0 };
3934 if ( !MP4_ReadBoxContainerChildren( p_stream
, p_box
, stoplist
) )
3938 const MP4_Box_t
*p_hdlr
= MP4_BoxGet( p_box
, "hdlr" );
3939 if ( p_hdlr
&& BOXDATA(p_hdlr
) && BOXDATA(p_hdlr
)->i_version
== 0 )
3941 p_box
->i_handler
= BOXDATA(p_hdlr
)->i_handler_type
;
3942 switch( p_box
->i_handler
)
3946 /* then it behaves like a container */
3947 return MP4_ReadBoxContainerChildren( p_stream
, p_box
, NULL
);
3949 /* skip parsing, will be seen as empty container */
3957 static int MP4_ReadBox_iods( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3960 VLC_UNUSED(i_unused
);
3962 MP4_READBOX_ENTER( MP4_Box_data_iods_t
, NULL
);
3963 MP4_GETVERSIONFLAGS( p_box
->data
.p_iods
);
3965 MP4_GET1BYTE( i_unused
); /* tag */
3966 MP4_GET1BYTE( i_unused
); /* length */
3968 MP4_GET2BYTES( p_box
->data
.p_iods
->i_object_descriptor
); /* 10bits, 6 other bits
3969 are used for other flags */
3970 MP4_GET1BYTE( p_box
->data
.p_iods
->i_OD_profile_level
);
3971 MP4_GET1BYTE( p_box
->data
.p_iods
->i_scene_profile_level
);
3972 MP4_GET1BYTE( p_box
->data
.p_iods
->i_audio_profile_level
);
3973 MP4_GET1BYTE( p_box
->data
.p_iods
->i_visual_profile_level
);
3974 MP4_GET1BYTE( p_box
->data
.p_iods
->i_graphics_profile_level
);
3978 "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
3979 "visual: %i, graphics: %i",
3980 p_box
->data
.p_iods
->i_object_descriptor
>> 6,
3981 p_box
->data
.p_iods
->i_OD_profile_level
,
3982 p_box
->data
.p_iods
->i_scene_profile_level
,
3983 p_box
->data
.p_iods
->i_audio_profile_level
,
3984 p_box
->data
.p_iods
->i_visual_profile_level
,
3985 p_box
->data
.p_iods
->i_graphics_profile_level
);
3988 MP4_READBOX_EXIT( 1 );
3991 static int MP4_ReadBox_btrt( stream_t
*p_stream
, MP4_Box_t
*p_box
)
3993 MP4_READBOX_ENTER( MP4_Box_data_btrt_t
, NULL
);
3996 MP4_READBOX_EXIT( 0 );
3998 MP4_GET4BYTES( p_box
->data
.p_btrt
->i_buffer_size
);
3999 MP4_GET4BYTES( p_box
->data
.p_btrt
->i_max_bitrate
);
4000 MP4_GET4BYTES( p_box
->data
.p_btrt
->i_avg_bitrate
);
4002 MP4_READBOX_EXIT( 1 );
4005 static int MP4_ReadBox_pasp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4007 MP4_READBOX_ENTER( MP4_Box_data_pasp_t
, NULL
);
4009 MP4_GET4BYTES( p_box
->data
.p_pasp
->i_horizontal_spacing
);
4010 MP4_GET4BYTES( p_box
->data
.p_pasp
->i_vertical_spacing
);
4014 "read box: \"paps\" %dx%d",
4015 p_box
->data
.p_pasp
->i_horizontal_spacing
,
4016 p_box
->data
.p_pasp
->i_vertical_spacing
);
4019 MP4_READBOX_EXIT( 1 );
4022 static int MP4_ReadBox_mehd( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4024 MP4_READBOX_ENTER( MP4_Box_data_mehd_t
, NULL
);
4026 MP4_GETVERSIONFLAGS( p_box
->data
.p_mehd
);
4027 if( p_box
->data
.p_mehd
->i_version
== 1 )
4028 MP4_GET8BYTES( p_box
->data
.p_mehd
->i_fragment_duration
);
4029 else /* version == 0 */
4030 MP4_GET4BYTES( p_box
->data
.p_mehd
->i_fragment_duration
);
4034 "read box: \"mehd\" frag dur. %"PRIu64
"",
4035 p_box
->data
.p_mehd
->i_fragment_duration
);
4038 MP4_READBOX_EXIT( 1 );
4041 static int MP4_ReadBox_trex( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4043 MP4_READBOX_ENTER( MP4_Box_data_trex_t
, NULL
);
4044 MP4_GETVERSIONFLAGS( p_box
->data
.p_trex
);
4046 MP4_GET4BYTES( p_box
->data
.p_trex
->i_track_ID
);
4047 MP4_GET4BYTES( p_box
->data
.p_trex
->i_default_sample_description_index
);
4048 MP4_GET4BYTES( p_box
->data
.p_trex
->i_default_sample_duration
);
4049 MP4_GET4BYTES( p_box
->data
.p_trex
->i_default_sample_size
);
4050 MP4_GET4BYTES( p_box
->data
.p_trex
->i_default_sample_flags
);
4054 "read box: \"trex\" trackID: %"PRIu32
"",
4055 p_box
->data
.p_trex
->i_track_ID
);
4058 MP4_READBOX_EXIT( 1 );
4061 static void MP4_FreeBox_sdtp( MP4_Box_t
*p_box
)
4063 FREENULL( p_box
->data
.p_sdtp
->p_sample_table
);
4066 static int MP4_ReadBox_sdtp( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4068 uint32_t i_sample_count
;
4069 MP4_READBOX_ENTER( MP4_Box_data_sdtp_t
, MP4_FreeBox_sdtp
);
4070 MP4_Box_data_sdtp_t
*p_sdtp
= p_box
->data
.p_sdtp
;
4071 MP4_GETVERSIONFLAGS( p_box
->data
.p_sdtp
);
4072 i_sample_count
= i_read
;
4074 p_sdtp
->p_sample_table
= malloc( i_sample_count
);
4075 if( unlikely(p_sdtp
->p_sample_table
== NULL
) )
4076 MP4_READBOX_EXIT( 0 );
4078 for( uint32_t i
= 0; i
< i_sample_count
; i
++ )
4079 MP4_GET1BYTE( p_sdtp
->p_sample_table
[i
] );
4082 msg_Dbg( p_stream
, "i_sample_count is %"PRIu32
"", i_sample_count
);
4083 if ( i_sample_count
> 3 )
4085 "read box: \"sdtp\" head: %"PRIx8
" %"PRIx8
" %"PRIx8
" %"PRIx8
"",
4086 p_sdtp
->p_sample_table
[0],
4087 p_sdtp
->p_sample_table
[1],
4088 p_sdtp
->p_sample_table
[2],
4089 p_sdtp
->p_sample_table
[3] );
4092 MP4_READBOX_EXIT( 1 );
4095 static int MP4_ReadBox_tsel( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4097 MP4_READBOX_ENTER( MP4_Box_data_tsel_t
, NULL
);
4099 MP4_GET4BYTES( i_version
);
4100 if ( i_version
!= 0 || i_read
< 4 )
4101 MP4_READBOX_EXIT( 0 );
4102 MP4_GET4BYTES( p_box
->data
.p_tsel
->i_switch_group
);
4103 /* ignore list of attributes as es are present before switch */
4104 MP4_READBOX_EXIT( 1 );
4107 static int MP4_ReadBox_mfro( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4109 MP4_READBOX_ENTER( MP4_Box_data_mfro_t
, NULL
);
4111 MP4_GETVERSIONFLAGS( p_box
->data
.p_mfro
);
4112 MP4_GET4BYTES( p_box
->data
.p_mfro
->i_size
);
4116 "read box: \"mfro\" size: %"PRIu32
"",
4117 p_box
->data
.p_mfro
->i_size
);
4120 MP4_READBOX_EXIT( 1 );
4123 static void MP4_FreeBox_tfra( MP4_Box_t
*p_box
)
4125 FREENULL( p_box
->data
.p_tfra
->p_time
);
4126 FREENULL( p_box
->data
.p_tfra
->p_moof_offset
);
4127 FREENULL( p_box
->data
.p_tfra
->p_traf_number
);
4128 FREENULL( p_box
->data
.p_tfra
->p_trun_number
);
4129 FREENULL( p_box
->data
.p_tfra
->p_sample_number
);
4132 static int MP4_ReadBox_tfra( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4134 #define READ_VARIABLE_LENGTH(lengthvar, p_array) switch (lengthvar)\
4137 MP4_GET1BYTE( p_array[i] );\
4140 MP4_GET2BYTES( *((uint16_t *)&p_array[i*2]) );\
4143 MP4_GET3BYTES( *((uint32_t *)&p_array[i*4]) );\
4146 MP4_GET4BYTES( *((uint32_t *)&p_array[i*4]) );\
4151 #define FIX_VARIABLE_LENGTH(lengthvar) if ( lengthvar == 3 ) lengthvar = 4
4153 uint32_t i_number_of_entries
;
4154 MP4_READBOX_ENTER( MP4_Box_data_tfra_t
, MP4_FreeBox_tfra
);
4155 MP4_Box_data_tfra_t
*p_tfra
= p_box
->data
.p_tfra
;
4156 MP4_GETVERSIONFLAGS( p_box
->data
.p_tfra
);
4157 if ( p_tfra
->i_version
> 1 )
4158 MP4_READBOX_EXIT( 0 );
4159 MP4_GET4BYTES( p_tfra
->i_track_ID
);
4160 uint32_t i_lengths
= 0;
4161 MP4_GET4BYTES( i_lengths
);
4162 MP4_GET4BYTES( p_tfra
->i_number_of_entries
);
4163 i_number_of_entries
= p_tfra
->i_number_of_entries
;
4164 p_tfra
->i_length_size_of_traf_num
= i_lengths
>> 4;
4165 p_tfra
->i_length_size_of_trun_num
= ( i_lengths
& 0x0c ) >> 2;
4166 p_tfra
->i_length_size_of_sample_num
= i_lengths
& 0x03;
4168 size_t size
= 4 + 4*p_tfra
->i_version
; /* size in {4, 8} */
4169 p_tfra
->p_time
= calloc( i_number_of_entries
, size
);
4170 p_tfra
->p_moof_offset
= calloc( i_number_of_entries
, size
);
4172 size
= 1 + p_tfra
->i_length_size_of_traf_num
; /* size in [|1, 4|] */
4173 if ( size
== 3 ) size
++;
4174 p_tfra
->p_traf_number
= calloc( i_number_of_entries
, size
);
4175 size
= 1 + p_tfra
->i_length_size_of_trun_num
;
4176 if ( size
== 3 ) size
++;
4177 p_tfra
->p_trun_number
= calloc( i_number_of_entries
, size
);
4178 size
= 1 + p_tfra
->i_length_size_of_sample_num
;
4179 if ( size
== 3 ) size
++;
4180 p_tfra
->p_sample_number
= calloc( i_number_of_entries
, size
);
4182 if( !p_tfra
->p_time
|| !p_tfra
->p_moof_offset
|| !p_tfra
->p_traf_number
4183 || !p_tfra
->p_trun_number
|| !p_tfra
->p_sample_number
)
4186 unsigned i_fields_length
= 3 + p_tfra
->i_length_size_of_traf_num
4187 + p_tfra
->i_length_size_of_trun_num
4188 + p_tfra
->i_length_size_of_sample_num
;
4191 for( i
= 0; i
< i_number_of_entries
; i
++ )
4194 if( p_tfra
->i_version
== 1 )
4196 if ( i_read
< i_fields_length
+ 16 )
4198 MP4_GET8BYTES( *((uint64_t *)&p_tfra
->p_time
[i
*2]) );
4199 MP4_GET8BYTES( *((uint64_t *)&p_tfra
->p_moof_offset
[i
*2]) );
4203 if ( i_read
< i_fields_length
+ 8 )
4205 MP4_GET4BYTES( p_tfra
->p_time
[i
] );
4206 MP4_GET4BYTES( p_tfra
->p_moof_offset
[i
] );
4209 READ_VARIABLE_LENGTH(p_tfra
->i_length_size_of_traf_num
, p_tfra
->p_traf_number
);
4210 READ_VARIABLE_LENGTH(p_tfra
->i_length_size_of_trun_num
, p_tfra
->p_trun_number
);
4211 READ_VARIABLE_LENGTH(p_tfra
->i_length_size_of_sample_num
, p_tfra
->p_sample_number
);
4213 if ( i
< i_number_of_entries
)
4214 i_number_of_entries
= i
;
4216 FIX_VARIABLE_LENGTH(p_tfra
->i_length_size_of_traf_num
);
4217 FIX_VARIABLE_LENGTH(p_tfra
->i_length_size_of_trun_num
);
4218 FIX_VARIABLE_LENGTH(p_tfra
->i_length_size_of_sample_num
);
4220 #ifdef MP4_ULTRA_VERBOSE
4221 for( i
= 0; i
< i_number_of_entries
; i
++ )
4223 if( p_tfra
->i_version
== 0 )
4225 msg_Dbg( p_stream
, "tfra[%"PRIu32
"] time[%"PRIu32
"]: %"PRIu32
", "
4226 "moof_offset[%"PRIu32
"]: %"PRIu32
"",
4228 i
, p_tfra
->p_time
[i
],
4229 i
, p_tfra
->p_moof_offset
[i
] );
4233 msg_Dbg( p_stream
, "tfra[%"PRIu32
"] time[%"PRIu32
"]: %"PRIu64
", "
4234 "moof_offset[%"PRIu32
"]: %"PRIu64
"",
4236 i
, ((uint64_t *)(p_tfra
->p_time
))[i
],
4237 i
, ((uint64_t *)(p_tfra
->p_moof_offset
))[i
] );
4242 msg_Dbg( p_stream
, "tfra[%"PRIu32
"] %"PRIu32
" entries",
4243 p_tfra
->i_track_ID
, i_number_of_entries
);
4246 MP4_READBOX_EXIT( 1 );
4248 MP4_READBOX_EXIT( 0 );
4250 #undef READ_VARIABLE_LENGTH
4251 #undef FIX_VARIABLE_LENGTH
4254 static int MP4_ReadBox_pnot( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4256 if ( p_box
->i_size
!= 20 )
4258 MP4_READBOX_ENTER( MP4_Box_data_pnot_t
, NULL
);
4259 MP4_GET4BYTES( p_box
->data
.p_pnot
->i_date
);
4261 MP4_GET2BYTES( i_version
);
4262 if ( i_version
!= 0 )
4263 MP4_READBOX_EXIT( 0 );
4264 MP4_GETFOURCC( p_box
->data
.p_pnot
->i_type
);
4265 MP4_GET2BYTES( p_box
->data
.p_pnot
->i_index
);
4266 MP4_READBOX_EXIT( 1 );
4269 static int MP4_ReadBox_SA3D( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4271 MP4_READBOX_ENTER( MP4_Box_data_SA3D_t
, NULL
);
4274 MP4_GET1BYTE( i_version
);
4275 if ( i_version
!= 0 )
4276 MP4_READBOX_EXIT( 0 );
4278 MP4_GET1BYTE( p_box
->data
.p_SA3D
->i_ambisonic_type
);
4279 MP4_GET4BYTES( p_box
->data
.p_SA3D
->i_ambisonic_order
);
4280 MP4_GET1BYTE( p_box
->data
.p_SA3D
->i_ambisonic_channel_ordering
);
4281 MP4_GET1BYTE( p_box
->data
.p_SA3D
->i_ambisonic_normalization
);
4282 MP4_GET4BYTES( p_box
->data
.p_SA3D
->i_num_channels
);
4283 MP4_READBOX_EXIT( 1 );
4287 static int MP4_ReadBox_default( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4289 if( !p_box
->p_father
)
4293 if( p_box
->p_father
->i_type
== ATOM_stsd
)
4295 MP4_Box_t
*p_mdia
= MP4_BoxGet( p_box
, "../../../.." );
4298 if( p_mdia
== NULL
|| p_mdia
->i_type
!= ATOM_mdia
||
4299 (p_hdlr
= MP4_BoxGet( p_mdia
, "hdlr" )) == NULL
)
4303 switch( p_hdlr
->data
.p_hdlr
->i_handler_type
)
4306 return MP4_ReadBox_sample_soun( p_stream
, p_box
);
4308 return MP4_ReadBox_sample_vide( p_stream
, p_box
);
4310 return MP4_ReadBox_sample_hint8( p_stream
, p_box
);
4312 return MP4_ReadBox_sample_text( p_stream
, p_box
);
4315 return MP4_ReadBox_sample_tx3g( p_stream
, p_box
);
4318 "unknown handler type in stsd (incompletely loaded)" );
4324 if MP4_BOX_TYPE_ASCII()
4326 "unknown box type %4.4s (incompletely loaded)",
4327 (char*)&p_box
->i_type
);
4330 "unknown box type c%3.3s (incompletely loaded)",
4331 (char*)&p_box
->i_type
+1 );
4332 p_box
->e_flags
|= BOX_FLAG_INCOMPLETE
;
4337 /**** ------------------------------------------------------------------- ****/
4339 static int MP4_ReadBox_uuid( stream_t
*p_stream
, MP4_Box_t
*p_box
)
4341 if( !CmpUUID( &p_box
->i_uuid
, &TfrfBoxUUID
) )
4342 return MP4_ReadBox_tfrf( p_stream
, p_box
);
4343 if( !CmpUUID( &p_box
->i_uuid
, &TfxdBoxUUID
) )
4344 return MP4_ReadBox_tfxd( p_stream
, p_box
);
4345 if( !CmpUUID( &p_box
->i_uuid
, &XML360BoxUUID
) )
4346 return MP4_ReadBox_XML360( p_stream
, p_box
);
4347 if( !CmpUUID( &p_box
->i_uuid
, &PS3DDSBoxUUID
) && p_box
->i_size
== 28 )
4348 return MP4_ReadBox_Binary( p_stream
, p_box
);
4351 msg_Warn( p_stream
, "Unknown uuid type box: "
4352 "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-"
4353 "%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
4354 p_box
->i_uuid
.b
[0], p_box
->i_uuid
.b
[1], p_box
->i_uuid
.b
[2], p_box
->i_uuid
.b
[3],
4355 p_box
->i_uuid
.b
[4], p_box
->i_uuid
.b
[5], p_box
->i_uuid
.b
[6], p_box
->i_uuid
.b
[7],
4356 p_box
->i_uuid
.b
[8], p_box
->i_uuid
.b
[9], p_box
->i_uuid
.b
[10], p_box
->i_uuid
.b
[11],
4357 p_box
->i_uuid
.b
[12], p_box
->i_uuid
.b
[13], p_box
->i_uuid
.b
[14], p_box
->i_uuid
.b
[15] );
4359 msg_Warn( p_stream
, "Unknown uuid type box" );
4364 /**** ------------------------------------------------------------------- ****/
4365 /**** "Higher level" Functions ****/
4366 /**** ------------------------------------------------------------------- ****/
4371 int (*MP4_ReadBox_function
)( stream_t
*p_stream
, MP4_Box_t
*p_box
);
4372 uint32_t i_parent
; /* set parent to restrict, duplicating if needed; 0 for any */
4373 } MP4_Box_Function
[] =
4376 { ATOM_moov
, MP4_ReadBoxContainer
, 0 },
4377 { ATOM_foov
, MP4_ReadBoxContainer
, 0 },
4378 { ATOM_trak
, MP4_ReadBoxContainer
, ATOM_moov
},
4379 { ATOM_trak
, MP4_ReadBoxContainer
, ATOM_foov
},
4380 { ATOM_mdia
, MP4_ReadBoxContainer
, ATOM_trak
},
4381 { ATOM_moof
, MP4_ReadBoxContainer
, 0 },
4382 { ATOM_minf
, MP4_ReadBoxContainer
, ATOM_mdia
},
4383 { ATOM_stbl
, MP4_ReadBoxContainer
, ATOM_minf
},
4384 { ATOM_dinf
, MP4_ReadBoxContainer
, ATOM_minf
},
4385 { ATOM_dinf
, MP4_ReadBoxContainer
, ATOM_meta
},
4386 { ATOM_edts
, MP4_ReadBoxContainer
, ATOM_trak
},
4387 { ATOM_udta
, MP4_ReadBoxContainer
, 0 },
4388 { ATOM_nmhd
, MP4_ReadBoxContainer
, ATOM_minf
},
4389 { ATOM_hnti
, MP4_ReadBoxContainer
, ATOM_udta
},
4390 { ATOM_rmra
, MP4_ReadBoxContainer
, ATOM_moov
},
4391 { ATOM_rmda
, MP4_ReadBoxContainer
, ATOM_rmra
},
4392 { ATOM_tref
, MP4_ReadBoxContainer
, ATOM_trak
},
4393 { ATOM_gmhd
, MP4_ReadBoxContainer
, ATOM_minf
},
4394 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_stsd
},
4395 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_mp4a
}, /* some quicktime mp4a/wave/mp4a.. */
4396 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_WMA2
}, /* flip4mac */
4397 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_in24
},
4398 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_in32
},
4399 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_fl32
},
4400 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_fl64
},
4401 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_QDMC
},
4402 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_QDM2
},
4403 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_XiFL
}, /* XiphQT */
4404 { ATOM_wave
, MP4_ReadBoxContainer
, ATOM_XiVs
}, /* XiphQT */
4405 { ATOM_ilst
, MP4_ReadBox_ilst
, ATOM_meta
},
4406 { ATOM_mvex
, MP4_ReadBoxContainer
, ATOM_moov
},
4407 { ATOM_mvex
, MP4_ReadBoxContainer
, ATOM_ftyp
},
4410 { ATOM_ftyp
, MP4_ReadBox_ftyp
, 0 },
4411 { ATOM_styp
, MP4_ReadBox_ftyp
, 0 },
4412 { ATOM_cmov
, MP4_ReadBox_cmov
, 0 },
4413 { ATOM_mvhd
, MP4_ReadBox_mvhd
, ATOM_moov
},
4414 { ATOM_mvhd
, MP4_ReadBox_mvhd
, ATOM_foov
},
4415 { ATOM_tkhd
, MP4_ReadBox_tkhd
, ATOM_trak
},
4416 { ATOM_load
, MP4_ReadBox_load
, ATOM_trak
},
4417 { ATOM_mdhd
, MP4_ReadBox_mdhd
, ATOM_mdia
},
4418 { ATOM_hdlr
, MP4_ReadBox_hdlr
, ATOM_mdia
},
4419 { ATOM_hdlr
, MP4_ReadBox_hdlr
, ATOM_meta
},
4420 { ATOM_hdlr
, MP4_ReadBox_hdlr
, ATOM_minf
},
4421 { ATOM_vmhd
, MP4_ReadBox_vmhd
, ATOM_minf
},
4422 { ATOM_smhd
, MP4_ReadBox_smhd
, ATOM_minf
},
4423 { ATOM_hmhd
, MP4_ReadBox_hmhd
, ATOM_minf
},
4424 { ATOM_alis
, MP4_ReadBoxSkip
, ATOM_dref
},
4425 { ATOM_url
, MP4_ReadBox_url
, 0 },
4426 { ATOM_urn
, MP4_ReadBox_urn
, 0 },
4427 { ATOM_dref
, MP4_ReadBox_LtdContainer
, 0 },
4428 { ATOM_stts
, MP4_ReadBox_stts
, ATOM_stbl
},
4429 { ATOM_ctts
, MP4_ReadBox_ctts
, ATOM_stbl
},
4430 { ATOM_cslg
, MP4_ReadBox_cslg
, ATOM_stbl
},
4431 { ATOM_stsd
, MP4_ReadBox_LtdContainer
, ATOM_stbl
},
4432 { ATOM_stsz
, MP4_ReadBox_stsz
, ATOM_stbl
},
4433 { ATOM_stsc
, MP4_ReadBox_stsc
, ATOM_stbl
},
4434 { ATOM_stco
, MP4_ReadBox_stco_co64
, ATOM_stbl
},
4435 { ATOM_co64
, MP4_ReadBox_stco_co64
, ATOM_stbl
},
4436 { ATOM_stss
, MP4_ReadBox_stss
, ATOM_stbl
},
4437 { ATOM_stsh
, MP4_ReadBox_stsh
, ATOM_stbl
},
4438 { ATOM_stdp
, MP4_ReadBox_stdp
, 0 },
4439 { ATOM_padb
, MP4_ReadBox_padb
, 0 },
4440 { ATOM_elst
, MP4_ReadBox_elst
, ATOM_edts
},
4441 { ATOM_cprt
, MP4_ReadBox_cprt
, 0 },
4442 { ATOM_esds
, MP4_ReadBox_esds
, ATOM_wave
}, /* mp4a in wave chunk */
4443 { ATOM_esds
, MP4_ReadBox_esds
, ATOM_mp4a
},
4444 { ATOM_esds
, MP4_ReadBox_esds
, ATOM_mp4v
},
4445 { ATOM_esds
, MP4_ReadBox_esds
, ATOM_mp4s
},
4446 { ATOM_dcom
, MP4_ReadBox_dcom
, 0 },
4447 { ATOM_dfLa
, MP4_ReadBox_Binary
, ATOM_fLaC
},
4448 { ATOM_cmvd
, MP4_ReadBox_cmvd
, 0 },
4449 { ATOM_avcC
, MP4_ReadBox_avcC
, ATOM_avc1
},
4450 { ATOM_avcC
, MP4_ReadBox_avcC
, ATOM_avc3
},
4451 { ATOM_hvcC
, MP4_ReadBox_Binary
, 0 },
4452 { ATOM_vpcC
, MP4_ReadBox_vpcC
, ATOM_vp08
},
4453 { ATOM_vpcC
, MP4_ReadBox_vpcC
, ATOM_vp09
},
4454 { ATOM_vpcC
, MP4_ReadBox_vpcC
, ATOM_vp10
},
4455 { ATOM_dac3
, MP4_ReadBox_dac3
, 0 },
4456 { ATOM_dec3
, MP4_ReadBox_dec3
, 0 },
4457 { ATOM_dvc1
, MP4_ReadBox_dvc1
, ATOM_vc1
},
4458 { ATOM_fiel
, MP4_ReadBox_fiel
, 0 },
4459 { ATOM_glbl
, MP4_ReadBox_Binary
, ATOM_FFV1
},
4460 { ATOM_enda
, MP4_ReadBox_enda
, 0 },
4461 { ATOM_iods
, MP4_ReadBox_iods
, 0 },
4462 { ATOM_pasp
, MP4_ReadBox_pasp
, 0 },
4463 { ATOM_btrt
, MP4_ReadBox_btrt
, 0 }, /* codecs bitrate stsd/????/btrt */
4464 { ATOM_keys
, MP4_ReadBox_keys
, ATOM_meta
},
4465 { ATOM_colr
, MP4_ReadBox_colr
, 0 },
4468 { ATOM_vCtH
, MP4_ReadBox_Binary
, ATOM_wave
},
4469 { ATOM_vCtC
, MP4_ReadBox_Binary
, ATOM_wave
},
4470 { ATOM_vCtd
, MP4_ReadBox_Binary
, ATOM_wave
},
4471 { ATOM_fCtS
, MP4_ReadBox_Binary
, ATOM_wave
},
4473 /* Samples groups specific information */
4474 { ATOM_sbgp
, MP4_ReadBox_sbgp
, ATOM_stbl
},
4475 { ATOM_sbgp
, MP4_ReadBox_sbgp
, ATOM_traf
},
4476 { ATOM_sgpd
, MP4_ReadBox_sgpd
, ATOM_stbl
},
4477 { ATOM_sgpd
, MP4_ReadBox_sgpd
, ATOM_traf
},
4479 /* Quicktime preview atoms, all at root */
4480 { ATOM_pnot
, MP4_ReadBox_pnot
, 0 },
4481 { ATOM_pict
, MP4_ReadBox_Binary
, 0 },
4482 { ATOM_PICT
, MP4_ReadBox_Binary
, 0 },
4484 /* Nothing to do with this box */
4485 { ATOM_mdat
, MP4_ReadBoxSkip
, 0 },
4486 { ATOM_skip
, MP4_ReadBoxSkip
, 0 },
4487 { ATOM_free
, MP4_ReadBoxSkip
, 0 },
4488 { ATOM_wide
, MP4_ReadBoxSkip
, 0 },
4489 { ATOM_binm
, MP4_ReadBoxSkip
, 0 },
4492 { ATOM_tx3g
, MP4_ReadBox_sample_tx3g
, 0 },
4493 { ATOM_c608
, MP4_ReadBox_sample_clcp
, ATOM_stsd
},
4494 //{ ATOM_text, MP4_ReadBox_sample_text, 0 },
4495 /* In sample WebVTT subtitle atoms. No ATOM_wvtt in normal parsing */
4496 { ATOM_vttc
, MP4_ReadBoxContainer
, ATOM_wvtt
},
4497 { ATOM_payl
, MP4_ReadBox_Binary
, ATOM_vttc
},
4500 { ATOM_soun
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4501 { ATOM_agsm
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4502 { ATOM_ac3
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4503 { ATOM_eac3
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4504 { ATOM_fLaC
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4505 { ATOM_lpcm
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4506 { ATOM_ms02
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4507 { ATOM_ms11
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4508 { ATOM_ms55
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4509 { ATOM__mp3
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4510 { ATOM_mp4a
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4511 { ATOM_twos
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4512 { ATOM_sowt
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4513 { ATOM_QDMC
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4514 { ATOM_QDM2
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4515 { ATOM_ima4
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4516 { ATOM_IMA4
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4517 { ATOM_dvi
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4518 { ATOM_alaw
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4519 { ATOM_ulaw
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4520 { ATOM_MAC3
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4521 { ATOM_MAC6
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4522 { ATOM_Qclp
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4523 { ATOM_samr
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4524 { ATOM_sawb
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4525 { ATOM_OggS
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4526 { ATOM_alac
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4527 { ATOM_WMA2
, MP4_ReadBox_sample_soun
, ATOM_stsd
}, /* flip4mac */
4528 { ATOM_wma
, MP4_ReadBox_sample_soun
, ATOM_stsd
}, /* ismv wmapro */
4529 { ATOM_Opus
, MP4_ReadBox_sample_soun
, ATOM_stsd
},
4530 /* Sound extensions */
4531 { ATOM_chan
, MP4_ReadBox_stsdext_chan
, 0 },
4532 { ATOM_WMA2
, MP4_ReadBox_WMA2
, ATOM_wave
}, /* flip4mac */
4533 { ATOM_dOps
, MP4_ReadBox_Binary
, ATOM_Opus
},
4534 { ATOM_wfex
, MP4_ReadBox_WMA2
, ATOM_wma
}, /* ismv formatex */
4536 /* Both uncompressed sound and video */
4537 { ATOM_raw
, MP4_ReadBox_default
, ATOM_stsd
},
4539 { ATOM_drmi
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4540 { ATOM_vide
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4541 { ATOM_mp4v
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4542 { ATOM_SVQ1
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4543 { ATOM_SVQ3
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4544 { ATOM_ZyGo
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4545 { ATOM_DIVX
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4546 { ATOM_XVID
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4547 { ATOM_h263
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4548 { ATOM_s263
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4549 { ATOM_cvid
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4550 { ATOM_3IV1
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4551 { ATOM_3iv1
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4552 { ATOM_3IV2
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4553 { ATOM_3iv2
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4554 { ATOM_3IVD
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4555 { ATOM_3ivd
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4556 { ATOM_3VID
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4557 { ATOM_3vid
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4558 { ATOM_FFV1
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4559 { ATOM_mjpa
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4560 { ATOM_mjpb
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4561 { ATOM_qdrw
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4562 { ATOM_mp2v
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4563 { ATOM_hdv2
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4564 { ATOM_WMV3
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4566 { ATOM_mjqt
, MP4_ReadBox_default
, 0 }, /* found in mjpa/b */
4567 { ATOM_mjht
, MP4_ReadBox_default
, 0 },
4569 { ATOM_dvc
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4570 { ATOM_dvp
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4571 { ATOM_dv5n
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4572 { ATOM_dv5p
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4573 { ATOM_VP31
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4574 { ATOM_vp31
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4575 { ATOM_h264
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4577 { ATOM_jpeg
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4578 { ATOM_vc1
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4579 { ATOM_avc1
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4580 { ATOM_avc3
, MP4_ReadBox_sample_vide
, ATOM_stsd
},
4582 { ATOM_rrtp
, MP4_ReadBox_sample_hint8
, ATOM_stsd
},
4584 { ATOM_yv12
, MP4_ReadBox_sample_vide
, 0 },
4585 { ATOM_yuv2
, MP4_ReadBox_sample_vide
, 0 },
4587 { ATOM_strf
, MP4_ReadBox_strf
, ATOM_WMV3
}, /* flip4mac */
4588 { ATOM_ASF
, MP4_ReadBox_ASF
, ATOM_WMV3
}, /* flip4mac */
4589 { ATOM_ASF
, MP4_ReadBox_ASF
, ATOM_wave
}, /* flip4mac */
4591 { ATOM_mp4s
, MP4_ReadBox_sample_mp4s
, ATOM_stsd
},
4593 /* XXX there is 2 box where we could find this entry stbl and tref*/
4594 { ATOM_hint
, MP4_ReadBox_default
, 0 },
4596 /* found in tref box */
4597 { ATOM_dpnd
, MP4_ReadBox_default
, 0 },
4598 { ATOM_ipir
, MP4_ReadBox_default
, 0 },
4599 { ATOM_mpod
, MP4_ReadBox_default
, 0 },
4600 { ATOM_chap
, MP4_ReadBox_tref_generic
, 0 },
4603 { ATOM_rtp
, MP4_ReadBox_rtp
, ATOM_hnti
},
4604 { ATOM_sdp
, MP4_ReadBox_sdp
, ATOM_hnti
},
4606 /* found in rrtp sample description */
4607 { ATOM_tims
, MP4_ReadBox_tims
, 0 },
4608 { ATOM_tsro
, MP4_ReadBox_tsro
, 0 },
4609 { ATOM_tssy
, MP4_ReadBox_tssy
, 0 },
4611 /* found in rmra/rmda */
4612 { ATOM_rdrf
, MP4_ReadBox_rdrf
, ATOM_rmda
},
4613 { ATOM_rmdr
, MP4_ReadBox_rmdr
, ATOM_rmda
},
4614 { ATOM_rmqu
, MP4_ReadBox_rmqu
, ATOM_rmda
},
4615 { ATOM_rmvc
, MP4_ReadBox_rmvc
, ATOM_rmda
},
4617 { ATOM_drms
, MP4_ReadBox_sample_soun
, 0 },
4618 { ATOM_sinf
, MP4_ReadBoxContainer
, 0 },
4619 { ATOM_schi
, MP4_ReadBoxContainer
, 0 },
4620 { ATOM_user
, MP4_ReadBox_drms
, 0 },
4621 { ATOM_key
, MP4_ReadBox_drms
, 0 },
4622 { ATOM_iviv
, MP4_ReadBox_drms
, 0 },
4623 { ATOM_priv
, MP4_ReadBox_drms
, 0 },
4624 { ATOM_frma
, MP4_ReadBox_frma
, ATOM_sinf
}, /* and rinf */
4625 { ATOM_frma
, MP4_ReadBox_frma
, ATOM_wave
}, /* flip4mac */
4626 { ATOM_skcr
, MP4_ReadBox_skcr
, 0 },
4628 /* ilst meta tags */
4629 { ATOM_0xa9ART
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4630 { ATOM_0xa9alb
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4631 { ATOM_0xa9cmt
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4632 { ATOM_0xa9com
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4633 { ATOM_0xa9cpy
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4634 { ATOM_0xa9day
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4635 { ATOM_0xa9des
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4636 { ATOM_0xa9enc
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4637 { ATOM_0xa9gen
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4638 { ATOM_0xa9grp
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4639 { ATOM_0xa9lyr
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4640 { ATOM_0xa9nam
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4641 { ATOM_0xa9too
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4642 { ATOM_0xa9trk
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4643 { ATOM_0xa9wrt
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4644 { ATOM_aART
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4645 { ATOM_atID
, MP4_ReadBox_Metadata
, ATOM_ilst
}, /* iTunes */
4646 { ATOM_cnID
, MP4_ReadBox_Metadata
, ATOM_ilst
}, /* iTunes */
4647 { ATOM_covr
, MP4_ReadBoxContainer
, ATOM_ilst
},
4648 { ATOM_desc
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4649 { ATOM_disk
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4650 { ATOM_flvr
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4651 { ATOM_gnre
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4652 { ATOM_rtng
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4653 { ATOM_trkn
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4654 { ATOM_xid_
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4655 { ATOM_gshh
, MP4_ReadBox_Metadata
, ATOM_ilst
}, /* YouTube gs?? */
4656 { ATOM_gspm
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4657 { ATOM_gspu
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4658 { ATOM_gssd
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4659 { ATOM_gsst
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4660 { ATOM_gstd
, MP4_ReadBox_Metadata
, ATOM_ilst
},
4661 { ATOM_ITUN
, MP4_ReadBox_Metadata
, ATOM_ilst
}, /* iTunesInfo */
4664 { ATOM_0x40PRM
, MP4_ReadBox_Binary
, ATOM_udta
},
4665 { ATOM_0x40PRQ
, MP4_ReadBox_Binary
, ATOM_udta
},
4666 { ATOM_0xa9ART
, MP4_ReadBox_Binary
, ATOM_udta
},
4667 { ATOM_0xa9alb
, MP4_ReadBox_Binary
, ATOM_udta
},
4668 { ATOM_0xa9ard
, MP4_ReadBox_Binary
, ATOM_udta
},
4669 { ATOM_0xa9arg
, MP4_ReadBox_Binary
, ATOM_udta
},
4670 { ATOM_0xa9aut
, MP4_ReadBox_Binary
, ATOM_udta
},
4671 { ATOM_0xa9cak
, MP4_ReadBox_Binary
, ATOM_udta
},
4672 { ATOM_0xa9cmt
, MP4_ReadBox_Binary
, ATOM_udta
},
4673 { ATOM_0xa9con
, MP4_ReadBox_Binary
, ATOM_udta
},
4674 { ATOM_0xa9com
, MP4_ReadBox_Binary
, ATOM_udta
},
4675 { ATOM_0xa9cpy
, MP4_ReadBox_Binary
, ATOM_udta
},
4676 { ATOM_0xa9day
, MP4_ReadBox_Binary
, ATOM_udta
},
4677 { ATOM_0xa9des
, MP4_ReadBox_Binary
, ATOM_udta
},
4678 { ATOM_0xa9dir
, MP4_ReadBox_Binary
, ATOM_udta
},
4679 { ATOM_0xa9dis
, MP4_ReadBox_Binary
, ATOM_udta
},
4680 { ATOM_0xa9dsa
, MP4_ReadBox_Binary
, ATOM_udta
},
4681 { ATOM_0xa9fmt
, MP4_ReadBox_Binary
, ATOM_udta
},
4682 { ATOM_0xa9gen
, MP4_ReadBox_Binary
, ATOM_udta
},
4683 { ATOM_0xa9grp
, MP4_ReadBox_Binary
, ATOM_udta
},
4684 { ATOM_0xa9hst
, MP4_ReadBox_Binary
, ATOM_udta
},
4685 { ATOM_0xa9inf
, MP4_ReadBox_Binary
, ATOM_udta
},
4686 { ATOM_0xa9isr
, MP4_ReadBox_Binary
, ATOM_udta
},
4687 { ATOM_0xa9lab
, MP4_ReadBox_Binary
, ATOM_udta
},
4688 { ATOM_0xa9lal
, MP4_ReadBox_Binary
, ATOM_udta
},
4689 { ATOM_0xa9lnt
, MP4_ReadBox_Binary
, ATOM_udta
},
4690 { ATOM_0xa9lyr
, MP4_ReadBox_Binary
, ATOM_udta
},
4691 { ATOM_0xa9mak
, MP4_ReadBox_Binary
, ATOM_udta
},
4692 { ATOM_0xa9mal
, MP4_ReadBox_Binary
, ATOM_udta
},
4693 { ATOM_0xa9mod
, MP4_ReadBox_Binary
, ATOM_udta
},
4694 { ATOM_0xa9nam
, MP4_ReadBox_Binary
, ATOM_udta
},
4695 { ATOM_0xa9ope
, MP4_ReadBox_Binary
, ATOM_udta
},
4696 { ATOM_0xa9phg
, MP4_ReadBox_Binary
, ATOM_udta
},
4697 { ATOM_0xa9PRD
, MP4_ReadBox_Binary
, ATOM_udta
},
4698 { ATOM_0xa9prd
, MP4_ReadBox_Binary
, ATOM_udta
},
4699 { ATOM_0xa9prf
, MP4_ReadBox_Binary
, ATOM_udta
},
4700 { ATOM_0xa9pub
, MP4_ReadBox_Binary
, ATOM_udta
},
4701 { ATOM_0xa9req
, MP4_ReadBox_Binary
, ATOM_udta
},
4702 { ATOM_0xa9sne
, MP4_ReadBox_Binary
, ATOM_udta
},
4703 { ATOM_0xa9snm
, MP4_ReadBox_Binary
, ATOM_udta
},
4704 { ATOM_0xa9sol
, MP4_ReadBox_Binary
, ATOM_udta
},
4705 { ATOM_0xa9src
, MP4_ReadBox_Binary
, ATOM_udta
},
4706 { ATOM_0xa9st3
, MP4_ReadBox_Binary
, ATOM_udta
},
4707 { ATOM_0xa9swr
, MP4_ReadBox_Binary
, ATOM_udta
},
4708 { ATOM_0xa9thx
, MP4_ReadBox_Binary
, ATOM_udta
},
4709 { ATOM_0xa9too
, MP4_ReadBox_Binary
, ATOM_udta
},
4710 { ATOM_0xa9trk
, MP4_ReadBox_Binary
, ATOM_udta
},
4711 { ATOM_0xa9url
, MP4_ReadBox_Binary
, ATOM_udta
},
4712 { ATOM_0xa9wrn
, MP4_ReadBox_Binary
, ATOM_udta
},
4713 { ATOM_0xa9xpd
, MP4_ReadBox_Binary
, ATOM_udta
},
4714 { ATOM_0xa9xyz
, MP4_ReadBox_Binary
, ATOM_udta
},
4715 { ATOM_chpl
, MP4_ReadBox_chpl
, ATOM_udta
}, /* nero unlabeled chapters list */
4716 { ATOM_MCPS
, MP4_ReadBox_Binary
, ATOM_udta
},
4717 { ATOM_name
, MP4_ReadBox_Binary
, ATOM_udta
},
4718 { ATOM_vndr
, MP4_ReadBox_Binary
, ATOM_udta
},
4719 { ATOM_SDLN
, MP4_ReadBox_Binary
, ATOM_udta
},
4720 { ATOM_HMMT
, MP4_ReadBox_HMMT
, ATOM_udta
}, /* GoPro HiLight tags */
4722 /* udta, non meta */
4723 { ATOM_tsel
, MP4_ReadBox_tsel
, ATOM_udta
},
4725 /* iTunes/Quicktime meta info */
4726 { ATOM_meta
, MP4_ReadBox_meta
, 0 },
4727 { ATOM_ID32
, MP4_ReadBox_Binary
, ATOM_meta
}, /* ID3v2 in 3GPP / ETSI TS 126 244 8.3 */
4728 { ATOM_data
, MP4_ReadBox_data
, 0 }, /* ilst/@too and others, ITUN/data */
4729 { ATOM_mean
, MP4_ReadBox_Binary
, ATOM_ITUN
},
4730 { ATOM_name
, MP4_ReadBox_Binary
, ATOM_ITUN
},
4732 /* found in smoothstreaming */
4733 { ATOM_traf
, MP4_ReadBoxContainer
, ATOM_moof
},
4734 { ATOM_mfra
, MP4_ReadBoxContainer
, 0 },
4735 { ATOM_mfhd
, MP4_ReadBox_mfhd
, ATOM_moof
},
4736 { ATOM_sidx
, MP4_ReadBox_sidx
, 0 },
4737 { ATOM_tfhd
, MP4_ReadBox_tfhd
, ATOM_traf
},
4738 { ATOM_trun
, MP4_ReadBox_trun
, ATOM_traf
},
4739 { ATOM_tfdt
, MP4_ReadBox_tfdt
, ATOM_traf
},
4740 { ATOM_trex
, MP4_ReadBox_trex
, ATOM_mvex
},
4741 { ATOM_mehd
, MP4_ReadBox_mehd
, ATOM_mvex
},
4742 { ATOM_sdtp
, MP4_ReadBox_sdtp
, 0 },
4743 { ATOM_tfra
, MP4_ReadBox_tfra
, ATOM_mfra
},
4744 { ATOM_mfro
, MP4_ReadBox_mfro
, ATOM_mfra
},
4745 { ATOM_uuid
, MP4_ReadBox_uuid
, 0 },
4747 /* spatial/360°/VR */
4748 { ATOM_st3d
, MP4_ReadBox_st3d
, ATOM_avc1
},
4749 { ATOM_st3d
, MP4_ReadBox_st3d
, ATOM_mp4v
},
4750 { ATOM_sv3d
, MP4_ReadBoxContainer
, ATOM_avc1
},
4751 { ATOM_sv3d
, MP4_ReadBoxContainer
, ATOM_mp4v
},
4752 { ATOM_proj
, MP4_ReadBoxContainer
, ATOM_sv3d
},
4753 { ATOM_prhd
, MP4_ReadBox_prhd
, ATOM_proj
},
4754 { ATOM_equi
, MP4_ReadBox_equi
, ATOM_proj
},
4755 { ATOM_cbmp
, MP4_ReadBox_cbmp
, ATOM_proj
},
4758 { ATOM_SA3D
, MP4_ReadBox_SA3D
, 0 },
4761 { 0, MP4_ReadBox_default
, 0 }
4764 static int MP4_Box_Read_Specific( stream_t
*p_stream
, MP4_Box_t
*p_box
, MP4_Box_t
*p_father
)
4768 for( i_index
= 0; ; i_index
++ )
4770 if ( MP4_Box_Function
[i_index
].i_parent
&&
4771 p_father
&& p_father
->i_type
!= MP4_Box_Function
[i_index
].i_parent
)
4774 if( ( MP4_Box_Function
[i_index
].i_type
== p_box
->i_type
)||
4775 ( MP4_Box_Function
[i_index
].i_type
== 0 ) )
4781 if( !(MP4_Box_Function
[i_index
].MP4_ReadBox_function
)( p_stream
, p_box
) )
4783 return VLC_EGENERIC
;
4789 static void MP4_Box_Clean_Specific( MP4_Box_t
*p_box
)
4791 if( p_box
->pf_free
)
4792 p_box
->pf_free( p_box
);
4795 /*****************************************************************************
4796 * MP4_ReadBox : parse the actual box and the children
4797 * XXX : Do not go to the next box
4798 *****************************************************************************/
4799 static MP4_Box_t
*MP4_ReadBox( stream_t
*p_stream
, MP4_Box_t
*p_father
)
4801 MP4_Box_t
*p_box
= calloc( 1, sizeof( MP4_Box_t
) ); /* Needed to ensure simple on error handler */
4805 if( !MP4_PeekBoxHeader( p_stream
, p_box
) )
4807 msg_Warn( p_stream
, "cannot read one box" );
4812 if( p_father
&& p_father
->i_size
> 0 &&
4813 p_father
->i_pos
+ p_father
->i_size
< p_box
->i_pos
+ p_box
->i_size
)
4815 msg_Dbg( p_stream
, "out of bound child" );
4820 if( !p_box
->i_size
)
4822 msg_Dbg( p_stream
, "found an empty box (null size)" );
4826 p_box
->p_father
= p_father
;
4828 if( MP4_Box_Read_Specific( p_stream
, p_box
, p_father
) != VLC_SUCCESS
)
4830 uint64_t i_end
= p_box
->i_pos
+ p_box
->i_size
;
4831 MP4_BoxFree( p_box
);
4832 MP4_Seek( p_stream
, i_end
); /* Skip the failed box */
4839 /*****************************************************************************
4840 * MP4_BoxNew : creates and initializes an arbitrary box
4841 *****************************************************************************/
4842 MP4_Box_t
* MP4_BoxNew( uint32_t i_type
)
4844 MP4_Box_t
*p_box
= calloc( 1, sizeof( MP4_Box_t
) );
4845 if( likely( p_box
!= NULL
) )
4847 p_box
->i_type
= i_type
;
4852 /*****************************************************************************
4853 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
4855 *****************************************************************************/
4856 void MP4_BoxFree( MP4_Box_t
*p_box
)
4863 for( p_child
= p_box
->p_first
; p_child
!= NULL
; )
4867 p_next
= p_child
->p_next
;
4868 MP4_BoxFree( p_child
);
4872 MP4_Box_Clean_Specific( p_box
);
4874 if( p_box
->data
.p_payload
)
4875 free( p_box
->data
.p_payload
);
4880 MP4_Box_t
*MP4_BoxGetNextChunk( stream_t
*s
)
4882 /* p_chunk is a virtual root container for the moof and mdat boxes */
4883 MP4_Box_t
*p_fakeroot
;
4884 MP4_Box_t
*p_tmp_box
;
4886 p_fakeroot
= MP4_BoxNew( ATOM_root
);
4887 if( unlikely( p_fakeroot
== NULL
) )
4889 p_fakeroot
->i_shortsize
= 1;
4891 const uint32_t stoplist
[] = { ATOM_moov
, ATOM_moof
, 0 };
4892 MP4_ReadBoxContainerChildren( s
, p_fakeroot
, stoplist
);
4894 p_tmp_box
= p_fakeroot
->p_first
;
4895 if( p_tmp_box
== NULL
)
4897 MP4_BoxFree( p_fakeroot
);
4900 else while( p_tmp_box
)
4902 p_fakeroot
->i_size
+= p_tmp_box
->i_size
;
4903 p_tmp_box
= p_tmp_box
->p_next
;
4909 /*****************************************************************************
4910 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
4911 *****************************************************************************
4912 * The first box is a virtual box "root" and is the father for all first
4913 * level boxes for the file, a sort of virtual contener
4914 *****************************************************************************/
4915 MP4_Box_t
*MP4_BoxGetRoot( stream_t
*p_stream
)
4919 MP4_Box_t
*p_vroot
= MP4_BoxNew( ATOM_root
);
4920 if( p_vroot
== NULL
)
4923 p_vroot
->i_shortsize
= 1;
4924 int64_t i_size
= stream_Size( p_stream
);
4926 p_vroot
->i_size
= i_size
;
4928 /* First get the moov */
4929 const uint32_t stoplist
[] = { ATOM_moov
, ATOM_mdat
, 0 };
4930 i_result
= MP4_ReadBoxContainerChildren( p_stream
, p_vroot
, stoplist
);
4932 /* mdat appeared first */
4933 if( i_result
&& !MP4_BoxGet( p_vroot
, "moov" ) )
4936 if( vlc_stream_Control( p_stream
, STREAM_CAN_SEEK
, &b_seekable
) != VLC_SUCCESS
|| !b_seekable
)
4938 msg_Err( p_stream
, "no moov before mdat and the stream is not seekable" );
4942 /* continue loading up to moov */
4943 const uint32_t stoplist
[] = { ATOM_moov
, 0 };
4944 i_result
= MP4_ReadBoxContainerChildren( p_stream
, p_vroot
, stoplist
);
4950 /* If there is a mvex box, it means fragmented MP4, and we're done */
4951 if( MP4_BoxCount( p_vroot
, "moov/mvex" ) > 0 )
4953 /* Read a bit more atoms as we might have an index between moov and moof */
4954 const uint32_t stoplist
[] = { ATOM_sidx
, 0 };
4955 const uint32_t excludelist
[] = { ATOM_moof
, ATOM_mdat
, 0 };
4956 MP4_ReadBoxContainerChildrenIndexed( p_stream
, p_vroot
, stoplist
, excludelist
, false );
4960 if( vlc_stream_Tell( p_stream
) + 8 < (uint64_t) stream_Size( p_stream
) )
4962 /* Get the rest of the file */
4963 i_result
= MP4_ReadBoxContainerChildren( p_stream
, p_vroot
, NULL
);
4972 /* check if there is a cmov, if so replace
4973 compressed moov by uncompressed one */
4974 if( ( ( p_moov
= MP4_BoxGet( p_vroot
, "moov" ) ) &&
4975 ( p_cmov
= MP4_BoxGet( p_vroot
, "moov/cmov" ) ) ) ||
4976 ( ( p_moov
= MP4_BoxGet( p_vroot
, "foov" ) ) &&
4977 ( p_cmov
= MP4_BoxGet( p_vroot
, "foov/cmov" ) ) ) )
4979 /* rename the compressed moov as a box to skip */
4980 p_moov
->i_type
= ATOM_skip
;
4982 /* get uncompressed p_moov */
4983 p_moov
= p_cmov
->data
.p_cmov
->p_moov
;
4984 p_cmov
->data
.p_cmov
->p_moov
= NULL
;
4986 /* make p_root father of this new moov */
4987 p_moov
->p_father
= p_vroot
;
4989 /* insert this new moov box as first child of p_root */
4990 p_moov
->p_next
= p_vroot
->p_first
;
4991 p_vroot
->p_first
= p_moov
;
4997 MP4_BoxFree( p_vroot
);
4998 MP4_Seek( p_stream
, 0 );
5003 static void MP4_BoxDumpStructure_Internal( stream_t
*s
, const MP4_Box_t
*p_box
,
5004 unsigned int i_level
)
5006 const MP4_Box_t
*p_child
;
5007 uint32_t i_displayedtype
= p_box
->i_type
;
5008 if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype
)[0] = 'c';
5012 msg_Dbg( s
, "dumping root Box \"%4.4s\"",
5013 (char*)&i_displayedtype
);
5018 if( i_level
>= (sizeof(str
) - 1)/4 )
5021 memset( str
, ' ', sizeof(str
) );
5022 for( unsigned i
= 0; i
< i_level
; i
++ )
5027 snprintf( &str
[i_level
* 4], sizeof(str
) - 4*i_level
,
5028 "+ %4.4s size %"PRIu64
" offset %" PRIuMAX
"%s",
5029 (char*)&i_displayedtype
, p_box
->i_size
,
5030 (uintmax_t)p_box
->i_pos
,
5031 p_box
->e_flags
& BOX_FLAG_INCOMPLETE
? " (\?\?\?\?)" : "" );
5032 msg_Dbg( s
, "%s", str
);
5034 p_child
= p_box
->p_first
;
5037 MP4_BoxDumpStructure_Internal( s
, p_child
, i_level
+ 1 );
5038 p_child
= p_child
->p_next
;
5042 void MP4_BoxDumpStructure( stream_t
*s
, const MP4_Box_t
*p_box
)
5044 MP4_BoxDumpStructure_Internal( s
, p_box
, 0 );
5048 /*****************************************************************************
5049 *****************************************************************************
5051 ** High level methods to acces an MP4 file
5053 *****************************************************************************
5054 *****************************************************************************/
5055 static bool get_token( char **ppsz_path
, char **ppsz_token
, int *pi_number
)
5058 if( !*ppsz_path
[0] )
5064 i_len
= strcspn( *ppsz_path
, "/[" );
5065 if( !i_len
&& **ppsz_path
== '/' )
5069 *ppsz_token
= strndup( *ppsz_path
, i_len
);
5070 if( unlikely(!*ppsz_token
) )
5073 *ppsz_path
+= i_len
;
5075 /* Parse the token number token[n] */
5076 if( **ppsz_path
== '[' )
5079 *pi_number
= strtol( *ppsz_path
, NULL
, 10 );
5080 while( **ppsz_path
&& **ppsz_path
!= ']' )
5084 if( **ppsz_path
== ']' )
5094 /* Forward to start of next token */
5095 while( **ppsz_path
== '/' )
5103 static void MP4_BoxGet_Internal( const MP4_Box_t
**pp_result
, const MP4_Box_t
*p_box
,
5104 const char *psz_fmt
, va_list args
)
5108 char *psz_token
= NULL
;
5116 if( vasprintf( &psz_path
, psz_fmt
, args
) == -1 )
5119 if( !psz_path
|| !psz_path
[0] )
5126 // fprintf( stderr, "path:'%s'\n", psz_path );
5127 psz_dup
= psz_path
; /* keep this pointer, as it need to be unallocated */
5132 if( !get_token( &psz_path
, &psz_token
, &i_number
) )
5134 // fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
5135 // psz_path,psz_token,i_number );
5143 if( !strcmp( psz_token
, "/" ) )
5146 while( p_box
&& p_box
->i_type
!= ATOM_root
)
5148 p_box
= p_box
->p_father
;
5156 if( !strcmp( psz_token
, "." ) )
5161 if( !strcmp( psz_token
, ".." ) )
5163 p_box
= p_box
->p_father
;
5170 if( strlen( psz_token
) == 4 )
5173 i_fourcc
= VLC_FOURCC( psz_token
[0], psz_token
[1],
5174 psz_token
[2], psz_token
[3] );
5175 p_box
= p_box
->p_first
;
5182 if( p_box
->i_type
== i_fourcc
)
5190 p_box
= p_box
->p_next
;
5194 if( *psz_token
== '\0' )
5196 p_box
= p_box
->p_first
;
5208 p_box
= p_box
->p_next
;
5213 // fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
5217 FREENULL( psz_token
);
5229 /*****************************************************************************
5230 * MP4_BoxGet: find a box given a path relative to p_box
5231 *****************************************************************************
5232 * Path Format: . .. / as usual
5233 * [number] to specifie box number ex: trak[12]
5235 * ex: /moov/trak[12]
5237 *****************************************************************************/
5238 MP4_Box_t
*MP4_BoxGet( const MP4_Box_t
*p_box
, const char *psz_fmt
, ... )
5241 const MP4_Box_t
*p_result
;
5243 va_start( args
, psz_fmt
);
5244 MP4_BoxGet_Internal( &p_result
, p_box
, psz_fmt
, args
);
5247 return( (MP4_Box_t
*) p_result
);
5250 /*****************************************************************************
5251 * MP4_BoxCount: count box given a path relative to p_box
5252 *****************************************************************************
5253 * Path Format: . .. / as usual
5254 * [number] to specifie box number ex: trak[12]
5256 * ex: /moov/trak[12]
5258 *****************************************************************************/
5259 unsigned MP4_BoxCount( const MP4_Box_t
*p_box
, const char *psz_fmt
, ... )
5263 const MP4_Box_t
*p_result
, *p_next
;
5265 va_start( args
, psz_fmt
);
5266 MP4_BoxGet_Internal( &p_result
, p_box
, psz_fmt
, args
);
5274 for( p_next
= p_result
->p_next
; p_next
!= NULL
; p_next
= p_next
->p_next
)
5276 if( p_next
->i_type
== p_result
->i_type
)