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

35
#include "libmp4.h"
36
#include "languages.h"
gnarula's avatar
gnarula committed
37
#include <math.h>
38
#include <assert.h>
39 40

/* Some assumptions:
41
 * The input method HAS to be seekable
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
42
 */
43

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

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

Laurent Aimar's avatar
Laurent Aimar committed
60
    /* date begin at 1 jan 1904 */
61 62
    if ( !b_relative )
        i_date += ((INT64_C(1904) * 365) + 17) * 24 * 60 * 60;
Laurent Aimar's avatar
Laurent Aimar committed
63

64 65 66 67
    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;
68
    sprintf( psz, "%dd-%2.2dh:%2.2dm:%2.2ds", i_day, i_hour, i_min, i_sec );
69
}
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
70
#endif
71

Laurent Aimar's avatar
Laurent Aimar committed
72 73 74
/*****************************************************************************
 * Some prototypes.
 *****************************************************************************/
sigmunau's avatar
sigmunau committed
75
static MP4_Box_t *MP4_ReadBox( stream_t *p_stream, MP4_Box_t *p_father );
76 77
static int MP4_Box_Read_Specific( stream_t *p_stream, MP4_Box_t *p_box, MP4_Box_t *p_father );
static void MP4_Box_Clean_Specific( MP4_Box_t *p_box );
78
static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box );
79

80
int MP4_Seek( stream_t *p_stream, uint64_t i_pos )
81
{
82 83 84 85 86
    /* Prevent prefetch breakage */
    uint64_t i_size = stream_Size( p_stream );
    if( i_size > 0 && i_pos >= i_size )
        return VLC_EGENERIC;

87
    bool b_canseek = false;
88
    if ( vlc_stream_Control( p_stream, STREAM_CAN_SEEK, &b_canseek ) != VLC_SUCCESS ||
89 90 91
         b_canseek )
    {
        /* can seek or don't know */
92
        return vlc_stream_Seek( p_stream, i_pos );
93 94 95
    }
    /* obviously can't seek then */

96
    int64_t i_current_pos = vlc_stream_Tell( p_stream );
97 98 99 100 101 102 103 104
    if ( i_current_pos < 0 || i_pos < (uint64_t)i_current_pos )
        return VLC_EGENERIC;

    size_t i_toread = i_pos - i_current_pos;
    if( i_toread == 0 )
        return VLC_SUCCESS;
    else if( i_toread > (1<<17) )
        return VLC_EGENERIC;
105 106 107 108

    if( vlc_stream_Read( p_stream, NULL, i_toread ) != (ssize_t)i_toread )
        return VLC_EGENERIC;
    return VLC_SUCCESS;
109 110
}

