1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/time.h>
23#include <sound/core.h>
24#include <sound/pcm.h>
25#define SND_PCM_FORMAT_UNKNOWN (-1)
26
27
28
29
30struct pcm_format_data {
31 unsigned char width;
32 unsigned char phys;
33 signed char le;
34 signed char signd;
35 unsigned char silence[8];
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,
77 .silence = { 0x00, 0x80, 0x00, 0x00 },
78 },
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
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
194
195
196
197
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
209EXPORT_SYMBOL(snd_pcm_format_signed);
210
211
212
213
214
215
216
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
228EXPORT_SYMBOL(snd_pcm_format_unsigned);
229
230
231
232
233
234
235
236int snd_pcm_format_linear(snd_pcm_format_t format)
237{
238 return snd_pcm_format_signed(format) >= 0;
239}
240
241EXPORT_SYMBOL(snd_pcm_format_linear);
242
243
244
245
246
247
248
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
260EXPORT_SYMBOL(snd_pcm_format_little_endian);
261
262
263
264
265
266
267
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
279EXPORT_SYMBOL(snd_pcm_format_big_endian);
280
281
282
283
284
285
286
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
298EXPORT_SYMBOL(snd_pcm_format_width);
299
300
301
302
303
304
305
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
317EXPORT_SYMBOL(snd_pcm_format_physical_width);
318
319
320
321
322
323
324
325
326
327ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
328{
329 int phys_width = snd_pcm_format_physical_width(format);
330 if (phys_width < 0)
331 return -EINVAL;
332 return samples * phys_width / 8;
333}
334
335EXPORT_SYMBOL(snd_pcm_format_size);
336
337
338
339
340
341
342
343const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
344{
345 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
346 return NULL;
347 if (! pcm_formats[format].phys)
348 return NULL;
349 return pcm_formats[format].silence;
350}
351
352EXPORT_SYMBOL(snd_pcm_format_silence_64);
353
354
355
356
357
358
359
360
361
362
363
364int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
365{
366 int width;
367 unsigned char *dst, *pat;
368
369 if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
370 return -EINVAL;
371 if (samples == 0)
372 return 0;
373 width = pcm_formats[format].phys;
374 pat = pcm_formats[format].silence;
375 if (! width)
376 return -EINVAL;
377
378 if (pcm_formats[format].signd == 1 || width <= 8) {
379 unsigned int bytes = samples * width / 8;
380 memset(data, *pat, bytes);
381 return 0;
382 }
383
384 width /= 8;
385 dst = data;
386#if 0
387 while (samples--) {
388 memcpy(dst, pat, width);
389 dst += width;
390 }
391#else
392
393 switch (width) {
394 case 2:
395 while (samples--) {
396 memcpy(dst, pat, 2);
397 dst += 2;
398 }
399 break;
400 case 3:
401 while (samples--) {
402 memcpy(dst, pat, 3);
403 dst += 3;
404 }
405 break;
406 case 4:
407 while (samples--) {
408 memcpy(dst, pat, 4);
409 dst += 4;
410 }
411 break;
412 case 8:
413 while (samples--) {
414 memcpy(dst, pat, 8);
415 dst += 8;
416 }
417 break;
418 }
419#endif
420 return 0;
421}
422
423EXPORT_SYMBOL(snd_pcm_format_set_silence);
424
425
426
427
428
429
430
431
432
433
434int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
435{
436 int i;
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];
440 break;
441 }
442 }
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];
446 break;
447 }
448 }
449 return 0;
450}
451
452EXPORT_SYMBOL(snd_pcm_limit_hw_rates);
453
454
455
456
457
458
459
460
461unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
462{
463 unsigned int i;
464
465 for (i = 0; i < snd_pcm_known_rates.count; i++)
466 if (snd_pcm_known_rates.list[i] == rate)
467 return 1u << i;
468 return SNDRV_PCM_RATE_KNOT;
469}
470EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);
471