Statistics
| Branch: | Revision:

ffmpeg / libavformat / electronicarts.c @ 890d2799

History | View | Annotate | Download (14.4 KB)

1 ad81a9fe Mike Melanson
/* Electronic Arts Multimedia File Demuxer
2
 * Copyright (c) 2004  The ffmpeg Project
3
 *
4 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7 ad81a9fe Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
10 ad81a9fe Mike Melanson
 *
11 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
12 ad81a9fe Mike Melanson
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
18 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ad81a9fe Mike Melanson
 */
20
21
/**
22
 * @file electronicarts.c
23
 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
24
 * by Robin Kay (komadori at gekkou.co.uk)
25
 */
26
27
#include "avformat.h"
28
29
#define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
30 7bb65d89 Peter Ross
#define SEAD_TAG MKTAG('S', 'E', 'A', 'D')    /* Sxxx header */
31
#define SNDC_TAG MKTAG('S', 'N', 'D', 'C')    /* Sxxx data */
32
#define SEND_TAG MKTAG('S', 'E', 'N', 'D')    /* Sxxx end */
33 94e8c227 Aurelien Jacobs
#define ISNh_TAG MKTAG('1', 'S', 'N', 'h')    /* 1SNx header */
34 98168220 Aurelien Jacobs
#define EACS_TAG MKTAG('E', 'A', 'C', 'S')
35 94e8c227 Aurelien Jacobs
#define ISNd_TAG MKTAG('1', 'S', 'N', 'd')    /* 1SNx data */
36
#define ISNe_TAG MKTAG('1', 'S', 'N', 'e')    /* 1SNx end */
37 ad81a9fe Mike Melanson
#define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
38 d3e412d0 Aurelien Jacobs
#define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
39 ad81a9fe Mike Melanson
#define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
40
#define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
41 968fc6b9 Aurelien Jacobs
#define kVGT_TAG MKTAG('k', 'V', 'G', 'T')    /* TGV i-frame */
42
#define MADk_TAG MKTAG('M', 'A', 'D', 'k')    /* MAD i-frame */
43
#define MPCh_TAG MKTAG('M', 'P', 'C', 'h')    /* MPEG2 */
44 d3e412d0 Aurelien Jacobs
#define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
45
#define MV0K_TAG MKTAG('M', 'V', '0', 'K')
46
#define MV0F_TAG MKTAG('M', 'V', '0', 'F')
47 968fc6b9 Aurelien Jacobs
#define MVIh_TAG MKTAG('M', 'V', 'I', 'h')    /* CMV header */
48 ad81a9fe Mike Melanson
49
typedef struct EaDemuxContext {
50 97e5dcc0 Aurelien Jacobs
    int big_endian;
51
52 af704ee4 Aurelien Jacobs
    int video_codec;
53 d3e412d0 Aurelien Jacobs
    AVRational time_base;
54 ad81a9fe Mike Melanson
    int video_stream_index;
55
56 af704ee4 Aurelien Jacobs
    int audio_codec;
57 ad81a9fe Mike Melanson
    int audio_stream_index;
58
    int audio_frame_counter;
59
60
    int64_t audio_pts;
61
62 ffbd0bcd Aurelien Jacobs
    int bytes;
63 6c867e04 Aurelien Jacobs
    int sample_rate;
64 ad81a9fe Mike Melanson
    int num_channels;
65
    int num_samples;
66
} EaDemuxContext;
67
68
static uint32_t read_arbitary(ByteIOContext *pb) {
69
    uint8_t size, byte;
70
    int i;
71
    uint32_t word;
72
73
    size = get_byte(pb);
74
75
    word = 0;
76
    for (i = 0; i < size; i++) {
77
        byte = get_byte(pb);
78
        word <<= 8;
79
        word |= byte;
80
    }
81
82
    return word;
83
}
84
85
/*
86 215eb102 Aurelien Jacobs
 * Process PT/GSTR sound header
87
 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
88 ad81a9fe Mike Melanson
 */
