FreeBSD kernel sound device code
feeder_format.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
42#include "feeder_if.h"
43
44#define SND_USE_FXDIV
45#include "snd_fxdiv_gen.h"
46
47SND_DECLARE_FILE("$FreeBSD$");
48#endif
49
50#define FEEDFORMAT_RESERVOIR (SND_CHN_MAX * PCM_32_BPS)
51
52INTPCM_DECLARE(intpcm_conv_tables)
53
55 uint32_t ibps, obps;
56 uint32_t ialign, oalign, channels;
59 uint8_t reservoir[FEEDFORMAT_RESERVOIR];
60};
61
62/*
63 * dummy ac3/dts passthrough, etc.
64 * XXX assume as s16le.
65 */
66static __inline intpcm_t
67intpcm_read_null(uint8_t *src __unused)
68{
69
70 return (0);
71}
72
73static __inline void
74intpcm_write_null(uint8_t *dst, intpcm_t v __unused)
75{
76
77 _PCM_WRITE_S16_LE(dst, 0);
78}
79
80#define FEEDFORMAT_ENTRY(SIGN, BIT, ENDIAN) \
81 { \
82 AFMT_##SIGN##BIT##_##ENDIAN, \
83 intpcm_read_##SIGN##BIT##ENDIAN, \
84 intpcm_write_##SIGN##BIT##ENDIAN \
85 }
86
87static const struct {
88 uint32_t format;
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),
106 {
107 AFMT_MU_LAW,
108 intpcm_read_ulaw, intpcm_write_ulaw
109 },
110 {
111 AFMT_A_LAW,
112 intpcm_read_alaw, intpcm_write_alaw
113 },
114 {
115 AFMT_AC3,
117 }
119
120#define FEEDFORMAT_TAB_SIZE \
121 ((int32_t)(sizeof(feed_format_ops) / sizeof(feed_format_ops[0])))
122
123static int
125{
126 struct feed_format_info *info;
127 intpcm_read_t *rd_op;
128 intpcm_write_t *wr_op;
129 int i;
130
131 if (f->desc->in == f->desc->out ||
133 return (EINVAL);
134
135 rd_op = NULL;
136 wr_op = NULL;
137
138 for (i = 0; i < FEEDFORMAT_TAB_SIZE &&
139 (rd_op == NULL || wr_op == NULL); i++) {
140 if (rd_op == NULL &&
141 AFMT_ENCODING(f->desc->in) == feed_format_ops[i].format)
142 rd_op = feed_format_ops[i].read;
143 if (wr_op == NULL &&
144 AFMT_ENCODING(f->desc->out) == feed_format_ops[i].format)
145 wr_op = feed_format_ops[i].write;
146 }
147
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);
152 return (EINVAL);
153 }
154
155 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO);
156 if (info == NULL)
157 return (ENOMEM);
158
159 info->channels = AFMT_CHANNEL(f->desc->in);
160
161 info->ibps = AFMT_BPS(f->desc->in);
162 info->ialign = info->ibps * info->channels;
163 info->read = rd_op;
164
165 info->obps = AFMT_BPS(f->desc->out);
166 info->oalign = info->obps * info->channels;
167 info->write = wr_op;
168
169 f->data = info;
170
171 return (0);
172}
173
174static int
176{
177 struct feed_format_info *info;
178
179 info = f->data;
180 if (info != NULL)
181 free(info, M_DEVBUF);
182
183 f->data = NULL;
184
185 return (0);
186}
187
188static int
190{
191 struct feed_format_info *info;
192
193 info = f->data;
194
195 switch (what) {
197 if (value < SND_CHN_MIN || value > SND_CHN_MAX)
198 return (EINVAL);
199 info->channels = (uint32_t)value;
200 info->ialign = info->ibps * info->channels;
201 info->oalign = info->obps * info->channels;
202 break;
203 default:
204 return (EINVAL);
205 break;
206 }
207
208 return (0);
209}
210
211static int
212feed_format_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
213 uint32_t count, void *source)
214{
215 struct feed_format_info *info;
216 intpcm_t v;
217 uint32_t j;
218 uint8_t *src, *dst;
219
220 info = f->data;
221 dst = b;
222 count = SND_FXROUND(count, info->oalign);
223
224 do {
225 if (count < info->oalign)
226 break;
227
228 if (count < info->ialign) {
229 src = info->reservoir;
230 j = info->ialign;
231 } else {
232 if (info->ialign == info->oalign)
233 j = count;
234 else if (info->ialign > info->oalign)
235 j = SND_FXROUND(count, info->ialign);
236 else
237 j = SND_FXDIV(count, info->oalign) *
238 info->ialign;
239 src = dst + count - j;
240 }
241
242 j = SND_FXDIV(FEEDER_FEED(f->source, c, src, j, source),
243 info->ialign);
244 if (j == 0)
245 break;
246
247 j *= info->channels;
248 count -= j * info->obps;
249
250 do {
251 v = info->read(src);
252 info->write(dst, v);
253 dst += info->obps;
254 src += info->ibps;
255 } while (--j != 0);
256
257 } while (count != 0);
258
259 return (dst - b);
260}
261
263 { FEEDER_FORMAT, 0, 0, 0, 0 },
264 { 0, 0, 0, 0, 0 }
265};
266
267static 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),
273};
274
275FEEDER_DECLARE(feeder_format, NULL);
276
277/* Extern */
280{
281 int i;
282
283 for (i = 0; i < FEEDFORMAT_TAB_SIZE; i++) {
285 return (feed_format_ops[i].read);
286 }
287
288 return (NULL);
289}
290
293{
294 int i;
295
296 for (i = 0; i < FEEDFORMAT_TAB_SIZE; i++) {
298 return (feed_format_ops[i].write);
299 }
300
301 return (NULL);
302}
struct pcm_channel * c
Definition: channel_if.m:106
METHOD int free
Definition: channel_if.m:110
struct snd_dbuf * b
Definition: channel_if.m:105
@ FEEDFORMAT_CHANNELS
Definition: feeder.h:94
@ FEEDER_FORMAT
Definition: feeder.h:83
static int feed_format_init(struct pcm_feeder *f)
static const struct @43 feed_format_ops[]
static int feed_format_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b, uint32_t count, void *source)
static int feed_format_set(struct pcm_feeder *f, int what, int value)
#define FEEDFORMAT_ENTRY(SIGN, BIT, ENDIAN)
Definition: feeder_format.c:80
static __inline void intpcm_write_null(uint8_t *dst, intpcm_t v __unused)
Definition: feeder_format.c:74
intpcm_read_t * feeder_format_read_op(uint32_t format)
uint32_t format
Definition: feeder_format.c:88
FEEDER_DECLARE(feeder_format, NULL)
static int feed_format_free(struct pcm_feeder *f)
SND_DECLARE_FILE("$FreeBSD$")
static struct pcm_feederdesc feeder_format_desc[]
intpcm_write_t * write
Definition: feeder_format.c:90
#define FEEDFORMAT_TAB_SIZE
intpcm_read_t * read
Definition: feeder_format.c:89
static kobj_method_t feeder_format_methods[]
static __inline intpcm_t intpcm_read_null(uint8_t *src __unused)
Definition: feeder_format.c:67
intpcm_write_t * feeder_format_write_op(uint32_t format)
#define FEEDFORMAT_RESERVOIR
Definition: feeder_format.c:50
u_int32_t count
Definition: feeder_if.m:86
void * source
Definition: feeder_if.m:87
int what
Definition: feeder_if.m:73
uint32_t value
Definition: hdaa.c:58
intpcm_t intpcm_read_t(uint8_t *)
Definition: intpcm.h:34
void intpcm_write_t(uint8_t *, intpcm_t)
Definition: intpcm.h:35
#define INTPCM_DECLARE(t)
Definition: intpcm.h:90
#define SND_CHN_MAX
Definition: matrix.h:183
#define KOBJMETHOD_END
Definition: midi.c:76
u_int32_t src
Definition: mixer_if.m:66
int32_t intpcm_t
Definition: pcm.h:54
#define _PCM_WRITE_S16_LE(b8, val)
Definition: pcm.h:120
#define AFMT_ENCODING(v)
Definition: sound.h:222
#define AFMT_BPS(v)
Definition: sound.h:235
#define AFMT_CHANNEL(v)
Definition: sound.h:227
intpcm_write_t * write
Definition: feeder_format.c:58
intpcm_read_t * read
Definition: feeder_format.c:57
uint8_t reservoir[FEEDFORMAT_RESERVOIR]
Definition: feeder_format.c:59
struct pcm_feeder * source
Definition: feeder.h:51
void * data
Definition: feeder.h:49
struct pcm_feederdesc * desc
Definition: feeder.h:48
u_int32_t out
Definition: feeder.h:34
u_int32_t in
Definition: feeder.h:34