libmp4.c 125 KB
Newer Older
1
2
3
/*****************************************************************************
 * libmp4.c : LibMP4 library for mp4 module for vlc
 *****************************************************************************
4
 * Copyright (C) 2001-2004, 2010 the VideoLAN team
5
6
 *
 * Author: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8
9
10
11
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
12
 *
13
14
15
16
17
18
19
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22

23
24
25
26
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

27
#include <vlc_common.h>
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
28
#include <vlc_stream.h>                               /* stream_Peek*/
29

30
#ifdef HAVE_ZLIB_H
31
#   include <zlib.h>                                  /* for compressed moov */
32
33
#endif

34
#include "libmp4.h"
gnarula's avatar
gnarula committed
35
#include <math.h>
36
37

/* Some assumptions:
38
 * The input method HAS to be seekable
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
39
 */
40

gnarula's avatar
gnarula committed
41
42
43
44
45
46
47
/* convert 16.16 fixed point to floating point */
static double conv_fx( int32_t fx ) {
    double fp = fx;
    fp /= 65536.;
    return fp;
}

48
/* some functions for mp4 encoding of variables */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
49
#ifdef MP4_VERBOSE
Laurent Aimar's avatar
Laurent Aimar committed
50
static void MP4_ConvertDate2Str( char *psz, uint64_t i_date )
51
52
53
54
55
56
{
    int i_day;
    int i_hour;
    int i_min;
    int i_sec;

Laurent Aimar's avatar
Laurent Aimar committed
57
    /* date begin at 1 jan 1904 */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
58
    i_date += ((INT64_C(1904) * 365) + 17) * 24 * 60 * 60;
Laurent Aimar's avatar
Laurent Aimar committed
59

60
61
62
63
    i_day = i_date / ( 60*60*24);
    i_hour = ( i_date /( 60*60 ) ) % 60;
    i_min  = ( i_date / 60 ) % 60;
    i_sec =  i_date % 60;
64
    sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds", i_day, i_hour, i_min, i_sec );
65
}
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
66
#endif
67

Laurent Aimar's avatar
Laurent Aimar committed
68
69
70
/*****************************************************************************
 * Some prototypes.
 *****************************************************************************/
sigmunau's avatar
sigmunau committed
71
static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
72
73
74


/*****************************************************************************
75
 * MP4_ReadBoxCommon : Load only common parameters for all boxes
76
 *****************************************************************************
77
 * p_box need to be an already allocated MP4_Box_t, and all data
78
79
80
81
 *  will only be peek not read
 *
 * RETURN : 0 if it fail, 1 otherwise
 *****************************************************************************/
