linear.c 5.7 KB

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