OSDN Git Service

More changes for dmix plugin:
[android-x86/external-alsa-lib.git] / src / pcm / pcm_alaw.c
1 /**
2  * \file pcm/pcm_alaw.c
3  * \ingroup PCM_Plugins
4  * \brief PCM A-Law Conversion Plugin Interface
5  * \author Abramo Bagnara <abramo@alsa-project.org>
6  * \date 2000-2001
7  */
8 /*
9  *  PCM - A-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 #ifndef PIC
34 /* entry for static linking */
35 const char *_snd_module_pcm_alaw = "";
36 #endif
37
38 #ifndef DOC_HIDDEN
39
40 typedef void (*alaw_f)(const snd_pcm_channel_area_t *dst_areas,
41                        snd_pcm_uframes_t dst_offset,
42                        const snd_pcm_channel_area_t *src_areas,
43                        snd_pcm_uframes_t src_offset,
44                        unsigned int channels, snd_pcm_uframes_t frames,
45                        unsigned int getputidx);
46
47 typedef struct {
48         /* This field need to be the first */
49         snd_pcm_plugin_t plug;
50         unsigned int getput_idx;
51         alaw_f func;
52         snd_pcm_format_t sformat;
53 } snd_pcm_alaw_t;
54
55 #endif
56
57 static inline int val_seg(int val)
58 {
59         int r = 1;
60         val >>= 8;
61         if (val & 0xf0) {
62                 val >>= 4;
63                 r += 4;
64         }
65         if (val & 0x0c) {
66                 val >>= 2;
67                 r += 2;
68         }
69         if (val & 0x02)
70                 r += 1;
71         return r;
72 }
73
74 /*
75  * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
76  *
77  * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
78  *
79  *              Linear Input Code       Compressed Code
80  *      ------------------------        ---------------
81  *      0000000wxyza                    000wxyz
82  *      0000001wxyza                    001wxyz
83  *      000001wxyzab                    010wxyz
84  *      00001wxyzabc                    011wxyz
85  *      0001wxyzabcd                    100wxyz
86  *      001wxyzabcde                    101wxyz
87  *      01wxyzabcdef                    110wxyz
88  *      1wxyzabcdefg                    111wxyz
89  *
90  * For further information see John C. Bellamy's Digital Telephony, 1982,
91  * John Wiley & Sons, pps 98-111 and 472-476.
92  */
93
94 static unsigned char s16_to_alaw(int pcm_val)
95 {
96         int             mask;
97         int             seg;
98         unsigned char   aval;
99
100         if (pcm_val >= 0) {
101                 mask = 0xD5;
102         } else {
103                 mask = 0x55;
104                 pcm_val = -pcm_val;
105                 if (pcm_val > 0x7fff)
106                         pcm_val = 0x7fff;
107         }
108
109         if (pcm_val < 256)
110                 aval = pcm_val >> 4;
111         else {
112                 /* Convert the scaled magnitude to segment number. */
113                 seg = val_seg(pcm_val);
114                 aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
115         }
116         return aval ^ mask;
117 }
118
119 /*
120  * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
121  *
122  */
123 static int alaw_to_s16(unsigned char a_val)
124 {
125         int             t;
126         int             seg;
127
128         a_val ^= 0x55;
129         t = a_val & 0x7f;
130         if (t < 16)
131                 t = (t << 4) + 8;
132         else {
133                 seg = (t >> 4) & 0x07;
134                 t = ((t & 0x0f) << 4) + 0x108;
135                 t <<= seg -1;
136         }
137         return ((a_val & 0x80) ? t : -t);
138 }
139
140 #ifndef DOC_HIDDEN
141
142 void snd_pcm_alaw_decode(const snd_pcm_channel_area_t *dst_areas,
143                          snd_pcm_uframes_t dst_offset,
144                          const snd_pcm_channel_area_t *src_areas,
145                          snd_pcm_uframes_t src_offset,
146                          unsigned int channels, snd_pcm_uframes_t frames,
147                          unsigned int putidx)
148 {
149 #define PUT16_LABELS
150 #include "plugin_ops.h"
151 #undef PUT16_LABELS
152         void *put = put16_labels[putidx];
153         unsigned int channel;
154         for (channel = 0; channel < channels; ++channel) {
155                 const unsigned char *src;
156                 char *dst;
157                 int src_step, dst_step;
158                 snd_pcm_uframes_t frames1;
159                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
160                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
161                 src = snd_pcm_channel_area_addr(src_area, src_offset);
162                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
163                 src_step = snd_pcm_channel_area_step(src_area);
164                 dst_step = snd_pcm_channel_area_step(dst_area);
165                 frames1 = frames;
166                 while (frames1-- > 0) {
167                         int16_t sample = alaw_to_s16(*src);
168                         goto *put;
169 #define PUT16_END after
170 #include "plugin_ops.h"
171 #undef PUT16_END
172                 after:
173                         src += src_step;
174                         dst += dst_step;
175                 }
176         }
177 }
178
179 void snd_pcm_alaw_encode(const snd_pcm_channel_area_t *dst_areas,
180                          snd_pcm_uframes_t dst_offset,
181                          const snd_pcm_channel_area_t *src_areas,
182                          snd_pcm_uframes_t src_offset,
183                          unsigned int channels, snd_pcm_uframes_t frames,
184                          unsigned int getidx)
185 {
186 #define GET16_LABELS
187 #include "plugin_ops.h"
188 #undef GET16_LABELS
189         void *get = get16_labels[getidx];
190         unsigned int channel;
191         int16_t sample = 0;
192         for (channel = 0; channel < channels; ++channel) {
193                 const char *src;
194                 char *dst;
195                 int src_step, dst_step;
196                 snd_pcm_uframes_t frames1;
197                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
198                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
199                 src = snd_pcm_channel_area_addr(src_area, src_offset);
200                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
201                 src_step = snd_pcm_channel_area_step(src_area);
202                 dst_step = snd_pcm_channel_area_step(dst_area);
203                 frames1 = frames;
204                 while (frames1-- > 0) {
205                         goto *get;
206 #define GET16_END after
207 #include "plugin_ops.h"
208 #undef GET16_END
209                 after:
210                         *dst = s16_to_alaw(sample);
211                         src += src_step;
212                         dst += dst_step;
213                 }
214         }
215 }
216
217 #endif /* DOC_HIDDEN */
218
219 static int snd_pcm_alaw_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
220 {
221         snd_pcm_alaw_t *alaw = pcm->private_data;
222         int err;
223         snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
224         err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
225                                          &access_mask);
226         if (err < 0)
227                 return err;
228         if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
229                 snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
230                 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
231                                                  &format_mask);
232         } else {
233                 err = _snd_pcm_hw_params_set_format(params, 
234                                                    SND_PCM_FORMAT_A_LAW);
235         }
236         if (err < 0)
237                 return err;
238         err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
239         if (err < 0)
240                 return err;
241         params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
242         return 0;
243 }
244
245 static int snd_pcm_alaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
246 {
247         snd_pcm_alaw_t *alaw = pcm->private_data;
248         snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
249         _snd_pcm_hw_params_any(sparams);
250         _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
251                                    &saccess_mask);
252         _snd_pcm_hw_params_set_format(sparams, alaw->sformat);
253         _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
254         return 0;
255 }
256
257 static int snd_pcm_alaw_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
258                                             snd_pcm_hw_params_t *sparams)
259 {
260         int err;
261         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
262                               SND_PCM_HW_PARBIT_RATE |
263                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
264                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
265                               SND_PCM_HW_PARBIT_PERIODS |
266                               SND_PCM_HW_PARBIT_PERIOD_TIME |
267                               SND_PCM_HW_PARBIT_BUFFER_TIME |
268                               SND_PCM_HW_PARBIT_TICK_TIME);
269         err = _snd_pcm_hw_params_refine(sparams, links, params);
270         if (err < 0)
271                 return err;
272         return 0;
273 }
274         
275 static int snd_pcm_alaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
276                                             snd_pcm_hw_params_t *sparams)
277 {
278         int err;
279         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
280                               SND_PCM_HW_PARBIT_RATE |
281                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
282                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
283                               SND_PCM_HW_PARBIT_PERIODS |
284                               SND_PCM_HW_PARBIT_PERIOD_TIME |
285                               SND_PCM_HW_PARBIT_BUFFER_TIME |
286                               SND_PCM_HW_PARBIT_TICK_TIME);
287         err = _snd_pcm_hw_params_refine(params, links, sparams);
288         if (err < 0)
289                 return err;
290         return 0;
291 }
292
293 static int snd_pcm_alaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
294 {
295         return snd_pcm_hw_refine_slave(pcm, params,
296                                        snd_pcm_alaw_hw_refine_cprepare,
297                                        snd_pcm_alaw_hw_refine_cchange,
298                                        snd_pcm_alaw_hw_refine_sprepare,
299                                        snd_pcm_alaw_hw_refine_schange,
300                                        snd_pcm_plugin_hw_refine_slave);
301 }
302
303 static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
304 {
305         snd_pcm_alaw_t *alaw = pcm->private_data;
306         snd_pcm_format_t format;
307         int err = snd_pcm_hw_params_slave(pcm, params,
308                                           snd_pcm_alaw_hw_refine_cchange,
309                                           snd_pcm_alaw_hw_refine_sprepare,
310                                           snd_pcm_alaw_hw_refine_schange,
311                                           snd_pcm_plugin_hw_params_slave);
312         if (err < 0)
313                 return err;
314
315         err = INTERNAL(snd_pcm_hw_params_get_format)(params, &format);
316         if (err < 0)
317                 return err;
318                 
319         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
320                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
321                         alaw->getput_idx = snd_pcm_linear_get_index(format, SND_PCM_FORMAT_S16);
322                         alaw->func = snd_pcm_alaw_encode;
323                 } else {
324                         alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
325                         alaw->func = snd_pcm_alaw_decode;
326                 }
327         } else {
328                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
329                         alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, format);
330                         alaw->func = snd_pcm_alaw_decode;
331                 } else {
332                         alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
333                         alaw->func = snd_pcm_alaw_encode;
334                 }
335         }
336         return 0;
337 }
338
339 static snd_pcm_uframes_t
340 snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
341                          const snd_pcm_channel_area_t *areas,
342                          snd_pcm_uframes_t offset,
343                          snd_pcm_uframes_t size,
344                          const snd_pcm_channel_area_t *slave_areas,
345                          snd_pcm_uframes_t slave_offset,
346                          snd_pcm_uframes_t *slave_sizep)
347 {
348         snd_pcm_alaw_t *alaw = pcm->private_data;
349         if (size > *slave_sizep)
350                 size = *slave_sizep;
351         alaw->func(slave_areas, slave_offset,
352                    areas, offset, 
353                    pcm->channels, size,
354                    alaw->getput_idx);
355         *slave_sizep = size;
356         return size;
357 }
358
359 static snd_pcm_uframes_t
360 snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
361                         const snd_pcm_channel_area_t *areas,
362                         snd_pcm_uframes_t offset,
363                         snd_pcm_uframes_t size,
364                         const snd_pcm_channel_area_t *slave_areas,
365                         snd_pcm_uframes_t slave_offset,
366                         snd_pcm_uframes_t *slave_sizep)
367 {
368         snd_pcm_alaw_t *alaw = pcm->private_data;
369         if (size > *slave_sizep)
370                 size = *slave_sizep;
371         alaw->func(areas, offset, 
372                    slave_areas, slave_offset,
373                    pcm->channels, size,
374                    alaw->getput_idx);
375         *slave_sizep = size;
376         return size;
377 }
378
379 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
380 {
381         snd_pcm_alaw_t *alaw = pcm->private_data;
382         snd_output_printf(out, "A-Law conversion PCM (%s)\n", 
383                 snd_pcm_format_name(alaw->sformat));
384         if (pcm->setup) {
385                 snd_output_printf(out, "Its setup is:\n");
386                 snd_pcm_dump_setup(pcm, out);
387         }
388         snd_output_printf(out, "Slave: ");
389         snd_pcm_dump(alaw->plug.slave, out);
390 }
391
392 static snd_pcm_ops_t snd_pcm_alaw_ops = {
393         close: snd_pcm_plugin_close,
394         info: snd_pcm_plugin_info,
395         hw_refine: snd_pcm_alaw_hw_refine,
396         hw_params: snd_pcm_alaw_hw_params,
397         hw_free: snd_pcm_plugin_hw_free,
398         sw_params: snd_pcm_plugin_sw_params,
399         channel_info: snd_pcm_plugin_channel_info,
400         dump: snd_pcm_alaw_dump,
401         nonblock: snd_pcm_plugin_nonblock,
402         async: snd_pcm_plugin_async,
403         poll_revents: snd_pcm_plugin_poll_revents,
404         mmap: snd_pcm_plugin_mmap,
405         munmap: snd_pcm_plugin_munmap,
406 };
407
408 /**
409  * \brief Creates a new A-Law conversion PCM
410  * \param pcmp Returns created PCM handle
411  * \param name Name of PCM
412  * \param sformat Slave (destination) format
413  * \param slave Slave PCM handle
414  * \param close_slave When set, the slave PCM handle is closed with copy PCM
415  * \retval zero on success otherwise a negative error code
416  * \warning Using of this function might be dangerous in the sense
417  *          of compatibility reasons. The prototype might be freely
418  *          changed in future.
419  */           
420 int snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
421 {
422         snd_pcm_t *pcm;
423         snd_pcm_alaw_t *alaw;
424         int err;
425         assert(pcmp && slave);
426         if (snd_pcm_format_linear(sformat) != 1 &&
427             sformat != SND_PCM_FORMAT_A_LAW)
428                 return -EINVAL;
429         alaw = calloc(1, sizeof(snd_pcm_alaw_t));
430         if (!alaw) {
431                 return -ENOMEM;
432         }
433         snd_pcm_plugin_init(&alaw->plug);
434         alaw->sformat = sformat;
435         alaw->plug.read = snd_pcm_alaw_read_areas;
436         alaw->plug.write = snd_pcm_alaw_write_areas;
437         alaw->plug.undo_read = snd_pcm_plugin_undo_read_generic;
438         alaw->plug.undo_write = snd_pcm_plugin_undo_write_generic;
439         alaw->plug.slave = slave;
440         alaw->plug.close_slave = close_slave;
441
442         err = snd_pcm_new(&pcm, SND_PCM_TYPE_ALAW, name, slave->stream, slave->mode);
443         if (err < 0) {
444                 free(alaw);
445                 return err;
446         }
447         pcm->ops = &snd_pcm_alaw_ops;
448         pcm->fast_ops = &snd_pcm_plugin_fast_ops;
449         pcm->private_data = alaw;
450         pcm->poll_fd = slave->poll_fd;
451         pcm->poll_events = slave->poll_events;
452         snd_pcm_set_hw_ptr(pcm, &alaw->plug.hw_ptr, -1, 0);
453         snd_pcm_set_appl_ptr(pcm, &alaw->plug.appl_ptr, -1, 0);
454         *pcmp = pcm;
455
456         return 0;
457 }
458
459 /*! \page pcm_plugins
460
461 \section pcm_plugins_alaw Plugin: A-Law
462
463 This plugin converts A-Law samples to linear or linear to A-Law samples
464 from master A-Law conversion PCM to given slave PCM. The channel count,
465 format and rate must match for both of them.
466
467 \code
468 pcm.name {
469         type alaw               # A-Law conversion PCM
470         slave STR               # Slave name
471         # or
472         slave {                 # Slave definition
473                 pcm STR         # Slave PCM name
474                 # or
475                 pcm { }         # Slave PCM definition
476         }
477 }
478 \endcode
479
480 \subsection pcm_plugins_alaw_funcref Function reference
481
482 <UL>
483   <LI>snd_pcm_alaw_open()
484   <LI>_snd_pcm_alaw_open()
485 </UL>
486
487 */
488
489 /**
490  * \brief Creates a new A-Law conversion PCM
491  * \param pcmp Returns created PCM handle
492  * \param name Name of PCM
493  * \param root Root configuration node
494  * \param conf Configuration node with copy PCM description
495  * \param stream Stream type
496  * \param mode Stream mode
497  * \retval zero on success otherwise a negative error code
498  * \warning Using of this function might be dangerous in the sense
499  *          of compatibility reasons. The prototype might be freely
500  *          changed in future.
501  */
502 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name,
503                        snd_config_t *root, snd_config_t *conf, 
504                        snd_pcm_stream_t stream, int mode)
505 {
506         snd_config_iterator_t i, next;
507         int err;
508         snd_pcm_t *spcm;
509         snd_config_t *slave = NULL, *sconf;
510         snd_pcm_format_t sformat;
511         snd_config_for_each(i, next, conf) {
512                 snd_config_t *n = snd_config_iterator_entry(i);
513                 const char *id;
514                 if (snd_config_get_id(n, &id) < 0)
515                         continue;
516                 if (snd_pcm_conf_generic_id(id))
517                         continue;
518                 if (strcmp(id, "slave") == 0) {
519                         slave = n;
520                         continue;
521                 }
522                 SNDERR("Unknown field %s", id);
523                 return -EINVAL;
524         }
525         if (!slave) {
526                 SNDERR("slave is not defined");
527                 return -EINVAL;
528         }
529         err = snd_pcm_slave_conf(root, slave, &sconf, 1,
530                                  SND_PCM_HW_PARAM_FORMAT, SCONF_MANDATORY, &sformat);
531         if (err < 0)
532                 return err;
533         if (snd_pcm_format_linear(sformat) != 1 &&
534             sformat != SND_PCM_FORMAT_A_LAW) {
535                 snd_config_delete(sconf);
536                 SNDERR("invalid slave format");
537                 return -EINVAL;
538         }
539         err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode);
540         snd_config_delete(sconf);
541         if (err < 0)
542                 return err;
543         err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);
544         if (err < 0)
545                 snd_pcm_close(spcm);
546         return err;
547 }
548 #ifndef DOC_HIDDEN
549 SND_DLSYM_BUILD_VERSION(_snd_pcm_alaw_open, SND_PCM_DLSYM_VERSION);
550 #endif