PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/sound/core/pcm_misc.c

https://gitlab.com/webhaikal/SenseiRN3
C | 518 lines | 360 code | 34 blank | 124 comment | 44 complexity | c7bd47b07064aac1edbb3f3ae5c7fea6 MD5 | raw file
  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. #define SND_PCM_FORMAT_UNKNOWN (-1)
  26. /* NOTE: "signed" prefix must be given below since the default char is
  27. * unsigned on some architectures!
  28. */
  29. struct pcm_format_data {
  30. unsigned char width; /* bit width */
  31. unsigned char phys; /* physical bit width */
  32. signed char le; /* 0 = big-endian, 1 = little-endian, -1 = others */
  33. signed char signd; /* 0 = unsigned, 1 = signed, -1 = others */
  34. unsigned char silence[8]; /* silence data to fill */
  35. };
  36. /* we do lots of calculations on snd_pcm_format_t; shut up sparse */
  37. #define INT __force int
  38. static struct pcm_format_data pcm_formats[(INT)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. [SNDRV_PCM_FORMAT_G723_24] = {
  132. .width = 3, .phys = 3, .le = -1, .signd = -1,
  133. .silence = {},
  134. },
  135. [SNDRV_PCM_FORMAT_G723_40] = {
  136. .width = 5, .phys = 5, .le = -1, .signd = -1,
  137. .silence = {},
  138. },
  139. [SNDRV_PCM_FORMAT_DSD_U8] = {
  140. .width = 8, .phys = 8, .le = 1, .signd = 0,
  141. .silence = {},
  142. },
  143. [SNDRV_PCM_FORMAT_DSD_U16_LE] = {
  144. .width = 16, .phys = 16, .le = 1, .signd = 0,
  145. .silence = {},
  146. },
  147. /* FIXME: the following three formats are not defined properly yet */
  148. [SNDRV_PCM_FORMAT_MPEG] = {
  149. .le = -1, .signd = -1,
  150. },
  151. [SNDRV_PCM_FORMAT_GSM] = {
  152. .le = -1, .signd = -1,
  153. },
  154. [SNDRV_PCM_FORMAT_SPECIAL] = {
  155. /* set the width and phys same as S16_LE */
  156. .width = 16, .phys = 16, .le = -1, .signd = -1,
  157. .silence = {},
  158. },
  159. [SNDRV_PCM_FORMAT_S24_3LE] = {
  160. .width = 24, .phys = 24, .le = 1, .signd = 1,
  161. .silence = {},
  162. },
  163. [SNDRV_PCM_FORMAT_S24_3BE] = {
  164. .width = 24, .phys = 24, .le = 0, .signd = 1,
  165. .silence = {},
  166. },
  167. [SNDRV_PCM_FORMAT_U24_3LE] = {
  168. .width = 24, .phys = 24, .le = 1, .signd = 0,
  169. .silence = { 0x00, 0x00, 0x80 },
  170. },
  171. [SNDRV_PCM_FORMAT_U24_3BE] = {
  172. .width = 24, .phys = 24, .le = 0, .signd = 0,
  173. .silence = { 0x80, 0x00, 0x00 },
  174. },
  175. [SNDRV_PCM_FORMAT_S20_3LE] = {
  176. .width = 20, .phys = 24, .le = 1, .signd = 1,
  177. .silence = {},
  178. },
  179. [SNDRV_PCM_FORMAT_S20_3BE] = {
  180. .width = 20, .phys = 24, .le = 0, .signd = 1,
  181. .silence = {},
  182. },
  183. [SNDRV_PCM_FORMAT_U20_3LE] = {
  184. .width = 20, .phys = 24, .le = 1, .signd = 0,
  185. .silence = { 0x00, 0x00, 0x08 },
  186. },
  187. [SNDRV_PCM_FORMAT_U20_3BE] = {
  188. .width = 20, .phys = 24, .le = 0, .signd = 0,
  189. .silence = { 0x08, 0x00, 0x00 },
  190. },
  191. [SNDRV_PCM_FORMAT_S18_3LE] = {
  192. .width = 18, .phys = 24, .le = 1, .signd = 1,
  193. .silence = {},
  194. },
  195. [SNDRV_PCM_FORMAT_S18_3BE] = {
  196. .width = 18, .phys = 24, .le = 0, .signd = 1,
  197. .silence = {},
  198. },
  199. [SNDRV_PCM_FORMAT_U18_3LE] = {
  200. .width = 18, .phys = 24, .le = 1, .signd = 0,
  201. .silence = { 0x00, 0x00, 0x02 },
  202. },
  203. [SNDRV_PCM_FORMAT_U18_3BE] = {
  204. .width = 18, .phys = 24, .le = 0, .signd = 0,
  205. .silence = { 0x02, 0x00, 0x00 },
  206. },
  207. [SNDRV_PCM_FORMAT_G723_24_1B] = {
  208. .width = 3, .phys = 8, .le = -1, .signd = -1,
  209. .silence = {},
  210. },
  211. [SNDRV_PCM_FORMAT_G723_40_1B] = {
  212. .width = 5, .phys = 8, .le = -1, .signd = -1,
  213. .silence = {},
  214. },
  215. };
  216. /**
  217. * snd_pcm_format_signed - Check the PCM format is signed linear
  218. * @format: the format to check
  219. *
  220. * Return: 1 if the given PCM format is signed linear, 0 if unsigned
  221. * linear, and a negative error code for non-linear formats.
  222. */
  223. int snd_pcm_format_signed(snd_pcm_format_t format)
  224. {
  225. int val;
  226. if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
  227. return -EINVAL;
  228. if ((val = pcm_formats[(INT)format].signd) < 0)
  229. return -EINVAL;
  230. return val;
  231. }
  232. EXPORT_SYMBOL(snd_pcm_format_signed);
  233. /**
  234. * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
  235. * @format: the format to check
  236. *
  237. * Return: 1 if the given PCM format is unsigned linear, 0 if signed
  238. * linear, and a negative error code for non-linear formats.
  239. */
  240. int snd_pcm_format_unsigned(snd_pcm_format_t format)
  241. {
  242. int val;
  243. val = snd_pcm_format_signed(format);
  244. if (val < 0)
  245. return val;
  246. return !val;
  247. }
  248. EXPORT_SYMBOL(snd_pcm_format_unsigned);
  249. /**
  250. * snd_pcm_format_linear - Check the PCM format is linear
  251. * @format: the format to check
  252. *
  253. * Return: 1 if the given PCM format is linear, 0 if not.
  254. */
  255. int snd_pcm_format_linear(snd_pcm_format_t format)
  256. {
  257. return snd_pcm_format_signed(format) >= 0;
  258. }
  259. EXPORT_SYMBOL(snd_pcm_format_linear);
  260. /**
  261. * snd_pcm_format_little_endian - Check the PCM format is little-endian
  262. * @format: the format to check
  263. *
  264. * Return: 1 if the given PCM format is little-endian, 0 if
  265. * big-endian, or a negative error code if endian not specified.
  266. */
  267. int snd_pcm_format_little_endian(snd_pcm_format_t format)
  268. {
  269. int val;
  270. if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
  271. return -EINVAL;
  272. if ((val = pcm_formats[(INT)format].le) < 0)
  273. return -EINVAL;
  274. return val;
  275. }
  276. EXPORT_SYMBOL(snd_pcm_format_little_endian);
  277. /**
  278. * snd_pcm_format_big_endian - Check the PCM format is big-endian
  279. * @format: the format to check
  280. *
  281. * Return: 1 if the given PCM format is big-endian, 0 if
  282. * little-endian, or a negative error code if endian not specified.
  283. */
  284. int snd_pcm_format_big_endian(snd_pcm_format_t format)
  285. {
  286. int val;
  287. val = snd_pcm_format_little_endian(format);
  288. if (val < 0)
  289. return val;
  290. return !val;
  291. }
  292. EXPORT_SYMBOL(snd_pcm_format_big_endian);
  293. /**
  294. * snd_pcm_format_width - return the bit-width of the format
  295. * @format: the format to check
  296. *
  297. * Return: The bit-width of the format, or a negative error code
  298. * if unknown format.
  299. */
  300. int snd_pcm_format_width(snd_pcm_format_t format)
  301. {
  302. int val;
  303. if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
  304. return -EINVAL;
  305. if ((val = pcm_formats[(INT)format].width) == 0)
  306. return -EINVAL;
  307. return val;
  308. }
  309. EXPORT_SYMBOL(snd_pcm_format_width);
  310. /**
  311. * snd_pcm_format_physical_width - return the physical bit-width of the format
  312. * @format: the format to check
  313. *
  314. * Return: The physical bit-width of the format, or a negative error code
  315. * if unknown format.
  316. */
  317. int snd_pcm_format_physical_width(snd_pcm_format_t format)
  318. {
  319. int val;
  320. if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
  321. return -EINVAL;
  322. if ((val = pcm_formats[(INT)format].phys) == 0)
  323. return -EINVAL;
  324. return val;
  325. }
  326. EXPORT_SYMBOL(snd_pcm_format_physical_width);
  327. /**
  328. * snd_pcm_format_size - return the byte size of samples on the given format
  329. * @format: the format to check
  330. * @samples: sampling rate
  331. *
  332. * Return: The byte size of the given samples for the format, or a
  333. * negative error code if unknown format.
  334. */
  335. ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
  336. {
  337. int phys_width = snd_pcm_format_physical_width(format);
  338. if (phys_width < 0)
  339. return -EINVAL;
  340. return samples * phys_width / 8;
  341. }
  342. EXPORT_SYMBOL(snd_pcm_format_size);
  343. /**
  344. * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
  345. * @format: the format to check
  346. *
  347. * Return: The format pattern to fill or %NULL if error.
  348. */
  349. const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
  350. {
  351. if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
  352. return NULL;
  353. if (! pcm_formats[(INT)format].phys)
  354. return NULL;
  355. return pcm_formats[(INT)format].silence;
  356. }
  357. EXPORT_SYMBOL(snd_pcm_format_silence_64);
  358. /**
  359. * snd_pcm_format_set_silence - set the silence data on the buffer
  360. * @format: the PCM format
  361. * @data: the buffer pointer
  362. * @samples: the number of samples to set silence
  363. *
  364. * Sets the silence data on the buffer for the given samples.
  365. *
  366. * Return: Zero if successful, or a negative error code on failure.
  367. */
  368. int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
  369. {
  370. int width;
  371. unsigned char *dst, *pat;
  372. if ((INT)format < 0 || (INT)format > (INT)SNDRV_PCM_FORMAT_LAST)
  373. return -EINVAL;
  374. if (samples == 0)
  375. return 0;
  376. width = pcm_formats[(INT)format].phys; /* physical width */
  377. pat = pcm_formats[(INT)format].silence;
  378. if (! width)
  379. return -EINVAL;
  380. /* signed or 1 byte data */
  381. if (pcm_formats[(INT)format].signd == 1 || width <= 8) {
  382. unsigned int bytes = samples * width / 8;
  383. memset(data, *pat, bytes);
  384. return 0;
  385. }
  386. /* non-zero samples, fill using a loop */
  387. width /= 8;
  388. dst = data;
  389. #if 0
  390. while (samples--) {
  391. memcpy(dst, pat, width);
  392. dst += width;
  393. }
  394. #else
  395. /* a bit optimization for constant width */
  396. switch (width) {
  397. case 2:
  398. while (samples--) {
  399. memcpy(dst, pat, 2);
  400. dst += 2;
  401. }
  402. break;
  403. case 3:
  404. while (samples--) {
  405. memcpy(dst, pat, 3);
  406. dst += 3;
  407. }
  408. break;
  409. case 4:
  410. while (samples--) {
  411. memcpy(dst, pat, 4);
  412. dst += 4;
  413. }
  414. break;
  415. case 8:
  416. while (samples--) {
  417. memcpy(dst, pat, 8);
  418. dst += 8;
  419. }
  420. break;
  421. }
  422. #endif
  423. return 0;
  424. }
  425. EXPORT_SYMBOL(snd_pcm_format_set_silence);
  426. /**
  427. * snd_pcm_limit_hw_rates - determine rate_min/rate_max fields
  428. * @runtime: the runtime instance
  429. *
  430. * Determines the rate_min and rate_max fields from the rates bits of
  431. * the given runtime->hw.
  432. *
  433. * Return: Zero if successful.
  434. */
  435. int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
  436. {
  437. int i;
  438. for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
  439. if (runtime->hw.rates & (1 << i)) {
  440. runtime->hw.rate_min = snd_pcm_known_rates.list[i];
  441. break;
  442. }
  443. }
  444. for (i = (int)snd_pcm_known_rates.count - 1; i >= 0; i--) {
  445. if (runtime->hw.rates & (1 << i)) {
  446. runtime->hw.rate_max = snd_pcm_known_rates.list[i];
  447. break;
  448. }
  449. }
  450. return 0;
  451. }
  452. EXPORT_SYMBOL(snd_pcm_limit_hw_rates);
  453. /**
  454. * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
  455. * @rate: the sample rate to convert
  456. *
  457. * Return: The SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
  458. * SNDRV_PCM_RATE_KNOT for an unknown rate.
  459. */
  460. unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
  461. {
  462. unsigned int i;
  463. for (i = 0; i < snd_pcm_known_rates.count; i++)
  464. if (snd_pcm_known_rates.list[i] == rate)
  465. return 1u << i;
  466. return SNDRV_PCM_RATE_KNOT;
  467. }
  468. EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);
  469. /**
  470. * snd_pcm_rate_bit_to_rate - converts SNDRV_PCM_RATE_xxx bit to sample rate
  471. * @rate_bit: the rate bit to convert
  472. *
  473. * Return: The sample rate that corresponds to the given SNDRV_PCM_RATE_xxx flag
  474. * or 0 for an unknown rate bit.
  475. */
  476. unsigned int snd_pcm_rate_bit_to_rate(unsigned int rate_bit)
  477. {
  478. unsigned int i;
  479. for (i = 0; i < snd_pcm_known_rates.count; i++)
  480. if ((1u << i) == rate_bit)
  481. return snd_pcm_known_rates.list[i];
  482. return 0;
  483. }
  484. EXPORT_SYMBOL(snd_pcm_rate_bit_to_rate);