packetizer: hevc: add poc debug
[vlc.git] / modules / codec / adpcm.c
bloba1979bd344aa7e2bed1e82dbc7b9776372bb8ae5
1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
28 * Documentation: http://www.pcisys.net/~melanson/codecs/adpcm.txt
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 static int OpenDecoder( vlc_object_t * );
42 static void CloseDecoder( vlc_object_t * );
44 static int DecodeAudio( decoder_t *, block_t * );
45 static void Flush( decoder_t * );
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 ()
55 /*****************************************************************************
56 * Local prototypes
57 *****************************************************************************/
58 enum adpcm_codec_e
60 ADPCM_IMA_QT,
61 ADPCM_IMA_WAV,
62 ADPCM_MS,
63 ADPCM_DK3,
64 ADPCM_DK4,
65 ADPCM_EA
68 struct decoder_sys_t
70 enum adpcm_codec_e codec;
72 size_t i_block;
73 size_t i_samplesperblock;
75 date_t end_date;
76 int16_t *prev;
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 * );
86 static const int pi_channels_maps[6] =
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
97 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
98 static const int i_index_table[16] =
100 -1, -1, -1, -1, 2, 4, 6, 8,
101 -1, -1, -1, -1, 2, 4, 6, 8
104 static const int i_step_table[89] =
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
117 static const int i_adaptation_table[16] =
119 230, 230, 230, 230, 307, 409, 512, 614,
120 768, 614, 512, 409, 307, 230, 230, 230
123 static const int i_adaptation_coeff1[7] =
125 256, 512, 0, 192, 240, 460, 392
128 static const int i_adaptation_coeff2[7] =
130 0, -256, 0, 64, 0, -208, -232
133 /*****************************************************************************
134 * OpenDecoder: probe the decoder and return score
135 *****************************************************************************/
136 static int OpenDecoder( vlc_object_t *p_this )
138 decoder_t *p_dec = (decoder_t*)p_this;
139 decoder_sys_t *p_sys;
141 switch( p_dec->fmt_in.i_codec )
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;
154 if( p_dec->fmt_in.audio.i_rate <= 0 )
156 msg_Err( p_dec, "bad samplerate" );
157 return VLC_EGENERIC;
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;
165 p_sys->prev = NULL;
166 p_sys->i_samplesperblock = 0;
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 )
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) )
198 free( p_sys );
199 return VLC_ENOMEM;
201 break;
204 if (i_channels > i_max_channels || i_channels == 0)
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;
212 if( p_dec->fmt_in.audio.i_blockalign <= 0 )
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 );
218 else
220 p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
223 /* calculate samples per block */
224 switch( p_sys->codec )
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 )
232 p_sys->i_samplesperblock = 2 * ( p_sys->i_block - 4 * i_channels )
233 / i_channels;
235 break;
236 case ADPCM_MS:
237 if( p_sys->i_block >= 7 * i_channels )
239 p_sys->i_samplesperblock =
240 2 * (p_sys->i_block - 7 * i_channels) / i_channels + 2;
242 break;
243 case ADPCM_DK4:
244 if( p_sys->i_block >= 4 * i_channels )
246 p_sys->i_samplesperblock =
247 2 * (p_sys->i_block - 4 * i_channels) / i_channels + 1;
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 )
258 p_sys->i_samplesperblock =
259 2 * (p_sys->i_block - i_channels) / i_channels;
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 );
269 if (p_sys->i_samplesperblock == 0)
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;
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];
283 date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
284 date_Set( &p_sys->end_date, 0 );
286 p_dec->pf_decode = DecodeAudio;
287 p_dec->pf_flush = Flush;
289 return VLC_SUCCESS;
292 /*****************************************************************************
293 * Flush:
294 *****************************************************************************/
295 static void Flush( decoder_t *p_dec )
297 decoder_sys_t *p_sys = p_dec->p_sys;
299 date_Set( &p_sys->end_date, 0 );
302 /*****************************************************************************
303 * DecodeBlock:
304 *****************************************************************************/
305 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
307 decoder_sys_t *p_sys = p_dec->p_sys;
308 block_t *p_block;
310 if( !*pp_block ) return NULL;
312 p_block = *pp_block;
314 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
316 Flush( p_dec );
317 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
318 goto drop;
321 if( p_block->i_pts > VLC_TS_INVALID &&
322 p_block->i_pts != date_Get( &p_sys->end_date ) )
324 date_Set( &p_sys->end_date, p_block->i_pts );
326 else if( !date_Get( &p_sys->end_date ) )
327 /* We've just started the stream, wait for the first PTS. */
328 goto drop;
330 /* Don't re-use the same pts twice */
331 p_block->i_pts = VLC_TS_INVALID;
333 if( p_block->i_buffer >= p_sys->i_block )
335 block_t *p_out;
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;
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;
347 switch( p_sys->codec )
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;
376 p_block->p_buffer += p_sys->i_block;
377 p_block->i_buffer -= p_sys->i_block;
378 return p_out;
381 drop:
382 block_Release( p_block );
383 *pp_block = NULL;
384 return NULL;
387 static int DecodeAudio( decoder_t *p_dec, block_t *p_block )
389 if( p_block == NULL ) /* No Drain */
390 return VLCDEC_SUCCESS;
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;
398 /*****************************************************************************
399 * CloseDecoder:
400 *****************************************************************************/
401 static void CloseDecoder( vlc_object_t *p_this )
403 decoder_t *p_dec = (decoder_t *)p_this;
404 decoder_sys_t *p_sys = p_dec->p_sys;
406 free( p_sys->prev );
407 free( p_sys );
410 /*****************************************************************************
411 * Local functions
412 *****************************************************************************/
413 #define CLAMP( v, min, max ) \
414 if( (v) < (min) ) (v) = (min); \
415 if( (v) > (max) ) (v) = (max)
417 #define GetByte( v ) \
418 (v) = *p_buffer; p_buffer++;
420 #define GetWord( v ) \
421 (v) = *p_buffer; p_buffer++; \
422 (v) |= ( *p_buffer ) << 8; p_buffer++; \
423 if( (v)&0x8000 ) (v) -= 0x010000;
426 * MS
428 typedef struct adpcm_ms_channel_s
430 int i_idelta;
431 int i_sample1, i_sample2;
432 int i_coeff1, i_coeff2;
434 } adpcm_ms_channel_t;
437 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
438 int i_nibble )
440 int i_predictor;
441 int i_snibble;
442 /* expand sign */
444 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
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;
450 CLAMP( i_predictor, -32768, 32767 );
452 p_channel->i_sample2 = p_channel->i_sample1;
453 p_channel->i_sample1 = i_predictor;
455 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
456 p_channel->i_idelta ) / 256;
457 if( p_channel->i_idelta < 16 )
459 p_channel->i_idelta = 16;
461 return( i_predictor );
464 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
465 uint8_t *p_buffer )
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;
472 size_t i_total_samples = p_sys->i_samplesperblock;
473 if(i_total_samples < 2)
474 return;
476 b_stereo = p_dec->fmt_out.audio.i_channels == 2 ? 1 : 0;
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];
483 if( b_stereo )
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];
490 GetWord( channel[0].i_idelta );
491 if( b_stereo )
493 GetWord( channel[1].i_idelta );
496 GetWord( channel[0].i_sample1 );
497 if( b_stereo )
499 GetWord( channel[1].i_sample1 );
502 GetWord( channel[0].i_sample2 );
503 if( b_stereo )
505 GetWord( channel[1].i_sample2 );
508 if( b_stereo )
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;
515 else
517 *p_sample++ = channel[0].i_sample2;
518 *p_sample++ = channel[0].i_sample1;
521 for( i_total_samples -= 2; i_total_samples >= 2; i_total_samples -= 2, p_buffer++ )
523 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
524 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
525 (*p_buffer)&0x0f);
530 * IMA-WAV
532 typedef struct adpcm_ima_wav_channel_s
534 int i_predictor;
535 int i_step_index;
537 } adpcm_ima_wav_channel_t;
539 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
540 int i_nibble )
542 int i_diff;
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;
553 CLAMP( p_channel->i_predictor, -32768, 32767 );
555 p_channel->i_step_index += i_index_table[i_nibble];
557 CLAMP( p_channel->i_step_index, 0, 88 );
559 return( p_channel->i_predictor );
562 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
563 uint8_t *p_buffer )
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;
570 b_stereo = p_dec->fmt_out.audio.i_channels == 2 ? 1 : 0;
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++;
577 if( b_stereo )
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++;
585 if( b_stereo )
587 for( i_nibbles = 2 * (p_sys->i_block - 8);
588 i_nibbles > 0;
589 i_nibbles -= 16 )
591 int i;
593 for( i = 0; i < 4; i++ )
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);
600 p_buffer += 4;
602 for( i = 0; i < 4; i++ )
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);
609 p_buffer += 4;
610 p_sample += 16;
616 else
618 for( i_nibbles = 2 * (p_sys->i_block - 4);
619 i_nibbles > 0;
620 i_nibbles -= 2, p_buffer++ )
622 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
623 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
629 * Ima4 in QT file
631 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
632 uint8_t *p_buffer )
634 adpcm_ima_wav_channel_t channel[2];
635 int i_nibbles;
636 int i_ch;
637 int i_step;
639 i_step = p_dec->fmt_out.audio.i_channels;
641 for( i_ch = 0; i_ch < p_dec->fmt_out.audio.i_channels; i_ch++ )
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;
647 CLAMP( channel[i_ch].i_step_index, 0, 88 );
648 p_buffer += 2;
650 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
652 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
653 p_sample += i_step;
655 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
656 p_sample += i_step;
658 p_buffer++;
661 /* Next channel */
662 p_sample += 1 - 64 * i_step;
667 * Dk4
669 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
670 uint8_t *p_buffer )
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;
677 b_stereo = p_dec->fmt_out.audio.i_channels == 2 ? 1 : 0;
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++;
684 if( b_stereo )
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++;
692 /* first output predictor */
693 *p_sample++ = channel[0].i_predictor;
694 if( b_stereo )
696 *p_sample++ = channel[1].i_predictor;
699 for( i_nibbles = 0;
700 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
701 i_nibbles++ )
703 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
704 (*p_buffer) >> 4);
705 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
706 (*p_buffer)&0x0f);
708 p_buffer++;
713 * Dk3
715 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
716 uint8_t *p_buffer )
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;
724 p_buffer += 10;
726 GetWord( sum.i_predictor );
727 GetWord( diff.i_predictor );
728 GetByte( sum.i_step_index );
729 GetByte( diff.i_step_index );
731 i_diff_value = diff.i_predictor;
732 /* we process 6 nibbles at once */
733 while( p_buffer + 1 <= p_end )
735 /* first 3 nibbles */
736 AdpcmImaWavExpandNibble( &sum,
737 (*p_buffer)&0x0f);
739 AdpcmImaWavExpandNibble( &diff,
740 (*p_buffer) >> 4 );
742 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
744 *p_sample++ = sum.i_predictor + i_diff_value;
745 *p_sample++ = sum.i_predictor - i_diff_value;
747 p_buffer++;
749 AdpcmImaWavExpandNibble( &sum,
750 (*p_buffer)&0x0f);
752 *p_sample++ = sum.i_predictor + i_diff_value;
753 *p_sample++ = sum.i_predictor - i_diff_value;
755 /* now last 3 nibbles */
756 AdpcmImaWavExpandNibble( &sum,
757 (*p_buffer)>>4);
758 p_buffer++;
759 if( p_buffer < p_end )
761 AdpcmImaWavExpandNibble( &diff,
762 (*p_buffer)&0x0f );
764 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
766 *p_sample++ = sum.i_predictor + i_diff_value;
767 *p_sample++ = sum.i_predictor - i_diff_value;
769 AdpcmImaWavExpandNibble( &sum,
770 (*p_buffer)>>4);
771 p_buffer++;
773 *p_sample++ = sum.i_predictor + i_diff_value;
774 *p_sample++ = sum.i_predictor - i_diff_value;
781 * EA ADPCM
783 #define MAX_CHAN 5
784 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
785 uint8_t *p_buffer )
787 static const int16_t EATable[]=
789 0x0000, 0x00F0, 0x01CC, 0x0188, 0x0000, 0x0000, 0xFF30, 0xFF24,
790 0x0000, 0x0001, 0x0003, 0x0004, 0x0007, 0x0008, 0x000A, 0x000B,
791 0x0000, 0xFFFF, 0xFFFD, 0xFFFC,
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];
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;
802 for (unsigned c = 0; c < chans; c++)
804 uint8_t input = p_buffer[c];
806 c1[c] = EATable[input >> 4];
807 c2[c] = EATable[(input >> 4) + 4];
808 d[c] = (input & 0xf) + 8;
811 for (p_buffer += chans; p_buffer < p_end; p_buffer += chans)
813 union { uint32_t u; int32_t i; } spl;
815 for (unsigned c = 0; c < chans; c++)
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;
824 *(p_sample++) = spl.i;
827 for (unsigned c = 0; c < chans; c++)
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;
836 *(p_sample++) = spl.i;