1 /*****************************************************************************
2  * adpcm.c : adpcm variant audio decoder
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VLC authors and VideoLAN
5  * $Id: 26278d2d8d70e499783939b5bd96a80d5ea76a8e $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Rémi Denis-Courmont
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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_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 int DecodeAudio( decoder_t *, block_t * );
45 static void Flush( decoder_t * );
46 
47 vlc_module_begin ()
48     set_description( N_("ADPCM audio decoder") )
49     set_capability( "audio 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     int16_t            *prev;
77 };
78 
79 static void DecodeAdpcmMs    ( decoder_t *, int16_t *, uint8_t * );
80 static void DecodeAdpcmImaWav( decoder_t *, int16_t *, uint8_t * );
81 static void DecodeAdpcmImaQT ( decoder_t *, int16_t *, uint8_t * );
82 static void DecodeAdpcmDk4   ( decoder_t *, int16_t *, uint8_t * );
83 static void DecodeAdpcmDk3   ( decoder_t *, int16_t *, uint8_t * );
84 static void DecodeAdpcmEA    ( decoder_t *, int16_t *, uint8_t * );
85 
86 static const int pi_channels_maps[6] =
87 {
88     0,
89     AOUT_CHAN_CENTER,
90     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
91     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
92     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
93     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
94      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
95 };
96 
97 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
98 static const int i_index_table[16] =
99 {
100     -1, -1, -1, -1, 2, 4, 6, 8,
101     -1, -1, -1, -1, 2, 4, 6, 8
102 };
103 
104 static const int i_step_table[89] =
105 {
106     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
107     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
108     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
109     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
110     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
111     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
112     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
113     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
114     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
115 };
116 
117 static const int i_adaptation_table[16] =
118 {
119     230, 230, 230, 230, 307, 409, 512, 614,
120     768, 614, 512, 409, 307, 230, 230, 230
121 };
122 
123 static const int i_adaptation_coeff1[7] =
124 {
125     256, 512, 0, 192, 240, 460, 392
126 };
127 
128 static const int i_adaptation_coeff2[7] =
129 {
130     0, -256, 0, 64, 0, -208, -232
131 };
132 
133 /*****************************************************************************
134  * OpenDecoder: probe the decoder and return score
135  *****************************************************************************/
OpenDecoder(vlc_object_t * p_this)136 static int OpenDecoder( vlc_object_t *p_this )
137 {
138     decoder_t *p_dec = (decoder_t*)p_this;
139     decoder_sys_t *p_sys;
140 
141     switch( p_dec->fmt_in.i_codec )
142     {
143         case VLC_CODEC_ADPCM_IMA_QT:
144         case VLC_CODEC_ADPCM_IMA_WAV:
145         case VLC_CODEC_ADPCM_MS:
146         case VLC_CODEC_ADPCM_DK4:
147         case VLC_CODEC_ADPCM_DK3:
148         case VLC_CODEC_ADPCM_XA_EA:
149             break;
150         default:
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     p_sys = malloc(sizeof(*p_sys));
162     if( unlikely(p_sys == NULL) )
163         return VLC_ENOMEM;
164 
165     p_sys->prev = NULL;
166     p_sys->i_samplesperblock = 0;
167 
168     unsigned i_channels = p_dec->fmt_in.audio.i_channels;
169     uint8_t i_max_channels = 5;
170     switch( p_dec->fmt_in.i_codec )
171     {
172         case VLC_CODEC_ADPCM_IMA_QT: /* IMA ADPCM */
173             p_sys->codec = ADPCM_IMA_QT;
174             i_max_channels = 2;
175             break;
176         case VLC_CODEC_ADPCM_IMA_WAV: /* IMA ADPCM */
177             p_sys->codec = ADPCM_IMA_WAV;
178             i_max_channels = 2;
179             break;
180         case VLC_CODEC_ADPCM_MS: /* MS ADPCM */
181             p_sys->codec = ADPCM_MS;
182             i_max_channels = 2;
183             break;
184         case VLC_CODEC_ADPCM_DK4: /* Duck DK4 ADPCM */
185             p_sys->codec = ADPCM_DK4;
186             i_max_channels = 2;
187             break;
188         case VLC_CODEC_ADPCM_DK3: /* Duck DK3 ADPCM */
189             p_sys->codec = ADPCM_DK3;
190             i_max_channels = 2;
191             break;
192         case VLC_CODEC_ADPCM_XA_EA: /* EA ADPCM */
193             p_sys->codec = ADPCM_EA;
194             p_sys->prev = calloc( 2 * p_dec->fmt_in.audio.i_channels,
195                                   sizeof( int16_t ) );
196             if( unlikely(p_sys->prev == NULL) )
197             {
198                 free( p_sys );
199                 return VLC_ENOMEM;
200             }
201             break;
202     }
203 
204     if (i_channels > i_max_channels || i_channels == 0)
205     {
206         free(p_sys->prev);
207         free(p_sys);
208         msg_Err( p_dec, "Invalid number of channels %i", p_dec->fmt_in.audio.i_channels );
209         return VLC_EGENERIC;
210     }
211 
212     if( p_dec->fmt_in.audio.i_blockalign <= 0 )
213     {
214         p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
215             34 * p_dec->fmt_in.audio.i_channels : 1024;
216         msg_Warn( p_dec, "block size undefined, using %zu", p_sys->i_block );
217     }
218     else
219     {
220         p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
221     }
222 
223     /* calculate samples per block */
224     switch( p_sys->codec )
225     {
226     case ADPCM_IMA_QT:
227         p_sys->i_samplesperblock = 64;
228         break;
229     case ADPCM_IMA_WAV:
230         if( p_sys->i_block >= 4 * i_channels )
231         {
232             p_sys->i_samplesperblock = 2 * ( p_sys->i_block - 4 * i_channels )
233                                      / i_channels;
234         }
235         break;
236     case ADPCM_MS:
237         if( p_sys->i_block >= 7 * i_channels )
238         {
239             p_sys->i_samplesperblock =
240                 2 * (p_sys->i_block - 7 * i_channels) / i_channels + 2;
241         }
242         break;
243     case ADPCM_DK4:
244         if( p_sys->i_block >= 4 * i_channels )
245         {
246             p_sys->i_samplesperblock =
247                 2 * (p_sys->i_block - 4 * i_channels) / i_channels + 1;
248         }
249         break;
250     case ADPCM_DK3:
251         i_channels = 2;
252         if( p_sys->i_block >= 16 )
253             p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
254         break;
255     case ADPCM_EA:
256         if( p_sys->i_block >= i_channels )
257         {
258             p_sys->i_samplesperblock =
259                 2 * (p_sys->i_block - i_channels) / i_channels;
260         }
261     }
262 
263     msg_Dbg( p_dec, "format: samplerate:%d Hz channels:%d bits/sample:%d "
264              "blockalign:%zu samplesperblock:%zu",
265              p_dec->fmt_in.audio.i_rate, i_channels,
266              p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
267              p_sys->i_samplesperblock );
268 
269     if (p_sys->i_samplesperblock == 0)
270     {
271         free(p_sys->prev);
272         free(p_sys);
273         msg_Err( p_dec, "Error computing number of samples per block");
274         return VLC_EGENERIC;
275     }
276 
277     p_dec->p_sys = p_sys;
278     p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
279     p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
280     p_dec->fmt_out.audio.i_channels = i_channels;
281     p_dec->fmt_out.audio.i_physical_channels = pi_channels_maps[i_channels];
282 
283     date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
284     date_Set( &p_sys->end_date, 0 );
285 
286     p_dec->pf_decode = DecodeAudio;
287     p_dec->pf_flush  = Flush;
288 
289     return VLC_SUCCESS;
290 }
291 
292 /*****************************************************************************
293  * Flush:
294  *****************************************************************************/
Flush(decoder_t * p_dec)295 static void Flush( decoder_t *p_dec )
296 {
297     decoder_sys_t *p_sys = p_dec->p_sys;
298 
299     date_Set( &p_sys->end_date, 0 );
300 }
301 
302 /*****************************************************************************
303  * DecodeBlock:
304  *****************************************************************************/
DecodeBlock(decoder_t * p_dec,block_t ** pp_block)305 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
306 {
307     decoder_sys_t *p_sys  = p_dec->p_sys;
308     block_t *p_block;
309 
310     if( !*pp_block ) return NULL;
311 
312     p_block = *pp_block;
313 
314     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
315     {
316         Flush( p_dec );
317         if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
318             goto drop;
319     }
320 
321     if( p_block->i_pts > VLC_TS_INVALID &&
322         p_block->i_pts != date_Get( &p_sys->end_date ) )
323     {
324         date_Set( &p_sys->end_date, p_block->i_pts );
325     }
326     else if( !date_Get( &p_sys->end_date ) )
327         /* We've just started the stream, wait for the first PTS. */
328         goto drop;
329 
330     /* Don't re-use the same pts twice */
331     p_block->i_pts = VLC_TS_INVALID;
332 
333     if( p_block->i_buffer >= p_sys->i_block )
334     {
335         block_t *p_out;
336 
337         if( decoder_UpdateAudioFormat( p_dec ) )
338             goto drop;
339         p_out = decoder_NewAudioBuffer( p_dec, p_sys->i_samplesperblock );
340         if( p_out == NULL )
341             goto drop;
342 
343         p_out->i_pts = date_Get( &p_sys->end_date );
344         p_out->i_length = date_Increment( &p_sys->end_date,
345                                      p_sys->i_samplesperblock ) - p_out->i_pts;
346 
347         switch( p_sys->codec )
348         {
349         case ADPCM_IMA_QT:
350             DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
351                               p_block->p_buffer );
352             break;
353         case ADPCM_IMA_WAV:
354             DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
355                                p_block->p_buffer );
356             break;
357         case ADPCM_MS:
358             DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
359                            p_block->p_buffer );
360             break;
361         case ADPCM_DK4:
362             DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
363                             p_block->p_buffer );
364             break;
365         case ADPCM_DK3:
366             DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
367                             p_block->p_buffer );
368             break;
369         case ADPCM_EA:
370             DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
371                            p_block->p_buffer );
372         default:
373             break;
374         }
375 
376         p_block->p_buffer += p_sys->i_block;
377         p_block->i_buffer -= p_sys->i_block;
378         return p_out;
379     }
380 
381 drop:
382     block_Release( p_block );
383     *pp_block = NULL;
384     return NULL;
385 }
386 
DecodeAudio(decoder_t * p_dec,block_t * p_block)387 static int DecodeAudio( decoder_t *p_dec, block_t *p_block )
388 {
389     if( p_block == NULL ) /* No Drain */
390         return VLCDEC_SUCCESS;
391 
392     block_t **pp_block = &p_block, *p_out;
393     while( ( p_out = DecodeBlock( p_dec, pp_block ) ) != NULL )
394         decoder_QueueAudio( p_dec, p_out );
395     return VLCDEC_SUCCESS;
396 }
397 
398 /*****************************************************************************
399  * CloseDecoder:
400  *****************************************************************************/
CloseDecoder(vlc_object_t * p_this)401 static void CloseDecoder( vlc_object_t *p_this )
402 {
403     decoder_t *p_dec = (decoder_t *)p_this;
404     decoder_sys_t *p_sys = p_dec->p_sys;
405 
406     free( p_sys->prev );
407     free( p_sys );
408 }
409 
410 /*****************************************************************************
411  * Local functions
412  *****************************************************************************/
413 #define CLAMP( v, min, max ) \
414     if( (v) < (min) ) (v) = (min); \
415     if( (v) > (max) ) (v) = (max)
416 
417 #define GetByte( v ) \
418     (v) = *p_buffer; p_buffer++;
419 
420 #define GetWord( v ) \
421     (v) = *p_buffer; p_buffer++; \
422     (v) |= ( *p_buffer ) << 8; p_buffer++; \
423     if( (v)&0x8000 ) (v) -= 0x010000;
424 
425 /*
426  * MS
427  */
428 typedef struct adpcm_ms_channel_s
429 {
430     int i_idelta;
431     int i_sample1, i_sample2;
432     int i_coeff1, i_coeff2;
433 
434 } adpcm_ms_channel_t;
435 
436 
AdpcmMsExpandNibble(adpcm_ms_channel_t * p_channel,int i_nibble)437 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
438                                int i_nibble )
439 {
440     int i_predictor;
441     int i_snibble;
442     /* expand sign */
443 
444     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
445 
446     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
447                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
448                   i_snibble * p_channel->i_idelta;
449 
450     CLAMP( i_predictor, -32768, 32767 );
451 
452     p_channel->i_sample2 = p_channel->i_sample1;
453     p_channel->i_sample1 = i_predictor;
454 
455     p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
456                             p_channel->i_idelta ) / 256;
457     if( p_channel->i_idelta < 16 )
458     {
459         p_channel->i_idelta = 16;
460     }
461     return( i_predictor );
462 }
463 
DecodeAdpcmMs(decoder_t * p_dec,int16_t * p_sample,uint8_t * p_buffer)464 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
465                            uint8_t *p_buffer )
466 {
467     decoder_sys_t *p_sys  = p_dec->p_sys;
468     adpcm_ms_channel_t channel[2];
469     int b_stereo;
470     int i_block_predictor;
471 
472     size_t i_total_samples = p_sys->i_samplesperblock;
473     if(i_total_samples < 2)
474         return;
475 
476     b_stereo = p_dec->fmt_out.audio.i_channels == 2 ? 1 : 0;
477 
478     GetByte( i_block_predictor );
479     CLAMP( i_block_predictor, 0, 6 );
480     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
481     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
482 
483     if( b_stereo )
484     {
485         GetByte( i_block_predictor );
486         CLAMP( i_block_predictor, 0, 6 );
487         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
488         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
489     }
490     GetWord( channel[0].i_idelta );
491     if( b_stereo )
492     {
493         GetWord( channel[1].i_idelta );
494     }
495 
496     GetWord( channel[0].i_sample1 );
497     if( b_stereo )
498     {
499         GetWord( channel[1].i_sample1 );
500     }
501 
502     GetWord( channel[0].i_sample2 );
503     if( b_stereo )
504     {
505         GetWord( channel[1].i_sample2 );
506     }
507 
508     if( b_stereo )
509     {
510         *p_sample++ = channel[0].i_sample2;
511         *p_sample++ = channel[1].i_sample2;
512         *p_sample++ = channel[0].i_sample1;
513         *p_sample++ = channel[1].i_sample1;
514     }
515     else
516     {
517         *p_sample++ = channel[0].i_sample2;
518         *p_sample++ = channel[0].i_sample1;
519     }
520 
521     for( i_total_samples -= 2; i_total_samples >= 2; i_total_samples -= 2, p_buffer++ )
522     {
523         *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
524         *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
525                                            (*p_buffer)&0x0f);
526     }
527 }
528 
529 /*
530  * IMA-WAV
531  */
532 typedef struct adpcm_ima_wav_channel_s
533 {
534     int i_predictor;
535     int i_step_index;
536 
537 } adpcm_ima_wav_channel_t;
538 
AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t * p_channel,int i_nibble)539 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
540                                    int i_nibble )
541 {
542     int i_diff;
543 
544     i_diff = i_step_table[p_channel->i_step_index] >> 3;
545     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
546     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
547     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
548     if( i_nibble&0x08 )
549         p_channel->i_predictor -= i_diff;
550     else
551         p_channel->i_predictor += i_diff;
552 
553     CLAMP( p_channel->i_predictor, -32768, 32767 );
554 
555     p_channel->i_step_index += i_index_table[i_nibble];
556 
557     CLAMP( p_channel->i_step_index, 0, 88 );
558 
559     return( p_channel->i_predictor );
560 }
561 
DecodeAdpcmImaWav(decoder_t * p_dec,int16_t * p_sample,uint8_t * p_buffer)562 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
563                                uint8_t *p_buffer )
564 {
565     decoder_sys_t *p_sys  = p_dec->p_sys;
566     adpcm_ima_wav_channel_t channel[2];
567     int                     i_nibbles;
568     int                     b_stereo;
569 
570     b_stereo = p_dec->fmt_out.audio.i_channels == 2 ? 1 : 0;
571 
572     GetWord( channel[0].i_predictor );
573     GetByte( channel[0].i_step_index );
574     CLAMP( channel[0].i_step_index, 0, 88 );
575     p_buffer++;
576 
577     if( b_stereo )
578     {
579         GetWord( channel[1].i_predictor );
580         GetByte( channel[1].i_step_index );
581         CLAMP( channel[1].i_step_index, 0, 88 );
582         p_buffer++;
583     }
584 
585     if( b_stereo )
586     {
587         for( i_nibbles = 2 * (p_sys->i_block - 8);
588              i_nibbles > 0;
589              i_nibbles -= 16 )
590         {
591             int i;
592 
593             for( i = 0; i < 4; i++ )
594             {
595                 p_sample[i * 4] =
596                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
597                 p_sample[i * 4 + 2] =
598                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
599             }
600             p_buffer += 4;
601 
602             for( i = 0; i < 4; i++ )
603             {
604                 p_sample[i * 4 + 1] =
605                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
606                 p_sample[i * 4 + 3] =
607                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
608             }
609             p_buffer += 4;
610             p_sample += 16;
611 
612         }
613 
614 
615     }
616     else
617     {
618         for( i_nibbles = 2 * (p_sys->i_block - 4);
619              i_nibbles > 0;
620              i_nibbles -= 2, p_buffer++ )
621         {
622             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
623             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
624         }
625     }
626 }
627 
628 /*
629  * Ima4 in QT file
630  */
DecodeAdpcmImaQT(decoder_t * p_dec,int16_t * p_sample,uint8_t * p_buffer)631 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
632                               uint8_t *p_buffer )
633 {
634     adpcm_ima_wav_channel_t channel[2];
635     int                     i_nibbles;
636     int                     i_ch;
637     int                     i_step;
638 
639     i_step = p_dec->fmt_out.audio.i_channels;
640 
641     for( i_ch = 0; i_ch < p_dec->fmt_out.audio.i_channels; i_ch++ )
642     {
643         /* load preambule */
644         channel[i_ch].i_predictor  = (int16_t)((( ( p_buffer[0] << 1 )|(  p_buffer[1] >> 7 ) ))<<7);
645         channel[i_ch].i_step_index = p_buffer[1]&0x7f;
646 
647         CLAMP( channel[i_ch].i_step_index, 0, 88 );
648         p_buffer += 2;
649 
650         for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
651         {
652             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
653             p_sample += i_step;
654 
655             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
656             p_sample += i_step;
657 
658             p_buffer++;
659         }
660 
661         /* Next channel */
662         p_sample += 1 - 64 * i_step;
663     }
664 }
665 
666 /*
667  * Dk4
668  */
DecodeAdpcmDk4(decoder_t * p_dec,int16_t * p_sample,uint8_t * p_buffer)669 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
670                             uint8_t *p_buffer )
671 {
672     decoder_sys_t *p_sys  = p_dec->p_sys;
673     adpcm_ima_wav_channel_t channel[2];
674     size_t                  i_nibbles;
675     int                     b_stereo;
676 
677     b_stereo = p_dec->fmt_out.audio.i_channels == 2 ? 1 : 0;
678 
679     GetWord( channel[0].i_predictor );
680     GetByte( channel[0].i_step_index );
681     CLAMP( channel[0].i_step_index, 0, 88 );
682     p_buffer++;
683 
684     if( b_stereo )
685     {
686         GetWord( channel[1].i_predictor );
687         GetByte( channel[1].i_step_index );
688         CLAMP( channel[1].i_step_index, 0, 88 );
689         p_buffer++;
690     }
691 
692     /* first output predictor */
693     *p_sample++ = channel[0].i_predictor;
694     if( b_stereo )
695     {
696         *p_sample++ = channel[1].i_predictor;
697     }
698 
699     for( i_nibbles = 0;
700          i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
701          i_nibbles++ )
702     {
703         *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
704                                               (*p_buffer) >> 4);
705         *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
706                                                (*p_buffer)&0x0f);
707 
708         p_buffer++;
709     }
710 }
711 
712 /*
713  * Dk3
714  */
DecodeAdpcmDk3(decoder_t * p_dec,int16_t * p_sample,uint8_t * p_buffer)715 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
716                             uint8_t *p_buffer )
717 {
718     decoder_sys_t *p_sys  = p_dec->p_sys;
719     uint8_t                 *p_end = &p_buffer[p_sys->i_block];
720     adpcm_ima_wav_channel_t sum;
721     adpcm_ima_wav_channel_t diff;
722     int                     i_diff_value;
723 
724     p_buffer += 10;
725 
726     GetWord( sum.i_predictor );
727     GetWord( diff.i_predictor );
728     GetByte( sum.i_step_index );
729     GetByte( diff.i_step_index );
730 
731     i_diff_value = diff.i_predictor;
732     /* we process 6 nibbles at once */
733     while( p_buffer + 1 <= p_end )
734     {
735         /* first 3 nibbles */
736         AdpcmImaWavExpandNibble( &sum,
737                                  (*p_buffer)&0x0f);
738 
739         AdpcmImaWavExpandNibble( &diff,
740                                  (*p_buffer) >> 4 );
741 
742         i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
743 
744         *p_sample++ = sum.i_predictor + i_diff_value;
745         *p_sample++ = sum.i_predictor - i_diff_value;
746 
747         p_buffer++;
748 
749         AdpcmImaWavExpandNibble( &sum,
750                                  (*p_buffer)&0x0f);
751 
752         *p_sample++ = sum.i_predictor + i_diff_value;
753         *p_sample++ = sum.i_predictor - i_diff_value;
754 
755         /* now last 3 nibbles */
756         AdpcmImaWavExpandNibble( &sum,
757                                  (*p_buffer)>>4);
758         p_buffer++;
759         if( p_buffer < p_end )
760         {
761             AdpcmImaWavExpandNibble( &diff,
762                                      (*p_buffer)&0x0f );
763 
764             i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
765 
766             *p_sample++ = sum.i_predictor + i_diff_value;
767             *p_sample++ = sum.i_predictor - i_diff_value;
768 
769             AdpcmImaWavExpandNibble( &sum,
770                                      (*p_buffer)>>4);
771             p_buffer++;
772 
773             *p_sample++ = sum.i_predictor + i_diff_value;
774             *p_sample++ = sum.i_predictor - i_diff_value;
775         }
776     }
777 }
778 
779 
780 /*
781  * EA ADPCM
782  */
783 #define MAX_CHAN 5
DecodeAdpcmEA(decoder_t * p_dec,int16_t * p_sample,uint8_t * p_buffer)784 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
785                            uint8_t *p_buffer )
786 {
787     static const int16_t EATable[]=
788     {
789         0x0000, 0x00F0, 0x01CC, 0x0188, 0x0000, 0x0000, 0xFF30, 0xFF24,
790         0x0000, 0x0001, 0x0003, 0x0004, 0x0007, 0x0008, 0x000A, 0x000B,
791         0x0000, 0xFFFF, 0xFFFD, 0xFFFC,
792     };
793     decoder_sys_t *p_sys  = p_dec->p_sys;
794     int_fast32_t c1[MAX_CHAN], c2[MAX_CHAN];
795     int_fast8_t d[MAX_CHAN];
796 
797     unsigned chans = p_dec->fmt_out.audio.i_channels;
798     const uint8_t *p_end = &p_buffer[p_sys->i_block];
799     int16_t *prev = p_sys->prev;
800     int16_t *cur = prev + chans;
801 
802     for (unsigned c = 0; c < chans; c++)
803     {
804         uint8_t input = p_buffer[c];
805 
806         c1[c] = EATable[input >> 4];
807         c2[c] = EATable[(input >> 4) + 4];
808         d[c] = (input & 0xf) + 8;
809     }
810 
811     for (p_buffer += chans; p_buffer < p_end; p_buffer += chans)
812     {
813         union { uint32_t u; int32_t i; } spl;
814 
815         for (unsigned c = 0; c < chans; c++)
816         {
817             spl.u = (p_buffer[c] & 0xf0u) << 24u;
818             spl.i >>= d[c];
819             spl.i = (spl.i + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
820             CLAMP(spl.i, -32768, 32767);
821             prev[c] = cur[c];
822             cur[c] = spl.i;
823 
824             *(p_sample++) = spl.i;
825         }
826 
827         for (unsigned c = 0; c < chans; c++)
828         {
829             spl.u = (p_buffer[c] & 0x0fu) << 28u;
830             spl.i >>= d[c];
831             spl.i = (spl.i + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
832             CLAMP(spl.i, -32768, 32767);
833             prev[c] = cur[c];
834             cur[c] = spl.i;
835 
836             *(p_sample++) = spl.i;
837         }
838     }
839 }
840