111 112 113 114 115 116 117
static void MP4_BoxAddChild( MP4_Box_t *p_parent, MP4_Box_t *p_childbox )
{
    if( !p_parent->p_first )
            p_parent->p_first = p_childbox;
    else
            p_parent->p_last->p_next = p_childbox;
    p_parent->p_last = p_childbox;
118
    p_childbox->p_father = p_parent;
119 120
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
MP4_Box_t * MP4_BoxExtract( MP4_Box_t **pp_chain, uint32_t i_type )
{
    MP4_Box_t *p_box = *pp_chain;
    while( p_box )
    {
        if( p_box->i_type == i_type )
        {
            *pp_chain = p_box->p_next;
            p_box->p_next = NULL;
            return p_box;
        }
        pp_chain = &p_box->p_next;
        p_box = p_box->p_next;
    }
    return NULL;
}

138 139 140
/* Don't use vlc_stream_Seek directly */
#undef vlc_stream_Seek
#define vlc_stream_Seek(a,b) __NO__
141 142

/*****************************************************************************
143
 * MP4_PeekBoxHeader : Load only common parameters for all boxes
144
 *****************************************************************************
145
 * p_box need to be an already allocated MP4_Box_t, and all data
146 147 148 149
 *  will only be peek not read
 *
 * RETURN : 0 if it fail, 1 otherwise
 *****************************************************************************/
150
static int MP4_PeekBoxHeader( stream_t *p_stream, MP4_Box_t *p_box )
151
{
152
    int      i_read;
153
    const uint8_t  *p_peek;
154

155
    if( ( ( i_read = vlc_stream_Peek( p_stream, &p_peek, 32 ) ) < 8 ) )
156
    {
157
        return 0;
158
    }
159
    p_box->i_pos = vlc_stream_Tell( p_stream );
160

161
    p_box->data.p_payload = NULL;
162 163 164 165
    p_box->p_father = NULL;
    p_box->p_first  = NULL;
    p_box->p_last  = NULL;
    p_box->p_next   = NULL;
166

167 168 169 170 171 172 173
    MP4_GET4BYTES( p_box->i_shortsize );
    MP4_GETFOURCC( p_box->i_type );

    /* Now special case */

    if( p_box->i_shortsize == 1 )
    {
174 175
        if( i_read < 8 )
            return 0;
176 177 178 179 180 181 182 183
        /* 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 */
    }
184

185 186 187 188
    if( UINT64_MAX - p_box->i_size < p_box->i_pos )
        return 0;

    if( p_box->i_type == ATOM_uuid )
189
    {
190 191
        if( i_read < 16 )
            return 0;
192 193 194
        /* get extented type on 16 bytes */
        GetUUID( &p_box->i_uuid, p_peek );
    }
195

196
#ifdef MP4_ULTRA_VERBOSE
197 198
    if( p_box->i_size )
    {
199
        if MP4_BOX_TYPE_ASCII()
200 201
            msg_Dbg( p_stream, "found Box: %4.4s size %"PRId64" %"PRId64,
                    (char*)&p_box->i_type, p_box->i_size, p_box->i_pos );
202
        else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
203
            msg_Dbg( p_stream, "found Box: c%3.3s size %"PRId64,
204
                    (char*)&p_box->i_type+1, p_box->i_size );
205 206 207
    }
#endif

208
    return 1;
209 210 211
}

/*****************************************************************************
212
 * MP4_ReadBoxRestricted : Reads box from current position
213
 *****************************************************************************
214 215
 * if p_box == NULL, box is invalid or failed, position undefined
 * on success, position is past read box or EOF
216
 *****************************************************************************/
217
static MP4_Box_t *MP4_ReadBoxRestricted( stream_t *p_stream, MP4_Box_t *p_father,
218 219
                                         const uint32_t onlytypes[], const uint32_t nottypes[],
                                         bool *pb_restrictionhit )
220
{
221 222 223
    MP4_Box_t peekbox = { 0 };
    if ( !MP4_PeekBoxHeader( p_stream, &peekbox ) )
        return NULL;
224

225
    if( peekbox.i_size < 8 )
226
    {
227
        msg_Warn( p_stream, "found an invalid sized %"PRIu64" box %4.4s @%"PRIu64 ,
228
                  peekbox.i_size, (char *) &peekbox.i_type, vlc_stream_Tell(p_stream) );
229
        return NULL;
230 231
    }

232
    for( size_t i=0; nottypes && nottypes[i]; i++ )
233
    {
234
        if( nottypes[i] == peekbox.i_type )
235 236
        {
            *pb_restrictionhit = true;
237
            return NULL;
238
        }
239 240
    }

241
    for( size_t i=0; onlytypes && onlytypes[i]; i++ )
242
    {
243
        if( onlytypes[i] != peekbox.i_type )
244 245
        {
            *pb_restrictionhit = true;
246
            return NULL;
247
        }
248
    }
249

250 251 252 253 254 255 256 257 258 259 260
    /* if father's size == 0, it means unknown or infinite size,
     * and we skip the followong check */
    if( p_father && p_father->i_size > 0 )
    {
        const uint64_t i_box_next = peekbox.i_size + peekbox.i_pos;
        const uint64_t i_father_next = p_father->i_size + p_father->i_pos;
        /* check if it's within p-father */
        if( i_box_next > i_father_next )
        {
            msg_Warn( p_stream, "out of bound child %4.4s", (char*) &peekbox.i_type );
            return NULL; /* out of bound */
261 262
        }
    }
263 264 265 266 267 268 269 270

    /* Everything seems OK */
    MP4_Box_t *p_box = (MP4_Box_t *) malloc( sizeof(MP4_Box_t) );
    if( !p_box )
        return NULL;
    *p_box = peekbox;

    const uint64_t i_next = p_box->i_pos + p_box->i_size;
271
    p_box->p_father = p_father;
272 273 274
    if( MP4_Box_Read_Specific( p_stream, p_box, p_father ) != VLC_SUCCESS )
    {
        msg_Warn( p_stream, "Failed reading box %4.4s", (char*) &peekbox.i_type );
275
        MP4_BoxFree( p_box );
276
        p_box = NULL;
277 278 279
    }

    /* Check is we consumed all data */
280
    if( vlc_stream_Tell( p_stream ) < i_next )
281
    {
282 283
        MP4_Seek( p_stream, i_next - 1 ); /*  since past seek can fail when hitting EOF */
        MP4_Seek( p_stream, i_next );
284 285 286 287 288 289
        if( vlc_stream_Tell( p_stream ) < i_next - 1 ) /* Truncated box */
        {
            msg_Warn( p_stream, "truncated box %4.4s discarded", (char*) &peekbox.i_type );
            MP4_BoxFree( p_box );
            p_box = NULL;
        }
290 291
    }

292 293
    if ( p_box )
        MP4_BoxAddChild( p_father, p_box );
294 295 296 297

    return p_box;
}

298
/*****************************************************************************
299
 * For all known box a loader is given,
300 301
 * you have to be already read container header
 * without container size, file position on exit is unknown
302
 *****************************************************************************/
303
static int MP4_ReadBoxContainerChildrenIndexed( stream_t *p_stream,
304 305
               MP4_Box_t *p_container, const uint32_t stoplist[],
               const uint32_t excludelist[], bool b_indexed )
306
{
307 308
    /* Size of root container is set to 0 when unknown, for exemple
     * with a DASH stream. In that case, we skip the following check */
309
    if( (p_container->i_size || p_container->p_father)
310
            && ( vlc_stream_Tell( p_stream ) + ((b_indexed)?16:8) >
311
        (uint64_t)(p_container->i_pos + p_container->i_size) )
312
      )
313 314
    {
        /* there is no box to load */
315
        return 0;
316
    }
317

318
    uint64_t i_last_pos = 0; /* used to detect read failure loops */
319 320
    const uint64_t i_end = p_container->i_pos + p_container->i_size;
    MP4_Box_t *p_box = NULL;
321
    bool b_onexclude = false;
322
    bool b_continue;
323 324
    do
    {
325
        b_continue = false;
326 327
        if ( p_container->i_size )
        {
328
            const uint64_t i_tell = vlc_stream_Tell( p_stream );
329 330 331 332
            if( i_tell + ((b_indexed)?16:8) >= i_end )
                break;
        }

333 334 335 336
        uint32_t i_index = 0;
        if ( b_indexed )
        {
            uint8_t read[8];
337
            if ( vlc_stream_Read( p_stream, read, 8 ) < 8 )
338
                break;
339 340
            i_index = GetDWBE(&read[4]);
        }
341 342
        b_onexclude = false; /* If stopped due exclude list */
        if( (p_box = MP4_ReadBoxRestricted( p_stream, p_container, NULL, excludelist, &b_onexclude )) )
343
        {
344
            b_continue = true;
345 346 347 348 349 350 351
            p_box->i_index = i_index;
            for(size_t i=0; stoplist && stoplist[i]; i++)
            {
                if( p_box->i_type == stoplist[i] )
                    return 1;
            }
        }
352

353 354
        const uint64_t i_tell = vlc_stream_Tell( p_stream );
        if ( p_container->i_size && i_tell >= i_end )
yhuelf's avatar
yhuelf committed
355
        {
356 357 358 359 360 361 362 363
            assert( i_tell == i_end );
            break;
        }

        if ( !p_box )
        {
            /* Continue with next if box fails to load */
            if( i_last_pos == i_tell )
364
                break;
365 366
            i_last_pos = i_tell;
            b_continue = true;
yhuelf's avatar
yhuelf committed
367
        }
368

369
    } while( b_continue );
370

371
    /* Always move to end of container */
372
    if ( !b_onexclude &&  p_container->i_size )
373
    {
374
        const uint64_t i_tell = vlc_stream_Tell( p_stream );
375 376 377
        if ( i_tell != i_end )
            MP4_Seek( p_stream, i_end );
    }
378

379
    return 1;
380 381
}

382 383 384 385 386 387 388
int MP4_ReadBoxContainerRestricted( stream_t *p_stream, MP4_Box_t *p_container,
                                    const uint32_t stoplist[], const uint32_t excludelist[] )
{
    return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
                                                stoplist, excludelist, false );
}

