blob: 1dbd5ad418cd5e3c20324e330c3016795717f5aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCM Interface - misc routines
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/time.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040023#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <sound/core.h>
25#include <sound/pcm.h>
26#define SND_PCM_FORMAT_UNKNOWN (-1)
27
28/* NOTE: "signed" prefix must be given below since the default char is
29 * unsigned on some architectures!
30 */
31struct 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 */
37};
38
Clemens Ladischfea952e2011-02-14 11:00:47 +010039/* we do lots of calculations on snd_pcm_format_t; shut up sparse */
40#define INT __force int
41
42static struct pcm_format_data pcm_formats[(INT)SNDRV_PCM_FORMAT_LAST+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 [SNDRV_PCM_FORMAT_S8] = {
44 .width = 8, .phys = 8, .le = -1, .signd = 1,
45 .silence = {},
46 },
47 [SNDRV_PCM_FORMAT_U8] = {
48 .width = 8, .phys = 8, .le = -1, .signd = 0,
49 .silence = { 0x80 },
50 },
51 [SNDRV_PCM_FORMAT_S16_LE] = {
52 .width = 16, .phys = 16, .le = 1, .signd = 1,
53 .silence = {},
54 },
55 [SNDRV_PCM_FORMAT_S16_BE] = {
56 .width = 16, .phys = 16, .le = 0, .signd = 1,
57 .silence = {},
58 },
59 [SNDRV_PCM_FORMAT_U16_LE] = {
60 .width = 16, .phys = 16, .le = 1, .signd = 0,
61 .silence = { 0x00, 0x80 },
62 },
63 [SNDRV_PCM_FORMAT_U16_BE] = {
64 .width = 16, .phys = 16, .le = 0, .signd = 0,
65 .silence = { 0x80, 0x00 },
66 },
67 [SNDRV_PCM_FORMAT_S24_LE] = {
68 .width = 24, .phys = 32, .le = 1, .signd = 1,
69 .silence = {},
70 },
71 [SNDRV_PCM_FORMAT_S24_BE] = {
72 .width = 24, .phys = 32, .le = 0, .signd = 1,
73 .silence = {},
74 },
75 [SNDRV_PCM_FORMAT_U24_LE] = {
76 .width = 24, .phys = 32, .le = 1, .signd = 0,
77 .silence = { 0x00, 0x00, 0x80 },
78 },
79 [SNDRV_PCM_FORMAT_U24_BE] = {
80 .width = 24, .phys = 32, .le = 0, .signd = 0,
Jaroslav Kysela67a7be72007-12-13 10:36:03 +010081 .silence = { 0x00, 0x80, 0x00, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 },
83 [SNDRV_PCM_FORMAT_S32_LE] = {
84 .width = 32, .phys = 32, .le = 1, .signd = 1,
85 .silence = {},
86 },
87 [SNDRV_PCM_FORMAT_S32_BE] = {
88 .width = 32, .phys = 32, .le = 0, .signd = 1,
89 .silence = {},
90 },
91 [SNDRV_PCM_FORMAT_U32_LE] = {
92 .width = 32, .phys = 32, .le = 1, .signd = 0,
93 .silence = { 0x00, 0x00, 0x00, 0x80 },
94 },
95 [SNDRV_PCM_FORMAT_U32_BE] = {
96 .width = 32, .phys = 32, .le = 0, .signd = 0,
97 .silence = { 0x80, 0x00, 0x00, 0x00 },
98 },
99 [SNDRV_PCM_FORMAT_FLOAT_LE] = {
100 .width = 32, .phys = 32, .le = 1, .signd = -1,
101 .silence = {},
102 },
103 [SNDRV_PCM_FORMAT_FLOAT_BE] = {
104 .width = 32, .phys = 32, .le = 0, .signd = -1,
105 .silence = {},
106 },
107 [SNDRV_PCM_FORMAT_FLOAT64_LE] = {
108 .width = 64, .phys = 64, .le = 1, .signd = -1,
109 .silence = {},
110 },
111 [SNDRV_PCM_FORMAT_FLOAT64_BE] = {
112 .width = 64, .phys = 64, .le = 0, .signd = -1,
113 .silence = {},
114 },
115 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE] = {
116 .width = 32, .phys = 32, .le = 1, .signd = -1,
117 .silence = {},
118 },
119 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE] = {
120 .width = 32, .phys = 32, .le = 0, .signd = -1,
121 .silence = {},
122 },
123 [SNDRV_PCM_FORMAT_MU_LAW] = {
124 .width = 8, .phys = 8, .le = -1, .signd = -1,
125 .silence = { 0x7f },
126 },
127 [SNDRV_PCM_FORMAT_A_LAW] = {
128 .width = 8, .phys = 8, .le = -1, .signd = -1,
129 .silence = { 0x55 },
130 },
131 [SNDRV_PCM_FORMAT_IMA_ADPCM] = {
132 .width = 4, .phys = 4, .le = -1, .signd = -1,
133 .silence = {},
134 },
Ben Collins15c0cee2010-05-28 11:43:45 -0400135 [SNDRV_PCM_FORMAT_G723_24] = {
136 .width = 3, .phys = 3, .le = -1, .signd = -1,
137 .silence = {},
138 },
139 [SNDRV_PCM_FORMAT_G723_40] = {
140 .width = 5, .phys = 5, .le = -1, .signd = -1,
141 .silence = {},
142 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* FIXME: the following three formats are not defined properly yet */
144 [SNDRV_PCM_FORMAT_MPEG] = {
145 .le = -1, .signd = -1,
146 },
147 [SNDRV_PCM_FORMAT_GSM] = {
148 .le = -1, .signd = -1,
149 },
150 [SNDRV_PCM_FORMAT_SPECIAL] = {
Helen Zengb9d00ce2011-10-13 17:25:50 -0700151 /* set the width and phys same as S16_LE */
152 .width = 16, .phys = 16, .le = -1, .signd = -1,
153 .silence = {},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 },
155 [SNDRV_PCM_FORMAT_S24_3LE] = {
156 .width = 24, .phys = 24, .le = 1, .signd = 1,
157 .silence = {},
158 },
159 [SNDRV_PCM_FORMAT_S24_3BE] = {
160 .width = 24, .phys = 24, .le = 0, .signd = 1,
161 .silence = {},
162 },
163 [SNDRV_PCM_FORMAT_U24_3LE] = {
164 .width = 24, .phys = 24, .le = 1, .signd = 0,
165 .silence = { 0x00, 0x00, 0x80 },
166 },
167 [SNDRV_PCM_FORMAT_U24_3BE] = {
168 .width = 24, .phys = 24, .le = 0, .signd = 0,
169 .silence = { 0x80, 0x00, 0x00 },
170 },
171 [SNDRV_PCM_FORMAT_S20_3LE] = {
172 .width = 20, .phys = 24, .le = 1, .signd = 1,
173 .silence = {},
174 },
175 [SNDRV_PCM_FORMAT_S20_3BE] = {
176 .width = 20, .phys = 24, .le = 0, .signd = 1,
177 .silence = {},
178 },
179 [SNDRV_PCM_FORMAT_U20_3LE] = {
180 .width = 20, .phys = 24, .le = 1, .signd = 0,
181 .silence = { 0x00, 0x00, 0x08 },
182 },
183 [SNDRV_PCM_FORMAT_U20_3BE] = {
184 .width = 20, .phys = 24, .le = 0, .signd = 0,
185 .silence = { 0x08, 0x00, 0x00 },
186 },
187 [SNDRV_PCM_FORMAT_S18_3LE] = {
188 .width = 18, .phys = 24, .le = 1, .signd = 1,
189 .silence = {},
190 },
191 [SNDRV_PCM_FORMAT_S18_3BE] = {
192 .width = 18, .phys = 24, .le = 0, .signd = 1,
193 .silence = {},
194 },
195 [SNDRV_PCM_FORMAT_U18_3LE] = {
196 .width = 18, .phys = 24, .le = 1, .signd = 0,
197 .silence = { 0x00, 0x00, 0x02 },
198 },
199 [SNDRV_PCM_FORMAT_U18_3BE] = {
200 .width = 18, .phys = 24, .le = 0, .signd = 0,
201 .silence = { 0x02, 0x00, 0x00 },
202 },
Ben Collins15c0cee2010-05-28 11:43:45 -0400203 [SNDRV_PCM_FORMAT_G723_24_1B] = {
204 .width = 3, .phys = 8, .le = -1, .signd = -1,
205 .silence = {},
206 },
207 [SNDRV_PCM_FORMAT_G723_40_1B] = {
208 .width = 5, .phys = 8, .le = -1, .signd = -1,
209 .silence = {},
210 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
213
214/**
215 * snd_pcm_format_signed - Check the PCM format is signed linear
216 * @format: the format to check
217 *
218 * Returns 1 if the given PCM format is signed linear, 0 if unsigned
219 * linear, and a negative error code for non-linear formats.
220 */
221int snd_pcm_format_signed(snd_pcm_format_t format)
222{
223 int val;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100224 if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -EINVAL;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100226 if ((val = pcm_formats[(INT)format].signd) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -EINVAL;
228 return val;
229}
230
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200231EXPORT_SYMBOL(snd_pcm_format_signed);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/**
234 * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
235 * @format: the format to check
236 *
237 * Returns 1 if the given PCM format is unsigned linear, 0 if signed
238 * linear, and a negative error code for non-linear formats.
239 */
240int snd_pcm_format_unsigned(snd_pcm_format_t format)
241{
242 int val;
243
244 val = snd_pcm_format_signed(format);
245 if (val < 0)
246 return val;
247 return !val;
248}
249
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200250EXPORT_SYMBOL(snd_pcm_format_unsigned);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/**
253 * snd_pcm_format_linear - Check the PCM format is linear
254 * @format: the format to check
255 *
256 * Returns 1 if the given PCM format is linear, 0 if not.
257 */
258int snd_pcm_format_linear(snd_pcm_format_t format)
259{
260 return snd_pcm_format_signed(format) >= 0;
261}
262
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200263EXPORT_SYMBOL(snd_pcm_format_linear);
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/**
266 * snd_pcm_format_little_endian - Check the PCM format is little-endian
267 * @format: the format to check
268 *
269 * Returns 1 if the given PCM format is little-endian, 0 if
270 * big-endian, or a negative error code if endian not specified.
271 */
272int snd_pcm_format_little_endian(snd_pcm_format_t format)
273{
274 int val;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100275 if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EINVAL;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100277 if ((val = pcm_formats[(INT)format].le) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -EINVAL;
279 return val;
280}
281
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200282EXPORT_SYMBOL(snd_pcm_format_little_endian);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/**
285 * snd_pcm_format_big_endian - Check the PCM format is big-endian
286 * @format: the format to check
287 *
288 * Returns 1 if the given PCM format is big-endian, 0 if
289 * little-endian, or a negative error code if endian not specified.
290 */
291int snd_pcm_format_big_endian(snd_pcm_format_t format)
292{
293 int val;
294
295 val = snd_pcm_format_little_endian(format);
296 if (val < 0)
297 return val;
298 return !val;
299}
300
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200301EXPORT_SYMBOL(snd_pcm_format_big_endian);
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * snd_pcm_format_width - return the bit-width of the format
305 * @format: the format to check
306 *
307 * Returns the bit-width of the format, or a negative error code
308 * if unknown format.
309 */
310int snd_pcm_format_width(snd_pcm_format_t format)
311{
312 int val;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100313 if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -EINVAL;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100315 if ((val = pcm_formats[(INT)format].width) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EINVAL;
317 return val;
318}
319
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200320EXPORT_SYMBOL(snd_pcm_format_width);
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/**
323 * snd_pcm_format_physical_width - return the physical bit-width of the format
324 * @format: the format to check
325 *
326 * Returns the physical bit-width of the format, or a negative error code
327 * if unknown format.
328 */
329int snd_pcm_format_physical_width(snd_pcm_format_t format)
330{
331 int val;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100332 if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -EINVAL;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100334 if ((val = pcm_formats[(INT)format].phys) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return -EINVAL;
336 return val;
337}
338
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200339EXPORT_SYMBOL(snd_pcm_format_physical_width);
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/**
342 * snd_pcm_format_size - return the byte size of samples on the given format
343 * @format: the format to check
Randy Dunlapa66547f2008-10-17 11:28:11 -0700344 * @samples: sampling rate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *
346 * Returns the byte size of the given samples for the format, or a
347 * negative error code if unknown format.
348 */
349ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
350{
351 int phys_width = snd_pcm_format_physical_width(format);
352 if (phys_width < 0)
353 return -EINVAL;
354 return samples * phys_width / 8;
355}
356
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200357EXPORT_SYMBOL(snd_pcm_format_size);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/**
360 * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
361 * @format: the format to check
362 *
363 * Returns the format pattern to fill or NULL if error.
364 */
365const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
366{
Clemens Ladischfea952e2011-02-14 11:00:47 +0100367 if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return NULL;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100369 if (! pcm_formats[(INT)format].phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return NULL;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100371 return pcm_formats[(INT)format].silence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200374EXPORT_SYMBOL(snd_pcm_format_silence_64);
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/**
377 * snd_pcm_format_set_silence - set the silence data on the buffer
378 * @format: the PCM format
379 * @data: the buffer pointer
380 * @samples: the number of samples to set silence
381 *
382 * Sets the silence data on the buffer for the given samples.
383 *
384 * Returns zero if successful, or a negative error code on failure.
385 */
386int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
387{
388 int width;
389 unsigned char *dst, *pat;
390
Clemens Ladischfea952e2011-02-14 11:00:47 +0100391 if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -EINVAL;
393 if (samples == 0)
394 return 0;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100395 width = pcm_formats[(INT)format].phys; /* physical width */
396 pat = pcm_formats[(INT)format].silence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (! width)
398 return -EINVAL;
399 /* signed or 1 byte data */
Clemens Ladischfea952e2011-02-14 11:00:47 +0100400 if (pcm_formats[(INT)format].signd == 1 || width <= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unsigned int bytes = samples * width / 8;
402 memset(data, *pat, bytes);
403 return 0;
404 }
405 /* non-zero samples, fill using a loop */
406 width /= 8;
407 dst = data;
408#if 0
409 while (samples--) {
410 memcpy(dst, pat, width);
411 dst += width;
412 }
413#else
414 /* a bit optimization for constant width */
415 switch (width) {
416 case 2:
417 while (samples--) {
418 memcpy(dst, pat, 2);
419 dst += 2;
420 }
421 break;
422 case 3:
423 while (samples--) {
424 memcpy(dst, pat, 3);
425 dst += 3;
426 }
427 break;
428 case 4:
429 while (samples--) {
430 memcpy(dst, pat, 4);
431 dst += 4;
432 }
433 break;
434 case 8:
435 while (samples--) {
436 memcpy(dst, pat, 8);
437 dst += 8;
438 }
439 break;
440 }
441#endif
442 return 0;
443}
444
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200445EXPORT_SYMBOL(snd_pcm_format_set_silence);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/**
448 * snd_pcm_limit_hw_rates - determine rate_min/rate_max fields
449 * @runtime: the runtime instance
450 *
451 * Determines the rate_min and rate_max fields from the rates bits of
452 * the given runtime->hw.
453 *
454 * Returns zero if successful.
455 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100456int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int i;
Clemens Ladisch7653d552007-08-13 17:38:54 +0200459 for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (runtime->hw.rates & (1 << i)) {
Clemens Ladisch7653d552007-08-13 17:38:54 +0200461 runtime->hw.rate_min = snd_pcm_known_rates.list[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
463 }
464 }
Clemens Ladisch7653d552007-08-13 17:38:54 +0200465 for (i = (int)snd_pcm_known_rates.count - 1; i >= 0; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (runtime->hw.rates & (1 << i)) {
Clemens Ladisch7653d552007-08-13 17:38:54 +0200467 runtime->hw.rate_max = snd_pcm_known_rates.list[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469 }
470 }
471 return 0;
472}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200473
474EXPORT_SYMBOL(snd_pcm_limit_hw_rates);
Clemens Ladisch918f3a02007-08-13 17:40:54 +0200475
476/**
477 * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
478 * @rate: the sample rate to convert
479 *
480 * Returns the SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
481 * SNDRV_PCM_RATE_KNOT for an unknown rate.
482 */
483unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
484{
485 unsigned int i;
486
487 for (i = 0; i < snd_pcm_known_rates.count; i++)
488 if (snd_pcm_known_rates.list[i] == rate)
489 return 1u << i;
490 return SNDRV_PCM_RATE_KNOT;
491}
492EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);