pcm_misc.c 13 KB

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