389
int MP4_ReadBoxContainerChildren( stream_t *p_stream, MP4_Box_t *p_container,
390
                                  const uint32_t stoplist[] )
391 392
{
    return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_container,
393
                                                stoplist, NULL, false );
394 395
}

396 397 398 399 400 401 402 403 404 405 406 407 408 409
static void MP4_BoxOffsetUp( MP4_Box_t *p_box, uint64_t i_offset )
{
    while(p_box)
    {
        p_box->i_pos += i_offset;
        MP4_BoxOffsetUp( p_box->p_first, i_offset );
        p_box = p_box->p_next;
    }
}

/* Reads within an already read/in memory box (containers without having to seek) */
static int MP4_ReadBoxContainerRawInBox( stream_t *p_stream, MP4_Box_t *p_container,
                                         uint8_t *p_buffer, uint64_t i_size, uint64_t i_offset )
{
410 411
    if(!p_container)
        return 0;
412 413
    stream_t *p_substream = vlc_stream_MemoryNew( p_stream, p_buffer, i_size,
                                                  true );
414 415
    if( !p_substream )
        return 0;
416
    MP4_Box_t *p_last = p_container->p_last;
417
    MP4_ReadBoxContainerChildren( p_substream, p_container, NULL );
418
    vlc_stream_Delete( p_substream );
419 420 421 422 423 424 425 426 427 428
    /* do pos fixup */
    if( p_container )
    {
        MP4_Box_t *p_box = p_last ? p_last : p_container->p_first;
        MP4_BoxOffsetUp(p_box, i_offset);
    }

    return 1;
}

