mov.c 96.8 KB
Newer Older
1
/*
2
 * MOV demuxer
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5
 *
6
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * Libav is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Fabrice Bellard's avatar
Fabrice Bellard committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
17
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with Libav; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22 23

#include <limits.h>
24

Baptiste Coudurier's avatar
Baptiste Coudurier committed
25
//#define DEBUG
26
//#define MOV_EXPORT_ALL_METADATA
27

28
#include "libavutil/attributes.h"
29
#include "libavutil/audioconvert.h"
30
#include "libavutil/intreadwrite.h"
31
#include "libavutil/intfloat.h"
32
#include "libavutil/mathematics.h"
33
#include "libavutil/avstring.h"
34
#include "libavutil/dict.h"
35
#include "libavcodec/ac3tab.h"
36
#include "avformat.h"
37
#include "internal.h"
38
#include "avio_internal.h"
39
#include "riff.h"
40
#include "isom.h"
41
#include "libavcodec/get_bits.h"
Raivo Hool's avatar
Raivo Hool committed
42
#include "id3v1.h"
43
#include "mov_chan.h"
44

45
#if CONFIG_ZLIB
46 47 48
#include <zlib.h>
#endif

49 50
/*
 * First version by Francois Revol revol@free.fr
51
 * Seek function by Gael Chardon gael.dev@4now.net
52 53
 */

54 55
#include "qtpalette.h"

Gael Chardon's avatar
Gael Chardon committed
56

57 58 59
#undef NDEBUG
#include <assert.h>

60 61 62
/* those functions parse an atom */
/* links atom IDs to parse functions */
typedef struct MOVParseTableEntry {
63
    uint32_t type;
64
    int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
65 66
} MOVParseTableEntry;

67
static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
68

69 70
static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
                                             unsigned len, const char *key)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
71 72 73
{
    char buf[16];

74
    short current, total = 0;
75
    avio_rb16(pb); // unknown
76
    current = avio_rb16(pb);
77 78
    if (len >= 6)
        total = avio_rb16(pb);
79 80 81 82 83
    if (!total)
        snprintf(buf, sizeof(buf), "%d", current);
    else
        snprintf(buf, sizeof(buf), "%d/%d", current, total);
    av_dict_set(&c->fc->metadata, key, buf, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
84 85 86 87

    return 0;
}

88 89
static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
                                            unsigned len, const char *key)
90
{
91
    char buf[16];
92

93 94 95 96
    /* bypass padding bytes */
    avio_r8(pb);
    avio_r8(pb);
    avio_r8(pb);
97

98
    snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
99
    av_dict_set(&c->fc->metadata, key, buf, 0);
100

101
    return 0;
102 103
}

104 105
static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
                                        unsigned len, const char *key)
106
{
107
    char buf[16];
108

109
    snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
110
    av_dict_set(&c->fc->metadata, key, buf, 0);
111

112
    return 0;
113 114
}

Raivo Hool's avatar
Raivo Hool committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
                             unsigned len, const char *key)
{
    short genre;
    char buf[20];

    avio_r8(pb); // unknown

    genre = avio_r8(pb);
    if (genre < 1 || genre > ID3v1_GENRE_MAX)
        return 0;
    snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
    av_dict_set(&c->fc->metadata, key, buf, 0);

    return 0;
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
static const uint32_t mac_to_unicode[128] = {
    0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
    0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
    0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
    0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
    0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
    0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
    0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
    0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
    0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
    0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
    0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
    0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
    0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
    0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
    0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
    0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
};

151
static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
152 153 154 155 156 157 158
                               char *dst, int dstlen)
{
    char *p = dst;
    char *end = dst+dstlen-1;
    int i;

    for (i = 0; i < len; i++) {
159
        uint8_t t, c = avio_r8(pb);
160 161 162 163 164 165 166 167 168
        if (c < 0x80 && p < end)
            *p++ = c;
        else
            PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
    }
    *p = 0;
    return p - dst;
}

