1 /* Electronic Arts Multimedia File Demuxer
2 * Copyright (c) 2004 The ffmpeg Project
3 * Copyright (c) 2006-2008 Peter Ross
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
25 * by Robin Kay (komadori at gekkou.co.uk)
28 #include "libavutil/intreadwrite.h"
32 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
33 #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
34 #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
35 #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
36 #define SHEN_TAG MKTAG('S', 'H', 'E', 'N') /* SxEN header */
37 #define SDEN_TAG MKTAG('S', 'D', 'E', 'N') /* SxEN data */
38 #define SEEN_TAG MKTAG('S', 'E', 'E', 'N') /* SxEN end */
39 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
40 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
41 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
42 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
43 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
44 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
45 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
46 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
47 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV i-frame */
48 #define fVGT_TAG MKTAG('f', 'V', 'G', 'T') /* TGV p-frame */
49 #define mTCD_TAG MKTAG('m', 'T', 'C', 'D') /* MDEC */
50 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
51 #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD p-frame */
52 #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
53 #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG2 */
54 #define TGQs_TAG MKTAG('T', 'G', 'Q', 's') /* TGQ i-frame (appears in .TGQ files) */
55 #define pQGT_TAG MKTAG('p', 'Q', 'G', 'T') /* TGQ i-frame (appears in .UV files) */
56 #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T') /* TQI/UV2 i-frame (.UV2/.WVE) */
57 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
58 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
59 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
60 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
61 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV i-frame */
63 typedef struct EaDemuxContext {
66 enum CodecID video_codec;
69 int video_stream_index;
71 enum CodecID audio_codec;
72 int audio_stream_index;
73 int audio_frame_counter;
81 static uint32_t read_arbitary(AVIOContext *pb) {
89 for (i = 0; i < size; i++) {
99 * Process PT/GSTR sound header
100 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
102 static int process_audio_header_elements(AVFormatContext *s)
105 EaDemuxContext *ea = s->priv_data;
106 AVIOContext *pb = s->pb;
107 int compression_type = -1, revision = -1, revision2 = -1;
110 ea->sample_rate = -1;
111 ea->num_channels = 1;
113 while (!pb->eof_reached && inHeader) {
120 av_log (s, AV_LOG_DEBUG, "entered audio subheader\n");
122 while (!pb->eof_reached && inSubheader) {
124 subbyte = avio_r8(pb);
128 revision = read_arbitary(pb);
129 av_log (s, AV_LOG_DEBUG, "revision (element 0x80) set to 0x%08x\n", revision);
132 ea->num_channels = read_arbitary(pb);
133 av_log (s, AV_LOG_DEBUG, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
136 compression_type = read_arbitary(pb);
137 av_log (s, AV_LOG_DEBUG, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
140 ea->sample_rate = read_arbitary(pb);
141 av_log (s, AV_LOG_DEBUG, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
144 ea->num_samples = read_arbitary(pb);
145 av_log (s, AV_LOG_DEBUG, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
148 av_log (s, AV_LOG_DEBUG, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
149 av_log (s, AV_LOG_DEBUG, "exited audio subheader\n");
153 revision2 = read_arbitary(pb);
154 av_log (s, AV_LOG_DEBUG, "revision2 (element 0xA0) set to 0x%08x\n", revision2);
157 av_log (s, AV_LOG_DEBUG, "end of header block reached (within audio subheader)\n");
162 av_log (s, AV_LOG_DEBUG, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
168 av_log (s, AV_LOG_DEBUG, "end of header block reached\n");
172 av_log (s, AV_LOG_DEBUG, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
177 switch (compression_type) {
178 case 0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
179 case 7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
182 case 1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
183 case 2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
184 case 3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
187 av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
191 case 8: ea->audio_codec = CODEC_ID_PCM_S16LE_PLANAR; break;
192 case 10: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
193 case 16: ea->audio_codec = CODEC_ID_MP3; break;
196 ea->audio_codec = CODEC_ID_NONE;
197 av_log(s, AV_LOG_ERROR, "unsupported stream type; revision2=%i\n", revision2);
202 av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
206 if (ea->sample_rate == -1)
207 ea->sample_rate = revision==3 ? 48000 : 22050;
213 * Process EACS sound header
214 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
216 static int process_audio_header_eacs(AVFormatContext *s)
218 EaDemuxContext *ea = s->priv_data;
219 AVIOContext *pb = s->pb;
220 int compression_type;
222 ea->sample_rate = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
223 ea->bytes = avio_r8(pb); /* 1=8-bit, 2=16-bit */
224 ea->num_channels = avio_r8(pb);
225 compression_type = avio_r8(pb);
228 switch (compression_type) {
231 case 1: ea->audio_codec = CODEC_ID_PCM_S8; break;
232 case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
235 case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
236 case 2: ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_EACS; break;
238 av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
245 * Process SEAD sound header
246 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
248 static int process_audio_header_sead(AVFormatContext *s)
250 EaDemuxContext *ea = s->priv_data;
251 AVIOContext *pb = s->pb;
253 ea->sample_rate = avio_rl32(pb);
254 ea->bytes = avio_rl32(pb); /* 1=8-bit, 2=16-bit */
255 ea->num_channels = avio_rl32(pb);
256 ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_SEAD;
261 static int process_video_header_mdec(AVFormatContext *s)
263 EaDemuxContext *ea = s->priv_data;
264 AVIOContext *pb = s->pb;
266 ea->width = avio_rl16(pb);
267 ea->height = avio_rl16(pb);
268 ea->time_base = (AVRational){1,15};
269 ea->video_codec = CODEC_ID_MDEC;
273 static int process_video_header_vp6(AVFormatContext *s)
275 EaDemuxContext *ea = s->priv_data;
276 AVIOContext *pb = s->pb;
279 ea->time_base.den = avio_rl32(pb);
280 ea->time_base.num = avio_rl32(pb);
281 ea->video_codec = CODEC_ID_VP6;
287 * Process EA file header
288 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
290 static int process_ea_header(AVFormatContext *s) {
291 uint32_t blockid, size = 0;
292 EaDemuxContext *ea = s->priv_data;
293 AVIOContext *pb = s->pb;
296 for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
297 unsigned int startpos = avio_tell(pb);
300 blockid = avio_rl32(pb);
301 size = avio_rl32(pb);
303 ea->big_endian = size > 0x000FFFFF;
305 size = av_bswap32(size);
309 if (avio_rl32(pb) != EACS_TAG) {
310 av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
313 err = process_audio_header_eacs(s);
318 blockid = avio_rl32(pb);
319 if (blockid == GSTR_TAG) {
321 } else if ((blockid & 0xFFFF)!=PT00_TAG) {
322 av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
325 err = process_audio_header_elements(s);
329 err = process_audio_header_sead(s);
333 ea->video_codec = CODEC_ID_CMV;
334 ea->time_base = (AVRational){0,0};
338 ea->video_codec = CODEC_ID_TGV;
339 ea->time_base = (AVRational){0,0};
343 err = process_video_header_mdec(s);
347 ea->video_codec = CODEC_ID_MPEG2VIDEO;
352 ea->video_codec = CODEC_ID_TGQ;
356 ea->video_codec = CODEC_ID_TQI;
360 ea->video_codec = CODEC_ID_MAD;
364 err = process_video_header_vp6(s);
369 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
373 avio_seek(pb, startpos + size, SEEK_SET);
376 avio_seek(pb, 0, SEEK_SET);
382 static int ea_probe(AVProbeData *p)
384 switch (AV_RL32(&p->buf[0])) {
398 if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
400 return AVPROBE_SCORE_MAX;
403 static int ea_read_header(AVFormatContext *s,
404 AVFormatParameters *ap)
406 EaDemuxContext *ea = s->priv_data;
409 if (!process_ea_header(s))
412 if (ea->video_codec) {
413 /* initialize the video decoder stream */
414 st = avformat_new_stream(s, NULL);
416 return AVERROR(ENOMEM);
417 ea->video_stream_index = st->index;
418 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
419 st->codec->codec_id = ea->video_codec;
420 st->codec->codec_tag = 0; /* no fourcc */
421 st->codec->time_base = ea->time_base;
422 st->codec->width = ea->width;
423 st->codec->height = ea->height;
426 if (ea->audio_codec) {
427 if (ea->num_channels <= 0) {
428 av_log(s, AV_LOG_WARNING, "Unsupported number of channels: %d\n", ea->num_channels);
432 if (ea->sample_rate <= 0) {
433 av_log(s, AV_LOG_ERROR, "Unsupported sample rate: %d\n", ea->sample_rate);
438 /* initialize the audio decoder stream */
439 st = avformat_new_stream(s, NULL);
441 return AVERROR(ENOMEM);
442 avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
443 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
444 st->codec->codec_id = ea->audio_codec;
445 st->codec->codec_tag = 0; /* no tag */
446 st->codec->channels = ea->num_channels;
447 st->codec->sample_rate = ea->sample_rate;
448 st->codec->bits_per_coded_sample = ea->bytes * 8;
449 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
450 st->codec->bits_per_coded_sample / 4;
451 st->codec->block_align = st->codec->channels*st->codec->bits_per_coded_sample;
452 ea->audio_stream_index = st->index;
453 ea->audio_frame_counter = 0;
459 static int ea_read_packet(AVFormatContext *s,
462 EaDemuxContext *ea = s->priv_data;
463 AVIOContext *pb = s->pb;
466 unsigned int chunk_type, chunk_size;
468 int av_uninit(num_samples);
470 while (!packet_read) {
471 chunk_type = avio_rl32(pb);
472 chunk_size = (ea->big_endian ? avio_rb32(pb) : avio_rl32(pb)) - 8;
474 switch (chunk_type) {
477 /* header chunk also contains data; skip over the header portion*/
484 if (!ea->audio_codec) {
485 avio_skip(pb, chunk_size);
487 } else if (ea->audio_codec == CODEC_ID_PCM_S16LE_PLANAR ||
488 ea->audio_codec == CODEC_ID_MP3) {
489 num_samples = avio_rl32(pb);
493 ret = av_get_packet(pb, pkt, chunk_size);
496 pkt->stream_index = ea->audio_stream_index;
498 pkt->pts *= ea->audio_frame_counter;
499 pkt->pts /= ea->sample_rate;
501 switch (ea->audio_codec) {
502 case CODEC_ID_ADPCM_EA:
503 /* 2 samples/byte, 1 or 2 samples per frame depending
504 * on stereo; chunk also has 12-byte header */
505 ea->audio_frame_counter += ((chunk_size - 12) * 2) /
508 case CODEC_ID_PCM_S16LE_PLANAR:
510 ea->audio_frame_counter += num_samples;
513 ea->audio_frame_counter += chunk_size /
514 (ea->bytes * ea->num_channels);
535 key = AV_PKT_FLAG_KEY;
540 avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
542 goto get_video_packet;
545 avio_skip(pb, 8); // skip ea dct header
547 goto get_video_packet;
552 key = AV_PKT_FLAG_KEY;
555 ret = av_get_packet(pb, pkt, chunk_size);
558 pkt->stream_index = ea->video_stream_index;
564 avio_skip(pb, chunk_size);
572 AVInputFormat ff_ea_demuxer = {
574 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia Format"),
575 .priv_data_size = sizeof(EaDemuxContext),
576 .read_probe = ea_probe,
577 .read_header = ea_read_header,
578 .read_packet = ea_read_packet,