pcm_misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. /**
  205. * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
  206. * @format: the format to check
  207. *
  208. * Returns 1 if the given PCM format is unsigned linear, 0 if signed
  209. * linear, and a negative error code for non-linear formats.
  210. */
  211. int snd_pcm_format_unsigned(snd_pcm_format_t format)
  212. {
  213. int val;
  214. val = snd_pcm_format_signed(format);
  215. if (val < 0)
  216. return val;
  217. return !val;
  218. }
  219. /**
  220. * snd_pcm_format_linear - Check the PCM format is linear
  221. * @format: the format to check
  222. *
  223. * Returns 1 if the given PCM format is linear, 0 if not.
  224. */
  225. int snd_pcm_format_linear(snd_pcm_format_t format)
  226. {
  227. return snd_pcm_format_signed(format) >= 0;
  228. }
  229. /**
  230. * snd_pcm_format_little_endian - Check the PCM format is little-endian
  231. * @format: the format to check
  232. *
  233. * Returns 1 if the given PCM format is little-endian, 0 if
  234. * big-endian, or a negative error code if endian not specified.
  235. */
  236. int snd_pcm_format_little_endian(snd_pcm_format_t format)
  237. {
  238. int val;
  239. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  240. return -EINVAL;
  241. if ((val = pcm_formats[format].le) < 0)
  242. return -EINVAL;
  243. return val;
  244. }
  245. /**
  246. * snd_pcm_format_big_endian - Check the PCM format is big-endian
  247. * @format: the format to check
  248. *
  249. * Returns 1 if the given PCM format is big-endian, 0 if
  250. * little-endian, or a negative error code if endian not specified.
  251. */
  252. int snd_pcm_format_big_endian(snd_pcm_format_t format)
  253. {
  254. int val;
  255. val = snd_pcm_format_little_endian(format);
  256. if (val < 0)
  257. return val;
  258. return !val;
  259. }
  260. /**
  261. * snd_pcm_format_width - return the bit-width of the format
  262. * @format: the format to check
  263. *
  264. * Returns the bit-width of the format, or a negative error code
  265. * if unknown format.
  266. */
  267. int snd_pcm_format_width(snd_pcm_format_t format)
  268. {
  269. int val;
  270. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  271. return -EINVAL;
  272. if ((val = pcm_formats[format].width) == 0)
  273. return -EINVAL;
  274. return val;
  275. }
  276. /**
  277. * snd_pcm_format_physical_width - return the physical bit-width of the format
  278. * @format: the format to check
  279. *
  280. * Returns the physical bit-width of the format, or a negative error code
  281. * if unknown format.
  282. */
  283. int snd_pcm_format_physical_width(snd_pcm_format_t format)
  284. {
  285. int val;
  286. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  287. return -EINVAL;
  288. if ((val = pcm_formats[format].phys) == 0)
  289. return -EINVAL;
  290. return val;
  291. }
  292. /**
  293. * snd_pcm_format_size - return the byte size of samples on the given format
  294. * @format: the format to check
  295. *
  296. * Returns the byte size of the given samples for the format, or a
  297. * negative error code if unknown format.
  298. */
  299. ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
  300. {
  301. int phys_width = snd_pcm_format_physical_width(format);
  302. if (phys_width < 0)
  303. return -EINVAL;
  304. return samples * phys_width / 8;
  305. }
  306. /**
  307. * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
  308. * @format: the format to check
  309. *
  310. * Returns the format pattern to fill or NULL if error.
  311. */
  312. const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
  313. {
  314. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  315. return NULL;
  316. if (! pcm_formats[format].phys)
  317. return NULL;
  318. return pcm_formats[format].silence;
  319. }
  320. /**
  321. * snd_pcm_format_set_silence - set the silence data on the buffer
  322. * @format: the PCM format
  323. * @data: the buffer pointer
  324. * @samples: the number of samples to set silence
  325. *
  326. * Sets the silence data on the buffer for the given samples.
  327. *
  328. * Returns zero if successful, or a negative error code on failure.
  329. */
  330. int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
  331. {
  332. int width;
  333. unsigned char *dst, *pat;
  334. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  335. return -EINVAL;
  336. if (samples == 0)
  337. return 0;
  338. width = pcm_formats[format].phys; /* physical width */
  339. pat = pcm_formats[format].silence;
  340. if (! width)
  341. return -EINVAL;
  342. /* signed or 1 byte data */
  343. if (pcm_formats[format].signd == 1 || width <= 8) {
  344. unsigned int bytes = samples * width / 8;
  345. memset(data, *pat, bytes);
  346. return 0;
  347. }
  348. /* non-zero samples, fill using a loop */
  349. width /= 8;
  350. dst = data;
  351. #if 0
  352. while (samples--) {
  353. memcpy(dst, pat, width);
  354. dst += width;
  355. }
  356. #else
  357. /* a bit optimization for constant width */
  358. switch (width) {
  359. case 2:
  360. while (samples--) {
  361. memcpy(dst, pat, 2);
  362. dst += 2;
  363. }
  364. break;
  365. case 3:
  366. while (samples--) {
  367. memcpy(dst, pat, 3);
  368. dst += 3;
  369. }
  370. break;
  371. case 4:
  372. while (samples--) {
  373. memcpy(dst, pat, 4);
  374. dst += 4;
  375. }
  376. break;
  377. case 8:
  378. while (samples--) {
  379. memcpy(dst, pat, 8);
  380. dst += 8;
  381. }
  382. break;
  383. }
  384. #endif
  385. return 0;
  386. }
  387. /* [width][unsigned][bigendian] */
  388. static int linear_formats[4][2][2] = {
  389. {{ SNDRV_PCM_FORMAT_S8, SNDRV_PCM_FORMAT_S8},
  390. { SNDRV_PCM_FORMAT_U8, SNDRV_PCM_FORMAT_U8}},
  391. {{SNDRV_PCM_FORMAT_S16_LE, SNDRV_PCM_FORMAT_S16_BE},
  392. {SNDRV_PCM_FORMAT_U16_LE, SNDRV_PCM_FORMAT_U16_BE}},
  393. {{SNDRV_PCM_FORMAT_S24_LE, SNDRV_PCM_FORMAT_S24_BE},
  394. {SNDRV_PCM_FORMAT_U24_LE, SNDRV_PCM_FORMAT_U24_BE}},
  395. {{SNDRV_PCM_FORMAT_S32_LE, SNDRV_PCM_FORMAT_S32_BE},
  396. {SNDRV_PCM_FORMAT_U32_LE, SNDRV_PCM_FORMAT_U32_BE}}
  397. };
  398. /**
  399. * snd_pcm_build_linear_format - return the suitable linear format for the given condition
  400. * @width: the bit-width
  401. * @unsignd: 1 if unsigned, 0 if signed.
  402. * @big_endian: 1 if big-endian, 0 if little-endian
  403. *
  404. * Returns the suitable linear format for the given condition.
  405. */
  406. snd_pcm_format_t snd_pcm_build_linear_format(int width, int unsignd, int big_endian)
  407. {
  408. if (width & 7)
  409. return SND_PCM_FORMAT_UNKNOWN;
  410. width = (width / 8) - 1;
  411. if (width < 0 || width >= 4)
  412. return SND_PCM_FORMAT_UNKNOWN;
  413. return linear_formats[width][!!unsignd][!!big_endian];
  414. }
  415. /**
  416. * snd_pcm_limit_hw_rates - determine rate_min/rate_max fields
  417. * @runtime: the runtime instance
  418. *
  419. * Determines the rate_min and rate_max fields from the rates bits of
  420. * the given runtime->hw.
  421. *
  422. * Returns zero if successful.
  423. */
  424. int snd_pcm_limit_hw_rates(snd_pcm_runtime_t *runtime)
  425. {
  426. static unsigned rates[] = {
  427. /* ATTENTION: these values depend on the definition in pcm.h! */
  428. 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
  429. 64000, 88200, 96000, 176400, 192000
  430. };
  431. int i;
  432. for (i = 0; i < (int)ARRAY_SIZE(rates); i++) {
  433. if (runtime->hw.rates & (1 << i)) {
  434. runtime->hw.rate_min = rates[i];
  435. break;
  436. }
  437. }
  438. for (i = (int)ARRAY_SIZE(rates) - 1; i >= 0; i--) {
  439. if (runtime->hw.rates & (1 << i)) {
  440. runtime->hw.rate_max = rates[i];
  441. break;
  442. }
  443. }
  444. return 0;
  445. }