2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file oggdec_filter.c Paraslash's ogg vorbis decoder. */
10 #include <vorbis/vorbisfile.h>
14 #include "oggdec_filter.cmdline.h"
18 #include "buffer_tree.h"
23 /** Determine byte sex. */
24 #ifdef WORDS_BIGENDIAN
30 /** Data specific to the oggdec filter. */
31 struct private_oggdec_data {
32 /** Describes an ogg vorbis file. */
34 /** The input buffer. */
36 /** The length of \a inbuf. */
38 /** The number of bytes consumed from the input buffer. */
40 /** When to start producing output. */
41 struct timeval stream_start;
42 /** The number of channels of the current stream. */
43 unsigned int channels;
44 /** Current sample rate in Hz. */
45 unsigned int samplerate;
49 static size_t cb_read_nobtr(void *buf, size_t size, size_t nmemb, void *datasource)
51 struct filter_node *fn = datasource;
52 struct private_oggdec_data *pod = fn->private_data;
53 size_t ret, have = pod->inbuf_len - pod->converted;
54 char *p = pod->inbuf + pod->converted;
56 // PARA_DEBUG_LOG("pod = %p\n", pod);
57 // PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
58 if (pod->inbuf_len < size) {
59 if (*fn->fc->input_error)
64 ret = PARA_MIN(nmemb, have / size) * size;
66 pod->converted += ret;
70 static size_t cb_read_btr(void *buf, size_t size, size_t nmemb, void *datasource)
72 struct filter_node *fn = datasource;
73 struct btr_node *btrn = fn->btrn;
77 * oggvorbis always uses size == 1. Other sizes would complicate the code
78 * for no real gain. So we simply don't support size != 1.
81 //PARA_DEBUG_LOG("vorbis requests %zu x %zu = %zu bytes\n", size, nmemb, size * nmemb);
85 size_t nbytes = btr_next_buffer(btrn, &btr_buf);
88 nbytes = PARA_MIN(nmemb - copied, nbytes);
89 memcpy(buf + copied, btr_buf, nbytes);
91 btr_consume(btrn, nbytes);
98 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
100 struct filter_node *fn = datasource;
103 return cb_read_btr(buf, size, nmemb, datasource);
105 return cb_read_nobtr(buf, size, nmemb, datasource);
109 * Custom data seeking function.
111 * Since we want the data source to be treated as unseekable at all
112 * times, the provided seek callback always returns -1 (failure).
114 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
115 __a_unused int whence)
120 static int cb_close(__a_unused void *datasource)
125 static const ov_callbacks ovc = {
126 .read_func = cb_read,
127 .seek_func = cb_seek,
128 .close_func = cb_close,
130 * The tell function need not be provided if the data IO abstraction is
136 static void ogg_open(struct filter_node *fn)
138 struct private_oggdec_data *pod = para_calloc(
139 sizeof(struct private_oggdec_data));
140 struct oggdec_filter_args_info *conf = fn->conf;
142 fn->private_data = pod;
143 fn->bufsize = conf->bufsize_arg * 1024;
144 fn->buf = para_malloc(fn->bufsize);
147 static void ogg_close(struct filter_node *fn)
149 struct private_oggdec_data *pod = fn->private_data;
151 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
156 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
159 free(fn->private_data);
160 fn->private_data = NULL;
163 #define OGGDEC_MAX_PENDING (640 * 1024)
164 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
167 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
169 struct filter_node *fn = btr_context(btrn);
170 struct private_oggdec_data *pod = fn->private_data;
172 if (!strcmp(cmd, "samplerate")) {
173 if (pod->samplerate == 0)
174 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
175 *result = make_message("%u", pod->samplerate);
178 if (!strcmp(cmd, "channels")) {
179 if (pod->channels == 0)
180 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
181 *result = make_message("%u", pod->channels);
184 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
187 static void ogg_pre_select(struct sched *s, struct task *t)
189 struct filter_node *fn = container_of(t, struct filter_node, task);
190 size_t iqs = btr_get_input_queue_size(fn->btrn);
195 if (btr_bytes_pending(fn->btrn) > OGGDEC_MAX_PENDING)
196 return; /* FIXME, should use reasonable bound on timeout */
197 s->timeout.tv_sec = 0;
198 s->timeout.tv_usec = 1;
201 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
203 struct filter_node *fn = container_of(t, struct filter_node, task);
204 struct private_oggdec_data *pod = fn->private_data;
205 struct btr_node *btrn = fn->btrn;
211 ret = prepare_filter_node(btrn, pod->min_iqs);
216 len = btr_next_buffer(btrn, &in);
217 iqs = btr_get_input_queue_size(btrn);
221 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
222 PARA_NOTICE_LOG("input queue: %zu, opening ov callbacks\n", iqs);
223 oret = ov_open_callbacks(fn, pod->vf,
224 NULL, /* no initial buffer */
225 0, /* no initial bytes */
226 ovc); /* the ov_open_callbacks */
227 if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
228 /* this might be due to the input buffer being too small */
229 if (!btr_no_parent(btrn)) {
232 pod->min_iqs = iqs + 1;
235 ret = (oret == OV_ENOTVORBIS)?
236 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
239 ret = -E_OGGDEC_READ;
240 if (oret == OV_EREAD)
242 ret = -E_OGGDEC_VERSION;
243 if (oret == OV_EVERSION)
245 ret = -E_OGGDEC_FAULT;
248 pod->channels = ov_info(pod->vf, 0)->channels;
249 pod->samplerate = ov_info(pod->vf, 0)->rate;
250 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
252 ///* wait a bit to avoid buffer underruns */
253 //tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
257 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
258 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
259 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
264 if (btr_no_parent(btrn))
268 if (read_ret == OV_HOLE)
271 ret = -E_OGGDEC_BADLINK;
274 btr_add_output(out, read_ret, btrn);
284 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
287 struct private_oggdec_data *pod = fn->private_data;
288 struct oggdec_filter_args_info *conf = fn->conf;
289 /* make the buffer known to the read callback cb_read() */
290 pod->inbuf = inbuffer;
291 pod->inbuf_len = len;
295 if (*fn->fc->input_error < 0)
296 return *fn->fc->input_error;
299 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
300 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
301 ret = ov_open_callbacks(fn, pod->vf,
302 NULL, /* no initial buffer */
303 0, /* no initial bytes */
304 ovc); /* the ov_open_callbacks */
305 if (ret == OV_ENOTVORBIS || ret == OV_EBADHEADER) {
306 /* this might be due to the input buffer being too small */
307 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
309 PARA_INFO_LOG("initial input buffer %zd/%d, "
310 "waiting for more data\n", len, ib);
315 return ret == OV_ENOTVORBIS?
316 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
319 return -E_OGGDEC_READ;
320 if (ret == OV_EVERSION)
321 return -E_OGGDEC_VERSION;
323 return -E_OGGDEC_FAULT;
324 fn->fc->channels = ov_info(pod->vf, 0)->channels;
325 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
326 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
328 /* wait a bit to avoid buffer underruns */
329 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
330 return pod->converted;
332 if (tv_diff(now, &pod->stream_start, NULL) < 0) {
333 PARA_DEBUG_LOG("initial delay..\n");
336 while (fn->loaded < fn->bufsize) {
337 int length = fn->bufsize - fn->loaded;
338 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
339 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
341 return pod->converted;
342 if (read_ret == OV_HOLE) {
344 PARA_INFO_LOG("hole, delaying playback\n");
345 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
347 return pod->converted;
350 return -E_OGGDEC_BADLINK;
351 fn->loaded += read_ret;
353 return pod->converted;
356 static int oggdec_parse_config(int argc, char **argv, void **config)
359 struct oggdec_filter_args_info *ogg_conf;
361 ogg_conf = para_calloc(sizeof(*ogg_conf));
362 ret = -E_OGGDEC_SYNTAX;
363 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
365 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
366 if (ogg_conf->bufsize_arg < 0)
368 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
370 if (ogg_conf->initial_buffer_arg < 0)
372 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
382 * The init function of the ogg vorbis decoder.
384 * \param f Its fields are filled in by the function.
386 void oggdec_filter_init(struct filter *f)
388 struct oggdec_filter_args_info dummy;
390 oggdec_cmdline_parser_init(&dummy);
392 f->close = ogg_close;
393 f->convert = ogg_convert;
394 f->pre_select = ogg_pre_select;
395 f->post_select = ogg_post_select;
396 f->parse_config = oggdec_parse_config;
397 f->execute = oggdec_execute;
398 f->help = (struct ggo_help) {
399 .short_help = oggdec_filter_args_info_help,
400 .detailed_help = oggdec_filter_args_info_detailed_help