sigmunau's avatar
sigmunau committed
429
static int MP4_ReadBoxContainer( stream_t *p_stream, MP4_Box_t *p_container )
430
{
431 432
    if( p_container->i_size &&
        ( p_container->i_size <= (size_t)mp4_box_headersize(p_container ) + 8 ) )
433 434
    {
        /* container is empty, 8 stand for the first header in this box */
435
        return 1;
436
    }
437

438
    /* enter box */
439 440 441
    if ( MP4_Seek( p_stream, p_container->i_pos +
                      mp4_box_headersize( p_container ) ) )
        return 0;
442
    return MP4_ReadBoxContainerChildren( p_stream, p_container, NULL );
443 444
}

sigmunau's avatar
sigmunau committed
445
static int MP4_ReadBoxSkip( stream_t *p_stream, MP4_Box_t *p_box )
446
{
447
    /* XXX sometime moov is hiden in a free box */
448
    if( p_box->p_father &&
449
        p_box->p_father->i_type == ATOM_root &&
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
450
        p_box->i_type == ATOM_free )
451
    {
452
        const uint8_t *p_peek;
453 454 455
        int     i_read;
        vlc_fourcc_t i_fcc;

456
        i_read  = vlc_stream_Peek( p_stream, &p_peek, 44 );
457

458 459
        p_peek += mp4_box_headersize( p_box ) + 4;
        i_read -= mp4_box_headersize( p_box ) + 4;
460 461 462 463 464

        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
465
            if( i_fcc == ATOM_cmov || i_fcc == ATOM_mvhd )
466
            {
sigmunau's avatar
sigmunau committed
467
                msg_Warn( p_stream, "detected moov hidden in a free box ..." );
468

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
469
                p_box->i_type = ATOM_foov;
470 471 472 473
                return MP4_ReadBoxContainer( p_stream, p_box );
            }
        }
    }
474

475
    /* Nothing to do */
476
#ifdef MP4_ULTRA_VERBOSE
477 478 479 480
    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 );
481
#endif
482
    return 1;
483 484
}