89 215eb102 Aurelien Jacobs
static int process_audio_header_elements(AVFormatContext *s)
90
{
91 a066c5b7 Aurelien Jacobs
    int inHeader = 1;
92 e4141433 Nicholas Tung
    EaDemuxContext *ea = s->priv_data;
93 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
94 725d86bf Aurelien Jacobs
    int compression_type = -1, revision = -1, revision2 = -1;
95 ad81a9fe Mike Melanson
96 ffbd0bcd Aurelien Jacobs
    ea->bytes = 2;
97 6c867e04 Aurelien Jacobs
    ea->sample_rate = -1;
98 9853ce80 Aurelien Jacobs
    ea->num_channels = 1;
99
100 ad81a9fe Mike Melanson
    while (inHeader) {
101
        int inSubheader;
102
        uint8_t byte;
103 c03c4cac Aurelien Jacobs
        byte = get_byte(pb);
104 ad81a9fe Mike Melanson
105
        switch (byte) {
106
        case 0xFD:
107
            av_log (s, AV_LOG_INFO, "entered audio subheader\n");
108
            inSubheader = 1;
109
            while (inSubheader) {
110
                uint8_t subbyte;
111 c03c4cac Aurelien Jacobs
                subbyte = get_byte(pb);
112 ad81a9fe Mike Melanson
113
                switch (subbyte) {
114 6c867e04 Aurelien Jacobs
                case 0x80:
115
                    revision = read_arbitary(pb);
116
                    av_log (s, AV_LOG_INFO, "revision (element 0x80) set to 0x%08x\n", revision);
117
                    break;
118 ad81a9fe Mike Melanson
                case 0x82:
119
                    ea->num_channels = read_arbitary(pb);
120
                    av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
121
                    break;
122
                case 0x83:
123 e5d34ab6 Aurelien Jacobs
                    compression_type = read_arbitary(pb);
124
                    av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
125 ad81a9fe Mike Melanson
                    break;
126 6c867e04 Aurelien Jacobs
                case 0x84:
127
                    ea->sample_rate = read_arbitary(pb);
128
                    av_log (s, AV_LOG_INFO, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
129
                    break;
130 ad81a9fe Mike Melanson
                case 0x85:
131
                    ea->num_samples = read_arbitary(pb);
132
                    av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
133
                    break;
134
                case 0x8A:
135
                    av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
136
                    av_log (s, AV_LOG_INFO, "exited audio subheader\n");
137
                    inSubheader = 0;
138
                    break;
139 725d86bf Aurelien Jacobs
                case 0xA0:
140
                    revision2 = read_arbitary(pb);
141
                    av_log (s, AV_LOG_INFO, "revision2 (element 0xA0) set to 0x%08x\n", revision2);
142
                    break;
143 d3e412d0 Aurelien Jacobs
                case 0xFF:
144 d51bd73c Aurelien Jacobs
                    av_log (s, AV_LOG_INFO, "end of header block reached (within audio subheader)\n");
145 d3e412d0 Aurelien Jacobs
                    inSubheader = 0;
146
                    inHeader = 0;
147
                    break;
148 ad81a9fe Mike Melanson
                default:
149
                    av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
150
                    break;
151
                }
152
            }
153
            break;
154
        case 0xFF:
155
            av_log (s, AV_LOG_INFO, "end of header block reached\n");
156
            inHeader = 0;
157
            break;
158
        default:
159
            av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
160
            break;
161
        }
162
    }
163
164 93fa8b2b Aurelien Jacobs
    switch (compression_type) {
165 fd402a5a Aurelien Jacobs
    case  0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
166 93fa8b2b Aurelien Jacobs
    case  7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
167 e7583962 Peter Ross
    case -1:
168
        switch (revision) {
169
        case  1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
170
        case  2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
171
        case  3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
172 725d86bf Aurelien Jacobs
        case -1: break;
173 e7583962 Peter Ross
        default:
174
            av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
175
            return 0;
176
        }
177 725d86bf Aurelien Jacobs
        switch (revision2) {
178
        case  8: ea->audio_codec = CODEC_ID_PCM_S16LE_PLANAR; break;
179
        default:
180
            av_log(s, AV_LOG_ERROR, "unsupported stream type; revision2=%i\n", revision2);
181
            return 0;
182
        }
183 e7583962 Peter Ross
        break;
184 93fa8b2b Aurelien Jacobs
    default:
185
        av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
186
        return 0;
187
    }
188 af704ee4 Aurelien Jacobs
189 6c867e04 Aurelien Jacobs
    if (ea->sample_rate == -1)
190
        ea->sample_rate = revision==3 ? 48000 : 22050;
191
192 215eb102 Aurelien Jacobs
    return 1;
193
}
194
195 98168220 Aurelien Jacobs
/*
196
 * Process EACS sound header
197
 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
198
 */
199
static int process_audio_header_eacs(AVFormatContext *s)
200
{
201
    EaDemuxContext *ea = s->priv_data;
202 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
203 98168220 Aurelien Jacobs
    int compression_type;
204
205
    ea->sample_rate  = ea->big_endian ? get_be32(pb) : get_le32(pb);
206
    ea->bytes        = get_byte(pb);   /* 1=8-bit, 2=16-bit */
207
    ea->num_channels = get_byte(pb);
208
    compression_type = get_byte(pb);
209
    url_fskip(pb, 13);
210
211
    switch (compression_type) {
212
    case 0:
213
        switch (ea->bytes) {
214
        case 1: ea->audio_codec = CODEC_ID_PCM_S8;    break;
215
        case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
216
        }
217
        break;
218
    case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
219 fac84d3c Peter Ross
    case 2: ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_EACS; break;
220 98168220 Aurelien Jacobs
    default:
221
        av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
222
    }
223
224
    return 1;
225
}
226
227 7bb65d89 Peter Ross
/*
228
 * Process SEAD sound header
229
 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
230
 */
231
static int process_audio_header_sead(AVFormatContext *s)
232
{
233
    EaDemuxContext *ea = s->priv_data;
234 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
235 7bb65d89 Peter Ross
236
    ea->sample_rate  = get_le32(pb);
237
    ea->bytes        = get_le32(pb);  /* 1=8-bit, 2=16-bit */
238
    ea->num_channels = get_le32(pb);
239
    ea->audio_codec  = CODEC_ID_ADPCM_IMA_EA_SEAD;
240
241
    return 1;
242
}
243
244 f4097430 Aurelien Jacobs
static int process_video_header_vp6(AVFormatContext *s)
245
{
246
    EaDemuxContext *ea = s->priv_data;
247 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
248 f4097430 Aurelien Jacobs
249
    url_fskip(pb, 16);
250
    ea->time_base.den = get_le32(pb);
251
    ea->time_base.num = get_le32(pb);
252 af704ee4 Aurelien Jacobs
    ea->video_codec = CODEC_ID_VP6;
253 f4097430 Aurelien Jacobs
254
    return 1;
255
}
256
257 215eb102 Aurelien Jacobs
/*
258
 * Process EA file header
259
 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
260
 */
261
static int process_ea_header(AVFormatContext *s) {
262
    uint32_t blockid, size = 0;
263
    EaDemuxContext *ea = s->priv_data;
264 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
265 f8cab062 Aurelien Jacobs
    int i;
266
267
    for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
268
        unsigned int startpos = url_ftell(pb);
269 2c82386d Aurelien Jacobs
        int err = 0;
270 215eb102 Aurelien Jacobs
271 fa34563b Aurelien Jacobs
        blockid = get_le32(pb);
272 215eb102 Aurelien Jacobs
        size = get_le32(pb);
273 97e5dcc0 Aurelien Jacobs
        if (i == 0)
274
            ea->big_endian = size > 0x000FFFFF;
275
        if (ea->big_endian)
276
            size = bswap_32(size);
277 f8cab062 Aurelien Jacobs
278
        switch (blockid) {
279 94e8c227 Aurelien Jacobs
            case ISNh_TAG:
280 98168220 Aurelien Jacobs
                if (get_le32(pb) != EACS_TAG) {
281
                    av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
282
                    return 0;
283
                }
284
                err = process_audio_header_eacs(s);
285
                break;
286
287 f8cab062 Aurelien Jacobs
            case SCHl_TAG :
288 fa34563b Aurelien Jacobs
                blockid = get_le32(pb);
289
                if (blockid == GSTR_TAG) {
290
                    url_fskip(pb, 4);
291
                } else if (blockid != PT00_TAG) {
292
                    av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
293
                    return 0;
294
                }
295 2c82386d Aurelien Jacobs
                err = process_audio_header_elements(s);
296 f8cab062 Aurelien Jacobs
                break;
297
298 7bb65d89 Peter Ross
            case SEAD_TAG:
299
                err = process_audio_header_sead(s);
300
                break;
301
302 f8cab062 Aurelien Jacobs
            case MVhd_TAG :
303 2c82386d Aurelien Jacobs
                err = process_video_header_vp6(s);
304 f8cab062 Aurelien Jacobs
                break;
305
        }
306
307 2c82386d Aurelien Jacobs
        if (err < 0) {
308
            av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
309
            return err;
310
        }
311
312 f8cab062 Aurelien Jacobs
        url_fseek(pb, startpos + size, SEEK_SET);
313
    }
314 215eb102 Aurelien Jacobs
315 f8cab062 Aurelien Jacobs
    url_fseek(pb, 0, SEEK_SET);
316 ad81a9fe Mike Melanson
317
    return 1;
318
}
319
320
321
static int ea_probe(AVProbeData *p)
322
{
323 39fe9d79 Aurelien Jacobs
    switch (AV_RL32(&p->buf[0])) {
324 97e6ee20 Aurelien Jacobs
    case ISNh_TAG:
325 39fe9d79 Aurelien Jacobs
    case SCHl_TAG:
326 7bb65d89 Peter Ross
    case SEAD_TAG:
327 968fc6b9 Aurelien Jacobs
    case kVGT_TAG:
328
    case MADk_TAG:
329
    case MPCh_TAG:
330 39fe9d79 Aurelien Jacobs
    case MVhd_TAG:
331 968fc6b9 Aurelien Jacobs
    case MVIh_TAG:
332 d3e412d0 Aurelien Jacobs
        return AVPROBE_SCORE_MAX;
333 39fe9d79 Aurelien Jacobs
    }
334 d3e412d0 Aurelien Jacobs
    return 0;
335 ad81a9fe Mike Melanson
}
336
337
static int ea_read_header(AVFormatContext *s,
338
                          AVFormatParameters *ap)
