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 *****************************************************************************/
36 #include <vlc_codec.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int OpenDecoder( vlc_object_t * );
42 static void CloseDecoder( vlc_object_t * );
44 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
47 set_description( _("ADPCM audio decoder") );
48 set_capability( "decoder", 50 );
49 set_category( CAT_INPUT );
50 set_subcategory( SUBCAT_INPUT_ACODEC );
51 set_callbacks( OpenDecoder, CloseDecoder );
54 /*****************************************************************************
56 *****************************************************************************/
69 enum adpcm_codec_e codec;
72 size_t i_samplesperblock;
74 audio_date_t end_date;
77 static void DecodeAdpcmMs ( decoder_t *, int16_t *, uint8_t * );
78 static void DecodeAdpcmImaWav( decoder_t *, int16_t *, uint8_t * );
79 static void DecodeAdpcmImaQT ( decoder_t *, int16_t *, uint8_t * );
80 static void DecodeAdpcmDk4 ( decoder_t *, int16_t *, uint8_t * );
81 static void DecodeAdpcmDk3 ( decoder_t *, int16_t *, uint8_t * );
82 static void DecodeAdpcmEA ( decoder_t *, int16_t *, uint8_t * );
84 static const int pi_channels_maps[6] =
88 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
89 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
90 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
91 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
92 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
95 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
96 static const int i_index_table[16] =
98 -1, -1, -1, -1, 2, 4, 6, 8,
99 -1, -1, -1, -1, 2, 4, 6, 8
102 static const int i_step_table[89] =
104 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
105 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
106 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
107 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
108 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
109 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
110 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
111 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
112 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
115 static const int i_adaptation_table[16] =
117 230, 230, 230, 230, 307, 409, 512, 614,
118 768, 614, 512, 409, 307, 230, 230, 230
121 static const int i_adaptation_coeff1[7] =
123 256, 512, 0, 192, 240, 460, 392
126 static const int i_adaptation_coeff2[7] =
128 0, -256, 0, 64, 0, -208, -232
131 /*****************************************************************************
132 * OpenDecoder: probe the decoder and return score
133 *****************************************************************************/
134 static int OpenDecoder( vlc_object_t *p_this )
136 decoder_t *p_dec = (decoder_t*)p_this;
137 decoder_sys_t *p_sys;
139 switch( p_dec->fmt_in.i_codec )
141 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
142 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
143 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
144 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
145 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
146 case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
152 if( p_dec->fmt_in.audio.i_channels <= 0 ||
153 p_dec->fmt_in.audio.i_channels > 5 )
155 msg_Err( p_dec, "invalid number of channel (not between 1 and 5): %i",
156 p_dec->fmt_in.audio.i_channels );
160 if( p_dec->fmt_in.audio.i_rate <= 0 )
162 msg_Err( p_dec, "bad samplerate" );
166 /* Allocate the memory needed to store the decoder's structure */
167 if( ( p_dec->p_sys = p_sys =
168 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
170 msg_Err( p_dec, "out of memory" );
174 switch( p_dec->fmt_in.i_codec )
176 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
177 p_sys->codec = ADPCM_IMA_QT;
179 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
180 p_sys->codec = ADPCM_IMA_WAV;
182 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
183 p_sys->codec = ADPCM_MS;
185 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
186 p_sys->codec = ADPCM_DK4;
188 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
189 p_sys->codec = ADPCM_DK3;
191 case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
192 p_sys->codec = ADPCM_EA;
193 p_dec->fmt_in.p_extra = calloc( 2 * p_dec->fmt_in.audio.i_channels,
195 if( p_dec->fmt_in.p_extra == NULL )
203 if( p_dec->fmt_in.audio.i_blockalign <= 0 )
205 p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
206 34 * p_dec->fmt_in.audio.i_channels : 1024;
207 msg_Warn( p_dec, "block size undefined, using %d", p_sys->i_block );
211 p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
214 /* calculate samples per block */
215 switch( p_sys->codec )
218 p_sys->i_samplesperblock = 64;
221 p_sys->i_samplesperblock =
222 2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
223 p_dec->fmt_in.audio.i_channels;
226 p_sys->i_samplesperblock =
227 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
228 p_dec->fmt_in.audio.i_channels + 2;
231 p_sys->i_samplesperblock =
232 2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
233 p_dec->fmt_in.audio.i_channels + 1;
236 p_dec->fmt_in.audio.i_channels = 2;
237 p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
240 p_sys->i_samplesperblock =
241 2 * (p_sys->i_block - p_dec->fmt_in.audio.i_channels) /
242 p_dec->fmt_in.audio.i_channels;
245 msg_Dbg( p_dec, "format: samplerate:%d Hz channels:%d bits/sample:%d "
246 "blockalign:%d samplesperblock:%d",
247 p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
248 p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
249 p_sys->i_samplesperblock );
251 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
252 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
253 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
254 p_dec->fmt_out.audio.i_physical_channels =
255 p_dec->fmt_out.audio.i_original_channels =
256 pi_channels_maps[p_dec->fmt_in.audio.i_channels];
258 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
259 aout_DateSet( &p_sys->end_date, 0 );
261 p_dec->pf_decode_audio = DecodeBlock;
266 /*****************************************************************************
268 *****************************************************************************/
269 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
271 decoder_sys_t *p_sys = p_dec->p_sys;
274 if( !pp_block || !*pp_block ) return NULL;
278 if( p_block->i_pts != 0 &&
279 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
281 aout_DateSet( &p_sys->end_date, p_block->i_pts );
283 else if( !aout_DateGet( &p_sys->end_date ) )
285 /* We've just started the stream, wait for the first PTS. */
286 block_Release( p_block );
290 /* Don't re-use the same pts twice */
293 if( p_block->i_buffer >= p_sys->i_block )
295 aout_buffer_t *p_out;
297 p_out = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_samplesperblock );
300 block_Release( p_block );
304 p_out->start_date = aout_DateGet( &p_sys->end_date );
306 aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
308 switch( p_sys->codec )
311 DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
315 DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
319 DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
323 DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
327 DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
331 DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
337 p_block->p_buffer += p_sys->i_block;
338 p_block->i_buffer -= p_sys->i_block;
342 block_Release( p_block );
346 /*****************************************************************************
348 *****************************************************************************/
349 static void CloseDecoder( vlc_object_t *p_this )
351 decoder_t *p_dec = (decoder_t *)p_this;
352 decoder_sys_t *p_sys = p_dec->p_sys;
354 if( p_sys->codec == ADPCM_EA )
355 free( p_dec->fmt_in.p_extra );
359 /*****************************************************************************
361 *****************************************************************************/
362 #define CLAMP( v, min, max ) \
363 if( (v) < (min) ) (v) = (min); \
364 if( (v) > (max) ) (v) = (max)
366 #define GetByte( v ) \
367 (v) = *p_buffer; p_buffer++;
369 #define GetWord( v ) \
370 (v) = *p_buffer; p_buffer++; \
371 (v) |= ( *p_buffer ) << 8; p_buffer++; \
372 if( (v)&0x8000 ) (v) -= 0x010000;
377 typedef struct adpcm_ms_channel_s
380 int i_sample1, i_sample2;
381 int i_coeff1, i_coeff2;
383 } adpcm_ms_channel_t;
386 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
393 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
395 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
396 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
397 i_snibble * p_channel->i_idelta;
399 CLAMP( i_predictor, -32768, 32767 );
401 p_channel->i_sample2 = p_channel->i_sample1;
402 p_channel->i_sample1 = i_predictor;
404 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
405 p_channel->i_idelta ) / 256;
406 if( p_channel->i_idelta < 16 )
408 p_channel->i_idelta = 16;
410 return( i_predictor );
413 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
416 decoder_sys_t *p_sys = p_dec->p_sys;
417 adpcm_ms_channel_t channel[2];
420 int i_block_predictor;
422 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
424 GetByte( i_block_predictor );
425 CLAMP( i_block_predictor, 0, 6 );
426 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
427 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
431 GetByte( i_block_predictor );
432 CLAMP( i_block_predictor, 0, 6 );
433 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
434 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
436 GetWord( channel[0].i_idelta );
439 GetWord( channel[1].i_idelta );
442 GetWord( channel[0].i_sample1 );
445 GetWord( channel[1].i_sample1 );
448 GetWord( channel[0].i_sample2 );
451 GetWord( channel[1].i_sample2 );
456 *p_sample++ = channel[0].i_sample2;
457 *p_sample++ = channel[1].i_sample2;
458 *p_sample++ = channel[0].i_sample1;
459 *p_sample++ = channel[1].i_sample1;
463 *p_sample++ = channel[0].i_sample2;
464 *p_sample++ = channel[0].i_sample1;
467 for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
468 i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
470 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
471 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
479 typedef struct adpcm_ima_wav_channel_s
484 } adpcm_ima_wav_channel_t;
486 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
491 i_diff = i_step_table[p_channel->i_step_index] >> 3;
492 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
493 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
494 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
496 p_channel->i_predictor -= i_diff;
498 p_channel->i_predictor += i_diff;
500 CLAMP( p_channel->i_predictor, -32768, 32767 );
502 p_channel->i_step_index += i_index_table[i_nibble];
504 CLAMP( p_channel->i_step_index, 0, 88 );
506 return( p_channel->i_predictor );
509 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
512 decoder_sys_t *p_sys = p_dec->p_sys;
513 adpcm_ima_wav_channel_t channel[2];
517 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
519 GetWord( channel[0].i_predictor );
520 GetByte( channel[0].i_step_index );
521 CLAMP( channel[0].i_step_index, 0, 88 );
526 GetWord( channel[1].i_predictor );
527 GetByte( channel[1].i_step_index );
528 CLAMP( channel[1].i_step_index, 0, 88 );
534 for( i_nibbles = 2 * (p_sys->i_block - 8);
540 for( i = 0; i < 4; i++ )
543 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
544 p_sample[i * 4 + 2] =
545 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
549 for( i = 0; i < 4; i++ )
551 p_sample[i * 4 + 1] =
552 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
553 p_sample[i * 4 + 3] =
554 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
565 for( i_nibbles = 2 * (p_sys->i_block - 4);
567 i_nibbles -= 2, p_buffer++ )
569 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
570 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
578 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
581 adpcm_ima_wav_channel_t channel[2];
586 i_step = p_dec->fmt_in.audio.i_channels;
588 for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
591 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
592 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
594 CLAMP( channel[i_ch].i_step_index, 0, 88 );
597 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
599 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
602 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
609 p_sample += 1 - 64 * i_step;
616 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
619 decoder_sys_t *p_sys = p_dec->p_sys;
620 adpcm_ima_wav_channel_t channel[2];
624 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
626 GetWord( channel[0].i_predictor );
627 GetByte( channel[0].i_step_index );
628 CLAMP( channel[0].i_step_index, 0, 88 );
633 GetWord( channel[1].i_predictor );
634 GetByte( channel[1].i_step_index );
635 CLAMP( channel[1].i_step_index, 0, 88 );
639 /* first output predictor */
640 *p_sample++ = channel[0].i_predictor;
643 *p_sample++ = channel[1].i_predictor;
647 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
650 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
652 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
662 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
665 decoder_sys_t *p_sys = p_dec->p_sys;
666 uint8_t *p_end = &p_buffer[p_sys->i_block];
667 adpcm_ima_wav_channel_t sum;
668 adpcm_ima_wav_channel_t diff;
673 GetWord( sum.i_predictor );
674 GetWord( diff.i_predictor );
675 GetByte( sum.i_step_index );
676 GetByte( diff.i_step_index );
678 i_diff_value = diff.i_predictor;
679 /* we process 6 nibbles at once */
680 while( p_buffer + 1 <= p_end )
682 /* first 3 nibbles */
683 AdpcmImaWavExpandNibble( &sum,
686 AdpcmImaWavExpandNibble( &diff,
689 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
691 *p_sample++ = sum.i_predictor + i_diff_value;
692 *p_sample++ = sum.i_predictor - i_diff_value;
696 AdpcmImaWavExpandNibble( &sum,
699 *p_sample++ = sum.i_predictor + i_diff_value;
700 *p_sample++ = sum.i_predictor - i_diff_value;
702 /* now last 3 nibbles */
703 AdpcmImaWavExpandNibble( &sum,
706 if( p_buffer < p_end )
708 AdpcmImaWavExpandNibble( &diff,
711 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
713 *p_sample++ = sum.i_predictor + i_diff_value;
714 *p_sample++ = sum.i_predictor - i_diff_value;
716 AdpcmImaWavExpandNibble( &sum,
720 *p_sample++ = sum.i_predictor + i_diff_value;
721 *p_sample++ = sum.i_predictor - i_diff_value;
731 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
734 static const uint32_t EATable[]=
736 0x00000000, 0x000000F0, 0x000001CC, 0x00000188,
737 0x00000000, 0x00000000, 0xFFFFFF30, 0xFFFFFF24,
738 0x00000000, 0x00000001, 0x00000003, 0x00000004,
739 0x00000007, 0x00000008, 0x0000000A, 0x0000000B,
740 0x00000000, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFC
742 decoder_sys_t *p_sys = p_dec->p_sys;
744 unsigned i_channels, c;
746 int32_t c1[MAX_CHAN], c2[MAX_CHAN];
749 i_channels = p_dec->fmt_in.audio.i_channels;
750 p_end = &p_buffer[p_sys->i_block];
752 prev = (int16_t *)p_dec->fmt_in.p_extra;
753 cur = prev + i_channels;
755 for (c = 0; c < i_channels; c++)
760 c1[c] = EATable[input >> 4];
761 c2[c] = EATable[(input >> 4) + 4];
762 d[c] = (input & 0xf) + 8;
765 for( p_buffer += i_channels; p_buffer < p_end ; p_buffer += i_channels)
767 for (c = 0; c < i_channels; c++)
771 spl = (p_buffer[c] >> 4) & 0xf;
772 spl = (spl << 0x1c) >> d[c];
773 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
774 CLAMP( spl, -32768, 32767 );
781 for (c = 0; c < i_channels; c++)
785 spl = p_buffer[c] & 0xf;
786 spl = (spl << 0x1c) >> d[c];
787 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
788 CLAMP( spl, -32768, 32767 );