Anton Khirnov's avatar
Anton Khirnov committed
169 170 171 172 173
static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
{
    AVPacket pkt;
    AVStream *st;
    MOVStreamContext *sc;
174
    enum AVCodecID id;
Anton Khirnov's avatar
Anton Khirnov committed
175 176 177
    int ret;

    switch (type) {
178 179 180
    case 0xd:  id = AV_CODEC_ID_MJPEG; break;
    case 0xe:  id = AV_CODEC_ID_PNG;   break;
    case 0x1b: id = AV_CODEC_ID_BMP;   break;
Anton Khirnov's avatar
Anton Khirnov committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    default:
        av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
        avio_skip(pb, len);
        return 0;
    }

    st = avformat_new_stream(c->fc, NULL);
    if (!st)
        return AVERROR(ENOMEM);
    sc = av_mallocz(sizeof(*sc));
    if (!sc)
        return AVERROR(ENOMEM);
    st->priv_data = sc;

    ret = av_get_packet(pb, &pkt, len);
    if (ret < 0)
        return ret;

    st->disposition              |= AV_DISPOSITION_ATTACHED_PIC;

    st->attached_pic              = pkt;
    st->attached_pic.stream_index = st->index;
    st->attached_pic.flags       |= AV_PKT_FLAG_KEY;

    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
    st->codec->codec_id   = id;

    return 0;
}

211
static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
212 213 214 215 216 217
{
#ifdef MOV_EXPORT_ALL_METADATA
    char tmp_key[5];
#endif
    char str[1024], key2[16], language[4] = {0};
    const char *key = NULL;
218 219
    uint16_t langcode = 0;
    uint32_t data_type = 0, str_size;
220
    int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
221 222 223 224

    switch (atom.type) {
    case MKTAG(0xa9,'n','a','m'): key = "title";     break;
    case MKTAG(0xa9,'a','u','t'):
225
    case MKTAG(0xa9,'A','R','T'): key = "artist";    break;
Raivo Hool's avatar
Raivo Hool committed
226
    case MKTAG( 'a','A','R','T'): key = "album_artist";    break;
227
    case MKTAG(0xa9,'w','r','t'): key = "composer";  break;
228
    case MKTAG( 'c','p','r','t'):
229 230 231 232
    case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
    case MKTAG(0xa9,'c','m','t'):
    case MKTAG(0xa9,'i','n','f'): key = "comment";   break;
    case MKTAG(0xa9,'a','l','b'): key = "album";     break;
233
    case MKTAG(0xa9,'d','a','y'): key = "date";      break;
234
    case MKTAG(0xa9,'g','e','n'): key = "genre";     break;
Raivo Hool's avatar
Raivo Hool committed
235 236
    case MKTAG( 'g','n','r','e'): key = "genre";
        parse = mov_metadata_gnre; break;
237
    case MKTAG(0xa9,'t','o','o'):
238
    case MKTAG(0xa9,'s','w','r'): key = "encoder";   break;
239
    case MKTAG(0xa9,'e','n','c'): key = "encoder";   break;
240 241 242 243 244
    case MKTAG( 'd','e','s','c'): key = "description";break;
    case MKTAG( 'l','d','e','s'): key = "synopsis";  break;
    case MKTAG( 't','v','s','h'): key = "show";      break;
    case MKTAG( 't','v','e','n'): key = "episode_id";break;
    case MKTAG( 't','v','n','n'): key = "network";   break;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
245
    case MKTAG( 't','r','k','n'): key = "track";
246 247 248
        parse = mov_metadata_track_or_disc_number; break;
    case MKTAG( 'd','i','s','k'): key = "disc";
        parse = mov_metadata_track_or_disc_number; break;
249
    case MKTAG( 't','v','e','s'): key = "episode_sort";
250
        parse = mov_metadata_int8_bypass_padding; break;
251
    case MKTAG( 't','v','s','n'): key = "season_number";
252
        parse = mov_metadata_int8_bypass_padding; break;
253
    case MKTAG( 's','t','i','k'): key = "media_type";
254
        parse = mov_metadata_int8_no_padding; break;
255 256 257 258
    case MKTAG( 'h','d','v','d'): key = "hd_video";
        parse = mov_metadata_int8_no_padding; break;
    case MKTAG( 'p','g','a','p'): key = "gapless_playback";
        parse = mov_metadata_int8_no_padding; break;
259 260 261
    }

    if (c->itunes_metadata && atom.size > 8) {
262 263
        int data_size = avio_rb32(pb);
        int tag = avio_rl32(pb);
264
        if (tag == MKTAG('d','a','t','a')) {
265 266
            data_type = avio_rb32(pb); // type
            avio_rb32(pb); // unknown
267 268
            str_size = data_size - 16;
            atom.size -= 16;
Anton Khirnov's avatar
Anton Khirnov committed
269 270 271 272 273 274 275 276

            if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
                int ret = mov_read_covr(c, pb, data_type, str_size);
                if (ret < 0) {
                    av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
                    return ret;
                }
            }
277 278
        } else return 0;
    } else if (atom.size > 4 && key && !c->itunes_metadata) {
279 280
        str_size = avio_rb16(pb); // string length
        langcode = avio_rb16(pb);
281
        ff_mov_lang_to_iso639(langcode, language);
282 283 284 285 286 287 288 289 290 291 292 293 294 295
        atom.size -= 4;
    } else
        str_size = atom.size;

