OSDN Git Service

Added I/O classes
[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 typedef void (*alaw_f)(const snd_pcm_channel_area_t *src_areas,
27                         snd_pcm_uframes_t src_offset,
28                         const snd_pcm_channel_area_t *dst_areas,
29                         snd_pcm_uframes_t dst_offset,
30                         unsigned int channels, snd_pcm_uframes_t frames, int getputidx);
31
32 typedef struct {
33         /* This field need to be the first */
34         snd_pcm_plugin_t plug;
35         int getput_idx;
36         alaw_f func;
37         int sformat;
38 } snd_pcm_alaw_t;
39
40 static inline int val_seg(int val)
41 {
42         int r = 1;
43         val >>= 8;
44         if (val & 0xf0) {
45                 val >>= 4;
46                 r += 4;
47         }
48         if (val & 0x0c) {
49                 val >>= 2;
50                 r += 2;
51         }
52         if (val & 0x02)
53                 r += 1;
54         return r;
55 }
56
57 /*
58  * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
59  *
60  * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
61  *
62  *              Linear Input Code       Compressed Code
63  *      ------------------------        ---------------
64  *      0000000wxyza                    000wxyz
65  *      0000001wxyza                    001wxyz
66  *      000001wxyzab                    010wxyz
67  *      00001wxyzabc                    011wxyz
68  *      0001wxyzabcd                    100wxyz
69  *      001wxyzabcde                    101wxyz
70  *      01wxyzabcdef                    110wxyz
71  *      1wxyzabcdefg                    111wxyz
72  *
73  * For further information see John C. Bellamy's Digital Telephony, 1982,
74  * John Wiley & Sons, pps 98-111 and 472-476.
75  */
76
77 static unsigned char s16_to_alaw(int pcm_val)
78 {
79         int             mask;
80         int             seg;
81         unsigned char   aval;
82
83         if (pcm_val >= 0) {
84                 mask = 0xD5;
85         } else {
86                 mask = 0x55;
87                 pcm_val = -pcm_val;
88                 if (pcm_val > 0x7fff)
89                         pcm_val = 0x7fff;
90         }
91
92         if (pcm_val < 256)
93                 aval = pcm_val >> 4;
94         else {
95                 /* Convert the scaled magnitude to segment number. */
96                 seg = val_seg(pcm_val);
97                 aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
98         }
99         return aval ^ mask;
100 }
101
102 /*
103  * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
104  *
105  */
106 static int alaw_to_s16(unsigned char a_val)
107 {
108         int             t;
109         int             seg;
110
111         a_val ^= 0x55;
112         t = a_val & 0x7f;
113         if (t < 16)
114                 t = (t << 4) + 8;
115         else {
116                 seg = (t >> 4) & 0x07;
117                 t = ((t & 0x0f) << 4) + 0x108;
118                 t <<= seg -1;
119         }
120         return ((a_val & 0x80) ? t : -t);
121 }
122
123 static void alaw_decode(const snd_pcm_channel_area_t *src_areas,
124                         snd_pcm_uframes_t src_offset,
125                         const snd_pcm_channel_area_t *dst_areas,
126                         snd_pcm_uframes_t dst_offset,
127                         unsigned int channels, snd_pcm_uframes_t frames, int putidx)
128 {
129 #define PUT16_LABELS
130 #include "plugin_ops.h"
131 #undef PUT16_LABELS
132         void *put = put16_labels[putidx];
133         unsigned int channel;
134         for (channel = 0; channel < channels; ++channel) {
135                 char *src;
136                 char *dst;
137                 int src_step, dst_step;
138                 snd_pcm_uframes_t frames1;
139                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
140                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
141 #if 0
142                 if (!src_area->enabled) {
143                         if (dst_area->wanted)
144                                 snd_pcm_area_silence(&dst_areas[channel], dst_offset, frames, dst_sfmt);
145                         dst_area->enabled = 0;
146                         continue;
147                 }
148                 dst_area->enabled = 1;
149 #endif
150                 src = snd_pcm_channel_area_addr(src_area, src_offset);
151                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
152                 src_step = snd_pcm_channel_area_step(src_area);
153                 dst_step = snd_pcm_channel_area_step(dst_area);
154                 frames1 = frames;
155                 while (frames1-- > 0) {
156                         int16_t sample = alaw_to_s16(*src);
157                         goto *put;
158 #define PUT16_END after
159 #include "plugin_ops.h"
160 #undef PUT16_END
161                 after:
162                         src += src_step;
163                         dst += dst_step;
164                 }
165         }
166 }
167
168 static void alaw_encode(const snd_pcm_channel_area_t *src_areas,
169                          snd_pcm_uframes_t src_offset,
170                          const snd_pcm_channel_area_t *dst_areas,
171                          snd_pcm_uframes_t dst_offset,
172                          unsigned int channels, snd_pcm_uframes_t frames, int getidx)
173 {
174 #define GET16_LABELS
175 #include "plugin_ops.h"
176 #undef GET16_LABELS
177         void *get = get16_labels[getidx];
178         unsigned int channel;
179         int16_t sample = 0;
180         for (channel = 0; channel < channels; ++channel) {
181                 char *src;
182                 char *dst;
183                 int src_step, dst_step;
184                 snd_pcm_uframes_t frames1;
185                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
186                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
187 #if 0
188                 if (!src_area->enabled) {
189                         if (dst_area->wanted)
190                                 snd_pcm_area_silence(&dst_area->area, 0, frames, dst_sfmt);
191                         dst_area->enabled = 0;
192                         continue;
193                 }
194                 dst_area->enabled = 1;
195 #endif
196                 src = snd_pcm_channel_area_addr(src_area, src_offset);
197                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
198                 src_step = snd_pcm_channel_area_step(src_area);
199                 dst_step = snd_pcm_channel_area_step(dst_area);
200                 frames1 = frames;
201                 while (frames1-- > 0) {
202                         goto *get;
203 #define GET16_END after
204 #include "plugin_ops.h"
205 #undef GET16_END
206                 after:
207                         *dst = s16_to_alaw(sample);
208                         src += src_step;
209                         dst += dst_step;
210                 }
211         }
212 }
213
214 static int snd_pcm_alaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
215 {
216         snd_pcm_alaw_t *alaw = pcm->private;
217         snd_pcm_t *slave = alaw->plug.slave;
218         int err;
219         unsigned int cmask, lcmask;
220         snd_pcm_hw_params_t sparams;
221         mask_t *access_mask = alloca(mask_sizeof());
222         mask_t *saccess_mask = alloca(mask_sizeof());
223         mask_load(access_mask, SND_PCM_ACCBIT_PLUGIN);
224         mask_load(saccess_mask, SND_PCM_ACCBIT_MMAP);
225         cmask = params->cmask;
226         params->cmask = 0;
227         err = _snd_pcm_hw_param_mask(params, SND_PCM_HW_PARAM_ACCESS,
228                                       access_mask);
229         if (err < 0)
230                 return err;
231         if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
232                 mask_t *format_mask = alloca(mask_sizeof());
233                 mask_load(format_mask, SND_PCM_FMTBIT_LINEAR);
234                 err = _snd_pcm_hw_param_mask(params,
235                                               SND_PCM_HW_PARAM_FORMAT,
236                                               format_mask);
237                 if (err < 0)
238                         return err;
239         } else {
240                 err = _snd_pcm_hw_param_set(params,
241                                              SND_PCM_HW_PARAM_FORMAT,
242                                              SND_PCM_FORMAT_A_LAW, 0);
243                 if (err < 0)
244                         return err;
245         }
246         err = _snd_pcm_hw_param_set(params, SND_PCM_HW_PARAM_SUBFORMAT,
247                                      SND_PCM_SUBFORMAT_STD, 0);
248         if (err < 0)
249                 return err;
250         lcmask = params->cmask;
251         params->cmask |= cmask;
252
253         _snd_pcm_hw_params_any(&sparams);
254         _snd_pcm_hw_param_mask(&sparams, SND_PCM_HW_PARAM_ACCESS,
255                                saccess_mask);
256         _snd_pcm_hw_param_set(&sparams, SND_PCM_HW_PARAM_FORMAT,
257                               alaw->sformat, 0);
258         _snd_pcm_hw_param_set(&sparams, SND_PCM_HW_PARAM_SUBFORMAT,
259                               SND_PCM_SUBFORMAT_STD, 0);
260         err = snd_pcm_hw_refine2(params, &sparams,
261                                  snd_pcm_generic_hw_link, slave,
262                                  SND_PCM_HW_PARBIT_CHANNELS |
263                                  SND_PCM_HW_PARBIT_RATE |
264                                  SND_PCM_HW_PARBIT_PERIOD_SIZE |
265                                  SND_PCM_HW_PARBIT_BUFFER_SIZE |
266                                  SND_PCM_HW_PARBIT_PERIODS |
267                                  SND_PCM_HW_PARBIT_PERIOD_TIME |
268                                  SND_PCM_HW_PARBIT_BUFFER_TIME |
269                                  SND_PCM_HW_PARBIT_TICK_TIME);
270         params->cmask |= lcmask;
271         if (err < 0)
272                 return err;
273         params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
274         return 0;
275 }
276
277 static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
278 {
279         snd_pcm_alaw_t *alaw = pcm->private;
280         snd_pcm_t *slave = alaw->plug.slave;
281         int err;
282         snd_pcm_hw_params_t sparams;
283         unsigned int links;
284         mask_t *saccess_mask = alloca(mask_sizeof());
285         mask_load(saccess_mask, SND_PCM_ACCBIT_MMAP);
286
287         _snd_pcm_hw_params_any(&sparams);
288         _snd_pcm_hw_param_mask(&sparams, SND_PCM_HW_PARAM_ACCESS,
289                                saccess_mask);
290         _snd_pcm_hw_param_set(&sparams, SND_PCM_HW_PARAM_FORMAT,
291                               alaw->sformat, 0);
292         _snd_pcm_hw_param_set(&sparams, SND_PCM_HW_PARAM_SUBFORMAT,
293                               SND_PCM_SUBFORMAT_STD, 0);
294         links = SND_PCM_HW_PARBIT_CHANNELS |
295                 SND_PCM_HW_PARBIT_RATE |
296                 SND_PCM_HW_PARBIT_PERIOD_SIZE |
297                 SND_PCM_HW_PARBIT_BUFFER_SIZE |
298                 SND_PCM_HW_PARBIT_PERIODS |
299                 SND_PCM_HW_PARBIT_PERIOD_TIME |
300                 SND_PCM_HW_PARBIT_BUFFER_TIME |
301                 SND_PCM_HW_PARBIT_TICK_TIME;
302         err = snd_pcm_hw_params_refine(&sparams, links, params);
303         assert(err >= 0);
304         err = snd_pcm_hw_params(slave, &sparams);
305         params->cmask = 0;
306         sparams.cmask = ~0U;
307         snd_pcm_hw_params_refine(params, links, &sparams);
308         if (err < 0)
309                 return err;
310         params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
311         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
312                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
313                         alaw->getput_idx = get_index(snd_pcm_hw_param_value(params, SND_PCM_HW_PARAM_FORMAT, 0), SND_PCM_FORMAT_S16);
314                         alaw->func = alaw_encode;
315                 } else {
316                         alaw->getput_idx = put_index(SND_PCM_FORMAT_S16, alaw->sformat);
317                         alaw->func = alaw_decode;
318                 }
319         } else {
320                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
321                         alaw->getput_idx = put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_param_value(params, SND_PCM_HW_PARAM_FORMAT, 0));
322                         alaw->func = alaw_decode;
323                 } else {
324                         alaw->getput_idx = get_index(alaw->sformat, SND_PCM_FORMAT_S16);
325                         alaw->func = alaw_encode;
326                 }
327         }
328         return 0;
329 }
330
331 static snd_pcm_sframes_t snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
332                                         const snd_pcm_channel_area_t *areas,
333                                         snd_pcm_uframes_t offset,
334                                         snd_pcm_uframes_t size,
335                                         snd_pcm_uframes_t *slave_sizep)
336 {
337         snd_pcm_alaw_t *alaw = pcm->private;
338         snd_pcm_t *slave = alaw->plug.slave;
339         snd_pcm_uframes_t xfer = 0;
340         snd_pcm_sframes_t err = 0;
341         if (slave_sizep && *slave_sizep < size)
342                 size = *slave_sizep;
343         assert(size > 0);
344         while (xfer < size) {
345                 snd_pcm_uframes_t frames = snd_pcm_mmap_playback_xfer(slave, size - xfer);
346                 alaw->func(areas, offset, 
347                             snd_pcm_mmap_areas(slave), snd_pcm_mmap_offset(slave),
348                             pcm->channels, frames,
349                             alaw->getput_idx);
350                 err = snd_pcm_mmap_forward(slave, frames);
351                 if (err < 0)
352                         break;
353                 assert((snd_pcm_uframes_t)err == frames);
354                 offset += err;
355                 xfer += err;
356                 snd_pcm_mmap_hw_forward(pcm, err);
357         }
358         if (xfer > 0) {
359                 if (slave_sizep)
360                         *slave_sizep = xfer;
361                 return xfer;
362         }
363         return err;
364 }
365
366 static snd_pcm_sframes_t snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
367                                        const snd_pcm_channel_area_t *areas,
368                                        snd_pcm_uframes_t offset,
369                                        snd_pcm_uframes_t size,
370                                        snd_pcm_uframes_t *slave_sizep)
371 {
372         snd_pcm_alaw_t *alaw = pcm->private;
373         snd_pcm_t *slave = alaw->plug.slave;
374         snd_pcm_uframes_t xfer = 0;
375         snd_pcm_sframes_t err = 0;
376         if (slave_sizep && *slave_sizep < size)
377                 size = *slave_sizep;
378         assert(size > 0);
379         while (xfer < size) {
380                 snd_pcm_uframes_t frames = snd_pcm_mmap_capture_xfer(slave, size - xfer);
381                 alaw->func(snd_pcm_mmap_areas(slave), snd_pcm_mmap_offset(slave),
382                            areas, offset, 
383                            pcm->channels, frames,
384                            alaw->getput_idx);
385                 err = snd_pcm_mmap_forward(slave, frames);
386                 if (err < 0)
387                         break;
388                 assert((snd_pcm_uframes_t)err == frames);
389                 offset += err;
390                 xfer += err;
391                 snd_pcm_mmap_hw_forward(pcm, err);
392         }
393         if (xfer > 0) {
394                 if (slave_sizep)
395                         *slave_sizep = xfer;
396                 return xfer;
397         }
398         return err;
399 }
400
401 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
402 {
403         snd_pcm_alaw_t *alaw = pcm->private;
404         snd_output_printf(out, "A-Law conversion PCM (%s)\n", 
405                 snd_pcm_format_name(alaw->sformat));
406         if (pcm->setup) {
407                 snd_output_printf(out, "Its setup is:\n");
408                 snd_pcm_dump_setup(pcm, out);
409         }
410         snd_output_printf(out, "Slave: ");
411         snd_pcm_dump(alaw->plug.slave, out);
412 }
413
414 snd_pcm_ops_t snd_pcm_alaw_ops = {
415         close: snd_pcm_plugin_close,
416         card: snd_pcm_plugin_card,
417         info: snd_pcm_plugin_info,
418         hw_refine: snd_pcm_alaw_hw_refine,
419         hw_params: snd_pcm_alaw_hw_params,
420         sw_params: snd_pcm_plugin_sw_params,
421         channel_info: snd_pcm_plugin_channel_info,
422         dump: snd_pcm_alaw_dump,
423         nonblock: snd_pcm_plugin_nonblock,
424         async: snd_pcm_plugin_async,
425         mmap: snd_pcm_plugin_mmap,
426         munmap: snd_pcm_plugin_munmap,
427 };
428
429 int snd_pcm_alaw_open(snd_pcm_t **pcmp, char *name, int sformat, snd_pcm_t *slave, int close_slave)
430 {
431         snd_pcm_t *pcm;
432         snd_pcm_alaw_t *alaw;
433         assert(pcmp && slave);
434         if (snd_pcm_format_linear(sformat) != 1 &&
435             sformat != SND_PCM_FORMAT_A_LAW)
436                 return -EINVAL;
437         alaw = calloc(1, sizeof(snd_pcm_alaw_t));
438         if (!alaw) {
439                 return -ENOMEM;
440         }
441         alaw->sformat = sformat;
442         alaw->plug.read = snd_pcm_alaw_read_areas;
443         alaw->plug.write = snd_pcm_alaw_write_areas;
444         alaw->plug.slave = slave;
445         alaw->plug.close_slave = close_slave;
446
447         pcm = calloc(1, sizeof(snd_pcm_t));
448         if (!pcm) {
449                 free(alaw);
450                 return -ENOMEM;
451         }
452         if (name)
453                 pcm->name = strdup(name);
454         pcm->type = SND_PCM_TYPE_ALAW;
455         pcm->stream = slave->stream;
456         pcm->mode = slave->mode;
457         pcm->ops = &snd_pcm_alaw_ops;
458         pcm->op_arg = pcm;
459         pcm->fast_ops = &snd_pcm_plugin_fast_ops;
460         pcm->fast_op_arg = pcm;
461         pcm->private = alaw;
462         pcm->poll_fd = slave->poll_fd;
463         pcm->hw_ptr = &alaw->plug.hw_ptr;
464         pcm->appl_ptr = &alaw->plug.appl_ptr;
465         *pcmp = pcm;
466
467         return 0;
468 }
469
470 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, char *name,
471                          snd_config_t *conf, 
472                          int stream, int mode)
473 {
474         snd_config_iterator_t i;
475         char *sname = NULL;
476         int err;
477         snd_pcm_t *spcm;
478         int sformat = -1;
479         snd_config_foreach(i, conf) {
480                 snd_config_t *n = snd_config_entry(i);
481                 if (strcmp(n->id, "comment") == 0)
482                         continue;
483                 if (strcmp(n->id, "type") == 0)
484                         continue;
485                 if (strcmp(n->id, "stream") == 0)
486                         continue;
487                 if (strcmp(n->id, "sname") == 0) {
488                         err = snd_config_string_get(n, &sname);
489                         if (err < 0) {
490                                 ERR("Invalid type for %s", n->id);
491                                 return -EINVAL;
492                         }
493                         continue;
494                 }
495                 if (strcmp(n->id, "sformat") == 0) {
496                         char *f;
497                         err = snd_config_string_get(n, &f);
498                         if (err < 0) {
499                                 ERR("Invalid type for %s", n->id);
500                                 return -EINVAL;
501                         }
502                         sformat = snd_pcm_format_value(f);
503                         if (sformat < 0) {
504                                 ERR("Unknown sformat");
505                                 return -EINVAL;
506                         }
507                         if (snd_pcm_format_linear(sformat) != 1 &&
508                             sformat != SND_PCM_FORMAT_A_LAW) {
509                                 ERR("Invalid sformat");
510                                 return -EINVAL;
511                         }
512                         continue;
513                 }
514                 ERR("Unknown field %s", n->id);
515                 return -EINVAL;
516         }
517         if (!sname) {
518                 ERR("sname is not defined");
519                 return -EINVAL;
520         }
521         if (sformat < 0) {
522                 ERR("sformat is not defined");
523                 return -EINVAL;
524         }
525         /* This is needed cause snd_config_update may destroy config */
526         sname = strdup(sname);
527         if (!sname)
528                 return  -ENOMEM;
529         err = snd_pcm_open(&spcm, sname, stream, mode);
530         free(sname);
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                                 
539