1cd1f02853e2a1fec99c66de57514f18d680c959
[paraslash.git] / oggdec.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 /** \file oggdec.c paraslash's ogg vorbis decoder */
20
21 #include "para.h"
22
23 #include "oggdec_filter.cmdline.h"
24 #include "list.h"
25 #include "sched.h"
26 #include "filter.h"
27 #include "error.h"
28 #include "string.h"
29
30 #include <vorbis/vorbisfile.h>
31
32 /** \cond some internal constants */
33 #define BITS 16
34 #ifdef WORDS_BIGENDIAN
35 #define ENDIAN 1
36 #else
37 #define ENDIAN 0
38 #endif
39 #define SIGN 1
40 /** \endcond */
41
42 /** data specific to the oggdec filter */
43 struct private_oggdec_data {
44 /** describes an ogg vorbis file */
45 OggVorbis_File *vf;
46 /** the input buffer */
47 char *inbuf;
48 /** the length of \a inbuf */
49 size_t inbuf_len;
50 /** the number of bytes consumed from the input buffer */
51 size_t converted;
52 };
53
54 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
55 {
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;
60
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)
65 return 0;
66 errno = EAGAIN;
67 return (size_t)-1;
68 }
69 ret = PARA_MIN(nmemb, have / size) * size;
70 memcpy(buf, p, ret);
71 pod->converted += ret;
72 return ret;
73 }
74
75 /*
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).
79 */
80 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
81 __a_unused int whence)
82 {
83 return -1;
84 }
85
86 static int cb_close(__a_unused void *datasource)
87 {
88 return 0;
89 }
90
91 static const ov_callbacks ovc = {
92 .read_func = cb_read,
93 .seek_func = cb_seek,
94 .close_func = cb_close,
95 /*
96 * The tell function need not be provided if
97 * the data IO abstraction is not seekable
98 */
99 .tell_func = NULL
100 };
101
102 static void ogg_open(struct filter_node *fn)
103 {
104 struct private_oggdec_data *pod = para_calloc(
105 sizeof(struct private_oggdec_data));
106 struct oggdec_filter_args_info *conf = fn->conf;
107
108 fn->private_data = pod;
109 fn->bufsize = conf->bufsize_arg * 1024;
110 fn->buf = para_malloc(fn->bufsize);
111 }
112
113 static void ogg_close(struct filter_node *fn)
114 {
115 struct private_oggdec_data *pod = fn->private_data;
116 if (pod->vf) {
117 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
118 ov_clear(pod->vf);
119 free(pod->vf);
120 pod->vf = NULL;
121 } else
122 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
123 free(fn->buf);
124 fn->buf = NULL;
125 free(fn->private_data);
126 fn->private_data = NULL;
127 }
128
129 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
130 {
131 ssize_t ret;
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;
137 pod->converted = 0;
138
139 if (!pod->vf) {
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);
144 return 0;
145 }
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 */
152 if (ret == OV_EREAD)
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;
160 if (ret < 0)
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,
165 fn->fc->samplerate);
166 }
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;
173 if (read_ret < 0)
174 return -E_OGGDEC_BADLINK;
175 fn->loaded += read_ret;
176 }
177 return pod->converted;
178 }
179
180 static void *oggdec_parse_config(int argc, char **argv)
181 {
182 struct oggdec_filter_args_info *ret = para_calloc(sizeof(struct oggdec_filter_args_info));
183 if (!oggdec_cmdline_parser(argc, argv, ret))
184 return ret;
185 free(ret);
186 return NULL;
187 }
188
189 /** the init function of the ogg vorbis decoder */
190 void oggdec_init(struct filter *f)
191 {
192 f->open = ogg_open;
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;
197 }