sigmunau's avatar
sigmunau committed
82
int MP4_ReadBoxCommon( stream_t *p_stream, MP4_Box_t *p_box )
83
{
84
    int      i_read;
85
    const uint8_t  *p_peek;
86

sigmunau's avatar
sigmunau committed
87
    if( ( ( i_read = stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
88
    {
89
        return 0;
90
    }
sigmunau's avatar
sigmunau committed
91
    p_box->i_pos = stream_Tell( p_stream );
92
93
94
95
96
97

    p_box->data.p_data = NULL;
    p_box->p_father = NULL;
    p_box->p_first  = NULL;
    p_box->p_last  = NULL;
    p_box->p_next   = NULL;
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    MP4_GET4BYTES( p_box->i_shortsize );
    MP4_GETFOURCC( p_box->i_type );

    /* Now special case */

    if( p_box->i_shortsize == 1 )
    {
        /* get the true size on 64 bits */
        MP4_GET8BYTES( p_box->i_size );
    }
    else
    {
        p_box->i_size = p_box->i_shortsize;
        /* XXX size of 0 means that the box extends to end of file */
    }
114

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
115
    if( p_box->i_type == ATOM_uuid )
116
117
118
119
120
121
122
123
124
125
126
127
    {
        /* get extented type on 16 bytes */
        GetUUID( &p_box->i_uuid, p_peek );
        p_peek += 16; i_read -= 16;
    }
    else
    {
        CreateUUID( &p_box->i_uuid, p_box->i_type );
    }
#ifdef MP4_VERBOSE
    if( p_box->i_size )
    {
128
        if MP4_BOX_TYPE_ASCII()
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
129
            msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64,
130
131
                    (char*)&p_box->i_type, p_box->i_size );
        else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
132
            msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
133
                    (char*)&p_box->i_type+1, p_box->i_size );
134
135
136
    }
#endif

137
    return 1;
138
139
140
}

/*****************************************************************************
141
 * MP4_NextBox : Go to the next box
142
 *****************************************************************************
143
 * if p_box == NULL, go to the next box in which we are( at the begining ).
144
 *****************************************************************************/
sigmunau's avatar
sigmunau committed
145
static int MP4_NextBox( stream_t *p_stream, MP4_Box_t *p_box )
146
147
148
149
150
151
152
153
154
155
156
{
    MP4_Box_t box;

    if( !p_box )
    {
        MP4_ReadBoxCommon( p_stream, &box );
        p_box = &box;
    }

    if( !p_box->i_size )
    {
157
        return 2; /* Box with infinite size */
158
159
160
161
    }

    if( p_box->p_father )
    {
162
163
164
        /* if father's size == 0, it means unknown or infinite size,
         * and we skip the followong check */
        if( p_box->p_father->i_size > 0 )
165
        {
166
167
168
169
170
171
172
173
174
175
            const off_t i_box_end = p_box->i_size + p_box->i_pos;
            const off_t i_father_end = p_box->p_father->i_size + p_box->p_father->i_pos;

            /* check if it's within p-father */
            if( i_box_end >= i_father_end )
            {
                if( i_box_end > i_father_end )
                    msg_Dbg( p_stream, "out of bound child" );
                return 0; /* out of bound */
            }
176
177
        }
    }
sigmunau's avatar
sigmunau committed
178
    if( stream_Seek( p_stream, p_box->i_size + p_box->i_pos ) )
179
180
181
182
183
    {
        return 0;
    }

    return 1;
184
185
186
}

/*****************************************************************************
187
 * For all known box a loader is given,
188
189
 *  XXX: all common struct have to be already read by MP4_ReadBoxCommon
 *       after called one of theses functions, file position is unknown
190
 *       you need to call MP4_GotoBox to go where you want
191
 *****************************************************************************/
192
193
static int MP4_ReadBoxContainerChildren( stream_t *p_stream,
                                    MP4_Box_t *p_container, uint32_t i_last_child )
194
195
{
    MP4_Box_t *p_box;
196

197
198
199
200
    /* Size of root container is set to 0 when unknown, for exemple
     * with a DASH stream. In that case, we skip the following check */
    if( p_container->i_size
            && ( stream_Tell( p_stream ) + 8 >
201
        (off_t)(p_container->i_pos + p_container->i_size) )
202
      )
203
204
    {
        /* there is no box to load */
205
        return 0;
206
    }
207

208
209
    do
    {
210
211
        if( ( p_box = MP4_ReadBox( p_stream, p_container ) ) == NULL ) break;

Laurent Aimar's avatar
Laurent Aimar committed
212
        /* chain this box with the father and the other at same level */
213
214
        if( !p_container->p_first ) p_container->p_first = p_box;
        else p_container->p_last->p_next = p_box;
Laurent Aimar's avatar
Laurent Aimar committed
215
        p_container->p_last = p_box;
216

217
218
219
        if( p_box->i_type == i_last_child )
            break;

Laurent Aimar's avatar
Laurent Aimar committed
220
    } while( MP4_NextBox( p_stream, p_box ) == 1 );
221

222
    return 1;
223
224
}

225
226
227
228
229
static int MP4_ReadBoxContainerRaw( stream_t *p_stream, MP4_Box_t *p_container )
{
    return MP4_ReadBoxContainerChildren( p_stream, p_container, 0 );
}

sigmunau's avatar
sigmunau committed
230
static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
231
{
232
233
    if( p_container->i_size &&
        ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
234
235
    {
        /* container is empty, 8 stand for the first header in this box */
236
        return 1;
237
    }
238

239
    /* enter box */
240
    stream_Seek( p_stream, p_container->i_pos +
241
                 mp4_box_headersize( p_container ) );
242

243
    return MP4_ReadBoxContainerRaw( p_stream, p_container );
244
245
}

246
static void MP4_FreeBox_Common( MP4_Box_t *p_box )
247
248
{
    /* Up to now do nothing */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
249
    (void)p_box;
250
251
}

sigmunau's avatar
sigmunau committed
252
static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
253
{
254
    /* XXX sometime moov is hiden in a free box */
255
    if( p_box->p_father &&
256
        p_box->p_father->i_type == ATOM_root &&
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
257
        p_box->i_type == ATOM_free )
258
    {
259
        const uint8_t *p_peek;
260
261
262
        int     i_read;
        vlc_fourcc_t i_fcc;

sigmunau's avatar
sigmunau committed
263
        i_read  = stream_Peek( p_stream, &p_peek, 44 );
264

265
266
        p_peek += mp4_box_headersize( p_box ) + 4;
        i_read -= mp4_box_headersize( p_box ) + 4;
267
268
269
270
271

        if( i_read >= 8 )
        {
            i_fcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
272
            if( i_fcc == ATOM_cmov || i_fcc == ATOM_mvhd )
273
            {
sigmunau's avatar
sigmunau committed
274
                msg_Warn( p_stream, "detected moov hidden in a free box ..." );
275

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
276
                p_box->i_type = ATOM_foov;
277
278
279
280
                return MP4_ReadBoxContainer( p_stream, p_box );
            }
        }
    }
281

282
283
    /* Nothing to do */
#ifdef MP4_VERBOSE
284
285
286
287
    if MP4_BOX_TYPE_ASCII()
        msg_Dbg( p_stream, "skip box: \"%4.4s\"", (char*)&p_box->i_type );
    else
        msg_Dbg( p_stream, "skip box: \"c%3.3s\"", (char*)&p_box->i_type+1 );
288
#endif
289
    return 1;
290
291
}

sigmunau's avatar
sigmunau committed
292
static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
293
294
{
    MP4_READBOX_ENTER( MP4_Box_data_ftyp_t );
295

296
297
    MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
    MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
298

299
300
    if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
301
302
303
304
        uint32_t *tab = p_box->data.p_ftyp->i_compatible_brands =
            calloc( p_box->data.p_ftyp->i_compatible_brands_count,
                    sizeof(uint32_t));

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
305
        if( unlikely( tab == NULL ) )
306
            MP4_READBOX_EXIT( 0 );
307

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
308
        for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
309
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
310
            MP4_GETFOURCC( tab[i] );
311
312
313
314
315
316
317
        }
    }
    else
    {
        p_box->data.p_ftyp->i_compatible_brands = NULL;
    }

318
    MP4_READBOX_EXIT( 1 );
319
320
}

321
static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
322
{
zorglub's avatar
zorglub committed
323
    FREENULL( p_box->data.p_ftyp->i_compatible_brands );
324
325
326
}


sigmunau's avatar
sigmunau committed
327
static int MP4_ReadBox_mvhd(  stream_t *p_stream, MP4_Box_t *p_box )
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
{
#ifdef MP4_VERBOSE
    char s_creation_time[128];
    char s_modification_time[128];
    char s_duration[128];
#endif
    MP4_READBOX_ENTER( MP4_Box_data_mvhd_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );

    if( p_box->data.p_mvhd->i_version )
    {
        MP4_GET8BYTES( p_box->data.p_mvhd->i_creation_time );
        MP4_GET8BYTES( p_box->data.p_mvhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
        MP4_GET8BYTES( p_box->data.p_mvhd->i_duration );
    }
    else
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_creation_time );
        MP4_GET4BYTES( p_box->data.p_mvhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_mvhd->i_timescale );
        MP4_GET4BYTES( p_box->data.p_mvhd->i_duration );
    }
