937
|
1 /*
|
|
2 * Creative Voice File demuxer.
|
|
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
|
8 * version 2 of the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This library is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Lesser General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Lesser General Public
|
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 */
|
|
19
|
|
20 #include "avformat.h"
|
|
21 #include "avi.h" /* for CodecTag */
|
|
22 #include "voc.h"
|
|
23
|
|
24
|
|
25 typedef enum voc_type {
|
|
26 VOC_TYPE_EOF = 0x00,
|
|
27 VOC_TYPE_VOICE_DATA = 0x01,
|
|
28 VOC_TYPE_VOICE_DATA_CONT = 0x02,
|
|
29 VOC_TYPE_SILENCE = 0x03,
|
|
30 VOC_TYPE_MARKER = 0x04,
|
|
31 VOC_TYPE_ASCII = 0x05,
|
|
32 VOC_TYPE_REPETITION_START = 0x06,
|
|
33 VOC_TYPE_REPETITION_END = 0x07,
|
|
34 VOC_TYPE_EXTENDED = 0x08,
|
|
35 VOC_TYPE_NEW_VOICE_DATA = 0x09,
|
|
36 } voc_type_t;
|
|
37
|
|
38
|
|
39 static const int voc_max_pkt_size = 2048;
|
|
40 static const unsigned char voc_magic[] = "Creative Voice File\x1A";
|
|
41
|
|
42 static const CodecTag voc_codec_tags[] = {
|
|
43 {CODEC_ID_PCM_U8, 0x00},
|
|
44 {CODEC_ID_PCM_S16LE, 0x04},
|
|
45 {CODEC_ID_PCM_ALAW, 0x06},
|
|
46 {CODEC_ID_PCM_MULAW, 0x07},
|
|
47 {CODEC_ID_ADPCM_CT, 0x0200},
|
|
48 {0, 0},
|
|
49 };
|
|
50
|
|
51
|
|
52 #ifdef CONFIG_DEMUXERS
|
|
53
|
|
54 static int voc_probe(AVProbeData *p)
|
|
55 {
|
|
56 int version, check;
|
|
57
|
|
58 if (p->buf_size < 26)
|
|
59 return 0;
|
|
60 if (memcmp(p->buf, voc_magic, sizeof(voc_magic) - 1))
|
|
61 return 0;
|
|
62 version = p->buf[22] | (p->buf[23] << 8);
|
|
63 check = p->buf[24] | (p->buf[25] << 8);
|
|
64 if (~version + 0x1234 != check)
|
|
65 return 10;
|
|
66
|
|
67 return AVPROBE_SCORE_MAX;
|
|
68 }
|
|
69
|
|
70 static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
|
|
71 {
|
|
72 voc_dec_context_t *voc = s->priv_data;
|
|
73 ByteIOContext *pb = &s->pb;
|
|
74 int header_size;
|
|
75 AVStream *st;
|
|
76
|
|
77 url_fskip(pb, 20);
|
|
78 header_size = get_le16(pb) - 22;
|
|
79 if (header_size != 4) {
|
|
80 av_log(s, AV_LOG_ERROR, "unkown header size: %d\n", header_size);
|
|
81 return AVERROR_NOTSUPP;
|
|
82 }
|
|
83 url_fskip(pb, header_size);
|
|
84 st = av_new_stream(s, 0);
|
|
85 if (!st)
|
|
86 return AVERROR_NOMEM;
|
|
87 st->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
88
|
|
89 voc->remaining_size = 0;
|
|
90 return 0;
|
|
91 }
|
|
92
|
|
93 int
|
|
94 voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
|
|
95 {
|
|
96 voc_dec_context_t *voc = s->priv_data;
|
|
97 AVCodecContext *dec = st->codec;
|
|
98 ByteIOContext *pb = &s->pb;
|
|
99 voc_type_t type;
|
|
100 int size;
|
|
101 int sample_rate = 0;
|
|
102 int channels = 1;
|
|
103
|
|
104 while (!voc->remaining_size) {
|
|
105 type = get_byte(pb);
|
|
106 if (type == VOC_TYPE_EOF)
|
|
107 return AVERROR_IO;
|
|
108 voc->remaining_size = get_le24(pb);
|
|
109 max_size -= 4;
|
|
110
|
|
111 switch (type) {
|
|
112 case VOC_TYPE_VOICE_DATA:
|
|
113 dec->sample_rate = 1000000 / (256 - get_byte(pb));
|
|
114 if (sample_rate)
|
|
115 dec->sample_rate = sample_rate;
|
|
116 dec->channels = channels;
|
|
117 dec->codec_id = codec_get_id(voc_codec_tags, get_byte(pb));
|
|
118 voc->remaining_size -= 2;
|
|
119 max_size -= 2;
|
|
120 channels = 1;
|
|
121 break;
|
|
122
|
|
123 case VOC_TYPE_VOICE_DATA_CONT:
|
|
124 break;
|
|
125
|
|
126 case VOC_TYPE_EXTENDED:
|
|
127 sample_rate = get_le16(pb);
|
|
128 get_byte(pb);
|
|
129 channels = get_byte(pb) + 1;
|
|
130 sample_rate = 256000000 / (channels * (65536 - sample_rate));
|
|
131 voc->remaining_size = 0;
|
|
132 max_size -= 4;
|
|
133 break;
|
|
134
|
|
135 case VOC_TYPE_NEW_VOICE_DATA:
|
|
136 dec->sample_rate = get_le32(pb);
|
|
137 dec->bits_per_sample = get_byte(pb);
|
|
138 dec->channels = get_byte(pb);
|
|
139 dec->codec_id = codec_get_id(voc_codec_tags, get_le16(pb));
|
|
140 url_fskip(pb, 4);
|
|
141 voc->remaining_size -= 12;
|
|
142 max_size -= 12;
|
|
143 break;
|
|
144
|
|
145 default:
|
|
146 url_fskip(pb, voc->remaining_size);
|
|
147 max_size -= voc->remaining_size;
|
|
148 voc->remaining_size = 0;
|
|
149 break;
|
|
150 }
|
|
151 }
|
|
152
|
|
153 dec->bit_rate = dec->sample_rate * dec->bits_per_sample;
|
|
154
|
|
155 if (max_size <= 0)
|
|
156 max_size = voc_max_pkt_size;
|
|
157 size = FFMIN(voc->remaining_size, max_size);
|
|
158 voc->remaining_size -= size;
|
|
159 return av_get_packet(pb, pkt, size);
|
|
160 }
|
|
161
|
|
162 static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
163 {
|
|
164 return voc_get_packet(s, pkt, s->streams[0], 0);
|
|
165 }
|
|
166
|
|
167 static int voc_read_close(AVFormatContext *s)
|
|
168 {
|
|
169 return 0;
|
|
170 }
|
|
171
|
|
172 static AVInputFormat voc_iformat = {
|
|
173 "voc",
|
|
174 "Creative Voice File format",
|
|
175 sizeof(voc_dec_context_t),
|
|
176 voc_probe,
|
|
177 voc_read_header,
|
|
178 voc_read_packet,
|
|
179 voc_read_close,
|
|
180 };
|
|
181
|
|
182 #endif /* CONFIG_DEMUXERS */
|
|
183
|
|
184
|
|
185 #ifdef CONFIG_MUXERS
|
|
186
|
|
187 typedef struct voc_enc_context {
|
|
188 int param_written;
|
|
189 } voc_enc_context_t;
|
|
190
|
|
191 static int voc_write_header(AVFormatContext *s)
|
|
192 {
|
|
193 ByteIOContext *pb = &s->pb;
|
|
194 const int header_size = 26;
|
|
195 const int version = 0x0114;
|
|
196
|
|
197 if (s->nb_streams != 1
|
|
198 || s->streams[0]->codec->codec_type != CODEC_TYPE_AUDIO)
|
|
199 return AVERROR_NOTSUPP;
|
|
200
|
|
201 put_buffer(pb, voc_magic, sizeof(voc_magic) - 1);
|
|
202 put_le16(pb, header_size);
|
|
203 put_le16(pb, version);
|
|
204 put_le16(pb, ~version + 0x1234);
|
|
205
|
|
206 return 0;
|
|
207 }
|
|
208
|
|
209 static int voc_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
210 {
|
|
211 voc_enc_context_t *voc = s->priv_data;
|
|
212 AVCodecContext *enc = s->streams[0]->codec;
|
|
213 ByteIOContext *pb = &s->pb;
|
|
214
|
|
215 if (!voc->param_written) {
|
|
216 int format = codec_get_tag(voc_codec_tags, enc->codec_id);
|
|
217
|
|
218 if (format > 0xFF) {
|
|
219 put_byte(pb, VOC_TYPE_NEW_VOICE_DATA);
|
|
220 put_le24(pb, pkt->size + 12);
|
|
221 put_le32(pb, enc->sample_rate);
|
|
222 put_byte(pb, enc->bits_per_sample);
|
|
223 put_byte(pb, enc->channels);
|
|
224 put_le16(pb, format);
|
|
225 put_le32(pb, 0);
|
|
226 } else {
|
|
227 if (s->streams[0]->codec->channels > 1) {
|
|
228 put_byte(pb, VOC_TYPE_EXTENDED);
|
|
229 put_le24(pb, 4);
|
|
230 put_le16(pb, 65536-256000000/(enc->sample_rate*enc->channels));
|
|
231 put_byte(pb, format);
|
|
232 put_byte(pb, enc->channels - 1);
|
|
233 }
|
|
234 put_byte(pb, VOC_TYPE_VOICE_DATA);
|
|
235 put_le24(pb, pkt->size + 2);
|
|
236 put_byte(pb, 256 - 1000000 / enc->sample_rate);
|
|
237 put_byte(pb, format);
|
|
238 }
|
|
239 voc->param_written = 1;
|
|
240 } else {
|
|
241 put_byte(pb, VOC_TYPE_VOICE_DATA_CONT);
|
|
242 put_le24(pb, pkt->size);
|
|
243 }
|
|
244
|
|
245 put_buffer(pb, pkt->data, pkt->size);
|
|
246 return 0;
|
|
247 }
|
|
248
|
|
249 static int voc_write_trailer(AVFormatContext *s)
|
|
250 {
|
|
251 put_byte(&s->pb, 0);
|
|
252 return 0;
|
|
253 }
|
|
254
|
|
255 static AVOutputFormat voc_oformat = {
|
|
256 "voc",
|
|
257 "Creative Voice File format",
|
|
258 "audio/x-voc",
|
|
259 "voc",
|
|
260 sizeof(voc_enc_context_t),
|
|
261 CODEC_ID_PCM_U8,
|
|
262 CODEC_ID_NONE,
|
|
263 voc_write_header,
|
|
264 voc_write_packet,
|
|
265 voc_write_trailer,
|
|
266 };
|
|
267
|
|
268 #endif /* CONFIG_MUXERS */
|
|
269
|
|
270
|
|
271 int voc_init(void)
|
|
272 {
|
|
273 #ifdef CONFIG_DEMUXERS
|
|
274 av_register_input_format(&voc_iformat);
|
|
275 #endif /* CONFIG_DEMUXERS */
|
|
276 #ifdef CONFIG_MUXERS
|
|
277 av_register_output_format(&voc_oformat);
|
|
278 #endif /* CONFIG_MUXERS */
|
|
279 return 0;
|
|
280 }
|