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