352
353
    MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
    MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
354
355
    MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );

356

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
357
    for( unsigned i = 0; i < 2; i++ )
358
359
360
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
361
    for( unsigned i = 0; i < 9; i++ )
362
363
364
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
365
    for( unsigned i = 0; i < 6; i++ )
366
367
368
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
    }
369

370
    MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
371
372


373
374
#ifdef MP4_VERBOSE
    MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
375
    MP4_ConvertDate2Str( s_modification_time,
376
377
378
                         p_box->data.p_mvhd->i_modification_time );
    if( p_box->data.p_mvhd->i_rate )
    {
379
        MP4_ConvertDate2Str( s_duration,
380
381
382
383
384
                 p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_rate );
    }
    else
    {
        s_duration[0] = 0;
385
    }
sigmunau's avatar
sigmunau committed
386
    msg_Dbg( p_stream, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
387
388
                  s_creation_time,
                  s_modification_time,
389
                  (uint32_t)p_box->data.p_mvhd->i_timescale,
390
391
392
                  s_duration,
                  (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
                  (float)p_box->data.p_mvhd->i_volume / 256 ,
393
                  (uint32_t)p_box->data.p_mvhd->i_next_track_id );
394
395
396
397
#endif
    MP4_READBOX_EXIT( 1 );
}

398
399
400
401
static int MP4_ReadBox_mfhd(  stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_mfhd_t );

F. Yhuel's avatar
F. Yhuel committed
402
403
    MP4_GETVERSIONFLAGS( p_box->data.p_mvhd );

404
405
406
407
408
409
410
411
412
    MP4_GET4BYTES( p_box->data.p_mfhd->i_sequence_number );

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream, "read box: \"mfhd\" sequence number %d",
                  p_box->data.p_mfhd->i_sequence_number );
#endif
    MP4_READBOX_EXIT( 1 );
}

413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
static int MP4_ReadBox_tfxd(  stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_tfxd_t );

    MP4_Box_data_tfxd_t *p_tfxd_data = p_box->data.p_tfxd;
    MP4_GETVERSIONFLAGS( p_tfxd_data );

    if( p_tfxd_data->i_version == 0 )
    {
        MP4_GET4BYTES( p_tfxd_data->i_fragment_abs_time );
        MP4_GET4BYTES( p_tfxd_data->i_fragment_duration );
    }
    else
    {
        MP4_GET8BYTES( p_tfxd_data->i_fragment_abs_time );
        MP4_GET8BYTES( p_tfxd_data->i_fragment_duration );
    }

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream, "read box: \"tfxd\" version %d, flags 0x%x, "\
            "fragment duration %"PRIu64", fragment abs time %"PRIu64,
                p_tfxd_data->i_version,
                p_tfxd_data->i_flags,
                p_tfxd_data->i_fragment_duration,
                p_tfxd_data->i_fragment_abs_time
           );
#endif

    MP4_READBOX_EXIT( 1 );
}

static int MP4_ReadBox_tfrf(  stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_tfxd_t );

    MP4_Box_data_tfrf_t *p_tfrf_data = p_box->data.p_tfrf;
    MP4_GETVERSIONFLAGS( p_tfrf_data );

    MP4_GET1BYTE( p_tfrf_data->i_fragment_count );

    p_tfrf_data->p_tfrf_data_fields = calloc( p_tfrf_data->i_fragment_count,
                                              sizeof( TfrfBoxDataFields_t ) );
    if( !p_tfrf_data->p_tfrf_data_fields )
        MP4_READBOX_EXIT( 0 );

    for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
    {
        TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
        if( p_tfrf_data->i_version == 0 )
        {
            MP4_GET4BYTES( TfrfBoxDataField->i_fragment_abs_time );
            MP4_GET4BYTES( TfrfBoxDataField->i_fragment_duration );
        }
        else
        {
            MP4_GET8BYTES( TfrfBoxDataField->i_fragment_abs_time );
            MP4_GET8BYTES( TfrfBoxDataField->i_fragment_duration );
        }
    }

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream, "read box: \"tfrf\" version %d, flags 0x%x, "\
            "fragment count %"PRIu8, p_tfrf_data->i_version,
                p_tfrf_data->i_flags, p_tfrf_data->i_fragment_count );

    for( uint8_t i = 0; i < p_tfrf_data->i_fragment_count; i++ )
    {
        TfrfBoxDataFields_t *TfrfBoxDataField = &p_tfrf_data->p_tfrf_data_fields[i];
        msg_Dbg( p_stream, "\"tfrf\" fragment duration %"PRIu64", "\
                                    "fragment abs time %"PRIu64,
                    TfrfBoxDataField->i_fragment_duration,
                    TfrfBoxDataField->i_fragment_abs_time );
    }

#endif

    MP4_READBOX_EXIT( 1 );
}

static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
{
    FREENULL( p_box->data.p_tfrf->p_tfrf_data_fields );
}

497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
static int MP4_ReadBox_stra( stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_stra_t );
    MP4_Box_data_stra_t *p_stra = p_box->data.p_stra;

    uint8_t i_reserved;
    MP4_GET1BYTE( p_stra->i_es_cat );
    MP4_GET1BYTE( i_reserved );
    MP4_GET2BYTES( p_stra->i_track_ID );

    MP4_GET4BYTES( p_stra->i_timescale );
    MP4_GET8BYTES( p_stra->i_duration );

    MP4_GET4BYTES( p_stra->FourCC );
    MP4_GET4BYTES( p_stra->Bitrate );
    MP4_GET4BYTES( p_stra->MaxWidth );
    MP4_GET4BYTES( p_stra->MaxHeight );
    MP4_GET4BYTES( p_stra->SamplingRate );
    MP4_GET4BYTES( p_stra->Channels );
    MP4_GET4BYTES( p_stra->BitsPerSample );
    MP4_GET4BYTES( p_stra->AudioTag );
    MP4_GET2BYTES( p_stra->nBlockAlign );

    MP4_GET1BYTE( i_reserved );
    MP4_GET1BYTE( i_reserved );
    MP4_GET1BYTE( i_reserved );
