]> git.sesse.net Git - ffmpeg/blob - libavformat/electronicarts.c
8214480229e976af62aae8c72b9a8e67254460c1
[ffmpeg] / libavformat / electronicarts.c
1 /* Electronic Arts Multimedia File Demuxer
2  * Copyright (c) 2004  The ffmpeg Project
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file electronicarts.c
23  * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
24  * by Robin Kay (komadori at gekkou.co.uk)
25  */
26
27 #include "avformat.h"
28
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 */
48
49 typedef struct EaDemuxContext {
50     int big_endian;
51
52     int video_codec;
53     AVRational time_base;
54     int video_stream_index;
55
56     int audio_codec;
57     int audio_stream_index;
58     int audio_frame_counter;
59
60     int64_t audio_pts;
61
62     int bytes;
63     int sample_rate;
64     int num_channels;
65     int num_samples;
66 } EaDemuxContext;
67
68 static uint32_t read_arbitary(ByteIOContext *pb) {
69     uint8_t size, byte;
70     int i;
71     uint32_t word;
72
73     size = get_byte(pb);
74
75     word = 0;
76     for (i = 0; i < size; i++) {
77         byte = get_byte(pb);
78         word <<= 8;
79         word |= byte;
80     }
81
82     return word;
83 }
84
85 /*
86  * Process PT/GSTR sound header
87  * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
88  */
89 static int process_audio_header_elements(AVFormatContext *s)
90 {
91     int inHeader = 1;
92     EaDemuxContext *ea = s->priv_data;
93     ByteIOContext *pb = &s->pb;
94     int compression_type = -1, revision = -1;
95
96     ea->bytes = 2;
97     ea->sample_rate = -1;
98     ea->num_channels = 1;
99
100     while (inHeader) {
101         int inSubheader;
102         uint8_t byte;
103         byte = get_byte(pb);
104
105         switch (byte) {
106         case 0xFD:
107             av_log (s, AV_LOG_INFO, "entered audio subheader\n");
108             inSubheader = 1;
109             while (inSubheader) {
110                 uint8_t subbyte;
111                 subbyte = get_byte(pb);
112
113                 switch (subbyte) {
114                 case 0x80:
115                     revision = read_arbitary(pb);
116                     av_log (s, AV_LOG_INFO, "revision (element 0x80) set to 0x%08x\n", revision);
117                     break;
118                 case 0x82:
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);
121                     break;
122                 case 0x83:
123                     compression_type = read_arbitary(pb);
124                     av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
125                     break;
126                 case 0x84:
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);
129                     break;
130                 case 0x85:
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);
133                     break;
134                 case 0x8A:
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");
137                     inSubheader = 0;
138                     break;
139                 case 0xFF:
140                     av_log (s, AV_LOG_INFO, "end of header block reached (within audio subheader)\n");
141                     inSubheader = 0;
142                     inHeader = 0;
143                     break;
144                 default:
145                     av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
146                     break;
147                 }
148             }
149             break;
150         case 0xFF:
151             av_log (s, AV_LOG_INFO, "end of header block reached\n");
152             inHeader = 0;
153             break;
154         default:
155             av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
156             break;
157         }
158     }
159
160     switch (compression_type) {
161     case  0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
162     case  7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
163     case -1:
164         switch (revision) {
165         case  1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
166         case  2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
167         case  3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
168         default:
169             av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
170             return 0;
171         }
172         break;
173     default:
174         av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
175         return 0;
176     }
177
178     if (ea->sample_rate == -1)
179         ea->sample_rate = revision==3 ? 48000 : 22050;
180
181     return 1;
182 }
183
184 /*
185  * Process EACS sound header
186  * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
187  */
188 static int process_audio_header_eacs(AVFormatContext *s)
189 {
190     EaDemuxContext *ea = s->priv_data;
191     ByteIOContext *pb = &s->pb;
192     int compression_type;
193
194     ea->sample_rate  = ea->big_endian ? get_be32(pb) : get_le32(pb);
195     ea->bytes        = get_byte(pb);   /* 1=8-bit, 2=16-bit */
196     ea->num_channels = get_byte(pb);
197     compression_type = get_byte(pb);
198     url_fskip(pb, 13);
199
200     switch (compression_type) {
201     case 0:
202         switch (ea->bytes) {
203         case 1: ea->audio_codec = CODEC_ID_PCM_S8;    break;
204         case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
205         }
206         break;
207     case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
208     case 2: ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_EACS; break;
209     default:
210         av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
211     }
212
213     return 1;
214 }
215
216 /*
217  * Process SEAD sound header
218  * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
219  */
220 static int process_audio_header_sead(AVFormatContext *s)
221 {
222     EaDemuxContext *ea = s->priv_data;
223     ByteIOContext *pb = &s->pb;
224
225     ea->sample_rate  = get_le32(pb);
226     ea->bytes        = get_le32(pb);  /* 1=8-bit, 2=16-bit */
227     ea->num_channels = get_le32(pb);
228     ea->audio_codec  = CODEC_ID_ADPCM_IMA_EA_SEAD;
229
230     return 1;
231 }
232
233 static int process_video_header_vp6(AVFormatContext *s)
234 {
235     EaDemuxContext *ea = s->priv_data;
236     ByteIOContext *pb = &s->pb;
237
238     url_fskip(pb, 16);
239     ea->time_base.den = get_le32(pb);
240     ea->time_base.num = get_le32(pb);
241     ea->video_codec = CODEC_ID_VP6;
242
243     return 1;
244 }
245
246 /*
247  * Process EA file header
248  * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
249  */
250 static int process_ea_header(AVFormatContext *s) {
251     uint32_t blockid, size = 0;
252     EaDemuxContext *ea = s->priv_data;
253     ByteIOContext *pb = &s->pb;
254     int i;
255
256     for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
257         unsigned int startpos = url_ftell(pb);
258         int err = 0;
259
260         blockid = get_le32(pb);
261         size = get_le32(pb);
262         if (i == 0)
263             ea->big_endian = size > 0x000FFFFF;
264         if (ea->big_endian)
265             size = bswap_32(size);
266
267         switch (blockid) {
268             case ISNh_TAG:
269                 if (get_le32(pb) != EACS_TAG) {
270                     av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
271                     return 0;
272                 }
273                 err = process_audio_header_eacs(s);
274                 break;
275
276             case SCHl_TAG :
277                 blockid = get_le32(pb);
278                 if (blockid == GSTR_TAG) {
279                     url_fskip(pb, 4);
280                 } else if (blockid != PT00_TAG) {
281                     av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
282                     return 0;
283                 }
284                 err = process_audio_header_elements(s);
285                 break;
286
287             case SEAD_TAG:
288                 err = process_audio_header_sead(s);
289                 break;
290
291             case MVhd_TAG :
292                 err = process_video_header_vp6(s);
293                 break;
294         }
295
296         if (err < 0) {
297             av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
298             return err;
299         }
300
301         url_fseek(pb, startpos + size, SEEK_SET);
302     }
303
304     url_fseek(pb, 0, SEEK_SET);
305
306     return 1;
307 }
308
309
310 static int ea_probe(AVProbeData *p)
311 {
312     switch (AV_RL32(&p->buf[0])) {
313     case ISNh_TAG:
314     case SCHl_TAG:
315     case SEAD_TAG:
316     case kVGT_TAG:
317     case MADk_TAG:
318     case MPCh_TAG:
319     case MVhd_TAG:
320     case MVIh_TAG:
321         return AVPROBE_SCORE_MAX;
322     }
323     return 0;
324 }
325
326 static int ea_read_header(AVFormatContext *s,
327                           AVFormatParameters *ap)
328 {
329     EaDemuxContext *ea = s->priv_data;
330     AVStream *st;
331
332     if (!process_ea_header(s))
333         return AVERROR(EIO);
334
335     if (ea->video_codec) {
336         /* initialize the video decoder stream */
337         st = av_new_stream(s, 0);
338         if (!st)
339             return AVERROR(ENOMEM);
340         ea->video_stream_index = st->index;
341         st->codec->codec_type = CODEC_TYPE_VIDEO;
342         st->codec->codec_id = ea->video_codec;
343         st->codec->codec_tag = 0;  /* no fourcc */
344         st->codec->time_base = ea->time_base;
345     }
346
347     if (ea->audio_codec) {
348         /* initialize the audio decoder stream */
349         st = av_new_stream(s, 0);
350         if (!st)
351             return AVERROR(ENOMEM);
352         av_set_pts_info(st, 33, 1, ea->sample_rate);
353         st->codec->codec_type = CODEC_TYPE_AUDIO;
354         st->codec->codec_id = ea->audio_codec;
355         st->codec->codec_tag = 0;  /* no tag */
356         st->codec->channels = ea->num_channels;
357         st->codec->sample_rate = ea->sample_rate;
358         st->codec->bits_per_sample = ea->bytes * 8;
359         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
360             st->codec->bits_per_sample / 4;
361         st->codec->block_align = st->codec->channels*st->codec->bits_per_sample;
362         ea->audio_stream_index = st->index;
363         ea->audio_frame_counter = 0;
364     }
365
366     return 1;
367 }
368
369 static int ea_read_packet(AVFormatContext *s,
370                           AVPacket *pkt)
371 {
372     EaDemuxContext *ea = s->priv_data;
373     ByteIOContext *pb = &s->pb;
374     int ret = 0;
375     int packet_read = 0;
376     unsigned int chunk_type, chunk_size;
377     int key = 0;
378
379     while (!packet_read) {
380         chunk_type = get_le32(pb);
381         chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
382
383         switch (chunk_type) {
384         /* audio data */
385         case ISNh_TAG:
386             /* header chunk also contains data; skip over the header portion*/
387             url_fskip(pb, 32);
388             chunk_size -= 32;
389         case ISNd_TAG:
390         case SCDl_TAG:
391         case SNDC_TAG:
392             if (!ea->audio_codec) {
393                 url_fskip(pb, chunk_size);
394                 break;
395             }
396             ret = av_get_packet(pb, pkt, chunk_size);
397             if (ret != chunk_size)
398                 ret = AVERROR(EIO);
399             else {
400                     pkt->stream_index = ea->audio_stream_index;
401                     pkt->pts = 90000;
402                     pkt->pts *= ea->audio_frame_counter;
403                     pkt->pts /= ea->sample_rate;
404
405                     switch (ea->audio_codec) {
406                     case CODEC_ID_ADPCM_EA:
407                     /* 2 samples/byte, 1 or 2 samples per frame depending
408                      * on stereo; chunk also has 12-byte header */
409                     ea->audio_frame_counter += ((chunk_size - 12) * 2) /
410                         ea->num_channels;
411                         break;
412                     default:
413                         ea->audio_frame_counter += chunk_size /
414                             (ea->bytes * ea->num_channels);
415                     }
416             }
417
418             packet_read = 1;
419             break;
420
421         /* ending tag */
422         case 0:
423         case ISNe_TAG:
424         case SCEl_TAG:
425         case SEND_TAG:
426             ret = AVERROR(EIO);
427             packet_read = 1;
428             break;
429
430         case MV0K_TAG:
431             key = PKT_FLAG_KEY;
432         case MV0F_TAG:
433             ret = av_get_packet(pb, pkt, chunk_size);
434             if (ret != chunk_size)
435                 ret = AVERROR_IO;
436             else {
437                 pkt->stream_index = ea->video_stream_index;
438                 pkt->flags |= key;
439             }
440             packet_read = 1;
441             break;
442
443         default:
444             url_fseek(pb, chunk_size, SEEK_CUR);
445             break;
446         }
447     }
448
449     return ret;
450 }
451
452 AVInputFormat ea_demuxer = {
453     "ea",
454     "Electronic Arts Multimedia Format",
455     sizeof(EaDemuxContext),
456     ea_probe,
457     ea_read_header,
458     ea_read_packet,
459 };