OSDN Git Service

Changes for static build.
[android-x86/external-alsa-lib.git] / src / pcm / pcm_alaw.c
1 /*
2  *  PCM - A-Law conversion
3  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
4  *
5  *
6  *   This library is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU Library General Public License as
8  *   published by the Free Software Foundation; either version 2 of
9  *   the License, or (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU Library General Public License for more details.
15  *
16  *   You should have received a copy of the GNU Library General Public
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21   
22 #include <byteswap.h>
23 #include "pcm_local.h"
24 #include "pcm_plugin.h"
25
26 #ifndef PIC
27 /* entry for static linking */
28 const char *_snd_module_pcm_alaw = "";
29 #endif
30
31 typedef void (*alaw_f)(const snd_pcm_channel_area_t *dst_areas,
32                        snd_pcm_uframes_t dst_offset,
33                        const snd_pcm_channel_area_t *src_areas,
34                        snd_pcm_uframes_t src_offset,
35                        unsigned int channels, snd_pcm_uframes_t frames,
36                        unsigned int getputidx);
37
38 typedef struct {
39         /* This field need to be the first */
40         snd_pcm_plugin_t plug;
41         unsigned int getput_idx;
42         alaw_f func;
43         snd_pcm_format_t sformat;
44 } snd_pcm_alaw_t;
45
46 static inline int val_seg(int val)
47 {
48         int r = 1;
49         val >>= 8;
50         if (val & 0xf0) {
51                 val >>= 4;
52                 r += 4;
53         }
54         if (val & 0x0c) {
55                 val >>= 2;
56                 r += 2;
57         }
58         if (val & 0x02)
59                 r += 1;
60         return r;
61 }
62
63 /*
64  * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
65  *
66  * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
67  *
68  *              Linear Input Code       Compressed Code
69  *      ------------------------        ---------------
70  *      0000000wxyza                    000wxyz
71  *      0000001wxyza                    001wxyz
72  *      000001wxyzab                    010wxyz
73  *      00001wxyzabc                    011wxyz
74  *      0001wxyzabcd                    100wxyz
75  *      001wxyzabcde                    101wxyz
76  *      01wxyzabcdef                    110wxyz
77  *      1wxyzabcdefg                    111wxyz
78  *
79  * For further information see John C. Bellamy's Digital Telephony, 1982,
80  * John Wiley & Sons, pps 98-111 and 472-476.
81  */
82
83 static unsigned char s16_to_alaw(int pcm_val)
84 {
85         int             mask;
86         int             seg;
87         unsigned char   aval;
88
89         if (pcm_val >= 0) {
90                 mask = 0xD5;
91         } else {
92                 mask = 0x55;
93                 pcm_val = -pcm_val;
94                 if (pcm_val > 0x7fff)
95                         pcm_val = 0x7fff;
96         }
97
98         if (pcm_val < 256)
99                 aval = pcm_val >> 4;
100         else {
101                 /* Convert the scaled magnitude to segment number. */
102                 seg = val_seg(pcm_val);
103                 aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
104         }
105         return aval ^ mask;
106 }
107
108 /*
109  * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
110  *
111  */
112 static int alaw_to_s16(unsigned char a_val)
113 {
114         int             t;
115         int             seg;
116
117         a_val ^= 0x55;
118         t = a_val & 0x7f;
119         if (t < 16)
120                 t = (t << 4) + 8;
121         else {
122                 seg = (t >> 4) & 0x07;
123                 t = ((t & 0x0f) << 4) + 0x108;
124                 t <<= seg -1;
125         }
126         return ((a_val & 0x80) ? t : -t);
127 }
128
129 void snd_pcm_alaw_decode(const snd_pcm_channel_area_t *dst_areas,
130                          snd_pcm_uframes_t dst_offset,
131                          const snd_pcm_channel_area_t *src_areas,
132                          snd_pcm_uframes_t src_offset,
133                          unsigned int channels, snd_pcm_uframes_t frames,
134                          unsigned int putidx)
135 {
136 #define PUT16_LABELS
137 #include "plugin_ops.h"
138 #undef PUT16_LABELS
139         void *put = put16_labels[putidx];
140         unsigned int channel;
141         for (channel = 0; channel < channels; ++channel) {
142                 const unsigned char *src;
143                 char *dst;
144                 int src_step, dst_step;
145                 snd_pcm_uframes_t frames1;
146                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
147                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
148                 src = snd_pcm_channel_area_addr(src_area, src_offset);
149                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
150                 src_step = snd_pcm_channel_area_step(src_area);
151                 dst_step = snd_pcm_channel_area_step(dst_area);
152                 frames1 = frames;
153                 while (frames1-- > 0) {
154                         int16_t sample = alaw_to_s16(*src);
155                         goto *put;
156 #define PUT16_END after
157 #include "plugin_ops.h"
158 #undef PUT16_END
159                 after:
160                         src += src_step;
161                         dst += dst_step;
162                 }
163         }
164 }
165
166 void snd_pcm_alaw_encode(const snd_pcm_channel_area_t *dst_areas,
167                          snd_pcm_uframes_t dst_offset,
168                          const snd_pcm_channel_area_t *src_areas,
169                          snd_pcm_uframes_t src_offset,
170                          unsigned int channels, snd_pcm_uframes_t frames,
171                          unsigned int getidx)
172 {
173 #define GET16_LABELS
174 #include "plugin_ops.h"
175 #undef GET16_LABELS
176         void *get = get16_labels[getidx];
177         unsigned int channel;
178         int16_t sample = 0;
179         for (channel = 0; channel < channels; ++channel) {
180                 const char *src;
181                 char *dst;
182                 int src_step, dst_step;
183                 snd_pcm_uframes_t frames1;
184                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
185                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
186                 src = snd_pcm_channel_area_addr(src_area, src_offset);
187                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
188                 src_step = snd_pcm_channel_area_step(src_area);
189                 dst_step = snd_pcm_channel_area_step(dst_area);
190                 frames1 = frames;
191                 while (frames1-- > 0) {
192                         goto *get;
193 #define GET16_END after
194 #include "plugin_ops.h"
195 #undef GET16_END
196                 after:
197                         *dst = s16_to_alaw(sample);
198                         src += src_step;
199                         dst += dst_step;
200                 }
201         }
202 }
203
204 static int snd_pcm_alaw_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
205 {
206         snd_pcm_alaw_t *alaw = pcm->private_data;
207         int err;
208         snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
209         err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
210                                          &access_mask);
211         if (err < 0)
212                 return err;
213         if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
214                 snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
215                 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
216                                                  &format_mask);
217         } else {
218                 err = _snd_pcm_hw_params_set_format(params, 
219                                                    SND_PCM_FORMAT_A_LAW);
220         }
221         if (err < 0)
222                 return err;
223         err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
224         if (err < 0)
225                 return err;
226         params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
227         return 0;
228 }
229
230 static int snd_pcm_alaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
231 {
232         snd_pcm_alaw_t *alaw = pcm->private_data;
233         snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
234         _snd_pcm_hw_params_any(sparams);
235         _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
236                                    &saccess_mask);
237         _snd_pcm_hw_params_set_format(sparams, alaw->sformat);
238         _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
239         return 0;
240 }
241
242 static int snd_pcm_alaw_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
243                                             snd_pcm_hw_params_t *sparams)
244 {
245         int err;
246         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
247                               SND_PCM_HW_PARBIT_RATE |
248                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
249                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
250                               SND_PCM_HW_PARBIT_PERIODS |
251                               SND_PCM_HW_PARBIT_PERIOD_TIME |
252                               SND_PCM_HW_PARBIT_BUFFER_TIME |
253                               SND_PCM_HW_PARBIT_TICK_TIME);
254         err = _snd_pcm_hw_params_refine(sparams, links, params);
255         if (err < 0)
256                 return err;
257         return 0;
258 }
259         
260 static int snd_pcm_alaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
261                                             snd_pcm_hw_params_t *sparams)
262 {
263         int err;
264         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
265                               SND_PCM_HW_PARBIT_RATE |
266                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
267                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
268                               SND_PCM_HW_PARBIT_PERIODS |
269                               SND_PCM_HW_PARBIT_PERIOD_TIME |
270                               SND_PCM_HW_PARBIT_BUFFER_TIME |
271                               SND_PCM_HW_PARBIT_TICK_TIME);
272         err = _snd_pcm_hw_params_refine(params, links, sparams);
273         if (err < 0)
274                 return err;
275         return 0;
276 }
277
278 static int snd_pcm_alaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
279 {
280         return snd_pcm_hw_refine_slave(pcm, params,
281                                        snd_pcm_alaw_hw_refine_cprepare,
282                                        snd_pcm_alaw_hw_refine_cchange,
283                                        snd_pcm_alaw_hw_refine_sprepare,
284                                        snd_pcm_alaw_hw_refine_schange,
285                                        snd_pcm_plugin_hw_refine_slave);
286 }
287
288 static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
289 {
290         snd_pcm_alaw_t *alaw = pcm->private_data;
291         int err = snd_pcm_hw_params_slave(pcm, params,
292                                           snd_pcm_alaw_hw_refine_cchange,
293                                           snd_pcm_alaw_hw_refine_sprepare,
294                                           snd_pcm_alaw_hw_refine_schange,
295                                           snd_pcm_plugin_hw_params_slave);
296         if (err < 0)
297                 return err;
298
299         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
300                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
301                         alaw->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
302                         alaw->func = snd_pcm_alaw_encode;
303                 } else {
304                         alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
305                         alaw->func = snd_pcm_alaw_decode;
306                 }
307         } else {
308                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
309                         alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
310                         alaw->func = snd_pcm_alaw_decode;
311                 } else {
312                         alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
313                         alaw->func = snd_pcm_alaw_encode;
314                 }
315         }
316         return 0;
317 }
318
319 static snd_pcm_uframes_t
320 snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
321                          const snd_pcm_channel_area_t *areas,
322                          snd_pcm_uframes_t offset,
323                          snd_pcm_uframes_t size,
324                          const snd_pcm_channel_area_t *slave_areas,
325                          snd_pcm_uframes_t slave_offset,
326                          snd_pcm_uframes_t *slave_sizep)
327 {
328         snd_pcm_alaw_t *alaw = pcm->private_data;
329         if (size > *slave_sizep)
330                 size = *slave_sizep;
331         alaw->func(slave_areas, slave_offset,
332                    areas, offset, 
333                    pcm->channels, size,
334                    alaw->getput_idx);
335         *slave_sizep = size;
336         return size;
337 }
338
339 static snd_pcm_uframes_t
340 snd_pcm_alaw_read_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(areas, offset, 
352                    slave_areas, slave_offset,
353                    pcm->channels, size,
354                    alaw->getput_idx);
355         *slave_sizep = size;
356         return size;
357 }
358
359 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
360 {
361         snd_pcm_alaw_t *alaw = pcm->private_data;
362         snd_output_printf(out, "A-Law conversion PCM (%s)\n", 
363                 snd_pcm_format_name(alaw->sformat));
364         if (pcm->setup) {
365                 snd_output_printf(out, "Its setup is:\n");
366                 snd_pcm_dump_setup(pcm, out);
367         }
368         snd_output_printf(out, "Slave: ");
369         snd_pcm_dump(alaw->plug.slave, out);
370 }
371
372 snd_pcm_ops_t snd_pcm_alaw_ops = {
373         close: snd_pcm_plugin_close,
374         info: snd_pcm_plugin_info,
375         hw_refine: snd_pcm_alaw_hw_refine,
376         hw_params: snd_pcm_alaw_hw_params,
377         hw_free: snd_pcm_plugin_hw_free,
378         sw_params: snd_pcm_plugin_sw_params,
379         channel_info: snd_pcm_plugin_channel_info,
380         dump: snd_pcm_alaw_dump,
381         nonblock: snd_pcm_plugin_nonblock,
382         async: snd_pcm_plugin_async,
383         mmap: snd_pcm_plugin_mmap,
384         munmap: snd_pcm_plugin_munmap,
385 };
386
387 int snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
388 {
389         snd_pcm_t *pcm;
390         snd_pcm_alaw_t *alaw;
391         int err;
392         assert(pcmp && slave);
393         if (snd_pcm_format_linear(sformat) != 1 &&
394             sformat != SND_PCM_FORMAT_A_LAW)
395                 return -EINVAL;
396         alaw = calloc(1, sizeof(snd_pcm_alaw_t));
397         if (!alaw) {
398                 return -ENOMEM;
399         }
400         alaw->sformat = sformat;
401         alaw->plug.read = snd_pcm_alaw_read_areas;
402         alaw->plug.write = snd_pcm_alaw_write_areas;
403         alaw->plug.slave = slave;
404         alaw->plug.close_slave = close_slave;
405
406         err = snd_pcm_new(&pcm, SND_PCM_TYPE_ALAW, name, slave->stream, slave->mode);
407         if (err < 0) {
408                 free(alaw);
409                 return err;
410         }
411         pcm->ops = &snd_pcm_alaw_ops;
412         pcm->fast_ops = &snd_pcm_plugin_fast_ops;
413         pcm->private_data = alaw;
414         pcm->poll_fd = slave->poll_fd;
415         pcm->hw_ptr = &alaw->plug.hw_ptr;
416         pcm->appl_ptr = &alaw->plug.appl_ptr;
417         *pcmp = pcm;
418
419         return 0;
420 }
421
422 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name,
423                        snd_config_t *root, snd_config_t *conf, 
424                        snd_pcm_stream_t stream, int mode)
425 {
426         snd_config_iterator_t i, next;
427         int err;
428         snd_pcm_t *spcm;
429         snd_config_t *slave = NULL, *sconf;
430         snd_pcm_format_t sformat;
431         snd_config_for_each(i, next, conf) {
432                 snd_config_t *n = snd_config_iterator_entry(i);
433                 const char *id = snd_config_get_id(n);
434                 if (snd_pcm_conf_generic_id(id))
435                         continue;
436                 if (strcmp(id, "slave") == 0) {
437                         slave = n;
438                         continue;
439                 }
440                 SNDERR("Unknown field %s", id);
441                 return -EINVAL;
442         }
443         if (!slave) {
444                 SNDERR("slave is not defined");
445                 return -EINVAL;
446         }
447         err = snd_pcm_slave_conf(root, slave, &sconf, 1,
448                                  SND_PCM_HW_PARAM_FORMAT, SCONF_MANDATORY, &sformat);
449         if (err < 0)
450                 return err;
451         if (snd_pcm_format_linear(sformat) != 1 &&
452             sformat != SND_PCM_FORMAT_A_LAW) {
453                 snd_config_delete(sconf);
454                 SNDERR("invalid slave format");
455                 return -EINVAL;
456         }
457         err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode);
458         snd_config_delete(sconf);
459         if (err < 0)
460                 return err;
461         err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);
462         if (err < 0)
463                 snd_pcm_close(spcm);
464         return err;
465 }
466 SND_DLSYM_BUILD_VERSION(_snd_pcm_alaw_open, SND_PCM_DLSYM_VERSION);