OSDN Git Service

pcm_share cleaning
[android-x86/external-alsa-lib.git] / src / pcm / pcm_adpcm.c
1 /*
2  *  PCM - Ima-ADPC conversion
3  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
4  *  Copyright (c) 1999 by Uros Bizjak <uros@kss-loka.si>
5  *                        Jaroslav Kysela <perex@suse.cz>
6  *
7  *  Based on Version 1.2, 18-Dec-92 implementation of Intel/DVI ADPCM code
8  *  by Jack Jansen, CWI, Amsterdam <Jack.Jansen@cwi.nl>, Copyright 1992
9  *  by Stichting Mathematisch Centrum, Amsterdam, The Netherlands.
10  *
11  *   This library is free software; you can redistribute it and/or modify
12  *   it under the terms of the GNU Library General Public License as
13  *   published by the Free Software Foundation; either version 2 of
14  *   the License, or (at your option) any later version.
15  *
16  *   This program is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU Library General Public License for more details.
20  *
21  *   You should have received a copy of the GNU Library General Public
22  *   License along with this library; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  */
26   
27 /*
28 These routines convert 16 bit linear PCM samples to 4 bit ADPCM code
29 and vice versa. The ADPCM code used is the Intel/DVI ADPCM code which
30 is being recommended by the IMA Digital Audio Technical Working Group.
31
32 The algorithm for this coder was taken from:
33 Proposal for Standardized Audio Interstreamge Formats,
34 IMA compatability project proceedings, Vol 2, Issue 2, May 1992.
35
36 - No, this is *not* a G.721 coder/decoder. The algorithm used by G.721
37   is very complicated, requiring oodles of floating-point ops per
38   sample (resulting in very poor performance). I have not done any
39   tests myself but various people have assured my that 721 quality is
40   actually lower than DVI quality.
41
42 - No, it probably isn't a RIFF ADPCM decoder either. Trying to decode
43   RIFF ADPCM with these routines seems to result in something
44   recognizable but very distorted.
45
46 - No, it is not a CDROM-XA coder either, as far as I know. I haven't
47   come across a good description of XA yet.
48  */
49
50 #include <byteswap.h>
51 #include "pcm_local.h"
52 #include "pcm_plugin.h"
53
54 typedef void (*adpcm_f)(const snd_pcm_channel_area_t *dst_areas,
55                         snd_pcm_uframes_t dst_offset,
56                         const snd_pcm_channel_area_t *src_areas,
57                         snd_pcm_uframes_t src_offset,
58                         unsigned int channels, snd_pcm_uframes_t frames, int getputidx,
59                         snd_pcm_adpcm_state_t *states);
60
61 typedef struct {
62         /* This field need to be the first */
63         snd_pcm_plugin_t plug;
64         int getput_idx;
65         adpcm_f func;
66         snd_pcm_format_t sformat;
67         snd_pcm_adpcm_state_t *states;
68 } snd_pcm_adpcm_t;
69
70 /* First table lookup for Ima-ADPCM quantizer */
71 static char IndexAdjust[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
72
73 /* Second table lookup for Ima-ADPCM quantizer */
74 static short StepSize[89] = {
75         7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
76         19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
77         50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
78         130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
79         337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
80         876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
81         2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
82         5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
83         15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
84 };
85
86 static char adpcm_encoder(int sl, snd_pcm_adpcm_state_t * state)
87 {
88         short diff;             /* Difference between sl and predicted sample */
89         short pred_diff;        /* Predicted difference to next sample */
90
91         unsigned char sign;     /* sign of diff */
92         short step;             /* holds previous StepSize value */
93         unsigned char adjust_idx;       /* Index to IndexAdjust lookup table */
94
95         int i;
96
97         /* Compute difference to previous predicted value */
98         diff = sl - state->pred_val;
99         sign = (diff < 0) ? 0x8 : 0x0;
100         if (sign) {
101                 diff = -diff;
102         }
103
104         /*
105          * This code *approximately* computes:
106          *    adjust_idx = diff * 4 / step;
107          *    pred_diff = (adjust_idx + 0.5) * step / 4;
108          *
109          * But in shift step bits are dropped. The net result of this is
110          * that even if you have fast mul/div hardware you cannot put it to
111          * good use since the fixup would be too expensive.
112          */
113
114         step = StepSize[state->step_idx];
115
116         /* Divide and clamp */
117         pred_diff = step >> 3;
118         for (adjust_idx = 0, i = 0x4; i; i >>= 1, step >>= 1) {
119                 if (diff >= step) {
120                         adjust_idx |= i;
121                         diff -= step;
122                         pred_diff += step;
123                 }
124         }
125
126         /* Update and clamp previous predicted value */
127         state->pred_val += sign ? -pred_diff : pred_diff;
128
129         if (state->pred_val > 32767) {
130                 state->pred_val = 32767;
131         } else if (state->pred_val < -32768) {
132                 state->pred_val = -32768;
133         }
134
135         /* Update and clamp StepSize lookup table index */
136         state->step_idx += IndexAdjust[adjust_idx];
137
138         if (state->step_idx < 0) {
139                 state->step_idx = 0;
140         } else if (state->step_idx > 88) {
141                 state->step_idx = 88;
142         }
143         return (sign | adjust_idx);
144 }
145
146
147 static int adpcm_decoder(unsigned char code, snd_pcm_adpcm_state_t * state)
148 {
149         short pred_diff;        /* Predicted difference to next sample */
150         short step;             /* holds previous StepSize value */
151         char sign;
152
153         int i;
154
155         /* Separate sign and magnitude */
156         sign = code & 0x8;
157         code &= 0x7;
158
159         /*
160          * Computes pred_diff = (code + 0.5) * step / 4,
161          * but see comment in adpcm_coder.
162          */
163
164         step = StepSize[state->step_idx];
165
166         /* Compute difference and new predicted value */
167         pred_diff = step >> 3;
168         for (i = 0x4; i; i >>= 1, step >>= 1) {
169                 if (code & i) {
170                         pred_diff += step;
171                 }
172         }
173         state->pred_val += (sign) ? -pred_diff : pred_diff;
174
175         /* Clamp output value */
176         if (state->pred_val > 32767) {
177                 state->pred_val = 32767;
178         } else if (state->pred_val < -32768) {
179                 state->pred_val = -32768;
180         }
181
182         /* Find new StepSize index value */
183         state->step_idx += IndexAdjust[code];
184
185         if (state->step_idx < 0) {
186                 state->step_idx = 0;
187         } else if (state->step_idx > 88) {
188                 state->step_idx = 88;
189         }
190         return (state->pred_val);
191 }
192
193 void snd_pcm_adpcm_decode(const snd_pcm_channel_area_t *dst_areas,
194                           snd_pcm_uframes_t dst_offset,
195                           const snd_pcm_channel_area_t *src_areas,
196                           snd_pcm_uframes_t src_offset,
197                           unsigned int channels, snd_pcm_uframes_t frames, int putidx,
198                           snd_pcm_adpcm_state_t *states)
199 {
200 #define PUT16_LABELS
201 #include "plugin_ops.h"
202 #undef PUT16_LABELS
203         void *put = put16_labels[putidx];
204         unsigned int channel;
205         for (channel = 0; channel < channels; ++channel, ++states) {
206                 const char *src;
207                 int srcbit;
208                 char *dst;
209                 int src_step, srcbit_step, dst_step;
210                 snd_pcm_uframes_t frames1;
211                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
212                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
213                 srcbit = src_area->first + src_area->step * src_offset;
214                 src = src_area->addr + srcbit / 8;
215                 srcbit %= 8;
216                 src_step = src_area->step / 8;
217                 srcbit_step = src_area->step % 8;
218                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
219                 dst_step = snd_pcm_channel_area_step(dst_area);
220                 frames1 = frames;
221                 while (frames1-- > 0) {
222                         int16_t sample;
223                         int v;
224                         if (srcbit)
225                                 v = *src & 0x0f;
226                         else
227                                 v = (*src >> 4) & 0x0f;
228                         sample = adpcm_decoder(v, states);
229                         goto *put;
230 #define PUT16_END after
231 #include "plugin_ops.h"
232 #undef PUT16_END
233                 after:
234                         src += src_step;
235                         srcbit += srcbit_step;
236                         if (srcbit == 8) {
237                                 src++;
238                                 srcbit = 0;
239                         }
240                         dst += dst_step;
241                 }
242         }
243 }
244
245 void snd_pcm_adpcm_encode(const snd_pcm_channel_area_t *dst_areas,
246                           snd_pcm_uframes_t dst_offset,
247                           const snd_pcm_channel_area_t *src_areas,
248                           snd_pcm_uframes_t src_offset,
249                           unsigned int channels, snd_pcm_uframes_t frames, int getidx,
250                           snd_pcm_adpcm_state_t *states)
251 {
252 #define GET16_LABELS
253 #include "plugin_ops.h"
254 #undef GET16_LABELS
255         void *get = get16_labels[getidx];
256         unsigned int channel;
257         int16_t sample = 0;
258         for (channel = 0; channel < channels; ++channel, ++states) {
259                 const char *src;
260                 char *dst;
261                 int dstbit;
262                 int src_step, dst_step, dstbit_step;
263                 snd_pcm_uframes_t frames1;
264                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
265                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
266                 src = snd_pcm_channel_area_addr(src_area, src_offset);
267                 src_step = snd_pcm_channel_area_step(src_area);
268                 dstbit = dst_area->first + dst_area->step * dst_offset;
269                 dst = dst_area->addr + dstbit / 8;
270                 dstbit %= 8;
271                 dst_step = dst_area->step / 8;
272                 dstbit_step = dst_area->step % 8;
273                 frames1 = frames;
274                 while (frames1-- > 0) {
275                         int v;
276                         goto *get;
277 #define GET16_END after
278 #include "plugin_ops.h"
279 #undef GET16_END
280                 after:
281                         v = adpcm_encoder(sample, states);
282                         if (dstbit)
283                                 *dst = (*dst & 0xf0) | v;
284                         else
285                                 *dst = (*dst & 0x0f) | (v << 4);
286                         src += src_step;
287                         dst += dst_step;
288                         dstbit += dstbit_step;
289                         if (dstbit == 8) {
290                                 dst++;
291                                 dstbit = 0;
292                         }
293                 }
294         }
295 }
296
297 static int snd_pcm_adpcm_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
298 {
299         snd_pcm_adpcm_t *adpcm = pcm->private_data;
300         int err;
301         snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
302         err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
303                                          &access_mask);
304         if (err < 0)
305                 return err;
306         if (adpcm->sformat == SND_PCM_FORMAT_IMA_ADPCM) {
307                 snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
308                 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
309                                                  &format_mask);
310         } else {
311                 err = _snd_pcm_hw_params_set_format(params,
312                                                    SND_PCM_FORMAT_IMA_ADPCM);
313         }
314         if (err < 0)
315                 return err;
316         err = _snd_pcm_hw_params_set_subformat(params,
317                                                SND_PCM_SUBFORMAT_STD);
318         if (err < 0)
319                 return err;
320         params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
321         return 0;
322 }
323
324 static int snd_pcm_adpcm_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
325 {
326         snd_pcm_adpcm_t *adpcm = pcm->private_data;
327         snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
328         _snd_pcm_hw_params_any(sparams);
329         _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
330                                    &saccess_mask);
331         _snd_pcm_hw_params_set_format(sparams, adpcm->sformat);
332         _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
333         return 0;
334 }
335
336 static int snd_pcm_adpcm_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
337                                             snd_pcm_hw_params_t *sparams)
338 {
339         int err;
340         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
341                               SND_PCM_HW_PARBIT_RATE |
342                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
343                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
344                               SND_PCM_HW_PARBIT_PERIODS |
345                               SND_PCM_HW_PARBIT_PERIOD_TIME |
346                               SND_PCM_HW_PARBIT_BUFFER_TIME |
347                               SND_PCM_HW_PARBIT_TICK_TIME);
348         err = _snd_pcm_hw_params_refine(sparams, links, params);
349         if (err < 0)
350                 return err;
351         return 0;
352 }
353         
354 static int snd_pcm_adpcm_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
355                                             snd_pcm_hw_params_t *sparams)
356 {
357         int err;
358         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
359                               SND_PCM_HW_PARBIT_RATE |
360                               SND_PCM_HW_PARBIT_PERIOD_SIZE |
361                               SND_PCM_HW_PARBIT_BUFFER_SIZE |
362                               SND_PCM_HW_PARBIT_PERIODS |
363                               SND_PCM_HW_PARBIT_PERIOD_TIME |
364                               SND_PCM_HW_PARBIT_BUFFER_TIME |
365                               SND_PCM_HW_PARBIT_TICK_TIME);
366         err = _snd_pcm_hw_params_refine(params, links, sparams);
367         if (err < 0)
368                 return err;
369         return 0;
370 }
371
372 static int snd_pcm_adpcm_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
373 {
374         return snd_pcm_hw_refine_slave(pcm, params,
375                                        snd_pcm_adpcm_hw_refine_cprepare,
376                                        snd_pcm_adpcm_hw_refine_cchange,
377                                        snd_pcm_adpcm_hw_refine_sprepare,
378                                        snd_pcm_adpcm_hw_refine_schange,
379                                        snd_pcm_plugin_hw_refine_slave);
380 }
381
382 static int snd_pcm_adpcm_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
383 {
384         snd_pcm_adpcm_t *adpcm = pcm->private_data;
385         int err = snd_pcm_hw_params_slave(pcm, params,
386                                           snd_pcm_adpcm_hw_refine_cchange,
387                                           snd_pcm_adpcm_hw_refine_sprepare,
388                                           snd_pcm_adpcm_hw_refine_schange,
389                                           snd_pcm_plugin_hw_params_slave);
390         if (err < 0)
391                 return err;
392
393
394         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
395                 if (adpcm->sformat == SND_PCM_FORMAT_IMA_ADPCM) {
396                         adpcm->getput_idx = snd_pcm_linear_get_index(snd_pcm_hw_params_get_format(params), SND_PCM_FORMAT_S16);
397                         adpcm->func = snd_pcm_adpcm_encode;
398                 } else {
399                         adpcm->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, adpcm->sformat);
400                         adpcm->func = snd_pcm_adpcm_decode;
401                 }
402         } else {
403                 if (adpcm->sformat == SND_PCM_FORMAT_IMA_ADPCM) {
404                         adpcm->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, snd_pcm_hw_params_get_format(params));
405                         adpcm->func = snd_pcm_adpcm_decode;
406                 } else {
407                         adpcm->getput_idx = snd_pcm_linear_get_index(adpcm->sformat, SND_PCM_FORMAT_S16);
408                         adpcm->func = snd_pcm_adpcm_encode;
409                 }
410         }
411         assert(!adpcm->states);
412         adpcm->states = malloc(snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_CHANNELS, 0) * sizeof(*adpcm->states));
413         return 0;
414 }
415
416 static int snd_pcm_adpcm_hw_free(snd_pcm_t *pcm)
417 {
418         snd_pcm_adpcm_t *adpcm = pcm->private_data;
419         if (adpcm->states) {
420                 free(adpcm->states);
421                 adpcm->states = 0;
422         }
423         return snd_pcm_hw_free(adpcm->plug.slave);
424 }
425
426 static int snd_pcm_adpcm_init(snd_pcm_t *pcm)
427 {
428         snd_pcm_adpcm_t *adpcm = pcm->private_data;
429         unsigned int k;
430         for (k = 0; k < pcm->channels; ++k) {
431                 adpcm->states[k].pred_val = 0;
432                 adpcm->states[k].step_idx = 0;
433         }
434         return 0;
435 }
436
437 static snd_pcm_uframes_t
438 snd_pcm_adpcm_write_areas(snd_pcm_t *pcm,
439                           const snd_pcm_channel_area_t *areas,
440                           snd_pcm_uframes_t offset,
441                           snd_pcm_uframes_t size,
442                           const snd_pcm_channel_area_t *slave_areas,
443                           snd_pcm_uframes_t slave_offset,
444                           snd_pcm_uframes_t *slave_sizep)
445 {
446         snd_pcm_adpcm_t *adpcm = pcm->private_data;
447         if (size > *slave_sizep)
448                 size = *slave_sizep;
449         adpcm->func(slave_areas, slave_offset,
450                     areas, offset, 
451                     pcm->channels, size,
452                     adpcm->getput_idx, adpcm->states);
453         *slave_sizep = size;
454         return size;
455 }
456
457 static snd_pcm_uframes_t
458 snd_pcm_adpcm_read_areas(snd_pcm_t *pcm,
459                          const snd_pcm_channel_area_t *areas,
460                          snd_pcm_uframes_t offset,
461                          snd_pcm_uframes_t size,
462                          const snd_pcm_channel_area_t *slave_areas,
463                          snd_pcm_uframes_t slave_offset,
464                          snd_pcm_uframes_t *slave_sizep)
465 {
466         snd_pcm_adpcm_t *adpcm = pcm->private_data;
467         if (size > *slave_sizep)
468                 size = *slave_sizep;
469         adpcm->func(areas, offset, 
470                     slave_areas, slave_offset,
471                     pcm->channels, size,
472                     adpcm->getput_idx, adpcm->states);
473         *slave_sizep = size;
474         return size;
475 }
476
477 static void snd_pcm_adpcm_dump(snd_pcm_t *pcm, snd_output_t *out)
478 {
479         snd_pcm_adpcm_t *adpcm = pcm->private_data;
480         snd_output_printf(out, "Ima-ADPCM conversion PCM (%s)\n", 
481                 snd_pcm_format_name(adpcm->sformat));
482         if (pcm->setup) {
483                 snd_output_printf(out, "Its setup is:\n");
484                 snd_pcm_dump_setup(pcm, out);
485         }
486         snd_output_printf(out, "Slave: ");
487         snd_pcm_dump(adpcm->plug.slave, out);
488 }
489
490 snd_pcm_ops_t snd_pcm_adpcm_ops = {
491         close: snd_pcm_plugin_close,
492         info: snd_pcm_plugin_info,
493         hw_refine: snd_pcm_adpcm_hw_refine,
494         hw_params: snd_pcm_adpcm_hw_params,
495         hw_free: snd_pcm_adpcm_hw_free,
496         sw_params: snd_pcm_plugin_sw_params,
497         channel_info: snd_pcm_plugin_channel_info,
498         dump: snd_pcm_adpcm_dump,
499         nonblock: snd_pcm_plugin_nonblock,
500         async: snd_pcm_plugin_async,
501         mmap: snd_pcm_plugin_mmap,
502         munmap: snd_pcm_plugin_munmap,
503 };
504
505 int snd_pcm_adpcm_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
506 {
507         snd_pcm_t *pcm;
508         snd_pcm_adpcm_t *adpcm;
509         assert(pcmp && slave);
510         if (snd_pcm_format_linear(sformat) != 1 &&
511             sformat != SND_PCM_FORMAT_IMA_ADPCM)
512                 return -EINVAL;
513         adpcm = calloc(1, sizeof(snd_pcm_adpcm_t));
514         if (!adpcm) {
515                 return -ENOMEM;
516         }
517         adpcm->sformat = sformat;
518         adpcm->plug.read = snd_pcm_adpcm_read_areas;
519         adpcm->plug.write = snd_pcm_adpcm_write_areas;
520         adpcm->plug.init = snd_pcm_adpcm_init;
521         adpcm->plug.slave = slave;
522         adpcm->plug.close_slave = close_slave;
523
524         pcm = calloc(1, sizeof(snd_pcm_t));
525         if (!pcm) {
526                 free(adpcm);
527                 return -ENOMEM;
528         }
529         if (name)
530                 pcm->name = strdup(name);
531         pcm->type = SND_PCM_TYPE_ADPCM;
532         pcm->stream = slave->stream;
533         pcm->mode = slave->mode;
534         pcm->ops = &snd_pcm_adpcm_ops;
535         pcm->op_arg = pcm;
536         pcm->fast_ops = &snd_pcm_plugin_fast_ops;
537         pcm->fast_op_arg = pcm;
538         pcm->private_data = adpcm;
539         pcm->poll_fd = slave->poll_fd;
540         pcm->hw_ptr = &adpcm->plug.hw_ptr;
541         pcm->appl_ptr = &adpcm->plug.appl_ptr;
542         *pcmp = pcm;
543
544         return 0;
545 }
546
547 int _snd_pcm_adpcm_open(snd_pcm_t **pcmp, const char *name,
548                          snd_config_t *conf, 
549                          snd_pcm_stream_t stream, int mode)
550 {
551         snd_config_iterator_t i, next;
552         const char *sname = NULL;
553         int err;
554         snd_pcm_t *spcm;
555         snd_config_t *slave = NULL;
556         snd_pcm_format_t sformat;
557         snd_config_for_each(i, next, conf) {
558                 snd_config_t *n = snd_config_iterator_entry(i);
559                 const char *id = snd_config_get_id(n);
560                 if (strcmp(id, "comment") == 0)
561                         continue;
562                 if (strcmp(id, "type") == 0)
563                         continue;
564                 if (strcmp(id, "slave") == 0) {
565                         slave = n;
566                         continue;
567                 }
568                 SNDERR("Unknown field %s", id);
569                 return -EINVAL;
570         }
571         if (!slave) {
572                 SNDERR("slave is not defined");
573                 return -EINVAL;
574         }
575         err = snd_pcm_slave_conf(slave, &sname, 1,
576                                  SND_PCM_HW_PARAM_FORMAT, 1, &sformat);
577         if (err < 0)
578                 return err;
579         if (snd_pcm_format_linear(sformat) != 1 &&
580             sformat != SND_PCM_FORMAT_IMA_ADPCM) {
581                 SNDERR("invalid slave format");
582                 return -EINVAL;
583         }
584         /* This is needed cause snd_config_update may destroy config */
585         sname = strdup(sname);
586         if (!sname)
587                 return  -ENOMEM;
588         err = snd_pcm_open(&spcm, sname, stream, mode);
589         free((void *) sname);
590         if (err < 0)
591                 return err;
592         err = snd_pcm_adpcm_open(pcmp, name, sformat, spcm, 1);
593         if (err < 0)
594                 snd_pcm_close(spcm);
595         return err;
596 }
597                                 
598