Statistics
| Branch: | Revision:

ffmpeg / libavformat / electronicarts.c @ e19e051e

History | View | Annotate | Download (17.9 KB)

1
/* Electronic Arts Multimedia File Demuxer
2
 * Copyright (c) 2004  The ffmpeg Project
3
 * Copyright (c) 2006-2008 Peter Ross
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * 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
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * 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
 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
25
 * by Robin Kay (komadori at gekkou.co.uk)
26
 */
27

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

    
31
#define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
32
#define SEAD_TAG MKTAG('S', 'E', 'A', 'D')    /* Sxxx header */
33
#define SNDC_TAG MKTAG('S', 'N', 'D', 'C')    /* Sxxx data */
34
#define SEND_TAG MKTAG('S', 'E', 'N', 'D')    /* Sxxx end */
35
#define SHEN_TAG MKTAG('S', 'H', 'E', 'N')    /* SxEN header */
36
#define SDEN_TAG MKTAG('S', 'D', 'E', 'N')    /* SxEN data */
37
#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
#define ISNd_TAG MKTAG('1', 'S', 'N', 'd')    /* 1SNx data */
41
#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
#define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
45
#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
#define MADk_TAG MKTAG('M', 'A', 'D', 'k')    /* MAD i-frame */
50
#define MADm_TAG MKTAG('M', 'A', 'D', 'm')    /* MAD p-frame */
51
#define MADe_TAG MKTAG('M', 'A', 'D', 'e')    /* MAD lqp-frame */
52
#define MPCh_TAG MKTAG('M', 'P', 'C', 'h')    /* MPEG2 */
53
#define TGQs_TAG MKTAG('T', 'G', 'Q', 's')    /* TGQ i-frame (appears in .TGQ files) */
54
#define pQGT_TAG MKTAG('p', 'Q', 'G', 'T')    /* TGQ i-frame (appears in .UV files) */
55
#define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')    /* TQI/UV2 i-frame (.UV2/.WVE) */
56
#define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
57
#define MV0K_TAG MKTAG('M', 'V', '0', 'K')
58
#define MV0F_TAG MKTAG('M', 'V', '0', 'F')
59
#define MVIh_TAG MKTAG('M', 'V', 'I', 'h')    /* CMV header */
60
#define MVIf_TAG MKTAG('M', 'V', 'I', 'f')    /* CMV i-frame */
61

    
62
typedef struct EaDemuxContext {
63
    int big_endian;
64

    
65
    enum CodecID video_codec;
66
    AVRational time_base;
67
    int width, height;
68
    int video_stream_index;
69

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

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

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

    
85
    size = get_byte(pb);
86

    
87
    word = 0;
88
    for (i = 0; i < size; i++) {
89
        byte = get_byte(pb);
90
        word <<= 8;
91
        word |= byte;
92
    }
93

    
94
    return word;
95
}
96

    
97
/*
98
 * Process PT/GSTR sound header
99
 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
100
 */
101
static int process_audio_header_elements(AVFormatContext *s)
102
{
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

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

    
112
    while (inHeader) {
113
        int inSubheader;
114
        uint8_t byte;
115
        byte = get_byte(pb);
116
        if (!byte)
117
            break;
118

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

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

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

    
207
    if (ea->sample_rate == -1)
208
        ea->sample_rate = revision==3 ? 48000 : 22050;
209

    
210
    return 1;
211
}
212

    
213
/*
214
 * Process EACS sound header
215
 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
216
 */
217
static int process_audio_header_eacs(AVFormatContext *s)
218
{
219
    EaDemuxContext *ea = s->priv_data;
220
    ByteIOContext *pb = s->pb;
221
    int compression_type;
222

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

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

    
242
    return 1;
243
}
244

    
245
/*
246
 * Process SEAD sound header
247
 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
248
 */
249
static int process_audio_header_sead(AVFormatContext *s)
250
{
251
    EaDemuxContext *ea = s->priv_data;
252
    ByteIOContext *pb = s->pb;
253

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

    
259
    return 1;
260
}
261

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

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

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

    
284
    return 1;
285
}
286

    
287
/*
288
 * Process EA file header
289
 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
290
 */