339
{
340 e4141433 Nicholas Tung
    EaDemuxContext *ea = s->priv_data;
341 ad81a9fe Mike Melanson
    AVStream *st;
342
343
    if (!process_ea_header(s))
344 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
345 ad81a9fe Mike Melanson
346 ab89dbd4 Aurelien Jacobs
    if (ea->video_codec) {
347 227a388d Aurelien Jacobs
        /* initialize the video decoder stream */
348
        st = av_new_stream(s, 0);
349
        if (!st)
350
            return AVERROR(ENOMEM);
351
        ea->video_stream_index = st->index;
352
        st->codec->codec_type = CODEC_TYPE_VIDEO;
353 af704ee4 Aurelien Jacobs
        st->codec->codec_id = ea->video_codec;
354 227a388d Aurelien Jacobs
        st->codec->codec_tag = 0;  /* no fourcc */
355
        st->codec->time_base = ea->time_base;
356 d3e412d0 Aurelien Jacobs
    }
357 ad81a9fe Mike Melanson
358 c611463f Aurelien Jacobs
    if (ea->audio_codec) {
359 a2c9473d Aurelien Jacobs
        /* initialize the audio decoder stream */
360
        st = av_new_stream(s, 0);
361
        if (!st)
362
            return AVERROR(ENOMEM);
363
        av_set_pts_info(st, 33, 1, ea->sample_rate);
364
        st->codec->codec_type = CODEC_TYPE_AUDIO;
365
        st->codec->codec_id = ea->audio_codec;
366
        st->codec->codec_tag = 0;  /* no tag */
367
        st->codec->channels = ea->num_channels;
368
        st->codec->sample_rate = ea->sample_rate;
369
        st->codec->bits_per_sample = ea->bytes * 8;
370
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
371
            st->codec->bits_per_sample / 4;
372
        st->codec->block_align = st->codec->channels*st->codec->bits_per_sample;
373
        ea->audio_stream_index = st->index;
374
        ea->audio_frame_counter = 0;
375 c611463f Aurelien Jacobs
    }
376 ad81a9fe Mike Melanson
377
    return 1;
378
}
379
380
static int ea_read_packet(AVFormatContext *s,
381
                          AVPacket *pkt)
