pcm_misc.c 13 KB

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