2 * PCM Interface - misc routines
3 * Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
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.
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.
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
22 #include <sound/driver.h>
23 #include <linux/time.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #define SND_PCM_FORMAT_UNKNOWN (-1)
28 /* NOTE: "signed" prefix must be given below since the default char is
29 * unsigned on some architectures!
31 struct pcm_format_data {
32 unsigned char width; /* bit width */
33 unsigned char phys; /* physical bit width */
34 signed char le; /* 0 = big-endian, 1 = little-endian, -1 = others */
35 signed char signd; /* 0 = unsigned, 1 = signed, -1 = others */
36 unsigned char silence[8]; /* silence data to fill */
39 static struct pcm_format_data pcm_formats[SNDRV_PCM_FORMAT_LAST+1] = {
40 [SNDRV_PCM_FORMAT_S8] = {
41 .width = 8, .phys = 8, .le = -1, .signd = 1,
44 [SNDRV_PCM_FORMAT_U8] = {
45 .width = 8, .phys = 8, .le = -1, .signd = 0,
48 [SNDRV_PCM_FORMAT_S16_LE] = {
49 .width = 16, .phys = 16, .le = 1, .signd = 1,
52 [SNDRV_PCM_FORMAT_S16_BE] = {
53 .width = 16, .phys = 16, .le = 0, .signd = 1,
56 [SNDRV_PCM_FORMAT_U16_LE] = {
57 .width = 16, .phys = 16, .le = 1, .signd = 0,
58 .silence = { 0x00, 0x80 },
60 [SNDRV_PCM_FORMAT_U16_BE] = {
61 .width = 16, .phys = 16, .le = 0, .signd = 0,
62 .silence = { 0x80, 0x00 },
64 [SNDRV_PCM_FORMAT_S24_LE] = {
65 .width = 24, .phys = 32, .le = 1, .signd = 1,
68 [SNDRV_PCM_FORMAT_S24_BE] = {
69 .width = 24, .phys = 32, .le = 0, .signd = 1,
72 [SNDRV_PCM_FORMAT_U24_LE] = {
73 .width = 24, .phys = 32, .le = 1, .signd = 0,
74 .silence = { 0x00, 0x00, 0x80 },
76 [SNDRV_PCM_FORMAT_U24_BE] = {
77 .width = 24, .phys = 32, .le = 0, .signd = 0,
78 .silence = { 0x80, 0x00, 0x00 },
80 [SNDRV_PCM_FORMAT_S32_LE] = {
81 .width = 32, .phys = 32, .le = 1, .signd = 1,
84 [SNDRV_PCM_FORMAT_S32_BE] = {
85 .width = 32, .phys = 32, .le = 0, .signd = 1,
88 [SNDRV_PCM_FORMAT_U32_LE] = {
89 .width = 32, .phys = 32, .le = 1, .signd = 0,
90 .silence = { 0x00, 0x00, 0x00, 0x80 },
92 [SNDRV_PCM_FORMAT_U32_BE] = {
93 .width = 32, .phys = 32, .le = 0, .signd = 0,
94 .silence = { 0x80, 0x00, 0x00, 0x00 },
96 [SNDRV_PCM_FORMAT_FLOAT_LE] = {
97 .width = 32, .phys = 32, .le = 1, .signd = -1,
100 [SNDRV_PCM_FORMAT_FLOAT_BE] = {
101 .width = 32, .phys = 32, .le = 0, .signd = -1,
104 [SNDRV_PCM_FORMAT_FLOAT64_LE] = {
105 .width = 64, .phys = 64, .le = 1, .signd = -1,
108 [SNDRV_PCM_FORMAT_FLOAT64_BE] = {
109 .width = 64, .phys = 64, .le = 0, .signd = -1,
112 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE] = {
113 .width = 32, .phys = 32, .le = 1, .signd = -1,
116 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE] = {
117 .width = 32, .phys = 32, .le = 0, .signd = -1,
120 [SNDRV_PCM_FORMAT_MU_LAW] = {
121 .width = 8, .phys = 8, .le = -1, .signd = -1,
124 [SNDRV_PCM_FORMAT_A_LAW] = {
125 .width = 8, .phys = 8, .le = -1, .signd = -1,
128 [SNDRV_PCM_FORMAT_IMA_ADPCM] = {
129 .width = 4, .phys = 4, .le = -1, .signd = -1,
132 /* FIXME: the following three formats are not defined properly yet */
133 [SNDRV_PCM_FORMAT_MPEG] = {
134 .le = -1, .signd = -1,
136 [SNDRV_PCM_FORMAT_GSM] = {
137 .le = -1, .signd = -1,
139 [SNDRV_PCM_FORMAT_SPECIAL] = {
140 .le = -1, .signd = -1,
142 [SNDRV_PCM_FORMAT_S24_3LE] = {
143 .width = 24, .phys = 24, .le = 1, .signd = 1,
146 [SNDRV_PCM_FORMAT_S24_3BE] = {
147 .width = 24, .phys = 24, .le = 0, .signd = 1,
150 [SNDRV_PCM_FORMAT_U24_3LE] = {
151 .width = 24, .phys = 24, .le = 1, .signd = 0,
152 .silence = { 0x00, 0x00, 0x80 },
154 [SNDRV_PCM_FORMAT_U24_3BE] = {
155 .width = 24, .phys = 24, .le = 0, .signd = 0,
156 .silence = { 0x80, 0x00, 0x00 },
158 [SNDRV_PCM_FORMAT_S20_3LE] = {
159 .width = 20, .phys = 24, .le = 1, .signd = 1,
162 [SNDRV_PCM_FORMAT_S20_3BE] = {
163 .width = 20, .phys = 24, .le = 0, .signd = 1,
166 [SNDRV_PCM_FORMAT_U20_3LE] = {
167 .width = 20, .phys = 24, .le = 1, .signd = 0,
168 .silence = { 0x00, 0x00, 0x08 },
170 [SNDRV_PCM_FORMAT_U20_3BE] = {
171 .width = 20, .phys = 24, .le = 0, .signd = 0,
172 .silence = { 0x08, 0x00, 0x00 },
174 [SNDRV_PCM_FORMAT_S18_3LE] = {
175 .width = 18, .phys = 24, .le = 1, .signd = 1,
178 [SNDRV_PCM_FORMAT_S18_3BE] = {
179 .width = 18, .phys = 24, .le = 0, .signd = 1,
182 [SNDRV_PCM_FORMAT_U18_3LE] = {
183 .width = 18, .phys = 24, .le = 1, .signd = 0,
184 .silence = { 0x00, 0x00, 0x02 },
186 [SNDRV_PCM_FORMAT_U18_3BE] = {
187 .width = 18, .phys = 24, .le = 0, .signd = 0,
188 .silence = { 0x02, 0x00, 0x00 },
194 * snd_pcm_format_signed - Check the PCM format is signed linear
195 * @format: the format to check
197 * Returns 1 if the given PCM format is signed linear, 0 if unsigned
198 * linear, and a negative error code for non-linear formats.
200 int snd_pcm_format_signed(snd_pcm_format_t format)
203 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
205 if ((val = pcm_formats[format].signd) < 0)
210 EXPORT_SYMBOL(snd_pcm_format_signed);
213 * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
214 * @format: the format to check
216 * Returns 1 if the given PCM format is unsigned linear, 0 if signed
217 * linear, and a negative error code for non-linear formats.
219 int snd_pcm_format_unsigned(snd_pcm_format_t format)
223 val = snd_pcm_format_signed(format);
229 EXPORT_SYMBOL(snd_pcm_format_unsigned);
232 * snd_pcm_format_linear - Check the PCM format is linear
233 * @format: the format to check
235 * Returns 1 if the given PCM format is linear, 0 if not.
237 int snd_pcm_format_linear(snd_pcm_format_t format)
239 return snd_pcm_format_signed(format) >= 0;
242 EXPORT_SYMBOL(snd_pcm_format_linear);
245 * snd_pcm_format_little_endian - Check the PCM format is little-endian
246 * @format: the format to check
248 * Returns 1 if the given PCM format is little-endian, 0 if
249 * big-endian, or a negative error code if endian not specified.
251 int snd_pcm_format_little_endian(snd_pcm_format_t format)
254 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
256 if ((val = pcm_formats[format].le) < 0)
261 EXPORT_SYMBOL(snd_pcm_format_little_endian);
264 * snd_pcm_format_big_endian - Check the PCM format is big-endian
265 * @format: the format to check
267 * Returns 1 if the given PCM format is big-endian, 0 if
268 * little-endian, or a negative error code if endian not specified.
270 int snd_pcm_format_big_endian(snd_pcm_format_t format)
274 val = snd_pcm_format_little_endian(format);
280 EXPORT_SYMBOL(snd_pcm_format_big_endian);
283 * snd_pcm_format_width - return the bit-width of the format
284 * @format: the format to check
286 * Returns the bit-width of the format, or a negative error code
289 int snd_pcm_format_width(snd_pcm_format_t format)
292 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
294 if ((val = pcm_formats[format].width) == 0)
299 EXPORT_SYMBOL(snd_pcm_format_width);
302 * snd_pcm_format_physical_width - return the physical bit-width of the format
303 * @format: the format to check
305 * Returns the physical bit-width of the format, or a negative error code
308 int snd_pcm_format_physical_width(snd_pcm_format_t format)
311 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
313 if ((val = pcm_formats[format].phys) == 0)
318 EXPORT_SYMBOL(snd_pcm_format_physical_width);
321 * snd_pcm_format_size - return the byte size of samples on the given format
322 * @format: the format to check
324 * Returns the byte size of the given samples for the format, or a
325 * negative error code if unknown format.
327 ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
329 int phys_width = snd_pcm_format_physical_width(format);
332 return samples * phys_width / 8;
335 EXPORT_SYMBOL(snd_pcm_format_size);
338 * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
339 * @format: the format to check
341 * Returns the format pattern to fill or NULL if error.
343 const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
345 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
347 if (! pcm_formats[format].phys)
349 return pcm_formats[format].silence;
352 EXPORT_SYMBOL(snd_pcm_format_silence_64);
355 * snd_pcm_format_set_silence - set the silence data on the buffer
356 * @format: the PCM format
357 * @data: the buffer pointer
358 * @samples: the number of samples to set silence
360 * Sets the silence data on the buffer for the given samples.
362 * Returns zero if successful, or a negative error code on failure.
364 int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
367 unsigned char *dst, *pat;
369 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
373 width = pcm_formats[format].phys; /* physical width */
374 pat = pcm_formats[format].silence;
377 /* signed or 1 byte data */
378 if (pcm_formats[format].signd == 1 || width <= 8) {
379 unsigned int bytes = samples * width / 8;
380 memset(data, *pat, bytes);
383 /* non-zero samples, fill using a loop */
388 memcpy(dst, pat, width);
392 /* a bit optimization for constant width */
423 EXPORT_SYMBOL(snd_pcm_format_set_silence);
426 * snd_pcm_limit_hw_rates - determine rate_min/rate_max fields
427 * @runtime: the runtime instance
429 * Determines the rate_min and rate_max fields from the rates bits of
430 * the given runtime->hw.
432 * Returns zero if successful.
434 int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
437 for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
438 if (runtime->hw.rates & (1 << i)) {
439 runtime->hw.rate_min = snd_pcm_known_rates.list[i];
443 for (i = (int)snd_pcm_known_rates.count - 1; i >= 0; i--) {
444 if (runtime->hw.rates & (1 << i)) {
445 runtime->hw.rate_max = snd_pcm_known_rates.list[i];
452 EXPORT_SYMBOL(snd_pcm_limit_hw_rates);
455 * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
456 * @rate: the sample rate to convert
458 * Returns the SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
459 * SNDRV_PCM_RATE_KNOT for an unknown rate.
461 unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
465 for (i = 0; i < snd_pcm_known_rates.count; i++)
466 if (snd_pcm_known_rates.list[i] == rate)
468 return SNDRV_PCM_RATE_KNOT;
470 EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);