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 *****************************************************************************/
31 #include <vlc/decoder.h>
33 /*****************************************************************************
35 *****************************************************************************/
36 static int OpenDecoder( vlc_object_t * );
37 static void CloseDecoder( vlc_object_t * );
39 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
42 set_description( _("ADPCM audio decoder") );
43 set_capability( "decoder", 50 );
44 set_category( CAT_INPUT );
45 set_subcategory( SUBCAT_INPUT_ACODEC );
46 set_callbacks( OpenDecoder, CloseDecoder );
49 /*****************************************************************************
51 *****************************************************************************/
64 enum adpcm_codec_e codec;
67 int i_samplesperblock;
69 audio_date_t end_date;
72 static void DecodeAdpcmMs ( decoder_t *, int16_t *, uint8_t * );
73 static void DecodeAdpcmImaWav( decoder_t *, int16_t *, uint8_t * );
74 static void DecodeAdpcmImaQT ( decoder_t *, int16_t *, uint8_t * );
75 static void DecodeAdpcmDk4 ( decoder_t *, int16_t *, uint8_t * );
76 static void DecodeAdpcmDk3 ( decoder_t *, int16_t *, uint8_t * );
77 static void DecodeAdpcmEA ( decoder_t *, int16_t *, uint8_t * );
79 static int pi_channels_maps[6] =
83 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
84 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
85 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
86 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
87 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
90 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
91 static int i_index_table[16] =
93 -1, -1, -1, -1, 2, 4, 6, 8,
94 -1, -1, -1, -1, 2, 4, 6, 8
97 static int i_step_table[89] =
99 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
100 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
101 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
102 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
103 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
104 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
105 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
106 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
107 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
110 static int i_adaptation_table[16] =
112 230, 230, 230, 230, 307, 409, 512, 614,
113 768, 614, 512, 409, 307, 230, 230, 230
116 static int i_adaptation_coeff1[7] =
118 256, 512, 0, 192, 240, 460, 392
121 static int i_adaptation_coeff2[7] =
123 0, -256, 0, 64, 0, -208, -232
126 /*****************************************************************************
127 * OpenDecoder: probe the decoder and return score
128 *****************************************************************************/
129 static int OpenDecoder( vlc_object_t *p_this )
131 decoder_t *p_dec = (decoder_t*)p_this;
132 decoder_sys_t *p_sys;
134 switch( p_dec->fmt_in.i_codec )
136 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
137 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
138 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
139 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
140 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
141 case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
147 if( p_dec->fmt_in.audio.i_channels <= 0 ||
148 p_dec->fmt_in.audio.i_channels > 5 )
150 msg_Err( p_dec, "bad channels count(1-5)" );
154 if( p_dec->fmt_in.audio.i_rate <= 0 )
156 msg_Err( p_dec, "bad samplerate" );
160 /* Allocate the memory needed to store the decoder's structure */
161 if( ( p_dec->p_sys = p_sys =
162 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
164 msg_Err( p_dec, "out of memory" );
168 switch( p_dec->fmt_in.i_codec )
170 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
171 p_sys->codec = ADPCM_IMA_QT;
173 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
174 p_sys->codec = ADPCM_IMA_WAV;
176 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
177 p_sys->codec = ADPCM_MS;
179 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
180 p_sys->codec = ADPCM_DK4;
182 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
183 p_sys->codec = ADPCM_DK3;
185 case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
186 p_sys->codec = ADPCM_EA;
187 p_dec->fmt_in.p_extra = calloc( 2 * p_dec->fmt_in.audio.i_channels,
189 if( p_dec->fmt_in.p_extra == NULL )
197 if( p_dec->fmt_in.audio.i_blockalign <= 0 )
199 p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
200 34 * p_dec->fmt_in.audio.i_channels : 1024;
201 msg_Warn( p_dec, "block size undefined, using %d", p_sys->i_block );
205 p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
208 /* calculate samples per block */
209 switch( p_sys->codec )
212 p_sys->i_samplesperblock = 64;
215 p_sys->i_samplesperblock =
216 2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
217 p_dec->fmt_in.audio.i_channels;
220 p_sys->i_samplesperblock =
221 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
222 p_dec->fmt_in.audio.i_channels + 2;
225 p_sys->i_samplesperblock =
226 2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
227 p_dec->fmt_in.audio.i_channels + 1;
230 p_dec->fmt_in.audio.i_channels = 2;
231 p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
234 p_sys->i_samplesperblock =
235 2 * (p_sys->i_block - p_dec->fmt_in.audio.i_channels) /
236 p_dec->fmt_in.audio.i_channels;
239 msg_Dbg( p_dec, "format: samplerate:%dHz channels:%d bits/sample:%d "
240 "blockalign:%d samplesperblock:%d",
241 p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
242 p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
243 p_sys->i_samplesperblock );
245 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
246 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
247 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
248 p_dec->fmt_out.audio.i_physical_channels =
249 p_dec->fmt_out.audio.i_original_channels =
250 pi_channels_maps[p_dec->fmt_in.audio.i_channels];
252 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
253 aout_DateSet( &p_sys->end_date, 0 );
255 p_dec->pf_decode_audio = DecodeBlock;
260 /*****************************************************************************
262 *****************************************************************************/
263 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
265 decoder_sys_t *p_sys = p_dec->p_sys;
268 if( !pp_block || !*pp_block ) return NULL;
272 if( p_block->i_pts != 0 &&
273 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
275 aout_DateSet( &p_sys->end_date, p_block->i_pts );
277 else if( !aout_DateGet( &p_sys->end_date ) )
279 /* We've just started the stream, wait for the first PTS. */
280 block_Release( p_block );
284 /* Don't re-use the same pts twice */
287 if( p_block->i_buffer >= p_sys->i_block )
289 aout_buffer_t *p_out;
291 p_out = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_samplesperblock );
294 block_Release( p_block );
298 p_out->start_date = aout_DateGet( &p_sys->end_date );
300 aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
302 switch( p_sys->codec )
305 DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
309 DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
313 DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
317 DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
321 DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
325 DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
331 p_block->p_buffer += p_sys->i_block;
332 p_block->i_buffer -= p_sys->i_block;
336 block_Release( p_block );
340 /*****************************************************************************
342 *****************************************************************************/
343 static void CloseDecoder( vlc_object_t *p_this )
345 decoder_t *p_dec = (decoder_t *)p_this;
346 decoder_sys_t *p_sys = p_dec->p_sys;
348 if( p_sys->codec == ADPCM_EA )
349 free( p_dec->fmt_in.p_extra );
353 /*****************************************************************************
355 *****************************************************************************/
356 #define CLAMP( v, min, max ) \
357 if( (v) < (min) ) (v) = (min); \
358 if( (v) > (max) ) (v) = (max)
360 #define GetByte( v ) \
361 (v) = *p_buffer; p_buffer++;
363 #define GetWord( v ) \
364 (v) = *p_buffer; p_buffer++; \
365 (v) |= ( *p_buffer ) << 8; p_buffer++; \
366 if( (v)&0x8000 ) (v) -= 0x010000;
371 typedef struct adpcm_ms_channel_s
374 int i_sample1, i_sample2;
375 int i_coeff1, i_coeff2;
377 } adpcm_ms_channel_t;
380 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
387 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
389 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
390 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
391 i_snibble * p_channel->i_idelta;
393 CLAMP( i_predictor, -32768, 32767 );
395 p_channel->i_sample2 = p_channel->i_sample1;
396 p_channel->i_sample1 = i_predictor;
398 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
399 p_channel->i_idelta ) / 256;
400 if( p_channel->i_idelta < 16 )
402 p_channel->i_idelta = 16;
404 return( i_predictor );
407 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
410 decoder_sys_t *p_sys = p_dec->p_sys;
411 adpcm_ms_channel_t channel[2];
414 int i_block_predictor;
416 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
418 GetByte( i_block_predictor );
419 CLAMP( i_block_predictor, 0, 6 );
420 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
421 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
425 GetByte( i_block_predictor );
426 CLAMP( i_block_predictor, 0, 6 );
427 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
428 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
430 GetWord( channel[0].i_idelta );
433 GetWord( channel[1].i_idelta );
436 GetWord( channel[0].i_sample1 );
439 GetWord( channel[1].i_sample1 );
442 GetWord( channel[0].i_sample2 );
445 GetWord( channel[1].i_sample2 );
450 *p_sample++ = channel[0].i_sample2;
451 *p_sample++ = channel[1].i_sample2;
452 *p_sample++ = channel[0].i_sample1;
453 *p_sample++ = channel[1].i_sample1;
457 *p_sample++ = channel[0].i_sample2;
458 *p_sample++ = channel[0].i_sample1;
461 for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
462 i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
464 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
465 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
473 typedef struct adpcm_ima_wav_channel_s
478 } adpcm_ima_wav_channel_t;
480 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
485 i_diff = i_step_table[p_channel->i_step_index] >> 3;
486 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
487 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
488 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
490 p_channel->i_predictor -= i_diff;
492 p_channel->i_predictor += i_diff;
494 CLAMP( p_channel->i_predictor, -32768, 32767 );
496 p_channel->i_step_index += i_index_table[i_nibble];
498 CLAMP( p_channel->i_step_index, 0, 88 );
500 return( p_channel->i_predictor );
503 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
506 decoder_sys_t *p_sys = p_dec->p_sys;
507 adpcm_ima_wav_channel_t channel[2];
511 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
513 GetWord( channel[0].i_predictor );
514 GetByte( channel[0].i_step_index );
515 CLAMP( channel[0].i_step_index, 0, 88 );
520 GetWord( channel[1].i_predictor );
521 GetByte( channel[1].i_step_index );
522 CLAMP( channel[1].i_step_index, 0, 88 );
528 for( i_nibbles = 2 * (p_sys->i_block - 8);
534 for( i = 0; i < 4; i++ )
537 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
538 p_sample[i * 4 + 2] =
539 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
543 for( i = 0; i < 4; i++ )
545 p_sample[i * 4 + 1] =
546 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
547 p_sample[i * 4 + 3] =
548 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
559 for( i_nibbles = 2 * (p_sys->i_block - 4);
561 i_nibbles -= 2, p_buffer++ )
563 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
564 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
572 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
575 adpcm_ima_wav_channel_t channel[2];
580 i_step = p_dec->fmt_in.audio.i_channels;
582 for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
585 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
586 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
588 CLAMP( channel[i_ch].i_step_index, 0, 88 );
591 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
593 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
596 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
603 p_sample += 1 - 64 * i_step;
610 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
613 decoder_sys_t *p_sys = p_dec->p_sys;
614 adpcm_ima_wav_channel_t channel[2];
618 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
620 GetWord( channel[0].i_predictor );
621 GetByte( channel[0].i_step_index );
622 CLAMP( channel[0].i_step_index, 0, 88 );
627 GetWord( channel[1].i_predictor );
628 GetByte( channel[1].i_step_index );
629 CLAMP( channel[1].i_step_index, 0, 88 );
633 /* first output predictor */
634 *p_sample++ = channel[0].i_predictor;
637 *p_sample++ = channel[1].i_predictor;
641 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
644 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
646 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
656 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
659 decoder_sys_t *p_sys = p_dec->p_sys;
660 uint8_t *p_end = &p_buffer[p_sys->i_block];
661 adpcm_ima_wav_channel_t sum;
662 adpcm_ima_wav_channel_t diff;
667 GetWord( sum.i_predictor );
668 GetWord( diff.i_predictor );
669 GetByte( sum.i_step_index );
670 GetByte( diff.i_step_index );
672 i_diff_value = diff.i_predictor;
673 /* we process 6 nibbles at once */
674 while( p_buffer + 1 <= p_end )
676 /* first 3 nibbles */
677 AdpcmImaWavExpandNibble( &sum,
680 AdpcmImaWavExpandNibble( &diff,
683 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
685 *p_sample++ = sum.i_predictor + i_diff_value;
686 *p_sample++ = sum.i_predictor - i_diff_value;
690 AdpcmImaWavExpandNibble( &sum,
693 *p_sample++ = sum.i_predictor + i_diff_value;
694 *p_sample++ = sum.i_predictor - i_diff_value;
696 /* now last 3 nibbles */
697 AdpcmImaWavExpandNibble( &sum,
700 if( p_buffer < p_end )
702 AdpcmImaWavExpandNibble( &diff,
705 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
707 *p_sample++ = sum.i_predictor + i_diff_value;
708 *p_sample++ = sum.i_predictor - i_diff_value;
710 AdpcmImaWavExpandNibble( &sum,
714 *p_sample++ = sum.i_predictor + i_diff_value;
715 *p_sample++ = sum.i_predictor - i_diff_value;
725 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
728 static const uint32_t EATable[]=
730 0x00000000, 0x000000F0, 0x000001CC, 0x00000188,
731 0x00000000, 0x00000000, 0xFFFFFF30, 0xFFFFFF24,
732 0x00000000, 0x00000001, 0x00000003, 0x00000004,
733 0x00000007, 0x00000008, 0x0000000A, 0x0000000B,
734 0x00000000, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFC
736 decoder_sys_t *p_sys = p_dec->p_sys;
738 unsigned i_channels, c;
740 int32_t c1[MAX_CHAN], c2[MAX_CHAN];
743 i_channels = p_dec->fmt_in.audio.i_channels;
744 p_end = &p_buffer[p_sys->i_block];
746 prev = (int16_t *)p_dec->fmt_in.p_extra;
747 cur = prev + i_channels;
749 for (c = 0; c < i_channels; c++)
754 c1[c] = EATable[input >> 4];
755 c2[c] = EATable[(input >> 4) + 4];
756 d[c] = (input & 0xf) + 8;
759 for( p_buffer += i_channels; p_buffer < p_end ; p_buffer += i_channels)
761 for (c = 0; c < i_channels; c++)
765 spl = (p_buffer[c] >> 4) & 0xf;
766 spl = (spl << 0x1c) >> d[c];
767 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
768 CLAMP( spl, -32768, 32767 );
775 for (c = 0; c < i_channels; c++)
779 spl = p_buffer[c] & 0xf;
780 spl = (spl << 0x1c) >> d[c];
781 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
782 CLAMP( spl, -32768, 32767 );