523
524
525
526
    MP4_GET1BYTE( p_stra->cpd_len );
    if( p_stra->cpd_len > i_read )
        goto error;
    p_stra->CodecPrivateData = malloc( p_stra->cpd_len );
527
528
    if( unlikely( p_stra->CodecPrivateData == NULL ) )
        goto error;
529
    memcpy( p_stra->CodecPrivateData, p_peek, p_stra->cpd_len );
530
531

#ifdef MP4_VERBOSE
532
533
    msg_Dbg( p_stream, "es_cat is %"PRIu8", birate is %"PRIu32,
              p_stra->i_es_cat, p_stra->Bitrate );
534
535
536
537
538
539
540
541
542
543
544
545
#endif

    MP4_READBOX_EXIT( 1 );
error:
    MP4_READBOX_EXIT( 0 );
}

static void MP4_FreeBox_stra( MP4_Box_t *p_box )
{
    FREENULL( p_box->data.p_stra->CodecPrivateData );
}

546
547
548
549
550
551
static int MP4_ReadBox_uuid( stream_t *p_stream, MP4_Box_t *p_box )
{
    if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
        return MP4_ReadBox_tfrf( p_stream, p_box );
    if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
        return MP4_ReadBox_tfxd( p_stream, p_box );
552
553
554
555
    if( !CmpUUID( &p_box->i_uuid, &SmooBoxUUID ) )
        return MP4_ReadBoxContainer( p_stream, p_box );
    if( !CmpUUID( &p_box->i_uuid, &StraBoxUUID ) )
        return MP4_ReadBox_stra( p_stream, p_box );
556
557
558
559
560
561
562
563
564
565
566

    msg_Warn( p_stream, "Unknown uuid type box" );
    return 1;
}

static void MP4_FreeBox_uuid( MP4_Box_t *p_box )
{
    if( !CmpUUID( &p_box->i_uuid, &TfrfBoxUUID ) )
        return MP4_FreeBox_tfrf( p_box );
    if( !CmpUUID( &p_box->i_uuid, &TfxdBoxUUID ) )
        return MP4_FreeBox_Common( p_box );
567
568
569
570
    if( !CmpUUID( &p_box->i_uuid, &SmooBoxUUID ) )
        return MP4_FreeBox_Common( p_box );
    if( !CmpUUID( &p_box->i_uuid, &StraBoxUUID ) )
        return MP4_FreeBox_stra( p_box );
571
572
}

573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
static int MP4_ReadBox_sidx(  stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_sidx_t );

    MP4_Box_data_sidx_t *p_sidx_data = p_box->data.p_sidx;
    MP4_GETVERSIONFLAGS( p_sidx_data );

    MP4_GET4BYTES( p_sidx_data->i_reference_ID );
    MP4_GET4BYTES( p_sidx_data->i_timescale );

    if( p_sidx_data->i_version == 0 )
    {
        MP4_GET4BYTES( p_sidx_data->i_earliest_presentation_time );
        MP4_GET4BYTES( p_sidx_data->i_first_offset );
    }
    else
    {
        MP4_GET8BYTES( p_sidx_data->i_earliest_presentation_time );
        MP4_GET8BYTES( p_sidx_data->i_first_offset );
    }

    uint16_t i_reserved;
    MP4_GET2BYTES( i_reserved );
    MP4_GET2BYTES( p_sidx_data->i_reference_count );
    uint16_t i_count = p_sidx_data->i_reference_count;

    p_sidx_data->p_items = calloc( i_count, sizeof( MP4_Box_sidx_item_t ) );
    uint32_t tmp;
    for( unsigned i = 0; i < i_count; i++ )
    {
        MP4_GET4BYTES( tmp );
        p_sidx_data->p_items[i].b_reference_type = (bool)((tmp & 0x80000000)>>24);
        p_sidx_data->p_items[i].i_referenced_size = tmp & 0x7fffffff;
        MP4_GET4BYTES( p_sidx_data->p_items[i].i_subsegment_duration );

        MP4_GET4BYTES( tmp );
        p_sidx_data->p_items[i].b_starts_with_SAP = (bool)((tmp & 0x80000000)>>24);
        p_sidx_data->p_items[i].i_SAP_type = (tmp & 0x70000000)>>24;
        p_sidx_data->p_items[i].i_SAP_delta_time = tmp & 0xfffffff;
    }

#ifdef MP4_VERBOSE
    msg_Dbg( p_stream, "read box: \"sidx\" version %d, flags 0x%x, "\
            "ref_ID %"PRIu32", timescale %"PRIu32", ref_count %"PRIu16", "\
            "first subsegmt duration %"PRIu32,
                p_sidx_data->i_version,
                p_sidx_data->i_flags,
                p_sidx_data->i_reference_ID,
                p_sidx_data->i_timescale,
                p_sidx_data->i_reference_count,
                p_sidx_data->p_items[0].i_subsegment_duration
           );
#endif

    MP4_READBOX_EXIT( 1 );
}

static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
{
    FREENULL( p_box->data.p_sidx->p_items );
}

635
636
637
638
639
640
static int MP4_ReadBox_tfhd(  stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_tfhd_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );

Frédéric Yhuel's avatar
Frédéric Yhuel committed
641
642
643
644
645
646
647
    if( p_box->data.p_tfhd->i_version != 0 )
    {
        msg_Warn( p_stream, "'tfhd' box with version != 0. "\
                " Don't know what to do with that, please patch" );
        MP4_READBOX_EXIT( 0 );
    }

648
649
    MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );

