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