Coverage Report

Created: 2026-09-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/mp4/libmp4.c
Line
Count
Source
1
/*****************************************************************************
2
 * libmp4.c : LibMP4 library for mp4 module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
5
 *
6
 * Author: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
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.
12
 *
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.
17
 *
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
 *****************************************************************************/
22
23
#ifdef HAVE_CONFIG_H
24
# include "config.h"
25
#endif
26
27
#include <vlc_common.h>
28
#include <vlc_stream.h>                               /* vlc_stream_Peek*/
29
#include <vlc_strings.h>                              /* vlc_ascii_tolower */
30
31
#ifdef HAVE_ZLIB_H
32
#   include <zlib.h>                                  /* for compressed moov */
33
#endif
34
35
#include "libmp4.h"
36
#include "languages.h"
37
#include <math.h>
38
#include <assert.h>
39
#include <limits.h>
40
#include <stdckdint.h>
41
42
422
#define MP4_DEPTH_MAX 32
43
44
/* Some assumptions:
45
 * The input method HAS to be seekable
46
 */
47
48
/* convert 16.16 fixed point to floating point */
49
12
static double conv_fx( int32_t fx ) {
50
12
    double fp = fx;
51
12
    fp /= 65536.;
52
12
    return fp;
53
12
}
54
55
/* some functions for mp4 encoding of variables */
56
#ifdef MP4_VERBOSE
57
static char * MP4_Time2Str( stime_t i_duration, uint32_t i_scale )
58
2
{
59
2
    uint64_t i_time = (i_scale) ? i_duration / i_scale : 0;
60
2
    unsigned h = ( i_time /( 60*60 ) ) % 60;
61
2
    unsigned m = ( i_time / 60 ) % 60;
62
2
    unsigned s = i_time % 60;
63
2
    uint64_t ms;
64
2
    if ( i_scale == 0 || ckd_mul( &ms, 1000, i_duration ) )
65
0
        ms = 0;
66
2
    else
67
2
        ms = (ms / i_scale) % 1000;
68
69
2
    char *out;
70
2
    if( asprintf( &out, "%u:%.2u:%.2u:%.3" PRIu64, h, m, s, ms ) < 0 )
71
0
        return NULL;
72
2
    return out;
73
2
}
74
#endif
75
76
#define MP4_GETX_PRIVATE(dst, code, size) \
77
8.96k
    do \
78
8.96k
    { \
79
8.96k
        if( (i_read) >= (size) ) \
80
8.96k
        { \
81
8.96k
            dst = (code); \
82
8.96k
            p_peek += (size); \
83
8.96k
            i_read -= (size); \
84
8.96k
        } \
85
8.96k
        else \
86
8.96k
        { \
87
2
            dst = 0; \
88
2
            i_read = 0; \
89
2
        } \
90
8.96k
    } while(0)
91
92
#define MP4_COPY_BYTES( dst, size ) \
93
0
    do \
94
0
    { \
95
0
        if( (i_read) >= (size) ) \
96
0
        { \
97
0
            memcpy( dst, p_peek, size ); \
98
0
            p_peek += (size); \
99
0
            i_read -= (size); \
100
0
        } \
101
0
        else \
102
0
        { \
103
0
            i_read = 0; \
104
0
        } \
105
0
    } while(0)
106
107
#define MP4_SKIPBYTES(size) \
108
216
    do \
109
216
    { \
110
216
        if( (i_read) >= (size) ) \
111
216
        { \
112
215
            p_peek += (size); \
113
215
            i_read -= (size); \
114
215
        } \
115
216
        else \
116
216
        { \
117
1
            i_read = 0; \
118
1
        } \
119
216
    } while(0)
120
121
1.28k
#define MP4_GET1BYTE( dst )  MP4_GETX_PRIVATE( dst, *p_peek, 1 )
122
1.31k
#define MP4_GET2BYTES( dst ) MP4_GETX_PRIVATE( dst, GetWBE(p_peek), 2 )
123
249
#define MP4_GET3BYTES( dst ) MP4_GETX_PRIVATE( dst, Get24bBE(p_peek), 3 )
124
3.08k
#define MP4_GET4BYTES( dst ) MP4_GETX_PRIVATE( dst, GetDWBE(p_peek), 4 )
125
11
#define MP4_GET8BYTES( dst ) MP4_GETX_PRIVATE( dst, GetQWBE(p_peek), 8 )
126
127
0
#define MP4_GET2BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetWLE(p_peek), 2 )
128
3.02k
#define MP4_GET4BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetDWLE(p_peek), 4 )
129
#define MP4_GET8BYTESLE( dst ) MP4_GETX_PRIVATE( dst, GetQWLE(p_peek), 8 )
130
3.02k
#define MP4_GETFOURCC( dst )   MP4_GET4BYTESLE( dst )
131
132
#define MP4_GETVERSIONFLAGS( p_void ) \
133
79
    MP4_GET1BYTE( p_void->i_version ); \
134
79
    MP4_GET3BYTES( p_void->i_flags )
135
136
#define READ_SAMPLE_DESC_COMMON_8BYTES_HEADER \
137
1
    do\
138
1
    {\
139
1
        if( i_read < 8 )\
140
1
            MP4_READBOX_EXIT( 0 );\
141
7
        for( unsigned i = 0; i < 6 ; i++ )\
142
1
            MP4_GET1BYTE( p_box->data.p_sample_gen->i_reserved1[i] );\
143
1
        MP4_GET2BYTES( p_box->data.p_sample_gen->i_data_reference_index );\
144
1
    } while(0)
145
146
147
static char *mp4_getstringz( uint8_t **restrict in, uint64_t *restrict size )
148
100
{
149
100
    assert( *size <= SSIZE_MAX );
150
151
100
    if( *size == 0 )
152
1
        return NULL;
153
154
99
    size_t len = strnlen( (const char *)*in, *size );
155
99
    if( len == 0 ) /* Null string stored */
156
57
    {
157
57
        *in += 1;
158
57
        *size -= 1;
159
57
        return NULL;
160
57
    }
161
162
42
    const bool b_terminated = len < *size;
163
42
    char *ret = malloc( len + 1 );
164
42
    if( likely(ret != NULL) )
165
42
    {
166
42
        memcpy( ret, *in, len );
167
42
        ret[len] = 0; // ensure termination
168
42
    }
169
42
    if( b_terminated ) // terminated
170
10
        len++;
171
42
    *in += len;
172
42
    *size -= len;
173
42
    return ret;
174
99
}
175
176
#define MP4_GETSTRINGZ( p_str ) \
177
0
    do \
178
0
        (p_str) = mp4_getstringz( &p_peek, &i_read ); \
179
0
    while(0)
180
181
/* This macro is used when we want to printf the box type
182
 * APPLE annotation box is :
183
 *  either 0xA9 + 24-bit ASCII text string (and 0xA9 isn't printable)
184
 *  either 32-bit ASCII text string
185
 */
186
3.19k
#define MP4_BOX_TYPE_ASCII() ( ((unsigned char*)&p_box->i_type)[0] != 0xA9 )
187
188
static inline uint32_t Get24bBE( const uint8_t *p )
189
249
{
190
249
    return( ( p[0] <<16 ) + ( p[1] <<8 ) + p[2] );
191
249
}
192
193
static inline void GetUUID( UUID_t *p_uuid, const uint8_t *p_buff )
194
4
{
195
4
    memcpy( p_uuid, p_buff, 16 );
196
4
}
197
198
static unsigned GetDepth( const MP4_Box_t *box )
199
422
{
200
422
    unsigned i = 0;
201
1.22k
    for( ; box ; box = box->p_father )
202
806
        i++;
203
422
    return i;
204
422
}
205
206
static video_palette_t * ReadQuicktimePalette( uint8_t **pp_peek, uint64_t *pi_read )
207
0
{
208
0
    uint8_t *p_peek = *pp_peek;
209
0
    uint64_t i_read = *pi_read;
210
0
    uint32_t v;
211
0
    MP4_GET4BYTES(v);
212
0
    if( v != 0 )
213
0
        return NULL;
214
0
    MP4_GET2BYTES(v); // should be == 0x8000, but does not in the wild
215
0
    uint16_t i_colors_count;
216
0
    MP4_GET2BYTES(i_colors_count);
217
0
    if( i_colors_count == UINT16_MAX ||
218
0
        ++i_colors_count > VIDEO_PALETTE_COLORS_MAX ||
219
0
        i_read < i_colors_count * 8U)
220
0
        return NULL;
221
0
    video_palette_t *p_palette = malloc( sizeof(video_palette_t) );
222
0
    if( p_palette == NULL )
223
0
        return NULL;
224
0
    for( uint16_t i=0; i<i_colors_count; i++ )
225
0
    {
226
0
        MP4_SKIPBYTES(2);
227
0
        MP4_GET2BYTES(p_palette->palette[i][0]);
228
0
        MP4_GET2BYTES(p_palette->palette[i][1]);
229
0
        MP4_GET2BYTES(p_palette->palette[i][2]);
230
0
        p_palette->palette[i][3] = 0xFF;
231
0
    }
232
0
    p_palette->i_entries = i_colors_count;
233
0
    *pp_peek = p_peek;
234
0
    *pi_read = i_read;
235
0
    return p_palette;
236
0
}
237
238
static uint8_t *mp4_readbox_enter_common( stream_t *s, MP4_Box_t *box,
239
                                          size_t typesize,
240
                                          void (*release)( MP4_Box_t * ),
241
                                          uint64_t readsize )
242
622
{
243
622
    const size_t headersize = mp4_box_headersize( box );
244
245
622
    if( unlikely(readsize < headersize) || unlikely(readsize > SSIZE_MAX) )
246
0
        return NULL;
247
248
622
    uint8_t *buf = malloc( readsize );
249
622
    if( unlikely(buf == NULL) )
250
0
        return NULL;
251
252
622
    ssize_t val = vlc_stream_Read( s, buf, readsize );
253
622
    if( (size_t)val != readsize )
254
0
    {
255
0
        msg_Warn( s, "mp4: wanted %"PRIu64" bytes, got %zd", readsize, val );
256
0
        goto error;
257
0
    }
258
259
622
    box->data.p_payload = calloc( 1, typesize );
260
622
    if( unlikely(box->data.p_payload == NULL) )
261
0
        goto error;
262
263
622
    box->pf_free = release;
264
622
    return buf;
265
0
error:
266
0
    free( buf );
267
0
    return NULL;
268
622
}
269
270
static uint8_t *mp4_readbox_enter_partial( stream_t *s, MP4_Box_t *box,
271
                                           size_t typesize,
272
                                           void (*release)( MP4_Box_t * ),
273
                                           uint64_t *restrict readsize )
274
57
{
275
57
    if( (uint64_t)*readsize > box->i_size )
276
0
        *readsize = box->i_size;
277
278
57
    return mp4_readbox_enter_common( s, box, typesize, release, *readsize );
279
57
}
280
281
static uint8_t *mp4_readbox_enter( stream_t *s, MP4_Box_t *box,
282
                                   size_t typesize,
283
                                   void (*release)( MP4_Box_t * ) )
284
565
{
285
565
    uint64_t readsize = box->i_size;
286
565
    return mp4_readbox_enter_common( s, box, typesize, release, readsize );
287
565
}
288
289
290
#define MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_TYPE_t, maxread, release ) \
291
57
    uint64_t i_read = (maxread); \
292
57
    uint8_t *p_buff = mp4_readbox_enter_partial( p_stream, p_box, \
293
57
        sizeof( MP4_Box_data_TYPE_t ), release, &i_read ); \
294
57
    if( unlikely(p_buff == NULL) ) \
295
57
        return 0; \
296
57
    const size_t header_size = mp4_box_headersize( p_box ); \
297
57
    uint8_t *p_peek = p_buff + header_size; \
298
57
    i_read -= header_size
299
300
#define MP4_READBOX_ENTER( MP4_Box_data_TYPE_t, release ) \
301
565
    uint8_t *p_buff = mp4_readbox_enter( p_stream, p_box, \
302
565
        sizeof(MP4_Box_data_TYPE_t), release ); \
303
565
    if( unlikely(p_buff == NULL) ) \
304
565
        return 0; \
305
565
    uint64_t i_read = p_box->i_size; \
306
565
    const size_t header_size = mp4_box_headersize( p_box ); \
307
565
    uint8_t *p_peek = p_buff + header_size; \
308
565
    i_read -= header_size
309
310
#define MP4_READBOX_EXIT( i_code ) \
311
622
    do \
312
622
    { \
313
622
        free( p_buff ); \
314
622
        return( i_code ); \
315
622
    } while (0)
316
317
/*****************************************************************************
318
 * Some prototypes.
319
 *****************************************************************************/
320
static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
321
static MP4_Box_t *MP4_ReadBoxUsing( stream_t *p_stream, MP4_Box_t *p_father,
322
                                    int(*MP4_ReadBox_function)(stream_t *, MP4_Box_t *) );
323
static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father );
324
static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box );
325
326
int MP4_Seek( stream_t *p_stream, uint64_t i_pos )
327
2.72k
{
328
2.72k
    bool b_canseek = false;
329
2.72k
    if ( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_canseek ) != VLC_SUCCESS ||
330
2.72k
         b_canseek )
331
2.72k
    {
332
        /* can seek or don't know */
333
2.72k
        return vlc_stream_Seek( p_stream, i_pos );
334
2.72k
    }
335
    /* obviously can't seek then */
336
337
0
    int64_t i_current_pos = vlc_stream_Tell( p_stream );
338
0
    if ( i_current_pos < 0 || i_pos < (uint64_t)i_current_pos )
339
0
        return VLC_EGENERIC;
340
341
0
    uint64_t i_toread = i_pos - i_current_pos;
342
0
    if( i_toread == 0 )
343
0
        return VLC_SUCCESS;
344
0
    if( i_toread > SSIZE_MAX ) // we can't read more than that
345
0
        return VLC_EGENERIC;
346
347
0
    if( vlc_stream_Read( p_stream, NULL, i_toread ) != (ssize_t) i_toread )
348
0
        return VLC_EGENERIC;
349
0
    return VLC_SUCCESS;
350
0
}
351
352
static void MP4_BoxAddChild( MP4_Box_t *p_parent, MP4_Box_t *p_childbox )
353
1.97k
{
354
1.97k
    if( !p_parent->p_first )
355
392
            p_parent->p_first = p_childbox;
356
1.58k
    else
357
1.58k
            p_parent->p_last->p_next = p_childbox;
358
1.97k
    p_parent->p_last = p_childbox;
359
1.97k
    p_childbox->p_father = p_parent;
360
1.97k
}
361
362
MP4_Box_t * MP4_BoxExtract( MP4_Box_t **pp_chain, uint32_t i_type )
363
0
{
364
0
    MP4_Box_t *p_box = *pp_chain;
365
0
    while( p_box )
366
0
    {
367
0
        if( p_box->i_type == i_type )
368
0
        {
369
0
            *pp_chain = p_box->p_next;
370
0
            p_box->p_next = NULL;
371
0
            p_box->p_father = NULL;
372
0
            return p_box;
373
0
        }
374
0
        pp_chain = &p_box->p_next;
375
0
        p_box = p_box->p_next;
376
0
    }
377
0
    return NULL;
378
0
}
379
380
/* Don't use vlc_stream_Seek directly */
381
#undef vlc_stream_Seek
382
#define vlc_stream_Seek(a,b) __NO__
383
384
/*****************************************************************************
385
 * MP4_PeekBoxHeader : Load only common parameters for all boxes
386
 *****************************************************************************
387
 * p_box need to be an already allocated MP4_Box_t, and all data
388
 *  will only be peek not read
389
 *
390
 * RETURN : 0 if it fail, 1 otherwise
391
 *****************************************************************************/
392
static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box )
393
2.22k
{
394
2.22k
    ssize_t i_read;
395
2.22k
    const uint8_t  *p_peek;
396
397
2.22k
    if( ( ( i_read = vlc_stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
398
0
    {
399
0
        return 0;
400
0
    }
401
2.22k
    p_box->i_pos = vlc_stream_Tell( p_stream );
402
403
2.22k
    p_box->data.p_payload = NULL;
404
2.22k
    p_box->p_father = NULL;
405
2.22k
    p_box->p_first  = NULL;
406
2.22k
    p_box->p_last  = NULL;
407
2.22k
    p_box->p_next   = NULL;
408
409
2.22k
    MP4_GET4BYTES( p_box->i_shortsize );
410
2.22k
    MP4_GETFOURCC( p_box->i_type );
411
412
    /* Now special case */
413
414
2.22k
    if( p_box->i_shortsize == 1 )
415
11
    {
416
11
        if( i_read < 8 )
417
1
            return 0;
418
        /* get the true size on 64 bits */
419
10
        MP4_GET8BYTES( p_box->i_size );
420
10
    }
421
2.21k
    else
422
2.21k
    {
423
2.21k
        p_box->i_size = p_box->i_shortsize;
424
        /* XXX size of 0 means that the box extends to end of file */
425
2.21k
    }
426
427
2.22k
    if( UINT64_MAX - p_box->i_size < p_box->i_pos )
428
0
        return 0;
429
430
2.22k
    if( p_box->i_type == ATOM_uuid )
431
4
    {
432
4
        if( i_read < 16 )
433
0
            return 0;
434
        /* get extended type on 16 bytes */
435
4
        GetUUID( &p_box->i_uuid, p_peek );
436
4
    }
437
438
#ifdef MP4_ULTRA_VERBOSE
439
    if( p_box->i_size )
440
    {
441
        if MP4_BOX_TYPE_ASCII()
442
            msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64" %"PRId64,
443
                    (char*)&p_box->i_type, p_box->i_size, p_box->i_pos );
444
        else
445
            msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
446
                    (char*)&p_box->i_type+1, p_box->i_size );
447
    }
448
#endif
449
450
2.22k
    return 1;
451
2.22k
}
452
453
/*****************************************************************************
454
 * MP4_ReadBoxRestricted : Reads box from current position
455
 *****************************************************************************
456
 * if p_box == NULL, box is invalid or failed, position undefined
457
 * on success, position is past read box or EOF
458
 *****************************************************************************/
459
static MP4_Box_t *MP4_ReadBoxRestricted( stream_t *p_stream, MP4_Box_t *p_father,
460
                                         const uint32_t stopbefore[], bool *pb_restrictionhit )
461
2.09k
{
462
2.09k
    MP4_Box_t peekbox = { 0 };
463
2.09k
    if ( !MP4_PeekBoxHeader( p_stream, &peekbox ) )
464
1
        return NULL;
465
466
2.09k
    if( peekbox.i_size < 8 )
467
22
    {
468
22
        msg_Warn( p_stream, "found an invalid sized %"PRIu64" box %4.4s @%"PRIu64 ,
469
22
                  peekbox.i_size, (char *) &peekbox.i_type, vlc_stream_Tell(p_stream) );
470
22
        return NULL;
471
22
    }
472
473
2.07k
    for( size_t i=0; stopbefore && stopbefore[i]; i++ )
474
0
    {
475
0
        if( stopbefore[i] == peekbox.i_type )
476
0
        {
477
0
            *pb_restrictionhit = true;
478
0
            return NULL;
479
0
        }
480
0
    }
481
482
    /* if father's size == 0, it means unknown or infinite size,
483
     * and we skip the followong check */
484
2.07k
    if( p_father && p_father->i_size > 0 )
485
2.07k
    {
486
2.07k
        const uint64_t i_box_next = peekbox.i_size + peekbox.i_pos;
487
2.07k
        const uint64_t i_father_next = p_father->i_size + p_father->i_pos;
488
        /* check if it's within p-father */
489
2.07k
        if( i_box_next > i_father_next )
490
213
        {
491
213
            msg_Warn( p_stream, "out of bound child %4.4s", (char*) &peekbox.i_type );
492
213
            return NULL; /* out of bound */
493
213
        }
494
2.07k
    }
495
496
    /* Everything seems OK */
497
1.86k
    MP4_Box_t *p_box = (MP4_Box_t *) malloc( sizeof(MP4_Box_t) );
498
1.86k
    if( !p_box )
499
0
        return NULL;
500
1.86k
    *p_box = peekbox;
501
502
1.86k
    const uint64_t i_next = p_box->i_pos + p_box->i_size;
503
1.86k
    p_box->p_father = p_father;
504
1.86k
    if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
505
11
    {
506
11
        msg_Warn( p_stream, "Failed reading box %4.4s", (char*) &peekbox.i_type );
507
11
        MP4_BoxFree( p_box );
508
11
        p_box = NULL;
509
11
    }
510
511
    /* Check is we consumed all data */
512
1.86k
    if( vlc_stream_Tell( p_stream ) < i_next )
513
1.20k
    {
514
1.20k
        MP4_Seek( p_stream, i_next - 1 ); /*  since past seek can fail when hitting EOF */
515
1.20k
        MP4_Seek( p_stream, i_next );
516
1.20k
        if( vlc_stream_Tell( p_stream ) < i_next - 1 ) /* Truncated box */
517
0
        {
518
0
            msg_Warn( p_stream, "truncated box %4.4s discarded", (char*) &peekbox.i_type );
519
0
            MP4_BoxFree( p_box );
520
0
            p_box = NULL;
521
0
        }
522
1.20k
    }
523
524
1.86k
    if ( p_box )
525
1.84k
        MP4_BoxAddChild( p_father, p_box );
526
527
1.86k
    return p_box;
528
1.86k
}
529
530
/*****************************************************************************
531
 * For all known box a loader is given,
532
 * you have to be already read container header
533
 * without container size, file position on exit is unknown
534
 *****************************************************************************/
535
static int MP4_ReadBoxContainerChildrenIndexed( stream_t *p_stream,
536
               MP4_Box_t *p_container, const uint32_t stoplist[],
537
               const uint32_t excludelist[], bool b_indexed )
538
598
{
539
    /* Size of root container is set to 0 when unknown, for example
540
     * with a DASH stream. In that case, we skip the following check */
541
598
    if( (p_container->i_size || p_container->p_father)
542
598
            && ( vlc_stream_Tell( p_stream ) + ((b_indexed)?16:8) >
543
598
        (uint64_t)(p_container->i_pos + p_container->i_size) )
544
598
      )
545
176
    {
546
        /* there is no box to load */
547
176
        return 0;
548
176
    }
549
550
422
    if( GetDepth( p_container ) > MP4_DEPTH_MAX ) /* Prevent unbounded recursions */
551
0
        return 1;
552
553
422
    uint64_t i_last_pos = 0; /* used to detect read failure loops */
554
422
    const uint64_t i_end = p_container->i_pos + p_container->i_size;
555
422
    MP4_Box_t *p_box = NULL;
556
422
    bool b_onexclude = false;
557
422
    bool b_continue;
558
422
    do
559
2.14k
    {
560
2.14k
        b_continue = false;
561
2.14k
        if ( p_container->i_size )
562
2.14k
        {
563
2.14k
            const uint64_t i_tell = vlc_stream_Tell( p_stream );
564
2.14k
            if( i_tell + ((b_indexed)?16:8) >= i_end )
565
44
                break;
566
2.14k
        }
567
568
2.09k
        uint32_t i_index = 0;
569
2.09k
        if ( b_indexed )
570
0
        {
571
0
            uint8_t read[8];
572
0
            if ( vlc_stream_Read( p_stream, read, 8 ) < 8 )
573
0
                break;
574
0
            i_index = GetDWBE(&read[4]);
575
0
        }
576
2.09k
        b_onexclude = false; /* If stopped due exclude list */
577
2.09k
        if( (p_box = MP4_ReadBoxRestricted( p_stream, p_container, excludelist, &b_onexclude )) )
578
1.84k
        {
579
1.84k
            b_continue = true;
580
1.84k
            p_box->i_index = i_index;
581
3.26k
            for(size_t i=0; stoplist && stoplist[i]; i++)
582
1.49k
            {
583
1.49k
                if( p_box->i_type == stoplist[i] )
584
78
                    return 1;
585
1.49k
            }
586
1.84k
        }
587
588
2.01k
        const uint64_t i_tell = vlc_stream_Tell( p_stream );
589
2.01k
        if ( p_container->i_size && i_tell >= i_end )
590
181
        {
591
181
            assert( i_tell == i_end );
592
181
            break;
593
181
        }
594
595
1.83k
        if ( !p_box )
596
241
        {
597
            /* Continue with next if box fails to load */
598
241
            if( i_last_pos == i_tell )
599
119
                break;
600
122
            i_last_pos = i_tell;
601
122
            b_continue = true;
602
122
        }
603
604
1.83k
    } while( b_continue );
605
606
    /* Always move to end of container */
607
344
    if ( !b_onexclude &&  p_container->i_size )
608
344
    {
609
344
        const uint64_t i_tell = vlc_stream_Tell( p_stream );
610
344
        if ( i_tell != i_end )
611
163
            MP4_Seek( p_stream, i_end );
612
344
    }
613
614
344
    return 1;
615
422
}
616
617
int MP4_ReadBoxContainerRestricted( stream_t *p_stream, MP4_Box_t *p_container,
618
                                    const uint32_t stoplist[], const uint32_t excludelist[] )
619
0
{
620
0
    return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
621
0
                                                stoplist, excludelist, false );
622
0
}
623
624
int MP4_ReadBoxContainerChildren( stream_t *p_stream, MP4_Box_t *p_container,
625
                                  const uint32_t stoplist[] )
626
598
{
627
598
    return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
628
598
                                                stoplist, NULL, false );
629
598
}
630
631
static void MP4_BoxOffsetUp( MP4_Box_t *p_box, uint64_t i_offset )
632
1
{
633
1
    while(p_box)
634
0
    {
635
0
        p_box->i_pos += i_offset;
636
0
        MP4_BoxOffsetUp( p_box->p_first, i_offset );
637
0
        p_box = p_box->p_next;
638
0
    }
639
1
}
640
641
/* Reads within an already read/in memory box (containers without having to seek) */
642
static int MP4_ReadBoxContainerRawInBox( stream_t *p_stream, MP4_Box_t *p_container,
643
                                         uint8_t *p_buffer, uint64_t i_size, uint64_t i_offset )
644
1
{
645
1
    if(!p_container)
646
0
        return 0;
647
1
    stream_t *p_substream = vlc_stream_MemoryNew( p_stream, p_buffer, i_size,
648
1
                                                  true );
649
1
    if( !p_substream )
650
0
        return 0;
651
1
    MP4_Box_t *p_last = p_container->p_last;
652
1
    MP4_ReadBoxContainerChildren( p_substream, p_container, NULL );
653
1
    vlc_stream_Delete( p_substream );
654
    /* do pos fixup */
655
1
    if( p_container )
656
1
    {
657
1
        MP4_Box_t *p_box = p_last ? p_last : p_container->p_first;
658
1
        MP4_BoxOffsetUp(p_box, i_offset);
659
1
    }
660
661
1
    return 1;
662
1
}
663
664
static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
665
233
{
666
233
    if( p_container->i_size &&
667
233
        ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
668
138
    {
669
        /* container is empty, 8 stand for the first header in this box */
670
138
        return 1;
671
138
    }
672
673
    /* enter box */
674
95
    if ( MP4_Seek( p_stream, p_container->i_pos +
675
95
                      mp4_box_headersize( p_container ) ) )
676
0
        return 0;
677
95
    return MP4_ReadBoxContainerChildren( p_stream, p_container, NULL );
678
95
}
679
680
static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
681
18
{
682
    /* XXX sometime moov is hidden in a free box */
683
18
    if( p_box->p_father &&
684
18
        p_box->p_father->i_type == ATOM_root &&
685
12
        p_box->i_type == ATOM_free )
686
0
    {
687
0
        const uint8_t *p_peek;
688
0
        size_t header_size = mp4_box_headersize( p_box ) + 4;
689
0
        vlc_fourcc_t i_fcc;
690
691
0
        ssize_t i_read = vlc_stream_Peek( p_stream, &p_peek, 44 );
692
0
        if( unlikely(i_read < (ssize_t)header_size) )
693
0
            return 0;
694
695
0
        p_peek += header_size;
696
0
        i_read -= header_size;
697
698
0
        if( i_read >= 8 )
699
0
        {
700
0
            i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
701
702
0
            if( i_fcc == ATOM_cmov || i_fcc == ATOM_mvhd )
703
0
            {
704
0
                msg_Warn( p_stream, "detected moov hidden in a free box ..." );
705
706
0
                p_box->i_type = ATOM_foov;
707
0
                return MP4_ReadBoxContainer( p_stream, p_box );
708
0
            }
709
0
        }
710
0
    }
711
712
    /* Nothing to do */
713
#ifdef MP4_ULTRA_VERBOSE
714
    if MP4_BOX_TYPE_ASCII()
715
        msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
716
    else
717
        msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
718
#endif
719
18
    return 1;
720
18
}
721
722
static int MP4_ReadBox_mdia( stream_t *p_stream, MP4_Box_t *p_container )
723
1
{
724
1
    if( p_container->i_size &&
725
1
        ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
726
0
    {
727
        /* container is empty, 8 stand for the first header in this box */
728
0
        return 1;
729
0
    }
730
731
    /* enter box */
732
1
    if ( MP4_Seek( p_stream, p_container->i_pos +
733
1
                               mp4_box_headersize( p_container ) ) )
734
0
        return 0;
735
1
    int ret = MP4_ReadBoxContainerChildren( p_stream, p_container, NULL );
736
1
    MP4_Box_t *stsd = MP4_BoxGet( p_container, "minf/stbl/stsd" );
737
1
    if( stsd && stsd->i_handler == 0 )
738
0
    {
739
        /* Delayed parsing due to missing hdlr */
740
0
        if( MP4_Seek( p_stream, stsd->i_pos ) ||
741
0
            MP4_Box_Read_Specific( p_stream, stsd, stsd->p_father ) ||
742
0
            stsd->i_handler == 0 )
743
0
            msg_Warn( p_stream, "Failed to re-parse stsd" );
744
0
    }
745
746
1
    return ret;
747
1
}
748
749
static int MP4_ReadBox_ilst( stream_t *p_stream, MP4_Box_t *p_box )
750
0
{
751
0
    if( p_box->i_size < 8 || vlc_stream_Read( p_stream, NULL, 8 ) != 8 )
752
0
        return 0;
753
754
    /* Find our handler */
755
0
    if ( !p_box->i_handler && p_box->p_father )
756
0
    {
757
0
        const MP4_Box_t *p_sibling = p_box->p_father->p_first;
758
0
        while( p_sibling )
759
0
        {
760
0
            if ( p_sibling->i_type == ATOM_hdlr && p_sibling->data.p_hdlr )
761
0
            {
762
0
                p_box->i_handler = p_sibling->data.p_hdlr->i_handler_type;
763
0
                break;
764
0
            }
765
0
            p_sibling = p_sibling->p_next;
766
0
        }
767
0
    }
768
769
0
    switch( p_box->i_handler )
770
0
    {
771
0
    case 0:
772
0
        msg_Warn( p_stream, "no handler for ilst atom" );
773
0
        return 0;
774
0
    case HANDLER_mdta:
775
0
        return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_box, NULL, NULL, true );
776
0
    case HANDLER_mdir:
777
0
        return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
778
0
    default:
779
0
        msg_Warn( p_stream, "Unknown ilst handler type '%4.4s'", (char*)&p_box->i_handler );
780
0
        return 0;
781
0
    }
782
0
}
783
784
static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
785
182
{
786
182
    free( p_box->data.p_ftyp->i_compatible_brands );
787
182
}
788
789
static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
790
182
{
791
182
    MP4_READBOX_ENTER( MP4_Box_data_ftyp_t, MP4_FreeBox_ftyp );
792
793
182
    MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
794
182
    MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
795
796
182
    p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4;
797
182
    if( p_box->data.p_ftyp->i_compatible_brands_count > 0 )
798
94
    {
799
94
        uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
800
94
            vlc_alloc( p_box->data.p_ftyp->i_compatible_brands_count,
801
94
                       sizeof(uint32_t) );
802
803
94
        if( unlikely( tab == NULL ) )
804
0
            MP4_READBOX_EXIT( 0 );
805
806
473
        for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
807
379
        {
808
379
            MP4_GETFOURCC( tab[i] );
809
379
        }
810
94
    }
811
88
    else
812
88
    {
813
88
        p_box->data.p_ftyp->i_compatible_brands = NULL;
814
88
    }
815
816
182
    MP4_READBOX_EXIT( 1 );
817
182
}
818
819
820
static int MP4_ReadBox_mvhd(  stream_t *p_stream, MP4_Box_t *p_box )
821
1
{
822
1
    MP4_READBOX_ENTER( MP4_Box_data_mvhd_t, NULL );
823
824
1
    MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
825
826
1
    if( p_box->data.p_mvhd->i_version )
827
0
    {
828
0
        MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
829
0
        MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
830
0
        MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
831
0
        MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
832
0
    }
833
1
    else
834
1
    {
835
1
        MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
836
1
        MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
837
1
        MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
838
1
        MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
839
1
    }
840
1
    MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
841
1
    MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
842
1
    MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );
843
844
845
3
    for( unsigned i = 0; i < 2; i++ )
846
2
    {
847
2
        MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
848
2
    }
849
10
    for( unsigned i = 0; i < 9; i++ )
850
9
    {
851
9
        MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
852
9
    }
853
7
    for( unsigned i = 0; i < 6; i++ )
854
6
    {
855
6
        MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
856
6
    }
857
858
1
    MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
859
860
861
1
#ifdef MP4_VERBOSE
862
1
    char *psz_duration = MP4_Time2Str( p_box->data.p_mvhd->i_duration, p_box->data.p_mvhd->i_timescale );
863
1
    msg_Dbg( p_stream, "read box: \"mvhd\" timescale %"PRIu32" duration %"PRIu64" (%s) rate %.2f volume %.2f",
864
1
                  p_box->data.p_mvhd->i_timescale,
865
1
                  p_box->data.p_mvhd->i_duration,
866
1
                  psz_duration,
867
1
                  (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
868
1
                  (float)p_box->data.p_mvhd->i_volume / 256 );
869
1
    free( psz_duration );
870
1
#endif
871
1
    MP4_READBOX_EXIT( 1 );
872
1
}
873
874
static int MP4_ReadBox_mfhd(  stream_t *p_stream, MP4_Box_t *p_box )
875
0
{
876
0
    MP4_READBOX_ENTER( MP4_Box_data_mfhd_t, NULL );
877
878
0
    MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );
879
880
0
    MP4_GET4BYTES( p_box->data.p_mfhd->i_sequence_number );
881
882
0
#ifdef MP4_VERBOSE
883
0
    msg_Dbg( p_stream, "read box: \"mfhd\" sequence number %d",
884
0
                  p_box->data.p_mfhd->i_sequence_number );
885
0
#endif
886
0
    MP4_READBOX_EXIT( 1 );
887
0
}
888
889
static int MP4_ReadBox_tfxd(  stream_t *p_stream, MP4_Box_t *p_box )
890
0
{
891
0
    MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, NULL );
892
893
0
    MP4_Box_data_tfxd_t *p_tfxd_data = p_box->data.p_tfxd;
894
0
    MP4_GETVERSIONFLAGS( p_tfxd_data );
895
896
0
    if( p_tfxd_data->i_version == 0 )
897
0
    {
898
0
        MP4_GET4BYTES( p_tfxd_data->i_fragment_abs_time );
899
0
        MP4_GET4BYTES( p_tfxd_data->i_fragment_duration );
900
0
    }
901
0
    else
902
0
    {
903
0
        MP4_GET8BYTES( p_tfxd_data->i_fragment_abs_time );
904
0
        MP4_GET8BYTES( p_tfxd_data->i_fragment_duration );
905
0
    }