Frédéric Yhuel's avatar
Frédéric Yhuel committed
650
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
651
    {
Frédéric Yhuel's avatar
Frédéric Yhuel committed
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
        msg_Dbg( p_stream, "'duration-is-empty' flag is present "\
                "=> no samples for this time interval." );
        p_box->data.p_tfhd->b_empty = true;
    }
    else
        p_box->data.p_tfhd->b_empty = false;

    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
        MP4_GET8BYTES( p_box->data.p_tfhd->i_base_data_offset );
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
        MP4_GET4BYTES( p_box->data.p_tfhd->i_sample_description_index );
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
        MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_duration );
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
        MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_size );
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
        MP4_GET4BYTES( p_box->data.p_tfhd->i_default_sample_flags );
669
670

#ifdef MP4_VERBOSE
Frédéric Yhuel's avatar
Frédéric Yhuel committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
    char psz_base[128] = "\0";
    char psz_desc[128] = "\0";
    char psz_dura[128] = "\0";
    char psz_size[128] = "\0";
    char psz_flag[128] = "\0";
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
        snprintf(psz_base, sizeof(psz_base), "base offset %"PRId64, p_box->data.p_tfhd->i_base_data_offset);
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_SAMPLE_DESC_INDEX )
        snprintf(psz_desc, sizeof(psz_desc), "sample description index %d", p_box->data.p_tfhd->i_sample_description_index);
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
        snprintf(psz_dura, sizeof(psz_dura), "sample duration %d", p_box->data.p_tfhd->i_default_sample_duration);
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
        snprintf(psz_size, sizeof(psz_size), "sample size %d", p_box->data.p_tfhd->i_default_sample_size);
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_FLAGS )
        snprintf(psz_flag, sizeof(psz_flag), "sample flags 0x%x", p_box->data.p_tfhd->i_default_sample_flags);

    msg_Dbg( p_stream, "read box: \"tfhd\" version %d flags 0x%x track ID %d %s %s %s %s %s",
                p_box->data.p_tfhd->i_version,
                p_box->data.p_tfhd->i_flags,
                p_box->data.p_tfhd->i_track_ID,
                psz_base, psz_desc, psz_dura, psz_size, psz_flag );
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#endif

    MP4_READBOX_EXIT( 1 );
}

static int MP4_ReadBox_trun(  stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_trun_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_trun );

    MP4_GET4BYTES( p_box->data.p_trun->i_sample_count );

    if( p_box->data.p_trun->i_flags & MP4_TRUN_DATA_OFFSET )
706
        MP4_GET4BYTES( p_box->data.p_trun->i_data_offset );
707
708
709
710
711
712
    if( p_box->data.p_trun->i_flags & MP4_TRUN_FIRST_FLAGS )
        MP4_GET4BYTES( p_box->data.p_trun->i_first_sample_flags );

    p_box->data.p_trun->p_samples =
      calloc( p_box->data.p_trun->i_sample_count, sizeof(MP4_descriptor_trun_sample_t) );
    if ( p_box->data.p_trun->p_samples == NULL )
713
        MP4_READBOX_EXIT( 0 );
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728

    for( unsigned int i = 0; i<p_box->data.p_trun->i_sample_count; i++ )
    {
        MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
        if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
            MP4_GET4BYTES( p_sample->i_duration );
        if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
            MP4_GET4BYTES( p_sample->i_size );
        if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_FLAGS )
            MP4_GET4BYTES( p_sample->i_flags );
        if( p_box->data.p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
            MP4_GET4BYTES( p_sample->i_composition_time_offset );
    }

#ifdef MP4_VERBOSE
729
    msg_Dbg( p_stream, "read box: \"trun\" version %u flags 0x%x sample count %u",
730
731
732
733
734
735
736
                  p_box->data.p_trun->i_version,
                  p_box->data.p_trun->i_flags,
                  p_box->data.p_trun->i_sample_count );

    for( unsigned int i = 0; i<p_box->data.p_trun->i_sample_count; i++ )
    {
        MP4_descriptor_trun_sample_t *p_sample = &p_box->data.p_trun->p_samples[i];
737
738
        msg_Dbg( p_stream, "read box: \"trun\" sample %4.4u flags 0x%x "\
            "duration %"PRIu32" size %"PRIu32" composition time offset %"PRIu32,
739
740
741
742
743
744
745
746
747
748
749
750
751
752
                        i, p_sample->i_flags, p_sample->i_duration,
                        p_sample->i_size, p_sample->i_composition_time_offset );
    }
#endif

    MP4_READBOX_EXIT( 1 );
}

static void MP4_FreeBox_trun( MP4_Box_t *p_box )
{
    FREENULL( p_box->data.p_trun->p_samples );
}


sigmunau's avatar
sigmunau committed
753
static int MP4_ReadBox_tkhd(  stream_t *p_stream, MP4_Box_t *p_box )
754
755
756
757
758
759
760
{
#ifdef MP4_VERBOSE
    char s_creation_time[128];
    char s_modification_time[128];
    char s_duration[128];
#endif
    MP4_READBOX_ENTER( MP4_Box_data_tkhd_t );
761

762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
    MP4_GETVERSIONFLAGS( p_box->data.p_tkhd );

    if( p_box->data.p_tkhd->i_version )
    {
        MP4_GET8BYTES( p_box->data.p_tkhd->i_creation_time );
        MP4_GET8BYTES( p_box->data.p_tkhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
        MP4_GET8BYTES( p_box->data.p_tkhd->i_duration );
    }
    else
    {
        MP4_GET4BYTES( p_box->data.p_tkhd->i_creation_time );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_track_ID );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved );
        MP4_GET4BYTES( p_box->data.p_tkhd->i_duration );
    }
780

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
781
    for( unsigned i = 0; i < 2; i++ )
782
783
784
785
786
787
788
789
    {
        MP4_GET4BYTES( p_box->data.p_tkhd->i_reserved2[i] );
    }
    MP4_GET2BYTES( p_box->data.p_tkhd->i_layer );
    MP4_GET2BYTES( p_box->data.p_tkhd->i_predefined );
    MP4_GET2BYTES( p_box->data.p_tkhd->i_volume );
    MP4_GET2BYTES( p_box->data.p_tkhd->i_reserved3 );

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
790
    for( unsigned i = 0; i < 9; i++ )
