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') /* MPEG-2 */
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 AVCodecID video_codec;
69 int video_stream_index;
71 enum AVCodecID audio_codec;
72 int audio_stream_index;
80 static uint32_t read_arbitrary(AVIOContext *pb)
89 for (i = 0; i < size; i++) {
98 static int process_audio_header_elements(AVFormatContext *s)
100 EaDemuxContext *ea = s->priv_data;
101 AVIOContext *pb = s->pb;
103 int compression_type = -1, revision = -1, revision2 = -1;
106 ea->sample_rate = -1;
107 ea->num_channels = 1;
109 while (!pb->eof_reached && in_header) {
116 av_log(s, AV_LOG_DEBUG, "entered audio subheader\n");
118 while (!pb->eof_reached && in_subheader) {
120 subbyte = avio_r8(pb);
124 revision = read_arbitrary(pb);
125 av_log(s, AV_LOG_DEBUG,
126 "revision (element 0x80) set to 0x%08x\n", revision);
129 ea->num_channels = read_arbitrary(pb);
130 av_log(s, AV_LOG_DEBUG,
131 "num_channels (element 0x82) set to 0x%08x\n",
135 compression_type = read_arbitrary(pb);
136 av_log(s, AV_LOG_DEBUG,
137 "compression_type (element 0x83) set to 0x%08x\n",
141 ea->sample_rate = read_arbitrary(pb);
142 av_log(s, AV_LOG_DEBUG,
143 "sample_rate (element 0x84) set to %i\n",
147 ea->num_samples = read_arbitrary(pb);
148 av_log(s, AV_LOG_DEBUG,
149 "num_samples (element 0x85) set to 0x%08x\n",
153 av_log(s, AV_LOG_DEBUG,
154 "element 0x%02x set to 0x%08x\n",
155 subbyte, read_arbitrary(pb));
156 av_log(s, AV_LOG_DEBUG, "exited audio subheader\n");
160 revision2 = read_arbitrary(pb);
161 av_log(s, AV_LOG_DEBUG,
162 "revision2 (element 0xA0) set to 0x%08x\n",
166 av_log(s, AV_LOG_DEBUG,
167 "end of header block reached (within audio subheader)\n");
172 av_log(s, AV_LOG_DEBUG,
173 "element 0x%02x set to 0x%08x\n",
174 subbyte, read_arbitrary(pb));
180 av_log(s, AV_LOG_DEBUG, "end of header block reached\n");
184 av_log(s, AV_LOG_DEBUG,
185 "header element 0x%02x set to 0x%08x\n",
186 byte, read_arbitrary(pb));
191 switch (compression_type) {
193 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
196 ea->audio_codec = AV_CODEC_ID_ADPCM_EA;
201 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1;
204 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2;
207 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3;
212 av_log(s, AV_LOG_ERROR,
213 "unsupported stream type; revision=%i\n", revision);
218 ea->audio_codec = AV_CODEC_ID_PCM_S16LE_PLANAR;
221 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2;
224 ea->audio_codec = AV_CODEC_ID_MP3;
229 ea->audio_codec = AV_CODEC_ID_NONE;
230 av_log(s, AV_LOG_ERROR,
231 "unsupported stream type; revision2=%i\n", revision2);
236 av_log(s, AV_LOG_ERROR,
237 "unsupported stream type; compression_type=%i\n",
242 if (ea->sample_rate == -1)
243 ea->sample_rate = revision == 3 ? 48000 : 22050;
248 static void process_audio_header_eacs(AVFormatContext *s)
250 EaDemuxContext *ea = s->priv_data;
251 AVIOContext *pb = s->pb;
252 int compression_type;
254 ea->sample_rate = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
255 ea->bytes = avio_r8(pb); /* 1=8-bit, 2=16-bit */
256 ea->num_channels = avio_r8(pb);
257 compression_type = avio_r8(pb);
260 switch (compression_type) {
264 ea->audio_codec = AV_CODEC_ID_PCM_S8;
267 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
272 ea->audio_codec = AV_CODEC_ID_PCM_MULAW;
276 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS;
279 av_log(s, AV_LOG_ERROR,
280 "unsupported stream type; audio compression_type=%i\n",
285 static void process_audio_header_sead(AVFormatContext *s)
287 EaDemuxContext *ea = s->priv_data;
288 AVIOContext *pb = s->pb;
290 ea->sample_rate = avio_rl32(pb);
291 ea->bytes = avio_rl32(pb); /* 1=8-bit, 2=16-bit */
292 ea->num_channels = avio_rl32(pb);
293 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
296 static void process_video_header_mdec(AVFormatContext *s)
298 EaDemuxContext *ea = s->priv_data;
299 AVIOContext *pb = s->pb;
301 ea->width = avio_rl16(pb);
302 ea->height = avio_rl16(pb);
303 ea->time_base = (AVRational) { 1, 15 };
304 ea->video_codec = AV_CODEC_ID_MDEC;
307 static void process_video_header_vp6(AVFormatContext *s)
309 EaDemuxContext *ea = s->priv_data;
310 AVIOContext *pb = s->pb;
313 ea->time_base.den = avio_rl32(pb);
314 ea->time_base.num = avio_rl32(pb);
315 ea->video_codec = AV_CODEC_ID_VP6;
318 static void process_video_header_cmv(AVFormatContext *s)
320 EaDemuxContext *ea = s->priv_data;
323 avio_skip(s->pb, 10);
324 fps = avio_rl16(s->pb);
326 ea->time_base = (AVRational) { 1, fps };
327 ea->video_codec = AV_CODEC_ID_CMV;
330 /* Process EA file header.
331 * Return 1 if the EA file is valid and successfully opened, 0 otherwise. */
332 static int process_ea_header(AVFormatContext *s)
334 uint32_t blockid, size = 0;
335 EaDemuxContext *ea = s->priv_data;
336 AVIOContext *pb = s->pb;
339 for (i = 0; i < 5 && (!ea->audio_codec || !ea->video_codec); i++) {
340 unsigned int startpos = avio_tell(pb);
343 blockid = avio_rl32(pb);
344 size = avio_rl32(pb);
346 ea->big_endian = size > 0x000FFFFF;
348 size = av_bswap32(size);
352 if (avio_rl32(pb) != EACS_TAG) {
353 av_log(s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
356 process_audio_header_eacs(s);
361 blockid = avio_rl32(pb);
362 if (blockid == GSTR_TAG) {
364 } else if ((blockid & 0xFFFF) != PT00_TAG) {
365 av_log(s, AV_LOG_ERROR, "unknown SCHl headerid\n");
368 err = process_audio_header_elements(s);
372 process_audio_header_sead(s);
376 process_video_header_cmv(s);
380 ea->video_codec = AV_CODEC_ID_TGV;
381 ea->time_base = (AVRational) { 1, 15 };
385 process_video_header_mdec(s);
389 ea->video_codec = AV_CODEC_ID_MPEG2VIDEO;
394 ea->video_codec = AV_CODEC_ID_TGQ;
398 ea->video_codec = AV_CODEC_ID_TQI;
402 ea->video_codec = AV_CODEC_ID_MAD;
406 process_video_header_vp6(s);
411 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
415 avio_seek(pb, startpos + size, SEEK_SET);
418 avio_seek(pb, 0, SEEK_SET);
423 static int ea_probe(AVProbeData *p)
425 switch (AV_RL32(&p->buf[0])) {
439 if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
442 return AVPROBE_SCORE_MAX;
445 static int ea_read_header(AVFormatContext *s)
447 EaDemuxContext *ea = s->priv_data;
450 if (!process_ea_header(s))
453 if (ea->video_codec) {
454 /* initialize the video decoder stream */
455 st = avformat_new_stream(s, NULL);
457 return AVERROR(ENOMEM);
458 ea->video_stream_index = st->index;
459 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
460 st->codec->codec_id = ea->video_codec;
461 st->codec->codec_tag = 0; /* no fourcc */
462 st->codec->width = ea->width;
463 st->codec->height = ea->height;
464 avpriv_set_pts_info(st, 33, ea->time_base.num, ea->time_base.den);
465 st->avg_frame_rate = (AVRational) { ea->time_base.den,
469 if (ea->audio_codec) {
470 if (ea->num_channels <= 0 || ea->num_channels > 2) {
471 av_log(s, AV_LOG_WARNING,
472 "Unsupported number of channels: %d\n", ea->num_channels);
476 if (ea->sample_rate <= 0) {
477 av_log(s, AV_LOG_ERROR,
478 "Unsupported sample rate: %d\n", ea->sample_rate);
482 if (ea->bytes <= 0) {
483 av_log(s, AV_LOG_ERROR,
484 "Invalid number of bytes per sample: %d\n", ea->bytes);
485 ea->audio_codec = AV_CODEC_ID_NONE;
489 /* initialize the audio decoder stream */
490 st = avformat_new_stream(s, NULL);
492 return AVERROR(ENOMEM);
493 avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
494 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
495 st->codec->codec_id = ea->audio_codec;
496 st->codec->codec_tag = 0; /* no tag */
497 st->codec->channels = ea->num_channels;
498 st->codec->sample_rate = ea->sample_rate;
499 st->codec->bits_per_coded_sample = ea->bytes * 8;
500 st->codec->bit_rate = st->codec->channels *
501 st->codec->sample_rate *
502 st->codec->bits_per_coded_sample / 4;
503 st->codec->block_align = st->codec->channels *
504 st->codec->bits_per_coded_sample;
505 ea->audio_stream_index = st->index;
512 static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
514 EaDemuxContext *ea = s->priv_data;
515 AVIOContext *pb = s->pb;
516 unsigned int chunk_type, chunk_size;
517 int ret = 0, packet_read = 0, key = 0;
518 int av_uninit(num_samples);
520 while (!packet_read) {
521 chunk_type = avio_rl32(pb);
522 chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
524 return AVERROR_INVALIDDATA;
527 switch (chunk_type) {
530 /* header chunk also contains data; skip over the header portion */
532 return AVERROR_INVALIDDATA;
539 if (!ea->audio_codec) {
540 avio_skip(pb, chunk_size);
542 } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
543 ea->audio_codec == AV_CODEC_ID_MP3) {
544 num_samples = avio_rl32(pb);
548 ret = av_get_packet(pb, pkt, chunk_size);
551 pkt->stream_index = ea->audio_stream_index;
553 switch (ea->audio_codec) {
554 case AV_CODEC_ID_ADPCM_EA:
555 case AV_CODEC_ID_ADPCM_EA_R1:
556 case AV_CODEC_ID_ADPCM_EA_R2:
557 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
558 case AV_CODEC_ID_ADPCM_EA_R3:
560 av_log(s, AV_LOG_ERROR, "Packet is too short\n");
562 return AVERROR_INVALIDDATA;
564 if (ea->audio_codec == AV_CODEC_ID_ADPCM_EA_R3)
565 pkt->duration = AV_RB32(pkt->data);
567 pkt->duration = AV_RL32(pkt->data);
569 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
570 pkt->duration = ret * 2 / ea->num_channels;
572 case AV_CODEC_ID_PCM_S16LE_PLANAR:
573 case AV_CODEC_ID_MP3:
574 pkt->duration = num_samples;
577 pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
598 key = AV_PKT_FLAG_KEY;
603 avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
605 goto get_video_packet;
608 avio_skip(pb, 8); // skip ea DCT header
610 goto get_video_packet;
615 key = AV_PKT_FLAG_KEY;
618 ret = av_get_packet(pb, pkt, chunk_size);
621 pkt->stream_index = ea->video_stream_index;
627 avio_skip(pb, chunk_size);
635 AVInputFormat ff_ea_demuxer = {
637 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
638 .priv_data_size = sizeof(EaDemuxContext),
639 .read_probe = ea_probe,
640 .read_header = ea_read_header,
641 .read_packet = ea_read_packet,