filter/audiod: Be nice to filters without close method.
[paraslash.git] / oggdec_filter.c
1 /*
2  * Copyright (C) 2005-2009 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 "oggdec_filter.cmdline.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "buffer_tree.h"
19 #include "filter.h"
20 #include "error.h"
21 #include "string.h"
22
23 /** Determine byte sex. */
24 #ifdef WORDS_BIGENDIAN
25 #define ENDIAN 1
26 #else
27 #define ENDIAN 0
28 #endif
29
30 /** Data specific to the oggdec filter. */
31 struct private_oggdec_data {
32         /** Describes an ogg vorbis file. */
33         OggVorbis_File *vf;
34         /** The number of bytes consumed from the input buffer. */
35         size_t converted;
36         /** When to start producing output. */
37         struct timeval stream_start;
38         /** The number of channels of the current stream. */
39         unsigned int channels;
40         /** Current sample rate in Hz. */
41         unsigned int samplerate;
42 };
43
44 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
45 {
46         struct filter_node *fn = datasource;
47         struct private_oggdec_data *pod = fn->private_data;
48         struct btr_node *btrn = fn->btrn;
49         char *btr_buf;
50         size_t nbytes = btr_next_buffer(btrn, &btr_buf), tmp;
51
52         /**
53          * oggvorbis always uses size == 1. Other sizes would complicate the code
54          * for no real gain. So we simply don't support size != 1.
55          */
56         assert(size == 1);
57         assert(pod->converted <= nbytes);
58         tmp = nbytes - pod->converted;
59         PARA_DEBUG_LOG("vorbis requests %zu bytes have %zu\n", nmemb, tmp);
60         tmp = PARA_MIN(tmp, nmemb);
61         if (tmp == 0)
62                 return 0;
63         memcpy(buf, btr_buf + pod->converted, tmp);
64         pod->converted += tmp;
65         return tmp;
66 }
67
68 /*
69  * Custom data seeking function.
70  *
71  * Since we want the data source to be treated as unseekable at all
72  * times, the provided seek callback always returns -1 (failure).
73  */
74 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
75                 __a_unused int whence)
76 {
77         return -1;
78 }
79
80 static int cb_close(__a_unused void *datasource)
81 {
82         return 0;
83 }
84
85 static const ov_callbacks ovc = {
86         .read_func = cb_read,
87         .seek_func = cb_seek,
88         .close_func = cb_close,
89         /*
90          * The tell function need not be provided if the data IO abstraction is
91          * not seekable
92          */
93         .tell_func = NULL
94 };
95
96 static void ogg_open(struct filter_node *fn)
97 {
98         struct private_oggdec_data *pod = para_calloc(
99                 sizeof(struct private_oggdec_data));
100         struct oggdec_filter_args_info *conf = fn->conf;
101
102         fn->private_data = pod;
103         fn->bufsize = conf->bufsize_arg * 1024;
104         fn->buf = para_malloc(fn->bufsize);
105         fn->min_iqs = 8000;
106 }
107
108 static void ogg_close(struct filter_node *fn)
109 {
110         struct private_oggdec_data *pod = fn->private_data;
111         if (pod->vf) {
112                 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
113                 ov_clear(pod->vf);
114                 free(pod->vf);
115                 pod->vf = NULL;
116         } else
117                 PARA_DEBUG_LOG("nothing to close\n");
118         free(fn->buf);
119         fn->buf = NULL;
120         free(fn->private_data);
121         fn->private_data = NULL;
122 }
123
124 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
125
126 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
127 {
128         struct filter_node *fn = btr_context(btrn);
129         struct private_oggdec_data *pod = fn->private_data;
130
131         if (!strcmp(cmd, "samplerate")) {
132                 if (pod->samplerate == 0)
133                         return -ERRNO_TO_PARA_ERROR(ENAVAIL);
134                 *result = make_message("%u", pod->samplerate);
135                 return 1;
136         }
137         if (!strcmp(cmd, "channels")) {
138                 if (pod->channels == 0)
139                         return -ERRNO_TO_PARA_ERROR(ENAVAIL);
140                 *result = make_message("%u", pod->channels);
141                 return 1;
142         }
143         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
144 }
145
146 static int ogg_init(struct filter_node *fn)
147 {
148         struct private_oggdec_data *pod = fn->private_data;
149         struct btr_node *btrn = fn->btrn;
150         int ret, oret;
151         size_t iqs;
152         struct timeval delay = {0, 500 * 1000};
153
154         pod->vf = para_malloc(sizeof(struct OggVorbis_File));
155         PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
156                 btr_get_input_queue_size(btrn), fn->min_iqs);
157 open:
158         oret = ov_open_callbacks(fn, pod->vf,
159                 NULL, /* no initial buffer */
160                 0, /* no initial bytes */
161                 ovc); /* the ov_open_callbacks */
162         if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
163                 /* this might be due to the input buffer being too small */
164                 if (!btr_no_parent(btrn)) {
165                         fn->min_iqs += 1000;
166                         iqs = btr_get_input_queue_size(btrn);
167                         ret = 0;
168                         if (iqs < fn->min_iqs)
169                                 goto out;
170                         PARA_CRIT_LOG("iqs: %zu\n", iqs);
171                         btr_merge(btrn, fn->min_iqs);
172                         pod->converted = 0;
173                         goto open;
174                 }
175                 ret = (oret == OV_ENOTVORBIS)?
176                         -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
177                 goto out;
178         }
179         ret = -E_OGGDEC_READ;
180         if (oret == OV_EREAD)
181                 goto out;
182         ret = -E_OGGDEC_VERSION;
183         if (oret == OV_EVERSION)
184                 goto out;
185         ret = -E_OGGDEC_FAULT;
186         if (oret < 0)
187                 goto out;
188         pod->channels = ov_info(pod->vf, 0)->channels;
189         pod->samplerate = ov_info(pod->vf, 0)->rate;
190         PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
191                 pod->samplerate);
192         /* wait a bit to avoid buffer underruns */
193         tv_add(now, &delay, &pod->stream_start);
194         ret = 1;
195 out:
196         if (ret <= 0) {
197                 free(pod->vf);
198                 pod->vf = NULL;
199         } else {
200                 btr_consume(btrn, pod->converted);
201                 pod->converted = 0;
202                 fn->min_iqs = 0;
203         }
204         return ret;
205 }
206
207 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
208 {
209         struct filter_node *fn = container_of(t, struct filter_node, task);
210         struct private_oggdec_data *pod = fn->private_data;
211         struct btr_node *btrn = fn->btrn;
212         size_t iqs, len;
213         int ret;
214         char *in;
215
216         pod->converted = 0;
217         t->error = 0;
218         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
219         if (ret <= 0)
220                 goto out;
221         btr_merge(btrn, fn->min_iqs);
222         len = btr_next_buffer(btrn, &in);
223         iqs = btr_get_input_queue_size(btrn);
224         if (!pod->vf) {
225                 ret = ogg_init(fn);
226                 if (ret <= 0)
227                         goto out;
228         }
229         for (;;) {
230                 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
231                 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
232                         ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
233                 btr_consume(btrn, pod->converted);
234                 pod->converted = 0;
235                 if (read_ret <= 0)
236                         free(out);
237                 if (read_ret == 0) {
238                         if (btr_no_parent(btrn))
239                                 ret = -E_OGGDEC_EOF;
240                         else
241                                 ret = 0;
242                         goto out;
243                 }
244                 ret = 0;
245                 if (read_ret == OV_HOLE)
246                         goto out;
247                 ret = -E_OGGDEC_BADLINK;
248                 if (read_ret < 0)
249                         goto out;
250                 btr_add_output(out, read_ret, btrn);
251                 if (btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL) == 0)
252                         return; /* enough data for the moment */
253         }
254 out:
255         if (ret < 0) {
256                 t->error = ret;
257                 btr_remove_node(btrn);
258         }
259 }
260
261 static int oggdec_parse_config(int argc, char **argv, void **config)
262 {
263         int ret;
264         struct oggdec_filter_args_info *ogg_conf;
265
266         ogg_conf = para_calloc(sizeof(*ogg_conf));
267         ret = -E_OGGDEC_SYNTAX;
268         if (oggdec_cmdline_parser(argc, argv, ogg_conf))
269                 goto err;
270         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
271         if (ogg_conf->bufsize_arg < 0)
272                 goto err;
273         if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
274                 goto err;
275         if (ogg_conf->initial_buffer_arg < 0)
276                 goto err;
277         if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
278                 goto err;
279         *config = ogg_conf;
280         return 1;
281 err:
282         free(ogg_conf);
283         return ret;
284 }
285
286 static void oggdec_free_config(void *conf)
287 {
288         oggdec_cmdline_parser_free(conf);
289 }
290
291 /**
292  * The init function of the ogg vorbis decoder.
293  *
294  * \param f Its fields are filled in by the function.
295  */
296 void oggdec_filter_init(struct filter *f)
297 {
298         struct oggdec_filter_args_info dummy;
299
300         oggdec_cmdline_parser_init(&dummy);
301         f->open = ogg_open;
302         f->close = ogg_close;
303         f->pre_select = generic_filter_pre_select;
304         f->post_select = ogg_post_select;
305         f->parse_config = oggdec_parse_config;
306         f->free_config = oggdec_free_config;
307         f->execute = oggdec_execute;
308         f->help = (struct ggo_help) {
309                 .short_help = oggdec_filter_args_info_help,
310                 .detailed_help = oggdec_filter_args_info_detailed_help
311         };
312 }