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, "invalid number of channel (not between 1 and 5): %i",
151 p_dec->fmt_in.audio.i_channels );
155 if( p_dec->fmt_in.audio.i_rate <= 0 )
157 msg_Err( p_dec, "bad samplerate" );
161 /* Allocate the memory needed to store the decoder's structure */
162 if( ( p_dec->p_sys = p_sys =
163 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
165 msg_Err( p_dec, "out of memory" );
169 switch( p_dec->fmt_in.i_codec )
171 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
172 p_sys->codec = ADPCM_IMA_QT;
174 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
175 p_sys->codec = ADPCM_IMA_WAV;
177 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
178 p_sys->codec = ADPCM_MS;
180 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
181 p_sys->codec = ADPCM_DK4;
183 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
184 p_sys->codec = ADPCM_DK3;
186 case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
187 p_sys->codec = ADPCM_EA;
188 p_dec->fmt_in.p_extra = calloc( 2 * p_dec->fmt_in.audio.i_channels,
190 if( p_dec->fmt_in.p_extra == NULL )
198 if( p_dec->fmt_in.audio.i_blockalign <= 0 )
200 p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
201 34 * p_dec->fmt_in.audio.i_channels : 1024;
202 msg_Warn( p_dec, "block size undefined, using %d", p_sys->i_block );
206 p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
209 /* calculate samples per block */
210 switch( p_sys->codec )
213 p_sys->i_samplesperblock = 64;
216 p_sys->i_samplesperblock =
217 2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
218 p_dec->fmt_in.audio.i_channels;
221 p_sys->i_samplesperblock =
222 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
223 p_dec->fmt_in.audio.i_channels + 2;
226 p_sys->i_samplesperblock =
227 2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
228 p_dec->fmt_in.audio.i_channels + 1;
231 p_dec->fmt_in.audio.i_channels = 2;
232 p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
235 p_sys->i_samplesperblock =
236 2 * (p_sys->i_block - p_dec->fmt_in.audio.i_channels) /
237 p_dec->fmt_in.audio.i_channels;
240 msg_Dbg( p_dec, "format: samplerate:%d Hz channels:%d bits/sample:%d "
241 "blockalign:%d samplesperblock:%d",
242 p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
243 p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
244 p_sys->i_samplesperblock );
246 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
247 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
248 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
249 p_dec->fmt_out.audio.i_physical_channels =
250 p_dec->fmt_out.audio.i_original_channels =
251 pi_channels_maps[p_dec->fmt_in.audio.i_channels];
253 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
254 aout_DateSet( &p_sys->end_date, 0 );
256 p_dec->pf_decode_audio = DecodeBlock;
261 /*****************************************************************************
263 *****************************************************************************/
264 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
266 decoder_sys_t *p_sys = p_dec->p_sys;
269 if( !pp_block || !*pp_block ) return NULL;
273 if( p_block->i_pts != 0 &&
274 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
276 aout_DateSet( &p_sys->end_date, p_block->i_pts );
278 else if( !aout_DateGet( &p_sys->end_date ) )
280 /* We've just started the stream, wait for the first PTS. */
281 block_Release( p_block );
285 /* Don't re-use the same pts twice */
288 if( p_block->i_buffer >= p_sys->i_block )
290 aout_buffer_t *p_out;
292 p_out = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_samplesperblock );
295 block_Release( p_block );
299 p_out->start_date = aout_DateGet( &p_sys->end_date );
301 aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
303 switch( p_sys->codec )
306 DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
310 DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
314 DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
318 DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
322 DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
326 DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
332 p_block->p_buffer += p_sys->i_block;
333 p_block->i_buffer -= p_sys->i_block;
337 block_Release( p_block );
341 /*****************************************************************************
343 *****************************************************************************/
344 static void CloseDecoder( vlc_object_t *p_this )
346 decoder_t *p_dec = (decoder_t *)p_this;
347 decoder_sys_t *p_sys = p_dec->p_sys;
349 if( p_sys->codec == ADPCM_EA )
350 free( p_dec->fmt_in.p_extra );
354 /*****************************************************************************
356 *****************************************************************************/
357 #define CLAMP( v, min, max ) \
358 if( (v) < (min) ) (v) = (min); \
359 if( (v) > (max) ) (v) = (max)
361 #define GetByte( v ) \
362 (v) = *p_buffer; p_buffer++;
364 #define GetWord( v ) \
365 (v) = *p_buffer; p_buffer++; \
366 (v) |= ( *p_buffer ) << 8; p_buffer++; \
367 if( (v)&0x8000 ) (v) -= 0x010000;
372 typedef struct adpcm_ms_channel_s
375 int i_sample1, i_sample2;
376 int i_coeff1, i_coeff2;
378 } adpcm_ms_channel_t;
381 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
388 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
390 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
391 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
392 i_snibble * p_channel->i_idelta;
394 CLAMP( i_predictor, -32768, 32767 );
396 p_channel->i_sample2 = p_channel->i_sample1;
397 p_channel->i_sample1 = i_predictor;
399 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
400 p_channel->i_idelta ) / 256;
401 if( p_channel->i_idelta < 16 )
403 p_channel->i_idelta = 16;
405 return( i_predictor );
408 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
411 decoder_sys_t *p_sys = p_dec->p_sys;
412 adpcm_ms_channel_t channel[2];
415 int i_block_predictor;
417 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
419 GetByte( i_block_predictor );
420 CLAMP( i_block_predictor, 0, 6 );
421 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
422 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
426 GetByte( i_block_predictor );
427 CLAMP( i_block_predictor, 0, 6 );
428 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
429 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
431 GetWord( channel[0].i_idelta );
434 GetWord( channel[1].i_idelta );
437 GetWord( channel[0].i_sample1 );
440 GetWord( channel[1].i_sample1 );
443 GetWord( channel[0].i_sample2 );
446 GetWord( channel[1].i_sample2 );
451 *p_sample++ = channel[0].i_sample2;
452 *p_sample++ = channel[1].i_sample2;
453 *p_sample++ = channel[0].i_sample1;
454 *p_sample++ = channel[1].i_sample1;
458 *p_sample++ = channel[0].i_sample2;
459 *p_sample++ = channel[0].i_sample1;
462 for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
463 i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
465 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
466 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
474 typedef struct adpcm_ima_wav_channel_s
479 } adpcm_ima_wav_channel_t;
481 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
486 i_diff = i_step_table[p_channel->i_step_index] >> 3;
487 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
488 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
489 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
491 p_channel->i_predictor -= i_diff;
493 p_channel->i_predictor += i_diff;
495 CLAMP( p_channel->i_predictor, -32768, 32767 );
497 p_channel->i_step_index += i_index_table[i_nibble];
499 CLAMP( p_channel->i_step_index, 0, 88 );
501 return( p_channel->i_predictor );
504 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
507 decoder_sys_t *p_sys = p_dec->p_sys;
508 adpcm_ima_wav_channel_t channel[2];
512 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
514 GetWord( channel[0].i_predictor );
515 GetByte( channel[0].i_step_index );
516 CLAMP( channel[0].i_step_index, 0, 88 );
521 GetWord( channel[1].i_predictor );
522 GetByte( channel[1].i_step_index );
523 CLAMP( channel[1].i_step_index, 0, 88 );
529 for( i_nibbles = 2 * (p_sys->i_block - 8);
535 for( i = 0; i < 4; i++ )
538 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
539 p_sample[i * 4 + 2] =
540 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
544 for( i = 0; i < 4; i++ )
546 p_sample[i * 4 + 1] =
547 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
548 p_sample[i * 4 + 3] =
549 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
560 for( i_nibbles = 2 * (p_sys->i_block - 4);
562 i_nibbles -= 2, p_buffer++ )
564 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
565 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
573 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
576 adpcm_ima_wav_channel_t channel[2];
581 i_step = p_dec->fmt_in.audio.i_channels;
583 for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
586 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
587 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
589 CLAMP( channel[i_ch].i_step_index, 0, 88 );
592 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
594 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
597 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
604 p_sample += 1 - 64 * i_step;
611 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
614 decoder_sys_t *p_sys = p_dec->p_sys;
615 adpcm_ima_wav_channel_t channel[2];
619 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
621 GetWord( channel[0].i_predictor );
622 GetByte( channel[0].i_step_index );
623 CLAMP( channel[0].i_step_index, 0, 88 );
628 GetWord( channel[1].i_predictor );
629 GetByte( channel[1].i_step_index );
630 CLAMP( channel[1].i_step_index, 0, 88 );
634 /* first output predictor */
635 *p_sample++ = channel[0].i_predictor;
638 *p_sample++ = channel[1].i_predictor;
642 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
645 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
647 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
657 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
660 decoder_sys_t *p_sys = p_dec->p_sys;
661 uint8_t *p_end = &p_buffer[p_sys->i_block];
662 adpcm_ima_wav_channel_t sum;
663 adpcm_ima_wav_channel_t diff;
668 GetWord( sum.i_predictor );
669 GetWord( diff.i_predictor );
670 GetByte( sum.i_step_index );
671 GetByte( diff.i_step_index );
673 i_diff_value = diff.i_predictor;
674 /* we process 6 nibbles at once */
675 while( p_buffer + 1 <= p_end )
677 /* first 3 nibbles */
678 AdpcmImaWavExpandNibble( &sum,
681 AdpcmImaWavExpandNibble( &diff,
684 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
686 *p_sample++ = sum.i_predictor + i_diff_value;
687 *p_sample++ = sum.i_predictor - i_diff_value;
691 AdpcmImaWavExpandNibble( &sum,
694 *p_sample++ = sum.i_predictor + i_diff_value;
695 *p_sample++ = sum.i_predictor - i_diff_value;
697 /* now last 3 nibbles */
698 AdpcmImaWavExpandNibble( &sum,
701 if( p_buffer < p_end )
703 AdpcmImaWavExpandNibble( &diff,
706 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
708 *p_sample++ = sum.i_predictor + i_diff_value;
709 *p_sample++ = sum.i_predictor - i_diff_value;
711 AdpcmImaWavExpandNibble( &sum,
715 *p_sample++ = sum.i_predictor + i_diff_value;
716 *p_sample++ = sum.i_predictor - i_diff_value;
726 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
729 static const uint32_t EATable[]=
731 0x00000000, 0x000000F0, 0x000001CC, 0x00000188,
732 0x00000000, 0x00000000, 0xFFFFFF30, 0xFFFFFF24,
733 0x00000000, 0x00000001, 0x00000003, 0x00000004,
734 0x00000007, 0x00000008, 0x0000000A, 0x0000000B,
735 0x00000000, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFC
737 decoder_sys_t *p_sys = p_dec->p_sys;
739 unsigned i_channels, c;
741 int32_t c1[MAX_CHAN], c2[MAX_CHAN];
744 i_channels = p_dec->fmt_in.audio.i_channels;
745 p_end = &p_buffer[p_sys->i_block];
747 prev = (int16_t *)p_dec->fmt_in.p_extra;
748 cur = prev + i_channels;
750 for (c = 0; c < i_channels; c++)
755 c1[c] = EATable[input >> 4];
756 c2[c] = EATable[(input >> 4) + 4];
757 d[c] = (input & 0xf) + 8;
760 for( p_buffer += i_channels; p_buffer < p_end ; p_buffer += i_channels)
762 for (c = 0; c < i_channels; c++)
766 spl = (p_buffer[c] >> 4) & 0xf;
767 spl = (spl << 0x1c) >> d[c];
768 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
769 CLAMP( spl, -32768, 32767 );
776 for (c = 0; c < i_channels; c++)
780 spl = p_buffer[c] & 0xf;
781 spl = (spl << 0x1c) >> d[c];
782 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
783 CLAMP( spl, -32768, 32767 );