xref: /openbmc/linux/sound/core/pcm_misc.c (revision 5c816641)
1 /*
2  *  PCM Interface - misc routines
3  *  Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
4  *
5  *
6  *   This library is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU Library General Public License as
8  *   published by the Free Software Foundation; either version 2 of
9  *   the License, or (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU Library General Public License for more details.
15  *
16  *   You should have received a copy of the GNU Library General Public
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21 
22 #include <linux/time.h>
23 #include <linux/export.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 
27 #include "pcm_local.h"
28 
29 #define SND_PCM_FORMAT_UNKNOWN (-1)
30 
31 /* NOTE: "signed" prefix must be given below since the default char is
32  *       unsigned on some architectures!
33  */
34 struct pcm_format_data {
35 	unsigned char width;	/* bit width */
36 	unsigned char phys;	/* physical bit width */
37 	signed char le;	/* 0 = big-endian, 1 = little-endian, -1 = others */
38 	signed char signd;	/* 0 = unsigned, 1 = signed, -1 = others */
39 	unsigned char silence[8];	/* silence data to fill */
40 };
41 
42 /* we do lots of calculations on snd_pcm_format_t; shut up sparse */
43 #define INT	__force int
44 
45 static bool valid_format(snd_pcm_format_t format)
46 {
47 	return (INT)format >= 0 && (INT)format <= (INT)SNDRV_PCM_FORMAT_LAST;
48 }
49 
50 static const struct pcm_format_data pcm_formats[(INT)SNDRV_PCM_FORMAT_LAST+1] = {
51 	[SNDRV_PCM_FORMAT_S8] = {
52 		.width = 8, .phys = 8, .le = -1, .signd = 1,
53 		.silence = {},
54 	},
55 	[SNDRV_PCM_FORMAT_U8] = {
56 		.width = 8, .phys = 8, .le = -1, .signd = 0,
57 		.silence = { 0x80 },
58 	},
59 	[SNDRV_PCM_FORMAT_S16_LE] = {
60 		.width = 16, .phys = 16, .le = 1, .signd = 1,
61 		.silence = {},
62 	},
63 	[SNDRV_PCM_FORMAT_S16_BE] = {
64 		.width = 16, .phys = 16, .le = 0, .signd = 1,
65 		.silence = {},
66 	},
67 	[SNDRV_PCM_FORMAT_U16_LE] = {
68 		.width = 16, .phys = 16, .le = 1, .signd = 0,
69 		.silence = { 0x00, 0x80 },
70 	},
71 	[SNDRV_PCM_FORMAT_U16_BE] = {
72 		.width = 16, .phys = 16, .le = 0, .signd = 0,
73 		.silence = { 0x80, 0x00 },
74 	},
75 	[SNDRV_PCM_FORMAT_S24_LE] = {
76 		.width = 24, .phys = 32, .le = 1, .signd = 1,
77 		.silence = {},
78 	},
79 	[SNDRV_PCM_FORMAT_S24_BE] = {
80 		.width = 24, .phys = 32, .le = 0, .signd = 1,
81 		.silence = {},
82 	},
83 	[SNDRV_PCM_FORMAT_U24_LE] = {
84 		.width = 24, .phys = 32, .le = 1, .signd = 0,
85 		.silence = { 0x00, 0x00, 0x80 },
86 	},
87 	[SNDRV_PCM_FORMAT_U24_BE] = {
88 		.width = 24, .phys = 32, .le = 0, .signd = 0,
89 		.silence = { 0x00, 0x80, 0x00, 0x00 },
90 	},
91 	[SNDRV_PCM_FORMAT_S32_LE] = {
92 		.width = 32, .phys = 32, .le = 1, .signd = 1,
93 		.silence = {},
94 	},
95 	[SNDRV_PCM_FORMAT_S32_BE] = {
96 		.width = 32, .phys = 32, .le = 0, .signd = 1,
97 		.silence = {},
98 	},
99 	[SNDRV_PCM_FORMAT_U32_LE] = {
100 		.width = 32, .phys = 32, .le = 1, .signd = 0,
101 		.silence = { 0x00, 0x00, 0x00, 0x80 },
102 	},
103 	[SNDRV_PCM_FORMAT_U32_BE] = {
104 		.width = 32, .phys = 32, .le = 0, .signd = 0,
105 		.silence = { 0x80, 0x00, 0x00, 0x00 },
106 	},
107 	[SNDRV_PCM_FORMAT_FLOAT_LE] = {
108 		.width = 32, .phys = 32, .le = 1, .signd = -1,
109 		.silence = {},
110 	},
111 	[SNDRV_PCM_FORMAT_FLOAT_BE] = {
112 		.width = 32, .phys = 32, .le = 0, .signd = -1,
113 		.silence = {},
114 	},
115 	[SNDRV_PCM_FORMAT_FLOAT64_LE] = {
116 		.width = 64, .phys = 64, .le = 1, .signd = -1,
117 		.silence = {},
118 	},
119 	[SNDRV_PCM_FORMAT_FLOAT64_BE] = {
120 		.width = 64, .phys = 64, .le = 0, .signd = -1,
121 		.silence = {},
122 	},
123 	[SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE] = {
124 		.width = 32, .phys = 32, .le = 1, .signd = -1,
125 		.silence = {},
126 	},
127 	[SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE] = {
128 		.width = 32, .phys = 32, .le = 0, .signd = -1,
129 		.silence = {},
130 	},
131 	[SNDRV_PCM_FORMAT_MU_LAW] = {
132 		.width = 8, .phys = 8, .le = -1, .signd = -1,
133 		.silence = { 0x7f },
134 	},
135 	[SNDRV_PCM_FORMAT_A_LAW] = {
136 		.width = 8, .phys = 8, .le = -1, .signd = -1,
137 		.silence = { 0x55 },
138 	},
139 	[SNDRV_PCM_FORMAT_IMA_ADPCM] = {
140 		.width = 4, .phys = 4, .le = -1, .signd = -1,
141 		.silence = {},
142 	},
143 	[SNDRV_PCM_FORMAT_G723_24] = {
144 		.width = 3, .phys = 3, .le = -1, .signd = -1,
145 		.silence = {},
146 	},
147 	[SNDRV_PCM_FORMAT_G723_40] = {
148 		.width = 5, .phys = 5, .le = -1, .signd = -1,
149 		.silence = {},
150 	},
151 	[SNDRV_PCM_FORMAT_DSD_U8] = {
152 		.width = 8, .phys = 8, .le = 1, .signd = 0,
153 		.silence = { 0x69 },
154 	},
155 	[SNDRV_PCM_FORMAT_DSD_U16_LE] = {
156 		.width = 16, .phys = 16, .le = 1, .signd = 0,
157 		.silence = { 0x69, 0x69 },
158 	},
159 	[SNDRV_PCM_FORMAT_DSD_U32_LE] = {
160 		.width = 32, .phys = 32, .le = 1, .signd = 0,
161 		.silence = { 0x69, 0x69, 0x69, 0x69 },
162 	},
163 	[SNDRV_PCM_FORMAT_DSD_U16_BE] = {
164 		.width = 16, .phys = 16, .le = 0, .signd = 0,
165 		.silence = { 0x69, 0x69 },
166 	},
167 	[SNDRV_PCM_FORMAT_DSD_U32_BE] = {
168 		.width = 32, .phys = 32, .le = 0, .signd = 0,
169 		.silence = { 0x69, 0x69, 0x69, 0x69 },
170 	},
171 	/* FIXME: the following two formats are not defined properly yet */
172 	[SNDRV_PCM_FORMAT_MPEG] = {
173 		.le = -1, .signd = -1,
174 	},
175 	[SNDRV_PCM_FORMAT_GSM] = {
176 		.le = -1, .signd = -1,
177 	},
178 	[SNDRV_PCM_FORMAT_S20_LE] = {
179 		.width = 20, .phys = 32, .le = 1, .signd = 1,
180 		.silence = {},
181 	},
182 	[SNDRV_PCM_FORMAT_S20_BE] = {
183 		.width = 20, .phys = 32, .le = 0, .signd = 1,
184 		.silence = {},
185 	},
186 	[SNDRV_PCM_FORMAT_U20_LE] = {
187 		.width = 20, .phys = 32, .le = 1, .signd = 0,
188 		.silence = { 0x00, 0x00, 0x08, 0x00 },
189 	},
190 	[SNDRV_PCM_FORMAT_U20_BE] = {
191 		.width = 20, .phys = 32, .le = 0, .signd = 0,
192 		.silence = { 0x00, 0x08, 0x00, 0x00 },
193 	},
194 	/* FIXME: the following format is not defined properly yet */
195 	[SNDRV_PCM_FORMAT_SPECIAL] = {
196 		.le = -1, .signd = -1,
197 	},
198 	[SNDRV_PCM_FORMAT_S24_3LE] = {
199 		.width = 24, .phys = 24, .le = 1, .signd = 1,
200 		.silence = {},
201 	},
202 	[SNDRV_PCM_FORMAT_S24_3BE] = {
203 		.width = 24, .phys = 24, .le = 0, .signd = 1,
204 		.silence = {},
205 	},
206 	[SNDRV_PCM_FORMAT_U24_3LE] = {
207 		.width = 24, .phys = 24, .le = 1, .signd = 0,
208 		.silence = { 0x00, 0x00, 0x80 },
209 	},
210 	[SNDRV_PCM_FORMAT_U24_3BE] = {
211 		.width = 24, .phys = 24, .le = 0, .signd = 0,
212 		.silence = { 0x80, 0x00, 0x00 },
213 	},
214 	[SNDRV_PCM_FORMAT_S20_3LE] = {
215 		.width = 20, .phys = 24, .le = 1, .signd = 1,
216 		.silence = {},
217 	},
218 	[SNDRV_PCM_FORMAT_S20_3BE] = {
219 		.width = 20, .phys = 24, .le = 0, .signd = 1,
220 		.silence = {},
221 	},
222 	[SNDRV_PCM_FORMAT_U20_3LE] = {
223 		.width = 20, .phys = 24, .le = 1, .signd = 0,
224 		.silence = { 0x00, 0x00, 0x08 },
225 	},
226 	[SNDRV_PCM_FORMAT_U20_3BE] = {
227 		.width = 20, .phys = 24, .le = 0, .signd = 0,
228 		.silence = { 0x08, 0x00, 0x00 },
229 	},
230 	[SNDRV_PCM_FORMAT_S18_3LE] = {
231 		.width = 18, .phys = 24, .le = 1, .signd = 1,
232 		.silence = {},
233 	},
234 	[SNDRV_PCM_FORMAT_S18_3BE] = {
235 		.width = 18, .phys = 24, .le = 0, .signd = 1,
236 		.silence = {},
237 	},
238 	[SNDRV_PCM_FORMAT_U18_3LE] = {
239 		.width = 18, .phys = 24, .le = 1, .signd = 0,
240 		.silence = { 0x00, 0x00, 0x02 },
241 	},
242 	[SNDRV_PCM_FORMAT_U18_3BE] = {
243 		.width = 18, .phys = 24, .le = 0, .signd = 0,
244 		.silence = { 0x02, 0x00, 0x00 },
245 	},
246 	[SNDRV_PCM_FORMAT_G723_24_1B] = {
247 		.width = 3, .phys = 8, .le = -1, .signd = -1,
248 		.silence = {},
249 	},
250 	[SNDRV_PCM_FORMAT_G723_40_1B] = {
251 		.width = 5, .phys = 8, .le = -1, .signd = -1,
252 		.silence = {},
253 	},
254 };
255 
256 
257 /**
258  * snd_pcm_format_signed - Check the PCM format is signed linear
259  * @format: the format to check
260  *
261  * Return: 1 if the given PCM format is signed linear, 0 if unsigned
262  * linear, and a negative error code for non-linear formats.
263  */
264 int snd_pcm_format_signed(snd_pcm_format_t format)
265 {
266 	int val;
267 	if (!valid_format(format))
268 		return -EINVAL;
269 	val = pcm_formats[(INT)format].signd;
270 	if (val < 0)
271 		return -EINVAL;
272 	return val;
273 }
274 EXPORT_SYMBOL(snd_pcm_format_signed);
275 
276 /**
277  * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
278  * @format: the format to check
279  *
280  * Return: 1 if the given PCM format is unsigned linear, 0 if signed
281  * linear, and a negative error code for non-linear formats.
282  */
283 int snd_pcm_format_unsigned(snd_pcm_format_t format)
284 {
285 	int val;
286 
287 	val = snd_pcm_format_signed(format);
288 	if (val < 0)
289 		return val;
290 	return !val;
291 }
292 EXPORT_SYMBOL(snd_pcm_format_unsigned);
293 
294 /**
295  * snd_pcm_format_linear - Check the PCM format is linear
296  * @format: the format to check
297  *
298  * Return: 1 if the given PCM format is linear, 0 if not.
299  */
300 int snd_pcm_format_linear(snd_pcm_format_t format)
301 {
302 	return snd_pcm_format_signed(format) >= 0;
303 }
304 EXPORT_SYMBOL(snd_pcm_format_linear);
305 
306 /**
307  * snd_pcm_format_little_endian - Check the PCM format is little-endian
308  * @format: the format to check
309  *
310  * Return: 1 if the given PCM format is little-endian, 0 if
311  * big-endian, or a negative error code if endian not specified.
312  */
313 int snd_pcm_format_little_endian(snd_pcm_format_t format)
314 {
315 	int val;
316 	if (!valid_format(format))
317 		return -EINVAL;
318 	val = pcm_formats[(INT)format].le;
319 	if (val < 0)
320 		return -EINVAL;
321 	return val;
322 }
323 EXPORT_SYMBOL(snd_pcm_format_little_endian);
324 
325 /**
326  * snd_pcm_format_big_endian - Check the PCM format is big-endian
327  * @format: the format to check
328  *
329  * Return: 1 if the given PCM format is big-endian, 0 if
330  * little-endian, or a negative error code if endian not specified.
331  */
332 int snd_pcm_format_big_endian(snd_pcm_format_t format)
333 {
334 	int val;
335 
336 	val = snd_pcm_format_little_endian(format);
337 	if (val < 0)
338 		return val;
339 	return !val;
340 }
341 EXPORT_SYMBOL(snd_pcm_format_big_endian);
342 
343 /**
344  * snd_pcm_format_width - return the bit-width of the format
345  * @format: the format to check
346  *
347  * Return: The bit-width of the format, or a negative error code
348  * if unknown format.
349  */
350 int snd_pcm_format_width(snd_pcm_format_t format)
351 {
352 	int val;
353 	if (!valid_format(format))
354 		return -EINVAL;
355 	val = pcm_formats[(INT)format].width;
356 	if (!val)
357 		return -EINVAL;
358 	return val;
359 }
360 EXPORT_SYMBOL(snd_pcm_format_width);
361 
362 /**
363  * snd_pcm_format_physical_width - return the physical bit-width of the format
364  * @format: the format to check
365  *
366  * Return: The physical bit-width of the format, or a negative error code
367  * if unknown format.
368  */
369 int snd_pcm_format_physical_width(snd_pcm_format_t format)
370 {
371 	int val;
372 	if (!valid_format(format))
373 		return -EINVAL;
374 	val = pcm_formats[(INT)format].phys;
375 	if (!val)
376 		return -EINVAL;
377 	return val;
378 }
379 EXPORT_SYMBOL(snd_pcm_format_physical_width);
380 
381 /**
382  * snd_pcm_format_size - return the byte size of samples on the given format
383  * @format: the format to check
384  * @samples: sampling rate
385  *
386  * Return: The byte size of the given samples for the format, or a
387  * negative error code if unknown format.
388  */
389 ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
390 {
391 	int phys_width = snd_pcm_format_physical_width(format);
392 	if (phys_width < 0)
393 		return -EINVAL;
394 	return samples * phys_width / 8;
395 }
396 EXPORT_SYMBOL(snd_pcm_format_size);
397 
398 /**
399  * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
400  * @format: the format to check
401  *
402  * Return: The format pattern to fill or %NULL if error.
403  */
404 const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
405 {
406 	if (!valid_format(format))
407 		return NULL;
408 	if (! pcm_formats[(INT)format].phys)
409 		return NULL;
410 	return pcm_formats[(INT)format].silence;
411 }
412 EXPORT_SYMBOL(snd_pcm_format_silence_64);
413 
414 /**
415  * snd_pcm_format_set_silence - set the silence data on the buffer
416  * @format: the PCM format
417  * @data: the buffer pointer
418  * @samples: the number of samples to set silence
419  *
420  * Sets the silence data on the buffer for the given samples.
421  *
422  * Return: Zero if successful, or a negative error code on failure.
423  */
424 int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
425 {
426 	int width;
427 	unsigned char *dst;
428 	const unsigned char *pat;
429 
430 	if (!valid_format(format))
431 		return -EINVAL;
432 	if (samples == 0)
433 		return 0;
434 	width = pcm_formats[(INT)format].phys; /* physical width */
435 	pat = pcm_formats[(INT)format].silence;
436 	if (! width)
437 		return -EINVAL;
438 	/* signed or 1 byte data */
439 	if (pcm_formats[(INT)format].signd == 1 || width <= 8) {
440 		unsigned int bytes = samples * width / 8;
441 		memset(data, *pat, bytes);
442 		return 0;
443 	}
444 	/* non-zero samples, fill using a loop */
445 	width /= 8;
446 	dst = data;
447 #if 0
448 	while (samples--) {
449 		memcpy(dst, pat, width);
450 		dst += width;
451 	}
452 #else
453 	/* a bit optimization for constant width */
454 	switch (width) {
455 	case 2:
456 		while (samples--) {
457 			memcpy(dst, pat, 2);
458 			dst += 2;
459 		}
460 		break;
461 	case 3:
462 		while (samples--) {
463 			memcpy(dst, pat, 3);
464 			dst += 3;
465 		}
466 		break;
467 	case 4:
468 		while (samples--) {
469 			memcpy(dst, pat, 4);
470 			dst += 4;
471 		}
472 		break;
473 	case 8:
474 		while (samples--) {
475 			memcpy(dst, pat, 8);
476 			dst += 8;
477 		}
478 		break;
479 	}
480 #endif
481 	return 0;
482 }
483 EXPORT_SYMBOL(snd_pcm_format_set_silence);
484 
485 /**
486  * snd_pcm_hw_limit_rates - determine rate_min/rate_max fields
487  * @hw: the pcm hw instance
488  *
489  * Determines the rate_min and rate_max fields from the rates bits of
490  * the given hw.
491  *
492  * Return: Zero if successful.
493  */
494 int snd_pcm_hw_limit_rates(struct snd_pcm_hardware *hw)
495 {
496 	int i;
497 	for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
498 		if (hw->rates & (1 << i)) {
499 			hw->rate_min = snd_pcm_known_rates.list[i];
500 			break;
501 		}
502 	}
503 	for (i = (int)snd_pcm_known_rates.count - 1; i >= 0; i--) {
504 		if (hw->rates & (1 << i)) {
505 			hw->rate_max = snd_pcm_known_rates.list[i];
506 			break;
507 		}
508 	}
509 	return 0;
510 }
511 EXPORT_SYMBOL(snd_pcm_hw_limit_rates);
512 
513 /**
514  * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
515  * @rate: the sample rate to convert
516  *
517  * Return: The SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
518  * SNDRV_PCM_RATE_KNOT for an unknown rate.
519  */
520 unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
521 {
522 	unsigned int i;
523 
524 	for (i = 0; i < snd_pcm_known_rates.count; i++)
525 		if (snd_pcm_known_rates.list[i] == rate)
526 			return 1u << i;
527 	return SNDRV_PCM_RATE_KNOT;
528 }
529 EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);
530 
531 /**
532  * snd_pcm_rate_bit_to_rate - converts SNDRV_PCM_RATE_xxx bit to sample rate
533  * @rate_bit: the rate bit to convert
534  *
535  * Return: The sample rate that corresponds to the given SNDRV_PCM_RATE_xxx flag
536  * or 0 for an unknown rate bit.
537  */
538 unsigned int snd_pcm_rate_bit_to_rate(unsigned int rate_bit)
539 {
540 	unsigned int i;
541 
542 	for (i = 0; i < snd_pcm_known_rates.count; i++)
543 		if ((1u << i) == rate_bit)
544 			return snd_pcm_known_rates.list[i];
545 	return 0;
546 }
547 EXPORT_SYMBOL(snd_pcm_rate_bit_to_rate);
548 
549 static unsigned int snd_pcm_rate_mask_sanitize(unsigned int rates)
550 {
551 	if (rates & SNDRV_PCM_RATE_CONTINUOUS)
552 		return SNDRV_PCM_RATE_CONTINUOUS;
553 	else if (rates & SNDRV_PCM_RATE_KNOT)
554 		return SNDRV_PCM_RATE_KNOT;
555 	return rates;
556 }
557 
558 /**
559  * snd_pcm_rate_mask_intersect - computes the intersection between two rate masks
560  * @rates_a: The first rate mask
561  * @rates_b: The second rate mask
562  *
563  * This function computes the rates that are supported by both rate masks passed
564  * to the function. It will take care of the special handling of
565  * SNDRV_PCM_RATE_CONTINUOUS and SNDRV_PCM_RATE_KNOT.
566  *
567  * Return: A rate mask containing the rates that are supported by both rates_a
568  * and rates_b.
569  */
570 unsigned int snd_pcm_rate_mask_intersect(unsigned int rates_a,
571 	unsigned int rates_b)
572 {
573 	rates_a = snd_pcm_rate_mask_sanitize(rates_a);
574 	rates_b = snd_pcm_rate_mask_sanitize(rates_b);
575 
576 	if (rates_a & SNDRV_PCM_RATE_CONTINUOUS)
577 		return rates_b;
578 	else if (rates_b & SNDRV_PCM_RATE_CONTINUOUS)
579 		return rates_a;
580 	else if (rates_a & SNDRV_PCM_RATE_KNOT)
581 		return rates_b;
582 	else if (rates_b & SNDRV_PCM_RATE_KNOT)
583 		return rates_a;
584 	return rates_a & rates_b;
585 }
586 EXPORT_SYMBOL_GPL(snd_pcm_rate_mask_intersect);
587 
588 /**
589  * snd_pcm_rate_range_to_bits - converts rate range to SNDRV_PCM_RATE_xxx bit
590  * @rate_min: the minimum sample rate
591  * @rate_max: the maximum sample rate
592  *
593  * This function has an implicit assumption: the rates in the given range have
594  * only the pre-defined rates like 44100 or 16000.
595  *
596  * Return: The SNDRV_PCM_RATE_xxx flag that corresponds to the given rate range,
597  * or SNDRV_PCM_RATE_KNOT for an unknown range.
598  */
599 unsigned int snd_pcm_rate_range_to_bits(unsigned int rate_min,
600 	unsigned int rate_max)
601 {
602 	unsigned int rates = 0;
603 	int i;
604 
605 	for (i = 0; i < snd_pcm_known_rates.count; i++) {
606 		if (snd_pcm_known_rates.list[i] >= rate_min
607 			&& snd_pcm_known_rates.list[i] <= rate_max)
608 			rates |= 1 << i;
609 	}
610 
611 	if (!rates)
612 		rates = SNDRV_PCM_RATE_KNOT;
613 
614 	return rates;
615 }
616 EXPORT_SYMBOL_GPL(snd_pcm_rate_range_to_bits);
617