pcm_misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 <sound/core.h>
  23. #include <sound/pcm.h>
  24. #define SND_PCM_FORMAT_UNKNOWN (-1)
  25. /* NOTE: "signed" prefix must be given below since the default char is
  26. * unsigned on some architectures!
  27. */
  28. struct pcm_format_data {
  29. unsigned char width; /* bit width */
  30. unsigned char phys; /* physical bit width */
  31. signed char le; /* 0 = big-endian, 1 = little-endian, -1 = others */
  32. signed char signd; /* 0 = unsigned, 1 = signed, -1 = others */
  33. unsigned char silence[8]; /* silence data to fill */
  34. };
  35. static struct pcm_format_data pcm_formats[SNDRV_PCM_FORMAT_LAST+1] = {
  36. [SNDRV_PCM_FORMAT_S8] = {
  37. .width = 8, .phys = 8, .le = -1, .signd = 1,
  38. .silence = {},
  39. },
  40. [SNDRV_PCM_FORMAT_U8] = {
  41. .width = 8, .phys = 8, .le = -1, .signd = 0,
  42. .silence = { 0x80 },
  43. },
  44. [SNDRV_PCM_FORMAT_S16_LE] = {
  45. .width = 16, .phys = 16, .le = 1, .signd = 1,
  46. .silence = {},
  47. },
  48. [SNDRV_PCM_FORMAT_S16_BE] = {
  49. .width = 16, .phys = 16, .le = 0, .signd = 1,
  50. .silence = {},
  51. },
  52. [SNDRV_PCM_FORMAT_U16_LE] = {
  53. .width = 16, .phys = 16, .le = 1, .signd = 0,
  54. .silence = { 0x00, 0x80 },
  55. },
  56. [SNDRV_PCM_FORMAT_U16_BE] = {
  57. .width = 16, .phys = 16, .le = 0, .signd = 0,
  58. .silence = { 0x80, 0x00 },
  59. },
  60. [SNDRV_PCM_FORMAT_S24_LE] = {
  61. .width = 24, .phys = 32, .le = 1, .signd = 1,
  62. .silence = {},
  63. },
  64. [SNDRV_PCM_FORMAT_S24_BE] = {
  65. .width = 24, .phys = 32, .le = 0, .signd = 1,
  66. .silence = {},
  67. },
  68. [SNDRV_PCM_FORMAT_U24_LE] = {
  69. .width = 24, .phys = 32, .le = 1, .signd = 0,
  70. .silence = { 0x00, 0x00, 0x80 },
  71. },
  72. [SNDRV_PCM_FORMAT_U24_BE] = {
  73. .width = 24, .phys = 32, .le = 0, .signd = 0,
  74. .silence = { 0x00, 0x80, 0x00, 0x00 },
  75. },
  76. [SNDRV_PCM_FORMAT_S32_LE] = {
  77. .width = 32, .phys = 32, .le = 1, .signd = 1,
  78. .silence = {},
  79. },
  80. [SNDRV_PCM_FORMAT_S32_BE] = {
  81. .width = 32, .phys = 32, .le = 0, .signd = 1,
  82. .silence = {},
  83. },
  84. [SNDRV_PCM_FORMAT_U32_LE] = {
  85. .width = 32, .phys = 32, .le = 1, .signd = 0,
  86. .silence = { 0x00, 0x00, 0x00, 0x80 },
  87. },
  88. [SNDRV_PCM_FORMAT_U32_BE] = {
  89. .width = 32, .phys = 32, .le = 0, .signd = 0,
  90. .silence = { 0x80, 0x00, 0x00, 0x00 },
  91. },
  92. [SNDRV_PCM_FORMAT_FLOAT_LE] = {
  93. .width = 32, .phys = 32, .le = 1, .signd = -1,
  94. .silence = {},
  95. },
  96. [SNDRV_PCM_FORMAT_FLOAT_BE] = {
  97. .width = 32, .phys = 32, .le = 0, .signd = -1,
  98. .silence = {},
  99. },
  100. [SNDRV_PCM_FORMAT_FLOAT64_LE] = {
  101. .width = 64, .phys = 64, .le = 1, .signd = -1,
  102. .silence = {},
  103. },
  104. [SNDRV_PCM_FORMAT_FLOAT64_BE] = {
  105. .width = 64, .phys = 64, .le = 0, .signd = -1,
  106. .silence = {},
  107. },
  108. [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE] = {
  109. .width = 32, .phys = 32, .le = 1, .signd = -1,
  110. .silence = {},
  111. },
  112. [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE] = {
  113. .width = 32, .phys = 32, .le = 0, .signd = -1,
  114. .silence = {},
  115. },
  116. [SNDRV_PCM_FORMAT_MU_LAW] = {
  117. .width = 8, .phys = 8, .le = -1, .signd = -1,
  118. .silence = { 0x7f },
  119. },
  120. [SNDRV_PCM_FORMAT_A_LAW] = {
  121. .width = 8, .phys = 8, .le = -1, .signd = -1,
  122. .silence = { 0x55 },
  123. },
  124. [SNDRV_PCM_FORMAT_IMA_ADPCM] = {
  125. .width = 4, .phys = 4, .le = -1, .signd = -1,
  126. .silence = {},
  127. },
  128. [SNDRV_PCM_FORMAT_G723_24] = {
  129. .width = 3, .phys = 3, .le = -1, .signd = -1,
  130. .silence = {},
  131. },
  132. [SNDRV_PCM_FORMAT_G723_40] = {
  133. .width = 5, .phys = 5, .le = -1, .signd = -1,
  134. .silence = {},
  135. },
  136. /* FIXME: the following three formats are not defined properly yet */
  137. [SNDRV_PCM_FORMAT_MPEG] = {
  138. .le = -1, .signd = -1,
  139. },
  140. [SNDRV_PCM_FORMAT_GSM] = {
  141. .le = -1, .signd = -1,
  142. },
  143. [SNDRV_PCM_FORMAT_SPECIAL] = {
  144. .le = -1, .signd = -1,
  145. },
  146. [SNDRV_PCM_FORMAT_S24_3LE] = {
  147. .width = 24, .phys = 24, .le = 1, .signd = 1,
  148. .silence = {},
  149. },
  150. [SNDRV_PCM_FORMAT_S24_3BE] = {
  151. .width = 24, .phys = 24, .le = 0, .signd = 1,
  152. .silence = {},
  153. },
  154. [SNDRV_PCM_FORMAT_U24_3LE] = {
  155. .width = 24, .phys = 24, .le = 1, .signd = 0,
  156. .silence = { 0x00, 0x00, 0x80 },
  157. },
  158. [SNDRV_PCM_FORMAT_U24_3BE] = {
  159. .width = 24, .phys = 24, .le = 0, .signd = 0,
  160. .silence = { 0x80, 0x00, 0x00 },
  161. },
  162. [SNDRV_PCM_FORMAT_S20_3LE] = {
  163. .width = 20, .phys = 24, .le = 1, .signd = 1,
  164. .silence = {},
  165. },
  166. [SNDRV_PCM_FORMAT_S20_3BE] = {
  167. .width = 20, .phys = 24, .le = 0, .signd = 1,
  168. .silence = {},
  169. },
  170. [SNDRV_PCM_FORMAT_U20_3LE] = {
  171. .width = 20, .phys = 24, .le = 1, .signd = 0,
  172. .silence = { 0x00, 0x00, 0x08 },
  173. },
  174. [SNDRV_PCM_FORMAT_U20_3BE] = {
  175. .width = 20, .phys = 24, .le = 0, .signd = 0,
  176. .silence = { 0x08, 0x00, 0x00 },
  177. },
  178. [SNDRV_PCM_FORMAT_S18_3LE] = {
  179. .width = 18, .phys = 24, .le = 1, .signd = 1,
  180. .silence = {},
  181. },
  182. [SNDRV_PCM_FORMAT_S18_3BE] = {
  183. .width = 18, .phys = 24, .le = 0, .signd = 1,
  184. .silence = {},
  185. },
  186. [SNDRV_PCM_FORMAT_U18_3LE] = {
  187. .width = 18, .phys = 24, .le = 1, .signd = 0,
  188. .silence = { 0x00, 0x00, 0x02 },
  189. },
  190. [SNDRV_PCM_FORMAT_U18_3BE] = {
  191. .width = 18, .phys = 24, .le = 0, .signd = 0,
  192. .silence = { 0x02, 0x00, 0x00 },
  193. },
  194. [SNDRV_PCM_FORMAT_G723_24_1B] = {
  195. .width = 3, .phys = 8, .le = -1, .signd = -1,
  196. .silence = {},
  197. },
  198. [SNDRV_PCM_FORMAT_G723_40_1B] = {
  199. .width = 5, .phys = 8, .le = -1, .signd = -1,
  200. .silence = {},
  201. },
  202. };
  203. /**
  204. * snd_pcm_format_signed - Check the PCM format is signed linear
  205. * @format: the format to check
  206. *
  207. * Returns 1 if the given PCM format is signed linear, 0 if unsigned
  208. * linear, and a negative error code for non-linear formats.
  209. */
  210. int snd_pcm_format_signed(snd_pcm_format_t format)
  211. {
  212. int val;
  213. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  214. return -EINVAL;
  215. if ((val = pcm_formats[format].signd) < 0)
  216. return -EINVAL;
  217. return val;
  218. }
  219. EXPORT_SYMBOL(snd_pcm_format_signed);
  220. /**
  221. * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
  222. * @format: the format to check
  223. *
  224. * Returns 1 if the given PCM format is unsigned linear, 0 if signed
  225. * linear, and a negative error code for non-linear formats.
  226. */
  227. int snd_pcm_format_unsigned(snd_pcm_format_t format)
  228. {
  229. int val;
  230. val = snd_pcm_format_signed(format);
  231. if (val < 0)
  232. return val;
  233. return !val;
  234. }
  235. EXPORT_SYMBOL(snd_pcm_format_unsigned);
  236. /**
  237. * snd_pcm_format_linear - Check the PCM format is linear
  238. * @format: the format to check
  239. *
  240. * Returns 1 if the given PCM format is linear, 0 if not.
  241. */
  242. int snd_pcm_format_linear(snd_pcm_format_t format)
  243. {
  244. return snd_pcm_format_signed(format) >= 0;
  245. }
  246. EXPORT_SYMBOL(snd_pcm_format_linear);
  247. /**
  248. * snd_pcm_format_little_endian - Check the PCM format is little-endian
  249. * @format: the format to check
  250. *
  251. * Returns 1 if the given PCM format is little-endian, 0 if
  252. * big-endian, or a negative error code if endian not specified.
  253. */
  254. int snd_pcm_format_little_endian(snd_pcm_format_t format)
  255. {
  256. int val;
  257. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  258. return -EINVAL;
  259. if ((val = pcm_formats[format].le) < 0)
  260. return -EINVAL;
  261. return val;
  262. }
  263. EXPORT_SYMBOL(snd_pcm_format_little_endian);
  264. /**
  265. * snd_pcm_format_big_endian - Check the PCM format is big-endian
  266. * @format: the format to check
  267. *
  268. * Returns 1 if the given PCM format is big-endian, 0 if
  269. * little-endian, or a negative error code if endian not specified.
  270. */
  271. int snd_pcm_format_big_endian(snd_pcm_format_t format)
  272. {
  273. int val;
  274. val = snd_pcm_format_little_endian(format);
  275. if (val < 0)
  276. return val;
  277. return !val;
  278. }
  279. EXPORT_SYMBOL(snd_pcm_format_big_endian);
  280. /**
  281. * snd_pcm_format_width - return the bit-width of the format
  282. * @format: the format to check
  283. *
  284. * Returns the bit-width of the format, or a negative error code
  285. * if unknown format.
  286. */
  287. int snd_pcm_format_width(snd_pcm_format_t format)
  288. {
  289. int val;
  290. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  291. return -EINVAL;
  292. if ((val = pcm_formats[format].width) == 0)
  293. return -EINVAL;
  294. return val;
  295. }
  296. EXPORT_SYMBOL(snd_pcm_format_width);
  297. /**
  298. * snd_pcm_format_physical_width - return the physical bit-width of the format
  299. * @format: the format to check
  300. *
  301. * Returns the physical bit-width of the format, or a negative error code
  302. * if unknown format.
  303. */
  304. int snd_pcm_format_physical_width(snd_pcm_format_t format)
  305. {
  306. int val;
  307. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  308. return -EINVAL;
  309. if ((val = pcm_formats[format].phys) == 0)
  310. return -EINVAL;
  311. return val;
  312. }
  313. EXPORT_SYMBOL(snd_pcm_format_physical_width);
  314. /**
  315. * snd_pcm_format_size - return the byte size of samples on the given format
  316. * @format: the format to check
  317. * @samples: sampling rate
  318. *
  319. * Returns the byte size of the given samples for the format, or a
  320. * negative error code if unknown format.
  321. */
  322. ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
  323. {
  324. int phys_width = snd_pcm_format_physical_width(format);
  325. if (phys_width < 0)
  326. return -EINVAL;
  327. return samples * phys_width / 8;
  328. }
  329. EXPORT_SYMBOL(snd_pcm_format_size);
  330. /**
  331. * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
  332. * @format: the format to check
  333. *
  334. * Returns the format pattern to fill or NULL if error.
  335. */
  336. const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
  337. {
  338. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  339. return NULL;
  340. if (! pcm_formats[format].phys)
  341. return NULL;
  342. return pcm_formats[format].silence;
  343. }
  344. EXPORT_SYMBOL(snd_pcm_format_silence_64);
  345. /**
  346. * snd_pcm_format_set_silence - set the silence data on the buffer
  347. * @format: the PCM format
  348. * @data: the buffer pointer
  349. * @samples: the number of samples to set silence
  350. *
  351. * Sets the silence data on the buffer for the given samples.
  352. *
  353. * Returns zero if successful, or a negative error code on failure.
  354. */
  355. int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
  356. {
  357. int width;
  358. unsigned char *dst, *pat;
  359. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  360. return -EINVAL;
  361. if (samples == 0)
  362. return 0;
  363. width = pcm_formats[format].phys; /* physical width */
  364. pat = pcm_formats[format].silence;
  365. if (! width)
  366. return -EINVAL;
  367. /* signed or 1 byte data */
  368. if (pcm_formats[format].signd == 1 || width <= 8) {
  369. unsigned int bytes = samples * width / 8;
  370. memset(data, *pat, bytes);
  371. return 0;
  372. }
  373. /* non-zero samples, fill using a loop */
  374. width /= 8;
  375. dst = data;
  376. #if 0
  377. while (samples--) {
  378. memcpy(dst, pat, width);
  379. dst += width;
  380. }
  381. #else
  382. /* a bit optimization for constant width */
  383. switch (width) {
  384. case 2:
  385. while (samples--) {
  386. memcpy(dst, pat, 2);
  387. dst += 2;
  388. }
  389. break;
  390. case 3:
  391. while (samples--) {
  392. memcpy(dst, pat, 3);
  393. dst += 3;
  394. }
  395. break;
  396. case 4:
  397. while (samples--) {
  398. memcpy(dst, pat, 4);
  399. dst += 4;
  400. }
  401. break;
  402. case 8:
  403. while (samples--) {
  404. memcpy(dst, pat, 8);
  405. dst += 8;
  406. }
  407. break;
  408. }
  409. #endif
  410. return 0;
  411. }
  412. EXPORT_SYMBOL(snd_pcm_format_set_silence);
  413. /**
  414. * snd_pcm_limit_hw_rates - determine rate_min/rate_max fields
  415. * @runtime: the runtime instance
  416. *
  417. * Determines the rate_min and rate_max fields from the rates bits of
  418. * the given runtime->hw.
  419. *
  420. * Returns zero if successful.
  421. */
  422. int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
  423. {
  424. int i;
  425. for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
  426. if (runtime->hw.rates & (1 << i)) {
  427. runtime->hw.rate_min = snd_pcm_known_rates.list[i];
  428. break;
  429. }
  430. }
  431. for (i = (int)snd_pcm_known_rates.count - 1; i >= 0; i--) {
  432. if (runtime->hw.rates & (1 << i)) {
  433. runtime->hw.rate_max = snd_pcm_known_rates.list[i];
  434. break;
  435. }
  436. }
  437. return 0;
  438. }
  439. EXPORT_SYMBOL(snd_pcm_limit_hw_rates);
  440. /**
  441. * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
  442. * @rate: the sample rate to convert
  443. *
  444. * Returns the SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
  445. * SNDRV_PCM_RATE_KNOT for an unknown rate.
  446. */
  447. unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
  448. {
  449. unsigned int i;
  450. for (i = 0; i < snd_pcm_known_rates.count; i++)
  451. if (snd_pcm_known_rates.list[i] == rate)
  452. return 1u << i;
  453. return SNDRV_PCM_RATE_KNOT;
  454. }
  455. EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);