1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: adpcm.c,v 1.16 2003/11/16 21:07:30 gbazin 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() */
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int OpenDecoder( vlc_object_t * );
41 static void CloseDecoder( vlc_object_t * );
43 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
46 set_description( _("ADPCM audio decoder") );
47 set_capability( "decoder", 50 );
48 set_callbacks( OpenDecoder, CloseDecoder );
51 /*****************************************************************************
53 *****************************************************************************/
65 enum adpcm_codec_e codec;
68 int i_samplesperblock;
70 audio_date_t end_date;
73 static void DecodeAdpcmMs ( decoder_t *, int16_t *, uint8_t * );
74 static void DecodeAdpcmImaWav( decoder_t *, int16_t *, uint8_t * );
75 static void DecodeAdpcmImaQT ( decoder_t *, int16_t *, uint8_t * );
76 static void DecodeAdpcmDk4 ( decoder_t *, int16_t *, uint8_t * );
77 static void DecodeAdpcmDk3 ( 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 */
146 /* Allocate the memory needed to store the decoder's structure */
147 if( ( p_dec->p_sys = p_sys =
148 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
150 msg_Err( p_dec, "out of memory" );
154 if( p_dec->fmt_in.audio.i_channels <= 0 ||
155 p_dec->fmt_in.audio.i_channels > 5 )
157 msg_Err( p_dec, "bad channels count(1-5)" );
161 if( p_dec->fmt_in.audio.i_rate <= 0 )
163 msg_Err( p_dec, "bad samplerate" );
167 switch( p_dec->fmt_in.i_codec )
169 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
170 p_sys->codec = ADPCM_IMA_QT;
172 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
173 p_sys->codec = ADPCM_IMA_WAV;
175 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
176 p_sys->codec = ADPCM_MS;
178 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
179 p_sys->codec = ADPCM_DK4;
181 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
182 p_sys->codec = ADPCM_DK3;
186 if( 1 )//( p_sys->i_block = p_wf->nBlockAlign ) <= 0 )
188 p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
189 34 * p_dec->fmt_in.audio.i_channels : 1024;
190 msg_Warn( p_dec, "block size undefined, -> using %d", p_sys->i_block );
193 /* calculate samples per block */
194 switch( p_sys->codec )
197 p_sys->i_samplesperblock = 64;
200 p_sys->i_samplesperblock =
201 2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
202 p_dec->fmt_in.audio.i_channels;
205 p_sys->i_samplesperblock =
206 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
207 p_dec->fmt_in.audio.i_channels + 2;
210 p_sys->i_samplesperblock =
211 2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
212 p_dec->fmt_in.audio.i_channels + 1;
215 p_dec->fmt_in.audio.i_channels = 2;
216 p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
220 msg_Dbg( p_dec, "format: samplerate:%dHz channels:%d bits/sample:%d "
221 "blockalign:%d samplesperblock %d",
222 p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
223 p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
224 p_sys->i_samplesperblock );
226 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
227 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
228 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
229 p_dec->fmt_out.audio.i_physical_channels =
230 p_dec->fmt_out.audio.i_original_channels =
231 pi_channels_maps[p_dec->fmt_in.audio.i_channels];
233 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
234 aout_DateSet( &p_sys->end_date, 0 );
236 p_dec->pf_decode_audio = DecodeBlock;
241 /*****************************************************************************
243 *****************************************************************************/
244 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
246 decoder_sys_t *p_sys = p_dec->p_sys;
249 if( !pp_block || !*pp_block ) return NULL;
253 if( p_block->i_pts != 0 &&
254 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
256 aout_DateSet( &p_sys->end_date, p_block->i_pts );
258 else if( !aout_DateGet( &p_sys->end_date ) )
260 /* We've just started the stream, wait for the first PTS. */
261 block_Release( p_block );
265 /* Don't re-use the same pts twice */
268 if( p_block->i_buffer >= p_sys->i_block )
270 aout_buffer_t *p_out;
272 p_out = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_samplesperblock );
275 block_Release( p_block );
279 p_out->start_date = aout_DateGet( &p_sys->end_date );
281 aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
283 switch( p_sys->codec )
286 DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
290 DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
294 DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
298 DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
302 DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
309 p_block->p_buffer += p_out->i_nb_bytes;
310 p_block->i_buffer -= p_out->i_nb_bytes;
314 block_Release( p_block );
318 /*****************************************************************************
320 *****************************************************************************/
321 static void CloseDecoder( vlc_object_t *p_this )
323 decoder_t *p_dec = (decoder_t *)p_this;
324 decoder_sys_t *p_sys = p_dec->p_sys;
329 /*****************************************************************************
331 *****************************************************************************/
332 #define CLAMP( v, min, max ) \
333 if( (v) < (min) ) (v) = (min); \
334 if( (v) > (max) ) (v) = (max)
336 #define GetByte( v ) \
337 (v) = *p_buffer; p_buffer++;
339 #define GetWord( v ) \
340 (v) = *p_buffer; p_buffer++; \
341 (v) |= ( *p_buffer ) << 8; p_buffer++; \
342 if( (v)&0x8000 ) (v) -= 0x010000;
347 typedef struct adpcm_ms_channel_s
350 int i_sample1, i_sample2;
351 int i_coeff1, i_coeff2;
353 } adpcm_ms_channel_t;
356 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
363 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
365 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
366 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
367 i_snibble * p_channel->i_idelta;
369 CLAMP( i_predictor, -32768, 32767 );
371 p_channel->i_sample2 = p_channel->i_sample1;
372 p_channel->i_sample1 = i_predictor;
374 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
375 p_channel->i_idelta ) / 256;
376 if( p_channel->i_idelta < 16 )
378 p_channel->i_idelta = 16;
380 return( i_predictor );
383 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
386 decoder_sys_t *p_sys = p_dec->p_sys;
387 adpcm_ms_channel_t channel[2];
390 int i_block_predictor;
392 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
394 GetByte( i_block_predictor );
395 CLAMP( i_block_predictor, 0, 6 );
396 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
397 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
401 GetByte( i_block_predictor );
402 CLAMP( i_block_predictor, 0, 6 );
403 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
404 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
406 GetWord( channel[0].i_idelta );
409 GetWord( channel[1].i_idelta );
412 GetWord( channel[0].i_sample1 );
415 GetWord( channel[1].i_sample1 );
418 GetWord( channel[0].i_sample2 );
421 GetWord( channel[1].i_sample2 );
426 *p_sample++ = channel[0].i_sample2;
427 *p_sample++ = channel[1].i_sample2;
428 *p_sample++ = channel[0].i_sample1;
429 *p_sample++ = channel[1].i_sample1;
433 *p_sample++ = channel[0].i_sample2;
434 *p_sample++ = channel[0].i_sample1;
437 for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
438 i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
440 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
441 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
449 typedef struct adpcm_ima_wav_channel_s
454 } adpcm_ima_wav_channel_t;
456 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
461 i_diff = i_step_table[p_channel->i_step_index] >> 3;
462 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
463 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
464 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
466 p_channel->i_predictor -= i_diff;
468 p_channel->i_predictor += i_diff;
470 CLAMP( p_channel->i_predictor, -32768, 32767 );
472 p_channel->i_step_index += i_index_table[i_nibble];
474 CLAMP( p_channel->i_step_index, 0, 88 );
476 return( p_channel->i_predictor );
479 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
482 decoder_sys_t *p_sys = p_dec->p_sys;
483 adpcm_ima_wav_channel_t channel[2];
487 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
489 GetWord( channel[0].i_predictor );
490 GetByte( channel[0].i_step_index );
491 CLAMP( channel[0].i_step_index, 0, 88 );
496 GetWord( channel[1].i_predictor );
497 GetByte( channel[1].i_step_index );
498 CLAMP( channel[1].i_step_index, 0, 88 );
504 for( i_nibbles = 2 * (p_sys->i_block - 8);
510 for( i = 0; i < 4; i++ )
513 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
514 p_sample[i * 4 + 2] =
515 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
519 for( i = 0; i < 4; i++ )
521 p_sample[i * 4 + 1] =
522 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
523 p_sample[i * 4 + 3] =
524 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
535 for( i_nibbles = 2 * (p_sys->i_block - 4);
537 i_nibbles -= 2, p_buffer++ )
539 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
540 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
548 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
551 adpcm_ima_wav_channel_t channel[2];
556 i_step = p_dec->fmt_in.audio.i_channels;
558 for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
561 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
562 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
564 CLAMP( channel[i_ch].i_step_index, 0, 88 );
567 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
569 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
572 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
579 p_sample += 1 - 64 * i_step;
586 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
589 decoder_sys_t *p_sys = p_dec->p_sys;
590 adpcm_ima_wav_channel_t channel[2];
594 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
596 GetWord( channel[0].i_predictor );
597 GetByte( channel[0].i_step_index );
598 CLAMP( channel[0].i_step_index, 0, 88 );
603 GetWord( channel[1].i_predictor );
604 GetByte( channel[1].i_step_index );
605 CLAMP( channel[1].i_step_index, 0, 88 );
609 /* first output predictor */
610 *p_sample++ = channel[0].i_predictor;
613 *p_sample++ = channel[1].i_predictor;
617 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
620 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
622 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
632 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
635 decoder_sys_t *p_sys = p_dec->p_sys;
636 uint8_t *p_end = &p_buffer[p_sys->i_block];
637 adpcm_ima_wav_channel_t sum;
638 adpcm_ima_wav_channel_t diff;
643 GetWord( sum.i_predictor );
644 GetWord( diff.i_predictor );
645 GetByte( sum.i_step_index );
646 GetByte( diff.i_step_index );
648 i_diff_value = diff.i_predictor;
649 /* we process 6 nibbles at once */
650 while( p_buffer + 1 <= p_end )
652 /* first 3 nibbles */
653 AdpcmImaWavExpandNibble( &sum,
656 AdpcmImaWavExpandNibble( &diff,
659 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
661 *p_sample++ = sum.i_predictor + i_diff_value;
662 *p_sample++ = sum.i_predictor - i_diff_value;
666 AdpcmImaWavExpandNibble( &sum,
669 *p_sample++ = sum.i_predictor + i_diff_value;
670 *p_sample++ = sum.i_predictor - i_diff_value;
672 /* now last 3 nibbles */
673 AdpcmImaWavExpandNibble( &sum,
676 if( p_buffer < p_end )
678 AdpcmImaWavExpandNibble( &diff,
681 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
683 *p_sample++ = sum.i_predictor + i_diff_value;
684 *p_sample++ = sum.i_predictor - i_diff_value;
686 AdpcmImaWavExpandNibble( &sum,
690 *p_sample++ = sum.i_predictor + i_diff_value;
691 *p_sample++ = sum.i_predictor - i_diff_value;