1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: adpcm.c,v 1.17 2003/11/22 23:39:14 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 *****************************************************************************/
30 #include <vlc/decoder.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static int OpenDecoder( vlc_object_t * );
36 static void CloseDecoder( vlc_object_t * );
38 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
41 set_description( _("ADPCM audio decoder") );
42 set_capability( "decoder", 50 );
43 set_callbacks( OpenDecoder, CloseDecoder );
46 /*****************************************************************************
48 *****************************************************************************/
60 enum adpcm_codec_e codec;
63 int i_samplesperblock;
65 audio_date_t end_date;
68 static void DecodeAdpcmMs ( decoder_t *, int16_t *, uint8_t * );
69 static void DecodeAdpcmImaWav( decoder_t *, int16_t *, uint8_t * );
70 static void DecodeAdpcmImaQT ( decoder_t *, int16_t *, uint8_t * );
71 static void DecodeAdpcmDk4 ( decoder_t *, int16_t *, uint8_t * );
72 static void DecodeAdpcmDk3 ( decoder_t *, int16_t *, uint8_t * );
74 static int pi_channels_maps[6] =
78 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
79 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
80 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
81 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
82 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
85 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
86 static int i_index_table[16] =
88 -1, -1, -1, -1, 2, 4, 6, 8,
89 -1, -1, -1, -1, 2, 4, 6, 8
92 static int i_step_table[89] =
94 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
95 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
96 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
97 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
98 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
99 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
100 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
101 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
102 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
105 static int i_adaptation_table[16] =
107 230, 230, 230, 230, 307, 409, 512, 614,
108 768, 614, 512, 409, 307, 230, 230, 230
111 static int i_adaptation_coeff1[7] =
113 256, 512, 0, 192, 240, 460, 392
116 static int i_adaptation_coeff2[7] =
118 0, -256, 0, 64, 0, -208, -232
121 /*****************************************************************************
122 * OpenDecoder: probe the decoder and return score
123 *****************************************************************************/
124 static int OpenDecoder( vlc_object_t *p_this )
126 decoder_t *p_dec = (decoder_t*)p_this;
127 decoder_sys_t *p_sys;
129 switch( p_dec->fmt_in.i_codec )
131 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
132 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
133 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
134 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
135 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
141 /* Allocate the memory needed to store the decoder's structure */
142 if( ( p_dec->p_sys = p_sys =
143 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
145 msg_Err( p_dec, "out of memory" );
149 if( p_dec->fmt_in.audio.i_channels <= 0 ||
150 p_dec->fmt_in.audio.i_channels > 5 )
152 msg_Err( p_dec, "bad channels count(1-5)" );
156 if( p_dec->fmt_in.audio.i_rate <= 0 )
158 msg_Err( p_dec, "bad samplerate" );
162 switch( p_dec->fmt_in.i_codec )
164 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
165 p_sys->codec = ADPCM_IMA_QT;
167 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
168 p_sys->codec = ADPCM_IMA_WAV;
170 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
171 p_sys->codec = ADPCM_MS;
173 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
174 p_sys->codec = ADPCM_DK4;
176 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
177 p_sys->codec = ADPCM_DK3;
181 if( 1 )//( p_sys->i_block = p_wf->nBlockAlign ) <= 0 )
183 p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
184 34 * p_dec->fmt_in.audio.i_channels : 1024;
185 msg_Warn( p_dec, "block size undefined, -> using %d", p_sys->i_block );
188 /* calculate samples per block */
189 switch( p_sys->codec )
192 p_sys->i_samplesperblock = 64;
195 p_sys->i_samplesperblock =
196 2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
197 p_dec->fmt_in.audio.i_channels;
200 p_sys->i_samplesperblock =
201 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
202 p_dec->fmt_in.audio.i_channels + 2;
205 p_sys->i_samplesperblock =
206 2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
207 p_dec->fmt_in.audio.i_channels + 1;
210 p_dec->fmt_in.audio.i_channels = 2;
211 p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
215 msg_Dbg( p_dec, "format: samplerate:%dHz channels:%d bits/sample:%d "
216 "blockalign:%d samplesperblock %d",
217 p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
218 p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
219 p_sys->i_samplesperblock );
221 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
222 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
223 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
224 p_dec->fmt_out.audio.i_physical_channels =
225 p_dec->fmt_out.audio.i_original_channels =
226 pi_channels_maps[p_dec->fmt_in.audio.i_channels];
228 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
229 aout_DateSet( &p_sys->end_date, 0 );
231 p_dec->pf_decode_audio = DecodeBlock;
236 /*****************************************************************************
238 *****************************************************************************/
239 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
241 decoder_sys_t *p_sys = p_dec->p_sys;
244 if( !pp_block || !*pp_block ) return NULL;
248 if( p_block->i_pts != 0 &&
249 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
251 aout_DateSet( &p_sys->end_date, p_block->i_pts );
253 else if( !aout_DateGet( &p_sys->end_date ) )
255 /* We've just started the stream, wait for the first PTS. */
256 block_Release( p_block );
260 /* Don't re-use the same pts twice */
263 if( p_block->i_buffer >= p_sys->i_block )
265 aout_buffer_t *p_out;
267 p_out = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_samplesperblock );
270 block_Release( p_block );
274 p_out->start_date = aout_DateGet( &p_sys->end_date );
276 aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
278 switch( p_sys->codec )
281 DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
285 DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
289 DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
293 DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
297 DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
304 p_block->p_buffer += p_out->i_nb_bytes;
305 p_block->i_buffer -= p_out->i_nb_bytes;
309 block_Release( p_block );
313 /*****************************************************************************
315 *****************************************************************************/
316 static void CloseDecoder( vlc_object_t *p_this )
318 decoder_t *p_dec = (decoder_t *)p_this;
319 decoder_sys_t *p_sys = p_dec->p_sys;
324 /*****************************************************************************
326 *****************************************************************************/
327 #define CLAMP( v, min, max ) \
328 if( (v) < (min) ) (v) = (min); \
329 if( (v) > (max) ) (v) = (max)
331 #define GetByte( v ) \
332 (v) = *p_buffer; p_buffer++;
334 #define GetWord( v ) \
335 (v) = *p_buffer; p_buffer++; \
336 (v) |= ( *p_buffer ) << 8; p_buffer++; \
337 if( (v)&0x8000 ) (v) -= 0x010000;
342 typedef struct adpcm_ms_channel_s
345 int i_sample1, i_sample2;
346 int i_coeff1, i_coeff2;
348 } adpcm_ms_channel_t;
351 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
358 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
360 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
361 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
362 i_snibble * p_channel->i_idelta;
364 CLAMP( i_predictor, -32768, 32767 );
366 p_channel->i_sample2 = p_channel->i_sample1;
367 p_channel->i_sample1 = i_predictor;
369 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
370 p_channel->i_idelta ) / 256;
371 if( p_channel->i_idelta < 16 )
373 p_channel->i_idelta = 16;
375 return( i_predictor );
378 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
381 decoder_sys_t *p_sys = p_dec->p_sys;
382 adpcm_ms_channel_t channel[2];
385 int i_block_predictor;
387 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
389 GetByte( i_block_predictor );
390 CLAMP( i_block_predictor, 0, 6 );
391 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
392 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
396 GetByte( i_block_predictor );
397 CLAMP( i_block_predictor, 0, 6 );
398 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
399 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
401 GetWord( channel[0].i_idelta );
404 GetWord( channel[1].i_idelta );
407 GetWord( channel[0].i_sample1 );
410 GetWord( channel[1].i_sample1 );
413 GetWord( channel[0].i_sample2 );
416 GetWord( channel[1].i_sample2 );
421 *p_sample++ = channel[0].i_sample2;
422 *p_sample++ = channel[1].i_sample2;
423 *p_sample++ = channel[0].i_sample1;
424 *p_sample++ = channel[1].i_sample1;
428 *p_sample++ = channel[0].i_sample2;
429 *p_sample++ = channel[0].i_sample1;
432 for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
433 i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
435 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
436 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
444 typedef struct adpcm_ima_wav_channel_s
449 } adpcm_ima_wav_channel_t;
451 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
456 i_diff = i_step_table[p_channel->i_step_index] >> 3;
457 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
458 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
459 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
461 p_channel->i_predictor -= i_diff;
463 p_channel->i_predictor += i_diff;
465 CLAMP( p_channel->i_predictor, -32768, 32767 );
467 p_channel->i_step_index += i_index_table[i_nibble];
469 CLAMP( p_channel->i_step_index, 0, 88 );
471 return( p_channel->i_predictor );
474 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
477 decoder_sys_t *p_sys = p_dec->p_sys;
478 adpcm_ima_wav_channel_t channel[2];
482 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
484 GetWord( channel[0].i_predictor );
485 GetByte( channel[0].i_step_index );
486 CLAMP( channel[0].i_step_index, 0, 88 );
491 GetWord( channel[1].i_predictor );
492 GetByte( channel[1].i_step_index );
493 CLAMP( channel[1].i_step_index, 0, 88 );
499 for( i_nibbles = 2 * (p_sys->i_block - 8);
505 for( i = 0; i < 4; i++ )
508 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
509 p_sample[i * 4 + 2] =
510 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
514 for( i = 0; i < 4; i++ )
516 p_sample[i * 4 + 1] =
517 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
518 p_sample[i * 4 + 3] =
519 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
530 for( i_nibbles = 2 * (p_sys->i_block - 4);
532 i_nibbles -= 2, p_buffer++ )
534 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
535 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
543 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
546 adpcm_ima_wav_channel_t channel[2];
551 i_step = p_dec->fmt_in.audio.i_channels;
553 for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
556 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
557 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
559 CLAMP( channel[i_ch].i_step_index, 0, 88 );
562 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
564 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
567 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
574 p_sample += 1 - 64 * i_step;
581 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
584 decoder_sys_t *p_sys = p_dec->p_sys;
585 adpcm_ima_wav_channel_t channel[2];
589 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
591 GetWord( channel[0].i_predictor );
592 GetByte( channel[0].i_step_index );
593 CLAMP( channel[0].i_step_index, 0, 88 );
598 GetWord( channel[1].i_predictor );
599 GetByte( channel[1].i_step_index );
600 CLAMP( channel[1].i_step_index, 0, 88 );
604 /* first output predictor */
605 *p_sample++ = channel[0].i_predictor;
608 *p_sample++ = channel[1].i_predictor;
612 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
615 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
617 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
627 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
630 decoder_sys_t *p_sys = p_dec->p_sys;
631 uint8_t *p_end = &p_buffer[p_sys->i_block];
632 adpcm_ima_wav_channel_t sum;
633 adpcm_ima_wav_channel_t diff;
638 GetWord( sum.i_predictor );
639 GetWord( diff.i_predictor );
640 GetByte( sum.i_step_index );
641 GetByte( diff.i_step_index );
643 i_diff_value = diff.i_predictor;
644 /* we process 6 nibbles at once */
645 while( p_buffer + 1 <= p_end )
647 /* first 3 nibbles */
648 AdpcmImaWavExpandNibble( &sum,
651 AdpcmImaWavExpandNibble( &diff,
654 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
656 *p_sample++ = sum.i_predictor + i_diff_value;
657 *p_sample++ = sum.i_predictor - i_diff_value;
661 AdpcmImaWavExpandNibble( &sum,
664 *p_sample++ = sum.i_predictor + i_diff_value;
665 *p_sample++ = sum.i_predictor - i_diff_value;
667 /* now last 3 nibbles */
668 AdpcmImaWavExpandNibble( &sum,
671 if( p_buffer < p_end )
673 AdpcmImaWavExpandNibble( &diff,
676 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
678 *p_sample++ = sum.i_predictor + i_diff_value;
679 *p_sample++ = sum.i_predictor - i_diff_value;
681 AdpcmImaWavExpandNibble( &sum,
685 *p_sample++ = sum.i_predictor + i_diff_value;
686 *p_sample++ = sum.i_predictor - i_diff_value;