791
792
793
794
795
    {
        MP4_GET4BYTES( p_box->data.p_tkhd->i_matrix[i] );
    }
    MP4_GET4BYTES( p_box->data.p_tkhd->i_width );
    MP4_GET4BYTES( p_box->data.p_tkhd->i_height );
796

gnarula's avatar
gnarula committed
797
798
799
    double rotation;    //angle in degrees to be rotated clockwise
    double scale[2];    // scale factor; sx = scale[0] , sy = scale[1]
    double translate[2];// amount to translate; tx = translate[0] , ty = translate[1]
800

gnarula's avatar
gnarula committed
801
    int *matrix = p_box->data.p_tkhd->i_matrix;
802

gnarula's avatar
gnarula committed
803
804
    translate[0] = conv_fx(matrix[6]);
    translate[1] = conv_fx(matrix[7]);
805

gnarula's avatar
gnarula committed
806
807
808
809
    scale[0] = sqrt(conv_fx(matrix[0]) * conv_fx(matrix[0]) +
                    conv_fx(matrix[3]) * conv_fx(matrix[3]));
    scale[1] = sqrt(conv_fx(matrix[1]) * conv_fx(matrix[1]) +
                    conv_fx(matrix[4]) * conv_fx(matrix[4]));
810

gnarula's avatar
gnarula committed
811
    rotation = atan2(conv_fx(matrix[1]) / scale[1], conv_fx(matrix[0]) / scale[0]) * 180 / M_PI;
812

gnarula's avatar
gnarula committed
813
814
815
    if (rotation < 0)
        rotation += 360.;

816
817
    p_box->data.p_tkhd->f_rotation = rotation;

818
819
820
821
#ifdef MP4_VERBOSE
    MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time );
    MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mvhd->i_modification_time );
    MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration );
822

gnarula's avatar
gnarula committed
823
    msg_Dbg( p_stream, "read box: \"tkhd\" creation %s modification %s duration %s track ID %d layer %d volume %f rotation %f scaleX %f scaleY %f translateX %f translateY %f width %f height %f. "
824
            "Matrix: %i %i %i %i %i %i %i %i %i",
825
826
827
828
829
830
                  s_creation_time,
                  s_modification_time,
                  s_duration,
                  p_box->data.p_tkhd->i_track_ID,
                  p_box->data.p_tkhd->i_layer,
                  (float)p_box->data.p_tkhd->i_volume / 256 ,
gnarula's avatar
gnarula committed
831
832
833
834
835
                  rotation,
                  scale[0],
                  scale[1],
                  translate[0],
                  translate[1],
836
                  (float)p_box->data.p_tkhd->i_width / 65536,
837
838
839
840
841
842
843
844
845
846
                  (float)p_box->data.p_tkhd->i_height / 65536,
                  p_box->data.p_tkhd->i_matrix[0],
                  p_box->data.p_tkhd->i_matrix[1],
                  p_box->data.p_tkhd->i_matrix[2],
                  p_box->data.p_tkhd->i_matrix[3],
                  p_box->data.p_tkhd->i_matrix[4],
                  p_box->data.p_tkhd->i_matrix[5],
                  p_box->data.p_tkhd->i_matrix[6],
                  p_box->data.p_tkhd->i_matrix[7],
                  p_box->data.p_tkhd->i_matrix[8] );
847
848
849
850
851
#endif
    MP4_READBOX_EXIT( 1 );
}


sigmunau's avatar
sigmunau committed
852
static int MP4_ReadBox_mdhd( stream_t *p_stream, MP4_Box_t *p_box )
853
{
854
    uint16_t i_language;
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
#ifdef MP4_VERBOSE
    char s_creation_time[128];
    char s_modification_time[128];
    char s_duration[128];
#endif
    MP4_READBOX_ENTER( MP4_Box_data_mdhd_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_mdhd );

    if( p_box->data.p_mdhd->i_version )
    {
        MP4_GET8BYTES( p_box->data.p_mdhd->i_creation_time );
        MP4_GET8BYTES( p_box->data.p_mdhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
        MP4_GET8BYTES( p_box->data.p_mdhd->i_duration );
    }
    else
    {
        MP4_GET4BYTES( p_box->data.p_mdhd->i_creation_time );
        MP4_GET4BYTES( p_box->data.p_mdhd->i_modification_time );
        MP4_GET4BYTES( p_box->data.p_mdhd->i_timescale );
        MP4_GET4BYTES( p_box->data.p_mdhd->i_duration );
    }
878
    p_box->data.p_mdhd->i_language_code = i_language = GetWBE( p_peek );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
879
    for( unsigned i = 0; i < 3; i++ )
880
    {
881
        p_box->data.p_mdhd->i_language[i] =
882
883
884
885
                    ( ( i_language >> ( (2-i)*5 ) )&0x1f ) + 0x60;
    }

    MP4_GET2BYTES( p_box->data.p_mdhd->i_predefined );
886

887
888
889
890
#ifdef MP4_VERBOSE
    MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mdhd->i_creation_time );
    MP4_ConvertDate2Str( s_modification_time, p_box->data.p_mdhd->i_modification_time );
    MP4_ConvertDate2Str( s_duration, p_box->data.p_mdhd->i_duration );
sigmunau's avatar
sigmunau committed
891
    msg_Dbg( p_stream, "read box: \"mdhd\" creation %s modification %s time scale %d duration %s language %c%c%c",
892
893
                  s_creation_time,
                  s_modification_time,
894
                  (uint32_t)p_box->data.p_mdhd->i_timescale,
895
896
897
898
899
900
901
902
903
                  s_duration,
                  p_box->data.p_mdhd->i_language[0],
                  p_box->data.p_mdhd->i_language[1],
                  p_box->data.p_mdhd->i_language[2] );
#endif
    MP4_READBOX_EXIT( 1 );
}