#ifdef MOV_EXPORT_ALL_METADATA
    if (!key) {
        snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
        key = tmp_key;
    }
#endif

    if (!key)
        return 0;
    if (atom.size < 0)
296
        return AVERROR_INVALIDDATA;
297 298

    str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
299 300

    if (parse)
301
        parse(c, pb, str_size, key);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
302
    else {
303 304 305
        if (data_type == 3 || (data_type == 0 && langcode < 0x800)) { // MAC Encoded
            mov_read_mac_string(c, pb, str_size, str, sizeof(str));
        } else {
306
            avio_read(pb, str, str_size);
307 308
            str[str_size] = 0;
        }
309
        av_dict_set(&c->fc->metadata, key, str, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
310 311
        if (*language && strcmp(language, "und")) {
            snprintf(key2, sizeof(key2), "%s-%s", key, language);
312
            av_dict_set(&c->fc->metadata, key2, str, 0);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
313
        }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
314
    }
315 316 317
    av_dlog(c->fc, "lang \"%3s\" ", language);
    av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
            key, str, (char*)&atom.type, str_size, atom.size);
318 319 320

    return 0;
}
321

322
static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
David Conrad's avatar
David Conrad committed
323 324
{
    int64_t start;
David Conrad's avatar
David Conrad committed
325
    int i, nb_chapters, str_len, version;
David Conrad's avatar
David Conrad committed
326 327 328 329 330
    char str[256+1];

    if ((atom.size -= 5) < 0)
        return 0;

331 332
    version = avio_r8(pb);
    avio_rb24(pb);
David Conrad's avatar
David Conrad committed
333
    if (version)
334 335
        avio_rb32(pb); // ???
    nb_chapters = avio_r8(pb);
David Conrad's avatar
David Conrad committed
336 337 338 339 340

    for (i = 0; i < nb_chapters; i++) {
        if (atom.size < 9)
            return 0;

341 342
        start = avio_rb64(pb);
        str_len = avio_r8(pb);
David Conrad's avatar
David Conrad committed
343 344 345 346

        if ((atom.size -= 9+str_len) < 0)
            return 0;

347
        avio_read(pb, str, str_len);
David Conrad's avatar
David Conrad committed
348
        str[str_len] = 0;
349
        avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
David Conrad's avatar
David Conrad committed
350 351 352 353
    }
    return 0;
}

