pcm_params.h 9.0 KB

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