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