pcm_misc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. val = pcm_formats[(INT)format].signd;
  261. if (val < 0)
  262. return -EINVAL;
  263. return val;
  264. }
  265. EXPORT_SYMBOL(snd_pcm_format_signed);
  266. /**
  267. * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
  268. * @format: the format to check
  269. *
  270. * Return: 1 if the given PCM format is unsigned linear, 0 if signed
  271. * linear, and a negative error code for non-linear formats.
  272. */
  273. int snd_pcm_format_unsigned(snd_pcm_format_t format)
  274. {
  275. int val;
  276. val = snd_pcm_format_signed(format);
  277. if (val < 0)
  278. return val;
  279. return !val;
  280. }
  281. EXPORT_SYMBOL(snd_pcm_format_unsigned);
  282. /**
  283. * snd_pcm_format_linear - Check the PCM format is linear
  284. * @format: the format to check
  285. *
  286. * Return: 1 if the given PCM format is linear, 0 if not.
  287. */
  288. int snd_pcm_format_linear(snd_pcm_format_t format)
  289. {
  290. return snd_pcm_format_signed(format) >= 0;
  291. }
  292. EXPORT_SYMBOL(snd_pcm_format_linear);
  293. /**
  294. * snd_pcm_format_little_endian - Check the PCM format is little-endian
  295. * @format: the format to check
  296. *
  297. * Return: 1 if the given PCM format is little-endian, 0 if
  298. * big-endian, or a negative error code if endian not specified.
  299. */
  300. int snd_pcm_format_little_endian(snd_pcm_format_t format)
  301. {
  302. int val;
  303. if (!valid_format(format))
  304. return -EINVAL;
  305. val = pcm_formats[(INT)format].le;
  306. if (val < 0)
  307. return -EINVAL;
  308. return val;
  309. }
  310. EXPORT_SYMBOL(snd_pcm_format_little_endian);
  311. /**
  312. * snd_pcm_format_big_endian - Check the PCM format is big-endian
  313. * @format: the format to check
  314. *
  315. * Return: 1 if the given PCM format is big-endian, 0 if
  316. * little-endian, or a negative error code if endian not specified.
  317. */
  318. int snd_pcm_format_big_endian(snd_pcm_format_t format)
  319. {
  320. int val;
  321. val = snd_pcm_format_little_endian(format);
  322. if (val < 0)
  323. return val;
  324. return !val;
  325. }
  326. EXPORT_SYMBOL(snd_pcm_format_big_endian);
  327. /**
  328. * snd_pcm_format_width - return the bit-width of the format
  329. * @format: the format to check
  330. *
  331. * Return: The bit-width of the format, or a negative error code
  332. * if unknown format.
  333. */
  334. int snd_pcm_format_width(snd_pcm_format_t format)
  335. {
  336. int val;
  337. if (!valid_format(format))
  338. return -EINVAL;
  339. val = pcm_formats[(INT)format].width;
  340. if (!val)
  341. return -EINVAL;
  342. return val;
  343. }
  344. EXPORT_SYMBOL(snd_pcm_format_width);
  345. /**
  346. * snd_pcm_format_physical_width - return the physical bit-width of the format
  347. * @format: the format to check
  348. *
  349. * Return: The physical bit-width of the format, or a negative error code
  350. * if unknown format.
  351. */
  352. int snd_pcm_format_physical_width(snd_pcm_format_t format)
  353. {
  354. int val;
  355. if (!valid_format(format))
  356. return -EINVAL;
  357. val = pcm_formats[(INT)format].phys;
  358. if (!val)
  359. return -EINVAL;
  360. return val;
  361. }
  362. EXPORT_SYMBOL(snd_pcm_format_physical_width);
  363. /**
  364. * snd_pcm_format_size - return the byte size of samples on the given format
  365. * @format: the format to check
  366. * @samples: sampling rate
  367. *
  368. * Return: The byte size of the given samples for the format, or a
  369. * negative error code if unknown format.
  370. */
  371. ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
  372. {
  373. int phys_width = snd_pcm_format_physical_width(format);
  374. if (phys_width < 0)
  375. return -EINVAL;
  376. return samples * phys_width / 8;
  377. }
  378. EXPORT_SYMBOL(snd_pcm_format_size);
  379. /**
  380. * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
  381. * @format: the format to check
  382. *
  383. * Return: The format pattern to fill or %NULL if error.
  384. */
  385. const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
  386. {
  387. if (!valid_format(format))
  388. return NULL;
  389. if (! pcm_formats[(INT)format].phys)
  390. return NULL;
  391. return pcm_formats[(INT)format].silence;
  392. }
  393. EXPORT_SYMBOL(snd_pcm_format_silence_64);
  394. /**
  395. * snd_pcm_format_set_silence - set the silence data on the buffer
  396. * @format: the PCM format
  397. * @data: the buffer pointer
  398. * @samples: the number of samples to set silence
  399. *
  400. * Sets the silence data on the buffer for the given samples.
  401. *
  402. * Return: Zero if successful, or a negative error code on failure.
  403. */
  404. int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
  405. {
  406. int width;
  407. unsigned char *dst;
  408. const unsigned char *pat;
  409. if (!valid_format(format))
  410. return -EINVAL;
  411. if (samples == 0)
  412. return 0;
  413. width = pcm_formats[(INT)format].phys; /* physical width */
  414. pat = pcm_formats[(INT)format].silence;
  415. if (!width || !pat)
  416. return -EINVAL;
  417. /* signed or 1 byte data */
  418. if (pcm_formats[(INT)format].signd == 1 || width <= 8) {
  419. unsigned int bytes = samples * width / 8;
  420. memset(data, *pat, bytes);
  421. return 0;
  422. }
  423. /* non-zero samples, fill using a loop */
  424. width /= 8;
  425. dst = data;
  426. #if 0
  427. while (samples--) {
  428. memcpy(dst, pat, width);
  429. dst += width;
  430. }
  431. #else
  432. /* a bit optimization for constant width */
  433. switch (width) {
  434. case 2:
  435. while (samples--) {
  436. memcpy(dst, pat, 2);
  437. dst += 2;
  438. }
  439. break;
  440. case 3:
  441. while (samples--) {
  442. memcpy(dst, pat, 3);
  443. dst += 3;
  444. }
  445. break;
  446. case 4:
  447. while (samples--) {
  448. memcpy(dst, pat, 4);
  449. dst += 4;
  450. }
  451. break;
  452. case 8:
  453. while (samples--) {
  454. memcpy(dst, pat, 8);
  455. dst += 8;
  456. }
  457. break;
  458. }
  459. #endif
  460. return 0;
  461. }
  462. EXPORT_SYMBOL(snd_pcm_format_set_silence);
  463. /**
  464. * snd_pcm_hw_limit_rates - determine rate_min/rate_max fields
  465. * @hw: the pcm hw instance
  466. *
  467. * Determines the rate_min and rate_max fields from the rates bits of
  468. * the given hw.
  469. *
  470. * Return: Zero if successful.
  471. */
  472. int snd_pcm_hw_limit_rates(struct snd_pcm_hardware *hw)
  473. {
  474. int i;
  475. unsigned int rmin, rmax;
  476. rmin = UINT_MAX;
  477. rmax = 0;
  478. for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
  479. if (hw->rates & (1 << i)) {
  480. rmin = min(rmin, snd_pcm_known_rates.list[i]);
  481. rmax = max(rmax, snd_pcm_known_rates.list[i]);
  482. }
  483. }
  484. if (rmin > rmax)
  485. return -EINVAL;
  486. hw->rate_min = rmin;
  487. hw->rate_max = rmax;
  488. return 0;
  489. }
  490. EXPORT_SYMBOL(snd_pcm_hw_limit_rates);
  491. /**
  492. * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
  493. * @rate: the sample rate to convert
  494. *
  495. * Return: The SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
  496. * SNDRV_PCM_RATE_KNOT for an unknown rate.
  497. */
  498. unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
  499. {
  500. unsigned int i;
  501. for (i = 0; i < snd_pcm_known_rates.count; i++)
  502. if (snd_pcm_known_rates.list[i] == rate)
  503. return 1u << i;
  504. return SNDRV_PCM_RATE_KNOT;
  505. }
  506. EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);
  507. /**
  508. * snd_pcm_rate_bit_to_rate - converts SNDRV_PCM_RATE_xxx bit to sample rate
  509. * @rate_bit: the rate bit to convert
  510. *
  511. * Return: The sample rate that corresponds to the given SNDRV_PCM_RATE_xxx flag
  512. * or 0 for an unknown rate bit.
  513. */
  514. unsigned int snd_pcm_rate_bit_to_rate(unsigned int rate_bit)
  515. {
  516. unsigned int i;
  517. for (i = 0; i < snd_pcm_known_rates.count; i++)
  518. if ((1u << i) == rate_bit)
  519. return snd_pcm_known_rates.list[i];
  520. return 0;
  521. }
  522. EXPORT_SYMBOL(snd_pcm_rate_bit_to_rate);
  523. static unsigned int snd_pcm_rate_mask_sanitize(unsigned int rates)
  524. {
  525. if (rates & SNDRV_PCM_RATE_CONTINUOUS)
  526. return SNDRV_PCM_RATE_CONTINUOUS;
  527. else if (rates & SNDRV_PCM_RATE_KNOT)
  528. return SNDRV_PCM_RATE_KNOT;
  529. return rates;
  530. }
  531. /**
  532. * snd_pcm_rate_mask_intersect - computes the intersection between two rate masks
  533. * @rates_a: The first rate mask
  534. * @rates_b: The second rate mask
  535. *
  536. * This function computes the rates that are supported by both rate masks passed
  537. * to the function. It will take care of the special handling of
  538. * SNDRV_PCM_RATE_CONTINUOUS and SNDRV_PCM_RATE_KNOT.
  539. *
  540. * Return: A rate mask containing the rates that are supported by both rates_a
  541. * and rates_b.
  542. */
  543. unsigned int snd_pcm_rate_mask_intersect(unsigned int rates_a,
  544. unsigned int rates_b)
  545. {
  546. rates_a = snd_pcm_rate_mask_sanitize(rates_a);
  547. rates_b = snd_pcm_rate_mask_sanitize(rates_b);
  548. if (rates_a & SNDRV_PCM_RATE_CONTINUOUS)
  549. return rates_b;
  550. else if (rates_b & SNDRV_PCM_RATE_CONTINUOUS)
  551. return rates_a;
  552. else if (rates_a & SNDRV_PCM_RATE_KNOT)
  553. return rates_b;
  554. else if (rates_b & SNDRV_PCM_RATE_KNOT)
  555. return rates_a;
  556. return rates_a & rates_b;
  557. }
  558. EXPORT_SYMBOL_GPL(snd_pcm_rate_mask_intersect);
  559. /**
  560. * snd_pcm_rate_range_to_bits - converts rate range to SNDRV_PCM_RATE_xxx bit
  561. * @rate_min: the minimum sample rate
  562. * @rate_max: the maximum sample rate
  563. *
  564. * This function has an implicit assumption: the rates in the given range have
  565. * only the pre-defined rates like 44100 or 16000.
  566. *
  567. * Return: The SNDRV_PCM_RATE_xxx flag that corresponds to the given rate range,
  568. * or SNDRV_PCM_RATE_KNOT for an unknown range.
  569. */
  570. unsigned int snd_pcm_rate_range_to_bits(unsigned int rate_min,
  571. unsigned int rate_max)
  572. {
  573. unsigned int rates = 0;
  574. int i;
  575. for (i = 0; i < snd_pcm_known_rates.count; i++) {
  576. if (snd_pcm_known_rates.list[i] >= rate_min
  577. && snd_pcm_known_rates.list[i] <= rate_max)
  578. rates |= 1 << i;
  579. }
  580. if (!rates)
  581. rates = SNDRV_PCM_RATE_KNOT;
  582. return rates;
  583. }
  584. EXPORT_SYMBOL_GPL(snd_pcm_rate_range_to_bits);