]> git.sesse.net Git - vlc/blob - modules/codec/adpcm.c
mediacodec: fix warning
[vlc] / modules / codec / adpcm.c
1 /*****************************************************************************
2  * adpcm.c : adpcm variant audio decoder
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          RĂ©mi Denis-Courmont <rem # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *
28  * Documentation: http://www.pcisys.net/~melanson/codecs/adpcm.txt
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  OpenDecoder( vlc_object_t * );
42 static void CloseDecoder( vlc_object_t * );
43
44 static block_t *DecodeBlock( decoder_t *, block_t ** );
45
46 vlc_module_begin ()
47     set_description( N_("ADPCM audio decoder") )
48     set_capability( "decoder", 50 )
49     set_category( CAT_INPUT )
50     set_subcategory( SUBCAT_INPUT_ACODEC )
51     set_callbacks( OpenDecoder, CloseDecoder )
52 vlc_module_end ()
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 enum adpcm_codec_e
58 {
59     ADPCM_IMA_QT,
60     ADPCM_IMA_WAV,
61     ADPCM_MS,
62     ADPCM_DK3,
63     ADPCM_DK4,
64     ADPCM_EA
65 };
66
67 struct decoder_sys_t
68 {
69     enum adpcm_codec_e codec;
70
71     size_t              i_block;
72     size_t              i_samplesperblock;
73
74     date_t              end_date;
75 };
76
77 static void DecodeAdpcmMs    ( decoder_t *, int16_t *, uint8_t * );
78 static void DecodeAdpcmImaWav( decoder_t *, int16_t *, uint8_t * );
79 static void DecodeAdpcmImaQT ( decoder_t *, int16_t *, uint8_t * );
80 static void DecodeAdpcmDk4   ( decoder_t *, int16_t *, uint8_t * );
81 static void DecodeAdpcmDk3   ( decoder_t *, int16_t *, uint8_t * );
82 static void DecodeAdpcmEA    ( decoder_t *, int16_t *, uint8_t * );
83
84 static const int pi_channels_maps[6] =
85 {
86     0,
87     AOUT_CHAN_CENTER,
88     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
89     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
90     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
91     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
92      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
93 };
94
95 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
96 static const int i_index_table[16] =
97 {
98     -1, -1, -1, -1, 2, 4, 6, 8,
99     -1, -1, -1, -1, 2, 4, 6, 8
100 };
101
102 static const int i_step_table[89] =
103 {
104     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
105     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
106     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
107     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
108     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
109     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
110     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
111     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
112     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
113 };
114
115 static const int i_adaptation_table[16] =
116 {
117     230, 230, 230, 230, 307, 409, 512, 614,
118     768, 614, 512, 409, 307, 230, 230, 230
119 };
120
121 static const int i_adaptation_coeff1[7] =
122 {
123     256, 512, 0, 192, 240, 460, 392
124 };
125
126 static const int i_adaptation_coeff2[7] =
127 {
128     0, -256, 0, 64, 0, -208, -232
129 };
130
131 /*****************************************************************************
132  * OpenDecoder: probe the decoder and return score
133  *****************************************************************************/
134 static int OpenDecoder( vlc_object_t *p_this )
135 {
136     decoder_t *p_dec = (decoder_t*)p_this;
137     decoder_sys_t *p_sys;
138
139     switch( p_dec->fmt_in.i_codec )
140     {
141         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
142         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
143         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
144         case VLC_CODEC_ADPCM_DK3:
145         case VLC_CODEC_ADPCM_DK4:
146         case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
147             break;
148         default:
149             return VLC_EGENERIC;
150     }
151
152     if( p_dec->fmt_in.audio.i_channels <= 0 ||
153         p_dec->fmt_in.audio.i_channels > 5 )
154     {
155         msg_Err( p_dec, "invalid number of channel (not between 1 and 5): %i",
156                  p_dec->fmt_in.audio.i_channels );
157         return VLC_EGENERIC;
158     }
159
160     if( p_dec->fmt_in.audio.i_rate <= 0 )
161     {
162         msg_Err( p_dec, "bad samplerate" );
163         return VLC_EGENERIC;
164     }
165
166     /* Allocate the memory needed to store the decoder's structure */
167     if( ( p_dec->p_sys = p_sys =
168           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
169         return VLC_ENOMEM;
170
171     switch( p_dec->fmt_in.i_codec )
172     {
173         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
174             p_sys->codec = ADPCM_IMA_QT;
175             break;
176         case VLC_CODEC_ADPCM_IMA_WAV: /* IMA ADPCM */
177             p_sys->codec = ADPCM_IMA_WAV;
178             break;
179         case VLC_CODEC_ADPCM_MS: /* MS ADPCM */
180             p_sys->codec = ADPCM_MS;
181             break;
182         case VLC_CODEC_ADPCM_DK4: /* Duck DK4 ADPCM */
183             p_sys->codec = ADPCM_DK4;
184             break;
185         case VLC_CODEC_ADPCM_DK3: /* Duck DK3 ADPCM */
186             p_sys->codec = ADPCM_DK3;
187             break;
188         case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
189             p_sys->codec = ADPCM_EA;
190             p_dec->fmt_in.p_extra = calloc( 2 * p_dec->fmt_in.audio.i_channels,
191                                             sizeof( int16_t ) );
192             if( p_dec->fmt_in.p_extra == NULL )
193             {
194                 free( p_sys );
195                 return VLC_ENOMEM;
196             }
197             break;
198     }
199
200     if( p_dec->fmt_in.audio.i_blockalign <= 0 )
201     {
202         p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
203             34 * p_dec->fmt_in.audio.i_channels : 1024;
204         msg_Warn( p_dec, "block size undefined, using %zu", p_sys->i_block );
205     }
206     else
207     {
208         p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
209     }
210
211     /* calculate samples per block */
212     switch( p_sys->codec )
213     {
214     case ADPCM_IMA_QT:
215         p_sys->i_samplesperblock = 64;
216         break;
217     case ADPCM_IMA_WAV:
218         p_sys->i_samplesperblock =
219             2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
220             p_dec->fmt_in.audio.i_channels;
221         break;
222     case ADPCM_MS:
223         p_sys->i_samplesperblock =
224             2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
225             p_dec->fmt_in.audio.i_channels + 2;
226         break;
227     case ADPCM_DK4:
228         p_sys->i_samplesperblock =
229             2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
230             p_dec->fmt_in.audio.i_channels + 1;
231         break;
232     case ADPCM_DK3:
233         p_dec->fmt_in.audio.i_channels = 2;
234         p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
235         break;
236     case ADPCM_EA:
237         p_sys->i_samplesperblock =
238             2 * (p_sys->i_block - p_dec->fmt_in.audio.i_channels) /
239             p_dec->fmt_in.audio.i_channels;
240     }
241
242     msg_Dbg( p_dec, "format: samplerate:%d Hz channels:%d bits/sample:%d "
243              "blockalign:%zu samplesperblock:%zu",
244              p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
245              p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
246              p_sys->i_samplesperblock );
247
248     p_dec->fmt_out.i_cat = AUDIO_ES;
249     p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
250     p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
251     p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
252     p_dec->fmt_out.audio.i_physical_channels =
253         p_dec->fmt_out.audio.i_original_channels =
254             pi_channels_maps[p_dec->fmt_in.audio.i_channels];
255
256     date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
257     date_Set( &p_sys->end_date, 0 );
258
259     p_dec->pf_decode_audio = DecodeBlock;
260
261     return VLC_SUCCESS;
262 }
263
264 /*****************************************************************************
265  * DecodeBlock:
266  *****************************************************************************/
267 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
268 {
269     decoder_sys_t *p_sys  = p_dec->p_sys;
270     block_t *p_block;
271
272     if( !pp_block || !*pp_block ) return NULL;
273
274     p_block = *pp_block;
275
276     if( p_block->i_pts > VLC_TS_INVALID &&
277         p_block->i_pts != date_Get( &p_sys->end_date ) )
278     {
279         date_Set( &p_sys->end_date, p_block->i_pts );
280     }
281     else if( !date_Get( &p_sys->end_date ) )
282     {
283         /* We've just started the stream, wait for the first PTS. */
284         block_Release( p_block );
285         return NULL;
286     }
287
288     /* Don't re-use the same pts twice */
289     p_block->i_pts = VLC_TS_INVALID;
290
291     if( p_block->i_buffer >= p_sys->i_block )
292     {
293         block_t *p_out;
294
295         p_out = decoder_NewAudioBuffer( p_dec, p_sys->i_samplesperblock );
296         if( p_out == NULL )
297         {
298             block_Release( p_block );
299             return NULL;
300         }
301
302         p_out->i_pts = date_Get( &p_sys->end_date );
303         p_out->i_length = date_Increment( &p_sys->end_date,
304                                      p_sys->i_samplesperblock ) - p_out->i_pts;
305
306         switch( p_sys->codec )
307         {
308         case ADPCM_IMA_QT:
309             DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
310                               p_block->p_buffer );
311             break;
312         case ADPCM_IMA_WAV:
313             DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
314                                p_block->p_buffer );
315             break;
316         case ADPCM_MS:
317             DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
318                            p_block->p_buffer );
319             break;
320         case ADPCM_DK4:
321             DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
322                             p_block->p_buffer );
323             break;
324         case ADPCM_DK3:
325             DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
326                             p_block->p_buffer );
327             break;
328         case ADPCM_EA:
329             DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
330                            p_block->p_buffer );
331         default:
332             break;
333         }
334
335         p_block->p_buffer += p_sys->i_block;
336         p_block->i_buffer -= p_sys->i_block;
337         return p_out;
338     }
339
340     block_Release( p_block );
341     return NULL;
342 }
343
344 /*****************************************************************************
345  * CloseDecoder:
346  *****************************************************************************/
347 static void CloseDecoder( vlc_object_t *p_this )
348 {
349     decoder_t *p_dec = (decoder_t *)p_this;
350     decoder_sys_t *p_sys = p_dec->p_sys;
351
352     if( p_sys->codec == ADPCM_EA )
353         free( p_dec->fmt_in.p_extra );
354     free( p_sys );
355 }
356
357 /*****************************************************************************
358  * Local functions
359  *****************************************************************************/
360 #define CLAMP( v, min, max ) \
361     if( (v) < (min) ) (v) = (min); \
362     if( (v) > (max) ) (v) = (max)
363
364 #define GetByte( v ) \
365     (v) = *p_buffer; p_buffer++;
366
367 #define GetWord( v ) \
368     (v) = *p_buffer; p_buffer++; \
369     (v) |= ( *p_buffer ) << 8; p_buffer++; \
370     if( (v)&0x8000 ) (v) -= 0x010000;
371
372 /*
373  * MS
374  */
375 typedef struct adpcm_ms_channel_s
376 {
377     int i_idelta;
378     int i_sample1, i_sample2;
379     int i_coeff1, i_coeff2;
380
381 } adpcm_ms_channel_t;
382
383
384 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
385                                int i_nibble )
386 {
387     int i_predictor;
388     int i_snibble;
389     /* expand sign */
390
391     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
392
393     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
394                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
395                   i_snibble * p_channel->i_idelta;
396
397     CLAMP( i_predictor, -32768, 32767 );
398
399     p_channel->i_sample2 = p_channel->i_sample1;
400     p_channel->i_sample1 = i_predictor;
401
402     p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
403                             p_channel->i_idelta ) / 256;
404     if( p_channel->i_idelta < 16 )
405     {
406         p_channel->i_idelta = 16;
407     }
408     return( i_predictor );
409 }
410
411 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
412                            uint8_t *p_buffer )
413 {
414     decoder_sys_t *p_sys  = p_dec->p_sys;
415     adpcm_ms_channel_t channel[2];
416     int i_nibbles;
417     int b_stereo;
418     int i_block_predictor;
419
420     b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
421
422     GetByte( i_block_predictor );
423     CLAMP( i_block_predictor, 0, 6 );
424     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
425     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
426
427     if( b_stereo )
428     {
429         GetByte( i_block_predictor );
430         CLAMP( i_block_predictor, 0, 6 );
431         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
432         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
433     }
434     GetWord( channel[0].i_idelta );
435     if( b_stereo )
436     {
437         GetWord( channel[1].i_idelta );
438     }
439
440     GetWord( channel[0].i_sample1 );
441     if( b_stereo )
442     {
443         GetWord( channel[1].i_sample1 );
444     }
445
446     GetWord( channel[0].i_sample2 );
447     if( b_stereo )
448     {
449         GetWord( channel[1].i_sample2 );
450     }
451
452     if( b_stereo )
453     {
454         *p_sample++ = channel[0].i_sample2;
455         *p_sample++ = channel[1].i_sample2;
456         *p_sample++ = channel[0].i_sample1;
457         *p_sample++ = channel[1].i_sample1;
458     }
459     else
460     {
461         *p_sample++ = channel[0].i_sample2;
462         *p_sample++ = channel[0].i_sample1;
463     }
464
465     for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
466          i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
467     {
468         *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
469         *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
470                                            (*p_buffer)&0x0f);
471     }
472 }
473
474 /*
475  * IMA-WAV
476  */
477 typedef struct adpcm_ima_wav_channel_s
478 {
479     int i_predictor;
480     int i_step_index;
481
482 } adpcm_ima_wav_channel_t;
483
484 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
485                                    int i_nibble )
486 {
487     int i_diff;
488
489     i_diff = i_step_table[p_channel->i_step_index] >> 3;
490     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
491     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
492     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
493     if( i_nibble&0x08 )
494         p_channel->i_predictor -= i_diff;
495     else
496         p_channel->i_predictor += i_diff;
497
498     CLAMP( p_channel->i_predictor, -32768, 32767 );
499
500     p_channel->i_step_index += i_index_table[i_nibble];
501
502     CLAMP( p_channel->i_step_index, 0, 88 );
503
504     return( p_channel->i_predictor );
505 }
506
507 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
508                                uint8_t *p_buffer )
509 {
510     decoder_sys_t *p_sys  = p_dec->p_sys;
511     adpcm_ima_wav_channel_t channel[2];
512     int                     i_nibbles;
513     int                     b_stereo;
514
515     b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
516
517     GetWord( channel[0].i_predictor );
518     GetByte( channel[0].i_step_index );
519     CLAMP( channel[0].i_step_index, 0, 88 );
520     p_buffer++;
521
522     if( b_stereo )
523     {
524         GetWord( channel[1].i_predictor );
525         GetByte( channel[1].i_step_index );
526         CLAMP( channel[1].i_step_index, 0, 88 );
527         p_buffer++;
528     }
529
530     if( b_stereo )
531     {
532         for( i_nibbles = 2 * (p_sys->i_block - 8);
533              i_nibbles > 0;
534              i_nibbles -= 16 )
535         {
536             int i;
537
538             for( i = 0; i < 4; i++ )
539             {
540                 p_sample[i * 4] =
541                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
542                 p_sample[i * 4 + 2] =
543                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
544             }
545             p_buffer += 4;
546
547             for( i = 0; i < 4; i++ )
548             {
549                 p_sample[i * 4 + 1] =
550                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
551                 p_sample[i * 4 + 3] =
552                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
553             }
554             p_buffer += 4;
555             p_sample += 16;
556
557         }
558
559
560     }
561     else
562     {
563         for( i_nibbles = 2 * (p_sys->i_block - 4);
564              i_nibbles > 0;
565              i_nibbles -= 2, p_buffer++ )
566         {
567             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
568             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
569         }
570     }
571 }
572
573 /*
574  * Ima4 in QT file
575  */
576 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
577                               uint8_t *p_buffer )
578 {
579     adpcm_ima_wav_channel_t channel[2];
580     int                     i_nibbles;
581     int                     i_ch;
582     int                     i_step;
583
584     i_step = p_dec->fmt_in.audio.i_channels;
585
586     for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
587     {
588         /* load preambule */
589         channel[i_ch].i_predictor  = (int16_t)((( ( p_buffer[0] << 1 )|(  p_buffer[1] >> 7 ) ))<<7);
590         channel[i_ch].i_step_index = p_buffer[1]&0x7f;
591
592         CLAMP( channel[i_ch].i_step_index, 0, 88 );
593         p_buffer += 2;
594
595         for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
596         {
597             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
598             p_sample += i_step;
599
600             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
601             p_sample += i_step;
602
603             p_buffer++;
604         }
605
606         /* Next channel */
607         p_sample += 1 - 64 * i_step;
608     }
609 }
610
611 /*
612  * Dk4
613  */
614 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
615                             uint8_t *p_buffer )
616 {
617     decoder_sys_t *p_sys  = p_dec->p_sys;
618     adpcm_ima_wav_channel_t channel[2];
619     size_t                  i_nibbles;
620     int                     b_stereo;
621
622     b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
623
624     GetWord( channel[0].i_predictor );
625     GetByte( channel[0].i_step_index );
626     CLAMP( channel[0].i_step_index, 0, 88 );
627     p_buffer++;
628
629     if( b_stereo )
630     {
631         GetWord( channel[1].i_predictor );
632         GetByte( channel[1].i_step_index );
633         CLAMP( channel[1].i_step_index, 0, 88 );
634         p_buffer++;
635     }
636
637     /* first output predictor */
638     *p_sample++ = channel[0].i_predictor;
639     if( b_stereo )
640     {
641         *p_sample++ = channel[1].i_predictor;
642     }
643
644     for( i_nibbles = 0;
645          i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
646          i_nibbles++ )
647     {
648         *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
649                                               (*p_buffer) >> 4);
650         *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
651                                                (*p_buffer)&0x0f);
652
653         p_buffer++;
654     }
655 }
656
657 /*
658  * Dk3
659  */
660 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
661                             uint8_t *p_buffer )
662 {
663     decoder_sys_t *p_sys  = p_dec->p_sys;
664     uint8_t                 *p_end = &p_buffer[p_sys->i_block];
665     adpcm_ima_wav_channel_t sum;
666     adpcm_ima_wav_channel_t diff;
667     int                     i_diff_value;
668
669     p_buffer += 10;
670
671     GetWord( sum.i_predictor );
672     GetWord( diff.i_predictor );
673     GetByte( sum.i_step_index );
674     GetByte( diff.i_step_index );
675
676     i_diff_value = diff.i_predictor;
677     /* we process 6 nibbles at once */
678     while( p_buffer + 1 <= p_end )
679     {
680         /* first 3 nibbles */
681         AdpcmImaWavExpandNibble( &sum,
682                                  (*p_buffer)&0x0f);
683
684         AdpcmImaWavExpandNibble( &diff,
685                                  (*p_buffer) >> 4 );
686
687         i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
688
689         *p_sample++ = sum.i_predictor + i_diff_value;
690         *p_sample++ = sum.i_predictor - i_diff_value;
691
692         p_buffer++;
693
694         AdpcmImaWavExpandNibble( &sum,
695                                  (*p_buffer)&0x0f);
696
697         *p_sample++ = sum.i_predictor + i_diff_value;
698         *p_sample++ = sum.i_predictor - i_diff_value;
699
700         /* now last 3 nibbles */
701         AdpcmImaWavExpandNibble( &sum,
702                                  (*p_buffer)>>4);
703         p_buffer++;
704         if( p_buffer < p_end )
705         {
706             AdpcmImaWavExpandNibble( &diff,
707                                      (*p_buffer)&0x0f );
708
709             i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
710
711             *p_sample++ = sum.i_predictor + i_diff_value;
712             *p_sample++ = sum.i_predictor - i_diff_value;
713
714             AdpcmImaWavExpandNibble( &sum,
715                                      (*p_buffer)>>4);
716             p_buffer++;
717
718             *p_sample++ = sum.i_predictor + i_diff_value;
719             *p_sample++ = sum.i_predictor - i_diff_value;
720         }
721     }
722 }
723
724
725 /*
726  * EA ADPCM
727  */
728 #define MAX_CHAN 5
729 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
730                            uint8_t *p_buffer )
731 {
732     static const int16_t EATable[]=
733     {
734         0x0000, 0x00F0, 0x01CC, 0x0188, 0x0000, 0x0000, 0xFF30, 0xFF24,
735         0x0000, 0x0001, 0x0003, 0x0004, 0x0007, 0x0008, 0x000A, 0x000B,
736         0x0000, 0xFFFF, 0xFFFD, 0xFFFC,
737     };
738     decoder_sys_t *p_sys  = p_dec->p_sys;
739     int_fast32_t c1[MAX_CHAN], c2[MAX_CHAN];
740     int_fast8_t d[MAX_CHAN];
741
742     unsigned chans = p_dec->fmt_in.audio.i_channels;
743     const uint8_t *p_end = &p_buffer[p_sys->i_block];
744     int16_t *prev = (int16_t *)p_dec->fmt_in.p_extra;
745     int16_t *cur = prev + chans;
746
747     for (unsigned c = 0; c < chans; c++)
748     {
749         uint8_t input = p_buffer[c];
750
751         c1[c] = EATable[input >> 4];
752         c2[c] = EATable[(input >> 4) + 4];
753         d[c] = (input & 0xf) + 8;
754     }
755
756     for (p_buffer += chans; p_buffer < p_end; p_buffer += chans)
757     {
758         union { uint32_t u; int32_t i; } spl;
759
760         for (unsigned c = 0; c < chans; c++)
761         {
762             spl.u = (p_buffer[c] & 0xf0u) << 24u;
763             spl.i >>= d[c];
764             spl.i = (spl.i + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
765             CLAMP(spl.i, -32768, 32767);
766             prev[c] = cur[c];
767             cur[c] = spl.i;
768
769             *(p_sample++) = spl.i;
770         }
771
772         for (unsigned c = 0; c < chans; c++)
773         {
774             spl.u = (p_buffer[c] & 0x0fu) << 28u;
775             spl.i >>= d[c];
776             spl.i = (spl.i + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
777             CLAMP(spl.i, -32768, 32767);
778             prev[c] = cur[c];
779             cur[c] = spl.i;
780
781             *(p_sample++) = spl.i;
782         }
783     }
784 }