1bc6d2f748e8cbe7c3067e3479e91ef771fb7667
[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 struct timeval stream_start;
39 unsigned int sample_rate;
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 return decoder_execute(cmd, pod->sample_rate, pod->channels, result);
125 }
126
127 static int ogg_init(struct filter_node *fn)
128 {
129 struct private_oggdec_data *pod = fn->private_data;
130 struct btr_node *btrn = fn->btrn;
131 int ret, oret;
132 size_t iqs;
133
134 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
135 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
136 btr_get_input_queue_size(btrn), fn->min_iqs);
137 open:
138 oret = ov_open_callbacks(fn, pod->vf,
139 NULL, /* no initial buffer */
140 0, /* no initial bytes */
141 ovc); /* the ov_open_callbacks */
142 if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
143 /* this might be due to the input buffer being too small */
144 if (!btr_no_parent(btrn)) {
145 fn->min_iqs += 1000;
146 iqs = btr_get_input_queue_size(btrn);
147 ret = 0;
148 if (iqs < fn->min_iqs)
149 goto out;
150 PARA_CRIT_LOG("iqs: %zu\n", iqs);
151 btr_merge(btrn, fn->min_iqs);
152 pod->converted = 0;
153 goto open;
154 }
155 ret = (oret == OV_ENOTVORBIS)?
156 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
157 goto out;
158 }
159 ret = -E_OGGDEC_READ;
160 if (oret == OV_EREAD)
161 goto out;
162 ret = -E_OGGDEC_VERSION;
163 if (oret == OV_EVERSION)
164 goto out;
165 ret = -E_OGGDEC_FAULT;
166 if (oret < 0)
167 goto out;
168 pod->channels = ov_info(pod->vf, 0)->channels;
169 pod->sample_rate = ov_info(pod->vf, 0)->rate;
170 tv_add(now, &(struct timeval)EMBRACE(0, 300 * 1000), &pod->stream_start);
171 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
172 pod->sample_rate);
173 ret = 1;
174 out:
175 if (ret <= 0) {
176 free(pod->vf);
177 pod->vf = NULL;
178 } else {
179 btr_consume(btrn, pod->converted);
180 pod->converted = 0;
181 fn->min_iqs = 0;
182 }
183 return ret;
184 }
185
186 static void ogg_pre_select(__a_unused struct sched *s, struct task *t)
187 {
188 struct filter_node *fn = container_of(t, struct filter_node, task);
189 struct private_oggdec_data *pod = fn->private_data;
190 int ret;
191
192 t->error = 0;
193 ret = btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL);
194 if (ret < 0)
195 sched_min_delay(s);
196 else
197 sched_request_barrier(&pod->stream_start, s);
198 }
199
200 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
201 {
202 struct filter_node *fn = container_of(t, struct filter_node, task);
203 struct private_oggdec_data *pod = fn->private_data;
204 struct btr_node *btrn = fn->btrn;
205 int ret, ns;
206
207 if (tv_diff(&pod->stream_start, now, NULL) > 0)
208 return;
209 pod->converted = 0;
210 t->error = 0;
211 ret = ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
212 if (!pod->vf) {
213 if (ret <= 0)
214 goto out;
215 btr_merge(btrn, fn->min_iqs);
216 ret = ogg_init(fn);
217 if (ret <= 0)
218 goto out;
219 }
220 for (;;) {
221 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
222 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
223 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
224 btr_consume(btrn, pod->converted);
225 pod->converted = 0;
226 if (read_ret <= 0)
227 free(out);
228 ret = ns;
229 if (read_ret == 0 || read_ret == OV_HOLE)
230 goto out;
231 ret = -E_OGGDEC_BADLINK;
232 if (read_ret < 0)
233 goto out;
234 btr_add_output(out, read_ret, btrn);
235 if (btr_get_output_queue_size(btrn) > 128 * 1024)
236 return; /* enough data for the moment */
237 }
238 out:
239 if (ret < 0) {
240 t->error = ret;
241 btr_remove_node(btrn);
242 }
243 }
244
245 /**
246 * The init function of the ogg vorbis decoder.
247 *
248 * \param f Its fields are filled in by the function.
249 */
250 void oggdec_filter_init(struct filter *f)
251 {
252 f->open = ogg_open;
253 f->close = ogg_close;
254 f->pre_select = ogg_pre_select;
255 f->post_select = ogg_post_select;
256 f->execute = oggdec_execute;
257 }