pcm_params.h 7.7 KB

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