pcm_params.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #ifndef __SOUND_PCM_PARAMS_H
  2. #define __SOUND_PCM_PARAMS_H
  3. /*
  4. * PCM params helpers
  5. * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <sound/pcm.h>
  24. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  25. struct snd_pcm_hw_params *params,
  26. snd_pcm_hw_param_t var, int *dir);
  27. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  28. struct snd_pcm_hw_params *params,
  29. snd_pcm_hw_param_t var, int *dir);
  30. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  31. snd_pcm_hw_param_t var, int *dir);
  32. #define SNDRV_MASK_BITS 64 /* we use so far 64bits only */
  33. #define SNDRV_MASK_SIZE (SNDRV_MASK_BITS / 32)
  34. #define MASK_OFS(i) ((i) >> 5)
  35. #define MASK_BIT(i) (1U << ((i) & 31))
  36. static inline size_t snd_mask_sizeof(void)
  37. {
  38. return sizeof(struct snd_mask);
  39. }
  40. static inline void snd_mask_none(struct snd_mask *mask)
  41. {
  42. memset(mask, 0, sizeof(*mask));
  43. }
  44. static inline void snd_mask_any(struct snd_mask *mask)
  45. {
  46. memset(mask, 0xff, SNDRV_MASK_SIZE * sizeof(u_int32_t));
  47. }
  48. static inline int snd_mask_empty(const struct snd_mask *mask)
  49. {
  50. int i;
  51. for (i = 0; i < SNDRV_MASK_SIZE; i++)
  52. if (mask->bits[i])
  53. return 0;
  54. return 1;
  55. }
  56. static inline unsigned int snd_mask_min(const struct snd_mask *mask)
  57. {
  58. int i;
  59. for (i = 0; i < SNDRV_MASK_SIZE; i++) {
  60. if (mask->bits[i])
  61. return __ffs(mask->bits[i]) + (i << 5);
  62. }
  63. return 0;
  64. }
  65. static inline unsigned int snd_mask_max(const struct snd_mask *mask)
  66. {
  67. int i;
  68. for (i = SNDRV_MASK_SIZE - 1; i >= 0; i--) {
  69. if (mask->bits[i])
  70. return __fls(mask->bits[i]) + (i << 5);
  71. }
  72. return 0;
  73. }
  74. static inline void snd_mask_set(struct snd_mask *mask, unsigned int val)
  75. {
  76. mask->bits[MASK_OFS(val)] |= MASK_BIT(val);
  77. }
  78. /* Most of drivers need only this one */
  79. static inline void snd_mask_set_format(struct snd_mask *mask,
  80. snd_pcm_format_t format)
  81. {
  82. snd_mask_set(mask, (__force unsigned int)format);
  83. }
  84. static inline void snd_mask_reset(struct snd_mask *mask, unsigned int val)
  85. {
  86. mask->bits[MASK_OFS(val)] &= ~MASK_BIT(val);
  87. }
  88. static inline void snd_mask_set_range(struct snd_mask *mask,
  89. unsigned int from, unsigned int to)
  90. {
  91. unsigned int i;
  92. for (i = from; i <= to; i++)
  93. mask->bits[MASK_OFS(i)] |= MASK_BIT(i);
  94. }
  95. static inline void snd_mask_reset_range(struct snd_mask *mask,
  96. unsigned int from, unsigned int to)
  97. {
  98. unsigned int i;
  99. for (i = from; i <= to; i++)
  100. mask->bits[MASK_OFS(i)] &= ~MASK_BIT(i);
  101. }
  102. static inline void snd_mask_leave(struct snd_mask *mask, unsigned int val)
  103. {
  104. unsigned int v;
  105. v = mask->bits[MASK_OFS(val)] & MASK_BIT(val);
  106. snd_mask_none(mask);
  107. mask->bits[MASK_OFS(val)] = v;
  108. }
  109. static inline void snd_mask_intersect(struct snd_mask *mask,
  110. const struct snd_mask *v)
  111. {
  112. int i;
  113. for (i = 0; i < SNDRV_MASK_SIZE; i++)
  114. mask->bits[i] &= v->bits[i];
  115. }
  116. static inline int snd_mask_eq(const struct snd_mask *mask,
  117. const struct snd_mask *v)
  118. {
  119. return ! memcmp(mask, v, SNDRV_MASK_SIZE * sizeof(u_int32_t));
  120. }
  121. static inline void snd_mask_copy(struct snd_mask *mask,
  122. const struct snd_mask *v)
  123. {
  124. *mask = *v;
  125. }
  126. static inline int snd_mask_test(const struct snd_mask *mask, unsigned int val)
  127. {
  128. return mask->bits[MASK_OFS(val)] & MASK_BIT(val);
  129. }
  130. static inline int snd_mask_single(const struct snd_mask *mask)
  131. {
  132. int i, c = 0;
  133. for (i = 0; i < SNDRV_MASK_SIZE; i++) {
  134. if (! mask->bits[i])
  135. continue;
  136. if (mask->bits[i] & (mask->bits[i] - 1))
  137. return 0;
  138. if (c)
  139. return 0;
  140. c++;
  141. }
  142. return 1;
  143. }
  144. static inline int snd_mask_refine(struct snd_mask *mask,
  145. const struct snd_mask *v)
  146. {
  147. struct snd_mask old;
  148. snd_mask_copy(&old, mask);
  149. snd_mask_intersect(mask, v);
  150. if (snd_mask_empty(mask))
  151. return -EINVAL;
  152. return !snd_mask_eq(mask, &old);
  153. }
  154. static inline int snd_mask_refine_first(struct snd_mask *mask)
  155. {
  156. if (snd_mask_single(mask))
  157. return 0;
  158. snd_mask_leave(mask, snd_mask_min(mask));
  159. return 1;
  160. }
  161. static inline int snd_mask_refine_last(struct snd_mask *mask)
  162. {
  163. if (snd_mask_single(mask))
  164. return 0;
  165. snd_mask_leave(mask, snd_mask_max(mask));
  166. return 1;
  167. }
  168. static inline int snd_mask_refine_min(struct snd_mask *mask, unsigned int val)
  169. {
  170. if (snd_mask_min(mask) >= val)
  171. return 0;
  172. snd_mask_reset_range(mask, 0, val - 1);
  173. if (snd_mask_empty(mask))
  174. return -EINVAL;
  175. return 1;
  176. }
  177. static inline int snd_mask_refine_max(struct snd_mask *mask, unsigned int val)
  178. {
  179. if (snd_mask_max(mask) <= val)
  180. return 0;
  181. snd_mask_reset_range(mask, val + 1, SNDRV_MASK_BITS);
  182. if (snd_mask_empty(mask))
  183. return -EINVAL;
  184. return 1;
  185. }
  186. static inline int snd_mask_refine_set(struct snd_mask *mask, unsigned int val)
  187. {
  188. int changed;
  189. changed = !snd_mask_single(mask);
  190. snd_mask_leave(mask, val);
  191. if (snd_mask_empty(mask))
  192. return -EINVAL;
  193. return changed;
  194. }
  195. static inline int snd_mask_value(const struct snd_mask *mask)
  196. {
  197. return snd_mask_min(mask);
  198. }
  199. static inline void snd_interval_any(struct snd_interval *i)
  200. {
  201. i->min = 0;
  202. i->openmin = 0;
  203. i->max = UINT_MAX;
  204. i->openmax = 0;
  205. i->integer = 0;
  206. i->empty = 0;
  207. }
  208. static inline void snd_interval_none(struct snd_interval *i)
  209. {
  210. i->empty = 1;
  211. }
  212. static inline int snd_interval_checkempty(const struct snd_interval *i)
  213. {
  214. return (i->min > i->max ||
  215. (i->min == i->max && (i->openmin || i->openmax)));
  216. }
  217. static inline int snd_interval_empty(const struct snd_interval *i)
  218. {
  219. return i->empty;
  220. }
  221. static inline int snd_interval_single(const struct snd_interval *i)
  222. {
  223. return (i->min == i->max ||
  224. (i->min + 1 == i->max && (i->openmin || i->openmax)));
  225. }
  226. static inline int snd_interval_value(const struct snd_interval *i)
  227. {
  228. if (i->openmin && !i->openmax)
  229. return i->max;
  230. return i->min;
  231. }
  232. static inline int snd_interval_min(const struct snd_interval *i)
  233. {
  234. return i->min;
  235. }
  236. static inline int snd_interval_max(const struct snd_interval *i)
  237. {
  238. unsigned int v;
  239. v = i->max;
  240. if (i->openmax)
  241. v--;
  242. return v;
  243. }
  244. static inline int snd_interval_test(const struct snd_interval *i, unsigned int val)
  245. {
  246. return !((i->min > val || (i->min == val && i->openmin) ||
  247. i->max < val || (i->max == val && i->openmax)));
  248. }
  249. static inline void snd_interval_copy(struct snd_interval *d, const struct snd_interval *s)
  250. {
  251. *d = *s;
  252. }
  253. static inline int snd_interval_setinteger(struct snd_interval *i)
  254. {
  255. if (i->integer)
  256. return 0;
  257. if (i->openmin && i->openmax && i->min == i->max)
  258. return -EINVAL;
  259. i->integer = 1;
  260. return 1;
  261. }
  262. static inline int snd_interval_eq(const struct snd_interval *i1, const struct snd_interval *i2)
  263. {
  264. if (i1->empty)
  265. return i2->empty;
  266. if (i2->empty)
  267. return i1->empty;
  268. return i1->min == i2->min && i1->openmin == i2->openmin &&
  269. i1->max == i2->max && i1->openmax == i2->openmax;
  270. }
  271. /**
  272. * params_access - get the access type from the hw params
  273. * @p: hw params
  274. */
  275. static inline snd_pcm_access_t params_access(const struct snd_pcm_hw_params *p)
  276. {
  277. return (__force snd_pcm_access_t)snd_mask_min(hw_param_mask_c(p,
  278. SNDRV_PCM_HW_PARAM_ACCESS));
  279. }
  280. /**
  281. * params_format - get the sample format from the hw params
  282. * @p: hw params
  283. */
  284. static inline snd_pcm_format_t params_format(const struct snd_pcm_hw_params *p)
  285. {
  286. return (__force snd_pcm_format_t)snd_mask_min(hw_param_mask_c(p,
  287. SNDRV_PCM_HW_PARAM_FORMAT));
  288. }
  289. /**
  290. * params_subformat - get the sample subformat from the hw params
  291. * @p: hw params
  292. */
  293. static inline snd_pcm_subformat_t
  294. params_subformat(const struct snd_pcm_hw_params *p)
  295. {
  296. return (__force snd_pcm_subformat_t)snd_mask_min(hw_param_mask_c(p,
  297. SNDRV_PCM_HW_PARAM_SUBFORMAT));
  298. }
  299. /**
  300. * params_period_bytes - get the period size (in bytes) from the hw params
  301. * @p: hw params
  302. */
  303. static inline unsigned int
  304. params_period_bytes(const struct snd_pcm_hw_params *p)
  305. {
  306. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_PERIOD_BYTES)->min;
  307. }
  308. /**
  309. * params_width - get the number of bits of the sample format from the hw params
  310. * @p: hw params
  311. *
  312. * This function returns the number of bits per sample that the selected sample
  313. * format of the hw params has.
  314. */
  315. static inline int params_width(const struct snd_pcm_hw_params *p)
  316. {
  317. return snd_pcm_format_width(params_format(p));
  318. }
  319. /*
  320. * params_physical_width - get the storage size of the sample format from the hw params
  321. * @p: hw params
  322. *
  323. * This functions returns the number of bits per sample that the selected sample
  324. * format of the hw params takes up in memory. This will be equal or larger than
  325. * params_width().
  326. */
  327. static inline int params_physical_width(const struct snd_pcm_hw_params *p)
  328. {
  329. return snd_pcm_format_physical_width(params_format(p));
  330. }
  331. static inline void
  332. params_set_format(struct snd_pcm_hw_params *p, snd_pcm_format_t fmt)
  333. {
  334. snd_mask_set_format(hw_param_mask(p, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
  335. }
  336. #endif /* __SOUND_PCM_PARAMS_H */