485 486
static int MP4_ReadBox_ilst( stream_t *p_stream, MP4_Box_t *p_box )
{
487
    if( p_box->i_size < 8 || vlc_stream_Read( p_stream, NULL, 8 ) < 8 )
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
        return 0;

    /* Find our handler */
    if ( !p_box->i_handler && p_box->p_father )
    {
        const MP4_Box_t *p_sibling = p_box->p_father->p_first;
        while( p_sibling )
        {
            if ( p_sibling->i_type == ATOM_hdlr && p_sibling->data.p_hdlr )
            {
                p_box->i_handler = p_sibling->data.p_hdlr->i_handler_type;
                break;
            }
            p_sibling = p_sibling->p_next;
        }
    }

    switch( p_box->i_handler )
    {
    case 0:
        msg_Warn( p_stream, "no handler for ilst atom" );
        return 0;
510
    case HANDLER_mdta:
511
        return MP4_ReadBoxContainerChildrenIndexed( p_stream, p_box, NULL, NULL, true );
512
    case HANDLER_mdir:
513
        return MP4_ReadBoxContainerChildren( p_stream, p_box, NULL );
514 515 516 517 518 519
    default:
        msg_Warn( p_stream, "Unknown ilst handler type '%4.4s'", (char*)&p_box->i_handler );
        return 0;
    }
}

520 521 522 523 524
static void MP4_FreeBox_ftyp( MP4_Box_t *p_box )
{
    FREENULL( p_box->data.p_ftyp->i_compatible_brands );
}

sigmunau's avatar
sigmunau committed
525
static int MP4_ReadBox_ftyp( stream_t *p_stream, MP4_Box_t *p_box )
526
{
527
    MP4_READBOX_ENTER( MP4_Box_data_ftyp_t, MP4_FreeBox_ftyp );
528

529 530
    MP4_GETFOURCC( p_box->data.p_ftyp->i_major_brand );
    MP4_GET4BYTES( p_box->data.p_ftyp->i_minor_version );
531

532 533
    if( ( p_box->data.p_ftyp->i_compatible_brands_count = i_read / 4 ) )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
534 535 536 537
        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
538
        if( unlikely( tab == NULL ) )
539
            MP4_READBOX_EXIT( 0 );
540

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
541
        for( unsigned i = 0; i < p_box->data.p_ftyp->i_compatible_brands_count; i++ )
542
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
543
            MP4_GETFOURCC( tab[i] );
544 545 546 547 548 549 550
        }
    }
    else
    {
        p_box->data.p_ftyp->i_compatible_brands = NULL;
    }

551
    MP4_READBOX_EXIT( 1 );
552 553 554
}


sigmunau's avatar
sigmunau committed
555
static int MP4_ReadBox_mvhd(  stream_t *p_stream, MP4_Box_t *p_box )
556 557 558 559 560 561
{
#ifdef MP4_VERBOSE
    char s_creation_time[128];
    char s_modification_time[128];
    char s_duration[128];
#endif
562
    MP4_READBOX_ENTER( MP4_Box_data_mvhd_t, NULL );
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

    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 );
    }
580 581
    MP4_GET4BYTES( p_box->data.p_mvhd->i_rate );
    MP4_GET2BYTES( p_box->data.p_mvhd->i_volume );
582 583
    MP4_GET2BYTES( p_box->data.p_mvhd->i_reserved1 );

584

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
585
    for( unsigned i = 0; i < 2; i++ )
586 587 588
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_reserved2[i] );
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
589
    for( unsigned i = 0; i < 9; i++ )
590 591 592
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_matrix[i] );
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
593
    for( unsigned i = 0; i < 6; i++ )
594 595 596
    {
        MP4_GET4BYTES( p_box->data.p_mvhd->i_predefined[i] );
    }
597

598
    MP4_GET4BYTES( p_box->data.p_mvhd->i_next_track_id );
599 600


601
#ifdef MP4_VERBOSE
602
    MP4_ConvertDate2Str( s_creation_time, p_box->data.p_mvhd->i_creation_time, false );
603
    MP4_ConvertDate2Str( s_modification_time,
604
                         p_box->data.p_mvhd->i_modification_time, false );
605
    if( p_box->data.p_mvhd->i_rate && p_box->data.p_mvhd->i_timescale )
606
    {
607
        MP4_ConvertDate2Str( s_duration, p_box->data.p_mvhd->i_duration / p_box->data.p_mvhd->i_timescale, true );
608 609 610 611
    }
    else
    {
        s_duration[0] = 0;
612
    }
