rate.c 9.7 KB

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