]> git.sesse.net Git - ffmpeg/blob - libavformat/electronicarts.c
3d69a5681a804597509e1f698db0bab9e23d09d0
[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 PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
31 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
32 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
33 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
34 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
35 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
36 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
37
38 typedef struct EaDemuxContext {
39     int big_endian;
40
41     int video_codec;
42     AVRational time_base;
43     int video_stream_index;
44
45     int audio_codec;
46     int audio_stream_index;
47     int audio_frame_counter;
48
49     int64_t audio_pts;
50
51     int bytes;
52     int sample_rate;
53     int num_channels;
54     int num_samples;
55 } EaDemuxContext;
56
57 static uint32_t read_arbitary(ByteIOContext *pb) {
58     uint8_t size, byte;
59     int i;
60     uint32_t word;
61
62     size = get_byte(pb);
63
64     word = 0;
65     for (i = 0; i < size; i++) {
66         byte = get_byte(pb);
67         word <<= 8;
68         word |= byte;
69     }
70
71     return word;
72 }
73
74 /*
75  * Process PT/GSTR sound header
76  * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
77  */
78 static int process_audio_header_elements(AVFormatContext *s)
79 {
80     int inHeader = 1;
81     EaDemuxContext *ea = s->priv_data;
82     ByteIOContext *pb = &s->pb;
83     int compression_type = -1, revision = -1;
84
85     ea->bytes = 2;
86     ea->sample_rate = -1;
87     ea->num_channels = 1;
88
89     while (inHeader) {
90         int inSubheader;
91         uint8_t byte;
92         byte = get_byte(pb);
93
94         switch (byte) {
95         case 0xFD:
96             av_log (s, AV_LOG_INFO, "entered audio subheader\n");
97             inSubheader = 1;
98             while (inSubheader) {
99                 uint8_t subbyte;
100                 subbyte = get_byte(pb);
101
102                 switch (subbyte) {
103                 case 0x80:
104                     revision = read_arbitary(pb);
105                     av_log (s, AV_LOG_INFO, "revision (element 0x80) set to 0x%08x\n", revision);
106                     break;
107                 case 0x82:
108                     ea->num_channels = read_arbitary(pb);
109                     av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
110                     break;
111                 case 0x83:
112                     compression_type = read_arbitary(pb);
113                     av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
114                     break;
115                 case 0x84:
116                     ea->sample_rate = read_arbitary(pb);
117                     av_log (s, AV_LOG_INFO, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
118                     break;
119                 case 0x85:
120                     ea->num_samples = read_arbitary(pb);
121                     av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
122                     break;
123                 case 0x8A:
124                     av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
125                     av_log (s, AV_LOG_INFO, "exited audio subheader\n");
126                     inSubheader = 0;
127                     break;
128                 case 0xFF:
129                     av_log (s, AV_LOG_INFO, "end of header block reached (within audio subheader)\n");
130                     inSubheader = 0;
131                     inHeader = 0;
132                     break;
133                 default:
134                     av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
135                     break;
136                 }
137             }
138             break;
139         case 0xFF:
140             av_log (s, AV_LOG_INFO, "end of header block reached\n");
141             inHeader = 0;
142             break;
143         default:
144             av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
145             break;
146         }
147     }
148
149     switch (compression_type) {
150     case  0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
151     case  7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
152     default:
153         av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
154         return 0;
155     }
156
157     if (ea->sample_rate == -1)
158         ea->sample_rate = revision==3 ? 48000 : 22050;
159
160     return 1;
161 }
162
163 static int process_video_header_vp6(AVFormatContext *s)
164 {
165     EaDemuxContext *ea = s->priv_data;
166     ByteIOContext *pb = &s->pb;
167
168     url_fskip(pb, 16);
169     ea->time_base.den = get_le32(pb);
170     ea->time_base.num = get_le32(pb);
171     ea->video_codec = CODEC_ID_VP6;
172
173     return 1;
174 }
175
176 /*
177  * Process EA file header
178  * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
179  */
180 static int process_ea_header(AVFormatContext *s) {
181     uint32_t blockid, size = 0;
182     EaDemuxContext *ea = s->priv_data;
183     ByteIOContext *pb = &s->pb;
184     int i;
185
186     for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
187         unsigned int startpos = url_ftell(pb);
188         int err = 0;
189
190         blockid = get_le32(pb);
191         size = get_le32(pb);
192         if (i == 0)
193             ea->big_endian = size > 0x000FFFFF;
194         if (ea->big_endian)
195             size = bswap_32(size);
196
197         switch (blockid) {
198             case SCHl_TAG :
199                 blockid = get_le32(pb);
200                 if (blockid == GSTR_TAG) {
201                     url_fskip(pb, 4);
202                 } else if (blockid != PT00_TAG) {
203                     av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
204                     return 0;
205                 }
206                 err = process_audio_header_elements(s);
207                 break;
208
209             case MVhd_TAG :
210                 err = process_video_header_vp6(s);
211                 break;
212         }
213
214         if (err < 0) {
215             av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
216             return err;
217         }
218
219         url_fseek(pb, startpos + size, SEEK_SET);
220     }
221
222     url_fseek(pb, 0, SEEK_SET);
223
224     return 1;
225 }
226
227
228 static int ea_probe(AVProbeData *p)
229 {
230     uint32_t tag;
231
232     tag = AV_RL32(&p->buf[0]);
233     if (tag == SCHl_TAG || tag == MVhd_TAG)
234         return AVPROBE_SCORE_MAX;
235
236     return 0;
237 }
238
239 static int ea_read_header(AVFormatContext *s,
240                           AVFormatParameters *ap)
241 {
242     EaDemuxContext *ea = s->priv_data;
243     AVStream *st;
244
245     if (!process_ea_header(s))
246         return AVERROR(EIO);
247
248     if (ea->time_base.num && ea->time_base.den) {
249         /* initialize the video decoder stream */
250         st = av_new_stream(s, 0);
251         if (!st)
252             return AVERROR(ENOMEM);
253         ea->video_stream_index = st->index;
254         st->codec->codec_type = CODEC_TYPE_VIDEO;
255         st->codec->codec_id = ea->video_codec;
256         st->codec->codec_tag = 0;  /* no fourcc */
257         st->codec->time_base = ea->time_base;
258     }
259
260     if (ea->audio_codec) {
261     /* initialize the audio decoder stream */
262     st = av_new_stream(s, 0);
263     if (!st)
264         return AVERROR(ENOMEM);
265     av_set_pts_info(st, 33, 1, ea->sample_rate);
266     st->codec->codec_type = CODEC_TYPE_AUDIO;
267     st->codec->codec_id = ea->audio_codec;
268     st->codec->codec_tag = 0;  /* no tag */
269     st->codec->channels = ea->num_channels;
270     st->codec->sample_rate = ea->sample_rate;
271     st->codec->bits_per_sample = ea->bytes * 8;
272     st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
273         st->codec->bits_per_sample / 4;
274     st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
275
276     ea->audio_stream_index = st->index;
277     ea->audio_frame_counter = 0;
278     }
279
280     return 1;
281 }
282
283 static int ea_read_packet(AVFormatContext *s,
284                           AVPacket *pkt)
285 {
286     EaDemuxContext *ea = s->priv_data;
287     ByteIOContext *pb = &s->pb;
288     int ret = 0;
289     int packet_read = 0;
290     unsigned int chunk_type, chunk_size;
291     int key = 0;
292
293     while (!packet_read) {
294         chunk_type = get_le32(pb);
295         chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
296
297         switch (chunk_type) {
298         /* audio data */
299         case SCDl_TAG:
300             ret = av_get_packet(pb, pkt, chunk_size);
301             if (ret != chunk_size)
302                 ret = AVERROR(EIO);
303             else {
304                     pkt->stream_index = ea->audio_stream_index;
305                     pkt->pts = 90000;
306                     pkt->pts *= ea->audio_frame_counter;
307                     pkt->pts /= ea->sample_rate;
308
309                     switch (ea->audio_codec) {
310                     case CODEC_ID_ADPCM_EA:
311                     /* 2 samples/byte, 1 or 2 samples per frame depending
312                      * on stereo; chunk also has 12-byte header */
313                     ea->audio_frame_counter += ((chunk_size - 12) * 2) /
314                         ea->num_channels;
315                         break;
316                     default:
317                         ea->audio_frame_counter += chunk_size /
318                             (ea->bytes * ea->num_channels);
319                     }
320             }
321
322             packet_read = 1;
323             break;
324
325         /* ending tag */
326         case SCEl_TAG:
327             ret = AVERROR(EIO);
328             packet_read = 1;
329             break;
330
331         case MV0K_TAG:
332             key = PKT_FLAG_KEY;
333         case MV0F_TAG:
334             ret = av_get_packet(pb, pkt, chunk_size);
335             if (ret != chunk_size)
336                 ret = AVERROR_IO;
337             else {
338                 pkt->stream_index = ea->video_stream_index;
339                 pkt->flags |= key;
340             }
341             packet_read = 1;
342             break;
343
344         default:
345             url_fseek(pb, chunk_size, SEEK_CUR);
346             break;
347         }
348     }
349
350     return ret;
351 }
352
353 AVInputFormat ea_demuxer = {
354     "ea",
355     "Electronic Arts Multimedia Format",
356     sizeof(EaDemuxContext),
357     ea_probe,
358     ea_read_header,
359     ea_read_packet,
360 };