2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file oggdec.c paraslash's ogg vorbis decoder */
11 #include "oggdec_filter.cmdline.h"
18 #include <vorbis/vorbisfile.h>
20 /** \cond some internal constants */
22 #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 */
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_eof)
57 ret = PARA_MIN(nmemb, have / size) * size;
59 pod->converted += ret;
64 * cb_seek -- custom data seeking function
65 * Since we want the data source to be treated as unseekable at all
66 * times, the provided seek callback always returns -1 (failure).
68 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
69 __a_unused int whence)
74 static int cb_close(__a_unused void *datasource)
79 static const ov_callbacks ovc = {
82 .close_func = cb_close,
84 * The tell function need not be provided if
85 * the data IO abstraction is not seekable
90 static void ogg_open(struct filter_node *fn)
92 struct private_oggdec_data *pod = para_calloc(
93 sizeof(struct private_oggdec_data));
94 struct oggdec_filter_args_info *conf = fn->conf;
96 fn->private_data = pod;
97 fn->bufsize = conf->bufsize_arg * 1024;
98 fn->buf = para_malloc(fn->bufsize);
101 static void ogg_close(struct filter_node *fn)
103 struct private_oggdec_data *pod = fn->private_data;
105 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
110 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
113 free(fn->private_data);
114 fn->private_data = NULL;
117 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
120 struct private_oggdec_data *pod = fn->private_data;
121 struct oggdec_filter_args_info *conf = fn->conf;
122 /* make the buffer known to the read callback cb_read() */
123 pod->inbuf = inbuffer;
124 pod->inbuf_len = len;
128 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
129 if (len <ib && !*fn->fc->input_eof) {
130 PARA_DEBUG_LOG("initial input buffer %zd/%d, "
131 "waiting for more data\n", len, ib);
134 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
135 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
136 ret = ov_open_callbacks(fn, pod->vf,
137 NULL, /* no initial buffer */
138 0, /* no initial bytes */
139 ovc); /* the ov_open_callbacks */
141 return -E_OGGDEC_READ;
142 if (ret == OV_ENOTVORBIS)
143 return -E_OGGDEC_NOTVORBIS;
144 if (ret == OV_EVERSION)
145 return -E_OGGDEC_VERSION;
146 if (ret == OV_EBADHEADER)
147 return -E_OGGDEC_BADHEADER;
149 return -E_OGGDEC_FAULT;
150 fn->fc->channels = ov_info(pod->vf, 0)->channels;
151 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
152 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
155 while (!*fn->fc->input_eof && fn->loaded < fn->bufsize) {
156 int length = fn->bufsize - fn->loaded;
157 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
158 ENDIAN, BITS / 8, SIGN, NULL);
159 if (read_ret == OV_HOLE || !read_ret)
160 return pod->converted;
162 return -E_OGGDEC_BADLINK;
163 fn->loaded += read_ret;
165 return pod->converted;
168 static void *oggdec_parse_config(int argc, char **argv)
170 struct oggdec_filter_args_info *ret = para_calloc(sizeof(struct oggdec_filter_args_info));
171 if (!oggdec_cmdline_parser(argc, argv, ret))
177 /** the init function of the ogg vorbis decoder */
178 void oggdec_init(struct filter *f)
181 f->close = ogg_close;
182 f->convert = ogg_convert;
183 f->print_help = oggdec_cmdline_parser_print_help;
184 f->parse_config = oggdec_parse_config;