xref: /dragonfly/sys/dev/sound/pcm/feeder_format.c (revision ef2b2b9d)
1 /*-
2  * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * feeder_format: New generation of generic, any-to-any format converter, as
29  *                long as the sample values can be read _and_ write.
30  */
31 
32 #ifdef _KERNEL
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
34 #include "opt_snd.h"
35 #endif
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"
41 
42 #define SND_USE_FXDIV
43 #include "snd_fxdiv_gen.h"
44 
45 SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder_format.c 193640 2009-06-07 19:12:08Z ariff $");
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
65 intpcm_read_null(uint8_t *src __unused)
66 {
67 
68 	return (0);
69 }
70 
71 static __inline void
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 #define FEEDFORMAT_TAB_SIZE						\
119 	((int32_t)(sizeof(feed_format_ops) / sizeof(feed_format_ops[0])))
120 
121 static int
122 feed_format_init(struct pcm_feeder *f)
123 {
124 	struct feed_format_info *info;
125 	intpcm_read_t *rd_op;
126 	intpcm_write_t *wr_op;
127 	int i;
128 
129 	if (f->desc->in == f->desc->out ||
130 	    AFMT_CHANNEL(f->desc->in) != AFMT_CHANNEL(f->desc->out))
131 		return (EINVAL);
132 
133 	rd_op = NULL;
134 	wr_op = NULL;
135 
136 	for (i = 0; i < FEEDFORMAT_TAB_SIZE &&
137 	    (rd_op == NULL || wr_op == NULL); i++) {
138 		if (rd_op == NULL &&
139 		    AFMT_ENCODING(f->desc->in) == feed_format_ops[i].format)
140 			rd_op = feed_format_ops[i].read;
141 		if (wr_op == NULL &&
142 		    AFMT_ENCODING(f->desc->out) == feed_format_ops[i].format)
143 			wr_op = feed_format_ops[i].write;
144 	}
145 
146 	if (rd_op == NULL || wr_op == NULL) {
147 		kprintf("%s(): failed to initialize io ops "
148 		    "in=0x%08x out=0x%08x\n",
149 		    __func__, f->desc->in, f->desc->out);
150 		return (EINVAL);
151 	}
152 
153 	info = kmalloc(sizeof(*info), M_DEVBUF, M_WAITOK | M_ZERO);
154 	if (info == NULL)
155 		return (ENOMEM);
156 
157 	info->channels = AFMT_CHANNEL(f->desc->in);
158 
159 	info->ibps = AFMT_BPS(f->desc->in);
160 	info->ialign = info->ibps * info->channels;
161 	info->read = rd_op;
162 
163 	info->obps = AFMT_BPS(f->desc->out);
164 	info->oalign = info->obps * info->channels;
165 	info->write = wr_op;
166 
167 	f->data = info;
168 
169 	return (0);
170 }
171 
172 static int
173 feed_format_free(struct pcm_feeder *f)
174 {
175 	struct feed_format_info *info;
176 
177 	info = f->data;
178 	if (info != NULL)
179 		kfree(info, M_DEVBUF);
180 
181 	f->data = NULL;
182 
183 	return (0);
184 }
185 
186 static int
187 feed_format_set(struct pcm_feeder *f, int what, int value)
188 {
189 	struct feed_format_info *info;
190 
191 	info = f->data;
192 
193 	switch (what) {
194 	case FEEDFORMAT_CHANNELS:
195 		if (value < SND_CHN_MIN || value > SND_CHN_MAX)
196 			return (EINVAL);
197 		info->channels = (uint32_t)value;
198 		info->ialign = info->ibps * info->channels;
199 		info->oalign = info->obps * info->channels;
200 		break;
201 	default:
202 		return (EINVAL);
203 		break;
204 	}
205 
206 	return (0);
207 }
208 
209 static int
210 feed_format_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
211     uint32_t count, void *source)
212 {
213 	struct feed_format_info *info;
214 	intpcm_t v;
215 	uint32_t j;
216 	uint8_t *src, *dst;
217 
218 	info = f->data;
219 	dst = b;
220 	count = SND_FXROUND(count, info->oalign);
221 
222 	do {
223 		if (count < info->oalign)
224 			break;
225 
226 		if (count < info->ialign) {
227 			src = info->reservoir;
228 			j = info->ialign;
229 		} else {
230 			if (info->ialign == info->oalign)
231 				j = count;
232 			else if (info->ialign > info->oalign)
233 				j = SND_FXROUND(count, info->ialign);
234 			else
235 				j = SND_FXDIV(count, info->oalign) *
236 				    info->ialign;
237 			src = dst + count - j;
238 		}
239 
240 		j = SND_FXDIV(FEEDER_FEED(f->source, c, src, j, source),
241 		    info->ialign);
242 		if (j == 0)
243 			break;
244 
245 		j *= info->channels;
246 		count -= j * info->obps;
247 
248 		do {
249 			v = info->read(src);
250 			info->write(dst, v);
251 			dst += info->obps;
252 			src += info->ibps;
253 		} while (--j != 0);
254 
255 	} while (count != 0);
256 
257 	return (dst - b);
258 }
259 
260 static struct pcm_feederdesc feeder_format_desc[] = {
261 	{ FEEDER_FORMAT, 0, 0, 0, 0 },
262 	{ 0, 0, 0, 0, 0 }
263 };
264 
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),
270 	KOBJMETHOD_END
271 };
272 
273 FEEDER_DECLARE(feeder_format, NULL);
274 
275 /* Extern */
276 intpcm_read_t *
277 feeder_format_read_op(uint32_t format)
278 {
279 	int i;
280 
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);
284 	}
285 
286 	return (NULL);
287 }
288 
289 intpcm_write_t *
290 feeder_format_write_op(uint32_t format)
291 {
292 	int i;
293 
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);
297 	}
298 
299 	return (NULL);
300 }
301