2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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>
16 #include "buffer_tree.h"
21 /** Determine byte sex. */
22 #ifdef WORDS_BIGENDIAN
28 /** Data specific to the oggdec filter. */
29 struct private_oggdec_data {
30 /** Describes an ogg vorbis file. */
32 /** The number of bytes consumed from the input buffer. */
34 /** The number of channels of the current stream. */
35 unsigned int channels;
36 /** Current sample rate in Hz. */
37 unsigned int sample_rate;
38 /** Whether everything was decoded during the previous iteration. */
42 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
44 struct filter_node *fn = datasource;
45 struct private_oggdec_data *pod = fn->private_data;
46 struct btr_node *btrn = fn->btrn;
48 size_t nbytes = btr_next_buffer(btrn, &btr_buf), tmp;
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.
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);
61 memcpy(buf, btr_buf + pod->converted, tmp);
62 pod->converted += tmp;
67 * Custom data seeking function.
69 * Since we want the data source to be treated as unseekable at all
70 * times, the provided seek callback always returns -1 (failure).
72 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
73 __a_unused int whence)
78 static int cb_close(__a_unused void *datasource)
83 static const ov_callbacks ovc = {
86 .close_func = cb_close,
88 * The tell function need not be provided if the data IO abstraction is
94 static void ogg_open(struct filter_node *fn)
96 fn->private_data = para_calloc(sizeof(struct private_oggdec_data));
100 static void ogg_close(struct filter_node *fn)
102 struct private_oggdec_data *pod = fn->private_data;
104 if (pod && pod->vf) {
105 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
110 PARA_DEBUG_LOG("nothing to close\n");
112 fn->private_data = NULL;
115 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
117 struct filter_node *fn = btr_context(btrn);
118 struct private_oggdec_data *pod = fn->private_data;
120 return decoder_execute(cmd, pod->sample_rate, pod->channels, result);
123 static int ogg_init(struct filter_node *fn)
125 struct private_oggdec_data *pod = fn->private_data;
126 struct btr_node *btrn = fn->btrn;
129 struct OggVorbis_File *vf = para_malloc(sizeof(*vf));
131 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
132 btr_get_input_queue_size(btrn), fn->min_iqs);
134 oret = ov_open_callbacks(fn, vf,
135 NULL, /* no initial buffer */
136 0, /* no initial bytes */
137 ovc); /* the ov_open_callbacks */
138 if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
139 /* maybe the input buffer is too small */
140 if (!btr_no_parent(btrn)) {
142 iqs = btr_get_input_queue_size(btrn);
144 if (iqs < fn->min_iqs)
146 btr_merge(btrn, fn->min_iqs);
150 ret = (oret == OV_ENOTVORBIS)?
151 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
154 ret = -E_OGGDEC_READ;
155 if (oret == OV_EREAD)
157 ret = -E_OGGDEC_VERSION;
158 if (oret == OV_EVERSION)
160 ret = -E_OGGDEC_FAULT;
163 pod->channels = ov_info(vf, 0)->channels;
164 pod->sample_rate = ov_info(vf, 0)->rate;
165 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
172 btr_consume(btrn, pod->converted);
176 pod->have_more = true;
181 #define OGGDEC_MAX_OUTPUT_SIZE (96 * 1024)
182 #define OGGDEC_OUTPUT_CHUNK_SIZE (32 * 1024)
184 static void ogg_pre_select(struct sched *s, void *context)
186 struct filter_node *fn = context;
187 struct private_oggdec_data *pod = fn->private_data;
188 struct btr_node *btrn = fn->btrn;
191 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
193 return sched_min_delay(s);
196 if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
201 static int ogg_post_select(__a_unused struct sched *s, void *context)
203 struct filter_node *fn = context;
204 struct private_oggdec_data *pod = fn->private_data;
205 struct btr_node *btrn = fn->btrn;
209 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
211 if (ret != -E_BTR_EOF) /* fatal error */
213 if (fn->min_iqs == 0 && !pod->have_more) /* EOF */
215 /* last ov_read() returned OV_HOLE */
216 } else if (ret == 0 && !pod->have_more) /* nothing to do */
218 if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
223 btr_merge(btrn, fn->min_iqs);
228 buf = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
230 ret = ov_read(pod->vf, buf + have, OGGDEC_OUTPUT_CHUNK_SIZE - have,
231 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
232 btr_consume(btrn, pod->converted);
238 if (have >= OGGDEC_OUTPUT_CHUNK_SIZE)
241 pod->have_more = (ret > 0);
243 if (have < OGGDEC_OUTPUT_CHUNK_SIZE)
244 buf = para_realloc(buf, have);
245 btr_add_output(buf, have, btrn);
248 if (ret == OV_HOLE) /* avoid buffer underruns */
250 if (ret >= 0 || ret == OV_HOLE)
252 ret = -E_OGGDEC_BADLINK;
255 btr_remove_node(&fn->btrn);
260 * The init function of the ogg vorbis decoder.
262 * \param f Its fields are filled in by the function.
264 void oggdec_filter_init(struct filter *f)
267 f->close = ogg_close;
268 f->pre_select = ogg_pre_select;
269 f->post_select = ogg_post_select;
270 f->execute = oggdec_execute;