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