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