rate.c 9.8 KB

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