Fix com_stat() for arguments greater than one.
[paraslash.git] / oggdec.c
1 /*
2 * Copyright (C) 2005-2008 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_error)
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 * Custom data seeking function.
62 *
63 * Since we want the data source to be treated as unseekable at all
64 * times, the provided seek callback always returns -1 (failure).
65 */
66 static int cb_seek(__a_unused void *datasource, __a_unused ogg_int64_t offset,
67 __a_unused int whence)
68 {
69 return -1;
70 }
71
72 static int cb_close(__a_unused void *datasource)
73 {
74 return 0;
75 }
76
77 static const ov_callbacks ovc = {
78 .read_func = cb_read,
79 .seek_func = cb_seek,
80 .close_func = cb_close,
81 /*
82 * The tell function need not be provided if the data IO abstraction is
83 * not seekable
84 */
85 .tell_func = NULL
86 };
87
88 static void ogg_open(struct filter_node *fn)
89 {
90 struct private_oggdec_data *pod = para_calloc(
91 sizeof(struct private_oggdec_data));
92 struct oggdec_filter_args_info *conf = fn->conf;
93
94 fn->private_data = pod;
95 fn->bufsize = conf->bufsize_arg * 1024;
96 fn->buf = para_malloc(fn->bufsize);
97 }
98
99 static void ogg_close(struct filter_node *fn)
100 {
101 struct private_oggdec_data *pod = fn->private_data;
102 if (pod->vf) {
103 PARA_DEBUG_LOG("ov_clearing %p, pod = %p\n", pod->vf, pod);
104 ov_clear(pod->vf);
105 free(pod->vf);
106 pod->vf = NULL;
107 } else
108 PARA_DEBUG_LOG("nothing to close in fc %p, pod = %p\n", pod->vf, pod);
109 free(fn->buf);
110 fn->buf = NULL;
111 free(fn->private_data);
112 fn->private_data = NULL;
113 }
114
115 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
116 {
117 ssize_t ret;
118 struct private_oggdec_data *pod = fn->private_data;
119 struct oggdec_filter_args_info *conf = fn->conf;
120 /* make the buffer known to the read callback cb_read() */
121 pod->inbuf = inbuffer;
122 pod->inbuf_len = len;
123 pod->converted = 0;
124
125 if (!pod->vf) {
126 int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
127 if (len <ib && !*fn->fc->input_error) {
128 PARA_DEBUG_LOG("initial input buffer %zd/%d, "
129 "waiting for more data\n", len, ib);
130 return 0;
131 }
132 pod->vf = para_malloc(sizeof(struct OggVorbis_File));
133 PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
134 ret = ov_open_callbacks(fn, pod->vf,
135 NULL, /* no initial buffer */
136 0, /* no initial bytes */
137 ovc); /* the ov_open_callbacks */
138 if (ret == OV_EREAD)
139 return -E_OGGDEC_READ;
140 if (ret == OV_ENOTVORBIS)
141 return -E_OGGDEC_NOTVORBIS;
142 if (ret == OV_EVERSION)
143 return -E_OGGDEC_VERSION;
144 if (ret == OV_EBADHEADER)
145 return -E_OGGDEC_BADHEADER;
146 if (ret < 0)
147 return -E_OGGDEC_FAULT;
148 fn->fc->channels = ov_info(pod->vf, 0)->channels;
149 fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
150 PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
151 fn->fc->samplerate);
152 }
153 while (!*fn->fc->input_error && fn->loaded < fn->bufsize) {
154 int length = fn->bufsize - fn->loaded;
155 long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
156 ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
157 if (read_ret == OV_HOLE || !read_ret)
158 return pod->converted;
159 if (read_ret < 0)
160 return -E_OGGDEC_BADLINK;
161 fn->loaded += read_ret;
162 }
163 return pod->converted;
164 }
165
166 static void *oggdec_parse_config(int argc, char **argv)
167 {
168 struct oggdec_filter_args_info *ret = para_calloc(sizeof(struct oggdec_filter_args_info));
169 if (!oggdec_cmdline_parser(argc, argv, ret))
170 return ret;
171 free(ret);
172 return NULL;
173 }
174
175 /**
176 * The init function of the ogg vorbis decoder.
177 *
178 * \param f Its fields are filled in by the function.
179 */
180 void oggdec_init(struct filter *f)
181 {
182 f->open = ogg_open;
183 f->close = ogg_close;
184 f->convert = ogg_convert;
185 f->print_help = oggdec_cmdline_parser_print_help;
186 f->parse_config = oggdec_parse_config;
187 }