1 /*****************************************************************************
2 * adpcm.c : adpcm variant audio decoder
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: adpcm.c,v 1.7 2003/02/16 09:50:22 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
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 DecodeAdpcmDk4( adec_thread_t *, aout_buffer_t * );
85 /*****************************************************************************
87 *****************************************************************************/
90 set_description( _("ADPCM audio deocder") );
91 set_capability( "decoder", 50 );
92 set_callbacks( OpenDecoder, NULL );
96 static int pi_channels_maps[6] =
100 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
101 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
102 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
103 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
104 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
107 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
108 static int i_index_table[16] =
110 -1, -1, -1, -1, 2, 4, 6, 8,
111 -1, -1, -1, -1, 2, 4, 6, 8
114 static int i_step_table[89] =
116 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
117 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
118 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
119 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
120 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
121 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
122 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
123 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
124 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
127 static int i_adaptation_table[16] =
129 230, 230, 230, 230, 307, 409, 512, 614,
130 768, 614, 512, 409, 307, 230, 230, 230
133 static int i_adaptation_coeff1[7] =
135 256, 512, 0, 192, 240, 460, 392
138 static int i_adaptation_coeff2[7] =
140 0, -256, 0, 64, 0, -208, -232
144 /*****************************************************************************
145 * OpenDecoder: probe the decoder and return score
146 *****************************************************************************
147 * Tries to launch a decoder and return score so that the interface is able
149 *****************************************************************************/
150 static int OpenDecoder( vlc_object_t *p_this )
152 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
154 switch( p_fifo->i_fourcc )
156 // case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
157 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
158 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
159 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
160 // case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
162 p_fifo->pf_run = RunDecoder;
171 /*****************************************************************************
172 * RunDecoder: this function is called just after the thread is created
173 *****************************************************************************/
174 static int RunDecoder( decoder_fifo_t *p_fifo )
176 adec_thread_t *p_adec;
179 if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
181 msg_Err( p_fifo, "out of memory" );
182 DecoderError( p_fifo );
185 memset( p_adec, 0, sizeof( adec_thread_t ) );
187 p_adec->p_fifo = p_fifo;
189 if( InitThread( p_adec ) != 0 )
191 DecoderError( p_fifo );
195 while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
197 DecodeThread( p_adec );
201 if( ( b_error = p_adec->p_fifo->b_error ) )
203 DecoderError( p_adec->p_fifo );
216 #define FREE( p ) if( p ) free( p ); p = NULL
217 #define GetWLE( p ) \
218 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
220 #define GetDWLE( p ) \
221 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
222 ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
224 /*****************************************************************************
225 * InitThread: initialize data before entering main loop
226 *****************************************************************************/
228 static int InitThread( adec_thread_t * p_adec )
230 if( ( p_adec->p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_waveformatex ) == NULL )
232 msg_Err( p_adec->p_fifo, "missing format" );
235 /* fourcc to codec */
236 switch( p_adec->p_fifo->i_fourcc )
238 case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
239 p_adec->i_codec = ADPCM_IMA_QT;
241 case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
242 p_adec->i_codec = ADPCM_IMA_WAV;
244 case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
245 p_adec->i_codec = ADPCM_MS;
247 case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
248 p_adec->i_codec = ADPCM_DK4;
250 case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
251 p_adec->i_codec = ADPCM_DK3;
255 if( p_adec->p_wf->nChannels < 1 ||
256 p_adec->p_wf->nChannels > 2 )
258 msg_Err( p_adec->p_fifo, "bad channels count(1-2)" );
261 if( !( p_adec->i_block = p_adec->p_wf->nBlockAlign ) )
263 if( p_adec->i_codec == ADPCM_IMA_QT )
265 p_adec->i_block = 34;
269 p_adec->i_block = 1024; // XXX FIXME
271 msg_Err( p_adec->p_fifo,
272 "block size undefined, using %d default",
275 p_adec->p_block = NULL;
277 /* calculate samples per block */
278 switch( p_adec->i_codec )
281 p_adec->i_samplesperblock = 64;
284 p_adec->i_samplesperblock =
285 2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels )/
286 p_adec->p_wf->nChannels;
289 p_adec->i_samplesperblock =
290 2 * ( p_adec->i_block - 7 * p_adec->p_wf->nChannels ) /
291 p_adec->p_wf->nChannels + 2;
294 p_adec->i_samplesperblock =
295 2 * ( p_adec->i_block - 4 * p_adec->p_wf->nChannels ) /
296 p_adec->p_wf->nChannels + 1;
299 p_adec->i_samplesperblock = 0;
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 )
450 DecodeAdpcmImaWav( p_adec, p_aout_buffer );
453 DecodeAdpcmMs( p_adec, p_aout_buffer );
456 DecodeAdpcmDk4( p_adec, p_aout_buffer );
462 /* **** Now we can output these samples **** */
463 aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
468 /*****************************************************************************
469 * EndThread : faad decoder thread destruction
470 *****************************************************************************/
471 static void EndThread (adec_thread_t *p_adec)
473 if( p_adec->p_aout_input )
475 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
478 msg_Dbg( p_adec->p_fifo, "adpcm audio decoder closed" );
480 FREE( p_adec->p_buffer );
483 #define CLAMP( v, min, max ) \
484 if( (v) < (min) ) (v) = (min); \
485 if( (v) > (max) ) (v) = (max)
487 #define GetByte( v ) \
488 (v) = *p_buffer; p_buffer++;
490 #define GetWord( v ) \
491 (v) = *p_buffer; p_buffer++; \
492 (v) |= ( *p_buffer ) << 8; p_buffer++; \
493 if( (v)&0x8000 ) (v) -= 0x010000;
498 typedef struct adpcm_ms_channel_s
501 int i_sample1, i_sample2;
502 int i_coeff1, i_coeff2;
504 } adpcm_ms_channel_t;
507 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
514 i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
516 i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
517 p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
518 i_snibble * p_channel->i_idelta;
520 CLAMP( i_predictor, -32768, 32767 );
522 p_channel->i_sample2 = p_channel->i_sample1;
523 p_channel->i_sample1 = i_predictor;
525 p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
526 p_channel->i_idelta ) / 256;
527 if( p_channel->i_idelta < 16 )
529 p_channel->i_idelta = 16;
531 return( i_predictor );
534 static void DecodeAdpcmMs( adec_thread_t *p_adec,
535 aout_buffer_t *p_aout_buffer)
538 adpcm_ms_channel_t channel[2];
542 int i_block_predictor;
544 p_buffer = p_adec->p_block;
545 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
547 GetByte( i_block_predictor );
548 CLAMP( i_block_predictor, 0, 6 );
549 channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
550 channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
554 GetByte( i_block_predictor );
555 CLAMP( i_block_predictor, 0, 6 );
556 channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
557 channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
559 GetWord( channel[0].i_idelta );
562 GetWord( channel[1].i_idelta );
565 GetWord( channel[0].i_sample1 );
568 GetWord( channel[1].i_sample1 );
571 GetWord( channel[0].i_sample2 );
574 GetWord( channel[1].i_sample2 );
577 p_sample = (int16_t*)p_aout_buffer->p_buffer;
581 *p_sample = channel[0].i_sample2; p_sample++;
582 *p_sample = channel[1].i_sample2; p_sample++;
583 *p_sample = channel[0].i_sample1; p_sample++;
584 *p_sample = channel[1].i_sample1; p_sample++;
588 *p_sample = channel[0].i_sample2; p_sample++;
589 *p_sample = channel[0].i_sample1; p_sample++;
592 for( i_nibbles = 2 *( p_adec->i_block - 7 * p_adec->p_wf->nChannels );
593 i_nibbles > 0; i_nibbles -= 2,p_buffer++ )
595 *p_sample = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
598 *p_sample = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
609 typedef struct adpcm_ima_wav_channel_s
614 } adpcm_ima_wav_channel_t;
616 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
621 i_diff = i_step_table[p_channel->i_step_index] >> 3;
622 if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
623 if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
624 if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
626 p_channel->i_predictor -= i_diff;
628 p_channel->i_predictor += i_diff;
630 CLAMP( p_channel->i_predictor, -32768, 32767 );
632 p_channel->i_step_index += i_index_table[i_nibble];
634 CLAMP( p_channel->i_step_index, 0, 88 );
636 return( p_channel->i_predictor );
639 static void DecodeAdpcmImaWav( adec_thread_t *p_adec,
640 aout_buffer_t *p_aout_buffer)
643 adpcm_ima_wav_channel_t channel[2];
648 p_buffer = p_adec->p_block;
649 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
651 GetWord( channel[0].i_predictor );
652 GetByte( channel[0].i_step_index );
653 CLAMP( channel[0].i_step_index, 0, 88 );
658 GetWord( channel[1].i_predictor );
659 GetByte( channel[1].i_step_index );
660 CLAMP( channel[1].i_step_index, 0, 88 );
664 p_sample = (int16_t*)p_aout_buffer->p_buffer;
667 for( i_nibbles = 2 * (p_adec->i_block - 8);
673 for( i = 0; i < 4; i++ )
676 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
677 p_sample[i * 4 + 2] =
678 AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
682 for( i = 0; i < 4; i++ )
684 p_sample[i * 4 + 1] =
685 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
686 p_sample[i * 4 + 3] =
687 AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
698 for( i_nibbles = 2 * (p_adec->i_block - 4);
700 i_nibbles -= 2, p_buffer++ )
702 *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
704 *p_sample =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
714 static void DecodeAdpcmDk4( adec_thread_t *p_adec,
715 aout_buffer_t *p_aout_buffer)
718 adpcm_ima_wav_channel_t channel[2];
723 p_buffer = p_adec->p_block;
724 b_stereo = p_adec->p_wf->nChannels == 2 ? 1 : 0;
726 GetWord( channel[0].i_predictor );
727 GetByte( channel[0].i_step_index );
728 CLAMP( channel[0].i_step_index, 0, 88 );
733 GetWord( channel[1].i_predictor );
734 GetByte( channel[1].i_step_index );
735 CLAMP( channel[1].i_step_index, 0, 88 );
739 p_sample = (int16_t*)p_aout_buffer->p_buffer;
741 /* first output predictor */
742 *p_sample++ = channel[0].i_predictor;
745 *p_sample++ = channel[1].i_predictor;
749 i_nibbles < p_adec->i_block - 4 * (b_stereo ? 2:1 );
752 *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
754 *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],