1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: adpcm.c,v 1.15 2003/11/05 01:47:40 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
27 * Documentation: http://www.pcisys.net/~melanson/codecs/adpcm.txt
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
33 #include <vlc/decoder.h>
34 #include <vlc/input.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
44 set_description( _("ADPCM audio decoder") );
45 set_capability( "decoder", 50 );
46 set_callbacks( Open, NULL );
50 /*****************************************************************************
52 *****************************************************************************/
65 enum adpcm_codec_e codec;
68 int i_samplesperblock;
71 aout_instance_t * p_aout; /* opaque */
72 aout_input_t * p_aout_input; /* opaque */
73 audio_sample_format_t output_format;
78 static int Init ( decoder_t * );
79 static int Decode( decoder_t *, block_t * );
80 static int End ( decoder_t * );
84 static void DecodeAdpcmMs ( decoder_sys_t *, int16_t *, uint8_t * );
85 static void DecodeAdpcmImaWav ( decoder_sys_t *, int16_t *, uint8_t * );
86 static void DecodeAdpcmImaQT ( decoder_sys_t *, int16_t *, uint8_t * );
87 static void DecodeAdpcmDk4 ( decoder_sys_t *, int16_t *, uint8_t * );
88 static void DecodeAdpcmDk3 ( decoder_sys_t *, int16_t *, uint8_t * );
90 static int pi_channels_maps[6] =
94 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
95 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
96 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
97 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
98 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
101 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
102 static int i_index_table[16] =
104 -1, -1, -1, -1, 2, 4, 6, 8,
105 -1, -1, -1, -1, 2, 4, 6, 8
108 static int i_step_table[89] =
110 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
111 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
112 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
113 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
114 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
115 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
116 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
117 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
118 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
121 static int i_adaptation_table[16] =
123 230, 230, 230, 230, 307, 409, 512, 614,
124 768, 614, 512, 409, 307, 230, 230, 230
127 static int i_adaptation_coeff1[7] =
129 256, 512, 0, 192, 240, 460, 392
132 static int i_adaptation_coeff2[7] =
134 0, -256, 0, 64, 0, -208, -232
138 /*****************************************************************************
139 * OpenDecoder: probe the decoder and return score
140 *****************************************************************************
141 * Tries to launch a decoder and return score so that the interface is able
143 *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
146 decoder_t *p_dec = (decoder_t*)p_this;
148 switch( p_dec->p_fifo->i_fourcc )
150 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
151 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
152 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
153 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
154 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
156 p_dec->pf_init = Init;
157 p_dec->pf_decode = Decode;
160 p_dec->p_sys = malloc( sizeof( decoder_sys_t ) );
168 /*****************************************************************************
170 *****************************************************************************/
171 static int Init ( decoder_t *p_dec )
173 decoder_sys_t *p_sys = p_dec->p_sys;
176 if( ( p_wf = (WAVEFORMATEX*)p_dec->p_fifo->p_waveformatex ) == NULL )
178 msg_Err( p_dec, "unknown raw format" );
182 if( p_wf->nChannels < 1 || p_wf->nChannels > 2 )
184 msg_Err( p_dec, "bad channels count(1-2)" );
187 if( p_wf->nSamplesPerSec <= 0 )
189 msg_Err( p_dec, "bad samplerate" );
194 switch( p_dec->p_fifo->i_fourcc )
196 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
197 p_sys->codec = ADPCM_IMA_QT;
199 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
200 p_sys->codec = ADPCM_IMA_WAV;
202 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
203 p_sys->codec = ADPCM_MS;
205 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
206 p_sys->codec = ADPCM_DK4;
208 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
209 p_sys->codec = ADPCM_DK3;
213 if( ( p_sys->i_block = p_wf->nBlockAlign ) <= 0 )
215 p_sys->i_block = p_sys->codec==ADPCM_IMA_QT ? 34*p_wf->nChannels:1024;
216 msg_Warn( p_dec, "block size undefined, -> using %d", p_sys->i_block );
219 /* calculate samples per block */
220 switch( p_sys->codec )
223 p_sys->i_samplesperblock = 64;
226 p_sys->i_samplesperblock =
227 2 * ( p_sys->i_block - 4 * p_wf->nChannels )/ p_wf->nChannels;
230 p_sys->i_samplesperblock =
231 2 * (p_sys->i_block - 7 * p_wf->nChannels)/p_wf->nChannels + 2;
234 p_sys->i_samplesperblock =
235 2 * (p_sys->i_block - 4 * p_wf->nChannels)/p_wf->nChannels + 1;
239 p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
243 "format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d samplesperblock %d",
244 p_wf->nSamplesPerSec, p_wf->nChannels,
245 p_wf->wBitsPerSample, p_wf->nBlockAlign,
246 p_sys->i_samplesperblock );
248 p_sys->output_format.i_format = AOUT_FMT_S16_NE;
249 p_sys->output_format.i_rate = p_wf->nSamplesPerSec;
250 p_sys->output_format.i_physical_channels =
251 p_sys->output_format.i_original_channels =
252 pi_channels_maps[p_wf->nChannels];
254 p_sys->p_aout = NULL;
255 p_sys->p_aout_input = aout_DecNew( p_dec,
256 &p_sys->p_aout, &p_sys->output_format);
257 if( p_sys->p_aout_input == NULL )
259 msg_Err( p_dec, "cannot create aout" );
263 aout_DateInit( &p_sys->date, p_sys->output_format.i_rate );
264 aout_DateSet( &p_sys->date, 0 );
269 /*****************************************************************************
271 *****************************************************************************/
272 static int Decode( decoder_t *p_dec, block_t *p_block )
274 decoder_sys_t *p_sys = p_dec->p_sys;
275 mtime_t i_pts = p_block->i_pts;
276 uint8_t *p_data = p_block->p_buffer;
277 int i_data = p_block->i_buffer;
279 while( i_data >= p_sys->i_block )
283 if( i_pts != 0 && i_pts != aout_DateGet( &p_sys->date ) )
285 aout_DateSet( &p_sys->date, i_pts );
287 else if( !aout_DateGet( &p_sys->date ) )
289 block_Release( p_block );
294 out = aout_DecNewBuffer( p_sys->p_aout,
296 p_sys->i_samplesperblock );
299 msg_Err( p_dec, "cannot get aout buffer" );
300 block_Release( p_block );
303 out->start_date = aout_DateGet( &p_sys->date );
304 out->end_date = aout_DateIncrement( &p_sys->date,
305 p_sys->i_samplesperblock );
307 switch( p_sys->codec )
310 DecodeAdpcmImaQT( p_sys, (int16_t*)out->p_buffer, p_data );
313 DecodeAdpcmImaWav( p_sys, (int16_t*)out->p_buffer, p_data );
316 DecodeAdpcmMs( p_sys, (int16_t*)out->p_buffer, p_data );
319 DecodeAdpcmDk4( p_sys, (int16_t*)out->p_buffer, p_data );
322 DecodeAdpcmDk3( p_sys, (int16_t*)out->p_buffer, p_data );
327 aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input, out );
329 p_data += p_sys->i_block;
330 i_data -= p_sys->i_block;
333 block_Release( p_block );
337 /*****************************************************************************
339 *****************************************************************************/
340 static int End ( decoder_t *p_dec )
342 decoder_sys_t *p_sys = p_dec->p_sys;
344 if( p_sys->p_aout_input )
346 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
353 #define CLAMP( v, min, max ) \
354 if( (v) < (min) ) (v) = (min); \
355 if( (v) > (max) ) (v) = (max)
357 #define GetByte( v ) \
358 (v) = *p_buffer; p_buffer++;
360 #define GetWord( v ) \
361 (v) = *p_buffer; p_buffer++; \
362 (v) |= ( *p_buffer ) << 8; p_buffer++; \
363 if( (v)&0x8000 ) (v) -= 0x010000;
368 typedef struct adpcm_ms_channel_s
371 int i_sample1, i_sample2;
372 int i_coeff1, i_coeff2;
374 } adpcm_ms_channel_t;
377 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
384 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
386 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
387 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
388 i_snibble * p_channel->i_idelta;
390 CLAMP( i_predictor, -32768, 32767 );
392 p_channel->i_sample2 = p_channel->i_sample1;
393 p_channel->i_sample1 = i_predictor;
395 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
396 p_channel->i_idelta ) / 256;
397 if( p_channel->i_idelta < 16 )
399 p_channel->i_idelta = 16;
401 return( i_predictor );
404 static void DecodeAdpcmMs( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
406 adpcm_ms_channel_t channel[2];
409 int i_block_predictor;
411 b_stereo = p_sys->p_wf->nChannels == 2 ? 1 : 0;
413 GetByte( i_block_predictor );
414 CLAMP( i_block_predictor, 0, 6 );
415 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
416 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
420 GetByte( i_block_predictor );
421 CLAMP( i_block_predictor, 0, 6 );
422 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
423 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
425 GetWord( channel[0].i_idelta );
428 GetWord( channel[1].i_idelta );
431 GetWord( channel[0].i_sample1 );
434 GetWord( channel[1].i_sample1 );
437 GetWord( channel[0].i_sample2 );
440 GetWord( channel[1].i_sample2 );
445 *p_sample++ = channel[0].i_sample2;
446 *p_sample++ = channel[1].i_sample2;
447 *p_sample++ = channel[0].i_sample1;
448 *p_sample++ = channel[1].i_sample1;
452 *p_sample++ = channel[0].i_sample2;
453 *p_sample++ = channel[0].i_sample1;
456 for( i_nibbles = 2 *( p_sys->i_block - 7 * p_sys->p_wf->nChannels );
457 i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
459 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
460 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
468 typedef struct adpcm_ima_wav_channel_s
473 } adpcm_ima_wav_channel_t;
475 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
480 i_diff = i_step_table[p_channel->i_step_index] >> 3;
481 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
482 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
483 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
485 p_channel->i_predictor -= i_diff;
487 p_channel->i_predictor += i_diff;
489 CLAMP( p_channel->i_predictor, -32768, 32767 );
491 p_channel->i_step_index += i_index_table[i_nibble];
493 CLAMP( p_channel->i_step_index, 0, 88 );
495 return( p_channel->i_predictor );
498 static void DecodeAdpcmImaWav( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
500 adpcm_ima_wav_channel_t channel[2];
504 b_stereo = p_sys->p_wf->nChannels == 2 ? 1 : 0;
506 GetWord( channel[0].i_predictor );
507 GetByte( channel[0].i_step_index );
508 CLAMP( channel[0].i_step_index, 0, 88 );
513 GetWord( channel[1].i_predictor );
514 GetByte( channel[1].i_step_index );
515 CLAMP( channel[1].i_step_index, 0, 88 );
521 for( i_nibbles = 2 * (p_sys->i_block - 8);
527 for( i = 0; i < 4; i++ )
530 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
531 p_sample[i * 4 + 2] =
532 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
536 for( i = 0; i < 4; i++ )
538 p_sample[i * 4 + 1] =
539 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
540 p_sample[i * 4 + 3] =
541 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
552 for( i_nibbles = 2 * (p_sys->i_block - 4);
554 i_nibbles -= 2, p_buffer++ )
556 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
557 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
565 static void DecodeAdpcmImaQT( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
567 adpcm_ima_wav_channel_t channel[2];
572 i_step = p_sys->p_wf->nChannels;
574 for( i_ch = 0; i_ch < p_sys->p_wf->nChannels; i_ch++ )
577 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
578 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
580 CLAMP( channel[i_ch].i_step_index, 0, 88 );
583 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
585 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
588 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
595 p_sample += 1 - 64 * i_step;
603 static void DecodeAdpcmDk4( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
605 adpcm_ima_wav_channel_t channel[2];
609 b_stereo = p_sys->p_wf->nChannels == 2 ? 1 : 0;
611 GetWord( channel[0].i_predictor );
612 GetByte( channel[0].i_step_index );
613 CLAMP( channel[0].i_step_index, 0, 88 );
618 GetWord( channel[1].i_predictor );
619 GetByte( channel[1].i_step_index );
620 CLAMP( channel[1].i_step_index, 0, 88 );
624 /* first output predictor */
625 *p_sample++ = channel[0].i_predictor;
628 *p_sample++ = channel[1].i_predictor;
632 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
635 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
637 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
647 static void DecodeAdpcmDk3( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
649 uint8_t *p_end = &p_buffer[p_sys->i_block];
650 adpcm_ima_wav_channel_t sum;
651 adpcm_ima_wav_channel_t diff;
656 GetWord( sum.i_predictor );
657 GetWord( diff.i_predictor );
658 GetByte( sum.i_step_index );
659 GetByte( diff.i_step_index );
661 i_diff_value = diff.i_predictor;
662 /* we process 6 nibbles at once */
663 while( p_buffer + 1 <= p_end )
665 /* first 3 nibbles */
666 AdpcmImaWavExpandNibble( &sum,
669 AdpcmImaWavExpandNibble( &diff,
672 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
674 *p_sample++ = sum.i_predictor + i_diff_value;
675 *p_sample++ = sum.i_predictor - i_diff_value;
679 AdpcmImaWavExpandNibble( &sum,
682 *p_sample++ = sum.i_predictor + i_diff_value;
683 *p_sample++ = sum.i_predictor - i_diff_value;
685 /* now last 3 nibbles */
686 AdpcmImaWavExpandNibble( &sum,
689 if( p_buffer < p_end )
691 AdpcmImaWavExpandNibble( &diff,
694 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
696 *p_sample++ = sum.i_predictor + i_diff_value;
697 *p_sample++ = sum.i_predictor - i_diff_value;
699 AdpcmImaWavExpandNibble( &sum,
703 *p_sample++ = sum.i_predictor + i_diff_value;
704 *p_sample++ = sum.i_predictor - i_diff_value;