jamulus/src/audiocompr.cpp

555 lines
16 KiB
C++
Raw Normal View History

/******************************************************************************\
* Copyright (c) 2004-2009
*
* Author(s):
* Volker Fischer, Erik de Castro Lopo
*
2008-08-09 16:35:44 +02:00
* This code is based on the Open-Source implementation of IMA-ADPCM / MS-ADPCM
* written by Erik de Castro Lopo <erikd[at-#]mega-nerd[dot*]com> in 1999-2004
*
* Changes:
* - only support for one channel
* - put 2 audio samples in header to get even number of audio samples encoded
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\******************************************************************************/
#include "audiocompr.h"
/* Implementation *************************************************************/
int CAudioCompression::Init ( const int iNewAudioLen,
const EAudComprType eNewAuCoTy )
{
eAudComprType = eNewAuCoTy;
switch ( eNewAuCoTy )
{
case CT_NONE:
2007-12-31 14:09:12 +01:00
return iCodeSize = 2 * iNewAudioLen; // short = 2 * byte
case CT_IMAADPCM:
return ImaAdpcm.Init ( iNewAudioLen );
2008-08-09 16:35:44 +02:00
case CT_MSADPCM:
return MsAdpcm.Init ( iNewAudioLen );
default:
return 0;
}
}
CVector<unsigned char> CAudioCompression::Encode ( const CVector<short>& vecsAudio )
{
2007-09-08 12:45:14 +02:00
if ( eAudComprType == CT_NONE )
{
2007-12-31 14:09:12 +01:00
// no compression, simply ship pure samples
2007-09-08 12:45:14 +02:00
CVector<unsigned char> vecbyOut ( iCodeSize );
const int iAudSize = iCodeSize / 2;
2007-09-08 12:45:14 +02:00
for ( int i = 0; i < iAudSize; i++ )
{
2008-08-09 16:35:44 +02:00
vecbyOut[2 * i] = vecsAudio[i] & 0xFF;
2007-09-08 12:45:14 +02:00
vecbyOut[2 * i + 1] = ( vecsAudio[i] >> 8 ) & 0xFF;
}
return vecbyOut;
}
else
{
switch ( eAudComprType )
{
case CT_IMAADPCM:
2007-12-31 14:09:12 +01:00
return ImaAdpcm.Encode ( vecsAudio ); // IMA-ADPCM
2008-08-09 16:35:44 +02:00
case CT_MSADPCM:
return MsAdpcm.Encode ( vecsAudio ); // MS-ADPCM
default:
return CVector<unsigned char> ( 0 );
}
}
}
2007-09-08 12:45:14 +02:00
CVector<short> CAudioCompression::Decode ( const CVector<unsigned char>& vecbyAdpcm )
{
2007-09-08 12:45:14 +02:00
if ( eAudComprType == CT_NONE )
{
2007-12-31 14:09:12 +01:00
// no compression, reassemble pure samples
const int iAudSize = iCodeSize / 2;
2007-09-08 12:45:14 +02:00
CVector<short> vecsOut ( iAudSize );
2007-09-08 12:45:14 +02:00
for ( int i = 0; i < iAudSize; i++ )
{
2007-09-08 12:45:14 +02:00
int current = vecbyAdpcm[2 * i] | ( vecbyAdpcm[2 * i + 1] << 8 );
if ( current & 0x8000 )
{
current -= 0x10000;
}
vecsOut[i] = (short) current;
}
return vecsOut;
}
else
{
switch ( eAudComprType )
{
case CT_IMAADPCM:
2007-12-31 14:09:12 +01:00
return ImaAdpcm.Decode ( vecbyAdpcm ); // IMA-ADPCM
2008-08-09 16:35:44 +02:00
case CT_MSADPCM:
return MsAdpcm.Decode ( vecbyAdpcm ); // MS-ADPCM
default:
return CVector<short> ( 0 );
}
}
}
2008-08-09 16:35:44 +02:00
/******************************************************************************\
* IMA-ADPCM implementation *
\******************************************************************************/
2007-09-08 12:45:14 +02:00
int CImaAdpcm::Init ( const int iNewAudioLen )
{
2007-12-31 14:09:12 +01:00
// set lengths for audio and compressed data
iAudSize = iNewAudioLen;
2007-09-08 12:45:14 +02:00
iAdpcmSize = 4 /* bytes header */ + (int) ceil (
(double) ( iAudSize - 2 /* first two samples are in header */ ) / 2 );
iStepindEnc = 0;
return iAdpcmSize;
}
2007-09-08 12:45:14 +02:00
CVector<unsigned char> CImaAdpcm::Encode ( const CVector<short>& vecsAudio )
{
2008-09-15 09:19:34 +02:00
int i;
CVector<unsigned char> vecbyAdpcm;
CVector<unsigned char> vecbyAdpcmTemp;
2007-12-31 14:09:12 +01:00
// init size
2008-08-09 16:35:44 +02:00
vecbyAdpcm.Init ( iAdpcmSize );
2007-09-08 12:45:14 +02:00
vecbyAdpcmTemp.Init ( iAudSize );
2008-08-09 16:35:44 +02:00
/* Encode the block header ---------------------------------------------- */
vecbyAdpcm[0] = vecsAudio[0] & 0xFF;
2007-09-08 12:45:14 +02:00
vecbyAdpcm[1] = ( vecsAudio[0] >> 8 ) & 0xFF;
vecbyAdpcm[2] = iStepindEnc;
int iPrevAudio = vecsAudio[0];
2008-08-09 16:35:44 +02:00
/* Encode the samples as 4 bit ------------------------------------------ */
2007-09-08 12:45:14 +02:00
for ( i = 1; i < iAudSize; i++ )
{
2007-12-31 14:09:12 +01:00
// init diff and step
int diff = vecsAudio[i] - iPrevAudio;
2007-12-31 14:09:12 +01:00
Q_ASSERT ( iStepindEnc < IMA_STEP_SIZE_TAB_LEN );
int step = ima_step_size[iStepindEnc];
short bytecode = 0;
int vpdiff = step >> 3;
2007-09-08 12:45:14 +02:00
if ( diff < 0 )
{
bytecode = 8;
diff = -diff;
}
short mask = 4;
2007-09-08 12:45:14 +02:00
while ( mask )
{
2007-09-08 12:45:14 +02:00
if ( diff >= step )
{
bytecode |= mask;
diff -= step;
vpdiff += step;
}
step >>= 1;
mask >>= 1;
}
2007-09-08 12:45:14 +02:00
if ( bytecode & 8 )
{
iPrevAudio -= vpdiff;
}
else
{
iPrevAudio += vpdiff;
}
2007-12-31 14:09:12 +01:00
// adjust step size
Q_ASSERT ( bytecode < IMA_INDX_ADJUST_TAB_LEN );
iStepindEnc += ima_indx_adjust[bytecode];
2007-12-31 14:09:12 +01:00
// check that values do not exceed the bounds
2008-08-09 16:35:44 +02:00
iPrevAudio = CheckBounds ( iPrevAudio, _MINSHORT, _MAXSHORT );
2007-09-08 12:45:14 +02:00
iStepindEnc = CheckBounds ( iStepindEnc, 0, IMA_STEP_SIZE_TAB_LEN - 1 );
2008-08-09 16:35:44 +02:00
// use a temporary buffer as an intermediate result buffer
vecbyAdpcmTemp[i] = bytecode;
}
2008-08-09 16:35:44 +02:00
/* Pack the 4 bit encoded samples --------------------------------------- */
2007-12-31 14:09:12 +01:00
// The first encoded audio sample is in header
vecbyAdpcm[3] = vecbyAdpcmTemp[1] & 0x0F;
2007-09-08 12:45:14 +02:00
for ( i = 4; i < iAdpcmSize; i++ )
{
vecbyAdpcm[i] = vecbyAdpcmTemp[2 * i - 6] & 0x0F;
2007-09-08 12:45:14 +02:00
vecbyAdpcm[i] |= ( vecbyAdpcmTemp[2 * i - 5] << 4 ) & 0xF0;
}
return vecbyAdpcm;
}
2007-09-08 12:45:14 +02:00
CVector<short> CImaAdpcm::Decode ( const CVector<unsigned char>& vecbyAdpcm )
{
2008-09-15 09:19:34 +02:00
int i;
CVector<short> vecsAudio;
2007-09-08 12:45:14 +02:00
vecsAudio.Init ( iAudSize );
2008-08-09 16:35:44 +02:00
/* Read the block header ------------------------------------------------ */
2007-09-08 12:45:14 +02:00
int current = vecbyAdpcm[0] | ( vecbyAdpcm[1] << 8 );
if ( current & 0x8000 )
{
current -= 0x10000;
}
2007-12-31 14:09:12 +01:00
// get and bound step index
2007-09-08 12:45:14 +02:00
int iStepindDec = CheckBounds ( vecbyAdpcm[2], 0, IMA_STEP_SIZE_TAB_LEN - 1 );
2007-12-31 14:09:12 +01:00
// set first sample which was delivered in the header
vecsAudio[0] = current;
2008-08-09 16:35:44 +02:00
/* -------------------------------------------------------------------------
pull apart the packed 4 bit samples and store them in their correct
sample positions */
// the first encoded audio sample is in header
vecsAudio[1] = vecbyAdpcm[3] & 0x0F;
2007-09-08 12:45:14 +02:00
for ( i = 4; i < iAdpcmSize; i++ )
{
const short bytecode = vecbyAdpcm[i];
vecsAudio[2 * i - 6] = bytecode & 0x0F;
2007-09-08 12:45:14 +02:00
vecsAudio[2 * i - 5] = ( bytecode >> 4 ) & 0x0F;
}
2008-08-09 16:35:44 +02:00
/* Decode the encoded 4 bit samples ------------------------------------- */
2007-09-08 12:45:14 +02:00
for ( i = 1; i < iAudSize; i++ )
{
2008-09-15 09:19:34 +02:00
const short bytecode = vecsAudio[i] & 0xF;
Q_ASSERT ( iStepindDec < IMA_STEP_SIZE_TAB_LEN );
2007-12-31 14:09:12 +01:00
short step = ima_step_size[iStepindDec];
int current = vecsAudio[i - 1];
int diff = step >> 3;
2007-09-08 12:45:14 +02:00
if ( bytecode & 1 )
{
diff += step >> 2;
}
2007-09-08 12:45:14 +02:00
if ( bytecode & 2 )
{
diff += step >> 1;
}
2007-09-08 12:45:14 +02:00
if ( bytecode & 4 )
{
diff += step;
}
2007-09-08 12:45:14 +02:00
if ( bytecode & 8 )
{
diff = -diff;
}
current += diff;
2007-12-31 14:09:12 +01:00
Q_ASSERT ( bytecode < IMA_INDX_ADJUST_TAB_LEN );
iStepindDec += ima_indx_adjust[bytecode];
2007-12-31 14:09:12 +01:00
// check that values do not exceed the bounds
2007-09-08 12:45:14 +02:00
current = CheckBounds ( current, _MINSHORT, _MAXSHORT );
iStepindDec = CheckBounds ( iStepindDec, 0, IMA_STEP_SIZE_TAB_LEN - 1 );
vecsAudio[i] = current;
}
return vecsAudio;
}
2008-08-09 16:35:44 +02:00
/******************************************************************************\
* MS-ADPCM implementation *
\******************************************************************************/
/*
MS ADPCM block layout:
byte purpose
0 block predictor [0..6]
1,2 initial idelta (positive)
3,4 sample 1
5,6 sample 0
7..n packed bytecodes
*/
int CMsAdpcm::Init ( const int iNewAudioLen )
{
// set lengths for audio and compressed data
iAudSize = iNewAudioLen;
iAdpcmSize = 7 /* bytes header */ + (int) ceil (
(double) ( iAudSize - 2 /* first two samples are in header */ ) / 2 );
return iAdpcmSize;
}
CVector<unsigned char> CMsAdpcm::Encode ( const CVector<short>& vecsAudio )
{
CVector<short> vecsAudioTemp;
CVector<unsigned char> vecbyAdpcm;
// init size
vecsAudioTemp.Init ( iAudSize );
vecbyAdpcm.Init ( iAdpcmSize );
// copy input vector (because we want to overwrite it)
vecsAudioTemp = vecsAudio;
// choose predictor
int bpred;
int idelta;
ChoosePredictor ( vecsAudio, bpred, idelta );
/* Encode the block header ---------------------------------------------- */
vecbyAdpcm[0] = bpred;
vecbyAdpcm[1] = idelta & 0xFF;
vecbyAdpcm[2] = ( idelta >> 8 ) & 0xFF;
vecbyAdpcm[3] = vecsAudio[1] & 0xFF;
vecbyAdpcm[4] = ( vecsAudio[1] >> 8 ) & 0xFF;
vecbyAdpcm[5] = vecsAudio[0] & 0xFF;
vecbyAdpcm[6] = ( vecsAudio[0] >> 8 ) & 0xFF;
2008-08-09 16:35:44 +02:00
/* Encode the samples as 4 bit ------------------------------------------ */
unsigned int blockindx = 7;
unsigned char byte = 0;
2008-08-09 16:35:44 +02:00
for ( int k = 2; k < iAudSize; k++ )
{
2008-08-09 16:35:44 +02:00
const int predict = ( vecsAudioTemp[k - 1] * ms_AdaptCoeff1[bpred] +
vecsAudioTemp[k - 2] * ms_AdaptCoeff2[bpred] ) >> 8;
int errordelta = ( vecsAudio[k] - predict ) / idelta;
2008-08-09 16:35:44 +02:00
if ( errordelta < -8 )
2008-08-09 16:35:44 +02:00
{
errordelta = -8 ;
2008-08-09 16:35:44 +02:00
}
else
2008-08-09 16:35:44 +02:00
{
if (errordelta > 7)
{
errordelta = 7;
2008-08-09 16:35:44 +02:00
}
}
int newsamp = predict + ( idelta * errordelta );
2008-08-09 16:35:44 +02:00
if ( newsamp > 32767 )
2008-08-09 16:35:44 +02:00
{
newsamp = 32767;
2008-08-09 16:35:44 +02:00
}
else
2008-08-09 16:35:44 +02:00
{
if ( newsamp < -32768 )
{
newsamp = -32768;
2008-08-09 16:35:44 +02:00
}
}
if ( errordelta < 0 )
2008-08-09 16:35:44 +02:00
{
errordelta += 0x10;
2008-08-09 16:35:44 +02:00
}
byte = ( byte << 4 ) | ( errordelta & 0xF );
2008-08-09 16:35:44 +02:00
if ( k % 2 )
{
2008-08-09 16:35:44 +02:00
vecbyAdpcm[blockindx++] = byte;
byte = 0;
}
2008-08-09 16:35:44 +02:00
idelta = ( idelta * ms_AdaptationTable[errordelta] ) >> 8;
2008-08-09 16:35:44 +02:00
if ( idelta < 16 )
2008-08-09 16:35:44 +02:00
{
idelta = 16;
2008-08-09 16:35:44 +02:00
}
vecsAudioTemp[k] = newsamp;
}
2008-08-09 16:35:44 +02:00
return vecbyAdpcm;
}
CVector<short> CMsAdpcm::Decode ( const CVector<unsigned char>& vecbyAdpcm )
{
CVector<short> vecsAudio;
short bytecode;
vecsAudio.Init ( iAudSize );
/* Read the block header ------------------------------------------------ */
short bpred = vecbyAdpcm[0];
if ( bpred >= 7 )
{
// no valid MS ADPCM stream, do not decode
return vecsAudio;
}
short chan_idelta = vecbyAdpcm[1] | ( vecbyAdpcm[2] << 8 );
2008-08-09 16:35:44 +02:00
vecsAudio[1] = vecbyAdpcm[3] | ( vecbyAdpcm[4] << 8 );
vecsAudio[0] = vecbyAdpcm[5] | ( vecbyAdpcm[6] << 8 );
2008-08-09 16:35:44 +02:00
/* -------------------------------------------------------------------------
pull apart the packed 4 bit samples and store them in their correct
sample positions */
for ( int i = 7; i < iAdpcmSize; i++ )
{
bytecode = vecbyAdpcm[i];
vecsAudio[2 * i - 12] = ( bytecode >> 4 ) & 0x0F;
vecsAudio[2 * i - 11] = bytecode & 0x0F;
}
/* Decode the encoded 4 bit samples ------------------------------------- */
for ( int k = 2; k < iAudSize; k ++ )
{
bytecode = vecsAudio[k] & 0xF;
2008-08-09 16:35:44 +02:00
// compute next Adaptive Scale Factor (ASF)
int idelta = chan_idelta;
2008-08-09 16:35:44 +02:00
// => / 256 => FIXED_POINT_ADAPTATION_BASE == 256
chan_idelta = ( ms_AdaptationTable[bytecode] * idelta ) >> 8;
2008-08-09 16:35:44 +02:00
if ( chan_idelta < 16 )
2008-08-09 16:35:44 +02:00
{
chan_idelta = 16;
2008-08-09 16:35:44 +02:00
}
if ( bytecode & 0x8 )
2008-08-09 16:35:44 +02:00
{
bytecode -= 0x10;
2008-08-09 16:35:44 +02:00
}
// => / 256 => FIXED_POINT_COEFF_BASE == 256
const int predict = ( ( vecsAudio[k - 1] * ms_AdaptCoeff1[bpred] ) +
2008-08-09 16:35:44 +02:00
( vecsAudio[k - 2] * ms_AdaptCoeff2[bpred] ) ) >> 8;
int current = ( bytecode * idelta ) + predict;
2008-08-09 16:35:44 +02:00
if ( current > 32767 )
2008-08-09 16:35:44 +02:00
{
current = 32767;
2008-08-09 16:35:44 +02:00
}
else
2008-08-09 16:35:44 +02:00
{
if ( current < -32768 )
{
current = -32768;
2008-08-09 16:35:44 +02:00
}
}
vecsAudio[k] = current;
}
2008-08-09 16:35:44 +02:00
return vecsAudio;
2008-08-09 16:35:44 +02:00
}
void CMsAdpcm::ChoosePredictor ( const CVector<short>& vecsAudio,
int& block_pred,
int& idelta )
{
/*
Choosing the block predictor:
Each block requires a predictor and an idelta for each channel. The
predictor is in the range [0..6] which is an index into the two AdaptCoeff
tables. The predictor is chosen by trying all of the possible predictors on
a small set of samples at the beginning of the block. The predictor with the
smallest average abs (idelta) is chosen as the best predictor for this
block. The value of idelta is chosen to to give a 4 bit code value of +/- 4
(approx. half the max. code value). If the average abs (idelta) is zero, the
sixth predictor is chosen. If the value of idelta is less then 16 it is set
to 16.
*/
unsigned int best_bpred = 0;
unsigned int best_idelta = 0;
/* Microsoft uses an IDELTA_COUNT (number of sample pairs used to choose
best predictor) value of 3. The best possible results would be obtained
by using all the samples to choose the predictor. */
unsigned int idelta_count = min ( MSADPCM_IDELTA_COUNT, vecsAudio.Size() - 1 );
for ( unsigned int bpred = 0; bpred < MSADPCM_ADAPT_COEFF_COUNT; bpred++ )
{
2008-09-15 09:19:34 +02:00
unsigned int idelta_sum = 0;
2008-08-09 16:35:44 +02:00
for ( unsigned int k = 2; k < 2 + idelta_count; k++ )
2008-08-09 16:35:44 +02:00
{
idelta_sum += abs ( vecsAudio[k] -
2008-08-09 16:35:44 +02:00
( ( vecsAudio[k - 1] * ms_AdaptCoeff1[bpred] +
vecsAudio[k - 2] * ms_AdaptCoeff2[bpred] ) >> 8 ) );
}
idelta_sum /= ( 4 * idelta_count );
2008-08-09 16:35:44 +02:00
if ( bpred == 0 || idelta_sum < best_idelta )
{
2008-08-09 16:35:44 +02:00
best_bpred = bpred;
best_idelta = idelta_sum;
}
2008-08-09 16:35:44 +02:00
if ( !idelta_sum )
{
2008-08-09 16:35:44 +02:00
best_bpred = bpred;
best_idelta = 16;
break;
}
}
2008-08-09 16:35:44 +02:00
if ( best_idelta < 16 )
2008-08-09 16:35:44 +02:00
{
best_idelta = 16;
2008-08-09 16:35:44 +02:00
}
block_pred = best_bpred;
idelta = best_idelta;
2008-08-09 16:35:44 +02:00
return;
2008-08-09 16:35:44 +02:00
}