]> git.sesse.net Git - vlc/blob - modules/codec/adpcm.c
Ensure make dist works correctly.
[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_common.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( N_("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         return VLC_ENOMEM;
171
172     switch( p_dec->fmt_in.i_codec )
173     {
174         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
175             p_sys->codec = ADPCM_IMA_QT;
176             break;
177         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
178             p_sys->codec = ADPCM_IMA_WAV;
179             break;
180         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
181             p_sys->codec = ADPCM_MS;
182             break;
183         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
184             p_sys->codec = ADPCM_DK4;
185             break;
186         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
187             p_sys->codec = ADPCM_DK3;
188             break;
189         case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
190             p_sys->codec = ADPCM_EA;
191             p_dec->fmt_in.p_extra = calloc( 2 * p_dec->fmt_in.audio.i_channels,
192                                             sizeof( int16_t ) );
193             if( p_dec->fmt_in.p_extra == NULL )
194             {
195                 free( p_sys );
196                 return VLC_ENOMEM;
197             }
198             break;
199     }
200
201     if( p_dec->fmt_in.audio.i_blockalign <= 0 )
202     {
203         p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
204             34 * p_dec->fmt_in.audio.i_channels : 1024;
205         msg_Warn( p_dec, "block size undefined, using %zu", p_sys->i_block );
206     }
207     else
208     {
209         p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
210     }
211
212     /* calculate samples per block */
213     switch( p_sys->codec )
214     {
215     case ADPCM_IMA_QT:
216         p_sys->i_samplesperblock = 64;
217         break;
218     case ADPCM_IMA_WAV:
219         p_sys->i_samplesperblock =
220             2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
221             p_dec->fmt_in.audio.i_channels;
222         break;
223     case ADPCM_MS:
224         p_sys->i_samplesperblock =
225             2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
226             p_dec->fmt_in.audio.i_channels + 2;
227         break;
228     case ADPCM_DK4:
229         p_sys->i_samplesperblock =
230             2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
231             p_dec->fmt_in.audio.i_channels + 1;
232         break;
233     case ADPCM_DK3:
234         p_dec->fmt_in.audio.i_channels = 2;
235         p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
236         break;
237     case ADPCM_EA:
238         p_sys->i_samplesperblock =
239             2 * (p_sys->i_block - p_dec->fmt_in.audio.i_channels) /
240             p_dec->fmt_in.audio.i_channels;
241     }
242
243     msg_Dbg( p_dec, "format: samplerate:%d Hz channels:%d bits/sample:%d "
244              "blockalign:%zu samplesperblock:%zu",
245              p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
246              p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
247              p_sys->i_samplesperblock );
248
249     p_dec->fmt_out.i_cat = AUDIO_ES;
250     p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
251     p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
252     p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
253     p_dec->fmt_out.audio.i_physical_channels =
254         p_dec->fmt_out.audio.i_original_channels =
255             pi_channels_maps[p_dec->fmt_in.audio.i_channels];
256
257     aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
258     aout_DateSet( &p_sys->end_date, 0 );
259
260     p_dec->pf_decode_audio = DecodeBlock;
261
262     return VLC_SUCCESS;
263 }
264
265 /*****************************************************************************
266  * DecodeBlock:
267  *****************************************************************************/
268 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
269 {
270     decoder_sys_t *p_sys  = p_dec->p_sys;
271     block_t *p_block;
272
273     if( !pp_block || !*pp_block ) return NULL;
274
275     p_block = *pp_block;
276
277     if( p_block->i_pts != 0 &&
278         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
279     {
280         aout_DateSet( &p_sys->end_date, p_block->i_pts );
281     }
282     else if( !aout_DateGet( &p_sys->end_date ) )
283     {
284         /* We've just started the stream, wait for the first PTS. */
285         block_Release( p_block );
286         return NULL;
287     }
288
289     /* Don't re-use the same pts twice */
290     p_block->i_pts = 0;
291
292     if( p_block->i_buffer >= p_sys->i_block )
293     {
294         aout_buffer_t *p_out;
295
296         p_out = decoder_NewAudioBuffer( p_dec, p_sys->i_samplesperblock );
297         if( p_out == NULL )
298         {
299             block_Release( p_block );
300             return NULL;
301         }
302
303         p_out->start_date = aout_DateGet( &p_sys->end_date );
304         p_out->end_date =
305             aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
306
307         switch( p_sys->codec )
308         {
309         case ADPCM_IMA_QT:
310             DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
311                               p_block->p_buffer );
312             break;
313         case ADPCM_IMA_WAV:
314             DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
315                                p_block->p_buffer );
316             break;
317         case ADPCM_MS:
318             DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
319                            p_block->p_buffer );
320             break;
321         case ADPCM_DK4:
322             DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
323                             p_block->p_buffer );
324             break;
325         case ADPCM_DK3:
326             DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
327                             p_block->p_buffer );
328             break;
329         case ADPCM_EA:
330             DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
331                            p_block->p_buffer );
332         default:
333             break;
334         }
335
336         p_block->p_buffer += p_sys->i_block;
337         p_block->i_buffer -= p_sys->i_block;
338         return p_out;
339     }
340
341     block_Release( p_block );
342     return NULL;
343 }
344
345 /*****************************************************************************
346  * CloseDecoder:
347  *****************************************************************************/
348 static void CloseDecoder( vlc_object_t *p_this )
349 {
350     decoder_t *p_dec = (decoder_t *)p_this;
351     decoder_sys_t *p_sys = p_dec->p_sys;
352
353     if( p_sys->codec == ADPCM_EA )
354         free( p_dec->fmt_in.p_extra );
355     free( p_sys );
356 }
357
358 /*****************************************************************************
359  * Local functions
360  *****************************************************************************/
361 #define CLAMP( v, min, max ) \
362     if( (v) < (min) ) (v) = (min); \
363     if( (v) > (max) ) (v) = (max)
364
365 #define GetByte( v ) \
366     (v) = *p_buffer; p_buffer++;
367
368 #define GetWord( v ) \
369     (v) = *p_buffer; p_buffer++; \
370     (v) |= ( *p_buffer ) << 8; p_buffer++; \
371     if( (v)&0x8000 ) (v) -= 0x010000;
372
373 /*
374  * MS
375  */
376 typedef struct adpcm_ms_channel_s
377 {
378     int i_idelta;
379     int i_sample1, i_sample2;
380     int i_coeff1, i_coeff2;
381
382 } adpcm_ms_channel_t;
383
384
385 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
386                                int i_nibble )
387 {
388     int i_predictor;
389     int i_snibble;
390     /* expand sign */
391
392     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
393
394     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
395                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
396                   i_snibble * p_channel->i_idelta;
397
398     CLAMP( i_predictor, -32768, 32767 );
399
400     p_channel->i_sample2 = p_channel->i_sample1;
401     p_channel->i_sample1 = i_predictor;
402
403     p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
404                             p_channel->i_idelta ) / 256;
405     if( p_channel->i_idelta < 16 )
406     {
407         p_channel->i_idelta = 16;
408     }
409     return( i_predictor );
410 }
411
412 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
413                            uint8_t *p_buffer )
414 {
415     decoder_sys_t *p_sys  = p_dec->p_sys;
416     adpcm_ms_channel_t channel[2];
417     int i_nibbles;
418     int b_stereo;
419     int i_block_predictor;
420
421     b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
422
423     GetByte( i_block_predictor );
424     CLAMP( i_block_predictor, 0, 6 );
425     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
426     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
427
428     if( b_stereo )
429     {
430         GetByte( i_block_predictor );
431         CLAMP( i_block_predictor, 0, 6 );
432         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
433         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
434     }
435     GetWord( channel[0].i_idelta );
436     if( b_stereo )
437     {
438         GetWord( channel[1].i_idelta );
439     }
440
441     GetWord( channel[0].i_sample1 );
442     if( b_stereo )
443     {
444         GetWord( channel[1].i_sample1 );
445     }
446
447     GetWord( channel[0].i_sample2 );
448     if( b_stereo )
449     {
450         GetWord( channel[1].i_sample2 );
451     }
452
453     if( b_stereo )
454     {
455         *p_sample++ = channel[0].i_sample2;
456         *p_sample++ = channel[1].i_sample2;
457         *p_sample++ = channel[0].i_sample1;
458         *p_sample++ = channel[1].i_sample1;
459     }
460     else
461     {
462         *p_sample++ = channel[0].i_sample2;
463         *p_sample++ = channel[0].i_sample1;
464     }
465
466     for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
467          i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
468     {
469         *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
470         *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
471                                            (*p_buffer)&0x0f);
472     }
473 }
474
475 /*
476  * IMA-WAV
477  */
478 typedef struct adpcm_ima_wav_channel_s
479 {
480     int i_predictor;
481     int i_step_index;
482
483 } adpcm_ima_wav_channel_t;
484
485 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
486                                    int i_nibble )
487 {
488     int i_diff;
489
490     i_diff = i_step_table[p_channel->i_step_index] >> 3;
491     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
492     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
493     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
494     if( i_nibble&0x08 )
495         p_channel->i_predictor -= i_diff;
496     else
497         p_channel->i_predictor += i_diff;
498
499     CLAMP( p_channel->i_predictor, -32768, 32767 );
500
501     p_channel->i_step_index += i_index_table[i_nibble];
502
503     CLAMP( p_channel->i_step_index, 0, 88 );
504
505     return( p_channel->i_predictor );
506 }
507
508 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
509                                uint8_t *p_buffer )
510 {
511     decoder_sys_t *p_sys  = p_dec->p_sys;
512     adpcm_ima_wav_channel_t channel[2];
513     int                     i_nibbles;
514     int                     b_stereo;
515
516     b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
517
518     GetWord( channel[0].i_predictor );
519     GetByte( channel[0].i_step_index );
520     CLAMP( channel[0].i_step_index, 0, 88 );
521     p_buffer++;
522
523     if( b_stereo )
524     {
525         GetWord( channel[1].i_predictor );
526         GetByte( channel[1].i_step_index );
527         CLAMP( channel[1].i_step_index, 0, 88 );
528         p_buffer++;
529     }
530
531     if( b_stereo )
532     {
533         for( i_nibbles = 2 * (p_sys->i_block - 8);
534              i_nibbles > 0;
535              i_nibbles -= 16 )
536         {
537             int i;
538
539             for( i = 0; i < 4; i++ )
540             {
541                 p_sample[i * 4] =
542                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
543                 p_sample[i * 4 + 2] =
544                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
545             }
546             p_buffer += 4;
547
548             for( i = 0; i < 4; i++ )
549             {
550                 p_sample[i * 4 + 1] =
551                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
552                 p_sample[i * 4 + 3] =
553                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
554             }
555             p_buffer += 4;
556             p_sample += 16;
557
558         }
559
560
561     }
562     else
563     {
564         for( i_nibbles = 2 * (p_sys->i_block - 4);
565              i_nibbles > 0;
566              i_nibbles -= 2, p_buffer++ )
567         {
568             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
569             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
570         }
571     }
572 }
573
574 /*
575  * Ima4 in QT file
576  */
577 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
578                               uint8_t *p_buffer )
579 {
580     adpcm_ima_wav_channel_t channel[2];
581     int                     i_nibbles;
582     int                     i_ch;
583     int                     i_step;
584
585     i_step = p_dec->fmt_in.audio.i_channels;
586
587     for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
588     {
589         /* load preambule */
590         channel[i_ch].i_predictor  = (int16_t)((( ( p_buffer[0] << 1 )|(  p_buffer[1] >> 7 ) ))<<7);
591         channel[i_ch].i_step_index = p_buffer[1]&0x7f;
592
593         CLAMP( channel[i_ch].i_step_index, 0, 88 );
594         p_buffer += 2;
595
596         for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
597         {
598             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
599             p_sample += i_step;
600
601             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
602             p_sample += i_step;
603
604             p_buffer++;
605         }
606
607         /* Next channel */
608         p_sample += 1 - 64 * i_step;
609     }
610 }
611
612 /*
613  * Dk4
614  */
615 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
616                             uint8_t *p_buffer )
617 {
618     decoder_sys_t *p_sys  = p_dec->p_sys;
619     adpcm_ima_wav_channel_t channel[2];
620     size_t                  i_nibbles;
621     int                     b_stereo;
622
623     b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
624
625     GetWord( channel[0].i_predictor );
626     GetByte( channel[0].i_step_index );
627     CLAMP( channel[0].i_step_index, 0, 88 );
628     p_buffer++;
629
630     if( b_stereo )
631     {
632         GetWord( channel[1].i_predictor );
633         GetByte( channel[1].i_step_index );
634         CLAMP( channel[1].i_step_index, 0, 88 );
635         p_buffer++;
636     }
637
638     /* first output predictor */
639     *p_sample++ = channel[0].i_predictor;
640     if( b_stereo )
641     {
642         *p_sample++ = channel[1].i_predictor;
643     }
644
645     for( i_nibbles = 0;
646          i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
647          i_nibbles++ )
648     {
649         *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
650                                               (*p_buffer) >> 4);
651         *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
652                                                (*p_buffer)&0x0f);
653
654         p_buffer++;
655     }
656 }
657
658 /*
659  * Dk3
660  */
661 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
662                             uint8_t *p_buffer )
663 {
664     decoder_sys_t *p_sys  = p_dec->p_sys;
665     uint8_t                 *p_end = &p_buffer[p_sys->i_block];
666     adpcm_ima_wav_channel_t sum;
667     adpcm_ima_wav_channel_t diff;
668     int                     i_diff_value;
669
670     p_buffer += 10;
671
672     GetWord( sum.i_predictor );
673     GetWord( diff.i_predictor );
674     GetByte( sum.i_step_index );
675     GetByte( diff.i_step_index );
676
677     i_diff_value = diff.i_predictor;
678     /* we process 6 nibbles at once */
679     while( p_buffer + 1 <= p_end )
680     {
681         /* first 3 nibbles */
682         AdpcmImaWavExpandNibble( &sum,
683                                  (*p_buffer)&0x0f);
684
685         AdpcmImaWavExpandNibble( &diff,
686                                  (*p_buffer) >> 4 );
687
688         i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
689
690         *p_sample++ = sum.i_predictor + i_diff_value;
691         *p_sample++ = sum.i_predictor - i_diff_value;
692
693         p_buffer++;
694
695         AdpcmImaWavExpandNibble( &sum,
696                                  (*p_buffer)&0x0f);
697
698         *p_sample++ = sum.i_predictor + i_diff_value;
699         *p_sample++ = sum.i_predictor - i_diff_value;
700
701         /* now last 3 nibbles */
702         AdpcmImaWavExpandNibble( &sum,
703                                  (*p_buffer)>>4);
704         p_buffer++;
705         if( p_buffer < p_end )
706         {
707             AdpcmImaWavExpandNibble( &diff,
708                                      (*p_buffer)&0x0f );
709
710             i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
711
712             *p_sample++ = sum.i_predictor + i_diff_value;
713             *p_sample++ = sum.i_predictor - i_diff_value;
714
715             AdpcmImaWavExpandNibble( &sum,
716                                      (*p_buffer)>>4);
717             p_buffer++;
718
719             *p_sample++ = sum.i_predictor + i_diff_value;
720             *p_sample++ = sum.i_predictor - i_diff_value;
721         }
722     }
723 }
724
725
726 /*
727  * EA ADPCM
728  */
729 #define MAX_CHAN 5
730 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
731                            uint8_t *p_buffer )
732 {
733     static const uint32_t EATable[]=
734     {
735         0x00000000, 0x000000F0, 0x000001CC, 0x00000188,
736         0x00000000, 0x00000000, 0xFFFFFF30, 0xFFFFFF24,
737         0x00000000, 0x00000001, 0x00000003, 0x00000004,
738         0x00000007, 0x00000008, 0x0000000A, 0x0000000B,
739         0x00000000, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFC
740     };
741     decoder_sys_t *p_sys  = p_dec->p_sys;
742     uint8_t *p_end;
743     unsigned i_channels, c;
744     int16_t *prev, *cur;
745     int32_t c1[MAX_CHAN], c2[MAX_CHAN];
746     int8_t d[MAX_CHAN];
747
748     i_channels = p_dec->fmt_in.audio.i_channels;
749     p_end = &p_buffer[p_sys->i_block];
750
751     prev = (int16_t *)p_dec->fmt_in.p_extra;
752     cur = prev + i_channels;
753
754     for (c = 0; c < i_channels; c++)
755     {
756         uint8_t input;
757
758         input = p_buffer[c];
759         c1[c] = EATable[input >> 4];
760         c2[c] = EATable[(input >> 4) + 4];
761         d[c] = (input & 0xf) + 8;
762     }
763
764     for( p_buffer += i_channels; p_buffer < p_end ; p_buffer += i_channels)
765     {
766         for (c = 0; c < i_channels; c++)
767         {
768             int32_t spl;
769
770             spl = (p_buffer[c] >> 4) & 0xf;
771             spl = (spl << 0x1c) >> d[c];
772             spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
773             CLAMP( spl, -32768, 32767 );
774             prev[c] = cur[c];
775             cur[c] = spl;
776
777             *(p_sample++) = spl;
778         }
779
780         for (c = 0; c < i_channels; c++)
781         {
782             int32_t spl;
783
784             spl = p_buffer[c] & 0xf;
785             spl = (spl << 0x1c) >> d[c];
786             spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
787             CLAMP( spl, -32768, 32767 );
788             prev[c] = cur[c];
789             cur[c] = spl;
790
791             *(p_sample++) = spl;
792         }
793     }
794 }