pcm_misc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * PCM Interface - misc routines
  3. * Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
  4. *
  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. #include <linux/time.h>
  22. #include <linux/export.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include "pcm_local.h"
  26. #define SND_PCM_FORMAT_UNKNOWN (-1)
  27. /* NOTE: "signed" prefix must be given below since the default char is
  28. * unsigned on some architectures!
  29. */
  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 */
  36. };
  37. /* we do lots of calculations on snd_pcm_format_t; shut up sparse */
  38. #define INT __force int
  39. static bool valid_format(snd_pcm_format_t format)
  40. {
  41. return (INT)format >= 0 && (INT)format <= (INT)SNDRV_PCM_FORMAT_LAST;
  42. }
  43. static const struct pcm_format_data pcm_formats[(INT)SNDRV_PCM_FORMAT_LAST+1] = {
  44. [SNDRV_PCM_FORMAT_S8] = {
  45. .width = 8, .phys = 8, .le = -1, .signd = 1,
  46. .silence = {},
  47. },
  48. [SNDRV_PCM_FORMAT_U8] = {
  49. .width = 8, .phys = 8, .le = -1, .signd = 0,
  50. .silence = { 0x80 },
  51. },
  52. [SNDRV_PCM_FORMAT_S16_LE] = {
  53. .width = 16, .phys = 16, .le = 1, .signd = 1,
  54. .silence = {},
  55. },
  56. [SNDRV_PCM_FORMAT_S16_BE] = {
  57. .width = 16, .phys = 16, .le = 0, .signd = 1,
  58. .silence = {},
  59. },
  60. [SNDRV_PCM_FORMAT_U16_LE] = {
  61. .width = 16, .phys = 16, .le = 1, .signd = 0,
  62. .silence = { 0x00, 0x80 },
  63. },
  64. [SNDRV_PCM_FORMAT_U16_BE] = {
  65. .width = 16, .phys = 16, .le = 0, .signd = 0,
  66. .silence = { 0x80, 0x00 },
  67. },
  68. [SNDRV_PCM_FORMAT_S24_LE] = {
  69. .width = 24, .phys = 32, .le = 1, .signd = 1,
  70. .silence = {},
  71. },
  72. [SNDRV_PCM_FORMAT_S24_BE] = {
  73. .width = 24, .phys = 32, .le = 0, .signd = 1,
  74. .silence = {},
  75. },
  76. [SNDRV_PCM_FORMAT_U24_LE] = {
  77. .width = 24, .phys = 32, .le = 1, .signd = 0,
  78. .silence = { 0x00, 0x00, 0x80 },
  79. },
  80. [SNDRV_PCM_FORMAT_U24_BE] = {
  81. .width = 24, .phys = 32, .le = 0, .signd = 0,
  82. .silence = { 0x00, 0x80, 0x00, 0x00 },
  83. },
  84. [SNDRV_PCM_FORMAT_S32_LE] = {
  85. .width = 32, .phys = 32, .le = 1, .signd = 1,
  86. .silence = {},
  87. },
  88. [SNDRV_PCM_FORMAT_S32_BE] = {
  89. .width = 32, .phys = 32, .le = 0, .signd = 1,
  90. .silence = {},
  91. },
  92. [SNDRV_PCM_FORMAT_U32_LE] = {
  93. .width = 32, .phys = 32, .le = 1, .signd = 0,
  94. .silence = { 0x00, 0x00, 0x00, 0x80 },
  95. },
  96. [SNDRV_PCM_FORMAT_U32_BE] = {
  97. .width = 32, .phys = 32, .le = 0, .signd = 0,
  98. .silence = { 0x80, 0x00, 0x00, 0x00 },
  99. },
  100. [SNDRV_PCM_FORMAT_FLOAT_LE] = {
  101. .width = 32, .phys = 32, .le = 1, .signd = -1,
  102. .silence = {},
  103. },
  104. [SNDRV_PCM_FORMAT_FLOAT_BE] = {
  105. .width = 32, .phys = 32, .le = 0, .signd = -1,
  106. .silence = {},
  107. },
  108. [SNDRV_PCM_FORMAT_FLOAT64_LE] = {
  109. .width = 64, .phys = 64, .le = 1, .signd = -1,
  110. .silence = {},
  111. },
  112. [SNDRV_PCM_FORMAT_FLOAT64_BE] = {
  113. .width = 64, .phys = 64, .le = 0, .signd = -1,
  114. .silence = {},
  115. },
  116. [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE] = {
  117. .width = 32, .phys = 32, .le = 1, .signd = -1,
  118. .silence = {},
  119. },
  120. [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE] = {
  121. .width = 32, .phys = 32, .le = 0, .signd = -1,
  122. .silence = {},
  123. },
  124. [SNDRV_PCM_FORMAT_MU_LAW] = {
  125. .width = 8, .phys = 8, .le = -1, .signd = -1,
  126. .silence = { 0x7f },
  127. },
  128. [SNDRV_PCM_FORMAT_A_LAW] = {
  129. .width = 8, .phys = 8, .le = -1, .signd = -1,
  130. .silence = { 0x55 },
  131. },
  132. [SNDRV_PCM_FORMAT_IMA_ADPCM] = {
  133. .width = 4, .phys = 4, .le = -1, .signd = -1,
  134. .silence = {},
  135. },
  136. [SNDRV_PCM_FORMAT_G723_24] = {
  137. .width = 3, .phys = 3, .le = -1, .signd = -1,
  138. .silence = {},
  139. },
  140. [SNDRV_PCM_FORMAT_G723_40] = {
  141. .width = 5, .phys = 5, .le = -1, .signd = -1,
  142. .silence = {},
  143. },
  144. [SNDRV_PCM_FORMAT_DSD_U8] = {
  145. .width = 8, .phys = 8, .le = 1, .signd = 0,
  146. .silence = { 0x69 },
  147. },
  148. [SNDRV_PCM_FORMAT_DSD_U16_LE] = {
  149. .width = 16, .phys = 16, .le = 1, .signd = 0,
  150. .silence = { 0x69, 0x69 },
  151. },
  152. [SNDRV_PCM_FORMAT_DSD_U32_LE] = {
  153. .width = 32, .phys = 32, .le = 1, .signd = 0,
  154. .silence = { 0x69, 0x69, 0x69, 0x69 },
  155. },
  156. [SNDRV_PCM_FORMAT_DSD_U16_BE] = {
  157. .width = 16, .phys = 16, .le = 0, .signd = 0,
  158. .silence = { 0x69, 0x69 },
  159. },
  160. [SNDRV_PCM_FORMAT_DSD_U32_BE] = {
  161. .width = 32, .phys = 32, .le = 0, .signd = 0,
  162. .silence = { 0x69, 0x69, 0x69, 0x69 },
  163. },
  164. /* FIXME: the following two formats are not defined properly yet */
  165. [SNDRV_PCM_FORMAT_MPEG] = {
  166. .le = -1, .signd = -1,
  167. },
  168. [SNDRV_PCM_FORMAT_GSM] = {
  169. .le = -1, .signd = -1,
  170. },
  171. [SNDRV_PCM_FORMAT_S20_LE] = {
  172. .width = 20, .phys = 32, .le = 1, .signd = 1,
  173. .silence = {},
  174. },
  175. [SNDRV_PCM_FORMAT_S20_BE] = {
  176. .width = 20, .phys = 32, .le = 0, .signd = 1,
  177. .silence = {},
  178. },
  179. [SNDRV_PCM_FORMAT_U20_LE] = {
  180. .width = 20, .phys = 32, .le = 1, .signd = 0,
  181. .silence = { 0x00, 0x00, 0x08, 0x00 },
  182. },
  183. [SNDRV_PCM_FORMAT_U20_BE] = {
  184. .width = 20, .phys = 32, .le = 0, .signd = 0,
  185. .silence = { 0x00, 0x08, 0x00, 0x00 },
  186. },
  187. /* FIXME: the following format is not defined properly yet */
  188. [SNDRV_PCM_FORMAT_SPECIAL] = {
  189. .le = -1, .signd = -1,
  190. },
  191. [SNDRV_PCM_FORMAT_S24_3LE] = {
  192. .width = 24, .phys = 24, .le = 1, .signd = 1,
  193. .silence = {},
  194. },
  195. [SNDRV_PCM_FORMAT_S24_3BE] = {
  196. .width = 24, .phys = 24, .le = 0, .signd = 1,
  197. .silence = {},
  198. },
  199. [SNDRV_PCM_FORMAT_U24_3LE] = {
  200. .width = 24, .phys = 24, .le = 1, .signd = 0,
  201. .silence = { 0x00, 0x00, 0x80 },
  202. },
  203. [SNDRV_PCM_FORMAT_U24_3BE] = {
  204. .width = 24, .phys = 24, .le = 0, .signd = 0,
  205. .silence = { 0x80, 0x00, 0x00 },
  206. },
  207. [SNDRV_PCM_FORMAT_S20_3LE] = {
  208. .width = 20, .phys = 24, .le = 1, .signd = 1,
  209. .silence = {},
  210. },
  211. [SNDRV_PCM_FORMAT_S20_3BE] = {
  212. .width = 20, .phys = 24, .le = 0, .signd = 1,
  213. .silence = {},
  214. },
  215. [SNDRV_PCM_FORMAT_U20_3LE] = {
  216. .width = 20, .phys = 24, .le = 1, .signd = 0,
  217. .silence = { 0x00, 0x00, 0x08 },
  218. },
  219. [SNDRV_PCM_FORMAT_U20_3BE] = {
  220. .width = 20, .phys = 24, .le = 0, .signd = 0,
  221. .silence = { 0x08, 0x00, 0x00 },
  222. },
  223. [SNDRV_PCM_FORMAT_S18_3LE] = {
  224. .width = 18, .phys = 24, .le = 1, .signd = 1,
  225. .silence = {},
  226. },
  227. [SNDRV_PCM_FORMAT_S18_3BE] = {
  228. .width = 18, .phys = 24, .le = 0, .signd = 1,
  229. .silence = {},
  230. },
  231. [SNDRV_PCM_FORMAT_U18_3LE] = {
  232. .width = 18, .phys = 24, .le = 1, .signd = 0,
  233. .silence = { 0x00, 0x00, 0x02 },
  234. },
  235. [SNDRV_PCM_FORMAT_U18_3BE] = {
  236. .width = 18, .phys = 24, .le = 0, .signd = 0,
  237. .silence = { 0x02, 0x00, 0x00 },
  238. },
  239. [SNDRV_PCM_FORMAT_G723_24_1B] = {
  240. .width = 3, .phys = 8, .le = -1, .signd = -1,
  241. .silence = {},
  242. },
  243. [SNDRV_PCM_FORMAT_G723_40_1B] = {
  244. .width = 5, .phys = 8, .le = -1, .signd = -1,
  245. .silence = {},
  246. },
  247. };
  248. /**
  249. * snd_pcm_format_signed - Check the PCM format is signed linear
  250. * @format: the format to check
  251. *
  252. * Return: 1 if the given PCM format is signed linear, 0 if unsigned
  253. * linear, and a negative error code for non-linear formats.
  254. */
  255. int snd_pcm_format_signed(snd_pcm_format_t format)
  256. {
  257. int val;
  258. if (!valid_format(format))
  259. return -EINVAL;
  260. if ((val = pcm_formats[(INT)format].signd) < 0)
  261. return -EINVAL;
  262. return val;
  263. }
  264. EXPORT_SYMBOL(snd_pcm_format_signed);
  265. /**
  266. * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
  267. * @format: the format to check
  268. *
  269. * Return: 1 if the given PCM format is unsigned linear, 0 if signed
  270. * linear, and a negative error code for non-linear formats.
  271. */
  272. int snd_pcm_format_unsigned(snd_pcm_format_t format)
  273. {
  274. int val;
  275. val = snd_pcm_format_signed(format);
  276. if (val < 0)
  277. return val;
  278. return !val;
  279. }
  280. EXPORT_SYMBOL(snd_pcm_format_unsigned);
  281. /**
  282. * snd_pcm_format_linear - Check the PCM format is linear
  283. * @format: the format to check
  284. *
  285. * Return: 1 if the given PCM format is linear, 0 if not.
  286. */
  287. int snd_pcm_format_linear(snd_pcm_format_t format)
  288. {
  289. return snd_pcm_format_signed(format) >= 0;
  290. }
  291. EXPORT_SYMBOL(snd_pcm_format_linear);
  292. /**
  293. * snd_pcm_format_little_endian - Check the PCM format is little-endian
  294. * @format: the format to check
  295. *
  296. * Return: 1 if the given PCM format is little-endian, 0 if
  297. * big-endian, or a negative error code if endian not specified.
  298. */
  299. int snd_pcm_format_little_endian(snd_pcm_format_t format)
  300. {
  301. int val;
  302. if (!valid_format(format))
  303. return -EINVAL;
  304. if ((val = pcm_formats[(INT)format].le) < 0)
  305. return -EINVAL;
  306. return val;
  307. }
  308. EXPORT_SYMBOL(snd_pcm_format_little_endian);
  309. /**
  310. * snd_pcm_format_big_endian - Check the PCM format is big-endian
  311. * @format: the format to check
  312. *
  313. * Return: 1 if the given PCM format is big-endian, 0 if
  314. * little-endian, or a negative error code if endian not specified.
  315. */
  316. int snd_pcm_format_big_endian(snd_pcm_format_t format)
  317. {
  318. int val;
  319. val = snd_pcm_format_little_endian(format);
  320. if (val < 0)
  321. return val;
  322. return !val;
  323. }
  324. EXPORT_SYMBOL(snd_pcm_format_big_endian);
  325. /**
  326. * snd_pcm_format_width - return the bit-width of the format
  327. * @format: the format to check
  328. *
  329. * Return: The bit-width of the format, or a negative error code
  330. * if unknown format.
  331. */
  332. int snd_pcm_format_width(snd_pcm_format_t format)
  333. {
  334. int val;
  335. if (!valid_format(format))
  336. return -EINVAL;
  337. if ((val = pcm_formats[(INT)format].width) == 0)
  338. return -EINVAL;
  339. return val;
  340. }
  341. EXPORT_SYMBOL(snd_pcm_format_width);
  342. /**
  343. * snd_pcm_format_physical_width - return the physical bit-width of the format
  344. * @format: the format to check
  345. *
  346. * Return: The physical bit-width of the format, or a negative error code
  347. * if unknown format.
  348. */
  349. int snd_pcm_format_physical_width(snd_pcm_format_t format)
  350. {
  351. int val;
  352. if (!valid_format(format))
  353. return -EINVAL;
  354. if ((val = pcm_formats[(INT)format].phys) == 0)
  355. return -EINVAL;
  356. return val;
  357. }
  358. EXPORT_SYMBOL(snd_pcm_format_physical_width);
  359. /**
  360. * snd_pcm_format_size - return the byte size of samples on the given format
  361. * @format: the format to check
  362. * @samples: sampling rate
  363. *
  364. * Return: The byte size of the given samples for the format, or a
  365. * negative error code if unknown format.
  366. */
  367. ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
  368. {
  369. int phys_width = snd_pcm_format_physical_width(format);
  370. if (phys_width < 0)
  371. return -EINVAL;
  372. return samples * phys_width / 8;
  373. }
  374. EXPORT_SYMBOL(snd_pcm_format_size);
  375. /**
  376. * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
  377. * @format: the format to check
  378. *
  379. * Return: The format pattern to fill or %NULL if error.
  380. */
  381. const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
  382. {
  383. if (!valid_format(format))
  384. return NULL;
  385. if (! pcm_formats[(INT)format].phys)
  386. return NULL;
  387. return pcm_formats[(INT)format].silence;
  388. }
  389. EXPORT_SYMBOL(snd_pcm_format_silence_64);
  390. /**
  391. * snd_pcm_format_set_silence - set the silence data on the buffer
  392. * @format: the PCM format
  393. * @data: the buffer pointer
  394. * @samples: the number of samples to set silence
  395. *
  396. * Sets the silence data on the buffer for the given samples.
  397. *
  398. * Return: Zero if successful, or a negative error code on failure.
  399. */
  400. int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
  401. {
  402. int width;
  403. unsigned char *dst;
  404. const unsigned char *pat;
  405. if (!valid_format(format))
  406. return -EINVAL;
  407. if (samples == 0)
  408. return 0;
  409. width = pcm_formats[(INT)format].phys; /* physical width */
  410. pat = pcm_formats[(INT)format].silence;
  411. if (!width || !pat)
  412. return -EINVAL;
  413. /* signed or 1 byte data */
  414. if (pcm_formats[(INT)format].signd == 1 || width <= 8) {
  415. unsigned int bytes = samples * width / 8;
  416. memset(data, *pat, bytes);
  417. return 0;
  418. }
  419. /* non-zero samples, fill using a loop */
  420. width /= 8;
  421. dst = data;
  422. #if 0
  423. while (samples--) {
  424. memcpy(dst, pat, width);
  425. dst += width;
  426. }
  427. #else
  428. /* a bit optimization for constant width */
  429. switch (width) {
  430. case 2:
  431. while (samples--) {
  432. memcpy(dst, pat, 2);
  433. dst += 2;
  434. }
  435. break;
  436. case 3:
  437. while (samples--) {
  438. memcpy(dst, pat, 3);
  439. dst += 3;
  440. }
  441. break;
  442. case 4:
  443. while (samples--) {
  444. memcpy(dst, pat, 4);
  445. dst += 4;
  446. }
  447. break;
  448. case 8:
  449. while (samples--) {
  450. memcpy(dst, pat, 8);
  451. dst += 8;
  452. }
  453. break;
  454. }
  455. #endif
  456. return 0;
  457. }
  458. EXPORT_SYMBOL(snd_pcm_format_set_silence);
  459. /**
  460. * snd_pcm_hw_limit_rates - determine rate_min/rate_max fields
  461. * @hw: the pcm hw instance
  462. *
  463. * Determines the rate_min and rate_max fields from the rates bits of
  464. * the given hw.
  465. *
  466. * Return: Zero if successful.
  467. */
  468. int snd_pcm_hw_limit_rates(struct snd_pcm_hardware *hw)
  469. {
  470. int i;
  471. for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
  472. if (hw->rates & (1 << i)) {
  473. hw->rate_min = snd_pcm_known_rates.list[i];
  474. break;
  475. }
  476. }
  477. for (i = (int)snd_pcm_known_rates.count - 1; i >= 0; i--) {
  478. if (hw->rates & (1 << i)) {
  479. hw->rate_max = snd_pcm_known_rates.list[i];
  480. break;
  481. }
  482. }
  483. return 0;
  484. }
  485. EXPORT_SYMBOL(snd_pcm_hw_limit_rates);
  486. /**
  487. * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
  488. * @rate: the sample rate to convert
  489. *
  490. * Return: The SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
  491. * SNDRV_PCM_RATE_KNOT for an unknown rate.
  492. */
  493. unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
  494. {
  495. unsigned int i;
  496. for (i = 0; i < snd_pcm_known_rates.count; i++)
  497. if (snd_pcm_known_rates.list[i] == rate)
  498. return 1u << i;
  499. return SNDRV_PCM_RATE_KNOT;
  500. }
  501. EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);
  502. /**
  503. * snd_pcm_rate_bit_to_rate - converts SNDRV_PCM_RATE_xxx bit to sample rate
  504. * @rate_bit: the rate bit to convert
  505. *
  506. * Return: The sample rate that corresponds to the given SNDRV_PCM_RATE_xxx flag
  507. * or 0 for an unknown rate bit.
  508. */
  509. unsigned int snd_pcm_rate_bit_to_rate(unsigned int rate_bit)
  510. {
  511. unsigned int i;
  512. for (i = 0; i < snd_pcm_known_rates.count; i++)
  513. if ((1u << i) == rate_bit)
  514. return snd_pcm_known_rates.list[i];
  515. return 0;
  516. }
  517. EXPORT_SYMBOL(snd_pcm_rate_bit_to_rate);
  518. static unsigned int snd_pcm_rate_mask_sanitize(unsigned int rates)
  519. {
  520. if (rates & SNDRV_PCM_RATE_CONTINUOUS)
  521. return SNDRV_PCM_RATE_CONTINUOUS;
  522. else if (rates & SNDRV_PCM_RATE_KNOT)
  523. return SNDRV_PCM_RATE_KNOT;
  524. return rates;
  525. }
  526. /**
  527. * snd_pcm_rate_mask_intersect - computes the intersection between two rate masks
  528. * @rates_a: The first rate mask
  529. * @rates_b: The second rate mask
  530. *
  531. * This function computes the rates that are supported by both rate masks passed
  532. * to the function. It will take care of the special handling of
  533. * SNDRV_PCM_RATE_CONTINUOUS and SNDRV_PCM_RATE_KNOT.
  534. *
  535. * Return: A rate mask containing the rates that are supported by both rates_a
  536. * and rates_b.
  537. */
  538. unsigned int snd_pcm_rate_mask_intersect(unsigned int rates_a,
  539. unsigned int rates_b)
  540. {
  541. rates_a = snd_pcm_rate_mask_sanitize(rates_a);
  542. rates_b = snd_pcm_rate_mask_sanitize(rates_b);
  543. if (rates_a & SNDRV_PCM_RATE_CONTINUOUS)
  544. return rates_b;
  545. else if (rates_b & SNDRV_PCM_RATE_CONTINUOUS)
  546. return rates_a;
  547. else if (rates_a & SNDRV_PCM_RATE_KNOT)
  548. return rates_b;
  549. else if (rates_b & SNDRV_PCM_RATE_KNOT)
  550. return rates_a;
  551. return rates_a & rates_b;
  552. }
  553. EXPORT_SYMBOL_GPL(snd_pcm_rate_mask_intersect);
  554. /**
  555. * snd_pcm_rate_range_to_bits - converts rate range to SNDRV_PCM_RATE_xxx bit
  556. * @rate_min: the minimum sample rate
  557. * @rate_max: the maximum sample rate
  558. *
  559. * This function has an implicit assumption: the rates in the given range have
  560. * only the pre-defined rates like 44100 or 16000.
  561. *
  562. * Return: The SNDRV_PCM_RATE_xxx flag that corresponds to the given rate range,
  563. * or SNDRV_PCM_RATE_KNOT for an unknown range.
  564. */
  565. unsigned int snd_pcm_rate_range_to_bits(unsigned int rate_min,
  566. unsigned int rate_max)
  567. {
  568. unsigned int rates = 0;
  569. int i;
  570. for (i = 0; i < snd_pcm_known_rates.count; i++) {
  571. if (snd_pcm_known_rates.list[i] >= rate_min
  572. && snd_pcm_known_rates.list[i] <= rate_max)
  573. rates |= 1 << i;
  574. }
  575. if (!rates)
  576. rates = SNDRV_PCM_RATE_KNOT;
  577. return rates;
  578. }
  579. EXPORT_SYMBOL_GPL(snd_pcm_rate_range_to_bits);