OSDN Git Service

Improved plugin code (simpler, faster and multithread ready)
[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 *dst_areas,
27                        snd_pcm_uframes_t dst_offset,
28                        const snd_pcm_channel_area_t *src_areas,
29                        snd_pcm_uframes_t src_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         snd_pcm_format_t 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 void snd_pcm_alaw_decode(const snd_pcm_channel_area_t *dst_areas,
124                          snd_pcm_uframes_t dst_offset,
125                          const snd_pcm_channel_area_t *src_areas,
126                          snd_pcm_uframes_t src_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                 const 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 void snd_pcm_alaw_encode(const snd_pcm_channel_area_t *dst_areas,
169                          snd_pcm_uframes_t dst_offset,
170                          const snd_pcm_channel_area_t *src_areas,
171                          snd_pcm_uframes_t src_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                 const 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_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
215 {
216         snd_pcm_alaw_t *alaw = pcm->private_data;
217         int err;
218         snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_PLUGIN };
219         err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
220                                          &access_mask);
221         if (err < 0)
222                 return err;
223         if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
224                 snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
225                 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
226                                                  &format_mask);
227         } else {
228                 err = _snd_pcm_hw_params_set_format(params, 
229                                                    SND_PCM_FORMAT_A_LAW);
230         }
231         if (err < 0)
232                 return err;
233         err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
234         if (err < 0)
235                 return err;
236         params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
237         return 0;
238 }
239
240 static int snd_pcm_alaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
241 {
242         snd_pcm_alaw_t *alaw = pcm->private_data;
243         snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
244         _snd_pcm_hw_params_any(sparams);
245         _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
246                                    &saccess_mask);
247         _snd_pcm_hw_params_set_format(sparams, alaw->sformat);
248         _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
249         return 0;
250 }
251
252 static int snd_pcm_alaw_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
253                                             snd_pcm_hw_params_t *sparams)
254 {
255         int err;
256         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
257                               SND_PCM_HW_PARBIT_RATE |
258                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
259                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
260                               SND_PCM_HW_PARBIT_PERIODS |
261                               SND_PCM_HW_PARBIT_PERIOD_TIME |
262                               SND_PCM_HW_PARBIT_BUFFER_TIME |
263                               SND_PCM_HW_PARBIT_TICK_TIME);
264         err = _snd_pcm_hw_params_refine(sparams, links, params);
265         if (err < 0)
266                 return err;
267         return 0;
268 }
269         
270 static int snd_pcm_alaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
271                                             snd_pcm_hw_params_t *sparams)
272 {
273         int err;
274         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
275                               SND_PCM_HW_PARBIT_RATE |
276                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
277                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
278                               SND_PCM_HW_PARBIT_PERIODS |
279                               SND_PCM_HW_PARBIT_PERIOD_TIME |
280                               SND_PCM_HW_PARBIT_BUFFER_TIME |
281                               SND_PCM_HW_PARBIT_TICK_TIME);
282         err = _snd_pcm_hw_params_refine(params, links, sparams);
283         if (err < 0)
284                 return err;
285         return 0;
286 }
287
288 static int snd_pcm_alaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
289 {
290         return snd_pcm_hw_refine_slave(pcm, params,
291                                        snd_pcm_alaw_hw_refine_cprepare,
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_refine_slave);
296 }
297
298 static int snd_pcm_alaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
299 {
300         snd_pcm_alaw_t *alaw = pcm->private_data;
301         int err = snd_pcm_hw_params_slave(pcm, params,
302                                           snd_pcm_alaw_hw_refine_cchange,
303                                           snd_pcm_alaw_hw_refine_sprepare,
304                                           snd_pcm_alaw_hw_refine_schange,
305                                           snd_pcm_plugin_hw_params_slave);
306         if (err < 0)
307                 return err;
308
309         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
310                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
311                         alaw->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
312                         alaw->func = snd_pcm_alaw_encode;
313                 } else {
314                         alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, alaw->sformat);
315                         alaw->func = snd_pcm_alaw_decode;
316                 }
317         } else {
318                 if (alaw->sformat == SND_PCM_FORMAT_A_LAW) {
319                         alaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
320                         alaw->func = snd_pcm_alaw_decode;
321                 } else {
322                         alaw->getput_idx = snd_pcm_linear_get_index(alaw->sformat, SND_PCM_FORMAT_S16);
323                         alaw->func = snd_pcm_alaw_encode;
324                 }
325         }
326         return 0;
327 }
328
329 static snd_pcm_uframes_t
330 snd_pcm_alaw_write_areas(snd_pcm_t *pcm,
331                          const snd_pcm_channel_area_t *areas,
332                          snd_pcm_uframes_t offset,
333                          snd_pcm_uframes_t size,
334                          const snd_pcm_channel_area_t *slave_areas,
335                          snd_pcm_uframes_t slave_offset,
336                          snd_pcm_uframes_t *slave_sizep)
337 {
338         snd_pcm_alaw_t *alaw = pcm->private_data;
339         if (size > *slave_sizep)
340                 size = *slave_sizep;
341         alaw->func(slave_areas, slave_offset,
342                    areas, offset, 
343                    pcm->channels, size,
344                    alaw->getput_idx);
345         *slave_sizep = size;
346         return size;
347 }
348
349 static snd_pcm_uframes_t
350 snd_pcm_alaw_read_areas(snd_pcm_t *pcm,
351                         const snd_pcm_channel_area_t *areas,
352                         snd_pcm_uframes_t offset,
353                         snd_pcm_uframes_t size,
354                         const snd_pcm_channel_area_t *slave_areas,
355                         snd_pcm_uframes_t slave_offset,
356                         snd_pcm_uframes_t *slave_sizep)
357 {
358         snd_pcm_alaw_t *alaw = pcm->private_data;
359         if (size > *slave_sizep)
360                 size = *slave_sizep;
361         alaw->func(areas, offset, 
362                    slave_areas, slave_offset,
363                    pcm->channels, size,
364                    alaw->getput_idx);
365         *slave_sizep = size;
366         return size;
367 }
368
369 static void snd_pcm_alaw_dump(snd_pcm_t *pcm, snd_output_t *out)
370 {
371         snd_pcm_alaw_t *alaw = pcm->private_data;
372         snd_output_printf(out, "A-Law conversion PCM (%s)\n", 
373                 snd_pcm_format_name(alaw->sformat));
374         if (pcm->setup) {
375                 snd_output_printf(out, "Its setup is:\n");
376                 snd_pcm_dump_setup(pcm, out);
377         }
378         snd_output_printf(out, "Slave: ");
379         snd_pcm_dump(alaw->plug.slave, out);
380 }
381
382 snd_pcm_ops_t snd_pcm_alaw_ops = {
383         close: snd_pcm_plugin_close,
384         info: snd_pcm_plugin_info,
385         hw_refine: snd_pcm_alaw_hw_refine,
386         hw_params: snd_pcm_alaw_hw_params,
387         hw_free: snd_pcm_plugin_hw_free,
388         sw_params: snd_pcm_plugin_sw_params,
389         channel_info: snd_pcm_plugin_channel_info,
390         dump: snd_pcm_alaw_dump,
391         nonblock: snd_pcm_plugin_nonblock,
392         async: snd_pcm_plugin_async,
393         mmap: snd_pcm_plugin_mmap,
394         munmap: snd_pcm_plugin_munmap,
395 };
396
397 int snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
398 {
399         snd_pcm_t *pcm;
400         snd_pcm_alaw_t *alaw;
401         assert(pcmp && slave);
402         if (snd_pcm_format_linear(sformat) != 1 &&
403             sformat != SND_PCM_FORMAT_A_LAW)
404                 return -EINVAL;
405         alaw = calloc(1, sizeof(snd_pcm_alaw_t));
406         if (!alaw) {
407                 return -ENOMEM;
408         }
409         alaw->sformat = sformat;
410         alaw->plug.read = snd_pcm_alaw_read_areas;
411         alaw->plug.write = snd_pcm_alaw_write_areas;
412         alaw->plug.slave = slave;
413         alaw->plug.close_slave = close_slave;
414
415         pcm = calloc(1, sizeof(snd_pcm_t));
416         if (!pcm) {
417                 free(alaw);
418                 return -ENOMEM;
419         }
420         if (name)
421                 pcm->name = strdup(name);
422         pcm->type = SND_PCM_TYPE_ALAW;
423         pcm->stream = slave->stream;
424         pcm->mode = slave->mode;
425         pcm->ops = &snd_pcm_alaw_ops;
426         pcm->op_arg = pcm;
427         pcm->fast_ops = &snd_pcm_plugin_fast_ops;
428         pcm->fast_op_arg = pcm;
429         pcm->private_data = alaw;
430         pcm->poll_fd = slave->poll_fd;
431         pcm->hw_ptr = &alaw->plug.hw_ptr;
432         pcm->appl_ptr = &alaw->plug.appl_ptr;
433         *pcmp = pcm;
434
435         return 0;
436 }
437
438 int _snd_pcm_alaw_open(snd_pcm_t **pcmp, const char *name,
439                          snd_config_t *conf, 
440                          snd_pcm_stream_t stream, int mode)
441 {
442         snd_config_iterator_t i, next;
443         const char *sname = NULL;
444         int err;
445         snd_pcm_t *spcm;
446         snd_pcm_format_t sformat = SND_PCM_FORMAT_UNKNOWN;
447         snd_config_for_each(i, next, conf) {
448                 snd_config_t *n = snd_config_iterator_entry(i);
449                 const char *id = snd_config_get_id(n);
450                 if (strcmp(id, "comment") == 0)
451                         continue;
452                 if (strcmp(id, "type") == 0)
453                         continue;
454                 if (strcmp(id, "sname") == 0) {
455                         err = snd_config_get_string(n, &sname);
456                         if (err < 0) {
457                                 ERR("Invalid type for %s", id);
458                                 return -EINVAL;
459                         }
460                         continue;
461                 }
462                 if (strcmp(id, "sformat") == 0) {
463                         const char *f;
464                         err = snd_config_get_string(n, &f);
465                         if (err < 0) {
466                                 ERR("Invalid type for %s", id);
467                                 return -EINVAL;
468                         }
469                         sformat = snd_pcm_format_value(f);
470                         if (sformat == SND_PCM_FORMAT_UNKNOWN) {
471                                 ERR("Unknown sformat");
472                                 return -EINVAL;
473                         }
474                         if (snd_pcm_format_linear(sformat) != 1 &&
475                             sformat != SND_PCM_FORMAT_A_LAW) {
476                                 ERR("Invalid sformat");
477                                 return -EINVAL;
478                         }
479                         continue;
480                 }
481                 ERR("Unknown field %s", id);
482                 return -EINVAL;
483         }
484         if (!sname) {
485                 ERR("sname is not defined");
486                 return -EINVAL;
487         }
488         if (sformat == SND_PCM_FORMAT_UNKNOWN) {
489                 ERR("sformat is not defined");
490                 return -EINVAL;
491         }
492         /* This is needed cause snd_config_update may destroy config */
493         sname = strdup(sname);
494         if (!sname)
495                 return  -ENOMEM;
496         err = snd_pcm_open(&spcm, sname, stream, mode);
497         free((void *) sname);
498         if (err < 0)
499                 return err;
500         err = snd_pcm_alaw_open(pcmp, name, sformat, spcm, 1);
501         if (err < 0)
502                 snd_pcm_close(spcm);
503         return err;
504 }
505                                 
506