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