Coverage Report

Created: 2025-07-11 07:16

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