2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file oggdec_filter.c Paraslash's ogg vorbis decoder. */
10 #include <vorbis/vorbisfile.h>
13 #include "oggdec_filter.cmdline.h"
21 /** Determine byte sex. */
22 #ifdef WORDS_BIGENDIAN
28 /** Data specific to the oggdec filter. */
29 struct private_oggdec_data {
30 /** Describes an ogg vorbis file. */
32 /** The input buffer. */
34 /** The length of \a inbuf. */
36 /** The number of bytes consumed from the input buffer. */
38 /** When to start producing output. */
39 struct timeval stream_start;
42 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
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;
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)
57 ret = PARA_MIN(nmemb, have / size) * size;
59 pod->converted += ret;
64 * Custom data seeking function.
66 * Since we want the data source to be treated as unseekable at all
67 * times, the provided seek callback always returns -1 (failure).
69 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
70 __a_unused int whence)
75 static int cb_close(__a_unused void *datasource)
80 static const ov_callbacks ovc = {
83 .close_func = cb_close,
85 * The tell function need not be provided if the data IO abstraction is
91 static void ogg_open(struct filter_node *fn)
93 struct private_oggdec_data *pod = para_calloc(
94 sizeof(struct private_oggdec_data));
95 struct oggdec_filter_args_info *conf = fn->conf;
97 fn->private_data = pod;
98 fn->bufsize = conf->bufsize_arg * 1024;
99 fn->buf = para_malloc(fn->bufsize);
102 static void ogg_close(struct filter_node *fn)
104 struct private_oggdec_data *pod = fn->private_data;
106 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
111 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
114 free(fn->private_data);
115 fn->private_data = NULL;
118 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
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;
129 if (*fn->fc->input_error < 0)
130 return *fn->fc->input_error;
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 */
143 PARA_INFO_LOG("initial input buffer %zd/%d, "
144 "waiting for more data\n", len, ib);
149 return -E_OGGDEC_NOTVORBIS;
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;
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,
163 /* wait a bit to avoid buffer underruns */
164 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
165 return pod->converted;
167 if (tv_diff(now, &pod->stream_start, NULL) < 0) {
168 PARA_DEBUG_LOG("initial delay..\n");
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);
176 return pod->converted;
177 if (read_ret == OV_HOLE) {
179 PARA_INFO_LOG("hole, delaying playback\n");
180 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
182 return pod->converted;
185 return -E_OGGDEC_BADLINK;
186 fn->loaded += read_ret;
188 return pod->converted;
191 static int oggdec_parse_config(int argc, char **argv, void **config)
194 struct oggdec_filter_args_info *ogg_conf;
196 ogg_conf = para_calloc(sizeof(*ogg_conf));
197 ret = -E_OGGDEC_SYNTAX;
198 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
200 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
201 if (ogg_conf->bufsize_arg < 0)
203 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
205 if (ogg_conf->initial_buffer_arg < 0)
207 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
217 * The init function of the ogg vorbis decoder.
219 * \param f Its fields are filled in by the function.
221 void oggdec_filter_init(struct filter *f)
223 struct oggdec_filter_args_info dummy;
225 oggdec_cmdline_parser_init(&dummy);
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