2 * Copyright (C) 2005-2008 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 /** Determine byte sex. */
21 #ifdef WORDS_BIGENDIAN
27 /** Data specific to the oggdec filter. */
28 struct private_oggdec_data {
29 /** Describes an ogg vorbis file. */
31 /** The input buffer. */
33 /** The length of \a inbuf. */
35 /** The number of bytes consumed from the input buffer. */
39 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
41 struct filter_node *fn = datasource;
42 struct private_oggdec_data *pod = fn->private_data;
43 size_t ret, have = pod->inbuf_len - pod->converted;
44 char *p = pod->inbuf + pod->converted;
46 // PARA_DEBUG_LOG("pod = %p\n", pod);
47 // PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
48 if (pod->inbuf_len < size) {
49 if (*fn->fc->input_error)
54 ret = PARA_MIN(nmemb, have / size) * size;
56 pod->converted += ret;
61 * Custom data seeking function.
63 * Since we want the data source to be treated as unseekable at all
64 * times, the provided seek callback always returns -1 (failure).
66 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
67 __a_unused int whence)
72 static int cb_close(__a_unused void *datasource)
77 static const ov_callbacks ovc = {
80 .close_func = cb_close,
82 * The tell function need not be provided if the data IO abstraction is
88 static void ogg_open(struct filter_node *fn)
90 struct private_oggdec_data *pod = para_calloc(
91 sizeof(struct private_oggdec_data));
92 struct oggdec_filter_args_info *conf = fn->conf;
94 fn->private_data = pod;
95 fn->bufsize = conf->bufsize_arg * 1024;
96 fn->buf = para_malloc(fn->bufsize);
99 static void ogg_close(struct filter_node *fn)
101 struct private_oggdec_data *pod = fn->private_data;
103 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
108 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
111 free(fn->private_data);
112 fn->private_data = NULL;
115 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
118 struct private_oggdec_data *pod = fn->private_data;
119 struct oggdec_filter_args_info *conf = fn->conf;
120 /* make the buffer known to the read callback cb_read() */
121 pod->inbuf = inbuffer;
122 pod->inbuf_len = len;
126 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
127 if (len <ib && !*fn->fc->input_error) {
128 PARA_DEBUG_LOG("initial input buffer %zd/%d, "
129 "waiting for more data\n", len, ib);
132 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
133 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
134 ret = ov_open_callbacks(fn, pod->vf,
135 NULL, /* no initial buffer */
136 0, /* no initial bytes */
137 ovc); /* the ov_open_callbacks */
139 return -E_OGGDEC_READ;
140 if (ret == OV_ENOTVORBIS)
141 return -E_OGGDEC_NOTVORBIS;
142 if (ret == OV_EVERSION)
143 return -E_OGGDEC_VERSION;
144 if (ret == OV_EBADHEADER)
145 return -E_OGGDEC_BADHEADER;
147 return -E_OGGDEC_FAULT;
148 fn->fc->channels = ov_info(pod->vf, 0)->channels;
149 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
150 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
153 while (!*fn->fc->input_error && fn->loaded < fn->bufsize) {
154 int length = fn->bufsize - fn->loaded;
155 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
156 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
157 if (read_ret == OV_HOLE || !read_ret)
158 return pod->converted;
160 return -E_OGGDEC_BADLINK;
161 fn->loaded += read_ret;
163 return pod->converted;
166 static void *oggdec_parse_config(int argc, char **argv)
168 struct oggdec_filter_args_info *ret = para_calloc(sizeof(struct oggdec_filter_args_info));
169 if (!oggdec_cmdline_parser(argc, argv, ret))
176 * The init function of the ogg vorbis decoder.
178 * \param f Its fields are filled in by the function.
180 void oggdec_init(struct filter *f)
183 f->close = ogg_close;
184 f->convert = ogg_convert;
185 f->print_help = oggdec_cmdline_parser_print_help;
186 f->parse_config = oggdec_parse_config;