1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
28 * Documentation: http://www.pcisys.net/~melanson/codecs/adpcm.txt
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int OpenDecoder( vlc_object_t * );
43 static void CloseDecoder( vlc_object_t * );
45 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
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 );
55 /*****************************************************************************
57 *****************************************************************************/
70 enum adpcm_codec_e codec;
73 size_t i_samplesperblock;
75 audio_date_t end_date;
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 * );
85 static const int pi_channels_maps[6] =
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
96 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
97 static const int i_index_table[16] =
99 -1, -1, -1, -1, 2, 4, 6, 8,
100 -1, -1, -1, -1, 2, 4, 6, 8
103 static const int i_step_table[89] =
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
116 static const int i_adaptation_table[16] =
118 230, 230, 230, 230, 307, 409, 512, 614,
119 768, 614, 512, 409, 307, 230, 230, 230
122 static const int i_adaptation_coeff1[7] =
124 256, 512, 0, 192, 240, 460, 392
127 static const int i_adaptation_coeff2[7] =
129 0, -256, 0, 64, 0, -208, -232
132 /*****************************************************************************
133 * OpenDecoder: probe the decoder and return score
134 *****************************************************************************/
135 static int OpenDecoder( vlc_object_t *p_this )
137 decoder_t *p_dec = (decoder_t*)p_this;
138 decoder_sys_t *p_sys;
140 switch( p_dec->fmt_in.i_codec )
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 */
153 if( p_dec->fmt_in.audio.i_channels <= 0 ||
154 p_dec->fmt_in.audio.i_channels > 5 )
156 msg_Err( p_dec, "invalid number of channel (not between 1 and 5): %i",
157 p_dec->fmt_in.audio.i_channels );
161 if( p_dec->fmt_in.audio.i_rate <= 0 )
163 msg_Err( p_dec, "bad samplerate" );
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 )
172 switch( p_dec->fmt_in.i_codec )
174 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
175 p_sys->codec = ADPCM_IMA_QT;
177 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
178 p_sys->codec = ADPCM_IMA_WAV;
180 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
181 p_sys->codec = ADPCM_MS;
183 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
184 p_sys->codec = ADPCM_DK4;
186 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
187 p_sys->codec = ADPCM_DK3;
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,
193 if( p_dec->fmt_in.p_extra == NULL )
201 if( p_dec->fmt_in.audio.i_blockalign <= 0 )
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 );
209 p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
212 /* calculate samples per block */
213 switch( p_sys->codec )
216 p_sys->i_samplesperblock = 64;
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;
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;
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;
234 p_dec->fmt_in.audio.i_channels = 2;
235 p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
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;
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 );
249 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
250 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
251 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
252 p_dec->fmt_out.audio.i_physical_channels =
253 p_dec->fmt_out.audio.i_original_channels =
254 pi_channels_maps[p_dec->fmt_in.audio.i_channels];
256 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
257 aout_DateSet( &p_sys->end_date, 0 );
259 p_dec->pf_decode_audio = DecodeBlock;
264 /*****************************************************************************
266 *****************************************************************************/
267 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
269 decoder_sys_t *p_sys = p_dec->p_sys;
272 if( !pp_block || !*pp_block ) return NULL;
276 if( p_block->i_pts != 0 &&
277 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
279 aout_DateSet( &p_sys->end_date, p_block->i_pts );
281 else if( !aout_DateGet( &p_sys->end_date ) )
283 /* We've just started the stream, wait for the first PTS. */
284 block_Release( p_block );
288 /* Don't re-use the same pts twice */
291 if( p_block->i_buffer >= p_sys->i_block )
293 aout_buffer_t *p_out;
295 p_out = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_samplesperblock );
298 block_Release( p_block );
302 p_out->start_date = aout_DateGet( &p_sys->end_date );
304 aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
306 switch( p_sys->codec )
309 DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
313 DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
317 DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
321 DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
325 DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
329 DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
335 p_block->p_buffer += p_sys->i_block;
336 p_block->i_buffer -= p_sys->i_block;
340 block_Release( p_block );
344 /*****************************************************************************
346 *****************************************************************************/
347 static void CloseDecoder( vlc_object_t *p_this )
349 decoder_t *p_dec = (decoder_t *)p_this;
350 decoder_sys_t *p_sys = p_dec->p_sys;
352 if( p_sys->codec == ADPCM_EA )
353 free( p_dec->fmt_in.p_extra );
357 /*****************************************************************************
359 *****************************************************************************/
360 #define CLAMP( v, min, max ) \
361 if( (v) < (min) ) (v) = (min); \
362 if( (v) > (max) ) (v) = (max)
364 #define GetByte( v ) \
365 (v) = *p_buffer; p_buffer++;
367 #define GetWord( v ) \
368 (v) = *p_buffer; p_buffer++; \
369 (v) |= ( *p_buffer ) << 8; p_buffer++; \
370 if( (v)&0x8000 ) (v) -= 0x010000;
375 typedef struct adpcm_ms_channel_s
378 int i_sample1, i_sample2;
379 int i_coeff1, i_coeff2;
381 } adpcm_ms_channel_t;
384 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
391 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
393 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
394 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
395 i_snibble * p_channel->i_idelta;
397 CLAMP( i_predictor, -32768, 32767 );
399 p_channel->i_sample2 = p_channel->i_sample1;
400 p_channel->i_sample1 = i_predictor;
402 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
403 p_channel->i_idelta ) / 256;
404 if( p_channel->i_idelta < 16 )
406 p_channel->i_idelta = 16;
408 return( i_predictor );
411 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
414 decoder_sys_t *p_sys = p_dec->p_sys;
415 adpcm_ms_channel_t channel[2];
418 int i_block_predictor;
420 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
422 GetByte( i_block_predictor );
423 CLAMP( i_block_predictor, 0, 6 );
424 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
425 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
429 GetByte( i_block_predictor );
430 CLAMP( i_block_predictor, 0, 6 );
431 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
432 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
434 GetWord( channel[0].i_idelta );
437 GetWord( channel[1].i_idelta );
440 GetWord( channel[0].i_sample1 );
443 GetWord( channel[1].i_sample1 );
446 GetWord( channel[0].i_sample2 );
449 GetWord( channel[1].i_sample2 );
454 *p_sample++ = channel[0].i_sample2;
455 *p_sample++ = channel[1].i_sample2;
456 *p_sample++ = channel[0].i_sample1;
457 *p_sample++ = channel[1].i_sample1;
461 *p_sample++ = channel[0].i_sample2;
462 *p_sample++ = channel[0].i_sample1;
465 for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
466 i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
468 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
469 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
477 typedef struct adpcm_ima_wav_channel_s
482 } adpcm_ima_wav_channel_t;
484 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
489 i_diff = i_step_table[p_channel->i_step_index] >> 3;
490 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
491 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
492 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
494 p_channel->i_predictor -= i_diff;
496 p_channel->i_predictor += i_diff;
498 CLAMP( p_channel->i_predictor, -32768, 32767 );
500 p_channel->i_step_index += i_index_table[i_nibble];
502 CLAMP( p_channel->i_step_index, 0, 88 );
504 return( p_channel->i_predictor );
507 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
510 decoder_sys_t *p_sys = p_dec->p_sys;
511 adpcm_ima_wav_channel_t channel[2];
515 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
517 GetWord( channel[0].i_predictor );
518 GetByte( channel[0].i_step_index );
519 CLAMP( channel[0].i_step_index, 0, 88 );
524 GetWord( channel[1].i_predictor );
525 GetByte( channel[1].i_step_index );
526 CLAMP( channel[1].i_step_index, 0, 88 );
532 for( i_nibbles = 2 * (p_sys->i_block - 8);
538 for( i = 0; i < 4; i++ )
541 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
542 p_sample[i * 4 + 2] =
543 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
547 for( i = 0; i < 4; i++ )
549 p_sample[i * 4 + 1] =
550 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
551 p_sample[i * 4 + 3] =
552 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
563 for( i_nibbles = 2 * (p_sys->i_block - 4);
565 i_nibbles -= 2, p_buffer++ )
567 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
568 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
576 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
579 adpcm_ima_wav_channel_t channel[2];
584 i_step = p_dec->fmt_in.audio.i_channels;
586 for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
589 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
590 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
592 CLAMP( channel[i_ch].i_step_index, 0, 88 );
595 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
597 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
600 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
607 p_sample += 1 - 64 * i_step;
614 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
617 decoder_sys_t *p_sys = p_dec->p_sys;
618 adpcm_ima_wav_channel_t channel[2];
622 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
624 GetWord( channel[0].i_predictor );
625 GetByte( channel[0].i_step_index );
626 CLAMP( channel[0].i_step_index, 0, 88 );
631 GetWord( channel[1].i_predictor );
632 GetByte( channel[1].i_step_index );
633 CLAMP( channel[1].i_step_index, 0, 88 );
637 /* first output predictor */
638 *p_sample++ = channel[0].i_predictor;
641 *p_sample++ = channel[1].i_predictor;
645 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
648 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
650 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
660 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
663 decoder_sys_t *p_sys = p_dec->p_sys;
664 uint8_t *p_end = &p_buffer[p_sys->i_block];
665 adpcm_ima_wav_channel_t sum;
666 adpcm_ima_wav_channel_t diff;
671 GetWord( sum.i_predictor );
672 GetWord( diff.i_predictor );
673 GetByte( sum.i_step_index );
674 GetByte( diff.i_step_index );
676 i_diff_value = diff.i_predictor;
677 /* we process 6 nibbles at once */
678 while( p_buffer + 1 <= p_end )
680 /* first 3 nibbles */
681 AdpcmImaWavExpandNibble( &sum,
684 AdpcmImaWavExpandNibble( &diff,
687 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
689 *p_sample++ = sum.i_predictor + i_diff_value;
690 *p_sample++ = sum.i_predictor - i_diff_value;
694 AdpcmImaWavExpandNibble( &sum,
697 *p_sample++ = sum.i_predictor + i_diff_value;
698 *p_sample++ = sum.i_predictor - i_diff_value;
700 /* now last 3 nibbles */
701 AdpcmImaWavExpandNibble( &sum,
704 if( p_buffer < p_end )
706 AdpcmImaWavExpandNibble( &diff,
709 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
711 *p_sample++ = sum.i_predictor + i_diff_value;
712 *p_sample++ = sum.i_predictor - i_diff_value;
714 AdpcmImaWavExpandNibble( &sum,
718 *p_sample++ = sum.i_predictor + i_diff_value;
719 *p_sample++ = sum.i_predictor - i_diff_value;
729 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
732 static const uint32_t EATable[]=
734 0x00000000, 0x000000F0, 0x000001CC, 0x00000188,
735 0x00000000, 0x00000000, 0xFFFFFF30, 0xFFFFFF24,
736 0x00000000, 0x00000001, 0x00000003, 0x00000004,
737 0x00000007, 0x00000008, 0x0000000A, 0x0000000B,
738 0x00000000, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFC
740 decoder_sys_t *p_sys = p_dec->p_sys;
742 unsigned i_channels, c;
744 int32_t c1[MAX_CHAN], c2[MAX_CHAN];
747 i_channels = p_dec->fmt_in.audio.i_channels;
748 p_end = &p_buffer[p_sys->i_block];
750 prev = (int16_t *)p_dec->fmt_in.p_extra;
751 cur = prev + i_channels;
753 for (c = 0; c < i_channels; c++)
758 c1[c] = EATable[input >> 4];
759 c2[c] = EATable[(input >> 4) + 4];
760 d[c] = (input & 0xf) + 8;
763 for( p_buffer += i_channels; p_buffer < p_end ; p_buffer += i_channels)
765 for (c = 0; c < i_channels; c++)
769 spl = (p_buffer[c] >> 4) & 0xf;
770 spl = (spl << 0x1c) >> d[c];
771 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
772 CLAMP( spl, -32768, 32767 );
779 for (c = 0; c < i_channels; c++)
783 spl = p_buffer[c] & 0xf;
784 spl = (spl << 0x1c) >> d[c];
785 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
786 CLAMP( spl, -32768, 32767 );