1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: adpcm.c,v 1.3 2002/12/30 17:28:31 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 *****************************************************************************/
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->output_format.i_bytes_per_frame =
309 p_adec->p_wf->nChannels * 2;
310 p_adec->output_format.i_frame_length = 1;
311 p_adec->p_aout = NULL;
312 p_adec->p_aout_input = NULL;
314 /* **** Create a new audio output **** */
315 aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
316 p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
318 &p_adec->output_format );
319 if( !p_adec->p_aout_input )
321 msg_Err( p_adec->p_fifo, "cannot create aout" );
325 /* Init the BitStream */
326 InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
333 /*****************************************************************************
334 * DecodeThread: decodes a frame
335 *****************************************************************************/
336 static void DecodeThread( adec_thread_t *p_adec )
338 aout_buffer_t *p_aout_buffer;
341 CurrentPTS( &p_adec->bit_stream, &p_adec->pts, NULL );
343 GetChunk( &p_adec->bit_stream,
347 /* get output buffer */
348 if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
350 aout_DateSet( &p_adec->date, p_adec->pts );
352 else if( !aout_DateGet( &p_adec->date ) )
357 p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
358 p_adec->p_aout_input,
359 p_adec->i_samplesperblock );
362 msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
363 p_adec->p_fifo->b_error = 1;
367 p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
368 p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
369 p_adec->i_samplesperblock );
373 switch( p_adec->i_codec )
378 DecodeAdpcmImaWav( p_adec, p_aout_buffer );
381 DecodeAdpcmMs( p_adec, p_aout_buffer );
388 /* **** Now we can output these samples **** */
389 aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
393 /*****************************************************************************
394 * EndThread : faad decoder thread destruction
395 *****************************************************************************/
396 static void EndThread (adec_thread_t *p_adec)
398 if( p_adec->p_aout_input )
400 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
403 msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
405 free( p_adec->p_block );
408 #define CLAMP( v, min, max ) \
409 if( (v) < (min) ) (v) = (min); \
410 if( (v) > (max) ) (v) = (max)
412 #define GetByte( v ) \
413 (v) = *p_buffer; p_buffer++;
415 #define GetWord( v ) \
416 (v) = *p_buffer; p_buffer++; \
417 (v) |= ( *p_buffer ) << 8; p_buffer++; \
418 if( (v)&0x8000 ) (v) -= 0x010000;
420 typedef struct adpcm_ms_channel_s
423 int i_sample1, i_sample2;
424 int i_coeff1, i_coeff2;
426 } adpcm_ms_channel_t;
429 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
436 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
438 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
439 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
440 i_snibble * p_channel->i_idelta;
442 CLAMP( i_predictor, -32768, 32767 );
444 p_channel->i_sample2 = p_channel->i_sample1;
445 p_channel->i_sample1 = i_predictor;
447 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
448 p_channel->i_idelta ) / 256;
449 if( p_channel->i_idelta < 16 )
451 p_channel->i_idelta = 16;
453 return( i_predictor );
456 static void DecodeAdpcmMs( adec_thread_t *p_adec,
457 aout_buffer_t *p_aout_buffer)
460 adpcm_ms_channel_t channel[2];
464 int i_block_predictor;
466 p_buffer = p_adec->p_block;
467 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
469 GetByte( i_block_predictor );
470 CLAMP( i_block_predictor, 0, 6 );
471 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
472 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
476 GetByte( i_block_predictor );
477 CLAMP( i_block_predictor, 0, 6 );
478 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
479 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
481 GetWord( channel[0].i_idelta );
484 GetWord( channel[1].i_idelta );
487 GetWord( channel[0].i_sample1 );
490 GetWord( channel[1].i_sample1 );
493 GetWord( channel[0].i_sample2 );
496 GetWord( channel[1].i_sample2 );
499 p_sample = (int16_t*)p_aout_buffer->p_buffer;
503 *p_sample = channel[0].i_sample2; p_sample++;
504 *p_sample = channel[1].i_sample2; p_sample++;
505 *p_sample = channel[0].i_sample1; p_sample++;
506 *p_sample = channel[1].i_sample1; p_sample++;
510 *p_sample = channel[0].i_sample2; p_sample++;
511 *p_sample = channel[0].i_sample1; p_sample++;
514 for( i_nibbles = 2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
515 i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
517 *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
520 *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
528 typedef struct adpcm_ima_wav_channel_s
533 } adpcm_ima_wav_channel_t;
535 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
540 i_diff = i_step_table[p_channel->i_step_index] >> 3;
541 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
542 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
543 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
545 p_channel->i_predictor -= i_diff;
547 p_channel->i_predictor += i_diff;
549 CLAMP( p_channel->i_predictor, -32768, 32767 );
551 p_channel->i_step_index += i_index_table[i_nibble];
553 CLAMP( p_channel->i_step_index, 0, 88 );
555 return( p_channel->i_predictor );
558 static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
559 aout_buffer_t *p_aout_buffer)
562 adpcm_ima_wav_channel_t channel[2];
567 p_buffer = p_adec->p_block;
568 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
570 GetWord( channel[0].i_predictor );
571 GetByte( channel[0].i_step_index );
572 CLAMP( channel[0].i_step_index, 0, 88 );
577 GetWord( channel[1].i_predictor );
578 GetByte( channel[1].i_step_index );
579 CLAMP( channel[1].i_step_index, 0, 88 );
583 p_sample = (int16_t*)p_aout_buffer->p_buffer;
586 for( i_nibbles = 2 * (p_adec->i_block - 8);
592 for( i = 0; i < 4; i++ )
595 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
596 p_sample[i * 4 + 2] =
597 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
601 for( i = 0; i < 4; i++ )
603 p_sample[i * 4 + 1] =
604 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
605 p_sample[i * 4 + 3] =
606 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
617 for( i_nibbles = 2 * (p_adec->i_block - 4);
619 i_nibbles -= 2, p_buffer++ )
621 *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
623 *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );