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') /* 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;
70 int video_stream_index;
72 enum CodecID audio_codec;
73 int audio_stream_index;
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 (!url_feof(pb) && inHeader) {
120 av_log (s, AV_LOG_DEBUG, "entered audio subheader\n");
122 while (!url_feof(pb) && 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->nb_frames = avio_rl32(pb);
281 ea->time_base.den = avio_rl32(pb);
282 ea->time_base.num = avio_rl32(pb);
283 ea->video_codec = CODEC_ID_VP6;
288 static int process_video_header_cmv(AVFormatContext *s)
290 EaDemuxContext *ea = s->priv_data;
293 avio_skip(s->pb, 10);
294 fps = avio_rl16(s->pb);
296 ea->time_base = (AVRational){1, fps};
297 ea->video_codec = CODEC_ID_CMV;
303 * Process EA file header
304 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
306 static int process_ea_header(AVFormatContext *s) {
307 uint32_t blockid, size = 0;
308 EaDemuxContext *ea = s->priv_data;
309 AVIOContext *pb = s->pb;
312 for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
313 unsigned int startpos = avio_tell(pb);
316 blockid = avio_rl32(pb);
317 size = avio_rl32(pb);
319 ea->big_endian = size > 0x000FFFFF;
321 size = av_bswap32(size);
325 if (avio_rl32(pb) != EACS_TAG) {
326 av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
329 err = process_audio_header_eacs(s);
334 blockid = avio_rl32(pb);
335 if (blockid == GSTR_TAG) {
337 } else if ((blockid & 0xFFFF)!=PT00_TAG) {
338 av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
341 err = process_audio_header_elements(s);
345 err = process_audio_header_sead(s);
349 err = process_video_header_cmv(s);
353 ea->video_codec = CODEC_ID_TGV;
357 err = process_video_header_mdec(s);
361 ea->video_codec = CODEC_ID_MPEG2VIDEO;
366 ea->video_codec = CODEC_ID_TGQ;
370 ea->video_codec = CODEC_ID_TQI;
374 ea->video_codec = CODEC_ID_MAD;
378 err = process_video_header_vp6(s);
383 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
387 avio_seek(pb, startpos + size, SEEK_SET);
390 avio_seek(pb, 0, SEEK_SET);
396 static int ea_probe(AVProbeData *p)
398 switch (AV_RL32(&p->buf[0])) {
412 if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
414 return AVPROBE_SCORE_MAX;
417 static int ea_read_header(AVFormatContext *s)
419 EaDemuxContext *ea = s->priv_data;
422 if (process_ea_header(s)<=0)
425 if (ea->video_codec) {
426 /* initialize the video decoder stream */
427 st = avformat_new_stream(s, NULL);
429 return AVERROR(ENOMEM);
430 ea->video_stream_index = st->index;
431 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
432 st->codec->codec_id = ea->video_codec;
433 // parsing is necessary to make FFmpeg generate correct timestamps
434 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
435 st->need_parsing = AVSTREAM_PARSE_HEADERS;
436 st->codec->codec_tag = 0; /* no fourcc */
437 st->codec->width = ea->width;
438 st->codec->height = ea->height;
439 st->duration = st->nb_frames = ea->nb_frames;
440 if (ea->time_base.num)
441 avpriv_set_pts_info(st, 64, ea->time_base.num, ea->time_base.den);
443 st->avg_frame_rate = av_inv_q(ea->time_base);
446 if (ea->audio_codec) {
447 if (ea->num_channels <= 0) {
448 av_log(s, AV_LOG_WARNING, "Unsupported number of channels: %d\n", ea->num_channels);
452 if (ea->sample_rate <= 0) {
453 av_log(s, AV_LOG_ERROR, "Unsupported sample rate: %d\n", ea->sample_rate);
457 if (ea->bytes <= 0) {
458 av_log(s, AV_LOG_ERROR, "Invalid number of bytes per sample: %d\n", ea->bytes);
459 ea->audio_codec = CODEC_ID_NONE;
463 /* initialize the audio decoder stream */
464 st = avformat_new_stream(s, NULL);
466 return AVERROR(ENOMEM);
467 avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
468 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
469 st->codec->codec_id = ea->audio_codec;
470 st->codec->codec_tag = 0; /* no tag */
471 st->codec->channels = ea->num_channels;
472 st->codec->sample_rate = ea->sample_rate;
473 st->codec->bits_per_coded_sample = ea->bytes * 8;
474 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
475 st->codec->bits_per_coded_sample / 4;
476 st->codec->block_align = st->codec->channels*st->codec->bits_per_coded_sample;
477 ea->audio_stream_index = st->index;
484 static int ea_read_packet(AVFormatContext *s,
487 EaDemuxContext *ea = s->priv_data;
488 AVIOContext *pb = s->pb;
491 int partial_packet = 0;
492 unsigned int chunk_type, chunk_size;
494 int av_uninit(num_samples);
496 while (!packet_read || partial_packet) {
497 chunk_type = avio_rl32(pb);
498 chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
500 return AVERROR_INVALIDDATA;
503 switch (chunk_type) {
506 /* header chunk also contains data; skip over the header portion*/
508 return AVERROR_INVALIDDATA;
515 if (!ea->audio_codec) {
516 avio_skip(pb, chunk_size);
518 } else if (ea->audio_codec == CODEC_ID_PCM_S16LE_PLANAR ||
519 ea->audio_codec == CODEC_ID_MP3) {
520 num_samples = avio_rl32(pb);
524 if (partial_packet) {
525 av_log_ask_for_sample(s, "video header followed by audio packet not supported.\n");
529 ret = av_get_packet(pb, pkt, chunk_size);
532 pkt->stream_index = ea->audio_stream_index;
534 switch (ea->audio_codec) {
535 case CODEC_ID_ADPCM_EA:
536 case CODEC_ID_ADPCM_EA_R1:
537 case CODEC_ID_ADPCM_EA_R2:
538 case CODEC_ID_ADPCM_IMA_EA_EACS:
539 pkt->duration = AV_RL32(pkt->data);
541 case CODEC_ID_ADPCM_EA_R3:
542 pkt->duration = AV_RB32(pkt->data);
544 case CODEC_ID_ADPCM_IMA_EA_SEAD:
545 pkt->duration = ret * 2 / ea->num_channels;
547 case CODEC_ID_PCM_S16LE_PLANAR:
549 pkt->duration = num_samples;
552 pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
573 key = AV_PKT_FLAG_KEY;
578 avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
580 goto get_video_packet;
583 avio_skip(pb, 8); // skip ea dct header
585 goto get_video_packet;
590 key = AV_PKT_FLAG_KEY;
593 if (partial_packet) {
594 ret = av_append_packet(pb, pkt, chunk_size);
596 ret = av_get_packet(pb, pkt, chunk_size);
601 partial_packet = chunk_type == MVIh_TAG;
602 pkt->stream_index = ea->video_stream_index;
608 avio_skip(pb, chunk_size);
613 if (ret < 0 && partial_packet)
618 AVInputFormat ff_ea_demuxer = {
620 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
621 .priv_data_size = sizeof(EaDemuxContext),
622 .read_probe = ea_probe,
623 .read_header = ea_read_header,
624 .read_packet = ea_read_packet,