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 (*fn->fc->input_error < 0)
129 return *fn->fc->input_error;
131 PARA_DEBUG_LOG("initial input buffer %zd/%d, "
132 "waiting for more data\n", len, ib);
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 */
142 return -E_OGGDEC_READ;
143 if (ret == OV_ENOTVORBIS)
144 return -E_OGGDEC_NOTVORBIS;
145 if (ret == OV_EVERSION)
146 return -E_OGGDEC_VERSION;
147 if (ret == OV_EBADHEADER)
148 return -E_OGGDEC_BADHEADER;
150 return -E_OGGDEC_FAULT;
151 fn->fc->channels = ov_info(pod->vf, 0)->channels;
152 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
153 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
156 while (fn->loaded < fn->bufsize) {
157 int length = fn->bufsize - fn->loaded;
158 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
159 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
160 if (read_ret == OV_HOLE || !read_ret)
161 return pod->converted;
163 return -E_OGGDEC_BADLINK;
164 fn->loaded += read_ret;
166 return pod->converted;
169 static int oggdec_parse_config(int argc, char **argv, void **config)
172 struct oggdec_filter_args_info *ogg_conf;
174 ogg_conf = para_calloc(sizeof(*ogg_conf));
175 ret = -E_OGGDEC_SYNTAX;
176 if (oggdec_cmdline_parser(argc, argv, ogg_conf))
178 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
179 if (ogg_conf->bufsize_arg < 0)
181 if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
183 if (ogg_conf->initial_buffer_arg < 0)
185 if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
195 * The init function of the ogg vorbis decoder.
197 * \param f Its fields are filled in by the function.
199 void oggdec_filter_init(struct filter *f)
201 struct oggdec_filter_args_info dummy;
203 oggdec_cmdline_parser_init(&dummy);
205 f->close = ogg_close;
206 f->convert = ogg_convert;
207 f->parse_config = oggdec_parse_config;
208 f->help = (struct ggo_help) {
209 .short_help = oggdec_filter_args_info_help,
210 .detailed_help = oggdec_filter_args_info_detailed_help