Merge branch 'refs/heads/t/invalid-ids'
[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 "ggo.h"
16 #include "buffer_tree.h"
17 #include "filter.h"
18 #include "error.h"
19 #include "string.h"
20
21 /** Determine byte sex. */
22 #ifdef WORDS_BIGENDIAN
23 #define ENDIAN 1
24 #else
25 #define ENDIAN 0
26 #endif
27
28 /** Data specific to the oggdec filter. */
29 struct private_oggdec_data {
30 /** Describes an ogg vorbis file. */
31 OggVorbis_File *vf;
32 /** The number of bytes consumed from the input buffer. */
33 size_t converted;
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. */
39 bool have_more;
40 };
41
42 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
43 {
44 struct filter_node *fn = datasource;
45 struct private_oggdec_data *pod = fn->private_data;
46 struct btr_node *btrn = fn->btrn;
47 char *btr_buf;
48 size_t nbytes = btr_next_buffer(btrn, &btr_buf), tmp;
49
50 /**
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.
53 */
54 assert(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);
59 if (tmp == 0)
60 return 0;
61 memcpy(buf, btr_buf + pod->converted, tmp);
62 pod->converted += tmp;
63 return tmp;
64 }
65
66 /*
67 * Custom data seeking function.
68 *
69 * Since we want the data source to be treated as unseekable at all
70 * times, the provided seek callback always returns -1 (failure).
71 */
72 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
73 __a_unused int whence)
74 {
75 return -1;
76 }
77
78 static int cb_close(__a_unused void *datasource)
79 {
80 return 0;
81 }
82
83 static const ov_callbacks ovc = {
84 .read_func = cb_read,
85 .seek_func = cb_seek,
86 .close_func = cb_close,
87 /*
88 * The tell function need not be provided if the data IO abstraction is
89 * not seekable
90 */
91 .tell_func = NULL
92 };
93
94 static void ogg_open(struct filter_node *fn)
95 {
96 fn->private_data = para_calloc(sizeof(struct private_oggdec_data));
97 fn->min_iqs = 8000;
98 }
99
100 static void ogg_close(struct filter_node *fn)
101 {
102 struct private_oggdec_data *pod = fn->private_data;
103
104 if (pod && pod->vf) {
105 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
106 ov_clear(pod->vf);
107 free(pod->vf);
108 pod->vf = NULL;
109 } else
110 PARA_DEBUG_LOG("nothing to close\n");
111 free(pod);
112 fn->private_data = NULL;
113 }
114
115 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
116 {
117 struct filter_node *fn = btr_context(btrn);
118 struct private_oggdec_data *pod = fn->private_data;
119
120 return decoder_execute(cmd, pod->sample_rate, pod->channels, result);
121 }
122
123 static int ogg_init(struct filter_node *fn)
124 {
125 struct private_oggdec_data *pod = fn->private_data;
126 struct btr_node *btrn = fn->btrn;
127 int ret, oret;
128 size_t iqs;
129 struct OggVorbis_File *vf = para_malloc(sizeof(*vf));
130
131 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
132 btr_get_input_queue_size(btrn), fn->min_iqs);
133 open:
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)) {
141 fn->min_iqs += 1000;
142 iqs = btr_get_input_queue_size(btrn);
143 ret = 0;
144 if (iqs < fn->min_iqs)
145 goto out;
146 btr_merge(btrn, fn->min_iqs);
147 pod->converted = 0;
148 goto open;
149 }
150 ret = (oret == OV_ENOTVORBIS)?
151 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
152 goto out;
153 }
154 ret = -E_OGGDEC_READ;
155 if (oret == OV_EREAD)
156 goto out;
157 ret = -E_OGGDEC_VERSION;
158 if (oret == OV_EVERSION)
159 goto out;
160 ret = -E_OGGDEC_FAULT;
161 if (oret < 0)
162 goto out;
163 pod->channels = ov_info(vf, 0)->channels;
164 pod->sample_rate = ov_info(vf, 0)->rate;
165 PARA_NOTICE_LOG("%u channels, %u Hz\n", pod->channels,
166 pod->sample_rate);
167 ret = 1;
168 out:
169 if (ret <= 0)
170 free(vf);
171 else {
172 btr_consume(btrn, pod->converted);
173 pod->converted = 0;
174 fn->min_iqs = 0;
175 pod->vf = vf;
176 pod->have_more = true;
177 }
178 return ret;
179 }
180
181 /** Suspend decoding if output queue size is larger than that. */
182 #define OGGDEC_MAX_OUTPUT_SIZE (96 * 1024)
183
184 /**
185 * Allocate chunks of this size and produce at most one chunk of output per
186 * ->post_select() invocation. If the buffer could only be filled partially
187 * due to insufficient input being available, it is shrunk to the real output
188 * size and the resized buffer is fed into the output queue.
189 */
190 #define OGGDEC_OUTPUT_CHUNK_SIZE (32 * 1024)
191
192 static void ogg_pre_select(struct sched *s, void *context)
193 {
194 struct filter_node *fn = context;
195 struct private_oggdec_data *pod = fn->private_data;
196 struct btr_node *btrn = fn->btrn;
197 int ret;
198
199 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
200 if (ret != 0)
201 return sched_min_delay(s);
202 if (!pod->have_more)
203 return;
204 if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
205 return;
206 sched_min_delay(s);
207 }
208
209 static int ogg_post_select(__a_unused struct sched *s, void *context)
210 {
211 struct filter_node *fn = context;
212 struct private_oggdec_data *pod = fn->private_data;
213 struct btr_node *btrn = fn->btrn;
214 int ret, have;
215 char *buf;
216
217 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
218 if (ret < 0) {
219 if (ret != -E_BTR_EOF) /* fatal error */
220 goto out;
221 if (fn->min_iqs == 0 && !pod->have_more) /* EOF */
222 goto out;
223 /* last ov_read() returned OV_HOLE */
224 } else if (ret == 0 && !pod->have_more) /* nothing to do */
225 goto out;
226 if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
227 return 0;
228 if (!pod->vf) {
229 if (ret <= 0)
230 goto out;
231 btr_merge(btrn, fn->min_iqs);
232 ret = ogg_init(fn);
233 goto out;
234 }
235 have = 0;
236 buf = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
237 for (;;) {
238 ret = ov_read(pod->vf, buf + have, OGGDEC_OUTPUT_CHUNK_SIZE - have,
239 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
240 btr_consume(btrn, pod->converted);
241 pod->converted = 0;
242 if (ret <= 0)
243 break;
244 fn->min_iqs = 0;
245 have += ret;
246 if (have >= OGGDEC_OUTPUT_CHUNK_SIZE)
247 break;
248 }
249 pod->have_more = (ret > 0);
250 if (have > 0) {
251 if (have < OGGDEC_OUTPUT_CHUNK_SIZE)
252 buf = para_realloc(buf, have);
253 btr_add_output(buf, have, btrn);
254 } else
255 free(buf);
256 if (ret == OV_HOLE) /* avoid buffer underruns */
257 fn->min_iqs = 9000;
258 if (ret >= 0 || ret == OV_HOLE)
259 return 0;
260 ret = -E_OGGDEC_BADLINK;
261 out:
262 if (ret < 0)
263 btr_remove_node(&fn->btrn);
264 return ret;
265 }
266
267 /**
268 * The init function of the ogg vorbis decoder.
269 *
270 * \param f Its fields are filled in by the function.
271 */
272 void oggdec_filter_init(struct filter *f)
273 {
274 f->open = ogg_open;
275 f->close = ogg_close;
276 f->pre_select = ogg_pre_select;
277 f->post_select = ogg_post_select;
278 f->execute = oggdec_execute;
279 }