pcm_params.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 unsigned int ld2(u_int32_t v)
  37. {
  38. unsigned r = 0;
  39. if (v >= 0x10000) {
  40. v >>= 16;
  41. r += 16;
  42. }
  43. if (v >= 0x100) {
  44. v >>= 8;
  45. r += 8;
  46. }
  47. if (v >= 0x10) {
  48. v >>= 4;
  49. r += 4;
  50. }
  51. if (v >= 4) {
  52. v >>= 2;
  53. r += 2;
  54. }
  55. if (v >= 2)
  56. r++;
  57. return r;
  58. }
  59. static inline size_t snd_mask_sizeof(void)
  60. {
  61. return sizeof(struct snd_mask);
  62. }
  63. static inline void snd_mask_none(struct snd_mask *mask)
  64. {
  65. memset(mask, 0, sizeof(*mask));
  66. }
  67. static inline void snd_mask_any(struct snd_mask *mask)
  68. {
  69. memset(mask, 0xff, SNDRV_MASK_SIZE * sizeof(u_int32_t));
  70. }
  71. static inline int snd_mask_empty(const struct snd_mask *mask)
  72. {
  73. int i;
  74. for (i = 0; i < SNDRV_MASK_SIZE; i++)
  75. if (mask->bits[i])
  76. return 0;
  77. return 1;
  78. }
  79. static inline unsigned int snd_mask_min(const struct snd_mask *mask)
  80. {
  81. int i;
  82. for (i = 0; i < SNDRV_MASK_SIZE; i++) {
  83. if (mask->bits[i])
  84. return ffs(mask->bits[i]) - 1 + (i << 5);
  85. }
  86. return 0;
  87. }
  88. static inline unsigned int snd_mask_max(const struct snd_mask *mask)
  89. {
  90. int i;
  91. for (i = SNDRV_MASK_SIZE - 1; i >= 0; i--) {
  92. if (mask->bits[i])
  93. return ld2(mask->bits[i]) + (i << 5);
  94. }
  95. return 0;
  96. }
  97. static inline void snd_mask_set(struct snd_mask *mask, unsigned int val)
  98. {
  99. mask->bits[MASK_OFS(val)] |= MASK_BIT(val);
  100. }
  101. static inline void snd_mask_reset(struct snd_mask *mask, unsigned int val)
  102. {
  103. mask->bits[MASK_OFS(val)] &= ~MASK_BIT(val);
  104. }
  105. static inline void snd_mask_set_range(struct snd_mask *mask,
  106. unsigned int from, unsigned int to)
  107. {
  108. unsigned int i;
  109. for (i = from; i <= to; i++)
  110. mask->bits[MASK_OFS(i)] |= MASK_BIT(i);
  111. }
  112. static inline void snd_mask_reset_range(struct snd_mask *mask,
  113. unsigned int from, unsigned int to)
  114. {
  115. unsigned int i;
  116. for (i = from; i <= to; i++)
  117. mask->bits[MASK_OFS(i)] &= ~MASK_BIT(i);
  118. }
  119. static inline void snd_mask_leave(struct snd_mask *mask, unsigned int val)
  120. {
  121. unsigned int v;
  122. v = mask->bits[MASK_OFS(val)] & MASK_BIT(val);
  123. snd_mask_none(mask);
  124. mask->bits[MASK_OFS(val)] = v;
  125. }
  126. static inline void snd_mask_intersect(struct snd_mask *mask,
  127. const struct snd_mask *v)
  128. {
  129. int i;
  130. for (i = 0; i < SNDRV_MASK_SIZE; i++)
  131. mask->bits[i] &= v->bits[i];
  132. }
  133. static inline int snd_mask_eq(const struct snd_mask *mask,
  134. const struct snd_mask *v)
  135. {
  136. return ! memcmp(mask, v, SNDRV_MASK_SIZE * sizeof(u_int32_t));
  137. }
  138. static inline void snd_mask_copy(struct snd_mask *mask,
  139. const struct snd_mask *v)
  140. {
  141. *mask = *v;
  142. }
  143. static inline int snd_mask_test(const struct snd_mask *mask, unsigned int val)
  144. {
  145. return mask->bits[MASK_OFS(val)] & MASK_BIT(val);
  146. }
  147. static inline int snd_mask_single(const struct snd_mask *mask)
  148. {
  149. int i, c = 0;
  150. for (i = 0; i < SNDRV_MASK_SIZE; i++) {
  151. if (! mask->bits[i])
  152. continue;
  153. if (mask->bits[i] & (mask->bits[i] - 1))
  154. return 0;
  155. if (c)
  156. return 0;
  157. c++;
  158. }
  159. return 1;
  160. }
  161. static inline int snd_mask_refine(struct snd_mask *mask,
  162. const struct snd_mask *v)
  163. {
  164. struct snd_mask old;
  165. snd_mask_copy(&old, mask);
  166. snd_mask_intersect(mask, v);
  167. if (snd_mask_empty(mask))
  168. return -EINVAL;
  169. return !snd_mask_eq(mask, &old);
  170. }
  171. static inline int snd_mask_refine_first(struct snd_mask *mask)
  172. {
  173. if (snd_mask_single(mask))
  174. return 0;
  175. snd_mask_leave(mask, snd_mask_min(mask));
  176. return 1;
  177. }
  178. static inline int snd_mask_refine_last(struct snd_mask *mask)
  179. {
  180. if (snd_mask_single(mask))
  181. return 0;
  182. snd_mask_leave(mask, snd_mask_max(mask));
  183. return 1;
  184. }
  185. static inline int snd_mask_refine_min(struct snd_mask *mask, unsigned int val)
  186. {
  187. if (snd_mask_min(mask) >= val)
  188. return 0;
  189. snd_mask_reset_range(mask, 0, val - 1);
  190. if (snd_mask_empty(mask))
  191. return -EINVAL;
  192. return 1;
  193. }
  194. static inline int snd_mask_refine_max(struct snd_mask *mask, unsigned int val)
  195. {
  196. if (snd_mask_max(mask) <= val)
  197. return 0;
  198. snd_mask_reset_range(mask, val + 1, SNDRV_MASK_BITS);
  199. if (snd_mask_empty(mask))
  200. return -EINVAL;
  201. return 1;
  202. }
  203. static inline int snd_mask_refine_set(struct snd_mask *mask, unsigned int val)
  204. {
  205. int changed;
  206. changed = !snd_mask_single(mask);
  207. snd_mask_leave(mask, val);
  208. if (snd_mask_empty(mask))
  209. return -EINVAL;
  210. return changed;
  211. }
  212. static inline int snd_mask_value(const struct snd_mask *mask)
  213. {
  214. return snd_mask_min(mask);
  215. }
  216. static inline void snd_interval_any(struct snd_interval *i)
  217. {
  218. i->min = 0;
  219. i->openmin = 0;
  220. i->max = UINT_MAX;
  221. i->openmax = 0;
  222. i->integer = 0;
  223. i->empty = 0;
  224. }
  225. static inline void snd_interval_none(struct snd_interval *i)
  226. {
  227. i->empty = 1;
  228. }
  229. static inline int snd_interval_checkempty(const struct snd_interval *i)
  230. {
  231. return (i->min > i->max ||
  232. (i->min == i->max && (i->openmin || i->openmax)));
  233. }
  234. static inline int snd_interval_empty(const struct snd_interval *i)
  235. {
  236. return i->empty;
  237. }
  238. static inline int snd_interval_single(const struct snd_interval *i)
  239. {
  240. return (i->min == i->max ||
  241. (i->min + 1 == i->max && i->openmax));
  242. }
  243. static inline int snd_interval_value(const struct snd_interval *i)
  244. {
  245. return i->min;
  246. }
  247. static inline int snd_interval_min(const struct snd_interval *i)
  248. {
  249. return i->min;
  250. }
  251. static inline int snd_interval_max(const struct snd_interval *i)
  252. {
  253. unsigned int v;
  254. v = i->max;
  255. if (i->openmax)
  256. v--;
  257. return v;
  258. }
  259. static inline int snd_interval_test(const struct snd_interval *i, unsigned int val)
  260. {
  261. return !((i->min > val || (i->min == val && i->openmin) ||
  262. i->max < val || (i->max == val && i->openmax)));
  263. }
  264. static inline void snd_interval_copy(struct snd_interval *d, const struct snd_interval *s)
  265. {
  266. *d = *s;
  267. }
  268. static inline int snd_interval_setinteger(struct snd_interval *i)
  269. {
  270. if (i->integer)
  271. return 0;
  272. if (i->openmin && i->openmax && i->min == i->max)
  273. return -EINVAL;
  274. i->integer = 1;
  275. return 1;
  276. }
  277. static inline int snd_interval_eq(const struct snd_interval *i1, const struct snd_interval *i2)
  278. {
  279. if (i1->empty)
  280. return i2->empty;
  281. if (i2->empty)
  282. return i1->empty;
  283. return i1->min == i2->min && i1->openmin == i2->openmin &&
  284. i1->max == i2->max && i1->openmax == i2->openmax;
  285. }
  286. static inline unsigned int add(unsigned int a, unsigned int b)
  287. {
  288. if (a >= UINT_MAX - b)
  289. return UINT_MAX;
  290. return a + b;
  291. }
  292. static inline unsigned int sub(unsigned int a, unsigned int b)
  293. {
  294. if (a > b)
  295. return a - b;
  296. return 0;
  297. }
  298. #define params_access(p) ((__force snd_pcm_access_t)\
  299. snd_mask_min(hw_param_mask_c((p), SNDRV_PCM_HW_PARAM_ACCESS)))
  300. #define params_format(p) ((__force snd_pcm_format_t)\
  301. snd_mask_min(hw_param_mask_c((p), SNDRV_PCM_HW_PARAM_FORMAT)))
  302. #define params_subformat(p) \
  303. snd_mask_min(hw_param_mask_c((p), SNDRV_PCM_HW_PARAM_SUBFORMAT))
  304. static inline unsigned int
  305. params_period_bytes(const struct snd_pcm_hw_params *p)
  306. {
  307. return (params_period_size(p) *
  308. snd_pcm_format_physical_width(params_format(p)) *
  309. params_channels(p)) / 8;
  310. }
  311. #endif /* __SOUND_PCM_PARAMS_H */