382
{
383
    EaDemuxContext *ea = s->priv_data;
384 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
385 ad81a9fe Mike Melanson
    int ret = 0;
386
    int packet_read = 0;
387
    unsigned int chunk_type, chunk_size;
388 d3e412d0 Aurelien Jacobs
    int key = 0;
389 ad81a9fe Mike Melanson
390
    while (!packet_read) {
391 78168450 Aurelien Jacobs
        chunk_type = get_le32(pb);
392 597958ad Aurelien Jacobs
        chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
393 ad81a9fe Mike Melanson
394
        switch (chunk_type) {
395
        /* audio data */
396 94e8c227 Aurelien Jacobs
        case ISNh_TAG:
397 98168220 Aurelien Jacobs
            /* header chunk also contains data; skip over the header portion*/
398
            url_fskip(pb, 32);
399
            chunk_size -= 32;
400 94e8c227 Aurelien Jacobs
        case ISNd_TAG:
401 ad81a9fe Mike Melanson
        case SCDl_TAG:
402 7bb65d89 Peter Ross
        case SNDC_TAG:
403 018d0ff9 Aurelien Jacobs
            if (!ea->audio_codec) {
404
                url_fskip(pb, chunk_size);
405
                break;
406 725d86bf Aurelien Jacobs
            } else if (ea->audio_codec == CODEC_ID_PCM_S16LE_PLANAR) {
407
                url_fskip(pb, 12);  /* planar header */
408
                chunk_size -= 12;
409 018d0ff9 Aurelien Jacobs
            }
410 2692067a Michael Niedermayer
            ret = av_get_packet(pb, pkt, chunk_size);
411
            if (ret != chunk_size)
412 6f3e0b21 Panagiotis Issaris
                ret = AVERROR(EIO);
413 ad81a9fe Mike Melanson
            else {
414
                    pkt->stream_index = ea->audio_stream_index;
415
                    pkt->pts = 90000;
416
                    pkt->pts *= ea->audio_frame_counter;
417 6c867e04 Aurelien Jacobs
                    pkt->pts /= ea->sample_rate;
418 ad81a9fe Mike Melanson
419 200f41ad Aurelien Jacobs
                    switch (ea->audio_codec) {
420
                    case CODEC_ID_ADPCM_EA:
421 115329f1 Diego Biurrun
                    /* 2 samples/byte, 1 or 2 samples per frame depending
422 ad81a9fe Mike Melanson
                     * on stereo; chunk also has 12-byte header */
423
                    ea->audio_frame_counter += ((chunk_size - 12) * 2) /
424
                        ea->num_channels;
425 200f41ad Aurelien Jacobs
                        break;
426
                    default:
427
                        ea->audio_frame_counter += chunk_size /
428
                            (ea->bytes * ea->num_channels);
429
                    }
430 ad81a9fe Mike Melanson
            }
431
432
            packet_read = 1;
433
            break;
434
435
        /* ending tag */
436 44626387 Aurelien Jacobs
        case 0:
437 94e8c227 Aurelien Jacobs
        case ISNe_TAG:
438 ad81a9fe Mike Melanson
        case SCEl_TAG:
439 7bb65d89 Peter Ross
        case SEND_TAG:
440 6f3e0b21 Panagiotis Issaris
            ret = AVERROR(EIO);
441 ad81a9fe Mike Melanson
            packet_read = 1;
442
            break;
443
444 d3e412d0 Aurelien Jacobs
        case MV0K_TAG:
445
            key = PKT_FLAG_KEY;
446
        case MV0F_TAG:
447
            ret = av_get_packet(pb, pkt, chunk_size);
448
            if (ret != chunk_size)
449
                ret = AVERROR_IO;
450
            else {
451
                pkt->stream_index = ea->video_stream_index;
452
                pkt->flags |= key;
453
            }
454
            packet_read = 1;
455
            break;
456
457 ad81a9fe Mike Melanson
        default:
458
            url_fseek(pb, chunk_size, SEEK_CUR);
459
            break;
460
        }
461
    }
462
463
    return ret;
464
}
465
466 ff70e601 Måns Rullgård
AVInputFormat ea_demuxer = {
467 ad81a9fe Mike Melanson
    "ea",
468
    "Electronic Arts Multimedia Format",
469
    sizeof(EaDemuxContext),
470
    ea_probe,
471
    ea_read_header,
472
    ea_read_packet,
473
};