291
static int process_ea_header(AVFormatContext *s) {
292
    uint32_t blockid, size = 0;
293
    EaDemuxContext *ea = s->priv_data;
294
    ByteIOContext *pb = s->pb;
295
    int i;
296

    
297
    for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
298
        unsigned int startpos = url_ftell(pb);
299
        int err = 0;
300

    
301
        blockid = get_le32(pb);
302
        size = get_le32(pb);
303
        if (i == 0)
304
            ea->big_endian = size > 0x000FFFFF;
305
        if (ea->big_endian)
306
            size = av_bswap32(size);
307

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

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

    
329
            case SEAD_TAG:
330
                err = process_audio_header_sead(s);
331
                break;
332

    
333
            case MVIh_TAG :
334
                ea->video_codec = CODEC_ID_CMV;
335
                ea->time_base = (AVRational){0,0};
336
                break;
337

    
338
            case kVGT_TAG:
339
                ea->video_codec = CODEC_ID_TGV;
340
                ea->time_base = (AVRational){0,0};
341
                break;
342

    
343
            case mTCD_TAG :
344
                err = process_video_header_mdec(s);
345
                break;
346

    
347
            case MPCh_TAG:
348
                ea->video_codec = CODEC_ID_MPEG2VIDEO;
349
                break;
350

    
351
            case pQGT_TAG:
352
            case TGQs_TAG:
353
                ea->video_codec = CODEC_ID_TGQ;
354
                break;
355

    
356
            case pIQT_TAG:
357
                ea->video_codec = CODEC_ID_TQI;
358
                break;
359

    
360
            case MADk_TAG :
361
                ea->video_codec = CODEC_ID_MAD;
362
                break;
363

    
364
            case MVhd_TAG :
365
                err = process_video_header_vp6(s);
366
                break;
367
        }
368

    
369
        if (err < 0) {
370
            av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
371
            return err;
372
        }
373

    
374
        url_fseek(pb, startpos + size, SEEK_SET);
375
    }
376

    
377
    url_fseek(pb, 0, SEEK_SET);
378

    
379
    return 1;
380
}
381

    
382

    
383
static int ea_probe(AVProbeData *p)
384
{
385
    switch (AV_RL32(&p->buf[0])) {
386
    case ISNh_TAG:
387
    case SCHl_TAG:
388
    case SEAD_TAG:
389
    case SHEN_TAG:
390
    case kVGT_TAG:
391
    case MADk_TAG:
392
    case MPCh_TAG:
393
    case MVhd_TAG:
394
    case MVIh_TAG:
395
        break;
396
    default:
397
        return 0;
398
    }
399
    if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
400
        return 0;
401
    return AVPROBE_SCORE_MAX;
402
}
403

    
404
static int ea_read_header(AVFormatContext *s,
405
                          AVFormatParameters *ap)
406
{
407
    EaDemuxContext *ea = s->priv_data;
408
    AVStream *st;
409

    
410
    if (!process_ea_header(s))
411
        return AVERROR(EIO);
412

    
413
    if (ea->video_codec) {
414
        /* initialize the video decoder stream */
415
        st = av_new_stream(s, 0);
416
        if (!st)
417
            return AVERROR(ENOMEM);
418
        ea->video_stream_index = st->index;
419
        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
420
        st->codec->codec_id = ea->video_codec;
421
        st->codec->codec_tag = 0;  /* no fourcc */
422
        st->codec->time_base = ea->time_base;
423
        st->codec->width = ea->width;
424
        st->codec->height = ea->height;
425
    }
426

    
427
    if (ea->audio_codec) {
428
        if (ea->num_channels <= 0) {
429
            av_log(s, AV_LOG_WARNING, "Unsupported number of channels: %d\n", ea->num_channels);
430
            ea->audio_codec = 0;
431
            return 1;
432
        }
433
        if (ea->sample_rate <= 0) {
434
            av_log(s, AV_LOG_ERROR, "Unsupported sample rate: %d\n", ea->sample_rate);
435
            ea->audio_codec = 0;
436
            return 1;
437
        }
438

    
439
        /* initialize the audio decoder stream */
440
        st = av_new_stream(s, 0);
441
        if (!st)
442
            return AVERROR(ENOMEM);
443
        av_set_pts_info(st, 33, 1, ea->sample_rate);
444
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
445
        st->codec->codec_id = ea->audio_codec;
446
        st->codec->codec_tag = 0;  /* no tag */
447
        st->codec->channels = ea->num_channels;
448
        st->codec->sample_rate = ea->sample_rate;
449
        st->codec->bits_per_coded_sample = ea->bytes * 8;
450
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
451
            st->codec->bits_per_coded_sample / 4;
452
        st->codec->block_align = st->codec->channels*st->codec->bits_per_coded_sample;
453
        ea->audio_stream_index = st->index;
454
        ea->audio_frame_counter = 0;
455
    }
456

    
457
    return 1;
458
}
459

    
460
static int ea_read_packet(AVFormatContext *s,
461
                          AVPacket *pkt)
