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