linear.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include <linux/time.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include "pcm_plugin.h"
  27. /*
  28. * Basic linear conversion plugin
  29. */
  30. typedef struct linear_private_data {
  31. int conv;
  32. } linear_t;
  33. static void convert(snd_pcm_plugin_t *plugin,
  34. const snd_pcm_plugin_channel_t *src_channels,
  35. snd_pcm_plugin_channel_t *dst_channels,
  36. snd_pcm_uframes_t frames)
  37. {
  38. #define CONV_LABELS
  39. #include "plugin_ops.h"
  40. #undef CONV_LABELS
  41. linear_t *data = (linear_t *)plugin->extra_data;
  42. void *conv = conv_labels[data->conv];
  43. int channel;
  44. int nchannels = plugin->src_format.channels;
  45. for (channel = 0; channel < nchannels; ++channel) {
  46. char *src;
  47. char *dst;
  48. int src_step, dst_step;
  49. snd_pcm_uframes_t frames1;
  50. if (!src_channels[channel].enabled) {
  51. if (dst_channels[channel].wanted)
  52. snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
  53. dst_channels[channel].enabled = 0;
  54. continue;
  55. }
  56. dst_channels[channel].enabled = 1;
  57. src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
  58. dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
  59. src_step = src_channels[channel].area.step / 8;
  60. dst_step = dst_channels[channel].area.step / 8;
  61. frames1 = frames;
  62. while (frames1-- > 0) {
  63. goto *conv;
  64. #define CONV_END after
  65. #include "plugin_ops.h"
  66. #undef CONV_END
  67. after:
  68. src += src_step;
  69. dst += dst_step;
  70. }
  71. }
  72. }
  73. static snd_pcm_sframes_t linear_transfer(snd_pcm_plugin_t *plugin,
  74. const snd_pcm_plugin_channel_t *src_channels,
  75. snd_pcm_plugin_channel_t *dst_channels,
  76. snd_pcm_uframes_t frames)
  77. {
  78. linear_t *data;
  79. snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
  80. data = (linear_t *)plugin->extra_data;
  81. if (frames == 0)
  82. return 0;
  83. #ifdef CONFIG_SND_DEBUG
  84. {
  85. unsigned int channel;
  86. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  87. snd_assert(src_channels[channel].area.first % 8 == 0 &&
  88. src_channels[channel].area.step % 8 == 0,
  89. return -ENXIO);
  90. snd_assert(dst_channels[channel].area.first % 8 == 0 &&
  91. dst_channels[channel].area.step % 8 == 0,
  92. return -ENXIO);
  93. }
  94. }
  95. #endif
  96. convert(plugin, src_channels, dst_channels, frames);
  97. return frames;
  98. }
  99. int conv_index(int src_format, int dst_format)
  100. {
  101. int src_endian, dst_endian, sign, src_width, dst_width;
  102. sign = (snd_pcm_format_signed(src_format) !=
  103. snd_pcm_format_signed(dst_format));
  104. #ifdef SNDRV_LITTLE_ENDIAN
  105. src_endian = snd_pcm_format_big_endian(src_format);
  106. dst_endian = snd_pcm_format_big_endian(dst_format);
  107. #else
  108. src_endian = snd_pcm_format_little_endian(src_format);
  109. dst_endian = snd_pcm_format_little_endian(dst_format);
  110. #endif
  111. if (src_endian < 0)
  112. src_endian = 0;
  113. if (dst_endian < 0)
  114. dst_endian = 0;
  115. src_width = snd_pcm_format_width(src_format) / 8 - 1;
  116. dst_width = snd_pcm_format_width(dst_format) / 8 - 1;
  117. return src_width * 32 + src_endian * 16 + sign * 8 + dst_width * 2 + dst_endian;
  118. }
  119. int snd_pcm_plugin_build_linear(snd_pcm_plug_t *plug,
  120. snd_pcm_plugin_format_t *src_format,
  121. snd_pcm_plugin_format_t *dst_format,
  122. snd_pcm_plugin_t **r_plugin)
  123. {
  124. int err;
  125. struct linear_private_data *data;
  126. snd_pcm_plugin_t *plugin;
  127. snd_assert(r_plugin != NULL, return -ENXIO);
  128. *r_plugin = NULL;
  129. snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
  130. snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
  131. snd_assert(snd_pcm_format_linear(src_format->format) &&
  132. snd_pcm_format_linear(dst_format->format), return -ENXIO);
  133. err = snd_pcm_plugin_build(plug, "linear format conversion",
  134. src_format, dst_format,
  135. sizeof(linear_t), &plugin);
  136. if (err < 0)
  137. return err;
  138. data = (linear_t *)plugin->extra_data;
  139. data->conv = conv_index(src_format->format, dst_format->format);
  140. plugin->transfer = linear_transfer;
  141. *r_plugin = plugin;
  142. return 0;
  143. }