2 * PCM Interface - misc routines
3 * Copyright (c) 1998 by Jaroslav Kysela <perex@suse.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 struct pcm_format_data {
29 char width; /* bit width */
30 char phys; /* physical bit width */
31 char le; /* 0 = big-endian, 1 = little-endian, -1 = others */
32 char signd; /* 0 = unsigned, 1 = signed, -1 = others */
33 unsigned char silence[8]; /* silence data to fill */
36 static struct pcm_format_data pcm_formats[SNDRV_PCM_FORMAT_LAST+1] = {
37 [SNDRV_PCM_FORMAT_S8] = {
38 .width = 8, .phys = 8, .le = -1, .signd = 1,
41 [SNDRV_PCM_FORMAT_U8] = {
42 .width = 8, .phys = 8, .le = -1, .signd = 0,
45 [SNDRV_PCM_FORMAT_S16_LE] = {
46 .width = 16, .phys = 16, .le = 1, .signd = 1,
49 [SNDRV_PCM_FORMAT_S16_BE] = {
50 .width = 16, .phys = 16, .le = 0, .signd = 1,
53 [SNDRV_PCM_FORMAT_U16_LE] = {
54 .width = 16, .phys = 16, .le = 1, .signd = 0,
55 .silence = { 0x00, 0x80 },
57 [SNDRV_PCM_FORMAT_U16_BE] = {
58 .width = 16, .phys = 16, .le = 0, .signd = 0,
59 .silence = { 0x80, 0x00 },
61 [SNDRV_PCM_FORMAT_S24_LE] = {
62 .width = 24, .phys = 32, .le = 1, .signd = 1,
65 [SNDRV_PCM_FORMAT_S24_BE] = {
66 .width = 24, .phys = 32, .le = 0, .signd = 1,
69 [SNDRV_PCM_FORMAT_U24_LE] = {
70 .width = 24, .phys = 32, .le = 1, .signd = 0,
71 .silence = { 0x00, 0x00, 0x80 },
73 [SNDRV_PCM_FORMAT_U24_BE] = {
74 .width = 24, .phys = 32, .le = 0, .signd = 0,
75 .silence = { 0x80, 0x00, 0x00 },
77 [SNDRV_PCM_FORMAT_S32_LE] = {
78 .width = 32, .phys = 32, .le = 1, .signd = 1,
81 [SNDRV_PCM_FORMAT_S32_BE] = {
82 .width = 32, .phys = 32, .le = 0, .signd = 1,
85 [SNDRV_PCM_FORMAT_U32_LE] = {
86 .width = 32, .phys = 32, .le = 1, .signd = 0,
87 .silence = { 0x00, 0x00, 0x00, 0x80 },
89 [SNDRV_PCM_FORMAT_U32_BE] = {
90 .width = 32, .phys = 32, .le = 0, .signd = 0,
91 .silence = { 0x80, 0x00, 0x00, 0x00 },
93 [SNDRV_PCM_FORMAT_FLOAT_LE] = {
94 .width = 32, .phys = 32, .le = 1, .signd = -1,
97 [SNDRV_PCM_FORMAT_FLOAT_BE] = {
98 .width = 32, .phys = 32, .le = 0, .signd = -1,
101 [SNDRV_PCM_FORMAT_FLOAT64_LE] = {
102 .width = 64, .phys = 64, .le = 1, .signd = -1,
105 [SNDRV_PCM_FORMAT_FLOAT64_BE] = {
106 .width = 64, .phys = 64, .le = 0, .signd = -1,
109 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE] = {
110 .width = 32, .phys = 32, .le = 1, .signd = -1,
113 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE] = {
114 .width = 32, .phys = 32, .le = 0, .signd = -1,
117 [SNDRV_PCM_FORMAT_MU_LAW] = {
118 .width = 8, .phys = 8, .le = -1, .signd = -1,
121 [SNDRV_PCM_FORMAT_A_LAW] = {
122 .width = 8, .phys = 8, .le = -1, .signd = -1,
125 [SNDRV_PCM_FORMAT_IMA_ADPCM] = {
126 .width = 4, .phys = 4, .le = -1, .signd = -1,
129 /* FIXME: the following three formats are not defined properly yet */
130 [SNDRV_PCM_FORMAT_MPEG] = {
131 .le = -1, .signd = -1,
133 [SNDRV_PCM_FORMAT_GSM] = {
134 .le = -1, .signd = -1,
136 [SNDRV_PCM_FORMAT_SPECIAL] = {
137 .le = -1, .signd = -1,
139 [SNDRV_PCM_FORMAT_S24_3LE] = {
140 .width = 24, .phys = 24, .le = 1, .signd = 1,
143 [SNDRV_PCM_FORMAT_S24_3BE] = {
144 .width = 24, .phys = 24, .le = 0, .signd = 1,
147 [SNDRV_PCM_FORMAT_U24_3LE] = {
148 .width = 24, .phys = 24, .le = 1, .signd = 0,
149 .silence = { 0x00, 0x00, 0x80 },
151 [SNDRV_PCM_FORMAT_U24_3BE] = {
152 .width = 24, .phys = 24, .le = 0, .signd = 0,
153 .silence = { 0x80, 0x00, 0x00 },
155 [SNDRV_PCM_FORMAT_S20_3LE] = {
156 .width = 20, .phys = 24, .le = 1, .signd = 1,
159 [SNDRV_PCM_FORMAT_S20_3BE] = {
160 .width = 20, .phys = 24, .le = 0, .signd = 1,
163 [SNDRV_PCM_FORMAT_U20_3LE] = {
164 .width = 20, .phys = 24, .le = 1, .signd = 0,
165 .silence = { 0x00, 0x00, 0x08 },
167 [SNDRV_PCM_FORMAT_U20_3BE] = {
168 .width = 20, .phys = 24, .le = 0, .signd = 0,
169 .silence = { 0x08, 0x00, 0x00 },
171 [SNDRV_PCM_FORMAT_S18_3LE] = {
172 .width = 18, .phys = 24, .le = 1, .signd = 1,
175 [SNDRV_PCM_FORMAT_S18_3BE] = {
176 .width = 18, .phys = 24, .le = 0, .signd = 1,
179 [SNDRV_PCM_FORMAT_U18_3LE] = {
180 .width = 18, .phys = 24, .le = 1, .signd = 0,
181 .silence = { 0x00, 0x00, 0x02 },
183 [SNDRV_PCM_FORMAT_U18_3BE] = {
184 .width = 18, .phys = 24, .le = 0, .signd = 0,
185 .silence = { 0x02, 0x00, 0x00 },
191 * snd_pcm_format_signed - Check the PCM format is signed linear
192 * @format: the format to check
194 * Returns 1 if the given PCM format is signed linear, 0 if unsigned
195 * linear, and a negative error code for non-linear formats.
197 int snd_pcm_format_signed(snd_pcm_format_t format)
200 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
202 if ((val = pcm_formats[format].signd) < 0)
208 * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
209 * @format: the format to check
211 * Returns 1 if the given PCM format is unsigned linear, 0 if signed
212 * linear, and a negative error code for non-linear formats.
214 int snd_pcm_format_unsigned(snd_pcm_format_t format)
218 val = snd_pcm_format_signed(format);
225 * snd_pcm_format_linear - Check the PCM format is linear
226 * @format: the format to check
228 * Returns 1 if the given PCM format is linear, 0 if not.
230 int snd_pcm_format_linear(snd_pcm_format_t format)
232 return snd_pcm_format_signed(format) >= 0;
236 * snd_pcm_format_little_endian - Check the PCM format is little-endian
237 * @format: the format to check
239 * Returns 1 if the given PCM format is little-endian, 0 if
240 * big-endian, or a negative error code if endian not specified.
242 int snd_pcm_format_little_endian(snd_pcm_format_t format)
245 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
247 if ((val = pcm_formats[format].le) < 0)
253 * snd_pcm_format_big_endian - Check the PCM format is big-endian
254 * @format: the format to check
256 * Returns 1 if the given PCM format is big-endian, 0 if
257 * little-endian, or a negative error code if endian not specified.
259 int snd_pcm_format_big_endian(snd_pcm_format_t format)
263 val = snd_pcm_format_little_endian(format);
270 * snd_pcm_format_cpu_endian - Check the PCM format is CPU-endian
271 * @format: the format to check
273 * Returns 1 if the given PCM format is CPU-endian, 0 if
274 * opposite, or a negative error code if endian not specified.
276 int snd_pcm_format_cpu_endian(snd_pcm_format_t format)
278 #ifdef SNDRV_LITTLE_ENDIAN
279 return snd_pcm_format_little_endian(format);
281 return snd_pcm_format_big_endian(format);
286 * snd_pcm_format_width - return the bit-width of the format
287 * @format: the format to check
289 * Returns the bit-width of the format, or a negative error code
292 int snd_pcm_format_width(snd_pcm_format_t format)
295 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
297 if ((val = pcm_formats[format].width) == 0)
303 * snd_pcm_format_physical_width - return the physical bit-width of the format
304 * @format: the format to check
306 * Returns the physical bit-width of the format, or a negative error code
309 int snd_pcm_format_physical_width(snd_pcm_format_t format)
312 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
314 if ((val = pcm_formats[format].phys) == 0)
320 * snd_pcm_format_size - return the byte size of samples on the given format
321 * @format: the format to check
323 * Returns the byte size of the given samples for the format, or a
324 * negative error code if unknown format.
326 ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
328 int phys_width = snd_pcm_format_physical_width(format);
331 return samples * phys_width / 8;
335 * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
336 * @format: the format to check
338 * Returns the format pattern to fill or NULL if error.
340 const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
342 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
344 if (! pcm_formats[format].phys)
346 return pcm_formats[format].silence;
350 * snd_pcm_format_set_silence - set the silence data on the buffer
351 * @format: the PCM format
352 * @data: the buffer pointer
353 * @samples: the number of samples to set silence
355 * Sets the silence data on the buffer for the given samples.
357 * Returns zero if successful, or a negative error code on failure.
359 int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
362 unsigned char *dst, *pat;
364 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
368 width = pcm_formats[format].phys; /* physical width */
369 pat = pcm_formats[format].silence;
372 /* signed or 1 byte data */
373 if (pcm_formats[format].signd == 1 || width <= 8) {
374 unsigned int bytes = samples * width / 8;
375 memset(data, *pat, bytes);
378 /* non-zero samples, fill using a loop */
383 memcpy(dst, pat, width);
387 /* a bit optimization for constant width */
418 /* [width][unsigned][bigendian] */
419 static int linear_formats[4][2][2] = {
420 {{ SNDRV_PCM_FORMAT_S8, SNDRV_PCM_FORMAT_S8},
421 { SNDRV_PCM_FORMAT_U8, SNDRV_PCM_FORMAT_U8}},
422 {{SNDRV_PCM_FORMAT_S16_LE, SNDRV_PCM_FORMAT_S16_BE},
423 {SNDRV_PCM_FORMAT_U16_LE, SNDRV_PCM_FORMAT_U16_BE}},
424 {{SNDRV_PCM_FORMAT_S24_LE, SNDRV_PCM_FORMAT_S24_BE},
425 {SNDRV_PCM_FORMAT_U24_LE, SNDRV_PCM_FORMAT_U24_BE}},
426 {{SNDRV_PCM_FORMAT_S32_LE, SNDRV_PCM_FORMAT_S32_BE},
427 {SNDRV_PCM_FORMAT_U32_LE, SNDRV_PCM_FORMAT_U32_BE}}
431 * snd_pcm_build_linear_format - return the suitable linear format for the given condition
432 * @width: the bit-width
433 * @unsignd: 1 if unsigned, 0 if signed.
434 * @big_endian: 1 if big-endian, 0 if little-endian
436 * Returns the suitable linear format for the given condition.
438 snd_pcm_format_t snd_pcm_build_linear_format(int width, int unsignd, int big_endian)
441 return SND_PCM_FORMAT_UNKNOWN;
442 width = (width / 8) - 1;
443 if (width < 0 || width >= 4)
444 return SND_PCM_FORMAT_UNKNOWN;
445 return linear_formats[width][!!unsignd][!!big_endian];
449 * snd_pcm_limit_hw_rates - determine rate_min/rate_max fields
450 * @runtime: the runtime instance
452 * Determines the rate_min and rate_max fields from the rates bits of
453 * the given runtime->hw.
455 * Returns zero if successful.
457 int snd_pcm_limit_hw_rates(snd_pcm_runtime_t *runtime)
459 static unsigned rates[] = {
460 /* ATTENTION: these values depend on the definition in pcm.h! */
461 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
462 64000, 88200, 96000, 176400, 192000
465 for (i = 0; i < (int)ARRAY_SIZE(rates); i++) {
466 if (runtime->hw.rates & (1 << i)) {
467 runtime->hw.rate_min = rates[i];
471 for (i = (int)ARRAY_SIZE(rates) - 1; i >= 0; i--) {
472 if (runtime->hw.rates & (1 << i)) {
473 runtime->hw.rate_max = rates[i];