pcm_plugin.c 22 KB

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