rate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Rate conversion Plug-In
  3. * Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
  4. *
  5. *
  6. * This library is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Library General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <linux/time.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include "pcm_plugin.h"
  26. #define SHIFT 11
  27. #define BITS (1<<SHIFT)
  28. #define R_MASK (BITS-1)
  29. /*
  30. * Basic rate conversion plugin
  31. */
  32. typedef struct {
  33. signed short last_S1;
  34. signed short last_S2;
  35. } rate_channel_t;
  36. typedef void (*rate_f)(snd_pcm_plugin_t *plugin,
  37. const snd_pcm_plugin_channel_t *src_channels,
  38. snd_pcm_plugin_channel_t *dst_channels,
  39. int src_frames, int dst_frames);
  40. typedef struct rate_private_data {
  41. unsigned int pitch;
  42. unsigned int pos;
  43. rate_f func;
  44. int get, put;
  45. snd_pcm_sframes_t old_src_frames, old_dst_frames;
  46. rate_channel_t channels[0];
  47. } rate_t;
  48. static void rate_init(snd_pcm_plugin_t *plugin)
  49. {
  50. unsigned int channel;
  51. rate_t *data = (rate_t *)plugin->extra_data;
  52. data->pos = 0;
  53. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  54. data->channels[channel].last_S1 = 0;
  55. data->channels[channel].last_S2 = 0;
  56. }
  57. }
  58. static void resample_expand(snd_pcm_plugin_t *plugin,
  59. const snd_pcm_plugin_channel_t *src_channels,
  60. snd_pcm_plugin_channel_t *dst_channels,
  61. int src_frames, int dst_frames)
  62. {
  63. unsigned int pos = 0;
  64. signed int val;
  65. signed short S1, S2;
  66. char *src, *dst;
  67. unsigned int channel;
  68. int src_step, dst_step;
  69. int src_frames1, dst_frames1;
  70. rate_t *data = (rate_t *)plugin->extra_data;
  71. rate_channel_t *rchannels = data->channels;
  72. #define GET_S16_LABELS
  73. #define PUT_S16_LABELS
  74. #include "plugin_ops.h"
  75. #undef GET_S16_LABELS
  76. #undef PUT_S16_LABELS
  77. void *get = get_s16_labels[data->get];
  78. void *put = put_s16_labels[data->put];
  79. signed short sample = 0;
  80. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  81. pos = data->pos;
  82. S1 = rchannels->last_S1;
  83. S2 = rchannels->last_S2;
  84. if (!src_channels[channel].enabled) {
  85. if (dst_channels[channel].wanted)
  86. snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format);
  87. dst_channels[channel].enabled = 0;
  88. continue;
  89. }
  90. dst_channels[channel].enabled = 1;
  91. src = (char *)src_channels[channel].area.addr + src_channels[channel].area.first / 8;
  92. dst = (char *)dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
  93. src_step = src_channels[channel].area.step / 8;
  94. dst_step = dst_channels[channel].area.step / 8;
  95. src_frames1 = src_frames;
  96. dst_frames1 = dst_frames;
  97. while (dst_frames1-- > 0) {
  98. if (pos & ~R_MASK) {
  99. pos &= R_MASK;
  100. S1 = S2;
  101. if (src_frames1-- > 0) {
  102. goto *get;
  103. #define GET_S16_END after_get
  104. #include "plugin_ops.h"
  105. #undef GET_S16_END
  106. after_get:
  107. S2 = sample;
  108. src += src_step;
  109. }
  110. }
  111. val = S1 + ((S2 - S1) * (signed int)pos) / BITS;
  112. if (val < -32768)
  113. val = -32768;
  114. else if (val > 32767)
  115. val = 32767;
  116. sample = val;
  117. goto *put;
  118. #define PUT_S16_END after_put
  119. #include "plugin_ops.h"
  120. #undef PUT_S16_END
  121. after_put:
  122. dst += dst_step;
  123. pos += data->pitch;
  124. }
  125. rchannels->last_S1 = S1;
  126. rchannels->last_S2 = S2;
  127. rchannels++;
  128. }
  129. data->pos = pos;
  130. }
  131. static void resample_shrink(snd_pcm_plugin_t *plugin,
  132. const snd_pcm_plugin_channel_t *src_channels,
  133. snd_pcm_plugin_channel_t *dst_channels,
  134. int src_frames, int dst_frames)
  135. {
  136. unsigned int pos = 0;
  137. signed int val;
  138. signed short S1, S2;
  139. char *src, *dst;
  140. unsigned int channel;
  141. int src_step, dst_step;
  142. int src_frames1, dst_frames1;
  143. rate_t *data = (rate_t *)plugin->extra_data;
  144. rate_channel_t *rchannels = data->channels;
  145. #define GET_S16_LABELS
  146. #define PUT_S16_LABELS
  147. #include "plugin_ops.h"
  148. #undef GET_S16_LABELS
  149. #undef PUT_S16_LABELS
  150. void *get = get_s16_labels[data->get];
  151. void *put = put_s16_labels[data->put];
  152. signed short sample = 0;
  153. for (channel = 0; channel < plugin->src_format.channels; ++channel) {
  154. pos = data->pos;
  155. S1 = rchannels->last_S1;
  156. S2 = rchannels->last_S2;
  157. if (!src_channels[channel].enabled) {
  158. if (dst_channels[channel].wanted)
  159. snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format);
  160. dst_channels[channel].enabled = 0;
  161. continue;
  162. }
  163. dst_channels[channel].enabled = 1;
  164. src = (char *)src_channels[channel].area.addr + src_channels[channel].area.first / 8;
  165. dst = (char *)dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
  166. src_step = src_channels[channel].area.step / 8;
  167. dst_step = dst_channels[channel].area.step / 8;
  168. src_frames1 = src_frames;
  169. dst_frames1 = dst_frames;
  170. while (dst_frames1 > 0) {
  171. S1 = S2;
  172. if (src_frames1-- > 0) {
  173. goto *get;
  174. #define GET_S16_END after_get
  175. #include "plugin_ops.h"
  176. #undef GET_S16_END
  177. after_get:
  178. S2 = sample;
  179. src += src_step;
  180. }
  181. if (pos & ~R_MASK) {
  182. pos &= R_MASK;
  183. val = S1 + ((S2 - S1) * (signed int)pos) / BITS;
  184. if (val < -32768)
  185. val = -32768;
  186. else if (val > 32767)
  187. val = 32767;
  188. sample = val;
  189. goto *put;
  190. #define PUT_S16_END after_put
  191. #include "plugin_ops.h"
  192. #undef PUT_S16_END
  193. after_put:
  194. dst += dst_step;
  195. dst_frames1--;
  196. }
  197. pos += data->pitch;
  198. }
  199. rchannels->last_S1 = S1;
  200. rchannels->last_S2 = S2;
  201. rchannels++;
  202. }
  203. data->pos = pos;
  204. }
  205. static snd_pcm_sframes_t rate_src_frames(snd_pcm_plugin_t *plugin, snd_pcm_uframes_t frames)
  206. {
  207. rate_t *data;
  208. snd_pcm_sframes_t res;
  209. snd_assert(plugin != NULL, return -ENXIO);
  210. if (frames == 0)
  211. return 0;
  212. data = (rate_t *)plugin->extra_data;
  213. if (plugin->src_format.rate < plugin->dst_format.rate) {
  214. res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
  215. } else {
  216. res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
  217. }
  218. if (data->old_src_frames > 0) {
  219. snd_pcm_sframes_t frames1 = frames, res1 = data->old_dst_frames;
  220. while (data->old_src_frames < frames1) {
  221. frames1 >>= 1;
  222. res1 <<= 1;
  223. }
  224. while (data->old_src_frames > frames1) {
  225. frames1 <<= 1;
  226. res1 >>= 1;
  227. }
  228. if (data->old_src_frames == frames1)
  229. return res1;
  230. }
  231. data->old_src_frames = frames;
  232. data->old_dst_frames = res;
  233. return res;
  234. }
  235. static snd_pcm_sframes_t rate_dst_frames(snd_pcm_plugin_t *plugin, snd_pcm_uframes_t frames)
  236. {
  237. rate_t *data;
  238. snd_pcm_sframes_t res;
  239. snd_assert(plugin != NULL, return -ENXIO);
  240. if (frames == 0)
  241. return 0;
  242. data = (rate_t *)plugin->extra_data;
  243. if (plugin->src_format.rate < plugin->dst_format.rate) {
  244. res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
  245. } else {
  246. res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
  247. }
  248. if (data->old_dst_frames > 0) {
  249. snd_pcm_sframes_t frames1 = frames, res1 = data->old_src_frames;
  250. while (data->old_dst_frames < frames1) {
  251. frames1 >>= 1;
  252. res1 <<= 1;
  253. }
  254. while (data->old_dst_frames > frames1) {
  255. frames1 <<= 1;
  256. res1 >>= 1;
  257. }
  258. if (data->old_dst_frames == frames1)
  259. return res1;
  260. }
  261. data->old_dst_frames = frames;
  262. data->old_src_frames = res;
  263. return res;
  264. }
  265. static snd_pcm_sframes_t rate_transfer(snd_pcm_plugin_t *plugin,
  266. const snd_pcm_plugin_channel_t *src_channels,
  267. snd_pcm_plugin_channel_t *dst_channels,
  268. snd_pcm_uframes_t frames)
  269. {
  270. snd_pcm_uframes_t dst_frames;
  271. rate_t *data;
  272. snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
  273. if (frames == 0)
  274. return 0;
  275. #ifdef CONFIG_SND_DEBUG
  276. {
  277. unsigned int channel;
  278. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  279. snd_assert(src_channels[channel].area.first % 8 == 0 &&
  280. src_channels[channel].area.step % 8 == 0,
  281. return -ENXIO);
  282. snd_assert(dst_channels[channel].area.first % 8 == 0 &&
  283. dst_channels[channel].area.step % 8 == 0,
  284. return -ENXIO);
  285. }
  286. }
  287. #endif
  288. dst_frames = rate_dst_frames(plugin, frames);
  289. if (dst_frames > dst_channels[0].frames)
  290. dst_frames = dst_channels[0].frames;
  291. data = (rate_t *)plugin->extra_data;
  292. data->func(plugin, src_channels, dst_channels, frames, dst_frames);
  293. return dst_frames;
  294. }
  295. static int rate_action(snd_pcm_plugin_t *plugin,
  296. snd_pcm_plugin_action_t action,
  297. unsigned long udata ATTRIBUTE_UNUSED)
  298. {
  299. snd_assert(plugin != NULL, return -ENXIO);
  300. switch (action) {
  301. case INIT:
  302. case PREPARE:
  303. rate_init(plugin);
  304. break;
  305. default:
  306. break;
  307. }
  308. return 0; /* silenty ignore other actions */
  309. }
  310. int snd_pcm_plugin_build_rate(snd_pcm_plug_t *plug,
  311. snd_pcm_plugin_format_t *src_format,
  312. snd_pcm_plugin_format_t *dst_format,
  313. snd_pcm_plugin_t **r_plugin)
  314. {
  315. int err;
  316. rate_t *data;
  317. snd_pcm_plugin_t *plugin;
  318. snd_assert(r_plugin != NULL, return -ENXIO);
  319. *r_plugin = NULL;
  320. snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
  321. snd_assert(src_format->channels > 0, return -ENXIO);
  322. snd_assert(snd_pcm_format_linear(src_format->format) != 0, return -ENXIO);
  323. snd_assert(snd_pcm_format_linear(dst_format->format) != 0, return -ENXIO);
  324. snd_assert(src_format->rate != dst_format->rate, return -ENXIO);
  325. err = snd_pcm_plugin_build(plug, "rate conversion",
  326. src_format, dst_format,
  327. sizeof(rate_t) + src_format->channels * sizeof(rate_channel_t),
  328. &plugin);
  329. if (err < 0)
  330. return err;
  331. data = (rate_t *)plugin->extra_data;
  332. data->get = getput_index(src_format->format);
  333. snd_assert(data->get >= 0 && data->get < 4*2*2, return -EINVAL);
  334. data->put = getput_index(dst_format->format);
  335. snd_assert(data->put >= 0 && data->put < 4*2*2, return -EINVAL);
  336. if (src_format->rate < dst_format->rate) {
  337. data->pitch = ((src_format->rate << SHIFT) + (dst_format->rate >> 1)) / dst_format->rate;
  338. data->func = resample_expand;
  339. } else {
  340. data->pitch = ((dst_format->rate << SHIFT) + (src_format->rate >> 1)) / src_format->rate;
  341. data->func = resample_shrink;
  342. }
  343. data->pos = 0;
  344. rate_init(plugin);
  345. data->old_src_frames = data->old_dst_frames = 0;
  346. plugin->transfer = rate_transfer;
  347. plugin->src_frames = rate_src_frames;
  348. plugin->dst_frames = rate_dst_frames;
  349. plugin->action = rate_action;
  350. *r_plugin = plugin;
  351. return 0;
  352. }