2 * Creative Voice File demuxer.
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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
21 #include "avi.h" /* for CodecTag */
25 typedef enum voc_type {
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,
39 static const int voc_max_pkt_size = 2048;
40 static const unsigned char voc_magic[] = "Creative Voice File\x1A";
42 static const CodecTag voc_codec_tags[] = {
43 {CODEC_ID_PCM_U8, 0x00},
44 {CODEC_ID_ADPCM_SBPRO_4, 0x01},
45 {CODEC_ID_ADPCM_SBPRO_3, 0x02},
46 {CODEC_ID_ADPCM_SBPRO_2, 0x03},
47 {CODEC_ID_PCM_S16LE, 0x04},
48 {CODEC_ID_PCM_ALAW, 0x06},
49 {CODEC_ID_PCM_MULAW, 0x07},
50 {CODEC_ID_ADPCM_CT, 0x0200},
55 #ifdef CONFIG_DEMUXERS
57 static int voc_probe(AVProbeData *p)
63 if (memcmp(p->buf, voc_magic, sizeof(voc_magic) - 1))
65 version = p->buf[22] | (p->buf[23] << 8);
66 check = p->buf[24] | (p->buf[25] << 8);
67 if (~version + 0x1234 != check)
70 return AVPROBE_SCORE_MAX;
73 static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
75 voc_dec_context_t *voc = s->priv_data;
76 ByteIOContext *pb = &s->pb;
81 header_size = get_le16(pb) - 22;
82 if (header_size != 4) {
83 av_log(s, AV_LOG_ERROR, "unkown header size: %d\n", header_size);
84 return AVERROR_NOTSUPP;
86 url_fskip(pb, header_size);
87 st = av_new_stream(s, 0);
90 st->codec->codec_type = CODEC_TYPE_AUDIO;
92 voc->remaining_size = 0;
97 voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
99 voc_dec_context_t *voc = s->priv_data;
100 AVCodecContext *dec = st->codec;
101 ByteIOContext *pb = &s->pb;
107 while (!voc->remaining_size) {
109 if (type == VOC_TYPE_EOF)
111 voc->remaining_size = get_le24(pb);
115 case VOC_TYPE_VOICE_DATA:
116 dec->sample_rate = 1000000 / (256 - get_byte(pb));
118 dec->sample_rate = sample_rate;
119 dec->channels = channels;
120 dec->codec_id = codec_get_id(voc_codec_tags, get_byte(pb));
121 dec->bits_per_sample = av_get_bits_per_sample(dec->codec_id);
122 voc->remaining_size -= 2;
127 case VOC_TYPE_VOICE_DATA_CONT:
130 case VOC_TYPE_EXTENDED:
131 sample_rate = get_le16(pb);
133 channels = get_byte(pb) + 1;
134 sample_rate = 256000000 / (channels * (65536 - sample_rate));
135 voc->remaining_size = 0;
139 case VOC_TYPE_NEW_VOICE_DATA:
140 dec->sample_rate = get_le32(pb);
141 dec->bits_per_sample = get_byte(pb);
142 dec->channels = get_byte(pb);
143 dec->codec_id = codec_get_id(voc_codec_tags, get_le16(pb));
145 voc->remaining_size -= 12;
150 url_fskip(pb, voc->remaining_size);
151 max_size -= voc->remaining_size;
152 voc->remaining_size = 0;
157 dec->bit_rate = dec->sample_rate * dec->bits_per_sample;
160 max_size = voc_max_pkt_size;
161 size = FFMIN(voc->remaining_size, max_size);
162 voc->remaining_size -= size;
163 return av_get_packet(pb, pkt, size);
166 static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
168 return voc_get_packet(s, pkt, s->streams[0], 0);
171 static int voc_read_close(AVFormatContext *s)
176 static AVInputFormat voc_demuxer = {
178 "Creative Voice File format",
179 sizeof(voc_dec_context_t),
186 #endif /* CONFIG_DEMUXERS */
191 typedef struct voc_enc_context {
195 static int voc_write_header(AVFormatContext *s)
197 ByteIOContext *pb = &s->pb;
198 const int header_size = 26;
199 const int version = 0x0114;
201 if (s->nb_streams != 1
202 || s->streams[0]->codec->codec_type != CODEC_TYPE_AUDIO)
203 return AVERROR_NOTSUPP;
205 put_buffer(pb, voc_magic, sizeof(voc_magic) - 1);
206 put_le16(pb, header_size);
207 put_le16(pb, version);
208 put_le16(pb, ~version + 0x1234);
213 static int voc_write_packet(AVFormatContext *s, AVPacket *pkt)
215 voc_enc_context_t *voc = s->priv_data;
216 AVCodecContext *enc = s->streams[0]->codec;
217 ByteIOContext *pb = &s->pb;
219 if (!voc->param_written) {
220 int format = codec_get_tag(voc_codec_tags, enc->codec_id);
223 put_byte(pb, VOC_TYPE_NEW_VOICE_DATA);
224 put_le24(pb, pkt->size + 12);
225 put_le32(pb, enc->sample_rate);
226 put_byte(pb, enc->bits_per_sample);
227 put_byte(pb, enc->channels);
228 put_le16(pb, format);
231 if (s->streams[0]->codec->channels > 1) {
232 put_byte(pb, VOC_TYPE_EXTENDED);
234 put_le16(pb, 65536-256000000/(enc->sample_rate*enc->channels));
235 put_byte(pb, format);
236 put_byte(pb, enc->channels - 1);
238 put_byte(pb, VOC_TYPE_VOICE_DATA);
239 put_le24(pb, pkt->size + 2);
240 put_byte(pb, 256 - 1000000 / enc->sample_rate);
241 put_byte(pb, format);
243 voc->param_written = 1;
245 put_byte(pb, VOC_TYPE_VOICE_DATA_CONT);
246 put_le24(pb, pkt->size);
249 put_buffer(pb, pkt->data, pkt->size);
253 static int voc_write_trailer(AVFormatContext *s)
259 static AVOutputFormat voc_muxer = {
261 "Creative Voice File format",
264 sizeof(voc_enc_context_t),
272 #endif /* CONFIG_MUXERS */
277 #ifdef CONFIG_DEMUXERS
278 av_register_input_format(&voc_demuxer);
279 #endif /* CONFIG_DEMUXERS */
281 av_register_output_format(&voc_muxer);
282 #endif /* CONFIG_MUXERS */