linear.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Linear conversion Plug-In
  3. * Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>,
  4. * Abramo Bagnara <abramo@alsa-project.org>
  5. *
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Library General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <sound/driver.h>
  23. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  24. #include <linux/time.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include "pcm_plugin.h"
  28. /*
  29. * Basic linear conversion plugin
  30. */
  31. struct linear_priv {
  32. int conv;
  33. };
  34. static void convert(struct snd_pcm_plugin *plugin,
  35. const struct snd_pcm_plugin_channel *src_channels,
  36. struct snd_pcm_plugin_channel *dst_channels,
  37. snd_pcm_uframes_t frames)
  38. {
  39. #define CONV_LABELS
  40. #include "plugin_ops.h"
  41. #undef CONV_LABELS
  42. struct linear_priv *data = (struct linear_priv *)plugin->extra_data;
  43. void *conv = conv_labels[data->conv];
  44. int channel;
  45. int nchannels = plugin->src_format.channels;
  46. for (channel = 0; channel < nchannels; ++channel) {
  47. char *src;
  48. char *dst;
  49. int src_step, dst_step;
  50. snd_pcm_uframes_t frames1;
  51. if (!src_channels[channel].enabled) {
  52. if (dst_channels[channel].wanted)
  53. snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
  54. dst_channels[channel].enabled = 0;
  55. continue;
  56. }
  57. dst_channels[channel].enabled = 1;
  58. src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
  59. dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
  60. src_step = src_channels[channel].area.step / 8;
  61. dst_step = dst_channels[channel].area.step / 8;
  62. frames1 = frames;
  63. while (frames1-- > 0) {
  64. goto *conv;
  65. #define CONV_END after
  66. #include "plugin_ops.h"
  67. #undef CONV_END
  68. after:
  69. src += src_step;
  70. dst += dst_step;
  71. }
  72. }
  73. }
  74. static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin,
  75. const struct snd_pcm_plugin_channel *src_channels,
  76. struct snd_pcm_plugin_channel *dst_channels,
  77. snd_pcm_uframes_t frames)
  78. {
  79. struct linear_priv *data;
  80. snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
  81. data = (struct linear_priv *)plugin->extra_data;
  82. if (frames == 0)
  83. return 0;
  84. #ifdef CONFIG_SND_DEBUG
  85. {
  86. unsigned int channel;
  87. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  88. snd_assert(src_channels[channel].area.first % 8 == 0 &&
  89. src_channels[channel].area.step % 8 == 0,
  90. return -ENXIO);
  91. snd_assert(dst_channels[channel].area.first % 8 == 0 &&
  92. dst_channels[channel].area.step % 8 == 0,
  93. return -ENXIO);
  94. }
  95. }
  96. #endif
  97. convert(plugin, src_channels, dst_channels, frames);
  98. return frames;
  99. }
  100. static int conv_index(int src_format, int dst_format)
  101. {
  102. int src_endian, dst_endian, sign, src_width, dst_width;
  103. sign = (snd_pcm_format_signed(src_format) !=
  104. snd_pcm_format_signed(dst_format));
  105. #ifdef SNDRV_LITTLE_ENDIAN
  106. src_endian = snd_pcm_format_big_endian(src_format);
  107. dst_endian = snd_pcm_format_big_endian(dst_format);
  108. #else
  109. src_endian = snd_pcm_format_little_endian(src_format);
  110. dst_endian = snd_pcm_format_little_endian(dst_format);
  111. #endif
  112. if (src_endian < 0)
  113. src_endian = 0;
  114. if (dst_endian < 0)
  115. dst_endian = 0;
  116. src_width = snd_pcm_format_width(src_format) / 8 - 1;
  117. dst_width = snd_pcm_format_width(dst_format) / 8 - 1;
  118. return src_width * 32 + src_endian * 16 + sign * 8 + dst_width * 2 + dst_endian;
  119. }
  120. int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug,
  121. struct snd_pcm_plugin_format *src_format,
  122. struct snd_pcm_plugin_format *dst_format,
  123. struct snd_pcm_plugin **r_plugin)
  124. {
  125. int err;
  126. struct linear_priv *data;
  127. struct snd_pcm_plugin *plugin;
  128. snd_assert(r_plugin != NULL, return -ENXIO);
  129. *r_plugin = NULL;
  130. snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
  131. snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
  132. snd_assert(snd_pcm_format_linear(src_format->format) &&
  133. snd_pcm_format_linear(dst_format->format), return -ENXIO);
  134. err = snd_pcm_plugin_build(plug, "linear format conversion",
  135. src_format, dst_format,
  136. sizeof(struct linear_priv), &plugin);
  137. if (err < 0)
  138. return err;
  139. data = (struct linear_priv *)plugin->extra_data;
  140. data->conv = conv_index(src_format->format, dst_format->format);
  141. plugin->transfer = linear_transfer;
  142. *r_plugin = plugin;
  143. return 0;
  144. }
  145. #endif