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