Fix ogg streaming.
[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 /** Determine byte sex. */
21 #ifdef WORDS_BIGENDIAN
22 #define ENDIAN 1
23 #else
24 #define ENDIAN 0
25 #endif
26
27 /** Data specific to the oggdec filter. */
28 struct private_oggdec_data {
29 /** Describes an ogg vorbis file. */
30 OggVorbis_File *vf;
31 /** The input buffer. */
32 char *inbuf;
33 /** The length of \a inbuf. */
34 size_t inbuf_len;
35 /** The number of bytes consumed from the input buffer. */
36 size_t converted;
37 };
38
39 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
40 {
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;
45
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_eof)
50 return 0;
51 errno = EAGAIN;
52 return (size_t)-1;
53 }
54 ret = PARA_MIN(nmemb, have / size) * size;
55 memcpy(buf, p, ret);
56 pod->converted += ret;
57 return ret;
58 }
59
60 /*
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).
64 */
65 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
66 __a_unused int whence)
67 {
68 return -1;
69 }
70
71 static int cb_close(__a_unused void *datasource)
72 {
73 return 0;
74 }
75
76 static const ov_callbacks ovc = {
77 .read_func = cb_read,
78 .seek_func = cb_seek,
79 .close_func = cb_close,
80 /*
81 * The tell function need not be provided if
82 * the data IO abstraction is not seekable
83 */
84 .tell_func = NULL
85 };
86
87 static void ogg_open(struct filter_node *fn)
88 {
89 struct private_oggdec_data *pod = para_calloc(
90 sizeof(struct private_oggdec_data));
91 struct oggdec_filter_args_info *conf = fn->conf;
92
93 fn->private_data = pod;
94 fn->bufsize = conf->bufsize_arg * 1024;
95 fn->buf = para_malloc(fn->bufsize);
96 }
97
98 static void ogg_close(struct filter_node *fn)
99 {
100 struct private_oggdec_data *pod = fn->private_data;
101 if (pod->vf) {
102 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
103 ov_clear(pod->vf);
104 free(pod->vf);
105 pod->vf = NULL;
106 } else
107 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
108 free(fn->buf);
109 fn->buf = NULL;
110 free(fn->private_data);
111 fn->private_data = NULL;
112 }
113
114 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
115 {
116 ssize_t ret;
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;
122 pod->converted = 0;
123
124 if (!pod->vf) {
125 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
126 if (len <ib && !*fn->fc->input_eof) {
127 PARA_DEBUG_LOG("initial input buffer %zd/%d, "
128 "waiting for more data\n", len, ib);
129 return 0;
130 }
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 */
137 if (ret == OV_EREAD)
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;
145 if (ret < 0)
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,
150 fn->fc->samplerate);
151 }
152 while (!*fn->fc->input_eof && 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;
158 if (read_ret < 0)
159 return -E_OGGDEC_BADLINK;
160 fn->loaded += read_ret;
161 }
162 return pod->converted;
163 }
164
165 static void *oggdec_parse_config(int argc, char **argv)
166 {
167 struct oggdec_filter_args_info *ret = para_calloc(sizeof(struct oggdec_filter_args_info));
168 if (!oggdec_cmdline_parser(argc, argv, ret))
169 return ret;
170 free(ret);
171 return NULL;
172 }
173
174 /**
175 * The init function of the ogg vorbis decoder.
176 *
177 * \param f Its fields are filled in by the function.
178 */
179 void oggdec_init(struct filter *f)
180 {
181 f->open = ogg_open;
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;
186 }