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 47 SND_DECLARE_FILE("$FreeBSD$"); 48 #endif 49 50 #define FEEDFORMAT_RESERVOIR (SND_CHN_MAX * PCM_32_BPS) 51 52 INTPCM_DECLARE(intpcm_conv_tables) 53 54 struct feed_format_info { 55 uint32_t ibps, obps; 56 uint32_t ialign, oalign, channels; 57 intpcm_read_t *read; 58 intpcm_write_t *write; 59 uint8_t reservoir[FEEDFORMAT_RESERVOIR]; 60 }; 61 62 /* 63 * dummy ac3/dts passthrough, etc. 64 * XXX assume as s16le. 65 */ 66 static __inline intpcm_t 67 intpcm_read_null(uint8_t *src __unused) 68 { 69 70 return (0); 71 } 72 73 static __inline void 74 intpcm_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 87 static const struct { 88 uint32_t format; 89 intpcm_read_t *read; 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), 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, 116 intpcm_read_null, intpcm_write_null 117 } 118 }; 119 120 #define FEEDFORMAT_TAB_SIZE \ 121 ((int32_t)(sizeof(feed_format_ops) / sizeof(feed_format_ops[0]))) 122 123 static int 124 feed_format_init(struct pcm_feeder *f) 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 || 132 AFMT_CHANNEL(f->desc->in) != AFMT_CHANNEL(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 174 static int 175 feed_format_free(struct pcm_feeder *f) 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 188 static int 189 feed_format_set(struct pcm_feeder *f, int what, int value) 190 { 191 struct feed_format_info *info; 192 193 info = f->data; 194 195 switch (what) { 196 case FEEDFORMAT_CHANNELS: 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 211 static int 212 feed_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 262 static struct pcm_feederdesc feeder_format_desc[] = { 263 { FEEDER_FORMAT, 0, 0, 0, 0 }, 264 { 0, 0, 0, 0, 0 } 265 }; 266 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), 272 KOBJMETHOD_END 273 }; 274 275 FEEDER_DECLARE(feeder_format, NULL); 276 277 /* Extern */ 278 intpcm_read_t * 279 feeder_format_read_op(uint32_t format) 280 { 281 int i; 282 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); 286 } 287 288 return (NULL); 289 } 290 291 intpcm_write_t * 292 feeder_format_write_op(uint32_t format) 293 { 294 int i; 295 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); 299 } 300 301 return (NULL); 302 } 303