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