pcm_plugin.c 22 KB

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