]> git.sesse.net Git - ffmpeg/blob - libavformat/electronicarts.c
remove useless &0xFF
[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 SCDl_TAG MKTAG('S', 'C', 'D', 'l')
32 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
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 video_stream_index;
40
41     int audio_stream_index;
42     int audio_frame_counter;
43
44     int64_t audio_pts;
45
46     int num_channels;
47     int num_samples;
48     int compression_type;
49 } EaDemuxContext;
50
51 static uint32_t read_arbitary(ByteIOContext *pb) {
52     uint8_t size, byte;
53     int i;
54     uint32_t word;
55
56     size = get_byte(pb);
57
58     word = 0;
59     for (i = 0; i < size; i++) {
60         byte = get_byte(pb);
61         word <<= 8;
62         word |= byte;
63     }
64
65     return word;
66 }
67
68 /*
69  * Process WVE file header
70  * Returns 1 if the WVE file is valid and successfully opened, 0 otherwise
71  */
72 static int process_ea_header(AVFormatContext *s) {
73     int inHeader = 1;
74     uint32_t blockid, size;
75     EaDemuxContext *ea = s->priv_data;
76     ByteIOContext *pb = &s->pb;
77
78     if (get_le32(pb) != SCHl_TAG)
79         return 0;
80     size = get_le32(pb);
81     if (get_le32(pb) != PT00_TAG) {
82         av_log (s, AV_LOG_ERROR, "PT header missing\n");
83         return 0;
84     }
85
86     while (inHeader) {
87         int inSubheader;
88         uint8_t byte;
89         byte = get_byte(pb);
90
91         switch (byte) {
92         case 0xFD:
93             av_log (s, AV_LOG_INFO, "entered audio subheader\n");
94             inSubheader = 1;
95             while (inSubheader) {
96                 uint8_t subbyte;
97                 subbyte = get_byte(pb);
98
99                 switch (subbyte) {
100                 case 0x82:
101                     ea->num_channels = read_arbitary(pb);
102                     av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
103                     break;
104                 case 0x83:
105                     ea->compression_type = read_arbitary(pb);
106                     av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", ea->compression_type);
107                     break;
108                 case 0x85:
109                     ea->num_samples = read_arbitary(pb);
110                     av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
111                     break;
112                 case 0x8A:
113                     av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
114                     av_log (s, AV_LOG_INFO, "exited audio subheader\n");
115                     inSubheader = 0;
116                     break;
117                 default:
118                     av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
119                     break;
120                 }
121             }
122             break;
123         case 0xFF:
124             av_log (s, AV_LOG_INFO, "end of header block reached\n");
125             inHeader = 0;
126             break;
127         default:
128             av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
129             break;
130         }
131     }
132
133     if ((ea->num_channels != 2) || (ea->compression_type != 7)) {
134         av_log (s, AV_LOG_ERROR, "unsupported stream type\n");
135         return 0;
136     }
137
138     /* skip to the start of the data */
139     url_fseek(pb, size, SEEK_SET);
140
141     return 1;
142 }
143
144
145 static int ea_probe(AVProbeData *p)
146 {
147     if (AV_RL32(&p->buf[0]) != SCHl_TAG)
148         return 0;
149
150     return AVPROBE_SCORE_MAX;
151 }
152
153 static int ea_read_header(AVFormatContext *s,
154                           AVFormatParameters *ap)
155 {
156     EaDemuxContext *ea = s->priv_data;
157     AVStream *st;
158
159     if (!process_ea_header(s))
160         return AVERROR(EIO);
161
162 #if 0
163     /* initialize the video decoder stream */
164     st = av_new_stream(s, 0);
165     if (!st)
166         return AVERROR(ENOMEM);
167     av_set_pts_info(st, 33, 1, 90000);
168     ea->video_stream_index = st->index;
169     st->codec->codec_type = CODEC_TYPE_VIDEO;
170     st->codec->codec_id = CODEC_ID_EA_MJPEG;
171     st->codec->codec_tag = 0;  /* no fourcc */
172 #endif
173
174     /* initialize the audio decoder stream */
175     st = av_new_stream(s, 0);
176     if (!st)
177         return AVERROR(ENOMEM);
178     av_set_pts_info(st, 33, 1, EA_SAMPLE_RATE);
179     st->codec->codec_type = CODEC_TYPE_AUDIO;
180     st->codec->codec_id = CODEC_ID_ADPCM_EA;
181     st->codec->codec_tag = 0;  /* no tag */
182     st->codec->channels = ea->num_channels;
183     st->codec->sample_rate = EA_SAMPLE_RATE;
184     st->codec->bits_per_sample = EA_BITS_PER_SAMPLE;
185     st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
186         st->codec->bits_per_sample / 4;
187     st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
188
189     ea->audio_stream_index = st->index;
190     ea->audio_frame_counter = 0;
191
192     return 1;
193 }
194
195 static int ea_read_packet(AVFormatContext *s,
196                           AVPacket *pkt)
197 {
198     EaDemuxContext *ea = s->priv_data;
199     ByteIOContext *pb = &s->pb;
200     int ret = 0;
201     int packet_read = 0;
202     unsigned char preamble[EA_PREAMBLE_SIZE];
203     unsigned int chunk_type, chunk_size;
204
205     while (!packet_read) {
206
207         if (get_buffer(pb, preamble, EA_PREAMBLE_SIZE) != EA_PREAMBLE_SIZE)
208             return AVERROR(EIO);
209         chunk_type = AV_RL32(&preamble[0]);
210         chunk_size = AV_RL32(&preamble[4]) - EA_PREAMBLE_SIZE;
211
212         switch (chunk_type) {
213         /* audio data */
214         case SCDl_TAG:
215             ret = av_get_packet(pb, pkt, chunk_size);
216             if (ret != chunk_size)
217                 ret = AVERROR(EIO);
218             else {
219                     pkt->stream_index = ea->audio_stream_index;
220                     pkt->pts = 90000;
221                     pkt->pts *= ea->audio_frame_counter;
222                     pkt->pts /= EA_SAMPLE_RATE;
223
224                     /* 2 samples/byte, 1 or 2 samples per frame depending
225                      * on stereo; chunk also has 12-byte header */
226                     ea->audio_frame_counter += ((chunk_size - 12) * 2) /
227                         ea->num_channels;
228             }
229
230             packet_read = 1;
231             break;
232
233         /* ending tag */
234         case SCEl_TAG:
235             ret = AVERROR(EIO);
236             packet_read = 1;
237             break;
238
239         default:
240             url_fseek(pb, chunk_size, SEEK_CUR);
241             break;
242         }
243     }
244
245     return ret;
246 }
247
248 AVInputFormat ea_demuxer = {
249     "ea",
250     "Electronic Arts Multimedia Format",
251     sizeof(EaDemuxContext),
252     ea_probe,
253     ea_read_header,
254     ea_read_packet,
255 };