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 FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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;
70 int video_stream_index;
72 enum AVCodecID audio_codec;
73 int audio_stream_index;
81 static uint32_t read_arbitrary(AVIOContext *pb)
90 for (i = 0; i < size; i++) {
99 static int process_audio_header_elements(AVFormatContext *s)
101 EaDemuxContext *ea = s->priv_data;
102 AVIOContext *pb = s->pb;
104 int compression_type = -1, revision = -1, revision2 = -1;
107 ea->sample_rate = -1;
108 ea->num_channels = 1;
110 while (!url_feof(pb) && in_header) {
117 av_log(s, AV_LOG_DEBUG, "entered audio subheader\n");
119 while (!url_feof(pb) && in_subheader) {
121 subbyte = avio_r8(pb);
125 revision = read_arbitrary(pb);
126 av_log(s, AV_LOG_DEBUG,
127 "revision (element 0x80) set to 0x%08x\n", revision);
130 ea->num_channels = read_arbitrary(pb);
131 av_log(s, AV_LOG_DEBUG,
132 "num_channels (element 0x82) set to 0x%08x\n",
136 compression_type = read_arbitrary(pb);
137 av_log(s, AV_LOG_DEBUG,
138 "compression_type (element 0x83) set to 0x%08x\n",
142 ea->sample_rate = read_arbitrary(pb);
143 av_log(s, AV_LOG_DEBUG,
144 "sample_rate (element 0x84) set to %i\n",
148 ea->num_samples = read_arbitrary(pb);
149 av_log(s, AV_LOG_DEBUG,
150 "num_samples (element 0x85) set to 0x%08x\n",
154 av_log(s, AV_LOG_DEBUG,
155 "element 0x%02x set to 0x%08x\n",
156 subbyte, read_arbitrary(pb));
157 av_log(s, AV_LOG_DEBUG, "exited audio subheader\n");
161 revision2 = read_arbitrary(pb);
162 av_log(s, AV_LOG_DEBUG,
163 "revision2 (element 0xA0) set to 0x%08x\n",
167 av_log(s, AV_LOG_DEBUG,
168 "end of header block reached (within audio subheader)\n");
173 av_log(s, AV_LOG_DEBUG,
174 "element 0x%02x set to 0x%08x\n",
175 subbyte, read_arbitrary(pb));
181 av_log(s, AV_LOG_DEBUG, "end of header block reached\n");
185 av_log(s, AV_LOG_DEBUG,
186 "header element 0x%02x set to 0x%08x\n",
187 byte, read_arbitrary(pb));
192 switch (compression_type) {
194 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
197 ea->audio_codec = AV_CODEC_ID_ADPCM_EA;
202 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1;
205 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2;
208 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3;
213 avpriv_request_sample(s, "stream type; revision=%i", revision);
218 ea->audio_codec = AV_CODEC_ID_PCM_S16LE_PLANAR;
223 case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
224 case 3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
226 avpriv_request_sample(s, "stream type; revision=%i, revision2=%i", revision, revision2);
231 ea->audio_codec = AV_CODEC_ID_MP3;
236 ea->audio_codec = AV_CODEC_ID_NONE;
237 avpriv_request_sample(s, "stream type; revision2=%i", revision2);
242 avpriv_request_sample(s,
243 "stream type; compression_type=%i",
248 if (ea->sample_rate == -1)
249 ea->sample_rate = revision == 3 ? 48000 : 22050;
254 static void process_audio_header_eacs(AVFormatContext *s)
256 EaDemuxContext *ea = s->priv_data;
257 AVIOContext *pb = s->pb;
258 int compression_type;
260 ea->sample_rate = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
261 ea->bytes = avio_r8(pb); /* 1=8-bit, 2=16-bit */
262 ea->num_channels = avio_r8(pb);
263 compression_type = avio_r8(pb);
266 switch (compression_type) {
270 ea->audio_codec = AV_CODEC_ID_PCM_S8;
273 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
278 ea->audio_codec = AV_CODEC_ID_PCM_MULAW;
282 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS;
285 avpriv_request_sample(s,
286 "stream type; audio compression_type=%i",
291 static void process_audio_header_sead(AVFormatContext *s)
293 EaDemuxContext *ea = s->priv_data;
294 AVIOContext *pb = s->pb;
296 ea->sample_rate = avio_rl32(pb);
297 ea->bytes = avio_rl32(pb); /* 1=8-bit, 2=16-bit */
298 ea->num_channels = avio_rl32(pb);
299 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
302 static void process_video_header_mdec(AVFormatContext *s)
304 EaDemuxContext *ea = s->priv_data;
305 AVIOContext *pb = s->pb;
307 ea->width = avio_rl16(pb);
308 ea->height = avio_rl16(pb);
309 ea->time_base = (AVRational) { 1, 15 };
310 ea->video_codec = AV_CODEC_ID_MDEC;
313 static int process_video_header_vp6(AVFormatContext *s)
315 EaDemuxContext *ea = s->priv_data;
316 AVIOContext *pb = s->pb;
319 ea->nb_frames = avio_rl32(pb);
321 ea->time_base.den = avio_rl32(pb);
322 ea->time_base.num = avio_rl32(pb);
323 if (ea->time_base.den <= 0 || ea->time_base.num <= 0) {
324 av_log(s, AV_LOG_ERROR, "Timebase is invalid\n");
325 return AVERROR_INVALIDDATA;
327 ea->video_codec = AV_CODEC_ID_VP6;
332 static void process_video_header_cmv(AVFormatContext *s)
334 EaDemuxContext *ea = s->priv_data;
337 avio_skip(s->pb, 10);
338 fps = avio_rl16(s->pb);
340 ea->time_base = (AVRational) { 1, fps };
341 ea->video_codec = AV_CODEC_ID_CMV;
344 /* Process EA file header.
345 * Return 1 if the EA file is valid and successfully opened, 0 otherwise. */
346 static int process_ea_header(AVFormatContext *s)
348 uint32_t blockid, size = 0;
349 EaDemuxContext *ea = s->priv_data;
350 AVIOContext *pb = s->pb;
353 for (i = 0; i < 5 && (!ea->audio_codec || !ea->video_codec); i++) {
354 unsigned int startpos = avio_tell(pb);
357 blockid = avio_rl32(pb);
358 size = avio_rl32(pb);
360 ea->big_endian = size > 0x000FFFFF;
362 size = av_bswap32(size);
366 if (avio_rl32(pb) != EACS_TAG) {
367 avpriv_request_sample(s, "unknown 1SNh headerid");
370 process_audio_header_eacs(s);
375 blockid = avio_rl32(pb);
376 if (blockid == GSTR_TAG) {
378 } else if ((blockid & 0xFFFF) != PT00_TAG) {
379 avpriv_request_sample(s, "unknown SCHl headerid");
382 err = process_audio_header_elements(s);
386 process_audio_header_sead(s);
390 process_video_header_cmv(s);
394 ea->video_codec = AV_CODEC_ID_TGV;
398 process_video_header_mdec(s);
402 ea->video_codec = AV_CODEC_ID_MPEG2VIDEO;
407 ea->video_codec = AV_CODEC_ID_TGQ;
411 ea->video_codec = AV_CODEC_ID_TQI;
415 ea->video_codec = AV_CODEC_ID_MAD;
419 err = process_video_header_vp6(s);
424 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
428 avio_seek(pb, startpos + size, SEEK_SET);
431 avio_seek(pb, 0, SEEK_SET);
436 static int ea_probe(AVProbeData *p)
438 switch (AV_RL32(&p->buf[0])) {
452 if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
455 return AVPROBE_SCORE_MAX;
458 static int ea_read_header(AVFormatContext *s)
460 EaDemuxContext *ea = s->priv_data;
463 if (process_ea_header(s)<=0)
466 if (ea->video_codec) {
467 /* initialize the video decoder stream */
468 st = avformat_new_stream(s, NULL);
470 return AVERROR(ENOMEM);
471 ea->video_stream_index = st->index;
472 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
473 st->codec->codec_id = ea->video_codec;
474 // parsing is necessary to make FFmpeg generate correct timestamps
475 if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
476 st->need_parsing = AVSTREAM_PARSE_HEADERS;
477 st->codec->codec_tag = 0; /* no fourcc */
478 st->codec->width = ea->width;
479 st->codec->height = ea->height;
480 st->duration = st->nb_frames = ea->nb_frames;
481 if (ea->time_base.num)
482 avpriv_set_pts_info(st, 64, ea->time_base.num, ea->time_base.den);
484 st->avg_frame_rate = av_inv_q(ea->time_base);
487 if (ea->audio_codec) {
488 if (ea->num_channels <= 0) {
489 av_log(s, AV_LOG_WARNING,
490 "Unsupported number of channels: %d\n", ea->num_channels);
494 if (ea->sample_rate <= 0) {
495 av_log(s, AV_LOG_ERROR,
496 "Unsupported sample rate: %d\n", ea->sample_rate);
500 if (ea->bytes <= 0) {
501 av_log(s, AV_LOG_ERROR,
502 "Invalid number of bytes per sample: %d\n", ea->bytes);
503 ea->audio_codec = AV_CODEC_ID_NONE;
507 /* initialize the audio decoder stream */
508 st = avformat_new_stream(s, NULL);
510 return AVERROR(ENOMEM);
511 avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
512 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
513 st->codec->codec_id = ea->audio_codec;
514 st->codec->codec_tag = 0; /* no tag */
515 st->codec->channels = ea->num_channels;
516 st->codec->sample_rate = ea->sample_rate;
517 st->codec->bits_per_coded_sample = ea->bytes * 8;
518 st->codec->bit_rate = st->codec->channels *
519 st->codec->sample_rate *
520 st->codec->bits_per_coded_sample / 4;
521 st->codec->block_align = st->codec->channels *
522 st->codec->bits_per_coded_sample;
523 ea->audio_stream_index = st->index;
530 static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
532 EaDemuxContext *ea = s->priv_data;
533 AVIOContext *pb = s->pb;
534 int partial_packet = 0;
535 unsigned int chunk_type, chunk_size;
536 int ret = 0, packet_read = 0, key = 0;
537 int av_uninit(num_samples);
539 while (!packet_read || partial_packet) {
540 chunk_type = avio_rl32(pb);
541 chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
543 return AVERROR_INVALIDDATA;
546 switch (chunk_type) {
549 /* header chunk also contains data; skip over the header portion */
551 return AVERROR_INVALIDDATA;
558 if (!ea->audio_codec) {
559 avio_skip(pb, chunk_size);
561 } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
562 ea->audio_codec == AV_CODEC_ID_MP3) {
563 num_samples = avio_rl32(pb);
567 if (partial_packet) {
568 avpriv_request_sample(s, "video header followed by audio packet");
572 ret = av_get_packet(pb, pkt, chunk_size);
575 pkt->stream_index = ea->audio_stream_index;
577 switch (ea->audio_codec) {
578 case AV_CODEC_ID_ADPCM_EA:
579 case AV_CODEC_ID_ADPCM_EA_R1:
580 case AV_CODEC_ID_ADPCM_EA_R2:
581 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
583 pkt->duration = AV_RL32(pkt->data);
585 case AV_CODEC_ID_ADPCM_EA_R3:
587 pkt->duration = AV_RB32(pkt->data);
589 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
590 pkt->duration = ret * 2 / ea->num_channels;
592 case AV_CODEC_ID_PCM_S16LE_PLANAR:
593 case AV_CODEC_ID_MP3:
594 pkt->duration = num_samples;
597 pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
618 key = AV_PKT_FLAG_KEY;
623 avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
625 goto get_video_packet;
628 avio_skip(pb, 8); // skip ea DCT header
630 goto get_video_packet;
635 key = AV_PKT_FLAG_KEY;
638 if (partial_packet) {
639 ret = av_append_packet(pb, pkt, chunk_size);
641 ret = av_get_packet(pb, pkt, chunk_size);
646 partial_packet = chunk_type == MVIh_TAG;
647 pkt->stream_index = ea->video_stream_index;
653 avio_skip(pb, chunk_size);
658 if (ret < 0 && partial_packet)
663 AVInputFormat ff_ea_demuxer = {
665 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
666 .priv_data_size = sizeof(EaDemuxContext),
667 .read_probe = ea_probe,
668 .read_header = ea_read_header,
669 .read_packet = ea_read_packet,