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