sigmunau's avatar
sigmunau committed
613
    msg_Dbg( p_stream, "read box: \"mvhd\" creation %s modification %s time scale %d duration %s rate %f volume %f next track id %d",
614 615
                  s_creation_time,
                  s_modification_time,
616
                  (uint32_t)p_box->data.p_mvhd->i_timescale,
617 618 619
                  s_duration,
                  (float)p_box->data.p_mvhd->i_rate / (1<<16 ),
                  (float)p_box->data.p_mvhd->i_volume / 256 ,
620
                  (uint32_t)p_box->data.p_mvhd->i_next_track_id );
621 622 623 624
#endif
    MP4_READBOX_EXIT( 1 );
}

625 626
static int MP4_ReadBox_mfhd(  stream_t *p_stream, MP4_Box_t *p_box )
{
627
    MP4_READBOX_ENTER( MP4_Box_data_mfhd_t, NULL );
628

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

631 632 633 634 635 636 637 638 639
    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 );
}

640 641
static int MP4_ReadBox_tfxd(  stream_t *p_stream, MP4_Box_t *p_box )
{
642
    MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, NULL );
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

    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 );
}

671 672 673 674 675
static void MP4_FreeBox_tfrf( MP4_Box_t *p_box )
{
    FREENULL( p_box->data.p_tfrf->p_tfrf_data_fields );
}

676 677
static int MP4_ReadBox_tfrf(  stream_t *p_stream, MP4_Box_t *p_box )
{
678
    MP4_READBOX_ENTER( MP4_Box_data_tfxd_t, MP4_FreeBox_tfrf );
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

    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 );
}

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
static int MP4_ReadBox_XML360( stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_360_t, NULL );

    MP4_Box_data_360_t *p_360_data = p_box->data.p_360;

    /* Copy the string for pattern matching as it does not end
    with a '\0' in the stream. */
    char *psz_rdf = strndup((char *)p_peek, i_read);

    if ( unlikely( !psz_rdf ) )
        MP4_READBOX_EXIT( 0 );

    /* Try to find the string "GSpherical:Spherical" because the v1
    spherical video spec says the tag must be there. */

    if ( strcasestr( psz_rdf, "Gspherical:Spherical" ) )
        p_360_data->i_projection_mode = PROJECTION_MODE_EQUIRECTANGULAR;

    /* Try to find the stero mode. */
    if ( strcasestr( psz_rdf, "left-right" ) )
        msg_Dbg( p_stream, "Left-right stereo mode" );

    if ( strcasestr( psz_rdf, "top-bottom" ) )
        msg_Dbg( p_stream, "Top-bottom stereo mode" );

    free( psz_rdf );

    MP4_READBOX_EXIT( 1 );
}

755 756 757 758 759 760
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 );
761 762
    if( !CmpUUID( &p_box->i_uuid, &XML360BoxUUID ) )
        return MP4_ReadBox_XML360( p_stream, p_box );
763

764 765 766 767 768 769 770 771 772
#ifdef MP4_VERBOSE
    msg_Warn( p_stream, "Unknown uuid type box: "
    "0x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x"
    "%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
    p_box->i_uuid.b[0],  p_box->i_uuid.b[1],  p_box->i_uuid.b[2],  p_box->i_uuid.b[3],
    p_box->i_uuid.b[4],  p_box->i_uuid.b[5],  p_box->i_uuid.b[6],  p_box->i_uuid.b[7],
    p_box->i_uuid.b[8],  p_box->i_uuid.b[9],  p_box->i_uuid.b[10], p_box->i_uuid.b[11],
    p_box->i_uuid.b[12], p_box->i_uuid.b[13], p_box->i_uuid.b[14], p_box->i_uuid.b[15] );
#else
773
    msg_Warn( p_stream, "Unknown uuid type box" );
774
#endif
775 776 777
    return 1;
}

778 779 780 781
static int MP4_ReadBox_st3d( stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_st3d_t, NULL );

782 783 784 785 786 787 788 789 790
    uint8_t i_version;
    MP4_GET1BYTE( i_version );
    if ( i_version != 0 )
        MP4_READBOX_EXIT( 0 );

    uint32_t i_flags;
    VLC_UNUSED( i_flags );
    MP4_GET3BYTES( i_flags );

791 792 793 794 795 796 797 798 799 800
    MP4_Box_data_st3d_t *p_data = p_box->data.p_st3d;
    MP4_GET1BYTE( p_data->i_stereo_mode );

    MP4_READBOX_EXIT( 1 );
}