906
907
0
#ifdef MP4_VERBOSE
908
0
    msg_Dbg( p_stream, "read box: \"tfxd\" version %d, flags 0x%x, "\
909
0
            "fragment duration %"PRIu64", fragment abs time %"PRIu64,
910
0
                p_tfxd_data->i_version,
911
0
                p_tfxd_data->i_flags,
912
0
                p_tfxd_data->i_fragment_duration,
913
0
                p_tfxd_data->i_fragment_abs_time
914
0
           );
915
0
#endif
916
917
0
    MP4_READBOX_EXIT( 1 );
918
0
}
919
920
static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
921
0
{
922
0
    free( p_box->data.p_tfrf->p_tfrf_data_fields );
923
0
}
924
925
static int MP4_ReadBox_tfrf(  stream_t *p_stream, MP4_Box_t *p_box )
926
0
{
927
0
    MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, MP4_FreeBox_tfrf );
928
929
0
    MP4_Box_data_tfrf_t *p_tfrf_data = p_box->data.p_tfrf;
930
0
    MP4_GETVERSIONFLAGS( p_tfrf_data );
931
932
0
    MP4_GET1BYTE( p_tfrf_data->i_fragment_count );
933
934
0
    p_tfrf_data->p_tfrf_data_fields = calloc( p_tfrf_data->i_fragment_count,
935
0
                                              sizeof( TfrfBoxDataFields_t ) );
936
0
    if( !p_tfrf_data->p_tfrf_data_fields )
937
0
        MP4_READBOX_EXIT( 0 );
938
939
0
    for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
940
0
    {
941
0
        TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
942
0
        if( p_tfrf_data->i_version == 0 )
943
0
        {
944
0
            MP4_GET4BYTES( TfrfBoxDataField->i_fragment_abs_time );
945
0
            MP4_GET4BYTES( TfrfBoxDataField->i_fragment_duration );
946
0
        }
947
0
        else
948
0
        {
949
0
            MP4_GET8BYTES( TfrfBoxDataField->i_fragment_abs_time );
950
0
            MP4_GET8BYTES( TfrfBoxDataField->i_fragment_duration );
951
0
        }
952
0
    }
953
954
0
#ifdef MP4_VERBOSE
955
0
    msg_Dbg( p_stream, "read box: \"tfrf\" version %d, flags 0x%x, "\
956
0
            "fragment count %"PRIu8, p_tfrf_data->i_version,
957
0
                p_tfrf_data->i_flags, p_tfrf_data->i_fragment_count );
958
959
0
    for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
960
0
    {
961
0
        TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
962
0
        msg_Dbg( p_stream, "\"tfrf\" fragment duration %"PRIu64", "\
963
0
                                    "fragment abs time %"PRIu64,
964
0
                    TfrfBoxDataField->i_fragment_duration,
965
0
                    TfrfBoxDataField->i_fragment_abs_time );
966
0
    }
967
968
0
#endif
969
970
0
    MP4_READBOX_EXIT( 1 );
971
0
}
972
973
static int MP4_ReadBox_XML360( stream_t *p_stream, MP4_Box_t *p_box )
974
0
{
975
0
    MP4_READBOX_ENTER( MP4_Box_data_360_t, NULL );
976
977
0
    MP4_Box_data_360_t *p_360_data = p_box->data.p_360;
978
979
    /* Copy the string for pattern matching as it does not end
980
    with a '\0' in the stream. */
981
0
    char *psz_rdf = strndup((char *)p_peek, i_read);
982
983
0
    if ( unlikely( !psz_rdf ) )
984
0
        MP4_READBOX_EXIT( 0 );
985
986
    /* Try to find the string "GSpherical:Spherical" because the v1
987
    spherical video spec says the tag must be there. */
988
989
0
    if ( strcasestr( psz_rdf, "Gspherical:Spherical" ) )
990
0
        p_360_data->i_projection_mode = PROJECTION_MODE_EQUIRECTANGULAR;
991
992
    /* Try to find the stero mode. */
993
0
    if ( strcasestr( psz_rdf, "left-right" ) )
994
0
    {
995
0
        msg_Dbg( p_stream, "Left-right stereo mode" );
996
0
        p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_LEFT_RIGHT;
997
0
    }
998
999
0
    if ( strcasestr( psz_rdf, "top-bottom" ) )
1000
0
    {
1001
0
        msg_Dbg( p_stream, "Top-bottom stereo mode" );
1002
0
        p_360_data->e_stereo_mode = XML360_STEREOSCOPIC_TOP_BOTTOM;
1003
0
    }
1004
1005
0
    free( psz_rdf );
1006
1007
0
    MP4_READBOX_EXIT( 1 );
1008
0
}
1009
1010
static int MP4_ReadBox_st3d( stream_t *p_stream, MP4_Box_t *p_box )
1011
0
{
1012
0
    MP4_READBOX_ENTER( MP4_Box_data_st3d_t, NULL );
1013
1014
0
    uint8_t i_version;
1015
0
    MP4_GET1BYTE( i_version );
1016
0
    if ( i_version != 0 )
1017
0
        MP4_READBOX_EXIT( 0 );
1018
1019
0
    MP4_SKIPBYTES( 3 ); // flags
1020
1021
0
    MP4_Box_data_st3d_t *p_data = p_box->data.p_st3d;
1022
0
    MP4_GET1BYTE( p_data->i_stereo_mode );
1023
1024
0
    MP4_READBOX_EXIT( 1 );
1025
0
}
1026
1027
static int MP4_ReadBox_prhd( stream_t *p_stream, MP4_Box_t *p_box )
1028
0
{
1029
0
    MP4_READBOX_ENTER( MP4_Box_data_prhd_t, NULL );
1030
1031
0
    uint8_t i_version;
1032
0
    MP4_GET1BYTE( i_version );
1033
0
    if (i_version != 0)
1034
0
        MP4_READBOX_EXIT( 0 );
1035
1036
0
    MP4_SKIPBYTES( 3 ); // flags
1037
1038
0
    MP4_Box_data_prhd_t *p_data = p_box->data.p_prhd;
1039
0
    int32_t fixed16_16;
1040
0
    MP4_GET4BYTES( fixed16_16 );
1041
0
    p_data->f_pose_yaw_degrees   = (float) fixed16_16 / 65536.0f;
1042
1043
0
    MP4_GET4BYTES( fixed16_16 );
1044
0
    p_data->f_pose_pitch_degrees = (float) fixed16_16 / 65536.0f;
1045
1046
0
    MP4_GET4BYTES( fixed16_16 );
1047
0
    p_data->f_pose_roll_degrees  = (float) fixed16_16 / 65536.0f;
1048
1049
0
    MP4_READBOX_EXIT( 1 );
1050
0
}
1051
1052
static int MP4_ReadBox_equi( stream_t *p_stream, MP4_Box_t *p_box )
1053
0
{
1054
0
    MP4_READBOX_ENTER( MP4_Box_data_equi_t, NULL );
1055
1056
0
    uint8_t i_version;
1057
0
    MP4_GET1BYTE( i_version );
1058
0
    if (i_version != 0)
1059
0
        MP4_READBOX_EXIT( 0 );
1060
1061
0
    MP4_SKIPBYTES( 3 ); // flags
1062
1063
0
    MP4_Box_data_equi_t *p_data = p_box->data.p_equi;
1064
0
    MP4_GET4BYTES( p_data->i_projection_bounds_top );
1065
0
    MP4_GET4BYTES( p_data->i_projection_bounds_bottom );
1066
0
    MP4_GET4BYTES( p_data->i_projection_bounds_left );
1067
0
    MP4_GET4BYTES( p_data->i_projection_bounds_right );
1068
1069
0
    MP4_READBOX_EXIT( 1 );
1070
0
}
1071
1072
static int MP4_ReadBox_cbmp( stream_t *p_stream, MP4_Box_t *p_box )
1073
0
{
1074
0
    MP4_READBOX_ENTER( MP4_Box_data_cbmp_t, NULL );
1075
1076
0
    uint8_t i_version;
1077
0
    MP4_GET1BYTE( i_version );
1078
0
    if (i_version != 0)
1079
0
        MP4_READBOX_EXIT( 0 );
1080
1081
0
    MP4_SKIPBYTES( 3 ); // flags
1082
1083
0
    MP4_Box_data_cbmp_t *p_data = p_box->data.p_cbmp;
1084
0
    MP4_GET4BYTES( p_data->i_layout );
1085
0
    MP4_GET4BYTES( p_data->i_padding );
1086
1087
0
    MP4_READBOX_EXIT( 1 );
1088
0
}
1089
1090
static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
1091
0
{
1092
0
    free( p_box->data.p_sidx->p_items );
1093
0
}
1094
1095
static int MP4_ReadBox_sidx(  stream_t *p_stream, MP4_Box_t *p_box )
1096
0
{
1097
0
    MP4_READBOX_ENTER( MP4_Box_data_sidx_t, MP4_FreeBox_sidx );
1098
1099
0
    MP4_Box_data_sidx_t *p_sidx_data = p_box->data.p_sidx;
1100
0
    MP4_GETVERSIONFLAGS( p_sidx_data );
1101
1102
0
    MP4_GET4BYTES( p_sidx_data->i_reference_ID );
1103
0
    MP4_GET4BYTES( p_sidx_data->i_timescale );
1104
1105
0
    if( p_sidx_data->i_version == 0 )
1106
0
    {
1107
0
        MP4_GET4BYTES( p_sidx_data->i_earliest_presentation_time );
1108
0
        MP4_GET4BYTES( p_sidx_data->i_first_offset );
1109
0
    }
1110
0
    else
1111
0
    {
1112
0
        MP4_GET8BYTES( p_sidx_data->i_earliest_presentation_time );
1113
0
        MP4_GET8BYTES( p_sidx_data->i_first_offset );
1114
0
    }
1115
1116
0
    uint16_t i_count;
1117
1118
0
    MP4_SKIPBYTES( 2 ); // reserved
1119
0
    MP4_GET2BYTES( i_count );
1120
0
    if( i_count == 0 )
1121
0
        MP4_READBOX_EXIT( 1 );
1122
1123
0
    p_sidx_data->i_reference_count = i_count;
1124
0
    p_sidx_data->p_items = vlc_alloc( i_count, sizeof( MP4_Box_sidx_item_t ) );
1125
0
    if( unlikely(p_sidx_data->p_items == NULL) )
1126
0
        MP4_READBOX_EXIT( 0 );
1127
1128
0
    for( unsigned i = 0; i < i_count; i++ )
1129
0
    {
1130
0
        MP4_Box_sidx_item_t *item = p_sidx_data->p_items + i;
1131
0
        uint32_t tmp;
1132
1133
0
        MP4_GET4BYTES( tmp );
1134
0
        item->b_reference_type = tmp >> 31;
1135
0
        item->i_referenced_size = tmp & 0x7fffffff;
1136
0
        MP4_GET4BYTES( item->i_subsegment_duration );
1137
1138
0
        MP4_GET4BYTES( tmp );
1139
0
        item->b_starts_with_SAP = tmp >> 31;
1140
0
        item->i_SAP_type = (tmp >> 24) & 0x70;
1141
0
        item->i_SAP_delta_time = tmp & 0xfffffff;
1142
0
    }
1143
1144
0
#ifdef MP4_VERBOSE
1145
0
    msg_Dbg( p_stream, "read box: \"sidx\" version %d, flags 0x%x, "\
1146
0
            "ref_ID %"PRIu32", timescale %"PRIu32", ref_count %"PRIu16", "\
1147
0
            "first subsegmt duration %"PRIu32,
1148
0
                p_sidx_data->i_version,
1149
0
                p_sidx_data->i_flags,
1150
0
                p_sidx_data->i_reference_ID,
1151
0
                p_sidx_data->i_timescale,
1152
0
                p_sidx_data->i_reference_count,
1153
0
                p_sidx_data->p_items[0].i_subsegment_duration
1154
0
           );
1155
0
#endif
1156
1157
0
    MP4_READBOX_EXIT( 1 );
1158
0
}
1159
1160
static int MP4_ReadBox_tfhd(  stream_t *p_stream, MP4_Box_t *p_box )
1161
0
{
1162
0
    MP4_READBOX_ENTER( MP4_Box_data_tfhd_t, NULL );
1163
1164
0
    MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );
1165
1166
0
    if( p_box->data.p_tfhd->i_version != 0 )
1167
0
    {
1168
0
        msg_Warn( p_stream, "'tfhd' box with version != 0. "\
1169
0
                " Don't know what to do with that, please patch" );
1170
0
        MP4_READBOX_EXIT( 0 );
1171
0
    }
1172
1173
0
    MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );
1174
1175
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
1176
0
    {
1177
0
        msg_Dbg( p_stream, "'duration-is-empty' flag is present "\
1178
0
                "=> no samples for this time interval." );
1179
0
        p_box->data.p_tfhd->b_empty = true;
1180
0
    }
1181
0
    else
1182
0
        p_box->data.p_tfhd->b_empty = false;
1183
1184
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
1185
0
        MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
1186
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
1187
0
        MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
1188
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
1189
0
        MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
1190
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
1191
0
        MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
1192
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
1193
0
        MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
1194
1195
0
#ifdef MP4_VERBOSE
1196
0
    char psz_base[128] = "\0";
1197
0
    char psz_desc[128] = "\0";
1198
0
    char psz_dura[128] = "\0";
1199
0
    char psz_size[128] = "\0";
1200
0
    char psz_flag[128] = "\0";
1201
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
1202
0
        snprintf(psz_base, sizeof(psz_base), "base offset %"PRId64, p_box->data.p_tfhd->i_base_data_offset);
1203
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
1204
0
        snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
1205
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
1206
0
        snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
1207
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
1208
0
        snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
1209
0
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
1210
0
        snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);
1211
1212
0
    msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
1213
0
                p_box->data.p_tfhd->i_version,
1214
0
                p_box->data.p_tfhd->i_flags,
1215
0
                p_box->data.p_tfhd->i_track_ID,
1216
0
                psz_base, psz_desc, psz_dura, psz_size, psz_flag );
1217
0
#endif
1218
1219
0
    MP4_READBOX_EXIT( 1 );
1220
0
}
1221
1222
static void MP4_FreeBox_trun( MP4_Box_t *p_box )
1223
0
{
1224
0
    free( p_box->data.p_trun->p_samples );
1225
0
}
1226
1227
static int MP4_ReadBox_trun(  stream_t *p_stream, MP4_Box_t *p_box )
1228
0
{
1229
0
    uint32_t count;
1230
1231
0
    MP4_READBOX_ENTER( MP4_Box_data_trun_t, MP4_FreeBox_trun );
1232
0
    MP4_Box_data_trun_t *p_trun = p_box->data.p_trun;
1233
0
    MP4_GETVERSIONFLAGS( p_trun );
1234
0
    MP4_GET4BYTES( count );
1235
1236
0
    if( p_trun->i_flags & MP4_TRUN_DATA_OFFSET )
1237
0
        MP4_GET4BYTES( p_trun->i_data_offset );
1238
0
    if( p_trun->i_flags & MP4_TRUN_FIRST_FLAGS )
1239
0
        MP4_GET4BYTES( p_trun->i_first_sample_flags );
1240
1241
0
    uint64_t i_entry_size =
1242
0
        !!(p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION) +
1243
0
        !!(p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE) +
1244
0
        !!(p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS) +
1245
0
        !!(p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET);
1246
1247
0
    if( i_entry_size * 4 * count > i_read )
1248
0
        MP4_READBOX_EXIT( 0 );
1249
1250
0
    p_trun->p_samples = vlc_alloc( count, sizeof(MP4_descriptor_trun_sample_t) );
1251
0
    if ( p_trun->p_samples == NULL )
1252
0
        MP4_READBOX_EXIT( 0 );
1253
0
    p_trun->i_sample_count = count;
1254
1255
0
    for( unsigned int i = 0; i < count; i++ )
1256
0
    {
1257
0
        MP4_descriptor_trun_sample_t *p_sample = &p_trun->p_samples[i];
1258
0
        if( p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
1259
0
            MP4_GET4BYTES( p_sample->i_duration );
1260
0
        if( p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
1261
0
            MP4_GET4BYTES( p_sample->i_size );
1262
0
        if( p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS )
1263
0
            MP4_GET4BYTES( p_sample->i_flags );
1264
0
        if( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
1265
0
            MP4_GET4BYTES( p_sample->i_composition_time_offset.v0 );
1266
0
    }
1267
1268
#ifdef MP4_ULTRA_VERBOSE
1269
    msg_Dbg( p_stream, "read box: \"trun\" version %u flags 0x%x sample count %"PRIu32,
1270
                  p_trun->i_version,
1271
                  p_trun->i_flags,
1272
                  p_trun->i_sample_count );
1273
1274
    for( unsigned int i = 0; i < count; i++ )
1275
    {
1276
        MP4_descriptor_trun_sample_t *p_sample = &p_trun->p_samples[i];
1277
        msg_Dbg( p_stream, "read box: \"trun\" sample %4.4u flags 0x%x "\
1278
            "duration %"PRIu32" size %"PRIu32" composition time offset %"PRIu32,
1279
                        i, p_sample->i_flags, p_sample->i_duration,
1280
                        p_sample->i_size, p_sample->i_composition_time_offset );
1281
    }
1282
#endif
1283
1284
0
    MP4_READBOX_EXIT( 1 );
1285
0
}
1286
1287
static int MP4_ReadBox_tfdt( stream_t *p_stream, MP4_Box_t *p_box )
1288
0
{
1289
0
    MP4_READBOX_ENTER( MP4_Box_data_tfdt_t, NULL );
1290
0
    if( i_read < 8 )
1291
0
        MP4_READBOX_EXIT( 0 );
1292
1293
0
    MP4_GETVERSIONFLAGS( p_box->data.p_tfdt );
1294
1295
0
    if( p_box->data.p_tfdt->i_version == 0 )
1296
0
        MP4_GET4BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1297
0
    else if( p_box->data.p_tfdt->i_version == 1 )
1298
0
        MP4_GET8BYTES( p_box->data.p_tfdt->i_base_media_decode_time );
1299
0
    else
1300
0
        MP4_READBOX_EXIT( 0 );
1301
1302
0
    MP4_READBOX_EXIT( 1 );
1303
0
}
1304
1305
static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
1306
1
{
1307
1
    MP4_READBOX_ENTER( MP4_Box_data_tkhd_t, NULL );
1308
1309
1
    MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );
1310
1311
1
    if( p_box->data.p_tkhd->i_version )
1312
0
    {
1313
0
        MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
1314
0
        MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
1315
0
        MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1316
0
        MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1317
0
        MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
1318
0
    }
1319
1
    else
1320
1
    {
1321
1
        MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
1322
1
        MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
1323
1
        MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
1324
1
        MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
1325
1
        MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
1326
1
    }
1327
1328
3
    for( unsigned i = 0; i < 2; i++ )
1329
2
    {
1330
2
        MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
1331
2
    }
1332
1
    MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
1333
1
    MP4_GET2BYTES( p_box->data.p_tkhd->i_alternate_group );
1334
1
    MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
1335
1
    MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );
1336
1337
10
    for( unsigned i = 0; i < 9; i++ )
1338
9
    {
1339
9
        MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
1340
9
    }
1341
1
    MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
1342
1
    MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
1343
1344
1
    double rotation = 0;//angle in degrees to be rotated clockwise
1345
1
    double scale[2];    // scale factor; sx = scale[0] , sy = scale[1]
1346
1
    int32_t *matrix = p_box->data.p_tkhd->i_matrix;
1347
1348
1
    int64_t det = (int64_t)matrix[0] * matrix[4] - (int64_t)matrix[1] * matrix[3];
1349
1
    if (det < 0) {
1350
        /* If determinant is negative copy the matrix and flip it horizontally. */
1351
0
        const int flip[] = { -1, 1, 1 };
1352
0
        for (int j = 0; j < 9; j++)
1353
0
            matrix[j] *= flip[j % 3];
1354
0
        p_box->data.p_tkhd->i_flip = 1;
1355
0
    }
1356
1357
1
    scale[0] = sqrt(conv_fx(matrix[0]) * conv_fx(matrix[0]) +
1358
1
                    conv_fx(matrix[3]) * conv_fx(matrix[3]));
1359
1
    scale[1] = sqrt(conv_fx(matrix[1]) * conv_fx(matrix[1]) +
1360
1
                    conv_fx(matrix[4]) * conv_fx(matrix[4]));
1361
1362
1
    if( likely(scale[0] > 0 && scale[1] > 0) )
1363
1
    {
1364
1
        rotation = atan2(conv_fx(matrix[1]) / scale[1],
1365
1
                         conv_fx(matrix[0]) / scale[0]) * 180 / M_PI;
1366
1
        if (rotation < 0)
1367
0
            rotation += 360.;
1368
1
    }
1369
1370
1
    p_box->data.p_tkhd->f_rotation = rotation;
1371
1372
1
#ifdef MP4_VERBOSE
1373
1
    double translate[2];// amount to translate; tx = translate[0] , ty = translate[1]
1374
1375
1
    translate[0] = conv_fx(matrix[6]);
1376
1
    translate[1] = conv_fx(matrix[7]);
1377
1378
1
    msg_Dbg( p_stream, "read box: \"tkhd\" track #%"PRIu32" duration %"PRIu64" layer %d "
1379
1
                       "volume %3.1f rotation %3.1f scale %.2fx%.2f translate +%.2f+%.2f size %ux%u. "
1380
1
                       "Matrix: %i %i %i %i %i %i %i %i %i",
1381
1
                  p_box->data.p_tkhd->i_track_ID,
1382
1
                  p_box->data.p_tkhd->i_duration,
1383
1
                  p_box->data.p_tkhd->i_layer,
1384
1
                  (float)p_box->data.p_tkhd->i_volume / 256 ,
1385
1
                  rotation,
1386
1
                  scale[0],
1387
1
                  scale[1],
1388
1
                  translate[0],
1389
1
                  translate[1],
1390
1
                  (unsigned)p_box->data.p_tkhd->i_width / BLOCK16x16,
1391
1
                  (unsigned)p_box->data.p_tkhd->i_height / BLOCK16x16,
1392
1
                  p_box->data.p_tkhd->i_matrix[0],
1393
1
                  p_box->data.p_tkhd->i_matrix[1],
1394
1
                  p_box->data.p_tkhd->i_matrix[2],
1395
1
                  p_box->data.p_tkhd->i_matrix[3],
1396
1
                  p_box->data.p_tkhd->i_matrix[4],
1397
1
                  p_box->data.p_tkhd->i_matrix[5],
1398
1
                  p_box->data.p_tkhd->i_matrix[6],
1399
1
                  p_box->data.p_tkhd->i_matrix[7],
1400
1
                  p_box->data.p_tkhd->i_matrix[8] );
1401
1
#endif
1402
1
    MP4_READBOX_EXIT( 1 );
1403
1
}
1404
1405
static int MP4_ReadBox_load( stream_t *p_stream, MP4_Box_t *p_box )
1406
0
{
1407
0
    if ( p_box->i_size != 24 )
1408
0
        return 0;
1409
0
    MP4_READBOX_ENTER( MP4_Box_data_load_t, NULL );
1410
0
    MP4_GET4BYTES( p_box->data.p_load->i_start_time );
1411
0
    MP4_GET4BYTES( p_box->data.p_load->i_duration );
1412
0
    MP4_GET4BYTES( p_box->data.p_load->i_flags );
1413
0
    MP4_GET4BYTES( p_box->data.p_load->i_hints );
1414
0
    MP4_READBOX_EXIT( 1 );
1415
0
}
1416
1417
static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
1418
1
{
1419
1
    uint16_t i_language;
1420
1
    MP4_READBOX_ENTER( MP4_Box_data_mdhd_t, NULL );
1421
1422
1
    MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );
1423
1424
1
    if( p_box->data.p_mdhd->i_version )
1425
0
    {
1426
0
        MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
1427
0
        MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
1428
0
        MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1429
0
        MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
1430
0
    }
1431
1
    else
1432
1
    {
1433
1
        MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
1434
1
        MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
1435
1
        MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
1436
1
        MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
1437
1
    }
1438
1439
1
    MP4_GET2BYTES( i_language );
1440
1
    decodeQtLanguageCode( i_language, p_box->data.p_mdhd->rgs_language,
1441
1
                          &p_box->data.p_mdhd->b_mac_encoding );
1442
1443
1
    MP4_GET2BYTES( p_box->data.p_mdhd->i_quality );
1444
1445
1
#ifdef MP4_VERBOSE
1446
1
    char *psz_duration = MP4_Time2Str( p_box->data.p_mdhd->i_duration, p_box->data.p_mdhd->i_timescale );
1447
1
    msg_Dbg( p_stream, "read box: \"mdhd\" timescale %"PRIu32" duration %"PRIu64" (%s) language %3.3s",
1448
1
                  p_box->data.p_mdhd->i_timescale,
1449
1
                  p_box->data.p_mdhd->i_duration,
1450
1
                  psz_duration,
1451
1
                  (char*) &p_box->data.p_mdhd->rgs_language );
1452
1
    free( psz_duration );
1453
1
#endif
1454
1
    MP4_READBOX_EXIT( 1 );
1455
1
}
1456
1457
static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
1458
69
{
1459
69
    free( p_box->data.p_hdlr->psz_name );
1460
69
}
1461
1462
static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
1463
69
{
1464
69
    MP4_READBOX_ENTER( MP4_Box_data_hdlr_t, MP4_FreeBox_hdlr );
1465
1466
69
    MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
1467
1468
69
    MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
1469
69
    MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );
1470
1471
69
    MP4_SKIPBYTES( 3*4 ); // reserved[3]
1472
69
    p_box->data.p_hdlr->psz_name = NULL;
1473
1474
69
    if( i_read >= SSIZE_MAX )
1475
0
        MP4_READBOX_EXIT( 0 );
1476
1477
69
    if( i_read > 0 )
1478
69
    {
1479
69
        size_t i_copy;
1480
1481
        /* Yes, I love .mp4 :( */
1482
69
        if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
1483
1
        {
1484
1
            uint8_t i_len;
1485
1486
1
            MP4_GET1BYTE( i_len );
1487
1
            i_copy = (i_len <= i_read) ? i_len : i_read;
1488
1
        }
1489
68
        else
1490
68
            i_copy = i_read;
1491
1492
69
        uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_copy + 1 );
1493
69
        if( unlikely( psz == NULL ) )
1494
0
            MP4_READBOX_EXIT( 0 );
1495
1496
69
        memcpy( psz, p_peek, i_copy );
1497
69
        p_box->data.p_hdlr->psz_name[i_copy] = '\0';
1498
69
    }
1499
1500
69
#ifdef MP4_VERBOSE
1501
69
        msg_Dbg( p_stream, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
1502
69
                   (char*)&p_box->data.p_hdlr->i_handler_type,
1503
69
                   p_box->data.p_hdlr->psz_name );
1504
1505
69
#endif
1506
69
    MP4_READBOX_EXIT( 1 );
1507
69
}
1508
1509
static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
1510
0
{
1511
0
    MP4_READBOX_ENTER( MP4_Box_data_vmhd_t, NULL );
1512
1513
0
    MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
1514
1515
0
    MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
1516
0
    for( unsigned i = 0; i < 3; i++ )
1517
0
    {
1518
0
        MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
1519
0
    }
1520
1521
0
#ifdef MP4_VERBOSE
1522
0
    msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %" PRIu16 " opcolor (%" PRIu16 ", %" PRIu16 ", %" PRIu16 ")",
1523
0
                      p_box->data.p_vmhd->i_graphics_mode,
1524
0
                      p_box->data.p_vmhd->i_opcolor[0],
1525
0
                      p_box->data.p_vmhd->i_opcolor[1],
1526
0
                      p_box->data.p_vmhd->i_opcolor[2] );
1527
0
#endif
1528
0
    MP4_READBOX_EXIT( 1 );
1529
0
}
1530
1531
static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
1532
1
{
1533
1
    MP4_READBOX_ENTER( MP4_Box_data_smhd_t, NULL );
1534
1535
1
    MP4_GETVERSIONFLAGS( p_box->data.p_smhd );
1536
1537
1538
1539
1
    MP4_GET2BYTES( p_box->data.p_smhd->i_balance );
1540
1541
1
    MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
1542
1543
1
#ifdef MP4_VERBOSE
1544
1
    msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
1545
1
                      (float)p_box->data.p_smhd->i_balance / 256 );
1546
1
#endif
1547
1
    MP4_READBOX_EXIT( 1 );
1548
1
}
1549
1550
1551
static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
1552
0
{
1553
0
    MP4_READBOX_ENTER( MP4_Box_data_hmhd_t, NULL );
1554
1555
0
    MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );
1556
1557
0
    MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
1558
0
    MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );
1559
1560
0
    MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
1561
0
    MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );
1562
1563
0
    MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );
1564
1565
0
#ifdef MP4_VERBOSE
1566
0
    msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1567
0
                      p_box->data.p_hmhd->i_max_PDU_size,
1568
0
                      p_box->data.p_hmhd->i_avg_PDU_size,
1569
0
                      p_box->data.p_hmhd->i_max_bitrate,
1570
0
                      p_box->data.p_hmhd->i_avg_bitrate );
1571
0
#endif
1572
0
    MP4_READBOX_EXIT( 1 );
1573
0
}
1574
1575
static void MP4_FreeBox_url( MP4_Box_t *p_box )
1576
0
{
1577
0
    free( p_box->data.p_url->psz_location );
1578
0
}
1579
1580
static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
1581
0
{
1582
0
    MP4_READBOX_ENTER( MP4_Box_data_url_t, MP4_FreeBox_url );
1583
1584
0
    MP4_GETVERSIONFLAGS( p_box->data.p_url );
1585
0
    MP4_GETSTRINGZ( p_box->data.p_url->psz_location );
1586
1587
0
#ifdef MP4_VERBOSE
1588
0
    msg_Dbg( p_stream, "read box: \"url\" url: %s",
1589
0
                       p_box->data.p_url->psz_location );
1590
1591
0
#endif
1592
0
    MP4_READBOX_EXIT( 1 );
1593
0
}
1594
1595
static void MP4_FreeBox_urn( MP4_Box_t *p_box )
1596
0
{
1597
0
    free( p_box->data.p_urn->psz_name );
1598
0
    free( p_box->data.p_urn->psz_location );
1599
0
}
1600
1601
static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
1602
0
{
1603
0
    MP4_READBOX_ENTER( MP4_Box_data_urn_t, MP4_FreeBox_urn );
1604
1605
0
    MP4_GETVERSIONFLAGS( p_box->data.p_urn );
1606
1607
0
    MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
1608
0
    MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );
1609
1610
0
#ifdef MP4_VERBOSE
1611
0
    msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
1612
0
                      p_box->data.p_urn->psz_name,
1613
0
                      p_box->data.p_urn->psz_location );
1614
0
#endif
1615
0
    MP4_READBOX_EXIT( 1 );
1616
0
}
1617
1618
static int MP4_ReadBox_LtdContainer( stream_t *p_stream, MP4_Box_t *p_box )
1619
1
{
1620
1
    MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_lcont_t, 16, NULL );
1621
1
    if( i_read < 8 )
1622
0
        MP4_READBOX_EXIT( 0 );
1623
1624
1
    MP4_GETVERSIONFLAGS( p_box->data.p_lcont );
1625
1
    if( p_box->data.p_lcont->i_version != 0 )
1626
0
        MP4_READBOX_EXIT( 0 );
1627
1
    MP4_GET4BYTES( p_box->data.p_lcont->i_entry_count );
1628
1629
1
    uint32_t i_entry = 0;
1630
1
    i_read = p_box->i_size - 16;
1631
1
    while (i_read > 8 && i_entry < p_box->data.p_lcont->i_entry_count )
1632
1
    {
1633
1
        MP4_Box_t *p_childbox = MP4_ReadBox( p_stream, p_box );
1634
1
        if( !p_childbox )
1635
1
            break;
1636
0
        MP4_BoxAddChild( p_box, p_childbox );
1637
0
        i_entry++;
1638
1639
0
        if( i_read < p_childbox->i_size )
1640
0
            MP4_READBOX_EXIT( 0 );
1641
1642
0
        i_read -= p_childbox->i_size;
1643
0
    }
1644
1645
1
    if (i_entry != p_box->data.p_lcont->i_entry_count)
1646
1
        p_box->data.p_lcont->i_entry_count = i_entry;
1647
1648
1
#ifdef MP4_VERBOSE
1649
1
    msg_Dbg( p_stream, "read box: \"%4.4s\" entry-count %d", (char *)&p_box->i_type,
1650
1
                        p_box->data.p_lcont->i_entry_count );
1651
1652
1
#endif
1653
1654
1
    if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
1655
0
        MP4_READBOX_EXIT( 0 );
1656
1657
1
    MP4_READBOX_EXIT( 1 );
1658
1
}
1659
1660
static void MP4_FreeBox_stts( MP4_Box_t *p_box )
1661
1
{
1662
1
    free( p_box->data.p_stts->pi_sample_count );
1663
1
    free( p_box->data.p_stts->pi_sample_delta );
1664
1
}
1665
1666
static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
1667
1
{
1668
1
    uint32_t count;
1669
1670
1
    MP4_READBOX_ENTER( MP4_Box_data_stts_t, MP4_FreeBox_stts );
1671
1672
1
    MP4_GETVERSIONFLAGS( p_box->data.p_stts );
1673
1
    MP4_GET4BYTES( count );
1674
1675
1
    if( UINT64_C(8) * count > i_read )
1676
0
    {
1677
        /*count = i_read / 8;*/
1678
0
        MP4_READBOX_EXIT( 0 );
1679
0
    }
1680
1681
1
    p_box->data.p_stts->pi_sample_count = vlc_alloc( count, sizeof(uint32_t) );
1682
1
    p_box->data.p_stts->pi_sample_delta = vlc_alloc( count, sizeof(uint32_t) );
1683
1
    p_box->data.p_stts->i_entry_count = count;
1684
1685
1
    if( p_box->data.p_stts->pi_sample_count == NULL
1686
1
     || p_box->data.p_stts->pi_sample_delta == NULL )
1687
0
    {
1688
0
        MP4_FreeBox_stts( p_box );
1689
0
        MP4_READBOX_EXIT( 0 );
1690
0
    }
1691
1692
2
    for( uint32_t i = 0; i < count; i++ )
1693
1
    {
1694
1
        MP4_GET4BYTES( p_box->data.p_stts->pi_sample_count[i] );
1695
1
        MP4_GET4BYTES( p_box->data.p_stts->pi_sample_delta[i] );
1696
        /* Patch bogus durations, including negative stored values */
1697
1
        if( p_box->data.p_stts->pi_sample_delta[i] == 0 ||
1698
1
            p_box->data.p_stts->pi_sample_delta[i] >= 0xF0000000 )
1699
0
            p_box->data.p_stts->pi_sample_delta[i] = 1;
1700
1
    }
1701
1702
1
#ifdef MP4_VERBOSE
1703
1
    msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
1704
1
                      p_box->data.p_stts->i_entry_count );
1705
1706
1
#endif
1707
1
    MP4_READBOX_EXIT( 1 );
