pcm_misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. /* FIXME: the following three formats are not defined properly yet */
  129. [SNDRV_PCM_FORMAT_MPEG] = {
  130. .le = -1, .signd = -1,
  131. },
  132. [SNDRV_PCM_FORMAT_GSM] = {
  133. .le = -1, .signd = -1,
  134. },
  135. [SNDRV_PCM_FORMAT_SPECIAL] = {
  136. .le = -1, .signd = -1,
  137. },
  138. [SNDRV_PCM_FORMAT_S24_3LE] = {
  139. .width = 24, .phys = 24, .le = 1, .signd = 1,
  140. .silence = {},
  141. },
  142. [SNDRV_PCM_FORMAT_S24_3BE] = {
  143. .width = 24, .phys = 24, .le = 0, .signd = 1,
  144. .silence = {},
  145. },
  146. [SNDRV_PCM_FORMAT_U24_3LE] = {
  147. .width = 24, .phys = 24, .le = 1, .signd = 0,
  148. .silence = { 0x00, 0x00, 0x80 },
  149. },
  150. [SNDRV_PCM_FORMAT_U24_3BE] = {
  151. .width = 24, .phys = 24, .le = 0, .signd = 0,
  152. .silence = { 0x80, 0x00, 0x00 },
  153. },
  154. [SNDRV_PCM_FORMAT_S20_3LE] = {
  155. .width = 20, .phys = 24, .le = 1, .signd = 1,
  156. .silence = {},
  157. },
  158. [SNDRV_PCM_FORMAT_S20_3BE] = {
  159. .width = 20, .phys = 24, .le = 0, .signd = 1,
  160. .silence = {},
  161. },
  162. [SNDRV_PCM_FORMAT_U20_3LE] = {
  163. .width = 20, .phys = 24, .le = 1, .signd = 0,
  164. .silence = { 0x00, 0x00, 0x08 },
  165. },
  166. [SNDRV_PCM_FORMAT_U20_3BE] = {
  167. .width = 20, .phys = 24, .le = 0, .signd = 0,
  168. .silence = { 0x08, 0x00, 0x00 },
  169. },
  170. [SNDRV_PCM_FORMAT_S18_3LE] = {
  171. .width = 18, .phys = 24, .le = 1, .signd = 1,
  172. .silence = {},
  173. },
  174. [SNDRV_PCM_FORMAT_S18_3BE] = {
  175. .width = 18, .phys = 24, .le = 0, .signd = 1,
  176. .silence = {},
  177. },
  178. [SNDRV_PCM_FORMAT_U18_3LE] = {
  179. .width = 18, .phys = 24, .le = 1, .signd = 0,
  180. .silence = { 0x00, 0x00, 0x02 },
  181. },
  182. [SNDRV_PCM_FORMAT_U18_3BE] = {
  183. .width = 18, .phys = 24, .le = 0, .signd = 0,
  184. .silence = { 0x02, 0x00, 0x00 },
  185. },
  186. };
  187. /**
  188. * snd_pcm_format_signed - Check the PCM format is signed linear
  189. * @format: the format to check
  190. *
  191. * Returns 1 if the given PCM format is signed linear, 0 if unsigned
  192. * linear, and a negative error code for non-linear formats.
  193. */
  194. int snd_pcm_format_signed(snd_pcm_format_t format)
  195. {
  196. int val;
  197. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  198. return -EINVAL;
  199. if ((val = pcm_formats[format].signd) < 0)
  200. return -EINVAL;
  201. return val;
  202. }
  203. EXPORT_SYMBOL(snd_pcm_format_signed);
  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. EXPORT_SYMBOL(snd_pcm_format_unsigned);
  220. /**
  221. * snd_pcm_format_linear - Check the PCM format is linear
  222. * @format: the format to check
  223. *
  224. * Returns 1 if the given PCM format is linear, 0 if not.
  225. */
  226. int snd_pcm_format_linear(snd_pcm_format_t format)
  227. {
  228. return snd_pcm_format_signed(format) >= 0;
  229. }
  230. EXPORT_SYMBOL(snd_pcm_format_linear);
  231. /**
  232. * snd_pcm_format_little_endian - Check the PCM format is little-endian
  233. * @format: the format to check
  234. *
  235. * Returns 1 if the given PCM format is little-endian, 0 if
  236. * big-endian, or a negative error code if endian not specified.
  237. */
  238. int snd_pcm_format_little_endian(snd_pcm_format_t format)
  239. {
  240. int val;
  241. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  242. return -EINVAL;
  243. if ((val = pcm_formats[format].le) < 0)
  244. return -EINVAL;
  245. return val;
  246. }
  247. EXPORT_SYMBOL(snd_pcm_format_little_endian);
  248. /**
  249. * snd_pcm_format_big_endian - Check the PCM format is big-endian
  250. * @format: the format to check
  251. *
  252. * Returns 1 if the given PCM format is big-endian, 0 if
  253. * little-endian, or a negative error code if endian not specified.
  254. */
  255. int snd_pcm_format_big_endian(snd_pcm_format_t format)
  256. {
  257. int val;
  258. val = snd_pcm_format_little_endian(format);
  259. if (val < 0)
  260. return val;
  261. return !val;
  262. }
  263. EXPORT_SYMBOL(snd_pcm_format_big_endian);
  264. /**
  265. * snd_pcm_format_width - return the bit-width of the format
  266. * @format: the format to check
  267. *
  268. * Returns the bit-width of the format, or a negative error code
  269. * if unknown format.
  270. */
  271. int snd_pcm_format_width(snd_pcm_format_t format)
  272. {
  273. int val;
  274. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  275. return -EINVAL;
  276. if ((val = pcm_formats[format].width) == 0)
  277. return -EINVAL;
  278. return val;
  279. }
  280. EXPORT_SYMBOL(snd_pcm_format_width);
  281. /**
  282. * snd_pcm_format_physical_width - return the physical bit-width of the format
  283. * @format: the format to check
  284. *
  285. * Returns the physical bit-width of the format, or a negative error code
  286. * if unknown format.
  287. */
  288. int snd_pcm_format_physical_width(snd_pcm_format_t format)
  289. {
  290. int val;
  291. if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
  292. return -EINVAL;
  293. if ((val = pcm_formats[format].phys) == 0)
  294. return -EINVAL;
  295. return val;
  296. }
  297. EXPORT_SYMBOL(snd_pcm_format_physical_width);
  298. /**
  299. * snd_pcm_format_size - return the byte size of samples on the given format
  300. * @format: the format to check
  301. * @samples: sampling rate
  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. /**
  398. * snd_pcm_limit_hw_rates - determine rate_min/rate_max fields
  399. * @runtime: the runtime instance
  400. *
  401. * Determines the rate_min and rate_max fields from the rates bits of
  402. * the given runtime->hw.
  403. *
  404. * Returns zero if successful.
  405. */
  406. int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
  407. {
  408. int i;
  409. for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
  410. if (runtime->hw.rates & (1 << i)) {
  411. runtime->hw.rate_min = snd_pcm_known_rates.list[i];
  412. break;
  413. }
  414. }
  415. for (i = (int)snd_pcm_known_rates.count - 1; i >= 0; i--) {
  416. if (runtime->hw.rates & (1 << i)) {
  417. runtime->hw.rate_max = snd_pcm_known_rates.list[i];
  418. break;
  419. }
  420. }
  421. return 0;
  422. }
  423. EXPORT_SYMBOL(snd_pcm_limit_hw_rates);
  424. /**
  425. * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
  426. * @rate: the sample rate to convert
  427. *
  428. * Returns the SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
  429. * SNDRV_PCM_RATE_KNOT for an unknown rate.
  430. */
  431. unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
  432. {
  433. unsigned int i;
  434. for (i = 0; i < snd_pcm_known_rates.count; i++)
  435. if (snd_pcm_known_rates.list[i] == rate)
  436. return 1u << i;
  437. return SNDRV_PCM_RATE_KNOT;
  438. }
  439. EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);