sigmunau's avatar
sigmunau committed
904
static int MP4_ReadBox_hdlr( stream_t *p_stream, MP4_Box_t *p_box )
905
{
906
907
    int32_t i_reserved;

908
    MP4_READBOX_ENTER( MP4_Box_data_hdlr_t );
909
910

    MP4_GETVERSIONFLAGS( p_box->data.p_hdlr );
911

912
    MP4_GETFOURCC( p_box->data.p_hdlr->i_predefined );
913
914
    MP4_GETFOURCC( p_box->data.p_hdlr->i_handler_type );

915
916
917
    MP4_GET4BYTES( i_reserved );
    MP4_GET4BYTES( i_reserved );
    MP4_GET4BYTES( i_reserved );
918
    p_box->data.p_hdlr->psz_name = NULL;
919

920
    if( i_read > 0 )
921
    {
922
        uint8_t *psz = p_box->data.p_hdlr->psz_name = malloc( i_read + 1 );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
923
        if( unlikely( psz == NULL ) )
924
            MP4_READBOX_EXIT( 0 );
925

926
927
928
929
930
        /* Yes, I love .mp4 :( */
        if( p_box->data.p_hdlr->i_predefined == VLC_FOURCC( 'm', 'h', 'l', 'r' ) )
        {
            uint8_t i_len;
            int i_copy;
931

932
933
            MP4_GET1BYTE( i_len );
            i_copy = __MIN( i_read, i_len );
934

935
            memcpy( psz, p_peek, i_copy );
936
937
938
939
            p_box->data.p_hdlr->psz_name[i_copy] = '\0';
        }
        else
        {
940
            memcpy( psz, p_peek, i_read );
941
942
943
            p_box->data.p_hdlr->psz_name[i_read] = '\0';
        }
    }
944
945

#ifdef MP4_VERBOSE
946
        msg_Dbg( p_stream, "read box: \"hdlr\" handler type: \"%4.4s\" name: \"%s\"",
947
948
                   (char*)&p_box->data.p_hdlr->i_handler_type,
                   p_box->data.p_hdlr->psz_name );
949
950

#endif
951
    MP4_READBOX_EXIT( 1 );
952
953
}

954
static void MP4_FreeBox_hdlr( MP4_Box_t *p_box )
955
{
zorglub's avatar
zorglub committed
956
    FREENULL( p_box->data.p_hdlr->psz_name );
957
958
}

sigmunau's avatar
sigmunau committed
959
static int MP4_ReadBox_vmhd( stream_t *p_stream, MP4_Box_t *p_box )
960
{
961
    MP4_READBOX_ENTER( MP4_Box_data_vmhd_t );
962
963

    MP4_GETVERSIONFLAGS( p_box->data.p_vmhd );
964

965
    MP4_GET2BYTES( p_box->data.p_vmhd->i_graphics_mode );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
966
    for( unsigned i = 0; i < 3; i++ )
967
968
969
    {
        MP4_GET2BYTES( p_box->data.p_vmhd->i_opcolor[i] );
    }
970

971
#ifdef MP4_VERBOSE
sigmunau's avatar
sigmunau committed
972
    msg_Dbg( p_stream, "read box: \"vmhd\" graphics-mode %d opcolor (%d, %d, %d)",
973
974
975
976
977
978
979
980
                      p_box->data.p_vmhd->i_graphics_mode,
                      p_box->data.p_vmhd->i_opcolor[0],
                      p_box->data.p_vmhd->i_opcolor[1],
                      p_box->data.p_vmhd->i_opcolor[2] );
#endif
    MP4_READBOX_EXIT( 1 );
}

sigmunau's avatar
sigmunau committed
981
static int MP4_ReadBox_smhd( stream_t *p_stream, MP4_Box_t *p_box )
982
{
983
    MP4_READBOX_ENTER( MP4_Box_data_smhd_t );
984
985
986
987
988
989
990
991

    MP4_GETVERSIONFLAGS( p_box->data.p_smhd );



    MP4_GET2BYTES( p_box->data.p_smhd->i_balance );

    MP4_GET2BYTES( p_box->data.p_smhd->i_reserved );
992

993
#ifdef MP4_VERBOSE
sigmunau's avatar
sigmunau committed
994
    msg_Dbg( p_stream, "read box: \"smhd\" balance %f",
995
996
997
998
999
1000
                      (float)p_box->data.p_smhd->i_balance / 256 );
#endif
    MP4_READBOX_EXIT( 1 );
}


sigmunau's avatar
sigmunau committed
1001
static int MP4_ReadBox_hmhd( stream_t *p_stream, MP4_Box_t *p_box )
1002
{
1003
    MP4_READBOX_ENTER( MP4_Box_data_hmhd_t );
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015

    MP4_GETVERSIONFLAGS( p_box->data.p_hmhd );

    MP4_GET2BYTES( p_box->data.p_hmhd->i_max_PDU_size );
    MP4_GET2BYTES( p_box->data.p_hmhd->i_avg_PDU_size );

    MP4_GET4BYTES( p_box->data.p_hmhd->i_max_bitrate );
    MP4_GET4BYTES( p_box->data.p_hmhd->i_avg_bitrate );

    MP4_GET4BYTES( p_box->data.p_hmhd->i_reserved );

#ifdef MP4_VERBOSE
sigmunau's avatar
sigmunau committed
1016
    msg_Dbg( p_stream, "read box: \"hmhd\" maxPDU-size %d avgPDU-size %d max-bitrate %d avg-bitrate %d",
1017
1018
1019
1020
1021
1022
1023
1024
                      p_box->data.p_hmhd->i_max_PDU_size,
                      p_box->data.p_hmhd->i_avg_PDU_size,
                      p_box->data.p_hmhd->i_max_bitrate,
                      p_box->data.p_hmhd->i_avg_bitrate );
#endif
    MP4_READBOX_EXIT( 1 );
}

