Mercurial > libavcodec.hg
comparison aaccoder.c @ 12178:b3c39e9d4d3e libavcodec
aacenc: Template quantize_and_encode_band_cost().
| author | alexc |
|---|---|
| date | Fri, 16 Jul 2010 20:02:46 +0000 |
| parents | 081702713f47 |
| children | fca82e8683d4 |
comparison
equal
deleted
inserted
replaced
| 12177:a39530f3dc94 | 12178:b3c39e9d4d3e |
|---|---|
| 97 /** | 97 /** |
| 98 * Calculate rate distortion cost for quantizing with given codebook | 98 * Calculate rate distortion cost for quantizing with given codebook |
| 99 * | 99 * |
| 100 * @return quantization distortion | 100 * @return quantization distortion |
| 101 */ | 101 */ |
| 102 static float quantize_and_encode_band_cost(struct AACEncContext *s, | 102 static av_always_inline float quantize_and_encode_band_cost_template( |
| 103 struct AACEncContext *s, | |
| 103 PutBitContext *pb, const float *in, | 104 PutBitContext *pb, const float *in, |
| 104 const float *scaled, int size, int scale_idx, | 105 const float *scaled, int size, int scale_idx, |
| 105 int cb, const float lambda, const float uplim, | 106 int cb, const float lambda, const float uplim, |
| 106 int *bits) | 107 int *bits, int BT_ZERO, int BT_UNSIGNED, |
| 108 int BT_PAIR, int BT_ESC) | |
| 107 { | 109 { |
| 108 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; | 110 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; |
| 109 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512]; | 111 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512]; |
| 110 const float CLIPPED_ESCAPE = 165140.0f*IQ; | 112 const float CLIPPED_ESCAPE = 165140.0f*IQ; |
| 111 int i, j, k; | 113 int i, j, k; |
| 112 float cost = 0; | 114 float cost = 0; |
| 113 const int dim = cb < FIRST_PAIR_BT ? 4 : 2; | 115 const int dim = BT_PAIR ? 2 : 4; |
| 114 int resbits = 0; | 116 int resbits = 0; |
| 115 const float Q34 = sqrtf(Q * sqrtf(Q)); | 117 const float Q34 = sqrtf(Q * sqrtf(Q)); |
| 116 const int range = aac_cb_range[cb]; | 118 const int range = aac_cb_range[cb]; |
| 117 const int maxval = aac_cb_maxval[cb]; | 119 const int maxval = aac_cb_maxval[cb]; |
| 118 int off; | 120 int off; |
| 119 | 121 |
| 120 if (!cb) { | 122 if (BT_ZERO) { |
| 121 for (i = 0; i < size; i++) | 123 for (i = 0; i < size; i++) |
| 122 cost += in[i]*in[i]; | 124 cost += in[i]*in[i]; |
| 123 if (bits) | 125 if (bits) |
| 124 *bits = 0; | 126 *bits = 0; |
| 125 return cost * lambda; | 127 return cost * lambda; |
| 126 } | 128 } |
| 127 if (!scaled) { | 129 if (!scaled) { |
| 128 abs_pow34_v(s->scoefs, in, size); | 130 abs_pow34_v(s->scoefs, in, size); |
| 129 scaled = s->scoefs; | 131 scaled = s->scoefs; |
| 130 } | 132 } |
| 131 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval); | 133 quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval); |
| 132 if (IS_CODEBOOK_UNSIGNED(cb)) { | 134 if (BT_UNSIGNED) { |
| 133 off = 0; | 135 off = 0; |
| 134 } else { | 136 } else { |
| 135 off = maxval; | 137 off = maxval; |
| 136 } | 138 } |
| 137 for (i = 0; i < size; i += dim) { | 139 for (i = 0; i < size; i += dim) { |
| 144 curidx *= range; | 146 curidx *= range; |
| 145 curidx += quants[j] + off; | 147 curidx += quants[j] + off; |
| 146 } | 148 } |
| 147 curbits = ff_aac_spectral_bits[cb-1][curidx]; | 149 curbits = ff_aac_spectral_bits[cb-1][curidx]; |
| 148 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; | 150 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; |
| 149 if (IS_CODEBOOK_UNSIGNED(cb)) { | 151 if (BT_UNSIGNED) { |
| 150 for (k = 0; k < dim; k++) { | 152 for (k = 0; k < dim; k++) { |
| 151 float t = fabsf(in[i+k]); | 153 float t = fabsf(in[i+k]); |
| 152 float di; | 154 float di; |
| 153 if (vec[k] == 64.0f) { //FIXME: slow | 155 if (BT_ESC && vec[k] == 64.0f) { //FIXME: slow |
| 154 if (t >= CLIPPED_ESCAPE) { | 156 if (t >= CLIPPED_ESCAPE) { |
| 155 di = t - CLIPPED_ESCAPE; | 157 di = t - CLIPPED_ESCAPE; |
| 156 curbits += 21; | 158 curbits += 21; |
| 157 } else { | 159 } else { |
| 158 int c = av_clip(quant(t, Q), 0, 8191); | 160 int c = av_clip(quant(t, Q), 0, 8191); |
| 176 resbits += curbits; | 178 resbits += curbits; |
| 177 if (cost >= uplim) | 179 if (cost >= uplim) |
| 178 return uplim; | 180 return uplim; |
| 179 if (pb) { | 181 if (pb) { |
| 180 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]); | 182 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]); |
| 181 if (IS_CODEBOOK_UNSIGNED(cb)) | 183 if (BT_UNSIGNED) |
| 182 for (j = 0; j < dim; j++) | 184 for (j = 0; j < dim; j++) |
| 183 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f) | 185 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f) |
| 184 put_bits(pb, 1, in[i+j] < 0.0f); | 186 put_bits(pb, 1, in[i+j] < 0.0f); |
| 185 if (cb == ESC_BT) { | 187 if (BT_ESC) { |
| 186 for (j = 0; j < 2; j++) { | 188 for (j = 0; j < 2; j++) { |
| 187 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) { | 189 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) { |
| 188 int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191); | 190 int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191); |
| 189 int len = av_log2(coef); | 191 int len = av_log2(coef); |
| 190 | 192 |
| 198 | 200 |
| 199 if (bits) | 201 if (bits) |
| 200 *bits = resbits; | 202 *bits = resbits; |
| 201 return cost; | 203 return cost; |
| 202 } | 204 } |
| 205 | |
| 206 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC) \ | |
| 207 static float quantize_and_encode_band_cost_ ## NAME( \ | |
| 208 struct AACEncContext *s, \ | |
| 209 PutBitContext *pb, const float *in, \ | |
| 210 const float *scaled, int size, int scale_idx, \ | |
| 211 int cb, const float lambda, const float uplim, \ | |
| 212 int *bits) { \ | |
| 213 return quantize_and_encode_band_cost_template( \ | |
| 214 s, pb, in, scaled, size, scale_idx, \ | |
| 215 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \ | |
| 216 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC); \ | |
| 217 } | |
| 218 | |
| 219 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0) | |
| 220 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0) | |
| 221 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0) | |
| 222 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0) | |
| 223 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0) | |
| 224 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1) | |
| 225 | |
| 226 static float (*quantize_and_encode_band_cost_arr[])( | |
| 227 struct AACEncContext *s, | |
| 228 PutBitContext *pb, const float *in, | |
| 229 const float *scaled, int size, int scale_idx, | |
| 230 int cb, const float lambda, const float uplim, | |
| 231 int *bits) = { | |
| 232 quantize_and_encode_band_cost_ZERO, | |
| 233 quantize_and_encode_band_cost_SQUAD, | |
| 234 quantize_and_encode_band_cost_SQUAD, | |
| 235 quantize_and_encode_band_cost_UQUAD, | |
| 236 quantize_and_encode_band_cost_UQUAD, | |
| 237 quantize_and_encode_band_cost_SPAIR, | |
| 238 quantize_and_encode_band_cost_SPAIR, | |
| 239 quantize_and_encode_band_cost_UPAIR, | |
| 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_ESC, | |
| 244 }; | |
| 245 | |
| 246 #define quantize_and_encode_band_cost( \ | |
| 247 s, pb, in, scaled, size, scale_idx, cb, \ | |
| 248 lambda, uplim, bits) \ | |
| 249 quantize_and_encode_band_cost_arr[cb]( \ | |
| 250 s, pb, in, scaled, size, scale_idx, cb, \ | |
| 251 lambda, uplim, bits) | |
| 252 | |
| 203 static float quantize_band_cost(struct AACEncContext *s, const float *in, | 253 static float quantize_band_cost(struct AACEncContext *s, const float *in, |
| 204 const float *scaled, int size, int scale_idx, | 254 const float *scaled, int size, int scale_idx, |
| 205 int cb, const float lambda, const float uplim, | 255 int cb, const float lambda, const float uplim, |
| 206 int *bits) | 256 int *bits) |
| 207 { | 257 { |
