pcm_misc.c 15 KB

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