1 /* Electronic Arts Multimedia File Demuxer
2 * Copyright (c) 2004 The ffmpeg Project
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * @file electronicarts.c
23 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
24 * by Robin Kay (komadori at gekkou.co.uk)
29 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
30 #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
31 #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
32 #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
33 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
34 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
35 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
36 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
37 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
38 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
39 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
40 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
41 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV i-frame */
42 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
43 #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG2 */
44 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
45 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
46 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
47 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
49 typedef struct EaDemuxContext
{
52 enum CodecID video_codec
;
54 int video_stream_index
;
56 enum CodecID audio_codec
;
57 int audio_stream_index
;
58 int audio_frame_counter
;
68 static uint32_t read_arbitary(ByteIOContext
*pb
) {
76 for (i
= 0; i
< size
; i
++) {
86 * Process PT/GSTR sound header
87 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
89 static int process_audio_header_elements(AVFormatContext
*s
)
92 EaDemuxContext
*ea
= s
->priv_data
;
93 ByteIOContext
*pb
= s
->pb
;
94 int compression_type
= -1, revision
= -1, revision2
= -1;
107 av_log (s
, AV_LOG_INFO
, "entered audio subheader\n");
109 while (inSubheader
) {
111 subbyte
= get_byte(pb
);
115 revision
= read_arbitary(pb
);
116 av_log (s
, AV_LOG_INFO
, "revision (element 0x80) set to 0x%08x\n", revision
);
119 ea
->num_channels
= read_arbitary(pb
);
120 av_log (s
, AV_LOG_INFO
, "num_channels (element 0x82) set to 0x%08x\n", ea
->num_channels
);
123 compression_type
= read_arbitary(pb
);
124 av_log (s
, AV_LOG_INFO
, "compression_type (element 0x83) set to 0x%08x\n", compression_type
);
127 ea
->sample_rate
= read_arbitary(pb
);
128 av_log (s
, AV_LOG_INFO
, "sample_rate (element 0x84) set to %i\n", ea
->sample_rate
);
131 ea
->num_samples
= read_arbitary(pb
);
132 av_log (s
, AV_LOG_INFO
, "num_samples (element 0x85) set to 0x%08x\n", ea
->num_samples
);
135 av_log (s
, AV_LOG_INFO
, "element 0x%02x set to 0x%08x\n", subbyte
, read_arbitary(pb
));
136 av_log (s
, AV_LOG_INFO
, "exited audio subheader\n");
140 revision2
= read_arbitary(pb
);
141 av_log (s
, AV_LOG_INFO
, "revision2 (element 0xA0) set to 0x%08x\n", revision2
);
144 av_log (s
, AV_LOG_INFO
, "end of header block reached (within audio subheader)\n");
149 av_log (s
, AV_LOG_INFO
, "element 0x%02x set to 0x%08x\n", subbyte
, read_arbitary(pb
));
155 av_log (s
, AV_LOG_INFO
, "end of header block reached\n");
159 av_log (s
, AV_LOG_INFO
, "header element 0x%02x set to 0x%08x\n", byte
, read_arbitary(pb
));
164 switch (compression_type
) {
165 case 0: ea
->audio_codec
= CODEC_ID_PCM_S16LE
; break;
166 case 7: ea
->audio_codec
= CODEC_ID_ADPCM_EA
; break;
169 case 1: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R1
; break;
170 case 2: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R2
; break;
171 case 3: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R3
; break;
174 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; revision=%i\n", revision
);
178 case 8: ea
->audio_codec
= CODEC_ID_PCM_S16LE_PLANAR
; break;
180 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; revision2=%i\n", revision2
);
185 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; compression_type=%i\n", compression_type
);
189 if (ea
->sample_rate
== -1)
190 ea
->sample_rate
= revision
==3 ? 48000 : 22050;
196 * Process EACS sound header
197 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
199 static int process_audio_header_eacs(AVFormatContext
*s
)
201 EaDemuxContext
*ea
= s
->priv_data
;
202 ByteIOContext
*pb
= s
->pb
;
203 int compression_type
;
205 ea
->sample_rate
= ea
->big_endian
? get_be32(pb
) : get_le32(pb
);
206 ea
->bytes
= get_byte(pb
); /* 1=8-bit, 2=16-bit */
207 ea
->num_channels
= get_byte(pb
);
208 compression_type
= get_byte(pb
);
211 switch (compression_type
) {
214 case 1: ea
->audio_codec
= CODEC_ID_PCM_S8
; break;
215 case 2: ea
->audio_codec
= CODEC_ID_PCM_S16LE
; break;
218 case 1: ea
->audio_codec
= CODEC_ID_PCM_MULAW
; ea
->bytes
= 1; break;
219 case 2: ea
->audio_codec
= CODEC_ID_ADPCM_IMA_EA_EACS
; break;
221 av_log (s
, AV_LOG_ERROR
, "unsupported stream type; audio compression_type=%i\n", compression_type
);
228 * Process SEAD sound header
229 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
231 static int process_audio_header_sead(AVFormatContext
*s
)
233 EaDemuxContext
*ea
= s
->priv_data
;
234 ByteIOContext
*pb
= s
->pb
;
236 ea
->sample_rate
= get_le32(pb
);
237 ea
->bytes
= get_le32(pb
); /* 1=8-bit, 2=16-bit */
238 ea
->num_channels
= get_le32(pb
);
239 ea
->audio_codec
= CODEC_ID_ADPCM_IMA_EA_SEAD
;
244 static int process_video_header_vp6(AVFormatContext
*s
)
246 EaDemuxContext
*ea
= s
->priv_data
;
247 ByteIOContext
*pb
= s
->pb
;
250 ea
->time_base
.den
= get_le32(pb
);
251 ea
->time_base
.num
= get_le32(pb
);
252 ea
->video_codec
= CODEC_ID_VP6
;
258 * Process EA file header
259 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
261 static int process_ea_header(AVFormatContext
*s
) {
262 uint32_t blockid
, size
= 0;
263 EaDemuxContext
*ea
= s
->priv_data
;
264 ByteIOContext
*pb
= s
->pb
;
267 for (i
=0; i
<5 && (!ea
->audio_codec
|| !ea
->video_codec
); i
++) {
268 unsigned int startpos
= url_ftell(pb
);
271 blockid
= get_le32(pb
);
274 ea
->big_endian
= size
> 0x000FFFFF;
276 size
= bswap_32(size
);
280 if (get_le32(pb
) != EACS_TAG
) {
281 av_log (s
, AV_LOG_ERROR
, "unknown 1SNh headerid\n");
284 err
= process_audio_header_eacs(s
);
288 blockid
= get_le32(pb
);
289 if (blockid
== GSTR_TAG
) {
291 } else if (blockid
!= PT00_TAG
) {
292 av_log (s
, AV_LOG_ERROR
, "unknown SCHl headerid\n");
295 err
= process_audio_header_elements(s
);
299 err
= process_audio_header_sead(s
);
303 err
= process_video_header_vp6(s
);
308 av_log(s
, AV_LOG_ERROR
, "error parsing header: %i\n", err
);
312 url_fseek(pb
, startpos
+ size
, SEEK_SET
);
315 url_fseek(pb
, 0, SEEK_SET
);
321 static int ea_probe(AVProbeData
*p
)
323 switch (AV_RL32(&p
->buf
[0])) {
332 return AVPROBE_SCORE_MAX
;
337 static int ea_read_header(AVFormatContext
*s
,
338 AVFormatParameters
*ap
)
340 EaDemuxContext
*ea
= s
->priv_data
;
343 if (!process_ea_header(s
))
346 if (ea
->video_codec
) {
347 /* initialize the video decoder stream */
348 st
= av_new_stream(s
, 0);
350 return AVERROR(ENOMEM
);
351 ea
->video_stream_index
= st
->index
;
352 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
353 st
->codec
->codec_id
= ea
->video_codec
;
354 st
->codec
->codec_tag
= 0; /* no fourcc */
355 st
->codec
->time_base
= ea
->time_base
;
358 if (ea
->audio_codec
) {
359 /* initialize the audio decoder stream */
360 st
= av_new_stream(s
, 0);
362 return AVERROR(ENOMEM
);
363 av_set_pts_info(st
, 33, 1, ea
->sample_rate
);
364 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
365 st
->codec
->codec_id
= ea
->audio_codec
;
366 st
->codec
->codec_tag
= 0; /* no tag */
367 st
->codec
->channels
= ea
->num_channels
;
368 st
->codec
->sample_rate
= ea
->sample_rate
;
369 st
->codec
->bits_per_sample
= ea
->bytes
* 8;
370 st
->codec
->bit_rate
= st
->codec
->channels
* st
->codec
->sample_rate
*
371 st
->codec
->bits_per_sample
/ 4;
372 st
->codec
->block_align
= st
->codec
->channels
*st
->codec
->bits_per_sample
;
373 ea
->audio_stream_index
= st
->index
;
374 ea
->audio_frame_counter
= 0;
380 static int ea_read_packet(AVFormatContext
*s
,
383 EaDemuxContext
*ea
= s
->priv_data
;
384 ByteIOContext
*pb
= s
->pb
;
387 unsigned int chunk_type
, chunk_size
;
390 while (!packet_read
) {
391 chunk_type
= get_le32(pb
);
392 chunk_size
= (ea
->big_endian
? get_be32(pb
) : get_le32(pb
)) - 8;
394 switch (chunk_type
) {
397 /* header chunk also contains data; skip over the header portion*/
403 if (!ea
->audio_codec
) {
404 url_fskip(pb
, chunk_size
);
406 } else if (ea
->audio_codec
== CODEC_ID_PCM_S16LE_PLANAR
) {
407 url_fskip(pb
, 12); /* planar header */
410 ret
= av_get_packet(pb
, pkt
, chunk_size
);
411 if (ret
!= chunk_size
)
414 pkt
->stream_index
= ea
->audio_stream_index
;
416 pkt
->pts
*= ea
->audio_frame_counter
;
417 pkt
->pts
/= ea
->sample_rate
;
419 switch (ea
->audio_codec
) {
420 case CODEC_ID_ADPCM_EA
:
421 /* 2 samples/byte, 1 or 2 samples per frame depending
422 * on stereo; chunk also has 12-byte header */
423 ea
->audio_frame_counter
+= ((chunk_size
- 12) * 2) /
427 ea
->audio_frame_counter
+= chunk_size
/
428 (ea
->bytes
* ea
->num_channels
);
447 ret
= av_get_packet(pb
, pkt
, chunk_size
);
448 if (ret
!= chunk_size
)
451 pkt
->stream_index
= ea
->video_stream_index
;
458 url_fseek(pb
, chunk_size
, SEEK_CUR
);
466 AVInputFormat ea_demuxer
= {
468 "Electronic Arts Multimedia Format",
469 sizeof(EaDemuxContext
),