pcm_misc.c 16 KB

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