sigmunau's avatar
sigmunau committed
1025
static int MP4_ReadBox_url( stream_t *p_stream, MP4_Box_t *p_box )
1026
{
1027
    MP4_READBOX_ENTER( MP4_Box_data_url_t );
1028
1029
1030
1031
1032

    MP4_GETVERSIONFLAGS( p_box->data.p_url );
    MP4_GETSTRINGZ( p_box->data.p_url->psz_location );

#ifdef MP4_VERBOSE
sigmunau's avatar
sigmunau committed
1033
    msg_Dbg( p_stream, "read box: \"url\" url: %s",
1034
1035
1036
1037
1038
1039
1040
                       p_box->data.p_url->psz_location );

#endif
    MP4_READBOX_EXIT( 1 );
}


1041
static void MP4_FreeBox_url( MP4_Box_t *p_box )
1042
{
1043
    FREENULL( p_box->data.p_url->psz_location );
1044
1045
}

sigmunau's avatar
sigmunau committed
1046
static int MP4_ReadBox_urn( stream_t *p_stream, MP4_Box_t *p_box )
1047
1048
1049
1050
1051
1052
{
    MP4_READBOX_ENTER( MP4_Box_data_urn_t );

    MP4_GETVERSIONFLAGS( p_box->data.p_urn );

    MP4_GETSTRINGZ( p_box->data.p_urn->psz_name );
1053
1054
    MP4_GETSTRINGZ( p_box->data.p_urn->psz_location );

1055
#ifdef MP4_VERBOSE
sigmunau's avatar
sigmunau committed
1056
    msg_Dbg( p_stream, "read box: \"urn\" name %s location %s",
1057
1058
1059
1060
1061
                      p_box->data.p_urn->psz_name,
                      p_box->data.p_urn->psz_location );
#endif
    MP4_READBOX_EXIT( 1 );
}
1062
static void MP4_FreeBox_urn( MP4_Box_t *p_box )
1063
{
zorglub's avatar
zorglub committed
1064
1065
    FREENULL( p_box->data.p_urn->psz_name );
    FREENULL( p_box->data.p_urn->psz_location );
1066
1067
1068
}


sigmunau's avatar
sigmunau committed
1069
static int MP4_ReadBox_dref( stream_t *p_stream, MP4_Box_t *p_box )
1070
1071
{
    MP4_READBOX_ENTER( MP4_Box_data_dref_t );
1072

1073
1074
1075
    MP4_GETVERSIONFLAGS( p_box->data.p_dref );

    MP4_GET4BYTES( p_box->data.p_dref->i_entry_count );
1076

1077
    stream_Seek( p_stream, p_box->i_pos + mp4_box_headersize( p_box ) + 8 );
1078
1079
1080
    MP4_ReadBoxContainerRaw( p_stream, p_box );

#ifdef MP4_VERBOSE
sigmunau's avatar
sigmunau committed
1081
    msg_Dbg( p_stream, "read box: \"dref\" entry-count %d",
1082
1083
1084
1085
1086
1087
                      p_box->data.p_dref->i_entry_count );

#endif
    MP4_READBOX_EXIT( 1 );
}

1088
1089
1090
1091
1092
static void MP4_FreeBox_stts( MP4_Box_t *p_box )
{
    FREENULL( p_box->data.p_stts->i_sample_count );
    FREENULL( p_box->data.p_stts->i_sample_delta );
}
1093

sigmunau's avatar
sigmunau committed
1094
static int MP4_ReadBox_stts( stream_t *p_stream, MP4_Box_t *p_box )
1095
{
1096
    MP4_READBOX_ENTER( MP4_Box_data_stts_t );
1097
1098
1099
1100

    MP4_GETVERSIONFLAGS( p_box->data.p_stts );
    MP4_GET4BYTES( p_box->data.p_stts->i_entry_count );

1101
    p_box->data.p_stts->i_sample_count =
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1102
        calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
1103
    p_box->data.p_stts->i_sample_delta =
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1104
        calloc( p_box->data.p_stts->i_entry_count, sizeof(uint32_t) );
1105
1106
1107
    if( p_box->data.p_stts->i_sample_count == NULL
     || p_box->data.p_stts->i_sample_delta == NULL )
    {
1108
        MP4_READBOX_EXIT( 0 );
1109
    }
1110

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1111
    for( unsigned int i = 0; (i < p_box->data.p_stts->i_entry_count )&&( i_read >=8 ); i++ )
1112
1113
1114
1115
    {
        MP4_GET4BYTES( p_box->data.p_stts->i_sample_count[i] );
        MP4_GET4BYTES( p_box->data.p_stts->i_sample_delta[i] );
    }
1116

1117
#ifdef MP4_VERBOSE
sigmunau's avatar
sigmunau committed
1118
    msg_Dbg( p_stream, "read box: \"stts\" entry-count %d",
1119
1120
1121
                      p_box->data.p_stts->i_entry_count );

#endif
1122
    MP4_READBOX_EXIT( 1 );
1123
1124
}

1125
1126
1127
1128
1129
1130
1131

static void MP4_FreeBox_ctts( MP4_Box_t *p_box )
{
    FREENULL( p_box->data.p_ctts->i_sample_count );
    FREENULL( p_box->data.p_ctts->i_sample_offset );
}

sigmunau's avatar
sigmunau committed
1132
static int MP4_ReadBox_ctts( stream_t *p_stream, MP4_Box_t *p_box )
1133
1134
{
    MP4_READBOX_ENTER( MP4_Box_data_ctts_t );
1135

1136
    MP4_GETVERSIONFLAGS( p_box->data.p_ctts );
1137

1138
1139
    MP4_GET4BYTES( p_box->data.p_ctts->i_entry_count );

1140
    p_box->data.p_ctts->i_sample_count =
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1141
        calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
1142
    p_box->data.p_ctts->i_sample_offset =
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1143
        calloc( p_box->data.p_ctts->i_entry_count, sizeof(uint32_t) );
1144
1145
1146
    if( ( p_box->data.p_ctts->i_sample_count == NULL )
     || ( p_box->data.p_ctts->i_sample_offset == NULL ) )
    {
1147
        MP4_READBOX_EXIT( 0 );
1148
    }