RC4: Allocate 8 extra bytes for send buffer.
[paraslash.git] / oggdec_filter.c
1 /*
2 * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file oggdec_filter.c Paraslash's ogg vorbis decoder. */
8
9 #include <regex.h>
10 #include <vorbis/vorbisfile.h>
11 #include <stdbool.h>
12
13 #include "para.h"
14 #include "oggdec_filter.cmdline.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "buffer_tree.h"
19 #include "filter.h"
20 #include "error.h"
21 #include "string.h"
22
23 /** Determine byte sex. */
24 #ifdef WORDS_BIGENDIAN
25 #define ENDIAN 1
26 #else
27 #define ENDIAN 0
28 #endif
29
30 /** Data specific to the oggdec filter. */
31 struct private_oggdec_data {
32 /** Describes an ogg vorbis file. */
33 OggVorbis_File *vf;
34 /** The number of bytes consumed from the input buffer. */
35 size_t converted;
36 /** The number of channels of the current stream. */
37 unsigned int channels;
38 /** Current sample rate in Hz. */
39 unsigned int samplerate;
40 };
41
42 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
43 {
44 struct filter_node *fn = datasource;
45 struct private_oggdec_data *pod = fn->private_data;
46 struct btr_node *btrn = fn->btrn;
47 char *btr_buf;
48 size_t nbytes = btr_next_buffer(btrn, &btr_buf), tmp;
49
50 /**
51 * oggvorbis always uses size == 1. Other sizes would complicate the code
52 * for no real gain. So we simply don't support size != 1.
53 */
54 assert(size == 1);
55 assert(pod->converted <= nbytes);
56 tmp = nbytes - pod->converted;
57 PARA_DEBUG_LOG("vorbis requests %zu bytes have %zu\n", nmemb, tmp);
58 tmp = PARA_MIN(tmp, nmemb);
59 if (tmp == 0)
60 return 0;
61 memcpy(buf, btr_buf + pod->converted, tmp);
62 pod->converted += tmp;
63 return tmp;
64 }
65
66 /*
67 * Custom data seeking function.
68 *
69 * Since we want the data source to be treated as unseekable at all
70 * times, the provided seek callback always returns -1 (failure).
71 */
72 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
73 __a_unused int whence)
74 {
75 return -1;
76 }
77
78 static int cb_close(__a_unused void *datasource)
79 {
80 return 0;
81 }
82
83 static const ov_callbacks ovc = {
84 .read_func = cb_read,
85 .seek_func = cb_seek,
86 .close_func = cb_close,
87 /*
88 * The tell function need not be provided if the data IO abstraction is
89 * not seekable
90 */
91 .tell_func = NULL
92 };
93
94 static void ogg_open(struct filter_node *fn)
95 {
96 struct private_oggdec_data *pod = para_calloc(
97 sizeof(struct private_oggdec_data));
98
99 fn->private_data = pod;
100 fn->min_iqs = 8000;
101 }
102
103 static void ogg_close(struct filter_node *fn)
104 {
105 struct private_oggdec_data *pod = fn->private_data;
106 if (pod->vf) {
107 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
108 ov_clear(pod->vf);
109 free(pod->vf);
110 pod->vf = NULL;
111 } else
112 PARA_DEBUG_LOG("nothing to close\n");
113 free(fn->private_data);
114 fn->private_data = NULL;
115 }
116
117 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
118
119 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
120 {
121 struct filter_node *fn = btr_context(btrn);
122 struct private_oggdec_data *pod = fn->private_data;
123
124 if (!strcmp(cmd, "samplerate")) {
125 if (pod->samplerate == 0)
126 return -E_BTR_NAVAIL;
127 *result = make_message("%u", pod->samplerate);
128 return 1;
129 }
130 if (!strcmp(cmd, "channels")) {
131 if (pod->channels == 0)
132 return -E_BTR_NAVAIL;
133 *result = make_message("%u", pod->channels);
134 return 1;
135 }
136 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
137 }
138
139 static int ogg_init(struct filter_node *fn)
140 {
141 struct private_oggdec_data *pod = fn->private_data;
142 struct btr_node *btrn = fn->btrn;
143 int ret, oret;
144 size_t iqs;
145
146 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
147 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
148 btr_get_input_queue_size(btrn), fn->min_iqs);
149 open:
150 oret = ov_open_callbacks(fn, pod->vf,
151 NULL, /* no initial buffer */
152 0, /* no initial bytes */
153 ovc); /* the ov_open_callbacks */
154 if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
155 /* this might be due to the input buffer being too small */
156 if (!btr_no_parent(btrn)) {
157 fn->min_iqs += 1000;
158 iqs = btr_get_input_queue_size(btrn);
159 ret = 0;
160 if (iqs < fn->min_iqs)
161 goto out;
162 PARA_CRIT_LOG("iqs: %zu\n", iqs);
163 btr_merge(btrn, fn->min_iqs);
164 pod->converted = 0;
165 goto open;
166 }
167 ret = (oret == OV_ENOTVORBIS)?
168 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
169 goto out;
170 }
171 ret = -E_OGGDEC_READ;
172 if (oret == OV_EREAD)
173 goto out;
174 ret = -E_OGGDEC_VERSION;
175 if (oret == OV_EVERSION)
176 goto out;
177 ret = -E_OGGDEC_FAULT;
178 if (oret < 0)
179 goto out;
180 pod->channels = ov_info(pod->vf, 0)->channels;
181 pod->samplerate = ov_info(pod->vf, 0)->rate;
182 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
183 pod->samplerate);
184 ret = 1;
185 out:
186 if (ret <= 0) {
187 free(pod->vf);
188 pod->vf = NULL;
189 } else {
190 btr_consume(btrn, pod->converted);
191 pod->converted = 0;
192 fn->min_iqs = 0;
193 }
194 return ret;
195 }
196
197 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
198 {
199 struct filter_node *fn = container_of(t, struct filter_node, task);
200 struct private_oggdec_data *pod = fn->private_data;
201 struct btr_node *btrn = fn->btrn;
202 int ret, ns;
203
204 pod->converted = 0;
205 t->error = 0;
206 ret = ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
207 if (!pod->vf) {
208 if (ret <= 0)
209 goto out;
210 btr_merge(btrn, fn->min_iqs);
211 ret = ogg_init(fn);
212 if (ret <= 0)
213 goto out;
214 }
215 for (;;) {
216 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
217 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
218 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
219 btr_consume(btrn, pod->converted);
220 pod->converted = 0;
221 if (read_ret <= 0)
222 free(out);
223 ret = ns;
224 if (read_ret == 0 || read_ret == OV_HOLE)
225 goto out;
226 ret = -E_OGGDEC_BADLINK;
227 if (read_ret < 0)
228 goto out;
229 btr_add_output(out, read_ret, btrn);
230 if (btr_get_output_queue_size(btrn) > 640 * 1024)
231 return; /* enough data for the moment */
232 }
233 out:
234 if (ret < 0) {
235 t->error = ret;
236 btr_remove_node(btrn);
237 }
238 }
239
240 static int oggdec_parse_config(int argc, char **argv, void **config)
241 {
242 int ret;
243 struct oggdec_filter_args_info *ogg_conf;
244
245 ogg_conf = para_calloc(sizeof(*ogg_conf));
246 ret = -E_OGGDEC_SYNTAX;
247 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
248 goto err;
249 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
250 if (ogg_conf->bufsize_arg < 0)
251 goto err;
252 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
253 goto err;
254 if (ogg_conf->initial_buffer_arg < 0)
255 goto err;
256 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
257 goto err;
258 *config = ogg_conf;
259 return 1;
260 err:
261 free(ogg_conf);
262 return ret;
263 }
264
265 static void oggdec_free_config(void *conf)
266 {
267 oggdec_cmdline_parser_free(conf);
268 }
269
270 /**
271 * The init function of the ogg vorbis decoder.
272 *
273 * \param f Its fields are filled in by the function.
274 */
275 void oggdec_filter_init(struct filter *f)
276 {
277 struct oggdec_filter_args_info dummy;
278
279 oggdec_cmdline_parser_init(&dummy);
280 f->open = ogg_open;
281 f->close = ogg_close;
282 f->pre_select = generic_filter_pre_select;
283 f->post_select = ogg_post_select;
284 f->parse_config = oggdec_parse_config;
285 f->free_config = oggdec_free_config;
286 f->execute = oggdec_execute;
287 f->help = (struct ggo_help) {
288 .short_help = oggdec_filter_args_info_help,
289 .detailed_help = oggdec_filter_args_info_detailed_help
290 };
291 }