pcm_params.h 8.54 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef __SOUND_PCM_PARAMS_H
#define __SOUND_PCM_PARAMS_H

/*
 *  PCM params helpers
 *  Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

25 26 27
extern int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
				 snd_pcm_hw_param_t var, const struct snd_mask *val);
extern unsigned int snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
Linus Torvalds's avatar
Linus Torvalds committed
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,
Linus Torvalds's avatar
Linus Torvalds committed
30
					       snd_pcm_hw_param_t var, int *dir);
31
extern int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
Linus Torvalds's avatar
Linus Torvalds committed
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,
Linus Torvalds's avatar
Linus Torvalds committed
34
					snd_pcm_hw_param_t var);
35
extern int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
				 snd_pcm_hw_param_t var, unsigned int val, int dir);

/* To share the same code we have  alsa-lib */
#define INLINE static inline
#define assert(a) (void)(a)

#define SNDRV_MASK_BITS	64	/* we use so far 64bits only */
#define SNDRV_MASK_SIZE	(SNDRV_MASK_BITS / 32)
#define MASK_OFS(i)	((i) >> 5)
#define MASK_BIT(i)	(1U << ((i) & 31))

INLINE unsigned int ld2(u_int32_t v)
{
        unsigned r = 0;

        if (v >= 0x10000) {
                v >>= 16;
                r += 16;
        }
        if (v >= 0x100) {
                v >>= 8;
                r += 8;
        }
        if (v >= 0x10) {
                v >>= 4;
                r += 4;
        }
        if (v >= 4) {
                v >>= 2;
                r += 2;
        }
        if (v >= 2)
                r++;
        return r;
}

INLINE size_t snd_mask_sizeof(void)
{
74
	return sizeof(struct snd_mask);
Linus Torvalds's avatar
Linus Torvalds committed
75 76
}

77
INLINE void snd_mask_none(struct snd_mask *mask)
Linus Torvalds's avatar
Linus Torvalds committed
78 79 80 81
{
	memset(mask, 0, sizeof(*mask));
}

82
INLINE void snd_mask_any(struct snd_mask *mask)
Linus Torvalds's avatar
Linus Torvalds committed
83 84 85 86
{
	memset(mask, 0xff, SNDRV_MASK_SIZE * sizeof(u_int32_t));
}

87
INLINE int snd_mask_empty(const struct snd_mask *mask)
Linus Torvalds's avatar
Linus Torvalds committed
88 89 90 91 92 93 94 95
{
	int i;
	for (i = 0; i < SNDRV_MASK_SIZE; i++)
		if (mask->bits[i])
			return 0;
	return 1;
}

96
INLINE unsigned int snd_mask_min(const struct snd_mask *mask)
Linus Torvalds's avatar
Linus Torvalds committed
97 98 99 100 101 102 103 104 105 106
{
	int i;
	assert(!snd_mask_empty(mask));
	for (i = 0; i < SNDRV_MASK_SIZE; i++) {
		if (mask->bits[i])
			return ffs(mask->bits[i]) - 1 + (i << 5);
	}
	return 0;
}

107
INLINE unsigned int snd_mask_max(const struct snd_mask *mask)
Linus Torvalds's avatar
Linus Torvalds committed
108 109 110 111 112 113 114 115 116 117
{
	int i;
	assert(!snd_mask_empty(mask));
	for (i = SNDRV_MASK_SIZE - 1; i >= 0; i--) {
		if (mask->bits[i])
			return ld2(mask->bits[i]) + (i << 5);
	}
	return 0;
}

118
INLINE void snd_mask_set(struct snd_mask *mask, unsigned int val)
Linus Torvalds's avatar
Linus Torvalds committed
119 120 121 122 123
{
	assert(val <= SNDRV_MASK_BITS);
	mask->bits[MASK_OFS(val)] |= MASK_BIT(val);
}

124
INLINE void snd_mask_reset(struct snd_mask *mask, unsigned int val)
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129
{
	assert(val <= SNDRV_MASK_BITS);
	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)
Linus Torvalds's avatar
Linus Torvalds committed
131 132 133 134 135 136 137
{
	unsigned int i;
	assert(to <= SNDRV_MASK_BITS && from <= to);
	for (i = from; i <= to; i++)
		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)
