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