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)
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 */
64 #define AVP6_TAG MKTAG('A', 'V', 'P', '6')
66 typedef struct VideoProperties {
74 typedef struct EaDemuxContext {
77 VideoProperties video;
79 enum AVCodecID audio_codec;
80 int audio_stream_index;
88 static uint32_t read_arbitrary(AVIOContext *pb)
97 for (i = 0; i < size; i++) {
106 static int process_audio_header_elements(AVFormatContext *s)
108 EaDemuxContext *ea = s->priv_data;
109 AVIOContext *pb = s->pb;
111 int compression_type = -1, revision = -1, revision2 = -1;
114 ea->sample_rate = -1;
115 ea->num_channels = 1;
117 while (!avio_feof(pb) && in_header) {
124 av_log(s, AV_LOG_DEBUG, "entered audio subheader\n");
126 while (!avio_feof(pb) && in_subheader) {
128 subbyte = avio_r8(pb);
132 revision = read_arbitrary(pb);
133 av_log(s, AV_LOG_DEBUG,
134 "revision (element 0x80) set to 0x%08x\n", revision);
137 ea->num_channels = read_arbitrary(pb);
138 av_log(s, AV_LOG_DEBUG,
139 "num_channels (element 0x82) set to 0x%08x\n",
143 compression_type = read_arbitrary(pb);
144 av_log(s, AV_LOG_DEBUG,
145 "compression_type (element 0x83) set to 0x%08x\n",
149 ea->sample_rate = read_arbitrary(pb);
150 av_log(s, AV_LOG_DEBUG,
151 "sample_rate (element 0x84) set to %i\n",
155 ea->num_samples = read_arbitrary(pb);
156 av_log(s, AV_LOG_DEBUG,
157 "num_samples (element 0x85) set to 0x%08x\n",
161 av_log(s, AV_LOG_DEBUG,
162 "element 0x%02x set to 0x%08"PRIx32"\n",
163 subbyte, read_arbitrary(pb));
164 av_log(s, AV_LOG_DEBUG, "exited audio subheader\n");
168 revision2 = read_arbitrary(pb);
169 av_log(s, AV_LOG_DEBUG,
170 "revision2 (element 0xA0) set to 0x%08x\n",
174 av_log(s, AV_LOG_DEBUG,
175 "end of header block reached (within audio subheader)\n");
180 av_log(s, AV_LOG_DEBUG,
181 "element 0x%02x set to 0x%08"PRIx32"\n",
182 subbyte, read_arbitrary(pb));
188 av_log(s, AV_LOG_DEBUG, "end of header block reached\n");
192 av_log(s, AV_LOG_DEBUG,
193 "header element 0x%02x set to 0x%08"PRIx32"\n",
194 byte, read_arbitrary(pb));
199 switch (compression_type) {
201 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
204 ea->audio_codec = AV_CODEC_ID_ADPCM_EA;
209 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1;
212 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2;
215 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3;
220 avpriv_request_sample(s, "stream type; revision=%i", revision);
225 ea->audio_codec = AV_CODEC_ID_PCM_S16LE_PLANAR;
230 case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
231 case 3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
233 avpriv_request_sample(s, "stream type; revision=%i, revision2=%i", revision, revision2);
238 ea->audio_codec = AV_CODEC_ID_MP3;
243 ea->audio_codec = AV_CODEC_ID_NONE;
244 avpriv_request_sample(s, "stream type; revision2=%i", revision2);
249 avpriv_request_sample(s,
250 "stream type; compression_type=%i",
255 if (ea->sample_rate == -1)
256 ea->sample_rate = revision == 3 ? 48000 : 22050;
261 static void process_audio_header_eacs(AVFormatContext *s)
263 EaDemuxContext *ea = s->priv_data;
264 AVIOContext *pb = s->pb;
265 int compression_type;
267 ea->sample_rate = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
268 ea->bytes = avio_r8(pb); /* 1=8-bit, 2=16-bit */
269 ea->num_channels = avio_r8(pb);
270 compression_type = avio_r8(pb);
273 switch (compression_type) {
277 ea->audio_codec = AV_CODEC_ID_PCM_S8;
280 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
285 ea->audio_codec = AV_CODEC_ID_PCM_MULAW;
289 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS;
292 avpriv_request_sample(s,
293 "stream type; audio compression_type=%i",
298 static void process_audio_header_sead(AVFormatContext *s)
300 EaDemuxContext *ea = s->priv_data;
301 AVIOContext *pb = s->pb;
303 ea->sample_rate = avio_rl32(pb);
304 ea->bytes = avio_rl32(pb); /* 1=8-bit, 2=16-bit */
305 ea->num_channels = avio_rl32(pb);
306 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
309 static void process_video_header_mdec(AVFormatContext *s, VideoProperties *video)
311 AVIOContext *pb = s->pb;
313 video->width = avio_rl16(pb);
314 video->height = avio_rl16(pb);
315 video->time_base = (AVRational) { 1, 15 };
316 video->codec = AV_CODEC_ID_MDEC;
319 static int process_video_header_vp6(AVFormatContext *s, VideoProperties *video)
321 AVIOContext *pb = s->pb;
324 video->nb_frames = avio_rl32(pb);
326 video->time_base.den = avio_rl32(pb);
327 video->time_base.num = avio_rl32(pb);
328 if (video->time_base.den <= 0 || video->time_base.num <= 0) {
329 av_log(s, AV_LOG_ERROR, "Timebase is invalid\n");
330 return AVERROR_INVALIDDATA;
332 video->codec = AV_CODEC_ID_VP6;
337 static void process_video_header_cmv(AVFormatContext *s, VideoProperties *video)
341 avio_skip(s->pb, 10);
342 fps = avio_rl16(s->pb);
344 video->time_base = (AVRational) { 1, fps };
345 video->codec = AV_CODEC_ID_CMV;
348 /* Process EA file header.
349 * Return 1 if the EA file is valid and successfully opened, 0 otherwise. */
350 static int process_ea_header(AVFormatContext *s)
352 uint32_t blockid, size = 0;
353 EaDemuxContext *ea = s->priv_data;
354 AVIOContext *pb = s->pb;
357 for (i = 0; i < 5 && (!ea->audio_codec || !ea->video.codec); i++) {
358 uint64_t startpos = avio_tell(pb);
361 blockid = avio_rl32(pb);
362 size = avio_rl32(pb);
364 ea->big_endian = size > av_bswap32(size);
366 size = av_bswap32(size);
369 av_log(s, AV_LOG_ERROR, "chunk size too small\n");
370 return AVERROR_INVALIDDATA;
375 if (avio_rl32(pb) != EACS_TAG) {
376 avpriv_request_sample(s, "unknown 1SNh headerid");
379 process_audio_header_eacs(s);
384 blockid = avio_rl32(pb);
385 if (blockid == GSTR_TAG) {
387 } else if ((blockid & 0xFFFF) != PT00_TAG) {
388 avpriv_request_sample(s, "unknown SCHl headerid");
391 err = process_audio_header_elements(s);
395 process_audio_header_sead(s);
399 process_video_header_cmv(s, &ea->video);
403 ea->video.codec = AV_CODEC_ID_TGV;
407 process_video_header_mdec(s, &ea->video);
411 ea->video.codec = AV_CODEC_ID_MPEG2VIDEO;
416 ea->video.codec = AV_CODEC_ID_TGQ;
417 ea->video.time_base = (AVRational) { 1, 15 };
421 ea->video.codec = AV_CODEC_ID_TQI;
422 ea->video.time_base = (AVRational) { 1, 15 };
426 ea->video.codec = AV_CODEC_ID_MAD;
428 ea->video.time_base = (AVRational) { avio_rl16(pb), 1000 };
432 err = process_video_header_vp6(s, &ea->video);
437 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
441 avio_seek(pb, startpos + size, SEEK_SET);
444 avio_seek(pb, 0, SEEK_SET);
449 static int ea_probe(AVProbeData *p)
451 unsigned big_endian, size;
453 switch (AV_RL32(&p->buf[0])) {
468 size = AV_RL32(&p->buf[4]);
469 big_endian = size > 0x000FFFFF;
471 size = av_bswap32(size);
472 if (size > 0xfffff || size < 8)
475 return AVPROBE_SCORE_MAX;
478 static int init_video_stream(AVFormatContext *s, VideoProperties *video)
485 /* initialize the video decoder stream */
486 st = avformat_new_stream(s, NULL);
488 return AVERROR(ENOMEM);
489 video->stream_index = st->index;
490 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
491 st->codec->codec_id = video->codec;
492 // parsing is necessary to make FFmpeg generate correct timestamps
493 if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
494 st->need_parsing = AVSTREAM_PARSE_HEADERS;
495 st->codec->codec_tag = 0; /* no fourcc */
496 st->codec->width = video->width;
497 st->codec->height = video->height;
498 st->duration = st->nb_frames = video->nb_frames;
499 if (video->time_base.num)
500 avpriv_set_pts_info(st, 64, video->time_base.num, video->time_base.den);
502 st->avg_frame_rate = av_inv_q(video->time_base);
506 static int ea_read_header(AVFormatContext *s)
508 EaDemuxContext *ea = s->priv_data;
511 if (process_ea_header(s)<=0)
514 if (init_video_stream(s, &ea->video))
515 return AVERROR(ENOMEM);
517 if (ea->audio_codec) {
518 if (ea->num_channels <= 0 || ea->num_channels > 2) {
519 av_log(s, AV_LOG_WARNING,
520 "Unsupported number of channels: %d\n", ea->num_channels);
524 if (ea->sample_rate <= 0) {
525 av_log(s, AV_LOG_ERROR,
526 "Unsupported sample rate: %d\n", ea->sample_rate);
530 if (ea->bytes <= 0) {
531 av_log(s, AV_LOG_ERROR,
532 "Invalid number of bytes per sample: %d\n", ea->bytes);
533 ea->audio_codec = AV_CODEC_ID_NONE;
537 /* initialize the audio decoder stream */
538 st = avformat_new_stream(s, NULL);
540 return AVERROR(ENOMEM);
541 avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
542 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
543 st->codec->codec_id = ea->audio_codec;
544 st->codec->codec_tag = 0; /* no tag */
545 st->codec->channels = ea->num_channels;
546 st->codec->sample_rate = ea->sample_rate;
547 st->codec->bits_per_coded_sample = ea->bytes * 8;
548 st->codec->bit_rate = st->codec->channels *
549 st->codec->sample_rate *
550 st->codec->bits_per_coded_sample / 4;
551 st->codec->block_align = st->codec->channels *
552 st->codec->bits_per_coded_sample;
553 ea->audio_stream_index = st->index;
560 static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
562 EaDemuxContext *ea = s->priv_data;
563 AVIOContext *pb = s->pb;
564 int partial_packet = 0;
565 unsigned int chunk_type, chunk_size;
566 int ret = 0, packet_read = 0, key = 0;
567 int av_uninit(num_samples);
569 while (!packet_read || partial_packet) {
570 chunk_type = avio_rl32(pb);
571 chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
573 return AVERROR_INVALIDDATA;
576 switch (chunk_type) {
579 /* header chunk also contains data; skip over the header portion */
581 return AVERROR_INVALIDDATA;
588 if (!ea->audio_codec) {
589 avio_skip(pb, chunk_size);
591 } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
592 ea->audio_codec == AV_CODEC_ID_MP3) {
593 num_samples = avio_rl32(pb);
598 if (partial_packet) {
599 avpriv_request_sample(s, "video header followed by audio packet");
607 ret = av_get_packet(pb, pkt, chunk_size);
610 pkt->stream_index = ea->audio_stream_index;
612 switch (ea->audio_codec) {
613 case AV_CODEC_ID_ADPCM_EA:
614 case AV_CODEC_ID_ADPCM_EA_R1:
615 case AV_CODEC_ID_ADPCM_EA_R2:
616 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
617 case AV_CODEC_ID_ADPCM_EA_R3:
619 av_log(s, AV_LOG_ERROR, "Packet is too short\n");
621 return AVERROR_INVALIDDATA;
623 if (ea->audio_codec == AV_CODEC_ID_ADPCM_EA_R3)
624 pkt->duration = AV_RB32(pkt->data);
626 pkt->duration = AV_RL32(pkt->data);
628 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
629 pkt->duration = ret * 2 / ea->num_channels;
631 case AV_CODEC_ID_PCM_S16LE_PLANAR:
632 case AV_CODEC_ID_MP3:
633 pkt->duration = num_samples;
636 pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
657 key = AV_PKT_FLAG_KEY;
662 avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
664 goto get_video_packet;
668 return AVERROR_INVALIDDATA;
670 avio_skip(pb, 8); // skip ea DCT header
672 goto get_video_packet;
677 key = AV_PKT_FLAG_KEY;
683 if (partial_packet) {
684 ret = av_append_packet(pb, pkt, chunk_size);
686 ret = av_get_packet(pb, pkt, chunk_size);
691 partial_packet = chunk_type == MVIh_TAG;
692 pkt->stream_index = ea->video.stream_index;
698 avio_skip(pb, chunk_size);
703 if (ret < 0 && partial_packet)
708 AVInputFormat ff_ea_demuxer = {
710 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
711 .priv_data_size = sizeof(EaDemuxContext),
712 .read_probe = ea_probe,
713 .read_header = ea_read_header,
714 .read_packet = ea_read_packet,