oggdec: Replace stream start delay by minimum input queue size.
[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 struct private_oggdec_data *pod = para_calloc(
96 sizeof(struct private_oggdec_data));
97
98 fn->private_data = pod;
99 fn->min_iqs = 8000;
100 }
101
102 static void ogg_close(struct filter_node *fn)
103 {
104 struct private_oggdec_data *pod = fn->private_data;
105 if (pod->vf) {
106 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
107 ov_clear(pod->vf);
108 free(pod->vf);
109 pod->vf = NULL;
110 } else
111 PARA_DEBUG_LOG("nothing to close\n");
112 free(fn->private_data);
113 fn->private_data = NULL;
114 }
115
116 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
117
118 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
119 {
120 struct filter_node *fn = btr_context(btrn);
121 struct private_oggdec_data *pod = fn->private_data;
122
123 return decoder_execute(cmd, pod->sample_rate, pod->channels, result);
124 }
125
126 static int ogg_init(struct filter_node *fn)
127 {
128 struct private_oggdec_data *pod = fn->private_data;
129 struct btr_node *btrn = fn->btrn;
130 int ret, oret;
131 size_t iqs;
132
133 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
134 PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
135 btr_get_input_queue_size(btrn), fn->min_iqs);
136 open:
137 oret = ov_open_callbacks(fn, pod->vf,
138 NULL, /* no initial buffer */
139 0, /* no initial bytes */
140 ovc); /* the ov_open_callbacks */
141 if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
142 /* this might be due to the input buffer being too small */
143 if (!btr_no_parent(btrn)) {
144 fn->min_iqs += 1000;
145 iqs = btr_get_input_queue_size(btrn);
146 ret = 0;
147 if (iqs < fn->min_iqs)
148 goto out;
149 PARA_CRIT_LOG("iqs: %zu\n", iqs);
150 btr_merge(btrn, fn->min_iqs);
151 pod->converted = 0;
152 goto open;
153 }
154 ret = (oret == OV_ENOTVORBIS)?
155 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
156 goto out;
157 }
158 ret = -E_OGGDEC_READ;
159 if (oret == OV_EREAD)
160 goto out;
161 ret = -E_OGGDEC_VERSION;
162 if (oret == OV_EVERSION)
163 goto out;
164 ret = -E_OGGDEC_FAULT;
165 if (oret < 0)
166 goto out;
167 pod->channels = ov_info(pod->vf, 0)->channels;
168 pod->sample_rate = ov_info(pod->vf, 0)->rate;
169 PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
170 pod->sample_rate);
171 ret = 1;
172 out:
173 if (ret <= 0) {
174 free(pod->vf);
175 pod->vf = NULL;
176 } else {
177 btr_consume(btrn, pod->converted);
178 pod->converted = 0;
179 fn->min_iqs = 0;
180 }
181 return ret;
182 }
183
184 static void ogg_pre_select(__a_unused struct sched *s, struct task *t)
185 {
186 struct filter_node *fn = container_of(t, struct filter_node, task);
187 int ret;
188
189 t->error = 0;
190 ret = btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL);
191 if (ret != 0)
192 sched_min_delay(s);
193 else
194 sched_request_timeout_ms(100, s);
195 }
196
197 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
198 {
199 struct filter_node *fn = container_of(t, struct filter_node, task);
200 struct private_oggdec_data *pod = fn->private_data;
201 struct btr_node *btrn = fn->btrn;
202 int ret, ns;
203
204 pod->converted = 0;
205 t->error = 0;
206 ret = ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
207 if (ret <= 0)
208 goto out;
209 if (!pod->vf) {
210 if (ret <= 0)
211 goto out;
212 btr_merge(btrn, fn->min_iqs);
213 ret = ogg_init(fn);
214 if (ret <= 0)
215 goto out;
216 }
217 for (;;) {
218 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
219 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
220 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
221 btr_consume(btrn, pod->converted);
222 pod->converted = 0;
223 if (read_ret <= 0)
224 free(out);
225 ret = ns;
226 if (read_ret == OV_HOLE) /* avoid buffer underruns */
227 fn->min_iqs = 9000;
228 if (read_ret == 0 || read_ret == OV_HOLE)
229 goto out;
230 ret = -E_OGGDEC_BADLINK;
231 if (read_ret < 0)
232 goto out;
233 btr_add_output(out, read_ret, btrn);
234 fn->min_iqs = 0;
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 }