Merge commit /fml/ag-raetsch/home/maan/tmp/paraslash_meins/paraslash/ of HEAD
[paraslash.git] / oggdec.c
1 /*
2  * Copyright (C) 2005-2006 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 #define ENDIAN 0
35 #define SIGN 1
36 /** \endcond */
37
38 /** data specific to the oggdec filter */
39 struct private_oggdec_data {
40         /** describes an ogg vorbis file */
41         OggVorbis_File *vf;
42         /** the input buffer */
43         char *inbuf;
44         /** the length of \a inbuf */
45         size_t inbuf_len;
46         /** the number of bytes consumed from the input buffer */
47         size_t converted;
48 };
49
50 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
51 {
52         struct filter_node *fn = datasource;
53         struct private_oggdec_data *pod = fn->private_data;
54         size_t ret, have = pod->inbuf_len - pod->converted;
55         char *p = pod->inbuf + pod->converted;
56
57 //      PARA_DEBUG_LOG("pod = %p\n", pod);
58 //      PARA_DEBUG_LOG("vorbis requests %d bytes, have %d\n", size * nmemb, have);
59         if (pod->inbuf_len < size) {
60                 if (*fn->fc->input_eof)
61                         return 0;
62                 errno = EAGAIN;
63                 return -1;
64         }
65         ret = PARA_MIN(nmemb, have / size) * size;
66         memcpy(buf, p, ret);
67         pod->converted += ret;
68         return ret;
69 }
70
71 /*
72  * cb_seek -- custom data seeking function
73  * Since we want the data source to be treated as unseekable at all
74  * times, the provided seek callback always returns -1 (failure).
75  */
76 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
77                 __a_unused int whence)
78 {
79         return -1;
80 }
81
82 static int cb_close(__a_unused void *datasource)
83 {
84         return 0;
85 }
86
87 static const ov_callbacks ovc = {
88         .read_func = cb_read,
89         .seek_func = cb_seek,
90         .close_func = cb_close,
91         /*
92          * The tell function need not be provided if
93          * the data IO abstraction is not seekable
94          */
95         .tell_func = NULL
96 };
97
98 static void ogg_open(struct filter_node *fn)
99 {
100         struct private_oggdec_data *pod = para_calloc(
101                 sizeof(struct private_oggdec_data));
102         struct oggdec_filter_args_info *conf = fn->conf;
103
104         fn->private_data = pod;
105         fn->bufsize = conf->bufsize_arg * 1024;
106         fn->buf = para_malloc(fn->bufsize);
107 }
108
109 static void ogg_close(struct filter_node *fn)
110 {
111         struct private_oggdec_data *pod = fn->private_data;
112         if (pod->vf) {
113                 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
114                 ov_clear(pod->vf);
115                 free(pod->vf);
116                 pod->vf = NULL;
117         } else
118                 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
119         free(fn->buf);
120         fn->buf = NULL;
121         free(fn->private_data);
122         fn->private_data = NULL;
123 }
124
125 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
126 {
127         ssize_t ret;
128         struct private_oggdec_data *pod = fn->private_data;
129         struct oggdec_filter_args_info *conf = fn->conf;
130         /* make the buffer known to the read callback cb_read() */
131         pod->inbuf = inbuffer;
132         pod->inbuf_len = len;
133         pod->converted = 0;
134
135         if (!pod->vf) {
136                 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
137                 if (len <ib && !*fn->fc->input_eof) {
138                         PARA_DEBUG_LOG("initial input buffer %zd/%d, "
139                                 "waiting for more data\n", len, ib);
140                         return 0;
141                 }
142                 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
143                 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
144                 ret = ov_open_callbacks(fn, pod->vf,
145                         NULL, /* no initial buffer */
146                         0, /* no initial bytes */
147                         ovc); /* the ov_open_callbacks */
148                 if (ret == OV_EREAD)
149                         return -E_OGGDEC_READ;
150                 if (ret == OV_ENOTVORBIS)
151                         return -E_OGGDEC_NOTVORBIS;
152                 if (ret == OV_EVERSION)
153                         return -E_OGGDEC_VERSION;
154                 if (ret == OV_EBADHEADER)
155                         return -E_OGGDEC_BADHEADER;
156                 if (ret < 0)
157                         return -E_OGGDEC_FAULT;
158                 fn->fc->channels = ov_info(pod->vf, 0)->channels;
159                 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
160                 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels, fn->fc->samplerate);
161         }
162 again:
163         ret = ov_read(pod->vf, fn->buf + fn->loaded, fn->bufsize - fn->loaded,
164                 ENDIAN, BITS / 8, SIGN, NULL);
165         if (ret == OV_HOLE || !ret) {
166                 return pod->converted;
167         }
168         if (ret < 0)
169                 return -E_OGGDEC_BADLINK;
170         fn->loaded += ret;
171         if (!*fn->fc->input_eof && fn->loaded < fn->bufsize)
172                 goto again;
173         return pod->converted;
174 }
175
176 static void *oggdec_parse_config(int argc, char **argv)
177 {
178         struct oggdec_filter_args_info *ret = para_calloc(sizeof(struct oggdec_filter_args_info));
179         if (!oggdec_cmdline_parser(argc, argv, ret))
180                 return ret;
181         free(ret);
182         return NULL;
183 }
184
185 /** the init function of the ogg vorbis decoder */
186 void oggdec_init(struct filter *f)
187 {
188         f->open = ogg_open;
189         f->close = ogg_close;
190         f->convert = ogg_convert;
191         f->print_help = oggdec_cmdline_parser_print_help;
192         f->parse_config = oggdec_parse_config;
193 }