1 /* Electronic Arts Multimedia File Demuxer
2 * Copyright (c) 2004 The ffmpeg Project
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * @file electronicarts.c
23 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
24 * by Robin Kay (komadori at gekkou.co.uk)
29 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
30 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
31 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
32 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
33 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
34 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
35 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
36 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
37 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
38 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
39 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
40 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
42 typedef struct EaDemuxContext {
47 int video_stream_index;
50 int audio_stream_index;
51 int audio_frame_counter;
61 static uint32_t read_arbitary(ByteIOContext *pb) {
69 for (i = 0; i < size; i++) {
79 * Process PT/GSTR sound header
80 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
82 static int process_audio_header_elements(AVFormatContext *s)
85 EaDemuxContext *ea = s->priv_data;
86 ByteIOContext *pb = &s->pb;
87 int compression_type = -1, revision = -1;
100 av_log (s, AV_LOG_INFO, "entered audio subheader\n");
102 while (inSubheader) {
104 subbyte = get_byte(pb);
108 revision = read_arbitary(pb);
109 av_log (s, AV_LOG_INFO, "revision (element 0x80) set to 0x%08x\n", revision);
112 ea->num_channels = read_arbitary(pb);
113 av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
116 compression_type = read_arbitary(pb);
117 av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
120 ea->sample_rate = read_arbitary(pb);
121 av_log (s, AV_LOG_INFO, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
124 ea->num_samples = read_arbitary(pb);
125 av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
128 av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
129 av_log (s, AV_LOG_INFO, "exited audio subheader\n");
133 av_log (s, AV_LOG_INFO, "end of header block reached (within audio subheader)\n");
138 av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
144 av_log (s, AV_LOG_INFO, "end of header block reached\n");
148 av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
153 switch (compression_type) {
154 case 0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
155 case 7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
158 case 1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
159 case 2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
160 case 3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
162 av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
167 av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
171 if (ea->sample_rate == -1)
172 ea->sample_rate = revision==3 ? 48000 : 22050;
178 * Process EACS sound header
179 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
181 static int process_audio_header_eacs(AVFormatContext *s)
183 EaDemuxContext *ea = s->priv_data;
184 ByteIOContext *pb = &s->pb;
185 int compression_type;
187 ea->sample_rate = ea->big_endian ? get_be32(pb) : get_le32(pb);
188 ea->bytes = get_byte(pb); /* 1=8-bit, 2=16-bit */
189 ea->num_channels = get_byte(pb);
190 compression_type = get_byte(pb);
193 switch (compression_type) {
196 case 1: ea->audio_codec = CODEC_ID_PCM_S8; break;
197 case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
200 case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
202 av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
208 static int process_video_header_vp6(AVFormatContext *s)
210 EaDemuxContext *ea = s->priv_data;
211 ByteIOContext *pb = &s->pb;
214 ea->time_base.den = get_le32(pb);
215 ea->time_base.num = get_le32(pb);
216 ea->video_codec = CODEC_ID_VP6;
222 * Process EA file header
223 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
225 static int process_ea_header(AVFormatContext *s) {
226 uint32_t blockid, size = 0;
227 EaDemuxContext *ea = s->priv_data;
228 ByteIOContext *pb = &s->pb;
231 for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
232 unsigned int startpos = url_ftell(pb);
235 blockid = get_le32(pb);
238 ea->big_endian = size > 0x000FFFFF;
240 size = bswap_32(size);
244 if (get_le32(pb) != EACS_TAG) {
245 av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
248 err = process_audio_header_eacs(s);
252 blockid = get_le32(pb);
253 if (blockid == GSTR_TAG) {
255 } else if (blockid != PT00_TAG) {
256 av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
259 err = process_audio_header_elements(s);
263 err = process_video_header_vp6(s);
268 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
272 url_fseek(pb, startpos + size, SEEK_SET);
275 url_fseek(pb, 0, SEEK_SET);
281 static int ea_probe(AVProbeData *p)
283 switch (AV_RL32(&p->buf[0])) {
286 return AVPROBE_SCORE_MAX;
291 static int ea_read_header(AVFormatContext *s,
292 AVFormatParameters *ap)
294 EaDemuxContext *ea = s->priv_data;
297 if (!process_ea_header(s))
300 if (ea->video_codec) {
301 /* initialize the video decoder stream */
302 st = av_new_stream(s, 0);
304 return AVERROR(ENOMEM);
305 ea->video_stream_index = st->index;
306 st->codec->codec_type = CODEC_TYPE_VIDEO;
307 st->codec->codec_id = ea->video_codec;
308 st->codec->codec_tag = 0; /* no fourcc */
309 st->codec->time_base = ea->time_base;
312 if (ea->audio_codec) {
313 /* initialize the audio decoder stream */
314 st = av_new_stream(s, 0);
316 return AVERROR(ENOMEM);
317 av_set_pts_info(st, 33, 1, ea->sample_rate);
318 st->codec->codec_type = CODEC_TYPE_AUDIO;
319 st->codec->codec_id = ea->audio_codec;
320 st->codec->codec_tag = 0; /* no tag */
321 st->codec->channels = ea->num_channels;
322 st->codec->sample_rate = ea->sample_rate;
323 st->codec->bits_per_sample = ea->bytes * 8;
324 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
325 st->codec->bits_per_sample / 4;
326 st->codec->block_align = st->codec->channels*st->codec->bits_per_sample;
327 ea->audio_stream_index = st->index;
328 ea->audio_frame_counter = 0;
334 static int ea_read_packet(AVFormatContext *s,
337 EaDemuxContext *ea = s->priv_data;
338 ByteIOContext *pb = &s->pb;
341 unsigned int chunk_type, chunk_size;
344 while (!packet_read) {
345 chunk_type = get_le32(pb);
346 chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
348 switch (chunk_type) {
351 /* header chunk also contains data; skip over the header portion*/
356 if (!ea->audio_codec) {
357 url_fskip(pb, chunk_size);
360 ret = av_get_packet(pb, pkt, chunk_size);
361 if (ret != chunk_size)
364 pkt->stream_index = ea->audio_stream_index;
366 pkt->pts *= ea->audio_frame_counter;
367 pkt->pts /= ea->sample_rate;
369 switch (ea->audio_codec) {
370 case CODEC_ID_ADPCM_EA:
371 /* 2 samples/byte, 1 or 2 samples per frame depending
372 * on stereo; chunk also has 12-byte header */
373 ea->audio_frame_counter += ((chunk_size - 12) * 2) /
377 ea->audio_frame_counter += chunk_size /
378 (ea->bytes * ea->num_channels);
396 ret = av_get_packet(pb, pkt, chunk_size);
397 if (ret != chunk_size)
400 pkt->stream_index = ea->video_stream_index;
407 url_fseek(pb, chunk_size, SEEK_CUR);
415 AVInputFormat ea_demuxer = {
417 "Electronic Arts Multimedia Format",
418 sizeof(EaDemuxContext),