Statistics
| Branch: | Revision:

ffmpeg / libavformat / electronicarts.c @ 74afdc55

History | View | Annotate | Download (17.3 KB)

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