1708
1
}
1709
1710
1711
static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
1712
0
{
1713
0
    free( p_box->data.p_ctts->pi_sample_count );
1714
0
    free( p_box->data.p_ctts->pi_sample_offset );
1715
0
}
1716
1717
static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
1718
0
{
1719
0
    uint32_t count;
1720
1721
0
    MP4_READBOX_ENTER( MP4_Box_data_ctts_t, MP4_FreeBox_ctts );
1722
1723
0
    MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1724
0
    MP4_GET4BYTES( count );
1725
1726
0
    if( UINT64_C(8) * count > i_read )
1727
0
        MP4_READBOX_EXIT( 0 );
1728
1729
0
    p_box->data.p_ctts->pi_sample_count = vlc_alloc( count, sizeof(uint32_t) );
1730
0
    p_box->data.p_ctts->pi_sample_offset = vlc_alloc( count, sizeof(int32_t) );
1731
0
    if( unlikely(p_box->data.p_ctts->pi_sample_count == NULL
1732
0
              || p_box->data.p_ctts->pi_sample_offset == NULL) )
1733
0
    {
1734
0
        MP4_FreeBox_ctts( p_box );
1735
0
        MP4_READBOX_EXIT( 0 );
1736
0
    }
1737
0
    p_box->data.p_ctts->i_entry_count = count;
1738
1739
0
    for( uint32_t i = 0; i < count; i++ )
1740
0
    {
1741
0
        MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_count[i] );
1742
0
        MP4_GET4BYTES( p_box->data.p_ctts->pi_sample_offset[i] );
1743
0
    }
1744
1745
0
#ifdef MP4_VERBOSE
1746
0
    msg_Dbg( p_stream, "read box: \"ctts\" entry-count %"PRIu32, count );
1747
1748
0
#endif
1749
0
    MP4_READBOX_EXIT( 1 );
1750
0
}
1751
1752
static int MP4_ReadBox_cslg( stream_t *p_stream, MP4_Box_t *p_box )
1753
0
{
1754
0
    MP4_READBOX_ENTER( MP4_Box_data_cslg_t, NULL );
1755
1756
0
    uint8_t i_version;
1757
0
    MP4_GET1BYTE( i_version );
1758
0
    MP4_SKIPBYTES( 3 ); // flags
1759
1760
0
    if( i_version > 1 )
1761
0
        MP4_READBOX_EXIT( 0 );
1762
1763
0
    union { int32_t s; uint32_t u; } u32;
1764
0
    union { int64_t s; uint64_t u; } u64;
1765
0
#define DOREAD_CSLG( readbytes, temp, member ) \
1766
0
        { readbytes( temp.u ); member = temp.s; }
1767
1768
0
#define READ_CSLG( readbytes, temp ) {\
1769
0
    DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->ct_to_dts_shift );\
1770
0
    DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->i_least_delta );\
1771
0
    DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->i_max_delta );\
1772
0
    DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->i_composition_starttime );\
1773
0
    DOREAD_CSLG( readbytes, temp, p_box->data.p_cslg->i_composition_endtime ); }
1774
1775
0
    if( i_version == 0 )
1776
0
        READ_CSLG( MP4_GET4BYTES, u32 )
1777
0
    else
1778
0
        READ_CSLG( MP4_GET8BYTES, u64 )
1779
1780
0
    MP4_READBOX_EXIT( 1 );
1781
0
}
1782
1783
static uint64_t MP4_ReadLengthDescriptor( uint8_t **restrict bufp,
1784
                                          uint64_t *restrict lenp )
1785
0
{
1786
0
    unsigned char *buf = *bufp;
1787
0
    uint64_t len = *lenp;
1788
0
    unsigned char b;
1789
0
    uint64_t value = 0;
1790
1791
0
    do
1792
0
    {
1793
0
        if (unlikely(len == 0))
1794
0
            return UINT64_C(-1); /* end of bit stream */
1795
0
        if (unlikely(value > (UINT64_MAX >> 7)))
1796
0
            return UINT64_C(-1); /* integer overflow */
1797
1798
0
        b = *(buf++);
1799
0
        len--;
1800
0
        value = (value << 7) + (b & 0x7f);
1801
0
    }
1802
0
    while (b & 0x80);
1803
1804
0
    *bufp = buf;
1805
0
    *lenp = len;
1806
0
    return value;
1807
0
}
1808
1809
1810
static void MP4_FreeBox_esds( MP4_Box_t *p_box )
1811
0
{
1812
0
    free( p_box->data.p_esds->es_descriptor.psz_URL );
1813
0
    if( p_box->data.p_esds->es_descriptor.p_decConfigDescr )
1814
0
    {
1815
0
        free( p_box->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info );
1816
0
        free( p_box->data.p_esds->es_descriptor.p_decConfigDescr );
1817
0
    }
1818
0
}
1819
1820
static int MP4_ReadBox_esds( stream_t *p_stream, MP4_Box_t *p_box )
1821
0
{
1822
0
#define es_descriptor p_box->data.p_esds->es_descriptor
1823
0
    uint64_t i_len;
1824
0
    unsigned int i_flags;
1825
0
    unsigned int i_type;
1826
1827
0
    MP4_READBOX_ENTER( MP4_Box_data_esds_t, MP4_FreeBox_esds );
1828
1829
0
    MP4_GETVERSIONFLAGS( p_box->data.p_esds );
1830
1831
1832
0
    MP4_GET1BYTE( i_type );
1833
0
    if( i_type == 0x03 ) /* MP4ESDescrTag ISO/IEC 14496-1 */
1834
0
    {
1835
0
        i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1836
0
        if( unlikely(i_len == UINT64_C(-1)) )
1837
0
            MP4_READBOX_EXIT( 0 );
1838
1839
0
#ifdef MP4_VERBOSE
1840
0
        msg_Dbg( p_stream, "found esds MPEG4ESDescr (%"PRIu64" bytes)",
1841
0
                 i_len );
1842
0
#endif
1843
1844
0
        MP4_GET2BYTES( es_descriptor.i_ES_ID );
1845
0
        MP4_GET1BYTE( i_flags );
1846
0
        es_descriptor.b_stream_dependence = ( (i_flags&0x80) != 0);
1847
0
        es_descriptor.b_url = ( (i_flags&0x40) != 0);
1848
0
        es_descriptor.b_OCRstream = ( (i_flags&0x20) != 0);
1849
1850
0
        es_descriptor.i_stream_priority = i_flags&0x1f;
1851
0
        if( es_descriptor.b_stream_dependence )
1852
0
        {
1853
0
            MP4_GET2BYTES( es_descriptor.i_depend_on_ES_ID );
1854
0
        }
1855
0
        if( es_descriptor.b_url && i_read > 0 )
1856
0
        {
1857
0
            uint8_t i_url;
1858
1859
0
            MP4_GET1BYTE( i_url );
1860
0
            if( i_url > i_read )
1861
0
                MP4_READBOX_EXIT( 1 );
1862
0
            es_descriptor.psz_URL = malloc( (unsigned) i_url + 1 );
1863
0
            if( es_descriptor.psz_URL )
1864
0
            {
1865
0
                memcpy( es_descriptor.psz_URL, p_peek, i_url );
1866
0
                es_descriptor.psz_URL[i_url] = 0;
1867
0
            }
1868
0
            p_peek += i_url;
1869
0
            i_read -= i_url;
1870
0
        }
1871
0
        else
1872
0
        {
1873
0
            es_descriptor.psz_URL = NULL;
1874
0
        }
1875
0
        if( es_descriptor.b_OCRstream )
1876
0
        {
1877
0
            MP4_GET2BYTES( es_descriptor.i_OCR_ES_ID );
1878
0
        }
1879
0
        MP4_GET1BYTE( i_type ); /* get next type */
1880
0
    }
1881
1882
0
    if( i_type != 0x04)/* MP4DecConfigDescrTag ISO/IEC 14496-1 8.3.4 */
1883
0
    {
1884
0
         es_descriptor.p_decConfigDescr = NULL;
1885
0
         MP4_READBOX_EXIT( 1 ); /* rest isn't interesting up to now */
1886
0
    }
1887
1888
0
    i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1889
0
    if( unlikely(i_len == UINT64_C(-1)) )
1890
0
        MP4_READBOX_EXIT( 0 );
1891
0
#ifdef MP4_VERBOSE
1892
0
    msg_Dbg( p_stream, "found esds MP4DecConfigDescr (%"PRIu64" bytes)",
1893
0
             i_len );
1894
0
#endif
1895
1896
0
    es_descriptor.p_decConfigDescr =
1897
0
            calloc( 1, sizeof( MP4_descriptor_decoder_config_t ));
1898
0
    if( unlikely( es_descriptor.p_decConfigDescr == NULL ) )
1899
0
        MP4_READBOX_EXIT( 0 );
1900
1901
0
    MP4_GET1BYTE( es_descriptor.p_decConfigDescr->i_objectProfileIndication );
1902
0
    MP4_GET1BYTE( i_flags );
1903
0
    es_descriptor.p_decConfigDescr->i_streamType = i_flags >> 2;
1904
0
    es_descriptor.p_decConfigDescr->b_upStream = ( i_flags >> 1 )&0x01;
1905
0
    MP4_GET3BYTES( es_descriptor.p_decConfigDescr->i_buffer_sizeDB );
1906
0
    MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_max_bitrate );
1907
0
    MP4_GET4BYTES( es_descriptor.p_decConfigDescr->i_avg_bitrate );
1908
0
    MP4_GET1BYTE( i_type );
1909
0
    if( i_type !=  0x05 )/* MP4DecSpecificDescrTag ISO/IEC 14496-1 8.3.5 */
1910
0
    {
1911
0
        es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = 0;
1912
0
        es_descriptor.p_decConfigDescr->p_decoder_specific_info  = NULL;
1913
0
        MP4_READBOX_EXIT( 1 );
1914
0
    }
1915
1916
0
    i_len = MP4_ReadLengthDescriptor( &p_peek, &i_read );
1917
0
    if( unlikely(i_len == UINT64_C(-1)) )
1918
0
        MP4_READBOX_EXIT( 0 );
1919
0
#ifdef MP4_VERBOSE
1920
0
    msg_Dbg( p_stream, "found esds MP4DecSpecificDescr (%"PRIu64" bytes)",
1921
0
             i_len );
1922
0
#endif
1923
0
    if( i_len > i_read )
1924
0
        MP4_READBOX_EXIT( 0 );
1925
1926
0
    es_descriptor.p_decConfigDescr->i_decoder_specific_info_len = i_len;
1927
0
    es_descriptor.p_decConfigDescr->p_decoder_specific_info = malloc( i_len );
1928
0
    if( unlikely( es_descriptor.p_decConfigDescr->p_decoder_specific_info == NULL ) )
1929
0
        MP4_READBOX_EXIT( 0 );
1930
1931
0
    memcpy( es_descriptor.p_decConfigDescr->p_decoder_specific_info,
1932
0
            p_peek, i_len );
1933
1934
0
    MP4_READBOX_EXIT( 1 );
1935
0
#undef es_descriptor
1936
0
}
1937
1938
static void MP4_FreeBox_av1C( MP4_Box_t *p_box )
1939
12
{
1940
12
    MP4_Box_data_av1C_t *p_av1C = p_box->data.p_av1C;
1941
12
    free( p_av1C->p_av1C );
1942
12
}
1943
1944
static int MP4_ReadBox_av1C( stream_t *p_stream, MP4_Box_t *p_box )
1945
12
{
1946
12
    MP4_Box_data_av1C_t *p_av1C;
1947
1948
12
    MP4_READBOX_ENTER( MP4_Box_data_av1C_t, MP4_FreeBox_av1C );
1949
12
    p_av1C = p_box->data.p_av1C;
1950
1951
12
    if( i_read < 4 ||
1952
12
       p_peek[0] != 0x81 ) /* marker / version */
1953
0
        MP4_READBOX_EXIT( 0 );
1954
1955
12
    p_av1C->p_av1C = malloc( i_read );
1956
12
    if( p_av1C->p_av1C )
1957
12
    {
1958
12
        memcpy( p_av1C->p_av1C, p_peek, i_read );
1959
12
        p_av1C->i_av1C = i_read;
1960
12
    }
1961
1962
12
    uint8_t i_8b;
1963
12
    MP4_GET1BYTE( i_8b ); /* marker / version */
1964
1965
12
    MP4_GET1BYTE( i_8b );
1966
12
    p_av1C->i_profile = i_8b >> 5;
1967
12
    p_av1C->i_level = i_8b & 0x1F;
1968
1969
12
    MP4_GET1BYTE( i_8b );
1970
12
    MP4_GET1BYTE( i_8b );
1971
1972
12
    if( i_8b & 0x10 ) /* delay flag */
1973
3
        p_av1C->i_presentation_delay = 1 + (i_8b & 0x0F);
1974
9
    else
1975
9
        p_av1C->i_presentation_delay = 0;
1976
1977
12
    MP4_READBOX_EXIT( 1 );
1978
12
}
1979
1980
static void MP4_FreeBox_avcC( MP4_Box_t *p_box )
1981
0
{
1982
0
    MP4_Box_data_avcC_t *p_avcC = p_box->data.p_avcC;
1983
0
    free( p_avcC->p_avcC );
1984
0
}
1985
1986
static int MP4_ReadBox_avcC( stream_t *p_stream, MP4_Box_t *p_box )
1987
0
{
1988
0
    MP4_Box_data_avcC_t *p_avcC;
1989
1990
0
    MP4_READBOX_ENTER( MP4_Box_data_avcC_t, MP4_FreeBox_avcC );
1991
0
    p_avcC = p_box->data.p_avcC;
1992
1993
0
    if( i_read > 0 )
1994
0
    {
1995
0
        p_avcC->p_avcC = malloc( i_read );
1996
0
        if( p_avcC->p_avcC )
1997
0
        {
1998
0
            memcpy( p_avcC->p_avcC, p_peek, i_read );
1999
0
            p_avcC->i_avcC = i_read;
2000
0
        }
2001
0
    }
2002
2003
0
    MP4_GET1BYTE( p_avcC->i_version );
2004
0
    MP4_GET1BYTE( p_avcC->i_profile );
2005
0
    MP4_GET1BYTE( p_avcC->i_profile_compatibility );
2006
0
    MP4_GET1BYTE( p_avcC->i_level );
2007
0
#ifdef MP4_VERBOSE
2008
0
    msg_Dbg( p_stream,
2009
0
             "read box: \"avcC\" version=%d profile=0x%x level=0x%x",
2010
0
             p_avcC->i_version, p_avcC->i_profile, p_avcC->i_level );
2011
0
#endif
2012
0
    MP4_READBOX_EXIT( 1 );
2013
0
}
2014
2015
static void MP4_FreeBox_vpcC( MP4_Box_t *p_box )
2016
0
{
2017
0
    free( p_box->data.p_vpcC->p_codec_init_data );
2018
0
}
2019
2020
static int MP4_ReadBox_vpcC( stream_t *p_stream, MP4_Box_t *p_box )
2021
0
{
2022
0
    MP4_READBOX_ENTER( MP4_Box_data_vpcC_t, MP4_FreeBox_vpcC );
2023
0
    MP4_Box_data_vpcC_t *p_vpcC = p_box->data.p_vpcC;
2024
2025
0
    if( p_box->i_size < 9 )
2026
0
        MP4_READBOX_EXIT( 0 );
2027
2028
0
    MP4_GET1BYTE( p_vpcC->i_version );
2029
0
    if( p_vpcC->i_version > 1 )
2030
0
        MP4_READBOX_EXIT( 0 );
2031
2032
    /* Skip flags */
2033
0
    MP4_SKIPBYTES( 3 ); // flags
2034
2035
0
    MP4_GET1BYTE( p_vpcC->i_profile );
2036
0
    MP4_GET1BYTE( p_vpcC->i_level );
2037
0
    MP4_GET1BYTE( p_vpcC->i_bit_depth );
2038
2039
    /* Deprecated one
2040
       https://github.com/webmproject/vp9-dash/blob/master/archive/VPCodecISOMediaFileFormatBinding-v0.docx */
2041
0
    if( p_vpcC->i_version == 0 )
2042
0
    {
2043
0
        p_vpcC->i_color_primaries = p_vpcC->i_bit_depth & 0x0F;
2044
0
        p_vpcC->i_bit_depth >>= 4;
2045
0
        MP4_GET1BYTE( p_vpcC->i_chroma_subsampling );
2046
0
        p_vpcC->i_xfer_function = ( p_vpcC->i_chroma_subsampling & 0x0F ) >> 1;
2047
0
        p_vpcC->i_fullrange = p_vpcC->i_chroma_subsampling & 0x01;
2048
0
        p_vpcC->i_chroma_subsampling >>= 4;
2049
0
    }
2050
0
    else
2051
0
    {
2052
0
        p_vpcC->i_chroma_subsampling = ( p_vpcC->i_bit_depth & 0x0F ) >> 1;
2053
0
        p_vpcC->i_fullrange = p_vpcC->i_bit_depth & 0x01;
2054
0
        p_vpcC->i_bit_depth >>= 4;
2055
0
        MP4_GET1BYTE( p_vpcC->i_color_primaries );
2056
0
        MP4_GET1BYTE( p_vpcC->i_xfer_function );
2057
0
        MP4_GET1BYTE( p_vpcC->i_matrix_coeffs );
2058
0
    }
2059
2060
0
    MP4_GET2BYTES( p_vpcC->i_codec_init_datasize );
2061
0
    if( p_vpcC->i_codec_init_datasize > i_read )
2062
0
        p_vpcC->i_codec_init_datasize = i_read;
2063
2064
0
    if( p_vpcC->i_codec_init_datasize )
2065
0
    {
2066
0
        p_vpcC->p_codec_init_data = malloc( i_read );
2067
0
        if( !p_vpcC->p_codec_init_data )
2068
0
            MP4_READBOX_EXIT( 0 );
2069
0
        memcpy( p_vpcC->p_codec_init_data, p_peek, i_read );
2070
0
    }
2071
2072
0
    MP4_READBOX_EXIT( 1 );
2073
0
}
2074
2075
static int MP4_ReadBox_SmDm( stream_t *p_stream, MP4_Box_t *p_box )
2076
0
{
2077
0
    MP4_READBOX_ENTER( MP4_Box_data_SmDm_t, NULL );
2078
0
    MP4_Box_data_SmDm_t *p_SmDm = p_box->data.p_SmDm;
2079
2080
    /* SmDm: version/flags RGB */
2081
    /* mdcv: version/flags GBR or not */
2082
0
    if( p_box->i_type != ATOM_mdcv )
2083
0
    {
2084
0
        uint8_t i_version;
2085
0
        MP4_GET1BYTE( i_version );
2086
0
        MP4_SKIPBYTES( 3 ); // flags
2087
0
        if( i_version != 0 )
2088
0
            MP4_READBOX_EXIT( 0 );
2089
0
    }
2090
2091
0
    const uint8_t RGB2GBR[3] = {2,0,1};
2092
0
    for(int i=0; i<6; i++)
2093
0
    {
2094
0
        int index = (p_box->i_type != ATOM_mdcv) ? RGB2GBR[i/2] + i%2 : i;
2095
0
        MP4_GET2BYTES( p_SmDm->primaries[index] );
2096
2097
        /* convert from fixed point to 0.00002 resolution */
2098
0
        if(p_box->i_type != ATOM_mdcv)
2099
0
            p_SmDm->primaries[index] = 50000 *
2100
0
                    (double)p_SmDm->primaries[index] / (double)(1<<16);
2101
0
    }
2102
0
    for(int i=0; i<2; i++)
2103
0
    {
2104
0
        MP4_GET2BYTES( p_SmDm->white_point[i] );
2105
0
        if(p_box->i_type != ATOM_mdcv)
2106
0
            p_SmDm->white_point[i] = 50000 *
2107
0
                    (double)p_SmDm->white_point[i] / (double)(1<<16);
2108
0
    }
2109
2110
0
    MP4_GET4BYTES( p_SmDm->i_luminanceMax );
2111
0
    MP4_GET4BYTES( p_SmDm->i_luminanceMin );
2112
0
    if(p_box->i_type != ATOM_mdcv)
2113
0
    {
2114
0
        p_SmDm->i_luminanceMax = 10000 *
2115
0
                (double)p_SmDm->i_luminanceMax / (double) (1<<8);
2116
0
        p_SmDm->i_luminanceMin = 10000 *
2117
0
                (double)p_SmDm->i_luminanceMin / (double) (1<<14);
2118
0
    }
2119
2120
0
    MP4_READBOX_EXIT( 1 );
2121
0
}
2122
2123
static int MP4_ReadBox_CoLL( stream_t *p_stream, MP4_Box_t *p_box )
2124
0
{
2125
0
    MP4_READBOX_ENTER( MP4_Box_data_CoLL_t, NULL );
2126
0
    MP4_Box_data_CoLL_t *p_CoLL = p_box->data.p_CoLL;
2127
2128
0
    if( p_box->i_type != ATOM_clli )
2129
0
    {
2130
0
        uint8_t i_version;
2131
0
        MP4_GET1BYTE( i_version );
2132
0
        MP4_SKIPBYTES( 3 ); // flags
2133
0
        if( i_version != 0 )
2134
0
            MP4_READBOX_EXIT( 0 );
2135
0
    }
2136
2137
0
    MP4_GET2BYTES( p_CoLL->i_maxCLL );
2138
0
    MP4_GET2BYTES( p_CoLL->i_maxFALL );
2139
0
    MP4_READBOX_EXIT( 1 );
2140
0
}
2141
2142
static void MP4_FreeBox_WMA2( MP4_Box_t *p_box )
2143
0
{
2144
0
    free( p_box->data.p_WMA2->p_extra );
2145
0
}
2146
2147
static int MP4_ReadBox_WMA2( stream_t *p_stream, MP4_Box_t *p_box )
2148
0
{
2149
0
    MP4_READBOX_ENTER( MP4_Box_data_WMA2_t, MP4_FreeBox_WMA2 );
2150
2151
0
    MP4_Box_data_WMA2_t *p_WMA2 = p_box->data.p_WMA2;
2152
2153
0
    MP4_GET2BYTESLE( p_WMA2->Format.wFormatTag );
2154
0
    MP4_GET2BYTESLE( p_WMA2->Format.nChannels );
2155
0
    MP4_GET4BYTESLE( p_WMA2->Format.nSamplesPerSec );
2156
0
    MP4_GET4BYTESLE( p_WMA2->Format.nAvgBytesPerSec );
2157
0
    MP4_GET2BYTESLE( p_WMA2->Format.nBlockAlign );
2158
0
    MP4_GET2BYTESLE( p_WMA2->Format.wBitsPerSample );
2159
2160
0
    uint16_t i_cbSize;
2161
0
    MP4_GET2BYTESLE( i_cbSize );
2162
2163
0
    if( i_cbSize > i_read )
2164
0
        goto error;
2165
2166
0
    p_WMA2->i_extra = i_cbSize;
2167
0
    if ( p_WMA2->i_extra )
2168
0
    {
2169
0
        p_WMA2->p_extra = malloc( p_WMA2->i_extra );
2170
0
        if ( ! p_WMA2->p_extra )
2171
0
            goto error;
2172
0
        memcpy( p_WMA2->p_extra, p_peek, p_WMA2->i_extra );
2173
0
    }
2174
2175
0
    MP4_READBOX_EXIT( 1 );
2176
2177
0
error:
2178
0
    MP4_READBOX_EXIT( 0 );
2179
0
}
2180
2181
static void MP4_FreeBox_strf( MP4_Box_t *p_box )
2182
0
{
2183
0
    free( p_box->data.p_strf->p_extra );
2184
0
}
2185
2186
static int MP4_ReadBox_strf( stream_t *p_stream, MP4_Box_t *p_box )
2187
0
{
2188
0
    MP4_READBOX_ENTER( MP4_Box_data_strf_t, MP4_FreeBox_strf );
2189
2190
0
    MP4_Box_data_strf_t *p_strf = p_box->data.p_strf;
2191
2192
0
    if( i_read < 40 )
2193
0
        goto error;
2194
2195
0
    MP4_GET4BYTESLE( p_strf->bmiHeader.biSize );
2196
0
    MP4_GET4BYTESLE( p_strf->bmiHeader.biWidth );
2197
0
    MP4_GET4BYTESLE( p_strf->bmiHeader.biHeight );
2198
0
    MP4_GET2BYTESLE( p_strf->bmiHeader.biPlanes );
2199
0
    MP4_GET2BYTESLE( p_strf->bmiHeader.biBitCount );
2200
0
    MP4_GETFOURCC( p_strf->bmiHeader.biCompression );
2201
0
    MP4_GET4BYTESLE( p_strf->bmiHeader.biSizeImage );
2202
0
    MP4_GET4BYTESLE( p_strf->bmiHeader.biXPelsPerMeter );
2203
0
    MP4_GET4BYTESLE( p_strf->bmiHeader.biYPelsPerMeter );
2204
0
    MP4_GET4BYTESLE( p_strf->bmiHeader.biClrUsed );
2205
0
    MP4_GET4BYTESLE( p_strf->bmiHeader.biClrImportant );
2206
2207
0
    p_strf->i_extra = i_read;
2208
0
    if ( p_strf->i_extra )
2209
0
    {
2210
0
        p_strf->p_extra = malloc( p_strf->i_extra );
2211
0
        if ( ! p_strf->p_extra )
2212
0
            goto error;
2213
0
        memcpy( p_strf->p_extra, p_peek, i_read );
2214
0
    }
2215
2216
0
    MP4_READBOX_EXIT( 1 );
2217
2218
0
error:
2219
0
    MP4_READBOX_EXIT( 0 );
2220
0
}
2221
2222
static int MP4_ReadBox_ASF( stream_t *p_stream, MP4_Box_t *p_box )
2223
0
{
2224
0
    MP4_READBOX_ENTER( MP4_Box_data_ASF_t, NULL );
2225
2226
0
    MP4_Box_data_ASF_t *p_asf = p_box->data.p_asf;
2227
2228
0
    if (i_read != 8)
2229
0
        MP4_READBOX_EXIT( 0 );
2230
2231
0
    MP4_GET1BYTE( p_asf->i_stream_number );
2232
    /* remaining is unknown */
2233
2234
0
    MP4_READBOX_EXIT( 1 );
2235
0
}
2236
2237
static void MP4_FreeBox_sbgp( MP4_Box_t *p_box )
2238
0
{
2239
0
    MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2240
0
    free( p_sbgp->p_entries );
2241
0
}
2242
2243
static int MP4_ReadBox_sbgp( stream_t *p_stream, MP4_Box_t *p_box )
2244
0
{
2245
0
    MP4_READBOX_ENTER( MP4_Box_data_sbgp_t, MP4_FreeBox_sbgp );
2246
0
    MP4_Box_data_sbgp_t *p_sbgp = p_box->data.p_sbgp;
2247
0
    uint32_t i_flags;
2248
2249
0
    if ( i_read < 12 )
2250
0
        MP4_READBOX_EXIT( 0 );
2251
2252
0
    MP4_GET1BYTE( p_sbgp->i_version );
2253
0
    MP4_GET3BYTES( i_flags );
2254
0
    if( i_flags != 0 )
2255
0
        MP4_READBOX_EXIT( 0 );
2256
2257
0
    MP4_GETFOURCC( p_sbgp->i_grouping_type );
2258
2259
0
    if( p_sbgp->i_version == 1 )
2260
0
    {
2261
0
        if( i_read < 8 )
2262
0
            MP4_READBOX_EXIT( 0 );
2263
0
        MP4_GET4BYTES( p_sbgp->i_grouping_type_parameter );
2264
0
    }
2265
2266
0
    MP4_GET4BYTES( p_sbgp->i_entry_count );
2267
0
    if( p_sbgp->i_entry_count > i_read / (4 + 4) )
2268
0
        p_sbgp->i_entry_count = i_read / (4 + 4);
2269
2270
0
    p_sbgp->p_entries = vlc_alloc( p_sbgp->i_entry_count, sizeof(*p_sbgp->p_entries) );
2271
0
    if( !p_sbgp->p_entries )
2272
0
    {
2273
0
        MP4_FreeBox_sbgp( p_box );
2274
0
        MP4_READBOX_EXIT( 0 );
2275
0
    }
2276
2277
0
    for( uint32_t i=0; i<p_sbgp->i_entry_count; i++ )
2278
0
    {
2279
0
        MP4_GET4BYTES( p_sbgp->p_entries[i].i_sample_count );
2280
0
        MP4_GET4BYTES( p_sbgp->p_entries[i].i_group_description_index );
2281
0
    }
2282
2283
0
#ifdef MP4_VERBOSE
2284
0
    msg_Dbg( p_stream,
2285
0
        "read box: \"sbgp\" grouping type %4.4s", (char*) &p_sbgp->i_grouping_type );
2286
 #ifdef MP4_ULTRA_VERBOSE
2287
    for (uint32_t i = 0; i < p_sbgp->i_entry_count; i++)
2288
        msg_Dbg( p_stream, "\t samples %" PRIu32 " group %" PRIu32,
2289
                 p_sbgp->entries[i].i_sample_count,
2290
                 p_sbgp->entries[i].i_group_description_index );
2291
 #endif
2292
0
#endif
2293
2294
0
    MP4_READBOX_EXIT( 1 );
2295
0
}
2296
2297
static void MP4_FreeBox_sgpd( MP4_Box_t *p_box )
2298
0
{
2299
0
    MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2300
0
    free( p_sgpd->p_entries );
2301
0
}
2302
2303
static int MP4_ReadBox_sgpd( stream_t *p_stream, MP4_Box_t *p_box )
2304
0
{
2305
0
    MP4_READBOX_ENTER( MP4_Box_data_sgpd_t, MP4_FreeBox_sgpd );
2306
0
    MP4_Box_data_sgpd_t *p_sgpd = p_box->data.p_sgpd;
2307
0
    uint32_t i_flags;
2308
0
    uint32_t i_default_length = 0;
2309
2310
0
    if ( i_read < 8 )
2311
0
        MP4_READBOX_EXIT( 0 );
2312
2313
0
    MP4_GET1BYTE( p_sgpd->i_version );
2314
0
    MP4_GET3BYTES( i_flags );
2315
0
    if( i_flags != 0 )
2316
0
        MP4_READBOX_EXIT( 0 );
2317
2318
0
    MP4_GETFOURCC( p_sgpd->i_grouping_type );
2319
2320
0
    switch( p_sgpd->i_grouping_type )
2321
0
    {
2322
0
        case SAMPLEGROUP_rap:
2323
0
        case SAMPLEGROUP_roll:
2324
0
            break;
2325
2326
0
        default:
2327
0
#ifdef MP4_VERBOSE
2328
0
    msg_Dbg( p_stream,
2329
0
        "read box: \"sgpd\" grouping type %4.4s (unimplemented)", (char*) &p_sgpd->i_grouping_type );
2330
0
#endif
2331
0
            MP4_READBOX_EXIT( 1 );
2332
0
    }
2333
2334
0
    if( p_sgpd->i_version == 1 )
2335
0
    {
2336
0
        if( i_read < 8 )
2337
0
            MP4_READBOX_EXIT( 0 );
2338
0
        MP4_GET4BYTES( i_default_length );
2339
0
    }
2340
0
    else if( p_sgpd->i_version >= 2 )
2341
0
    {
2342
0
        if( i_read < 8 )
2343
0
            MP4_READBOX_EXIT( 0 );
2344
0
        MP4_GET4BYTES( p_sgpd->i_default_sample_description_index );
2345
0
    }
2346
2347
0
    MP4_GET4BYTES( p_sgpd->i_entry_count );
2348
2349
0
    p_sgpd->p_entries = vlc_alloc( p_sgpd->i_entry_count, sizeof(*p_sgpd->p_entries) );
2350
0
    if( !p_sgpd->p_entries )
2351
0
        MP4_READBOX_EXIT( 0 );
2352
2353
0
    uint32_t i = 0;
2354
0
    for( ; i<p_sgpd->i_entry_count; i++ )
2355
0
    {
2356
0
        uint32_t i_description_length = i_default_length;
2357
0
        if( p_sgpd->i_version == 1 && i_default_length == 0 )
2358
0
        {
2359
0
            if( i_read < 4 )
2360
0
                break;
2361
0
            MP4_GET4BYTES( i_description_length );
2362
0
        }
2363
2364
0
        if( p_sgpd->i_version == 1 && i_read < i_description_length )
2365
0
            break;
2366
2367
0
        switch( p_sgpd->i_grouping_type )
2368
0
        {
2369
0
            case SAMPLEGROUP_rap:
2370
0
                {
2371
0
                    if( i_read < 1 )
2372
0
                    {
2373
0
                        free( p_sgpd->p_entries );
2374
0
                        p_sgpd->i_entry_count = 0;
2375
0
                        p_sgpd->p_entries = NULL;
2376
0
                        MP4_READBOX_EXIT( 0 );
2377
0
                    }
2378
0
                    uint8_t i_data;
2379
0
                    MP4_GET1BYTE( i_data );
2380
0
                    p_sgpd->p_entries[i].rap.i_num_leading_samples_known = i_data & 0x80;
2381
0
                    p_sgpd->p_entries[i].rap.i_num_leading_samples = i_data & 0x7F;
2382
0
                }
2383
0
                break;
2384
2385
0
            case SAMPLEGROUP_roll:
2386
0
                {
2387
0
                    if( i_read < 2 )
2388
0
                    {
2389
0
                        free( p_sgpd->p_entries );
2390
0
                        p_sgpd->i_entry_count = 0;
2391
0
                        p_sgpd->p_entries = NULL;
2392
0
                        MP4_READBOX_EXIT( 0 );
2393
0
                    }
2394
0
                    union
2395
0
                    {
2396
0
                        uint16_t u;
2397
0
                        int16_t  s;
2398
0
                    } readsigned;
2399
0
                    MP4_GET2BYTES( readsigned.u );
2400
0
                    p_sgpd->p_entries[i].roll.i_roll_distance = readsigned.s;
2401
0
                }
2402
0
                break;
2403
2404
0
            default:
2405
0
                vlc_assert_unreachable();
2406
0
                free( p_sgpd->p_entries );
2407
0
                p_sgpd->i_entry_count = 0;
2408
0
                p_sgpd->p_entries = NULL;
2409
0
                MP4_READBOX_EXIT( 0 );
2410
0
        }
2411
0
    }
2412
2413
0
    if( i != p_sgpd->i_entry_count )
2414
0
        p_sgpd->i_entry_count = i;
2415
2416
0
#ifdef MP4_VERBOSE
2417
0
    msg_Dbg( p_stream,
2418
0
        "read box: \"sgpd\" grouping type %4.4s", (char*) &p_sgpd->i_grouping_type );
2419
0
#endif
2420
2421
0
    MP4_READBOX_EXIT( 1 );
2422
0
}
2423
2424
static void MP4_FreeBox_stsdext_chan( MP4_Box_t *p_box )
2425
0
{
2426
0
    MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2427
0
    CoreAudio_Layout_Clean( &p_chan->layout );
2428
0
}
2429
2430
static int MP4_ReadBox_stsdext_chan( stream_t *p_stream, MP4_Box_t *p_box )
2431
0
{
2432
0
    MP4_READBOX_ENTER( MP4_Box_data_chan_t, MP4_FreeBox_stsdext_chan );
2433
0
    MP4_Box_data_chan_t *p_chan = p_box->data.p_chan;
2434
2435
0
    if ( i_read < 16 )
2436
0
        MP4_READBOX_EXIT( 0 );
2437
2438
0
    MP4_GET1BYTE( p_chan->i_version );
2439
0
    MP4_GET3BYTES( p_chan->i_channels_flags );
2440
0
    MP4_GET4BYTES( p_chan->layout.i_channels_layout_tag );
2441
0
    MP4_GET4BYTES( p_chan->layout.i_channels_bitmap );
2442
0
    MP4_GET4BYTES( p_chan->layout.i_channels_description_count );
2443
2444
0
    size_t i_descsize = 8 + 3 * sizeof(float);
2445
0
    if ( i_read < p_chan->layout.i_channels_description_count * i_descsize )
2446
0
        MP4_READBOX_EXIT( 0 );
2447
2448
0
    p_chan->layout.p_descriptions =
2449
0
        vlc_alloc( p_chan->layout.i_channels_description_count, i_descsize );
2450
2451
0
    if ( !p_chan->layout.p_descriptions )
2452
0
        MP4_READBOX_EXIT( 0 );
2453
2454
0
    uint32_t i;
2455
0
    for( i=0; i<p_chan->layout.i_channels_description_count; i++ )
2456
0
    {
2457
0
        if ( i_read < 20 )
2458
0
            break;
2459
0
        MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_label );
2460
0
        MP4_GET4BYTES( p_chan->layout.p_descriptions[i].i_channel_flags );
2461
0
        MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[0] );
