Initial Import
[profile/ivi/alsa-lib.git] / src / pcm / pcm_mulaw.c
1 /**
2  * \file pcm/pcm_mulaw.c
3  * \ingroup PCM_Plugins
4  * \brief PCM Mu-Law Conversion Plugin Interface
5  * \author Abramo Bagnara <abramo@alsa-project.org>
6  * \date 2000-2001
7  */
8 /*
9  *  PCM - Mu-Law conversion
10  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
11  *
12  *
13  *   This library is free software; you can redistribute it and/or modify
14  *   it under the terms of the GNU Lesser General Public License as
15  *   published by the Free Software Foundation; either version 2.1 of
16  *   the License, or (at your option) any later version.
17  *
18  *   This program is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU Lesser General Public License for more details.
22  *
23  *   You should have received a copy of the GNU Lesser General Public
24  *   License along with this library; if not, write to the Free Software
25  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26  *
27  */
28   
29 #include <byteswap.h>
30 #include "pcm_local.h"
31 #include "pcm_plugin.h"
32
33 #include "plugin_ops.h"
34
35 #ifndef PIC
36 /* entry for static linking */
37 const char *_snd_module_pcm_mulaw = "";
38 #endif
39
40 #ifndef DOC_HIDDEN
41
42 typedef void (*mulaw_f)(const snd_pcm_channel_area_t *src_areas,
43                         snd_pcm_uframes_t src_offset,
44                         const snd_pcm_channel_area_t *dst_areas,
45                         snd_pcm_uframes_t dst_offset,
46                         unsigned int channels, snd_pcm_uframes_t frames,
47                         unsigned int getputidx);
48
49 typedef struct {
50         /* This field need to be the first */
51         snd_pcm_plugin_t plug;
52         unsigned int getput_idx;
53         mulaw_f func;
54         snd_pcm_format_t sformat;
55 } snd_pcm_mulaw_t;
56
57 #endif
58
59 static inline int val_seg(int val)
60 {
61         int r = 0;
62         val >>= 7;
63         if (val & 0xf0) {
64                 val >>= 4;
65                 r += 4;
66         }
67         if (val & 0x0c) {
68                 val >>= 2;
69                 r += 2;
70         }
71         if (val & 0x02)
72                 r += 1;
73         return r;
74 }
75
76 /*
77  * s16_to_ulaw() - Convert a linear PCM value to u-law
78  *
79  * In order to simplify the encoding process, the original linear magnitude
80  * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
81  * (33 - 8191). The result can be seen in the following encoding table:
82  *
83  *      Biased Linear Input Code        Compressed Code
84  *      ------------------------        ---------------
85  *      00000001wxyza                   000wxyz
86  *      0000001wxyzab                   001wxyz
87  *      000001wxyzabc                   010wxyz
88  *      00001wxyzabcd                   011wxyz
89  *      0001wxyzabcde                   100wxyz
90  *      001wxyzabcdef                   101wxyz
91  *      01wxyzabcdefg                   110wxyz
92  *      1wxyzabcdefgh                   111wxyz
93  *
94  * Each biased linear code has a leading 1 which identifies the segment
95  * number. The value of the segment number is equal to 7 minus the number
96  * of leading 0's. The quantization interval is directly available as the
97  * four bits wxyz.  * The trailing bits (a - h) are ignored.
98  *
99  * Ordinarily the complement of the resulting code word is used for
100  * transmission, and so the code word is complemented before it is returned.
101  *
102  * For further information see John C. Bellamy's Digital Telephony, 1982,
103  * John Wiley & Sons, pps 98-111 and 472-476.
104  */
105
106 static unsigned char s16_to_ulaw(int pcm_val)   /* 2's complement (16-bit range) */
107 {
108         int mask;
109         int seg;
110         unsigned char uval;
111
112         if (pcm_val < 0) {
113                 pcm_val = 0x84 - pcm_val;
114                 mask = 0x7f;
115         } else {
116                 pcm_val += 0x84;
117                 mask = 0xff;
118         }
119         if (pcm_val > 0x7fff)
120                 pcm_val = 0x7fff;
121
122         /* Convert the scaled magnitude to segment number. */
123         seg = val_seg(pcm_val);
124
125         /*
126          * Combine the sign, segment, quantization bits;
127          * and complement the code word.
128          */
129         uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
130         return uval ^ mask;
131 }
132
133 /*
134  * ulaw_to_s16() - Convert a u-law value to 16-bit linear PCM
135  *
136  * First, a biased linear code is derived from the code word. An unbiased
137  * output can then be obtained by subtracting 33 from the biased code.
138  *
139  * Note that this function expects to be passed the complement of the
140  * original code word. This is in keeping with ISDN conventions.
141  */
142 static int ulaw_to_s16(unsigned char u_val)
143 {
144         int t;
145
146         /* Complement to obtain normal u-law value. */
147         u_val = ~u_val;
148
149         /*
150          * Extract and bias the quantization bits. Then
151          * shift up by the segment number and subtract out the bias.
152          */
153         t = ((u_val & 0x0f) << 3) + 0x84;
154         t <<= (u_val & 0x70) >> 4;
155
156         return ((u_val & 0x80) ? (0x84 - t) : (t - 0x84));
157 }
158
159 #ifndef DOC_HIDDEN
160
161 void snd_pcm_mulaw_decode(const snd_pcm_channel_area_t *dst_areas,
162                           snd_pcm_uframes_t dst_offset,
163                           const snd_pcm_channel_area_t *src_areas,
164                           snd_pcm_uframes_t src_offset,
165                           unsigned int channels, snd_pcm_uframes_t frames,
166                           unsigned int putidx)
167 {
168 #define PUT16_LABELS
169 #include "plugin_ops.h"
170 #undef PUT16_LABELS
171         void *put = put16_labels[putidx];
172         unsigned int channel;
173         for (channel = 0; channel < channels; ++channel) {
174                 const unsigned char *src;
175                 char *dst;
176                 int src_step, dst_step;
177                 snd_pcm_uframes_t frames1;
178                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
179                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
180                 src = snd_pcm_channel_area_addr(src_area, src_offset);
181                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
182                 src_step = snd_pcm_channel_area_step(src_area);
183                 dst_step = snd_pcm_channel_area_step(dst_area);
184                 frames1 = frames;
185                 while (frames1-- > 0) {
186                         int16_t sample = ulaw_to_s16(*src);
187                         goto *put;
188 #define PUT16_END after
189 #include "plugin_ops.h"
190 #undef PUT16_END
191                 after:
192                         src += src_step;
193                         dst += dst_step;
194                 }
195         }
196 }
197
198 void snd_pcm_mulaw_encode(const snd_pcm_channel_area_t *dst_areas,
199                           snd_pcm_uframes_t dst_offset,
200                           const snd_pcm_channel_area_t *src_areas,
201                           snd_pcm_uframes_t src_offset,
202                           unsigned int channels, snd_pcm_uframes_t frames,
203                           unsigned int getidx)
204 {
205 #define GET16_LABELS
206 #include "plugin_ops.h"
207 #undef GET16_LABELS
208         void *get = get16_labels[getidx];
209         unsigned int channel;
210         int16_t sample = 0;
211         for (channel = 0; channel < channels; ++channel) {
212                 const char *src;
213                 char *dst;
214                 int src_step, dst_step;
215                 snd_pcm_uframes_t frames1;
216                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
217                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
218                 src = snd_pcm_channel_area_addr(src_area, src_offset);
219                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
220                 src_step = snd_pcm_channel_area_step(src_area);
221                 dst_step = snd_pcm_channel_area_step(dst_area);
222                 frames1 = frames;
223                 while (frames1-- > 0) {
224                         goto *get;
225 #define GET16_END after
226 #include "plugin_ops.h"
227 #undef GET16_END
228                 after:
229                         *dst = s16_to_ulaw(sample);
230                         src += src_step;
231                         dst += dst_step;
232                 }
233         }
234 }
235
236 #endif /* DOC_HIDDEN */
237
238 static int snd_pcm_mulaw_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
239 {
240         snd_pcm_mulaw_t *mulaw = pcm->private_data;
241         int err;
242         snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
243         err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
244                                          &access_mask);
245         if (err < 0)
246                 return err;
247         if (mulaw->sformat == SND_PCM_FORMAT_MU_LAW) {
248                 snd_pcm_format_mask_t format_mask= { SND_PCM_FMTBIT_LINEAR };
249                 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
250                                                  &format_mask);
251         } else {
252                 err = _snd_pcm_hw_params_set_format(params,
253                                                    SND_PCM_FORMAT_MU_LAW);
254         }
255         err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
256         if (err < 0)
257                 return err;
258         params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
259         return 0;
260 }
261
262 static int snd_pcm_mulaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
263 {
264         snd_pcm_mulaw_t *mulaw = pcm->private_data;
265         snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
266         _snd_pcm_hw_params_any(sparams);
267         _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
268                                    &saccess_mask);
269         _snd_pcm_hw_params_set_format(sparams, mulaw->sformat);
270         _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
271         return 0;
272 }
273
274 static int snd_pcm_mulaw_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
275                                             snd_pcm_hw_params_t *sparams)
276 {
277         int err;
278         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
279                               SND_PCM_HW_PARBIT_RATE |
280                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
281                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
282                               SND_PCM_HW_PARBIT_PERIODS |
283                               SND_PCM_HW_PARBIT_PERIOD_TIME |
284                               SND_PCM_HW_PARBIT_BUFFER_TIME |
285                               SND_PCM_HW_PARBIT_TICK_TIME);
286         err = _snd_pcm_hw_params_refine(sparams, links, params);
287         if (err < 0)
288                 return err;
289         return 0;
290 }
291         
292 static int snd_pcm_mulaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
293                                             snd_pcm_hw_params_t *sparams)
294 {
295         int err;
296         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
297                               SND_PCM_HW_PARBIT_RATE |
298                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
299                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
300                               SND_PCM_HW_PARBIT_PERIODS |
301                               SND_PCM_HW_PARBIT_PERIOD_TIME |
302                               SND_PCM_HW_PARBIT_BUFFER_TIME |
303                               SND_PCM_HW_PARBIT_TICK_TIME);
304         err = _snd_pcm_hw_params_refine(params, links, sparams);
305         if (err < 0)
306                 return err;
307         return 0;
308 }
309
310 static int snd_pcm_mulaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
311 {
312         return snd_pcm_hw_refine_slave(pcm, params,
313                                        snd_pcm_mulaw_hw_refine_cprepare,
314                                        snd_pcm_mulaw_hw_refine_cchange,
315                                        snd_pcm_mulaw_hw_refine_sprepare,
316                                        snd_pcm_mulaw_hw_refine_schange,
317                                        snd_pcm_generic_hw_refine);
318 }
319
320 static int snd_pcm_mulaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
321 {
322         snd_pcm_mulaw_t *mulaw = pcm->private_data;
323         snd_pcm_format_t format;
324         int err = snd_pcm_hw_params_slave(pcm, params,
325                                           snd_pcm_mulaw_hw_refine_cchange,
326                                           snd_pcm_mulaw_hw_refine_sprepare,
327                                           snd_pcm_mulaw_hw_refine_schange,
328                                           snd_pcm_generic_hw_params);
329         if (err < 0)
330                 return err;
331
332         err = INTERNAL(snd_pcm_hw_params_get_format)(params, &format);
333         if (err < 0)
334                 return err;
335
336         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
337                 if (mulaw->sformat == SND_PCM_FORMAT_MU_LAW) {
338                         mulaw->getput_idx = snd_pcm_linear_get_index(format, SND_PCM_FORMAT_S16);
339                         mulaw->func = snd_pcm_mulaw_encode;
340                 } else {
341                         mulaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, mulaw->sformat);
342                         mulaw->func = snd_pcm_mulaw_decode;
343                 }
344         } else {
345                 if (mulaw->sformat == SND_PCM_FORMAT_MU_LAW) {
346                         mulaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, format);
347                         mulaw->func = snd_pcm_mulaw_decode;
348                 } else {
349                         mulaw->getput_idx = snd_pcm_linear_get_index(mulaw->sformat, SND_PCM_FORMAT_S16);
350                         mulaw->func = snd_pcm_mulaw_encode;
351                 }
352         }
353         return 0;
354 }
355
356 static snd_pcm_uframes_t
357 snd_pcm_mulaw_write_areas(snd_pcm_t *pcm,
358                           const snd_pcm_channel_area_t *areas,
359                           snd_pcm_uframes_t offset,
360                           snd_pcm_uframes_t size,
361                           const snd_pcm_channel_area_t *slave_areas,
362                           snd_pcm_uframes_t slave_offset,
363                           snd_pcm_uframes_t *slave_sizep)
364 {
365         snd_pcm_mulaw_t *mulaw = pcm->private_data;
366         if (size > *slave_sizep)
367                 size = *slave_sizep;
368         mulaw->func(slave_areas, slave_offset,
369                     areas, offset, 
370                     pcm->channels, size,
371                     mulaw->getput_idx);
372         *slave_sizep = size;
373         return size;
374 }
375
376 static snd_pcm_uframes_t
377 snd_pcm_mulaw_read_areas(snd_pcm_t *pcm,
378                          const snd_pcm_channel_area_t *areas,
379                          snd_pcm_uframes_t offset,
380                          snd_pcm_uframes_t size,
381                          const snd_pcm_channel_area_t *slave_areas,
382                          snd_pcm_uframes_t slave_offset,
383                          snd_pcm_uframes_t *slave_sizep)
384 {
385         snd_pcm_mulaw_t *mulaw = pcm->private_data;
386         if (size > *slave_sizep)
387                 size = *slave_sizep;
388         mulaw->func(areas, offset, 
389                     slave_areas, slave_offset,
390                     pcm->channels, size,
391                     mulaw->getput_idx);
392         *slave_sizep = size;
393         return size;
394 }
395
396 static void snd_pcm_mulaw_dump(snd_pcm_t *pcm, snd_output_t *out)
397 {
398         snd_pcm_mulaw_t *mulaw = pcm->private_data;
399         snd_output_printf(out, "Mu-Law conversion PCM (%s)\n", 
400                 snd_pcm_format_name(mulaw->sformat));
401         if (pcm->setup) {
402                 snd_output_printf(out, "Its setup is:\n");
403                 snd_pcm_dump_setup(pcm, out);
404         }
405         snd_output_printf(out, "Slave: ");
406         snd_pcm_dump(mulaw->plug.gen.slave, out);
407 }
408
409 static const snd_pcm_ops_t snd_pcm_mulaw_ops = {
410         .close = snd_pcm_generic_close,
411         .info = snd_pcm_generic_info,
412         .hw_refine = snd_pcm_mulaw_hw_refine,
413         .hw_params = snd_pcm_mulaw_hw_params,
414         .hw_free = snd_pcm_generic_hw_free,
415         .sw_params = snd_pcm_generic_sw_params,
416         .channel_info = snd_pcm_generic_channel_info,
417         .dump = snd_pcm_mulaw_dump,
418         .nonblock = snd_pcm_generic_nonblock,
419         .async = snd_pcm_generic_async,
420         .mmap = snd_pcm_generic_mmap,
421         .munmap = snd_pcm_generic_munmap,
422 };
423
424 /**
425  * \brief Creates a new Mu-Law conversion PCM
426  * \param pcmp Returns created PCM handle
427  * \param name Name of PCM
428  * \param sformat Slave (destination) format
429  * \param slave Slave PCM handle
430  * \param close_slave When set, the slave PCM handle is closed with copy PCM
431  * \retval zero on success otherwise a negative error code
432  * \warning Using of this function might be dangerous in the sense
433  *          of compatibility reasons. The prototype might be freely
434  *          changed in future.
435  */
436 int snd_pcm_mulaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
437 {
438         snd_pcm_t *pcm;
439         snd_pcm_mulaw_t *mulaw;
440         int err;
441         assert(pcmp && slave);
442         if (snd_pcm_format_linear(sformat) != 1 &&
443             sformat != SND_PCM_FORMAT_MU_LAW)
444                 return -EINVAL;
445         mulaw = calloc(1, sizeof(snd_pcm_mulaw_t));
446         if (!mulaw) {
447                 return -ENOMEM;
448         }
449         snd_pcm_plugin_init(&mulaw->plug);
450         mulaw->sformat = sformat;
451         mulaw->plug.read = snd_pcm_mulaw_read_areas;
452         mulaw->plug.write = snd_pcm_mulaw_write_areas;
453         mulaw->plug.undo_read = snd_pcm_plugin_undo_read_generic;
454         mulaw->plug.undo_write = snd_pcm_plugin_undo_write_generic;
455         mulaw->plug.gen.slave = slave;
456         mulaw->plug.gen.close_slave = close_slave;
457
458         err = snd_pcm_new(&pcm, SND_PCM_TYPE_MULAW, name, slave->stream, slave->mode);
459         if (err < 0) {
460                 free(mulaw);
461                 return err;
462         }
463         pcm->ops = &snd_pcm_mulaw_ops;
464         pcm->fast_ops = &snd_pcm_plugin_fast_ops;
465         pcm->private_data = mulaw;
466         pcm->poll_fd = slave->poll_fd;
467         pcm->poll_events = slave->poll_events;
468         pcm->monotonic = slave->monotonic;
469         snd_pcm_set_hw_ptr(pcm, &mulaw->plug.hw_ptr, -1, 0);
470         snd_pcm_set_appl_ptr(pcm, &mulaw->plug.appl_ptr, -1, 0);
471         *pcmp = pcm;
472
473         return 0;
474 }
475
476 /*! \page pcm_plugins
477
478 \section pcm_plugins_mulaw Plugin: Mu-Law
479
480 This plugin converts Mu-Law samples to linear or linear to Mu-Law samples
481 from master Mu-Law conversion PCM to given slave PCM. The channel count,
482 format and rate must match for both of them.
483
484 \code
485 pcm.name {
486         type mulaw              # Mu-Law conversion PCM
487         slave STR               # Slave name
488         # or
489         slave {                 # Slave definition
490                 pcm STR         # Slave PCM name
491                 # or
492                 pcm { }         # Slave PCM definition
493                 format STR      # Slave format
494         }
495 }
496 \endcode
497
498 \subsection pcm_plugins_mulaw_funcref Function reference
499
500 <UL>
501   <LI>snd_pcm_mulaw_open()
502   <LI>_snd_pcm_mulaw_open()
503 </UL>
504
505 */
506
507 /**
508  * \brief Creates a new Mu-Law conversion PCM
509  * \param pcmp Returns created PCM handle
510  * \param name Name of PCM
511  * \param root Root configuration node
512  * \param conf Configuration node with copy PCM description
513  * \param stream Stream type
514  * \param mode Stream mode
515  * \retval zero on success otherwise a negative error code
516  * \warning Using of this function might be dangerous in the sense
517  *          of compatibility reasons. The prototype might be freely
518  *          changed in future.
519  */
520 int _snd_pcm_mulaw_open(snd_pcm_t **pcmp, const char *name,
521                         snd_config_t *root, snd_config_t *conf, 
522                         snd_pcm_stream_t stream, int mode)
523 {
524         snd_config_iterator_t i, next;
525         int err;
526         snd_pcm_t *spcm;
527         snd_config_t *slave = NULL, *sconf;
528         snd_pcm_format_t sformat;
529         snd_config_for_each(i, next, conf) {
530                 snd_config_t *n = snd_config_iterator_entry(i);
531                 const char *id;
532                 if (snd_config_get_id(n, &id) < 0)
533                         continue;
534                 if (snd_pcm_conf_generic_id(id))
535                         continue;
536                 if (strcmp(id, "slave") == 0) {
537                         slave = n;
538                         continue;
539                 }
540                 SNDERR("Unknown field %s", id);
541                 return -EINVAL;
542         }
543         if (!slave) {
544                 SNDERR("slave is not defined");
545                 return -EINVAL;
546         }
547         err = snd_pcm_slave_conf(root, slave, &sconf, 1,
548                                  SND_PCM_HW_PARAM_FORMAT, SCONF_MANDATORY, &sformat);
549         if (err < 0)
550                 return err;
551         if (snd_pcm_format_linear(sformat) != 1 &&
552             sformat != SND_PCM_FORMAT_MU_LAW) {
553                 snd_config_delete(sconf);
554                 SNDERR("invalid slave format");
555                 return -EINVAL;
556         }
557         err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode, conf);
558         snd_config_delete(sconf);
559         if (err < 0)
560                 return err;
561         err = snd_pcm_mulaw_open(pcmp, name, sformat, spcm, 1);
562         if (err < 0)
563                 snd_pcm_close(spcm);
564         return err;
565 }
566 #ifndef DOC_HIDDEN
567 SND_DLSYM_BUILD_VERSION(_snd_pcm_mulaw_open, SND_PCM_DLSYM_VERSION);
568 #endif