120 lines
4.1 KiB
C
Raw Normal View History

/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
/******************************************************************
iLBC Speech Coder ANSI-C Source Code
WebRtcIlbcfix_StateSearch.c
******************************************************************/
#include "defines.h"
#include "constants.h"
#include "abs_quant.h"
/*----------------------------------------------------------------*
* encoding of start state
*---------------------------------------------------------------*/
void WebRtcIlbcfix_StateSearch(
IlbcEncoder *iLBCenc_inst,
/* (i) Encoder instance */
iLBC_bits *iLBC_encbits,/* (i/o) Encoded bits (output idxForMax
and idxVec, input state_first) */
int16_t *residual, /* (i) target residual vector */
int16_t *syntDenum, /* (i) lpc synthesis filter */
int16_t *weightDenum /* (i) weighting filter denuminator */
) {
Update a ton of audio code to use size_t more correctly and in general reduce use of int16_t/uint16_t. This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects. This was be reviewed and approved in pieces: https://codereview.webrtc.org/1224093003 https://codereview.webrtc.org/1224123002 https://codereview.webrtc.org/1224163002 https://codereview.webrtc.org/1225133003 https://codereview.webrtc.org/1225173002 https://codereview.webrtc.org/1227163003 https://codereview.webrtc.org/1227203003 https://codereview.webrtc.org/1227213002 https://codereview.webrtc.org/1227893002 https://codereview.webrtc.org/1228793004 https://codereview.webrtc.org/1228803003 https://codereview.webrtc.org/1228823002 https://codereview.webrtc.org/1228823003 https://codereview.webrtc.org/1228843002 https://codereview.webrtc.org/1230693002 https://codereview.webrtc.org/1231713002 The change is being landed as TBR to all the folks who reviewed the above. BUG=chromium:81439 TEST=none R=andrew@webrtc.org, pbos@webrtc.org TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher Review URL: https://codereview.webrtc.org/1230503003 . Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
size_t k, index;
int16_t maxVal;
int16_t scale, shift;
int32_t maxValsq;
int16_t scaleRes;
int16_t max;
int i;
/* Stack based */
int16_t numerator[1+LPC_FILTERORDER];
int16_t residualLongVec[2*STATE_SHORT_LEN_30MS+LPC_FILTERORDER];
int16_t sampleMa[2*STATE_SHORT_LEN_30MS];
int16_t *residualLong = &residualLongVec[LPC_FILTERORDER];
int16_t *sampleAr = residualLong;
/* Scale to maximum 12 bits to avoid saturation in circular convolution filter */
max = WebRtcSpl_MaxAbsValueW16(residual, iLBCenc_inst->state_short_len);
scaleRes = WebRtcSpl_GetSizeInBits(max)-12;
scaleRes = WEBRTC_SPL_MAX(0, scaleRes);
/* Set up the filter coefficients for the circular convolution */
for (i=0; i<LPC_FILTERORDER+1; i++) {
numerator[i] = (syntDenum[LPC_FILTERORDER-i]>>scaleRes);
}
/* Copy the residual to a temporary buffer that we can filter
* and set the remaining samples to zero.
*/
WEBRTC_SPL_MEMCPY_W16(residualLong, residual, iLBCenc_inst->state_short_len);
WebRtcSpl_MemSetW16(residualLong + iLBCenc_inst->state_short_len, 0, iLBCenc_inst->state_short_len);
/* Run the Zero-Pole filter (Ciurcular convolution) */
WebRtcSpl_MemSetW16(residualLongVec, 0, LPC_FILTERORDER);
Update a ton of audio code to use size_t more correctly and in general reduce use of int16_t/uint16_t. This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects. This was be reviewed and approved in pieces: https://codereview.webrtc.org/1224093003 https://codereview.webrtc.org/1224123002 https://codereview.webrtc.org/1224163002 https://codereview.webrtc.org/1225133003 https://codereview.webrtc.org/1225173002 https://codereview.webrtc.org/1227163003 https://codereview.webrtc.org/1227203003 https://codereview.webrtc.org/1227213002 https://codereview.webrtc.org/1227893002 https://codereview.webrtc.org/1228793004 https://codereview.webrtc.org/1228803003 https://codereview.webrtc.org/1228823002 https://codereview.webrtc.org/1228823003 https://codereview.webrtc.org/1228843002 https://codereview.webrtc.org/1230693002 https://codereview.webrtc.org/1231713002 The change is being landed as TBR to all the folks who reviewed the above. BUG=chromium:81439 TEST=none R=andrew@webrtc.org, pbos@webrtc.org TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher Review URL: https://codereview.webrtc.org/1230503003 . Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
WebRtcSpl_FilterMAFastQ12(residualLong, sampleMa, numerator,
LPC_FILTERORDER + 1,
iLBCenc_inst->state_short_len + LPC_FILTERORDER);
WebRtcSpl_MemSetW16(&sampleMa[iLBCenc_inst->state_short_len + LPC_FILTERORDER], 0, iLBCenc_inst->state_short_len - LPC_FILTERORDER);
WebRtcSpl_FilterARFastQ12(
sampleMa, sampleAr,
Match existing type usage better. This makes a variety of small changes to synchronize bits of code using different types, remove useless code or casts, and add explicit casts in some places previously doing implicit ones. For example: * Change a few type declarations to better match how the majority of code uses those objects. * Eliminate "< 0" check for unsigned values. * Replace "(float)sin(x)", where |x| is also a float, with "sinf(x)", and similar. * Add casts to uint32_t in many places timestamps were used and the existing code stored signed values into the unsigned objects. * Remove downcasts when the results would be passed to a larger type, e.g. calling "foo((int16_t)x)" with an int |x| when foo() takes an int instead of an int16_t. * Similarly, add casts when passing a larger type to a function taking a smaller one. * Add casts to int16_t when doing something like "int16_t = int16_t + int16_t" as the "+" operation would implicitly upconvert to int, and similar. * Use "false" instead of "0" for setting a bool. * Shift a few temp types when doing a multi-stage calculation involving typecasts, so as to put the most logical/semantically correct type possible into the temps. For example, when doing "int foo = int + int; size_t bar = (size_t)foo + size_t;", we might change |foo| to a size_t and move the cast if it makes more sense for |foo| to be represented as a size_t. BUG=none R=andrew@webrtc.org, asapersson@webrtc.org, henrika@webrtc.org, juberti@webrtc.org, kwiberg@webrtc.org TBR=andrew, asapersson, henrika Review URL: https://codereview.webrtc.org/1168753002 Cr-Commit-Position: refs/heads/master@{#9419}
2015-06-11 12:55:50 -07:00
syntDenum, LPC_FILTERORDER+1, 2 * iLBCenc_inst->state_short_len);
for(k=0;k<iLBCenc_inst->state_short_len;k++){
sampleAr[k] += sampleAr[k+iLBCenc_inst->state_short_len];
}
/* Find maximum absolute value in the vector */
maxVal=WebRtcSpl_MaxAbsValueW16(sampleAr, iLBCenc_inst->state_short_len);
/* Find the best index */
if ((((int32_t)maxVal)<<scaleRes)<23170) {
maxValsq=((int32_t)maxVal*maxVal)<<(2+2*scaleRes);
} else {
maxValsq=(int32_t)WEBRTC_SPL_WORD32_MAX;
}
index=0;
for (i=0;i<63;i++) {
if (maxValsq>=WebRtcIlbcfix_kChooseFrgQuant[i]) {
index=i+1;
} else {
i=63;
}
}
iLBC_encbits->idxForMax=index;
/* Rescale the vector before quantization */
scale=WebRtcIlbcfix_kScale[index];
if (index<27) { /* scale table is in Q16, fout[] is in Q(-1) and we want the result to be in Q11 */
shift=4;
} else { /* scale table is in Q21, fout[] is in Q(-1) and we want the result to be in Q11 */
shift=9;
}
/* Set up vectors for AbsQuant and rescale it with the scale factor */
WebRtcSpl_ScaleVectorWithSat(sampleAr, sampleAr, scale,
iLBCenc_inst->state_short_len, (int16_t)(shift-scaleRes));
/* Quantize the values in fout[] */
WebRtcIlbcfix_AbsQuant(iLBCenc_inst, iLBC_encbits, sampleAr, weightDenum);
return;
}