2462
0
        MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[1] );
2463
0
        MP4_GET4BYTES( p_chan->layout.p_descriptions[i].f_coordinates[2] );
2464
0
    }
2465
0
    if ( i<p_chan->layout.i_channels_description_count )
2466
0
        p_chan->layout.i_channels_description_count = i;
2467
2468
0
#ifdef MP4_VERBOSE
2469
0
    msg_Dbg( p_stream,
2470
0
             "read box: \"chan\" flags=0x%x tag=0x%x bitmap=0x%x descriptions=%u",
2471
0
             p_chan->i_channels_flags, p_chan->layout.i_channels_layout_tag,
2472
0
             p_chan->layout.i_channels_bitmap, p_chan->layout.i_channels_description_count );
2473
0
#endif
2474
0
    MP4_READBOX_EXIT( 1 );
2475
0
}
2476
2477
static int MP4_ReadBox_stsdext_srat( stream_t *p_stream, MP4_Box_t *p_box )
2478
0
{
2479
0
    MP4_READBOX_ENTER( MP4_Box_data_srat_t, NULL );
2480
0
    MP4_Box_data_srat_t *p_srat = p_box->data.p_srat;
2481
0
    if ( i_read != 8 )
2482
0
        MP4_READBOX_EXIT( 0 );
2483
2484
0
    MP4_SKIPBYTES( 4 ); /* version flags */
2485
0
    MP4_GET4BYTES( p_srat->i_sample_rate );
2486
2487
0
    MP4_READBOX_EXIT( 1 );
2488
0
}
2489
2490
static int MP4_ReadBox_dec3( stream_t *p_stream, MP4_Box_t *p_box )
2491
0
{
2492
0
    MP4_READBOX_ENTER( MP4_Box_data_dec3_t, NULL );
2493
2494
0
    MP4_Box_data_dec3_t *p_dec3 = p_box->data.p_dec3;
2495
2496
0
    unsigned i_header;
2497
0
    MP4_GET2BYTES( i_header );
2498
2499
0
    p_dec3->i_data_rate = i_header >> 3;
2500
0
    p_dec3->i_num_ind_sub = (i_header & 0x7) + 1;
2501
0
    for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++) {
2502
0
        MP4_GET3BYTES( i_header );
2503
0
        p_dec3->stream[i].i_fscod = ( i_header >> 22 ) & 0x03;
2504
0
        p_dec3->stream[i].i_bsid  = ( i_header >> 17 ) & 0x01f;
2505
0
        p_dec3->stream[i].i_bsmod = ( i_header >> 12 ) & 0x01f;
2506
0
        p_dec3->stream[i].i_acmod = ( i_header >> 9 ) & 0x07;
2507
0
        p_dec3->stream[i].i_lfeon = ( i_header >> 8 ) & 0x01;
2508
0
        p_dec3->stream[i].i_num_dep_sub = (i_header >> 1) & 0x0f;
2509
0
        if (p_dec3->stream[i].i_num_dep_sub) {
2510
0
            MP4_GET1BYTE( p_dec3->stream[i].i_chan_loc );
2511
0
            p_dec3->stream[i].i_chan_loc |= (i_header & 1) << 8;
2512
0
        } else
2513
0
            p_dec3->stream[i].i_chan_loc = 0;
2514
0
    }
2515
2516
0
#ifdef MP4_VERBOSE
2517
0
    msg_Dbg( p_stream,
2518
0
        "read box: \"dec3\" bitrate %dkbps %d independent substreams",
2519
0
            p_dec3->i_data_rate, p_dec3->i_num_ind_sub);
2520
2521
0
    for (uint8_t i = 0; i < p_dec3->i_num_ind_sub; i++)
2522
0
        msg_Dbg( p_stream,
2523
0
                "\tstream %d: bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x "
2524
0
                "num dependent subs=%d chan_loc=0x%x",
2525
0
                i, p_dec3->stream[i].i_bsid, p_dec3->stream[i].i_bsmod, p_dec3->stream[i].i_acmod,
2526
0
                p_dec3->stream[i].i_lfeon, p_dec3->stream[i].i_num_dep_sub, p_dec3->stream[i].i_chan_loc );
2527
0
#endif
2528
0
    MP4_READBOX_EXIT( 1 );
2529
0
}
2530
2531
static int MP4_ReadBox_dac3( stream_t *p_stream, MP4_Box_t *p_box )
2532
0
{
2533
0
    MP4_Box_data_dac3_t *p_dac3;
2534
0
    MP4_READBOX_ENTER( MP4_Box_data_dac3_t, NULL );
2535
2536
0
    p_dac3 = p_box->data.p_dac3;
2537
2538
0
    unsigned i_header;
2539
0
    MP4_GET3BYTES( i_header );
2540
2541
0
    p_dac3->i_fscod = ( i_header >> 22 ) & 0x03;
2542
0
    p_dac3->i_bsid  = ( i_header >> 17 ) & 0x01f;
2543
0
    p_dac3->i_bsmod = ( i_header >> 14 ) & 0x07;
2544
0
    p_dac3->i_acmod = ( i_header >> 11 ) & 0x07;
2545
0
    p_dac3->i_lfeon = ( i_header >> 10 ) & 0x01;
2546
0
    p_dac3->i_bitrate_code = ( i_header >> 5) & 0x1f;
2547
2548
0
#ifdef MP4_VERBOSE
2549
0
    msg_Dbg( p_stream,
2550
0
             "read box: \"dac3\" fscod=0x%x bsid=0x%x bsmod=0x%x acmod=0x%x lfeon=0x%x bitrate_code=0x%x",
2551
0
             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 );
2552
0
#endif
2553
0
    MP4_READBOX_EXIT( 1 );
2554
0
}
2555
2556
static void MP4_FreeBox_dvc1( MP4_Box_t *p_box )
2557
0
{
2558
0
    free( p_box->data.p_dvc1->p_vc1 );
2559
0
}
2560
2561
static int MP4_ReadBox_dvc1( stream_t *p_stream, MP4_Box_t *p_box )
2562
0
{
2563
0
    MP4_READBOX_ENTER( MP4_Box_data_dvc1_t, MP4_FreeBox_dvc1 );
2564
0
    if( i_read < 7 )
2565
0
        MP4_READBOX_EXIT( 0 );
2566
2567
0
    MP4_Box_data_dvc1_t *p_dvc1 = p_box->data.p_dvc1;
2568
0
    MP4_GET1BYTE( p_dvc1->i_profile_level );
2569
0
    p_dvc1->i_vc1 = i_read; /* Header + profile_level */
2570
0
    if( p_dvc1->i_vc1 > 0 && (p_dvc1->p_vc1 = malloc( p_dvc1->i_vc1 )) )
2571
0
        memcpy( p_dvc1->p_vc1, p_peek, i_read );
2572
2573
0
#ifdef MP4_VERBOSE
2574
0
    uint8_t i_profile = (p_dvc1->i_profile_level & 0xf0) >> 4;
2575
0
    msg_Dbg( p_stream, "read box: \"dvc1\" profile=%"PRIu8, i_profile );
2576
0
#endif
2577
2578
0
    MP4_READBOX_EXIT( 1 );
2579
0
}
2580
2581
static int MP4_ReadBox_fiel( stream_t *p_stream, MP4_Box_t *p_box )
2582
0
{
2583
0
    MP4_Box_data_fiel_t *p_fiel;
2584
0
    MP4_READBOX_ENTER( MP4_Box_data_fiel_t, NULL );
2585
0
    p_fiel = p_box->data.p_fiel;
2586
0
    if(i_read < 2)
2587
0
        MP4_READBOX_EXIT( 0 );
2588
0
    if(p_peek[0] == 2) /* Interlaced */
2589
0
    {
2590
        /*
2591
         * 0 – There is only one field.
2592
         * 1 – T is displayed earliest, T is stored first in the file.
2593
         * 6 – B is displayed earliest, B is stored first in the file.
2594
         * 9 – B is displayed earliest, T is stored first in the file.
2595
         * 14 – T is displayed earliest, B is stored first in the file.
2596
        */
2597
0
        if(p_peek[1] == 0)
2598
0
            p_fiel->i_flags = BLOCK_FLAG_SINGLE_FIELD;
2599
0
        else if(p_peek[1] == 1 || p_peek[1] == 9)
2600
0
            p_fiel->i_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
2601
0
        else if(p_peek[1] == 6 || p_peek[1] == 14)
2602
0
            p_fiel->i_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
2603
0
    }
2604
0
    MP4_READBOX_EXIT( 1 );
2605
0
}
2606
2607
static int MP4_ReadBox_enda( stream_t *p_stream, MP4_Box_t *p_box )
2608
0
{
2609
0
    MP4_Box_data_enda_t *p_enda;
2610
0
    MP4_READBOX_ENTER( MP4_Box_data_enda_t, NULL );
2611
2612
0
    p_enda = p_box->data.p_enda;
2613
2614
0
    MP4_GET2BYTES( p_enda->i_little_endian );
2615
2616
0
#ifdef MP4_VERBOSE
2617
0
    msg_Dbg( p_stream,
2618
0
             "read box: \"enda\" little_endian=%d", p_enda->i_little_endian );
2619
0
#endif
2620
0
    MP4_READBOX_EXIT( 1 );
2621
0
}
2622
2623
static int MP4_ReadBox_pcmC( stream_t *p_stream, MP4_Box_t *p_box )
2624
0
{
2625
0
    MP4_READBOX_ENTER( MP4_Box_data_pcmC_t, NULL );
2626
0
    if(i_read != 6)
2627
0
        MP4_READBOX_EXIT( 0 );
2628
0
    uint32_t temp;
2629
0
    MP4_GET4BYTES(temp);
2630
0
    if(temp != 0) /* support only v0 */
2631
0
        MP4_READBOX_EXIT( 0 );
2632
0
    MP4_GET1BYTE(p_box->data.p_pcmC->i_format_flags);
2633
0
    MP4_GET1BYTE(p_box->data.p_pcmC->i_sample_size);
2634
0
    MP4_READBOX_EXIT( 1 );
2635
0
}
2636
2637
static void MP4_FreeBox_sample_soun( MP4_Box_t *p_box )
2638
1
{
2639
1
    free( p_box->data.p_sample_soun->p_qt_description );
2640
1
}
2641
2642
static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
2643
1
{
2644
1
    p_box->i_handler = ATOM_soun;
2645
1
    MP4_READBOX_ENTER( MP4_Box_data_sample_soun_t, MP4_FreeBox_sample_soun );
2646
1
    p_box->data.p_sample_soun->p_qt_description = NULL;
2647
2648
1
    size_t i_actually_read = i_read + header_size;
2649
2650
    /* Sanity check needed because the "wave" box does also contain an
2651
     * "mp4a" box that we don't understand. */
2652
1
    if( i_read < 28 )
2653
0
    {
2654
0
        MP4_READBOX_EXIT( 1 );
2655
0
    }
2656
2657
1
    READ_SAMPLE_DESC_COMMON_8BYTES_HEADER;
2658
2659
    /*
2660
     * XXX hack -> produce a copy of the nearly complete chunk
2661
     */
2662
1
    p_box->data.p_sample_soun->i_qt_description = 0;
2663
1
    p_box->data.p_sample_soun->p_qt_description = NULL;
2664
1
    if( i_read > 0 )
2665
1
    {
2666
1
        p_box->data.p_sample_soun->p_qt_description = malloc( i_read );
2667
1
        if( p_box->data.p_sample_soun->p_qt_description )
2668
1
        {
2669
1
            p_box->data.p_sample_soun->i_qt_description = i_read;
2670
1
            memcpy( p_box->data.p_sample_soun->p_qt_description, p_peek, i_read );
2671
1
        }
2672
1
    }
2673
2674
1
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_version );
2675
1
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_qt_revision_level );
2676
1
    MP4_GET4BYTES( p_box->data.p_sample_soun->i_qt_vendor );
2677
2678
1
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_channelcount );
2679
1
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_samplesize );
2680
1
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_compressionid );
2681
1
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_reserved3 );
2682
1
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratehi );
2683
1
    MP4_GET2BYTES( p_box->data.p_sample_soun->i_sampleratelo );
2684
2685
1
#ifdef MP4_VERBOSE
2686
1
    msg_Dbg( p_stream,
2687
1
             "read box: \"soun\" stsd qt_version %"PRIu16" compid=%"PRIx16,
2688
1
             p_box->data.p_sample_soun->i_qt_version,
2689
1
             p_box->data.p_sample_soun->i_compressionid );
2690
1
#endif
2691
    /* @36 bytes */
2692
1
    if( p_box->data.p_sample_soun->i_qt_version == 1 && i_read >= 16 )
2693
0
    {
2694
        /* SoundDescriptionV1 */
2695
0
        MP4_GET4BYTES( p_box->data.p_sample_soun->i_sample_per_packet );
2696
0
        MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_packet );
2697
0
        MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_frame );
2698
0
        MP4_GET4BYTES( p_box->data.p_sample_soun->i_bytes_per_sample );
2699
2700
0
#ifdef MP4_VERBOSE
2701
0
        msg_Dbg( p_stream,
2702
0
                 "read box: \"soun\" V1 sample/packet=%d bytes/packet=%d "
2703
0
                 "bytes/frame=%d bytes/sample=%d",
2704
0
                 p_box->data.p_sample_soun->i_sample_per_packet,
2705
0
                 p_box->data.p_sample_soun->i_bytes_per_packet,
2706
0
                 p_box->data.p_sample_soun->i_bytes_per_frame,
2707
0
                 p_box->data.p_sample_soun->i_bytes_per_sample );
2708
0
#endif
2709
        /* @52 bytes */
2710
0
    }
2711
1
    else if( p_box->data.p_sample_soun->i_qt_version == 2 && i_read >= 36 )
2712
0
    {
2713
        /* SoundDescriptionV2 */
2714
0
        double f_sample_rate;
2715
0
        int64_t i_dummy64;
2716
0
        uint32_t i_channel, i_extoffset, i_dummy32;
2717
2718
        /* Checks */
2719
0
        if ( p_box->data.p_sample_soun->i_channelcount != 0x3  ||
2720
0
             p_box->data.p_sample_soun->i_samplesize != 0x0010 ||
2721
0
             p_box->data.p_sample_soun->i_compressionid != 0xFFFE ||
2722
0
             p_box->data.p_sample_soun->i_reserved3 != 0x0     ||
2723
0
             p_box->data.p_sample_soun->i_sampleratehi != 0x1  ||//65536
2724
0
             p_box->data.p_sample_soun->i_sampleratelo != 0x0 )  //remainder
2725
0
        {
2726
0
            msg_Err( p_stream, "invalid stsd V2 box defaults" );
2727
0
            MP4_READBOX_EXIT( 0 );
2728
0
        }
2729
        /* !Checks */
2730
2731
0
        MP4_GET4BYTES( i_extoffset ); /* offset to stsd extensions */
2732
0
        MP4_GET8BYTES( i_dummy64 );
2733
0
        memcpy( &f_sample_rate, &i_dummy64, 8 );
2734
0
        msg_Dbg( p_stream, "read box: %f Hz", f_sample_rate );
2735
        /* Rounding error with lo, but we don't care since we do not support fractional audio rate */
2736
0
        p_box->data.p_sample_soun->i_sampleratehi = (uint16_t)f_sample_rate;
2737
0
        p_box->data.p_sample_soun->i_sampleratelo = (f_sample_rate - p_box->data.p_sample_soun->i_sampleratehi);
2738
2739
0
        MP4_GET4BYTES( i_channel );
2740
0
        p_box->data.p_sample_soun->i_channelcount = i_channel;
2741
2742
0
        MP4_GET4BYTES( i_dummy32 );
2743
0
        if ( i_dummy32 != 0x7F000000 )
2744
0
        {
2745
0
            msg_Err( p_stream, "invalid stsd V2 box" );
2746
0
            MP4_READBOX_EXIT( 0 );
2747
0
        }
2748
2749
0
        MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbitsperchannel );
2750
0
        MP4_GET4BYTES( p_box->data.p_sample_soun->i_formatflags );
2751
0
        MP4_GET4BYTES( p_box->data.p_sample_soun->i_constbytesperaudiopacket );
2752
0
        MP4_GET4BYTES( p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2753
2754
0
#ifdef MP4_VERBOSE
2755
0
        msg_Dbg( p_stream, "read box: \"soun\" V2 rate=%f bitsperchannel=%u "
2756
0
                           "flags=%u bytesperpacket=%u lpcmframesperpacket=%u",
2757
0
                 f_sample_rate,
2758
0
                 p_box->data.p_sample_soun->i_constbitsperchannel,
2759
0
                 p_box->data.p_sample_soun->i_formatflags,
2760
0
                 p_box->data.p_sample_soun->i_constbytesperaudiopacket,
2761
0
                 p_box->data.p_sample_soun->i_constLPCMframesperaudiopacket );
2762
0
#endif
2763
        /* @72 bytes + */
2764
0
        if( i_extoffset > i_actually_read )
2765
0
            i_extoffset = i_actually_read;
2766
0
        p_peek = &p_buff[i_extoffset];
2767
0
        i_read = i_actually_read - i_extoffset;
2768
0
    }
2769
1
    else
2770
1
    {
2771
1
        p_box->data.p_sample_soun->i_sample_per_packet = 0;
2772
1
        p_box->data.p_sample_soun->i_bytes_per_packet = 0;
2773
1
        p_box->data.p_sample_soun->i_bytes_per_frame = 0;
2774
1
        p_box->data.p_sample_soun->i_bytes_per_sample = 0;
2775
2776
1
#ifdef MP4_VERBOSE
2777
1
        msg_Dbg( p_stream, "read box: \"soun\" V0 or qt1/2 (rest=%"PRIu64")",
2778
1
                 i_read );
2779
1
#endif
2780
        /* @36 bytes */
2781
1
    }
2782
2783
1
    if( p_box->i_type == ATOM_drms )
2784
0
    {
2785
0
        msg_Warn( p_stream, "DRM protected streams are not supported." );
2786
0
        MP4_READBOX_EXIT( 0 );
2787
0
    }
2788
2789
1
    if( p_box->i_type == ATOM_samr || p_box->i_type == ATOM_sawb )
2790
0
    {
2791
        /* Ignore channelcount for AMR (3gpp AMRSpecificBox) */
2792
0
        p_box->data.p_sample_soun->i_channelcount = 1;
2793
0
    }
2794
2795
    /* Loads extensions */
2796
1
    MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2797
1
                                  p_box->i_pos + p_peek - p_buff ); /* esds/wave/... */
2798
2799
1
#ifdef MP4_VERBOSE
2800
1
    msg_Dbg( p_stream, "read box: \"soun\" in stsd channel %d "
2801
1
             "sample size %d sample rate %f",
2802
1
             p_box->data.p_sample_soun->i_channelcount,
2803
1
             p_box->data.p_sample_soun->i_samplesize,
2804
1
             (float)p_box->data.p_sample_soun->i_sampleratehi +
2805
1
             (float)p_box->data.p_sample_soun->i_sampleratelo / BLOCK16x16 );
2806
2807
1
#endif
2808
1
    MP4_READBOX_EXIT( 1 );
2809
1
}
2810
2811
static void MP4_FreeBox_sample_vide( MP4_Box_t *p_box )
2812
0
{
2813
0
    free( p_box->data.p_sample_vide->p_qt_image_description );
2814
0
    free( p_box->data.p_sample_vide->p_palette );
2815
0
}
2816
2817
int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
2818
0
{
2819
0
    p_box->i_handler = ATOM_vide;
2820
0
    MP4_READBOX_ENTER( MP4_Box_data_sample_vide_t, MP4_FreeBox_sample_vide );
2821
2822
0
    size_t i_actually_read = i_read + header_size;
2823
2824
0
    READ_SAMPLE_DESC_COMMON_8BYTES_HEADER;
2825
2826
    /*
2827
     * XXX hack -> produce a copy of the nearly complete chunk
2828
     */
2829
0
    if( i_read > 0 )
2830
0
    {
2831
0
        p_box->data.p_sample_vide->p_qt_image_description = malloc( i_read );
2832
0
        if( unlikely( p_box->data.p_sample_vide->p_qt_image_description == NULL ) )
2833
0
            MP4_READBOX_EXIT( 0 );
2834
0
        p_box->data.p_sample_vide->i_qt_image_description = i_read;
2835
0
        memcpy( p_box->data.p_sample_vide->p_qt_image_description,
2836
0
                p_peek, i_read );
2837
0
    }
2838
0
    else
2839
0
    {
2840
0
        p_box->data.p_sample_vide->i_qt_image_description = 0;
2841
0
        p_box->data.p_sample_vide->p_qt_image_description = NULL;
2842
0
    }
2843
2844
0
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_version );
2845
0
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_revision_level );
2846
0
    MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_vendor );
2847
2848
0
    MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_temporal_quality );
2849
0
    MP4_GET4BYTES( p_box->data.p_sample_vide->i_qt_spatial_quality );
2850
2851
0
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_width );
2852
0
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_height );
2853
2854
0
    MP4_GET4BYTES( p_box->data.p_sample_vide->i_horizresolution );
2855
0
    MP4_GET4BYTES( p_box->data.p_sample_vide->i_vertresolution );
2856
2857
0
    MP4_GET4BYTES( p_box->data.p_sample_vide->reserved );
2858
0
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_frame_count );
2859
2860
0
    if ( i_read < 32 )
2861
0
        MP4_READBOX_EXIT( 0 );
2862
0
    if( p_peek[0] <= 31 ) // Must be Pascal String
2863
0
    {
2864
0
        memcpy( &p_box->data.p_sample_vide->sz_compressorname, &p_peek[1], p_peek[0] );
2865
0
        p_box->data.p_sample_vide->sz_compressorname[p_peek[0]] = 0;
2866
0
    }
2867
0
    p_peek += 32; i_read -= 32;
2868
2869
0
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_depth );
2870
0
    MP4_GET2BYTES( p_box->data.p_sample_vide->i_qt_color_table );
2871
2872
0
    if( p_box->data.p_sample_vide->i_depth == 8 &&
2873
0
        p_box->data.p_sample_vide->i_qt_color_table == 0 )
2874
0
    {
2875
0
        p_box->data.p_sample_vide->p_palette = ReadQuicktimePalette( &p_peek, &i_read );
2876
0
        if( p_box->data.p_sample_vide->p_palette == NULL )
2877
0
            MP4_READBOX_EXIT( 0 );
2878
0
    }
2879
2880
0
    if( p_box->i_type == ATOM_drmi )
2881
0
    {
2882
0
        msg_Warn( p_stream, "DRM protected streams are not supported." );
2883
0
        MP4_READBOX_EXIT( 0 );
2884
0
    }
2885
2886
0
    if( i_actually_read > 78 && p_peek - p_buff > 78 )
2887
0
    {
2888
0
        MP4_ReadBoxContainerRawInBox( p_stream, p_box, p_peek, i_read,
2889
0
                                      p_box->i_pos + p_peek - p_buff );
2890
0
    }
2891
2892
0
#ifdef MP4_VERBOSE
2893
0
    msg_Dbg( p_stream, "read box: \"vide\" in stsd %dx%d depth %d (%s)",
2894
0
                      p_box->data.p_sample_vide->i_width,
2895
0
                      p_box->data.p_sample_vide->i_height,
2896
0
                      p_box->data.p_sample_vide->i_depth,
2897
0
                      p_box->data.p_sample_vide->sz_compressorname );
2898
2899
0
#endif
2900
0
    MP4_READBOX_EXIT( 1 );
2901
0
}
2902
2903
static void MP4_FreeBox_sample_generic( MP4_Box_t *p_box )
2904
0
{
2905
0
    free( p_box->data.p_sample_gen->p_data );
2906
0
}
2907
2908
static int MP4_ReadBox_sample_generic( stream_t *p_stream, MP4_Box_t *p_box )
2909
0
{
2910
0
    MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_generic_t, 16, NULL );
2911
2912
0
    READ_SAMPLE_DESC_COMMON_8BYTES_HEADER;
2913
2914
0
    switch( p_box->i_type )
2915
0
    {
2916
0
        case ATOM_mp4s:
2917
0
            p_box->i_handler = ATOM_text;
2918
0
            break;
2919
0
        default:
2920
0
            msg_Warn( p_stream, "Unknown mapping for %4.4s with generic handler",
2921
0
                      (const char *)&p_box->i_type );
2922
0
            break;
2923
0
    }
2924
2925
0
    MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
2926
2927
0
    if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2928
0
        MP4_READBOX_EXIT( 0 );
2929
2930
0
    MP4_READBOX_EXIT( 1 );
2931
0
}
2932
2933
static int MP4_ReadBox_sample_hint8( stream_t *p_stream, MP4_Box_t *p_box )
2934
0
{
2935
0
    MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_sample_generic_t, 24, MP4_FreeBox_sample_generic );
2936
2937
0
    READ_SAMPLE_DESC_COMMON_8BYTES_HEADER;
2938
2939
0
    if( !(p_box->data.p_sample_gen->p_data = malloc(8)) )
2940
0
        MP4_READBOX_EXIT( 0 );
2941
2942
0
    MP4_GET8BYTES( *(p_box->data.p_sample_gen->p_data) );
2943
0
    p_box->data.p_sample_gen->i_data = 8;
2944
2945
0
    MP4_ReadBoxContainerChildren(p_stream, p_box, NULL);
2946
2947
0
    if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
2948
0
        MP4_READBOX_EXIT( 0 );
2949
2950
0
    MP4_READBOX_EXIT( 1 );
2951
0
}
2952
2953
static int MP4_ReadBox_sample_text( stream_t *p_stream, MP4_Box_t *p_box )
2954
0
{
2955
0
    p_box->i_handler = ATOM_text;
2956
0
    MP4_READBOX_ENTER( MP4_Box_data_sample_generic_t, MP4_FreeBox_sample_generic );
2957
2958
0
    READ_SAMPLE_DESC_COMMON_8BYTES_HEADER;
2959
2960
0
    if( i_read )
2961
0
    {
2962
0
        p_box->data.p_sample_gen->p_data = malloc( i_read );
2963
0
        if( !p_box->data.p_sample_gen->p_data )
2964
0
            MP4_READBOX_EXIT( 0 );
2965
0
        memcpy( p_box->data.p_sample_gen->p_data, p_peek, i_read );
2966
0
    }
2967
0
    p_box->data.p_sample_gen->i_data = i_read;
2968
2969
0
#ifdef MP4_VERBOSE
2970
0
    msg_Dbg( p_stream, "read box: \"%4.4s\" in stsd", (const char*) &p_box->i_type );
2971
0
#endif
2972
0
    MP4_READBOX_EXIT( 1 );
2973
0
}
2974
2975
static int MP4_ReadBox_stsd( stream_t *p_stream, MP4_Box_t *p_box )
2976
1
{
2977
1
    MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_lcont_t, 16, NULL );
2978
1
    if( i_read < 8 )
2979
0
        MP4_READBOX_EXIT( 0 );
2980
2981
1
    MP4_GETVERSIONFLAGS( p_box->data.p_lcont );
2982
1
    if( p_box->data.p_lcont->i_version > 1 )
2983
0
        MP4_READBOX_EXIT( 0 );
2984
1
    MP4_GET4BYTES( p_box->data.p_lcont->i_entry_count );
2985
2986
1
    const MP4_Box_t *p_mdia = MP4_BoxGet( p_box, "../../../" );
2987
1
    const MP4_Box_t *p_hdlr;
2988
1
    if( p_mdia == NULL || p_mdia->i_type != ATOM_mdia ||
2989
1
        (p_hdlr = MP4_BoxGet( p_mdia, "hdlr" )) == NULL )
2990
0
    {
2991
0
        if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) == VLC_SUCCESS )
2992
0
        {
2993
0
            msg_Warn( p_stream, "missing hdlr for stsd, delaying" );
2994
0
            MP4_READBOX_EXIT( 1 );
2995
0
        }
2996
0
        MP4_READBOX_EXIT( 0 );
2997
0
    }
2998
2999
    /* Tag stsd with handler type that children can just read from */
3000
1
    p_box->i_handler = BOXDATA(p_hdlr)->i_handler_type;
3001
3002
1
    uint32_t i_entry = 0;
3003
1
    i_read = p_box->i_size - 16;
3004
2
    while (i_read > 8 && i_entry < p_box->data.p_lcont->i_entry_count )
3005
1
    {
3006
1
        int(*pf_read)(stream_t *, MP4_Box_t *);
3007
1
        switch( BOXDATA(p_hdlr)->i_handler_type )
3008
1
        {
3009
1
            case ATOM_soun:
3010
1
                pf_read = MP4_ReadBox_sample_soun;
3011
1
                break;
3012
0
            case ATOM_vide:
3013
0
            case ATOM_pict: /* heif */
3014
0
                pf_read = MP4_ReadBox_sample_vide;
3015
0
                break;
3016
0
            case ATOM_hint:
3017
0
                pf_read = MP4_ReadBox_sample_hint8;
3018
0
                break;
3019
0
            case ATOM_clcp:
3020
0
            case ATOM_text:
3021
0
            case ATOM_subt:
3022
0
            case ATOM_tx3g:
3023
0
            case ATOM_sbtl:
3024
0
                pf_read = MP4_ReadBox_sample_text;
3025
0
                break;
3026
0
            case ATOM_subp: /* see #13464 */
3027
0
                pf_read = MP4_ReadBox_sample_generic;
3028
0
                break;
3029
0
            default:
3030
0
                pf_read = NULL;
3031
0
                msg_Warn( p_stream, "unknown handler type %4.4s in stsd",
3032
0
                          (const char *)& BOXDATA(p_hdlr)->i_handler_type );
3033
0
                break;
3034
1
        }
3035
3036
1
        if( !pf_read )
3037
0
            break;
3038
3039
1
        MP4_Box_t *p_sample = MP4_ReadBoxUsing( p_stream, p_box, pf_read );
3040
1
        if( !p_sample )
3041
0
            break;
3042
3043
        /* write back stsd handler in case of final handler set by child */
3044
1
        p_box->i_handler = p_sample->i_handler;
3045
3046
1
        MP4_BoxAddChild( p_box, p_sample );
3047
1
        i_entry++;
3048
3049
1
        if( i_read < p_sample->i_size )
3050
0
            MP4_READBOX_EXIT( 0 );
3051
3052
1
        i_read -= p_sample->i_size;
3053
1
    }
3054
3055
1
    if (i_entry != p_box->data.p_lcont->i_entry_count)
3056
0
        p_box->data.p_lcont->i_entry_count = i_entry;
3057
3058
1
#ifdef MP4_VERBOSE
3059
1
    msg_Dbg( p_stream, "read box: \"%4.4s\" entry-count %d", (char *)&p_box->i_type,
3060
1
                        p_box->data.p_lcont->i_entry_count );
3061
3062
1
#endif
3063
3064
1
    if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
3065
0
        MP4_READBOX_EXIT( 0 );
3066
3067
1
    MP4_READBOX_EXIT( 1 );