Linus Torvalds's avatar
Linus Torvalds committed
139 140 141 142 143 144 145
{
	unsigned int i;
	assert(to <= SNDRV_MASK_BITS && from <= to);
	for (i = from; i <= to; i++)
		mask->bits[MASK_OFS(i)] &= ~MASK_BIT(i);
}

146
INLINE void snd_mask_leave(struct snd_mask *mask, unsigned int val)
Linus Torvalds's avatar
Linus Torvalds committed
147 148 149 150 151 152 153 154
{
	unsigned int v;
	assert(val <= SNDRV_MASK_BITS);
	v = mask->bits[MASK_OFS(val)] & MASK_BIT(val);
	snd_mask_none(mask);
	mask->bits[MASK_OFS(val)] = v;
}

155
INLINE void snd_mask_intersect(struct snd_mask *mask, const struct snd_mask *v)
Linus Torvalds's avatar
Linus Torvalds committed
156 157 158 159 160 161
{
	int i;
	for (i = 0; i < SNDRV_MASK_SIZE; i++)
		mask->bits[i] &= v->bits[i];
}

162
INLINE int snd_mask_eq(const struct snd_mask *mask, const struct snd_mask *v)
Linus Torvalds's avatar
Linus Torvalds committed
163 164 165 166
{
	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)
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170 171
{
	*mask = *v;
}

172
INLINE int snd_mask_test(const struct snd_mask *mask, unsigned int val)
Linus Torvalds's avatar
Linus Torvalds committed
173 174 175 176 177
{
	assert(val <= SNDRV_MASK_BITS);
	return mask->bits[MASK_OFS(val)] & MASK_BIT(val);
}

178
INLINE int snd_mask_single(const struct snd_mask *mask)
Linus Torvalds's avatar
Linus Torvalds committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
{
	int i, c = 0;
	assert(!snd_mask_empty(mask));
	for (i = 0; i < SNDRV_MASK_SIZE; i++) {
		if (! mask->bits[i])
			continue;
		if (mask->bits[i] & (mask->bits[i] - 1))
			return 0;
		if (c)
			return 0;
		c++;
	}
	return 1;
}

194
INLINE int snd_mask_refine(struct snd_mask *mask, const struct snd_mask *v)
Linus Torvalds's avatar
Linus Torvalds committed
195
{
196
	struct snd_mask old;
Linus Torvalds's avatar
Linus Torvalds committed
197 198 199 200 201 202 203 204
	assert(!snd_mask_empty(mask));
	snd_mask_copy(&old, mask);
	snd_mask_intersect(mask, v);
	if (snd_mask_empty(mask))
		return -EINVAL;
	return !snd_mask_eq(mask, &old);
}

205
INLINE int snd_mask_refine_first(struct snd_mask *mask)
Linus Torvalds's avatar
Linus Torvalds committed
206 207 208 209 210 211 212 213
{
	assert(!snd_mask_empty(mask));
	if (snd_mask_single(mask))
		return 0;
	snd_mask_leave(mask, snd_mask_min(mask));
	return 1;
}

214
INLINE int snd_mask_refine_last(struct snd_mask *mask)
Linus Torvalds's avatar
Linus Torvalds committed
215 216 217 218 219 220 221 222
{
	assert(!snd_mask_empty(mask));
	if (snd_mask_single(mask))
		return 0;
	snd_mask_leave(mask, snd_mask_max(mask));
	return 1;
}

223
INLINE int snd_mask_refine_min(struct snd_mask *mask, unsigned int val)
Linus Torvalds's avatar
Linus Torvalds committed
224 225 226 227 228 229 230 231 232 233
{
	assert(!snd_mask_empty(mask));
	if (snd_mask_min(mask) >= val)
		return 0;
	snd_mask_reset_range(mask, 0, val - 1);
	if (snd_mask_empty(mask))
		return -EINVAL;
	return 1;
}

234
INLINE int snd_mask_refine_max(struct snd_mask *mask, unsigned int val)
Linus Torvalds's avatar
Linus Torvalds committed
235 236 237 238 239 240 241 242 243 244
{
	assert(!snd_mask_empty(mask));
	if (snd_mask_max(mask) <= val)
		return 0;
	snd_mask_reset_range(mask, val + 1, SNDRV_MASK_BITS);
	if (snd_mask_empty(mask))
		return -EINVAL;
	return 1;
}

