electronicarts.c 20 KB
Newer Older
1 2
/* Electronic Arts Multimedia File Demuxer
 * Copyright (c) 2004  The ffmpeg Project
Peter Ross's avatar
Peter Ross committed
3
 * Copyright (c) 2006-2008 Peter Ross
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21 22
 */

/**
23
 * @file
24 25 26 27
 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
 * by Robin Kay (komadori at gekkou.co.uk)
 */

28
#include "libavutil/intreadwrite.h"
29
#include "avformat.h"
30
#include "internal.h"
31 32

#define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
Peter Ross's avatar
Peter Ross committed
33 34 35
#define SEAD_TAG MKTAG('S', 'E', 'A', 'D')    /* Sxxx header */
#define SNDC_TAG MKTAG('S', 'N', 'D', 'C')    /* Sxxx data */
#define SEND_TAG MKTAG('S', 'E', 'N', 'D')    /* Sxxx end */
36 37 38
#define SHEN_TAG MKTAG('S', 'H', 'E', 'N')    /* SxEN header */
#define SDEN_TAG MKTAG('S', 'D', 'E', 'N')    /* SxEN data */
#define SEEN_TAG MKTAG('S', 'E', 'E', 'N')    /* SxEN end */
39
#define ISNh_TAG MKTAG('1', 'S', 'N', 'h')    /* 1SNx header */
40
#define EACS_TAG MKTAG('E', 'A', 'C', 'S')
41 42
#define ISNd_TAG MKTAG('1', 'S', 'N', 'd')    /* 1SNx data */
#define ISNe_TAG MKTAG('1', 'S', 'N', 'e')    /* 1SNx end */
43
#define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
44
#define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
45 46
#define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
#define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
47
#define kVGT_TAG MKTAG('k', 'V', 'G', 'T')    /* TGV i-frame */
48
#define fVGT_TAG MKTAG('f', 'V', 'G', 'T')    /* TGV p-frame */
49
#define mTCD_TAG MKTAG('m', 'T', 'C', 'D')    /* MDEC */
50
#define MADk_TAG MKTAG('M', 'A', 'D', 'k')    /* MAD i-frame */
51 52
#define MADm_TAG MKTAG('M', 'A', 'D', 'm')    /* MAD p-frame */
#define MADe_TAG MKTAG('M', 'A', 'D', 'e')    /* MAD lqp-frame */
53
#define MPCh_TAG MKTAG('M', 'P', 'C', 'h')    /* MPEG2 */
54 55
#define TGQs_TAG MKTAG('T', 'G', 'Q', 's')    /* TGQ i-frame (appears in .TGQ files) */
#define pQGT_TAG MKTAG('p', 'Q', 'G', 'T')    /* TGQ i-frame (appears in .UV files) */
56
#define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')    /* TQI/UV2 i-frame (.UV2/.WVE) */
57 58 59
#define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
#define MV0K_TAG MKTAG('M', 'V', '0', 'K')
#define MV0F_TAG MKTAG('M', 'V', '0', 'F')
60
#define MVIh_TAG MKTAG('M', 'V', 'I', 'h')    /* CMV header */
Peter Ross's avatar
Peter Ross committed
61
#define MVIf_TAG MKTAG('M', 'V', 'I', 'f')    /* CMV i-frame */
62 63

typedef struct EaDemuxContext {
64 65
    int big_endian;

66
    enum AVCodecID video_codec;
67
    AVRational time_base;
68
    int width, height;
69
    int nb_frames;
70 71
    int video_stream_index;

72
    enum AVCodecID audio_codec;
73 74
    int audio_stream_index;

75
    int bytes;
76
    int sample_rate;
77 78 79 80
    int num_channels;
    int num_samples;
} EaDemuxContext;

81
static uint32_t read_arbitary(AVIOContext *pb) {
82 83 84 85
    uint8_t size, byte;
    int i;
    uint32_t word;

86
    size = avio_r8(pb);
87 88 89

    word = 0;
    for (i = 0; i < size; i++) {
90
        byte = avio_r8(pb);
91 92 93 94 95 96 97 98
        word <<= 8;
        word |= byte;
    }

    return word;
}