3068
1
}
3069
3070
static void MP4_FreeBox_stsz( MP4_Box_t *p_box )
3071
1
{
3072
1
    free( p_box->data.p_stsz->i_entry_size );
3073
1
}
3074
3075
static int MP4_ReadBox_stsz( stream_t *p_stream, MP4_Box_t *p_box )
3076
1
{
3077
1
    uint32_t count;
3078
3079
1
    MP4_READBOX_ENTER( MP4_Box_data_stsz_t, MP4_FreeBox_stsz );
3080
3081
1
    MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
3082
3083
1
    MP4_GET4BYTES( p_box->data.p_stsz->i_sample_size );
3084
1
    MP4_GET4BYTES( count );
3085
1
    p_box->data.p_stsz->i_sample_count = count;
3086
3087
1
    if( p_box->data.p_stsz->i_sample_size == 0 )
3088
0
    {
3089
0
        if( UINT64_C(4) * count > i_read )
3090
0
            MP4_READBOX_EXIT( 0 );
3091
3092
0
        p_box->data.p_stsz->i_entry_size =
3093
0
            vlc_alloc( count, sizeof(uint32_t) );
3094
0
        if( unlikely( !p_box->data.p_stsz->i_entry_size ) )
3095
0
            MP4_READBOX_EXIT( 0 );
3096
3097
0
        for( uint32_t i = 0; i < count; i++ )
3098
0
        {
3099
0
            MP4_GET4BYTES( p_box->data.p_stsz->i_entry_size[i] );
3100
0
        }
3101
0
    }
3102
1
    else
3103
1
        p_box->data.p_stsz->i_entry_size = NULL;
3104
3105
1
#ifdef MP4_VERBOSE
3106
1
    msg_Dbg( p_stream, "read box: \"stsz\" sample-size %d sample-count %d",
3107
1
                      p_box->data.p_stsz->i_sample_size,
3108
1
                      p_box->data.p_stsz->i_sample_count );
3109
3110
1
#endif
3111
1
    MP4_READBOX_EXIT( 1 );
3112
1
}
3113
3114
static int MP4_ReadBox_stz2( stream_t *p_stream, MP4_Box_t *p_box )
3115
0
{
3116
0
    uint32_t count;
3117
0
    uint8_t field_size;
3118
3119
0
    MP4_READBOX_ENTER( MP4_Box_data_stsz_t, MP4_FreeBox_stsz );
3120
3121
0
    MP4_GETVERSIONFLAGS( p_box->data.p_stsz );
3122
3123
0
    uint32_t reserved;
3124
0
    MP4_GET3BYTES( reserved );
3125
0
    (void) reserved;
3126
3127
0
    MP4_GET1BYTE(field_size);
3128
3129
0
    MP4_GET4BYTES( count );
3130
0
    p_box->data.p_stsz->i_sample_count = count;
3131
3132
0
    if( field_size != 4 && field_size != 8 && field_size != 16 )
3133
0
        MP4_READBOX_EXIT( 0 );
3134
0
    if( ( (uint64_t)field_size * count + 7 ) / 8 > i_read )
3135
0
        MP4_READBOX_EXIT( 0 );
3136
3137
0
    p_box->data.p_stsz->i_entry_size =
3138
0
            vlc_alloc( count, sizeof(uint32_t) );
3139
0
    if( unlikely( p_box->data.p_stsz->i_entry_size == NULL ) )
3140
0
        MP4_READBOX_EXIT( 0 );
3141
3142
0
    if( field_size == 16 )
3143
0
    {
3144
0
        for( uint32_t i = 0; i < count; i++ )
3145
0
            MP4_GET2BYTES( p_box->data.p_stsz->i_entry_size[i] );
3146
0
    }
3147
0
    else if( field_size == 8 )
3148
0
    {
3149
0
        for( uint32_t i = 0; i < count; i++ )
3150
0
            MP4_GET1BYTE( p_box->data.p_stsz->i_entry_size[i] );
3151
0
    }
3152
0
    else
3153
0
    {
3154
0
        vlc_assert( field_size == 4 );
3155
0
        count &= ~1;
3156
0
        for( uint32_t i = 0; i < count; i += 2 )
3157
0
        {
3158
0
            uint8_t entry;
3159
0
            MP4_GET1BYTE( entry );
3160
0
            p_box->data.p_stsz->i_entry_size[i] = entry >> 4;
3161
0
            p_box->data.p_stsz->i_entry_size[i + 1] = entry & 0x0F;
3162
0
        }
3163
0
        if( count < p_box->data.p_stsz->i_sample_count )
3164
0
        {
3165
0
            uint8_t entry;
3166
            /* ISO-14496-12: if the sizes do not fill an integral number of
3167
             * bytes, the last byte is padded with zeros.
3168
             */
3169
0
            MP4_GET1BYTE( entry );
3170
0
            p_box->data.p_stsz->i_entry_size[count] = entry >> 4;
3171
0
        }
3172
0
    }
3173
3174
0
#ifdef MP4_VERBOSE
3175
0
    msg_Dbg( p_stream, "read box: \"stz2\" field-size %d sample-count %d",
3176
0
             field_size,
3177
0
             p_box->data.p_stsz->i_sample_count );
3178
3179
0
#endif
3180
0
    MP4_READBOX_EXIT( 1 );
3181
0
}
3182
3183
static void MP4_FreeBox_stsc( MP4_Box_t *p_box )
3184
1
{
3185
1
    free( p_box->data.p_stsc->i_first_chunk );
3186
1
    free( p_box->data.p_stsc->i_samples_per_chunk );
3187
1
    free( p_box->data.p_stsc->i_sample_description_index );
3188
1
}
3189
3190
static int MP4_ReadBox_stsc( stream_t *p_stream, MP4_Box_t *p_box )
3191
1
{
3192
1
    uint32_t count;
3193
3194
1
    MP4_READBOX_ENTER( MP4_Box_data_stsc_t, MP4_FreeBox_stsc );
3195
3196
1
    MP4_GETVERSIONFLAGS( p_box->data.p_stsc );
3197
1
    MP4_GET4BYTES( count );
3198
3199
1
    if( UINT64_C(12) * count > i_read )
3200
0
        MP4_READBOX_EXIT( 0 );
3201
3202
1
    p_box->data.p_stsc->i_first_chunk = vlc_alloc( count, sizeof(uint32_t) );
3203
1
    p_box->data.p_stsc->i_samples_per_chunk = vlc_alloc( count,
3204
1
                                                         sizeof(uint32_t) );
3205
1
    p_box->data.p_stsc->i_sample_description_index = vlc_alloc( count,
3206
1
                                                            sizeof(uint32_t) );
3207
1
    if( unlikely( p_box->data.p_stsc->i_first_chunk == NULL
3208
1
     || p_box->data.p_stsc->i_samples_per_chunk == NULL
3209
1
     || p_box->data.p_stsc->i_sample_description_index == NULL ) )
3210
0
    {
3211
0
        MP4_READBOX_EXIT( 0 );
3212
0
    }
3213
1
    p_box->data.p_stsc->i_entry_count = count;
3214
3215
3
    for( uint32_t i = 0; i < count;i++ )
3216
2
    {
3217
2
        MP4_GET4BYTES( p_box->data.p_stsc->i_first_chunk[i] );
3218
2
        MP4_GET4BYTES( p_box->data.p_stsc->i_samples_per_chunk[i] );
3219
2
        MP4_GET4BYTES( p_box->data.p_stsc->i_sample_description_index[i] );
3220
2
    }
3221
3222
1
#ifdef MP4_VERBOSE
3223
1
    msg_Dbg( p_stream, "read box: \"stsc\" entry-count %d",
3224
1
                      p_box->data.p_stsc->i_entry_count );
3225
3226
1
#endif
3227
1
    MP4_READBOX_EXIT( 1 );
3228
1
}
3229
3230
static void MP4_FreeBox_sdp( MP4_Box_t *p_box )
3231
0
{
3232
0
    free( p_box->data.p_sdp->psz_text );
3233
0
}
3234
3235
static int MP4_ReadBox_sdp( stream_t *p_stream, MP4_Box_t *p_box )
3236
0
{
3237
0
   MP4_READBOX_ENTER( MP4_Box_data_sdp_t, MP4_FreeBox_sdp );
3238
3239
0
   MP4_GETSTRINGZ( p_box->data.p_sdp->psz_text );
3240
3241
0
   MP4_READBOX_EXIT( 1 );
3242
0
}
3243
3244
static void MP4_FreeBox_rtp( MP4_Box_t *p_box )
3245
0
{
3246
0
    free( p_box->data.p_moviehintinformation_rtp->psz_text );
3247
0
}
3248
3249
static int MP4_ReadBox_rtp( stream_t *p_stream, MP4_Box_t *p_box )
3250
0
{
3251
0
    MP4_READBOX_ENTER( MP4_Box_data_moviehintinformation_rtp_t, MP4_FreeBox_rtp );
3252
3253
0
    MP4_GET4BYTES( p_box->data.p_moviehintinformation_rtp->i_description_format );
3254
3255
0
    MP4_GETSTRINGZ( p_box->data.p_moviehintinformation_rtp->psz_text );
3256
3257
0
    MP4_READBOX_EXIT( 1 );
3258
0
}
3259
3260
static int MP4_ReadBox_tims( stream_t *p_stream, MP4_Box_t *p_box )
3261
0
{
3262
0
    MP4_READBOX_ENTER( MP4_Box_data_tims_t, NULL );
3263
3264
0
    MP4_GET4BYTES( p_box->data.p_tims->i_timescale );
3265
3266
0
    MP4_READBOX_EXIT( 1 );
3267
0
}
3268
3269
static int MP4_ReadBox_tsro( stream_t *p_stream, MP4_Box_t *p_box )
3270
0
{
3271
0
    MP4_READBOX_ENTER( MP4_Box_data_tsro_t, NULL );
3272
3273
0
    MP4_GET4BYTES( p_box->data.p_tsro->i_offset );
3274
3275
0
    MP4_READBOX_EXIT( 1 );
3276
0
}
3277
3278
static int MP4_ReadBox_tssy( stream_t *p_stream, MP4_Box_t *p_box )
3279
0
{
3280
0
    MP4_READBOX_ENTER( MP4_Box_data_tssy_t,  NULL );
3281
3282
0
    MP4_GET1BYTE( p_box->data.p_tssy->i_reserved_timestamp_sync );
3283
3284
0
    MP4_READBOX_EXIT( 1 );
3285
0
}
3286
3287
static void MP4_FreeBox_stco_co64( MP4_Box_t *p_box )
3288
1
{
3289
1
    free( p_box->data.p_co64->i_chunk_offset );
3290
1
}
3291
3292
static int MP4_ReadBox_stco_co64( stream_t *p_stream, MP4_Box_t *p_box )
3293
1
{
3294
1
    const bool sixtyfour = p_box->i_type != ATOM_stco;
3295
1
    uint32_t count;
3296
3297
1
    MP4_READBOX_ENTER( MP4_Box_data_co64_t, MP4_FreeBox_stco_co64 );
3298
3299
1
    MP4_GETVERSIONFLAGS( p_box->data.p_co64 );
3300
1
    MP4_GET4BYTES( count );
3301
3302
1
    if( (sixtyfour ? UINT64_C(8) : UINT64_C(4)) * count > i_read )
3303
0
        MP4_READBOX_EXIT( 0 );
3304
3305
1
    p_box->data.p_co64->i_chunk_offset = vlc_alloc( count, sizeof(uint64_t) );
3306
1
    if( unlikely(p_box->data.p_co64->i_chunk_offset == NULL) )
3307
0
        MP4_READBOX_EXIT( 0 );
3308
1
    p_box->data.p_co64->i_entry_count = count;
3309
3310
28
    for( uint32_t i = 0; i < count; i++ )
3311
27
    {
3312
27
        if( sixtyfour )
3313
27
            MP4_GET8BYTES( p_box->data.p_co64->i_chunk_offset[i] );
3314
27
        else
3315
27
            MP4_GET4BYTES( p_box->data.p_co64->i_chunk_offset[i] );
3316
27
    }
3317
3318
1
#ifdef MP4_VERBOSE
3319
1
    msg_Dbg( p_stream, "read box: \"co64\" entry-count %d",
3320
1
                      p_box->data.p_co64->i_entry_count );
3321
3322
1
#endif
3323
1
    MP4_READBOX_EXIT( 1 );
3324
1
}
3325
3326
static void MP4_FreeBox_stss( MP4_Box_t *p_box )
3327
0
{
3328
0
    free( p_box->data.p_stss->i_sample_number );
3329
0
}
3330
3331
static int MP4_ReadBox_stss( stream_t *p_stream, MP4_Box_t *p_box )
3332
0
{
3333
0
    uint32_t count;
3334
3335
0
    MP4_READBOX_ENTER( MP4_Box_data_stss_t, MP4_FreeBox_stss );
3336
3337
0
    MP4_GETVERSIONFLAGS( p_box->data.p_stss );
3338
0
    MP4_GET4BYTES( count );
3339
3340
0
    if( UINT64_C(4) * count > i_read )
3341
0
        MP4_READBOX_EXIT( 0 );
3342
3343
0
    p_box->data.p_stss->i_sample_number = vlc_alloc( count, sizeof(uint32_t) );
3344
0
    if( unlikely( p_box->data.p_stss->i_sample_number == NULL ) )
3345
0
        MP4_READBOX_EXIT( 0 );
3346
0
    p_box->data.p_stss->i_entry_count = count;
3347
3348
0
    for( uint32_t i = 0; i < count; i++ )
3349
0
    {
3350
0
        MP4_GET4BYTES( p_box->data.p_stss->i_sample_number[i] );
3351
0
    }
3352
3353
0
#ifdef MP4_VERBOSE
3354
0
    msg_Dbg( p_stream, "read box: \"stss\" entry-count %d",
3355
0
                      p_box->data.p_stss->i_entry_count );
3356
3357
0
#endif
3358
0
    MP4_READBOX_EXIT( 1 );
3359
0
}
3360
3361
static void MP4_FreeBox_stsh( MP4_Box_t *p_box )
3362
0
{
3363
0
    free( p_box->data.p_stsh->i_shadowed_sample_number );
3364
0
    free( p_box->data.p_stsh->i_sync_sample_number );
3365
0
}
3366
3367
static int MP4_ReadBox_stsh( stream_t *p_stream, MP4_Box_t *p_box )
3368
0
{
3369
0
    uint32_t count;
3370
3371
0
    MP4_READBOX_ENTER( MP4_Box_data_stsh_t, MP4_FreeBox_stsh );
3372
3373
0
    MP4_GETVERSIONFLAGS( p_box->data.p_stsh );
3374
0
    MP4_GET4BYTES( count );
3375
3376
0
    if( UINT64_C(8) * count > i_read )
3377
0
        MP4_READBOX_EXIT( 0 );
3378
3379
0
    p_box->data.p_stsh->i_shadowed_sample_number = vlc_alloc( count,
3380
0
                                                            sizeof(uint32_t) );
3381
0
    p_box->data.p_stsh->i_sync_sample_number = vlc_alloc( count,
3382
0
                                                          sizeof(uint32_t) );
3383
0
    if( p_box->data.p_stsh->i_shadowed_sample_number == NULL
3384
0
     || p_box->data.p_stsh->i_sync_sample_number == NULL )
3385
0
        MP4_READBOX_EXIT( 0 );
3386
0
    p_box->data.p_stsh->i_entry_count = count;
3387
3388
0
    for( uint32_t i = 0; i < p_box->data.p_stss->i_entry_count; i++ )
3389
0
    {
3390
0
        MP4_GET4BYTES( p_box->data.p_stsh->i_shadowed_sample_number[i] );
3391
0
        MP4_GET4BYTES( p_box->data.p_stsh->i_sync_sample_number[i] );
3392
0
    }
3393
3394
0
#ifdef MP4_VERBOSE
3395
0
    msg_Dbg( p_stream, "read box: \"stsh\" entry-count %d",
3396
0
                      p_box->data.p_stsh->i_entry_count );
3397
0
#endif
3398
0
    MP4_READBOX_EXIT( 1 );
3399
0
}
3400
3401
static void MP4_FreeBox_stdp( MP4_Box_t *p_box )
3402
0
{
3403
0
    free( p_box->data.p_stdp->i_priority );
3404
0
}
3405
3406
static int MP4_ReadBox_stdp( stream_t *p_stream, MP4_Box_t *p_box )
3407
0
{
3408
0
    MP4_READBOX_ENTER( MP4_Box_data_stdp_t, MP4_FreeBox_stdp );
3409
3410
0
    MP4_GETVERSIONFLAGS( p_box->data.p_stdp );
3411
3412
0
    p_box->data.p_stdp->i_priority =
3413
0
        calloc( i_read / 2, sizeof(*p_box->data.p_stdp->i_priority) );
3414
3415
0
    if( unlikely( !p_box->data.p_stdp->i_priority ) )
3416
0
        MP4_READBOX_EXIT( 0 );
3417
3418
0
    for( unsigned i = 0; i < i_read / 2 ; i++ )
3419
0
    {
3420
0
        MP4_GET2BYTES( p_box->data.p_stdp->i_priority[i] );
3421
0
    }
3422
3423
0
#ifdef MP4_VERBOSE
3424
0
    msg_Dbg( p_stream, "read box: \"stdp\" entry-count %"PRId64,
3425
0
                      i_read / 2 );
3426
3427
0
#endif
3428
0
    MP4_READBOX_EXIT( 1 );
3429
0
}
3430
3431
static void MP4_FreeBox_elst( MP4_Box_t *p_box )
3432
1
{
3433
1
    free( p_box->data.p_elst->entries );
3434
1
}
3435
3436
static int MP4_ReadBox_elst( stream_t *p_stream, MP4_Box_t *p_box )
3437
1
{
3438
1
    uint32_t count;
3439
3440
1
    MP4_READBOX_ENTER( MP4_Box_data_elst_t, MP4_FreeBox_elst );
3441
1
    MP4_Box_data_elst_t *p_elst = p_box->data.p_elst;
3442
3443
1
    uint8_t i_version;
3444
1
    MP4_GET1BYTE( i_version );
3445
1
    MP4_SKIPBYTES( 3 );  // flags
3446
3447
1
    if( i_version > 1 )
3448
0
        MP4_READBOX_EXIT( 0 );
3449
3450
1
    MP4_GET4BYTES( count );
3451
1
    if( count == 0 )
3452
1
        MP4_READBOX_EXIT( 1 );
3453
3454
0
    uint32_t i_entries_max = i_read / ((i_version == 1) ? 20 : 12);
3455
0
    if( count > i_entries_max )
3456
0
        count = i_entries_max;
3457
3458
0
    p_elst->entries = vlc_alloc( count, sizeof(*p_elst->entries) );
3459
0
    if( !p_elst->entries )
3460
0
        MP4_READBOX_EXIT( 0 );
3461
3462
0
    p_elst->i_entry_count = count;
3463
3464
0
    for( uint32_t i = 0; i < count; i++ )
3465
0
    {
3466
0
        uint64_t segment_duration;
3467
0
        int64_t media_time;
3468
3469
0
        if( i_version == 1 )
3470
0
        {
3471
0
            union { int64_t s; uint64_t u; } u;
3472
3473
0
            MP4_GET8BYTES( segment_duration );
3474
0
            MP4_GET8BYTES( u.u );
3475
0
            media_time = u.s;
3476
0
        }
3477
0
        else
3478
0
        {
3479
0
            union { int32_t s; uint32_t u; } u;
3480
3481
0
            MP4_GET4BYTES( segment_duration );
3482
0
            MP4_GET4BYTES( u.u );
3483
0
            media_time = u.s;
3484
0
        }
3485
3486
0
        p_elst->entries[i].i_segment_duration = segment_duration;
3487
0
        p_elst->entries[i].i_media_time = media_time;
3488
0
        MP4_GET2BYTES( p_elst->entries[i].i_media_rate_integer );
3489
0
        MP4_GET2BYTES( p_elst->entries[i].i_media_rate_fraction );
3490
0
    }
3491
3492
0
#ifdef MP4_VERBOSE
3493
0
    msg_Dbg( p_stream, "read box: \"elst\" entry-count %" PRIu32,
3494
0
             p_elst->i_entry_count );
3495
0
#endif
3496
0
    MP4_READBOX_EXIT( 1 );
3497
0
}
3498
3499
static void MP4_FreeBox_cprt( MP4_Box_t *p_box )
3500
0
{
3501
0
    free( p_box->data.p_cprt->psz_notice );
3502
0
}
3503
3504
static int MP4_ReadBox_cprt( stream_t *p_stream, MP4_Box_t *p_box )
3505
0
{
3506
0
    uint16_t i_language;
3507
0
    bool b_mac;
3508
3509
0
    MP4_READBOX_ENTER( MP4_Box_data_cprt_t, MP4_FreeBox_cprt );
3510
3511
0
    MP4_GETVERSIONFLAGS( p_box->data.p_cprt );
3512
3513
0
    MP4_GET2BYTES( i_language );
3514
0
    decodeQtLanguageCode( i_language, p_box->data.p_cprt->rgs_language, &b_mac );
3515
3516
0
    MP4_GETSTRINGZ( p_box->data.p_cprt->psz_notice );
3517
3518
0
#ifdef MP4_VERBOSE
3519
0
    msg_Dbg( p_stream, "read box: \"cprt\" language %3.3s notice %s",
3520
0
                      p_box->data.p_cprt->rgs_language,
3521
0
                      p_box->data.p_cprt->psz_notice );
3522
3523
0
#endif
3524
0
    MP4_READBOX_EXIT( 1 );
3525
0
}
3526
3527
static int MP4_ReadBox_dcom( stream_t *p_stream, MP4_Box_t *p_box )
3528
0
{
3529
0
    MP4_READBOX_ENTER( MP4_Box_data_dcom_t, NULL );
3530
3531
0
    MP4_GETFOURCC( p_box->data.p_dcom->i_algorithm );
3532
0
#ifdef MP4_VERBOSE
3533
0
    msg_Dbg( p_stream,
3534
0
             "read box: \"dcom\" compression algorithm : %4.4s",
3535
0
                      (char*)&p_box->data.p_dcom->i_algorithm );
3536
0
#endif
3537
0
    MP4_READBOX_EXIT( 1 );
3538
0
}
3539
3540
static void MP4_FreeBox_cmvd( MP4_Box_t *p_box )
3541
0
{
3542
0
    free( p_box->data.p_cmvd->p_data );
3543
0
}
3544
3545
static int MP4_ReadBox_cmvd( stream_t *p_stream, MP4_Box_t *p_box )
3546
0
{
3547
0
    MP4_READBOX_ENTER( MP4_Box_data_cmvd_t, MP4_FreeBox_cmvd );
3548
3549
0
    MP4_GET4BYTES( p_box->data.p_cmvd->i_uncompressed_size );
3550
3551
0
    p_box->data.p_cmvd->i_compressed_size = i_read;
3552
3553
0
    if( !( p_box->data.p_cmvd->p_data = malloc( i_read ) ) )
3554
0
        MP4_READBOX_EXIT( 0 );
3555
3556
    /* now copy compressed data */
3557
0
    memcpy( p_box->data.p_cmvd->p_data, p_peek,i_read);
3558
3559
0
#ifdef MP4_VERBOSE
3560
0
    msg_Dbg( p_stream, "read box: \"cmvd\" compressed data size %d",
3561
0
                      p_box->data.p_cmvd->i_compressed_size );
3562
0
#endif
3563
3564
0
    MP4_READBOX_EXIT( 1 );
3565
0
}
3566
3567
static int MP4_ReadBox_cmov( stream_t *p_stream, MP4_Box_t *p_box )
3568
0
{
3569
#ifndef HAVE_ZLIB
3570
    (void)p_box;
3571
    msg_Dbg( p_stream, "read box: \"cmov\" zlib unsupported" );
3572
    return 0;
3573
#else
3574
3575
0
    MP4_Box_t *p_dcom;
3576
0
    MP4_Box_t *p_cmvd;
3577
3578
0
    stream_t *p_stream_memory;
3579
0
    z_stream z_data;
3580
0
    uint8_t *p_data;
3581
3582
0
    if( !MP4_ReadBoxContainer( p_stream, p_box ) )
3583
0
    {
3584
0
        return 0;
3585
0
    }
3586
3587
0
    if( ( p_dcom = MP4_BoxGet( p_box, "dcom" ) ) == NULL ||
3588
0
        ( p_cmvd = MP4_BoxGet( p_box, "cmvd" ) ) == NULL ||
3589
0
        p_cmvd->data.p_cmvd->p_data == NULL )
3590
0
    {
3591
0
        msg_Warn( p_stream, "read box: \"cmov\" incomplete" );
3592
0
        return 0;
3593
0
    }
3594
3595
0
    if( p_dcom->data.p_dcom->i_algorithm != ATOM_zlib )
3596
0
    {
3597
0
        msg_Dbg( p_stream, "read box: \"cmov\" compression algorithm : %4.4s "
3598
0
                 "not supported", (char*)&p_dcom->data.p_dcom->i_algorithm );
3599
0
        return 0;
3600
0
    }
3601
3602
    /* decompress data */
3603
    /* allocate a new buffer */
3604
0
    if( !( p_data = malloc( p_cmvd->data.p_cmvd->i_uncompressed_size ) ) )
3605
0
        return 0;
3606
    /* init default structures */
3607
0
    z_data.next_in   = p_cmvd->data.p_cmvd->p_data;
3608
0
    z_data.avail_in  = p_cmvd->data.p_cmvd->i_compressed_size;
3609
0
    z_data.next_out  = p_data;
3610
0
    z_data.avail_out = p_cmvd->data.p_cmvd->i_uncompressed_size;
3611
0
    z_data.zalloc    = (alloc_func)Z_NULL;
3612
0
    z_data.zfree     = (free_func)Z_NULL;
3613
0
    z_data.opaque    = (voidpf)Z_NULL;
3614
3615
    /* init zlib */
3616
0
    if( inflateInit( &z_data ) != Z_OK )
3617
0
    {
3618
0
        msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3619
0
        free( p_data );
3620
0
        return 0;
3621
0
    }
3622
3623
    /* uncompress */
3624
0
    if( inflate( &z_data, Z_FINISH ) != Z_STREAM_END )
3625
0
    {
3626
0
        msg_Err( p_stream, "read box: \"cmov\" error while uncompressing" );
3627
0
        inflateEnd( &z_data );
3628
0
        free( p_data );
3629
0
        return 0;
3630
0
    }
3631
3632
0
    if( p_cmvd->data.p_cmvd->i_uncompressed_size != z_data.total_out )
3633
0
    {
3634
0
        msg_Warn( p_stream, "read box: \"cmov\" uncompressing data size "
3635
0
                  "mismatch" );
3636
0
    }
3637
3638
    /* close zlib */
3639
0
    if( inflateEnd( &z_data ) != Z_OK )
3640
0
    {
3641
0
        msg_Warn( p_stream, "read box: \"cmov\" error while uncompressing "
3642
0
                  "data (ignored)" );
3643
0
    }
3644
3645
0
    msg_Dbg( p_stream, "read box: \"cmov\" box successfully uncompressed" );
3646
3647
    /* now create a memory stream */
3648
0
    p_stream_memory = vlc_stream_MemoryNew( VLC_OBJECT(p_stream),
3649
0
                                            p_data, z_data.total_out, false );
3650
0
    if( !p_stream_memory )
3651
0
    {
3652
0
        free( p_data );
3653
0
        return 0;
3654
0
    }
3655
3656
    /* and read uncompressd moov */
3657
0
    MP4_Box_t *p_moov =  MP4_ReadBox( p_stream_memory, NULL );
3658
3659
0
    vlc_stream_Delete( p_stream_memory );
3660
3661
0
    if( p_moov )
3662
0
        MP4_BoxAddChild( p_box, p_moov );
3663
3664
0
#ifdef MP4_VERBOSE
3665
0
    msg_Dbg( p_stream, "read box: \"cmov\" compressed movie header completed");
3666
0
#endif
3667
3668
0
    return p_moov ? 1 : 0;
3669
0
#endif /* HAVE_ZLIB */
3670
0
}
3671
3672
static void MP4_FreeBox_rdrf( MP4_Box_t *p_box )
3673
0
{
3674
0
    free( p_box->data.p_rdrf->psz_ref );
3675
0
}
3676
3677
static int MP4_ReadBox_rdrf( stream_t *p_stream, MP4_Box_t *p_box )
3678
0
{
3679
0
    uint32_t i_len;
3680
0
    MP4_READBOX_ENTER( MP4_Box_data_rdrf_t, MP4_FreeBox_rdrf );
3681
3682
0
    MP4_GETVERSIONFLAGS( p_box->data.p_rdrf );
3683
0
    MP4_GETFOURCC( p_box->data.p_rdrf->i_ref_type );
3684
0
    MP4_GET4BYTES( i_len );
3685
3686
0
    if( i_len != 0 )
3687
0
    {
3688
0
        p_box->data.p_rdrf->psz_ref = malloc( i_len + 1 );
3689
0
        if( p_box->data.p_rdrf->psz_ref == NULL )
3690
0
            MP4_READBOX_EXIT( 0 );
3691
3692
0
        MP4_COPY_BYTES( p_box->data.p_rdrf->psz_ref, i_len );
3693
0
        p_box->data.p_rdrf->psz_ref[i_len] = '\0';
3694
0
    }
3695
0
    else
3696
0
    {
3697
0
        p_box->data.p_rdrf->psz_ref = NULL;
3698
0
    }
3699
3700
0
#ifdef MP4_VERBOSE
3701
0
    msg_Dbg( p_stream,
3702
0
            "read box: \"rdrf\" type:%4.4s ref %s",
3703
0
            (char*)&p_box->data.p_rdrf->i_ref_type,
3704
0
            p_box->data.p_rdrf->psz_ref );
3705
0
#endif
3706
0
    MP4_READBOX_EXIT( 1 );
3707
0
}
3708
3709
3710
3711
static int MP4_ReadBox_rmdr( stream_t *p_stream, MP4_Box_t *p_box )
3712
0
{
3713
0
    MP4_READBOX_ENTER( MP4_Box_data_rmdr_t, NULL );
3714
3715
0
    MP4_GETVERSIONFLAGS( p_box->data.p_rmdr );
3716
3717
0
    MP4_GET4BYTES( p_box->data.p_rmdr->i_rate );
3718
3719
0
#ifdef MP4_VERBOSE
3720
0
    msg_Dbg( p_stream,
3721
0
             "read box: \"rmdr\" rate:%d",
3722
0
             p_box->data.p_rmdr->i_rate );
3723
0
#endif
3724
0
    MP4_READBOX_EXIT( 1 );
3725
0
}
3726
3727
static int MP4_ReadBox_rmqu( stream_t *p_stream, MP4_Box_t *p_box )
3728
0
{
3729
0
    MP4_READBOX_ENTER( MP4_Box_data_rmqu_t, NULL );
3730
3731
0
    MP4_GET4BYTES( p_box->data.p_rmqu->i_quality );
3732
3733
0
#ifdef MP4_VERBOSE
3734
0
    msg_Dbg( p_stream,
3735
0
             "read box: \"rmqu\" quality:%d",
3736
0
             p_box->data.p_rmqu->i_quality );
3737
0
#endif
3738
0
    MP4_READBOX_EXIT( 1 );
3739
0
}
3740
3741
static int MP4_ReadBox_rmvc( stream_t *p_stream, MP4_Box_t *p_box )
3742
0
{
3743
0
    MP4_READBOX_ENTER( MP4_Box_data_rmvc_t, NULL );
3744
0
    MP4_GETVERSIONFLAGS( p_box->data.p_rmvc );
3745
3746
0
    MP4_GETFOURCC( p_box->data.p_rmvc->i_gestaltType );
3747
0
    MP4_GET4BYTES( p_box->data.p_rmvc->i_val1 );
3748
0
    MP4_GET4BYTES( p_box->data.p_rmvc->i_val2 );
3749
0
    MP4_GET2BYTES( p_box->data.p_rmvc->i_checkType );
3750
3751
0
#ifdef MP4_VERBOSE
3752
0
    msg_Dbg( p_stream,
3753
0
             "read box: \"rmvc\" gestaltType:%4.4s val1:0x%x val2:0x%x checkType:0x%x",
3754
0
             (char*)&p_box->data.p_rmvc->i_gestaltType,
3755
0
             p_box->data.p_rmvc->i_val1,p_box->data.p_rmvc->i_val2,
3756
0
             p_box->data.p_rmvc->i_checkType );
3757
0
#endif
3758
3759
0
    MP4_READBOX_EXIT( 1 );
3760
0
}
3761
3762
static int MP4_ReadBox_frma( stream_t *p_stream, MP4_Box_t *p_box )
3763
0
{
3764
0
    MP4_READBOX_ENTER( MP4_Box_data_frma_t, NULL );
3765
3766
0
    MP4_GETFOURCC( p_box->data.p_frma->i_type );
3767
3768
0
#ifdef MP4_VERBOSE
3769
0
    msg_Dbg( p_stream, "read box: \"frma\" i_type:%4.4s",
3770
0
             (char *)&p_box->data.p_frma->i_type );
3771
0
#endif
3772
3773
0
    MP4_READBOX_EXIT( 1 );
3774
0
}
3775
3776
static int MP4_ReadBox_skcr( stream_t *p_stream, MP4_Box_t *p_box )
3777
0
{
3778
0
    MP4_READBOX_ENTER( MP4_Box_data_skcr_t, NULL );
3779
3780
0
    MP4_GET4BYTES( p_box->data.p_skcr->i_init );
3781
0
    MP4_GET4BYTES( p_box->data.p_skcr->i_encr );
3782
0
    MP4_GET4BYTES( p_box->data.p_skcr->i_decr );
3783
3784
0
#ifdef MP4_VERBOSE
3785
0
    msg_Dbg( p_stream, "read box: \"skcr\" i_init:%d i_encr:%d i_decr:%d",
3786
0
             p_box->data.p_skcr->i_init,
3787
0
             p_box->data.p_skcr->i_encr,
3788
0
             p_box->data.p_skcr->i_decr );
3789
0
#endif
3790
3791
0
    MP4_READBOX_EXIT( 1 );
3792
0
}
3793
3794
static int MP4_ReadBox_drms( stream_t *p_stream, MP4_Box_t *p_box )
3795
0
{
3796
0
    VLC_UNUSED(p_box);
3797
    /* ATOMs 'user', 'key', 'iviv', and 'priv' will be skipped,
3798
     * so unless data decrypt itself by magic, there will be no playback,
3799
     * but we never know... */
3800
0
    msg_Warn( p_stream, "DRM protected streams are not supported." );
3801
0
    return 1;
3802
0
}
3803
3804
static void MP4_FreeBox_Binary( MP4_Box_t *p_box )
3805
0
{
3806
0
    free( p_box->data.p_binary->p_blob );
3807
0
}
3808
3809
static int MP4_ReadBox_Binary( stream_t *p_stream, MP4_Box_t *p_box )
3810
0
{
3811
0
    MP4_READBOX_ENTER( MP4_Box_data_binary_t, MP4_FreeBox_Binary );
3812
0
    i_read = __MIN( i_read, UINT32_MAX );
3813
0
    if ( i_read > 0 )
3814
0
    {
3815
0
        p_box->data.p_binary->p_blob = malloc( i_read );
3816
0
        if ( p_box->data.p_binary->p_blob )
3817
0
        {
3818
0
            memcpy( p_box->data.p_binary->p_blob, p_peek, i_read );
3819
0
            p_box->data.p_binary->i_blob = i_read;
3820
0
        }
3821
0
    }
3822
0
    MP4_READBOX_EXIT( 1 );
3823
0
}
3824
3825
static void MP4_FreeBox_data( MP4_Box_t *p_box )
3826
5
{
3827
5
    free( p_box->data.p_data->p_blob );
3828
5
}
3829
3830
static int MP4_ReadBox_data( stream_t *p_stream, MP4_Box_t *p_box )
3831
5
{
3832
5
    MP4_READBOX_ENTER( MP4_Box_data_data_t, MP4_FreeBox_data );
3833
5
    MP4_Box_data_data_t *p_data = p_box->data.p_data;
3834
3835
5
    if ( i_read < 8 || i_read - 8 > UINT32_MAX )
3836
4
        MP4_READBOX_EXIT( 0 );
3837
3838
1
    uint8_t i_type;
3839
1
    MP4_GET1BYTE( i_type );
3840
1
    if ( i_type != 0 )
3841
0
    {
3842
0
#ifdef MP4_VERBOSE
3843
0
        msg_Dbg( p_stream, "skipping unknown 'data' atom with type %"PRIu8, i_type );
3844
0
#endif
3845
0
        MP4_READBOX_EXIT( 0 );
3846
0
    }
3847
3848
1
    MP4_GET3BYTES( p_data->e_wellknowntype );
3849
1
    MP4_GET2BYTES( p_data->locale.i_country );
3850
1
    MP4_GET2BYTES( p_data->locale.i_language );
3851
1
#ifdef MP4_VERBOSE
3852
1
        msg_Dbg( p_stream, "read 'data' atom: knowntype=%"PRIu32", country=%"PRIu16" lang=%"PRIu16
3853
1
                 ", size %"PRIu64" bytes", p_data->e_wellknowntype,
3854
1
                 p_data->locale.i_country, p_data->locale.i_language, i_read );
3855
1
#endif
3856
1
    p_box->data.p_data->p_blob = malloc( i_read );
3857
1
    if ( !p_box->data.p_data->p_blob )
3858
0
        MP4_READBOX_EXIT( 0 );
3859
3860
1
    p_box->data.p_data->i_blob = i_read;
3861
1
    memcpy( p_box->data.p_data->p_blob, p_peek, i_read);
3862
3863
1
    MP4_READBOX_EXIT( 1 );
3864
1
}
3865
3866
static int MP4_ReadBox_Metadata( stream_t *p_stream, MP4_Box_t *p_box )
3867
0
{
3868
0
    const uint8_t *p_peek;
3869
0
    if ( vlc_stream_Peek( p_stream, &p_peek, 16 ) < 16 )
3870
0
        return 0;
3871
0
    if ( vlc_stream_Read( p_stream, NULL, 8 ) != 8 )
3872
0
        return 0;
3873
0
    const uint32_t stoplist[] = { ATOM_data, 0 };
3874
0
    return MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist );
