1 #ifndef __SOUND_PCM_PARAMS_H
2 #define __SOUND_PCM_PARAMS_H
6 * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 extern int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
26 snd_pcm_hw_param_t var, const struct snd_mask *val);
27 extern unsigned int snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
28 snd_pcm_hw_param_t var, int *dir);
29 extern unsigned int snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
30 snd_pcm_hw_param_t var, int *dir);
31 extern int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
32 snd_pcm_hw_param_t var, unsigned int val, int dir);
33 extern int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
34 snd_pcm_hw_param_t var);
35 extern int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
36 snd_pcm_hw_param_t var, unsigned int val, int dir);
38 /* To share the same code we have alsa-lib */
39 #define INLINE static inline
40 #define assert(a) (void)(a)
42 #define SNDRV_MASK_BITS 64 /* we use so far 64bits only */
43 #define SNDRV_MASK_SIZE (SNDRV_MASK_BITS / 32)
44 #define MASK_OFS(i) ((i) >> 5)
45 #define MASK_BIT(i) (1U << ((i) & 31))
47 INLINE unsigned int ld2(u_int32_t v)
72 INLINE size_t snd_mask_sizeof(void)
74 return sizeof(struct snd_mask);
77 INLINE void snd_mask_none(struct snd_mask *mask)
79 memset(mask, 0, sizeof(*mask));
82 INLINE void snd_mask_any(struct snd_mask *mask)
84 memset(mask, 0xff, SNDRV_MASK_SIZE * sizeof(u_int32_t));
87 INLINE int snd_mask_empty(const struct snd_mask *mask)
90 for (i = 0; i < SNDRV_MASK_SIZE; i++)
96 INLINE unsigned int snd_mask_min(const struct snd_mask *mask)
99 assert(!snd_mask_empty(mask));
100 for (i = 0; i < SNDRV_MASK_SIZE; i++) {
102 return ffs(mask->bits[i]) - 1 + (i << 5);
107 INLINE unsigned int snd_mask_max(const struct snd_mask *mask)
110 assert(!snd_mask_empty(mask));
111 for (i = SNDRV_MASK_SIZE - 1; i >= 0; i--) {
113 return ld2(mask->bits[i]) + (i << 5);
118 INLINE void snd_mask_set(struct snd_mask *mask, unsigned int val)
120 assert(val <= SNDRV_MASK_BITS);
121 mask->bits[MASK_OFS(val)] |= MASK_BIT(val);
124 INLINE void snd_mask_reset(struct snd_mask *mask, unsigned int val)
126 assert(val <= SNDRV_MASK_BITS);
127 mask->bits[MASK_OFS(val)] &= ~MASK_BIT(val);
130 INLINE void snd_mask_set_range(struct snd_mask *mask, unsigned int from, unsigned int to)
133 assert(to <= SNDRV_MASK_BITS && from <= to);
134 for (i = from; i <= to; i++)
135 mask->bits[MASK_OFS(i)] |= MASK_BIT(i);
138 INLINE void snd_mask_reset_range(struct snd_mask *mask, unsigned int from, unsigned int to)
141 assert(to <= SNDRV_MASK_BITS && from <= to);
142 for (i = from; i <= to; i++)
143 mask->bits[MASK_OFS(i)] &= ~MASK_BIT(i);
146 INLINE void snd_mask_leave(struct snd_mask *mask, unsigned int val)
149 assert(val <= SNDRV_MASK_BITS);
150 v = mask->bits[MASK_OFS(val)] & MASK_BIT(val);
152 mask->bits[MASK_OFS(val)] = v;
155 INLINE void snd_mask_intersect(struct snd_mask *mask, const struct snd_mask *v)
158 for (i = 0; i < SNDRV_MASK_SIZE; i++)
159 mask->bits[i] &= v->bits[i];
162 INLINE int snd_mask_eq(const struct snd_mask *mask, const struct snd_mask *v)
164 return ! memcmp(mask, v, SNDRV_MASK_SIZE * sizeof(u_int32_t));
167 INLINE void snd_mask_copy(struct snd_mask *mask, const struct snd_mask *v)
172 INLINE int snd_mask_test(const struct snd_mask *mask, unsigned int val)
174 assert(val <= SNDRV_MASK_BITS);
175 return mask->bits[MASK_OFS(val)] & MASK_BIT(val);
178 INLINE int snd_mask_single(const struct snd_mask *mask)
181 assert(!snd_mask_empty(mask));
182 for (i = 0; i < SNDRV_MASK_SIZE; i++) {
185 if (mask->bits[i] & (mask->bits[i] - 1))
194 INLINE int snd_mask_refine(struct snd_mask *mask, const struct snd_mask *v)
197 assert(!snd_mask_empty(mask));
198 snd_mask_copy(&old, mask);
199 snd_mask_intersect(mask, v);
200 if (snd_mask_empty(mask))
202 return !snd_mask_eq(mask, &old);
205 INLINE int snd_mask_refine_first(struct snd_mask *mask)
207 assert(!snd_mask_empty(mask));
208 if (snd_mask_single(mask))
210 snd_mask_leave(mask, snd_mask_min(mask));
214 INLINE int snd_mask_refine_last(struct snd_mask *mask)
216 assert(!snd_mask_empty(mask));
217 if (snd_mask_single(mask))
219 snd_mask_leave(mask, snd_mask_max(mask));
223 INLINE int snd_mask_refine_min(struct snd_mask *mask, unsigned int val)
225 assert(!snd_mask_empty(mask));
226 if (snd_mask_min(mask) >= val)
228 snd_mask_reset_range(mask, 0, val - 1);
229 if (snd_mask_empty(mask))
234 INLINE int snd_mask_refine_max(struct snd_mask *mask, unsigned int val)
236 assert(!snd_mask_empty(mask));
237 if (snd_mask_max(mask) <= val)
239 snd_mask_reset_range(mask, val + 1, SNDRV_MASK_BITS);
240 if (snd_mask_empty(mask))
245 INLINE int snd_mask_refine_set(struct snd_mask *mask, unsigned int val)
248 assert(!snd_mask_empty(mask));
249 changed = !snd_mask_single(mask);
250 snd_mask_leave(mask, val);
251 if (snd_mask_empty(mask))
256 INLINE int snd_mask_value(const struct snd_mask *mask)
258 assert(!snd_mask_empty(mask));
259 return snd_mask_min(mask);
262 INLINE void snd_interval_any(struct snd_interval *i)
272 INLINE void snd_interval_none(struct snd_interval *i)
277 INLINE int snd_interval_checkempty(const struct snd_interval *i)
279 return (i->min > i->max ||
280 (i->min == i->max && (i->openmin || i->openmax)));
283 INLINE int snd_interval_empty(const struct snd_interval *i)
288 INLINE int snd_interval_single(const struct snd_interval *i)
290 assert(!snd_interval_empty(i));
291 return (i->min == i->max ||
292 (i->min + 1 == i->max && i->openmax));
295 INLINE int snd_interval_value(const struct snd_interval *i)
297 assert(snd_interval_single(i));
301 INLINE int snd_interval_min(const struct snd_interval *i)
303 assert(!snd_interval_empty(i));
307 INLINE int snd_interval_max(const struct snd_interval *i)
310 assert(!snd_interval_empty(i));
317 INLINE int snd_interval_test(const struct snd_interval *i, unsigned int val)
319 return !((i->min > val || (i->min == val && i->openmin) ||
320 i->max < val || (i->max == val && i->openmax)));
323 INLINE void snd_interval_copy(struct snd_interval *d, const struct snd_interval *s)
328 INLINE int snd_interval_setinteger(struct snd_interval *i)
332 if (i->openmin && i->openmax && i->min == i->max)
338 INLINE int snd_interval_eq(const struct snd_interval *i1, const struct snd_interval *i2)
344 return i1->min == i2->min && i1->openmin == i2->openmin &&
345 i1->max == i2->max && i1->openmax == i2->openmax;
348 static inline unsigned int add(unsigned int a, unsigned int b)
350 if (a >= UINT_MAX - b)
355 static inline unsigned int sub(unsigned int a, unsigned int b)
365 #endif /* __SOUND_PCM_PARAMS_H */