354
static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
355
{
356 357
    AVStream *st;
    MOVStreamContext *sc;
358 359
    int entries, i, j;

360 361 362 363 364
    if (c->fc->nb_streams < 1)
        return 0;
    st = c->fc->streams[c->fc->nb_streams-1];
    sc = st->priv_data;

365 366
    avio_rb32(pb); // version + flags
    entries = avio_rb32(pb);
367
    if (entries >= UINT_MAX / sizeof(*sc->drefs))
368
        return AVERROR_INVALIDDATA;
369
    av_free(sc->drefs);
370
    sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
371 372 373
    if (!sc->drefs)
        return AVERROR(ENOMEM);
    sc->drefs_count = entries;
374 375

    for (i = 0; i < sc->drefs_count; i++) {
376
        MOVDref *dref = &sc->drefs[i];
377
        uint32_t size = avio_rb32(pb);
378
        int64_t next = avio_tell(pb) + size - 4;
379

380
        if (size < 12)
381
            return AVERROR_INVALIDDATA;
382

383 384
        dref->type = avio_rl32(pb);
        avio_rb32(pb); // version + flags
Luca Barbato's avatar
Luca Barbato committed
385
        av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
386 387 388 389 390 391

        if (dref->type == MKTAG('a','l','i','s') && size > 150) {
            /* macintosh alias record */
            uint16_t volume_len, len;
            int16_t type;

392
            avio_skip(pb, 10);
393

394
            volume_len = avio_r8(pb);
395
            volume_len = FFMIN(volume_len, 27);
396
            avio_read(pb, dref->volume, 27);
397 398
            dref->volume[volume_len] = 0;
            av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
399

400
            avio_skip(pb, 12);
401

402
            len = avio_r8(pb);
403
            len = FFMIN(len, 63);
404
            avio_read(pb, dref->filename, 63);
405 406 407
            dref->filename[len] = 0;
            av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);

408
            avio_skip(pb, 16);
409 410

            /* read next level up_from_alias/down_to_target */
411 412
            dref->nlvl_from = avio_rb16(pb);
            dref->nlvl_to   = avio_rb16(pb);
413 414 415
            av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
                   dref->nlvl_from, dref->nlvl_to);

416
            avio_skip(pb, 16);
417

418
            for (type = 0; type != -1 && avio_tell(pb) < next; ) {
419 420
                type = avio_rb16(pb);
                len = avio_rb16(pb);
421 422 423 424
                av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
                if (len&1)
                    len += 1;
                if (type == 2) { // absolute path
425
                    av_free(dref->path);
426
                    dref->path = av_mallocz(len+1);
427 428
                    if (!dref->path)
                        return AVERROR(ENOMEM);
429
                    avio_read(pb, dref->path, len);
430
                    if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
431 432 433 434 435 436 437 438
                        len -= volume_len;
                        memmove(dref->path, dref->path+volume_len, len);
                        dref->path[len] = 0;
                    }
                    for (j = 0; j < len; j++)
                        if (dref->path[j] == ':')
                            dref->path[j] = '/';
                    av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
439 440 441 442 443
                } else if (type == 0) { // directory name
                    av_free(dref->dir);
                    dref->dir = av_malloc(len+1);
                    if (!dref->dir)
                        return AVERROR(ENOMEM);
444
                    avio_read(pb, dref->dir, len);
445 446 447 448 449
                    dref->dir[len] = 0;
                    for (j = 0; j < len; j++)
                        if (dref->dir[j] == ':')
                            dref->dir[j] = '/';
                    av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
450
                } else
451
                    avio_skip(pb, len);
452 453
            }
        }
454
        avio_seek(pb, next, SEEK_SET);
455 456 457 458
    }
    return 0;
}

459
static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
460
{
461
    AVStream *st;
462
    uint32_t type;
463
    uint32_t av_unused ctype;
464

465 466 467 468 469
    if (c->fc->nb_streams < 1) // meta before first trak
        return 0;

    st = c->fc->streams[c->fc->nb_streams-1];

470 471
    avio_r8(pb); /* version */
    avio_rb24(pb); /* flags */
472 473

    /* component type */
474 475
    ctype = avio_rl32(pb);
    type = avio_rl32(pb); /* component subtype */
476

Luca Barbato's avatar
Luca Barbato committed
477 478
    av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
    av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
479

480
    if     (type == MKTAG('v','i','d','e'))
481
        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
482
    else if (type == MKTAG('s','o','u','n'))
483
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
484
    else if (type == MKTAG('m','1','a',' '))
485
        st->codec->codec_id = AV_CODEC_ID_MP2;
486
    else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
487
        st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
488

489 490 491
    avio_rb32(pb); /* component  manufacture */
    avio_rb32(pb); /* component flags */
    avio_rb32(pb); /* component flags mask */
492 493 494 495

    return 0;
}

