electronicarts.c 17 KB
Newer Older
1 2
/* Electronic Arts Multimedia File Demuxer
 * Copyright (c) 2004  The ffmpeg Project
pross's avatar
pross 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 libavformat/electronicarts.c
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 30 31
#include "avformat.h"

#define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
aurel's avatar
aurel committed
32 33 34
#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 */
35 36 37
#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 */
38
#define ISNh_TAG MKTAG('1', 'S', 'N', 'h')    /* 1SNx header */
39
#define EACS_TAG MKTAG('E', 'A', 'C', 'S')
40 41
#define ISNd_TAG MKTAG('1', 'S', 'N', 'd')    /* 1SNx data */
#define ISNe_TAG MKTAG('1', 'S', 'N', 'e')    /* 1SNx end */
42
#define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
43
#define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
44 45
#define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
#define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
46
#define kVGT_TAG MKTAG('k', 'V', 'G', 'T')    /* TGV i-frame */
47
#define fVGT_TAG MKTAG('f', 'V', 'G', 'T')    /* TGV p-frame */
48
#define mTCD_TAG MKTAG('m', 'T', 'C', 'D')    /* MDEC */
49 50
#define MADk_TAG MKTAG('M', 'A', 'D', 'k')    /* MAD i-frame */
#define MPCh_TAG MKTAG('M', 'P', 'C', 'h')    /* MPEG2 */
51 52
#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) */
53
#define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')    /* TQI/UV2 i-frame (.UV2/.WVE) */
54 55 56
#define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
#define MV0K_TAG MKTAG('M', 'V', '0', 'K')
#define MV0F_TAG MKTAG('M', 'V', '0', 'F')
57
#define MVIh_TAG MKTAG('M', 'V', 'I', 'h')    /* CMV header */
pross's avatar
pross committed
58
#define MVIf_TAG MKTAG('M', 'V', 'I', 'f')    /* CMV i-frame */
59 60

typedef struct EaDemuxContext {
61 62
    int big_endian;

63
    enum CodecID video_codec;
64
    AVRational time_base;
65
    int width, height;
66 67
    int video_stream_index;

68
    enum CodecID audio_codec;
69 70 71 72 73
    int audio_stream_index;
    int audio_frame_counter;

    int64_t audio_pts;

aurel's avatar
aurel committed
74
    int bytes;
75
    int sample_rate;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    int num_channels;
    int num_samples;
} EaDemuxContext;

static uint32_t read_arbitary(ByteIOContext *pb) {
    uint8_t size, byte;
    int i;
    uint32_t word;

    size = get_byte(pb);

    word = 0;
    for (i = 0; i < size; i++) {
        byte = get_byte(pb);
        word <<= 8;
        word |= byte;
    }

    return word;
}

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

aurel's avatar
aurel committed
108
    ea->bytes = 2;
109
    ea->sample_rate = -1;
aurel's avatar
aurel committed
110 111
    ea->num_channels = 1;

