1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: adpcm.c,v 1.13 2003/09/02 20:19:25 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
47 typedef struct adec_thread_s
55 int i_samplesperblock;
57 uint8_t *p_buffer; /* buffer for gather pes */ \
58 int i_buffer; /* bytes present in p_buffer */
60 /* Input properties */
61 decoder_fifo_t *p_fifo;
63 /* Output properties */
64 aout_instance_t * p_aout; /* opaque */
65 aout_input_t * p_aout_input; /* opaque */
66 audio_sample_format_t output_format;
73 static int OpenDecoder ( vlc_object_t * );
75 static int RunDecoder ( decoder_fifo_t * );
76 static int InitThread ( adec_thread_t * );
77 static void DecodeThread ( adec_thread_t * );
78 static void EndThread ( adec_thread_t * );
81 static void DecodeAdpcmMs ( adec_thread_t *, aout_buffer_t * );
82 static void DecodeAdpcmImaWav ( adec_thread_t *, aout_buffer_t * );
83 static void DecodeAdpcmImaQT ( adec_thread_t *, aout_buffer_t * );
84 static void DecodeAdpcmDk4 ( adec_thread_t *, aout_buffer_t * );
85 static void DecodeAdpcmDk3 ( adec_thread_t *, aout_buffer_t * );
87 /*****************************************************************************
89 *****************************************************************************/
92 set_description( _("ADPCM audio decoder") );
93 set_capability( "decoder", 50 );
94 set_callbacks( OpenDecoder, NULL );
98 static int pi_channels_maps[6] =
102 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
103 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
105 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
106 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
109 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
110 static int i_index_table[16] =
112 -1, -1, -1, -1, 2, 4, 6, 8,
113 -1, -1, -1, -1, 2, 4, 6, 8
116 static int i_step_table[89] =
118 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
119 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
120 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
121 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
122 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
123 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
124 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
125 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
126 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
129 static int i_adaptation_table[16] =
131 230, 230, 230, 230, 307, 409, 512, 614,
132 768, 614, 512, 409, 307, 230, 230, 230
135 static int i_adaptation_coeff1[7] =
137 256, 512, 0, 192, 240, 460, 392
140 static int i_adaptation_coeff2[7] =
142 0, -256, 0, 64, 0, -208, -232
146 /*****************************************************************************
147 * OpenDecoder: probe the decoder and return score
148 *****************************************************************************
149 * Tries to launch a decoder and return score so that the interface is able
151 *****************************************************************************/
152 static int OpenDecoder( vlc_object_t *p_this )
154 decoder_t *p_dec = (decoder_t*)p_this;
156 switch( p_dec->p_fifo->i_fourcc )
158 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
159 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
160 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
161 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
162 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
164 p_dec->pf_run = RunDecoder;
172 /*****************************************************************************
173 * RunDecoder: this function is called just after the thread is created
174 *****************************************************************************/
175 static int RunDecoder( decoder_fifo_t *p_fifo )
177 adec_thread_t *p_adec;
180 if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
182 msg_Err( p_fifo, "out of memory" );
183 DecoderError( p_fifo );
186 memset( p_adec, 0, sizeof( adec_thread_t ) );
188 p_adec->p_fifo = p_fifo;
190 if( InitThread( p_adec ) != 0 )
192 DecoderError( p_fifo );
196 while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
198 DecodeThread( p_adec );
202 if( ( b_error = p_adec->p_fifo->b_error ) )
204 DecoderError( p_adec->p_fifo );
217 #define FREE( p ) if( p ) free( p ); p = NULL
219 /*****************************************************************************
220 * InitThread: initialize data before entering main loop
221 *****************************************************************************/
223 static int InitThread( adec_thread_t * p_adec )
225 if( ( p_adec->p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_waveformatex ) == NULL )
227 msg_Err( p_adec->p_fifo, "missing format" );
230 /* fourcc to codec */
231 switch( p_adec->p_fifo->i_fourcc )
233 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
234 p_adec->i_codec = ADPCM_IMA_QT;
236 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
237 p_adec->i_codec = ADPCM_IMA_WAV;
239 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
240 p_adec->i_codec = ADPCM_MS;
242 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
243 p_adec->i_codec = ADPCM_DK4;
245 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
246 p_adec->i_codec = ADPCM_DK3;
250 if( p_adec->p_wf->nChannels < 1 ||
251 p_adec->p_wf->nChannels > 2 )
253 msg_Err( p_adec->p_fifo, "bad channels count(1-2)" );
256 if( !( p_adec->i_block = p_adec->p_wf->nBlockAlign ) )
258 if( p_adec->i_codec == ADPCM_IMA_QT )
260 p_adec->i_block = 34 * p_adec->p_wf->nChannels;
264 p_adec->i_block = 1024; // XXX FIXME
266 msg_Warn( p_adec->p_fifo,
267 "block size undefined, using %d default",
270 p_adec->p_block = NULL;
272 /* calculate samples per block */
273 switch( p_adec->i_codec )
276 p_adec->i_samplesperblock = 64;
279 p_adec->i_samplesperblock =
280 2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels )/
281 p_adec->p_wf->nChannels;
284 p_adec->i_samplesperblock =
285 2 * ( p_adec->i_block - 7 * p_adec->p_wf->nChannels ) /
286 p_adec->p_wf->nChannels + 2;
289 p_adec->i_samplesperblock =
290 2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels ) /
291 p_adec->p_wf->nChannels + 1;
294 p_adec->p_wf->nChannels = 2;
295 p_adec->i_samplesperblock = ( 4 * ( p_adec->i_block - 16 ) + 2 )/ 3;
298 msg_Err( p_adec->p_fifo, "unknown adpcm variant" );
302 msg_Dbg( p_adec->p_fifo,
303 "format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d samplesperblock %d",
304 p_adec->p_wf->nSamplesPerSec,
305 p_adec->p_wf->nChannels,
306 p_adec->p_wf->wBitsPerSample,
307 p_adec->p_wf->nBlockAlign,
308 p_adec->i_samplesperblock );
310 //p_adec->output_format.i_format = VLC_FOURCC('s','1','6','l');
311 /* FIXME good way ? */
312 p_adec->output_format.i_format = AOUT_FMT_S16_NE;
313 p_adec->output_format.i_rate = p_adec->p_wf->nSamplesPerSec;
316 p_adec->output_format.i_physical_channels =
317 p_adec->output_format.i_original_channels =
318 pi_channels_maps[p_adec->p_wf->nChannels];
319 p_adec->p_aout = NULL;
320 p_adec->p_aout_input = NULL;
322 /* **** Create a new audio output **** */
323 aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
324 p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
326 &p_adec->output_format );
327 if( !p_adec->p_aout_input )
329 msg_Err( p_adec->p_fifo, "cannot create aout" );
333 /* Init the BitStream */
334 // InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
341 static void GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
346 data_packet_t *p_data;
349 p_data = p_pes->p_first;
350 while( p_data != NULL && i_count < i_max )
353 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
359 p_data->p_payload_start,
363 p_data = p_data->p_next;
368 if( i_count < i_max )
370 memset( p_buf, 0, i_max - i_count );
374 /*****************************************************************************
375 * DecodeThread: decodes a frame
376 *****************************************************************************/
377 static void DecodeThread( adec_thread_t *p_adec )
379 aout_buffer_t *p_aout_buffer;
384 /* **** Get a new frames from streams **** */
387 input_ExtractPES( p_adec->p_fifo, &p_pes );
390 p_adec->p_fifo->b_error = 1;
393 if( p_pes->i_pts != 0 )
395 p_adec->pts = p_pes->i_pts;
397 i_frame_size = p_pes->i_pes_size;
399 if( i_frame_size > 0 )
401 if( p_adec->i_buffer < i_frame_size + 16 )
403 FREE( p_adec->p_buffer );
404 p_adec->p_buffer = malloc( i_frame_size + 16 );
405 p_adec->i_buffer = i_frame_size + 16;
408 GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
410 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
412 } while( i_frame_size <= 0 );
414 for( p_adec->p_block = p_adec->p_buffer;
415 i_frame_size >= p_adec->i_block;
416 p_adec->p_block += p_adec->i_block, i_frame_size -= p_adec->i_block )
418 /* get output buffer */
419 if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
421 aout_DateSet( &p_adec->date, p_adec->pts );
423 else if( !aout_DateGet( &p_adec->date ) )
429 p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
430 p_adec->p_aout_input,
431 p_adec->i_samplesperblock );
434 msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
435 p_adec->p_fifo->b_error = 1;
439 p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
440 p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
441 p_adec->i_samplesperblock );
445 switch( p_adec->i_codec )
448 DecodeAdpcmImaQT( p_adec, p_aout_buffer );
451 DecodeAdpcmImaWav( p_adec, p_aout_buffer );
454 DecodeAdpcmMs( p_adec, p_aout_buffer );
457 DecodeAdpcmDk4( p_adec, p_aout_buffer );
459 DecodeAdpcmDk3( p_adec, p_aout_buffer );
465 /* **** Now we can output these samples **** */
466 aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
471 /*****************************************************************************
472 * EndThread : faad decoder thread destruction
473 *****************************************************************************/
474 static void EndThread (adec_thread_t *p_adec)
476 if( p_adec->p_aout_input )
478 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
481 msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
483 FREE( p_adec->p_buffer );
486 #define CLAMP( v, min, max ) \
487 if( (v) < (min) ) (v) = (min); \
488 if( (v) > (max) ) (v) = (max)
490 #define GetByte( v ) \
491 (v) = *p_buffer; p_buffer++;
493 #define GetWord( v ) \
494 (v) = *p_buffer; p_buffer++; \
495 (v) |= ( *p_buffer ) << 8; p_buffer++; \
496 if( (v)&0x8000 ) (v) -= 0x010000;
501 typedef struct adpcm_ms_channel_s
504 int i_sample1, i_sample2;
505 int i_coeff1, i_coeff2;
507 } adpcm_ms_channel_t;
510 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
517 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
519 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
520 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
521 i_snibble * p_channel->i_idelta;
523 CLAMP( i_predictor, -32768, 32767 );
525 p_channel->i_sample2 = p_channel->i_sample1;
526 p_channel->i_sample1 = i_predictor;
528 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
529 p_channel->i_idelta ) / 256;
530 if( p_channel->i_idelta < 16 )
532 p_channel->i_idelta = 16;
534 return( i_predictor );
537 static void DecodeAdpcmMs( adec_thread_t *p_adec,
538 aout_buffer_t *p_aout_buffer)
541 adpcm_ms_channel_t channel[2];
545 int i_block_predictor;
547 p_buffer = p_adec->p_block;
548 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
550 GetByte( i_block_predictor );
551 CLAMP( i_block_predictor, 0, 6 );
552 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
553 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
557 GetByte( i_block_predictor );
558 CLAMP( i_block_predictor, 0, 6 );
559 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
560 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
562 GetWord( channel[0].i_idelta );
565 GetWord( channel[1].i_idelta );
568 GetWord( channel[0].i_sample1 );
571 GetWord( channel[1].i_sample1 );
574 GetWord( channel[0].i_sample2 );
577 GetWord( channel[1].i_sample2 );
580 p_sample = (int16_t*)p_aout_buffer->p_buffer;
584 *p_sample = channel[0].i_sample2; p_sample++;
585 *p_sample = channel[1].i_sample2; p_sample++;
586 *p_sample = channel[0].i_sample1; p_sample++;
587 *p_sample = channel[1].i_sample1; p_sample++;
591 *p_sample = channel[0].i_sample2; p_sample++;
592 *p_sample = channel[0].i_sample1; p_sample++;
595 for( i_nibbles = 2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
596 i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
598 *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
601 *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
612 typedef struct adpcm_ima_wav_channel_s
617 } adpcm_ima_wav_channel_t;
619 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
624 i_diff = i_step_table[p_channel->i_step_index] >> 3;
625 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
626 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
627 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
629 p_channel->i_predictor -= i_diff;
631 p_channel->i_predictor += i_diff;
633 CLAMP( p_channel->i_predictor, -32768, 32767 );
635 p_channel->i_step_index += i_index_table[i_nibble];
637 CLAMP( p_channel->i_step_index, 0, 88 );
639 return( p_channel->i_predictor );
642 static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
643 aout_buffer_t *p_aout_buffer)
646 adpcm_ima_wav_channel_t channel[2];
651 p_buffer = p_adec->p_block;
652 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
654 GetWord( channel[0].i_predictor );
655 GetByte( channel[0].i_step_index );
656 CLAMP( channel[0].i_step_index, 0, 88 );
661 GetWord( channel[1].i_predictor );
662 GetByte( channel[1].i_step_index );
663 CLAMP( channel[1].i_step_index, 0, 88 );
667 p_sample = (int16_t*)p_aout_buffer->p_buffer;
670 for( i_nibbles = 2 * (p_adec->i_block - 8);
676 for( i = 0; i < 4; i++ )
679 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
680 p_sample[i * 4 + 2] =
681 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
685 for( i = 0; i < 4; i++ )
687 p_sample[i * 4 + 1] =
688 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
689 p_sample[i * 4 + 3] =
690 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
701 for( i_nibbles = 2 * (p_adec->i_block - 4);
703 i_nibbles -= 2, p_buffer++ )
705 *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
707 *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
716 static void DecodeAdpcmImaQT( adec_thread_t *p_adec,
717 aout_buffer_t *p_aout_buffer )
720 adpcm_ima_wav_channel_t channel[2];
726 p_buffer = p_adec->p_block;
727 i_step = p_adec->p_wf->nChannels;
729 for( i_ch = 0; i_ch < p_adec->p_wf->nChannels; i_ch++ )
731 p_sample = ((int16_t*)p_aout_buffer->p_buffer) + i_ch;
733 channel[i_ch].i_predictor = (int16_t)((( ( p_buffer[0] << 1 )|( p_buffer[1] >> 7 ) ))<<7);
734 channel[i_ch].i_step_index = p_buffer[1]&0x7f;
736 CLAMP( channel[i_ch].i_step_index, 0, 88 );
739 for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
741 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
744 *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
756 static void DecodeAdpcmDk4( adec_thread_t *p_adec,
757 aout_buffer_t *p_aout_buffer)
760 adpcm_ima_wav_channel_t channel[2];
765 p_buffer = p_adec->p_block;
766 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
768 GetWord( channel[0].i_predictor );
769 GetByte( channel[0].i_step_index );
770 CLAMP( channel[0].i_step_index, 0, 88 );
775 GetWord( channel[1].i_predictor );
776 GetByte( channel[1].i_step_index );
777 CLAMP( channel[1].i_step_index, 0, 88 );
781 p_sample = (int16_t*)p_aout_buffer->p_buffer;
783 /* first output predictor */
784 *p_sample++ = channel[0].i_predictor;
787 *p_sample++ = channel[1].i_predictor;
791 i_nibbles < p_adec->i_block - 4 * (b_stereo ? 2:1 );
794 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
796 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
807 static void DecodeAdpcmDk3( adec_thread_t *p_adec,
808 aout_buffer_t *p_aout_buffer)
810 uint8_t *p_buffer, *p_end;
811 adpcm_ima_wav_channel_t sum;
812 adpcm_ima_wav_channel_t diff;
816 p_buffer = p_adec->p_block;
817 p_end = p_buffer + p_adec->i_block;
820 GetWord( sum.i_predictor );
821 GetWord( diff.i_predictor );
822 GetByte( sum.i_step_index );
823 GetByte( diff.i_step_index );
825 p_sample = (int16_t*)p_aout_buffer->p_buffer;
826 i_diff_value = diff.i_predictor;
827 /* we process 6 nibbles at once */
828 //for( i_group = 0; i_group < ( p_adec->i_block -16 ) / 3; i_group++ )
829 while( p_buffer + 1 <= p_end )
831 /* first 3 nibbles */
832 AdpcmImaWavExpandNibble( &sum,
835 AdpcmImaWavExpandNibble( &diff,
838 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
840 *p_sample++ = sum.i_predictor + i_diff_value;
841 *p_sample++ = sum.i_predictor - i_diff_value;
845 AdpcmImaWavExpandNibble( &sum,
848 *p_sample++ = sum.i_predictor + i_diff_value;
849 *p_sample++ = sum.i_predictor - i_diff_value;
851 /* now last 3 nibbles */
852 AdpcmImaWavExpandNibble( &sum,
855 if( p_buffer < p_end )
857 AdpcmImaWavExpandNibble( &diff,
860 i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
862 *p_sample++ = sum.i_predictor + i_diff_value;
863 *p_sample++ = sum.i_predictor - i_diff_value;
865 AdpcmImaWavExpandNibble( &sum,
869 *p_sample++ = sum.i_predictor + i_diff_value;
870 *p_sample++ = sum.i_predictor - i_diff_value;