OSDN Git Service

Added initial comments for PCM plugins
[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         int err = snd_pcm_hw_params_slave(pcm, params,
307                                           snd_pcm_alaw_hw_refine_cchange,
308                                           snd_pcm_alaw_hw_refine_sprepare,
309                                           snd_pcm_alaw_hw_refine_schange,
310                                           snd_pcm_plugin_hw_params_slave);
311         if (err < 0)
312                 return err;
313
314         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
315                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
316                         alaw->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
317                         alaw->func = snd_pcm_alaw_encode;
318                 } else {
319                         alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
320                         alaw->func = snd_pcm_alaw_decode;
321                 }
322         } else {
323                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
324                         alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
325                         alaw->func = snd_pcm_alaw_decode;
326                 } else {
327                         alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
328                         alaw->func = snd_pcm_alaw_encode;
329                 }
330         }
331         return 0;
332 }
333
334 static snd_pcm_uframes_t
335 snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
336                          const snd_pcm_channel_area_t *areas,
337                          snd_pcm_uframes_t offset,
338                          snd_pcm_uframes_t size,
339                          const snd_pcm_channel_area_t *slave_areas,
340                          snd_pcm_uframes_t slave_offset,
341                          snd_pcm_uframes_t *slave_sizep)
342 {
343         snd_pcm_alaw_t *alaw = pcm->private_data;
344         if (size > *slave_sizep)
345                 size = *slave_sizep;
346         alaw->func(slave_areas, slave_offset,
347                    areas, offset, 
348                    pcm->channels, size,
349                    alaw->getput_idx);
350         *slave_sizep = size;
351         return size;
352 }
353
354 static snd_pcm_uframes_t
355 snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
356                         const snd_pcm_channel_area_t *areas,
357                         snd_pcm_uframes_t offset,
358                         snd_pcm_uframes_t size,
359                         const snd_pcm_channel_area_t *slave_areas,
360                         snd_pcm_uframes_t slave_offset,
361                         snd_pcm_uframes_t *slave_sizep)
362 {
363         snd_pcm_alaw_t *alaw = pcm->private_data;
364         if (size > *slave_sizep)
365                 size = *slave_sizep;
366         alaw->func(areas, offset, 
367                    slave_areas, slave_offset,
368                    pcm->channels, size,
369                    alaw->getput_idx);
370         *slave_sizep = size;
371         return size;
372 }
373
374 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
375 {
376         snd_pcm_alaw_t *alaw = pcm->private_data;
377         snd_output_printf(out, "A-Law conversion PCM (%s)\n", 
378                 snd_pcm_format_name(alaw->sformat));
379         if (pcm->setup) {
380                 snd_output_printf(out, "Its setup is:\n");
381                 snd_pcm_dump_setup(pcm, out);
382         }
383         snd_output_printf(out, "Slave: ");
384         snd_pcm_dump(alaw->plug.slave, out);
385 }
386
387 static snd_pcm_ops_t snd_pcm_alaw_ops = {
388         close: snd_pcm_plugin_close,
389         info: snd_pcm_plugin_info,
390         hw_refine: snd_pcm_alaw_hw_refine,
391         hw_params: snd_pcm_alaw_hw_params,
392         hw_free: snd_pcm_plugin_hw_free,
393         sw_params: snd_pcm_plugin_sw_params,
394         channel_info: snd_pcm_plugin_channel_info,
395         dump: snd_pcm_alaw_dump,
396         nonblock: snd_pcm_plugin_nonblock,
397         async: snd_pcm_plugin_async,
398         mmap: snd_pcm_plugin_mmap,
399         munmap: snd_pcm_plugin_munmap,
400 };
401
402 /**
403  * \brief Creates a new A-Law conversion PCM
404  * \param pcmp Returns created PCM handle
405  * \param name Name of PCM
406  * \param sformat Slave (destination) format
407  * \param slave Slave PCM handle
408  * \param close_slave When set, the slave PCM handle is closed with copy PCM
409  * \retval zero on success otherwise a negative error code
410  * \warning Using of this function might be dangerous in the sense
411  *          of compatibility reasons. The prototype might be freely
412  *          changed in future.
413  */           
414 int snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
415 {
416         snd_pcm_t *pcm;
417         snd_pcm_alaw_t *alaw;
418         int err;
419         assert(pcmp && slave);
420         if (snd_pcm_format_linear(sformat) != 1 &&
421             sformat != SND_PCM_FORMAT_A_LAW)
422                 return -EINVAL;
423         alaw = calloc(1, sizeof(snd_pcm_alaw_t));
424         if (!alaw) {
425                 return -ENOMEM;
426         }
427         alaw->sformat = sformat;
428         alaw->plug.read = snd_pcm_alaw_read_areas;
429         alaw->plug.write = snd_pcm_alaw_write_areas;
430         alaw->plug.slave = slave;
431         alaw->plug.close_slave = close_slave;
432
433         err = snd_pcm_new(&pcm, SND_PCM_TYPE_ALAW, name, slave->stream, slave->mode);
434         if (err < 0) {
435                 free(alaw);
436                 return err;
437         }
438         pcm->ops = &snd_pcm_alaw_ops;
439         pcm->fast_ops = &snd_pcm_plugin_fast_ops;
440         pcm->private_data = alaw;
441         pcm->poll_fd = slave->poll_fd;
442         pcm->hw_ptr = &alaw->plug.hw_ptr;
443         pcm->appl_ptr = &alaw->plug.appl_ptr;
444         *pcmp = pcm;
445
446         return 0;
447 }
448
449 /*! \page pcm_plugins
450
451 \section pcm_plugins_alaw Plugin: A-Law
452
453 This plugin converts A-Law samples to linear or linear to A-Law samples
454 from master A-Law conversion PCM to given slave PCM. The channel count,
455 format and rate must match for both of them.
456
457 \code
458 pcm.name {
459         type alaw               # A-Law conversion PCM
460         slave STR               # Slave name
461         # or
462         slave {                 # Slave definition
463                 pcm STR         # Slave PCM name
464                 # or
465                 pcm { }         # Slave PCM definition
466         }
467 }
468 \endcode
469
470 \subsection pcm_plugins_alaw_funcref Function reference
471
472 <UL>
473   <LI>snd_pcm_alaw_open()
474   <LI>_snd_pcm_alaw_open()
475 </UL>
476
477 */
478
479 /**
480  * \brief Creates a new A-Law conversion PCM
481  * \param pcmp Returns created PCM handle
482  * \param name Name of PCM
483  * \param root Root configuration node
484  * \param conf Configuration node with copy PCM description
485  * \param stream Stream type
486  * \param mode Stream mode
487  * \retval zero on success otherwise a negative error code
488  * \warning Using of this function might be dangerous in the sense
489  *          of compatibility reasons. The prototype might be freely
490  *          changed in future.
491  */
492 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name,
493                        snd_config_t *root, snd_config_t *conf, 
494                        snd_pcm_stream_t stream, int mode)
495 {
496         snd_config_iterator_t i, next;
497         int err;
498         snd_pcm_t *spcm;
499         snd_config_t *slave = NULL, *sconf;
500         snd_pcm_format_t sformat;
501         snd_config_for_each(i, next, conf) {
502                 snd_config_t *n = snd_config_iterator_entry(i);
503                 const char *id;
504                 if (snd_config_get_id(n, &id) < 0)
505                         continue;
506                 if (snd_pcm_conf_generic_id(id))
507                         continue;
508                 if (strcmp(id, "slave") == 0) {
509                         slave = n;
510                         continue;
511                 }
512                 SNDERR("Unknown field %s", id);
513                 return -EINVAL;
514         }
515         if (!slave) {
516                 SNDERR("slave is not defined");
517                 return -EINVAL;
518         }
519         err = snd_pcm_slave_conf(root, slave, &sconf, 1,
520                                  SND_PCM_HW_PARAM_FORMAT, SCONF_MANDATORY, &sformat);
521         if (err < 0)
522                 return err;
523         if (snd_pcm_format_linear(sformat) != 1 &&
524             sformat != SND_PCM_FORMAT_A_LAW) {
525                 snd_config_delete(sconf);
526                 SNDERR("invalid slave format");
527                 return -EINVAL;
528         }
529         err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode);
530         snd_config_delete(sconf);
531         if (err < 0)
532                 return err;
533         err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);
534         if (err < 0)
535                 snd_pcm_close(spcm);
536         return err;
537 }
538 #ifndef DOC_HIDDEN
539 SND_DLSYM_BUILD_VERSION(_snd_pcm_alaw_open, SND_PCM_DLSYM_VERSION);
540 #endif