pcm_plugin.c 22 KB

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