Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

adpcm.c

00001 /*****************************************************************************
00002  * adpcm.c : adpcm variant audio decoder
00003  *****************************************************************************
00004  * Copyright (C) 2001, 2002 the VideoLAN team
00005  * $Id: adpcm.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *          Remi Denis-Courmont <rem # videolan.org>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *
00028  * Documentation: http://www.pcisys.net/~melanson/codecs/adpcm.txt
00029  *****************************************************************************/
00030 #include <vlc/vlc.h>
00031 #include <vlc/decoder.h>
00032 
00033 /*****************************************************************************
00034  * Module descriptor
00035  *****************************************************************************/
00036 static int  OpenDecoder( vlc_object_t * );
00037 static void CloseDecoder( vlc_object_t * );
00038 
00039 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
00040 
00041 vlc_module_begin();
00042     set_description( _("ADPCM audio decoder") );
00043     set_capability( "decoder", 50 );
00044     set_category( CAT_INPUT );
00045     set_subcategory( SUBCAT_INPUT_ACODEC );
00046     set_callbacks( OpenDecoder, CloseDecoder );
00047 vlc_module_end();
00048 
00049 /*****************************************************************************
00050  * Local prototypes
00051  *****************************************************************************/
00052 enum adpcm_codec_e
00053 {
00054     ADPCM_IMA_QT,
00055     ADPCM_IMA_WAV,
00056     ADPCM_MS,
00057     ADPCM_DK3,
00058     ADPCM_DK4,
00059     ADPCM_EA
00060 };
00061 
00062 struct decoder_sys_t
00063 {
00064     enum adpcm_codec_e codec;
00065 
00066     int                 i_block;
00067     int                 i_samplesperblock;
00068 
00069     audio_date_t        end_date;
00070 };
00071 
00072 static void DecodeAdpcmMs    ( decoder_t *, int16_t *, uint8_t * );
00073 static void DecodeAdpcmImaWav( decoder_t *, int16_t *, uint8_t * );
00074 static void DecodeAdpcmImaQT ( decoder_t *, int16_t *, uint8_t * );
00075 static void DecodeAdpcmDk4   ( decoder_t *, int16_t *, uint8_t * );
00076 static void DecodeAdpcmDk3   ( decoder_t *, int16_t *, uint8_t * );
00077 static void DecodeAdpcmEA    ( decoder_t *, int16_t *, uint8_t * );
00078 
00079 static int pi_channels_maps[6] =
00080 {
00081     0,
00082     AOUT_CHAN_CENTER,
00083     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
00084     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER,
00085     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT,
00086     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
00087      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
00088 };
00089 
00090 /* Various table from http://www.pcisys.net/~melanson/codecs/adpcm.txt */
00091 static int i_index_table[16] =
00092 {
00093     -1, -1, -1, -1, 2, 4, 6, 8,
00094     -1, -1, -1, -1, 2, 4, 6, 8
00095 };
00096 
00097 static int i_step_table[89] =
00098 {
00099     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
00100     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
00101     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
00102     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
00103     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
00104     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
00105     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
00106     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
00107     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
00108 };
00109 
00110 static int i_adaptation_table[16] =
00111 {
00112     230, 230, 230, 230, 307, 409, 512, 614,
00113     768, 614, 512, 409, 307, 230, 230, 230
00114 };
00115 
00116 static int i_adaptation_coeff1[7] =
00117 {
00118     256, 512, 0, 192, 240, 460, 392
00119 };
00120 
00121 static int i_adaptation_coeff2[7] =
00122 {
00123     0, -256, 0, 64, 0, -208, -232
00124 };
00125 
00126 /*****************************************************************************
00127  * OpenDecoder: probe the decoder and return score
00128  *****************************************************************************/
00129 static int OpenDecoder( vlc_object_t *p_this )
00130 {
00131     decoder_t *p_dec = (decoder_t*)p_this;
00132     decoder_sys_t *p_sys;
00133 
00134     switch( p_dec->fmt_in.i_codec )
00135     {
00136         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
00137         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
00138         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
00139         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
00140         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
00141         case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
00142             break;
00143         default:
00144             return VLC_EGENERIC;
00145     }
00146 
00147     if( p_dec->fmt_in.audio.i_channels <= 0 ||
00148         p_dec->fmt_in.audio.i_channels > 5 )
00149     {
00150         msg_Err( p_dec, "bad channels count(1-5)" );
00151         return VLC_EGENERIC;
00152     }
00153 
00154     if( p_dec->fmt_in.audio.i_rate <= 0 )
00155     {
00156         msg_Err( p_dec, "bad samplerate" );
00157         return VLC_EGENERIC;
00158     }
00159 
00160     /* Allocate the memory needed to store the decoder's structure */
00161     if( ( p_dec->p_sys = p_sys =
00162           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
00163     {
00164         msg_Err( p_dec, "out of memory" );
00165         return VLC_ENOMEM;
00166     }
00167 
00168     switch( p_dec->fmt_in.i_codec )
00169     {
00170         case VLC_FOURCC('i','m','a', '4'): /* IMA ADPCM */
00171             p_sys->codec = ADPCM_IMA_QT;
00172             break;
00173         case VLC_FOURCC('m','s',0x00,0x11): /* IMA ADPCM */
00174             p_sys->codec = ADPCM_IMA_WAV;
00175             break;
00176         case VLC_FOURCC('m','s',0x00,0x02): /* MS ADPCM */
00177             p_sys->codec = ADPCM_MS;
00178             break;
00179         case VLC_FOURCC('m','s',0x00,0x61): /* Duck DK4 ADPCM */
00180             p_sys->codec = ADPCM_DK4;
00181             break;
00182         case VLC_FOURCC('m','s',0x00,0x62): /* Duck DK3 ADPCM */
00183             p_sys->codec = ADPCM_DK3;
00184             break;
00185         case VLC_FOURCC('X','A','J', 0): /* EA ADPCM */
00186             p_sys->codec = ADPCM_EA;
00187             p_dec->fmt_in.p_extra = calloc( 2 * p_dec->fmt_in.audio.i_channels,
00188                                             sizeof( int16_t ) );
00189             if( p_dec->fmt_in.p_extra == NULL )
00190             {
00191                 free( p_sys );
00192                 return VLC_ENOMEM;
00193             }
00194             break;
00195     }
00196 
00197     if( p_dec->fmt_in.audio.i_blockalign <= 0 )
00198     {
00199         p_sys->i_block = (p_sys->codec == ADPCM_IMA_QT) ?
00200             34 * p_dec->fmt_in.audio.i_channels : 1024;
00201         msg_Warn( p_dec, "block size undefined, using %d", p_sys->i_block );
00202     }
00203     else
00204     {
00205         p_sys->i_block = p_dec->fmt_in.audio.i_blockalign;
00206     }
00207 
00208     /* calculate samples per block */
00209     switch( p_sys->codec )
00210     {
00211     case ADPCM_IMA_QT:
00212         p_sys->i_samplesperblock = 64;
00213         break;
00214     case ADPCM_IMA_WAV:
00215         p_sys->i_samplesperblock =
00216             2 * ( p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels ) /
00217             p_dec->fmt_in.audio.i_channels;
00218         break;
00219     case ADPCM_MS:
00220         p_sys->i_samplesperblock =
00221             2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels) /
00222             p_dec->fmt_in.audio.i_channels + 2;
00223         break;
00224     case ADPCM_DK4:
00225         p_sys->i_samplesperblock =
00226             2 * (p_sys->i_block - 4 * p_dec->fmt_in.audio.i_channels) /
00227             p_dec->fmt_in.audio.i_channels + 1;
00228         break;
00229     case ADPCM_DK3:
00230         p_dec->fmt_in.audio.i_channels = 2;
00231         p_sys->i_samplesperblock = ( 4 * ( p_sys->i_block - 16 ) + 2 )/ 3;
00232         break;
00233     case ADPCM_EA:
00234         p_sys->i_samplesperblock =
00235             2 * (p_sys->i_block - p_dec->fmt_in.audio.i_channels) /
00236             p_dec->fmt_in.audio.i_channels;
00237     }
00238 
00239     msg_Dbg( p_dec, "format: samplerate:%dHz channels:%d bits/sample:%d "
00240              "blockalign:%d samplesperblock:%d",
00241              p_dec->fmt_in.audio.i_rate, p_dec->fmt_in.audio.i_channels,
00242              p_dec->fmt_in.audio.i_bitspersample, p_sys->i_block,
00243              p_sys->i_samplesperblock );
00244 
00245     p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
00246     p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
00247     p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
00248     p_dec->fmt_out.audio.i_physical_channels =
00249         p_dec->fmt_out.audio.i_original_channels =
00250             pi_channels_maps[p_dec->fmt_in.audio.i_channels];
00251 
00252     aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
00253     aout_DateSet( &p_sys->end_date, 0 );
00254 
00255     p_dec->pf_decode_audio = DecodeBlock;
00256 
00257     return VLC_SUCCESS;
00258 }
00259 
00260 /*****************************************************************************
00261  * DecodeBlock:
00262  *****************************************************************************/
00263 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
00264 {
00265     decoder_sys_t *p_sys  = p_dec->p_sys;
00266     block_t *p_block;
00267 
00268     if( !pp_block || !*pp_block ) return NULL;
00269 
00270     p_block = *pp_block;
00271 
00272     if( p_block->i_pts != 0 &&
00273         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
00274     {
00275         aout_DateSet( &p_sys->end_date, p_block->i_pts );
00276     }
00277     else if( !aout_DateGet( &p_sys->end_date ) )
00278     {
00279         /* We've just started the stream, wait for the first PTS. */
00280         block_Release( p_block );
00281         return NULL;
00282     }
00283 
00284     /* Don't re-use the same pts twice */
00285     p_block->i_pts = 0;
00286 
00287     if( p_block->i_buffer >= p_sys->i_block )
00288     {
00289         aout_buffer_t *p_out;
00290 
00291         p_out = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_samplesperblock );
00292         if( p_out == NULL )
00293         {
00294             block_Release( p_block );
00295             return NULL;
00296         }
00297 
00298         p_out->start_date = aout_DateGet( &p_sys->end_date );
00299         p_out->end_date =
00300             aout_DateIncrement( &p_sys->end_date, p_sys->i_samplesperblock );
00301 
00302         switch( p_sys->codec )
00303         {
00304         case ADPCM_IMA_QT:
00305             DecodeAdpcmImaQT( p_dec, (int16_t*)p_out->p_buffer,
00306                               p_block->p_buffer );
00307             break;
00308         case ADPCM_IMA_WAV:
00309             DecodeAdpcmImaWav( p_dec, (int16_t*)p_out->p_buffer,
00310                                p_block->p_buffer );
00311             break;
00312         case ADPCM_MS:
00313             DecodeAdpcmMs( p_dec, (int16_t*)p_out->p_buffer,
00314                            p_block->p_buffer );
00315             break;
00316         case ADPCM_DK4:
00317             DecodeAdpcmDk4( p_dec, (int16_t*)p_out->p_buffer,
00318                             p_block->p_buffer );
00319             break;
00320         case ADPCM_DK3:
00321             DecodeAdpcmDk3( p_dec, (int16_t*)p_out->p_buffer,
00322                             p_block->p_buffer );
00323             break;
00324         case ADPCM_EA:
00325             DecodeAdpcmEA( p_dec, (int16_t*)p_out->p_buffer,
00326                            p_block->p_buffer );
00327         default:
00328             break;
00329         }
00330 
00331         p_block->p_buffer += p_sys->i_block;
00332         p_block->i_buffer -= p_sys->i_block;
00333         return p_out;
00334     }
00335 
00336     block_Release( p_block );
00337     return NULL;
00338 }
00339 
00340 /*****************************************************************************
00341  * CloseDecoder:
00342  *****************************************************************************/
00343 static void CloseDecoder( vlc_object_t *p_this )
00344 {
00345     decoder_t *p_dec = (decoder_t *)p_this;
00346     decoder_sys_t *p_sys = p_dec->p_sys;
00347 
00348     if( p_sys->codec == ADPCM_EA )
00349         free( p_dec->fmt_in.p_extra );
00350     free( p_sys );
00351 }
00352 
00353 /*****************************************************************************
00354  * Local functions
00355  *****************************************************************************/
00356 #define CLAMP( v, min, max ) \
00357     if( (v) < (min) ) (v) = (min); \
00358     if( (v) > (max) ) (v) = (max)
00359 
00360 #define GetByte( v ) \
00361     (v) = *p_buffer; p_buffer++;
00362 
00363 #define GetWord( v ) \
00364     (v) = *p_buffer; p_buffer++; \
00365     (v) |= ( *p_buffer ) << 8; p_buffer++; \
00366     if( (v)&0x8000 ) (v) -= 0x010000;
00367 
00368 /*
00369  * MS
00370  */
00371 typedef struct adpcm_ms_channel_s
00372 {
00373     int i_idelta;
00374     int i_sample1, i_sample2;
00375     int i_coeff1, i_coeff2;
00376 
00377 } adpcm_ms_channel_t;
00378 
00379 
00380 static int AdpcmMsExpandNibble(adpcm_ms_channel_t *p_channel,
00381                                int i_nibble )
00382 {
00383     int i_predictor;
00384     int i_snibble;
00385     /* expand sign */
00386 
00387     i_snibble = i_nibble - ( i_nibble&0x08 ? 0x10 : 0 );
00388 
00389     i_predictor = ( p_channel->i_sample1 * p_channel->i_coeff1 +
00390                     p_channel->i_sample2 * p_channel->i_coeff2 ) / 256 +
00391                   i_snibble * p_channel->i_idelta;
00392 
00393     CLAMP( i_predictor, -32768, 32767 );
00394 
00395     p_channel->i_sample2 = p_channel->i_sample1;
00396     p_channel->i_sample1 = i_predictor;
00397 
00398     p_channel->i_idelta = ( i_adaptation_table[i_nibble] *
00399                             p_channel->i_idelta ) / 256;
00400     if( p_channel->i_idelta < 16 )
00401     {
00402         p_channel->i_idelta = 16;
00403     }
00404     return( i_predictor );
00405 }
00406 
00407 static void DecodeAdpcmMs( decoder_t *p_dec, int16_t *p_sample,
00408                            uint8_t *p_buffer )
00409 {
00410     decoder_sys_t *p_sys  = p_dec->p_sys;
00411     adpcm_ms_channel_t channel[2];
00412     int i_nibbles;
00413     int b_stereo;
00414     int i_block_predictor;
00415 
00416     b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
00417 
00418     GetByte( i_block_predictor );
00419     CLAMP( i_block_predictor, 0, 6 );
00420     channel[0].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
00421     channel[0].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
00422 
00423     if( b_stereo )
00424     {
00425         GetByte( i_block_predictor );
00426         CLAMP( i_block_predictor, 0, 6 );
00427         channel[1].i_coeff1 = i_adaptation_coeff1[i_block_predictor];
00428         channel[1].i_coeff2 = i_adaptation_coeff2[i_block_predictor];
00429     }
00430     GetWord( channel[0].i_idelta );
00431     if( b_stereo )
00432     {
00433         GetWord( channel[1].i_idelta );
00434     }
00435 
00436     GetWord( channel[0].i_sample1 );
00437     if( b_stereo )
00438     {
00439         GetWord( channel[1].i_sample1 );
00440     }
00441 
00442     GetWord( channel[0].i_sample2 );
00443     if( b_stereo )
00444     {
00445         GetWord( channel[1].i_sample2 );
00446     }
00447 
00448     if( b_stereo )
00449     {
00450         *p_sample++ = channel[0].i_sample2;
00451         *p_sample++ = channel[1].i_sample2;
00452         *p_sample++ = channel[0].i_sample1;
00453         *p_sample++ = channel[1].i_sample1;
00454     }
00455     else
00456     {
00457         *p_sample++ = channel[0].i_sample2;
00458         *p_sample++ = channel[0].i_sample1;
00459     }
00460 
00461     for( i_nibbles = 2 * (p_sys->i_block - 7 * p_dec->fmt_in.audio.i_channels);
00462          i_nibbles > 0; i_nibbles -= 2, p_buffer++ )
00463     {
00464         *p_sample++ = AdpcmMsExpandNibble( &channel[0], (*p_buffer) >> 4);
00465         *p_sample++ = AdpcmMsExpandNibble( &channel[b_stereo ? 1 : 0],
00466                                            (*p_buffer)&0x0f);
00467     }
00468 }
00469 
00470 /*
00471  * IMA-WAV
00472  */
00473 typedef struct adpcm_ima_wav_channel_s
00474 {
00475     int i_predictor;
00476     int i_step_index;
00477 
00478 } adpcm_ima_wav_channel_t;
00479 
00480 static int AdpcmImaWavExpandNibble(adpcm_ima_wav_channel_t *p_channel,
00481                                    int i_nibble )
00482 {
00483     int i_diff;
00484 
00485     i_diff = i_step_table[p_channel->i_step_index] >> 3;
00486     if( i_nibble&0x04 ) i_diff += i_step_table[p_channel->i_step_index];
00487     if( i_nibble&0x02 ) i_diff += i_step_table[p_channel->i_step_index]>>1;
00488     if( i_nibble&0x01 ) i_diff += i_step_table[p_channel->i_step_index]>>2;
00489     if( i_nibble&0x08 )
00490         p_channel->i_predictor -= i_diff;
00491     else
00492         p_channel->i_predictor += i_diff;
00493 
00494     CLAMP( p_channel->i_predictor, -32768, 32767 );
00495 
00496     p_channel->i_step_index += i_index_table[i_nibble];
00497 
00498     CLAMP( p_channel->i_step_index, 0, 88 );
00499 
00500     return( p_channel->i_predictor );
00501 }
00502 
00503 static void DecodeAdpcmImaWav( decoder_t *p_dec, int16_t *p_sample,
00504                                uint8_t *p_buffer )
00505 {
00506     decoder_sys_t *p_sys  = p_dec->p_sys;
00507     adpcm_ima_wav_channel_t channel[2];
00508     int                     i_nibbles;
00509     int                     b_stereo;
00510 
00511     b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
00512 
00513     GetWord( channel[0].i_predictor );
00514     GetByte( channel[0].i_step_index );
00515     CLAMP( channel[0].i_step_index, 0, 88 );
00516     p_buffer++;
00517 
00518     if( b_stereo )
00519     {
00520         GetWord( channel[1].i_predictor );
00521         GetByte( channel[1].i_step_index );
00522         CLAMP( channel[1].i_step_index, 0, 88 );
00523         p_buffer++;
00524     }
00525 
00526     if( b_stereo )
00527     {
00528         for( i_nibbles = 2 * (p_sys->i_block - 8);
00529              i_nibbles > 0;
00530              i_nibbles -= 16 )
00531         {
00532             int i;
00533 
00534             for( i = 0; i < 4; i++ )
00535             {
00536                 p_sample[i * 4] =
00537                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i]&0x0f);
00538                 p_sample[i * 4 + 2] =
00539                     AdpcmImaWavExpandNibble(&channel[0],p_buffer[i] >> 4);
00540             }
00541             p_buffer += 4;
00542 
00543             for( i = 0; i < 4; i++ )
00544             {
00545                 p_sample[i * 4 + 1] =
00546                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i]&0x0f);
00547                 p_sample[i * 4 + 3] =
00548                     AdpcmImaWavExpandNibble(&channel[1],p_buffer[i] >> 4);
00549             }
00550             p_buffer += 4;
00551             p_sample += 16;
00552 
00553         }
00554 
00555 
00556     }
00557     else
00558     {
00559         for( i_nibbles = 2 * (p_sys->i_block - 4);
00560              i_nibbles > 0;
00561              i_nibbles -= 2, p_buffer++ )
00562         {
00563             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer)&0x0f );
00564             *p_sample++ =AdpcmImaWavExpandNibble( &channel[0], (*p_buffer) >> 4 );
00565         }
00566     }
00567 }
00568 
00569 /*
00570  * Ima4 in QT file
00571  */
00572 static void DecodeAdpcmImaQT( decoder_t *p_dec, int16_t *p_sample,
00573                               uint8_t *p_buffer )
00574 {
00575     adpcm_ima_wav_channel_t channel[2];
00576     int                     i_nibbles;
00577     int                     i_ch;
00578     int                     i_step;
00579 
00580     i_step = p_dec->fmt_in.audio.i_channels;
00581 
00582     for( i_ch = 0; i_ch < p_dec->fmt_in.audio.i_channels; i_ch++ )
00583     {
00584         /* load preambule */
00585         channel[i_ch].i_predictor  = (int16_t)((( ( p_buffer[0] << 1 )|(  p_buffer[1] >> 7 ) ))<<7);
00586         channel[i_ch].i_step_index = p_buffer[1]&0x7f;
00587 
00588         CLAMP( channel[i_ch].i_step_index, 0, 88 );
00589         p_buffer += 2;
00590 
00591         for( i_nibbles = 0; i_nibbles < 64; i_nibbles +=2 )
00592         {
00593             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer)&0x0f);
00594             p_sample += i_step;
00595 
00596             *p_sample = AdpcmImaWavExpandNibble( &channel[i_ch], (*p_buffer >> 4)&0x0f);
00597             p_sample += i_step;
00598 
00599             p_buffer++;
00600         }
00601 
00602         /* Next channel */
00603         p_sample += 1 - 64 * i_step;
00604     }
00605 }
00606 
00607 /*
00608  * Dk4
00609  */
00610 static void DecodeAdpcmDk4( decoder_t *p_dec, int16_t *p_sample,
00611                             uint8_t *p_buffer )
00612 {
00613     decoder_sys_t *p_sys  = p_dec->p_sys;
00614     adpcm_ima_wav_channel_t channel[2];
00615     int                     i_nibbles;
00616     int                     b_stereo;
00617 
00618     b_stereo = p_dec->fmt_in.audio.i_channels == 2 ? 1 : 0;
00619 
00620     GetWord( channel[0].i_predictor );
00621     GetByte( channel[0].i_step_index );
00622     CLAMP( channel[0].i_step_index, 0, 88 );
00623     p_buffer++;
00624 
00625     if( b_stereo )
00626     {
00627         GetWord( channel[1].i_predictor );
00628         GetByte( channel[1].i_step_index );
00629         CLAMP( channel[1].i_step_index, 0, 88 );
00630         p_buffer++;
00631     }
00632 
00633     /* first output predictor */
00634     *p_sample++ = channel[0].i_predictor;
00635     if( b_stereo )
00636     {
00637         *p_sample++ = channel[1].i_predictor;
00638     }
00639 
00640     for( i_nibbles = 0;
00641          i_nibbles < p_sys->i_block - 4 * (b_stereo ? 2:1 );
00642          i_nibbles++ )
00643     {
00644         *p_sample++ = AdpcmImaWavExpandNibble( &channel[0],
00645                                               (*p_buffer) >> 4);
00646         *p_sample++ = AdpcmImaWavExpandNibble( &channel[b_stereo ? 1 : 0],
00647                                                (*p_buffer)&0x0f);
00648 
00649         p_buffer++;
00650     }
00651 }
00652 
00653 /*
00654  * Dk3
00655  */
00656 static void DecodeAdpcmDk3( decoder_t *p_dec, int16_t *p_sample,
00657                             uint8_t *p_buffer )
00658 {
00659     decoder_sys_t *p_sys  = p_dec->p_sys;
00660     uint8_t                 *p_end = &p_buffer[p_sys->i_block];
00661     adpcm_ima_wav_channel_t sum;
00662     adpcm_ima_wav_channel_t diff;
00663     int                     i_diff_value;
00664 
00665     p_buffer += 10;
00666 
00667     GetWord( sum.i_predictor );
00668     GetWord( diff.i_predictor );
00669     GetByte( sum.i_step_index );
00670     GetByte( diff.i_step_index );
00671 
00672     i_diff_value = diff.i_predictor;
00673     /* we process 6 nibbles at once */
00674     while( p_buffer + 1 <= p_end )
00675     {
00676         /* first 3 nibbles */
00677         AdpcmImaWavExpandNibble( &sum,
00678                                  (*p_buffer)&0x0f);
00679 
00680         AdpcmImaWavExpandNibble( &diff,
00681                                  (*p_buffer) >> 4 );
00682 
00683         i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
00684 
00685         *p_sample++ = sum.i_predictor + i_diff_value;
00686         *p_sample++ = sum.i_predictor - i_diff_value;
00687 
00688         p_buffer++;
00689 
00690         AdpcmImaWavExpandNibble( &sum,
00691                                  (*p_buffer)&0x0f);
00692 
00693         *p_sample++ = sum.i_predictor + i_diff_value;
00694         *p_sample++ = sum.i_predictor - i_diff_value;
00695 
00696         /* now last 3 nibbles */
00697         AdpcmImaWavExpandNibble( &sum,
00698                                  (*p_buffer)>>4);
00699         p_buffer++;
00700         if( p_buffer < p_end )
00701         {
00702             AdpcmImaWavExpandNibble( &diff,
00703                                      (*p_buffer)&0x0f );
00704 
00705             i_diff_value = ( i_diff_value + diff.i_predictor ) / 2;
00706 
00707             *p_sample++ = sum.i_predictor + i_diff_value;
00708             *p_sample++ = sum.i_predictor - i_diff_value;
00709 
00710             AdpcmImaWavExpandNibble( &sum,
00711                                      (*p_buffer)>>4);
00712             p_buffer++;
00713 
00714             *p_sample++ = sum.i_predictor + i_diff_value;
00715             *p_sample++ = sum.i_predictor - i_diff_value;
00716         }
00717     }
00718 }
00719 
00720 
00721 /*
00722  * EA ADPCM
00723  */
00724 #define MAX_CHAN 5
00725 static void DecodeAdpcmEA( decoder_t *p_dec, int16_t *p_sample,
00726                            uint8_t *p_buffer )
00727 {
00728     static const uint32_t EATable[]=
00729     {
00730         0x00000000, 0x000000F0, 0x000001CC, 0x00000188,
00731         0x00000000, 0x00000000, 0xFFFFFF30, 0xFFFFFF24,
00732         0x00000000, 0x00000001, 0x00000003, 0x00000004,
00733         0x00000007, 0x00000008, 0x0000000A, 0x0000000B,
00734         0x00000000, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFC
00735     };
00736     decoder_sys_t *p_sys  = p_dec->p_sys;
00737     uint8_t *p_end;
00738     unsigned i_channels, c;
00739     int16_t *prev, *cur;
00740     int32_t c1[MAX_CHAN], c2[MAX_CHAN];
00741     int8_t d[MAX_CHAN];
00742 
00743     i_channels = p_dec->fmt_in.audio.i_channels;
00744     p_end = &p_buffer[p_sys->i_block];
00745 
00746     prev = (int16_t *)p_dec->fmt_in.p_extra;
00747     cur = prev + i_channels;
00748 
00749     for (c = 0; c < i_channels; c++)
00750     {
00751         uint8_t input;
00752 
00753         input = p_buffer[c];
00754         c1[c] = EATable[input >> 4];
00755         c2[c] = EATable[(input >> 4) + 4];
00756         d[c] = (input & 0xf) + 8;
00757     }
00758 
00759     for( p_buffer += i_channels; p_buffer < p_end ; p_buffer += i_channels)
00760     {
00761         for (c = 0; c < i_channels; c++)
00762         {
00763             int32_t spl;
00764 
00765             spl = (p_buffer[c] >> 4) & 0xf;
00766             spl = (spl << 0x1c) >> d[c];
00767             spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
00768             CLAMP( spl, -32768, 32767 );
00769             prev[c] = cur[c];
00770             cur[c] = spl;
00771 
00772             *(p_sample++) = spl;
00773         }
00774 
00775         for (c = 0; c < i_channels; c++)
00776         {
00777             int32_t spl;
00778 
00779             spl = p_buffer[c] & 0xf;
00780             spl = (spl << 0x1c) >> d[c];
00781             spl = (spl + cur[c] * c1[c] + prev[c] * c2[c] + 0x80) >> 8;
00782             CLAMP( spl, -32768, 32767 );
00783             prev[c] = cur[c];
00784             cur[c] = spl;
00785 
00786             *(p_sample++) = spl;
00787         }
00788     }
00789 }

Generated on Tue Dec 20 10:14:28 2005 for vlc-0.8.4a by  doxygen 1.4.2