This commit moves the quantizer to a separate header file. This allows the quantizer to be used from a separate files outside of aaccoder without having to put another function pointer and will result in a slight speedup as the compiler can do more optimizations. This is required for commits following.
Unified · Split
| @@ -254,6 +254,7 @@ typedef struct SingleChannelElement { · 253 unmodified lines | ||
| 254 | 254 | DECLARE_ALIGNED(32, INTFLOAT, saved)[1536]; ///< overlap |
| 255 | 255 | DECLARE_ALIGNED(32, INTFLOAT, ret_buf)[2048]; ///< PCM output buffer |
| 256 | 256 | DECLARE_ALIGNED(16, INTFLOAT, ltp_state)[3072]; ///< time signal for LTP |
| 257 | + DECLARE_ALIGNED(32, AAC_FLOAT, pqcoeffs)[1024]; ///< quantization error of coefs (used by encoder) | |
| 257 | 258 | PredictorState predictor_state[MAX_PREDICTORS]; |
| 258 | 259 | INTFLOAT *ret; ///< PCM output |
| 259 | 260 | } SingleChannelElement; |
| @@ -41,6 +41,7 @@ · 40 unmodified lines | ||
| 41 | 41 | #include "aactab.h" |
| 42 | 42 | #include "aacenctab.h" |
| 43 | 43 | #include "aacenc_utils.h" |
| 44 | +#include "aacenc_quantization.h" | |
| 44 | 45 | #include "aac_tablegen_decl.h" |
| 45 | 46 | |
| 46 | 47 | /** Frequency in Hz for lower limit of noise substitution **/ |
| @@ -55,223 +56,6 @@ · 8 unmodified lines | ||
| 55 | 56 | * excessive PNS and little PNS usage. */ |
| 56 | 57 | #define NOISE_LAMBDA_NUMERATOR 252.1f |
| 57 | 58 | |
| 58 | −/** Frequency in Hz for lower limit of intensity stereo **/ | |
| 59 | −#define INT_STEREO_LOW_LIMIT 6100 | |
| 60 | − | |
| 61 | −/** | |
| 62 | − * Calculate rate distortion cost for quantizing with given codebook | |
| 63 | − * | |
| 64 | − * @return quantization distortion | |
| 65 | − */ | |
| 66 | −static av_always_inline float quantize_and_encode_band_cost_template( | |
| 67 | − struct AACEncContext *s, | |
| 68 | − PutBitContext *pb, const float *in, | |
| 69 | − const float *scaled, int size, int scale_idx, | |
| 70 | − int cb, const float lambda, const float uplim, | |
| 71 | − int *bits, int BT_ZERO, int BT_UNSIGNED, | |
| 72 | − int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, | |
| 73 | − const float ROUNDING) | |
| 74 | −{ | |
| 75 | − const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512; | |
| 76 | − const float Q = ff_aac_pow2sf_tab [q_idx]; | |
| 77 | − const float Q34 = ff_aac_pow34sf_tab[q_idx]; | |
| 78 | − const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; | |
| 79 | − const float CLIPPED_ESCAPE = 165140.0f*IQ; | |
| 80 | − int i, j; | |
| 81 | − float cost = 0; | |
| 82 | − const int dim = BT_PAIR ? 2 : 4; | |
| 83 | − int resbits = 0; | |
| 84 | − int off; | |
| 85 | − | |
| 86 | − if (BT_ZERO || BT_NOISE || BT_STEREO) { | |
| 87 | − for (i = 0; i < size; i++) | |
| 88 | − cost += in[i]*in[i]; | |
| 89 | − if (bits) | |
| 90 | − *bits = 0; | |
| 91 | − return cost * lambda; | |
| 92 | − } | |
| 93 | − if (!scaled) { | |
| 94 | − abs_pow34_v(s->scoefs, in, size); | |
| 95 | − scaled = s->scoefs; | |
| 96 | − } | |
| 97 | − quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, aac_cb_maxval[cb], ROUNDING); | |
| 98 | − if (BT_UNSIGNED) { | |
| 99 | − off = 0; | |
| 100 | − } else { | |
| 101 | − off = aac_cb_maxval[cb]; | |
| 102 | − } | |
| 103 | − for (i = 0; i < size; i += dim) { | |
| 104 | − const float *vec; | |
| 105 | − int *quants = s->qcoefs + i; | |
| 106 | − int curidx = 0; | |
| 107 | − int curbits; | |
| 108 | − float rd = 0.0f; | |
| 109 | − for (j = 0; j < dim; j++) { | |
| 110 | − curidx *= aac_cb_range[cb]; | |
| 111 | − curidx += quants[j] + off; | |
| 112 | − } | |
| 113 | − curbits = ff_aac_spectral_bits[cb-1][curidx]; | |
| 114 | − vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; | |
| 115 | − if (BT_UNSIGNED) { | |
| 116 | − for (j = 0; j < dim; j++) { | |
| 117 | − float t = fabsf(in[i+j]); | |
| 118 | − float di; | |
| 119 | − if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow | |
| 120 | − if (t >= CLIPPED_ESCAPE) { | |
| 121 | − di = t - CLIPPED_ESCAPE; | |
| 122 | − curbits += 21; | |
| 123 | − } else { | |
| 124 | − int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13); | |
| 125 | − di = t - c*cbrtf(c)*IQ; | |
| 126 | − curbits += av_log2(c)*2 - 4 + 1; | |
| 127 | − } | |
| 128 | − } else { | |
| 129 | − di = t - vec[j]*IQ; | |
| 130 | − } | |
| 131 | − if (vec[j] != 0.0f) | |
| 132 | − curbits++; | |
| 133 | − rd += di*di; | |
| 134 | − } | |
| 135 | − } else { | |
| 136 | − for (j = 0; j < dim; j++) { | |
| 137 | − float di = in[i+j] - vec[j]*IQ; | |
| 138 | − rd += di*di; | |
| 139 | − } | |
| 140 | − } | |
| 141 | − cost += rd * lambda + curbits; | |
| 142 | − resbits += curbits; | |
| 143 | − if (cost >= uplim) | |
| 144 | − return uplim; | |
| 145 | − if (pb) { | |
| 146 | − put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]); | |
| 147 | − if (BT_UNSIGNED) | |
| 148 | − for (j = 0; j < dim; j++) | |
| 149 | − if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f) | |
| 150 | − put_bits(pb, 1, in[i+j] < 0.0f); | |
| 151 | − if (BT_ESC) { | |
| 152 | − for (j = 0; j < 2; j++) { | |
| 153 | − if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) { | |
| 154 | − int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13); | |
| 155 | − int len = av_log2(coef); | |
| 156 | − | |
| 157 | − put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); | |
| 158 | − put_sbits(pb, len, coef); | |
| 159 | − } | |
| 160 | − } | |
| 161 | − } | |
| 162 | − } | |
| 163 | − } | |
| 164 | − | |
| 165 | − if (bits) | |
| 166 | − *bits = resbits; | |
| 167 | − return cost; | |
| 168 | −} | |
| 169 | − | |
| 170 | −static float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, | |
| 171 | − const float *in, const float *scaled, | |
| 172 | − int size, int scale_idx, int cb, | |
| 173 | − const float lambda, const float uplim, | |
| 174 | − int *bits) { | |
| 175 | − av_assert0(0); | |
| 176 | − return 0.0f; | |
| 177 | −} | |
| 178 | − | |
| 179 | −#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \ | |
| 180 | −static float quantize_and_encode_band_cost_ ## NAME( \ | |
| 181 | − struct AACEncContext *s, \ | |
| 182 | − PutBitContext *pb, const float *in, \ | |
| 183 | − const float *scaled, int size, int scale_idx, \ | |
| 184 | − int cb, const float lambda, const float uplim, \ | |
| 185 | − int *bits) { \ | |
| 186 | − return quantize_and_encode_band_cost_template( \ | |
| 187 | − s, pb, in, scaled, size, scale_idx, \ | |
| 188 | − BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \ | |
| 189 | − BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \ | |
| 190 | − ROUNDING); \ | |
| 191 | −} | |
| 192 | − | |
| 193 | −QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD) | |
| 194 | −QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD) | |
| 195 | −QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD) | |
| 196 | −QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD) | |
| 197 | −QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD) | |
| 198 | −QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD) | |
| 199 | −QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO) | |
| 200 | −QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD) | |
| 201 | −QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD) | |
| 202 | − | |
| 203 | −static float (*const quantize_and_encode_band_cost_arr[])( | |
| 204 | − struct AACEncContext *s, | |
| 205 | − PutBitContext *pb, const float *in, | |
| 206 | − const float *scaled, int size, int scale_idx, | |
| 207 | − int cb, const float lambda, const float uplim, | |
| 208 | − int *bits) = { | |
| 209 | − quantize_and_encode_band_cost_ZERO, | |
| 210 | − quantize_and_encode_band_cost_SQUAD, | |
| 211 | − quantize_and_encode_band_cost_SQUAD, | |
| 212 | − quantize_and_encode_band_cost_UQUAD, | |
| 213 | − quantize_and_encode_band_cost_UQUAD, | |
| 214 | − quantize_and_encode_band_cost_SPAIR, | |
| 215 | − quantize_and_encode_band_cost_SPAIR, | |
| 216 | − quantize_and_encode_band_cost_UPAIR, | |
| 217 | − quantize_and_encode_band_cost_UPAIR, | |
| 218 | − quantize_and_encode_band_cost_UPAIR, | |
| 219 | − quantize_and_encode_band_cost_UPAIR, | |
| 220 | − quantize_and_encode_band_cost_ESC, | |
| 221 | − quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ | |
| 222 | − quantize_and_encode_band_cost_NOISE, | |
| 223 | − quantize_and_encode_band_cost_STEREO, | |
| 224 | − quantize_and_encode_band_cost_STEREO, | |
| 225 | −}; | |
| 226 | − | |
| 227 | −static float (*const quantize_and_encode_band_cost_rtz_arr[])( | |
| 228 | − struct AACEncContext *s, | |
| 229 | − PutBitContext *pb, const float *in, | |
| 230 | − const float *scaled, int size, int scale_idx, | |
| 231 | − int cb, const float lambda, const float uplim, | |
| 232 | − int *bits) = { | |
| 233 | − quantize_and_encode_band_cost_ZERO, | |
| 234 | − quantize_and_encode_band_cost_SQUAD, | |
| 235 | − quantize_and_encode_band_cost_SQUAD, | |
| 236 | − quantize_and_encode_band_cost_UQUAD, | |
| 237 | − quantize_and_encode_band_cost_UQUAD, | |
| 238 | − quantize_and_encode_band_cost_SPAIR, | |
| 239 | − quantize_and_encode_band_cost_SPAIR, | |
| 240 | − quantize_and_encode_band_cost_UPAIR, | |
| 241 | − quantize_and_encode_band_cost_UPAIR, | |
| 242 | − quantize_and_encode_band_cost_UPAIR, | |
| 243 | − quantize_and_encode_band_cost_UPAIR, | |
| 244 | − quantize_and_encode_band_cost_ESC_RTZ, | |
| 245 | − quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ | |
| 246 | − quantize_and_encode_band_cost_NOISE, | |
| 247 | − quantize_and_encode_band_cost_STEREO, | |
| 248 | − quantize_and_encode_band_cost_STEREO, | |
| 249 | −}; | |
| 250 | − | |
| 251 | −#define quantize_and_encode_band_cost( \ | |
| 252 | − s, pb, in, scaled, size, scale_idx, cb, \ | |
| 253 | − lambda, uplim, bits, rtz) \ | |
| 254 | − ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \ | |
| 255 | − s, pb, in, scaled, size, scale_idx, cb, \ | |
| 256 | − lambda, uplim, bits) | |
| 257 | − | |
| 258 | −static float quantize_band_cost(struct AACEncContext *s, const float *in, | |
| 259 | − const float *scaled, int size, int scale_idx, | |
| 260 | − int cb, const float lambda, const float uplim, | |
| 261 | − int *bits, int rtz) | |
| 262 | −{ | |
| 263 | − return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx, | |
| 264 | − cb, lambda, uplim, bits, rtz); | |
| 265 | −} | |
| 266 | − | |
| 267 | −static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, | |
| 268 | − const float *in, int size, int scale_idx, | |
| 269 | − int cb, const float lambda, int rtz) | |
| 270 | −{ | |
| 271 | − quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda, | |
| 272 | − INFINITY, NULL, rtz); | |
| 273 | −} | |
| 274 | − | |
| 275 | 59 | /** |
| 276 | 60 | * structure used in optimal codebook search |
| 277 | 61 | */ |
| @@ -355,11 +355,14 @@ static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce) · 354 unmodified lines | ||
| 355 | 355 | continue; |
| 356 | 356 | } |
| 357 | 357 | for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) |
| 358 | − s->coder->quantize_and_encode_band(s, &s->pb, sce->coeffs + start + w2*128, | |
| 358 | + s->coder->quantize_and_encode_band(s, &s->pb, | |
| 359 | + &sce->coeffs[start + w2*128], | |
| 360 | + &sce->pqcoeffs[start + w2*128], | |
| 359 | 361 | sce->ics.swb_sizes[i], |
| 360 | 362 | sce->sf_idx[w*16 + i], |
| 361 | 363 | sce->band_type[w*16 + i], |
| 362 | − s->lambda, sce->ics.window_clipping[w]); | |
| 364 | + s->lambda, | |
| 365 | + sce->ics.window_clipping[w]); | |
| 363 | 366 | start += sce->ics.swb_sizes[i]; |
| 364 | 367 | } |
| 365 | 368 | } |
| @@ -55,7 +55,7 @@ typedef struct AACCoefficientsEncoder { · 54 unmodified lines | ||
| 55 | 55 | SingleChannelElement *sce, const float lambda); |
| 56 | 56 | void (*encode_window_bands_info)(struct AACEncContext *s, SingleChannelElement *sce, |
| 57 | 57 | int win, int group_len, const float lambda); |
| 58 | − void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, int size, | |
| 58 | + void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size, | |
| 59 | 59 | int scale_idx, int cb, const float lambda, int rtz); |
| 60 | 60 | void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce); |
| 61 | 61 | void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce); |
| @@ -0,0 +1,255 @@ | ||
| 1 | +/* | |
| 2 | + * AAC encoder intensity stereo | |
| 3 | + * Copyright (C) 2015 Rostislav Pehlivanov | |
| 4 | + * | |
| 5 | + * This file is part of FFmpeg. | |
| 6 | + * | |
| 7 | + * FFmpeg is free software; you can redistribute it and/or | |
| 8 | + * modify it under the terms of the GNU Lesser General Public | |
| 9 | + * License as published by the Free Software Foundation; either | |
| 10 | + * version 2.1 of the License, or (at your option) any later version. | |
| 11 | + * | |
| 12 | + * FFmpeg is distributed in the hope that it will be useful, | |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 | + * Lesser General Public License for more details. | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU Lesser General Public | |
| 18 | + * License along with FFmpeg; if not, write to the Free Software | |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 20 | + */ | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * @file | |
| 24 | + * AAC encoder quantizer | |
| 25 | + * @author Rostislav Pehlivanov ( atomnuker gmail com ) | |
| 26 | + */ | |
| 27 | + | |
| 28 | +#include "aactab.h" | |
| 29 | +#include "aacenc.h" | |
| 30 | +#include "aacenctab.h" | |
| 31 | +#include "aacenc_utils.h" | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * Calculate rate distortion cost for quantizing with given codebook | |
| 35 | + * | |
| 36 | + * @return quantization distortion | |
| 37 | + */ | |
| 38 | +static av_always_inline float quantize_and_encode_band_cost_template( | |
| 39 | + struct AACEncContext *s, | |
| 40 | + PutBitContext *pb, const float *in, float *out, | |
| 41 | + const float *scaled, int size, int scale_idx, | |
| 42 | + int cb, const float lambda, const float uplim, | |
| 43 | + int *bits, int BT_ZERO, int BT_UNSIGNED, | |
| 44 | + int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, | |
| 45 | + const float ROUNDING) | |
| 46 | +{ | |
| 47 | + const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512; | |
| 48 | + const float Q = ff_aac_pow2sf_tab [q_idx]; | |
| 49 | + const float Q34 = ff_aac_pow34sf_tab[q_idx]; | |
| 50 | + const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; | |
| 51 | + const float CLIPPED_ESCAPE = 165140.0f*IQ; | |
| 52 | + int i, j; | |
| 53 | + float cost = 0; | |
| 54 | + const int dim = BT_PAIR ? 2 : 4; | |
| 55 | + int resbits = 0; | |
| 56 | + int off; | |
| 57 | + | |
| 58 | + if (BT_ZERO || BT_NOISE || BT_STEREO) { | |
| 59 | + for (i = 0; i < size; i++) | |
| 60 | + cost += in[i]*in[i]; | |
| 61 | + if (bits) | |
| 62 | + *bits = 0; | |
| 63 | + if (out) { | |
| 64 | + for (i = 0; i < size; i += dim) | |
| 65 | + for (j = 0; j < dim; j++) | |
| 66 | + out[i+j] = 0.0f; | |
| 67 | + } | |
| 68 | + return cost * lambda; | |
| 69 | + } | |
| 70 | + if (!scaled) { | |
| 71 | + abs_pow34_v(s->scoefs, in, size); | |
| 72 | + scaled = s->scoefs; | |
| 73 | + } | |
| 74 | + quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, aac_cb_maxval[cb], ROUNDING); | |
| 75 | + if (BT_UNSIGNED) { | |
| 76 | + off = 0; | |
| 77 | + } else { | |
| 78 | + off = aac_cb_maxval[cb]; | |
| 79 | + } | |
| 80 | + for (i = 0; i < size; i += dim) { | |
| 81 | + const float *vec; | |
| 82 | + int *quants = s->qcoefs + i; | |
| 83 | + int curidx = 0; | |
| 84 | + int curbits; | |
| 85 | + float quantized, rd = 0.0f; | |
| 86 | + for (j = 0; j < dim; j++) { | |
| 87 | + curidx *= aac_cb_range[cb]; | |
| 88 | + curidx += quants[j] + off; | |
| 89 | + } | |
| 90 | + curbits = ff_aac_spectral_bits[cb-1][curidx]; | |
| 91 | + vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; | |
| 92 | + if (BT_UNSIGNED) { | |
| 93 | + for (j = 0; j < dim; j++) { | |
| 94 | + float t = fabsf(in[i+j]); | |
| 95 | + float di; | |
| 96 | + if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow | |
| 97 | + if (t >= CLIPPED_ESCAPE) { | |
| 98 | + quantized = CLIPPED_ESCAPE; | |
| 99 | + curbits += 21; | |
| 100 | + } else { | |
| 101 | + int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13); | |
| 102 | + quantized = c*cbrtf(c)*IQ; | |
| 103 | + curbits += av_log2(c)*2 - 4 + 1; | |
| 104 | + } | |
| 105 | + } else { | |
| 106 | + quantized = vec[j]*IQ; | |
| 107 | + } | |
| 108 | + di = t - quantized; | |
| 109 | + if (out) | |
| 110 | + out[i+j] = copysignf(quantized, in[i+j]); | |
| 111 | + if (vec[j] != 0.0f) | |
| 112 | + curbits++; | |
| 113 | + rd += di*di; | |
| 114 | + } | |
| 115 | + } else { | |
| 116 | + for (j = 0; j < dim; j++) { | |
| 117 | + quantized = vec[j]*IQ; | |
| 118 | + if (out) | |
| 119 | + out[i+j] = quantized; | |
| 120 | + rd += (in[i+j] - quantized)*(in[i+j] - quantized); | |
| 121 | + } | |
| 122 | + } | |
| 123 | + cost += rd * lambda + curbits; | |
| 124 | + resbits += curbits; | |
| 125 | + if (cost >= uplim) | |
| 126 | + return uplim; | |
| 127 | + if (pb) { | |
| 128 | + put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]); | |
| 129 | + if (BT_UNSIGNED) | |
| 130 | + for (j = 0; j < dim; j++) | |
| 131 | + if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f) | |
| 132 | + put_bits(pb, 1, in[i+j] < 0.0f); | |
| 133 | + if (BT_ESC) { | |
| 134 | + for (j = 0; j < 2; j++) { | |
| 135 | + if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) { | |
| 136 | + int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13); | |
| 137 | + int len = av_log2(coef); | |
| 138 | + | |
| 139 | + put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2); | |
| 140 | + put_sbits(pb, len, coef); | |
| 141 | + } | |
| 142 | + } | |
| 143 | + } | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + if (bits) | |
| 148 | + *bits = resbits; | |
| 149 | + return cost; | |
| 150 | +} | |
| 151 | + | |
| 152 | +static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, | |
| 153 | + const float *in, float *quant, const float *scaled, | |
| 154 | + int size, int scale_idx, int cb, | |
| 155 | + const float lambda, const float uplim, | |
| 156 | + int *bits) { | |
| 157 | + av_assert0(0); | |
| 158 | + return 0.0f; | |
| 159 | +} | |
| 160 | + | |
| 161 | +#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \ | |
| 162 | +static float quantize_and_encode_band_cost_ ## NAME( \ | |
| 163 | + struct AACEncContext *s, \ | |
| 164 | + PutBitContext *pb, const float *in, float *quant, \ | |
| 165 | + const float *scaled, int size, int scale_idx, \ | |
| 166 | + int cb, const float lambda, const float uplim, \ | |
| 167 | + int *bits) { \ | |
| 168 | + return quantize_and_encode_band_cost_template( \ | |
| 169 | + s, pb, in, quant, scaled, size, scale_idx, \ | |
| 170 | + BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \ | |
| 171 | + BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \ | |
| 172 | + ROUNDING); \ | |
| 173 | +} | |
| 174 | + | |
| 175 | +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0, 0, 0, ROUND_STANDARD) | |
| 176 | +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD) | |
| 177 | +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD) | |
| 178 | +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD) | |
| 179 | +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD) | |
| 180 | +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1, 0, 0, ROUND_STANDARD) | |
| 181 | +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO) | |
| 182 | +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD) | |
| 183 | +QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD) | |
| 184 | + | |
| 185 | +static float (*const quantize_and_encode_band_cost_arr[])( | |
| 186 | + struct AACEncContext *s, | |
| 187 | + PutBitContext *pb, const float *in, float *quant, | |
| 188 | + const float *scaled, int size, int scale_idx, | |
| 189 | + int cb, const float lambda, const float uplim, | |
| 190 | + int *bits) = { | |
| 191 | + quantize_and_encode_band_cost_ZERO, | |
| 192 | + quantize_and_encode_band_cost_SQUAD, | |
| 193 | + quantize_and_encode_band_cost_SQUAD, | |
| 194 | + quantize_and_encode_band_cost_UQUAD, | |
| 195 | + quantize_and_encode_band_cost_UQUAD, | |
| 196 | + quantize_and_encode_band_cost_SPAIR, | |
| 197 | + quantize_and_encode_band_cost_SPAIR, | |
| 198 | + quantize_and_encode_band_cost_UPAIR, | |
| 199 | + quantize_and_encode_band_cost_UPAIR, | |
| 200 | + quantize_and_encode_band_cost_UPAIR, | |
| 201 | + quantize_and_encode_band_cost_UPAIR, | |
| 202 | + quantize_and_encode_band_cost_ESC, | |
| 203 | + quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ | |
| 204 | + quantize_and_encode_band_cost_NOISE, | |
| 205 | + quantize_and_encode_band_cost_STEREO, | |
| 206 | + quantize_and_encode_band_cost_STEREO, | |
| 207 | +}; | |
| 208 | + | |
| 209 | +static float (*const quantize_and_encode_band_cost_rtz_arr[])( | |
| 210 | + struct AACEncContext *s, | |
| 211 | + PutBitContext *pb, const float *in, float *quant, | |
| 212 | + const float *scaled, int size, int scale_idx, | |
| 213 | + int cb, const float lambda, const float uplim, | |
| 214 | + int *bits) = { | |
| 215 | + quantize_and_encode_band_cost_ZERO, | |
| 216 | + quantize_and_encode_band_cost_SQUAD, | |
| 217 | + quantize_and_encode_band_cost_SQUAD, | |
| 218 | + quantize_and_encode_band_cost_UQUAD, | |
| 219 | + quantize_and_encode_band_cost_UQUAD, | |
| 220 | + quantize_and_encode_band_cost_SPAIR, | |
| 221 | + quantize_and_encode_band_cost_SPAIR, | |
| 222 | + quantize_and_encode_band_cost_UPAIR, | |
| 223 | + quantize_and_encode_band_cost_UPAIR, | |
| 224 | + quantize_and_encode_band_cost_UPAIR, | |
| 225 | + quantize_and_encode_band_cost_UPAIR, | |
| 226 | + quantize_and_encode_band_cost_ESC_RTZ, | |
| 227 | + quantize_and_encode_band_cost_NONE, /* CB 12 doesn't exist */ | |
| 228 | + quantize_and_encode_band_cost_NOISE, | |
| 229 | + quantize_and_encode_band_cost_STEREO, | |
| 230 | + quantize_and_encode_band_cost_STEREO, | |
| 231 | +}; | |
| 232 | + | |
| 233 | +#define quantize_and_encode_band_cost( \ | |
| 234 | + s, pb, in, quant, scaled, size, scale_idx, cb, \ | |
| 235 | + lambda, uplim, bits, rtz) \ | |
| 236 | + ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \ | |
| 237 | + s, pb, in, quant, scaled, size, scale_idx, cb, \ | |
| 238 | + lambda, uplim, bits) | |
| 239 | + | |
| 240 | +static inline float quantize_band_cost(struct AACEncContext *s, const float *in, | |
| 241 | + const float *scaled, int size, int scale_idx, | |
| 242 | + int cb, const float lambda, const float uplim, | |
| 243 | + int *bits, int rtz) | |
| 244 | +{ | |
| 245 | + return quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx, | |
| 246 | + cb, lambda, uplim, bits, rtz); | |
| 247 | +} | |
| 248 | + | |
| 249 | +static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, | |
| 250 | + const float *in, float *out, int size, int scale_idx, | |
| 251 | + int cb, const float lambda, int rtz) | |
| 252 | +{ | |
| 253 | + quantize_and_encode_band_cost(s, pb, in, out, NULL, size, scale_idx, cb, lambda, | |
| 254 | + INFINITY, NULL, rtz); | |
| 255 | +} | |