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