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 /** 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 * cb_seek -- custom data seeking function
62 * Since we want the data source to be treated as unseekable at all
63 * times, the provided seek callback always returns -1 (failure).
65 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
66 __a_unused int whence)
71 static int cb_close(__a_unused void *datasource)
76 static const ov_callbacks ovc = {
79 .close_func = cb_close,
81 * The tell function need not be provided if
82 * the data IO abstraction is not seekable
87 static void ogg_open(struct filter_node *fn)
89 struct private_oggdec_data *pod = para_calloc(
90 sizeof(struct private_oggdec_data));
91 struct oggdec_filter_args_info *conf = fn->conf;
93 fn->private_data = pod;
94 fn->bufsize = conf->bufsize_arg * 1024;
95 fn->buf = para_malloc(fn->bufsize);
98 static void ogg_close(struct filter_node *fn)
100 struct private_oggdec_data *pod = fn->private_data;
102 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
107 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
110 free(fn->private_data);
111 fn->private_data = NULL;
114 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
117 struct private_oggdec_data *pod = fn->private_data;
118 struct oggdec_filter_args_info *conf = fn->conf;
119 /* make the buffer known to the read callback cb_read() */
120 pod->inbuf = inbuffer;
121 pod->inbuf_len = len;
125 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
126 if (len <ib && !*fn->fc->input_error) {
127 PARA_DEBUG_LOG("initial input buffer %zd/%d, "
128 "waiting for more data\n", len, ib);
131 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
132 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
133 ret = ov_open_callbacks(fn, pod->vf,
134 NULL, /* no initial buffer */
135 0, /* no initial bytes */
136 ovc); /* the ov_open_callbacks */
138 return -E_OGGDEC_READ;
139 if (ret == OV_ENOTVORBIS)
140 return -E_OGGDEC_NOTVORBIS;
141 if (ret == OV_EVERSION)
142 return -E_OGGDEC_VERSION;
143 if (ret == OV_EBADHEADER)
144 return -E_OGGDEC_BADHEADER;
146 return -E_OGGDEC_FAULT;
147 fn->fc->channels = ov_info(pod->vf, 0)->channels;
148 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
149 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
152 while (!*fn->fc->input_error && fn->loaded < fn->bufsize) {
153 int length = fn->bufsize - fn->loaded;
154 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
155 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
156 if (read_ret == OV_HOLE || !read_ret)
157 return pod->converted;
159 return -E_OGGDEC_BADLINK;
160 fn->loaded += read_ret;
162 return pod->converted;
165 static void *oggdec_parse_config(int argc, char **argv)
167 struct oggdec_filter_args_info *ret = para_calloc(sizeof(struct oggdec_filter_args_info));
168 if (!oggdec_cmdline_parser(argc, argv, ret))
175 * The init function of the ogg vorbis decoder.
177 * \param f Its fields are filled in by the function.
179 void oggdec_init(struct filter *f)
182 f->close = ogg_close;
183 f->convert = ogg_convert;
184 f->print_help = oggdec_cmdline_parser_print_help;
185 f->parse_config = oggdec_parse_config;