3875
0
}
3876
3877
/* Chapter support */
3878
static void MP4_FreeBox_chpl( MP4_Box_t *p_box )
3879
0
{
3880
0
    MP4_Box_data_chpl_t *p_chpl = p_box->data.p_chpl;
3881
0
    for( unsigned i = 0; i < p_chpl->i_chapter; i++ )
3882
0
        free( p_chpl->chapter[i].psz_name );
3883
0
}
3884
3885
static int MP4_ReadBox_chpl( stream_t *p_stream, MP4_Box_t *p_box )
3886
0
{
3887
0
    MP4_Box_data_chpl_t *p_chpl;
3888
0
    int i;
3889
0
    MP4_READBOX_ENTER( MP4_Box_data_chpl_t, MP4_FreeBox_chpl );
3890
3891
0
    p_chpl = p_box->data.p_chpl;
3892
3893
0
    MP4_GETVERSIONFLAGS( p_chpl );
3894
3895
0
    if ( i_read < 5 || p_chpl->i_version != 0x1 )
3896
0
        MP4_READBOX_EXIT( 0 );
3897
3898
0
    MP4_SKIPBYTES( 4 );
3899
3900
0
    MP4_GET1BYTE( p_chpl->i_chapter );
3901
3902
0
    for( i = 0; i < p_chpl->i_chapter; i++ )
3903
0
    {
3904
0
        uint64_t i_start;
3905
0
        uint8_t i_len;
3906
0
        int i_copy;
3907
0
        if ( i_read < 9 )
3908
0
            break;
3909
0
        MP4_GET8BYTES( i_start );
3910
0
        MP4_GET1BYTE( i_len );
3911
3912
0
        p_chpl->chapter[i].psz_name = malloc( i_len + 1 );
3913
0
        if( !p_chpl->chapter[i].psz_name )
3914
0
            MP4_READBOX_EXIT( 0 );
3915
3916
0
        i_copy = __MIN( i_len, i_read );
3917
0
        if( i_copy > 0 )
3918
0
            memcpy( p_chpl->chapter[i].psz_name, p_peek, i_copy );
3919
0
        p_chpl->chapter[i].psz_name[i_copy] = '\0';
3920
0
        p_chpl->chapter[i].i_start = i_start;
3921
3922
0
        p_peek += i_copy;
3923
0
        i_read -= i_copy;
3924
0
    }
3925
3926
0
    if ( i != p_chpl->i_chapter )
3927
0
        p_chpl->i_chapter = i;
3928
3929
    /* Bubble sort by increasing start date */
3930
0
    do
3931
0
    {
3932
0
        for( i = 0; i < p_chpl->i_chapter - 1; i++ )
3933
0
        {
3934
0
            if( p_chpl->chapter[i].i_start > p_chpl->chapter[i+1].i_start )
3935
0
            {
3936
0
                char *psz = p_chpl->chapter[i+1].psz_name;
3937
0
                int64_t i64 = p_chpl->chapter[i+1].i_start;
3938
3939
0
                p_chpl->chapter[i+1].psz_name = p_chpl->chapter[i].psz_name;
3940
0
                p_chpl->chapter[i+1].i_start = p_chpl->chapter[i].i_start;
3941
3942
0
                p_chpl->chapter[i].psz_name = psz;
3943
0
                p_chpl->chapter[i].i_start = i64;
3944
3945
0
                i = -1;
3946
0
                break;
3947
0
            }
3948
0
        }
3949
0
    } while( i == -1 );
3950
3951
0
#ifdef MP4_VERBOSE
3952
0
    msg_Dbg( p_stream, "read box: \"chpl\" %d chapters",
3953
0
                       p_chpl->i_chapter );
3954
0
#endif
3955
0
    MP4_READBOX_EXIT( 1 );
3956
0
}
3957
3958
/* GoPro HiLight tags support */
3959
static void MP4_FreeBox_HMMT( MP4_Box_t *p_box )
3960
0
{
3961
0
    free( p_box->data.p_hmmt->pi_chapter_start );
3962
0
}
3963
3964
static int MP4_ReadBox_HMMT( stream_t *p_stream, MP4_Box_t *p_box )
3965
0
{
3966
0
#define MAX_CHAPTER_COUNT 100
3967
3968
0
    MP4_Box_data_HMMT_t *p_hmmt;
3969
0
    MP4_READBOX_ENTER( MP4_Box_data_HMMT_t, MP4_FreeBox_HMMT );
3970
3971
0
    if( i_read < 4 )
3972
0
        MP4_READBOX_EXIT( 0 );
3973
3974
0
    p_hmmt = p_box->data.p_hmmt;
3975
3976
0
    MP4_GET4BYTES( p_hmmt->i_chapter_count );
3977
3978
0
    if( p_hmmt->i_chapter_count <= 0 )
3979
0
    {
3980
0
        p_hmmt->pi_chapter_start = NULL;
3981
0
        MP4_READBOX_EXIT( 1 );
3982
0
    }
3983
3984
0
    if( ( i_read / sizeof(uint32_t) ) < p_hmmt->i_chapter_count )
3985
0
        MP4_READBOX_EXIT( 0 );
3986
3987
    /* Cameras are allowing a maximum of 100 tags */
3988
0
    if( p_hmmt->i_chapter_count > MAX_CHAPTER_COUNT )
3989
0
        p_hmmt->i_chapter_count = MAX_CHAPTER_COUNT;
3990
3991
0
    p_hmmt->pi_chapter_start = vlc_alloc( p_hmmt->i_chapter_count, sizeof(uint32_t) );
3992
0
    if( p_hmmt->pi_chapter_start == NULL )
3993
0
        MP4_READBOX_EXIT( 0 );
3994
3995
0
    for( uint32_t i = 0; i < p_hmmt->i_chapter_count; i++ )
3996
0
    {
3997
0
        MP4_GET4BYTES( p_hmmt->pi_chapter_start[i] );
3998
0
    }
3999
4000
0
#ifdef MP4_VERBOSE
4001
0
    msg_Dbg( p_stream, "read box: \"HMMT\" %d HiLight tags", p_hmmt->i_chapter_count );
4002
0
#endif
4003
4004
0
    MP4_READBOX_EXIT( 1 );
4005
0
}
4006
4007
static void MP4_FreeBox_TrackReference( MP4_Box_t *p_box )
4008
0
{
4009
0
    free( p_box->data.p_track_reference->i_track_ID );
4010
0
}
4011
4012
static int MP4_ReadBox_TrackReference( stream_t *p_stream, MP4_Box_t *p_box )
4013
0
{
4014
0
    uint32_t count;
4015
4016
0
    MP4_READBOX_ENTER( MP4_Box_data_trak_reference_t, MP4_FreeBox_TrackReference );
4017
4018
0
    p_box->data.p_track_reference->i_track_ID = NULL;
4019
0
    count = i_read / sizeof(uint32_t);
4020
0
    p_box->data.p_track_reference->i_entry_count = count;
4021
0
    p_box->data.p_track_reference->i_track_ID = vlc_alloc( count,
4022
0
                                                        sizeof(uint32_t) );
4023
0
    if( p_box->data.p_track_reference->i_track_ID == NULL )
4024
0
        MP4_READBOX_EXIT( 0 );
4025
4026
0
    for( unsigned i = 0; i < count; i++ )
4027
0
    {
4028
0
        MP4_GET4BYTES( p_box->data.p_track_reference->i_track_ID[i] );
4029
0
    }
4030
0
#ifdef MP4_VERBOSE
4031
0
        msg_Dbg( p_stream, "read box: \"chap\" %d references",
4032
0
                 p_box->data.p_track_reference->i_entry_count );
4033
0
#endif
4034
4035
0
    MP4_READBOX_EXIT( 1 );
4036
0
}
4037
4038
static int MP4_ReadBox_tref( stream_t *p_stream, MP4_Box_t *p_box )
4039
0
{
4040
    /* skip header */
4041
0
    size_t i_header = mp4_box_headersize( p_box );
4042
0
    if( vlc_stream_Read( p_stream, NULL, i_header ) != (ssize_t) i_header )
4043
0
        return 0;
4044
    /* read each reference atom with forced handler */
4045
0
    uint64_t i_remain = p_box->i_size - 8;
4046
0
    while ( i_remain > 8 )
4047
0
    {
4048
0
        MP4_Box_t *p_childbox = MP4_ReadBoxUsing( p_stream, p_box,
4049
0
                                                  MP4_ReadBox_TrackReference );
4050
0
        if( !p_childbox || i_remain < p_childbox->i_size )
4051
0
        {
4052
0
            MP4_BoxFree( p_childbox );
4053
0
            break;
4054
0
        }
4055
4056
0
        MP4_BoxAddChild( p_box, p_childbox );
4057
0
        i_remain -= p_childbox->i_size;
4058
0
    }
4059
4060
0
    return MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) ? 0 : 1;
4061
0
}
4062
4063
static void MP4_FreeBox_keys( MP4_Box_t *p_box )
4064
0
{
4065
0
    for( uint32_t i=0; i<p_box->data.p_keys->i_entry_count; i++ )
4066
0
        free( p_box->data.p_keys->p_entries[i].psz_value );
4067
0
    free( p_box->data.p_keys->p_entries );
4068
0
}
4069
4070
static int MP4_ReadBox_keys( stream_t *p_stream, MP4_Box_t *p_box )
4071
0
{
4072
0
    MP4_READBOX_ENTER( MP4_Box_data_keys_t, MP4_FreeBox_keys );
4073
4074
0
    if ( i_read < 8 )
4075
0
        MP4_READBOX_EXIT( 0 );
4076
4077
0
    uint32_t i_count;
4078
0
    MP4_GET4BYTES( i_count ); /* reserved + flags */
4079
0
    if ( i_count != 0 )
4080
0
        MP4_READBOX_EXIT( 0 );
4081
4082
0
    MP4_GET4BYTES( i_count );
4083
0
    p_box->data.p_keys->p_entries = calloc( i_count, sizeof(*p_box->data.p_keys->p_entries) );
4084
0
    if ( !p_box->data.p_keys->p_entries )
4085
0
        MP4_READBOX_EXIT( 0 );
4086
0
    p_box->data.p_keys->i_entry_count = i_count;
4087
4088
0
    uint32_t i=0;
4089
0
    for( ; i < i_count; i++ )
4090
0
    {
4091
0
        if ( i_read < 8 )
4092
0
            break;
4093
0
        uint32_t i_keysize;
4094
0
        MP4_GET4BYTES( i_keysize );
4095
0
        if ( (i_keysize < 8) || (i_keysize - 4 > i_read) )
4096
0
            break;
4097
0
        MP4_GETFOURCC( p_box->data.p_keys->p_entries[i].i_namespace );
4098
0
        i_keysize -= 8;
4099
0
        p_box->data.p_keys->p_entries[i].psz_value = malloc( i_keysize + 1 );
4100
0
        if ( !p_box->data.p_keys->p_entries[i].psz_value )
4101
0
            break;
4102
0
        memcpy( p_box->data.p_keys->p_entries[i].psz_value, p_peek, i_keysize );
4103
0
        p_box->data.p_keys->p_entries[i].psz_value[i_keysize] = 0;
4104
0
        p_peek += i_keysize;
4105
0
        i_read -= i_keysize;
4106
#ifdef MP4_ULTRA_VERBOSE
4107
        msg_Dbg( p_stream, "read box: \"keys\": %u '%s'", i + 1,
4108
                 p_box->data.p_keys->p_entries[i].psz_value );
4109
#endif
4110
0
    }
4111
0
    if ( i < i_count )
4112
0
        p_box->data.p_keys->i_entry_count = i;
4113
4114
0
    MP4_READBOX_EXIT( 1 );
4115
0
}
4116
4117
static int MP4_ReadBox_colr( stream_t *p_stream, MP4_Box_t *p_box )
4118
0
{
4119
0
    MP4_READBOX_ENTER( MP4_Box_data_colr_t, NULL );
4120
0
    MP4_GETFOURCC( p_box->data.p_colr->i_type );
4121
0
    if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'c' ) ||
4122
0
         p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
4123
0
    {
4124
0
        MP4_GET2BYTES( p_box->data.p_colr->nclc.i_primary_idx );
4125
0
        MP4_GET2BYTES( p_box->data.p_colr->nclc.i_transfer_function_idx );
4126
0
        MP4_GET2BYTES( p_box->data.p_colr->nclc.i_matrix_idx );
4127
0
        if ( p_box->data.p_colr->i_type == VLC_FOURCC( 'n', 'c', 'l', 'x' ) )
4128
0
            MP4_GET1BYTE( p_box->data.p_colr->nclc.i_full_range );
4129
0
    }
4130
0
    else
4131
0
    {
4132
0
#ifdef MP4_VERBOSE
4133
0
        msg_Warn( p_stream, "Unhandled colr type: %4.4s", (char*)&p_box->data.p_colr->i_type );
4134
0
#endif
4135
0
    }
4136
0
    MP4_READBOX_EXIT( 1 );
4137
0
}
4138
4139
static int MP4_ReadBox_irot( stream_t *p_stream, MP4_Box_t *p_box )
4140
0
{
4141
0
    MP4_READBOX_ENTER( MP4_Box_data_irot_t, NULL );
4142
0
    MP4_GET1BYTE( p_box->data.p_irot->i_ccw_degrees );
4143
0
    p_box->data.p_irot->i_ccw_degrees &= 0x03;
4144
0
    p_box->data.p_irot->i_ccw_degrees *= 90;
4145
0
    MP4_READBOX_EXIT( 1 );
4146
0
}
4147
4148
static int MP4_ReadBox_dvcC( stream_t *p_stream, MP4_Box_t *p_box )
4149
0
{
4150
0
    MP4_Box_data_dvcC_t *p_dvcC;
4151
0
    uint16_t flags;
4152
0
    MP4_READBOX_ENTER( MP4_Box_data_dvcC_t, NULL );
4153
0
    p_dvcC = p_box->data.p_dvcC;
4154
0
    MP4_GET1BYTE( p_dvcC->i_version_major );
4155
0
    MP4_GET1BYTE( p_dvcC->i_version_minor );
4156
0
    MP4_GET2BYTES( flags );
4157
0
    p_dvcC->i_profile       = (flags >> 9) & 0x7f;  // 7 bits
4158
0
    p_dvcC->i_level         = (flags >> 3) & 0x3f;  // 6 bits
4159
0
    p_dvcC->i_rpu_present   = (flags >> 2) & 0x01;  // 1 bit
4160
0
    p_dvcC->i_el_present    = (flags >> 1) & 0x01;  // 1 bit
4161
0
    p_dvcC->i_bl_present    =  flags       & 0x01;  // 1 bit
4162
    /* TODO: remainder of box, if needed */
4163
0
    MP4_READBOX_EXIT( 1 );
4164
0
}
4165
4166
static int MP4_ReadBox_meta( stream_t *p_stream, MP4_Box_t *p_box )
4167
69
{
4168
69
    const uint8_t *p_peek;
4169
69
    const size_t i_headersize = mp4_box_headersize( p_box );
4170
4171
69
    if( p_box->i_size < 16 || p_box->i_size - i_headersize < 8 )
4172
1
        return 0;
4173
4174
    /* skip over box header */
4175
68
    if( vlc_stream_Read( p_stream, NULL, i_headersize ) != (ssize_t) i_headersize )
4176
0
        return 0;
4177
4178
    /* meta content starts with a 4 byte version/flags value (should be 0) */
4179
68
    if( vlc_stream_Peek( p_stream, &p_peek, 8 ) < 8 )
4180
0
        return 0;
4181
4182
68
    if( !memcmp( p_peek, "\0\0\0", 4 ) ) /* correct header case */
4183
67
    {
4184
67
        if( vlc_stream_Read( p_stream, NULL, 4 ) != 4 )
4185
0
            return 0;
4186
67
    }
4187
1
    else if( memcmp( &p_peek[4], "hdlr", 4 ) ) /* Broken, headerless ones */
4188
1
    {
4189
1
       return 0;
4190
1
    }
4191
4192
    /* load child atoms up to the handler (which should be next anyway) */
4193
67
    const uint32_t stoplist[] = { ATOM_hdlr, 0 };
4194
67
    if ( !MP4_ReadBoxContainerChildren( p_stream, p_box, stoplist ) )
4195
0
        return 0;
4196
4197
    /* Mandatory */
4198
67
    const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box, "hdlr" );
4199
67
    if ( p_hdlr && BOXDATA(p_hdlr) && BOXDATA(p_hdlr)->i_version == 0 )
4200
67
    {
4201
67
        p_box->i_handler = BOXDATA(p_hdlr)->i_handler_type;
4202
67
        switch( p_box->i_handler )
4203
67
        {
4204
67
            case HANDLER_pict:
4205
67
            case HANDLER_mdta:
4206
67
            case HANDLER_mdir:
4207
                /* then it behaves like a container */
4208
67
                return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
4209
0
            default:
4210
                /* skip parsing, will be seen as empty container */
4211
0
                break;
4212
67
        }
4213
67
    }
4214
4215
0
    return 1;
4216
67
}
4217
4218
static int MP4_ReadBox_iods( stream_t *p_stream, MP4_Box_t *p_box )
4219
0
{
4220
0
    MP4_READBOX_ENTER( MP4_Box_data_iods_t, NULL );
4221
0
    MP4_GETVERSIONFLAGS( p_box->data.p_iods );
4222
4223
0
    MP4_SKIPBYTES( 1 ); /* tag */
4224
0
    MP4_SKIPBYTES( 1 ); /* length */
4225
4226
0
    MP4_GET2BYTES( p_box->data.p_iods->i_object_descriptor ); /* 10bits, 6 other bits
4227
                                                              are used for other flags */
4228
0
    MP4_GET1BYTE( p_box->data.p_iods->i_OD_profile_level );
4229
0
    MP4_GET1BYTE( p_box->data.p_iods->i_scene_profile_level );
4230
0
    MP4_GET1BYTE( p_box->data.p_iods->i_audio_profile_level );
4231
0
    MP4_GET1BYTE( p_box->data.p_iods->i_visual_profile_level );
4232
0
    MP4_GET1BYTE( p_box->data.p_iods->i_graphics_profile_level );
4233
4234
0
#ifdef MP4_VERBOSE
4235
0
    msg_Dbg( p_stream,
4236
0
             "read box: \"iods\" objectDescriptorId: %i, OD: %i, scene: %i, audio: %i, "
4237
0
             "visual: %i, graphics: %i",
4238
0
             p_box->data.p_iods->i_object_descriptor >> 6,
4239
0
             p_box->data.p_iods->i_OD_profile_level,
4240
0
             p_box->data.p_iods->i_scene_profile_level,
4241
0
             p_box->data.p_iods->i_audio_profile_level,
4242
0
             p_box->data.p_iods->i_visual_profile_level,
4243
0
             p_box->data.p_iods->i_graphics_profile_level );
4244
0
#endif
4245
4246
0
    MP4_READBOX_EXIT( 1 );
4247
0
}
4248
4249
static int MP4_ReadBox_btrt( stream_t *p_stream, MP4_Box_t *p_box )
4250
0
{
4251
0
    MP4_READBOX_ENTER( MP4_Box_data_btrt_t, NULL );
4252
4253
0
    if(i_read != 12)
4254
0
        MP4_READBOX_EXIT( 0 );
4255
4256
0
    MP4_GET4BYTES( p_box->data.p_btrt->i_buffer_size );
4257
0
    MP4_GET4BYTES( p_box->data.p_btrt->i_max_bitrate );
4258
0
    MP4_GET4BYTES( p_box->data.p_btrt->i_avg_bitrate );
4259
4260
0
    MP4_READBOX_EXIT( 1 );
4261
0
}
4262
4263
static int MP4_ReadBox_pasp( stream_t *p_stream, MP4_Box_t *p_box )
4264
3
{
4265
3
    MP4_READBOX_ENTER( MP4_Box_data_pasp_t, NULL );
4266
4267
3
    MP4_GET4BYTES( p_box->data.p_pasp->i_horizontal_spacing );
4268
3
    MP4_GET4BYTES( p_box->data.p_pasp->i_vertical_spacing );
4269
4270
3
#ifdef MP4_VERBOSE
4271
3
    msg_Dbg( p_stream,
4272
3
             "read box: \"paps\" %dx%d",
4273
3
             p_box->data.p_pasp->i_horizontal_spacing,
4274
3
             p_box->data.p_pasp->i_vertical_spacing);
4275
3
#endif
4276
4277
3
    MP4_READBOX_EXIT( 1 );
4278
3
}
4279
4280
static int MP4_ReadBox_clap( stream_t *p_stream, MP4_Box_t *p_box )
4281
0
{
4282
0
    MP4_READBOX_ENTER( MP4_Box_data_clap_t, NULL );
4283
4284
0
    if ( i_read != 32 )
4285
0
        MP4_READBOX_EXIT( 0 );
4286
4287
0
    MP4_Box_data_clap_t *p_clap = p_box->data.p_clap;
4288
0
    uint32_t num, den;
4289
4290
0
    MP4_GET4BYTES( num ); MP4_GET4BYTES( den );
4291
0
    p_clap->i_width = num / (den ? den : 1);
4292
0
    MP4_GET4BYTES( num ); MP4_GET4BYTES( den );
4293
0
    p_clap->i_height = num / (den ? den : 1);
4294
0
    MP4_GET4BYTES( num ); MP4_GET4BYTES( den );
4295
0
    p_clap->i_x_offset = num / (den ? den : 1);
4296
0
    MP4_GET4BYTES( num ); MP4_GET4BYTES( den );
4297
0
    p_clap->i_y_offset = num / (den ? den : 1);
4298
4299
0
    if( UINT32_MAX - p_clap->i_width < p_clap->i_x_offset ||
4300
0
        UINT32_MAX - p_clap->i_height < p_clap->i_y_offset )
4301
0
        MP4_READBOX_EXIT( 0 );
4302
4303
0
#ifdef MP4_VERBOSE
4304
0
    msg_Dbg( p_stream,
4305
0
             "read box: \"clap\" %"PRIu32"x%"PRIu32"+%"PRIu32"+%"PRIu32,
4306
0
             p_box->data.p_clap->i_width, p_box->data.p_clap->i_height,
4307
0
             p_box->data.p_clap->i_x_offset, p_box->data.p_clap->i_y_offset );
4308
0
#endif
4309
4310
0
    MP4_READBOX_EXIT( 1 );
4311
0
}
4312
4313
static int MP4_ReadBox_mehd( stream_t *p_stream, MP4_Box_t *p_box )
4314
0
{
4315
0
    MP4_READBOX_ENTER( MP4_Box_data_mehd_t, NULL );
4316
4317
0
    MP4_GETVERSIONFLAGS( p_box->data.p_mehd );
4318
0
    if( p_box->data.p_mehd->i_version == 1 )
4319
0
        MP4_GET8BYTES( p_box->data.p_mehd->i_fragment_duration );
4320
0
    else /* version == 0 */
4321
0
        MP4_GET4BYTES( p_box->data.p_mehd->i_fragment_duration );
4322
4323
0
#ifdef MP4_VERBOSE
4324
0
    msg_Dbg( p_stream,
4325
0
             "read box: \"mehd\" frag dur. %"PRIu64"",
4326
0
             p_box->data.p_mehd->i_fragment_duration );
4327
0
#endif
4328
4329
0
    MP4_READBOX_EXIT( 1 );
4330
0
}
4331
4332
static int MP4_ReadBox_trex( stream_t *p_stream, MP4_Box_t *p_box )
4333
0
{
4334
0
    MP4_READBOX_ENTER( MP4_Box_data_trex_t, NULL );
4335
0
    MP4_GETVERSIONFLAGS( p_box->data.p_trex );
4336
4337
0
    MP4_GET4BYTES( p_box->data.p_trex->i_track_ID );
4338
0
    MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_description_index );
4339
0
    MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_duration );
4340
0
    MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_size );
4341
0
    MP4_GET4BYTES( p_box->data.p_trex->i_default_sample_flags );
4342
4343
0
#ifdef MP4_VERBOSE
4344
0
    msg_Dbg( p_stream,
4345
0
             "read box: \"trex\" trackID: %"PRIu32"",
4346
0
             p_box->data.p_trex->i_track_ID );
4347
0
#endif
4348
4349
0
    MP4_READBOX_EXIT( 1 );
4350
0
}
4351
4352
static void MP4_FreeBox_sdtp( MP4_Box_t *p_box )
4353
0
{
4354
0
    free( p_box->data.p_sdtp->p_sample_table );
4355
0
}
4356
4357
static int MP4_ReadBox_sdtp( stream_t *p_stream, MP4_Box_t *p_box )
4358
0
{
4359
0
    uint32_t i_sample_count;
4360
0
    MP4_READBOX_ENTER( MP4_Box_data_sdtp_t, MP4_FreeBox_sdtp );
4361
0
    MP4_Box_data_sdtp_t *p_sdtp = p_box->data.p_sdtp;
4362
0
    MP4_GETVERSIONFLAGS( p_box->data.p_sdtp );
4363
0
    i_sample_count = i_read;
4364
4365
0
    p_sdtp->p_sample_table = malloc( i_sample_count );
4366
0
    if( unlikely(p_sdtp->p_sample_table == NULL) )
4367
0
        MP4_READBOX_EXIT( 0 );
4368
4369
0
    for( uint32_t i = 0; i < i_sample_count; i++ )
4370
0
        MP4_GET1BYTE( p_sdtp->p_sample_table[i] );
4371
4372
0
#ifdef MP4_VERBOSE
4373
0
    msg_Dbg( p_stream, "i_sample_count is %"PRIu32"", i_sample_count );
4374
0
    if ( i_sample_count > 3 )
4375
0
        msg_Dbg( p_stream,
4376
0
             "read box: \"sdtp\" head: %"PRIx8" %"PRIx8" %"PRIx8" %"PRIx8"",
4377
0
                 p_sdtp->p_sample_table[0],
4378
0
                 p_sdtp->p_sample_table[1],
4379
0
                 p_sdtp->p_sample_table[2],
4380
0
                 p_sdtp->p_sample_table[3] );
4381
0
#endif
4382
4383
0
    MP4_READBOX_EXIT( 1 );
4384
0
}
4385
4386
static int MP4_ReadBox_tsel( stream_t *p_stream, MP4_Box_t *p_box )
4387
0
{
4388
0
    MP4_READBOX_ENTER( MP4_Box_data_tsel_t, NULL );
4389
0
    uint32_t i_version;
4390
0
    MP4_GET4BYTES( i_version );
4391
0
    if ( i_version != 0 || i_read < 4 )
4392
0
        MP4_READBOX_EXIT( 0 );
4393
0
    MP4_GET4BYTES( p_box->data.p_tsel->i_switch_group );
4394
    /* ignore list of attributes as es are present before switch */
4395
0
    MP4_READBOX_EXIT( 1 );
4396
0
}
4397
4398
static int MP4_ReadBox_mfro( stream_t *p_stream, MP4_Box_t *p_box )
4399
0
{
4400
0
    MP4_READBOX_ENTER( MP4_Box_data_mfro_t, NULL );
4401
4402
0
    MP4_GETVERSIONFLAGS( p_box->data.p_mfro );
4403
0
    MP4_GET4BYTES( p_box->data.p_mfro->i_size );
4404
4405
0
#ifdef MP4_VERBOSE
4406
0
    msg_Dbg( p_stream,
4407
0
             "read box: \"mfro\" size: %"PRIu32"",
4408
0
             p_box->data.p_mfro->i_size);
4409
0
#endif
4410
4411
0
    MP4_READBOX_EXIT( 1 );
4412
0
}
4413
4414
static void MP4_FreeBox_tfra( MP4_Box_t *p_box )
4415
0
{
4416
0
    free( p_box->data.p_tfra->p_time );
4417
0
    free( p_box->data.p_tfra->p_moof_offset );
4418
0
    free( p_box->data.p_tfra->p_traf_number );
4419
0
    free( p_box->data.p_tfra->p_trun_number );
4420
0
    free( p_box->data.p_tfra->p_sample_number );
4421
0
}
4422
4423
static int MP4_ReadBox_tfra( stream_t *p_stream, MP4_Box_t *p_box )
4424
0
{
4425
0
#define READ_VARIABLE_LENGTH(lengthvar, p_array) switch (lengthvar)\
4426
0
{\
4427
0
    case 0:\
4428
0
        MP4_GET1BYTE( p_array[i] );\
4429
0
        break;\
4430
0
    case 1:\
4431
0
        MP4_GET2BYTES( *((uint16_t *)&p_array[i*2]) );\
4432
0
        break;\
4433
0
    case 2:\
4434
0
        MP4_GET3BYTES( *((uint32_t *)&p_array[i*4]) );\
4435
0
        break;\
4436
0
    case 3:\
4437
0
        MP4_GET4BYTES( *((uint32_t *)&p_array[i*4]) );\
4438
0
        break;\
4439
0
    default:\
4440
0
        goto error;\
4441
0
}
4442
0
#define FIX_VARIABLE_LENGTH(lengthvar) if ( lengthvar == 3 ) lengthvar = 4
4443
4444
0
    uint32_t i_number_of_entries;
4445
0
    MP4_READBOX_ENTER( MP4_Box_data_tfra_t, MP4_FreeBox_tfra );
4446
0
    MP4_Box_data_tfra_t *p_tfra = p_box->data.p_tfra;
4447
0
    MP4_GETVERSIONFLAGS( p_box->data.p_tfra );
4448
0
    if ( p_tfra->i_version > 1 )
4449
0
        MP4_READBOX_EXIT( 0 );
4450
0
    MP4_GET4BYTES( p_tfra->i_track_ID );
4451
0
    uint32_t i_lengths = 0;
4452
0
    MP4_GET4BYTES( i_lengths );
4453
0
    MP4_GET4BYTES( p_tfra->i_number_of_entries );
4454
0
    i_number_of_entries = p_tfra->i_number_of_entries;
4455
0
    p_tfra->i_length_size_of_traf_num = i_lengths >> 4;
4456
0
    p_tfra->i_length_size_of_trun_num = ( i_lengths & 0x0c ) >> 2;
4457
0
    p_tfra->i_length_size_of_sample_num = i_lengths & 0x03;
4458
4459
0
    size_t size;
4460
0
    p_tfra->p_time = calloc( i_number_of_entries, sizeof(*p_tfra->p_time) );
4461
0
    p_tfra->p_moof_offset = calloc( i_number_of_entries, sizeof(*p_tfra->p_moof_offset) );
4462
4463
0
    size = 1 + p_tfra->i_length_size_of_traf_num; /* size in [|1, 4|] */
4464
0
    if ( size == 3 ) size++;
4465
0
    p_tfra->p_traf_number = calloc( i_number_of_entries, size );
4466
0
    size = 1 + p_tfra->i_length_size_of_trun_num;
4467
0
    if ( size == 3 ) size++;
4468
0
    p_tfra->p_trun_number = calloc( i_number_of_entries, size );
4469
0
    size = 1 + p_tfra->i_length_size_of_sample_num;
4470
0
    if ( size == 3 ) size++;
4471
0
    p_tfra->p_sample_number = calloc( i_number_of_entries, size );
4472
4473
0
    if( !p_tfra->p_time || !p_tfra->p_moof_offset || !p_tfra->p_traf_number
4474
0
                        || !p_tfra->p_trun_number || !p_tfra->p_sample_number )
4475
0
        goto error;
4476
4477
0
    unsigned i_fields_length = 3 + p_tfra->i_length_size_of_traf_num
4478
0
            + p_tfra->i_length_size_of_trun_num
4479
0
            + p_tfra->i_length_size_of_sample_num;
4480
4481
0
    uint32_t i;
4482
0
    for( i = 0; i < i_number_of_entries; i++ )
4483
0
    {
4484
4485
0
        if( p_tfra->i_version == 1 )
4486
0
        {
4487
0
            if ( i_read < i_fields_length + 16 )
4488
0
                break;
4489
0
            MP4_GET8BYTES( p_tfra->p_time[i] );
4490
0
            MP4_GET8BYTES( p_tfra->p_moof_offset[i] );
4491
0
        }
4492
0
        else
4493
0
        {
4494
0
            if ( i_read < i_fields_length + 8 )
4495
0
                break;
4496
0
            MP4_GET4BYTES( p_tfra->p_time[i] );
4497
0
            MP4_GET4BYTES( p_tfra->p_moof_offset[i] );
4498
0
        }
4499
4500
0
        READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num, p_tfra->p_traf_number);
4501
0
        READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num, p_tfra->p_trun_number);
4502
0
        READ_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num, p_tfra->p_sample_number);
4503
0
    }
4504
0
    if ( i < i_number_of_entries )
4505
0
        i_number_of_entries = i;
4506
4507
0
    FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_traf_num);
4508
0
    FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_trun_num);
4509
0
    FIX_VARIABLE_LENGTH(p_tfra->i_length_size_of_sample_num);
4510
4511
#ifdef MP4_ULTRA_VERBOSE
4512
    for( i = 0; i < i_number_of_entries; i++ )
4513
    {
4514
        msg_Dbg( p_stream, "tfra[%"PRIu32"] time[%"PRIu32"]: %"PRIu64", "
4515
                            "moof_offset[%"PRIu32"]: %"PRIu64"",
4516
                    p_tfra->i_track_ID,
4517
                    i, p_tfra->p_time[i],
4518
                    i, p_tfra->p_moof_offset[i] );
4519
    }
4520
#endif
4521
0
#ifdef MP4_VERBOSE
4522
0
    msg_Dbg( p_stream, "tfra[%"PRIu32"] %"PRIu32" entries",
4523
0
             p_tfra->i_track_ID, i_number_of_entries );
4524
0
#endif
4525
4526
0
    MP4_READBOX_EXIT( 1 );
4527
0
error:
4528
0
    MP4_READBOX_EXIT( 0 );
4529
4530
0
#undef READ_VARIABLE_LENGTH
4531
0
#undef FIX_VARIABLE_LENGTH
4532
0
}
4533
4534
static int MP4_ReadBox_pnot( stream_t *p_stream, MP4_Box_t *p_box )
4535
0
{
4536
0
    if ( p_box->i_size != 20 )
4537
0
        return 0;
4538
0
    MP4_READBOX_ENTER( MP4_Box_data_pnot_t, NULL );
4539
0
    MP4_GET4BYTES( p_box->data.p_pnot->i_date );
4540
0
    uint16_t i_version;
4541
0
    MP4_GET2BYTES( i_version );
4542
0
    if ( i_version != 0 )
4543
0
        MP4_READBOX_EXIT( 0 );
4544
0
    MP4_GETFOURCC( p_box->data.p_pnot->i_type );
4545
0
    MP4_GET2BYTES( p_box->data.p_pnot->i_index );
4546
0
    MP4_READBOX_EXIT( 1 );
4547
0
}
4548
4549
static int MP4_ReadBox_SA3D( stream_t *p_stream, MP4_Box_t *p_box )
4550
0
{
4551
0
    MP4_READBOX_ENTER( MP4_Box_data_SA3D_t, NULL );
4552
4553
0
    uint8_t i_version;
4554
0
    MP4_GET1BYTE( i_version );
4555
0
    if ( i_version != 0 )
4556
0
        MP4_READBOX_EXIT( 0 );
4557
4558
0
    MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_type );
4559
0
    MP4_GET4BYTES( p_box->data.p_SA3D->i_ambisonic_order );
4560
0
    MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_channel_ordering );
4561
0
    MP4_GET1BYTE( p_box->data.p_SA3D->i_ambisonic_normalization );
4562
0
    MP4_GET4BYTES( p_box->data.p_SA3D->i_num_channels );
4563
0
    MP4_READBOX_EXIT( 1 );
4564
0
}
4565
4566
static void MP4_FreeBox_Reference( MP4_Box_t *p_box )
4567
24
{
4568
24
    MP4_Box_data_refbox_t *p_data = p_box->data.p_refbox;
4569
24
    free( p_data->p_references );
4570
24
}
4571
4572
static int MP4_ReadBox_Reference( stream_t *p_stream, MP4_Box_t *p_box )
4573
24
{
4574
24
    MP4_READBOX_ENTER( MP4_Box_data_refbox_t, MP4_FreeBox_Reference );
4575
24
    MP4_Box_data_refbox_t *p_data = p_box->data.p_refbox;
4576
4577
24
    if( p_box->p_father->data.p_iref->i_flags == 0 )
4578
24
        MP4_GET2BYTES( p_data->i_from_item_id );
4579
0
    else
4580
24
        MP4_GET4BYTES( p_data->i_from_item_id );
4581
24
    MP4_GET2BYTES( p_data->i_reference_count );
4582
24
    if( i_read / ((p_box->p_father->data.p_iref->i_flags == 0 ) ? 2 : 4) <
4583
24
            p_data->i_reference_count )
4584
0
        MP4_READBOX_EXIT( 0 );
4585
4586
24
    p_data->p_references = vlc_alloc( p_data->i_reference_count, sizeof(*p_data->p_references) );
4587
24
    if( !p_data->p_references )
4588
0
        MP4_READBOX_EXIT( 0 );
4589
118
    for( uint16_t i=0; i<p_data->i_reference_count; i++ )
4590
94
    {
4591
94
        if( p_box->p_father->data.p_iref->i_flags == 0 )
4592
94
            MP4_GET2BYTES( p_data->p_references[i].i_to_item_id );
4593
0
        else
4594
94
            MP4_GET4BYTES( p_data->p_references[i].i_to_item_id );
4595
94
    }
4596
4597
24
    MP4_READBOX_EXIT( 1 );
4598
24
}
4599
4600
static int MP4_ReadBox_iref( stream_t *p_stream, MP4_Box_t *p_box )
4601
27
{
4602
27
    MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_iref_t, 12, NULL );
