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 PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
31 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
32 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
33 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
34 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
35 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
36 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
38 #define EA_SAMPLE_RATE 22050
39 #define EA_BITS_PER_SAMPLE 16
40 #define EA_PREAMBLE_SIZE 8
42 typedef struct EaDemuxContext {
44 int video_stream_index;
46 int audio_stream_index;
47 int audio_frame_counter;
56 static uint32_t read_arbitary(ByteIOContext *pb) {
64 for (i = 0; i < size; i++) {
74 * Process EA file header
75 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
77 static int process_ea_header(AVFormatContext *s) {
79 uint32_t blockid, size = 0;
81 EaDemuxContext *ea = s->priv_data;
82 ByteIOContext *pb = &s->pb;
84 blockid = get_le32(pb);
85 if (blockid == MVhd_TAG) {
90 ea->time_base = (AVRational) {num, den};
91 url_fskip(pb, size-32);
92 blockid = get_le32(pb);
94 if (blockid != SCHl_TAG)
97 blockid = get_le32(pb);
98 if (blockid == GSTR_TAG) {
100 } else if (blockid != PT00_TAG) {
101 av_log (s, AV_LOG_ERROR, "PT header missing\n");
112 av_log (s, AV_LOG_INFO, "entered audio subheader\n");
114 while (inSubheader) {
116 subbyte = get_byte(pb);
120 ea->num_channels = read_arbitary(pb);
121 av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
124 ea->compression_type = read_arbitary(pb);
125 av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", ea->compression_type);
128 ea->num_samples = read_arbitary(pb);
129 av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
132 av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
133 av_log (s, AV_LOG_INFO, "exited audio subheader\n");
141 av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
147 av_log (s, AV_LOG_INFO, "end of header block reached\n");
151 av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
156 /* skip to the start of the data */
157 url_fseek(pb, size, SEEK_SET);
163 static int ea_probe(AVProbeData *p)
167 tag = AV_RL32(&p->buf[0]);
168 if (tag == SCHl_TAG || tag == MVhd_TAG)
169 return AVPROBE_SCORE_MAX;
174 static int ea_read_header(AVFormatContext *s,
175 AVFormatParameters *ap)
177 EaDemuxContext *ea = s->priv_data;
180 if (!process_ea_header(s))
183 if (ea->time_base.num && ea->time_base.den) {
184 /* initialize the video decoder stream */
185 st = av_new_stream(s, 0);
187 return AVERROR(ENOMEM);
188 ea->video_stream_index = st->index;
189 st->codec->codec_type = CODEC_TYPE_VIDEO;
190 st->codec->codec_id = CODEC_ID_VP6;
191 st->codec->codec_tag = 0; /* no fourcc */
192 st->codec->time_base = ea->time_base;
195 /* initialize the audio decoder stream */
196 st = av_new_stream(s, 0);
198 return AVERROR(ENOMEM);
199 av_set_pts_info(st, 33, 1, EA_SAMPLE_RATE);
200 st->codec->codec_type = CODEC_TYPE_AUDIO;
201 st->codec->codec_id = CODEC_ID_ADPCM_EA;
202 st->codec->codec_tag = 0; /* no tag */
203 st->codec->channels = ea->num_channels;
204 st->codec->sample_rate = EA_SAMPLE_RATE;
205 st->codec->bits_per_sample = EA_BITS_PER_SAMPLE;
206 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
207 st->codec->bits_per_sample / 4;
208 st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
210 ea->audio_stream_index = st->index;
211 ea->audio_frame_counter = 0;
216 static int ea_read_packet(AVFormatContext *s,
219 EaDemuxContext *ea = s->priv_data;
220 ByteIOContext *pb = &s->pb;
223 unsigned char preamble[EA_PREAMBLE_SIZE];
224 unsigned int chunk_type, chunk_size;
227 while (!packet_read) {
229 if (get_buffer(pb, preamble, EA_PREAMBLE_SIZE) != EA_PREAMBLE_SIZE)
231 chunk_type = AV_RL32(&preamble[0]);
232 chunk_size = AV_RL32(&preamble[4]) - EA_PREAMBLE_SIZE;
234 switch (chunk_type) {
237 ret = av_get_packet(pb, pkt, chunk_size);
238 if (ret != chunk_size)
241 pkt->stream_index = ea->audio_stream_index;
243 pkt->pts *= ea->audio_frame_counter;
244 pkt->pts /= EA_SAMPLE_RATE;
246 /* 2 samples/byte, 1 or 2 samples per frame depending
247 * on stereo; chunk also has 12-byte header */
248 ea->audio_frame_counter += ((chunk_size - 12) * 2) /
264 ret = av_get_packet(pb, pkt, chunk_size);
265 if (ret != chunk_size)
268 pkt->stream_index = ea->video_stream_index;
275 url_fseek(pb, chunk_size, SEEK_CUR);
283 AVInputFormat ea_demuxer = {
285 "Electronic Arts Multimedia Format",
286 sizeof(EaDemuxContext),