1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: adpcm.c,v 1.1 2002/12/03 17:00:16 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 *****************************************************************************/
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
34 #include <stdlib.h> /* malloc(), free() */
35 #include <string.h> /* strdup() */
37 /*****************************************************************************
39 *****************************************************************************/
41 #define ADPCM_IMA_QT 1
42 #define ADPCM_IMA_WAV 2
45 typedef struct adec_thread_s
51 /* The bit stream structure handles the PES stream at the bit level */
52 bit_stream_t bit_stream;
55 int i_samplesperblock;
57 /* Input properties */
58 decoder_fifo_t *p_fifo;
60 /* Output properties */
61 aout_instance_t * p_aout; /* opaque */
62 aout_input_t * p_aout_input; /* opaque */
63 audio_sample_format_t output_format;
70 static int OpenDecoder ( vlc_object_t * );
72 static int RunDecoder ( decoder_fifo_t * );
73 static int InitThread ( adec_thread_t * );
74 static void DecodeThread ( adec_thread_t * );
75 static void EndThread ( adec_thread_t * );
78 static void DecodeAdpcmMs( adec_thread_t *, aout_buffer_t * );
79 static void DecodeAdpcmImaWav( adec_thread_t *, aout_buffer_t * );
81 /*****************************************************************************
83 *****************************************************************************/
86 set_description( _("ADPCM audio deocder") );
87 set_capability( "decoder", 50 );
88 set_callbacks( OpenDecoder, NULL );
92 static int pi_channels_maps[6] =
96 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
97 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
98 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
99 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
100 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
103 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
104 static int i_index_table[16] =
106 -1, -1, -1, -1, 2, 4, 6, 8,
107 -1, -1, -1, -1, 2, 4, 6, 8
110 static int i_step_table[89] =
112 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
113 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
114 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
115 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
116 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
117 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
118 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
119 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
120 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
123 static int i_adaptation_table[16] =
125 230, 230, 230, 230, 307, 409, 512, 614,
126 768, 614, 512, 409, 307, 230, 230, 230
129 static int i_adaptation_coeff1[7] =
131 256, 512, 0, 192, 240, 460, 392
134 static int i_adaptation_coeff2[7] =
136 0, -256, 0, 64, 0, -208, -232
140 /*****************************************************************************
141 * OpenDecoder: probe the decoder and return score
142 *****************************************************************************
143 * Tries to launch a decoder and return score so that the interface is able
145 *****************************************************************************/
146 static int OpenDecoder( vlc_object_t *p_this )
148 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
150 switch( p_fifo->i_fourcc )
152 // case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
153 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
154 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
155 // case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
156 // case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
158 p_fifo->pf_run = RunDecoder;
167 /*****************************************************************************
168 * RunDecoder: this function is called just after the thread is created
169 *****************************************************************************/
170 static int RunDecoder( decoder_fifo_t *p_fifo )
172 adec_thread_t *p_adec;
175 if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
177 msg_Err( p_fifo, "out of memory" );
178 DecoderError( p_fifo );
181 memset( p_adec, 0, sizeof( adec_thread_t ) );
183 p_adec->p_fifo = p_fifo;
185 if( InitThread( p_adec ) != 0 )
187 DecoderError( p_fifo );
191 while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
193 DecodeThread( p_adec );
197 if( ( b_error = p_adec->p_fifo->b_error ) )
199 DecoderError( p_adec->p_fifo );
212 #define FREE( p ) if( p ) free( p ); p = NULL
213 #define GetWLE( p ) \
214 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
216 #define GetDWLE( p ) \
217 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
218 ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
220 /*****************************************************************************
221 * InitThread: initialize data before entering main loop
222 *****************************************************************************/
224 static int InitThread( adec_thread_t * p_adec )
226 if( !( p_adec->p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_demux_data ) )
228 msg_Err( p_adec->p_fifo, "missing format" );
231 /* fourcc to codec */
232 switch( p_adec->p_fifo->i_fourcc )
234 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
235 p_adec->i_codec = ADPCM_IMA_QT;
237 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
238 p_adec->i_codec = ADPCM_IMA_WAV;
240 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
241 p_adec->i_codec = ADPCM_MS;
243 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
244 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
249 if( p_adec->p_wf->nChannels < 1 ||
250 p_adec->p_wf->nChannels > 2 )
252 msg_Err( p_adec->p_fifo, "bad channels count(1-2)" );
255 if( !( p_adec->i_block = p_adec->p_wf->nBlockAlign ) )
257 if( p_adec->i_codec == ADPCM_IMA_QT )
259 p_adec->i_block = 34;
263 p_adec->i_block = 1024; // XXX FIXME
265 msg_Err( p_adec->p_fifo,
266 "block size undefined, using %d default",
269 p_adec->p_block = malloc( p_adec->i_block );
271 /* calculate samples per block */
272 switch( p_adec->i_codec )
275 p_adec->i_samplesperblock = 64;
278 p_adec->i_samplesperblock =
279 2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels )/
280 p_adec->p_wf->nChannels;
283 p_adec->i_samplesperblock =
284 2 * ( p_adec->i_block - 7 * p_adec->p_wf->nChannels ) /
285 p_adec->p_wf->nChannels + 2;
288 p_adec->i_samplesperblock = 0;
291 msg_Dbg( p_adec->p_fifo,
292 "format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d samplesperblock %d",
293 p_adec->p_wf->nSamplesPerSec,
294 p_adec->p_wf->nChannels,
295 p_adec->p_wf->wBitsPerSample,
296 p_adec->p_wf->nBlockAlign,
297 p_adec->i_samplesperblock );
299 //p_adec->output_format.i_format = VLC_FOURCC('s','1','6','l');
300 /* FIXME good way ? */
301 p_adec->output_format.i_format = AOUT_FMT_S16_NE;
302 p_adec->output_format.i_rate = p_adec->p_wf->nSamplesPerSec;
305 p_adec->output_format.i_physical_channels =
306 p_adec->output_format.i_original_channels =
307 pi_channels_maps[p_adec->p_wf->nChannels];
308 p_adec->p_aout = NULL;
309 p_adec->p_aout_input = NULL;
311 /* **** Create a new audio output **** */
312 aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
313 p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
315 &p_adec->output_format );
316 if( !p_adec->p_aout_input )
318 msg_Err( p_adec->p_fifo, "cannot create aout" );
322 /* Init the BitStream */
323 InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
330 /*****************************************************************************
331 * DecodeThread: decodes a frame
332 *****************************************************************************/
333 static void DecodeThread( adec_thread_t *p_adec )
335 aout_buffer_t *p_aout_buffer;
338 CurrentPTS( &p_adec->bit_stream, &p_adec->pts, NULL );
340 GetChunk( &p_adec->bit_stream,
344 /* get output buffer */
345 if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
347 aout_DateSet( &p_adec->date, p_adec->pts );
349 else if( !aout_DateGet( &p_adec->date ) )
354 p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
355 p_adec->p_aout_input,
356 p_adec->i_samplesperblock );
359 msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
360 p_adec->p_fifo->b_error = 1;
364 p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
365 p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
366 p_adec->i_samplesperblock );
370 switch( p_adec->i_codec )
375 DecodeAdpcmImaWav( p_adec, p_aout_buffer );
378 DecodeAdpcmMs( p_adec, p_aout_buffer );
384 /* **** Now we can output these samples **** */
385 aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
389 /*****************************************************************************
390 * EndThread : faad decoder thread destruction
391 *****************************************************************************/
392 static void EndThread (adec_thread_t *p_adec)
394 if( p_adec->p_aout_input )
396 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
399 msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
401 free( p_adec->p_block );
404 #define CLAMP( v, min, max ) \
405 if( (v) < (min) ) (v) = (min); \
406 if( (v) > (max) ) (v) = (max)
408 #define GetByte( v ) \
409 (v) = *p_buffer; p_buffer++;
411 #define GetWord( v ) \
412 (v) = *p_buffer; p_buffer++; \
413 (v) |= ( *p_buffer ) << 8; p_buffer++; \
414 if( (v)&0x8000 ) (v) -= 0x010000;
416 typedef struct adpcm_ms_channel_s
419 int i_sample1, i_sample2;
420 int i_coeff1, i_coeff2;
422 } adpcm_ms_channel_t;
425 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
432 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
434 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
435 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
436 i_snibble * p_channel->i_idelta;
438 CLAMP( i_predictor, -32768, 32767 );
440 p_channel->i_sample2 = p_channel->i_sample1;
441 p_channel->i_sample1 = i_predictor;
443 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
444 p_channel->i_idelta ) / 256;
445 if( p_channel->i_idelta < 16 )
447 p_channel->i_idelta = 16;
449 return( i_predictor );
452 static void DecodeAdpcmMs( adec_thread_t *p_adec,
453 aout_buffer_t *p_aout_buffer)
456 adpcm_ms_channel_t channel[2];
460 int i_block_predictor;
462 p_buffer = p_adec->p_block;
463 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
465 GetByte( i_block_predictor );
466 CLAMP( i_block_predictor, 0, 6 );
467 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
468 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
472 GetByte( i_block_predictor );
473 CLAMP( i_block_predictor, 0, 6 );
474 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
475 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
477 GetWord( channel[0].i_idelta );
480 GetWord( channel[1].i_idelta );
483 GetWord( channel[0].i_sample1 );
486 GetWord( channel[1].i_sample1 );
489 GetWord( channel[0].i_sample2 );
492 GetWord( channel[1].i_sample2 );
495 p_sample = (int16_t*)p_aout_buffer->p_buffer;
499 *p_sample = channel[0].i_sample2; p_sample++;
500 *p_sample = channel[1].i_sample2; p_sample++;
501 *p_sample = channel[0].i_sample1; p_sample++;
502 *p_sample = channel[1].i_sample1; p_sample++;
506 *p_sample = channel[0].i_sample2; p_sample++;
507 *p_sample = channel[0].i_sample1; p_sample++;
510 for( i_nibbles = 2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
511 i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
513 *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
516 *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
524 typedef struct adpcm_ima_wav_channel_s
529 } adpcm_ima_wav_channel_t;
531 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
536 i_diff = i_step_table[p_channel->i_step_index] >> 3;
537 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
538 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
539 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
541 p_channel->i_predictor -= i_diff;
543 p_channel->i_predictor += i_diff;
545 CLAMP( p_channel->i_predictor, -32768, 32767 );
547 p_channel->i_step_index += i_index_table[i_nibble];
549 CLAMP( p_channel->i_step_index, 0, 88 );
551 return( p_channel->i_predictor );
554 static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
555 aout_buffer_t *p_aout_buffer)
558 adpcm_ima_wav_channel_t channel[2];
563 p_buffer = p_adec->p_block;
564 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
566 GetWord( channel[0].i_predictor );
567 GetByte( channel[0].i_step_index );
568 CLAMP( channel[0].i_step_index, 0, 88 );
573 GetWord( channel[1].i_predictor );
574 GetByte( channel[1].i_step_index );
575 CLAMP( channel[1].i_step_index, 0, 88 );
579 p_sample = (int16_t*)p_aout_buffer->p_buffer;
582 for( i_nibbles = 2 * (p_adec->i_block - 8);
588 for( i = 0; i < 4; i++ )
591 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
592 p_sample[i * 4 + 2] =
593 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
597 for( i = 0; i < 4; i++ )
599 p_sample[i * 4 + 1] =
600 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
601 p_sample[i * 4 + 3] =
602 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
613 for( i_nibbles = 2 * (p_adec->i_block - 4);
615 i_nibbles -= 2, p_buffer++ )
617 *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
619 *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );