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. */
11 #include "oggdec_filter.cmdline.h"
19 #include <vorbis/vorbisfile.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. */
40 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
42 struct filter_node *fn = datasource;
43 struct private_oggdec_data *pod = fn->private_data;
44 size_t ret, have = pod->inbuf_len - pod->converted;
45 char *p = pod->inbuf + pod->converted;
47 // PARA_DEBUG_LOG("pod = %p\n", pod);
48 // PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
49 if (pod->inbuf_len < size) {
50 if (*fn->fc->input_error)
55 ret = PARA_MIN(nmemb, have / size) * size;
57 pod->converted += ret;
62 * Custom data seeking function.
64 * Since we want the data source to be treated as unseekable at all
65 * times, the provided seek callback always returns -1 (failure).
67 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
68 __a_unused int whence)
73 static int cb_close(__a_unused void *datasource)
78 static const ov_callbacks ovc = {
81 .close_func = cb_close,
83 * The tell function need not be provided if the data IO abstraction is
89 static void ogg_open(struct filter_node *fn)
91 struct private_oggdec_data *pod = para_calloc(
92 sizeof(struct private_oggdec_data));
93 struct oggdec_filter_args_info *conf = fn->conf;
95 fn->private_data = pod;
96 fn->bufsize = conf->bufsize_arg * 1024;
97 fn->buf = para_malloc(fn->bufsize);
100 static void ogg_close(struct filter_node *fn)
102 struct private_oggdec_data *pod = fn->private_data;
104 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
109 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
112 free(fn->private_data);
113 fn->private_data = NULL;
116 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
119 struct private_oggdec_data *pod = fn->private_data;
120 struct oggdec_filter_args_info *conf = fn->conf;
121 /* make the buffer known to the read callback cb_read() */
122 pod->inbuf = inbuffer;
123 pod->inbuf_len = len;
127 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
128 if (len <ib && !*fn->fc->input_error) {
129 PARA_DEBUG_LOG("initial input buffer %zd/%d, "
130 "waiting for more data\n", len, ib);
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 */
140 return -E_OGGDEC_READ;
141 if (ret == OV_ENOTVORBIS)
142 return -E_OGGDEC_NOTVORBIS;
143 if (ret == OV_EVERSION)
144 return -E_OGGDEC_VERSION;
145 if (ret == OV_EBADHEADER)
146 return -E_OGGDEC_BADHEADER;
148 return -E_OGGDEC_FAULT;
149 fn->fc->channels = ov_info(pod->vf, 0)->channels;
150 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
151 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
154 while (fn->loaded < fn->bufsize) {
155 int length = fn->bufsize - fn->loaded;
156 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
157 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
158 if (read_ret == OV_HOLE || !read_ret)
159 return pod->converted;
161 return -E_OGGDEC_BADLINK;
162 fn->loaded += read_ret;
164 return pod->converted;
167 static int oggdec_parse_config(int argc, char **argv, void **config)
170 struct oggdec_filter_args_info *ogg_conf;
172 ogg_conf = para_calloc(sizeof(*ogg_conf));
173 ret = -E_OGGDEC_SYNTAX;
174 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
176 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
177 if (ogg_conf->bufsize_arg < 0)
179 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
181 if (ogg_conf->initial_buffer_arg < 0)
183 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
193 * The init function of the ogg vorbis decoder.
195 * \param f Its fields are filled in by the function.
197 void oggdec_filter_init(struct filter *f)
199 struct oggdec_filter_args_info dummy;
201 oggdec_cmdline_parser_init(&dummy);
203 f->close = ogg_close;
204 f->convert = ogg_convert;
205 f->parse_config = oggdec_parse_config;
206 f->help = (struct ggo_help) {
207 .short_help = oggdec_filter_args_info_help,
208 .detailed_help = oggdec_filter_args_info_detailed_help