pcm_plugin.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 = snd_pcm_plug_stream(plug);
  190. if (snd_BUG_ON(!plug))
  191. return -ENXIO;
  192. if (drv_frames == 0)
  193. return 0;
  194. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  195. plugin = snd_pcm_plug_last(plug);
  196. while (plugin && drv_frames > 0) {
  197. plugin_prev = plugin->prev;
  198. if (plugin->src_frames)
  199. drv_frames = plugin->src_frames(plugin, drv_frames);
  200. plugin = plugin_prev;
  201. }
  202. } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  203. plugin = snd_pcm_plug_first(plug);
  204. while (plugin && drv_frames > 0) {
  205. plugin_next = plugin->next;
  206. if (plugin->dst_frames)
  207. drv_frames = plugin->dst_frames(plugin, drv_frames);
  208. plugin = plugin_next;
  209. }
  210. } else
  211. snd_BUG();
  212. return drv_frames;
  213. }
  214. snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
  215. {
  216. struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
  217. snd_pcm_sframes_t frames;
  218. int stream = snd_pcm_plug_stream(plug);
  219. if (snd_BUG_ON(!plug))
  220. return -ENXIO;
  221. if (clt_frames == 0)
  222. return 0;
  223. frames = clt_frames;
  224. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  225. plugin = snd_pcm_plug_first(plug);
  226. while (plugin && frames > 0) {
  227. plugin_next = plugin->next;
  228. if (plugin->dst_frames) {
  229. frames = plugin->dst_frames(plugin, frames);
  230. if (frames < 0)
  231. return frames;
  232. }
  233. plugin = plugin_next;
  234. }
  235. } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
  236. plugin = snd_pcm_plug_last(plug);
  237. while (plugin) {
  238. plugin_prev = plugin->prev;
  239. if (plugin->src_frames) {
  240. frames = plugin->src_frames(plugin, frames);
  241. if (frames < 0)
  242. return frames;
  243. }
  244. plugin = plugin_prev;
  245. }
  246. } else
  247. snd_BUG();
  248. return frames;
  249. }
  250. static int snd_pcm_plug_formats(struct snd_mask *mask, int format)
  251. {
  252. struct snd_mask formats = *mask;
  253. u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
  254. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
  255. SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
  256. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
  257. SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
  258. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
  259. SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
  260. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
  261. SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
  262. snd_mask_set(&formats, SNDRV_PCM_FORMAT_MU_LAW);
  263. if (formats.bits[0] & (u32)linfmts)
  264. formats.bits[0] |= (u32)linfmts;
  265. if (formats.bits[1] & (u32)(linfmts >> 32))
  266. formats.bits[1] |= (u32)(linfmts >> 32);
  267. return snd_mask_test(&formats, format);
  268. }
  269. static int preferred_formats[] = {
  270. SNDRV_PCM_FORMAT_S16_LE,
  271. SNDRV_PCM_FORMAT_S16_BE,
  272. SNDRV_PCM_FORMAT_U16_LE,
  273. SNDRV_PCM_FORMAT_U16_BE,
  274. SNDRV_PCM_FORMAT_S24_3LE,
  275. SNDRV_PCM_FORMAT_S24_3BE,
  276. SNDRV_PCM_FORMAT_U24_3LE,
  277. SNDRV_PCM_FORMAT_U24_3BE,
  278. SNDRV_PCM_FORMAT_S24_LE,
  279. SNDRV_PCM_FORMAT_S24_BE,
  280. SNDRV_PCM_FORMAT_U24_LE,
  281. SNDRV_PCM_FORMAT_U24_BE,
  282. SNDRV_PCM_FORMAT_S32_LE,
  283. SNDRV_PCM_FORMAT_S32_BE,
  284. SNDRV_PCM_FORMAT_U32_LE,
  285. SNDRV_PCM_FORMAT_U32_BE,
  286. SNDRV_PCM_FORMAT_S8,
  287. SNDRV_PCM_FORMAT_U8
  288. };
  289. int snd_pcm_plug_slave_format(int format, struct snd_mask *format_mask)
  290. {
  291. int i;
  292. if (snd_mask_test(format_mask, format))
  293. return format;
  294. if (! snd_pcm_plug_formats(format_mask, format))
  295. return -EINVAL;
  296. if (snd_pcm_format_linear(format)) {
  297. unsigned int width = snd_pcm_format_width(format);
  298. int unsignd = snd_pcm_format_unsigned(format) > 0;
  299. int big = snd_pcm_format_big_endian(format) > 0;
  300. unsigned int badness, best = -1;
  301. int best_format = -1;
  302. for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
  303. int f = preferred_formats[i];
  304. unsigned int w;
  305. if (!snd_mask_test(format_mask, f))
  306. continue;
  307. w = snd_pcm_format_width(f);
  308. if (w >= width)
  309. badness = w - width;
  310. else
  311. badness = width - w + 32;
  312. badness += snd_pcm_format_unsigned(f) != unsignd;
  313. badness += snd_pcm_format_big_endian(f) != big;
  314. if (badness < best) {
  315. best_format = f;
  316. best = badness;
  317. }
  318. }
  319. return best_format >= 0 ? best_format : -EINVAL;
  320. } else {
  321. switch (format) {
  322. case SNDRV_PCM_FORMAT_MU_LAW:
  323. for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
  324. int format1 = preferred_formats[i];
  325. if (snd_mask_test(format_mask, format1))
  326. return format1;
  327. }
  328. default:
  329. return -EINVAL;
  330. }
  331. }
  332. }
  333. int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
  334. struct snd_pcm_hw_params *params,
  335. struct snd_pcm_hw_params *slave_params)
  336. {
  337. struct snd_pcm_plugin_format tmpformat;
  338. struct snd_pcm_plugin_format dstformat;
  339. struct snd_pcm_plugin_format srcformat;
  340. int src_access, dst_access;
  341. struct snd_pcm_plugin *plugin = NULL;
  342. int err;
  343. int stream = snd_pcm_plug_stream(plug);
  344. int slave_interleaved = (params_channels(slave_params) == 1 ||
  345. params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
  346. switch (stream) {
  347. case SNDRV_PCM_STREAM_PLAYBACK:
  348. dstformat.format = params_format(slave_params);
  349. dstformat.rate = params_rate(slave_params);
  350. dstformat.channels = params_channels(slave_params);
  351. srcformat.format = params_format(params);
  352. srcformat.rate = params_rate(params);
  353. srcformat.channels = params_channels(params);
  354. src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  355. dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
  356. SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
  357. break;
  358. case SNDRV_PCM_STREAM_CAPTURE:
  359. dstformat.format = params_format(params);
  360. dstformat.rate = params_rate(params);
  361. dstformat.channels = params_channels(params);
  362. srcformat.format = params_format(slave_params);
  363. srcformat.rate = params_rate(slave_params);
  364. srcformat.channels = params_channels(slave_params);
  365. src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
  366. SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
  367. dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  368. break;
  369. default:
  370. snd_BUG();
  371. return -EINVAL;
  372. }
  373. tmpformat = srcformat;
  374. pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
  375. srcformat.format,
  376. srcformat.rate,
  377. srcformat.channels);
  378. pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
  379. dstformat.format,
  380. dstformat.rate,
  381. dstformat.channels);
  382. /* Format change (linearization) */
  383. if (! rate_match(srcformat.rate, dstformat.rate) &&
  384. ! snd_pcm_format_linear(srcformat.format)) {
  385. if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
  386. return -EINVAL;
  387. tmpformat.format = SNDRV_PCM_FORMAT_S16;
  388. err = snd_pcm_plugin_build_mulaw(plug,
  389. &srcformat, &tmpformat,
  390. &plugin);
  391. if (err < 0)
  392. return err;
  393. err = snd_pcm_plugin_append(plugin);
  394. if (err < 0) {
  395. snd_pcm_plugin_free(plugin);
  396. return err;
  397. }
  398. srcformat = tmpformat;
  399. src_access = dst_access;
  400. }
  401. /* channels reduction */
  402. if (srcformat.channels > dstformat.channels) {
  403. tmpformat.channels = dstformat.channels;
  404. err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
  405. pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
  406. if (err < 0)
  407. return err;
  408. err = snd_pcm_plugin_append(plugin);
  409. if (err < 0) {
  410. snd_pcm_plugin_free(plugin);
  411. return err;
  412. }
  413. srcformat = tmpformat;
  414. src_access = dst_access;
  415. }
  416. /* rate resampling */
  417. if (!rate_match(srcformat.rate, dstformat.rate)) {
  418. if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
  419. /* convert to S16 for resampling */
  420. tmpformat.format = SNDRV_PCM_FORMAT_S16;
  421. err = snd_pcm_plugin_build_linear(plug,
  422. &srcformat, &tmpformat,
  423. &plugin);
  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. tmpformat.rate = dstformat.rate;
  435. err = snd_pcm_plugin_build_rate(plug,
  436. &srcformat, &tmpformat,
  437. &plugin);
  438. pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
  439. if (err < 0)
  440. return err;
  441. err = snd_pcm_plugin_append(plugin);
  442. if (err < 0) {
  443. snd_pcm_plugin_free(plugin);
  444. return err;
  445. }
  446. srcformat = tmpformat;
  447. src_access = dst_access;
  448. }
  449. /* format change */
  450. if (srcformat.format != dstformat.format) {
  451. tmpformat.format = dstformat.format;
  452. if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
  453. tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
  454. err = snd_pcm_plugin_build_mulaw(plug,
  455. &srcformat, &tmpformat,
  456. &plugin);
  457. }
  458. else if (snd_pcm_format_linear(srcformat.format) &&
  459. snd_pcm_format_linear(tmpformat.format)) {
  460. err = snd_pcm_plugin_build_linear(plug,
  461. &srcformat, &tmpformat,
  462. &plugin);
  463. }
  464. else
  465. return -EINVAL;
  466. pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
  467. if (err < 0)
  468. return err;
  469. err = snd_pcm_plugin_append(plugin);
  470. if (err < 0) {
  471. snd_pcm_plugin_free(plugin);
  472. return err;
  473. }
  474. srcformat = tmpformat;
  475. src_access = dst_access;
  476. }
  477. /* channels extension */
  478. if (srcformat.channels < dstformat.channels) {
  479. tmpformat.channels = dstformat.channels;
  480. err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
  481. pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
  482. if (err < 0)
  483. return err;
  484. err = snd_pcm_plugin_append(plugin);
  485. if (err < 0) {
  486. snd_pcm_plugin_free(plugin);
  487. return err;
  488. }
  489. srcformat = tmpformat;
  490. src_access = dst_access;
  491. }
  492. /* de-interleave */
  493. if (src_access != dst_access) {
  494. err = snd_pcm_plugin_build_copy(plug,
  495. &srcformat,
  496. &tmpformat,
  497. &plugin);
  498. pdprintf("interleave change (copy: returns %i)\n", err);
  499. if (err < 0)
  500. return err;
  501. err = snd_pcm_plugin_append(plugin);
  502. if (err < 0) {
  503. snd_pcm_plugin_free(plugin);
  504. return err;
  505. }
  506. }
  507. return 0;
  508. }
  509. snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
  510. char *buf,
  511. snd_pcm_uframes_t count,
  512. struct snd_pcm_plugin_channel **channels)
  513. {
  514. struct snd_pcm_plugin *plugin;
  515. struct snd_pcm_plugin_channel *v;
  516. struct snd_pcm_plugin_format *format;
  517. int width, nchannels, channel;
  518. int stream = snd_pcm_plug_stream(plug);
  519. if (snd_BUG_ON(!buf))
  520. return -ENXIO;
  521. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  522. plugin = snd_pcm_plug_first(plug);
  523. format = &plugin->src_format;
  524. } else {
  525. plugin = snd_pcm_plug_last(plug);
  526. format = &plugin->dst_format;
  527. }
  528. v = plugin->buf_channels;
  529. *channels = v;
  530. if ((width = snd_pcm_format_physical_width(format->format)) < 0)
  531. return width;
  532. nchannels = format->channels;
  533. if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  534. format->channels > 1))
  535. return -ENXIO;
  536. for (channel = 0; channel < nchannels; channel++, v++) {
  537. v->frames = count;
  538. v->enabled = 1;
  539. v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
  540. v->area.addr = buf;
  541. v->area.first = channel * width;
  542. v->area.step = nchannels * width;
  543. }
  544. return count;
  545. }
  546. 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)
  547. {
  548. struct snd_pcm_plugin *plugin, *next;
  549. struct snd_pcm_plugin_channel *dst_channels;
  550. int err;
  551. snd_pcm_sframes_t frames = size;
  552. plugin = snd_pcm_plug_first(plug);
  553. while (plugin && frames > 0) {
  554. if ((next = plugin->next) != NULL) {
  555. snd_pcm_sframes_t frames1 = frames;
  556. if (plugin->dst_frames)
  557. frames1 = plugin->dst_frames(plugin, frames);
  558. if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
  559. return err;
  560. }
  561. if (err != frames1) {
  562. frames = err;
  563. if (plugin->src_frames)
  564. frames = plugin->src_frames(plugin, frames1);
  565. }
  566. } else
  567. dst_channels = NULL;
  568. pdprintf("write plugin: %s, %li\n", plugin->name, frames);
  569. if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
  570. return frames;
  571. src_channels = dst_channels;
  572. plugin = next;
  573. }
  574. return snd_pcm_plug_client_size(plug, frames);
  575. }
  576. 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)
  577. {
  578. struct snd_pcm_plugin *plugin, *next;
  579. struct snd_pcm_plugin_channel *src_channels, *dst_channels;
  580. snd_pcm_sframes_t frames = size;
  581. int err;
  582. frames = snd_pcm_plug_slave_size(plug, frames);
  583. if (frames < 0)
  584. return frames;
  585. src_channels = NULL;
  586. plugin = snd_pcm_plug_first(plug);
  587. while (plugin && frames > 0) {
  588. if ((next = plugin->next) != NULL) {
  589. if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
  590. return err;
  591. }
  592. frames = err;
  593. } else {
  594. dst_channels = dst_channels_final;
  595. }
  596. pdprintf("read plugin: %s, %li\n", plugin->name, frames);
  597. if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
  598. return frames;
  599. plugin = next;
  600. src_channels = dst_channels;
  601. }
  602. return frames;
  603. }
  604. int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
  605. size_t samples, int format)
  606. {
  607. /* FIXME: sub byte resolution and odd dst_offset */
  608. unsigned char *dst;
  609. unsigned int dst_step;
  610. int width;
  611. const unsigned char *silence;
  612. if (!dst_area->addr)
  613. return 0;
  614. dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
  615. width = snd_pcm_format_physical_width(format);
  616. if (width <= 0)
  617. return -EINVAL;
  618. if (dst_area->step == (unsigned int) width && width >= 8)
  619. return snd_pcm_format_set_silence(format, dst, samples);
  620. silence = snd_pcm_format_silence_64(format);
  621. if (! silence)
  622. return -EINVAL;
  623. dst_step = dst_area->step / 8;
  624. if (width == 4) {
  625. /* Ima ADPCM */
  626. int dstbit = dst_area->first % 8;
  627. int dstbit_step = dst_area->step % 8;
  628. while (samples-- > 0) {
  629. if (dstbit)
  630. *dst &= 0xf0;
  631. else
  632. *dst &= 0x0f;
  633. dst += dst_step;
  634. dstbit += dstbit_step;
  635. if (dstbit == 8) {
  636. dst++;
  637. dstbit = 0;
  638. }
  639. }
  640. } else {
  641. width /= 8;
  642. while (samples-- > 0) {
  643. memcpy(dst, silence, width);
  644. dst += dst_step;
  645. }
  646. }
  647. return 0;
  648. }
  649. int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
  650. const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
  651. size_t samples, int format)
  652. {
  653. /* FIXME: sub byte resolution and odd dst_offset */
  654. char *src, *dst;
  655. int width;
  656. int src_step, dst_step;
  657. src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
  658. if (!src_area->addr)
  659. return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
  660. dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
  661. if (!dst_area->addr)
  662. return 0;
  663. width = snd_pcm_format_physical_width(format);
  664. if (width <= 0)
  665. return -EINVAL;
  666. if (src_area->step == (unsigned int) width &&
  667. dst_area->step == (unsigned int) width && width >= 8) {
  668. size_t bytes = samples * width / 8;
  669. memcpy(dst, src, bytes);
  670. return 0;
  671. }
  672. src_step = src_area->step / 8;
  673. dst_step = dst_area->step / 8;
  674. if (width == 4) {
  675. /* Ima ADPCM */
  676. int srcbit = src_area->first % 8;
  677. int srcbit_step = src_area->step % 8;
  678. int dstbit = dst_area->first % 8;
  679. int dstbit_step = dst_area->step % 8;
  680. while (samples-- > 0) {
  681. unsigned char srcval;
  682. if (srcbit)
  683. srcval = *src & 0x0f;
  684. else
  685. srcval = (*src & 0xf0) >> 4;
  686. if (dstbit)
  687. *dst = (*dst & 0xf0) | srcval;
  688. else
  689. *dst = (*dst & 0x0f) | (srcval << 4);
  690. src += src_step;
  691. srcbit += srcbit_step;
  692. if (srcbit == 8) {
  693. src++;
  694. srcbit = 0;
  695. }
  696. dst += dst_step;
  697. dstbit += dstbit_step;
  698. if (dstbit == 8) {
  699. dst++;
  700. dstbit = 0;
  701. }
  702. }
  703. } else {
  704. width /= 8;
  705. while (samples-- > 0) {
  706. memcpy(dst, src, width);
  707. src += src_step;
  708. dst += dst_step;
  709. }
  710. }
  711. return 0;
  712. }