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>
14 #include "oggdec_filter.cmdline.h"
18 #include "buffer_tree.h"
23 /** Determine byte sex. */
24 #ifdef WORDS_BIGENDIAN
30 /** Data specific to the oggdec filter. */
31 struct private_oggdec_data {
32 /** Describes an ogg vorbis file. */
34 /** The input buffer. */
36 /** The length of \a inbuf. */
38 /** The number of bytes consumed from the input buffer. */
40 /** When to start producing output. */
41 struct timeval stream_start;
44 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
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;
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)
59 ret = PARA_MIN(nmemb, have / size) * size;
61 pod->converted += ret;
66 * Custom data seeking function.
68 * Since we want the data source to be treated as unseekable at all
69 * times, the provided seek callback always returns -1 (failure).
71 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
72 __a_unused int whence)
77 static int cb_close(__a_unused void *datasource)
82 static const ov_callbacks ovc = {
85 .close_func = cb_close,
87 * The tell function need not be provided if the data IO abstraction is
93 static void ogg_open(struct filter_node *fn)
95 struct private_oggdec_data *pod = para_calloc(
96 sizeof(struct private_oggdec_data));
97 struct oggdec_filter_args_info *conf = fn->conf;
99 fn->private_data = pod;
100 fn->bufsize = conf->bufsize_arg * 1024;
101 fn->buf = para_malloc(fn->bufsize);
104 static void ogg_close(struct filter_node *fn)
106 struct private_oggdec_data *pod = fn->private_data;
108 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
113 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
116 free(fn->private_data);
117 fn->private_data = NULL;
120 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
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;
131 if (*fn->fc->input_error < 0)
132 return *fn->fc->input_error;
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 */
145 PARA_INFO_LOG("initial input buffer %zd/%d, "
146 "waiting for more data\n", len, ib);
151 return ret == OV_ENOTVORBIS?
152 -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
155 return -E_OGGDEC_READ;
156 if (ret == OV_EVERSION)
157 return -E_OGGDEC_VERSION;
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,
164 /* wait a bit to avoid buffer underruns */
165 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
166 return pod->converted;
168 if (tv_diff(now, &pod->stream_start, NULL) < 0) {
169 PARA_DEBUG_LOG("initial delay..\n");
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);
177 return pod->converted;
178 if (read_ret == OV_HOLE) {
180 PARA_INFO_LOG("hole, delaying playback\n");
181 tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
183 return pod->converted;
186 return -E_OGGDEC_BADLINK;
187 fn->loaded += read_ret;
189 return pod->converted;
192 static int oggdec_parse_config(int argc, char **argv, void **config)
195 struct oggdec_filter_args_info *ogg_conf;
197 ogg_conf = para_calloc(sizeof(*ogg_conf));
198 ret = -E_OGGDEC_SYNTAX;
199 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
201 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
202 if (ogg_conf->bufsize_arg < 0)
204 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
206 if (ogg_conf->initial_buffer_arg < 0)
208 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
218 * The init function of the ogg vorbis decoder.
220 * \param f Its fields are filled in by the function.
222 void oggdec_filter_init(struct filter *f)
224 struct oggdec_filter_args_info dummy;
226 oggdec_cmdline_parser_init(&dummy);
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