496
int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
497
{
498
    AVStream *st;
Mans Rullgard's avatar
Mans Rullgard committed
499
    int tag;
500

501
    if (fc->nb_streams < 1)
502
        return 0;
503
    st = fc->streams[fc->nb_streams-1];
504

505
    avio_rb32(pb); /* version + flags */
Mans Rullgard's avatar
Mans Rullgard committed
506
    ff_mp4_read_descr(fc, pb, &tag);
507
    if (tag == MP4ESDescrTag) {
508
        ff_mp4_parse_es_descr(pb, NULL);
509
    } else
510
        avio_rb16(pb); /* ID */
511

Mans Rullgard's avatar
Mans Rullgard committed
512
    ff_mp4_read_descr(fc, pb, &tag);
513 514
    if (tag == MP4DecConfigDescrTag)
        ff_mp4_read_dec_config_descr(fc, st, pb);
515 516 517
    return 0;
}

518
static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
519 520 521 522
{
    return ff_mov_read_esds(c->fc, pb, atom);
}

523
static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
524 525
{
    AVStream *st;
526
    int ac3info, acmod, lfeon, bsmod;
527

528 529
    if (c->fc->nb_streams < 1)
        return 0;
530 531
    st = c->fc->streams[c->fc->nb_streams-1];

532
    ac3info = avio_rb24(pb);
533
    bsmod = (ac3info >> 14) & 0x7;
534 535 536
    acmod = (ac3info >> 11) & 0x7;
    lfeon = (ac3info >> 10) & 0x1;
    st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
537 538 539
    st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
    if (lfeon)
        st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
540 541 542
    st->codec->audio_service_type = bsmod;
    if (st->codec->channels > 1 && bsmod == 0x7)
        st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
543 544 545 546

    return 0;
}

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
    AVStream *st;
    int eac3info, acmod, lfeon, bsmod;

    if (c->fc->nb_streams < 1)
        return 0;
    st = c->fc->streams[c->fc->nb_streams-1];

    /* No need to parse fields for additional independent substreams and its
     * associated dependent substreams since libavcodec's E-AC-3 decoder
     * does not support them yet. */
    avio_rb16(pb); /* data_rate and num_ind_sub */
    eac3info = avio_rb24(pb);
    bsmod = (eac3info >> 12) & 0x1f;
    acmod = (eac3info >>  9) & 0x7;
    lfeon = (eac3info >>  8) & 0x1;
    st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
    if (lfeon)
        st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
    st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
    st->codec->audio_service_type = bsmod;
    if (st->codec->channels > 1 && bsmod == 0x7)
        st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;

    return 0;
}

575 576 577 578 579 580 581 582 583 584 585
static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
    AVStream *st;

    if (c->fc->nb_streams < 1)
        return 0;
    st = c->fc->streams[c->fc->nb_streams-1];

    if (atom.size < 16)
        return 0;

586
    ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
587 588 589 590

    return 0;
}

591 592 593 594 595 596 597 598 599 600 601 602 603
static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
    AVStream *st;

    if (c->fc->nb_streams < 1)
        return 0;
    st = c->fc->streams[c->fc->nb_streams-1];

    ff_get_wav_header(pb, st->codec, atom.size);

    return 0;
}

604
static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
605
{
606 607
    const int num = avio_rb32(pb);
    const int den = avio_rb32(pb);
608 609 610 611 612 613
    AVStream *st;

    if (c->fc->nb_streams < 1)
        return 0;
    st = c->fc->streams[c->fc->nb_streams-1];

614 615 616 617 618 619 620
    if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
        (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
        av_log(c->fc, AV_LOG_WARNING,
               "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
               st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
               num, den);
    } else if (den != 0) {
621 622 623 624 625 626
        st->sample_aspect_ratio.num = num;
        st->sample_aspect_ratio.den = den;
    }
    return 0;
}

627
/* this atom contains actual media data */
628
static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
629
{
630
    if (atom.size == 0) /* wrong one (MP4) */
631 632 633 634 635
        return 0;
    c->found_mdat=1;
    return 0; /* now go for moov */
}

636
/* read major brand, minor version and compatible brands and store them as metadata */
637
static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
638
{
639 640 641 642 643 644
    uint32_t minor_ver;
    int comp_brand_size;
    char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
    char* comp_brands_str;
    uint8_t type[5] = {0};

645
    avio_read(pb, type, 4);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
646
    if (strcmp(type, "qt  "))
647
        c->isom = 1;
648
    av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
649
    av_dict_set(&c->fc->metadata, "major_brand", type, 0);
650
    minor_ver = avio_rb32(pb); /* minor version */
651
    snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
652
    av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
653 654 655

    comp_brand_size = atom.size - 8;
    if (comp_brand_size < 0)
656
        return AVERROR_INVALIDDATA;
657 658 659
    comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
    if (!comp_brands_str)
        return AVERROR(ENOMEM);
660
    avio_read(pb, comp_brands_str, comp_brand_size);
661
    comp_brands_str[comp_brand_size] = 0;
662
    av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
663 664
    av_freep(&comp_brands_str);

665 666 667
    return 0;
}

