* Calculate rate distortion cost for quantizing with given codebook * * @return quantization distortion */
| 72 | * @return quantization distortion |
| 73 | */ |
| 74 | static av_always_inline float quantize_and_encode_band_cost_template( |
| 75 | struct AACEncContext *s, |
| 76 | PutBitContext *pb, const float *in, float *out, |
| 77 | const float *scaled, int size, int scale_idx, |
| 78 | int cb, const float lambda, const float uplim, |
| 79 | int *bits, float *energy, int BT_ZERO, int BT_UNSIGNED, |
| 80 | int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, |
| 81 | const float ROUNDING) |
| 82 | { |
| 83 | const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512; |
| 84 | const float Q = ff_aac_pow2sf_tab [q_idx]; |
| 85 | const float Q34 = ff_aac_pow34sf_tab[q_idx]; |
| 86 | const float IQ = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512]; |
| 87 | const float CLIPPED_ESCAPE = 165140.0f*IQ; |
| 88 | float cost = 0; |
| 89 | float qenergy = 0; |
| 90 | const int dim = BT_PAIR ? 2 : 4; |
| 91 | int resbits = 0; |
| 92 | int off; |
| 93 | |
| 94 | if (BT_ZERO || BT_NOISE || BT_STEREO) { |
| 95 | for (int i = 0; i < size; i++) |
| 96 | cost += in[i]*in[i]; |
| 97 | if (bits) |
| 98 | *bits = 0; |
| 99 | if (energy) |
| 100 | *energy = qenergy; |
| 101 | if (out) { |
| 102 | for (int i = 0; i < size; i += dim) |
| 103 | for (int j = 0; j < dim; j++) |
| 104 | out[i+j] = 0.0f; |
| 105 | } |
| 106 | return cost * lambda; |
| 107 | } |
| 108 | if (!scaled) { |
| 109 | s->aacdsp.abs_pow34(s->scoefs, in, size); |
| 110 | scaled = s->scoefs; |
| 111 | } |
| 112 | s->aacdsp.quant_bands(s->qcoefs, in, scaled, size, !BT_UNSIGNED, aac_cb_maxval[cb], Q34, ROUNDING); |
| 113 | if (BT_UNSIGNED) { |
| 114 | off = 0; |
| 115 | } else { |
| 116 | off = aac_cb_maxval[cb]; |
| 117 | } |
| 118 | for (int i = 0; i < size; i += dim) { |
| 119 | const float *vec; |
| 120 | int *quants = s->qcoefs + i; |
| 121 | int curidx = 0; |
| 122 | int curbits; |
| 123 | float quantized, rd = 0.0f; |
| 124 | for (int j = 0; j < dim; j++) { |
| 125 | curidx *= aac_cb_range[cb]; |
| 126 | curidx += quants[j] + off; |
| 127 | } |
| 128 | curbits = ff_aac_spectral_bits[cb-1][curidx]; |
| 129 | vec = &ff_aac_codebook_vectors[cb-1][curidx*dim]; |
| 130 | if (BT_UNSIGNED) { |
| 131 | for (int j = 0; j < dim; j++) { |