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 <linux/time.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #define SND_PCM_FORMAT_UNKNOWN (-1)
27 /* NOTE: "signed" prefix must be given below since the default char is
28 * unsigned on some architectures!
30 struct pcm_format_data {
31 unsigned char width; /* bit width */
32 unsigned char phys; /* physical bit width */
33 signed char le; /* 0 = big-endian, 1 = little-endian, -1 = others */
34 signed char signd; /* 0 = unsigned, 1 = signed, -1 = others */
35 unsigned char silence[8]; /* silence data to fill */
38 static struct pcm_format_data pcm_formats[SNDRV_PCM_FORMAT_LAST+1] = {
39 [SNDRV_PCM_FORMAT_S8] = {
40 .width = 8, .phys = 8, .le = -1, .signd = 1,
43 [SNDRV_PCM_FORMAT_U8] = {
44 .width = 8, .phys = 8, .le = -1, .signd = 0,
47 [SNDRV_PCM_FORMAT_S16_LE] = {
48 .width = 16, .phys = 16, .le = 1, .signd = 1,
51 [SNDRV_PCM_FORMAT_S16_BE] = {
52 .width = 16, .phys = 16, .le = 0, .signd = 1,
55 [SNDRV_PCM_FORMAT_U16_LE] = {
56 .width = 16, .phys = 16, .le = 1, .signd = 0,
57 .silence = { 0x00, 0x80 },
59 [SNDRV_PCM_FORMAT_U16_BE] = {
60 .width = 16, .phys = 16, .le = 0, .signd = 0,
61 .silence = { 0x80, 0x00 },
63 [SNDRV_PCM_FORMAT_S24_LE] = {
64 .width = 24, .phys = 32, .le = 1, .signd = 1,
67 [SNDRV_PCM_FORMAT_S24_BE] = {
68 .width = 24, .phys = 32, .le = 0, .signd = 1,
71 [SNDRV_PCM_FORMAT_U24_LE] = {
72 .width = 24, .phys = 32, .le = 1, .signd = 0,
73 .silence = { 0x00, 0x00, 0x80 },
75 [SNDRV_PCM_FORMAT_U24_BE] = {
76 .width = 24, .phys = 32, .le = 0, .signd = 0,
77 .silence = { 0x00, 0x80, 0x00, 0x00 },
79 [SNDRV_PCM_FORMAT_S32_LE] = {
80 .width = 32, .phys = 32, .le = 1, .signd = 1,
83 [SNDRV_PCM_FORMAT_S32_BE] = {
84 .width = 32, .phys = 32, .le = 0, .signd = 1,
87 [SNDRV_PCM_FORMAT_U32_LE] = {
88 .width = 32, .phys = 32, .le = 1, .signd = 0,
89 .silence = { 0x00, 0x00, 0x00, 0x80 },
91 [SNDRV_PCM_FORMAT_U32_BE] = {
92 .width = 32, .phys = 32, .le = 0, .signd = 0,
93 .silence = { 0x80, 0x00, 0x00, 0x00 },
95 [SNDRV_PCM_FORMAT_FLOAT_LE] = {
96 .width = 32, .phys = 32, .le = 1, .signd = -1,
99 [SNDRV_PCM_FORMAT_FLOAT_BE] = {
100 .width = 32, .phys = 32, .le = 0, .signd = -1,
103 [SNDRV_PCM_FORMAT_FLOAT64_LE] = {
104 .width = 64, .phys = 64, .le = 1, .signd = -1,
107 [SNDRV_PCM_FORMAT_FLOAT64_BE] = {
108 .width = 64, .phys = 64, .le = 0, .signd = -1,
111 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE] = {
112 .width = 32, .phys = 32, .le = 1, .signd = -1,
115 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE] = {
116 .width = 32, .phys = 32, .le = 0, .signd = -1,
119 [SNDRV_PCM_FORMAT_MU_LAW] = {
120 .width = 8, .phys = 8, .le = -1, .signd = -1,
123 [SNDRV_PCM_FORMAT_A_LAW] = {
124 .width = 8, .phys = 8, .le = -1, .signd = -1,
127 [SNDRV_PCM_FORMAT_IMA_ADPCM] = {
128 .width = 4, .phys = 4, .le = -1, .signd = -1,
131 /* FIXME: the following three formats are not defined properly yet */
132 [SNDRV_PCM_FORMAT_MPEG] = {
133 .le = -1, .signd = -1,
135 [SNDRV_PCM_FORMAT_GSM] = {
136 .le = -1, .signd = -1,
138 [SNDRV_PCM_FORMAT_SPECIAL] = {
139 .le = -1, .signd = -1,
141 [SNDRV_PCM_FORMAT_S24_3LE] = {
142 .width = 24, .phys = 24, .le = 1, .signd = 1,
145 [SNDRV_PCM_FORMAT_S24_3BE] = {
146 .width = 24, .phys = 24, .le = 0, .signd = 1,
149 [SNDRV_PCM_FORMAT_U24_3LE] = {
150 .width = 24, .phys = 24, .le = 1, .signd = 0,
151 .silence = { 0x00, 0x00, 0x80 },
153 [SNDRV_PCM_FORMAT_U24_3BE] = {
154 .width = 24, .phys = 24, .le = 0, .signd = 0,
155 .silence = { 0x80, 0x00, 0x00 },
157 [SNDRV_PCM_FORMAT_S20_3LE] = {
158 .width = 20, .phys = 24, .le = 1, .signd = 1,
161 [SNDRV_PCM_FORMAT_S20_3BE] = {
162 .width = 20, .phys = 24, .le = 0, .signd = 1,
165 [SNDRV_PCM_FORMAT_U20_3LE] = {
166 .width = 20, .phys = 24, .le = 1, .signd = 0,
167 .silence = { 0x00, 0x00, 0x08 },
169 [SNDRV_PCM_FORMAT_U20_3BE] = {
170 .width = 20, .phys = 24, .le = 0, .signd = 0,
171 .silence = { 0x08, 0x00, 0x00 },
173 [SNDRV_PCM_FORMAT_S18_3LE] = {
174 .width = 18, .phys = 24, .le = 1, .signd = 1,
177 [SNDRV_PCM_FORMAT_S18_3BE] = {
178 .width = 18, .phys = 24, .le = 0, .signd = 1,
181 [SNDRV_PCM_FORMAT_U18_3LE] = {
182 .width = 18, .phys = 24, .le = 1, .signd = 0,
183 .silence = { 0x00, 0x00, 0x02 },
185 [SNDRV_PCM_FORMAT_U18_3BE] = {
186 .width = 18, .phys = 24, .le = 0, .signd = 0,
187 .silence = { 0x02, 0x00, 0x00 },
193 * snd_pcm_format_signed - Check the PCM format is signed linear
194 * @format: the format to check
196 * Returns 1 if the given PCM format is signed linear, 0 if unsigned
197 * linear, and a negative error code for non-linear formats.
199 int snd_pcm_format_signed(snd_pcm_format_t format)
202 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
204 if ((val = pcm_formats[format].signd) < 0)
209 EXPORT_SYMBOL(snd_pcm_format_signed);
212 * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
213 * @format: the format to check
215 * Returns 1 if the given PCM format is unsigned linear, 0 if signed
216 * linear, and a negative error code for non-linear formats.
218 int snd_pcm_format_unsigned(snd_pcm_format_t format)
222 val = snd_pcm_format_signed(format);
228 EXPORT_SYMBOL(snd_pcm_format_unsigned);
231 * snd_pcm_format_linear - Check the PCM format is linear
232 * @format: the format to check
234 * Returns 1 if the given PCM format is linear, 0 if not.
236 int snd_pcm_format_linear(snd_pcm_format_t format)
238 return snd_pcm_format_signed(format) >= 0;
241 EXPORT_SYMBOL(snd_pcm_format_linear);
244 * snd_pcm_format_little_endian - Check the PCM format is little-endian
245 * @format: the format to check
247 * Returns 1 if the given PCM format is little-endian, 0 if
248 * big-endian, or a negative error code if endian not specified.
250 int snd_pcm_format_little_endian(snd_pcm_format_t format)
253 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
255 if ((val = pcm_formats[format].le) < 0)
260 EXPORT_SYMBOL(snd_pcm_format_little_endian);
263 * snd_pcm_format_big_endian - Check the PCM format is big-endian
264 * @format: the format to check
266 * Returns 1 if the given PCM format is big-endian, 0 if
267 * little-endian, or a negative error code if endian not specified.
269 int snd_pcm_format_big_endian(snd_pcm_format_t format)
273 val = snd_pcm_format_little_endian(format);
279 EXPORT_SYMBOL(snd_pcm_format_big_endian);
282 * snd_pcm_format_width - return the bit-width of the format
283 * @format: the format to check
285 * Returns the bit-width of the format, or a negative error code
288 int snd_pcm_format_width(snd_pcm_format_t format)
291 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
293 if ((val = pcm_formats[format].width) == 0)
298 EXPORT_SYMBOL(snd_pcm_format_width);
301 * snd_pcm_format_physical_width - return the physical bit-width of the format
302 * @format: the format to check
304 * Returns the physical bit-width of the format, or a negative error code
307 int snd_pcm_format_physical_width(snd_pcm_format_t format)
310 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
312 if ((val = pcm_formats[format].phys) == 0)
317 EXPORT_SYMBOL(snd_pcm_format_physical_width);
320 * snd_pcm_format_size - return the byte size of samples on the given format
321 * @format: the format to check
322 * @samples: sampling rate
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);