2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
14 * This file contains definition of functions used to
15 * encode LPC parameters (Shape & gain) of the upper band.
19 #include "encode_lpc_swb.h"
25 #include "lpc_gain_swb_tables.h"
26 #include "lpc_shape_swb12_tables.h"
27 #include "lpc_shape_swb16_tables.h"
29 #include "webrtc/typedefs.h"
31 /******************************************************************************
32 * WebRtcIsac_RemoveLarMean()
34 * Remove the means from LAR coefficients.
37 * -lar : pointer to lar vectors. LAR vectors are
39 * -bandwidth : indicates if the given LAR vectors belong
40 * to SWB-12kHz or SWB-16kHz.
43 * -lar : pointer to mean-removed LAR:s.
48 WebRtcIsac_RemoveLarMean(
55 const double* meanLAR;
60 numVec = UB_LPC_VEC_PER_FRAME;
61 meanLAR = WebRtcIsac_kMeanLarUb12;
66 numVec = UB16_LPC_VEC_PER_FRAME;
67 meanLAR = WebRtcIsac_kMeanLarUb16;
74 for(vecCntr = 0; vecCntr < numVec; vecCntr++)
76 for(coeffCntr = 0; coeffCntr < UB_LPC_ORDER; coeffCntr++)
79 *lar++ -= meanLAR[coeffCntr];
85 /******************************************************************************
86 * WebRtcIsac_DecorrelateIntraVec()
88 * Remove the correlation amonge the components of LAR vectors. If LAR vectors
89 * of one frame are put in a matrix where each column is a LAR vector of a
90 * sub-frame, then this is equivalent to multiplying the LAR matrix with
91 * a decorrelting mtrix from left.
94 * -inLar : pointer to mean-removed LAR vecrtors.
95 * -bandwidth : indicates if the given LAR vectors belong
96 * to SWB-12kHz or SWB-16kHz.
99 * -out : decorrelated LAR vectors.
102 WebRtcIsac_DecorrelateIntraVec(
107 const double* ptrData;
108 const double* ptrRow;
113 const double* decorrMat;
118 decorrMat = &WebRtcIsac_kIntraVecDecorrMatUb12[0][0];
119 numVec = UB_LPC_VEC_PER_FRAME;
124 decorrMat = &WebRtcIsac_kIintraVecDecorrMatUb16[0][0];
125 numVec = UB16_LPC_VEC_PER_FRAME;
135 // data is assumed to contain 'numVec' of LAR
136 // vectors (mean removed) each of dimension 'UB_LPC_ORDER'
137 // concatenated one after the other.
141 for(larVecCntr = 0; larVecCntr < numVec; larVecCntr++)
143 for(rowCntr = 0; rowCntr < UB_LPC_ORDER; rowCntr++)
145 ptrRow = &decorrMat[rowCntr * UB_LPC_ORDER];
147 for(colCntr = 0; colCntr < UB_LPC_ORDER; colCntr++)
149 *out += ptrData[colCntr] * ptrRow[colCntr];
153 ptrData += UB_LPC_ORDER;
158 /******************************************************************************
159 * WebRtcIsac_DecorrelateInterVec()
161 * Remover the correlation among mean-removed LAR vectors. If LAR vectors
162 * of one frame are put in a matrix where each column is a LAR vector of a
163 * sub-frame, then this is equivalent to multiplying the LAR matrix with
164 * a decorrelting mtrix from right.
167 * -data : pointer to matrix of LAR vectors. The matrix
168 * is stored column-wise.
169 * -bandwidth : indicates if the given LAR vectors belong
170 * to SWB-12kHz or SWB-16kHz.
173 * -out : decorrelated LAR vectors.
176 WebRtcIsac_DecorrelateInterVec(
184 const double* decorrMat;
191 decorrMat = &WebRtcIsac_kInterVecDecorrMatUb12[0][0];
192 interVecDim = UB_LPC_VEC_PER_FRAME;
197 decorrMat = &WebRtcIsac_kInterVecDecorrMatUb16[0][0];
198 interVecDim = UB16_LPC_VEC_PER_FRAME;
208 // data is of size 'interVecDim' * 'UB_LPC_ORDER'
209 // That is 'interVecDim' of LAR vectors (mean removed)
210 // in columns each of dimension 'UB_LPC_ORDER'.
211 // matrix is stored column-wise.
214 for(coeffCntr = 0; coeffCntr < UB_LPC_ORDER; coeffCntr++)
216 for(colCntr = 0; colCntr < interVecDim; colCntr++)
218 out[coeffCntr + colCntr * UB_LPC_ORDER] = 0;
219 for(rowCntr = 0; rowCntr < interVecDim; rowCntr++)
221 out[coeffCntr + colCntr * UB_LPC_ORDER] +=
222 data[coeffCntr + rowCntr * UB_LPC_ORDER] *
223 decorrMat[rowCntr * interVecDim + colCntr];
230 /******************************************************************************
231 * WebRtcIsac_QuantizeUncorrLar()
233 * Quantize the uncorrelated parameters.
236 * -data : uncorrelated LAR vectors.
237 * -bandwidth : indicates if the given LAR vectors belong
238 * to SWB-12kHz or SWB-16kHz.
241 * -data : quantized version of the input.
242 * -idx : pointer to quantization indices.
245 WebRtcIsac_QuantizeUncorrLar(
253 const double* leftRecPoint;
254 double quantizationStepSize;
255 const int16_t* numQuantCell;
260 leftRecPoint = WebRtcIsac_kLpcShapeLeftRecPointUb12;
261 quantizationStepSize = WebRtcIsac_kLpcShapeQStepSizeUb12;
262 numQuantCell = WebRtcIsac_kLpcShapeNumRecPointUb12;
263 interVecDim = UB_LPC_VEC_PER_FRAME;
268 leftRecPoint = WebRtcIsac_kLpcShapeLeftRecPointUb16;
269 quantizationStepSize = WebRtcIsac_kLpcShapeQStepSizeUb16;
270 numQuantCell = WebRtcIsac_kLpcShapeNumRecPointUb16;
271 interVecDim = UB16_LPC_VEC_PER_FRAME;
279 // Quantize the parametrs.
281 for(cntr = 0; cntr < UB_LPC_ORDER * interVecDim; cntr++)
283 idx = (int32_t)floor((*data - leftRecPoint[cntr]) /
284 quantizationStepSize + 0.5);
289 else if(idx >= numQuantCell[cntr])
291 idx = numQuantCell[cntr] - 1;
294 *data++ = leftRecPoint[cntr] + idx * quantizationStepSize;
301 /******************************************************************************
302 * WebRtcIsac_DequantizeLpcParam()
304 * Get the quantized value of uncorrelated LARs given the quantization indices.
307 * -idx : pointer to quantiztion indices.
308 * -bandwidth : indicates if the given LAR vectors belong
309 * to SWB-12kHz or SWB-16kHz.
312 * -out : pointer to quantized values.
315 WebRtcIsac_DequantizeLpcParam(
322 const double* leftRecPoint;
323 double quantizationStepSize;
329 leftRecPoint = WebRtcIsac_kLpcShapeLeftRecPointUb12;
330 quantizationStepSize = WebRtcIsac_kLpcShapeQStepSizeUb12;
331 interVecDim = UB_LPC_VEC_PER_FRAME;
336 leftRecPoint = WebRtcIsac_kLpcShapeLeftRecPointUb16;
337 quantizationStepSize = WebRtcIsac_kLpcShapeQStepSizeUb16;
338 interVecDim = UB16_LPC_VEC_PER_FRAME;
346 // Dequantize given the quantization indices
349 for(cntr = 0; cntr < UB_LPC_ORDER * interVecDim; cntr++)
351 *out++ = leftRecPoint[cntr] + *idx++ * quantizationStepSize;
357 /******************************************************************************
358 * WebRtcIsac_CorrelateIntraVec()
360 * This is the inverse of WebRtcIsac_DecorrelateIntraVec().
363 * -data : uncorrelated parameters.
364 * -bandwidth : indicates if the given LAR vectors belong
365 * to SWB-12kHz or SWB-16kHz.
368 * -out : correlated parametrs.
371 WebRtcIsac_CorrelateIntraVec(
380 const double* ptrData;
381 const double* intraVecDecorrMat;
387 numVec = UB_LPC_VEC_PER_FRAME;
388 intraVecDecorrMat = &WebRtcIsac_kIntraVecDecorrMatUb12[0][0];
393 numVec = UB16_LPC_VEC_PER_FRAME;
394 intraVecDecorrMat = &WebRtcIsac_kIintraVecDecorrMatUb16[0][0];
403 for(vecCntr = 0; vecCntr < numVec; vecCntr++)
405 for(colCntr = 0; colCntr < UB_LPC_ORDER; colCntr++)
408 for(rowCntr = 0; rowCntr < UB_LPC_ORDER; rowCntr++)
410 *out += ptrData[rowCntr] *
411 intraVecDecorrMat[rowCntr * UB_LPC_ORDER + colCntr];
415 ptrData += UB_LPC_ORDER;
420 /******************************************************************************
421 * WebRtcIsac_CorrelateInterVec()
423 * This is the inverse of WebRtcIsac_DecorrelateInterVec().
427 * -bandwidth : indicates if the given LAR vectors belong
428 * to SWB-12kHz or SWB-16kHz.
431 * -out : correlated parametrs.
434 WebRtcIsac_CorrelateInterVec(
443 double myVec[UB16_LPC_VEC_PER_FRAME];
444 const double* interVecDecorrMat;
450 interVecDim = UB_LPC_VEC_PER_FRAME;
451 interVecDecorrMat = &WebRtcIsac_kInterVecDecorrMatUb12[0][0];
456 interVecDim = UB16_LPC_VEC_PER_FRAME;
457 interVecDecorrMat = &WebRtcIsac_kInterVecDecorrMatUb16[0][0];
464 for(coeffCntr = 0; coeffCntr < UB_LPC_ORDER; coeffCntr++)
466 for(rowCntr = 0; rowCntr < interVecDim; rowCntr++)
469 for(colCntr = 0; colCntr < interVecDim; colCntr++)
471 myVec[rowCntr] += data[coeffCntr + colCntr * UB_LPC_ORDER] * //*ptrData *
472 interVecDecorrMat[rowCntr * interVecDim + colCntr];
473 //ptrData += UB_LPC_ORDER;
477 for(rowCntr = 0; rowCntr < interVecDim; rowCntr++)
479 out[coeffCntr + rowCntr * UB_LPC_ORDER] = myVec[rowCntr];
485 /******************************************************************************
486 * WebRtcIsac_AddLarMean()
488 * This is the inverse of WebRtcIsac_RemoveLarMean()
491 * -data : pointer to mean-removed LAR:s.
492 * -bandwidth : indicates if the given LAR vectors belong
493 * to SWB-12kHz or SWB-16kHz.
496 * -data : pointer to LARs.
499 WebRtcIsac_AddLarMean(
506 const double* meanLAR;
512 numVec = UB_LPC_VEC_PER_FRAME;
513 meanLAR = WebRtcIsac_kMeanLarUb12;
518 numVec = UB16_LPC_VEC_PER_FRAME;
519 meanLAR = WebRtcIsac_kMeanLarUb16;
526 for(vecCntr = 0; vecCntr < numVec; vecCntr++)
528 for(coeffCntr = 0; coeffCntr < UB_LPC_ORDER; coeffCntr++)
530 *data++ += meanLAR[coeffCntr];
536 /******************************************************************************
537 * WebRtcIsac_ToLogDomainRemoveMean()
539 * Transform the LPC gain to log domain then remove the mean value.
542 * -lpcGain : pointer to LPC Gain, expecting 6 LPC gains
545 * -lpcGain : mean-removed in log domain.
548 WebRtcIsac_ToLogDomainRemoveMean(
552 for(coeffCntr = 0; coeffCntr < UB_LPC_GAIN_DIM; coeffCntr++)
554 data[coeffCntr] = log(data[coeffCntr]) - WebRtcIsac_kMeanLpcGain;
560 /******************************************************************************
561 * WebRtcIsac_DecorrelateLPGain()
563 * Decorrelate LPC gains. There are 6 LPC Gains per frame. This is like
564 * multiplying gain vector with decorrelating matrix.
567 * -data : LPC gain in log-domain with mean removed.
570 * -out : decorrelated parameters.
572 int16_t WebRtcIsac_DecorrelateLPGain(
579 for(colCntr = 0; colCntr < UB_LPC_GAIN_DIM; colCntr++)
582 for(rowCntr = 0; rowCntr < UB_LPC_GAIN_DIM; rowCntr++)
584 *out += data[rowCntr] * WebRtcIsac_kLpcGainDecorrMat[rowCntr][colCntr];
591 /******************************************************************************
592 * WebRtcIsac_QuantizeLpcGain()
594 * Quantize the decorrelated log-domain gains.
597 * -lpcGain : uncorrelated LPC gains.
600 * -idx : quantization indices
601 * -lpcGain : quantized value of the inpt.
603 double WebRtcIsac_QuantizeLpcGain(
608 for(coeffCntr = 0; coeffCntr < UB_LPC_GAIN_DIM; coeffCntr++)
610 *idx = (int)floor((*data - WebRtcIsac_kLeftRecPointLpcGain[coeffCntr]) /
611 WebRtcIsac_kQSizeLpcGain + 0.5);
617 else if(*idx >= WebRtcIsac_kNumQCellLpcGain[coeffCntr])
619 *idx = WebRtcIsac_kNumQCellLpcGain[coeffCntr] - 1;
621 *data = WebRtcIsac_kLeftRecPointLpcGain[coeffCntr] + *idx *
622 WebRtcIsac_kQSizeLpcGain;
630 /******************************************************************************
631 * WebRtcIsac_DequantizeLpcGain()
633 * Get the quantized values given the quantization indices.
636 * -idx : pointer to quantization indices.
639 * -lpcGains : quantized values of the given parametes.
641 int16_t WebRtcIsac_DequantizeLpcGain(
646 for(coeffCntr = 0; coeffCntr < UB_LPC_GAIN_DIM; coeffCntr++)
648 *out = WebRtcIsac_kLeftRecPointLpcGain[coeffCntr] + *idx *
649 WebRtcIsac_kQSizeLpcGain;
656 /******************************************************************************
657 * WebRtcIsac_CorrelateLpcGain()
659 * This is the inverse of WebRtcIsac_DecorrelateLPGain().
662 * -data : decorrelated parameters.
665 * -out : correlated parameters.
667 int16_t WebRtcIsac_CorrelateLpcGain(
674 for(rowCntr = 0; rowCntr < UB_LPC_GAIN_DIM; rowCntr++)
677 for(colCntr = 0; colCntr < UB_LPC_GAIN_DIM; colCntr++)
679 *out += WebRtcIsac_kLpcGainDecorrMat[rowCntr][colCntr] * data[colCntr];
688 /******************************************************************************
689 * WebRtcIsac_AddMeanToLinearDomain()
691 * This is the inverse of WebRtcIsac_ToLogDomainRemoveMean().
694 * -lpcGain : LPC gain in log-domain & mean removed
697 * -lpcGain : LPC gain in normal domain.
699 int16_t WebRtcIsac_AddMeanToLinearDomain(
703 for(coeffCntr = 0; coeffCntr < UB_LPC_GAIN_DIM; coeffCntr++)
705 lpcGains[coeffCntr] = exp(lpcGains[coeffCntr] + WebRtcIsac_kMeanLpcGain);