4603
27
    MP4_Box_data_iref_t *p_data = p_box->data.p_iref;
4604
27
    if( i_read < 4 )
4605
0
        MP4_READBOX_EXIT( 0 );
4606
4607
27
    MP4_GET1BYTE( p_data->i_version );
4608
27
    MP4_GET3BYTES( p_data->i_flags );
4609
27
    if( p_data->i_version > 0 )
4610
5
        MP4_READBOX_EXIT( 0 );
4611
4612
27
    assert( i_read == 0 );
4613
4614
22
    uint64_t i_remain = p_box->i_size - 12;
4615
46
    while ( i_remain > 8 )
4616
24
    {
4617
24
        MP4_Box_t *p_childbox = MP4_ReadBoxUsing( p_stream, p_box,
4618
24
                                                  MP4_ReadBox_Reference );
4619
24
        if( !p_childbox || i_remain < p_childbox->i_size )
4620
0
        {
4621
0
            MP4_BoxFree( p_childbox );
4622
0
            break;
4623
0
        }
4624
4625
24
        MP4_BoxAddChild( p_box, p_childbox );
4626
24
        i_remain -= p_childbox->i_size;
4627
24
    }
4628
4629
22
    if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
4630
0
        MP4_READBOX_EXIT( 0 );
4631
4632
22
    MP4_READBOX_EXIT( 1 );
4633
22
}
4634
4635
static void MP4_FreeBox_iloc( MP4_Box_t *p_box )
4636
46
{
4637
46
    MP4_Box_data_iloc_t *p_data = p_box->data.p_iloc;
4638
46
    if( p_data->p_items )
4639
46
    {
4640
245
        for( uint32_t i=0; i<p_data->i_item_count; i++ )
4641
199
            free( p_data->p_items[i].p_extents );
4642
46
        free( p_data->p_items );
4643
46
    }
4644
46
}
4645
4646
static int MP4_ReadBox_iloc( stream_t *p_stream, MP4_Box_t *p_box )
4647
46
{
4648
46
    MP4_READBOX_ENTER( MP4_Box_data_iloc_t, MP4_FreeBox_iloc );
4649
46
    MP4_Box_data_iloc_t *p_data = p_box->data.p_iloc;
4650
4651
46
    uint16_t i_foo;
4652
4653
46
    uint8_t i_version;
4654
46
    MP4_GET1BYTE( i_version );
4655
46
    MP4_SKIPBYTES( 3 ); // flags
4656
4657
46
    MP4_GET1BYTE( p_data->i_offset_size );
4658
46
    p_data->i_length_size = p_data->i_offset_size & 0x0F;
4659
46
    p_data->i_offset_size >>= 4;
4660
46
    MP4_GET1BYTE( p_data->i_base_offset_size );
4661
46
    if( i_version == 0 )
4662
38
        p_data->i_index_size = 0;
4663
8
    else
4664
8
        p_data->i_index_size = p_data->i_base_offset_size & 0x0F;
4665
46
    p_data->i_base_offset_size >>= 4;
4666
4667
    /* Only accept 0,4,8 */
4668
46
    if( (p_data->i_offset_size & 0xF3) || p_data->i_offset_size > 8 ||
4669
46
        (p_data->i_length_size & 0xF3) || p_data->i_length_size > 8 ||
4670
46
        (p_data->i_base_offset_size & 0xF3) || p_data->i_base_offset_size > 8 ||
4671
46
        (p_data->i_index_size & 0xF3) || p_data->i_index_size > 8 )
4672
0
        MP4_READBOX_EXIT( 0 );
4673
4674
46
    if( i_version < 2 )
4675
46
        MP4_GET2BYTES( p_data->i_item_count );
4676
0
    else if( i_version == 2 )
4677
0
        MP4_GET4BYTES( p_data->i_item_count );
4678
0
    else
4679
0
        MP4_READBOX_EXIT( 0 );
4680
4681
46
    if( i_read / 6 < p_data->i_item_count )
4682
0
        MP4_READBOX_EXIT( 0 );
4683
4684
46
    p_data->p_items = vlc_alloc( p_data->i_item_count, sizeof(p_data->p_items[0]) );
4685
46
    if( !p_data->p_items )
4686
0
        MP4_READBOX_EXIT( 0 );
4687
4688
245
    for( uint32_t i=0; i<p_data->i_item_count; i++ )
4689
199
    {
4690
199
        if( i_version < 2 )
4691
199
            MP4_GET2BYTES( p_data->p_items[i].i_item_id );
4692
0
        else
4693
199
            MP4_GET4BYTES( p_data->p_items[i].i_item_id );
4694
4695
199
        if( i_version > 0 )
4696
73
        {
4697
73
            MP4_GET2BYTES( i_foo );
4698
73
            p_data->p_items[i].i_construction_method = i_foo & 0x0F;
4699
73
        }
4700
126
        else p_data->p_items[i].i_construction_method = 0;
4701
4702
199
        MP4_GET2BYTES( p_data->p_items[i].i_data_reference_index );
4703
4704
199
        switch( p_data->i_base_offset_size )
4705
199
        {
4706
0
            case 4: MP4_GET4BYTES( p_data->p_items[i].i_base_offset ); break;
4707
1
            case 8: MP4_GET8BYTES( p_data->p_items[i].i_base_offset ); break;
4708
198
            default: p_data->p_items[i].i_base_offset = 0; break;
4709
199
        }
4710
4711
199
        MP4_GET2BYTES( p_data->p_items[i].i_extent_count );
4712
199
        if( p_data->p_items[i].i_extent_count == 0 ) // can not happen
4713
0
        {
4714
0
            p_data->i_item_count = i;
4715
0
            MP4_READBOX_EXIT( 0 );
4716
0
        }
4717
4718
199
        uint64_t i_entrysize = (( i_version > 0 ) ? p_data->i_index_size : 0) +
4719
199
                               p_data->i_offset_size + p_data->i_length_size;
4720
199
        if( i_entrysize > 0 &&
4721
199
            i_read / i_entrysize < p_data->p_items[i].i_extent_count )
4722
0
        {
4723
0
            p_data->i_item_count = i;
4724
0
            MP4_READBOX_EXIT( 0 );
4725
0
        }
4726
4727
199
        p_data->p_items[i].p_extents = vlc_alloc( p_data->p_items[i].i_extent_count,
4728
199
                                               sizeof(p_data->p_items[i].p_extents[0]) );
4729
199
        if(!p_data->p_items[i].p_extents)
4730
0
        {
4731
0
            p_data->i_item_count = i;
4732
0
            MP4_READBOX_EXIT( 0 );
4733
0
        }
4734
452
        for( uint16_t j=0; j<p_data->p_items[i].i_extent_count; j++ )
4735
253
        {
4736
253
            if( i_version > 0 )
4737
125
            {
4738
125
                switch( p_data->i_index_size )
4739
125
                {
4740
0
                    case 4: MP4_GET4BYTES( p_data->p_items[i].p_extents[j].i_extent_index ); break;
4741
0
                    case 8: MP4_GET8BYTES( p_data->p_items[i].p_extents[j].i_extent_index ); break;
4742
125
                    default: p_data->p_items[i].p_extents[j].i_extent_index = 0 ; break;
4743
125
                }
4744
125
            }
4745
253
            switch( p_data->i_offset_size )
4746
253
            {
4747
253
                case 4: MP4_GET4BYTES( p_data->p_items[i].p_extents[j].i_extent_offset ); break;
4748
0
                case 8: MP4_GET8BYTES( p_data->p_items[i].p_extents[j].i_extent_offset ); break;
4749
0
                default: p_data->p_items[i].p_extents[j].i_extent_offset = 0; break;
4750
253
            }
4751
253
            switch( p_data->i_length_size )
4752
253
            {
4753
253
                case 4: MP4_GET4BYTES( p_data->p_items[i].p_extents[j].i_extent_length ); break;
4754
0
                case 8: MP4_GET8BYTES( p_data->p_items[i].p_extents[j].i_extent_length ); break;
4755
0
                default: p_data->p_items[i].p_extents[j].i_extent_length = 0; break;
4756
253
            }
4757
253
        }
4758
199
    }
4759
4760
46
    MP4_READBOX_EXIT( 1 );
4761
46
}
4762
4763
static int MP4_ReadBox_iinf( stream_t *p_stream, MP4_Box_t *p_box )
4764
28
{
4765
28
    const uint8_t *p_versionpeek;
4766
28
    if( vlc_stream_Peek( p_stream, &p_versionpeek, 9 ) < 9 )
4767
0
        return 0;
4768
4769
28
    size_t i_header = 12 + (( p_versionpeek[8] == 0 ) ? 2 : 4);
4770
28
    MP4_READBOX_ENTER_PARTIAL( MP4_Box_data_iinf_t, i_header, NULL );
4771
28
    if( i_read + 8 < i_header )
4772
0
        MP4_READBOX_EXIT( 0 );
4773
4774
28
    uint8_t i_version;
4775
28
    MP4_GET1BYTE( i_version );
4776
28
    MP4_SKIPBYTES( 3 ); // flags
4777
28
    if( i_version > 2 )
4778
0
        MP4_READBOX_EXIT( 0 );
4779
4780
28
    if( i_version == 0 )
4781
28
        MP4_GET2BYTES( p_box->data.p_iinf->i_entry_count );
4782
17
    else
4783
28
        MP4_GET4BYTES( p_box->data.p_iinf->i_entry_count );
4784
4785
28
    assert( i_read == 0 );
4786
4787
28
    uint32_t i = 0;
4788
28
    uint64_t i_remain = p_box->i_size - i_header;
4789
128
    while ( i_remain > 8 && i < p_box->data.p_iinf->i_entry_count )
4790
100
    {
4791
100
        MP4_Box_t *p_childbox = MP4_ReadBox( p_stream, p_box );
4792
100
        if( !p_childbox || i_remain < p_childbox->i_size )
4793
0
        {
4794
0
            MP4_BoxFree( p_childbox );
4795
0
            p_box->data.p_iinf->i_entry_count = i;
4796
0
            break;
4797
0
        }
4798
4799
100
        MP4_BoxAddChild( p_box, p_childbox );
4800
100
        i_remain -= p_childbox->i_size;
4801
100
        i++;
4802
100
    }
4803
4804
28
    if ( MP4_Seek( p_stream, p_box->i_pos + p_box->i_size ) )
4805
0
        MP4_READBOX_EXIT( 0 );
4806
4807
28
    MP4_READBOX_EXIT( 1 );
4808
28
}
4809
4810
static void MP4_FreeBox_infe( MP4_Box_t *p_box )
4811
100
{
4812
100
    MP4_Box_data_infe_t *p_data = p_box->data.p_infe;
4813
100
    free( p_data->psz_content_encoding );
4814
100
    free( p_data->psz_content_type );
4815
100
    free( p_data->psz_item_name );
4816
100
    free( p_data->psz_item_uri_type );
4817
100
}
4818
4819
static int MP4_ReadBox_infe( stream_t *p_stream, MP4_Box_t *p_box )
4820
100
{
4821
100
    MP4_READBOX_ENTER( MP4_Box_data_infe_t, MP4_FreeBox_infe );
4822
100
    MP4_Box_data_infe_t *p_data = p_box->data.p_infe;
4823
4824
100
    uint8_t i_version;
4825
100
    MP4_GET1BYTE( i_version );
4826
100
    MP4_GET3BYTES( p_data->i_flags );
4827
100
    if( i_version > 3 )
4828
0
        MP4_READBOX_EXIT( 0 );
4829
4830
100
    if( i_version < 2 )
4831
0
    {
4832
0
        MP4_GET2BYTES( p_data->i_item_id );
4833
0
        MP4_GET2BYTES( p_data->i_item_protection_index );
4834
0
        p_data->psz_item_name = mp4_getstringz( &p_peek, &i_read );
4835
0
        if( i_read > 0 )
4836
0
        {
4837
0
            p_data->psz_content_type = mp4_getstringz( &p_peek, &i_read );
4838
0
            if( i_read > 0 )
4839
0
                p_data->psz_content_encoding = mp4_getstringz( &p_peek, &i_read );
4840
0
        }
4841
4842
        //if( i_version == 1 )
4843
0
        {
4844
            /* extensions, we do not care */
4845
0
        }
4846
0
    }
4847
100
    else
4848
100
    {
4849
100
        if( i_version == 2 )
4850
100
            MP4_GET2BYTES( p_data->i_item_id );
4851
0
        else
4852
100
            MP4_GET4BYTES( p_data->i_item_id );
4853
100
        MP4_GET2BYTES( p_data->i_item_protection_index );
4854
100
        MP4_GETFOURCC( p_data->item_type );
4855
100
        p_data->psz_item_name = mp4_getstringz( &p_peek, &i_read );
4856
100
        if( p_data->item_type == VLC_FOURCC('m','i','m','e') )
4857
0
        {
4858
0
            p_data->psz_content_type = mp4_getstringz( &p_peek, &i_read );
4859
0
            if( i_read > 0 )
4860
0
                p_data->psz_content_encoding = mp4_getstringz( &p_peek, &i_read );
4861
0
        }
4862
100
        else if( p_data->item_type == VLC_FOURCC('u','r','i',' ') )
4863
0
        {
4864
0
            p_data->psz_item_uri_type = mp4_getstringz( &p_peek, &i_read );
4865
0
        }
4866
100
    }
4867
4868
100
    MP4_READBOX_EXIT( 1 );
4869
100
}
4870
4871
static int MP4_ReadBox_pitm( stream_t *p_stream, MP4_Box_t *p_box )
4872
61
{
4873
61
    MP4_READBOX_ENTER( MP4_Box_data_pitm_t, NULL );
4874
61
    MP4_Box_data_pitm_t *p_data = p_box->data.p_pitm;
4875
4876
61
    uint8_t i_version;
4877
61
    MP4_GET1BYTE( i_version );
4878
61
    MP4_SKIPBYTES( 3 ); // flags
4879
4880
61
    if( i_version == 0 )
4881
61
        MP4_GET2BYTES( p_data->i_item_id );
4882
0
    else
4883
61
        MP4_GET4BYTES( p_data->i_item_id );
4884
4885
61
    MP4_READBOX_EXIT( 1 );
4886
61
}
4887
4888
static int MP4_ReadBox_ispe( stream_t *p_stream, MP4_Box_t *p_box )
4889
11
{
4890
11
    MP4_READBOX_ENTER( MP4_Box_data_ispe_t, NULL );
4891
11
    MP4_Box_data_ispe_t *p_data = p_box->data.p_ispe;
4892
4893
11
    uint8_t i_version;
4894
11
    MP4_GET1BYTE( i_version );
4895
11
    MP4_SKIPBYTES( 3 ); // flags
4896
11
    if( i_version > 0 )
4897
0
        MP4_READBOX_EXIT( 0 );
4898
4899
11
    MP4_GET4BYTES( p_data->i_width );
4900
11
    MP4_GET4BYTES( p_data->i_height );
4901
4902
11
    MP4_READBOX_EXIT( 1 );
4903
11
}
4904
4905
static void MP4_FreeBox_ipma( MP4_Box_t *p_box )
4906
42
{
4907
42
    MP4_Box_data_ipma_t *p_data = p_box->data.p_ipma;
4908
203
    for( uint32_t i=0; i<p_data->i_entry_count; i++ )
4909
161
        free( p_data->p_entries[i].p_assocs );
4910
42
    free( p_data->p_entries );
4911
42
}
4912
4913
static int MP4_ReadBox_ipma( stream_t *p_stream, MP4_Box_t *p_box )
4914
42
{
4915
42
    MP4_READBOX_ENTER( MP4_Box_data_ipma_t, MP4_FreeBox_ipma );
4916
42
    MP4_Box_data_ipma_t *p_data = p_box->data.p_ipma;
4917
4918
42
    uint8_t i_version;
4919
42
    uint32_t i_flags;
4920
42
    MP4_GET1BYTE( i_version );
4921
42
    MP4_GET3BYTES( i_flags );
4922
4923
42
    MP4_GET4BYTES( p_data->i_entry_count );
4924
42
    if( (i_read / ((i_version < 1) ? 3 : 5) <  p_data->i_entry_count) )
4925
0
    {
4926
0
        p_data->i_entry_count = 0;
4927
0
        MP4_READBOX_EXIT( 0 );
4928
0
    }
4929
4930
42
    p_data->p_entries = vlc_alloc( p_data->i_entry_count, sizeof(p_data->p_entries[0]) );
4931
42
    if( !p_data->p_entries )
4932
0
    {
4933
0
        p_data->i_entry_count = 0;
4934
0
        MP4_READBOX_EXIT( 0 );
4935
0
    }
4936
4937
203
    for( uint32_t i=0; i<p_data->i_entry_count; i++ )
4938
161
    {
4939
161
        if( i_read < ((i_version < 1) ? 3 : 5) )
4940
0
        {
4941
0
            p_data->i_entry_count = i;
4942
0
            MP4_READBOX_EXIT( 0 );
4943
0
        }
4944
161
        if( i_version < 1 )
4945
161
            MP4_GET2BYTES( p_data->p_entries[i].i_item_id );
4946
0
        else
4947
161
            MP4_GET4BYTES( p_data->p_entries[i].i_item_id );
4948
161
        MP4_GET1BYTE( p_data->p_entries[i].i_association_count );
4949
4950
161
        if( i_read / ((i_flags & 0x01) ? 2 : 1) <
4951
161
               p_data->p_entries[i].i_association_count )
4952
0
        {
4953
0
            p_data->i_entry_count = i;
4954
0
            MP4_READBOX_EXIT( 0 );
4955
0
        }
4956
4957
161
        p_data->p_entries[i].p_assocs =
4958
161
                vlc_alloc( p_data->p_entries[i].i_association_count,
4959
161
                           sizeof(p_data->p_entries[i].p_assocs[0]) );
4960
161
        if( !p_data->p_entries[i].p_assocs )
4961
0
        {
4962
0
            p_data->p_entries[i].i_association_count = 0;
4963
0
            p_data->i_entry_count = i;
4964
0
            MP4_READBOX_EXIT( 0 );
4965
0
        }
4966
4967
745
        for( uint8_t j=0; j<p_data->p_entries[i].i_association_count; j++ )
4968
584
        {
4969
584
            MP4_GET1BYTE( p_data->p_entries[i].p_assocs[j].i_property_index );
4970
584
            p_data->p_entries[i].p_assocs[j].b_essential =
4971
584
                    p_data->p_entries[i].p_assocs[j].i_property_index & 0x80;
4972
584
            p_data->p_entries[i].p_assocs[j].i_property_index &= 0x7F;
4973
584
            if( i_flags & 0x01 )
4974
0
            {
4975
0
                p_data->p_entries[i].p_assocs[j].i_property_index <<= 8;
4976
0
                uint8_t i_low;
4977
0
                MP4_GET1BYTE( i_low );
4978
0
                p_data->p_entries[i].p_assocs[j].i_property_index |= i_low;
4979
0
            }
4980
584
        }
4981
161
    }
4982
4983
42
    MP4_READBOX_EXIT( 1 );
4984
42
}
4985
4986
/* For generic */
4987
static int MP4_ReadBox_default( stream_t *p_stream, MP4_Box_t *p_box )
4988
1.04k
{
4989
1.04k
    if( !p_box->p_father )
4990
0
    {
4991
0
        goto unknown;
4992
0
    }
4993
4994
1.04k
unknown:
4995
1.04k
    if MP4_BOX_TYPE_ASCII()
4996
1.04k
        msg_Warn( p_stream,
4997
0
                "unknown box type %4.4s (incompletely loaded)",
4998
0
                (char*)&p_box->i_type );
4999
0
    else
5000
1.04k
        msg_Warn( p_stream,
5001
1.04k
                "unknown box type c%3.3s (incompletely loaded)",
5002
1.04k
                (char*)&p_box->i_type+1 );
5003
1.04k
    p_box->e_flags |= BOX_FLAG_INCOMPLETE;
5004
5005
1.04k
    return 1;
5006
1.04k
}
5007
5008
/**** ------------------------------------------------------------------- ****/
5009
5010
static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
5011
2
{
5012
2
    if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
5013
0
        return MP4_ReadBox_tfrf( p_stream, p_box );
5014
2
    if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
5015
0
        return MP4_ReadBox_tfxd( p_stream, p_box );
5016
2
    if( !CmpUUID( &p_box->i_uuid, &XML360BoxUUID ) )
5017
0
        return MP4_ReadBox_XML360( p_stream, p_box );
5018
2
    if( !CmpUUID( &p_box->i_uuid, &PS3DDSBoxUUID ) && p_box->i_size == 28 )
5019
0
        return MP4_ReadBox_Binary( p_stream, p_box );
5020
5021
2
#ifdef MP4_VERBOSE
5022
2
    msg_Warn( p_stream, "Unknown uuid type box: "
5023
2
    "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-"
5024
2
    "%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
5025
2
    p_box->i_uuid.b[0],  p_box->i_uuid.b[1],  p_box->i_uuid.b[2],  p_box->i_uuid.b[3],
5026
2
    p_box->i_uuid.b[4],  p_box->i_uuid.b[5],  p_box->i_uuid.b[6],  p_box->i_uuid.b[7],
5027
2
    p_box->i_uuid.b[8],  p_box->i_uuid.b[9],  p_box->i_uuid.b[10], p_box->i_uuid.b[11],
5028
2
    p_box->i_uuid.b[12], p_box->i_uuid.b[13], p_box->i_uuid.b[14], p_box->i_uuid.b[15] );
5029
#else
5030
    msg_Warn( p_stream, "Unknown uuid type box" );
5031
#endif
5032
2
    return 1;
5033
2
}
5034
5035
/**** ------------------------------------------------------------------- ****/
5036
/****                   "Higher level" Functions                          ****/
5037
/**** ------------------------------------------------------------------- ****/
5038
5039
static const struct
5040
{
5041
    uint32_t i_type;
5042
    int  (*MP4_ReadBox_function )( stream_t *p_stream, MP4_Box_t *p_box );
5043
    uint32_t i_parent; /* set parent to restrict, duplicating if needed; 0 for any */
5044
} MP4_Box_Function [] =
5045
{
5046
    /* Containers */
5047
    { ATOM_moov,    MP4_ReadBoxContainer,     0 },
5048
    { ATOM_trak,    MP4_ReadBoxContainer,     ATOM_moov },
5049
    { ATOM_trak,    MP4_ReadBoxContainer,     ATOM_foov },
5050
    { ATOM_mdia,    MP4_ReadBox_mdia,         ATOM_trak },
5051
    { ATOM_moof,    MP4_ReadBoxContainer,     0 },
5052
    { ATOM_minf,    MP4_ReadBoxContainer,     ATOM_mdia },
5053
    { ATOM_stbl,    MP4_ReadBoxContainer,     ATOM_minf },
5054
    { ATOM_dinf,    MP4_ReadBoxContainer,     ATOM_minf },
5055
    { ATOM_dinf,    MP4_ReadBoxContainer,     ATOM_meta },
5056
    { ATOM_edts,    MP4_ReadBoxContainer,     ATOM_trak },
5057
    { ATOM_udta,    MP4_ReadBoxContainer,     0 },
5058
    { ATOM_nmhd,    MP4_ReadBoxContainer,     ATOM_minf },
5059
    { ATOM_hnti,    MP4_ReadBoxContainer,     ATOM_udta },
5060
    { ATOM_rmra,    MP4_ReadBoxContainer,     ATOM_moov },
5061
    { ATOM_rmda,    MP4_ReadBoxContainer,     ATOM_rmra },
5062
    { ATOM_tref,    MP4_ReadBox_tref,         ATOM_trak },
5063
    { ATOM_gmhd,    MP4_ReadBoxContainer,     ATOM_minf },
5064
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_stsd },
5065
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_mp4a }, /* some quicktime mp4a/wave/mp4a.. */
5066
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_WMA2 }, /* flip4mac */
5067
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_in24 },
5068
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_in32 },
5069
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_fl32 },
5070
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_fl64 },
5071
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_QDMC },
5072
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_QDM2 },
5073
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_XiFL }, /* XiphQT */
5074
    { ATOM_wave,    MP4_ReadBoxContainer,     ATOM_XiVs }, /* XiphQT */
5075
    { ATOM_ilst,    MP4_ReadBox_ilst,         ATOM_meta },
5076
    { ATOM_mvex,    MP4_ReadBoxContainer,     ATOM_moov },
5077
    { ATOM_mvex,    MP4_ReadBoxContainer,     ATOM_ftyp },
5078
5079
    /* Quicktime compression */
5080
    { ATOM_foov,    MP4_ReadBoxContainer,     0 },
5081
    { ATOM_cmov,    MP4_ReadBox_cmov,         ATOM_foov },
5082
    { ATOM_cmov,    MP4_ReadBox_cmov,         ATOM_moov },
5083
    { ATOM_dcom,    MP4_ReadBox_dcom,         ATOM_cmov },
5084
    { ATOM_cmvd,    MP4_ReadBox_cmvd,         ATOM_cmov },
5085
5086
    /* specific box */
5087
    { ATOM_ftyp,    MP4_ReadBox_ftyp,         0 },
5088
    { ATOM_styp,    MP4_ReadBox_ftyp,         0 },
5089
    { ATOM_mvhd,    MP4_ReadBox_mvhd,         ATOM_moov },
5090
    { ATOM_mvhd,    MP4_ReadBox_mvhd,         ATOM_foov },
5091
    { ATOM_tkhd,    MP4_ReadBox_tkhd,         ATOM_trak },
5092
    { ATOM_load,    MP4_ReadBox_load,         ATOM_trak },
5093
    { ATOM_mdhd,    MP4_ReadBox_mdhd,         ATOM_mdia },
5094
    { ATOM_hdlr,    MP4_ReadBox_hdlr,         ATOM_mdia },
5095
    { ATOM_hdlr,    MP4_ReadBox_hdlr,         ATOM_meta },
5096
    { ATOM_hdlr,    MP4_ReadBox_hdlr,         ATOM_minf },
5097
    { ATOM_vmhd,    MP4_ReadBox_vmhd,         ATOM_minf },
5098
    { ATOM_smhd,    MP4_ReadBox_smhd,         ATOM_minf },
5099
    { ATOM_hmhd,    MP4_ReadBox_hmhd,         ATOM_minf },
5100
    { ATOM_alis,    MP4_ReadBoxSkip,          ATOM_dref },
5101
    { ATOM_url,     MP4_ReadBox_url,          0 },
5102
    { ATOM_urn,     MP4_ReadBox_urn,          0 },
5103
    { ATOM_dref,    MP4_ReadBox_LtdContainer, 0 },
5104
    { ATOM_stts,    MP4_ReadBox_stts,         ATOM_stbl },
5105
    { ATOM_ctts,    MP4_ReadBox_ctts,         ATOM_stbl },
5106
    { ATOM_cslg,    MP4_ReadBox_cslg,         ATOM_stbl },
5107
    { ATOM_stsd,    MP4_ReadBox_stsd,         ATOM_stbl },
5108
    { ATOM_stsz,    MP4_ReadBox_stsz,         ATOM_stbl },
5109
    { ATOM_stz2,    MP4_ReadBox_stz2,         ATOM_stbl },
5110
    { ATOM_stsc,    MP4_ReadBox_stsc,         ATOM_stbl },
5111
    { ATOM_stco,    MP4_ReadBox_stco_co64,    ATOM_stbl },
5112
    { ATOM_co64,    MP4_ReadBox_stco_co64,    ATOM_stbl },
5113
    { ATOM_stss,    MP4_ReadBox_stss,         ATOM_stbl },
5114
    { ATOM_stsh,    MP4_ReadBox_stsh,         ATOM_stbl },
5115
    { ATOM_stdp,    MP4_ReadBox_stdp,         0 },
5116
    { ATOM_elst,    MP4_ReadBox_elst,         ATOM_edts },
5117
    { ATOM_cprt,    MP4_ReadBox_cprt,         0 },
5118
    { ATOM_esds,    MP4_ReadBox_esds,         ATOM_wave }, /* mp4a in wave chunk */
5119
    { ATOM_esds,    MP4_ReadBox_esds,         ATOM_mp4a },
5120
    { ATOM_esds,    MP4_ReadBox_esds,         ATOM_mp4v },
5121
    { ATOM_esds,    MP4_ReadBox_esds,         ATOM_mp4s },
5122
    { ATOM_dfLa,    MP4_ReadBox_Binary,       ATOM_fLaC },
5123
    { ATOM_av1C,    MP4_ReadBox_av1C,         ATOM_av01 },
5124
    { ATOM_avcC,    MP4_ReadBox_avcC,         ATOM_avc1 },
5125
    { ATOM_avcC,    MP4_ReadBox_avcC,         ATOM_avc3 },
5126
    { ATOM_vvcC,    MP4_ReadBox_Binary,       ATOM_vvc1 },
5127
    { ATOM_apvC,    MP4_ReadBox_Binary,       ATOM_apv1 },
5128
    { ATOM_hvcC,    MP4_ReadBox_Binary,       0 },
5129
    { ATOM_jpeC,    MP4_ReadBox_Binary,       0 }, /* heif */
5130
    { ATOM_av1C,    MP4_ReadBox_av1C,         ATOM_ipco }, /* heif */
5131
    { ATOM_vpcC,    MP4_ReadBox_vpcC,         ATOM_vp08 },
5132
    { ATOM_vpcC,    MP4_ReadBox_vpcC,         ATOM_vp09 },
5133
    { ATOM_vpcC,    MP4_ReadBox_vpcC,         ATOM_vp10 },
5134
    { ATOM_SmDm,    MP4_ReadBox_SmDm,         ATOM_vpcC }, /* SMPTE2086 mastering display */
5135
    { ATOM_mdcv,    MP4_ReadBox_SmDm,         0 }, /* */
5136
    { ATOM_CoLL,    MP4_ReadBox_CoLL,         ATOM_vpcC }, /* CEA861-3 light level */
5137
    { ATOM_clli,    MP4_ReadBox_CoLL,         0 }, /* */
5138
    { ATOM_dac3,    MP4_ReadBox_dac3,         0 },
5139
    { ATOM_dec3,    MP4_ReadBox_dec3,         0 },
5140
    { ATOM_dvc1,    MP4_ReadBox_dvc1,         ATOM_vc1  },
5141
    { ATOM_fiel,    MP4_ReadBox_fiel,         0 },
5142
    { ATOM_glbl,    MP4_ReadBox_Binary,       0 },
5143
    { ATOM_enda,    MP4_ReadBox_enda,         0 },
5144
    { ATOM_pcmC,    MP4_ReadBox_pcmC,         0 }, /* ISO-IEC 23003-5 */
5145
    { ATOM_iods,    MP4_ReadBox_iods,         0 },
5146
    { ATOM_pasp,    MP4_ReadBox_pasp,         0 },
5147
    { ATOM_clap,    MP4_ReadBox_clap,         0 },
5148
    { ATOM_btrt,    MP4_ReadBox_btrt,         0 }, /* codecs bitrate stsd/????/btrt */
5149
    { ATOM_keys,    MP4_ReadBox_keys,         ATOM_meta },
5150
    { ATOM_colr,    MP4_ReadBox_colr,         0 },
5151
    { ATOM_irot,    MP4_ReadBox_irot,         0 }, /* heif */
5152
    { ATOM_dvcC,    MP4_ReadBox_dvcC,         0 }, /* dolby vision config record */
5153
    { ATOM_dvvC,    MP4_ReadBox_dvcC,         0 },
5154
    { ATOM_dvwC,    MP4_ReadBox_dvcC,         0 },
5155
5156
    /* XiphQT */
5157
    { ATOM_vCtH,    MP4_ReadBox_Binary,       ATOM_wave },
5158
    { ATOM_vCtC,    MP4_ReadBox_Binary,       ATOM_wave },
5159
    { ATOM_vCtd,    MP4_ReadBox_Binary,       ATOM_wave },
5160
    { ATOM_fCtS,    MP4_ReadBox_Binary,       ATOM_wave },
5161
5162
    /* Samples groups specific information */
5163
    { ATOM_sbgp,    MP4_ReadBox_sbgp,         ATOM_stbl },
5164
    { ATOM_sbgp,    MP4_ReadBox_sbgp,         ATOM_traf },
5165
    { ATOM_sgpd,    MP4_ReadBox_sgpd,         ATOM_stbl },
5166
    { ATOM_sgpd,    MP4_ReadBox_sgpd,         ATOM_traf },
5167
5168
    /* Quicktime preview atoms, all at root */
5169
    { ATOM_pnot,    MP4_ReadBox_pnot,         0 },
5170
    { ATOM_pict,    MP4_ReadBox_Binary,       0 },
5171
    { ATOM_PICT,    MP4_ReadBox_Binary,       0 },
5172
    /* Other preview atoms */
5173
    { ATOM_thum,    MP4_ReadBox_Binary,       0 },
5174
5175
    /* Nothing to do with this box */
5176
    { ATOM_mdat,    MP4_ReadBoxSkip,          0 },
5177
    { ATOM_skip,    MP4_ReadBoxSkip,          0 },
5178
    { ATOM_free,    MP4_ReadBoxSkip,          0 },
5179
    { ATOM_wide,    MP4_ReadBoxSkip,          0 },
5180
    { ATOM_binm,    MP4_ReadBoxSkip,          0 },
5181
5182
    /* In sample WebVTT subtitle atoms. No ATOM_wvtt in normal parsing */
5183
    { ATOM_vttc,    MP4_ReadBoxContainer,         ATOM_wvtt },
5184
    { ATOM_payl,    MP4_ReadBox_Binary,           ATOM_vttc },
5185
5186
    /* Sound extensions */
5187
    { ATOM_chan,    MP4_ReadBox_stsdext_chan, 0 },
5188
    { ATOM_srat,    MP4_ReadBox_stsdext_srat, 0 },
5189
    { ATOM_WMA2,    MP4_ReadBox_WMA2,         ATOM_wave }, /* flip4mac */
5190
    { ATOM_dOps,    MP4_ReadBox_Binary,       ATOM_Opus },
5191
    { ATOM_wfex,    MP4_ReadBox_WMA2,         ATOM_wma  }, /* ismv formatex */
5192
5193
    { ATOM_mjqt,    MP4_ReadBox_default,      0 }, /* found in mjpa/b */
5194
    { ATOM_mjht,    MP4_ReadBox_default,      0 },
5195
5196
    { ATOM_strf,    MP4_ReadBox_strf,         ATOM_WVC1 }, /* MS smooth */
5197
    { ATOM_strf,    MP4_ReadBox_strf,         ATOM_H264 }, /* MS smooth */
5198
5199
    { ATOM_strf,    MP4_ReadBox_strf,         ATOM_WMV3 }, /* flip4mac */
5200
    { ATOM_ASF ,    MP4_ReadBox_ASF,          ATOM_WMV3 }, /* flip4mac */
5201
    { ATOM_ASF ,    MP4_ReadBox_ASF,          ATOM_wave }, /* flip4mac */
5202
5203
    { ATOM_hint,    MP4_ReadBox_default,      ATOM_stbl },
5204
5205
    /* found in hnti */
5206
    { ATOM_rtp,     MP4_ReadBox_rtp,          ATOM_hnti },
5207
    { ATOM_sdp,     MP4_ReadBox_sdp,          ATOM_hnti },
5208
5209
    /* found in rrtp sample description */
5210
    { ATOM_tims,     MP4_ReadBox_tims,        0 },
5211
    { ATOM_tsro,     MP4_ReadBox_tsro,        0 },
5212
    { ATOM_tssy,     MP4_ReadBox_tssy,        0 },
5213
5214
    /* found in rmra/rmda */
