2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file oggdec.c paraslash's ogg vorbis decoder */
23 #include "oggdec_filter.cmdline.h"
30 #include <vorbis/vorbisfile.h>
32 /** \cond some internal constants */
34 #ifdef WORDS_BIGENDIAN
42 /** data specific to the oggdec filter */
43 struct private_oggdec_data {
44 /** describes an ogg vorbis file */
46 /** the input buffer */
48 /** the length of \a inbuf */
50 /** the number of bytes consumed from the input buffer */
54 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
56 struct filter_node *fn = datasource;
57 struct private_oggdec_data *pod = fn->private_data;
58 size_t ret, have = pod->inbuf_len - pod->converted;
59 char *p = pod->inbuf + pod->converted;
61 // PARA_DEBUG_LOG("pod = %p\n", pod);
62 // PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
63 if (pod->inbuf_len < size) {
64 if (*fn->fc->input_eof)
69 ret = PARA_MIN(nmemb, have / size) * size;
71 pod->converted += ret;
76 * cb_seek -- custom data seeking function
77 * Since we want the data source to be treated as unseekable at all
78 * times, the provided seek callback always returns -1 (failure).
80 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
81 __a_unused int whence)
86 static int cb_close(__a_unused void *datasource)
91 static const ov_callbacks ovc = {
94 .close_func = cb_close,
96 * The tell function need not be provided if
97 * the data IO abstraction is not seekable
102 static void ogg_open(struct filter_node *fn)
104 struct private_oggdec_data *pod = para_calloc(
105 sizeof(struct private_oggdec_data));
106 struct oggdec_filter_args_info *conf = fn->conf;
108 fn->private_data = pod;
109 fn->bufsize = conf->bufsize_arg * 1024;
110 fn->buf = para_malloc(fn->bufsize);
113 static void ogg_close(struct filter_node *fn)
115 struct private_oggdec_data *pod = fn->private_data;
117 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
122 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
125 free(fn->private_data);
126 fn->private_data = NULL;
129 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
132 struct private_oggdec_data *pod = fn->private_data;
133 struct oggdec_filter_args_info *conf = fn->conf;
134 /* make the buffer known to the read callback cb_read() */
135 pod->inbuf = inbuffer;
136 pod->inbuf_len = len;
140 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
141 if (len <ib && !*fn->fc->input_eof) {
142 PARA_DEBUG_LOG("initial input buffer %zd/%d, "
143 "waiting for more data\n", len, ib);
146 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
147 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
148 ret = ov_open_callbacks(fn, pod->vf,
149 NULL, /* no initial buffer */
150 0, /* no initial bytes */
151 ovc); /* the ov_open_callbacks */
153 return -E_OGGDEC_READ;
154 if (ret == OV_ENOTVORBIS)
155 return -E_OGGDEC_NOTVORBIS;
156 if (ret == OV_EVERSION)
157 return -E_OGGDEC_VERSION;
158 if (ret == OV_EBADHEADER)
159 return -E_OGGDEC_BADHEADER;
161 return -E_OGGDEC_FAULT;
162 fn->fc->channels = ov_info(pod->vf, 0)->channels;
163 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
164 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
167 while (!*fn->fc->input_eof && fn->loaded < fn->bufsize) {
168 int length = fn->bufsize - fn->loaded;
169 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
170 ENDIAN, BITS / 8, SIGN, NULL);
171 if (read_ret == OV_HOLE || !read_ret)
172 return pod->converted;
174 return -E_OGGDEC_BADLINK;
175 fn->loaded += read_ret;
177 return pod->converted;
180 static void *oggdec_parse_config(int argc, char **argv)
182 struct oggdec_filter_args_info *ret = para_calloc(sizeof(struct oggdec_filter_args_info));
183 if (!oggdec_cmdline_parser(argc, argv, ret))
189 /** the init function of the ogg vorbis decoder */
190 void oggdec_init(struct filter *f)
193 f->close = ogg_close;
194 f->convert = ogg_convert;
195 f->print_help = oggdec_cmdline_parser_print_help;
196 f->parse_config = oggdec_parse_config;