2 * Copyright (C) 2005-2010 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>
17 #include "buffer_tree.h"
22 /** Determine byte sex. */
23 #ifdef WORDS_BIGENDIAN
29 /** Data specific to the oggdec filter. */
30 struct private_oggdec_data {
31 /** Describes an ogg vorbis file. */
33 /** The number of bytes consumed from the input buffer. */
35 /** The number of channels of the current stream. */
36 unsigned int channels;
37 /** Current sample rate in Hz. */
38 unsigned int samplerate;
41 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
43 struct filter_node *fn = datasource;
44 struct private_oggdec_data *pod = fn->private_data;
45 struct btr_node *btrn = fn->btrn;
47 size_t nbytes = btr_next_buffer(btrn, &btr_buf), tmp;
50 * oggvorbis always uses size == 1. Other sizes would complicate the code
51 * for no real gain. So we simply don't support size != 1.
54 assert(pod->converted <= nbytes);
55 tmp = nbytes - pod->converted;
56 PARA_DEBUG_LOG("vorbis requests %zu bytes have %zu\n", nmemb, tmp);
57 tmp = PARA_MIN(tmp, nmemb);
60 memcpy(buf, btr_buf + pod->converted, tmp);
61 pod->converted += tmp;
66 * Custom data seeking function.
68 * Since we want the data source to be treated as unseekable at all
69 * times, the provided seek callback always returns -1 (failure).
71 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
72 __a_unused int whence)
77 static int cb_close(__a_unused void *datasource)
82 static const ov_callbacks ovc = {
85 .close_func = cb_close,
87 * The tell function need not be provided if the data IO abstraction is
93 static void ogg_open(struct filter_node *fn)
95 struct private_oggdec_data *pod = para_calloc(
96 sizeof(struct private_oggdec_data));
98 fn->private_data = pod;
102 static void ogg_close(struct filter_node *fn)
104 struct private_oggdec_data *pod = fn->private_data;
106 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
111 PARA_DEBUG_LOG("nothing to close\n");
112 free(fn->private_data);
113 fn->private_data = NULL;
116 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
118 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
120 struct filter_node *fn = btr_context(btrn);
121 struct private_oggdec_data *pod = fn->private_data;
123 if (!strcmp(cmd, "samplerate")) {
124 if (pod->samplerate == 0)
125 return -E_BTR_NAVAIL;
126 *result = make_message("%u", pod->samplerate);
129 if (!strcmp(cmd, "channels")) {
130 if (pod->channels == 0)
131 return -E_BTR_NAVAIL;
132 *result = make_message("%u", pod->channels);
135 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
138 static int ogg_init(struct filter_node *fn)
140 struct private_oggdec_data *pod = fn->private_data;
141 struct btr_node *btrn = fn->btrn;
145 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
146 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
147 btr_get_input_queue_size(btrn), fn->min_iqs);
149 oret = ov_open_callbacks(fn, pod->vf,
150 NULL, /* no initial buffer */
151 0, /* no initial bytes */
152 ovc); /* the ov_open_callbacks */
153 if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
154 /* this might be due to the input buffer being too small */
155 if (!btr_no_parent(btrn)) {
157 iqs = btr_get_input_queue_size(btrn);
159 if (iqs < fn->min_iqs)
161 PARA_CRIT_LOG("iqs: %zu\n", iqs);
162 btr_merge(btrn, fn->min_iqs);
166 ret = (oret == OV_ENOTVORBIS)?
167 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
170 ret = -E_OGGDEC_READ;
171 if (oret == OV_EREAD)
173 ret = -E_OGGDEC_VERSION;
174 if (oret == OV_EVERSION)
176 ret = -E_OGGDEC_FAULT;
179 pod->channels = ov_info(pod->vf, 0)->channels;
180 pod->samplerate = ov_info(pod->vf, 0)->rate;
181 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
189 btr_consume(btrn, pod->converted);
196 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
198 struct filter_node *fn = container_of(t, struct filter_node, task);
199 struct private_oggdec_data *pod = fn->private_data;
200 struct btr_node *btrn = fn->btrn;
205 ret = ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
209 btr_merge(btrn, fn->min_iqs);
215 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
216 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
217 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
218 btr_consume(btrn, pod->converted);
223 if (read_ret == 0 || read_ret == OV_HOLE)
225 ret = -E_OGGDEC_BADLINK;
228 btr_add_output(out, read_ret, btrn);
229 if (btr_get_output_queue_size(btrn) > 640 * 1024)
230 return; /* enough data for the moment */
235 btr_remove_node(btrn);
240 * The init function of the ogg vorbis decoder.
242 * \param f Its fields are filled in by the function.
244 void oggdec_filter_init(struct filter *f)
247 f->close = ogg_close;
248 f->pre_select = generic_filter_pre_select;
249 f->post_select = ogg_post_select;
250 f->execute = oggdec_execute;