blob: 89b7f549bebd772bb62378510a541bb0b052e967 [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>
23#include <sound/core.h>
24#include <sound/pcm.h>
25#define SND_PCM_FORMAT_UNKNOWN (-1)
26
27/* NOTE: "signed" prefix must be given below since the default char is
28 * unsigned on some architectures!
29 */
30struct 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 */
36};
37
38static 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,
41 .silence = {},
42 },
43 [SNDRV_PCM_FORMAT_U8] = {
44 .width = 8, .phys = 8, .le = -1, .signd = 0,
45 .silence = { 0x80 },
46 },
47 [SNDRV_PCM_FORMAT_S16_LE] = {
48 .width = 16, .phys = 16, .le = 1, .signd = 1,
49 .silence = {},
50 },
51 [SNDRV_PCM_FORMAT_S16_BE] = {
52 .width = 16, .phys = 16, .le = 0, .signd = 1,
53 .silence = {},
54 },
55 [SNDRV_PCM_FORMAT_U16_LE] = {
56 .width = 16, .phys = 16, .le = 1, .signd = 0,
57 .silence = { 0x00, 0x80 },
58 },
59 [SNDRV_PCM_FORMAT_U16_BE] = {
60 .width = 16, .phys = 16, .le = 0, .signd = 0,
61 .silence = { 0x80, 0x00 },
62 },
63 [SNDRV_PCM_FORMAT_S24_LE] = {
64 .width = 24, .phys = 32, .le = 1, .signd = 1,
65 .silence = {},
66 },
67 [SNDRV_PCM_FORMAT_S24_BE] = {
68 .width = 24, .phys = 32, .le = 0, .signd = 1,
69 .silence = {},
70 },
71 [SNDRV_PCM_FORMAT_U24_LE] = {
72 .width = 24, .phys = 32, .le = 1, .signd = 0,
73 .silence = { 0x00, 0x00, 0x80 },
74 },
75 [SNDRV_PCM_FORMAT_U24_BE] = {
76 .width = 24, .phys = 32, .le = 0, .signd = 0,
Jaroslav Kysela67a7be72007-12-13 10:36:03 +010077 .silence = { 0x00, 0x80, 0x00, 0x00 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 },
79 [SNDRV_PCM_FORMAT_S32_LE] = {
80 .width = 32, .phys = 32, .le = 1, .signd = 1,
81 .silence = {},
82 },
83 [SNDRV_PCM_FORMAT_S32_BE] = {
84 .width = 32, .phys = 32, .le = 0, .signd = 1,
85 .silence = {},
86 },
87 [SNDRV_PCM_FORMAT_U32_LE] = {
88 .width = 32, .phys = 32, .le = 1, .signd = 0,
89 .silence = { 0x00, 0x00, 0x00, 0x80 },
90 },
91 [SNDRV_PCM_FORMAT_U32_BE] = {
92 .width = 32, .phys = 32, .le = 0, .signd = 0,
93 .silence = { 0x80, 0x00, 0x00, 0x00 },
94 },
95 [SNDRV_PCM_FORMAT_FLOAT_LE] = {
96 .width = 32, .phys = 32, .le = 1, .signd = -1,
97 .silence = {},
98 },
99 [SNDRV_PCM_FORMAT_FLOAT_BE] = {
100 .width = 32, .phys = 32, .le = 0, .signd = -1,
101 .silence = {},
102 },
103 [SNDRV_PCM_FORMAT_FLOAT64_LE] = {
104 .width = 64, .phys = 64, .le = 1, .signd = -1,
105 .silence = {},
106 },
107 [SNDRV_PCM_FORMAT_FLOAT64_BE] = {
108 .width = 64, .phys = 64, .le = 0, .signd = -1,
109 .silence = {},
110 },
111 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE] = {
112 .width = 32, .phys = 32, .le = 1, .signd = -1,
113 .silence = {},
114 },
115 [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE] = {
116 .width = 32, .phys = 32, .le = 0, .signd = -1,
117 .silence = {},
118 },
119 [SNDRV_PCM_FORMAT_MU_LAW] = {
120 .width = 8, .phys = 8, .le = -1, .signd = -1,
121 .silence = { 0x7f },
122 },
123 [SNDRV_PCM_FORMAT_A_LAW] = {
124 .width = 8, .phys = 8, .le = -1, .signd = -1,
125 .silence = { 0x55 },
126 },
127 [SNDRV_PCM_FORMAT_IMA_ADPCM] = {
128 .width = 4, .phys = 4, .le = -1, .signd = -1,
129 .silence = {},
130 },
131 /* FIXME: the following three formats are not defined properly yet */
132 [SNDRV_PCM_FORMAT_MPEG] = {
133 .le = -1, .signd = -1,
134 },
135 [SNDRV_PCM_FORMAT_GSM] = {
136 .le = -1, .signd = -1,
137 },
138 [SNDRV_PCM_FORMAT_SPECIAL] = {
139 .le = -1, .signd = -1,
140 },
141 [SNDRV_PCM_FORMAT_S24_3LE] = {
142 .width = 24, .phys = 24, .le = 1, .signd = 1,
143 .silence = {},
144 },
145 [SNDRV_PCM_FORMAT_S24_3BE] = {
146 .width = 24, .phys = 24, .le = 0, .signd = 1,
147 .silence = {},
148 },
149 [SNDRV_PCM_FORMAT_U24_3LE] = {
150 .width = 24, .phys = 24, .le = 1, .signd = 0,
151 .silence = { 0x00, 0x00, 0x80 },
152 },
153 [SNDRV_PCM_FORMAT_U24_3BE] = {
154 .width = 24, .phys = 24, .le = 0, .signd = 0,
155 .silence = { 0x80, 0x00, 0x00 },
156 },
157 [SNDRV_PCM_FORMAT_S20_3LE] = {
158 .width = 20, .phys = 24, .le = 1, .signd = 1,
159 .silence = {},
160 },
161 [SNDRV_PCM_FORMAT_S20_3BE] = {
162 .width = 20, .phys = 24, .le = 0, .signd = 1,
163 .silence = {},
164 },
165 [SNDRV_PCM_FORMAT_U20_3LE] = {
166 .width = 20, .phys = 24, .le = 1, .signd = 0,
167 .silence = { 0x00, 0x00, 0x08 },
168 },
169 [SNDRV_PCM_FORMAT_U20_3BE] = {
170 .width = 20, .phys = 24, .le = 0, .signd = 0,
171 .silence = { 0x08, 0x00, 0x00 },
172 },
173 [SNDRV_PCM_FORMAT_S18_3LE] = {
174 .width = 18, .phys = 24, .le = 1, .signd = 1,
175 .silence = {},
176 },
177 [SNDRV_PCM_FORMAT_S18_3BE] = {
178 .width = 18, .phys = 24, .le = 0, .signd = 1,
179 .silence = {},
180 },
181 [SNDRV_PCM_FORMAT_U18_3LE] = {
182 .width = 18, .phys = 24, .le = 1, .signd = 0,
183 .silence = { 0x00, 0x00, 0x02 },
184 },
185 [SNDRV_PCM_FORMAT_U18_3BE] = {
186 .width = 18, .phys = 24, .le = 0, .signd = 0,
187 .silence = { 0x02, 0x00, 0x00 },
188 },
189};
190
191
192/**
193 * snd_pcm_format_signed - Check the PCM format is signed linear
194 * @format: the format to check
195 *
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.
198 */
199int snd_pcm_format_signed(snd_pcm_format_t format)
200{
201 int val;
202 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
203 return -EINVAL;
204 if ((val = pcm_formats[format].signd) < 0)
205 return -EINVAL;
206 return val;
207}
208
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200209EXPORT_SYMBOL(snd_pcm_format_signed);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/**
212 * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
213 * @format: the format to check
214 *
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.
217 */
218int snd_pcm_format_unsigned(snd_pcm_format_t format)
219{
220 int val;
221
222 val = snd_pcm_format_signed(format);
223 if (val < 0)
224 return val;
225 return !val;
226}
227
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200228EXPORT_SYMBOL(snd_pcm_format_unsigned);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/**
231 * snd_pcm_format_linear - Check the PCM format is linear
232 * @format: the format to check
233 *
234 * Returns 1 if the given PCM format is linear, 0 if not.
235 */
236int snd_pcm_format_linear(snd_pcm_format_t format)
237{
238 return snd_pcm_format_signed(format) >= 0;
239}
240
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200241EXPORT_SYMBOL(snd_pcm_format_linear);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/**
244 * snd_pcm_format_little_endian - Check the PCM format is little-endian
245 * @format: the format to check
246 *
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.
249 */
250int snd_pcm_format_little_endian(snd_pcm_format_t format)
251{
252 int val;
253 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
254 return -EINVAL;
255 if ((val = pcm_formats[format].le) < 0)
256 return -EINVAL;
257 return val;
258}
259
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200260EXPORT_SYMBOL(snd_pcm_format_little_endian);
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/**
263 * snd_pcm_format_big_endian - Check the PCM format is big-endian
264 * @format: the format to check
265 *
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.
268 */
269int snd_pcm_format_big_endian(snd_pcm_format_t format)
270{
271 int val;
272
273 val = snd_pcm_format_little_endian(format);
274 if (val < 0)
275 return val;
276 return !val;
277}
278
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200279EXPORT_SYMBOL(snd_pcm_format_big_endian);
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * snd_pcm_format_width - return the bit-width of the format
283 * @format: the format to check
284 *
285 * Returns the bit-width of the format, or a negative error code
286 * if unknown format.
287 */
288int snd_pcm_format_width(snd_pcm_format_t format)
289{
290 int val;
291 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
292 return -EINVAL;
293 if ((val = pcm_formats[format].width) == 0)
294 return -EINVAL;
295 return val;
296}
297
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200298EXPORT_SYMBOL(snd_pcm_format_width);
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/**
301 * snd_pcm_format_physical_width - return the physical bit-width of the format
302 * @format: the format to check
303 *
304 * Returns the physical bit-width of the format, or a negative error code
305 * if unknown format.
306 */
307int snd_pcm_format_physical_width(snd_pcm_format_t format)
308{
309 int val;
310 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
311 return -EINVAL;
312 if ((val = pcm_formats[format].phys) == 0)
313 return -EINVAL;
314 return val;
315}
316
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200317EXPORT_SYMBOL(snd_pcm_format_physical_width);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/**
320 * snd_pcm_format_size - return the byte size of samples on the given format
321 * @format: the format to check
322 *
323 * Returns the byte size of the given samples for the format, or a
324 * negative error code if unknown format.
325 */
326ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
327{
328 int phys_width = snd_pcm_format_physical_width(format);
329 if (phys_width < 0)
330 return -EINVAL;
331 return samples * phys_width / 8;
332}
333
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200334EXPORT_SYMBOL(snd_pcm_format_size);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/**
337 * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
338 * @format: the format to check
339 *
340 * Returns the format pattern to fill or NULL if error.
341 */
342const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
343{
344 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
345 return NULL;
346 if (! pcm_formats[format].phys)
347 return NULL;
348 return pcm_formats[format].silence;
349}
350
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200351EXPORT_SYMBOL(snd_pcm_format_silence_64);
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/**
354 * snd_pcm_format_set_silence - set the silence data on the buffer
355 * @format: the PCM format
356 * @data: the buffer pointer
357 * @samples: the number of samples to set silence
358 *
359 * Sets the silence data on the buffer for the given samples.
360 *
361 * Returns zero if successful, or a negative error code on failure.
362 */
363int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
364{
365 int width;
366 unsigned char *dst, *pat;
367
368 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
369 return -EINVAL;
370 if (samples == 0)
371 return 0;
372 width = pcm_formats[format].phys; /* physical width */
373 pat = pcm_formats[format].silence;
374 if (! width)
375 return -EINVAL;
376 /* signed or 1 byte data */
377 if (pcm_formats[format].signd == 1 || width <= 8) {
378 unsigned int bytes = samples * width / 8;
379 memset(data, *pat, bytes);
380 return 0;
381 }
382 /* non-zero samples, fill using a loop */
383 width /= 8;
384 dst = data;
385#if 0
386 while (samples--) {
387 memcpy(dst, pat, width);
388 dst += width;
389 }
390#else
391 /* a bit optimization for constant width */
392 switch (width) {
393 case 2:
394 while (samples--) {
395 memcpy(dst, pat, 2);
396 dst += 2;
397 }
398 break;
399 case 3:
400 while (samples--) {
401 memcpy(dst, pat, 3);
402 dst += 3;
403 }
404 break;
405 case 4:
406 while (samples--) {
407 memcpy(dst, pat, 4);
408 dst += 4;
409 }
410 break;
411 case 8:
412 while (samples--) {
413 memcpy(dst, pat, 8);
414 dst += 8;
415 }
416 break;
417 }
418#endif
419 return 0;
420}
421
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200422EXPORT_SYMBOL(snd_pcm_format_set_silence);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/**
425 * snd_pcm_limit_hw_rates - determine rate_min/rate_max fields
426 * @runtime: the runtime instance
427 *
428 * Determines the rate_min and rate_max fields from the rates bits of
429 * the given runtime->hw.
430 *
431 * Returns zero if successful.
432 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100433int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int i;
Clemens Ladisch7653d552007-08-13 17:38:54 +0200436 for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (runtime->hw.rates & (1 << i)) {
Clemens Ladisch7653d552007-08-13 17:38:54 +0200438 runtime->hw.rate_min = snd_pcm_known_rates.list[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
440 }
441 }
Clemens Ladisch7653d552007-08-13 17:38:54 +0200442 for (i = (int)snd_pcm_known_rates.count - 1; i >= 0; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (runtime->hw.rates & (1 << i)) {
Clemens Ladisch7653d552007-08-13 17:38:54 +0200444 runtime->hw.rate_max = snd_pcm_known_rates.list[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446 }
447 }
448 return 0;
449}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200450
451EXPORT_SYMBOL(snd_pcm_limit_hw_rates);
Clemens Ladisch918f3a02007-08-13 17:40:54 +0200452
453/**
454 * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
455 * @rate: the sample rate to convert
456 *
457 * Returns the SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
458 * SNDRV_PCM_RATE_KNOT for an unknown rate.
459 */
460unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
461{
462 unsigned int i;
463
464 for (i = 0; i < snd_pcm_known_rates.count; i++)
465 if (snd_pcm_known_rates.list[i] == rate)
466 return 1u << i;
467 return SNDRV_PCM_RATE_KNOT;
468}
469EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);