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)
30 #include "libavutil/intreadwrite.h"
34 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
35 #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
36 #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
37 #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
38 #define SHEN_TAG MKTAG('S', 'H', 'E', 'N') /* SxEN header */
39 #define SDEN_TAG MKTAG('S', 'D', 'E', 'N') /* SxEN data */
40 #define SEEN_TAG MKTAG('S', 'E', 'E', 'N') /* SxEN end */
41 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
42 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
43 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
44 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
45 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
46 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
47 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
48 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
49 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV I-frame */
50 #define fVGT_TAG MKTAG('f', 'V', 'G', 'T') /* TGV P-frame */
51 #define mTCD_TAG MKTAG('m', 'T', 'C', 'D') /* MDEC */
52 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD I-frame */
53 #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD P-frame */
54 #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
55 #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG-2 */
56 #define TGQs_TAG MKTAG('T', 'G', 'Q', 's') /* TGQ I-frame (appears in .TGQ files) */
57 #define pQGT_TAG MKTAG('p', 'Q', 'G', 'T') /* TGQ I-frame (appears in .UV files) */
58 #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T') /* TQI/UV2 I-frame (.UV2/.WVE) */
59 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
60 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
61 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
62 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
63 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV I-frame */
65 typedef struct EaDemuxContext {
68 enum AVCodecID video_codec;
71 int video_stream_index;
73 enum AVCodecID audio_codec;
74 int audio_stream_index;
82 static uint32_t read_arbitrary(AVIOContext *pb)
91 for (i = 0; i < size; i++) {
100 static int process_audio_header_elements(AVFormatContext *s)
102 EaDemuxContext *ea = s->priv_data;
103 AVIOContext *pb = s->pb;
105 int compression_type = -1, revision = -1, revision2 = -1;
108 ea->sample_rate = -1;
109 ea->num_channels = 1;
111 while (!pb->eof_reached && in_header) {
118 av_log(s, AV_LOG_DEBUG, "entered audio subheader\n");
120 while (!pb->eof_reached && in_subheader) {
122 subbyte = avio_r8(pb);
126 revision = read_arbitrary(pb);
127 av_log(s, AV_LOG_DEBUG,
128 "revision (element 0x80) set to 0x%08x\n", revision);
131 ea->num_channels = read_arbitrary(pb);
132 av_log(s, AV_LOG_DEBUG,
133 "num_channels (element 0x82) set to 0x%08x\n",
137 compression_type = read_arbitrary(pb);
138 av_log(s, AV_LOG_DEBUG,
139 "compression_type (element 0x83) set to 0x%08x\n",
143 ea->sample_rate = read_arbitrary(pb);
144 av_log(s, AV_LOG_DEBUG,
145 "sample_rate (element 0x84) set to %i\n",
149 ea->num_samples = read_arbitrary(pb);
150 av_log(s, AV_LOG_DEBUG,
151 "num_samples (element 0x85) set to 0x%08x\n",
155 av_log(s, AV_LOG_DEBUG,
156 "element 0x%02x set to 0x%08"PRIx32"\n",
157 subbyte, read_arbitrary(pb));
158 av_log(s, AV_LOG_DEBUG, "exited audio subheader\n");
162 revision2 = read_arbitrary(pb);
163 av_log(s, AV_LOG_DEBUG,
164 "revision2 (element 0xA0) set to 0x%08x\n",
168 av_log(s, AV_LOG_DEBUG,
169 "end of header block reached (within audio subheader)\n");
174 av_log(s, AV_LOG_DEBUG,
175 "element 0x%02x set to 0x%08"PRIx32"\n",
176 subbyte, read_arbitrary(pb));
182 av_log(s, AV_LOG_DEBUG, "end of header block reached\n");
186 av_log(s, AV_LOG_DEBUG,
187 "header element 0x%02x set to 0x%08"PRIx32"\n",
188 byte, read_arbitrary(pb));
193 switch (compression_type) {
195 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
198 ea->audio_codec = AV_CODEC_ID_ADPCM_EA;
203 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1;
206 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2;
209 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3;
214 av_log(s, AV_LOG_ERROR,
215 "unsupported stream type; revision=%i\n", revision);
220 ea->audio_codec = AV_CODEC_ID_PCM_S16LE_PLANAR;
223 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2;
226 ea->audio_codec = AV_CODEC_ID_MP3;
231 ea->audio_codec = AV_CODEC_ID_NONE;
232 av_log(s, AV_LOG_ERROR,
233 "unsupported stream type; revision2=%i\n", revision2);
238 av_log(s, AV_LOG_ERROR,
239 "unsupported stream type; compression_type=%i\n",
244 if (ea->sample_rate == -1)
245 ea->sample_rate = revision == 3 ? 48000 : 22050;
250 static void process_audio_header_eacs(AVFormatContext *s)
252 EaDemuxContext *ea = s->priv_data;
253 AVIOContext *pb = s->pb;
254 int compression_type;
256 ea->sample_rate = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
257 ea->bytes = avio_r8(pb); /* 1=8-bit, 2=16-bit */
258 ea->num_channels = avio_r8(pb);
259 compression_type = avio_r8(pb);
262 switch (compression_type) {
266 ea->audio_codec = AV_CODEC_ID_PCM_S8;
269 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
274 ea->audio_codec = AV_CODEC_ID_PCM_MULAW;
278 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS;
281 av_log(s, AV_LOG_ERROR,
282 "unsupported stream type; audio compression_type=%i\n",
287 static void process_audio_header_sead(AVFormatContext *s)
289 EaDemuxContext *ea = s->priv_data;
290 AVIOContext *pb = s->pb;
292 ea->sample_rate = avio_rl32(pb);
293 ea->bytes = avio_rl32(pb); /* 1=8-bit, 2=16-bit */
294 ea->num_channels = avio_rl32(pb);
295 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
298 static void process_video_header_mdec(AVFormatContext *s)
300 EaDemuxContext *ea = s->priv_data;
301 AVIOContext *pb = s->pb;
303 ea->width = avio_rl16(pb);
304 ea->height = avio_rl16(pb);
305 ea->time_base = (AVRational) { 1, 15 };
306 ea->video_codec = AV_CODEC_ID_MDEC;
309 static void process_video_header_vp6(AVFormatContext *s)
311 EaDemuxContext *ea = s->priv_data;
312 AVIOContext *pb = s->pb;
315 ea->time_base.den = avio_rl32(pb);
316 ea->time_base.num = avio_rl32(pb);
317 ea->video_codec = AV_CODEC_ID_VP6;
320 static void process_video_header_cmv(AVFormatContext *s)
322 EaDemuxContext *ea = s->priv_data;
325 avio_skip(s->pb, 10);
326 fps = avio_rl16(s->pb);
328 ea->time_base = (AVRational) { 1, fps };
329 ea->video_codec = AV_CODEC_ID_CMV;
332 /* Process EA file header.
333 * Return 1 if the EA file is valid and successfully opened, 0 otherwise. */
334 static int process_ea_header(AVFormatContext *s)
336 uint32_t blockid, size = 0;
337 EaDemuxContext *ea = s->priv_data;
338 AVIOContext *pb = s->pb;
341 for (i = 0; i < 5 && (!ea->audio_codec || !ea->video_codec); i++) {
342 unsigned int startpos = avio_tell(pb);
345 blockid = avio_rl32(pb);
346 size = avio_rl32(pb);
348 ea->big_endian = size > 0x000FFFFF;
350 size = av_bswap32(size);
354 if (avio_rl32(pb) != EACS_TAG) {
355 av_log(s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
358 process_audio_header_eacs(s);
363 blockid = avio_rl32(pb);
364 if (blockid == GSTR_TAG) {
366 } else if ((blockid & 0xFFFF) != PT00_TAG) {
367 av_log(s, AV_LOG_ERROR, "unknown SCHl headerid\n");
370 err = process_audio_header_elements(s);
374 process_audio_header_sead(s);
378 process_video_header_cmv(s);
382 ea->video_codec = AV_CODEC_ID_TGV;
383 ea->time_base = (AVRational) { 1, 15 };
387 process_video_header_mdec(s);
391 ea->video_codec = AV_CODEC_ID_MPEG2VIDEO;
396 ea->video_codec = AV_CODEC_ID_TGQ;
397 ea->time_base = (AVRational) { 1, 15 };
401 ea->video_codec = AV_CODEC_ID_TQI;
402 ea->time_base = (AVRational) { 1, 15 };
406 ea->video_codec = AV_CODEC_ID_MAD;
408 ea->time_base = (AVRational) { avio_rl16(pb), 1000 };
412 process_video_header_vp6(s);
417 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
421 avio_seek(pb, startpos + size, SEEK_SET);
424 avio_seek(pb, 0, SEEK_SET);
429 static int ea_probe(AVProbeData *p)
431 switch (AV_RL32(&p->buf[0])) {
445 if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
448 return AVPROBE_SCORE_MAX;
451 static int ea_read_header(AVFormatContext *s)
453 EaDemuxContext *ea = s->priv_data;
456 if (!process_ea_header(s))
459 if (ea->video_codec) {
460 /* initialize the video decoder stream */
461 st = avformat_new_stream(s, NULL);
463 return AVERROR(ENOMEM);
464 ea->video_stream_index = st->index;
465 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
466 st->codec->codec_id = ea->video_codec;
467 st->codec->codec_tag = 0; /* no fourcc */
468 st->codec->width = ea->width;
469 st->codec->height = ea->height;
470 avpriv_set_pts_info(st, 33, ea->time_base.num, ea->time_base.den);
471 st->avg_frame_rate = (AVRational) { ea->time_base.den,
475 if (ea->audio_codec) {
476 if (ea->num_channels <= 0 || ea->num_channels > 2) {
477 av_log(s, AV_LOG_WARNING,
478 "Unsupported number of channels: %d\n", ea->num_channels);
482 if (ea->sample_rate <= 0) {
483 av_log(s, AV_LOG_ERROR,
484 "Unsupported sample rate: %d\n", ea->sample_rate);
488 if (ea->bytes <= 0) {
489 av_log(s, AV_LOG_ERROR,
490 "Invalid number of bytes per sample: %d\n", ea->bytes);
491 ea->audio_codec = AV_CODEC_ID_NONE;
495 /* initialize the audio decoder stream */
496 st = avformat_new_stream(s, NULL);
498 return AVERROR(ENOMEM);
499 avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
500 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
501 st->codec->codec_id = ea->audio_codec;
502 st->codec->codec_tag = 0; /* no tag */
503 st->codec->channels = ea->num_channels;
504 st->codec->sample_rate = ea->sample_rate;
505 st->codec->bits_per_coded_sample = ea->bytes * 8;
506 st->codec->bit_rate = st->codec->channels *
507 st->codec->sample_rate *
508 st->codec->bits_per_coded_sample / 4;
509 st->codec->block_align = st->codec->channels *
510 st->codec->bits_per_coded_sample;
511 ea->audio_stream_index = st->index;
518 static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
520 EaDemuxContext *ea = s->priv_data;
521 AVIOContext *pb = s->pb;
522 unsigned int chunk_type, chunk_size;
523 int ret = 0, packet_read = 0, key = 0;
524 int av_uninit(num_samples);
526 while (!packet_read) {
527 chunk_type = avio_rl32(pb);
528 chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
530 return AVERROR_INVALIDDATA;
533 switch (chunk_type) {
536 /* header chunk also contains data; skip over the header portion */
538 return AVERROR_INVALIDDATA;
545 if (!ea->audio_codec) {
546 avio_skip(pb, chunk_size);
548 } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
549 ea->audio_codec == AV_CODEC_ID_MP3) {
550 num_samples = avio_rl32(pb);
557 ret = av_get_packet(pb, pkt, chunk_size);
560 pkt->stream_index = ea->audio_stream_index;
562 switch (ea->audio_codec) {
563 case AV_CODEC_ID_ADPCM_EA:
564 case AV_CODEC_ID_ADPCM_EA_R1:
565 case AV_CODEC_ID_ADPCM_EA_R2:
566 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
567 case AV_CODEC_ID_ADPCM_EA_R3:
569 av_log(s, AV_LOG_ERROR, "Packet is too short\n");
570 av_packet_unref(pkt);
571 return AVERROR_INVALIDDATA;
573 if (ea->audio_codec == AV_CODEC_ID_ADPCM_EA_R3)
574 pkt->duration = AV_RB32(pkt->data);
576 pkt->duration = AV_RL32(pkt->data);
578 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
579 pkt->duration = ret * 2 / ea->num_channels;
581 case AV_CODEC_ID_PCM_S16LE_PLANAR:
582 case AV_CODEC_ID_MP3:
583 pkt->duration = num_samples;
586 pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
607 key = AV_PKT_FLAG_KEY;
612 avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
614 goto get_video_packet;
618 return AVERROR_INVALIDDATA;
620 avio_skip(pb, 8); // skip ea DCT header
622 goto get_video_packet;
627 key = AV_PKT_FLAG_KEY;
633 ret = av_get_packet(pb, pkt, chunk_size);
636 pkt->stream_index = ea->video_stream_index;
642 avio_skip(pb, chunk_size);
650 AVInputFormat ff_ea_demuxer = {
652 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
653 .priv_data_size = sizeof(EaDemuxContext),
654 .read_probe = ea_probe,
655 .read_header = ea_read_header,
656 .read_packet = ea_read_packet,