pcm_misc.c 13 KB

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