pcm_misc.c 13 KB

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