linear.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Linear conversion Plug-In
  3. * Copyright (c) 1999 by Jaroslav Kysela <perex@perex.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. struct linear_priv {
  31. int cvt_endian; /* need endian conversion? */
  32. unsigned int src_ofs; /* byte offset in source format */
  33. unsigned int dst_ofs; /* byte soffset in destination format */
  34. unsigned int copy_ofs; /* byte offset in temporary u32 data */
  35. unsigned int dst_bytes; /* byte size of destination format */
  36. unsigned int copy_bytes; /* bytes to copy per conversion */
  37. unsigned int flip; /* MSB flip for signeness, done after endian conv */
  38. };
  39. static inline void do_convert(struct linear_priv *data,
  40. unsigned char *dst, unsigned char *src)
  41. {
  42. unsigned int tmp = 0;
  43. unsigned char *p = (unsigned char *)&tmp;
  44. memcpy(p + data->copy_ofs, src + data->src_ofs, data->copy_bytes);
  45. if (data->cvt_endian)
  46. tmp = swab32(tmp);
  47. tmp ^= data->flip;
  48. memcpy(dst, p + data->dst_ofs, data->dst_bytes);
  49. }
  50. static void convert(struct snd_pcm_plugin *plugin,
  51. const struct snd_pcm_plugin_channel *src_channels,
  52. struct snd_pcm_plugin_channel *dst_channels,
  53. snd_pcm_uframes_t frames)
  54. {
  55. struct linear_priv *data = (struct linear_priv *)plugin->extra_data;
  56. int channel;
  57. int nchannels = plugin->src_format.channels;
  58. for (channel = 0; channel < nchannels; ++channel) {
  59. char *src;
  60. char *dst;
  61. int src_step, dst_step;
  62. snd_pcm_uframes_t frames1;
  63. if (!src_channels[channel].enabled) {
  64. if (dst_channels[channel].wanted)
  65. snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
  66. dst_channels[channel].enabled = 0;
  67. continue;
  68. }
  69. dst_channels[channel].enabled = 1;
  70. src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
  71. dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
  72. src_step = src_channels[channel].area.step / 8;
  73. dst_step = dst_channels[channel].area.step / 8;
  74. frames1 = frames;
  75. while (frames1-- > 0) {
  76. do_convert(data, dst, src);
  77. src += src_step;
  78. dst += dst_step;
  79. }
  80. }
  81. }
  82. static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin,
  83. const struct snd_pcm_plugin_channel *src_channels,
  84. struct snd_pcm_plugin_channel *dst_channels,
  85. snd_pcm_uframes_t frames)
  86. {
  87. struct linear_priv *data;
  88. snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
  89. data = (struct linear_priv *)plugin->extra_data;
  90. if (frames == 0)
  91. return 0;
  92. #ifdef CONFIG_SND_DEBUG
  93. {
  94. unsigned int channel;
  95. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  96. snd_assert(src_channels[channel].area.first % 8 == 0 &&
  97. src_channels[channel].area.step % 8 == 0,
  98. return -ENXIO);
  99. snd_assert(dst_channels[channel].area.first % 8 == 0 &&
  100. dst_channels[channel].area.step % 8 == 0,
  101. return -ENXIO);
  102. }
  103. }
  104. #endif
  105. convert(plugin, src_channels, dst_channels, frames);
  106. return frames;
  107. }
  108. static void init_data(struct linear_priv *data, int src_format, int dst_format)
  109. {
  110. int src_le, dst_le, src_bytes, dst_bytes;
  111. src_bytes = snd_pcm_format_width(src_format) / 8;
  112. dst_bytes = snd_pcm_format_width(dst_format) / 8;
  113. src_le = snd_pcm_format_little_endian(src_format) > 0;
  114. dst_le = snd_pcm_format_little_endian(dst_format) > 0;
  115. data->dst_bytes = dst_bytes;
  116. data->cvt_endian = src_le != dst_le;
  117. data->copy_bytes = src_bytes < dst_bytes ? src_bytes : dst_bytes;
  118. if (src_le) {
  119. data->copy_ofs = 4 - data->copy_bytes;
  120. data->src_ofs = src_bytes - data->copy_bytes;
  121. } else
  122. data->src_ofs = snd_pcm_format_physical_width(src_format) / 8 -
  123. src_bytes;
  124. if (dst_le)
  125. data->dst_ofs = 4 - data->dst_bytes;
  126. else
  127. data->dst_ofs = snd_pcm_format_physical_width(dst_format) / 8 -
  128. dst_bytes;
  129. if (snd_pcm_format_signed(src_format) !=
  130. snd_pcm_format_signed(dst_format)) {
  131. if (dst_le)
  132. data->flip = cpu_to_le32(0x80000000);
  133. else
  134. data->flip = cpu_to_be32(0x80000000);
  135. }
  136. }
  137. int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug,
  138. struct snd_pcm_plugin_format *src_format,
  139. struct snd_pcm_plugin_format *dst_format,
  140. struct snd_pcm_plugin **r_plugin)
  141. {
  142. int err;
  143. struct linear_priv *data;
  144. struct snd_pcm_plugin *plugin;
  145. snd_assert(r_plugin != NULL, return -ENXIO);
  146. *r_plugin = NULL;
  147. snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
  148. snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
  149. snd_assert(snd_pcm_format_linear(src_format->format) &&
  150. snd_pcm_format_linear(dst_format->format), return -ENXIO);
  151. err = snd_pcm_plugin_build(plug, "linear format conversion",
  152. src_format, dst_format,
  153. sizeof(struct linear_priv), &plugin);
  154. if (err < 0)
  155. return err;
  156. data = (struct linear_priv *)plugin->extra_data;
  157. init_data(data, src_format->format, dst_format->format);
  158. plugin->transfer = linear_transfer;
  159. *r_plugin = plugin;
  160. return 0;
  161. }