5215
    { ATOM_rdrf,    MP4_ReadBox_rdrf,         ATOM_rmda },
5216
    { ATOM_rmdr,    MP4_ReadBox_rmdr,         ATOM_rmda },
5217
    { ATOM_rmqu,    MP4_ReadBox_rmqu,         ATOM_rmda },
5218
    { ATOM_rmvc,    MP4_ReadBox_rmvc,         ATOM_rmda },
5219
5220
    { ATOM_sinf,    MP4_ReadBoxContainer,     0 },
5221
    { ATOM_schi,    MP4_ReadBoxContainer,     0 },
5222
    { ATOM_user,    MP4_ReadBox_drms,         0 },
5223
    { ATOM_key,     MP4_ReadBox_drms,         0 },
5224
    { ATOM_iviv,    MP4_ReadBox_drms,         0 },
5225
    { ATOM_priv,    MP4_ReadBox_drms,         0 },
5226
    { ATOM_frma,    MP4_ReadBox_frma,         ATOM_sinf }, /* and rinf */
5227
    { ATOM_frma,    MP4_ReadBox_frma,         ATOM_wave }, /* flip4mac */
5228
    { ATOM_skcr,    MP4_ReadBox_skcr,         0 },
5229
5230
    /* ilst meta tags */
5231
    { ATOM_0xa9ART, MP4_ReadBox_Metadata,    ATOM_ilst },
5232
    { ATOM_0xa9alb, MP4_ReadBox_Metadata,    ATOM_ilst },
5233
    { ATOM_0xa9cmt, MP4_ReadBox_Metadata,    ATOM_ilst },
5234
    { ATOM_0xa9com, MP4_ReadBox_Metadata,    ATOM_ilst },
5235
    { ATOM_0xa9cpy, MP4_ReadBox_Metadata,    ATOM_ilst },
5236
    { ATOM_0xa9day, MP4_ReadBox_Metadata,    ATOM_ilst },
5237
    { ATOM_0xa9des, MP4_ReadBox_Metadata,    ATOM_ilst },
5238
    { ATOM_0xa9enc, MP4_ReadBox_Metadata,    ATOM_ilst },
5239
    { ATOM_0xa9gen, MP4_ReadBox_Metadata,    ATOM_ilst },
5240
    { ATOM_0xa9grp, MP4_ReadBox_Metadata,    ATOM_ilst },
5241
    { ATOM_0xa9lyr, MP4_ReadBox_Metadata,    ATOM_ilst },
5242
    { ATOM_0xa9nam, MP4_ReadBox_Metadata,    ATOM_ilst },
5243
    { ATOM_0xa9too, MP4_ReadBox_Metadata,    ATOM_ilst },
5244
    { ATOM_0xa9trk, MP4_ReadBox_Metadata,    ATOM_ilst },
5245
    { ATOM_0xa9wrt, MP4_ReadBox_Metadata,    ATOM_ilst },
5246
    { ATOM_aART,    MP4_ReadBox_Metadata,    ATOM_ilst },
5247
    { ATOM_atID,    MP4_ReadBox_Metadata,    ATOM_ilst }, /* iTunes */
5248
    { ATOM_cnID,    MP4_ReadBox_Metadata,    ATOM_ilst }, /* iTunes */
5249
    { ATOM_covr,    MP4_ReadBoxContainer,    ATOM_ilst },
5250
    { ATOM_cpil,    MP4_ReadBox_Metadata,    ATOM_ilst },
5251
    { ATOM_desc,    MP4_ReadBox_Metadata,    ATOM_ilst },
5252
    { ATOM_disk,    MP4_ReadBox_Metadata,    ATOM_ilst },
5253
    { ATOM_flvr,    MP4_ReadBox_Metadata,    ATOM_ilst },
5254
    { ATOM_gnre,    MP4_ReadBox_Metadata,    ATOM_ilst },
5255
    { ATOM_rtng,    MP4_ReadBox_Metadata,    ATOM_ilst },
5256
    { ATOM_trkn,    MP4_ReadBox_Metadata,    ATOM_ilst },
5257
    { ATOM_xid_,    MP4_ReadBox_Metadata,    ATOM_ilst },
5258
    { ATOM_gshh,    MP4_ReadBox_Metadata,    ATOM_ilst }, /* YouTube gs?? */
5259
    { ATOM_gspm,    MP4_ReadBox_Metadata,    ATOM_ilst },
5260
    { ATOM_gspu,    MP4_ReadBox_Metadata,    ATOM_ilst },
5261
    { ATOM_gssd,    MP4_ReadBox_Metadata,    ATOM_ilst },
5262
    { ATOM_gsst,    MP4_ReadBox_Metadata,    ATOM_ilst },
5263
    { ATOM_gstd,    MP4_ReadBox_Metadata,    ATOM_ilst },
5264
    { ATOM_ITUN,    MP4_ReadBox_Metadata,    ATOM_ilst }, /* iTunesInfo */
5265
    { ATOM_purl,    MP4_ReadBox_Metadata,    ATOM_ilst },
5266
5267
    /* udta */
5268
    { ATOM_0x40PRM, MP4_ReadBox_Binary,    ATOM_udta },
5269
    { ATOM_0x40PRQ, MP4_ReadBox_Binary,    ATOM_udta },
5270
    { ATOM_0xa9ART, MP4_ReadBox_Binary,    ATOM_udta },
5271
    { ATOM_0xa9alb, MP4_ReadBox_Binary,    ATOM_udta },
5272
    { ATOM_0xa9ard, MP4_ReadBox_Binary,    ATOM_udta },
5273
    { ATOM_0xa9arg, MP4_ReadBox_Binary,    ATOM_udta },
5274
    { ATOM_0xa9aut, MP4_ReadBox_Binary,    ATOM_udta },
5275
    { ATOM_0xa9cak, MP4_ReadBox_Binary,    ATOM_udta },
5276
    { ATOM_0xa9cmt, MP4_ReadBox_Binary,    ATOM_udta },
5277
    { ATOM_0xa9con, MP4_ReadBox_Binary,    ATOM_udta },
5278
    { ATOM_0xa9com, MP4_ReadBox_Binary,    ATOM_udta },
5279
    { ATOM_0xa9cpy, MP4_ReadBox_Binary,    ATOM_udta },
5280
    { ATOM_0xa9day, MP4_ReadBox_Binary,    ATOM_udta },
5281
    { ATOM_0xa9des, MP4_ReadBox_Binary,    ATOM_udta },
5282
    { ATOM_0xa9dir, MP4_ReadBox_Binary,    ATOM_udta },
5283
    { ATOM_0xa9dis, MP4_ReadBox_Binary,    ATOM_udta },
5284
    { ATOM_0xa9dsa, MP4_ReadBox_Binary,    ATOM_udta },
5285
    { ATOM_0xa9fmt, MP4_ReadBox_Binary,    ATOM_udta },
5286
    { ATOM_0xa9gen, MP4_ReadBox_Binary,    ATOM_udta },
5287
    { ATOM_0xa9grp, MP4_ReadBox_Binary,    ATOM_udta },
5288
    { ATOM_0xa9hst, MP4_ReadBox_Binary,    ATOM_udta },
5289
    { ATOM_0xa9inf, MP4_ReadBox_Binary,    ATOM_udta },
5290
    { ATOM_0xa9isr, MP4_ReadBox_Binary,    ATOM_udta },
5291
    { ATOM_0xa9lab, MP4_ReadBox_Binary,    ATOM_udta },
5292
    { ATOM_0xa9lal, MP4_ReadBox_Binary,    ATOM_udta },
5293
    { ATOM_0xa9lnt, MP4_ReadBox_Binary,    ATOM_udta },
5294
    { ATOM_0xa9lyr, MP4_ReadBox_Binary,    ATOM_udta },
5295
    { ATOM_0xa9mak, MP4_ReadBox_Binary,    ATOM_udta },
5296
    { ATOM_0xa9mal, MP4_ReadBox_Binary,    ATOM_udta },
5297
    { ATOM_0xa9mod, MP4_ReadBox_Binary,    ATOM_udta },
5298
    { ATOM_0xa9nam, MP4_ReadBox_Binary,    ATOM_udta },
5299
    { ATOM_0xa9ope, MP4_ReadBox_Binary,    ATOM_udta },
5300
    { ATOM_0xa9phg, MP4_ReadBox_Binary,    ATOM_udta },
5301
    { ATOM_0xa9PRD, MP4_ReadBox_Binary,    ATOM_udta },
5302
    { ATOM_0xa9prd, MP4_ReadBox_Binary,    ATOM_udta },
5303
    { ATOM_0xa9prf, MP4_ReadBox_Binary,    ATOM_udta },
5304
    { ATOM_0xa9pub, MP4_ReadBox_Binary,    ATOM_udta },
5305
    { ATOM_0xa9req, MP4_ReadBox_Binary,    ATOM_udta },
5306
    { ATOM_0xa9sne, MP4_ReadBox_Binary,    ATOM_udta },
5307
    { ATOM_0xa9snm, MP4_ReadBox_Binary,    ATOM_udta },
5308
    { ATOM_0xa9sol, MP4_ReadBox_Binary,    ATOM_udta },
5309
    { ATOM_0xa9src, MP4_ReadBox_Binary,    ATOM_udta },
5310
    { ATOM_0xa9st3, MP4_ReadBox_Binary,    ATOM_udta },
5311
    { ATOM_0xa9swr, MP4_ReadBox_Binary,    ATOM_udta },
5312
    { ATOM_0xa9thx, MP4_ReadBox_Binary,    ATOM_udta },
5313
    { ATOM_0xa9too, MP4_ReadBox_Binary,    ATOM_udta },
5314
    { ATOM_0xa9trk, MP4_ReadBox_Binary,    ATOM_udta },
5315
    { ATOM_0xa9url, MP4_ReadBox_Binary,    ATOM_udta },
5316
    { ATOM_0xa9wrn, MP4_ReadBox_Binary,    ATOM_udta },
5317
    { ATOM_0xa9xpd, MP4_ReadBox_Binary,    ATOM_udta },
5318
    { ATOM_0xa9xyz, MP4_ReadBox_Binary,    ATOM_udta },
5319
    { ATOM_chpl,    MP4_ReadBox_chpl,      ATOM_udta }, /* nero unlabeled chapters list */
5320
    { ATOM_MCPS,    MP4_ReadBox_Binary,    ATOM_udta },
5321
    { ATOM_name,    MP4_ReadBox_Binary,    ATOM_udta },
5322
    { ATOM_vndr,    MP4_ReadBox_Binary,    ATOM_udta },
5323
    { ATOM_SDLN,    MP4_ReadBox_Binary,    ATOM_udta },
5324
    { ATOM_HMMT,    MP4_ReadBox_HMMT,      ATOM_udta }, /* GoPro HiLight tags */
5325
5326
    /* udta, non meta */
5327
    { ATOM_tsel,    MP4_ReadBox_tsel,    ATOM_udta },
5328
5329
    /* iTunes/Quicktime meta info */
5330
    { ATOM_meta,    MP4_ReadBox_meta,    0 },
5331
    { ATOM_ID32,    MP4_ReadBox_Binary,  ATOM_meta }, /* ID3v2 in 3GPP / ETSI TS 126 244 8.3 */
5332
    { ATOM_data,    MP4_ReadBox_data,    0 }, /* ilst/@too and others, ITUN/data */
5333
    { ATOM_mean,    MP4_ReadBox_Binary,  ATOM_ITUN },
5334
    { ATOM_name,    MP4_ReadBox_Binary,  ATOM_ITUN },
5335
5336
    /* found in smoothstreaming */
5337
    { ATOM_traf,    MP4_ReadBoxContainer,    ATOM_moof },
5338
    { ATOM_mfra,    MP4_ReadBoxContainer,    0 },
5339
    { ATOM_mfhd,    MP4_ReadBox_mfhd,        ATOM_moof },
5340
    { ATOM_sidx,    MP4_ReadBox_sidx,        0 },
5341
    { ATOM_tfhd,    MP4_ReadBox_tfhd,        ATOM_traf },
5342
    { ATOM_trun,    MP4_ReadBox_trun,        ATOM_traf },
5343
    { ATOM_tfdt,    MP4_ReadBox_tfdt,        ATOM_traf },
5344
    { ATOM_trex,    MP4_ReadBox_trex,        ATOM_mvex },
5345
    { ATOM_mehd,    MP4_ReadBox_mehd,        ATOM_mvex },
5346
    { ATOM_sdtp,    MP4_ReadBox_sdtp,        0 },
5347
    { ATOM_tfra,    MP4_ReadBox_tfra,        ATOM_mfra },
5348
    { ATOM_mfro,    MP4_ReadBox_mfro,        ATOM_mfra },
5349
    { ATOM_uuid,    MP4_ReadBox_uuid,        0 },
5350
5351
    /* spatial/360°/VR */
5352
    { ATOM_st3d,    MP4_ReadBox_st3d,        0 },
5353
    { ATOM_sv3d,    MP4_ReadBoxContainer,    0 },
5354
    { ATOM_proj,    MP4_ReadBoxContainer,    ATOM_sv3d },
5355
    { ATOM_prhd,    MP4_ReadBox_prhd,        ATOM_proj },
5356
    { ATOM_equi,    MP4_ReadBox_equi,        ATOM_proj },
5357
    { ATOM_cbmp,    MP4_ReadBox_cbmp,        ATOM_proj },
5358
5359
    /* Ambisonics */
5360
    { ATOM_SA3D,    MP4_ReadBox_SA3D,        0 },
5361
5362
    /* iso4 brand meta references */
5363
    { ATOM_idat,    MP4_ReadBoxSkip,         ATOM_meta },
5364
    { ATOM_iloc,    MP4_ReadBox_iloc,        ATOM_meta },
5365
    { ATOM_iinf,    MP4_ReadBox_iinf,        ATOM_meta },
5366
    { ATOM_infe,    MP4_ReadBox_infe,        ATOM_iinf },
5367
    { ATOM_iref,    MP4_ReadBox_iref,        ATOM_meta },
5368
    { ATOM_pitm,    MP4_ReadBox_pitm,        ATOM_meta },
5369
5370
    /* HEIF specific meta references */
5371
    { ATOM_iprp,    MP4_ReadBoxContainer,    ATOM_meta },
5372
    { ATOM_ipco,    MP4_ReadBoxContainer,    ATOM_iprp },
5373
    { ATOM_ispe,    MP4_ReadBox_ispe,        ATOM_ipco },
5374
    { ATOM_ipma,    MP4_ReadBox_ipma,        ATOM_iprp },
5375
5376
    /* Last entry */
5377
    { 0,              MP4_ReadBox_default,   0 }
5378
};
5379
5380
static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father )
5381
1.96k
{
5382
1.96k
    int i_index;
5383
5384
436k
    for( i_index = 0; ; i_index++ )
5385
438k
    {
5386
438k
        if ( MP4_Box_Function[i_index].i_parent &&
5387
341k
             p_father && p_father->i_type != MP4_Box_Function[i_index].i_parent )
5388
337k
            continue;
5389
5390
100k
        if( ( MP4_Box_Function[i_index].i_type == p_box->i_type )||
5391
99.8k
            ( MP4_Box_Function[i_index].i_type == 0 ) )
5392
1.96k
        {
5393
1.96k
            break;
5394
1.96k
        }
5395
100k
    }
5396
5397
1.96k
    if( !(MP4_Box_Function[i_index].MP4_ReadBox_function)( p_stream, p_box ) )
5398
11
    {
5399
11
        return VLC_EGENERIC;
5400
11
    }
5401
5402
1.94k
    return VLC_SUCCESS;
5403
1.96k
}
5404
5405
static MP4_Box_t *MP4_ReadBoxAllocateCheck( stream_t *p_stream, MP4_Box_t *p_father )
5406
126
{
5407
126
    MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) ); /* Needed to ensure simple on error handler */
5408
126
    if( p_box == NULL )
5409
0
        return NULL;
5410
5411
126
    if( !MP4_PeekBoxHeader( p_stream, p_box ) )
5412
0
    {
5413
0
        msg_Warn( p_stream, "cannot read one box" );
5414
0
        free( p_box );
5415
0
        return NULL;
5416
0
    }
5417
5418
126
    if( p_father && p_father->i_size > 0 &&
5419
126
        p_father->i_pos + p_father->i_size < p_box->i_pos + p_box->i_size )
5420
1
    {
5421
1
        msg_Dbg( p_stream, "out of bound child" );
5422
1
        free( p_box );
5423
1
        return NULL;
5424
1
    }
5425
5426
125
    if( !p_box->i_size )
5427
0
    {
5428
0
        msg_Dbg( p_stream, "found an empty box (null size)" );
5429
0
        free( p_box );
5430
0
        return NULL;
5431
0
    }
5432
125
    p_box->p_father = p_father;
5433
5434
125
    return p_box;
5435
125
}
5436
5437
/*****************************************************************************
5438
 * MP4_ReadBoxUsing : parse the actual box and the children using handler
5439
 *****************************************************************************/
5440
static MP4_Box_t *MP4_ReadBoxUsing( stream_t *p_stream, MP4_Box_t *p_father,
5441
                                    int(*MP4_ReadBox_function)(stream_t *, MP4_Box_t *) )
5442
25
{
5443
25
    MP4_Box_t *p_box = MP4_ReadBoxAllocateCheck( p_stream, p_father );
5444
25
    if( !p_box )
5445
0
        return NULL;
5446
5447
25
    if( MP4_ReadBox_function( p_stream, p_box ) != 1 )
5448
0
    {
5449
0
        uint64_t i_end = p_box->i_pos + p_box->i_size;
5450
0
        MP4_BoxFree( p_box );
5451
0
        MP4_Seek( p_stream, i_end ); /* Skip the failed box */
5452
0
        return NULL;
5453
0
    }
5454
25
    return p_box;
5455
25
}
5456
5457
/*****************************************************************************
5458
 * MP4_ReadBox : parse the actual box and the children
5459
 *  XXX : Do not go to the next box
5460
 *****************************************************************************/
5461
static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father )
5462
101
{
5463
101
    MP4_Box_t *p_box = MP4_ReadBoxAllocateCheck( p_stream, p_father );
5464
101
    if( !p_box )
5465
1
        return NULL;
5466
5467
100
    if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
5468
0
    {
5469
0
        uint64_t i_end = p_box->i_pos + p_box->i_size;
5470
0
        MP4_BoxFree( p_box );
5471
0
        MP4_Seek( p_stream, i_end ); /* Skip the failed box */
5472
0
        return NULL;
5473
0
    }
5474
100
    return p_box;
5475
100
}
5476
5477
/*****************************************************************************
5478
 * MP4_BoxNew : creates and initializes an arbitrary box
5479
 *****************************************************************************/
5480
MP4_Box_t * MP4_BoxNew( uint32_t i_type )
5481
184
{
5482
184
    MP4_Box_t *p_box = calloc( 1, sizeof( MP4_Box_t ) );
5483
184
    if( likely( p_box != NULL ) )
5484
184
    {
5485
184
        p_box->i_type = i_type;
5486
184
    }
5487
184
    return p_box;
5488
184
}
5489
5490
/*****************************************************************************
5491
 * MP4_FreeBox : free memory after read with MP4_ReadBox and all
5492
 * the children
5493
 *****************************************************************************/
5494
void MP4_BoxFree( MP4_Box_t *p_box )
5495
2.16k
{
5496
2.16k
    MP4_Box_t    *p_child;
5497
5498
2.16k
    if( !p_box )
5499
0
        return; /* hehe */
5500
5501
4.14k
    for( p_child = p_box->p_first; p_child != NULL; )
5502
1.97k
    {
5503
1.97k
        MP4_Box_t *p_next;
5504
5505
1.97k
        p_next = p_child->p_next;
5506
1.97k
        MP4_BoxFree( p_child );
5507
1.97k
        p_child = p_next;
5508
1.97k
    }
5509
5510
2.16k
    if( p_box->pf_free )
5511
486
        p_box->pf_free( p_box );
5512
5513
2.16k
    free( p_box->data.p_payload );
5514
2.16k
    free( p_box );
5515
2.16k
}
5516
5517
MP4_Box_t *MP4_BoxGetNextChunk( stream_t *s )
5518
0
{
5519
    /* p_chunk is a virtual root container for the moof and mdat boxes */
5520
0
    MP4_Box_t *p_fakeroot;
5521
0
    MP4_Box_t *p_tmp_box;
5522
5523
0
    p_fakeroot = MP4_BoxNew( ATOM_root );
5524
0
    if( unlikely( p_fakeroot == NULL ) )
5525
0
        return NULL;
5526
0
    p_fakeroot->i_shortsize = 1;
5527
5528
0
    const uint32_t stoplist[] = { ATOM_moov, ATOM_moof, 0 };
5529
0
    MP4_ReadBoxContainerChildren( s, p_fakeroot, stoplist );
5530
5531
0
    p_tmp_box = p_fakeroot->p_first;
5532
0
    if( p_tmp_box == NULL )
5533
0
    {
5534
0
        MP4_BoxFree( p_fakeroot );
5535
0
        return NULL;
5536
0
    }
5537
0
    else while( p_tmp_box )
5538
0
    {
5539
0
        p_fakeroot->i_size += p_tmp_box->i_size;
5540
0
        p_tmp_box = p_tmp_box->p_next;
5541
0
    }
5542
5543
0
    return p_fakeroot;
5544
0
}
5545
5546
/*****************************************************************************
5547
 * MP4_BoxGetRoot : Parse the entire file, and create all boxes in memory
5548
 *****************************************************************************
5549
 *  The first box is a virtual box "root" and is the father for all first
5550
 *  level boxes for the file, a sort of virtual container
5551
 *****************************************************************************/
5552
MP4_Box_t *MP4_BoxGetRoot( stream_t *p_stream )
5553
184
{
5554
184
    int i_result;
5555
5556
184
    MP4_Box_t *p_vroot = MP4_BoxNew( ATOM_root );
5557
184
    if( p_vroot == NULL )
5558
0
        return NULL;
5559
5560
184
    p_vroot->i_shortsize = 1;
5561
184
    uint64_t i_size;
5562
184
    if( vlc_stream_GetSize( p_stream, &i_size ) == 0 )
5563
184
        p_vroot->i_size = i_size;
5564
5565
    /* First get the moov */
5566
184
    {
5567
184
        const uint32_t stoplist[] = { ATOM_moov, ATOM_mdat, 0 };
5568
184
        i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
5569
184
    }
5570
5571
    /* mdat appeared first */
5572
184
    if( i_result && !MP4_BoxGet( p_vroot, "moov" ) )
5573
178
    {
5574
178
        bool b_seekable;
5575
178
        if( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_seekable ) != VLC_SUCCESS || !b_seekable )
5576
0
        {
5577
0
            msg_Err( p_stream, "no moov before mdat and the stream is not seekable" );
5578
0
            goto error;
5579
0
        }
5580
5581
        /* continue loading up to moov */
5582
178
        const uint32_t stoplist[] = { ATOM_moov, 0 };
5583
178
        i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, stoplist );
5584
178
    }
5585
5586
184
    if( !i_result )
5587
176
        return p_vroot;
5588
5589
    /* If there is a mvex box, it means fragmented MP4, and we're done */
5590
8
    if( MP4_BoxCount( p_vroot, "moov/mvex" ) > 0 )
5591
0
    {
5592
        /* Read a bit more atoms as we might have an index between moov and moof */
5593
0
        const uint32_t stoplist[] = { ATOM_sidx, 0 };
5594
0
        const uint32_t excludelist[] = { ATOM_moof, ATOM_mdat, 0 };
5595
0
        MP4_ReadBoxContainerChildrenIndexed( p_stream, p_vroot, stoplist, excludelist, false );
5596
0
        return p_vroot;
5597
0
    }
5598
5599
8
    if( vlc_stream_Tell( p_stream ) + 8 < i_size )
5600
5
    {
5601
        /* Get the rest of the file */
5602
5
        i_result = MP4_ReadBoxContainerChildren( p_stream, p_vroot, NULL );
5603
5604
5
        if( !i_result )
5605
0
            goto error;
5606
5
    }
5607
5608
8
    MP4_Box_t *p_cmov;
5609
    /* check if there is a cmov, if so replace
5610
      compressed moov by  uncompressed one */
5611
8
    if( ( p_cmov = MP4_BoxGet( p_vroot, "moov/cmov" ) ) ||
5612
8
        ( p_cmov = MP4_BoxGet( p_vroot, "foov/cmov" ) ) )
5613
0
    {
5614
0
        MP4_Box_t *p_moov = MP4_BoxExtract( &p_vroot->p_first, p_cmov->p_father->i_type );
5615
        /* get uncompressed p_moov */
5616
0
        MP4_Box_t *p_umoov = MP4_BoxExtract( &p_cmov->p_first, ATOM_moov );
5617
        /* make p_root father of this new moov */
5618
0
        MP4_BoxAddChild( p_vroot, p_umoov );
5619
        /* Release old moov and compressed info */
5620
0
        MP4_BoxFree( p_moov );
5621
0
    }
5622
5623
8
    return p_vroot;
5624
5625
0
error:
5626
0
    MP4_BoxFree( p_vroot );
5627
0
    MP4_Seek( p_stream, 0 );
5628
0
    return NULL;
5629
8
}
5630
5631
5632
static void MP4_BoxDumpStructure_Internal( vlc_object_t *obj, const MP4_Box_t *p_box,
5633
                                           unsigned int i_level )
5634
2.15k
{
5635
2.15k
    const MP4_Box_t *p_child;
5636
2.15k
    uint32_t i_displayedtype = p_box->i_type;
5637
2.15k
    if( ! MP4_BOX_TYPE_ASCII() ) ((char*)&i_displayedtype)[0] = 'c';
5638
5639
2.15k
    if( !i_level )
5640
184
    {
5641
184
        msg_Dbg( obj, "dumping root Box \"%4.4s\"",
5642
184
                          (char*)&i_displayedtype );
5643
184
    }
5644
1.97k
    else
5645
1.97k
    {
5646
1.97k
        char str[512];
5647
1.97k
        if( i_level >= (sizeof(str) - 1)/4 )
5648
0
            return;
5649
5650
1.97k
        memset( str, ' ', sizeof(str) );
5651
5.51k
        for( unsigned i = 0; i < i_level; i++ )
5652
3.53k
        {
5653
3.53k
            str[i*4] = '|';
5654
3.53k
        }
5655
5656
1.97k
        snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
5657
1.97k
                  "+ %4.4s size %"PRIu64" offset %"PRIu64"%s",
5658
1.97k
                  (char *)&i_displayedtype, p_box->i_size, p_box->i_pos,
5659
1.97k
                  p_box->e_flags & BOX_FLAG_INCOMPLETE ? " (\?\?\?\?)" : "" );
5660
1.97k
        msg_Dbg( obj, "%s", str );
5661
1.97k
    }
5662
2.15k
    p_child = p_box->p_first;
5663
4.13k
    while( p_child )
5664
1.97k
    {
5665
1.97k
        MP4_BoxDumpStructure_Internal( obj, p_child, i_level + 1 );
5666
1.97k
        p_child = p_child->p_next;
5667
1.97k
    }
5668
2.15k
}
5669
5670
void MP4_BoxDumpStructure( vlc_object_t *obj, const MP4_Box_t *p_box )
5671
184
{
5672
184
    MP4_BoxDumpStructure_Internal( obj, p_box, 0 );
5673
184
}
5674
5675
5676
/*****************************************************************************
5677
 *****************************************************************************
5678
 **
5679
 **  High level methods to access an MP4 file
5680
 **
5681
 *****************************************************************************
5682
 *****************************************************************************/
5683
static bool get_token( const char **ppsz_path, char **ppsz_token, unsigned *pi_number )
5684
1.95k
{
5685
1.95k
    size_t i_len ;
5686
1.95k
    if( !*ppsz_path[0] )
5687
347
    {
5688
347
        *ppsz_token = NULL;
5689
347
        *pi_number = 0;
5690
347
        return true;
5691
347
    }
5692
1.60k
    i_len = strcspn( *ppsz_path, "/[" );
5693
1.60k
    if( !i_len && **ppsz_path == '/' )
5694
18
    {
5695
18
        i_len = 1;
5696
18
    }
5697
1.60k
    *ppsz_token = strndup( *ppsz_path, i_len );
5698
1.60k
    if( unlikely(!*ppsz_token) )
5699
0
        return false;
5700
5701
1.60k
    *ppsz_path += i_len;
5702
5703
    /* Parse the token number token[n] */
5704
1.60k
    if( **ppsz_path == '[' )
5705
80
    {
5706
80
        (*ppsz_path)++;
5707
80
        char *endptr = NULL;
5708
80
        *pi_number = strtoul( *ppsz_path, &endptr, 10 );
5709
80
        if( endptr == *ppsz_path || *endptr != ']' )
5710
0
        {
5711
0
            free(*ppsz_token);
5712
0
            return false;
5713
0
        }
5714
80
        *ppsz_path = endptr + 1;
5715
80
    }
5716
1.52k
    else
5717
1.52k
    {
5718
1.52k
        *pi_number = 0;
5719
1.52k
    }
5720
5721
    /* Forward to start of next token */
5722
2.58k
    while( **ppsz_path == '/' )
5723
982
    {
5724
982
        (*ppsz_path)++;
5725
982
    }
5726
5727
1.60k
    return true;
5728
1.60k
}
5729
5730
static const MP4_Box_t * MP4_BoxGet_Path( const MP4_Box_t *p_box, const char *psz_path)
5731
933
{
5732
933
    assert( psz_path && psz_path[0] );
5733
5734
//    fprintf( stderr, "path:'%s'\n", psz_path );
5735
2.53k
    for( ; p_box ; )
5736
1.95k
    {
5737
1.95k
        unsigned i_number;
5738
1.95k
        char *psz_token = NULL;
5739
5740
1.95k
        if( !get_token( &psz_path, &psz_token, &i_number ) )
5741
0
        {
5742
0
            p_box = NULL;
5743
0
            break;
5744
0
        }
5745
//        fprintf( stderr, "path:'%s', token:'%s' n:%d\n",
5746
//                 psz_path,psz_token,i_number );
5747
1.95k
        if( !psz_token )
5748
347
            break;
5749
5750
1.60k
        if( !strcmp( psz_token, "/" ) )
5751
18
        {
5752
            /* Find root box */
5753
18
            while( p_box && p_box->i_type != ATOM_root )
5754
0
                p_box = p_box->p_father;
5755
18
        }
5756
1.58k
        else
5757
1.58k
        if( !strcmp( psz_token, "." ) )
5758
0
        {
5759
            /* Do nothing */
5760
0
        }
5761
1.58k
        else
5762
1.58k
        if( !strcmp( psz_token, ".." ) )
5763
3
        {
5764
3
            p_box = p_box->p_father;
5765
3
        }
5766
1.58k
        else
5767
1.58k
        {
5768
1.58k
            size_t len = strlen( psz_token );
5769
1.58k
            if( len != 0 && len != 4 )
5770
0
            {
5771
//            fprintf( stderr, "Argg malformed token \"%s\"",psz_token );
5772
0
              free( psz_token );
5773
0
              p_box = NULL;
5774
0
              break;
5775
0
            }
5776
5777
1.58k
            uint32_t i_fourcc = 0 ;
5778
1.58k
            if( len == 4 )
5779
1.50k
                i_fourcc = VLC_FOURCC( psz_token[0], psz_token[1],
5780
1.58k
                                       psz_token[2], psz_token[3] );
5781
5782
6.91k
            for( p_box = p_box->p_first; p_box; p_box = p_box->p_next )
5783
6.32k
            {
5784
6.32k
                if( i_fourcc && p_box->i_type != i_fourcc )
5785
5.20k
                    continue;
5786
1.12k
                if( i_number == 0 )
5787
997
                    break;
5788
127
                i_number--;
5789
127
            }
5790
1.58k
        }
5791
5792
1.60k
        free( psz_token );
5793
1.60k
    }
5794
5795
933
    return p_box;
5796
933
}
5797
5798
static const MP4_Box_t * MP4_BoxGet_Internal( const MP4_Box_t *p_box,
5799
                                 const char *psz_fmt, va_list args)
5800
80
{
5801
80
    char *psz_path;
5802
5803
80
    if( !p_box )
5804
0
        return NULL;
5805
5806
80
    if( vasprintf( &psz_path, psz_fmt, args ) == -1 )
5807
0
        psz_path = NULL;
5808
5809
80
    if( !psz_path || !psz_path[0] )
5810
0
    {
5811
0
        free( psz_path );
5812
0
        return NULL;
5813
0
    }
5814
5815
80
    const MP4_Box_t *p_result = MP4_BoxGet_Path( p_box, psz_path );
5816
80
    free( psz_path );
5817
80
    return p_result;
5818
80
}
5819
5820
/*****************************************************************************
5821
 * MP4_BoxGet: find a box given a path relative to p_box
5822
 *****************************************************************************
5823
 * Path Format: . .. / as usual
5824
 *              [number] to specifie box number ex: trak[12]
5825
 *
5826
 * ex: /moov/trak[12]
5827
 *     ../mdia
5828
 *****************************************************************************/
5829
MP4_Box_t *MP4_BoxGetVa( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5830
80
{
5831
80
    va_list args;
5832
80
    const MP4_Box_t *p_result;
5833
5834
80
    va_start( args, psz_fmt );
5835
80
    p_result = MP4_BoxGet_Internal( p_box, psz_fmt, args );
5836
80
    va_end( args );
5837
5838
80
    return( (MP4_Box_t *) p_result );
5839
80
}
5840
5841
MP4_Box_t *MP4_BoxGet( const MP4_Box_t *p_box, const char *psz_fmt )
5842
844
{
5843
844
    return (MP4_Box_t *) MP4_BoxGet_Path( p_box, psz_fmt );
5844
844
}
5845
5846
/*****************************************************************************
5847
 * MP4_BoxCount: count box given a path relative to p_box
5848
 *****************************************************************************
5849
 * Path Format: . .. / as usual
5850
 *              [number] to specifie box number ex: trak[12]
5851
 *
5852
 * ex: /moov/trak[12]
5853
 *     ../mdia
5854
 *****************************************************************************/
5855
unsigned MP4_BoxCountVa( const MP4_Box_t *p_box, const char *psz_fmt, ... )
5856
0
{
5857
0
    va_list args;
5858
0
    unsigned i_count;
5859
0
    const MP4_Box_t *p_result, *p_next;
5860
5861
0
    va_start( args, psz_fmt );
5862
0
    p_result = MP4_BoxGet_Internal( p_box, psz_fmt, args );
5863
0
    va_end( args );
5864
0
    if( !p_result )
5865
0
    {
5866
0
        return( 0 );
5867
0
    }
5868
5869
0
    i_count = 1;
5870
0
    for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
5871
0
    {
5872
0
        if( p_next->i_type == p_result->i_type)
5873
0
        {
5874
0
            i_count++;
5875
0
        }
5876
0
    }
5877
0
    return( i_count );
5878
0
}
5879
5880
unsigned MP4_BoxCount( const MP4_Box_t *p_box, const char *psz_fmt )
5881
9
{
5882
9
    unsigned i_count;
5883
9
    const MP4_Box_t *p_result, *p_next;
5884
5885
9
    p_result = MP4_BoxGet_Path( p_box, psz_fmt );
5886
9
    if( !p_result )
5887
8
    {
5888
8
        return( 0 );
5889
8
    }
5890
5891
1
    i_count = 1;
5892
1
    for( p_next = p_result->p_next; p_next != NULL; p_next = p_next->p_next)
5893
0
    {
5894
0
        if( p_next->i_type == p_result->i_type)
5895
0
        {
5896
0
            i_count++;
5897
0
        }
5898
0
    }
5899
1
    return( i_count );
5900
9
}