Change year in COPYRIGHT to 2010.
[paraslash.git] / oggdec_filter.c
1 /*
2  * Copyright (C) 2005-2010 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         /** The number of channels of the current stream. */
37         unsigned int channels;
38         /** Current sample rate in Hz. */
39         unsigned int samplerate;
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         if (!strcmp(cmd, "samplerate")) {
125                 if (pod->samplerate == 0)
126                         return -E_BTR_NAVAIL;
127                 *result = make_message("%u", pod->samplerate);
128                 return 1;
129         }
130         if (!strcmp(cmd, "channels")) {
131                 if (pod->channels == 0)
132                         return -E_BTR_NAVAIL;
133                 *result = make_message("%u", pod->channels);
134                 return 1;
135         }
136         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
137 }
138
139 static int ogg_init(struct filter_node *fn)
140 {
141         struct private_oggdec_data *pod = fn->private_data;
142         struct btr_node *btrn = fn->btrn;
143         int ret, oret;
144         size_t iqs;
145
146         pod->vf = para_malloc(sizeof(struct OggVorbis_File));
147         PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
148                 btr_get_input_queue_size(btrn), fn->min_iqs);
149 open:
150         oret = ov_open_callbacks(fn, pod->vf,
151                 NULL, /* no initial buffer */
152                 0, /* no initial bytes */
153                 ovc); /* the ov_open_callbacks */
154         if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
155                 /* this might be due to the input buffer being too small */
156                 if (!btr_no_parent(btrn)) {
157                         fn->min_iqs += 1000;
158                         iqs = btr_get_input_queue_size(btrn);
159                         ret = 0;
160                         if (iqs < fn->min_iqs)
161                                 goto out;
162                         PARA_CRIT_LOG("iqs: %zu\n", iqs);
163                         btr_merge(btrn, fn->min_iqs);
164                         pod->converted = 0;
165                         goto open;
166                 }
167                 ret = (oret == OV_ENOTVORBIS)?
168                         -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
169                 goto out;
170         }
171         ret = -E_OGGDEC_READ;
172         if (oret == OV_EREAD)
173                 goto out;
174         ret = -E_OGGDEC_VERSION;
175         if (oret == OV_EVERSION)
176                 goto out;
177         ret = -E_OGGDEC_FAULT;
178         if (oret < 0)
179                 goto out;
180         pod->channels = ov_info(pod->vf, 0)->channels;
181         pod->samplerate = ov_info(pod->vf, 0)->rate;
182         PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
183                 pod->samplerate);
184         ret = 1;
185 out:
186         if (ret <= 0) {
187                 free(pod->vf);
188                 pod->vf = NULL;
189         } else {
190                 btr_consume(btrn, pod->converted);
191                 pod->converted = 0;
192                 fn->min_iqs = 0;
193         }
194         return ret;
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         size_t iqs, len;
203         int ret;
204         char *in;
205
206         pod->converted = 0;
207         t->error = 0;
208         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
209         if (ret <= 0)
210                 goto out;
211         btr_merge(btrn, fn->min_iqs);
212         len = btr_next_buffer(btrn, &in);
213         iqs = btr_get_input_queue_size(btrn);
214         if (!pod->vf) {
215                 ret = ogg_init(fn);
216                 if (ret <= 0)
217                         goto out;
218         }
219         for (;;) {
220                 char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
221                 ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
222                         ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
223                 btr_consume(btrn, pod->converted);
224                 pod->converted = 0;
225                 if (read_ret <= 0)
226                         free(out);
227                 if (read_ret == 0) {
228                         if (btr_no_parent(btrn))
229                                 ret = -E_OGGDEC_EOF;
230                         else
231                                 ret = 0;
232                         goto out;
233                 }
234                 ret = 0;
235                 if (read_ret == OV_HOLE)
236                         goto out;
237                 ret = -E_OGGDEC_BADLINK;
238                 if (read_ret < 0)
239                         goto out;
240                 btr_add_output(out, read_ret, btrn);
241                 if (btr_get_output_queue_size(btrn) > 640 * 1024)
242                         return; /* enough data for the moment */
243         }
244 out:
245         if (ret < 0) {
246                 t->error = ret;
247                 btr_remove_node(btrn);
248         }
249 }
250
251 static int oggdec_parse_config(int argc, char **argv, void **config)
252 {
253         int ret;
254         struct oggdec_filter_args_info *ogg_conf;
255
256         ogg_conf = para_calloc(sizeof(*ogg_conf));
257         ret = -E_OGGDEC_SYNTAX;
258         if (oggdec_cmdline_parser(argc, argv, ogg_conf))
259                 goto err;
260         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
261         if (ogg_conf->bufsize_arg < 0)
262                 goto err;
263         if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
264                 goto err;
265         if (ogg_conf->initial_buffer_arg < 0)
266                 goto err;
267         if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
268                 goto err;
269         *config = ogg_conf;
270         return 1;
271 err:
272         free(ogg_conf);
273         return ret;
274 }
275
276 static void oggdec_free_config(void *conf)
277 {
278         oggdec_cmdline_parser_free(conf);
279 }
280
281 /**
282  * The init function of the ogg vorbis decoder.
283  *
284  * \param f Its fields are filled in by the function.
285  */
286 void oggdec_filter_init(struct filter *f)
287 {
288         struct oggdec_filter_args_info dummy;
289
290         oggdec_cmdline_parser_init(&dummy);
291         f->open = ogg_open;
292         f->close = ogg_close;
293         f->pre_select = generic_filter_pre_select;
294         f->post_select = ogg_post_select;
295         f->parse_config = oggdec_parse_config;
296         f->free_config = oggdec_free_config;
297         f->execute = oggdec_execute;
298         f->help = (struct ggo_help) {
299                 .short_help = oggdec_filter_args_info_help,
300                 .detailed_help = oggdec_filter_args_info_detailed_help
301         };
302 }