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