pcm_params.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #ifndef __SOUND_PCM_PARAMS_H
  2. #define __SOUND_PCM_PARAMS_H
  3. /*
  4. * PCM params helpers
  5. * Copyright (c) by Abramo Bagnara <[email protected]>
  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. static inline void snd_mask_reset(struct snd_mask *mask, unsigned int val)
  79. {
  80. mask->bits[MASK_OFS(val)] &= ~MASK_BIT(val);
  81. }
  82. static inline void snd_mask_set_range(struct snd_mask *mask,
  83. unsigned int from, unsigned int to)
  84. {
  85. unsigned int i;
  86. for (i = from; i <= to; i++)
  87. mask->bits[MASK_OFS(i)] |= MASK_BIT(i);
  88. }
  89. static inline void snd_mask_reset_range(struct snd_mask *mask,
  90. unsigned int from, unsigned int to)
  91. {
  92. unsigned int i;
  93. for (i = from; i <= to; i++)
  94. mask->bits[MASK_OFS(i)] &= ~MASK_BIT(i);
  95. }
  96. static inline void snd_mask_leave(struct snd_mask *mask, unsigned int val)
  97. {
  98. unsigned int v, bits_index;
  99. bits_index = MASK_OFS(val);
  100. if (bits_index < ((SNDRV_MASK_MAX+31)/32)) {
  101. v = mask->bits[bits_index] & MASK_BIT(val);
  102. snd_mask_none(mask);
  103. mask->bits[bits_index] = v;
  104. }
  105. }
  106. static inline void snd_mask_intersect(struct snd_mask *mask,
  107. const struct snd_mask *v)
  108. {
  109. int i;
  110. for (i = 0; i < SNDRV_MASK_SIZE; i++)
  111. mask->bits[i] &= v->bits[i];
  112. }
  113. static inline int snd_mask_eq(const struct snd_mask *mask,
  114. const struct snd_mask *v)
  115. {
  116. return ! memcmp(mask, v, SNDRV_MASK_SIZE * sizeof(u_int32_t));
  117. }
  118. static inline void snd_mask_copy(struct snd_mask *mask,
  119. const struct snd_mask *v)
  120. {
  121. *mask = *v;
  122. }
  123. static inline int snd_mask_test(const struct snd_mask *mask, unsigned int val)
  124. {
  125. return mask->bits[MASK_OFS(val)] & MASK_BIT(val);
  126. }
  127. static inline int snd_mask_single(const struct snd_mask *mask)
  128. {
  129. int i, c = 0;
  130. for (i = 0; i < SNDRV_MASK_SIZE; i++) {
  131. if (! mask->bits[i])
  132. continue;
  133. if (mask->bits[i] & (mask->bits[i] - 1))
  134. return 0;
  135. if (c)
  136. return 0;
  137. c++;
  138. }
  139. return 1;
  140. }
  141. static inline int snd_mask_refine(struct snd_mask *mask,
  142. const struct snd_mask *v)
  143. {
  144. struct snd_mask old;
  145. snd_mask_copy(&old, mask);
  146. snd_mask_intersect(mask, v);
  147. if (snd_mask_empty(mask))
  148. return -EINVAL;
  149. return !snd_mask_eq(mask, &old);
  150. }
  151. static inline int snd_mask_refine_first(struct snd_mask *mask)
  152. {
  153. if (snd_mask_single(mask))
  154. return 0;
  155. snd_mask_leave(mask, snd_mask_min(mask));
  156. return 1;
  157. }
  158. static inline int snd_mask_refine_last(struct snd_mask *mask)
  159. {
  160. if (snd_mask_single(mask))
  161. return 0;
  162. snd_mask_leave(mask, snd_mask_max(mask));
  163. return 1;
  164. }
  165. static inline int snd_mask_refine_min(struct snd_mask *mask, unsigned int val)
  166. {
  167. if (snd_mask_min(mask) >= val)
  168. return 0;
  169. snd_mask_reset_range(mask, 0, val - 1);
  170. if (snd_mask_empty(mask))
  171. return -EINVAL;
  172. return 1;
  173. }
  174. static inline int snd_mask_refine_max(struct snd_mask *mask, unsigned int val)
  175. {
  176. if (snd_mask_max(mask) <= val)
  177. return 0;
  178. snd_mask_reset_range(mask, val + 1, SNDRV_MASK_BITS);
  179. if (snd_mask_empty(mask))
  180. return -EINVAL;
  181. return 1;
  182. }
  183. static inline int snd_mask_refine_set(struct snd_mask *mask, unsigned int val)
  184. {
  185. int changed;
  186. changed = !snd_mask_single(mask);
  187. snd_mask_leave(mask, val);
  188. if (snd_mask_empty(mask))
  189. return -EINVAL;
  190. return changed;
  191. }
  192. static inline int snd_mask_value(const struct snd_mask *mask)
  193. {
  194. return snd_mask_min(mask);
  195. }
  196. static inline void snd_interval_any(struct snd_interval *i)
  197. {
  198. i->min = 0;
  199. i->openmin = 0;
  200. i->max = UINT_MAX;
  201. i->openmax = 0;
  202. i->integer = 0;
  203. i->empty = 0;
  204. }
  205. static inline void snd_interval_none(struct snd_interval *i)
  206. {
  207. i->empty = 1;
  208. }
  209. static inline int snd_interval_checkempty(const struct snd_interval *i)
  210. {
  211. return (i->min > i->max ||
  212. (i->min == i->max && (i->openmin || i->openmax)));
  213. }
  214. static inline int snd_interval_empty(const struct snd_interval *i)
  215. {
  216. return i->empty;
  217. }
  218. static inline int snd_interval_single(const struct snd_interval *i)
  219. {
  220. return (i->min == i->max ||
  221. (i->min + 1 == i->max && (i->openmin || i->openmax)));
  222. }
  223. static inline int snd_interval_value(const struct snd_interval *i)
  224. {
  225. if (i->openmin && !i->openmax)
  226. return i->max;
  227. return i->min;
  228. }
  229. static inline int snd_interval_min(const struct snd_interval *i)
  230. {
  231. return i->min;
  232. }
  233. static inline int snd_interval_max(const struct snd_interval *i)
  234. {
  235. unsigned int v;
  236. v = i->max;
  237. if (i->openmax)
  238. v--;
  239. return v;
  240. }
  241. static inline int snd_interval_test(const struct snd_interval *i, unsigned int val)
  242. {
  243. return !((i->min > val || (i->min == val && i->openmin) ||
  244. i->max < val || (i->max == val && i->openmax)));
  245. }
  246. static inline void snd_interval_copy(struct snd_interval *d, const struct snd_interval *s)
  247. {
  248. *d = *s;
  249. }
  250. static inline int snd_interval_setinteger(struct snd_interval *i)
  251. {
  252. if (i->integer)
  253. return 0;
  254. if (i->openmin && i->openmax && i->min == i->max)
  255. return -EINVAL;
  256. i->integer = 1;
  257. return 1;
  258. }
  259. static inline int snd_interval_eq(const struct snd_interval *i1, const struct snd_interval *i2)
  260. {
  261. if (i1->empty)
  262. return i2->empty;
  263. if (i2->empty)
  264. return i1->empty;
  265. return i1->min == i2->min && i1->openmin == i2->openmin &&
  266. i1->max == i2->max && i1->openmax == i2->openmax;
  267. }
  268. /**
  269. * params_access - get the access type from the hw params
  270. * @p: hw params
  271. */
  272. static inline snd_pcm_access_t params_access(const struct snd_pcm_hw_params *p)
  273. {
  274. return (__force snd_pcm_access_t)snd_mask_min(hw_param_mask_c(p,
  275. SNDRV_PCM_HW_PARAM_ACCESS));
  276. }
  277. /**
  278. * params_format - get the sample format from the hw params
  279. * @p: hw params
  280. */
  281. static inline snd_pcm_format_t params_format(const struct snd_pcm_hw_params *p)
  282. {
  283. return (__force snd_pcm_format_t)snd_mask_min(hw_param_mask_c(p,
  284. SNDRV_PCM_HW_PARAM_FORMAT));
  285. }
  286. /**
  287. * params_subformat - get the sample subformat from the hw params
  288. * @p: hw params
  289. */
  290. static inline snd_pcm_subformat_t
  291. params_subformat(const struct snd_pcm_hw_params *p)
  292. {
  293. return (__force snd_pcm_subformat_t)snd_mask_min(hw_param_mask_c(p,
  294. SNDRV_PCM_HW_PARAM_SUBFORMAT));
  295. }
  296. /**
  297. * params_period_bytes - get the period size (in bytes) from the hw params
  298. * @p: hw params
  299. */
  300. static inline unsigned int
  301. params_period_bytes(const struct snd_pcm_hw_params *p)
  302. {
  303. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_PERIOD_BYTES)->min;
  304. }
  305. /**
  306. * params_width - get the number of bits of the sample format from the hw params
  307. * @p: hw params
  308. *
  309. * This function returns the number of bits per sample that the selected sample
  310. * format of the hw params has.
  311. */
  312. static inline int params_width(const struct snd_pcm_hw_params *p)
  313. {
  314. return snd_pcm_format_width(params_format(p));
  315. }
  316. /*
  317. * params_physical_width - get the storage size of the sample format from the hw params
  318. * @p: hw params
  319. *
  320. * This functions returns the number of bits per sample that the selected sample
  321. * format of the hw params takes up in memory. This will be equal or larger than
  322. * params_width().
  323. */
  324. static inline int params_physical_width(const struct snd_pcm_hw_params *p)
  325. {
  326. return snd_pcm_format_physical_width(params_format(p));
  327. }
  328. static inline void
  329. params_set_format(struct snd_pcm_hw_params *p, snd_pcm_format_t fmt)
  330. {
  331. snd_mask_set(hw_param_mask(p, SNDRV_PCM_HW_PARAM_FORMAT),
  332. (__force int)fmt);
  333. }
  334. #endif /* __SOUND_PCM_PARAMS_H */