linear.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. convert(plugin, src_channels, dst_channels, frames);
  104. return frames;
  105. }
  106. static void init_data(struct linear_priv *data,
  107. snd_pcm_format_t src_format, snd_pcm_format_t dst_format)
  108. {
  109. int src_le, dst_le, src_bytes, dst_bytes;
  110. src_bytes = snd_pcm_format_width(src_format) / 8;
  111. dst_bytes = snd_pcm_format_width(dst_format) / 8;
  112. src_le = snd_pcm_format_little_endian(src_format) > 0;
  113. dst_le = snd_pcm_format_little_endian(dst_format) > 0;
  114. data->dst_bytes = dst_bytes;
  115. data->cvt_endian = src_le != dst_le;
  116. data->copy_bytes = src_bytes < dst_bytes ? src_bytes : dst_bytes;
  117. if (src_le) {
  118. data->copy_ofs = 4 - data->copy_bytes;
  119. data->src_ofs = src_bytes - data->copy_bytes;
  120. } else
  121. data->src_ofs = snd_pcm_format_physical_width(src_format) / 8 -
  122. src_bytes;
  123. if (dst_le)
  124. data->dst_ofs = 4 - data->dst_bytes;
  125. else
  126. data->dst_ofs = snd_pcm_format_physical_width(dst_format) / 8 -
  127. dst_bytes;
  128. if (snd_pcm_format_signed(src_format) !=
  129. snd_pcm_format_signed(dst_format)) {
  130. if (dst_le)
  131. data->flip = (__force u32)cpu_to_le32(0x80000000);
  132. else
  133. data->flip = (__force u32)cpu_to_be32(0x80000000);
  134. }
  135. }
  136. int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug,
  137. struct snd_pcm_plugin_format *src_format,
  138. struct snd_pcm_plugin_format *dst_format,
  139. struct snd_pcm_plugin **r_plugin)
  140. {
  141. int err;
  142. struct linear_priv *data;
  143. struct snd_pcm_plugin *plugin;
  144. if (snd_BUG_ON(!r_plugin))
  145. return -ENXIO;
  146. *r_plugin = NULL;
  147. if (snd_BUG_ON(src_format->rate != dst_format->rate))
  148. return -ENXIO;
  149. if (snd_BUG_ON(src_format->channels != dst_format->channels))
  150. return -ENXIO;
  151. if (snd_BUG_ON(!snd_pcm_format_linear(src_format->format) ||
  152. !snd_pcm_format_linear(dst_format->format)))
  153. return -ENXIO;
  154. err = snd_pcm_plugin_build(plug, "linear format conversion",
  155. src_format, dst_format,
  156. sizeof(struct linear_priv), &plugin);
  157. if (err < 0)
  158. return err;
  159. data = (struct linear_priv *)plugin->extra_data;
  160. init_data(data, src_format->format, dst_format->format);
  161. plugin->transfer = linear_transfer;
  162. *r_plugin = plugin;
  163. return 0;
  164. }