112 113 114
    while (inHeader) {
        int inSubheader;
        uint8_t byte;
aurel's avatar
aurel committed
115
        byte = get_byte(pb);
116 117 118

        switch (byte) {
        case 0xFD:
119
            av_log (s, AV_LOG_DEBUG, "entered audio subheader\n");
120 121 122
            inSubheader = 1;
            while (inSubheader) {
                uint8_t subbyte;
aurel's avatar
aurel committed
123
                subbyte = get_byte(pb);
124 125

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

176
    switch (compression_type) {
aurel's avatar
aurel committed
177
    case  0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
178
    case  7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
aurel's avatar
aurel committed
179 180 181 182 183
    case -1:
        switch (revision) {
        case  1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
        case  2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
        case  3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
184
        case -1: break;
aurel's avatar
aurel committed
185 186 187 188
        default:
            av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
            return 0;
        }
189 190
        switch (revision2) {
        case  8: ea->audio_codec = CODEC_ID_PCM_S16LE_PLANAR; break;
191
        case 10: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
192
        case 16: ea->audio_codec = CODEC_ID_MP3; break;
pross's avatar
pross committed
193
        case -1: break;
194 195 196 197
        default:
            av_log(s, AV_LOG_ERROR, "unsupported stream type; revision2=%i\n", revision2);
            return 0;
        }
aurel's avatar
aurel committed
198
        break;
199 200 201 202
    default:
        av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
        return 0;
    }
203

204 205 206
    if (ea->sample_rate == -1)
        ea->sample_rate = revision==3 ? 48000 : 22050;

207 208 209
    return 1;
}

210 211 212 213 214 215 216
/*
 * 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;
217
    ByteIOContext *pb = s->pb;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    int compression_type;

    ea->sample_rate  = ea->big_endian ? get_be32(pb) : get_le32(pb);
    ea->bytes        = get_byte(pb);   /* 1=8-bit, 2=16-bit */
    ea->num_channels = get_byte(pb);
    compression_type = get_byte(pb);
    url_fskip(pb, 13);

    switch (compression_type) {
    case 0:
        switch (ea->bytes) {
        case 1: ea->audio_codec = CODEC_ID_PCM_S8;    break;
        case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
        }
        break;
    case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
aurel's avatar
aurel committed
234
    case 2: ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_EACS; break;
235 236 237 238 239 240 241
    default:
        av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
    }

    return 1;
}

aurel's avatar
aurel committed
242 243 244 245 246 247 248
/*
 * 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;
249
    ByteIOContext *pb = s->pb;
aurel's avatar
aurel committed
250 251 252 253 254 255 256 257 258

    ea->sample_rate  = get_le32(pb);
    ea->bytes        = get_le32(pb);  /* 1=8-bit, 2=16-bit */
    ea->num_channels = get_le32(pb);
    ea->audio_codec  = CODEC_ID_ADPCM_IMA_EA_SEAD;

    return 1;
}

259 260 261 262 263 264 265 266 267 268 269 270
static int process_video_header_mdec(AVFormatContext *s)
{
    EaDemuxContext *ea = s->priv_data;
    ByteIOContext *pb = s->pb;
    url_fskip(pb, 4);
    ea->width  = get_le16(pb);
    ea->height = get_le16(pb);
    ea->time_base = (AVRational){1,15};
    ea->video_codec = CODEC_ID_MDEC;
    return 1;
}

271 272 273
static int process_video_header_vp6(AVFormatContext *s)
{
    EaDemuxContext *ea = s->priv_data;
274
    ByteIOContext *pb = s->pb;
275 276 277 278

    url_fskip(pb, 16);
    ea->time_base.den = get_le32(pb);
    ea->time_base.num = get_le32(pb);
279
    ea->video_codec = CODEC_ID_VP6;
280 281 282 283

    return 1;
}

