]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/voc.c
give AVInput/OutputFormat structs consistent names
[frescor/ffmpeg.git] / libavformat / voc.c
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_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},
51     {0, 0},
52 };
53
54
55 #ifdef CONFIG_DEMUXERS
56
57 static int voc_probe(AVProbeData *p)
58 {
59     int version, check;
60
61     if (p->buf_size < 26)
62         return 0;
63     if (memcmp(p->buf, voc_magic, sizeof(voc_magic) - 1))
64         return 0;
65     version = p->buf[22] | (p->buf[23] << 8);
66     check = p->buf[24] | (p->buf[25] << 8);
67     if (~version + 0x1234 != check)
68         return 10;
69
70     return AVPROBE_SCORE_MAX;
71 }
72
73 static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
74 {
75     voc_dec_context_t *voc = s->priv_data;
76     ByteIOContext *pb = &s->pb;
77     int header_size;
78     AVStream *st;
79
80     url_fskip(pb, 20);
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;
85     }
86     url_fskip(pb, header_size);
87     st = av_new_stream(s, 0);
88     if (!st)
89         return AVERROR_NOMEM;
90     st->codec->codec_type = CODEC_TYPE_AUDIO;
91
92     voc->remaining_size = 0;
93     return 0;
94 }
95
96 int
97 voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
98 {
99     voc_dec_context_t *voc = s->priv_data;
100     AVCodecContext *dec = st->codec;
101     ByteIOContext *pb = &s->pb;
102     voc_type_t type;
103     int size;
104     int sample_rate = 0;
105     int channels = 1;
106
107     while (!voc->remaining_size) {
108         type = get_byte(pb);
109         if (type == VOC_TYPE_EOF)
110             return AVERROR_IO;
111         voc->remaining_size = get_le24(pb);
112         max_size -= 4;
113
114         switch (type) {
115         case VOC_TYPE_VOICE_DATA:
116             dec->sample_rate = 1000000 / (256 - get_byte(pb));
117             if (sample_rate)
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;
123             max_size -= 2;
124             channels = 1;
125             break;
126
127         case VOC_TYPE_VOICE_DATA_CONT:
128             break;
129
130         case VOC_TYPE_EXTENDED:
131             sample_rate = get_le16(pb);
132             get_byte(pb);
133             channels = get_byte(pb) + 1;
134             sample_rate = 256000000 / (channels * (65536 - sample_rate));
135             voc->remaining_size = 0;
136             max_size -= 4;
137             break;
138
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));
144             url_fskip(pb, 4);
145             voc->remaining_size -= 12;
146             max_size -= 12;
147             break;
148
149         default:
150             url_fskip(pb, voc->remaining_size);
151             max_size -= voc->remaining_size;
152             voc->remaining_size = 0;
153             break;
154         }
155     }
156
157     dec->bit_rate = dec->sample_rate * dec->bits_per_sample;
158
159     if (max_size <= 0)
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);
164 }
165
166 static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
167 {
168     return voc_get_packet(s, pkt, s->streams[0], 0);
169 }
170
171 static int voc_read_close(AVFormatContext *s)
172 {
173     return 0;
174 }
175
176 static AVInputFormat voc_demuxer = {
177     "voc",
178     "Creative Voice File format",
179     sizeof(voc_dec_context_t),
180     voc_probe,
181     voc_read_header,
182     voc_read_packet,
183     voc_read_close,
184 };
185
186 #endif /* CONFIG_DEMUXERS */
187
188
189 #ifdef CONFIG_MUXERS
190
191 typedef struct voc_enc_context {
192     int param_written;
193 } voc_enc_context_t;
194
195 static int voc_write_header(AVFormatContext *s)
196 {
197     ByteIOContext *pb = &s->pb;
198     const int header_size = 26;
199     const int version = 0x0114;
200
201     if (s->nb_streams != 1
202         || s->streams[0]->codec->codec_type != CODEC_TYPE_AUDIO)
203         return AVERROR_NOTSUPP;
204
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);
209
210     return 0;
211 }
212
213 static int voc_write_packet(AVFormatContext *s, AVPacket *pkt)
214 {
215     voc_enc_context_t *voc = s->priv_data;
216     AVCodecContext *enc = s->streams[0]->codec;
217     ByteIOContext *pb = &s->pb;
218
219     if (!voc->param_written) {
220         int format = codec_get_tag(voc_codec_tags, enc->codec_id);
221
222         if (format > 0xFF) {
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);
229             put_le32(pb, 0);
230         } else {
231             if (s->streams[0]->codec->channels > 1) {
232                 put_byte(pb, VOC_TYPE_EXTENDED);
233                 put_le24(pb, 4);
234                 put_le16(pb, 65536-256000000/(enc->sample_rate*enc->channels));
235                 put_byte(pb, format);
236                 put_byte(pb, enc->channels - 1);
237             }
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);
242         }
243         voc->param_written = 1;
244     } else {
245         put_byte(pb, VOC_TYPE_VOICE_DATA_CONT);
246         put_le24(pb, pkt->size);
247     }
248
249     put_buffer(pb, pkt->data, pkt->size);
250     return 0;
251 }
252
253 static int voc_write_trailer(AVFormatContext *s)
254 {
255     put_byte(&s->pb, 0);
256     return 0;
257 }
258
259 static AVOutputFormat voc_muxer = {
260     "voc",
261     "Creative Voice File format",
262     "audio/x-voc",
263     "voc",
264     sizeof(voc_enc_context_t),
265     CODEC_ID_PCM_U8,
266     CODEC_ID_NONE,
267     voc_write_header,
268     voc_write_packet,
269     voc_write_trailer,
270 };
271
272 #endif /* CONFIG_MUXERS */
273
274
275 int voc_init(void)
276 {
277 #ifdef CONFIG_DEMUXERS
278     av_register_input_format(&voc_demuxer);
279 #endif /* CONFIG_DEMUXERS */
280 #ifdef CONFIG_MUXERS
281     av_register_output_format(&voc_muxer);
282 #endif /* CONFIG_MUXERS */
283     return 0;
284 }