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 *****************************************************************************/
32 #include <vlc_codec.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int OpenDecoder( vlc_object_t * );
38 static void CloseDecoder( vlc_object_t * );
40 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
43 set_description( _("ADPCM audio decoder") );
44 set_capability( "decoder", 50 );
45 set_category( CAT_INPUT );
46 set_subcategory( SUBCAT_INPUT_ACODEC );
47 set_callbacks( OpenDecoder, CloseDecoder );
50 /*****************************************************************************
52 *****************************************************************************/
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 * );
78 static void DecodeAdpcmEA ( decoder_t *, int16_t *, uint8_t * );
80 static int pi_channels_maps[6] =
84 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
85 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
86 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
87 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
88 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
91 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
92 static int i_index_table[16] =
94 -1, -1, -1, -1, 2, 4, 6, 8,
95 -1, -1, -1, -1, 2, 4, 6, 8
98 static int i_step_table[89] =
100 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
101 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
102 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
103 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
104 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
105 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
106 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
107 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
108 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
111 static int i_adaptation_table[16] =
113 230, 230, 230, 230, 307, 409, 512, 614,
114 768, 614, 512, 409, 307, 230, 230, 230
117 static int i_adaptation_coeff1[7] =
119 256, 512, 0, 192, 240, 460, 392
122 static int i_adaptation_coeff2[7] =
124 0, -256, 0, 64, 0, -208, -232
127 /*****************************************************************************
128 * OpenDecoder: probe the decoder and return score
129 *****************************************************************************/
130 static int OpenDecoder( vlc_object_t *p_this )
132 decoder_t *p_dec = (decoder_t*)p_this;
133 decoder_sys_t *p_sys;
135 switch( p_dec->fmt_in.i_codec )
137 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
138 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
139 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
140 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
141 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
142 case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
148 if( p_dec->fmt_in.audio.i_channels <= 0 ||
149 p_dec->fmt_in.audio.i_channels > 5 )
151 msg_Err( p_dec, "invalid number of channel (not between 1 and 5): %i",
152 p_dec->fmt_in.audio.i_channels );
156 if( p_dec->fmt_in.audio.i_rate <= 0 )
158 msg_Err( p_dec, "bad samplerate" );
162 /* Allocate the memory needed to store the decoder's structure */
163 if( ( p_dec->p_sys = p_sys =
164 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
166 msg_Err( p_dec, "out of memory" );
170 switch( p_dec->fmt_in.i_codec )
172 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
173 p_sys->codec = ADPCM_IMA_QT;
175 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
176 p_sys->codec = ADPCM_IMA_WAV;
178 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
179 p_sys->codec = ADPCM_MS;
181 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
182 p_sys->codec = ADPCM_DK4;
184 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
185 p_sys->codec = ADPCM_DK3;
187 case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
188 p_sys->codec = ADPCM_EA;
189 p_dec->fmt_in.p_extra = calloc( 2 * p_dec->fmt_in.audio.i_channels,
191 if( p_dec->fmt_in.p_extra == NULL )
199 if( p_dec->fmt_in.audio.i_blockalign <= 0 )
201 p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
202 34 * p_dec->fmt_in.audio.i_channels : 1024;
203 msg_Warn( p_dec, "block size undefined, using %d", p_sys->i_block );
207 p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
210 /* calculate samples per block */
211 switch( p_sys->codec )
214 p_sys->i_samplesperblock = 64;
217 p_sys->i_samplesperblock =
218 2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
219 p_dec->fmt_in.audio.i_channels;
222 p_sys->i_samplesperblock =
223 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
224 p_dec->fmt_in.audio.i_channels + 2;
227 p_sys->i_samplesperblock =
228 2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
229 p_dec->fmt_in.audio.i_channels + 1;
232 p_dec->fmt_in.audio.i_channels = 2;
233 p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
236 p_sys->i_samplesperblock =
237 2 * (p_sys->i_block - p_dec->fmt_in.audio.i_channels) /
238 p_dec->fmt_in.audio.i_channels;
241 msg_Dbg( p_dec, "format: samplerate:%d Hz channels:%d bits/sample:%d "
242 "blockalign:%d samplesperblock:%d",
243 p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
244 p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
245 p_sys->i_samplesperblock );
247 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
248 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
249 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
250 p_dec->fmt_out.audio.i_physical_channels =
251 p_dec->fmt_out.audio.i_original_channels =
252 pi_channels_maps[p_dec->fmt_in.audio.i_channels];
254 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
255 aout_DateSet( &p_sys->end_date, 0 );
257 p_dec->pf_decode_audio = DecodeBlock;
262 /*****************************************************************************
264 *****************************************************************************/
265 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
267 decoder_sys_t *p_sys = p_dec->p_sys;
270 if( !pp_block || !*pp_block ) return NULL;
274 if( p_block->i_pts != 0 &&
275 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
277 aout_DateSet( &p_sys->end_date, p_block->i_pts );
279 else if( !aout_DateGet( &p_sys->end_date ) )
281 /* We've just started the stream, wait for the first PTS. */
282 block_Release( p_block );
286 /* Don't re-use the same pts twice */
289 if( p_block->i_buffer >= p_sys->i_block )
291 aout_buffer_t *p_out;
293 p_out = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_samplesperblock );
296 block_Release( p_block );
300 p_out->start_date = aout_DateGet( &p_sys->end_date );
302 aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
304 switch( p_sys->codec )
307 DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
311 DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
315 DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
319 DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
323 DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
327 DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
333 p_block->p_buffer += p_sys->i_block;
334 p_block->i_buffer -= p_sys->i_block;
338 block_Release( p_block );
342 /*****************************************************************************
344 *****************************************************************************/
345 static void CloseDecoder( vlc_object_t *p_this )
347 decoder_t *p_dec = (decoder_t *)p_this;
348 decoder_sys_t *p_sys = p_dec->p_sys;
350 if( p_sys->codec == ADPCM_EA )
351 free( p_dec->fmt_in.p_extra );
355 /*****************************************************************************
357 *****************************************************************************/
358 #define CLAMP( v, min, max ) \
359 if( (v) < (min) ) (v) = (min); \
360 if( (v) > (max) ) (v) = (max)
362 #define GetByte( v ) \
363 (v) = *p_buffer; p_buffer++;
365 #define GetWord( v ) \
366 (v) = *p_buffer; p_buffer++; \
367 (v) |= ( *p_buffer ) << 8; p_buffer++; \
368 if( (v)&0x8000 ) (v) -= 0x010000;
373 typedef struct adpcm_ms_channel_s
376 int i_sample1, i_sample2;
377 int i_coeff1, i_coeff2;
379 } adpcm_ms_channel_t;
382 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
389 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
391 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
392 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
393 i_snibble * p_channel->i_idelta;
395 CLAMP( i_predictor, -32768, 32767 );
397 p_channel->i_sample2 = p_channel->i_sample1;
398 p_channel->i_sample1 = i_predictor;
400 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
401 p_channel->i_idelta ) / 256;
402 if( p_channel->i_idelta < 16 )
404 p_channel->i_idelta = 16;
406 return( i_predictor );
409 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
412 decoder_sys_t *p_sys = p_dec->p_sys;
413 adpcm_ms_channel_t channel[2];
416 int i_block_predictor;
418 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
420 GetByte( i_block_predictor );
421 CLAMP( i_block_predictor, 0, 6 );
422 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
423 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
427 GetByte( i_block_predictor );
428 CLAMP( i_block_predictor, 0, 6 );
429 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
430 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
432 GetWord( channel[0].i_idelta );
435 GetWord( channel[1].i_idelta );
438 GetWord( channel[0].i_sample1 );
441 GetWord( channel[1].i_sample1 );
444 GetWord( channel[0].i_sample2 );
447 GetWord( channel[1].i_sample2 );
452 *p_sample++ = channel[0].i_sample2;
453 *p_sample++ = channel[1].i_sample2;
454 *p_sample++ = channel[0].i_sample1;
455 *p_sample++ = channel[1].i_sample1;
459 *p_sample++ = channel[0].i_sample2;
460 *p_sample++ = channel[0].i_sample1;
463 for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
464 i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
466 *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
467 *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
475 typedef struct adpcm_ima_wav_channel_s
480 } adpcm_ima_wav_channel_t;
482 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
487 i_diff = i_step_table[p_channel->i_step_index] >> 3;
488 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
489 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
490 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
492 p_channel->i_predictor -= i_diff;
494 p_channel->i_predictor += i_diff;
496 CLAMP( p_channel->i_predictor, -32768, 32767 );
498 p_channel->i_step_index += i_index_table[i_nibble];
500 CLAMP( p_channel->i_step_index, 0, 88 );
502 return( p_channel->i_predictor );
505 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
508 decoder_sys_t *p_sys = p_dec->p_sys;
509 adpcm_ima_wav_channel_t channel[2];
513 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
515 GetWord( channel[0].i_predictor );
516 GetByte( channel[0].i_step_index );
517 CLAMP( channel[0].i_step_index, 0, 88 );
522 GetWord( channel[1].i_predictor );
523 GetByte( channel[1].i_step_index );
524 CLAMP( channel[1].i_step_index, 0, 88 );
530 for( i_nibbles = 2 * (p_sys->i_block - 8);
536 for( i = 0; i < 4; i++ )
539 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
540 p_sample[i * 4 + 2] =
541 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
545 for( i = 0; i < 4; i++ )
547 p_sample[i * 4 + 1] =
548 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
549 p_sample[i * 4 + 3] =
550 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
561 for( i_nibbles = 2 * (p_sys->i_block - 4);
563 i_nibbles -= 2, p_buffer++ )
565 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
566 *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
574 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
577 adpcm_ima_wav_channel_t channel[2];
582 i_step = p_dec->fmt_in.audio.i_channels;
584 for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
587 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
588 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
590 CLAMP( channel[i_ch].i_step_index, 0, 88 );
593 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
595 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
598 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
605 p_sample += 1 - 64 * i_step;
612 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
615 decoder_sys_t *p_sys = p_dec->p_sys;
616 adpcm_ima_wav_channel_t channel[2];
620 b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
622 GetWord( channel[0].i_predictor );
623 GetByte( channel[0].i_step_index );
624 CLAMP( channel[0].i_step_index, 0, 88 );
629 GetWord( channel[1].i_predictor );
630 GetByte( channel[1].i_step_index );
631 CLAMP( channel[1].i_step_index, 0, 88 );
635 /* first output predictor */
636 *p_sample++ = channel[0].i_predictor;
639 *p_sample++ = channel[1].i_predictor;
643 i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
646 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
648 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
658 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
661 decoder_sys_t *p_sys = p_dec->p_sys;
662 uint8_t *p_end = &p_buffer[p_sys->i_block];
663 adpcm_ima_wav_channel_t sum;
664 adpcm_ima_wav_channel_t diff;
669 GetWord( sum.i_predictor );
670 GetWord( diff.i_predictor );
671 GetByte( sum.i_step_index );
672 GetByte( diff.i_step_index );
674 i_diff_value = diff.i_predictor;
675 /* we process 6 nibbles at once */
676 while( p_buffer + 1 <= p_end )
678 /* first 3 nibbles */
679 AdpcmImaWavExpandNibble( &sum,
682 AdpcmImaWavExpandNibble( &diff,
685 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
687 *p_sample++ = sum.i_predictor + i_diff_value;
688 *p_sample++ = sum.i_predictor - i_diff_value;
692 AdpcmImaWavExpandNibble( &sum,
695 *p_sample++ = sum.i_predictor + i_diff_value;
696 *p_sample++ = sum.i_predictor - i_diff_value;
698 /* now last 3 nibbles */
699 AdpcmImaWavExpandNibble( &sum,
702 if( p_buffer < p_end )
704 AdpcmImaWavExpandNibble( &diff,
707 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
709 *p_sample++ = sum.i_predictor + i_diff_value;
710 *p_sample++ = sum.i_predictor - i_diff_value;
712 AdpcmImaWavExpandNibble( &sum,
716 *p_sample++ = sum.i_predictor + i_diff_value;
717 *p_sample++ = sum.i_predictor - i_diff_value;
727 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
730 static const uint32_t EATable[]=
732 0x00000000, 0x000000F0, 0x000001CC, 0x00000188,
733 0x00000000, 0x00000000, 0xFFFFFF30, 0xFFFFFF24,
734 0x00000000, 0x00000001, 0x00000003, 0x00000004,
735 0x00000007, 0x00000008, 0x0000000A, 0x0000000B,
736 0x00000000, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFC
738 decoder_sys_t *p_sys = p_dec->p_sys;
740 unsigned i_channels, c;
742 int32_t c1[MAX_CHAN], c2[MAX_CHAN];
745 i_channels = p_dec->fmt_in.audio.i_channels;
746 p_end = &p_buffer[p_sys->i_block];
748 prev = (int16_t *)p_dec->fmt_in.p_extra;
749 cur = prev + i_channels;
751 for (c = 0; c < i_channels; c++)
756 c1[c] = EATable[input >> 4];
757 c2[c] = EATable[(input >> 4) + 4];
758 d[c] = (input & 0xf) + 8;
761 for( p_buffer += i_channels; p_buffer < p_end ; p_buffer += i_channels)
763 for (c = 0; c < i_channels; c++)
767 spl = (p_buffer[c] >> 4) & 0xf;
768 spl = (spl << 0x1c) >> d[c];
769 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
770 CLAMP( spl, -32768, 32767 );
777 for (c = 0; c < i_channels; c++)
781 spl = p_buffer[c] & 0xf;
782 spl = (spl << 0x1c) >> d[c];
783 spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
784 CLAMP( spl, -32768, 32767 );