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 electronicarts.c
24 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
25 * by Robin Kay (komadori at gekkou.co.uk)
30 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
31 #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
32 #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
33 #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
34 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
35 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
36 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
37 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
38 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
39 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
40 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
41 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
42 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV i-frame */
43 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
44 #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG2 */
45 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
46 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
47 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
48 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
49 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV i-frame */
51 typedef struct EaDemuxContext
{
54 enum CodecID video_codec
;
56 int video_stream_index
;
58 enum CodecID audio_codec
;
59 int audio_stream_index
;
60 int audio_frame_counter
;
70 static uint32_t read_arbitary(ByteIOContext
*pb
) {
78 for (i
= 0; i
< size
; i
++) {
88 * Process PT/GSTR sound header
89 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
91 static int process_audio_header_elements(AVFormatContext
*s
)
94 EaDemuxContext
*ea
= s
->priv_data
;
95 ByteIOContext
*pb
= s
->pb
;
96 int compression_type
= -1, revision
= -1, revision2
= -1;
100 ea
->num_channels
= 1;
109 av_log (s
, AV_LOG_INFO
, "entered audio subheader\n");
111 while (inSubheader
) {
113 subbyte
= get_byte(pb
);
117 revision
= read_arbitary(pb
);
118 av_log (s
, AV_LOG_INFO
, "revision (element 0x80) set to 0x%08x\n", revision
);
121 ea
->num_channels
= read_arbitary(pb
);
122 av_log (s
, AV_LOG_INFO
, "num_channels (element 0x82) set to 0x%08x\n", ea
->num_channels
);
125 compression_type
= read_arbitary(pb
);
126 av_log (s
, AV_LOG_INFO
, "compression_type (element 0x83) set to 0x%08x\n", compression_type
);
129 ea
->sample_rate
= read_arbitary(pb
);
130 av_log (s
, AV_LOG_INFO
, "sample_rate (element 0x84) set to %i\n", ea
->sample_rate
);
133 ea
->num_samples
= read_arbitary(pb
);
134 av_log (s
, AV_LOG_INFO
, "num_samples (element 0x85) set to 0x%08x\n", ea
->num_samples
);
137 av_log (s
, AV_LOG_INFO
, "element 0x%02x set to 0x%08x\n", subbyte
, read_arbitary(pb
));
138 av_log (s
, AV_LOG_INFO
, "exited audio subheader\n");
142 revision2
= read_arbitary(pb
);
143 av_log (s
, AV_LOG_INFO
, "revision2 (element 0xA0) set to 0x%08x\n", revision2
);
146 av_log (s
, AV_LOG_INFO
, "end of header block reached (within audio subheader)\n");
151 av_log (s
, AV_LOG_INFO
, "element 0x%02x set to 0x%08x\n", subbyte
, read_arbitary(pb
));
157 av_log (s
, AV_LOG_INFO
, "end of header block reached\n");
161 av_log (s
, AV_LOG_INFO
, "header element 0x%02x set to 0x%08x\n", byte
, read_arbitary(pb
));
166 switch (compression_type
) {
167 case 0: ea
->audio_codec
= CODEC_ID_PCM_S16LE
; break;
168 case 7: ea
->audio_codec
= CODEC_ID_ADPCM_EA
; break;
171 case 1: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R1
; break;
172 case 2: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R2
; break;
173 case 3: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R3
; break;
176 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; revision=%i\n", revision
);
180 case 8: ea
->audio_codec
= CODEC_ID_PCM_S16LE_PLANAR
; break;
182 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; revision2=%i\n", revision2
);
187 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; compression_type=%i\n", compression_type
);
191 if (ea
->sample_rate
== -1)
192 ea
->sample_rate
= revision
==3 ? 48000 : 22050;
198 * Process EACS sound header
199 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
201 static int process_audio_header_eacs(AVFormatContext
*s
)
203 EaDemuxContext
*ea
= s
->priv_data
;
204 ByteIOContext
*pb
= s
->pb
;
205 int compression_type
;
207 ea
->sample_rate
= ea
->big_endian
? get_be32(pb
) : get_le32(pb
);
208 ea
->bytes
= get_byte(pb
); /* 1=8-bit, 2=16-bit */
209 ea
->num_channels
= get_byte(pb
);
210 compression_type
= get_byte(pb
);
213 switch (compression_type
) {
216 case 1: ea
->audio_codec
= CODEC_ID_PCM_S8
; break;
217 case 2: ea
->audio_codec
= CODEC_ID_PCM_S16LE
; break;
220 case 1: ea
->audio_codec
= CODEC_ID_PCM_MULAW
; ea
->bytes
= 1; break;
221 case 2: ea
->audio_codec
= CODEC_ID_ADPCM_IMA_EA_EACS
; break;
223 av_log (s
, AV_LOG_ERROR
, "unsupported stream type; audio compression_type=%i\n", compression_type
);
230 * Process SEAD sound header
231 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
233 static int process_audio_header_sead(AVFormatContext
*s
)
235 EaDemuxContext
*ea
= s
->priv_data
;
236 ByteIOContext
*pb
= s
->pb
;
238 ea
->sample_rate
= get_le32(pb
);
239 ea
->bytes
= get_le32(pb
); /* 1=8-bit, 2=16-bit */
240 ea
->num_channels
= get_le32(pb
);
241 ea
->audio_codec
= CODEC_ID_ADPCM_IMA_EA_SEAD
;
246 static int process_video_header_vp6(AVFormatContext
*s
)
248 EaDemuxContext
*ea
= s
->priv_data
;
249 ByteIOContext
*pb
= s
->pb
;
252 ea
->time_base
.den
= get_le32(pb
);
253 ea
->time_base
.num
= get_le32(pb
);
254 ea
->video_codec
= CODEC_ID_VP6
;
260 * Process EA file header
261 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
263 static int process_ea_header(AVFormatContext
*s
) {
264 uint32_t blockid
, size
= 0;
265 EaDemuxContext
*ea
= s
->priv_data
;
266 ByteIOContext
*pb
= s
->pb
;
269 for (i
=0; i
<5 && (!ea
->audio_codec
|| !ea
->video_codec
); i
++) {
270 unsigned int startpos
= url_ftell(pb
);
273 blockid
= get_le32(pb
);
276 ea
->big_endian
= size
> 0x000FFFFF;
278 size
= bswap_32(size
);
282 if (get_le32(pb
) != EACS_TAG
) {
283 av_log (s
, AV_LOG_ERROR
, "unknown 1SNh headerid\n");
286 err
= process_audio_header_eacs(s
);
290 blockid
= get_le32(pb
);
291 if (blockid
== GSTR_TAG
) {
293 } else if (blockid
!= PT00_TAG
) {
294 av_log (s
, AV_LOG_ERROR
, "unknown SCHl headerid\n");
297 err
= process_audio_header_elements(s
);
301 err
= process_audio_header_sead(s
);
305 ea
->video_codec
= CODEC_ID_CMV
;
306 ea
->time_base
= (AVRational
){0,0};
310 err
= process_video_header_vp6(s
);
315 av_log(s
, AV_LOG_ERROR
, "error parsing header: %i\n", err
);
319 url_fseek(pb
, startpos
+ size
, SEEK_SET
);
322 url_fseek(pb
, 0, SEEK_SET
);
328 static int ea_probe(AVProbeData
*p
)
330 switch (AV_RL32(&p
->buf
[0])) {
339 return AVPROBE_SCORE_MAX
;
344 static int ea_read_header(AVFormatContext
*s
,
345 AVFormatParameters
*ap
)
347 EaDemuxContext
*ea
= s
->priv_data
;
350 if (!process_ea_header(s
))
353 if (ea
->video_codec
) {
354 /* initialize the video decoder stream */
355 st
= av_new_stream(s
, 0);
357 return AVERROR(ENOMEM
);
358 ea
->video_stream_index
= st
->index
;
359 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
360 st
->codec
->codec_id
= ea
->video_codec
;
361 st
->codec
->codec_tag
= 0; /* no fourcc */
362 st
->codec
->time_base
= ea
->time_base
;
365 if (ea
->audio_codec
) {
366 /* initialize the audio decoder stream */
367 st
= av_new_stream(s
, 0);
369 return AVERROR(ENOMEM
);
370 av_set_pts_info(st
, 33, 1, ea
->sample_rate
);
371 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
372 st
->codec
->codec_id
= ea
->audio_codec
;
373 st
->codec
->codec_tag
= 0; /* no tag */
374 st
->codec
->channels
= ea
->num_channels
;
375 st
->codec
->sample_rate
= ea
->sample_rate
;
376 st
->codec
->bits_per_sample
= ea
->bytes
* 8;
377 st
->codec
->bit_rate
= st
->codec
->channels
* st
->codec
->sample_rate
*
378 st
->codec
->bits_per_sample
/ 4;
379 st
->codec
->block_align
= st
->codec
->channels
*st
->codec
->bits_per_sample
;
380 ea
->audio_stream_index
= st
->index
;
381 ea
->audio_frame_counter
= 0;
387 static int ea_read_packet(AVFormatContext
*s
,
390 EaDemuxContext
*ea
= s
->priv_data
;
391 ByteIOContext
*pb
= s
->pb
;
394 unsigned int chunk_type
, chunk_size
;
397 while (!packet_read
) {
398 chunk_type
= get_le32(pb
);
399 chunk_size
= (ea
->big_endian
? get_be32(pb
) : get_le32(pb
)) - 8;
401 switch (chunk_type
) {
404 /* header chunk also contains data; skip over the header portion*/
410 if (!ea
->audio_codec
) {
411 url_fskip(pb
, chunk_size
);
413 } else if (ea
->audio_codec
== CODEC_ID_PCM_S16LE_PLANAR
) {
414 url_fskip(pb
, 12); /* planar header */
417 ret
= av_get_packet(pb
, pkt
, chunk_size
);
418 if (ret
!= chunk_size
)
421 pkt
->stream_index
= ea
->audio_stream_index
;
423 pkt
->pts
*= ea
->audio_frame_counter
;
424 pkt
->pts
/= ea
->sample_rate
;
426 switch (ea
->audio_codec
) {
427 case CODEC_ID_ADPCM_EA
:
428 /* 2 samples/byte, 1 or 2 samples per frame depending
429 * on stereo; chunk also has 12-byte header */
430 ea
->audio_frame_counter
+= ((chunk_size
- 12) * 2) /
434 ea
->audio_frame_counter
+= chunk_size
/
435 (ea
->bytes
* ea
->num_channels
);
454 url_fseek(pb
, -8, SEEK_CUR
); // include chunk preamble
456 goto get_video_packet
;
462 ret
= av_get_packet(pb
, pkt
, chunk_size
);
463 if (ret
!= chunk_size
)
466 pkt
->stream_index
= ea
->video_stream_index
;
473 url_fseek(pb
, chunk_size
, SEEK_CUR
);
481 AVInputFormat ea_demuxer
= {
483 NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia Format"),
484 sizeof(EaDemuxContext
),