ogg_pre_select(): Remove __a_unused.
[paraslash.git] / oggdec_filter.c
1 /*
2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
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 #include <stdbool.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "error.h"
20 #include "string.h"
21
22 /** Determine byte sex. */
23 #ifdef WORDS_BIGENDIAN
24 #define ENDIAN 1
25 #else
26 #define ENDIAN 0
27 #endif
28
29 /** Data specific to the oggdec filter. */
30 struct private_oggdec_data {
31 /** Describes an ogg vorbis file. */
32 OggVorbis_File *vf;
33 /** The number of bytes consumed from the input buffer. */
34 size_t converted;
35 /** The number of channels of the current stream. */
36 unsigned int channels;
37 /** Current sample rate in Hz. */
38 unsigned int sample_rate;
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 /* this might be due to the input buffer being 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 PARA_CRIT_LOG("iqs: %zu\n", iqs);
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("%d channels, %d 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 }
177 return ret;
178 }
179
180 static void ogg_pre_select(struct sched *s, struct task *t)
181 {
182 struct filter_node *fn = container_of(t, struct filter_node, task);
183 int ret;
184
185 t->error = 0;
186 ret = btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL);
187 if (ret != 0)
188 sched_min_delay(s);
189 else
190 sched_request_timeout_ms(100, s);
191 }
192
193 #define OGGDEC_MAX_OUTPUT_SIZE (128 * 1024)
194 #define OGGDEC_OUTPUT_CHUNK_SIZE (32 * 1024)
195
196 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
197 {
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;
201 int ret, have;
202 char *buf;
203
204 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
205 if (ret <= 0)
206 goto out;
207 if (!pod->vf) {
208 if (ret <= 0)
209 goto out;
210 btr_merge(btrn, fn->min_iqs);
211 ret = ogg_init(fn);
212 goto out;
213 }
214 have = 0;
215 buf = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
216 for (;;) {
217 ret = ov_read(pod->vf, buf + have, OGGDEC_OUTPUT_CHUNK_SIZE - have,
218 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
219 btr_consume(btrn, pod->converted);
220 pod->converted = 0;
221 if (ret <= 0)
222 break;
223 fn->min_iqs = 0;
224 have += ret;
225 if (have < OGGDEC_OUTPUT_CHUNK_SIZE)
226 continue;
227 if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
228 break;
229 btr_add_output(buf, have, btrn);
230 buf = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
231 have = 0;
232 }
233 if (have > 0)
234 btr_add_output(buf, have, btrn);
235 else
236 free(buf);
237 if (ret == OV_HOLE) /* avoid buffer underruns */
238 fn->min_iqs = 9000;
239 if (ret >= 0 || ret == OV_HOLE)
240 return;
241 ret = -E_OGGDEC_BADLINK;
242 out:
243 t->error = ret;
244 if (ret < 0)
245 btr_remove_node(btrn);
246 }
247
248 /**
249 * The init function of the ogg vorbis decoder.
250 *
251 * \param f Its fields are filled in by the function.
252 */
253 void oggdec_filter_init(struct filter *f)
254 {
255 f->open = ogg_open;
256 f->close = ogg_close;
257 f->pre_select = ogg_pre_select;
258 f->post_select = ogg_post_select;
259 f->execute = oggdec_execute;
260 }