Let helpers in portable_io.h receive void * arguments.
[paraslash.git] / oggdec_filter.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file oggdec_filter.c Paraslash's ogg vorbis decoder. */
8
9 #include <regex.h>
10 #include <vorbis/vorbisfile.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "buffer_tree.h"
16 #include "filter.h"
17 #include "error.h"
18 #include "string.h"
19
20 /** Determine byte sex. */
21 #ifdef WORDS_BIGENDIAN
22 #define ENDIAN 1
23 #else
24 #define ENDIAN 0
25 #endif
26
27 /** Data specific to the oggdec filter. */
28 struct private_oggdec_data {
29 /** Describes an ogg vorbis file. */
30 OggVorbis_File *vf;
31 /** The number of bytes consumed from the input buffer. */
32 size_t converted;
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. */
38 bool have_more;
39 };
40
41 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
42 {
43 struct filter_node *fn = datasource;
44 struct private_oggdec_data *pod = fn->private_data;
45 struct btr_node *btrn = fn->btrn;
46 char *btr_buf;
47 size_t nbytes = btr_next_buffer(btrn, &btr_buf), tmp;
48
49 /**
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.
52 */
53 assert(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);
58 if (tmp == 0)
59 return 0;
60 memcpy(buf, btr_buf + pod->converted, tmp);
61 pod->converted += tmp;
62 return tmp;
63 }
64
65 /*
66 * Custom data seeking function.
67 *
68 * Since we want the data source to be treated as unseekable at all
69 * times, the provided seek callback always returns -1 (failure).
70 */
71 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
72 __a_unused int whence)
73 {
74 return -1;
75 }
76
77 static int cb_close(__a_unused void *datasource)
78 {
79 return 0;
80 }
81
82 static const ov_callbacks ovc = {
83 .read_func = cb_read,
84 .seek_func = cb_seek,
85 .close_func = cb_close,
86 /*
87 * The tell function need not be provided if the data IO abstraction is
88 * not seekable
89 */
90 .tell_func = NULL
91 };
92
93 static void ogg_open(struct filter_node *fn)
94 {
95 fn->private_data = para_calloc(sizeof(struct private_oggdec_data));
96 fn->min_iqs = 8000;
97 }
98
99 static void ogg_close(struct filter_node *fn)
100 {
101 struct private_oggdec_data *pod = fn->private_data;
102
103 if (pod && pod->vf) {
104 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
105 ov_clear(pod->vf);
106 free(pod->vf);
107 pod->vf = NULL;
108 } else
109 PARA_DEBUG_LOG("nothing to close\n");
110 free(pod);
111 fn->private_data = NULL;
112 }
113
114 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
115 {
116 struct filter_node *fn = btr_context(btrn);
117 struct private_oggdec_data *pod = fn->private_data;
118
119 return decoder_execute(cmd, pod->sample_rate, pod->channels, result);
120 }
121
122 static int ogg_init(struct filter_node *fn)
123 {
124 struct private_oggdec_data *pod = fn->private_data;
125 struct btr_node *btrn = fn->btrn;
126 int ret, oret;
127 size_t iqs;
128 struct OggVorbis_File *vf = para_malloc(sizeof(*vf));
129
130 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
131 btr_get_input_queue_size(btrn), fn->min_iqs);
132 open:
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)) {
140 fn->min_iqs += 1000;
141 iqs = btr_get_input_queue_size(btrn);
142 ret = 0;
143 if (iqs < fn->min_iqs)
144 goto out;
145 btr_merge(btrn, fn->min_iqs);
146 pod->converted = 0;
147 goto open;
148 }
149 ret = (oret == OV_ENOTVORBIS)?
150 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
151 goto out;
152 }
153 ret = -E_OGGDEC_READ;
154 if (oret == OV_EREAD)
155 goto out;
156 ret = -E_OGGDEC_VERSION;
157 if (oret == OV_EVERSION)
158 goto out;
159 ret = -E_OGGDEC_FAULT;
160 if (oret < 0)
161 goto out;
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,
165 pod->sample_rate);
166 ret = 1;
167 out:
168 if (ret <= 0)
169 free(vf);
170 else {
171 btr_consume(btrn, pod->converted);
172 pod->converted = 0;
173 fn->min_iqs = 0;
174 pod->vf = vf;
175 pod->have_more = true;
176 }
177 return ret;
178 }
179
180 /** Suspend decoding if output queue size is larger than that. */
181 #define OGGDEC_MAX_OUTPUT_SIZE (96 * 1024)
182
183 /**
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.
188 */
189 #define OGGDEC_OUTPUT_CHUNK_SIZE (32 * 1024)
190
191 static void ogg_pre_select(struct sched *s, void *context)
192 {
193 struct filter_node *fn = context;
194 struct private_oggdec_data *pod = fn->private_data;
195 struct btr_node *btrn = fn->btrn;
196 int ret;
197
198 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
199 if (ret != 0)
200 return sched_min_delay(s);
201 if (!pod->have_more)
202 return;
203 if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
204 return;
205 sched_min_delay(s);
206 }
207
208 static int ogg_post_select(__a_unused struct sched *s, void *context)
209 {
210 struct filter_node *fn = context;
211 struct private_oggdec_data *pod = fn->private_data;
212 struct btr_node *btrn = fn->btrn;
213 int ret, have;
214 char *buf;
215
216 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
217 if (ret < 0) {
218 if (ret != -E_BTR_EOF) /* fatal error */
219 goto out;
220 if (fn->min_iqs == 0 && !pod->have_more) /* EOF */
221 goto out;
222 /* last ov_read() returned OV_HOLE */
223 } else if (ret == 0 && !pod->have_more) /* nothing to do */
224 goto out;
225 if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
226 return 0;
227 if (!pod->vf) {
228 if (ret <= 0)
229 goto out;
230 btr_merge(btrn, fn->min_iqs);
231 ret = ogg_init(fn);
232 goto out;
233 }
234 have = 0;
235 buf = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
236 for (;;) {
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);
240 pod->converted = 0;
241 if (ret <= 0)
242 break;
243 fn->min_iqs = 0;
244 have += ret;
245 if (have >= OGGDEC_OUTPUT_CHUNK_SIZE)
246 break;
247 }
248 pod->have_more = (ret > 0);
249 if (have > 0) {
250 if (have < OGGDEC_OUTPUT_CHUNK_SIZE)
251 buf = para_realloc(buf, have);
252 btr_add_output(buf, have, btrn);
253 } else
254 free(buf);
255 if (ret == OV_HOLE) /* avoid buffer underruns */
256 fn->min_iqs = 9000;
257 if (ret >= 0 || ret == OV_HOLE)
258 return 0;
259 ret = -E_OGGDEC_BADLINK;
260 out:
261 if (ret < 0)
262 btr_remove_node(&fn->btrn);
263 return ret;
264 }
265
266 const struct filter lsg_filter_cmd_com_oggdec_user_data = {
267 .open = ogg_open,
268 .close = ogg_close,
269 .pre_select = ogg_pre_select,
270 .post_select = ogg_post_select,
271 .execute = oggdec_execute
272 };