668
/* this atom should contain all header atoms */
669
static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
670
{
671 672 673 674
    int ret;

    if ((ret = mov_read_default(c, pb, atom)) < 0)
        return ret;
675 676 677 678 679 680
    /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
    /* so we don't parse the whole file if over a network */
    c->found_moov=1;
    return 0; /* now go for mdat */
}

681
static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
682
{
683
    c->fragment.moof_offset = avio_tell(pb) - 8;
684
    av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
685 686
    return mov_read_default(c, pb, atom);
}
687

688
static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
689 690 691
{
    char buffer[32];
    if (time) {
692
        struct tm *ptm;
693
        time -= 2082844800;  /* seconds between 1904-01-01 and Epoch */
694 695 696
        ptm = gmtime(&time);
        if (!ptm) return;
        strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
697
        av_dict_set(metadata, "creation_time", buffer, 0);
698 699 700
    }
}

701
static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
702
{
703 704 705
    AVStream *st;
    MOVStreamContext *sc;
    int version;
706
    char language[4] = {0};
707
    unsigned lang;
708
    time_t creation_time;
709

710 711 712 713 714
    if (c->fc->nb_streams < 1)
        return 0;
    st = c->fc->streams[c->fc->nb_streams-1];
    sc = st->priv_data;

715
    version = avio_r8(pb);
716 717 718 719
    if (version > 1) {
        av_log_ask_for_sample(c, "unsupported version %d\n", version);
        return AVERROR_PATCHWELCOME;
    }
720
    avio_rb24(pb); /* flags */
Baptiste Coudurier's avatar
clean  
Baptiste Coudurier committed
721
    if (version == 1) {
722 723
        creation_time = avio_rb64(pb);
        avio_rb64(pb);
Baptiste Coudurier's avatar
clean  
Baptiste Coudurier committed
724
    } else {
725 726
        creation_time = avio_rb32(pb);
        avio_rb32(pb); /* modification time */
Baptiste Coudurier's avatar
clean  
Baptiste Coudurier committed
727
    }
728
    mov_metadata_creation_time(&st->metadata, creation_time);
729

730 731
    sc->time_scale = avio_rb32(pb);
    st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
732

733
    lang = avio_rb16(pb); /* language */
734
    if (ff_mov_lang_to_iso639(lang, language))
735
        av_dict_set(&st->metadata, "language", language, 0);
736
    avio_rb16(pb); /* quality */
737 738 739 740

    return 0;
}

741
static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
742
{
743
    time_t creation_time;
744 745
    int version = avio_r8(pb); /* version */
    avio_rb24(pb); /* flags */
746

747
    if (version == 1) {
748 749
        creation_time = avio_rb64(pb);
        avio_rb64(pb);
750
    } else {
751 752
        creation_time = avio_rb32(pb);
        avio_rb32(pb); /* modification time */
753
    }
754
    mov_metadata_creation_time(&c->fc->metadata, creation_time);
755
    c->time_scale = avio_rb32(pb); /* time scale */
756

Luca Barbato's avatar
Luca Barbato committed
757
    av_dlog(c->fc, "time scale = %i\n", c->time_scale);
758

759 760
    c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
    avio_rb32(pb); /* preferred scale */
761

762
    avio_rb16(pb); /* preferred volume */
763

764
    avio_skip(pb, 10); /* reserved */
765

766
    avio_skip(pb, 36); /* display matrix */
767

768 769 770 771 772 773 774
    avio_rb32(pb); /* preview time */
    avio_rb32(pb); /* preview duration */
    avio_rb32(pb); /* poster time */
    avio_rb32(pb); /* selection time */
    avio_rb32(pb); /* selection duration */
    avio_rb32(pb); /* current time */
    avio_rb32(pb); /* next track ID */
775 776 777 778

    return 0;
}

779
static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
780
{