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>
15 #include "buffer_tree.h"
20 /** Determine byte sex. */
21 #ifdef WORDS_BIGENDIAN
27 /** Data specific to the oggdec filter. */
28 struct private_oggdec_data {
29 /** Describes an ogg vorbis file. */
31 /** The number of bytes consumed from the input buffer. */
33 /** The number of channels of the current stream. */
34 unsigned int channels;
35 /** Current sample rate in Hz. */
36 unsigned int sample_rate;
37 /** Whether everything was decoded during the previous iteration. */
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 fn->private_data = para_calloc(sizeof(struct private_oggdec_data));
99 static void ogg_close(struct filter_node *fn)
101 struct private_oggdec_data *pod = fn->private_data;
103 if (pod && pod->vf) {
104 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
109 PARA_DEBUG_LOG("nothing to close\n");
111 fn->private_data = NULL;
114 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
116 struct filter_node *fn = btr_context(btrn);
117 struct private_oggdec_data *pod = fn->private_data;
119 return decoder_execute(cmd, pod->sample_rate, pod->channels, result);
122 static int ogg_init(struct filter_node *fn)
124 struct private_oggdec_data *pod = fn->private_data;
125 struct btr_node *btrn = fn->btrn;
128 struct OggVorbis_File *vf = para_malloc(sizeof(*vf));
130 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
131 btr_get_input_queue_size(btrn), fn->min_iqs);
133 oret = ov_open_callbacks(fn, vf,
134 NULL, /* no initial buffer */
135 0, /* no initial bytes */
136 ovc); /* the ov_open_callbacks */
137 if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
138 /* maybe the input buffer is too small */
139 if (!btr_no_parent(btrn)) {
141 iqs = btr_get_input_queue_size(btrn);
143 if (iqs < fn->min_iqs)
145 btr_merge(btrn, fn->min_iqs);
149 ret = (oret == OV_ENOTVORBIS)?
150 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
153 ret = -E_OGGDEC_READ;
154 if (oret == OV_EREAD)
156 ret = -E_OGGDEC_VERSION;
157 if (oret == OV_EVERSION)
159 ret = -E_OGGDEC_FAULT;
162 pod->channels = ov_info(vf, 0)->channels;
163 pod->sample_rate = ov_info(vf, 0)->rate;
164 PARA_NOTICE_LOG("%u channels, %u Hz\n", pod->channels,
171 btr_consume(btrn, pod->converted);
175 pod->have_more = true;
180 /** Suspend decoding if output queue size is larger than that. */
181 #define OGGDEC_MAX_OUTPUT_SIZE (96 * 1024)
184 * Allocate chunks of this size and produce at most one chunk of output per
185 * ->post_select() invocation. If the buffer could only be filled partially
186 * due to insufficient input being available, it is shrunk to the real output
187 * size and the resized buffer is fed into the output queue.
189 #define OGGDEC_OUTPUT_CHUNK_SIZE (32 * 1024)
191 static void ogg_pre_select(struct sched *s, void *context)
193 struct filter_node *fn = context;
194 struct private_oggdec_data *pod = fn->private_data;
195 struct btr_node *btrn = fn->btrn;
198 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
200 return sched_min_delay(s);
203 if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
208 static int ogg_post_select(__a_unused struct sched *s, void *context)
210 struct filter_node *fn = context;
211 struct private_oggdec_data *pod = fn->private_data;
212 struct btr_node *btrn = fn->btrn;
216 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
218 if (ret != -E_BTR_EOF) /* fatal error */
220 if (fn->min_iqs == 0 && !pod->have_more) /* EOF */
222 /* last ov_read() returned OV_HOLE */
223 } else if (ret == 0 && !pod->have_more) /* nothing to do */
225 if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
230 btr_merge(btrn, fn->min_iqs);
235 buf = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
237 ret = ov_read(pod->vf, buf + have, OGGDEC_OUTPUT_CHUNK_SIZE - have,
238 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
239 btr_consume(btrn, pod->converted);
245 if (have >= OGGDEC_OUTPUT_CHUNK_SIZE)
248 pod->have_more = (ret > 0);
250 if (have < OGGDEC_OUTPUT_CHUNK_SIZE)
251 buf = para_realloc(buf, have);
252 btr_add_output(buf, have, btrn);
255 if (ret == OV_HOLE) /* avoid buffer underruns */
257 if (ret >= 0 || ret == OV_HOLE)
259 ret = -E_OGGDEC_BADLINK;
262 btr_remove_node(&fn->btrn);
266 const struct filter lsg_filter_cmd_com_oggdec_user_data = {
269 .pre_select = ogg_pre_select,
270 .post_select = ogg_post_select,
271 .execute = oggdec_execute