]> git.tuebingen.mpg.de Git - paraslash.git/blob - oggdec_filter.c
Merge topic branch t/sf_float into pu
[paraslash.git] / oggdec_filter.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file oggdec_filter.c Paraslash's ogg vorbis decoder. */
4
5 #include <regex.h>
6 #include <vorbis/vorbisfile.h>
7
8 #include "para.h"
9 #include "list.h"
10 #include "sched.h"
11 #include "buffer_tree.h"
12 #include "filter.h"
13 #include "error.h"
14 #include "string.h"
15
16 /** Determine byte sex. */
17 #ifdef WORDS_BIGENDIAN
18 #define ENDIAN 1
19 #else
20 #define ENDIAN 0
21 #endif
22
23 /** Data specific to the oggdec filter. */
24 struct private_oggdec_data {
25         /** Describes an ogg vorbis file. */
26         OggVorbis_File *vf;
27         /** The number of bytes consumed from the input buffer. */
28         size_t converted;
29         /** The number of channels of the current stream. */
30         unsigned int channels;
31         /** Current sample rate in Hz. */
32         unsigned int sample_rate;
33         /** Whether everything was decoded during the previous iteration. */
34         bool have_more;
35 };
36
37 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
38 {
39         struct filter_node *fn = datasource;
40         struct private_oggdec_data *pod = fn->private_data;
41         struct btr_node *btrn = fn->btrn;
42         char *btr_buf;
43         size_t nbytes = btr_next_buffer(btrn, &btr_buf), tmp;
44
45         /**
46          * oggvorbis always uses size == 1. Other sizes would complicate the code
47          * for no real gain. So we simply don't support size != 1.
48          */
49         assert(size == 1);
50         assert(pod->converted <= nbytes);
51         tmp = nbytes - pod->converted;
52         PARA_DEBUG_LOG("vorbis requests %zu bytes have %zu\n", nmemb, tmp);
53         tmp = PARA_MIN(tmp, nmemb);
54         if (tmp == 0)
55                 return 0;
56         memcpy(buf, btr_buf + pod->converted, tmp);
57         pod->converted += tmp;
58         return tmp;
59 }
60
61 /*
62  * Custom data seeking function.
63  *
64  * Since we want the data source to be treated as unseekable at all
65  * times, the provided seek callback always returns -1 (failure).
66  */
67 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
68                 __a_unused int whence)
69 {
70         return -1;
71 }
72
73 static int cb_close(__a_unused void *datasource)
74 {
75         return 0;
76 }
77
78 static const ov_callbacks ovc = {
79         .read_func = cb_read,
80         .seek_func = cb_seek,
81         .close_func = cb_close,
82         /*
83          * The tell function need not be provided if the data IO abstraction is
84          * not seekable
85          */
86         .tell_func = NULL
87 };
88
89 static void ogg_open(struct filter_node *fn)
90 {
91         fn->private_data = zalloc(sizeof(struct private_oggdec_data));
92         fn->min_iqs = 8000;
93 }
94
95 static void ogg_close(struct filter_node *fn)
96 {
97         struct private_oggdec_data *pod = fn->private_data;
98
99         if (pod && pod->vf) {
100                 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
101                 ov_clear(pod->vf);
102                 free(pod->vf);
103                 pod->vf = NULL;
104         } else
105                 PARA_DEBUG_LOG("nothing to close\n");
106         free(pod);
107         fn->private_data = NULL;
108 }
109
110 static int oggdec_execute(const struct btr_node *btrn, const char *cmd,
111                 char **result)
112 {
113         struct filter_node *fn = btr_context(btrn);
114         struct private_oggdec_data *pod = fn->private_data;
115
116         return decoder_execute(cmd, pod->sample_rate, pod->channels, result);
117 }
118
119 static int ogg_init(struct filter_node *fn)
120 {
121         struct private_oggdec_data *pod = fn->private_data;
122         struct btr_node *btrn = fn->btrn;
123         int ret, oret;
124         size_t iqs;
125         struct OggVorbis_File *vf = alloc(sizeof(*vf));
126
127         PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
128                 btr_get_input_queue_size(btrn), fn->min_iqs);
129 open:
130         oret = ov_open_callbacks(fn, vf,
131                 NULL, /* no initial buffer */
132                 0, /* no initial bytes */
133                 ovc); /* the ov_open_callbacks */
134         if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
135                 /* maybe the input buffer is too small */
136                 if (!btr_no_parent(btrn)) {
137                         fn->min_iqs += 1000;
138                         iqs = btr_get_input_queue_size(btrn);
139                         ret = 0;
140                         if (iqs < fn->min_iqs)
141                                 goto out;
142                         btr_merge(btrn, fn->min_iqs);
143                         pod->converted = 0;
144                         goto open;
145                 }
146                 ret = (oret == OV_ENOTVORBIS)?
147                         -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
148                 goto out;
149         }
150         ret = -E_OGGDEC_READ;
151         if (oret == OV_EREAD)
152                 goto out;
153         ret = -E_OGGDEC_VERSION;
154         if (oret == OV_EVERSION)
155                 goto out;
156         ret = -E_OGGDEC_FAULT;
157         if (oret < 0)
158                 goto out;
159         pod->channels = ov_info(vf, 0)->channels;
160         pod->sample_rate = ov_info(vf, 0)->rate;
161         PARA_NOTICE_LOG("%u channels, %u Hz\n", pod->channels,
162                 pod->sample_rate);
163         ret = 1;
164 out:
165         if (ret <= 0)
166                 free(vf);
167         else {
168                 btr_consume(btrn, pod->converted);
169                 pod->converted = 0;
170                 fn->min_iqs = 0;
171                 pod->vf = vf;
172                 pod->have_more = true;
173         }
174         return ret;
175 }
176
177 /** Suspend decoding if output queue size is larger than that. */
178 #define OGGDEC_MAX_OUTPUT_SIZE (96 * 1024)
179
180 /**
181   * Allocate chunks of this size and produce at most one chunk of output per
182   * ->post_monitor() invocation. If the buffer could only be filled partially
183   * due to insufficient input being available, it is shrunk to the real output
184   * size and the resized buffer is fed into the output queue.
185   */
186 #define OGGDEC_OUTPUT_CHUNK_SIZE (32 * 1024)
187
188 static void ogg_pre_monitor(struct sched *s, void *context)
189 {
190         struct filter_node *fn = context;
191         struct private_oggdec_data *pod = fn->private_data;
192         struct btr_node *btrn = fn->btrn;
193         int ret;
194
195         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
196         if (ret != 0)
197                 return sched_min_delay(s);
198         if (!pod->have_more)
199                 return;
200         if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
201                 return;
202         sched_min_delay(s);
203 }
204
205 static int ogg_post_monitor(__a_unused struct sched *s, void *context)
206 {
207         struct filter_node *fn = context;
208         struct private_oggdec_data *pod = fn->private_data;
209         struct btr_node *btrn = fn->btrn;
210         int ret, have;
211         char *buf;
212
213         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
214         if (ret < 0) {
215                 if (ret != -E_EOF) /* fatal error */
216                         goto out;
217                 if (fn->min_iqs == 0 && !pod->have_more) /* EOF */
218                         goto out;
219                 /* last ov_read() returned OV_HOLE */
220         } else if (ret == 0 && !pod->have_more) /* nothing to do */
221                 goto out;
222         if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
223                 return 0;
224         if (!pod->vf) {
225                 if (ret <= 0)
226                         goto out;
227                 btr_merge(btrn, fn->min_iqs);
228                 ret = ogg_init(fn);
229                 goto out;
230         }
231         have = 0;
232         buf = alloc(OGGDEC_OUTPUT_CHUNK_SIZE);
233         for (;;) {
234                 ret = ov_read(pod->vf, buf + have, OGGDEC_OUTPUT_CHUNK_SIZE - have,
235                         ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
236                 btr_consume(btrn, pod->converted);
237                 pod->converted = 0;
238                 if (ret <= 0)
239                         break;
240                 fn->min_iqs = 0;
241                 have += ret;
242                 if (have >= OGGDEC_OUTPUT_CHUNK_SIZE)
243                         break;
244         }
245         pod->have_more = (ret > 0);
246         if (have > 0) {
247                 if (have < OGGDEC_OUTPUT_CHUNK_SIZE)
248                         buf = para_realloc(buf, have);
249                 btr_add_output(buf, have, btrn);
250         } else
251                 free(buf);
252         if (ret == OV_HOLE) /* avoid buffer underruns */
253                 fn->min_iqs = 9000;
254         if (ret >= 0 || ret == OV_HOLE)
255                 return 0;
256         ret = -E_OGGDEC_BADLINK;
257 out:
258         if (ret < 0)
259                 btr_remove_node(&fn->btrn);
260         return ret;
261 }
262
263 const struct filter lsg_filter_cmd_com_oggdec_user_data = {
264         .open = ogg_open,
265         .close = ogg_close,
266         .pre_monitor = ogg_pre_monitor,
267         .post_monitor = ogg_post_monitor,
268         .execute = oggdec_execute
269 };