Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
[libav.git] / libavformat / electronicarts.c
1 /* Electronic Arts Multimedia File Demuxer
2 * Copyright (c) 2004 The ffmpeg Project
3 * Copyright (c) 2006-2008 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file electronicarts.c
24 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
25 * by Robin Kay (komadori at gekkou.co.uk)
26 */
27
28 #include "libavutil/intreadwrite.h"
29 #include "avformat.h"
30
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 MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG2 */
51 #define TGQs_TAG MKTAG('T', 'G', 'Q', 's') /* TGQ i-frame (appears in .TGQ files) */
52 #define pQGT_TAG MKTAG('p', 'Q', 'G', 'T') /* TGQ i-frame (appears in .UV files) */
53 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
54 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
55 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
56 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
57 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV i-frame */
58
59 typedef struct EaDemuxContext {
60 int big_endian;
61
62 enum CodecID video_codec;
63 AVRational time_base;
64 int width, height;
65 int video_stream_index;
66
67 enum CodecID audio_codec;
68 int audio_stream_index;
69 int audio_frame_counter;
70
71 int64_t audio_pts;
72
73 int bytes;
74 int sample_rate;
75 int num_channels;
76 int num_samples;
77 } EaDemuxContext;
78
79 static uint32_t read_arbitary(ByteIOContext *pb) {
80 uint8_t size, byte;
81 int i;
82 uint32_t word;
83
84 size = get_byte(pb);
85
86 word = 0;
87 for (i = 0; i < size; i++) {
88 byte = get_byte(pb);
89 word <<= 8;
90 word |= byte;
91 }
92
93 return word;
94 }
95
96 /*
97 * Process PT/GSTR sound header
98 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
99 */
100 static int process_audio_header_elements(AVFormatContext *s)
101 {
102 int inHeader = 1;
103 EaDemuxContext *ea = s->priv_data;
104 ByteIOContext *pb = s->pb;
105 int compression_type = -1, revision = -1, revision2 = -1;
106
107 ea->bytes = 2;
108 ea->sample_rate = -1;
109 ea->num_channels = 1;
110
111 while (inHeader) {
112 int inSubheader;
113 uint8_t byte;
114 byte = get_byte(pb);
115
116 switch (byte) {
117 case 0xFD:
118 av_log (s, AV_LOG_INFO, "entered audio subheader\n");
119 inSubheader = 1;
120 while (inSubheader) {
121 uint8_t subbyte;
122 subbyte = get_byte(pb);
123
124 switch (subbyte) {
125 case 0x80:
126 revision = read_arbitary(pb);
127 av_log (s, AV_LOG_INFO, "revision (element 0x80) set to 0x%08x\n", revision);
128 break;
129 case 0x82:
130 ea->num_channels = read_arbitary(pb);
131 av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
132 break;
133 case 0x83:
134 compression_type = read_arbitary(pb);
135 av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
136 break;
137 case 0x84:
138 ea->sample_rate = read_arbitary(pb);
139 av_log (s, AV_LOG_INFO, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
140 break;
141 case 0x85:
142 ea->num_samples = read_arbitary(pb);
143 av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
144 break;
145 case 0x8A:
146 av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
147 av_log (s, AV_LOG_INFO, "exited audio subheader\n");
148 inSubheader = 0;
149 break;
150 case 0xA0:
151 revision2 = read_arbitary(pb);
152 av_log (s, AV_LOG_INFO, "revision2 (element 0xA0) set to 0x%08x\n", revision2);
153 break;
154 case 0xFF:
155 av_log (s, AV_LOG_INFO, "end of header block reached (within audio subheader)\n");
156 inSubheader = 0;
157 inHeader = 0;
158 break;
159 default:
160 av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
161 break;
162 }
163 }
164 break;
165 case 0xFF:
166 av_log (s, AV_LOG_INFO, "end of header block reached\n");
167 inHeader = 0;
168 break;
169 default:
170 av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
171 break;
172 }
173 }
174
175 switch (compression_type) {
176 case 0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
177 case 7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
178 case -1:
179 switch (revision) {
180 case 1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
181 case 2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
182 case 3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
183 case -1: break;
184 default:
185 av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
186 return 0;
187 }
188 switch (revision2) {
189 case 8: ea->audio_codec = CODEC_ID_PCM_S16LE_PLANAR; break;
190 case 10: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
191 case 16: ea->audio_codec = CODEC_ID_MP3; break;
192 case -1: break;
193 default:
194 av_log(s, AV_LOG_ERROR, "unsupported stream type; revision2=%i\n", revision2);
195 return 0;
196 }
197 break;
198 default:
199 av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
200 return 0;
201 }
202
203 if (ea->sample_rate == -1)
204 ea->sample_rate = revision==3 ? 48000 : 22050;
205
206 return 1;
207 }
208
209 /*
210 * Process EACS sound header
211 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
212 */
213 static int process_audio_header_eacs(AVFormatContext *s)
214 {
215 EaDemuxContext *ea = s->priv_data;
216 ByteIOContext *pb = s->pb;
217 int compression_type;
218
219 ea->sample_rate = ea->big_endian ? get_be32(pb) : get_le32(pb);
220 ea->bytes = get_byte(pb); /* 1=8-bit, 2=16-bit */
221 ea->num_channels = get_byte(pb);
222 compression_type = get_byte(pb);
223 url_fskip(pb, 13);
224
225 switch (compression_type) {
226 case 0:
227 switch (ea->bytes) {
228 case 1: ea->audio_codec = CODEC_ID_PCM_S8; break;
229 case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
230 }
231 break;
232 case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
233 case 2: ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_EACS; break;
234 default:
235 av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
236 }
237
238 return 1;
239 }
240
241 /*
242 * Process SEAD sound header
243 * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
244 */
245 static int process_audio_header_sead(AVFormatContext *s)
246 {
247 EaDemuxContext *ea = s->priv_data;
248 ByteIOContext *pb = s->pb;
249
250 ea->sample_rate = get_le32(pb);
251 ea->bytes = get_le32(pb); /* 1=8-bit, 2=16-bit */
252 ea->num_channels = get_le32(pb);
253 ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_SEAD;
254
255 return 1;
256 }
257
258 static int process_video_header_mdec(AVFormatContext *s)
259 {
260 EaDemuxContext *ea = s->priv_data;
261 ByteIOContext *pb = s->pb;
262 url_fskip(pb, 4);
263 ea->width = get_le16(pb);
264 ea->height = get_le16(pb);
265 ea->time_base = (AVRational){1,15};
266 ea->video_codec = CODEC_ID_MDEC;
267 return 1;
268 }
269
270 static int process_video_header_vp6(AVFormatContext *s)
271 {
272 EaDemuxContext *ea = s->priv_data;
273 ByteIOContext *pb = s->pb;
274
275 url_fskip(pb, 16);
276 ea->time_base.den = get_le32(pb);
277 ea->time_base.num = get_le32(pb);
278 ea->video_codec = CODEC_ID_VP6;
279
280 return 1;
281 }
282
283 /*
284 * Process EA file header
285 * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
286 */
287 static int process_ea_header(AVFormatContext *s) {
288 uint32_t blockid, size = 0;
289 EaDemuxContext *ea = s->priv_data;
290 ByteIOContext *pb = s->pb;
291 int i;
292
293 for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
294 unsigned int startpos = url_ftell(pb);
295 int err = 0;
296
297 blockid = get_le32(pb);
298 size = get_le32(pb);
299 if (i == 0)
300 ea->big_endian = size > 0x000FFFFF;
301 if (ea->big_endian)
302 size = bswap_32(size);
303
304 switch (blockid) {
305 case ISNh_TAG:
306 if (get_le32(pb) != EACS_TAG) {
307 av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
308 return 0;
309 }
310 err = process_audio_header_eacs(s);
311 break;
312
313 case SCHl_TAG :
314 case SHEN_TAG :
315 blockid = get_le32(pb);
316 if (blockid == GSTR_TAG) {
317 url_fskip(pb, 4);
318 } else if ((blockid & 0xFFFF)!=PT00_TAG) {
319 av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
320 return 0;
321 }
322 err = process_audio_header_elements(s);
323 break;
324
325 case SEAD_TAG:
326 err = process_audio_header_sead(s);
327 break;
328
329 case MVIh_TAG :
330 ea->video_codec = CODEC_ID_CMV;
331 ea->time_base = (AVRational){0,0};
332 break;
333
334 case kVGT_TAG:
335 ea->video_codec = CODEC_ID_TGV;
336 ea->time_base = (AVRational){0,0};
337 break;
338
339 case mTCD_TAG :
340 err = process_video_header_mdec(s);
341 break;
342
343 case MPCh_TAG:
344 ea->video_codec = CODEC_ID_MPEG2VIDEO;
345 break;
346
347 case pQGT_TAG:
348 case TGQs_TAG:
349 ea->video_codec = CODEC_ID_TGQ;
350 break;
351
352 case MVhd_TAG :
353 err = process_video_header_vp6(s);
354 break;
355 }
356
357 if (err < 0) {
358 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
359 return err;
360 }
361
362 url_fseek(pb, startpos + size, SEEK_SET);
363 }
364
365 url_fseek(pb, 0, SEEK_SET);
366
367 return 1;
368 }
369
370
371 static int ea_probe(AVProbeData *p)
372 {
373 switch (AV_RL32(&p->buf[0])) {
374 case ISNh_TAG:
375 case SCHl_TAG:
376 case SEAD_TAG:
377 case SHEN_TAG:
378 case kVGT_TAG:
379 case MADk_TAG:
380 case MPCh_TAG:
381 case MVhd_TAG:
382 case MVIh_TAG:
383 return AVPROBE_SCORE_MAX;
384 }
385 return 0;
386 }
387
388 static int ea_read_header(AVFormatContext *s,
389 AVFormatParameters *ap)
390 {
391 EaDemuxContext *ea = s->priv_data;
392 AVStream *st;
393
394 if (!process_ea_header(s))
395 return AVERROR(EIO);
396
397 if (ea->video_codec) {
398 /* initialize the video decoder stream */
399 st = av_new_stream(s, 0);
400 if (!st)
401 return AVERROR(ENOMEM);
402 ea->video_stream_index = st->index;
403 st->codec->codec_type = CODEC_TYPE_VIDEO;
404 st->codec->codec_id = ea->video_codec;
405 st->codec->codec_tag = 0; /* no fourcc */
406 st->codec->time_base = ea->time_base;
407 st->codec->width = ea->width;
408 st->codec->height = ea->height;
409 }
410
411 if (ea->audio_codec) {
412 /* initialize the audio decoder stream */
413 st = av_new_stream(s, 0);
414 if (!st)
415 return AVERROR(ENOMEM);
416 av_set_pts_info(st, 33, 1, ea->sample_rate);
417 st->codec->codec_type = CODEC_TYPE_AUDIO;
418 st->codec->codec_id = ea->audio_codec;
419 st->codec->codec_tag = 0; /* no tag */
420 st->codec->channels = ea->num_channels;
421 st->codec->sample_rate = ea->sample_rate;
422 st->codec->bits_per_coded_sample = ea->bytes * 8;
423 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
424 st->codec->bits_per_coded_sample / 4;
425 st->codec->block_align = st->codec->channels*st->codec->bits_per_coded_sample;
426 ea->audio_stream_index = st->index;
427 ea->audio_frame_counter = 0;
428 }
429
430 return 1;
431 }
432
433 static int ea_read_packet(AVFormatContext *s,
434 AVPacket *pkt)
435 {
436 EaDemuxContext *ea = s->priv_data;
437 ByteIOContext *pb = s->pb;
438 int ret = 0;
439 int packet_read = 0;
440 unsigned int chunk_type, chunk_size;
441 int key = 0;
442 int num_samples;
443
444 while (!packet_read) {
445 chunk_type = get_le32(pb);
446 chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
447
448 switch (chunk_type) {
449 /* audio data */
450 case ISNh_TAG:
451 /* header chunk also contains data; skip over the header portion*/
452 url_fskip(pb, 32);
453 chunk_size -= 32;
454 case ISNd_TAG:
455 case SCDl_TAG:
456 case SNDC_TAG:
457 case SDEN_TAG:
458 if (!ea->audio_codec) {
459 url_fskip(pb, chunk_size);
460 break;
461 } else if (ea->audio_codec == CODEC_ID_PCM_S16LE_PLANAR ||
462 ea->audio_codec == CODEC_ID_MP3) {
463 num_samples = get_le32(pb);
464 url_fskip(pb, 8);
465 chunk_size -= 12;
466 }
467 ret = av_get_packet(pb, pkt, chunk_size);
468 if (ret != chunk_size)
469 ret = AVERROR(EIO);
470 else {
471 pkt->stream_index = ea->audio_stream_index;
472 pkt->pts = 90000;
473 pkt->pts *= ea->audio_frame_counter;
474 pkt->pts /= ea->sample_rate;
475
476 switch (ea->audio_codec) {
477 case CODEC_ID_ADPCM_EA:
478 /* 2 samples/byte, 1 or 2 samples per frame depending
479 * on stereo; chunk also has 12-byte header */
480 ea->audio_frame_counter += ((chunk_size - 12) * 2) /
481 ea->num_channels;
482 break;
483 case CODEC_ID_PCM_S16LE_PLANAR:
484 case CODEC_ID_MP3:
485 ea->audio_frame_counter += num_samples;
486 break;
487 default:
488 ea->audio_frame_counter += chunk_size /
489 (ea->bytes * ea->num_channels);
490 }
491 }
492
493 packet_read = 1;
494 break;
495
496 /* ending tag */
497 case 0:
498 case ISNe_TAG:
499 case SCEl_TAG:
500 case SEND_TAG:
501 case SEEN_TAG:
502 ret = AVERROR(EIO);
503 packet_read = 1;
504 break;
505
506 case MVIh_TAG:
507 case kVGT_TAG:
508 case pQGT_TAG:
509 case TGQs_TAG:
510 key = PKT_FLAG_KEY;
511 case MVIf_TAG:
512 case fVGT_TAG:
513 url_fseek(pb, -8, SEEK_CUR); // include chunk preamble
514 chunk_size += 8;
515 goto get_video_packet;
516
517 case mTCD_TAG:
518 url_fseek(pb, 8, SEEK_CUR); // skip ea dct header
519 chunk_size -= 8;
520 goto get_video_packet;
521
522 case MV0K_TAG:
523 case MPCh_TAG:
524 key = PKT_FLAG_KEY;
525 case MV0F_TAG:
526 get_video_packet:
527 ret = av_get_packet(pb, pkt, chunk_size);
528 if (ret != chunk_size)
529 ret = AVERROR_IO;
530 else {
531 pkt->stream_index = ea->video_stream_index;
532 pkt->flags |= key;
533 }
534 packet_read = 1;
535 break;
536
537 default:
538 url_fseek(pb, chunk_size, SEEK_CUR);
539 break;
540 }
541 }
542
543 return ret;
544 }
545
546 AVInputFormat ea_demuxer = {
547 "ea",
548 NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia Format"),
549 sizeof(EaDemuxContext),
550 ea_probe,
551 ea_read_header,
552 ea_read_packet,
553 };