string.c: Fix typos in comment.
[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 = para_calloc(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(struct btr_node *btrn, const char *cmd, char **result)
111 {
112         struct filter_node *fn = btr_context(btrn);
113         struct private_oggdec_data *pod = fn->private_data;
114
115         return decoder_execute(cmd, pod->sample_rate, pod->channels, result);
116 }
117
118 static int ogg_init(struct filter_node *fn)
119 {
120         struct private_oggdec_data *pod = fn->private_data;
121         struct btr_node *btrn = fn->btrn;
122         int ret, oret;
123         size_t iqs;
124         struct OggVorbis_File *vf = para_malloc(sizeof(*vf));
125
126         PARA_NOTICE_LOG("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
127                 btr_get_input_queue_size(btrn), fn->min_iqs);
128 open:
129         oret = ov_open_callbacks(fn, vf,
130                 NULL, /* no initial buffer */
131                 0, /* no initial bytes */
132                 ovc); /* the ov_open_callbacks */
133         if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
134                 /* maybe the input buffer is too small */
135                 if (!btr_no_parent(btrn)) {
136                         fn->min_iqs += 1000;
137                         iqs = btr_get_input_queue_size(btrn);
138                         ret = 0;
139                         if (iqs < fn->min_iqs)
140                                 goto out;
141                         btr_merge(btrn, fn->min_iqs);
142                         pod->converted = 0;
143                         goto open;
144                 }
145                 ret = (oret == OV_ENOTVORBIS)?
146                         -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
147                 goto out;
148         }
149         ret = -E_OGGDEC_READ;
150         if (oret == OV_EREAD)
151                 goto out;
152         ret = -E_OGGDEC_VERSION;
153         if (oret == OV_EVERSION)
154                 goto out;
155         ret = -E_OGGDEC_FAULT;
156         if (oret < 0)
157                 goto out;
158         pod->channels = ov_info(vf, 0)->channels;
159         pod->sample_rate = ov_info(vf, 0)->rate;
160         PARA_NOTICE_LOG("%u channels, %u Hz\n", pod->channels,
161                 pod->sample_rate);
162         ret = 1;
163 out:
164         if (ret <= 0)
165                 free(vf);
166         else {
167                 btr_consume(btrn, pod->converted);
168                 pod->converted = 0;
169                 fn->min_iqs = 0;
170                 pod->vf = vf;
171                 pod->have_more = true;
172         }
173         return ret;
174 }
175
176 /** Suspend decoding if output queue size is larger than that. */
177 #define OGGDEC_MAX_OUTPUT_SIZE (96 * 1024)
178
179 /**
180   * Allocate chunks of this size and produce at most one chunk of output per
181   * ->post_select() invocation. If the buffer could only be filled partially
182   * due to insufficient input being available, it is shrunk to the real output
183   * size and the resized buffer is fed into the output queue.
184   */
185 #define OGGDEC_OUTPUT_CHUNK_SIZE (32 * 1024)
186
187 static void ogg_pre_select(struct sched *s, void *context)
188 {
189         struct filter_node *fn = context;
190         struct private_oggdec_data *pod = fn->private_data;
191         struct btr_node *btrn = fn->btrn;
192         int ret;
193
194         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
195         if (ret != 0)
196                 return sched_min_delay(s);
197         if (!pod->have_more)
198                 return;
199         if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
200                 return;
201         sched_min_delay(s);
202 }
203
204 static int ogg_post_select(__a_unused struct sched *s, void *context)
205 {
206         struct filter_node *fn = context;
207         struct private_oggdec_data *pod = fn->private_data;
208         struct btr_node *btrn = fn->btrn;
209         int ret, have;
210         char *buf;
211
212         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
213         if (ret < 0) {
214                 if (ret != -E_BTR_EOF) /* fatal error */
215                         goto out;
216                 if (fn->min_iqs == 0 && !pod->have_more) /* EOF */
217                         goto out;
218                 /* last ov_read() returned OV_HOLE */
219         } else if (ret == 0 && !pod->have_more) /* nothing to do */
220                 goto out;
221         if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
222                 return 0;
223         if (!pod->vf) {
224                 if (ret <= 0)
225                         goto out;
226                 btr_merge(btrn, fn->min_iqs);
227                 ret = ogg_init(fn);
228                 goto out;
229         }
230         have = 0;
231         buf = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
232         for (;;) {
233                 ret = ov_read(pod->vf, buf + have, OGGDEC_OUTPUT_CHUNK_SIZE - have,
234                         ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
235                 btr_consume(btrn, pod->converted);
236                 pod->converted = 0;
237                 if (ret <= 0)
238                         break;
239                 fn->min_iqs = 0;
240                 have += ret;
241                 if (have >= OGGDEC_OUTPUT_CHUNK_SIZE)
242                         break;
243         }
244         pod->have_more = (ret > 0);
245         if (have > 0) {
246                 if (have < OGGDEC_OUTPUT_CHUNK_SIZE)
247                         buf = para_realloc(buf, have);
248                 btr_add_output(buf, have, btrn);
249         } else
250                 free(buf);
251         if (ret == OV_HOLE) /* avoid buffer underruns */
252                 fn->min_iqs = 9000;
253         if (ret >= 0 || ret == OV_HOLE)
254                 return 0;
255         ret = -E_OGGDEC_BADLINK;
256 out:
257         if (ret < 0)
258                 btr_remove_node(&fn->btrn);
259         return ret;
260 }
261
262 const struct filter lsg_filter_cmd_com_oggdec_user_data = {
263         .open = ogg_open,
264         .close = ogg_close,
265         .pre_select = ogg_pre_select,
266         .post_select = ogg_post_select,
267         .execute = oggdec_execute
268 };