1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: adpcm.c,v 1.14 2003/11/04 14:51:51 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 ) )
293 out = aout_DecNewBuffer( p_sys->p_aout,
295 p_sys->i_samplesperblock );
298 msg_Err( p_dec, "cannot get aout buffer" );
301 out->start_date = aout_DateGet( &p_sys->date );
302 out->end_date = aout_DateIncrement( &p_sys->date,
303 p_sys->i_samplesperblock );
305 switch( p_sys->codec )
308 DecodeAdpcmImaQT( p_sys, (int16_t*)out->p_buffer, p_data );
311 DecodeAdpcmImaWav( p_sys, (int16_t*)out->p_buffer, p_data );
314 DecodeAdpcmMs( p_sys, (int16_t*)out->p_buffer, p_data );
317 DecodeAdpcmDk4( p_sys, (int16_t*)out->p_buffer, p_data );
320 DecodeAdpcmDk3( p_sys, (int16_t*)out->p_buffer, p_data );
325 aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input, out );
327 p_data += p_sys->i_block;
328 i_data -= p_sys->i_block;
334 /*****************************************************************************
336 *****************************************************************************/
337 static int End ( decoder_t *p_dec )
339 decoder_sys_t *p_sys = p_dec->p_sys;
341 if( p_sys->p_aout_input )
343 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
350 #define CLAMP( v, min, max ) \
351 if( (v) < (min) ) (v) = (min); \
352 if( (v) > (max) ) (v) = (max)
354 #define GetByte( v ) \
355 (v) = *p_buffer; p_buffer++;
357 #define GetWord( v ) \
358 (v) = *p_buffer; p_buffer++; \
359 (v) |= ( *p_buffer ) << 8; p_buffer++; \
360 if( (v)&0x8000 ) (v) -= 0x010000;
365 typedef struct adpcm_ms_channel_s
368 int i_sample1, i_sample2;
369 int i_coeff1, i_coeff2;
371 } adpcm_ms_channel_t;
374 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
381 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
383 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
384 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
385 i_snibble * p_channel->i_idelta;
387 CLAMP( i_predictor, -32768, 32767 );
389 p_channel->i_sample2 = p_channel->i_sample1;
390 p_channel->i_sample1 = i_predictor;
392 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
393 p_channel->i_idelta ) / 256;
394 if( p_channel->i_idelta < 16 )
396 p_channel->i_idelta = 16;
398 return( i_predictor );
401 static void DecodeAdpcmMs( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
403 adpcm_ms_channel_t channel[2];
406 int i_block_predictor;
408 b_stereo = p_sys->p_wf->nChannels == 2 ? 1 : 0;
410 GetByte( i_block_predictor );
411 CLAMP( i_block_predictor, 0, 6 );
412 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
413 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
417 GetByte( i_block_predictor );
418 CLAMP( i_block_predictor, 0, 6 );
419 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
420 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
422 GetWord( channel[0].i_idelta );
425 GetWord( channel[1].i_idelta );
428 GetWord( channel[0].i_sample1 );
431 GetWord( channel[1].i_sample1 );
434 GetWord( channel[0].i_sample2 );
437 GetWord( channel[1].i_sample2 );
442 *p_sample++ = channel[0].i_sample2;
443 *p_sample++ = channel[1].i_sample2;
444 *p_sample++ = channel[0].i_sample1;
445 *p_sample++ = channel[1].i_sample1;
449 *p_sample++ = channel[0].i_sample2;
450 *p_sample++ = channel[0].i_sample1;
453 for( i_nibbles = 2 *( p_sys->i_block - 7 * p_sys->p_wf->nChannels );
454 i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
456 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
457 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
465 typedef struct adpcm_ima_wav_channel_s
470 } adpcm_ima_wav_channel_t;
472 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
477 i_diff = i_step_table[p_channel->i_step_index] >> 3;
478 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
479 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
480 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
482 p_channel->i_predictor -= i_diff;
484 p_channel->i_predictor += i_diff;
486 CLAMP( p_channel->i_predictor, -32768, 32767 );
488 p_channel->i_step_index += i_index_table[i_nibble];
490 CLAMP( p_channel->i_step_index, 0, 88 );
492 return( p_channel->i_predictor );
495 static void DecodeAdpcmImaWav( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
497 adpcm_ima_wav_channel_t channel[2];
501 b_stereo = p_sys->p_wf->nChannels == 2 ? 1 : 0;
503 GetWord( channel[0].i_predictor );
504 GetByte( channel[0].i_step_index );
505 CLAMP( channel[0].i_step_index, 0, 88 );
510 GetWord( channel[1].i_predictor );
511 GetByte( channel[1].i_step_index );
512 CLAMP( channel[1].i_step_index, 0, 88 );
518 for( i_nibbles = 2 * (p_sys->i_block - 8);
524 for( i = 0; i < 4; i++ )
527 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
528 p_sample[i * 4 + 2] =
529 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
533 for( i = 0; i < 4; i++ )
535 p_sample[i * 4 + 1] =
536 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
537 p_sample[i * 4 + 3] =
538 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
549 for( i_nibbles = 2 * (p_sys->i_block - 4);
551 i_nibbles -= 2, p_buffer++ )
553 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
554 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
562 static void DecodeAdpcmImaQT( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
564 adpcm_ima_wav_channel_t channel[2];
569 i_step = p_sys->p_wf->nChannels;
571 for( i_ch = 0; i_ch < p_sys->p_wf->nChannels; i_ch++ )
574 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
575 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
577 CLAMP( channel[i_ch].i_step_index, 0, 88 );
580 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
582 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
585 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
592 p_sample += 1 - 64 * i_step;
600 static void DecodeAdpcmDk4( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
602 adpcm_ima_wav_channel_t channel[2];
606 b_stereo = p_sys->p_wf->nChannels == 2 ? 1 : 0;
608 GetWord( channel[0].i_predictor );
609 GetByte( channel[0].i_step_index );
610 CLAMP( channel[0].i_step_index, 0, 88 );
615 GetWord( channel[1].i_predictor );
616 GetByte( channel[1].i_step_index );
617 CLAMP( channel[1].i_step_index, 0, 88 );
621 /* first output predictor */
622 *p_sample++ = channel[0].i_predictor;
625 *p_sample++ = channel[1].i_predictor;
629 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
632 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
634 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
644 static void DecodeAdpcmDk3( decoder_sys_t *p_sys, int16_t *p_sample, uint8_t *p_buffer )
646 uint8_t *p_end = &p_buffer[p_sys->i_block];
647 adpcm_ima_wav_channel_t sum;
648 adpcm_ima_wav_channel_t diff;
653 GetWord( sum.i_predictor );
654 GetWord( diff.i_predictor );
655 GetByte( sum.i_step_index );
656 GetByte( diff.i_step_index );
658 i_diff_value = diff.i_predictor;
659 /* we process 6 nibbles at once */
660 while( p_buffer + 1 <= p_end )
662 /* first 3 nibbles */
663 AdpcmImaWavExpandNibble( &sum,
666 AdpcmImaWavExpandNibble( &diff,
669 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
671 *p_sample++ = sum.i_predictor + i_diff_value;
672 *p_sample++ = sum.i_predictor - i_diff_value;
676 AdpcmImaWavExpandNibble( &sum,
679 *p_sample++ = sum.i_predictor + i_diff_value;
680 *p_sample++ = sum.i_predictor - i_diff_value;
682 /* now last 3 nibbles */
683 AdpcmImaWavExpandNibble( &sum,
686 if( p_buffer < p_end )
688 AdpcmImaWavExpandNibble( &diff,
691 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
693 *p_sample++ = sum.i_predictor + i_diff_value;
694 *p_sample++ = sum.i_predictor - i_diff_value;
696 AdpcmImaWavExpandNibble( &sum,
700 *p_sample++ = sum.i_predictor + i_diff_value;
701 *p_sample++ = sum.i_predictor - i_diff_value;