[FFmpeg-soc] [PATCH] aacenc: clean up quant sloppiness
Kostya
kostya.shishkov at gmail.com
Thu Jun 11 18:00:20 CEST 2009
On Thu, Jun 11, 2009 at 11:24:46AM -0400, Alex Converse wrote:
> Hi all,
>
> The quantizer code in the aac encoder is kind of sloppy. This cleans it up.
>
> Regards,
>
> Alex Converse
> commit 35d52275e36c88a429da06e4f6c6ffdab22e3863
> Author: Alex Converse <alex.converse at gmail.com>
> Date: Wed Apr 22 00:24:19 2009 -0400
>
> Clean up quant sloppiness.
> More cleanup
> More quant cleanup
>
> diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c
> index d3a2755..b1ed259 100644
> --- a/libavcodec/aaccoder.c
> +++ b/libavcodec/aaccoder.c
> @@ -59,20 +59,26 @@ static const uint8_t* run_value_bits[2] = {
> * @return absolute value of the quantized coefficient
> * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
> */
> +static av_always_inline int quant_clip(float coef, const float Q)
> +{
> + int ret = pow(coef * Q, 0.75) + 0.4054;
> + return FFMIN(ret, 8191);
> +}
> +
> static av_always_inline int quant(float coef, const float Q)
> {
> - return av_clip((int)(pow(fabsf(coef) * Q, 0.75) + 0.4054), 0, 8191);
> + return pow(coef * Q, 0.75) + 0.4054;
> }
>
> #if 1
>
> static av_always_inline int quant2(float coef, const float Q)
> {
> - return av_clip((int)(pow(fabsf(coef) * Q, 0.75)), 0, 8191);
> + return pow(coef * Q, 0.75);
> }
{
> -static const float aac_cb_range[12] = { 0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
> -static const float aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
> +static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
> +static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
}
Commit this change separately
> /**
> * Calculate rate distortion cost for quantizing with given codebook
> @@ -107,8 +113,8 @@ static float quantize_band_cost(const float *in, int size, int scale_idx, int cb
> int quants[4][2];
> mincost = 0.0f;
> for(j = 0; j < dim; j++){
> - quants[j][0] = quant2(in[i+j], Q);
> - quants[j][1] = quant (in[i+j], Q);
> + quants[j][0] = quant2(fabsf(in[i+j]), Q);
> + quants[j][1] = quant (fabsf(in[i+j]), Q);
> for(k = 0; k < 2; k++){
> quants[j][k] = FFMIN(quants[j][k], maxval);
> if(!IS_CODEBOOK_UNSIGNED(cb) && in[i+j] < 0.0f)
> @@ -151,7 +157,7 @@ static float quantize_band_cost(const float *in, int size, int scale_idx, int cb
> di = t - 165140.0f;
> curbits += 21;
> }else{
> - int c = quant(t, Q);
> + int c = quant_clip(t, Q);
> di = t - c*cbrt(c)*IQ;
> curbits += av_log2(c)*2 - 4 + 1;
> }
> @@ -210,8 +216,8 @@ static void quantize_and_encode_band(PutBitContext *pb, const float *in, int siz
> int quants[4][2];
> mincost = 0.0f;
> for(j = 0; j < dim; j++){
> - quants[j][0] = av_clip(quant2(in[i+j], Q), -maxval, maxval);
> - quants[j][1] = av_clip(quant (in[i+j], Q), -maxval, maxval);
> + quants[j][0] = quant2(fabsf(in[i+j]), Q);
> + quants[j][1] = quant (fabsf(in[i+j]), Q);
>
> for(k = 0; k < 2; k++){
> quants[j][k] = FFMIN(quants[j][k], maxval);
> if(!IS_CODEBOOK_UNSIGNED(cb) && in[i+j] < 0.0f)
> @@ -254,7 +260,7 @@ static void quantize_and_encode_band(PutBitContext *pb, const float *in, int siz
> di = t - 165140.0f;
> curbits += 21;
> }else{
> - int c = quant(t, Q);
> + int c = quant_clip(t, Q);
> di = t - c*cbrt(c)*IQ;
> curbits += av_log2(c)*2 - 4 + 1;
> }
> @@ -286,7 +292,7 @@ static void quantize_and_encode_band(PutBitContext *pb, const float *in, int siz
> if(cb == ESC_BT){
> for(j = 0; j < 2; j++){
> if(ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f){
> - int coef = quant(in[i+j], Q);
> + int coef = quant_clip(fabsf(in[i+j]), Q);
> int len = av_log2(coef);
>
> put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
> @@ -308,6 +314,7 @@ static float quantize_band_cost(const float *in, int size, int scale_idx, int cb
> const float lambda, const float uplim, int *bits)
> {
> const float Q = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
> + const float IQ = 1.0/Q;
> int i, j, k;
> float cost = 0;
> const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
> @@ -340,7 +347,7 @@ static float quantize_band_cost(const float *in, int size, int scale_idx, int cb
> di = t - 165140.0f;
> curbits += 21;
> }else{
> - int c = quant(t, 1.0/Q);
> + int c = quant_clip(t, IQ);
> di = t - c*cbrt(c)*Q;
> curbits += av_log2(c)*2 - 4 + 1;
> }
> @@ -413,7 +420,7 @@ static void quantize_and_encode_band(PutBitContext *pb, const float *in, int siz
> di = t - 165140.0f;
> curbits += 21;
> }else{
> - int c = quant(t, IQ);
> + int c = quant_clip(t, IQ);
> di = t - c*cbrt(c)*Q;
> curbits += av_log2(c)*2 - 4 + 1;
> }
> @@ -445,7 +452,7 @@ static void quantize_and_encode_band(PutBitContext *pb, const float *in, int siz
> if(cb == ESC_BT){
> for(j = 0; j < 2; j++){
> if(ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f){
> - int coef = quant(in[i+j], IQ);
> + int coef = quant_clip(fabsf(in[i+j]), IQ);
> int len = av_log2(coef);
>
> put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
No objections against introducing IQ where appropriate. Also commit
separately anytime.
As for having unlimited and limited quantizing - that's code
duplication. I think it's better to clip it after the call.
More information about the FFmpeg-soc
mailing list