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_PCM_S16LE, 0x04},
45 {CODEC_ID_PCM_ALAW, 0x06},
46 {CODEC_ID_PCM_MULAW, 0x07},
47 {CODEC_ID_ADPCM_CT, 0x0200},
52 #ifdef CONFIG_DEMUXERS
54 static int voc_probe(AVProbeData *p)
60 if (memcmp(p->buf, voc_magic, sizeof(voc_magic) - 1))
62 version = p->buf[22] | (p->buf[23] << 8);
63 check = p->buf[24] | (p->buf[25] << 8);
64 if (~version + 0x1234 != check)
67 return AVPROBE_SCORE_MAX;
70 static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
72 voc_dec_context_t *voc = s->priv_data;
73 ByteIOContext *pb = &s->pb;
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;
83 url_fskip(pb, header_size);
84 st = av_new_stream(s, 0);
87 st->codec->codec_type = CODEC_TYPE_AUDIO;
89 voc->remaining_size = 0;
94 voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
96 voc_dec_context_t *voc = s->priv_data;
97 AVCodecContext *dec = st->codec;
98 ByteIOContext *pb = &s->pb;
104 while (!voc->remaining_size) {
106 if (type == VOC_TYPE_EOF)
108 voc->remaining_size = get_le24(pb);
112 case VOC_TYPE_VOICE_DATA:
113 dec->sample_rate = 1000000 / (256 - get_byte(pb));
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;
123 case VOC_TYPE_VOICE_DATA_CONT:
126 case VOC_TYPE_EXTENDED:
127 sample_rate = get_le16(pb);
129 channels = get_byte(pb) + 1;
130 sample_rate = 256000000 / (channels * (65536 - sample_rate));
131 voc->remaining_size = 0;
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));
141 voc->remaining_size -= 12;
146 url_fskip(pb, voc->remaining_size);
147 max_size -= voc->remaining_size;
148 voc->remaining_size = 0;
153 dec->bit_rate = dec->sample_rate * dec->bits_per_sample;
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);
162 static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
164 return voc_get_packet(s, pkt, s->streams[0], 0);
167 static int voc_read_close(AVFormatContext *s)
172 static AVInputFormat voc_iformat = {
174 "Creative Voice File format",
175 sizeof(voc_dec_context_t),
182 #endif /* CONFIG_DEMUXERS */
187 typedef struct voc_enc_context {
191 static int voc_write_header(AVFormatContext *s)
193 ByteIOContext *pb = &s->pb;
194 const int header_size = 26;
195 const int version = 0x0114;
197 if (s->nb_streams != 1
198 || s->streams[0]->codec->codec_type != CODEC_TYPE_AUDIO)
199 return AVERROR_NOTSUPP;
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);
209 static int voc_write_packet(AVFormatContext *s, AVPacket *pkt)
211 voc_enc_context_t *voc = s->priv_data;
212 AVCodecContext *enc = s->streams[0]->codec;
213 ByteIOContext *pb = &s->pb;
215 if (!voc->param_written) {
216 int format = codec_get_tag(voc_codec_tags, enc->codec_id);
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);
227 if (s->streams[0]->codec->channels > 1) {
228 put_byte(pb, VOC_TYPE_EXTENDED);
230 put_le16(pb, 65536-256000000/(enc->sample_rate*enc->channels));
231 put_byte(pb, format);
232 put_byte(pb, enc->channels - 1);
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);
239 voc->param_written = 1;
241 put_byte(pb, VOC_TYPE_VOICE_DATA_CONT);
242 put_le24(pb, pkt->size);
245 put_buffer(pb, pkt->data, pkt->size);
249 static int voc_write_trailer(AVFormatContext *s)
255 static AVOutputFormat voc_oformat = {
257 "Creative Voice File format",
260 sizeof(voc_enc_context_t),
268 #endif /* CONFIG_MUXERS */
273 #ifdef CONFIG_DEMUXERS
274 av_register_input_format(&voc_iformat);
275 #endif /* CONFIG_DEMUXERS */
277 av_register_output_format(&voc_oformat);
278 #endif /* CONFIG_MUXERS */