rate.c 11 KB

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