2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 * feeder_format: New generation of generic, any-to-any format converter, as
31 * long as the sample values can be read _and_ write.
35 #ifdef HAVE_KERNEL_OPTION_HEADERS
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"
45 #include "snd_fxdiv_gen.h"
47 SND_DECLARE_FILE("$FreeBSD$");
50 #define FEEDFORMAT_RESERVOIR (SND_CHN_MAX * PCM_32_BPS)
52 INTPCM_DECLARE(intpcm_conv_tables)
54 struct feed_format_info {
56 uint32_t ialign, oalign, channels;
58 intpcm_write_t *write;
59 uint8_t reservoir[FEEDFORMAT_RESERVOIR];
63 * dummy ac3/dts passthrough, etc.
64 * XXX assume as s16le.
66 static __inline intpcm_t
67 intpcm_read_null(uint8_t *src __unused)
74 intpcm_write_null(uint8_t *dst, intpcm_t v __unused)
77 _PCM_WRITE_S16_LE(dst, 0);
80 #define FEEDFORMAT_ENTRY(SIGN, BIT, ENDIAN) \
82 AFMT_##SIGN##BIT##_##ENDIAN, \
83 intpcm_read_##SIGN##BIT##ENDIAN, \
84 intpcm_write_##SIGN##BIT##ENDIAN \
90 intpcm_write_t *write;
91 } feed_format_ops[] = {
92 FEEDFORMAT_ENTRY(S, 8, NE),
93 FEEDFORMAT_ENTRY(S, 16, LE),
94 FEEDFORMAT_ENTRY(S, 24, LE),
95 FEEDFORMAT_ENTRY(S, 32, LE),
96 FEEDFORMAT_ENTRY(S, 16, BE),
97 FEEDFORMAT_ENTRY(S, 24, BE),
98 FEEDFORMAT_ENTRY(S, 32, BE),
99 FEEDFORMAT_ENTRY(U, 8, NE),
100 FEEDFORMAT_ENTRY(U, 16, LE),
101 FEEDFORMAT_ENTRY(U, 24, LE),
102 FEEDFORMAT_ENTRY(U, 32, LE),
103 FEEDFORMAT_ENTRY(U, 16, BE),
104 FEEDFORMAT_ENTRY(U, 24, BE),
105 FEEDFORMAT_ENTRY(U, 32, BE),
108 intpcm_read_ulaw, intpcm_write_ulaw
112 intpcm_read_alaw, intpcm_write_alaw
116 intpcm_read_null, intpcm_write_null
120 #define FEEDFORMAT_TAB_SIZE \
121 ((int32_t)(sizeof(feed_format_ops) / sizeof(feed_format_ops[0])))
124 feed_format_init(struct pcm_feeder *f)
126 struct feed_format_info *info;
127 intpcm_read_t *rd_op;
128 intpcm_write_t *wr_op;
131 if (f->desc->in == f->desc->out ||
132 AFMT_CHANNEL(f->desc->in) != AFMT_CHANNEL(f->desc->out))
138 for (i = 0; i < FEEDFORMAT_TAB_SIZE &&
139 (rd_op == NULL || wr_op == NULL); i++) {
141 AFMT_ENCODING(f->desc->in) == feed_format_ops[i].format)
142 rd_op = feed_format_ops[i].read;
144 AFMT_ENCODING(f->desc->out) == feed_format_ops[i].format)
145 wr_op = feed_format_ops[i].write;
148 if (rd_op == NULL || wr_op == NULL) {
149 printf("%s(): failed to initialize io ops "
150 "in=0x%08x out=0x%08x\n",
151 __func__, f->desc->in, f->desc->out);
155 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO);
159 info->channels = AFMT_CHANNEL(f->desc->in);
161 info->ibps = AFMT_BPS(f->desc->in);
162 info->ialign = info->ibps * info->channels;
165 info->obps = AFMT_BPS(f->desc->out);
166 info->oalign = info->obps * info->channels;
175 feed_format_free(struct pcm_feeder *f)
177 struct feed_format_info *info;
181 free(info, M_DEVBUF);
189 feed_format_set(struct pcm_feeder *f, int what, int value)
191 struct feed_format_info *info;
196 case FEEDFORMAT_CHANNELS:
197 if (value < SND_CHN_MIN || value > SND_CHN_MAX)
199 info->channels = (uint32_t)value;
200 info->ialign = info->ibps * info->channels;
201 info->oalign = info->obps * info->channels;
212 feed_format_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
213 uint32_t count, void *source)
215 struct feed_format_info *info;
222 count = SND_FXROUND(count, info->oalign);
225 if (count < info->oalign)
228 if (count < info->ialign) {
229 src = info->reservoir;
232 if (info->ialign == info->oalign)
234 else if (info->ialign > info->oalign)
235 j = SND_FXROUND(count, info->ialign);
237 j = SND_FXDIV(count, info->oalign) *
239 src = dst + count - j;
242 j = SND_FXDIV(FEEDER_FEED(f->source, c, src, j, source),
248 count -= j * info->obps;
257 } while (count != 0);
262 static struct pcm_feederdesc feeder_format_desc[] = {
263 { FEEDER_FORMAT, 0, 0, 0, 0 },
267 static kobj_method_t feeder_format_methods[] = {
268 KOBJMETHOD(feeder_init, feed_format_init),
269 KOBJMETHOD(feeder_free, feed_format_free),
270 KOBJMETHOD(feeder_set, feed_format_set),
271 KOBJMETHOD(feeder_feed, feed_format_feed),
275 FEEDER_DECLARE(feeder_format, NULL);
279 feeder_format_read_op(uint32_t format)
283 for (i = 0; i < FEEDFORMAT_TAB_SIZE; i++) {
284 if (AFMT_ENCODING(format) == feed_format_ops[i].format)
285 return (feed_format_ops[i].read);
292 feeder_format_write_op(uint32_t format)
296 for (i = 0; i < FEEDFORMAT_TAB_SIZE; i++) {
297 if (AFMT_ENCODING(format) == feed_format_ops[i].format)
298 return (feed_format_ops[i].write);