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;
48 static size_t cb_read_nobtr(void *buf, size_t size, size_t nmemb, void *datasource)
50 struct filter_node *fn = datasource;
51 struct private_oggdec_data *pod = fn->private_data;
52 size_t ret, have = pod->inbuf_len - pod->converted;
53 char *p = pod->inbuf + pod->converted;
55 // PARA_DEBUG_LOG("pod = %p\n", pod);
56 // PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
57 if (pod->inbuf_len < size) {
58 if (*fn->fc->input_error)
63 ret = PARA_MIN(nmemb, have / size) * size;
65 pod->converted += ret;
69 static size_t cb_read_btr(void *buf, size_t size, size_t nmemb, void *datasource)
71 struct filter_node *fn = datasource;
72 struct private_oggdec_data *pod = fn->private_data;
73 struct btr_node *btrn = fn->btrn;
75 size_t nbytes = btr_next_buffer(btrn, &btr_buf), tmp;
78 * oggvorbis always uses size == 1. Other sizes would complicate the code
79 * for no real gain. So we simply don't support size != 1.
82 assert(pod->converted <= nbytes);
83 tmp = nbytes - pod->converted;
84 PARA_DEBUG_LOG("vorbis requests %zu bytes have %zu\n", nmemb, tmp);
85 tmp = PARA_MIN(tmp, nmemb);
88 memcpy(buf, btr_buf + pod->converted, tmp);
89 pod->converted += tmp;
93 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
95 struct filter_node *fn = datasource;
98 return cb_read_btr(buf, size, nmemb, datasource);
100 return cb_read_nobtr(buf, size, nmemb, datasource);
104 * Custom data seeking function.
106 * Since we want the data source to be treated as unseekable at all
107 * times, the provided seek callback always returns -1 (failure).
109 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
110 __a_unused int whence)
115 static int cb_close(__a_unused void *datasource)
120 static const ov_callbacks ovc = {
121 .read_func = cb_read,
122 .seek_func = cb_seek,
123 .close_func = cb_close,
125 * The tell function need not be provided if the data IO abstraction is
131 static void ogg_open(struct filter_node *fn)
133 struct private_oggdec_data *pod = para_calloc(
134 sizeof(struct private_oggdec_data));
135 struct oggdec_filter_args_info *conf = fn->conf;
137 fn->private_data = pod;
138 fn->bufsize = conf->bufsize_arg * 1024;
139 fn->buf = para_malloc(fn->bufsize);
143 static void ogg_close(struct filter_node *fn)
145 struct private_oggdec_data *pod = fn->private_data;
147 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
152 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
155 free(fn->private_data);
156 fn->private_data = NULL;
159 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
161 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
163 struct filter_node *fn = btr_context(btrn);
164 struct private_oggdec_data *pod = fn->private_data;
166 if (!strcmp(cmd, "samplerate")) {
167 if (pod->samplerate == 0)
168 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
169 *result = make_message("%u", pod->samplerate);
172 if (!strcmp(cmd, "channels")) {
173 if (pod->channels == 0)
174 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
175 *result = make_message("%u", pod->channels);
178 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
181 static int ogg_init(struct filter_node *fn)
183 struct private_oggdec_data *pod = fn->private_data;
184 struct btr_node *btrn = fn->btrn;
187 struct timeval delay = {0, 500 * 1000};
189 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
190 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
191 btr_get_input_queue_size(btrn), fn->min_iqs);
193 oret = ov_open_callbacks(fn, pod->vf,
194 NULL, /* no initial buffer */
195 0, /* no initial bytes */
196 ovc); /* the ov_open_callbacks */
197 if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
198 /* this might be due to the input buffer being too small */
199 if (!btr_no_parent(btrn)) {
201 iqs = btr_get_input_queue_size(btrn);
203 if (iqs < fn->min_iqs)
205 PARA_CRIT_LOG("iqs: %zu\n", iqs);
206 btr_merge(btrn, fn->min_iqs);
210 ret = (oret == OV_ENOTVORBIS)?
211 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
214 ret = -E_OGGDEC_READ;
215 if (oret == OV_EREAD)
217 ret = -E_OGGDEC_VERSION;
218 if (oret == OV_EVERSION)
220 ret = -E_OGGDEC_FAULT;
223 pod->channels = ov_info(pod->vf, 0)->channels;
224 pod->samplerate = ov_info(pod->vf, 0)->rate;
225 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
227 /* wait a bit to avoid buffer underruns */
228 tv_add(now, &delay, &pod->stream_start);
235 btr_consume(btrn, pod->converted);
242 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
244 struct filter_node *fn = container_of(t, struct filter_node, task);
245 struct private_oggdec_data *pod = fn->private_data;
246 struct btr_node *btrn = fn->btrn;
253 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
256 btr_merge(btrn, fn->min_iqs);
257 len = btr_next_buffer(btrn, &in);
258 iqs = btr_get_input_queue_size(btrn);
265 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
266 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
267 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
268 btr_consume(btrn, pod->converted);
273 if (btr_no_parent(btrn))
280 if (read_ret == OV_HOLE)
282 ret = -E_OGGDEC_BADLINK;
285 btr_add_output(out, read_ret, btrn);
286 if (btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL) == 0)
287 return; /* enough data for the moment */
292 btr_remove_node(btrn);
296 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
299 struct private_oggdec_data *pod = fn->private_data;
300 struct oggdec_filter_args_info *conf = fn->conf;
301 /* make the buffer known to the read callback cb_read() */
302 pod->inbuf = inbuffer;
303 pod->inbuf_len = len;
307 if (*fn->fc->input_error < 0)
308 return *fn->fc->input_error;
311 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
312 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
313 ret = ov_open_callbacks(fn, pod->vf,
314 NULL, /* no initial buffer */
315 0, /* no initial bytes */
316 ovc); /* the ov_open_callbacks */
317 if (ret == OV_ENOTVORBIS || ret == OV_EBADHEADER) {
318 /* this might be due to the input buffer being too small */
319 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
321 PARA_INFO_LOG("initial input buffer %zd/%d, "
322 "waiting for more data\n", len, ib);
327 return ret == OV_ENOTVORBIS?
328 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
331 return -E_OGGDEC_READ;
332 if (ret == OV_EVERSION)
333 return -E_OGGDEC_VERSION;
335 return -E_OGGDEC_FAULT;
336 fn->fc->channels = ov_info(pod->vf, 0)->channels;
337 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
338 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
340 /* wait a bit to avoid buffer underruns */
341 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
342 return pod->converted;
344 if (tv_diff(now, &pod->stream_start, NULL) < 0) {
345 PARA_DEBUG_LOG("initial delay..\n");
348 while (fn->loaded < fn->bufsize) {
349 int length = fn->bufsize - fn->loaded;
350 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
351 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
353 return pod->converted;
354 if (read_ret == OV_HOLE) {
356 PARA_INFO_LOG("hole, delaying playback\n");
357 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
359 return pod->converted;
362 return -E_OGGDEC_BADLINK;
363 fn->loaded += read_ret;
365 return pod->converted;
368 static int oggdec_parse_config(int argc, char **argv, void **config)
371 struct oggdec_filter_args_info *ogg_conf;
373 ogg_conf = para_calloc(sizeof(*ogg_conf));
374 ret = -E_OGGDEC_SYNTAX;
375 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
377 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
378 if (ogg_conf->bufsize_arg < 0)
380 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
382 if (ogg_conf->initial_buffer_arg < 0)
384 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
393 static void oggdec_free_config(void *conf)
395 oggdec_cmdline_parser_free(conf);
399 * The init function of the ogg vorbis decoder.
401 * \param f Its fields are filled in by the function.
403 void oggdec_filter_init(struct filter *f)
405 struct oggdec_filter_args_info dummy;
407 oggdec_cmdline_parser_init(&dummy);
409 f->close = ogg_close;
410 f->convert = ogg_convert;
411 f->pre_select = generic_filter_pre_select;
412 f->post_select = ogg_post_select;
413 f->parse_config = oggdec_parse_config;
414 f->free_config = oggdec_free_config;
415 f->execute = oggdec_execute;
416 f->help = (struct ggo_help) {
417 .short_help = oggdec_filter_args_info_help,
418 .detailed_help = oggdec_filter_args_info_detailed_help