pcm_misc.c 14 KB

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