3de62cbd7d494dd39b84b42006a5a43797550c91
[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 };
43
44 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
45 {
46 struct filter_node *fn = datasource;
47 struct private_oggdec_data *pod = fn->private_data;
48 size_t ret, have = pod->inbuf_len - pod->converted;
49 char *p = pod->inbuf + pod->converted;
50
51 // PARA_DEBUG_LOG("pod = %p\n", pod);
52 // PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
53 if (pod->inbuf_len < size) {
54 if (*fn->fc->input_error)
55 return 0;
56 errno = EAGAIN;
57 return (size_t)-1;
58 }
59 ret = PARA_MIN(nmemb, have / size) * size;
60 memcpy(buf, p, ret);
61 pod->converted += ret;
62 return ret;
63 }
64
65 /*
66 * Custom data seeking function.
67 *
68 * Since we want the data source to be treated as unseekable at all
69 * times, the provided seek callback always returns -1 (failure).
70 */
71 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
72 __a_unused int whence)
73 {
74 return -1;
75 }
76
77 static int cb_close(__a_unused void *datasource)
78 {
79 return 0;
80 }
81
82 static const ov_callbacks ovc = {
83 .read_func = cb_read,
84 .seek_func = cb_seek,
85 .close_func = cb_close,
86 /*
87 * The tell function need not be provided if the data IO abstraction is
88 * not seekable
89 */
90 .tell_func = NULL
91 };
92
93 static void ogg_open(struct filter_node *fn)
94 {
95 struct private_oggdec_data *pod = para_calloc(
96 sizeof(struct private_oggdec_data));
97 struct oggdec_filter_args_info *conf = fn->conf;
98
99 fn->private_data = pod;
100 fn->bufsize = conf->bufsize_arg * 1024;
101 fn->buf = para_malloc(fn->bufsize);
102 }
103
104 static void ogg_close(struct filter_node *fn)
105 {
106 struct private_oggdec_data *pod = fn->private_data;
107 if (pod->vf) {
108 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
109 ov_clear(pod->vf);
110 free(pod->vf);
111 pod->vf = NULL;
112 } else
113 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
114 free(fn->buf);
115 fn->buf = NULL;
116 free(fn->private_data);
117 fn->private_data = NULL;
118 }
119
120 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
121 {
122 ssize_t ret;
123 struct private_oggdec_data *pod = fn->private_data;
124 struct oggdec_filter_args_info *conf = fn->conf;
125 /* make the buffer known to the read callback cb_read() */
126 pod->inbuf = inbuffer;
127 pod->inbuf_len = len;
128 pod->converted = 0;
129
130 if (!pod->vf) {
131 if (*fn->fc->input_error < 0)
132 return *fn->fc->input_error;
133 if (!len)
134 return 0;
135 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
136 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
137 ret = ov_open_callbacks(fn, pod->vf,
138 NULL, /* no initial buffer */
139 0, /* no initial bytes */
140 ovc); /* the ov_open_callbacks */
141 if (ret == OV_ENOTVORBIS || ret == OV_EBADHEADER) {
142 /* this might be due to the input buffer being too small */
143 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
144 if (len < ib) {
145 PARA_INFO_LOG("initial input buffer %zd/%d, "
146 "waiting for more data\n", len, ib);
147 free(pod->vf);
148 pod->vf = NULL;
149 return 0;
150 }
151 return ret == OV_ENOTVORBIS?
152 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
153 }
154 if (ret == OV_EREAD)
155 return -E_OGGDEC_READ;
156 if (ret == OV_EVERSION)
157 return -E_OGGDEC_VERSION;
158 if (ret < 0)
159 return -E_OGGDEC_FAULT;
160 fn->fc->channels = ov_info(pod->vf, 0)->channels;
161 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
162 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
163 fn->fc->samplerate);
164 /* wait a bit to avoid buffer underruns */
165 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
166 return pod->converted;
167 }
168 if (tv_diff(now, &pod->stream_start, NULL) < 0) {
169 PARA_DEBUG_LOG("initial delay..\n");
170 return 0;
171 }
172 while (fn->loaded < fn->bufsize) {
173 int length = fn->bufsize - fn->loaded;
174 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
175 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
176 if (read_ret == 0)
177 return pod->converted;
178 if (read_ret == OV_HOLE) {
179 if (!fn->loaded) {
180 PARA_INFO_LOG("hole, delaying playback\n");
181 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
182 }
183 return pod->converted;
184 }
185 if (read_ret < 0)
186 return -E_OGGDEC_BADLINK;
187 fn->loaded += read_ret;
188 }
189 return pod->converted;
190 }
191
192 static int oggdec_parse_config(int argc, char **argv, void **config)
193 {
194 int ret;
195 struct oggdec_filter_args_info *ogg_conf;
196
197 ogg_conf = para_calloc(sizeof(*ogg_conf));
198 ret = -E_OGGDEC_SYNTAX;
199 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
200 goto err;
201 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
202 if (ogg_conf->bufsize_arg < 0)
203 goto err;
204 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
205 goto err;
206 if (ogg_conf->initial_buffer_arg < 0)
207 goto err;
208 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
209 goto err;
210 *config = ogg_conf;
211 return 1;
212 err:
213 free(ogg_conf);
214 return ret;
215 }
216
217 /**
218 * The init function of the ogg vorbis decoder.
219 *
220 * \param f Its fields are filled in by the function.
221 */
222 void oggdec_filter_init(struct filter *f)
223 {
224 struct oggdec_filter_args_info dummy;
225
226 oggdec_cmdline_parser_init(&dummy);
227 f->open = ogg_open;
228 f->close = ogg_close;
229 f->convert = ogg_convert;
230 f->parse_config = oggdec_parse_config;
231 f->help = (struct ggo_help) {
232 .short_help = oggdec_filter_args_info_help,
233 .detailed_help = oggdec_filter_args_info_detailed_help
234 };
235 }