284 285 286 287 288 289 290
/*
 * 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;
291
    ByteIOContext *pb = s->pb;
292 293 294 295
    int i;

    for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
        unsigned int startpos = url_ftell(pb);
aurel's avatar
aurel committed
296
        int err = 0;
297

aurel's avatar
aurel committed
298
        blockid = get_le32(pb);
299
        size = get_le32(pb);
300 301 302 303
        if (i == 0)
            ea->big_endian = size > 0x000FFFFF;
        if (ea->big_endian)
            size = bswap_32(size);
304 305

        switch (blockid) {
306
            case ISNh_TAG:
307 308 309 310 311 312 313
                if (get_le32(pb) != EACS_TAG) {
                    av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
                    return 0;
                }
                err = process_audio_header_eacs(s);
                break;

314
            case SCHl_TAG :
315
            case SHEN_TAG :
aurel's avatar
aurel committed
316 317 318
                blockid = get_le32(pb);
                if (blockid == GSTR_TAG) {
                    url_fskip(pb, 4);
319
                } else if ((blockid & 0xFFFF)!=PT00_TAG) {
aurel's avatar
aurel committed
320 321 322
                    av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
                    return 0;
                }
aurel's avatar
aurel committed
323
                err = process_audio_header_elements(s);
324 325
                break;

aurel's avatar
aurel committed
326 327 328 329
            case SEAD_TAG:
                err = process_audio_header_sead(s);
                break;

pross's avatar
pross committed
330 331 332 333 334
            case MVIh_TAG :
                ea->video_codec = CODEC_ID_CMV;
                ea->time_base = (AVRational){0,0};
                break;

335 336 337 338 339
            case kVGT_TAG:
                ea->video_codec = CODEC_ID_TGV;
                ea->time_base = (AVRational){0,0};
                break;

340 341 342 343
            case mTCD_TAG :
                err = process_video_header_mdec(s);
                break;

344 345 346 347
            case MPCh_TAG:
                ea->video_codec = CODEC_ID_MPEG2VIDEO;
                break;

348 349 350 351 352
            case pQGT_TAG:
            case TGQs_TAG:
                ea->video_codec = CODEC_ID_TGQ;
                break;

353 354 355 356
            case pIQT_TAG:
                ea->video_codec = CODEC_ID_TQI;
                break;

357
            case MVhd_TAG :
aurel's avatar
aurel committed
358
                err = process_video_header_vp6(s);
359 360 361
                break;
        }

aurel's avatar
aurel committed
362 363 364 365 366
        if (err < 0) {
            av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
            return err;
        }

367 368
        url_fseek(pb, startpos + size, SEEK_SET);
    }
369

370
    url_fseek(pb, 0, SEEK_SET);
371 372 373 374 375 376 377

    return 1;
}


static int ea_probe(AVProbeData *p)
{
378
    switch (AV_RL32(&p->buf[0])) {
379
    case ISNh_TAG:
380
    case SCHl_TAG:
aurel's avatar
aurel committed
381
    case SEAD_TAG:
382
    case SHEN_TAG:
383 384 385
    case kVGT_TAG:
    case MADk_TAG:
    case MPCh_TAG:
386
    case MVhd_TAG:
387
    case MVIh_TAG:
388
        return AVPROBE_SCORE_MAX;
389
    }
390
    return 0;
391 392 393 394 395
}

static int ea_read_header(AVFormatContext *s,
                          AVFormatParameters *ap)
{
396
    EaDemuxContext *ea = s->priv_data;
397 398 399
    AVStream *st;

    if (!process_ea_header(s))
400
        return AVERROR(EIO);
401

402
    if (ea->video_codec) {
aurel's avatar
aurel committed
403 404 405 406 407 408
        /* initialize the video decoder stream */
        st = av_new_stream(s, 0);
        if (!st)
            return AVERROR(ENOMEM);
        ea->video_stream_index = st->index;
        st->codec->codec_type = CODEC_TYPE_VIDEO;
409
        st->codec->codec_id = ea->video_codec;
aurel's avatar
aurel committed
410 411
        st->codec->codec_tag = 0;  /* no fourcc */
        st->codec->time_base = ea->time_base;
412 413
        st->codec->width = ea->width;
        st->codec->height = ea->height;
414
    }
415

416
    if (ea->audio_codec) {
aurel's avatar
aurel committed
417 418 419 420 421 422 423 424 425 426
        /* initialize the audio decoder stream */
        st = av_new_stream(s, 0);
        if (!st)
            return AVERROR(ENOMEM);
        av_set_pts_info(st, 33, 1, ea->sample_rate);
        st->codec->codec_type = CODEC_TYPE_AUDIO;
        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;
427
        st->codec->bits_per_coded_sample = ea->bytes * 8;
aurel's avatar
aurel committed
428
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
429 430
            st->codec->bits_per_coded_sample / 4;
        st->codec->block_align = st->codec->channels*st->codec->bits_per_coded_sample;
aurel's avatar
aurel committed
431 432
        ea->audio_stream_index = st->index;
        ea->audio_frame_counter = 0;
433
    }
434 435 436 437 438 439 440 441

    return 1;
}

