pcm_misc.c 13 KB

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