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 || 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 */
143 PARA_INFO_LOG("initial input buffer %zd/%d, "
144 "waiting for more data\n", len, ib);
149 return ret == OV_ENOTVORBIS?
150 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
153 return -E_OGGDEC_READ;
154 if (ret == OV_EVERSION)
155 return -E_OGGDEC_VERSION;
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,
162 /* wait a bit to avoid buffer underruns */
163 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
164 return pod->converted;
166 if (tv_diff(now, &pod->stream_start, NULL) < 0) {
167 PARA_DEBUG_LOG("initial delay..\n");
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);
175 return pod->converted;
176 if (read_ret == OV_HOLE) {
178 PARA_INFO_LOG("hole, delaying playback\n");
179 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
181 return pod->converted;
184 return -E_OGGDEC_BADLINK;
185 fn->loaded += read_ret;
187 return pod->converted;
190 static int oggdec_parse_config(int argc, char **argv, void **config)
193 struct oggdec_filter_args_info *ogg_conf;
195 ogg_conf = para_calloc(sizeof(*ogg_conf));
196 ret = -E_OGGDEC_SYNTAX;
197 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
199 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
200 if (ogg_conf->bufsize_arg < 0)
202 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
204 if (ogg_conf->initial_buffer_arg < 0)
206 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
216 * The init function of the ogg vorbis decoder.
218 * \param f Its fields are filled in by the function.
220 void oggdec_filter_init(struct filter *f)
222 struct oggdec_filter_args_info dummy;
224 oggdec_cmdline_parser_init(&dummy);
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