245
INLINE int snd_mask_refine_set(struct snd_mask *mask, unsigned int val)
Linus Torvalds's avatar
Linus Torvalds committed
246 247 248 249 250 251 252 253 254 255
{
	int changed;
	assert(!snd_mask_empty(mask));
	changed = !snd_mask_single(mask);
	snd_mask_leave(mask, val);
	if (snd_mask_empty(mask))
		return -EINVAL;
	return changed;
}

256
INLINE int snd_mask_value(const struct snd_mask *mask)
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260 261
{
	assert(!snd_mask_empty(mask));
	return snd_mask_min(mask);
}

262
INLINE void snd_interval_any(struct snd_interval *i)
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265 266 267 268 269 270 271
{
	i->min = 0;
	i->openmin = 0;
	i->max = UINT_MAX;
	i->openmax = 0;
	i->integer = 0;
	i->empty = 0;
}

272
INLINE void snd_interval_none(struct snd_interval *i)
Linus Torvalds's avatar
Linus Torvalds committed
273 274 275 276
{
	i->empty = 1;
}

277
INLINE int snd_interval_checkempty(const struct snd_interval *i)
Linus Torvalds's avatar
Linus Torvalds committed
278 279 280 281 282
{
	return (i->min > i->max ||
		(i->min == i->max && (i->openmin || i->openmax)));
}

283
INLINE int snd_interval_empty(const struct snd_interval *i)
Linus Torvalds's avatar
Linus Torvalds committed
284 285 286 287
{
	return i->empty;
}

288
INLINE int snd_interval_single(const struct snd_interval *i)
Linus Torvalds's avatar
Linus Torvalds committed
289 290 291 292 293 294
{
	assert(!snd_interval_empty(i));
	return (i->min == i->max || 
		(i->min + 1 == i->max && i->openmax));
}

295
INLINE int snd_interval_value(const struct snd_interval *i)
Linus Torvalds's avatar
Linus Torvalds committed
296 297 298 299 300
{
	assert(snd_interval_single(i));
	return i->min;
}

301
INLINE int snd_interval_min(const struct snd_interval *i)
Linus Torvalds's avatar
Linus Torvalds committed
302 303 304 305 306
{
	assert(!snd_interval_empty(i));
	return i->min;
}

307
INLINE int snd_interval_max(const struct snd_interval *i)
Linus Torvalds's avatar
Linus Torvalds committed
308 309 310 311 312 313 314 315 316
{
	unsigned int v;
	assert(!snd_interval_empty(i));
	v = i->max;
	if (i->openmax)
		v--;
	return v;
}

317
INLINE int snd_interval_test(const struct snd_interval *i, unsigned int val)
Linus Torvalds's avatar
Linus Torvalds committed
318 319 320 321 322
{
	return !((i->min > val || (i->min == val && i->openmin) ||
		  i->max < val || (i->max == val && i->openmax)));
}

323
INLINE void snd_interval_copy(struct snd_interval *d, const struct snd_interval *s)
Linus Torvalds's avatar
Linus Torvalds committed
324 325 326 327
{
	*d = *s;
}

328
INLINE int snd_interval_setinteger(struct snd_interval *i)
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332 333 334 335 336 337
{
	if (i->integer)
		return 0;
	if (i->openmin && i->openmax && i->min == i->max)
		return -EINVAL;
	i->integer = 1;
	return 1;
}

338
INLINE int snd_interval_eq(const struct snd_interval *i1, const struct snd_interval *i2)
Linus Torvalds's avatar
Linus Torvalds committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
{
	if (i1->empty)
		return i2->empty;
	if (i2->empty)
		return i1->empty;
	return i1->min == i2->min && i1->openmin == i2->openmin &&
		i1->max == i2->max && i1->openmax == i2->openmax;
}

static inline unsigned int add(unsigned int a, unsigned int b)
{
	if (a >= UINT_MAX - b)
		return UINT_MAX;
	return a + b;
}

static inline unsigned int sub(unsigned int a, unsigned int b)
{
	if (a > b)
		return a - b;
	return 0;
}

#undef INLINE
#undef assert

#endif /* __SOUND_PCM_PARAMS_H */