static int ea_read_packet(AVFormatContext *s,
                          AVPacket *pkt)
{
    EaDemuxContext *ea = s->priv_data;
442
    ByteIOContext *pb = s->pb;
443 444 445
    int ret = 0;
    int packet_read = 0;
    unsigned int chunk_type, chunk_size;
446
    int key = 0;
447
    int av_uninit(num_samples);
448 449

    while (!packet_read) {
aurel's avatar
aurel committed
450
        chunk_type = get_le32(pb);
aurel's avatar
aurel committed
451
        chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
452 453 454

        switch (chunk_type) {
        /* audio data */
455
        case ISNh_TAG:
456 457 458
            /* header chunk also contains data; skip over the header portion*/
            url_fskip(pb, 32);
            chunk_size -= 32;
459
        case ISNd_TAG:
460
        case SCDl_TAG:
aurel's avatar
aurel committed
461
        case SNDC_TAG:
462
        case SDEN_TAG:
aurel's avatar
aurel committed
463 464 465
            if (!ea->audio_codec) {
                url_fskip(pb, chunk_size);
                break;
466 467 468 469
            } else if (ea->audio_codec == CODEC_ID_PCM_S16LE_PLANAR ||
                       ea->audio_codec == CODEC_ID_MP3) {
                num_samples = get_le32(pb);
                url_fskip(pb, 8);
470
                chunk_size -= 12;
aurel's avatar
aurel committed
471
            }
michael's avatar
michael committed
472
            ret = av_get_packet(pb, pkt, chunk_size);
473 474
            if (ret < 0)
                return ret;
reimar's avatar
reimar committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
            pkt->stream_index = ea->audio_stream_index;
            pkt->pts = 90000;
            pkt->pts *= ea->audio_frame_counter;
            pkt->pts /= ea->sample_rate;

            switch (ea->audio_codec) {
            case CODEC_ID_ADPCM_EA:
                /* 2 samples/byte, 1 or 2 samples per frame depending
                 * on stereo; chunk also has 12-byte header */
                ea->audio_frame_counter += ((chunk_size - 12) * 2) /
                    ea->num_channels;
                break;
            case CODEC_ID_PCM_S16LE_PLANAR:
            case CODEC_ID_MP3:
                ea->audio_frame_counter += num_samples;
                break;
            default:
                ea->audio_frame_counter += chunk_size /
                    (ea->bytes * ea->num_channels);
            }
495 496 497 498 499

            packet_read = 1;
            break;

        /* ending tag */
500
        case 0:
501
        case ISNe_TAG:
502
        case SCEl_TAG:
aurel's avatar
aurel committed
503
        case SEND_TAG:
504
        case SEEN_TAG:
505
            ret = AVERROR(EIO);
506 507 508
            packet_read = 1;
            break;

pross's avatar
pross committed
509
        case MVIh_TAG:
510
        case kVGT_TAG:
511 512
        case pQGT_TAG:
        case TGQs_TAG:
pross's avatar
pross committed
513 514
            key = PKT_FLAG_KEY;
        case MVIf_TAG:
515
        case fVGT_TAG:
pross's avatar
pross committed
516 517 518 519
            url_fseek(pb, -8, SEEK_CUR);     // include chunk preamble
            chunk_size += 8;
            goto get_video_packet;

520 521 522 523 524
        case mTCD_TAG:
            url_fseek(pb, 8, SEEK_CUR);  // skip ea dct header
            chunk_size -= 8;
            goto get_video_packet;

525
        case MV0K_TAG:
526
        case MPCh_TAG:
527
        case pIQT_TAG:
528 529
            key = PKT_FLAG_KEY;
        case MV0F_TAG:
pross's avatar
pross committed
530
get_video_packet:
531
            ret = av_get_packet(pb, pkt, chunk_size);
532 533
            if (ret < 0)
                return ret;
reimar's avatar
reimar committed
534 535
            pkt->stream_index = ea->video_stream_index;
            pkt->flags |= key;
536 537 538
            packet_read = 1;
            break;

539 540 541 542 543 544 545 546 547
        default:
            url_fseek(pb, chunk_size, SEEK_CUR);
            break;
        }
    }

    return ret;
}

548
AVInputFormat ea_demuxer = {
549
    "ea",
550
    NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia Format"),
551 552 553 554 555
    sizeof(EaDemuxContext),
    ea_probe,
    ea_read_header,
    ea_read_packet,
};