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 SHEN_TAG MKTAG('S', 'H', 'E', 'N') /* SxEN header */
35 #define SDEN_TAG MKTAG('S', 'D', 'E', 'N') /* SxEN data */
36 #define SEEN_TAG MKTAG('S', 'E', 'E', 'N') /* SxEN end */
37 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
38 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
39 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
40 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
41 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
42 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
43 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
44 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
45 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV i-frame */
46 #define fVGT_TAG MKTAG('f', 'V', 'G', 'T') /* TGV p-frame */
47 #define mTCD_TAG MKTAG('m', 'T', 'C', 'D') /* MDEC */
48 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
49 #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG2 */
50 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
51 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
52 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
53 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
54 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV i-frame */
56 typedef struct EaDemuxContext
{
59 enum CodecID video_codec
;
62 int video_stream_index
;
64 enum CodecID audio_codec
;
65 int audio_stream_index
;
66 int audio_frame_counter
;
76 static uint32_t read_arbitary(ByteIOContext
*pb
) {
84 for (i
= 0; i
< size
; i
++) {
94 * Process PT/GSTR sound header
95 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
97 static int process_audio_header_elements(AVFormatContext
*s
)
100 EaDemuxContext
*ea
= s
->priv_data
;
101 ByteIOContext
*pb
= s
->pb
;
102 int compression_type
= -1, revision
= -1, revision2
= -1;
105 ea
->sample_rate
= -1;
106 ea
->num_channels
= 1;
115 av_log (s
, AV_LOG_INFO
, "entered audio subheader\n");
117 while (inSubheader
) {
119 subbyte
= get_byte(pb
);
123 revision
= read_arbitary(pb
);
124 av_log (s
, AV_LOG_INFO
, "revision (element 0x80) set to 0x%08x\n", revision
);
127 ea
->num_channels
= read_arbitary(pb
);
128 av_log (s
, AV_LOG_INFO
, "num_channels (element 0x82) set to 0x%08x\n", ea
->num_channels
);
131 compression_type
= read_arbitary(pb
);
132 av_log (s
, AV_LOG_INFO
, "compression_type (element 0x83) set to 0x%08x\n", compression_type
);
135 ea
->sample_rate
= read_arbitary(pb
);
136 av_log (s
, AV_LOG_INFO
, "sample_rate (element 0x84) set to %i\n", ea
->sample_rate
);
139 ea
->num_samples
= read_arbitary(pb
);
140 av_log (s
, AV_LOG_INFO
, "num_samples (element 0x85) set to 0x%08x\n", ea
->num_samples
);
143 av_log (s
, AV_LOG_INFO
, "element 0x%02x set to 0x%08x\n", subbyte
, read_arbitary(pb
));
144 av_log (s
, AV_LOG_INFO
, "exited audio subheader\n");
148 revision2
= read_arbitary(pb
);
149 av_log (s
, AV_LOG_INFO
, "revision2 (element 0xA0) set to 0x%08x\n", revision2
);
152 av_log (s
, AV_LOG_INFO
, "end of header block reached (within audio subheader)\n");
157 av_log (s
, AV_LOG_INFO
, "element 0x%02x set to 0x%08x\n", subbyte
, read_arbitary(pb
));
163 av_log (s
, AV_LOG_INFO
, "end of header block reached\n");
167 av_log (s
, AV_LOG_INFO
, "header element 0x%02x set to 0x%08x\n", byte
, read_arbitary(pb
));
172 switch (compression_type
) {
173 case 0: ea
->audio_codec
= CODEC_ID_PCM_S16LE
; break;
174 case 7: ea
->audio_codec
= CODEC_ID_ADPCM_EA
; break;
177 case 1: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R1
; break;
178 case 2: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R2
; break;
179 case 3: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R3
; break;
182 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; revision=%i\n", revision
);
186 case 8: ea
->audio_codec
= CODEC_ID_PCM_S16LE_PLANAR
; break;
187 case 10: ea
->audio_codec
= CODEC_ID_ADPCM_EA_R2
; break;
188 case 16: ea
->audio_codec
= CODEC_ID_MP3
; break;
191 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; revision2=%i\n", revision2
);
196 av_log(s
, AV_LOG_ERROR
, "unsupported stream type; compression_type=%i\n", compression_type
);
200 if (ea
->sample_rate
== -1)
201 ea
->sample_rate
= revision
==3 ? 48000 : 22050;
207 * Process EACS sound header
208 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
210 static int process_audio_header_eacs(AVFormatContext
*s
)
212 EaDemuxContext
*ea
= s
->priv_data
;
213 ByteIOContext
*pb
= s
->pb
;
214 int compression_type
;
216 ea
->sample_rate
= ea
->big_endian
? get_be32(pb
) : get_le32(pb
);
217 ea
->bytes
= get_byte(pb
); /* 1=8-bit, 2=16-bit */
218 ea
->num_channels
= get_byte(pb
);
219 compression_type
= get_byte(pb
);
222 switch (compression_type
) {
225 case 1: ea
->audio_codec
= CODEC_ID_PCM_S8
; break;
226 case 2: ea
->audio_codec
= CODEC_ID_PCM_S16LE
; break;
229 case 1: ea
->audio_codec
= CODEC_ID_PCM_MULAW
; ea
->bytes
= 1; break;
230 case 2: ea
->audio_codec
= CODEC_ID_ADPCM_IMA_EA_EACS
; break;
232 av_log (s
, AV_LOG_ERROR
, "unsupported stream type; audio compression_type=%i\n", compression_type
);
239 * Process SEAD sound header
240 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
242 static int process_audio_header_sead(AVFormatContext
*s
)
244 EaDemuxContext
*ea
= s
->priv_data
;
245 ByteIOContext
*pb
= s
->pb
;
247 ea
->sample_rate
= get_le32(pb
);
248 ea
->bytes
= get_le32(pb
); /* 1=8-bit, 2=16-bit */
249 ea
->num_channels
= get_le32(pb
);
250 ea
->audio_codec
= CODEC_ID_ADPCM_IMA_EA_SEAD
;
255 static int process_video_header_mdec(AVFormatContext
*s
)
257 EaDemuxContext
*ea
= s
->priv_data
;
258 ByteIOContext
*pb
= s
->pb
;
260 ea
->width
= get_le16(pb
);
261 ea
->height
= get_le16(pb
);
262 ea
->time_base
= (AVRational
){1,15};
263 ea
->video_codec
= CODEC_ID_MDEC
;
267 static int process_video_header_vp6(AVFormatContext
*s
)
269 EaDemuxContext
*ea
= s
->priv_data
;
270 ByteIOContext
*pb
= s
->pb
;
273 ea
->time_base
.den
= get_le32(pb
);
274 ea
->time_base
.num
= get_le32(pb
);
275 ea
->video_codec
= CODEC_ID_VP6
;
281 * Process EA file header
282 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
284 static int process_ea_header(AVFormatContext
*s
) {
285 uint32_t blockid
, size
= 0;
286 EaDemuxContext
*ea
= s
->priv_data
;
287 ByteIOContext
*pb
= s
->pb
;
290 for (i
=0; i
<5 && (!ea
->audio_codec
|| !ea
->video_codec
); i
++) {
291 unsigned int startpos
= url_ftell(pb
);
294 blockid
= get_le32(pb
);
297 ea
->big_endian
= size
> 0x000FFFFF;
299 size
= bswap_32(size
);
303 if (get_le32(pb
) != EACS_TAG
) {
304 av_log (s
, AV_LOG_ERROR
, "unknown 1SNh headerid\n");
307 err
= process_audio_header_eacs(s
);
312 blockid
= get_le32(pb
);
313 if (blockid
== GSTR_TAG
) {
315 } else if ((blockid
& 0xFFFF)!=PT00_TAG
) {
316 av_log (s
, AV_LOG_ERROR
, "unknown SCHl headerid\n");
319 err
= process_audio_header_elements(s
);
323 err
= process_audio_header_sead(s
);
327 ea
->video_codec
= CODEC_ID_CMV
;
328 ea
->time_base
= (AVRational
){0,0};
332 ea
->video_codec
= CODEC_ID_TGV
;
333 ea
->time_base
= (AVRational
){0,0};
337 err
= process_video_header_mdec(s
);
341 ea
->video_codec
= CODEC_ID_MPEG2VIDEO
;
345 err
= process_video_header_vp6(s
);
350 av_log(s
, AV_LOG_ERROR
, "error parsing header: %i\n", err
);
354 url_fseek(pb
, startpos
+ size
, SEEK_SET
);
357 url_fseek(pb
, 0, SEEK_SET
);
363 static int ea_probe(AVProbeData
*p
)
365 switch (AV_RL32(&p
->buf
[0])) {
375 return AVPROBE_SCORE_MAX
;
380 static int ea_read_header(AVFormatContext
*s
,
381 AVFormatParameters
*ap
)
383 EaDemuxContext
*ea
= s
->priv_data
;
386 if (!process_ea_header(s
))
389 if (ea
->video_codec
) {
390 /* initialize the video decoder stream */
391 st
= av_new_stream(s
, 0);
393 return AVERROR(ENOMEM
);
394 ea
->video_stream_index
= st
->index
;
395 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
396 st
->codec
->codec_id
= ea
->video_codec
;
397 st
->codec
->codec_tag
= 0; /* no fourcc */
398 st
->codec
->time_base
= ea
->time_base
;
399 st
->codec
->width
= ea
->width
;
400 st
->codec
->height
= ea
->height
;
403 if (ea
->audio_codec
) {
404 /* initialize the audio decoder stream */
405 st
= av_new_stream(s
, 0);
407 return AVERROR(ENOMEM
);
408 av_set_pts_info(st
, 33, 1, ea
->sample_rate
);
409 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
410 st
->codec
->codec_id
= ea
->audio_codec
;
411 st
->codec
->codec_tag
= 0; /* no tag */
412 st
->codec
->channels
= ea
->num_channels
;
413 st
->codec
->sample_rate
= ea
->sample_rate
;
414 st
->codec
->bits_per_coded_sample
= ea
->bytes
* 8;
415 st
->codec
->bit_rate
= st
->codec
->channels
* st
->codec
->sample_rate
*
416 st
->codec
->bits_per_coded_sample
/ 4;
417 st
->codec
->block_align
= st
->codec
->channels
*st
->codec
->bits_per_coded_sample
;
418 ea
->audio_stream_index
= st
->index
;
419 ea
->audio_frame_counter
= 0;
425 static int ea_read_packet(AVFormatContext
*s
,
428 EaDemuxContext
*ea
= s
->priv_data
;
429 ByteIOContext
*pb
= s
->pb
;
432 unsigned int chunk_type
, chunk_size
;
436 while (!packet_read
) {
437 chunk_type
= get_le32(pb
);
438 chunk_size
= (ea
->big_endian
? get_be32(pb
) : get_le32(pb
)) - 8;
440 switch (chunk_type
) {
443 /* header chunk also contains data; skip over the header portion*/
450 if (!ea
->audio_codec
) {
451 url_fskip(pb
, chunk_size
);
453 } else if (ea
->audio_codec
== CODEC_ID_PCM_S16LE_PLANAR
||
454 ea
->audio_codec
== CODEC_ID_MP3
) {
455 num_samples
= get_le32(pb
);
459 ret
= av_get_packet(pb
, pkt
, chunk_size
);
460 if (ret
!= chunk_size
)
463 pkt
->stream_index
= ea
->audio_stream_index
;
465 pkt
->pts
*= ea
->audio_frame_counter
;
466 pkt
->pts
/= ea
->sample_rate
;
468 switch (ea
->audio_codec
) {
469 case CODEC_ID_ADPCM_EA
:
470 /* 2 samples/byte, 1 or 2 samples per frame depending
471 * on stereo; chunk also has 12-byte header */
472 ea
->audio_frame_counter
+= ((chunk_size
- 12) * 2) /
475 case CODEC_ID_PCM_S16LE_PLANAR
:
477 ea
->audio_frame_counter
+= num_samples
;
480 ea
->audio_frame_counter
+= chunk_size
/
481 (ea
->bytes
* ea
->num_channels
);
503 url_fseek(pb
, -8, SEEK_CUR
); // include chunk preamble
505 goto get_video_packet
;
508 url_fseek(pb
, 8, SEEK_CUR
); // skip ea dct header
510 goto get_video_packet
;
517 ret
= av_get_packet(pb
, pkt
, chunk_size
);
518 if (ret
!= chunk_size
)
521 pkt
->stream_index
= ea
->video_stream_index
;
528 url_fseek(pb
, chunk_size
, SEEK_CUR
);
536 AVInputFormat ea_demuxer
= {
538 NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia Format"),
539 sizeof(EaDemuxContext
),