pcm_misc.c 15 KB

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