2 * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * feeder_format: New generation of generic, any-to-any format converter, as
29 * long as the sample values can be read _and_ write.
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
36 #include <dev/sound/pcm/sound.h>
37 #include <dev/sound/pcm/pcm.h>
38 #include <dev/sound/pcm/g711.h>
39 #include <dev/sound/pcm/intpcm.h>
40 #include "feeder_if.h"
43 #include "snd_fxdiv_gen.h"
45 SND_DECLARE_FILE("$FreeBSD$");
48 #define FEEDFORMAT_RESERVOIR (SND_CHN_MAX * PCM_32_BPS)
50 INTPCM_DECLARE(intpcm_conv_tables)
52 struct feed_format_info {
54 uint32_t ialign, oalign, channels;
56 intpcm_write_t *write;
57 uint8_t reservoir[FEEDFORMAT_RESERVOIR];
61 * dummy ac3/dts passthrough, etc.
62 * XXX assume as s16le.
64 static __inline intpcm_t
65 intpcm_read_null(uint8_t *src __unused)
72 intpcm_write_null(uint8_t *dst, intpcm_t v __unused)
75 _PCM_WRITE_S16_LE(dst, 0);
78 #define FEEDFORMAT_ENTRY(SIGN, BIT, ENDIAN) \
80 AFMT_##SIGN##BIT##_##ENDIAN, \
81 intpcm_read_##SIGN##BIT##ENDIAN, \
82 intpcm_write_##SIGN##BIT##ENDIAN \
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),
106 intpcm_read_ulaw, intpcm_write_ulaw
110 intpcm_read_alaw, intpcm_write_alaw
114 intpcm_read_null, intpcm_write_null
118 #define FEEDFORMAT_TAB_SIZE \
119 ((int32_t)(sizeof(feed_format_ops) / sizeof(feed_format_ops[0])))
122 feed_format_init(struct pcm_feeder *f)
124 struct feed_format_info *info;
125 intpcm_read_t *rd_op;
126 intpcm_write_t *wr_op;
129 if (f->desc->in == f->desc->out ||
130 AFMT_CHANNEL(f->desc->in) != AFMT_CHANNEL(f->desc->out))
136 for (i = 0; i < FEEDFORMAT_TAB_SIZE &&
137 (rd_op == NULL || wr_op == NULL); i++) {
139 AFMT_ENCODING(f->desc->in) == feed_format_ops[i].format)
140 rd_op = feed_format_ops[i].read;
142 AFMT_ENCODING(f->desc->out) == feed_format_ops[i].format)
143 wr_op = feed_format_ops[i].write;
146 if (rd_op == NULL || wr_op == NULL) {
147 printf("%s(): failed to initialize io ops "
148 "in=0x%08x out=0x%08x\n",
149 __func__, f->desc->in, f->desc->out);
153 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO);
157 info->channels = AFMT_CHANNEL(f->desc->in);
159 info->ibps = AFMT_BPS(f->desc->in);
160 info->ialign = info->ibps * info->channels;
163 info->obps = AFMT_BPS(f->desc->out);
164 info->oalign = info->obps * info->channels;
173 feed_format_free(struct pcm_feeder *f)
175 struct feed_format_info *info;
179 free(info, M_DEVBUF);
187 feed_format_set(struct pcm_feeder *f, int what, int value)
189 struct feed_format_info *info;
194 case FEEDFORMAT_CHANNELS:
195 if (value < SND_CHN_MIN || value > SND_CHN_MAX)
197 info->channels = (uint32_t)value;
198 info->ialign = info->ibps * info->channels;
199 info->oalign = info->obps * info->channels;
210 feed_format_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
211 uint32_t count, void *source)
213 struct feed_format_info *info;
220 count = SND_FXROUND(count, info->oalign);
223 if (count < info->oalign)
226 if (count < info->ialign) {
227 src = info->reservoir;
230 if (info->ialign == info->oalign)
232 else if (info->ialign > info->oalign)
233 j = SND_FXROUND(count, info->ialign);
235 j = SND_FXDIV(count, info->oalign) *
237 src = dst + count - j;
240 j = SND_FXDIV(FEEDER_FEED(f->source, c, src, j, source),
246 count -= j * info->obps;
255 } while (count != 0);
260 static struct pcm_feederdesc feeder_format_desc[] = {
261 { FEEDER_FORMAT, 0, 0, 0, 0 },
265 static kobj_method_t feeder_format_methods[] = {
266 KOBJMETHOD(feeder_init, feed_format_init),
267 KOBJMETHOD(feeder_free, feed_format_free),
268 KOBJMETHOD(feeder_set, feed_format_set),
269 KOBJMETHOD(feeder_feed, feed_format_feed),
273 FEEDER_DECLARE(feeder_format, NULL);
277 feeder_format_read_op(uint32_t format)
281 for (i = 0; i < FEEDFORMAT_TAB_SIZE; i++) {
282 if (AFMT_ENCODING(format) == feed_format_ops[i].format)
283 return (feed_format_ops[i].read);
290 feeder_format_write_op(uint32_t format)
294 for (i = 0; i < FEEDFORMAT_TAB_SIZE; i++) {
295 if (AFMT_ENCODING(format) == feed_format_ops[i].format)
296 return (feed_format_ops[i].write);