ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / oss / mulaw.c
1 /*
2  *  Mu-Law conversion Plug-In Interface
3  *  Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
4  *                        Uros Bizjak <uros@kss-loka.si>
5  *
6  *  Based on reference implementation by Sun Microsystems, Inc.
7  *
8  *   This library is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU Library General Public License as
10  *   published by the Free Software Foundation; either version 2 of
11  *   the License, or (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU Library General Public License for more details.
17  *
18  *   You should have received a copy of the GNU Library General Public
19  *   License along with this library; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */
23   
24 #include <sound/driver.h>
25 #include <linux/time.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include "pcm_plugin.h"
29
30 #define SIGN_BIT        (0x80)          /* Sign bit for a u-law byte. */
31 #define QUANT_MASK      (0xf)           /* Quantization field mask. */
32 #define NSEGS           (8)             /* Number of u-law segments. */
33 #define SEG_SHIFT       (4)             /* Left shift for segment number. */
34 #define SEG_MASK        (0x70)          /* Segment field mask. */
35
36 static inline int val_seg(int val)
37 {
38         int r = 0;
39         val >>= 7;
40         if (val & 0xf0) {
41                 val >>= 4;
42                 r += 4;
43         }
44         if (val & 0x0c) {
45                 val >>= 2;
46                 r += 2;
47         }
48         if (val & 0x02)
49                 r += 1;
50         return r;
51 }
52
53 #define BIAS            (0x84)          /* Bias for linear code. */
54
55 /*
56  * linear2ulaw() - Convert a linear PCM value to u-law
57  *
58  * In order to simplify the encoding process, the original linear magnitude
59  * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
60  * (33 - 8191). The result can be seen in the following encoding table:
61  *
62  *      Biased Linear Input Code        Compressed Code
63  *      ------------------------        ---------------
64  *      00000001wxyza                   000wxyz
65  *      0000001wxyzab                   001wxyz
66  *      000001wxyzabc                   010wxyz
67  *      00001wxyzabcd                   011wxyz
68  *      0001wxyzabcde                   100wxyz
69  *      001wxyzabcdef                   101wxyz
70  *      01wxyzabcdefg                   110wxyz
71  *      1wxyzabcdefgh                   111wxyz
72  *
73  * Each biased linear code has a leading 1 which identifies the segment
74  * number. The value of the segment number is equal to 7 minus the number
75  * of leading 0's. The quantization interval is directly available as the
76  * four bits wxyz.  * The trailing bits (a - h) are ignored.
77  *
78  * Ordinarily the complement of the resulting code word is used for
79  * transmission, and so the code word is complemented before it is returned.
80  *
81  * For further information see John C. Bellamy's Digital Telephony, 1982,
82  * John Wiley & Sons, pps 98-111 and 472-476.
83  */
84 static unsigned char linear2ulaw(int pcm_val)   /* 2's complement (16-bit range) */
85 {
86         int mask;
87         int seg;
88         unsigned char uval;
89
90         /* Get the sign and the magnitude of the value. */
91         if (pcm_val < 0) {
92                 pcm_val = BIAS - pcm_val;
93                 mask = 0x7F;
94         } else {
95                 pcm_val += BIAS;
96                 mask = 0xFF;
97         }
98         if (pcm_val > 0x7FFF)
99                 pcm_val = 0x7FFF;
100
101         /* Convert the scaled magnitude to segment number. */
102         seg = val_seg(pcm_val);
103
104         /*
105          * Combine the sign, segment, quantization bits;
106          * and complement the code word.
107          */
108         uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
109         return uval ^ mask;
110 }
111
112 /*
113  * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
114  *
115  * First, a biased linear code is derived from the code word. An unbiased
116  * output can then be obtained by subtracting 33 from the biased code.
117  *
118  * Note that this function expects to be passed the complement of the
119  * original code word. This is in keeping with ISDN conventions.
120  */
121 static int ulaw2linear(unsigned char u_val)
122 {
123         int t;
124
125         /* Complement to obtain normal u-law value. */
126         u_val = ~u_val;
127
128         /*
129          * Extract and bias the quantization bits. Then
130          * shift up by the segment number and subtract out the bias.
131          */
132         t = ((u_val & QUANT_MASK) << 3) + BIAS;
133         t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
134
135         return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
136 }
137
138 /*
139  *  Basic Mu-Law plugin
140  */
141
142 typedef void (*mulaw_f)(snd_pcm_plugin_t *plugin,
143                         const snd_pcm_plugin_channel_t *src_channels,
144                         snd_pcm_plugin_channel_t *dst_channels,
145                         snd_pcm_uframes_t frames);
146
147 typedef struct mulaw_private_data {
148         mulaw_f func;
149         int conv;
150 } mulaw_t;
151
152 static void mulaw_decode(snd_pcm_plugin_t *plugin,
153                         const snd_pcm_plugin_channel_t *src_channels,
154                         snd_pcm_plugin_channel_t *dst_channels,
155                         snd_pcm_uframes_t frames)
156 {
157 #define PUT_S16_LABELS
158 #include "plugin_ops.h"
159 #undef PUT_S16_LABELS
160         mulaw_t *data = (mulaw_t *)plugin->extra_data;
161         void *put = put_s16_labels[data->conv];
162         int channel;
163         int nchannels = plugin->src_format.channels;
164         for (channel = 0; channel < nchannels; ++channel) {
165                 char *src;
166                 char *dst;
167                 int src_step, dst_step;
168                 snd_pcm_uframes_t frames1;
169                 if (!src_channels[channel].enabled) {
170                         if (dst_channels[channel].wanted)
171                                 snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
172                         dst_channels[channel].enabled = 0;
173                         continue;
174                 }
175                 dst_channels[channel].enabled = 1;
176                 src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
177                 dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
178                 src_step = src_channels[channel].area.step / 8;
179                 dst_step = dst_channels[channel].area.step / 8;
180                 frames1 = frames;
181                 while (frames1-- > 0) {
182                         signed short sample = ulaw2linear(*src);
183                         goto *put;
184 #define PUT_S16_END after
185 #include "plugin_ops.h"
186 #undef PUT_S16_END
187                 after:
188                         src += src_step;
189                         dst += dst_step;
190                 }
191         }
192 }
193
194 static void mulaw_encode(snd_pcm_plugin_t *plugin,
195                         const snd_pcm_plugin_channel_t *src_channels,
196                         snd_pcm_plugin_channel_t *dst_channels,
197                         snd_pcm_uframes_t frames)
198 {
199 #define GET_S16_LABELS
200 #include "plugin_ops.h"
201 #undef GET_S16_LABELS
202         mulaw_t *data = (mulaw_t *)plugin->extra_data;
203         void *get = get_s16_labels[data->conv];
204         int channel;
205         int nchannels = plugin->src_format.channels;
206         signed short sample = 0;
207         for (channel = 0; channel < nchannels; ++channel) {
208                 char *src;
209                 char *dst;
210                 int src_step, dst_step;
211                 snd_pcm_uframes_t frames1;
212                 if (!src_channels[channel].enabled) {
213                         if (dst_channels[channel].wanted)
214                                 snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
215                         dst_channels[channel].enabled = 0;
216                         continue;
217                 }
218                 dst_channels[channel].enabled = 1;
219                 src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
220                 dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
221                 src_step = src_channels[channel].area.step / 8;
222                 dst_step = dst_channels[channel].area.step / 8;
223                 frames1 = frames;
224                 while (frames1-- > 0) {
225                         goto *get;
226 #define GET_S16_END after
227 #include "plugin_ops.h"
228 #undef GET_S16_END
229                 after:
230                         *dst = linear2ulaw(sample);
231                         src += src_step;
232                         dst += dst_step;
233                 }
234         }
235 }
236
237 static snd_pcm_sframes_t mulaw_transfer(snd_pcm_plugin_t *plugin,
238                               const snd_pcm_plugin_channel_t *src_channels,
239                               snd_pcm_plugin_channel_t *dst_channels,
240                               snd_pcm_uframes_t frames)
241 {
242         mulaw_t *data;
243
244         snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
245         if (frames == 0)
246                 return 0;
247 #ifdef CONFIG_SND_DEBUG
248         {
249                 unsigned int channel;
250                 for (channel = 0; channel < plugin->src_format.channels; channel++) {
251                         snd_assert(src_channels[channel].area.first % 8 == 0 &&
252                                    src_channels[channel].area.step % 8 == 0,
253                                    return -ENXIO);
254                         snd_assert(dst_channels[channel].area.first % 8 == 0 &&
255                                    dst_channels[channel].area.step % 8 == 0,
256                                    return -ENXIO);
257                 }
258         }
259 #endif
260         data = (mulaw_t *)plugin->extra_data;
261         data->func(plugin, src_channels, dst_channels, frames);
262         return frames;
263 }
264
265 int snd_pcm_plugin_build_mulaw(snd_pcm_plug_t *plug,
266                                snd_pcm_plugin_format_t *src_format,
267                                snd_pcm_plugin_format_t *dst_format,
268                                snd_pcm_plugin_t **r_plugin)
269 {
270         int err;
271         mulaw_t *data;
272         snd_pcm_plugin_t *plugin;
273         snd_pcm_plugin_format_t *format;
274         mulaw_f func;
275
276         snd_assert(r_plugin != NULL, return -ENXIO);
277         *r_plugin = NULL;
278
279         snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
280         snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
281
282         if (dst_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
283                 format = src_format;
284                 func = mulaw_encode;
285         }
286         else if (src_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
287                 format = dst_format;
288                 func = mulaw_decode;
289         }
290         else {
291                 snd_BUG();
292                 return -EINVAL;
293         }
294         snd_assert(snd_pcm_format_linear(format->format) != 0, return -ENXIO);
295
296         err = snd_pcm_plugin_build(plug, "Mu-Law<->linear conversion",
297                                    src_format, dst_format,
298                                    sizeof(mulaw_t), &plugin);
299         if (err < 0)
300                 return err;
301         data = (mulaw_t*)plugin->extra_data;
302         data->func = func;
303         data->conv = getput_index(format->format);
304         plugin->transfer = mulaw_transfer;
305         *r_plugin = plugin;
306         return 0;
307 }