rate.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Rate conversion Plug-In
  3. * Copyright (c) 1999 by Jaroslav Kysela <perex@perex.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 <linux/time.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include "pcm_plugin.h"
  25. #define SHIFT 11
  26. #define BITS (1<<SHIFT)
  27. #define R_MASK (BITS-1)
  28. /*
  29. * Basic rate conversion plugin
  30. */
  31. struct rate_channel {
  32. signed short last_S1;
  33. signed short last_S2;
  34. };
  35. typedef void (*rate_f)(struct snd_pcm_plugin *plugin,
  36. const struct snd_pcm_plugin_channel *src_channels,
  37. struct snd_pcm_plugin_channel *dst_channels,
  38. int src_frames, int dst_frames);
  39. struct rate_priv {
  40. unsigned int pitch;
  41. unsigned int pos;
  42. rate_f func;
  43. snd_pcm_sframes_t old_src_frames, old_dst_frames;
  44. struct rate_channel channels[0];
  45. };
  46. static void rate_init(struct snd_pcm_plugin *plugin)
  47. {
  48. unsigned int channel;
  49. struct rate_priv *data = (struct rate_priv *)plugin->extra_data;
  50. data->pos = 0;
  51. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  52. data->channels[channel].last_S1 = 0;
  53. data->channels[channel].last_S2 = 0;
  54. }
  55. }
  56. static void resample_expand(struct snd_pcm_plugin *plugin,
  57. const struct snd_pcm_plugin_channel *src_channels,
  58. struct snd_pcm_plugin_channel *dst_channels,
  59. int src_frames, int dst_frames)
  60. {
  61. unsigned int pos = 0;
  62. signed int val;
  63. signed short S1, S2;
  64. signed short *src, *dst;
  65. unsigned int channel;
  66. int src_step, dst_step;
  67. int src_frames1, dst_frames1;
  68. struct rate_priv *data = (struct rate_priv *)plugin->extra_data;
  69. struct rate_channel *rchannels = data->channels;
  70. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  71. pos = data->pos;
  72. S1 = rchannels->last_S1;
  73. S2 = rchannels->last_S2;
  74. if (!src_channels[channel].enabled) {
  75. if (dst_channels[channel].wanted)
  76. snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format);
  77. dst_channels[channel].enabled = 0;
  78. continue;
  79. }
  80. dst_channels[channel].enabled = 1;
  81. src = (signed short *)src_channels[channel].area.addr +
  82. src_channels[channel].area.first / 8 / 2;
  83. dst = (signed short *)dst_channels[channel].area.addr +
  84. dst_channels[channel].area.first / 8 / 2;
  85. src_step = src_channels[channel].area.step / 8 / 2;
  86. dst_step = dst_channels[channel].area.step / 8 / 2;
  87. src_frames1 = src_frames;
  88. dst_frames1 = dst_frames;
  89. while (dst_frames1-- > 0) {
  90. if (pos & ~R_MASK) {
  91. pos &= R_MASK;
  92. S1 = S2;
  93. if (src_frames1-- > 0) {
  94. S2 = *src;
  95. src += src_step;
  96. }
  97. }
  98. val = S1 + ((S2 - S1) * (signed int)pos) / BITS;
  99. if (val < -32768)
  100. val = -32768;
  101. else if (val > 32767)
  102. val = 32767;
  103. *dst = val;
  104. dst += dst_step;
  105. pos += data->pitch;
  106. }
  107. rchannels->last_S1 = S1;
  108. rchannels->last_S2 = S2;
  109. rchannels++;
  110. }
  111. data->pos = pos;
  112. }
  113. static void resample_shrink(struct snd_pcm_plugin *plugin,
  114. const struct snd_pcm_plugin_channel *src_channels,
  115. struct snd_pcm_plugin_channel *dst_channels,
  116. int src_frames, int dst_frames)
  117. {
  118. unsigned int pos = 0;
  119. signed int val;
  120. signed short S1, S2;
  121. signed short *src, *dst;
  122. unsigned int channel;
  123. int src_step, dst_step;
  124. int src_frames1, dst_frames1;
  125. struct rate_priv *data = (struct rate_priv *)plugin->extra_data;
  126. struct rate_channel *rchannels = data->channels;
  127. for (channel = 0; channel < plugin->src_format.channels; ++channel) {
  128. pos = data->pos;
  129. S1 = rchannels->last_S1;
  130. S2 = rchannels->last_S2;
  131. if (!src_channels[channel].enabled) {
  132. if (dst_channels[channel].wanted)
  133. snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format);
  134. dst_channels[channel].enabled = 0;
  135. continue;
  136. }
  137. dst_channels[channel].enabled = 1;
  138. src = (signed short *)src_channels[channel].area.addr +
  139. src_channels[channel].area.first / 8 / 2;
  140. dst = (signed short *)dst_channels[channel].area.addr +
  141. dst_channels[channel].area.first / 8 / 2;
  142. src_step = src_channels[channel].area.step / 8 / 2;
  143. dst_step = dst_channels[channel].area.step / 8 / 2;
  144. src_frames1 = src_frames;
  145. dst_frames1 = dst_frames;
  146. while (dst_frames1 > 0) {
  147. S1 = S2;
  148. if (src_frames1-- > 0) {
  149. S2 = *src;
  150. src += src_step;
  151. }
  152. if (pos & ~R_MASK) {
  153. pos &= R_MASK;
  154. val = S1 + ((S2 - S1) * (signed int)pos) / BITS;
  155. if (val < -32768)
  156. val = -32768;
  157. else if (val > 32767)
  158. val = 32767;
  159. *dst = val;
  160. dst += dst_step;
  161. dst_frames1--;
  162. }
  163. pos += data->pitch;
  164. }
  165. rchannels->last_S1 = S1;
  166. rchannels->last_S2 = S2;
  167. rchannels++;
  168. }
  169. data->pos = pos;
  170. }
  171. static snd_pcm_sframes_t rate_src_frames(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
  172. {
  173. struct rate_priv *data;
  174. snd_pcm_sframes_t res;
  175. if (snd_BUG_ON(!plugin))
  176. return -ENXIO;
  177. if (frames == 0)
  178. return 0;
  179. data = (struct rate_priv *)plugin->extra_data;
  180. if (plugin->src_format.rate < plugin->dst_format.rate) {
  181. res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
  182. } else {
  183. res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
  184. }
  185. if (data->old_src_frames > 0) {
  186. snd_pcm_sframes_t frames1 = frames, res1 = data->old_dst_frames;
  187. while (data->old_src_frames < frames1) {
  188. frames1 >>= 1;
  189. res1 <<= 1;
  190. }
  191. while (data->old_src_frames > frames1) {
  192. frames1 <<= 1;
  193. res1 >>= 1;
  194. }
  195. if (data->old_src_frames == frames1)
  196. return res1;
  197. }
  198. data->old_src_frames = frames;
  199. data->old_dst_frames = res;
  200. return res;
  201. }
  202. static snd_pcm_sframes_t rate_dst_frames(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
  203. {
  204. struct rate_priv *data;
  205. snd_pcm_sframes_t res;
  206. if (snd_BUG_ON(!plugin))
  207. return -ENXIO;
  208. if (frames == 0)
  209. return 0;
  210. data = (struct rate_priv *)plugin->extra_data;
  211. if (plugin->src_format.rate < plugin->dst_format.rate) {
  212. res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
  213. } else {
  214. res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
  215. }
  216. if (data->old_dst_frames > 0) {
  217. snd_pcm_sframes_t frames1 = frames, res1 = data->old_src_frames;
  218. while (data->old_dst_frames < frames1) {
  219. frames1 >>= 1;
  220. res1 <<= 1;
  221. }
  222. while (data->old_dst_frames > frames1) {
  223. frames1 <<= 1;
  224. res1 >>= 1;
  225. }
  226. if (data->old_dst_frames == frames1)
  227. return res1;
  228. }
  229. data->old_dst_frames = frames;
  230. data->old_src_frames = res;
  231. return res;
  232. }
  233. static snd_pcm_sframes_t rate_transfer(struct snd_pcm_plugin *plugin,
  234. const struct snd_pcm_plugin_channel *src_channels,
  235. struct snd_pcm_plugin_channel *dst_channels,
  236. snd_pcm_uframes_t frames)
  237. {
  238. snd_pcm_uframes_t dst_frames;
  239. struct rate_priv *data;
  240. if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
  241. return -ENXIO;
  242. if (frames == 0)
  243. return 0;
  244. #ifdef CONFIG_SND_DEBUG
  245. {
  246. unsigned int channel;
  247. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  248. if (snd_BUG_ON(src_channels[channel].area.first % 8 ||
  249. src_channels[channel].area.step % 8))
  250. return -ENXIO;
  251. if (snd_BUG_ON(dst_channels[channel].area.first % 8 ||
  252. dst_channels[channel].area.step % 8))
  253. return -ENXIO;
  254. }
  255. }
  256. #endif
  257. dst_frames = rate_dst_frames(plugin, frames);
  258. if (dst_frames > dst_channels[0].frames)
  259. dst_frames = dst_channels[0].frames;
  260. data = (struct rate_priv *)plugin->extra_data;
  261. data->func(plugin, src_channels, dst_channels, frames, dst_frames);
  262. return dst_frames;
  263. }
  264. static int rate_action(struct snd_pcm_plugin *plugin,
  265. enum snd_pcm_plugin_action action,
  266. unsigned long udata)
  267. {
  268. if (snd_BUG_ON(!plugin))
  269. return -ENXIO;
  270. switch (action) {
  271. case INIT:
  272. case PREPARE:
  273. rate_init(plugin);
  274. break;
  275. default:
  276. break;
  277. }
  278. return 0; /* silenty ignore other actions */
  279. }
  280. int snd_pcm_plugin_build_rate(struct snd_pcm_substream *plug,
  281. struct snd_pcm_plugin_format *src_format,
  282. struct snd_pcm_plugin_format *dst_format,
  283. struct snd_pcm_plugin **r_plugin)
  284. {
  285. int err;
  286. struct rate_priv *data;
  287. struct snd_pcm_plugin *plugin;
  288. if (snd_BUG_ON(!r_plugin))
  289. return -ENXIO;
  290. *r_plugin = NULL;
  291. if (snd_BUG_ON(src_format->channels != dst_format->channels))
  292. return -ENXIO;
  293. if (snd_BUG_ON(src_format->channels <= 0))
  294. return -ENXIO;
  295. if (snd_BUG_ON(src_format->format != SNDRV_PCM_FORMAT_S16))
  296. return -ENXIO;
  297. if (snd_BUG_ON(dst_format->format != SNDRV_PCM_FORMAT_S16))
  298. return -ENXIO;
  299. if (snd_BUG_ON(src_format->rate == dst_format->rate))
  300. return -ENXIO;
  301. err = snd_pcm_plugin_build(plug, "rate conversion",
  302. src_format, dst_format,
  303. sizeof(struct rate_priv) +
  304. src_format->channels * sizeof(struct rate_channel),
  305. &plugin);
  306. if (err < 0)
  307. return err;
  308. data = (struct rate_priv *)plugin->extra_data;
  309. if (src_format->rate < dst_format->rate) {
  310. data->pitch = ((src_format->rate << SHIFT) + (dst_format->rate >> 1)) / dst_format->rate;
  311. data->func = resample_expand;
  312. } else {
  313. data->pitch = ((dst_format->rate << SHIFT) + (src_format->rate >> 1)) / src_format->rate;
  314. data->func = resample_shrink;
  315. }
  316. data->pos = 0;
  317. rate_init(plugin);
  318. data->old_src_frames = data->old_dst_frames = 0;
  319. plugin->transfer = rate_transfer;
  320. plugin->src_frames = rate_src_frames;
  321. plugin->dst_frames = rate_dst_frames;
  322. plugin->action = rate_action;
  323. *r_plugin = plugin;
  324. return 0;
  325. }