oggdec fixes.
[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 input buffer. */
35         char *inbuf;
36         /** The length of \a inbuf. */
37         size_t inbuf_len;
38         /** The number of bytes consumed from the input buffer. */
39         size_t converted;
40         /** When to start producing output. */
41         struct timeval stream_start;
42         /** The number of channels of the current stream. */
43         unsigned int channels;
44         /** Current sample rate in Hz. */
45         unsigned int samplerate;
46 };
47
48 static size_t cb_read_nobtr(void *buf, size_t size, size_t nmemb, void *datasource)
49 {
50         struct filter_node *fn = datasource;
51         struct private_oggdec_data *pod = fn->private_data;
52         size_t ret, have = pod->inbuf_len - pod->converted;
53         char *p = pod->inbuf + pod->converted;
54
55 //      PARA_DEBUG_LOG("pod = %p\n", pod);
56 //      PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
57         if (pod->inbuf_len < size) {
58                 if (*fn->fc->input_error)
59                         return 0;
60                 errno = EAGAIN;
61                 return (size_t)-1;
62         }
63         ret = PARA_MIN(nmemb, have / size) * size;
64         memcpy(buf, p, ret);
65         pod->converted += ret;
66         return ret;
67 }
68
69 static size_t cb_read_btr(void *buf, size_t size, size_t nmemb, void *datasource)
70 {
71         struct filter_node *fn = datasource;
72         struct private_oggdec_data *pod = fn->private_data;
73         struct btr_node *btrn = fn->btrn;
74         char *btr_buf;
75         size_t nbytes = btr_next_buffer(btrn, &btr_buf), tmp;
76
77         /**
78          * oggvorbis always uses size == 1. Other sizes would complicate the code
79          * for no real gain. So we simply don't support size != 1.
80          */
81         assert(size == 1);
82         assert(pod->converted <= nbytes);
83         tmp = nbytes - pod->converted;
84         PARA_DEBUG_LOG("vorbis requests %zu bytes have %zu\n", nmemb, tmp);
85         tmp = PARA_MIN(tmp, nmemb);
86         if (tmp == 0)
87                 return 0;
88         memcpy(buf, btr_buf + pod->converted, tmp);
89         pod->converted += tmp;
90         return tmp;
91 }
92
93 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
94 {
95         struct filter_node *fn = datasource;
96
97         if (fn->btrn)
98                 return cb_read_btr(buf, size, nmemb, datasource);
99         else
100                 return cb_read_nobtr(buf, size, nmemb, datasource);
101 }
102
103 /*
104  * Custom data seeking function.
105  *
106  * Since we want the data source to be treated as unseekable at all
107  * times, the provided seek callback always returns -1 (failure).
108  */
109 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
110                 __a_unused int whence)
111 {
112         return -1;
113 }
114
115 static int cb_close(__a_unused void *datasource)
116 {
117         return 0;
118 }
119
120 static const ov_callbacks ovc = {
121         .read_func = cb_read,
122         .seek_func = cb_seek,
123         .close_func = cb_close,
124         /*
125          * The tell function need not be provided if the data IO abstraction is
126          * not seekable
127          */
128         .tell_func = NULL
129 };
130
131 static void ogg_open(struct filter_node *fn)
132 {
133         struct private_oggdec_data *pod = para_calloc(
134                 sizeof(struct private_oggdec_data));
135         struct oggdec_filter_args_info *conf = fn->conf;
136
137         fn->private_data = pod;
138         fn->bufsize = conf->bufsize_arg * 1024;
139         fn->buf = para_malloc(fn->bufsize);
140         fn->min_iqs = 8000;
141 }
142
143 static void ogg_close(struct filter_node *fn)
144 {
145         struct private_oggdec_data *pod = fn->private_data;
146         if (pod->vf) {
147                 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
148                 ov_clear(pod->vf);
149                 free(pod->vf);
150                 pod->vf = NULL;
151         } else
152                 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
153         free(fn->buf);
154         fn->buf = NULL;
155         free(fn->private_data);
156         fn->private_data = NULL;
157 }
158
159 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
160
161 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
162 {
163         struct filter_node *fn = btr_context(btrn);
164         struct private_oggdec_data *pod = fn->private_data;
165
166         if (!strcmp(cmd, "samplerate")) {
167                 if (pod->samplerate == 0)
168                         return -ERRNO_TO_PARA_ERROR(ENAVAIL);
169                 *result = make_message("%u", pod->samplerate);
170                 return 1;
171         }
172         if (!strcmp(cmd, "channels")) {
173                 if (pod->channels == 0)
174                         return -ERRNO_TO_PARA_ERROR(ENAVAIL);
175                 *result = make_message("%u", pod->channels);
176                 return 1;
177         }
178         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
179 }
180
181 static int ogg_init(struct filter_node *fn)
182 {
183         struct private_oggdec_data *pod = fn->private_data;
184         struct btr_node *btrn = fn->btrn;
185         int ret, oret;
186         size_t iqs;
187         struct timeval delay = {0, 500 * 1000};
188
189         pod->vf = para_malloc(sizeof(struct OggVorbis_File));
190         PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
191                 btr_get_input_queue_size(btrn), fn->min_iqs);
192 open:
193         oret = ov_open_callbacks(fn, pod->vf,
194                 NULL, /* no initial buffer */
195                 0, /* no initial bytes */
196                 ovc); /* the ov_open_callbacks */
197         if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
198                 /* this might be due to the input buffer being too small */
199                 if (!btr_no_parent(btrn)) {
200                         fn->min_iqs += 1000;
201                         iqs = btr_get_input_queue_size(btrn);
202                         ret = 0;
203                         if (iqs < fn->min_iqs)
204                                 goto out;
205                         PARA_CRIT_LOG("iqs: %zu\n", iqs);
206                         btr_merge(btrn, fn->min_iqs);
207                         pod->converted = 0;
208                         goto open;
209                 }
210                 ret = (oret == OV_ENOTVORBIS)?
211                         -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
212                 goto out;
213         }
214         ret = -E_OGGDEC_READ;
215         if (oret == OV_EREAD)
216                 goto out;
217         ret = -E_OGGDEC_VERSION;
218         if (oret == OV_EVERSION)
219                 goto out;
220         ret = -E_OGGDEC_FAULT;
221         if (oret < 0)
222                 goto out;
223         pod->channels = ov_info(pod->vf, 0)->channels;
224         pod->samplerate = ov_info(pod->vf, 0)->rate;
225         PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
226                 pod->samplerate);
227         /* wait a bit to avoid buffer underruns */
228         tv_add(now, &delay, &pod->stream_start);
229         ret = 1;
230 out:
231         if (ret <= 0) {
232                 free(pod->vf);
233                 pod->vf = NULL;
234         } else {
235                 btr_consume(btrn, pod->converted);
236                 pod->converted = 0;
237                 fn->min_iqs = 0;
238         }
239         return ret;
240 }
241
242 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
243 {
244         struct filter_node *fn = container_of(t, struct filter_node, task);
245         struct private_oggdec_data *pod = fn->private_data;
246         struct btr_node *btrn = fn->btrn;
247         size_t iqs, len;
248         int ret;
249         char *in;
250
251         pod->converted = 0;
252         t->error = 0;
253         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
254         if (ret <= 0)
255                 goto out;
256         btr_merge(btrn, fn->min_iqs);
257         len = btr_next_buffer(btrn, &in);
258         iqs = btr_get_input_queue_size(btrn);
259         if (!pod->vf) {
260                 ret = ogg_init(fn);
261                 if (ret <= 0)
262                         goto out;
263         }
264         for (;;) {
265                 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
266                 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
267                         ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
268                 btr_consume(btrn, pod->converted);
269                 pod->converted = 0;
270                 if (read_ret <= 0)
271                         free(out);
272                 if (read_ret == 0) {
273                         if (btr_no_parent(btrn))
274                                 ret = -E_OGGDEC_EOF;
275                         else
276                                 ret = 0;
277                         goto out;
278                 }
279                 ret = 0;
280                 if (read_ret == OV_HOLE)
281                         goto out;
282                 ret = -E_OGGDEC_BADLINK;
283                 if (read_ret < 0)
284                         goto out;
285                 btr_add_output(out, read_ret, btrn);
286                 if (btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL) == 0)
287                         return; /* enough data for the moment */
288         }
289 out:
290         if (ret < 0) {
291                 t->error = ret;
292                 btr_remove_node(btrn);
293         }
294 }
295
296 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
297 {
298         ssize_t ret;
299         struct private_oggdec_data *pod = fn->private_data;
300         struct oggdec_filter_args_info *conf = fn->conf;
301         /* make the buffer known to the read callback cb_read() */
302         pod->inbuf = inbuffer;
303         pod->inbuf_len = len;
304         pod->converted = 0;
305
306         if (!pod->vf) {
307                 if (*fn->fc->input_error < 0)
308                         return *fn->fc->input_error;
309                 if (!len)
310                         return 0;
311                 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
312                 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
313                 ret = ov_open_callbacks(fn, pod->vf,
314                         NULL, /* no initial buffer */
315                         0, /* no initial bytes */
316                         ovc); /* the ov_open_callbacks */
317                 if (ret == OV_ENOTVORBIS || ret == OV_EBADHEADER) {
318                         /* this might be due to the input buffer being too small */
319                         int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
320                         if (len < ib) {
321                                 PARA_INFO_LOG("initial input buffer %zd/%d, "
322                                         "waiting for more data\n", len, ib);
323                                 free(pod->vf);
324                                 pod->vf = NULL;
325                                 return 0;
326                         }
327                         return ret == OV_ENOTVORBIS?
328                                 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
329                 }
330                 if (ret == OV_EREAD)
331                         return -E_OGGDEC_READ;
332                 if (ret == OV_EVERSION)
333                         return -E_OGGDEC_VERSION;
334                 if (ret < 0)
335                         return -E_OGGDEC_FAULT;
336                 fn->fc->channels = ov_info(pod->vf, 0)->channels;
337                 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
338                 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
339                         fn->fc->samplerate);
340                 /* wait a bit to avoid buffer underruns */
341                 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
342                 return pod->converted;
343         }
344         if (tv_diff(now, &pod->stream_start, NULL) < 0) {
345                 PARA_DEBUG_LOG("initial delay..\n");
346                 return 0;
347         }
348         while (fn->loaded < fn->bufsize) {
349                 int length = fn->bufsize - fn->loaded;
350                 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
351                         ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
352                 if (read_ret == 0)
353                         return pod->converted;
354                 if (read_ret == OV_HOLE) {
355                         if (!fn->loaded) {
356                                 PARA_INFO_LOG("hole, delaying playback\n");
357                                 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
358                         }
359                         return pod->converted;
360                 }
361                 if (read_ret < 0)
362                         return -E_OGGDEC_BADLINK;
363                 fn->loaded += read_ret;
364         }
365         return pod->converted;
366 }
367
368 static int oggdec_parse_config(int argc, char **argv, void **config)
369 {
370         int ret;
371         struct oggdec_filter_args_info *ogg_conf;
372
373         ogg_conf = para_calloc(sizeof(*ogg_conf));
374         ret = -E_OGGDEC_SYNTAX;
375         if (oggdec_cmdline_parser(argc, argv, ogg_conf))
376                 goto err;
377         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
378         if (ogg_conf->bufsize_arg < 0)
379                 goto err;
380         if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
381                 goto err;
382         if (ogg_conf->initial_buffer_arg < 0)
383                 goto err;
384         if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
385                 goto err;
386         *config = ogg_conf;
387         return 1;
388 err:
389         free(ogg_conf);
390         return ret;
391 }
392
393 static void oggdec_free_config(void *conf)
394 {
395         oggdec_cmdline_parser_free(conf);
396 }
397
398 /**
399  * The init function of the ogg vorbis decoder.
400  *
401  * \param f Its fields are filled in by the function.
402  */
403 void oggdec_filter_init(struct filter *f)
404 {
405         struct oggdec_filter_args_info dummy;
406
407         oggdec_cmdline_parser_init(&dummy);
408         f->open = ogg_open;
409         f->close = ogg_close;
410         f->convert = ogg_convert;
411         f->pre_select = generic_filter_pre_select;
412         f->post_select = ogg_post_select;
413         f->parse_config = oggdec_parse_config;
414         f->free_config = oggdec_free_config;
415         f->execute = oggdec_execute;
416         f->help = (struct ggo_help) {
417                 .short_help = oggdec_filter_args_info_help,
418                 .detailed_help = oggdec_filter_args_info_detailed_help
419         };
420 }