]> git.sesse.net Git - ffmpeg/blob - libavformat/electronicarts.c
COSMETICS: Remove all trailing whitespace.
[ffmpeg] / libavformat / electronicarts.c
1 /* Electronic Arts Multimedia File Demuxer
2  * Copyright (c) 2004  The ffmpeg Project
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 /**
20  * @file electronicarts.c
21  * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
22  * by Robin Kay (komadori at gekkou.co.uk)
23  */
24
25 #include "avformat.h"
26
27 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
28 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
29 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
30 #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')
31 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
32 #define _TAG MKTAG('', '', '', '')
33
34 #define EA_SAMPLE_RATE 22050
35 #define EA_BITS_PER_SAMPLE 16
36 #define EA_PREAMBLE_SIZE 8
37
38 typedef struct EaDemuxContext {
39     int width;
40     int height;
41     int video_stream_index;
42     int track_count;
43
44     int audio_stream_index;
45     int audio_frame_counter;
46
47     int64_t audio_pts;
48     int64_t video_pts;
49     int video_pts_inc;
50     float fps;
51
52     int num_channels;
53     int num_samples;
54     int compression_type;
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 WVE file header
76  * Returns 1 if the WVE file is valid and successfully opened, 0 otherwise
77  */
78 static int process_ea_header(AVFormatContext *s) {
79     int inHeader;
80     uint32_t blockid, size;
81     EaDemuxContext *ea = (EaDemuxContext *)s->priv_data;
82     ByteIOContext *pb = &s->pb;
83
84     if (get_buffer(pb, (void*)&blockid, 4) != 4) {
85         return 0;
86     }
87     if (le2me_32(blockid) != SCHl_TAG) {
88         return 0;
89     }
90
91     if (get_buffer(pb, (void*)&size, 4) != 4) {
92         return 0;
93     }
94     size = le2me_32(size);
95
96     if (get_buffer(pb, (void*)&blockid, 4) != 4) {
97         return 0;
98     }
99     if (le2me_32(blockid) != PT00_TAG) {
100         av_log (s, AV_LOG_ERROR, "PT header missing\n");
101         return 0;
102     }
103
104     inHeader = 1;
105     while (inHeader) {
106         int inSubheader;
107         uint8_t byte;
108         byte = get_byte(pb) & 0xFF;
109
110         switch (byte) {
111         case 0xFD:
112             av_log (s, AV_LOG_INFO, "entered audio subheader\n");
113             inSubheader = 1;
114             while (inSubheader) {
115                 uint8_t subbyte;
116                 subbyte = get_byte(pb) & 0xFF;
117
118                 switch (subbyte) {
119                 case 0x82:
120                     ea->num_channels = read_arbitary(pb);
121                     av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
122                     break;
123                 case 0x83:
124                     ea->compression_type = read_arbitary(pb);
125                     av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", ea->compression_type);
126                     break;
127                 case 0x85:
128                     ea->num_samples = read_arbitary(pb);
129                     av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
130                     break;
131                 case 0x8A:
132                     av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
133                     av_log (s, AV_LOG_INFO, "exited audio subheader\n");
134                     inSubheader = 0;
135                     break;
136                 default:
137                     av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
138                     break;
139                 }
140             }
141             break;
142         case 0xFF:
143             av_log (s, AV_LOG_INFO, "end of header block reached\n");
144             inHeader = 0;
145             break;
146         default:
147             av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
148             break;
149         }
150     }
151
152     if ((ea->num_channels != 2) || (ea->compression_type != 7)) {
153         av_log (s, AV_LOG_ERROR, "unsupported stream type\n");
154         return 0;
155     }
156
157     /* skip to the start of the data */
158     url_fseek(pb, size, SEEK_SET);
159
160     return 1;
161 }
162
163
164 static int ea_probe(AVProbeData *p)
165 {
166     if (p->buf_size < 4)
167         return 0;
168
169     if (LE_32(&p->buf[0]) != SCHl_TAG)
170         return 0;
171
172     return AVPROBE_SCORE_MAX;
173 }
174
175 static int ea_read_header(AVFormatContext *s,
176                           AVFormatParameters *ap)
177 {
178     EaDemuxContext *ea = (EaDemuxContext *)s->priv_data;
179     AVStream *st;
180
181     if (!process_ea_header(s))
182         return AVERROR_IO;
183
184 #if 0
185     /* initialize the video decoder stream */
186     st = av_new_stream(s, 0);
187     if (!st)
188         return AVERROR_NOMEM;
189     av_set_pts_info(st, 33, 1, 90000);
190     ea->video_stream_index = st->index;
191     st->codec->codec_type = CODEC_TYPE_VIDEO;
192     st->codec->codec_id = CODEC_ID_EA_MJPEG;
193     st->codec->codec_tag = 0;  /* no fourcc */
194 #endif
195
196     /* initialize the audio decoder stream */
197     st = av_new_stream(s, 0);
198     if (!st)
199         return AVERROR_NOMEM;
200     av_set_pts_info(st, 33, 1, EA_SAMPLE_RATE);
201     st->codec->codec_type = CODEC_TYPE_AUDIO;
202     st->codec->codec_id = CODEC_ID_ADPCM_EA;
203     st->codec->codec_tag = 0;  /* no tag */
204     st->codec->channels = ea->num_channels;
205     st->codec->sample_rate = EA_SAMPLE_RATE;
206     st->codec->bits_per_sample = EA_BITS_PER_SAMPLE;
207     st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
208         st->codec->bits_per_sample / 4;
209     st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
210
211     ea->audio_stream_index = st->index;
212     ea->audio_frame_counter = 0;
213
214     return 1;
215 }
216
217 static int ea_read_packet(AVFormatContext *s,
218                           AVPacket *pkt)
219 {
220     EaDemuxContext *ea = s->priv_data;
221     ByteIOContext *pb = &s->pb;
222     int ret = 0;
223     int packet_read = 0;
224     unsigned char preamble[EA_PREAMBLE_SIZE];
225     unsigned int chunk_type, chunk_size;
226
227     while (!packet_read) {
228
229         if (get_buffer(pb, preamble, EA_PREAMBLE_SIZE) != EA_PREAMBLE_SIZE)
230             return AVERROR_IO;
231         chunk_type = LE_32(&preamble[0]);
232         chunk_size = LE_32(&preamble[4]) - EA_PREAMBLE_SIZE;
233
234         switch (chunk_type) {
235         /* audio data */
236         case SCDl_TAG:
237             ret = av_get_packet(pb, pkt, chunk_size);
238             if (ret != chunk_size)
239                 ret = AVERROR_IO;
240             else {
241                     pkt->stream_index = ea->audio_stream_index;
242                     pkt->pts = 90000;
243                     pkt->pts *= ea->audio_frame_counter;
244                     pkt->pts /= EA_SAMPLE_RATE;
245
246                     /* 2 samples/byte, 1 or 2 samples per frame depending
247                      * on stereo; chunk also has 12-byte header */
248                     ea->audio_frame_counter += ((chunk_size - 12) * 2) /
249                         ea->num_channels;
250             }
251
252             packet_read = 1;
253             break;
254
255         /* ending tag */
256         case SCEl_TAG:
257             ret = AVERROR_IO;
258             packet_read = 1;
259             break;
260
261         default:
262             url_fseek(pb, chunk_size, SEEK_CUR);
263             break;
264         }
265
266         /* ending packet */
267         if (chunk_type == SCEl_TAG) {
268         }
269     }
270
271     return ret;
272 }
273
274 static int ea_read_close(AVFormatContext *s)
275 {
276 //    EaDemuxContext *ea = (EaDemuxContext *)s->priv_data;
277
278     return 0;
279 }
280
281 static AVInputFormat ea_iformat = {
282     "ea",
283     "Electronic Arts Multimedia Format",
284     sizeof(EaDemuxContext),
285     ea_probe,
286     ea_read_header,
287     ea_read_packet,
288     ea_read_close,
289 };
290
291 int ea_init(void)
292 {
293     av_register_input_format(&ea_iformat);
294     return 0;
295 }