static int MP4_ReadBox_prhd( stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_prhd_t, NULL );

801 802 803 804 805 806 807 808 809
    uint8_t i_version;
    MP4_GET1BYTE( i_version );
    if (i_version != 0)
        MP4_READBOX_EXIT( 0 );

    uint32_t i_flags;
    VLC_UNUSED( i_flags );
    MP4_GET3BYTES( i_flags );

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
    MP4_Box_data_prhd_t *p_data = p_box->data.p_prhd;
    int32_t fixed16_16;
    MP4_GET4BYTES( fixed16_16 );
    p_data->f_pose_yaw_degrees   = (float) fixed16_16 / 65536.0f;

    MP4_GET4BYTES( fixed16_16 );
    p_data->f_pose_pitch_degrees = (float) fixed16_16 / 65536.0f;

    MP4_GET4BYTES( fixed16_16 );
    p_data->f_pose_roll_degrees  = (float) fixed16_16 / 65536.0f;

    MP4_READBOX_EXIT( 1 );
}

static int MP4_ReadBox_equi( stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_equi_t, NULL );

828 829 830 831 832 833 834 835 836
    uint8_t i_version;
    MP4_GET1BYTE( i_version );
    if (i_version != 0)
        MP4_READBOX_EXIT( 0 );

    uint32_t i_flags;
    VLC_UNUSED( i_flags );
    MP4_GET3BYTES( i_flags );

837 838 839 840 841 842 843 844 845 846 847 848 849
    MP4_Box_data_equi_t *p_data = p_box->data.p_equi;
    MP4_GET4BYTES( p_data->i_projection_bounds_top );
    MP4_GET4BYTES( p_data->i_projection_bounds_bottom );
    MP4_GET4BYTES( p_data->i_projection_bounds_left );
    MP4_GET4BYTES( p_data->i_projection_bounds_right );

    MP4_READBOX_EXIT( 1 );
}

static int MP4_ReadBox_cbmp( stream_t *p_stream, MP4_Box_t *p_box )
{
    MP4_READBOX_ENTER( MP4_Box_data_cbmp_t, NULL );

850 851 852 853 854 855 856 857 858
    uint8_t i_version;
    MP4_GET1BYTE( i_version );
    if (i_version != 0)
        MP4_READBOX_EXIT( 0 );

    uint32_t i_flags;
    VLC_UNUSED( i_flags );
    MP4_GET3BYTES( i_flags );

859 860 861 862 863 864 865
    MP4_Box_data_cbmp_t *p_data = p_box->data.p_cbmp;
    MP4_GET4BYTES( p_data->i_layout );
    MP4_GET4BYTES( p_data->i_padding );

    MP4_READBOX_EXIT( 1 );
}

866
static void MP4_FreeBox_sidx( MP4_Box_t *p_box )
867
{
868
    FREENULL( p_box->data.p_sidx->p_items );
869 870
}

871 872
static int MP4_ReadBox_sidx(  stream_t *p_stream, MP4_Box_t *p_box )
{
873
    MP4_READBOX_ENTER( MP4_Box_data_sidx_t, MP4_FreeBox_sidx );
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

    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;
893
    VLC_UNUSED(i_reserved);
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
    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 );
}

929 930
static int MP4_ReadBox_tfhd(  stream_t *p_stream, MP4_Box_t *p_box )
{
931
    MP4_READBOX_ENTER( MP4_Box_data_tfhd_t, NULL );
932 933 934

    MP4_GETVERSIONFLAGS( p_box->data.p_tfhd );

Frédéric Yhuel's avatar
Frédéric Yhuel committed
935 936 937 938 939 940 941
    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 );
    }

942 943
    MP4_GET4BYTES( p_box->data.p_tfhd->i_track_ID );

Frédéric Yhuel's avatar
Frédéric Yhuel committed
944
    if( p_box->data.p_tfhd->i_flags & MP4_TFHD_DURATION_IS_EMPTY )
945
    {
Frédéric Yhuel's avatar
Frédéric Yhuel committed
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
        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 );
963 964

#ifdef MP4_VERBOSE
Frédéric Yhuel's avatar
Frédéric Yhuel committed