/*
99 100
 * Process PT/GSTR sound header
 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
101
 */
102 103
static int process_audio_header_elements(AVFormatContext *s)
{
104
    int inHeader = 1;
105
    EaDemuxContext *ea = s->priv_data;
106
    AVIOContext *pb = s->pb;
107
    int compression_type = -1, revision = -1, revision2 = -1;
108

109
    ea->bytes = 2;
110
    ea->sample_rate = -1;
111 112
    ea->num_channels = 1;

113
    while (!url_feof(pb) && inHeader) {
114 115
        int inSubheader;
        uint8_t byte;
116
        byte = avio_r8(pb);
117 118 119

        switch (byte) {
        case 0xFD:
120
            av_log (s, AV_LOG_DEBUG, "entered audio subheader\n");
121
            inSubheader = 1;
122
            while (!url_feof(pb) && inSubheader) {
123
                uint8_t subbyte;
124
                subbyte = avio_r8(pb);
125 126

                switch (subbyte) {
127 128
                case 0x80:
                    revision = read_arbitary(pb);
129
                    av_log (s, AV_LOG_DEBUG, "revision (element 0x80) set to 0x%08x\n", revision);
130
                    break;
131 132
                case 0x82:
                    ea->num_channels = read_arbitary(pb);
133
                    av_log (s, AV_LOG_DEBUG, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
134 135
                    break;
                case 0x83:
136
                    compression_type = read_arbitary(pb);
137
                    av_log (s, AV_LOG_DEBUG, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
138
                    break;
139 140
                case 0x84:
                    ea->sample_rate = read_arbitary(pb);
141
                    av_log (s, AV_LOG_DEBUG, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
142
                    break;
143 144
                case 0x85:
                    ea->num_samples = read_arbitary(pb);
145
                    av_log (s, AV_LOG_DEBUG, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
146 147
                    break;
                case 0x8A:
148 149
                    av_log (s, AV_LOG_DEBUG, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
                    av_log (s, AV_LOG_DEBUG, "exited audio subheader\n");
150 151
                    inSubheader = 0;
                    break;
152 153
                case 0xA0:
                    revision2 = read_arbitary(pb);
154
                    av_log (s, AV_LOG_DEBUG, "revision2 (element 0xA0) set to 0x%08x\n", revision2);
155
                    break;
156
                case 0xFF:
157
                    av_log (s, AV_LOG_DEBUG, "end of header block reached (within audio subheader)\n");
158 159 160
                    inSubheader = 0;
                    inHeader = 0;
                    break;
161
                default:
162
                    av_log (s, AV_LOG_DEBUG, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
163 164 165 166 167
                    break;
                }
            }
            break;
        case 0xFF:
168
            av_log (s, AV_LOG_DEBUG, "end of header block reached\n");
169 170 171
            inHeader = 0;
            break;
        default:
172
            av_log (s, AV_LOG_DEBUG, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
173 174 175 176
            break;
        }
    }

177
    switch (compression_type) {
178 179
    case  0: ea->audio_codec = AV_CODEC_ID_PCM_S16LE; break;
    case  7: ea->audio_codec = AV_CODEC_ID_ADPCM_EA; break;
180 181
    case -1:
        switch (revision) {
182 183 184
        case  1: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
        case  2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
        case  3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3; break;
185
        case -1: break;
186
        default:
187
            avpriv_request_sample(s, "stream type; revision=%i", revision);
188 189
            return 0;
        }
190
        switch (revision2) {
191
        case  8: ea->audio_codec = AV_CODEC_ID_PCM_S16LE_PLANAR; break;
192 193 194 195 196 197
        case 10:
            switch (revision) {
            case -1:
            case  2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
            case  3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
            default:
198
                avpriv_request_sample(s, "stream type; revision=%i, revision2=%i", revision, revision2);
199 200 201
                return 0;
            }
            break;
202
        case 16: ea->audio_codec = AV_CODEC_ID_MP3; break;
203
        case -1: break;
204
        default:
205
            ea->audio_codec = AV_CODEC_ID_NONE;
206
            avpriv_request_sample(s, "stream type; revision2=%i", revision2);
207 208
            return 0;
        }
209
        break;
210
    default:
211
        avpriv_request_sample(s, "stream type; compression_type=%i", compression_type);
212 213
        return 0;
    }
214

215 216 217
    if (ea->sample_rate == -1)
        ea->sample_rate = revision==3 ? 48000 : 22050;

218 219 220
    return 1;
}

221 222 223 224 225 226 227
/*
 * Process EACS sound header
 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
 */
static int process_audio_header_eacs(AVFormatContext *s)
{
    EaDemuxContext *ea = s->priv_data;
228
    AVIOContext *pb = s->pb;
229 230
    int compression_type;

231 232 233 234
    ea->sample_rate  = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
    ea->bytes        = avio_r8(pb);   /* 1=8-bit, 2=16-bit */
    ea->num_channels = avio_r8(pb);
    compression_type = avio_r8(pb);
235
    avio_skip(pb, 13);
236 237 238 239

    switch (compression_type) {
    case 0:
        switch (ea->bytes) {
240 241
        case 1: ea->audio_codec = AV_CODEC_ID_PCM_S8;    break;
        case 2: ea->audio_codec = AV_CODEC_ID_PCM_S16LE; break;
242 243
        }
        break;
244 245
    case 1: ea->audio_codec = AV_CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
    case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS; break;
246
    default:
247
        avpriv_request_sample(s, "stream type; audio compression_type=%i", compression_type);
248 249 250 251 252
    }

    return 1;
}

Peter Ross's avatar
Peter Ross committed
253 254 255 256 257 258 259
/*
 * Process SEAD sound header
 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
 */
static int process_audio_header_sead(AVFormatContext *s)
{
    EaDemuxContext *ea = s->priv_data;
260
    AVIOContext *pb = s->pb;
Peter Ross's avatar
Peter Ross committed
261

262 263 264
    ea->sample_rate  = avio_rl32(pb);
    ea->bytes        = avio_rl32(pb);  /* 1=8-bit, 2=16-bit */
    ea->num_channels = avio_rl32(pb);
265
    ea->audio_codec  = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
Peter Ross's avatar
Peter Ross committed
266 267 268 269

    return 1;
}

270 271 272
static int process_video_header_mdec(AVFormatContext *s)
{
    EaDemuxContext *ea = s->priv_data;
273
    AVIOContext *pb = s->pb;
274
    avio_skip(pb, 4);
275 276
    ea->width  = avio_rl16(pb);
    ea->height = avio_rl16(pb);
277
    ea->time_base = (AVRational){1,15};
278
    ea->video_codec = AV_CODEC_ID_MDEC;
279 280 281
    return 1;
}

282 283 284
static int process_video_header_vp6(AVFormatContext *s)
{
    EaDemuxContext *ea = s->priv_data;
285
    AVIOContext *pb = s->pb;
286

287 288 289
    avio_skip(pb, 8);
    ea->nb_frames = avio_rl32(pb);
    avio_skip(pb, 4);
290 291
    ea->time_base.den = avio_rl32(pb);
    ea->time_base.num = avio_rl32(pb);
292 293 294 295
    if (ea->time_base.den <= 0 || ea->time_base.num <= 0) {
        av_log(s, AV_LOG_ERROR, "Timebase is invalid\n");
        return AVERROR_INVALIDDATA;
    }
296
    ea->video_codec = AV_CODEC_ID_VP6;
297 298 299 300

    return 1;
}

301 302 303 304 305 306 307 308 309
static int process_video_header_cmv(AVFormatContext *s)
{
    EaDemuxContext *ea = s->priv_data;
    int fps;

    avio_skip(s->pb, 10);
    fps = avio_rl16(s->pb);
    if (fps)
        ea->time_base = (AVRational){1, fps};
310
    ea->video_codec = AV_CODEC_ID_CMV;
311 312 313 314

    return 0;
}

315 316 317 318 319 320 321
/*
 * Process EA file header
 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
 */
static int process_ea_header(AVFormatContext *s) {
    uint32_t blockid, size = 0;
    EaDemuxContext *ea = s->priv_data;
322
    AVIOContext *pb = s->pb;
323 324 325
    int i;

    for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
326
        unsigned int startpos = avio_tell(pb);
327
        int err = 0;
328

329 330
        blockid = avio_rl32(pb);
        size = avio_rl32(pb);
331 332 333
        if (i == 0)
            ea->big_endian = size > 0x000FFFFF;
        if (ea->big_endian)
334
            size = av_bswap32(size);
335 336

        switch (blockid) {
337
            case ISNh_TAG:
338
                if (avio_rl32(pb) != EACS_TAG) {
339
                    avpriv_request_sample(s, "unknown 1SNh headerid");
340 341 342 343 344
                    return 0;
                }
                err = process_audio_header_eacs(s);
                break;

345
            case SCHl_TAG :
346
            case SHEN_TAG :
347
                blockid = avio_rl32(pb);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
348
                if (blockid == GSTR_TAG) {
349
                    avio_skip(pb, 4);
350
                } else if ((blockid & 0xFFFF)!=PT00_TAG) {
351
                    avpriv_request_sample(s, "unknown SCHl headerid");
Aurelien Jacobs's avatar
Aurelien Jacobs committed
352 353
                    return 0;
                }
354
                err = process_audio_header_elements(s);
355 356
                break;

Peter Ross's avatar
Peter Ross committed
357 358 359 360
            case SEAD_TAG:
                err = process_audio_header_sead(s);
                break;

Peter Ross's avatar
Peter Ross committed
361
            case MVIh_TAG :
362
                err = process_video_header_cmv(s);
Peter Ross's avatar
Peter Ross committed
363 364
                break;

365
            case kVGT_TAG:
366
                ea->video_codec = AV_CODEC_ID_TGV;
367 368
                break;

369 370 371 372
            case mTCD_TAG :
                err = process_video_header_mdec(s);
                break;

373
            case MPCh_TAG:
374
                ea->video_codec = AV_CODEC_ID_MPEG2VIDEO;
375 376
                break;

377 378
            case pQGT_TAG:
            case TGQs_TAG:
379
                ea->video_codec = AV_CODEC_ID_TGQ;
380 381
                break;

382
            case pIQT_TAG:
383
                ea->video_codec = AV_CODEC_ID_TQI;
384 385
                break;

386
            case MADk_TAG :
387
                ea->video_codec = AV_CODEC_ID_MAD;
388 389
                break;

390
            case MVhd_TAG :
391
                err = process_video_header_vp6(s);
392 393 394
                break;
        }

395 396 397 398 399
        if (err < 0) {
            av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
            return err;
        }

400
        avio_seek(pb, startpos + size, SEEK_SET);
401
    }
402

403
    avio_seek(pb, 0, SEEK_SET);
404 405 406 407 408 409 410

    return 1;
}


static int ea_probe(AVProbeData *p)
{
411
    switch (AV_RL32(&p->buf[0])) {
412
    case ISNh_TAG:
413
    case SCHl_TAG:
Peter Ross's avatar
Peter Ross committed
414
    case SEAD_TAG:
415
    case SHEN_TAG:
416 417 418
    case kVGT_TAG:
    case MADk_TAG:
    case MPCh_TAG:
419
    case MVhd_TAG:
420
    case MVIh_TAG:
421 422 423
        break;
    default:
        return 0;
424
    }
425 426 427
    if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
        return 0;
    return AVPROBE_SCORE_MAX;
428 429
}

430
static int ea_read_header(AVFormatContext *s)
431
{
432
    EaDemuxContext *ea = s->priv_data;
433 434
    AVStream *st;

435
    if (process_ea_header(s)<=0)
436
        return AVERROR(EIO);
437

438
    if (ea->video_codec) {
Aurelien Jacobs's avatar
Aurelien Jacobs committed
439
        /* initialize the video decoder stream */
440
        st = avformat_new_stream(s, NULL);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
441 442 443
        if (!st)
            return AVERROR(ENOMEM);
        ea->video_stream_index = st->index;
444
        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
445
        st->codec->codec_id = ea->video_codec;
446
        // parsing is necessary to make FFmpeg generate correct timestamps
447
        if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
448
            st->need_parsing = AVSTREAM_PARSE_HEADERS;
Aurelien Jacobs's avatar
Aurelien Jacobs committed
449
        st->codec->codec_tag = 0;  /* no fourcc */
450 451
        st->codec->width = ea->width;
        st->codec->height = ea->height;
452
        st->duration = st->nb_frames = ea->nb_frames;
453 454
        if (ea->time_base.num)
            avpriv_set_pts_info(st, 64, ea->time_base.num, ea->time_base.den);
455
        st->r_frame_rate =
456
        st->avg_frame_rate = av_inv_q(ea->time_base);
457
    }
458

459
    if (ea->audio_codec) {
460 461 462 463 464 465 466 467 468 469
        if (ea->num_channels <= 0) {
            av_log(s, AV_LOG_WARNING, "Unsupported number of channels: %d\n", ea->num_channels);
            ea->audio_codec = 0;
            return 1;
        }
        if (ea->sample_rate <= 0) {
            av_log(s, AV_LOG_ERROR, "Unsupported sample rate: %d\n", ea->sample_rate);
            ea->audio_codec = 0;
            return 1;
        }
470 471
        if (ea->bytes <= 0) {
            av_log(s, AV_LOG_ERROR, "Invalid number of bytes per sample: %d\n", ea->bytes);
472
            ea->audio_codec = AV_CODEC_ID_NONE;
473 474
            return 1;
        }
475

Aurelien Jacobs's avatar
Aurelien Jacobs committed
476
        /* initialize the audio decoder stream */
477
        st = avformat_new_stream(s, NULL);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
478 479
        if (!st)
            return AVERROR(ENOMEM);
480
        avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
481
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
Aurelien Jacobs's avatar
Aurelien Jacobs committed
482 483 484 485
        st->codec->codec_id = ea->audio_codec;
        st->codec->codec_tag = 0;  /* no tag */
        st->codec->channels = ea->num_channels;
        st->codec->sample_rate = ea->sample_rate;
486
        st->codec->bits_per_coded_sample = ea->bytes * 8;
Aurelien Jacobs's avatar
Aurelien Jacobs committed
487
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
488 489
            st->codec->bits_per_coded_sample / 4;
        st->codec->block_align = st->codec->channels*st->codec->bits_per_coded_sample;
Aurelien Jacobs's avatar
Aurelien Jacobs committed
490
        ea->audio_stream_index = st->index;
Justin Ruggles's avatar
Justin Ruggles committed
491
        st->start_time = 0;
492
    }
493 494 495 496 497 498 499 500

    return 1;
}

static int ea_read_packet(AVFormatContext *s,
                          AVPacket *pkt)
{
    EaDemuxContext *ea = s->priv_data;
501
    AVIOContext *pb = s->pb;
502 503
    int ret = 0;
    int packet_read = 0;
504
    int partial_packet = 0;
505
    unsigned int chunk_type, chunk_size;
506
    int key = 0;
507
    int av_uninit(num_samples);
508

509
    while (!packet_read || partial_packet) {
510
        chunk_type = avio_rl32(pb);
511 512 513 514
        chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
        if (chunk_size <= 8)
            return AVERROR_INVALIDDATA;
        chunk_size -= 8;
515 516 517

        switch (chunk_type) {
        /* audio data */
518
        case ISNh_TAG:
519
            /* header chunk also contains data; skip over the header portion*/
520 521
            if (chunk_size < 32)
                return AVERROR_INVALIDDATA;
522
            avio_skip(pb, 32);
523
            chunk_size -= 32;
524
        case ISNd_TAG:
525
        case SCDl_TAG:
Peter Ross's avatar
Peter Ross committed
526
        case SNDC_TAG:
527
        case SDEN_TAG:
528
            if (!ea->audio_codec) {
529
                avio_skip(pb, chunk_size);
530
                break;
531 532
            } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
                       ea->audio_codec == AV_CODEC_ID_MP3) {
533
                num_samples = avio_rl32(pb);
534
                avio_skip(pb, 8);
535
                chunk_size -= 12;
536
            }
537
            if (partial_packet) {
538
                avpriv_request_sample(s, "video header followed by audio packet");
539 540 541
                av_free_packet(pkt);
                partial_packet = 0;
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
542
            ret = av_get_packet(pb, pkt, chunk_size);
543 544
            if (ret < 0)
                return ret;
Reimar Döffinger's avatar
Reimar Döffinger committed
545 546 547
            pkt->stream_index = ea->audio_stream_index;

            switch (ea->audio_codec) {
548 549 550 551
            case AV_CODEC_ID_ADPCM_EA:
            case AV_CODEC_ID_ADPCM_EA_R1:
            case AV_CODEC_ID_ADPCM_EA_R2:
            case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
552 553
                if (pkt->size >= 4)
                    pkt->duration = AV_RL32(pkt->data);
Justin Ruggles's avatar
Justin Ruggles committed
554
                break;
555
            case AV_CODEC_ID_ADPCM_EA_R3:
556 557
                if (pkt->size >= 4)
                    pkt->duration = AV_RB32(pkt->data);
Justin Ruggles's avatar
Justin Ruggles committed
558
                break;
559
            case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
Justin Ruggles's avatar
Justin Ruggles committed
560
                pkt->duration = ret * 2 / ea->num_channels;
Reimar Döffinger's avatar
Reimar Döffinger committed
561
                break;
562 563
            case AV_CODEC_ID_PCM_S16LE_PLANAR:
            case AV_CODEC_ID_MP3:
Justin Ruggles's avatar
Justin Ruggles committed
564
                pkt->duration = num_samples;
Reimar Döffinger's avatar
Reimar Döffinger committed
565 566
                break;
            default:
Justin Ruggles's avatar
Justin Ruggles committed
567
                pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
Reimar Döffinger's avatar
Reimar Döffinger committed
568
            }
569 570 571 572 573

            packet_read = 1;
            break;

        /* ending tag */
574
        case 0:
575
        case ISNe_TAG:
576
        case SCEl_TAG:
Peter Ross's avatar
Peter Ross committed
577
        case SEND_TAG:
578
        case SEEN_TAG:
579
            ret = AVERROR(EIO);
580 581 582
            packet_read = 1;
            break;

Peter Ross's avatar
Peter Ross committed
583
        case MVIh_TAG:
584
        case kVGT_TAG:
585 586
        case pQGT_TAG:
        case TGQs_TAG:
587
        case MADk_TAG:
588
            key = AV_PKT_FLAG_KEY;
Peter Ross's avatar
Peter Ross committed
589
        case MVIf_TAG:
590
        case fVGT_TAG:
591 592
        case MADm_TAG:
        case MADe_TAG:
593
            avio_seek(pb, -8, SEEK_CUR);     // include chunk preamble
Peter Ross's avatar
Peter Ross committed
594 595 596
            chunk_size += 8;
            goto get_video_packet;

597
        case mTCD_TAG:
598
            avio_skip(pb, 8);  // skip ea dct header
599 600 601
            chunk_size -= 8;
            goto get_video_packet;

602
        case MV0K_TAG:
603
        case MPCh_TAG:
604
        case pIQT_TAG:
605
            key = AV_PKT_FLAG_KEY;
606
        case MV0F_TAG:
Peter Ross's avatar
Peter Ross committed
607
get_video_packet:
608 609 610 611 612 613 614 615 616
            if (partial_packet) {
                ret = av_append_packet(pb, pkt, chunk_size);
            } else
                ret = av_get_packet(pb, pkt, chunk_size);
            if (ret < 0) {
                packet_read = 1;
                break;
            }
            partial_packet = chunk_type == MVIh_TAG;
Reimar Döffinger's avatar
Reimar Döffinger committed
617 618
            pkt->stream_index = ea->video_stream_index;
            pkt->flags |= key;
619 620 621
            packet_read = 1;
            break;

622
        default:
623
            avio_skip(pb, chunk_size);
624 625 626 627
            break;
        }
    }

628 629
    if (ret < 0 && partial_packet)
        av_free_packet(pkt);
630 631 632
    return ret;
}

633
AVInputFormat ff_ea_demuxer = {
634
    .name           = "ea",
635
    .long_name      = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
636 637 638 639
    .priv_data_size = sizeof(EaDemuxContext),
    .read_probe     = ea_probe,
    .read_header    = ea_read_header,
    .read_packet    = ea_read_packet,
640
};