pcm_plugin.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*
  2. * PCM Plug-In shared (kernel/library) code
  3. * Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
  4. * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
  5. *
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Library General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #if 0
  23. #define PLUGIN_DEBUG
  24. #endif
  25. #include <sound/driver.h>
  26. #include <linux/slab.h>
  27. #include <linux/time.h>
  28. #include <linux/vmalloc.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include "pcm_plugin.h"
  33. #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
  34. #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
  35. static int snd_pcm_plugin_src_channels_mask(snd_pcm_plugin_t *plugin,
  36. bitset_t *dst_vmask,
  37. bitset_t **src_vmask)
  38. {
  39. bitset_t *vmask = plugin->src_vmask;
  40. bitset_copy(vmask, dst_vmask, plugin->src_format.channels);
  41. *src_vmask = vmask;
  42. return 0;
  43. }
  44. static int snd_pcm_plugin_dst_channels_mask(snd_pcm_plugin_t *plugin,
  45. bitset_t *src_vmask,
  46. bitset_t **dst_vmask)
  47. {
  48. bitset_t *vmask = plugin->dst_vmask;
  49. bitset_copy(vmask, src_vmask, plugin->dst_format.channels);
  50. *dst_vmask = vmask;
  51. return 0;
  52. }
  53. /*
  54. * because some cards might have rates "very close", we ignore
  55. * all "resampling" requests within +-5%
  56. */
  57. static int rate_match(unsigned int src_rate, unsigned int dst_rate)
  58. {
  59. unsigned int low = (src_rate * 95) / 100;
  60. unsigned int high = (src_rate * 105) / 100;
  61. return dst_rate >= low && dst_rate <= high;
  62. }
  63. static int snd_pcm_plugin_alloc(snd_pcm_plugin_t *plugin, snd_pcm_uframes_t frames)
  64. {
  65. snd_pcm_plugin_format_t *format;
  66. ssize_t width;
  67. size_t size;
  68. unsigned int channel;
  69. snd_pcm_plugin_channel_t *c;
  70. if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  71. format = &plugin->src_format;
  72. } else {
  73. format = &plugin->dst_format;
  74. }
  75. if ((width = snd_pcm_format_physical_width(format->format)) < 0)
  76. return width;
  77. size = frames * format->channels * width;
  78. snd_assert((size % 8) == 0, return -ENXIO);
  79. size /= 8;
  80. if (plugin->buf_frames < frames) {
  81. vfree(plugin->buf);
  82. plugin->buf = vmalloc(size);
  83. plugin->buf_frames = frames;
  84. }
  85. if (!plugin->buf) {
  86. plugin->buf_frames = 0;
  87. return -ENOMEM;
  88. }
  89. c = plugin->buf_channels;
  90. if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
  91. for (channel = 0; channel < format->channels; channel++, c++) {
  92. c->frames = frames;
  93. c->enabled = 1;
  94. c->wanted = 0;
  95. c->area.addr = plugin->buf;
  96. c->area.first = channel * width;
  97. c->area.step = format->channels * width;
  98. }
  99. } else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
  100. snd_assert((size % format->channels) == 0,);
  101. size /= format->channels;
  102. for (channel = 0; channel < format->channels; channel++, c++) {
  103. c->frames = frames;
  104. c->enabled = 1;
  105. c->wanted = 0;
  106. c->area.addr = plugin->buf + (channel * size);
  107. c->area.first = 0;
  108. c->area.step = width;
  109. }
  110. } else
  111. return -EINVAL;
  112. return 0;
  113. }
  114. int snd_pcm_plug_alloc(snd_pcm_plug_t *plug, snd_pcm_uframes_t frames)
  115. {
  116. int err;
  117. snd_assert(snd_pcm_plug_first(plug) != NULL, return -ENXIO);
  118. if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
  119. snd_pcm_plugin_t *plugin = snd_pcm_plug_first(plug);
  120. while (plugin->next) {
  121. if (plugin->dst_frames)
  122. frames = plugin->dst_frames(plugin, frames);
  123. snd_assert(frames > 0, return -ENXIO);
  124. plugin = plugin->next;
  125. err = snd_pcm_plugin_alloc(plugin, frames);
  126. if (err < 0)
  127. return err;
  128. }
  129. } else {
  130. snd_pcm_plugin_t *plugin = snd_pcm_plug_last(plug);
  131. while (plugin->prev) {
  132. if (plugin->src_frames)
  133. frames = plugin->src_frames(plugin, frames);
  134. snd_assert(frames > 0, return -ENXIO);
  135. plugin = plugin->prev;
  136. err = snd_pcm_plugin_alloc(plugin, frames);
  137. if (err < 0)
  138. return err;
  139. }
  140. }
  141. return 0;
  142. }
  143. snd_pcm_sframes_t snd_pcm_plugin_client_channels(snd_pcm_plugin_t *plugin,
  144. snd_pcm_uframes_t frames,
  145. snd_pcm_plugin_channel_t **channels)
  146. {
  147. *channels = plugin->buf_channels;
  148. return frames;
  149. }
  150. int snd_pcm_plugin_build(snd_pcm_plug_t *plug,
  151. const char *name,
  152. snd_pcm_plugin_format_t *src_format,
  153. snd_pcm_plugin_format_t *dst_format,
  154. size_t extra,
  155. snd_pcm_plugin_t **ret)
  156. {
  157. snd_pcm_plugin_t *plugin;
  158. unsigned int channels;
  159. snd_assert(plug != NULL, return -ENXIO);
  160. snd_assert(src_format != NULL && dst_format != NULL, return -ENXIO);
  161. plugin = kcalloc(1, sizeof(*plugin) + extra, GFP_KERNEL);
  162. if (plugin == NULL)
  163. return -ENOMEM;
  164. plugin->name = name;
  165. plugin->plug = plug;
  166. plugin->stream = snd_pcm_plug_stream(plug);
  167. plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  168. plugin->src_format = *src_format;
  169. plugin->src_width = snd_pcm_format_physical_width(src_format->format);
  170. snd_assert(plugin->src_width > 0, );
  171. plugin->dst_format = *dst_format;
  172. plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
  173. snd_assert(plugin->dst_width > 0, );
  174. if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
  175. channels = src_format->channels;
  176. else
  177. channels = dst_format->channels;
  178. plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL);
  179. if (plugin->buf_channels == NULL) {
  180. snd_pcm_plugin_free(plugin);
  181. return -ENOMEM;
  182. }
  183. plugin->src_vmask = bitset_alloc(src_format->channels);
  184. if (plugin->src_vmask == NULL) {
  185. snd_pcm_plugin_free(plugin);
  186. return -ENOMEM;
  187. }
  188. plugin->dst_vmask = bitset_alloc(dst_format->channels);
  189. if (plugin->dst_vmask == NULL) {
  190. snd_pcm_plugin_free(plugin);
  191. return -ENOMEM;
  192. }
  193. plugin->client_channels = snd_pcm_plugin_client_channels;
  194. plugin->src_channels_mask = snd_pcm_plugin_src_channels_mask;
  195. plugin->dst_channels_mask = snd_pcm_plugin_dst_channels_mask;
  196. *ret = plugin;
  197. return 0;
  198. }
  199. int snd_pcm_plugin_free(snd_pcm_plugin_t *plugin)
  200. {
  201. if (! plugin)
  202. return 0;
  203. if (plugin->private_free)
  204. plugin->private_free(plugin);
  205. kfree(plugin->buf_channels);
  206. vfree(plugin->buf);
  207. kfree(plugin->src_vmask);
  208. kfree(plugin->dst_vmask);
  209. kfree(plugin);
  210. return 0;
  211. }
  212. snd_pcm_sframes_t snd_pcm_plug_client_size(snd_pcm_plug_t *plug, snd_pcm_uframes_t drv_frames)
  213. {
  214. snd_pcm_plugin_t *plugin, *plugin_prev, *plugin_next;
  215. int stream = snd_pcm_plug_stream(plug);
  216. snd_assert(plug != NULL, return -ENXIO);
  217. if (drv_frames == 0)
  218. return 0;
  219. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  220. plugin = snd_pcm_plug_last(plug);
  221. while (plugin && drv_frames > 0) {
  222. plugin_prev = plugin->prev;
  223. if (plugin->src_frames)
  224. drv_frames = plugin->src_frames(plugin, drv_frames);
  225. plugin = plugin_prev;
  226. }
  227. } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  228. plugin = snd_pcm_plug_first(plug);
  229. while (plugin && drv_frames > 0) {
  230. plugin_next = plugin->next;
  231. if (plugin->dst_frames)
  232. drv_frames = plugin->dst_frames(plugin, drv_frames);
  233. plugin = plugin_next;
  234. }
  235. } else
  236. snd_BUG();
  237. return drv_frames;
  238. }
  239. snd_pcm_sframes_t snd_pcm_plug_slave_size(snd_pcm_plug_t *plug, snd_pcm_uframes_t clt_frames)
  240. {
  241. snd_pcm_plugin_t *plugin, *plugin_prev, *plugin_next;
  242. snd_pcm_sframes_t frames;
  243. int stream = snd_pcm_plug_stream(plug);
  244. snd_assert(plug != NULL, return -ENXIO);
  245. if (clt_frames == 0)
  246. return 0;
  247. frames = clt_frames;
  248. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  249. plugin = snd_pcm_plug_first(plug);
  250. while (plugin && frames > 0) {
  251. plugin_next = plugin->next;
  252. if (plugin->dst_frames) {
  253. frames = plugin->dst_frames(plugin, frames);
  254. if (frames < 0)
  255. return frames;
  256. }
  257. plugin = plugin_next;
  258. }
  259. } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  260. plugin = snd_pcm_plug_last(plug);
  261. while (plugin) {
  262. plugin_prev = plugin->prev;
  263. if (plugin->src_frames) {
  264. frames = plugin->src_frames(plugin, frames);
  265. if (frames < 0)
  266. return frames;
  267. }
  268. plugin = plugin_prev;
  269. }
  270. } else
  271. snd_BUG();
  272. return frames;
  273. }
  274. static int snd_pcm_plug_formats(snd_mask_t *mask, int format)
  275. {
  276. snd_mask_t formats = *mask;
  277. u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
  278. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
  279. SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  280. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
  281. SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
  282. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
  283. SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
  284. snd_mask_set(&formats, SNDRV_PCM_FORMAT_MU_LAW);
  285. if (formats.bits[0] & (u32)linfmts)
  286. formats.bits[0] |= (u32)linfmts;
  287. if (formats.bits[1] & (u32)(linfmts >> 32))
  288. formats.bits[1] |= (u32)(linfmts >> 32);
  289. return snd_mask_test(&formats, format);
  290. }
  291. static int preferred_formats[] = {
  292. SNDRV_PCM_FORMAT_S16_LE,
  293. SNDRV_PCM_FORMAT_S16_BE,
  294. SNDRV_PCM_FORMAT_U16_LE,
  295. SNDRV_PCM_FORMAT_U16_BE,
  296. SNDRV_PCM_FORMAT_S24_LE,
  297. SNDRV_PCM_FORMAT_S24_BE,
  298. SNDRV_PCM_FORMAT_U24_LE,
  299. SNDRV_PCM_FORMAT_U24_BE,
  300. SNDRV_PCM_FORMAT_S32_LE,
  301. SNDRV_PCM_FORMAT_S32_BE,
  302. SNDRV_PCM_FORMAT_U32_LE,
  303. SNDRV_PCM_FORMAT_U32_BE,
  304. SNDRV_PCM_FORMAT_S8,
  305. SNDRV_PCM_FORMAT_U8
  306. };
  307. int snd_pcm_plug_slave_format(int format, snd_mask_t *format_mask)
  308. {
  309. if (snd_mask_test(format_mask, format))
  310. return format;
  311. if (! snd_pcm_plug_formats(format_mask, format))
  312. return -EINVAL;
  313. if (snd_pcm_format_linear(format)) {
  314. int width = snd_pcm_format_width(format);
  315. int unsignd = snd_pcm_format_unsigned(format);
  316. int big = snd_pcm_format_big_endian(format);
  317. int format1;
  318. int wid, width1=width;
  319. int dwidth1 = 8;
  320. for (wid = 0; wid < 4; ++wid) {
  321. int end, big1 = big;
  322. for (end = 0; end < 2; ++end) {
  323. int sgn, unsignd1 = unsignd;
  324. for (sgn = 0; sgn < 2; ++sgn) {
  325. format1 = snd_pcm_build_linear_format(width1, unsignd1, big1);
  326. if (format1 >= 0 &&
  327. snd_mask_test(format_mask, format1))
  328. goto _found;
  329. unsignd1 = !unsignd1;
  330. }
  331. big1 = !big1;
  332. }
  333. if (width1 == 32) {
  334. dwidth1 = -dwidth1;
  335. width1 = width;
  336. }
  337. width1 += dwidth1;
  338. }
  339. return -EINVAL;
  340. _found:
  341. return format1;
  342. } else {
  343. unsigned int i;
  344. switch (format) {
  345. case SNDRV_PCM_FORMAT_MU_LAW:
  346. for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
  347. int format1 = preferred_formats[i];
  348. if (snd_mask_test(format_mask, format1))
  349. return format1;
  350. }
  351. default:
  352. return -EINVAL;
  353. }
  354. }
  355. }
  356. int snd_pcm_plug_format_plugins(snd_pcm_plug_t *plug,
  357. snd_pcm_hw_params_t *params,
  358. snd_pcm_hw_params_t *slave_params)
  359. {
  360. snd_pcm_plugin_format_t tmpformat;
  361. snd_pcm_plugin_format_t dstformat;
  362. snd_pcm_plugin_format_t srcformat;
  363. int src_access, dst_access;
  364. snd_pcm_plugin_t *plugin = NULL;
  365. int err;
  366. int stream = snd_pcm_plug_stream(plug);
  367. int slave_interleaved = (params_channels(slave_params) == 1 ||
  368. params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
  369. switch (stream) {
  370. case SNDRV_PCM_STREAM_PLAYBACK:
  371. dstformat.format = params_format(slave_params);
  372. dstformat.rate = params_rate(slave_params);
  373. dstformat.channels = params_channels(slave_params);
  374. srcformat.format = params_format(params);
  375. srcformat.rate = params_rate(params);
  376. srcformat.channels = params_channels(params);
  377. src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  378. dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
  379. SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
  380. break;
  381. case SNDRV_PCM_STREAM_CAPTURE:
  382. dstformat.format = params_format(params);
  383. dstformat.rate = params_rate(params);
  384. dstformat.channels = params_channels(params);
  385. srcformat.format = params_format(slave_params);
  386. srcformat.rate = params_rate(slave_params);
  387. srcformat.channels = params_channels(slave_params);
  388. src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
  389. SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
  390. dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  391. break;
  392. default:
  393. snd_BUG();
  394. return -EINVAL;
  395. }
  396. tmpformat = srcformat;
  397. pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
  398. srcformat.format,
  399. srcformat.rate,
  400. srcformat.channels);
  401. pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
  402. dstformat.format,
  403. dstformat.rate,
  404. dstformat.channels);
  405. /* Format change (linearization) */
  406. if ((srcformat.format != dstformat.format ||
  407. !rate_match(srcformat.rate, dstformat.rate) ||
  408. srcformat.channels != dstformat.channels) &&
  409. !snd_pcm_format_linear(srcformat.format)) {
  410. if (snd_pcm_format_linear(dstformat.format))
  411. tmpformat.format = dstformat.format;
  412. else
  413. tmpformat.format = SNDRV_PCM_FORMAT_S16;
  414. switch (srcformat.format) {
  415. case SNDRV_PCM_FORMAT_MU_LAW:
  416. err = snd_pcm_plugin_build_mulaw(plug,
  417. &srcformat, &tmpformat,
  418. &plugin);
  419. break;
  420. default:
  421. return -EINVAL;
  422. }
  423. pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
  424. if (err < 0)
  425. return err;
  426. err = snd_pcm_plugin_append(plugin);
  427. if (err < 0) {
  428. snd_pcm_plugin_free(plugin);
  429. return err;
  430. }
  431. srcformat = tmpformat;
  432. src_access = dst_access;
  433. }
  434. /* channels reduction */
  435. if (srcformat.channels > dstformat.channels) {
  436. int sv = srcformat.channels;
  437. int dv = dstformat.channels;
  438. route_ttable_entry_t *ttable = kcalloc(dv * sv, sizeof(*ttable), GFP_KERNEL);
  439. if (ttable == NULL)
  440. return -ENOMEM;
  441. #if 1
  442. if (sv == 2 && dv == 1) {
  443. ttable[0] = HALF;
  444. ttable[1] = HALF;
  445. } else
  446. #endif
  447. {
  448. int v;
  449. for (v = 0; v < dv; ++v)
  450. ttable[v * sv + v] = FULL;
  451. }
  452. tmpformat.channels = dstformat.channels;
  453. if (rate_match(srcformat.rate, dstformat.rate) &&
  454. snd_pcm_format_linear(dstformat.format))
  455. tmpformat.format = dstformat.format;
  456. err = snd_pcm_plugin_build_route(plug,
  457. &srcformat, &tmpformat,
  458. ttable, &plugin);
  459. kfree(ttable);
  460. pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
  461. if (err < 0) {
  462. snd_pcm_plugin_free(plugin);
  463. return err;
  464. }
  465. err = snd_pcm_plugin_append(plugin);
  466. if (err < 0) {
  467. snd_pcm_plugin_free(plugin);
  468. return err;
  469. }
  470. srcformat = tmpformat;
  471. src_access = dst_access;
  472. }
  473. /* rate resampling */
  474. if (!rate_match(srcformat.rate, dstformat.rate)) {
  475. tmpformat.rate = dstformat.rate;
  476. if (srcformat.channels == dstformat.channels &&
  477. snd_pcm_format_linear(dstformat.format))
  478. tmpformat.format = dstformat.format;
  479. err = snd_pcm_plugin_build_rate(plug,
  480. &srcformat, &tmpformat,
  481. &plugin);
  482. pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
  483. if (err < 0) {
  484. snd_pcm_plugin_free(plugin);
  485. return err;
  486. }
  487. err = snd_pcm_plugin_append(plugin);
  488. if (err < 0) {
  489. snd_pcm_plugin_free(plugin);
  490. return err;
  491. }
  492. srcformat = tmpformat;
  493. src_access = dst_access;
  494. }
  495. /* channels extension */
  496. if (srcformat.channels < dstformat.channels) {
  497. int sv = srcformat.channels;
  498. int dv = dstformat.channels;
  499. route_ttable_entry_t *ttable = kcalloc(dv * sv, sizeof(*ttable), GFP_KERNEL);
  500. if (ttable == NULL)
  501. return -ENOMEM;
  502. #if 0
  503. {
  504. int v;
  505. for (v = 0; v < sv; ++v)
  506. ttable[v * sv + v] = FULL;
  507. }
  508. #else
  509. {
  510. /* Playback is spreaded on all channels */
  511. int vd, vs;
  512. for (vd = 0, vs = 0; vd < dv; ++vd) {
  513. ttable[vd * sv + vs] = FULL;
  514. vs++;
  515. if (vs == sv)
  516. vs = 0;
  517. }
  518. }
  519. #endif
  520. tmpformat.channels = dstformat.channels;
  521. if (snd_pcm_format_linear(dstformat.format))
  522. tmpformat.format = dstformat.format;
  523. err = snd_pcm_plugin_build_route(plug,
  524. &srcformat, &tmpformat,
  525. ttable, &plugin);
  526. kfree(ttable);
  527. pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
  528. if (err < 0) {
  529. snd_pcm_plugin_free(plugin);
  530. return err;
  531. }
  532. err = snd_pcm_plugin_append(plugin);
  533. if (err < 0) {
  534. snd_pcm_plugin_free(plugin);
  535. return err;
  536. }
  537. srcformat = tmpformat;
  538. src_access = dst_access;
  539. }
  540. /* format change */
  541. if (srcformat.format != dstformat.format) {
  542. tmpformat.format = dstformat.format;
  543. if (tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
  544. err = snd_pcm_plugin_build_mulaw(plug,
  545. &srcformat, &tmpformat,
  546. &plugin);
  547. }
  548. else if (snd_pcm_format_linear(srcformat.format) &&
  549. snd_pcm_format_linear(tmpformat.format)) {
  550. err = snd_pcm_plugin_build_linear(plug,
  551. &srcformat, &tmpformat,
  552. &plugin);
  553. }
  554. else
  555. return -EINVAL;
  556. pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
  557. if (err < 0)
  558. return err;
  559. err = snd_pcm_plugin_append(plugin);
  560. if (err < 0) {
  561. snd_pcm_plugin_free(plugin);
  562. return err;
  563. }
  564. srcformat = tmpformat;
  565. src_access = dst_access;
  566. }
  567. /* de-interleave */
  568. if (src_access != dst_access) {
  569. err = snd_pcm_plugin_build_copy(plug,
  570. &srcformat,
  571. &tmpformat,
  572. &plugin);
  573. pdprintf("interleave change (copy: returns %i)\n", err);
  574. if (err < 0)
  575. return err;
  576. err = snd_pcm_plugin_append(plugin);
  577. if (err < 0) {
  578. snd_pcm_plugin_free(plugin);
  579. return err;
  580. }
  581. }
  582. return 0;
  583. }
  584. snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(snd_pcm_plug_t *plug,
  585. char *buf,
  586. snd_pcm_uframes_t count,
  587. snd_pcm_plugin_channel_t **channels)
  588. {
  589. snd_pcm_plugin_t *plugin;
  590. snd_pcm_plugin_channel_t *v;
  591. snd_pcm_plugin_format_t *format;
  592. int width, nchannels, channel;
  593. int stream = snd_pcm_plug_stream(plug);
  594. snd_assert(buf != NULL, return -ENXIO);
  595. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  596. plugin = snd_pcm_plug_first(plug);
  597. format = &plugin->src_format;
  598. } else {
  599. plugin = snd_pcm_plug_last(plug);
  600. format = &plugin->dst_format;
  601. }
  602. v = plugin->buf_channels;
  603. *channels = v;
  604. if ((width = snd_pcm_format_physical_width(format->format)) < 0)
  605. return width;
  606. nchannels = format->channels;
  607. snd_assert(plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || format->channels <= 1, return -ENXIO);
  608. for (channel = 0; channel < nchannels; channel++, v++) {
  609. v->frames = count;
  610. v->enabled = 1;
  611. v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
  612. v->area.addr = buf;
  613. v->area.first = channel * width;
  614. v->area.step = nchannels * width;
  615. }
  616. return count;
  617. }
  618. static int snd_pcm_plug_playback_channels_mask(snd_pcm_plug_t *plug,
  619. bitset_t *client_vmask)
  620. {
  621. snd_pcm_plugin_t *plugin = snd_pcm_plug_last(plug);
  622. if (plugin == NULL) {
  623. return 0;
  624. } else {
  625. int schannels = plugin->dst_format.channels;
  626. bitset_t bs[bitset_size(schannels)];
  627. bitset_t *srcmask;
  628. bitset_t *dstmask = bs;
  629. int err;
  630. bitset_one(dstmask, schannels);
  631. if (plugin == NULL) {
  632. bitset_and(client_vmask, dstmask, schannels);
  633. return 0;
  634. }
  635. while (1) {
  636. err = plugin->src_channels_mask(plugin, dstmask, &srcmask);
  637. if (err < 0)
  638. return err;
  639. dstmask = srcmask;
  640. if (plugin->prev == NULL)
  641. break;
  642. plugin = plugin->prev;
  643. }
  644. bitset_and(client_vmask, dstmask, plugin->src_format.channels);
  645. return 0;
  646. }
  647. }
  648. static int snd_pcm_plug_playback_disable_useless_channels(snd_pcm_plug_t *plug,
  649. snd_pcm_plugin_channel_t *src_channels)
  650. {
  651. snd_pcm_plugin_t *plugin = snd_pcm_plug_first(plug);
  652. unsigned int nchannels = plugin->src_format.channels;
  653. bitset_t bs[bitset_size(nchannels)];
  654. bitset_t *srcmask = bs;
  655. int err;
  656. unsigned int channel;
  657. for (channel = 0; channel < nchannels; channel++) {
  658. if (src_channels[channel].enabled)
  659. bitset_set(srcmask, channel);
  660. else
  661. bitset_reset(srcmask, channel);
  662. }
  663. err = snd_pcm_plug_playback_channels_mask(plug, srcmask);
  664. if (err < 0)
  665. return err;
  666. for (channel = 0; channel < nchannels; channel++) {
  667. if (!bitset_get(srcmask, channel))
  668. src_channels[channel].enabled = 0;
  669. }
  670. return 0;
  671. }
  672. static int snd_pcm_plug_capture_disable_useless_channels(snd_pcm_plug_t *plug,
  673. snd_pcm_plugin_channel_t *src_channels,
  674. snd_pcm_plugin_channel_t *client_channels)
  675. {
  676. snd_pcm_plugin_t *plugin = snd_pcm_plug_last(plug);
  677. unsigned int nchannels = plugin->dst_format.channels;
  678. bitset_t bs[bitset_size(nchannels)];
  679. bitset_t *dstmask = bs;
  680. bitset_t *srcmask;
  681. int err;
  682. unsigned int channel;
  683. for (channel = 0; channel < nchannels; channel++) {
  684. if (client_channels[channel].enabled)
  685. bitset_set(dstmask, channel);
  686. else
  687. bitset_reset(dstmask, channel);
  688. }
  689. while (plugin) {
  690. err = plugin->src_channels_mask(plugin, dstmask, &srcmask);
  691. if (err < 0)
  692. return err;
  693. dstmask = srcmask;
  694. plugin = plugin->prev;
  695. }
  696. plugin = snd_pcm_plug_first(plug);
  697. nchannels = plugin->src_format.channels;
  698. for (channel = 0; channel < nchannels; channel++) {
  699. if (!bitset_get(dstmask, channel))
  700. src_channels[channel].enabled = 0;
  701. }
  702. return 0;
  703. }
  704. snd_pcm_sframes_t snd_pcm_plug_write_transfer(snd_pcm_plug_t *plug, snd_pcm_plugin_channel_t *src_channels, snd_pcm_uframes_t size)
  705. {
  706. snd_pcm_plugin_t *plugin, *next;
  707. snd_pcm_plugin_channel_t *dst_channels;
  708. int err;
  709. snd_pcm_sframes_t frames = size;
  710. if ((err = snd_pcm_plug_playback_disable_useless_channels(plug, src_channels)) < 0)
  711. return err;
  712. plugin = snd_pcm_plug_first(plug);
  713. while (plugin && frames > 0) {
  714. if ((next = plugin->next) != NULL) {
  715. snd_pcm_sframes_t frames1 = frames;
  716. if (plugin->dst_frames)
  717. frames1 = plugin->dst_frames(plugin, frames);
  718. if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
  719. return err;
  720. }
  721. if (err != frames1) {
  722. frames = err;
  723. if (plugin->src_frames)
  724. frames = plugin->src_frames(plugin, frames1);
  725. }
  726. } else
  727. dst_channels = NULL;
  728. pdprintf("write plugin: %s, %li\n", plugin->name, frames);
  729. if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
  730. return frames;
  731. src_channels = dst_channels;
  732. plugin = next;
  733. }
  734. return snd_pcm_plug_client_size(plug, frames);
  735. }
  736. snd_pcm_sframes_t snd_pcm_plug_read_transfer(snd_pcm_plug_t *plug, snd_pcm_plugin_channel_t *dst_channels_final, snd_pcm_uframes_t size)
  737. {
  738. snd_pcm_plugin_t *plugin, *next;
  739. snd_pcm_plugin_channel_t *src_channels, *dst_channels;
  740. snd_pcm_sframes_t frames = size;
  741. int err;
  742. frames = snd_pcm_plug_slave_size(plug, frames);
  743. if (frames < 0)
  744. return frames;
  745. src_channels = NULL;
  746. plugin = snd_pcm_plug_first(plug);
  747. while (plugin && frames > 0) {
  748. if ((next = plugin->next) != NULL) {
  749. if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
  750. return err;
  751. }
  752. frames = err;
  753. if (!plugin->prev) {
  754. if ((err = snd_pcm_plug_capture_disable_useless_channels(plug, dst_channels, dst_channels_final)) < 0)
  755. return err;
  756. }
  757. } else {
  758. dst_channels = dst_channels_final;
  759. }
  760. pdprintf("read plugin: %s, %li\n", plugin->name, frames);
  761. if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
  762. return frames;
  763. plugin = next;
  764. src_channels = dst_channels;
  765. }
  766. return frames;
  767. }
  768. int snd_pcm_area_silence(const snd_pcm_channel_area_t *dst_area, size_t dst_offset,
  769. size_t samples, int format)
  770. {
  771. /* FIXME: sub byte resolution and odd dst_offset */
  772. unsigned char *dst;
  773. unsigned int dst_step;
  774. int width;
  775. const unsigned char *silence;
  776. if (!dst_area->addr)
  777. return 0;
  778. dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
  779. width = snd_pcm_format_physical_width(format);
  780. if (width <= 0)
  781. return -EINVAL;
  782. if (dst_area->step == (unsigned int) width && width >= 8)
  783. return snd_pcm_format_set_silence(format, dst, samples);
  784. silence = snd_pcm_format_silence_64(format);
  785. if (! silence)
  786. return -EINVAL;
  787. dst_step = dst_area->step / 8;
  788. if (width == 4) {
  789. /* Ima ADPCM */
  790. int dstbit = dst_area->first % 8;
  791. int dstbit_step = dst_area->step % 8;
  792. while (samples-- > 0) {
  793. if (dstbit)
  794. *dst &= 0xf0;
  795. else
  796. *dst &= 0x0f;
  797. dst += dst_step;
  798. dstbit += dstbit_step;
  799. if (dstbit == 8) {
  800. dst++;
  801. dstbit = 0;
  802. }
  803. }
  804. } else {
  805. width /= 8;
  806. while (samples-- > 0) {
  807. memcpy(dst, silence, width);
  808. dst += dst_step;
  809. }
  810. }
  811. return 0;
  812. }
  813. int snd_pcm_area_copy(const snd_pcm_channel_area_t *src_area, size_t src_offset,
  814. const snd_pcm_channel_area_t *dst_area, size_t dst_offset,
  815. size_t samples, int format)
  816. {
  817. /* FIXME: sub byte resolution and odd dst_offset */
  818. char *src, *dst;
  819. int width;
  820. int src_step, dst_step;
  821. src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
  822. if (!src_area->addr)
  823. return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
  824. dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
  825. if (!dst_area->addr)
  826. return 0;
  827. width = snd_pcm_format_physical_width(format);
  828. if (width <= 0)
  829. return -EINVAL;
  830. if (src_area->step == (unsigned int) width &&
  831. dst_area->step == (unsigned int) width && width >= 8) {
  832. size_t bytes = samples * width / 8;
  833. memcpy(dst, src, bytes);
  834. return 0;
  835. }
  836. src_step = src_area->step / 8;
  837. dst_step = dst_area->step / 8;
  838. if (width == 4) {
  839. /* Ima ADPCM */
  840. int srcbit = src_area->first % 8;
  841. int srcbit_step = src_area->step % 8;
  842. int dstbit = dst_area->first % 8;
  843. int dstbit_step = dst_area->step % 8;
  844. while (samples-- > 0) {
  845. unsigned char srcval;
  846. if (srcbit)
  847. srcval = *src & 0x0f;
  848. else
  849. srcval = (*src & 0xf0) >> 4;
  850. if (dstbit)
  851. *dst = (*dst & 0xf0) | srcval;
  852. else
  853. *dst = (*dst & 0x0f) | (srcval << 4);
  854. src += src_step;
  855. srcbit += srcbit_step;
  856. if (srcbit == 8) {
  857. src++;
  858. srcbit = 0;
  859. }
  860. dst += dst_step;
  861. dstbit += dstbit_step;
  862. if (dstbit == 8) {
  863. dst++;
  864. dstbit = 0;
  865. }
  866. }
  867. } else {
  868. width /= 8;
  869. while (samples-- > 0) {
  870. memcpy(dst, src, width);
  871. src += src_step;
  872. dst += dst_step;
  873. }
  874. }
  875. return 0;
  876. }