462
{
463
    EaDemuxContext *ea = s->priv_data;
464
    ByteIOContext *pb = s->pb;
465
    int ret = 0;
466
    int packet_read = 0;
467
    unsigned int chunk_type, chunk_size;
468
    int key = 0;
469
    int av_uninit(num_samples);
470

    
471
    while (!packet_read) {
472
        chunk_type = get_le32(pb);
473
        chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
474

    
475
        switch (chunk_type) {
476
        /* audio data */
477
        case ISNh_TAG:
478
            /* header chunk also contains data; skip over the header portion*/
479
            url_fskip(pb, 32);
480
            chunk_size -= 32;
481
        case ISNd_TAG:
482
        case SCDl_TAG:
483
        case SNDC_TAG:
484
        case SDEN_TAG:
485
            if (!ea->audio_codec) {
486
                url_fskip(pb, chunk_size);
487
                break;
488
            } else if (ea->audio_codec == CODEC_ID_PCM_S16LE_PLANAR ||
489
                       ea->audio_codec == CODEC_ID_MP3) {
490
                num_samples = get_le32(pb);
491
                url_fskip(pb, 8);
492
                chunk_size -= 12;
493
            }
494
            ret = av_get_packet(pb, pkt, chunk_size);
495
            if (ret < 0)
496
                return ret;
497
            pkt->stream_index = ea->audio_stream_index;
498
            pkt->pts = 90000;
499
            pkt->pts *= ea->audio_frame_counter;
500
            pkt->pts /= ea->sample_rate;
501

    
502
            switch (ea->audio_codec) {
503
            case CODEC_ID_ADPCM_EA:
504
                /* 2 samples/byte, 1 or 2 samples per frame depending
505
                 * on stereo; chunk also has 12-byte header */
506
                ea->audio_frame_counter += ((chunk_size - 12) * 2) /
507
                    ea->num_channels;
508
                break;
509
            case CODEC_ID_PCM_S16LE_PLANAR:
510
            case CODEC_ID_MP3:
511
                ea->audio_frame_counter += num_samples;
512
                break;
513
            default:
514
                ea->audio_frame_counter += chunk_size /
515
                    (ea->bytes * ea->num_channels);
516
            }
517

    
518
            packet_read = 1;
519
            break;
520

    
521
        /* ending tag */
522
        case 0:
523
        case ISNe_TAG:
524
        case SCEl_TAG:
525
        case SEND_TAG:
526
        case SEEN_TAG:
527
            ret = AVERROR(EIO);
528
            packet_read = 1;
529
            break;
530

    
531
        case MVIh_TAG:
532
        case kVGT_TAG:
533
        case pQGT_TAG:
534
        case TGQs_TAG:
535
        case MADk_TAG:
536
            key = AV_PKT_FLAG_KEY;
537
        case MVIf_TAG:
538
        case fVGT_TAG:
539
        case MADm_TAG:
540
        case MADe_TAG:
541
            url_fseek(pb, -8, SEEK_CUR);     // include chunk preamble
542
            chunk_size += 8;
543
            goto get_video_packet;
544

    
545
        case mTCD_TAG:
546
            url_fseek(pb, 8, SEEK_CUR);  // skip ea dct header
547
            chunk_size -= 8;
548
            goto get_video_packet;
549

    
550
        case MV0K_TAG:
551
        case MPCh_TAG:
552
        case pIQT_TAG:
553
            key = AV_PKT_FLAG_KEY;
554
        case MV0F_TAG:
555
get_video_packet:
556
            ret = av_get_packet(pb, pkt, chunk_size);
557
            if (ret < 0)
558
                return ret;
559
            pkt->stream_index = ea->video_stream_index;
560
            pkt->flags |= key;
561
            packet_read = 1;
562
            break;
563

    
564
        default:
565
            url_fseek(pb, chunk_size, SEEK_CUR);
566
            break;
567
        }
568
    }
569

    
570
    return ret;
571
}
572

    
573
AVInputFormat ea_demuxer = {
574
    "ea",
575
    NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia Format"),
576
    sizeof(EaDemuxContext),
577
    ea_probe,
578
    ea_read_header,
579
    ea_read_packet,
580
};