Merge commit 'meins/master'
[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 "para.h"
10
11 #include "oggdec_filter.cmdline.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "ggo.h"
15 #include "filter.h"
16 #include "error.h"
17 #include "string.h"
18
19 #include <vorbis/vorbisfile.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) {
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 -E_OGGDEC_NOTVORBIS;
150 }
151 if (ret == OV_EREAD)
152 return -E_OGGDEC_READ;
153 if (ret == OV_EVERSION)
154 return -E_OGGDEC_VERSION;
155 if (ret == OV_EBADHEADER)
156 return -E_OGGDEC_BADHEADER;
157 if (ret < 0)
158 return -E_OGGDEC_FAULT;
159 fn->fc->channels = ov_info(pod->vf, 0)->channels;
160 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
161 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
162 fn->fc->samplerate);
163 /* wait a bit to avoid buffer underruns */
164 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
165 return pod->converted;
166 }
167 if (tv_diff(now, &pod->stream_start, NULL) < 0) {
168 PARA_DEBUG_LOG("initial delay..\n");
169 return 0;
170 }
171 while (fn->loaded < fn->bufsize) {
172 int length = fn->bufsize - fn->loaded;
173 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
174 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
175 if (read_ret == 0)
176 return pod->converted;
177 if (read_ret == OV_HOLE) {
178 if (!fn->loaded) {
179 PARA_INFO_LOG("hole, delaying playback\n");
180 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
181 }
182 return pod->converted;
183 }
184 if (read_ret < 0)
185 return -E_OGGDEC_BADLINK;
186 fn->loaded += read_ret;
187 }
188 return pod->converted;
189 }
190
191 static int oggdec_parse_config(int argc, char **argv, void **config)
192 {
193 int ret;
194 struct oggdec_filter_args_info *ogg_conf;
195
196 ogg_conf = para_calloc(sizeof(*ogg_conf));
197 ret = -E_OGGDEC_SYNTAX;
198 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
199 goto err;
200 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
201 if (ogg_conf->bufsize_arg < 0)
202 goto err;
203 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
204 goto err;
205 if (ogg_conf->initial_buffer_arg < 0)
206 goto err;
207 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
208 goto err;
209 *config = ogg_conf;
210 return 1;
211 err:
212 free(ogg_conf);
213 return ret;
214 }
215
216 /**
217 * The init function of the ogg vorbis decoder.
218 *
219 * \param f Its fields are filled in by the function.
220 */
221 void oggdec_filter_init(struct filter *f)
222 {
223 struct oggdec_filter_args_info dummy;
224
225 oggdec_cmdline_parser_init(&dummy);
226 f->open = ogg_open;
227 f->close = ogg_close;
228 f->convert = ogg_convert;
229 f->parse_config = oggdec_parse_config;
230 f->help = (struct ggo_help) {
231 .short_help = oggdec_filter_args_info_help,
232 .detailed_help = oggdec_filter_args_info_detailed_help
233 };
234 }