Remove some unused error codes.
[paraslash.git] / oggdec_filter.c
1 /*
2 * Copyright (C) 2005-2010 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 samplerate;
39 struct timeval stream_start;
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 struct private_oggdec_data *pod = para_calloc(
97 sizeof(struct private_oggdec_data));
98
99 fn->private_data = pod;
100 fn->min_iqs = 8000;
101 }
102
103 static void ogg_close(struct filter_node *fn)
104 {
105 struct private_oggdec_data *pod = fn->private_data;
106 if (pod->vf) {
107 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
108 ov_clear(pod->vf);
109 free(pod->vf);
110 pod->vf = NULL;
111 } else
112 PARA_DEBUG_LOG("nothing to close\n");
113 free(fn->private_data);
114 fn->private_data = NULL;
115 }
116
117 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
118
119 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
120 {
121 struct filter_node *fn = btr_context(btrn);
122 struct private_oggdec_data *pod = fn->private_data;
123
124 if (!strcmp(cmd, "samplerate")) {
125 if (pod->samplerate == 0)
126 return -E_BTR_NAVAIL;
127 *result = make_message("%u", pod->samplerate);
128 return 1;
129 }
130 if (!strcmp(cmd, "channels")) {
131 if (pod->channels == 0)
132 return -E_BTR_NAVAIL;
133 *result = make_message("%u", pod->channels);
134 return 1;
135 }
136 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
137 }
138
139 static int ogg_init(struct filter_node *fn)
140 {
141 struct private_oggdec_data *pod = fn->private_data;
142 struct btr_node *btrn = fn->btrn;
143 int ret, oret;
144 size_t iqs;
145
146 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
147 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
148 btr_get_input_queue_size(btrn), fn->min_iqs);
149 open:
150 oret = ov_open_callbacks(fn, pod->vf,
151 NULL, /* no initial buffer */
152 0, /* no initial bytes */
153 ovc); /* the ov_open_callbacks */
154 if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
155 /* this might be due to the input buffer being too small */
156 if (!btr_no_parent(btrn)) {
157 fn->min_iqs += 1000;
158 iqs = btr_get_input_queue_size(btrn);
159 ret = 0;
160 if (iqs < fn->min_iqs)
161 goto out;
162 PARA_CRIT_LOG("iqs: %zu\n", iqs);
163 btr_merge(btrn, fn->min_iqs);
164 pod->converted = 0;
165 goto open;
166 }
167 ret = (oret == OV_ENOTVORBIS)?
168 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
169 goto out;
170 }
171 ret = -E_OGGDEC_READ;
172 if (oret == OV_EREAD)
173 goto out;
174 ret = -E_OGGDEC_VERSION;
175 if (oret == OV_EVERSION)
176 goto out;
177 ret = -E_OGGDEC_FAULT;
178 if (oret < 0)
179 goto out;
180 pod->channels = ov_info(pod->vf, 0)->channels;
181 pod->samplerate = ov_info(pod->vf, 0)->rate;
182 tv_add(now, &(struct timeval)EMBRACE(0, 300 * 1000), &pod->stream_start);
183 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
184 pod->samplerate);
185 ret = 1;
186 out:
187 if (ret <= 0) {
188 free(pod->vf);
189 pod->vf = NULL;
190 } else {
191 btr_consume(btrn, pod->converted);
192 pod->converted = 0;
193 fn->min_iqs = 0;
194 }
195 return ret;
196 }
197
198 static void ogg_pre_select(__a_unused struct sched *s, struct task *t)
199 {
200 struct filter_node *fn = container_of(t, struct filter_node, task);
201 struct private_oggdec_data *pod = fn->private_data;
202 int ret;
203
204 t->error = 0;
205 ret = btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL);
206 if (ret < 0)
207 sched_min_delay(s);
208 else
209 sched_request_barrier(&pod->stream_start, s);
210 }
211
212 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
213 {
214 struct filter_node *fn = container_of(t, struct filter_node, task);
215 struct private_oggdec_data *pod = fn->private_data;
216 struct btr_node *btrn = fn->btrn;
217 int ret, ns;
218
219 if (tv_diff(&pod->stream_start, now, NULL) > 0)
220 return;
221 pod->converted = 0;
222 t->error = 0;
223 ret = ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
224 if (!pod->vf) {
225 if (ret <= 0)
226 goto out;
227 btr_merge(btrn, fn->min_iqs);
228 ret = ogg_init(fn);
229 if (ret <= 0)
230 goto out;
231 }
232 for (;;) {
233 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
234 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
235 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
236 btr_consume(btrn, pod->converted);
237 pod->converted = 0;
238 if (read_ret <= 0)
239 free(out);
240 ret = ns;
241 if (read_ret == 0 || read_ret == OV_HOLE)
242 goto out;
243 ret = -E_OGGDEC_BADLINK;
244 if (read_ret < 0)
245 goto out;
246 btr_add_output(out, read_ret, btrn);
247 if (btr_get_output_queue_size(btrn) > 128 * 1024)
248 return; /* enough data for the moment */
249 }
250 out:
251 if (ret < 0) {
252 t->error = ret;
253 btr_remove_node(btrn);
254 }
255 }
256
257 /**
258 * The init function of the ogg vorbis decoder.
259 *
260 * \param f Its fields are filled in by the function.
261 */
262 void oggdec_filter_init(struct filter *f)
263 {
264 f->open = ogg_open;
265 f->close = ogg_close;
266 f->pre_select = ogg_pre_select;
267 f->post_select = ogg_post_select;
268 f->execute = oggdec_execute;
269 }