Revision 1676b099 libavcodec/aaccoder.c

View differences:

libavcodec/aaccoder.c
99 99
 *
100 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 104
                                PutBitContext *pb, const float *in,
104 105
                                const float *scaled, int size, int scale_idx,
105 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 110
    const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
109 111
    const float  Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
110 112
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
111 113
    int i, j, k;
112 114
    float cost = 0;
113
    const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
115
    const int dim = BT_PAIR ? 2 : 4;
114 116
    int resbits = 0;
115 117
    const float  Q34 = sqrtf(Q * sqrtf(Q));
116 118
    const int range  = aac_cb_range[cb];
117 119
    const int maxval = aac_cb_maxval[cb];
118 120
    int off;
119 121

  
120
    if (!cb) {
122
    if (BT_ZERO) {
121 123
        for (i = 0; i < size; i++)
122 124
            cost += in[i]*in[i];
123 125
        if (bits)
......
128 130
        abs_pow34_v(s->scoefs, in, size);
129 131
        scaled = s->scoefs;
130 132
    }
131
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
132
    if (IS_CODEBOOK_UNSIGNED(cb)) {
133
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
134
    if (BT_UNSIGNED) {
133 135
        off = 0;
134 136
    } else {
135 137
        off = maxval;
......
146 148
        }
147 149
            curbits =  ff_aac_spectral_bits[cb-1][curidx];
148 150
            vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
149
            if (IS_CODEBOOK_UNSIGNED(cb)) {
151
            if (BT_UNSIGNED) {
150 152
                for (k = 0; k < dim; k++) {
151 153
                    float t = fabsf(in[i+k]);
152 154
                    float di;
153
                    if (vec[k] == 64.0f) { //FIXME: slow
155
                    if (BT_ESC && vec[k] == 64.0f) { //FIXME: slow
154 156
                        if (t >= CLIPPED_ESCAPE) {
155 157
                            di = t - CLIPPED_ESCAPE;
156 158
                            curbits += 21;
......
178 180
            return uplim;
179 181
        if (pb) {
180 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 184
            for (j = 0; j < dim; j++)
183 185
                if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
184 186
                    put_bits(pb, 1, in[i+j] < 0.0f);
185
        if (cb == ESC_BT) {
187
        if (BT_ESC) {
186 188
            for (j = 0; j < 2; j++) {
187 189
                if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
188 190
                    int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
......
200 202
        *bits = resbits;
201 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 253
static float quantize_band_cost(struct AACEncContext *s, const float *in,
204 254
                                const float *scaled, int size, int scale_idx,
205 255
                                int cb, const float lambda, const float uplim,

Also available in: Unified diff