rate.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. S1 = *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. snd_assert(plugin != NULL, return -ENXIO);
  176. if (frames == 0)
  177. return 0;
  178. data = (struct rate_priv *)plugin->extra_data;
  179. if (plugin->src_format.rate < plugin->dst_format.rate) {
  180. res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
  181. } else {
  182. res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
  183. }
  184. if (data->old_src_frames > 0) {
  185. snd_pcm_sframes_t frames1 = frames, res1 = data->old_dst_frames;
  186. while (data->old_src_frames < frames1) {
  187. frames1 >>= 1;
  188. res1 <<= 1;
  189. }
  190. while (data->old_src_frames > frames1) {
  191. frames1 <<= 1;
  192. res1 >>= 1;
  193. }
  194. if (data->old_src_frames == frames1)
  195. return res1;
  196. }
  197. data->old_src_frames = frames;
  198. data->old_dst_frames = res;
  199. return res;
  200. }
  201. static snd_pcm_sframes_t rate_dst_frames(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
  202. {
  203. struct rate_priv *data;
  204. snd_pcm_sframes_t res;
  205. snd_assert(plugin != NULL, return -ENXIO);
  206. if (frames == 0)
  207. return 0;
  208. data = (struct rate_priv *)plugin->extra_data;
  209. if (plugin->src_format.rate < plugin->dst_format.rate) {
  210. res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
  211. } else {
  212. res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
  213. }
  214. if (data->old_dst_frames > 0) {
  215. snd_pcm_sframes_t frames1 = frames, res1 = data->old_src_frames;
  216. while (data->old_dst_frames < frames1) {
  217. frames1 >>= 1;
  218. res1 <<= 1;
  219. }
  220. while (data->old_dst_frames > frames1) {
  221. frames1 <<= 1;
  222. res1 >>= 1;
  223. }
  224. if (data->old_dst_frames == frames1)
  225. return res1;
  226. }
  227. data->old_dst_frames = frames;
  228. data->old_src_frames = res;
  229. return res;
  230. }
  231. static snd_pcm_sframes_t rate_transfer(struct snd_pcm_plugin *plugin,
  232. const struct snd_pcm_plugin_channel *src_channels,
  233. struct snd_pcm_plugin_channel *dst_channels,
  234. snd_pcm_uframes_t frames)
  235. {
  236. snd_pcm_uframes_t dst_frames;
  237. struct rate_priv *data;
  238. snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
  239. if (frames == 0)
  240. return 0;
  241. #ifdef CONFIG_SND_DEBUG
  242. {
  243. unsigned int channel;
  244. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  245. snd_assert(src_channels[channel].area.first % 8 == 0 &&
  246. src_channels[channel].area.step % 8 == 0,
  247. return -ENXIO);
  248. snd_assert(dst_channels[channel].area.first % 8 == 0 &&
  249. dst_channels[channel].area.step % 8 == 0,
  250. return -ENXIO);
  251. }
  252. }
  253. #endif
  254. dst_frames = rate_dst_frames(plugin, frames);
  255. if (dst_frames > dst_channels[0].frames)
  256. dst_frames = dst_channels[0].frames;
  257. data = (struct rate_priv *)plugin->extra_data;
  258. data->func(plugin, src_channels, dst_channels, frames, dst_frames);
  259. return dst_frames;
  260. }
  261. static int rate_action(struct snd_pcm_plugin *plugin,
  262. enum snd_pcm_plugin_action action,
  263. unsigned long udata)
  264. {
  265. snd_assert(plugin != NULL, return -ENXIO);
  266. switch (action) {
  267. case INIT:
  268. case PREPARE:
  269. rate_init(plugin);
  270. break;
  271. default:
  272. break;
  273. }
  274. return 0; /* silenty ignore other actions */
  275. }
  276. int snd_pcm_plugin_build_rate(struct snd_pcm_substream *plug,
  277. struct snd_pcm_plugin_format *src_format,
  278. struct snd_pcm_plugin_format *dst_format,
  279. struct snd_pcm_plugin **r_plugin)
  280. {
  281. int err;
  282. struct rate_priv *data;
  283. struct snd_pcm_plugin *plugin;
  284. snd_assert(r_plugin != NULL, return -ENXIO);
  285. *r_plugin = NULL;
  286. snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
  287. snd_assert(src_format->channels > 0, return -ENXIO);
  288. snd_assert(src_format->format == SNDRV_PCM_FORMAT_S16, return -ENXIO);
  289. snd_assert(dst_format->format == SNDRV_PCM_FORMAT_S16, return -ENXIO);
  290. snd_assert(src_format->rate != dst_format->rate, return -ENXIO);
  291. err = snd_pcm_plugin_build(plug, "rate conversion",
  292. src_format, dst_format,
  293. sizeof(struct rate_priv) +
  294. src_format->channels * sizeof(struct rate_channel),
  295. &plugin);
  296. if (err < 0)
  297. return err;
  298. data = (struct rate_priv *)plugin->extra_data;
  299. if (src_format->rate < dst_format->rate) {
  300. data->pitch = ((src_format->rate << SHIFT) + (dst_format->rate >> 1)) / dst_format->rate;
  301. data->func = resample_expand;
  302. } else {
  303. data->pitch = ((dst_format->rate << SHIFT) + (src_format->rate >> 1)) / src_format->rate;
  304. data->func = resample_shrink;
  305. }
  306. data->pos = 0;
  307. rate_init(plugin);
  308. data->old_src_frames = data->old_dst_frames = 0;
  309. plugin->transfer = rate_transfer;
  310. plugin->src_frames = rate_src_frames;
  311. plugin->dst_frames = rate_dst_frames;
  312. plugin->action = rate_action;
  313. *r_plugin = plugin;
  314. return 0;
  315. }