linear.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. struct linear_priv *data;
  87. if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
  88. 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. if (snd_BUG_ON(src_channels[channel].area.first % 8 ||
  97. src_channels[channel].area.step % 8))
  98. return -ENXIO;
  99. if (snd_BUG_ON(dst_channels[channel].area.first % 8 ||
  100. dst_channels[channel].area.step % 8))
  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. if (snd_BUG_ON(!r_plugin))
  146. return -ENXIO;
  147. *r_plugin = NULL;
  148. if (snd_BUG_ON(src_format->rate != dst_format->rate))
  149. return -ENXIO;
  150. if (snd_BUG_ON(src_format->channels != dst_format->channels))
  151. return -ENXIO;
  152. if (snd_BUG_ON(!snd_pcm_format_linear(src_format->format) ||
  153. !snd_pcm_format_linear(dst_format->format)))
  154. return -ENXIO;
  155. err = snd_pcm_plugin_build(plug, "linear format conversion",
  156. src_format, dst_format,
  157. sizeof(struct linear_priv), &plugin);
  158. if (err < 0)
  159. return err;
  160. data = (struct linear_priv *)plugin->extra_data;
  161. init_data(data, src_format->format, dst_format->format);
  162. plugin->transfer = linear_transfer;
  163. *r_plugin = plugin;
  164. return 0;
  165. }