1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * feeder_format: New generation of generic, any-to-any format converter, as
31 * long as the sample values can be read _and_ write.
32 */
33
34 #ifdef _KERNEL
35 #ifdef HAVE_KERNEL_OPTION_HEADERS
36 #include "opt_snd.h"
37 #endif
38 #include <dev/sound/pcm/sound.h>
39 #include <dev/sound/pcm/pcm.h>
40 #include <dev/sound/pcm/g711.h>
41 #include <dev/sound/pcm/intpcm.h>
42 #include "feeder_if.h"
43
44 #define SND_USE_FXDIV
45 #include "snd_fxdiv_gen.h"
46 #endif
47
48 #define FEEDFORMAT_RESERVOIR (SND_CHN_MAX * PCM_32_BPS)
49
50 INTPCM_DECLARE(intpcm_conv_tables)
51
52 struct feed_format_info {
53 uint32_t ibps, obps;
54 uint32_t ialign, oalign, channels;
55 intpcm_read_t *read;
56 intpcm_write_t *write;
57 uint8_t reservoir[FEEDFORMAT_RESERVOIR];
58 };
59
60 /*
61 * dummy ac3/dts passthrough, etc.
62 * XXX assume as s16le.
63 */
64 static __inline intpcm_t
intpcm_read_null(uint8_t * src __unused)65 intpcm_read_null(uint8_t *src __unused)
66 {
67
68 return (0);
69 }
70
71 static __inline void
intpcm_write_null(uint8_t * dst,intpcm_t v __unused)72 intpcm_write_null(uint8_t *dst, intpcm_t v __unused)
73 {
74
75 _PCM_WRITE_S16_LE(dst, 0);
76 }
77
78 #define FEEDFORMAT_ENTRY(SIGN, BIT, ENDIAN) \
79 { \
80 AFMT_##SIGN##BIT##_##ENDIAN, \
81 intpcm_read_##SIGN##BIT##ENDIAN, \
82 intpcm_write_##SIGN##BIT##ENDIAN \
83 }
84
85 static const struct {
86 uint32_t format;
87 intpcm_read_t *read;
88 intpcm_write_t *write;
89 } feed_format_ops[] = {
90 FEEDFORMAT_ENTRY(S, 8, NE),
91 FEEDFORMAT_ENTRY(S, 16, LE),
92 FEEDFORMAT_ENTRY(S, 24, LE),
93 FEEDFORMAT_ENTRY(S, 32, LE),
94 FEEDFORMAT_ENTRY(S, 16, BE),
95 FEEDFORMAT_ENTRY(S, 24, BE),
96 FEEDFORMAT_ENTRY(S, 32, BE),
97 FEEDFORMAT_ENTRY(U, 8, NE),
98 FEEDFORMAT_ENTRY(U, 16, LE),
99 FEEDFORMAT_ENTRY(U, 24, LE),
100 FEEDFORMAT_ENTRY(U, 32, LE),
101 FEEDFORMAT_ENTRY(U, 16, BE),
102 FEEDFORMAT_ENTRY(U, 24, BE),
103 FEEDFORMAT_ENTRY(U, 32, BE),
104 {
105 AFMT_MU_LAW,
106 intpcm_read_ulaw, intpcm_write_ulaw
107 },
108 {
109 AFMT_A_LAW,
110 intpcm_read_alaw, intpcm_write_alaw
111 },
112 {
113 AFMT_AC3,
114 intpcm_read_null, intpcm_write_null
115 }
116 };
117
118 static int
feed_format_init(struct pcm_feeder * f)119 feed_format_init(struct pcm_feeder *f)
120 {
121 struct feed_format_info *info;
122 intpcm_read_t *rd_op;
123 intpcm_write_t *wr_op;
124 size_t i;
125
126 if (f->desc->in == f->desc->out ||
127 AFMT_CHANNEL(f->desc->in) != AFMT_CHANNEL(f->desc->out))
128 return (EINVAL);
129
130 rd_op = NULL;
131 wr_op = NULL;
132
133 for (i = 0; i < nitems(feed_format_ops) &&
134 (rd_op == NULL || wr_op == NULL); i++) {
135 if (rd_op == NULL &&
136 AFMT_ENCODING(f->desc->in) == feed_format_ops[i].format)
137 rd_op = feed_format_ops[i].read;
138 if (wr_op == NULL &&
139 AFMT_ENCODING(f->desc->out) == feed_format_ops[i].format)
140 wr_op = feed_format_ops[i].write;
141 }
142
143 if (rd_op == NULL || wr_op == NULL) {
144 printf("%s(): failed to initialize io ops "
145 "in=0x%08x out=0x%08x\n",
146 __func__, f->desc->in, f->desc->out);
147 return (EINVAL);
148 }
149
150 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO);
151 if (info == NULL)
152 return (ENOMEM);
153
154 info->channels = AFMT_CHANNEL(f->desc->in);
155
156 info->ibps = AFMT_BPS(f->desc->in);
157 info->ialign = info->ibps * info->channels;
158 info->read = rd_op;
159
160 info->obps = AFMT_BPS(f->desc->out);
161 info->oalign = info->obps * info->channels;
162 info->write = wr_op;
163
164 f->data = info;
165
166 return (0);
167 }
168
169 static int
feed_format_free(struct pcm_feeder * f)170 feed_format_free(struct pcm_feeder *f)
171 {
172 struct feed_format_info *info;
173
174 info = f->data;
175 if (info != NULL)
176 free(info, M_DEVBUF);
177
178 f->data = NULL;
179
180 return (0);
181 }
182
183 static int
feed_format_set(struct pcm_feeder * f,int what,int value)184 feed_format_set(struct pcm_feeder *f, int what, int value)
185 {
186 struct feed_format_info *info;
187
188 info = f->data;
189
190 switch (what) {
191 case FEEDFORMAT_CHANNELS:
192 if (value < SND_CHN_MIN || value > SND_CHN_MAX)
193 return (EINVAL);
194 info->channels = (uint32_t)value;
195 info->ialign = info->ibps * info->channels;
196 info->oalign = info->obps * info->channels;
197 break;
198 default:
199 return (EINVAL);
200 break;
201 }
202
203 return (0);
204 }
205
206 static int
feed_format_feed(struct pcm_feeder * f,struct pcm_channel * c,uint8_t * b,uint32_t count,void * source)207 feed_format_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
208 uint32_t count, void *source)
209 {
210 struct feed_format_info *info;
211 intpcm_t v;
212 uint32_t j;
213 uint8_t *src, *dst;
214
215 info = f->data;
216 dst = b;
217 count = SND_FXROUND(count, info->oalign);
218
219 do {
220 if (count < info->oalign)
221 break;
222
223 if (count < info->ialign) {
224 src = info->reservoir;
225 j = info->ialign;
226 } else {
227 if (info->ialign == info->oalign)
228 j = count;
229 else if (info->ialign > info->oalign)
230 j = SND_FXROUND(count, info->ialign);
231 else
232 j = SND_FXDIV(count, info->oalign) *
233 info->ialign;
234 src = dst + count - j;
235 }
236
237 j = SND_FXDIV(FEEDER_FEED(f->source, c, src, j, source),
238 info->ialign);
239 if (j == 0)
240 break;
241
242 j *= info->channels;
243 count -= j * info->obps;
244
245 do {
246 v = info->read(src);
247 info->write(dst, v);
248 dst += info->obps;
249 src += info->ibps;
250 } while (--j != 0);
251
252 } while (count != 0);
253
254 return (dst - b);
255 }
256
257 static struct pcm_feederdesc feeder_format_desc[] = {
258 { FEEDER_FORMAT, 0, 0, 0, 0 },
259 { 0, 0, 0, 0, 0 }
260 };
261
262 static kobj_method_t feeder_format_methods[] = {
263 KOBJMETHOD(feeder_init, feed_format_init),
264 KOBJMETHOD(feeder_free, feed_format_free),
265 KOBJMETHOD(feeder_set, feed_format_set),
266 KOBJMETHOD(feeder_feed, feed_format_feed),
267 KOBJMETHOD_END
268 };
269
270 FEEDER_DECLARE(feeder_format, NULL);
271
272 /* Extern */
273 intpcm_read_t *
feeder_format_read_op(uint32_t format)274 feeder_format_read_op(uint32_t format)
275 {
276 size_t i;
277
278 for (i = 0; i < nitems(feed_format_ops); i++) {
279 if (AFMT_ENCODING(format) == feed_format_ops[i].format)
280 return (feed_format_ops[i].read);
281 }
282
283 return (NULL);
284 }
285
286 intpcm_write_t *
feeder_format_write_op(uint32_t format)287 feeder_format_write_op(uint32_t format)
288 {
289 size_t i;
290
291 for (i = 0; i < nitems(feed_format_ops); i++) {
292 if (AFMT_ENCODING(format) == feed_format_ops[i].format)
293 return (feed_format_ops[i].write);
294 }
295
296 return (NULL);
297 }
298