pcm_plugin.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  27. #include <linux/slab.h>
  28. #include <linux/time.h>
  29. #include <linux/vmalloc.h>
  30. #include <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include "pcm_plugin.h"
  34. #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
  35. #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
  36. /*
  37. * because some cards might have rates "very close", we ignore
  38. * all "resampling" requests within +-5%
  39. */
  40. static int rate_match(unsigned int src_rate, unsigned int dst_rate)
  41. {
  42. unsigned int low = (src_rate * 95) / 100;
  43. unsigned int high = (src_rate * 105) / 100;
  44. return dst_rate >= low && dst_rate <= high;
  45. }
  46. static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
  47. {
  48. struct snd_pcm_plugin_format *format;
  49. ssize_t width;
  50. size_t size;
  51. unsigned int channel;
  52. struct snd_pcm_plugin_channel *c;
  53. if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  54. format = &plugin->src_format;
  55. } else {
  56. format = &plugin->dst_format;
  57. }
  58. if ((width = snd_pcm_format_physical_width(format->format)) < 0)
  59. return width;
  60. size = frames * format->channels * width;
  61. snd_assert((size % 8) == 0, return -ENXIO);
  62. size /= 8;
  63. if (plugin->buf_frames < frames) {
  64. vfree(plugin->buf);
  65. plugin->buf = vmalloc(size);
  66. plugin->buf_frames = frames;
  67. }
  68. if (!plugin->buf) {
  69. plugin->buf_frames = 0;
  70. return -ENOMEM;
  71. }
  72. c = plugin->buf_channels;
  73. if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
  74. for (channel = 0; channel < format->channels; channel++, c++) {
  75. c->frames = frames;
  76. c->enabled = 1;
  77. c->wanted = 0;
  78. c->area.addr = plugin->buf;
  79. c->area.first = channel * width;
  80. c->area.step = format->channels * width;
  81. }
  82. } else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
  83. snd_assert((size % format->channels) == 0,);
  84. size /= format->channels;
  85. for (channel = 0; channel < format->channels; channel++, c++) {
  86. c->frames = frames;
  87. c->enabled = 1;
  88. c->wanted = 0;
  89. c->area.addr = plugin->buf + (channel * size);
  90. c->area.first = 0;
  91. c->area.step = width;
  92. }
  93. } else
  94. return -EINVAL;
  95. return 0;
  96. }
  97. int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
  98. {
  99. int err;
  100. snd_assert(snd_pcm_plug_first(plug) != NULL, return -ENXIO);
  101. if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
  102. struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug);
  103. while (plugin->next) {
  104. if (plugin->dst_frames)
  105. frames = plugin->dst_frames(plugin, frames);
  106. snd_assert(frames > 0, return -ENXIO);
  107. plugin = plugin->next;
  108. err = snd_pcm_plugin_alloc(plugin, frames);
  109. if (err < 0)
  110. return err;
  111. }
  112. } else {
  113. struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug);
  114. while (plugin->prev) {
  115. if (plugin->src_frames)
  116. frames = plugin->src_frames(plugin, frames);
  117. snd_assert(frames > 0, return -ENXIO);
  118. plugin = plugin->prev;
  119. err = snd_pcm_plugin_alloc(plugin, frames);
  120. if (err < 0)
  121. return err;
  122. }
  123. }
  124. return 0;
  125. }
  126. snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin,
  127. snd_pcm_uframes_t frames,
  128. struct snd_pcm_plugin_channel **channels)
  129. {
  130. *channels = plugin->buf_channels;
  131. return frames;
  132. }
  133. int snd_pcm_plugin_build(struct snd_pcm_substream *plug,
  134. const char *name,
  135. struct snd_pcm_plugin_format *src_format,
  136. struct snd_pcm_plugin_format *dst_format,
  137. size_t extra,
  138. struct snd_pcm_plugin **ret)
  139. {
  140. struct snd_pcm_plugin *plugin;
  141. unsigned int channels;
  142. snd_assert(plug != NULL, return -ENXIO);
  143. snd_assert(src_format != NULL && dst_format != NULL, return -ENXIO);
  144. plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL);
  145. if (plugin == NULL)
  146. return -ENOMEM;
  147. plugin->name = name;
  148. plugin->plug = plug;
  149. plugin->stream = snd_pcm_plug_stream(plug);
  150. plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  151. plugin->src_format = *src_format;
  152. plugin->src_width = snd_pcm_format_physical_width(src_format->format);
  153. snd_assert(plugin->src_width > 0, );
  154. plugin->dst_format = *dst_format;
  155. plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
  156. snd_assert(plugin->dst_width > 0, );
  157. if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
  158. channels = src_format->channels;
  159. else
  160. channels = dst_format->channels;
  161. plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL);
  162. if (plugin->buf_channels == NULL) {
  163. snd_pcm_plugin_free(plugin);
  164. return -ENOMEM;
  165. }
  166. plugin->client_channels = snd_pcm_plugin_client_channels;
  167. *ret = plugin;
  168. return 0;
  169. }
  170. int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
  171. {
  172. if (! plugin)
  173. return 0;
  174. if (plugin->private_free)
  175. plugin->private_free(plugin);
  176. kfree(plugin->buf_channels);
  177. vfree(plugin->buf);
  178. kfree(plugin);
  179. return 0;
  180. }
  181. snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
  182. {
  183. struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
  184. int stream = snd_pcm_plug_stream(plug);
  185. snd_assert(plug != NULL, return -ENXIO);
  186. if (drv_frames == 0)
  187. return 0;
  188. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  189. plugin = snd_pcm_plug_last(plug);
  190. while (plugin && drv_frames > 0) {
  191. plugin_prev = plugin->prev;
  192. if (plugin->src_frames)
  193. drv_frames = plugin->src_frames(plugin, drv_frames);
  194. plugin = plugin_prev;
  195. }
  196. } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  197. plugin = snd_pcm_plug_first(plug);
  198. while (plugin && drv_frames > 0) {
  199. plugin_next = plugin->next;
  200. if (plugin->dst_frames)
  201. drv_frames = plugin->dst_frames(plugin, drv_frames);
  202. plugin = plugin_next;
  203. }
  204. } else
  205. snd_BUG();
  206. return drv_frames;
  207. }
  208. snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
  209. {
  210. struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
  211. snd_pcm_sframes_t frames;
  212. int stream = snd_pcm_plug_stream(plug);
  213. snd_assert(plug != NULL, return -ENXIO);
  214. if (clt_frames == 0)
  215. return 0;
  216. frames = clt_frames;
  217. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  218. plugin = snd_pcm_plug_first(plug);
  219. while (plugin && frames > 0) {
  220. plugin_next = plugin->next;
  221. if (plugin->dst_frames) {
  222. frames = plugin->dst_frames(plugin, frames);
  223. if (frames < 0)
  224. return frames;
  225. }
  226. plugin = plugin_next;
  227. }
  228. } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  229. plugin = snd_pcm_plug_last(plug);
  230. while (plugin) {
  231. plugin_prev = plugin->prev;
  232. if (plugin->src_frames) {
  233. frames = plugin->src_frames(plugin, frames);
  234. if (frames < 0)
  235. return frames;
  236. }
  237. plugin = plugin_prev;
  238. }
  239. } else
  240. snd_BUG();
  241. return frames;
  242. }
  243. static int snd_pcm_plug_formats(struct snd_mask *mask, int format)
  244. {
  245. struct snd_mask formats = *mask;
  246. u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
  247. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
  248. SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  249. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
  250. SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
  251. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
  252. SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
  253. snd_mask_set(&formats, SNDRV_PCM_FORMAT_MU_LAW);
  254. if (formats.bits[0] & (u32)linfmts)
  255. formats.bits[0] |= (u32)linfmts;
  256. if (formats.bits[1] & (u32)(linfmts >> 32))
  257. formats.bits[1] |= (u32)(linfmts >> 32);
  258. return snd_mask_test(&formats, format);
  259. }
  260. static int preferred_formats[] = {
  261. SNDRV_PCM_FORMAT_S16_LE,
  262. SNDRV_PCM_FORMAT_S16_BE,
  263. SNDRV_PCM_FORMAT_U16_LE,
  264. SNDRV_PCM_FORMAT_U16_BE,
  265. SNDRV_PCM_FORMAT_S24_LE,
  266. SNDRV_PCM_FORMAT_S24_BE,
  267. SNDRV_PCM_FORMAT_U24_LE,
  268. SNDRV_PCM_FORMAT_U24_BE,
  269. SNDRV_PCM_FORMAT_S32_LE,
  270. SNDRV_PCM_FORMAT_S32_BE,
  271. SNDRV_PCM_FORMAT_U32_LE,
  272. SNDRV_PCM_FORMAT_U32_BE,
  273. SNDRV_PCM_FORMAT_S8,
  274. SNDRV_PCM_FORMAT_U8
  275. };
  276. int snd_pcm_plug_slave_format(int format, struct snd_mask *format_mask)
  277. {
  278. if (snd_mask_test(format_mask, format))
  279. return format;
  280. if (! snd_pcm_plug_formats(format_mask, format))
  281. return -EINVAL;
  282. if (snd_pcm_format_linear(format)) {
  283. int width = snd_pcm_format_width(format);
  284. int unsignd = snd_pcm_format_unsigned(format);
  285. int big = snd_pcm_format_big_endian(format);
  286. int format1;
  287. int wid, width1=width;
  288. int dwidth1 = 8;
  289. for (wid = 0; wid < 4; ++wid) {
  290. int end, big1 = big;
  291. for (end = 0; end < 2; ++end) {
  292. int sgn, unsignd1 = unsignd;
  293. for (sgn = 0; sgn < 2; ++sgn) {
  294. format1 = snd_pcm_build_linear_format(width1, unsignd1, big1);
  295. if (format1 >= 0 &&
  296. snd_mask_test(format_mask, format1))
  297. goto _found;
  298. unsignd1 = !unsignd1;
  299. }
  300. big1 = !big1;
  301. }
  302. if (width1 == 32) {
  303. dwidth1 = -dwidth1;
  304. width1 = width;
  305. }
  306. width1 += dwidth1;
  307. }
  308. return -EINVAL;
  309. _found:
  310. return format1;
  311. } else {
  312. unsigned int i;
  313. switch (format) {
  314. case SNDRV_PCM_FORMAT_MU_LAW:
  315. for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
  316. int format1 = preferred_formats[i];
  317. if (snd_mask_test(format_mask, format1))
  318. return format1;
  319. }
  320. default:
  321. return -EINVAL;
  322. }
  323. }
  324. }
  325. int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
  326. struct snd_pcm_hw_params *params,
  327. struct snd_pcm_hw_params *slave_params)
  328. {
  329. struct snd_pcm_plugin_format tmpformat;
  330. struct snd_pcm_plugin_format dstformat;
  331. struct snd_pcm_plugin_format srcformat;
  332. int src_access, dst_access;
  333. struct snd_pcm_plugin *plugin = NULL;
  334. int err;
  335. int stream = snd_pcm_plug_stream(plug);
  336. int slave_interleaved = (params_channels(slave_params) == 1 ||
  337. params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
  338. switch (stream) {
  339. case SNDRV_PCM_STREAM_PLAYBACK:
  340. dstformat.format = params_format(slave_params);
  341. dstformat.rate = params_rate(slave_params);
  342. dstformat.channels = params_channels(slave_params);
  343. srcformat.format = params_format(params);
  344. srcformat.rate = params_rate(params);
  345. srcformat.channels = params_channels(params);
  346. src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  347. dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
  348. SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
  349. break;
  350. case SNDRV_PCM_STREAM_CAPTURE:
  351. dstformat.format = params_format(params);
  352. dstformat.rate = params_rate(params);
  353. dstformat.channels = params_channels(params);
  354. srcformat.format = params_format(slave_params);
  355. srcformat.rate = params_rate(slave_params);
  356. srcformat.channels = params_channels(slave_params);
  357. src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
  358. SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
  359. dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  360. break;
  361. default:
  362. snd_BUG();
  363. return -EINVAL;
  364. }
  365. tmpformat = srcformat;
  366. pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
  367. srcformat.format,
  368. srcformat.rate,
  369. srcformat.channels);
  370. pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
  371. dstformat.format,
  372. dstformat.rate,
  373. dstformat.channels);
  374. /* Format change (linearization) */
  375. if (! rate_match(srcformat.rate, dstformat.rate) &&
  376. ! snd_pcm_format_linear(srcformat.format)) {
  377. if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
  378. return -EINVAL;
  379. tmpformat.format = SNDRV_PCM_FORMAT_S16;
  380. err = snd_pcm_plugin_build_mulaw(plug,
  381. &srcformat, &tmpformat,
  382. &plugin);
  383. if (err < 0)
  384. return err;
  385. err = snd_pcm_plugin_append(plugin);
  386. if (err < 0) {
  387. snd_pcm_plugin_free(plugin);
  388. return err;
  389. }
  390. srcformat = tmpformat;
  391. src_access = dst_access;
  392. }
  393. /* channels reduction */
  394. if (srcformat.channels > dstformat.channels) {
  395. tmpformat.channels = dstformat.channels;
  396. err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
  397. pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
  398. if (err < 0)
  399. return err;
  400. err = snd_pcm_plugin_append(plugin);
  401. if (err < 0) {
  402. snd_pcm_plugin_free(plugin);
  403. return err;
  404. }
  405. srcformat = tmpformat;
  406. src_access = dst_access;
  407. }
  408. /* rate resampling */
  409. if (!rate_match(srcformat.rate, dstformat.rate)) {
  410. if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
  411. /* convert to S16 for resampling */
  412. tmpformat.format = SNDRV_PCM_FORMAT_S16;
  413. err = snd_pcm_plugin_build_linear(plug,
  414. &srcformat, &tmpformat,
  415. &plugin);
  416. if (err < 0)
  417. return err;
  418. err = snd_pcm_plugin_append(plugin);
  419. if (err < 0) {
  420. snd_pcm_plugin_free(plugin);
  421. return err;
  422. }
  423. srcformat = tmpformat;
  424. src_access = dst_access;
  425. }
  426. tmpformat.rate = dstformat.rate;
  427. err = snd_pcm_plugin_build_rate(plug,
  428. &srcformat, &tmpformat,
  429. &plugin);
  430. pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
  431. if (err < 0)
  432. return err;
  433. err = snd_pcm_plugin_append(plugin);
  434. if (err < 0) {
  435. snd_pcm_plugin_free(plugin);
  436. return err;
  437. }
  438. srcformat = tmpformat;
  439. src_access = dst_access;
  440. }
  441. /* format change */
  442. if (srcformat.format != dstformat.format) {
  443. tmpformat.format = dstformat.format;
  444. if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
  445. tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
  446. err = snd_pcm_plugin_build_mulaw(plug,
  447. &srcformat, &tmpformat,
  448. &plugin);
  449. }
  450. else if (snd_pcm_format_linear(srcformat.format) &&
  451. snd_pcm_format_linear(tmpformat.format)) {
  452. err = snd_pcm_plugin_build_linear(plug,
  453. &srcformat, &tmpformat,
  454. &plugin);
  455. }
  456. else
  457. return -EINVAL;
  458. pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
  459. if (err < 0)
  460. return err;
  461. err = snd_pcm_plugin_append(plugin);
  462. if (err < 0) {
  463. snd_pcm_plugin_free(plugin);
  464. return err;
  465. }
  466. srcformat = tmpformat;
  467. src_access = dst_access;
  468. }
  469. /* channels extension */
  470. if (srcformat.channels < dstformat.channels) {
  471. tmpformat.channels = dstformat.channels;
  472. err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
  473. pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
  474. if (err < 0)
  475. return err;
  476. err = snd_pcm_plugin_append(plugin);
  477. if (err < 0) {
  478. snd_pcm_plugin_free(plugin);
  479. return err;
  480. }
  481. srcformat = tmpformat;
  482. src_access = dst_access;
  483. }
  484. /* de-interleave */
  485. if (src_access != dst_access) {
  486. err = snd_pcm_plugin_build_copy(plug,
  487. &srcformat,
  488. &tmpformat,
  489. &plugin);
  490. pdprintf("interleave change (copy: returns %i)\n", err);
  491. if (err < 0)
  492. return err;
  493. err = snd_pcm_plugin_append(plugin);
  494. if (err < 0) {
  495. snd_pcm_plugin_free(plugin);
  496. return err;
  497. }
  498. }
  499. return 0;
  500. }
  501. snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
  502. char *buf,
  503. snd_pcm_uframes_t count,
  504. struct snd_pcm_plugin_channel **channels)
  505. {
  506. struct snd_pcm_plugin *plugin;
  507. struct snd_pcm_plugin_channel *v;
  508. struct snd_pcm_plugin_format *format;
  509. int width, nchannels, channel;
  510. int stream = snd_pcm_plug_stream(plug);
  511. snd_assert(buf != NULL, return -ENXIO);
  512. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  513. plugin = snd_pcm_plug_first(plug);
  514. format = &plugin->src_format;
  515. } else {
  516. plugin = snd_pcm_plug_last(plug);
  517. format = &plugin->dst_format;
  518. }
  519. v = plugin->buf_channels;
  520. *channels = v;
  521. if ((width = snd_pcm_format_physical_width(format->format)) < 0)
  522. return width;
  523. nchannels = format->channels;
  524. snd_assert(plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || format->channels <= 1, return -ENXIO);
  525. for (channel = 0; channel < nchannels; channel++, v++) {
  526. v->frames = count;
  527. v->enabled = 1;
  528. v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
  529. v->area.addr = buf;
  530. v->area.first = channel * width;
  531. v->area.step = nchannels * width;
  532. }
  533. return count;
  534. }
  535. snd_pcm_sframes_t snd_pcm_plug_write_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *src_channels, snd_pcm_uframes_t size)
  536. {
  537. struct snd_pcm_plugin *plugin, *next;
  538. struct snd_pcm_plugin_channel *dst_channels;
  539. int err;
  540. snd_pcm_sframes_t frames = size;
  541. plugin = snd_pcm_plug_first(plug);
  542. while (plugin && frames > 0) {
  543. if ((next = plugin->next) != NULL) {
  544. snd_pcm_sframes_t frames1 = frames;
  545. if (plugin->dst_frames)
  546. frames1 = plugin->dst_frames(plugin, frames);
  547. if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
  548. return err;
  549. }
  550. if (err != frames1) {
  551. frames = err;
  552. if (plugin->src_frames)
  553. frames = plugin->src_frames(plugin, frames1);
  554. }
  555. } else
  556. dst_channels = NULL;
  557. pdprintf("write plugin: %s, %li\n", plugin->name, frames);
  558. if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
  559. return frames;
  560. src_channels = dst_channels;
  561. plugin = next;
  562. }
  563. return snd_pcm_plug_client_size(plug, frames);
  564. }
  565. snd_pcm_sframes_t snd_pcm_plug_read_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *dst_channels_final, snd_pcm_uframes_t size)
  566. {
  567. struct snd_pcm_plugin *plugin, *next;
  568. struct snd_pcm_plugin_channel *src_channels, *dst_channels;
  569. snd_pcm_sframes_t frames = size;
  570. int err;
  571. frames = snd_pcm_plug_slave_size(plug, frames);
  572. if (frames < 0)
  573. return frames;
  574. src_channels = NULL;
  575. plugin = snd_pcm_plug_first(plug);
  576. while (plugin && frames > 0) {
  577. if ((next = plugin->next) != NULL) {
  578. if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
  579. return err;
  580. }
  581. frames = err;
  582. } else {
  583. dst_channels = dst_channels_final;
  584. }
  585. pdprintf("read plugin: %s, %li\n", plugin->name, frames);
  586. if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
  587. return frames;
  588. plugin = next;
  589. src_channels = dst_channels;
  590. }
  591. return frames;
  592. }
  593. int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
  594. size_t samples, int format)
  595. {
  596. /* FIXME: sub byte resolution and odd dst_offset */
  597. unsigned char *dst;
  598. unsigned int dst_step;
  599. int width;
  600. const unsigned char *silence;
  601. if (!dst_area->addr)
  602. return 0;
  603. dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
  604. width = snd_pcm_format_physical_width(format);
  605. if (width <= 0)
  606. return -EINVAL;
  607. if (dst_area->step == (unsigned int) width && width >= 8)
  608. return snd_pcm_format_set_silence(format, dst, samples);
  609. silence = snd_pcm_format_silence_64(format);
  610. if (! silence)
  611. return -EINVAL;
  612. dst_step = dst_area->step / 8;
  613. if (width == 4) {
  614. /* Ima ADPCM */
  615. int dstbit = dst_area->first % 8;
  616. int dstbit_step = dst_area->step % 8;
  617. while (samples-- > 0) {
  618. if (dstbit)
  619. *dst &= 0xf0;
  620. else
  621. *dst &= 0x0f;
  622. dst += dst_step;
  623. dstbit += dstbit_step;
  624. if (dstbit == 8) {
  625. dst++;
  626. dstbit = 0;
  627. }
  628. }
  629. } else {
  630. width /= 8;
  631. while (samples-- > 0) {
  632. memcpy(dst, silence, width);
  633. dst += dst_step;
  634. }
  635. }
  636. return 0;
  637. }
  638. int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
  639. const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
  640. size_t samples, int format)
  641. {
  642. /* FIXME: sub byte resolution and odd dst_offset */
  643. char *src, *dst;
  644. int width;
  645. int src_step, dst_step;
  646. src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
  647. if (!src_area->addr)
  648. return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
  649. dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
  650. if (!dst_area->addr)
  651. return 0;
  652. width = snd_pcm_format_physical_width(format);
  653. if (width <= 0)
  654. return -EINVAL;
  655. if (src_area->step == (unsigned int) width &&
  656. dst_area->step == (unsigned int) width && width >= 8) {
  657. size_t bytes = samples * width / 8;
  658. memcpy(dst, src, bytes);
  659. return 0;
  660. }
  661. src_step = src_area->step / 8;
  662. dst_step = dst_area->step / 8;
  663. if (width == 4) {
  664. /* Ima ADPCM */
  665. int srcbit = src_area->first % 8;
  666. int srcbit_step = src_area->step % 8;
  667. int dstbit = dst_area->first % 8;
  668. int dstbit_step = dst_area->step % 8;
  669. while (samples-- > 0) {
  670. unsigned char srcval;
  671. if (srcbit)
  672. srcval = *src & 0x0f;
  673. else
  674. srcval = (*src & 0xf0) >> 4;
  675. if (dstbit)
  676. *dst = (*dst & 0xf0) | srcval;
  677. else
  678. *dst = (*dst & 0x0f) | (srcval << 4);
  679. src += src_step;
  680. srcbit += srcbit_step;
  681. if (srcbit == 8) {
  682. src++;
  683. srcbit = 0;
  684. }
  685. dst += dst_step;
  686. dstbit += dstbit_step;
  687. if (dstbit == 8) {
  688. dst++;
  689. dstbit = 0;
  690. }
  691. }
  692. } else {
  693. width /= 8;
  694. while (samples-- > 0) {
  695. memcpy(dst, src, width);
  696. src += src_step;
  697. dst += dst_step;
  698. }
  699. }
  700. return 0;
  701. }
  702. #endif