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
23 * @file libavformat/electronicarts.c
24 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
25 * by Robin Kay (komadori at gekkou.co.uk)
28 #include "libavutil/intreadwrite.h"
31 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
32 #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
33 #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
34 #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
35 #define SHEN_TAG MKTAG('S', 'H', 'E', 'N') /* SxEN header */
36 #define SDEN_TAG MKTAG('S', 'D', 'E', 'N') /* SxEN data */
37 #define SEEN_TAG MKTAG('S', 'E', 'E', 'N') /* SxEN end */
38 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
39 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
40 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
41 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
42 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
43 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
44 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
45 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
46 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV i-frame */
47 #define fVGT_TAG MKTAG('f', 'V', 'G', 'T') /* TGV p-frame */
48 #define mTCD_TAG MKTAG('m', 'T', 'C', 'D') /* MDEC */
49 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
50 #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD p-frame */
51 #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
52 #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG2 */
53 #define TGQs_TAG MKTAG('T', 'G', 'Q', 's') /* TGQ i-frame (appears in .TGQ files) */
54 #define pQGT_TAG MKTAG('p', 'Q', 'G', 'T') /* TGQ i-frame (appears in .UV files) */
55 #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T') /* TQI/UV2 i-frame (.UV2/.WVE) */
56 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
57 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
58 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
59 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
60 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV i-frame */
62 typedef struct EaDemuxContext
{
65 enum CodecID video_codec
;
68 int video_stream_index
;
70 enum CodecID audio_codec
;
71 int audio_stream_index
;
72 int audio_frame_counter
;
80 static uint32_t read_arbitary(ByteIOContext
*pb
) {
88 for (i
= 0; i
< size
; i
++) {
98 * Process PT/GSTR sound header
99 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
101 static int process_audio_header_elements(AVFormatContext
*s
)
104 EaDemuxContext
*ea
= s
->priv_data
;
105 ByteIOContext
*pb
= s
->pb
;
106 int compression_type
= -1, revision
= -1, revision2
= -1;
109 ea
->sample_rate
= -1;
110 ea
->num_channels
= 1;
119 av_log (s
, AV_LOG_DEBUG
, "entered audio subheader\n");
121 while (inSubheader
) {
123 subbyte
= get_byte(pb
);
127 revision
= read_arbitary(pb
);
128 av_log (s
, AV_LOG_DEBUG
, "revision (element 0x80) set to 0x%08x\n", revision
);
131 ea
->num_channels
= read_arbitary(pb
);
132 av_log (s
, AV_LOG_DEBUG
, "num_channels (element 0x82) set to 0x%08x\n", ea
->num_channels
);
135 compression_type
= read_arbitary(pb
);
136 av_log (s
, AV_LOG_DEBUG
, "compression_type (element 0x83) set to 0x%08x\n", compression_type
);
139 ea
->sample_rate
= read_arbitary(pb
);
140 av_log (s
, AV_LOG_DEBUG
, "sample_rate (element 0x84) set to %i\n", ea
->sample_rate
);
143 ea
->num_samples
= read_arbitary(pb
);
144 av_log (s
, AV_LOG_DEBUG
, "num_samples (element 0x85) set to 0x%08x\n", ea
->num_samples
);
147 av_log (s
, AV_LOG_DEBUG
, "element 0x%02x set to 0x%08x\n", subbyte
, read_arbitary(pb
));
148 av_log (s
, AV_LOG_DEBUG
, "exited audio subheader\n");
152 revision2
= read_arbitary(pb
);
153 av_log (s
, AV_LOG_DEBUG
, "revision2 (element 0xA0) set to 0x%08x\n", revision2
);
156 av_log (s
, AV_LOG_DEBUG
, "end of header block reached (within audio subheader)\n");
161 av_log (s
, AV_LOG_DEBUG
, "element 0x%02x set to 0x%08x\n", subbyte
, read_arbitary(pb
));
167 av_log (s
, AV_LOG_DEBUG
, "end of header block reached\n");
171 av_log (s
, AV_LOG_DEBUG
, "header element 0x%02x set to 0x%08x\n", byte
, read_arbitary(pb
));
176 switch (compression_type
) {
177 case 0: ea
->audio_codec
= CODEC_ID_PCM_S16LE
; break;
178 case 7: ea
->audio_codec
= CODEC_ID_ADPCM_EA
; break;
181 case 1: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R1
; break;
182 case 2: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R2
; break;
183 case 3: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R3
; break;
186 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; revision=%i\n", revision
);
190 case 8: ea
->audio_codec
= CODEC_ID_PCM_S16LE_PLANAR
; break;
191 case 10: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R2
; break;
192 case 16: ea
->audio_codec
= CODEC_ID_MP3
; break;
195 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; revision2=%i\n", revision2
);
200 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; compression_type=%i\n", compression_type
);
204 if (ea
->sample_rate
== -1)
205 ea
->sample_rate
= revision
==3 ? 48000 : 22050;
211 * Process EACS sound header
212 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
214 static int process_audio_header_eacs(AVFormatContext
*s
)
216 EaDemuxContext
*ea
= s
->priv_data
;
217 ByteIOContext
*pb
= s
->pb
;
218 int compression_type
;
220 ea
->sample_rate
= ea
->big_endian
? get_be32(pb
) : get_le32(pb
);
221 ea
->bytes
= get_byte(pb
); /* 1=8-bit, 2=16-bit */
222 ea
->num_channels
= get_byte(pb
);
223 compression_type
= get_byte(pb
);
226 switch (compression_type
) {
229 case 1: ea
->audio_codec
= CODEC_ID_PCM_S8
; break;
230 case 2: ea
->audio_codec
= CODEC_ID_PCM_S16LE
; break;
233 case 1: ea
->audio_codec
= CODEC_ID_PCM_MULAW
; ea
->bytes
= 1; break;
234 case 2: ea
->audio_codec
= CODEC_ID_ADPCM_IMA_EA_EACS
; break;
236 av_log (s
, AV_LOG_ERROR
, "unsupported stream type; audio compression_type=%i\n", compression_type
);
243 * Process SEAD sound header
244 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
246 static int process_audio_header_sead(AVFormatContext
*s
)
248 EaDemuxContext
*ea
= s
->priv_data
;
249 ByteIOContext
*pb
= s
->pb
;
251 ea
->sample_rate
= get_le32(pb
);
252 ea
->bytes
= get_le32(pb
); /* 1=8-bit, 2=16-bit */
253 ea
->num_channels
= get_le32(pb
);
254 ea
->audio_codec
= CODEC_ID_ADPCM_IMA_EA_SEAD
;
259 static int process_video_header_mdec(AVFormatContext
*s
)
261 EaDemuxContext
*ea
= s
->priv_data
;
262 ByteIOContext
*pb
= s
->pb
;
264 ea
->width
= get_le16(pb
);
265 ea
->height
= get_le16(pb
);
266 ea
->time_base
= (AVRational
){1,15};
267 ea
->video_codec
= CODEC_ID_MDEC
;
271 static int process_video_header_vp6(AVFormatContext
*s
)
273 EaDemuxContext
*ea
= s
->priv_data
;
274 ByteIOContext
*pb
= s
->pb
;
277 ea
->time_base
.den
= get_le32(pb
);
278 ea
->time_base
.num
= get_le32(pb
);
279 ea
->video_codec
= CODEC_ID_VP6
;
285 * Process EA file header
286 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
288 static int process_ea_header(AVFormatContext
*s
) {
289 uint32_t blockid
, size
= 0;
290 EaDemuxContext
*ea
= s
->priv_data
;
291 ByteIOContext
*pb
= s
->pb
;
294 for (i
=0; i
<5 && (!ea
->audio_codec
|| !ea
->video_codec
); i
++) {
295 unsigned int startpos
= url_ftell(pb
);
298 blockid
= get_le32(pb
);
301 ea
->big_endian
= size
> 0x000FFFFF;
303 size
= bswap_32(size
);
307 if (get_le32(pb
) != EACS_TAG
) {
308 av_log (s
, AV_LOG_ERROR
, "unknown 1SNh headerid\n");
311 err
= process_audio_header_eacs(s
);
316 blockid
= get_le32(pb
);
317 if (blockid
== GSTR_TAG
) {
319 } else if ((blockid
& 0xFFFF)!=PT00_TAG
) {
320 av_log (s
, AV_LOG_ERROR
, "unknown SCHl headerid\n");
323 err
= process_audio_header_elements(s
);
327 err
= process_audio_header_sead(s
);
331 ea
->video_codec
= CODEC_ID_CMV
;
332 ea
->time_base
= (AVRational
){0,0};
336 ea
->video_codec
= CODEC_ID_TGV
;
337 ea
->time_base
= (AVRational
){0,0};
341 err
= process_video_header_mdec(s
);
345 ea
->video_codec
= CODEC_ID_MPEG2VIDEO
;
350 ea
->video_codec
= CODEC_ID_TGQ
;
354 ea
->video_codec
= CODEC_ID_TQI
;
358 ea
->video_codec
= CODEC_ID_MAD
;
362 err
= process_video_header_vp6(s
);
367 av_log(s
, AV_LOG_ERROR
, "error parsing header: %i\n", err
);
371 url_fseek(pb
, startpos
+ size
, SEEK_SET
);
374 url_fseek(pb
, 0, SEEK_SET
);
380 static int ea_probe(AVProbeData
*p
)
382 switch (AV_RL32(&p
->buf
[0])) {
392 return AVPROBE_SCORE_MAX
;
397 static int ea_read_header(AVFormatContext
*s
,
398 AVFormatParameters
*ap
)
400 EaDemuxContext
*ea
= s
->priv_data
;
403 if (!process_ea_header(s
))
406 if (ea
->video_codec
) {
407 /* initialize the video decoder stream */
408 st
= av_new_stream(s
, 0);
410 return AVERROR(ENOMEM
);
411 ea
->video_stream_index
= st
->index
;
412 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
413 st
->codec
->codec_id
= ea
->video_codec
;
414 st
->codec
->codec_tag
= 0; /* no fourcc */
415 st
->codec
->time_base
= ea
->time_base
;
416 st
->codec
->width
= ea
->width
;
417 st
->codec
->height
= ea
->height
;
420 if (ea
->audio_codec
) {
421 /* initialize the audio decoder stream */
422 st
= av_new_stream(s
, 0);
424 return AVERROR(ENOMEM
);
425 av_set_pts_info(st
, 33, 1, ea
->sample_rate
);
426 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
427 st
->codec
->codec_id
= ea
->audio_codec
;
428 st
->codec
->codec_tag
= 0; /* no tag */
429 st
->codec
->channels
= ea
->num_channels
;
430 st
->codec
->sample_rate
= ea
->sample_rate
;
431 st
->codec
->bits_per_coded_sample
= ea
->bytes
* 8;
432 st
->codec
->bit_rate
= st
->codec
->channels
* st
->codec
->sample_rate
*
433 st
->codec
->bits_per_coded_sample
/ 4;
434 st
->codec
->block_align
= st
->codec
->channels
*st
->codec
->bits_per_coded_sample
;
435 ea
->audio_stream_index
= st
->index
;
436 ea
->audio_frame_counter
= 0;
442 static int ea_read_packet(AVFormatContext
*s
,
445 EaDemuxContext
*ea
= s
->priv_data
;
446 ByteIOContext
*pb
= s
->pb
;
449 unsigned int chunk_type
, chunk_size
;
451 int av_uninit(num_samples
);
453 while (!packet_read
) {
454 chunk_type
= get_le32(pb
);
455 chunk_size
= (ea
->big_endian
? get_be32(pb
) : get_le32(pb
)) - 8;
457 switch (chunk_type
) {
460 /* header chunk also contains data; skip over the header portion*/
467 if (!ea
->audio_codec
) {
468 url_fskip(pb
, chunk_size
);
470 } else if (ea
->audio_codec
== CODEC_ID_PCM_S16LE_PLANAR
||
471 ea
->audio_codec
== CODEC_ID_MP3
) {
472 num_samples
= get_le32(pb
);
476 ret
= av_get_packet(pb
, pkt
, chunk_size
);
479 pkt
->stream_index
= ea
->audio_stream_index
;
481 pkt
->pts
*= ea
->audio_frame_counter
;
482 pkt
->pts
/= ea
->sample_rate
;
484 switch (ea
->audio_codec
) {
485 case CODEC_ID_ADPCM_EA
:
486 /* 2 samples/byte, 1 or 2 samples per frame depending
487 * on stereo; chunk also has 12-byte header */
488 ea
->audio_frame_counter
+= ((chunk_size
- 12) * 2) /
491 case CODEC_ID_PCM_S16LE_PLANAR
:
493 ea
->audio_frame_counter
+= num_samples
;
496 ea
->audio_frame_counter
+= chunk_size
/
497 (ea
->bytes
* ea
->num_channels
);
523 url_fseek(pb
, -8, SEEK_CUR
); // include chunk preamble
525 goto get_video_packet
;
528 url_fseek(pb
, 8, SEEK_CUR
); // skip ea dct header
530 goto get_video_packet
;
538 ret
= av_get_packet(pb
, pkt
, chunk_size
);
541 pkt
->stream_index
= ea
->video_stream_index
;
547 url_fseek(pb
, chunk_size
, SEEK_CUR
);
555 AVInputFormat ea_demuxer
= {
557 NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia Format"),
558 sizeof(EaDemuxContext
),