Statistics
| Branch: | Revision:

ffmpeg / libavcodec / aaccoder.c @ 1676b099

History | View | Annotate | Download (42.6 KB)

1 78e65cd7 Alex Converse
/*
2
 * AAC coefficients encoder
3
 * Copyright (C) 2008-2009 Konstantin Shishkov
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 ba87f080 Diego Biurrun
 * @file
24 78e65cd7 Alex Converse
 * AAC coefficients encoder
25
 */
26
27
/***********************************
28
 *              TODOs:
29
 * speedup quantizer selection
30
 * add sane pulse detection
31
 ***********************************/
32
33 144c5e3d Alex Converse
#include <float.h>
34 78e65cd7 Alex Converse
#include "avcodec.h"
35
#include "put_bits.h"
36
#include "aac.h"
37
#include "aacenc.h"
38
#include "aactab.h"
39
40
/** bits needed to code codebook run value for long windows */
41
static const uint8_t run_value_bits_long[64] = {
42
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
43
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5, 10,
44
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
45
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
46
};
47
48
/** bits needed to code codebook run value for short windows */
49
static const uint8_t run_value_bits_short[16] = {
50
    3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
51
};
52
53 99d61d34 Diego Biurrun
static const uint8_t *run_value_bits[2] = {
54 78e65cd7 Alex Converse
    run_value_bits_long, run_value_bits_short
55
};
56
57
58
/**
59
 * Quantize one coefficient.
60
 * @return absolute value of the quantized coefficient
61
 * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
62
 */
63
static av_always_inline int quant(float coef, const float Q)
64
{
65 3d51be01 Alex Converse
    float a = coef * Q;
66
    return sqrtf(a * sqrtf(a)) + 0.4054;
67 78e65cd7 Alex Converse
}
68
69 4d986b71 Alex Converse
static void quantize_bands(int *out, const float *in, const float *scaled,
70 99d61d34 Diego Biurrun
                           int size, float Q34, int is_signed, int maxval)
71 78e65cd7 Alex Converse
{
72
    int i;
73
    double qc;
74
    for (i = 0; i < size; i++) {
75
        qc = scaled[i] * Q34;
76 4d986b71 Alex Converse
        out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
77 78e65cd7 Alex Converse
        if (is_signed && in[i] < 0.0f) {
78 4d986b71 Alex Converse
            out[i] = -out[i];
79 78e65cd7 Alex Converse
        }
80
    }
81
}
82
83 99d61d34 Diego Biurrun
static void abs_pow34_v(float *out, const float *in, const int size)
84 78e65cd7 Alex Converse
{
85
#ifndef USE_REALLY_FULL_SEARCH
86
    int i;
87 3d51be01 Alex Converse
    for (i = 0; i < size; i++) {
88
        float a = fabsf(in[i]);
89
        out[i] = sqrtf(a * sqrtf(a));
90
    }
91 78e65cd7 Alex Converse
#endif /* USE_REALLY_FULL_SEARCH */
92
}
93
94
static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
95
static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
96
97
/**
98
 * Calculate rate distortion cost for quantizing with given codebook
99
 *
100
 * @return quantization distortion
101
 */
102 1676b099 Alex Converse
static av_always_inline float quantize_and_encode_band_cost_template(
103
                                struct AACEncContext *s,
104 508f092a Alex Converse
                                PutBitContext *pb, const float *in,
105 99d61d34 Diego Biurrun
                                const float *scaled, int size, int scale_idx,
106
                                int cb, const float lambda, const float uplim,
107 1676b099 Alex Converse
                                int *bits, int BT_ZERO, int BT_UNSIGNED,
108
                                int BT_PAIR, int BT_ESC)
109 78e65cd7 Alex Converse
{
110
    const float IQ = 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];
112
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
113
    int i, j, k;
114
    float cost = 0;
115 1676b099 Alex Converse
    const int dim = BT_PAIR ? 2 : 4;
116 78e65cd7 Alex Converse
    int resbits = 0;
117 3d51be01 Alex Converse
    const float  Q34 = sqrtf(Q * sqrtf(Q));
118 99d61d34 Diego Biurrun
    const int range  = aac_cb_range[cb];
119 78e65cd7 Alex Converse
    const int maxval = aac_cb_maxval[cb];
120 4d986b71 Alex Converse
    int off;
121 78e65cd7 Alex Converse
122 1676b099 Alex Converse
    if (BT_ZERO) {
123 fd257dc4 Alex Converse
        for (i = 0; i < size; i++)
124 0bd9aa44 Alex Converse
            cost += in[i]*in[i];
125 a5762c9b Alex Converse
        if (bits)
126
            *bits = 0;
127 0bd9aa44 Alex Converse
        return cost * lambda;
128 78e65cd7 Alex Converse
    }
129 508f092a Alex Converse
    if (!scaled) {
130
        abs_pow34_v(s->scoefs, in, size);
131
        scaled = s->scoefs;
132
    }
133 1676b099 Alex Converse
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
134
    if (BT_UNSIGNED) {
135 4d986b71 Alex Converse
        off = 0;
136
    } else {
137
        off = maxval;
138
    }
139 fd257dc4 Alex Converse
    for (i = 0; i < size; i += dim) {
140 78e65cd7 Alex Converse
        const float *vec;
141 4d986b71 Alex Converse
        int *quants = s->qcoefs + i;
142
        int curidx = 0;
143
        int curbits;
144
        float rd = 0.0f;
145
        for (j = 0; j < dim; j++) {
146
            curidx *= range;
147
            curidx += quants[j] + off;
148
        }
149 99d61d34 Diego Biurrun
            curbits =  ff_aac_spectral_bits[cb-1][curidx];
150
            vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
151 1676b099 Alex Converse
            if (BT_UNSIGNED) {
152 fd257dc4 Alex Converse
                for (k = 0; k < dim; k++) {
153 78e65cd7 Alex Converse
                    float t = fabsf(in[i+k]);
154
                    float di;
155 1676b099 Alex Converse
                    if (BT_ESC && vec[k] == 64.0f) { //FIXME: slow
156 78e65cd7 Alex Converse
                        if (t >= CLIPPED_ESCAPE) {
157
                            di = t - CLIPPED_ESCAPE;
158
                            curbits += 21;
159 fd257dc4 Alex Converse
                        } else {
160 78e65cd7 Alex Converse
                            int c = av_clip(quant(t, Q), 0, 8191);
161 9d4f6f10 Alex Converse
                            di = t - c*cbrtf(c)*IQ;
162 78e65cd7 Alex Converse
                            curbits += av_log2(c)*2 - 4 + 1;
163
                        }
164 fd257dc4 Alex Converse
                    } else {
165 78e65cd7 Alex Converse
                        di = t - vec[k]*IQ;
166
                    }
167 fd257dc4 Alex Converse
                    if (vec[k] != 0.0f)
168 78e65cd7 Alex Converse
                        curbits++;
169 0bd9aa44 Alex Converse
                    rd += di*di;
170 78e65cd7 Alex Converse
                }
171 fd257dc4 Alex Converse
            } else {
172
                for (k = 0; k < dim; k++) {
173 78e65cd7 Alex Converse
                    float di = in[i+k] - vec[k]*IQ;
174 0bd9aa44 Alex Converse
                    rd += di*di;
175 78e65cd7 Alex Converse
                }
176
            }
177 4d986b71 Alex Converse
        cost    += rd * lambda + curbits;
178
        resbits += curbits;
179 fd257dc4 Alex Converse
        if (cost >= uplim)
180 78e65cd7 Alex Converse
            return uplim;
181 508f092a Alex Converse
        if (pb) {
182 4d986b71 Alex Converse
        put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
183 1676b099 Alex Converse
        if (BT_UNSIGNED)
184 fd257dc4 Alex Converse
            for (j = 0; j < dim; j++)
185 4d986b71 Alex Converse
                if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
186 78e65cd7 Alex Converse
                    put_bits(pb, 1, in[i+j] < 0.0f);
187 1676b099 Alex Converse
        if (BT_ESC) {
188 fd257dc4 Alex Converse
            for (j = 0; j < 2; j++) {
189 4d986b71 Alex Converse
                if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
190 78e65cd7 Alex Converse
                    int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
191
                    int len = av_log2(coef);
192
193
                    put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
194
                    put_bits(pb, len, coef & ((1 << len) - 1));
195
                }
196
            }
197
        }
198 508f092a Alex Converse
        }
199 78e65cd7 Alex Converse
    }
200 508f092a Alex Converse
201
    if (bits)
202
        *bits = resbits;
203
    return cost;
204
}
205 1676b099 Alex Converse
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
253 508f092a Alex Converse
static float quantize_band_cost(struct AACEncContext *s, const float *in,
254
                                const float *scaled, int size, int scale_idx,
255
                                int cb, const float lambda, const float uplim,
256
                                int *bits)
257
{
258
    return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
259
                                         cb, lambda, uplim, bits);
260
}
261
262
static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
263
                                     const float *in, int size, int scale_idx,
264
                                     int cb, const float lambda)
265
{
266
    quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
267
                                  INFINITY, NULL);
268 78e65cd7 Alex Converse
}
269
270 0ecfa7b7 Alex Converse
static float find_max_val(int group_len, int swb_size, const float *scaled) {
271 05e659ef Alex Converse
    float maxval = 0.0f;
272 0ecfa7b7 Alex Converse
    int w2, i;
273 05e659ef Alex Converse
    for (w2 = 0; w2 < group_len; w2++) {
274
        for (i = 0; i < swb_size; i++) {
275
            maxval = FFMAX(maxval, scaled[w2*128+i]);
276
        }
277
    }
278 0ecfa7b7 Alex Converse
    return maxval;
279
}
280
281
static int find_min_book(float maxval, int sf) {
282
    float Q = ff_aac_pow2sf_tab[200 - sf + SCALE_ONE_POS - SCALE_DIV_512];
283
    float Q34 = sqrtf(Q * sqrtf(Q));
284
    int qmaxval, cb;
285 05e659ef Alex Converse
    qmaxval = maxval * Q34 + 0.4054f;
286
    if      (qmaxval ==  0) cb = 0;
287
    else if (qmaxval ==  1) cb = 1;
288
    else if (qmaxval ==  2) cb = 3;
289
    else if (qmaxval <=  4) cb = 5;
290
    else if (qmaxval <=  7) cb = 7;
291
    else if (qmaxval <= 12) cb = 9;
292
    else                    cb = 11;
293
    return cb;
294
}
295
296 78e65cd7 Alex Converse
/**
297
 * structure used in optimal codebook search
298
 */
299
typedef struct BandCodingPath {
300
    int prev_idx; ///< pointer to the previous path point
301
    float cost;   ///< path cost
302
    int run;
303
} BandCodingPath;
304
305
/**
306
 * Encode band info for single window group bands.
307
 */
308
static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
309
                                     int win, int group_len, const float lambda)
310
{
311
    BandCodingPath path[120][12];
312
    int w, swb, cb, start, start2, size;
313
    int i, j;
314 99d61d34 Diego Biurrun
    const int max_sfb  = sce->ics.max_sfb;
315 78e65cd7 Alex Converse
    const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
316 99d61d34 Diego Biurrun
    const int run_esc  = (1 << run_bits) - 1;
317 78e65cd7 Alex Converse
    int idx, ppos, count;
318
    int stackrun[120], stackcb[120], stack_len;
319
    float next_minrd = INFINITY;
320
    int next_mincb = 0;
321
322
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
323
    start = win*128;
324 fd257dc4 Alex Converse
    for (cb = 0; cb < 12; cb++) {
325 99d61d34 Diego Biurrun
        path[0][cb].cost     = 0.0f;
326 78e65cd7 Alex Converse
        path[0][cb].prev_idx = -1;
327 99d61d34 Diego Biurrun
        path[0][cb].run      = 0;
328 78e65cd7 Alex Converse
    }
329 fd257dc4 Alex Converse
    for (swb = 0; swb < max_sfb; swb++) {
330 78e65cd7 Alex Converse
        start2 = start;
331
        size = sce->ics.swb_sizes[swb];
332 fd257dc4 Alex Converse
        if (sce->zeroes[win*16 + swb]) {
333
            for (cb = 0; cb < 12; cb++) {
334 78e65cd7 Alex Converse
                path[swb+1][cb].prev_idx = cb;
335 99d61d34 Diego Biurrun
                path[swb+1][cb].cost     = path[swb][cb].cost;
336
                path[swb+1][cb].run      = path[swb][cb].run + 1;
337 78e65cd7 Alex Converse
            }
338 fd257dc4 Alex Converse
        } else {
339 78e65cd7 Alex Converse
            float minrd = next_minrd;
340
            int mincb = next_mincb;
341
            next_minrd = INFINITY;
342
            next_mincb = 0;
343 fd257dc4 Alex Converse
            for (cb = 0; cb < 12; cb++) {
344 78e65cd7 Alex Converse
                float cost_stay_here, cost_get_here;
345
                float rd = 0.0f;
346 fd257dc4 Alex Converse
                for (w = 0; w < group_len; w++) {
347 78e65cd7 Alex Converse
                    FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
348
                    rd += quantize_band_cost(s, sce->coeffs + start + w*128,
349
                                             s->scoefs + start + w*128, size,
350
                                             sce->sf_idx[(win+w)*16+swb], cb,
351
                                             lambda / band->threshold, INFINITY, NULL);
352
                }
353
                cost_stay_here = path[swb][cb].cost + rd;
354
                cost_get_here  = minrd              + rd + run_bits + 4;
355 fd257dc4 Alex Converse
                if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
356 99d61d34 Diego Biurrun
                    != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
357 78e65cd7 Alex Converse
                    cost_stay_here += run_bits;
358
                if (cost_get_here < cost_stay_here) {
359
                    path[swb+1][cb].prev_idx = mincb;
360
                    path[swb+1][cb].cost     = cost_get_here;
361
                    path[swb+1][cb].run      = 1;
362
                } else {
363
                    path[swb+1][cb].prev_idx = cb;
364
                    path[swb+1][cb].cost     = cost_stay_here;
365
                    path[swb+1][cb].run      = path[swb][cb].run + 1;
366
                }
367
                if (path[swb+1][cb].cost < next_minrd) {
368
                    next_minrd = path[swb+1][cb].cost;
369
                    next_mincb = cb;
370
                }
371
            }
372
        }
373
        start += sce->ics.swb_sizes[swb];
374
    }
375
376
    //convert resulting path from backward-linked list
377
    stack_len = 0;
378 99d61d34 Diego Biurrun
    idx       = 0;
379 c8f47d8b Diego Biurrun
    for (cb = 1; cb < 12; cb++)
380 fd257dc4 Alex Converse
        if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
381 78e65cd7 Alex Converse
            idx = cb;
382
    ppos = max_sfb;
383 99d61d34 Diego Biurrun
    while (ppos > 0) {
384 78e65cd7 Alex Converse
        cb = idx;
385
        stackrun[stack_len] = path[ppos][cb].run;
386
        stackcb [stack_len] = cb;
387
        idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
388
        ppos -= path[ppos][cb].run;
389
        stack_len++;
390
    }
391
    //perform actual band info encoding
392
    start = 0;
393 fd257dc4 Alex Converse
    for (i = stack_len - 1; i >= 0; i--) {
394 78e65cd7 Alex Converse
        put_bits(&s->pb, 4, stackcb[i]);
395
        count = stackrun[i];
396
        memset(sce->zeroes + win*16 + start, !stackcb[i], count);
397
        //XXX: memset when band_type is also uint8_t
398 fd257dc4 Alex Converse
        for (j = 0; j < count; j++) {
399 78e65cd7 Alex Converse
            sce->band_type[win*16 + start] =  stackcb[i];
400
            start++;
401
        }
402 99d61d34 Diego Biurrun
        while (count >= run_esc) {
403 78e65cd7 Alex Converse
            put_bits(&s->pb, run_bits, run_esc);
404
            count -= run_esc;
405
        }
406
        put_bits(&s->pb, run_bits, count);
407
    }
408
}
409
410 759510e6 Alex Converse
static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
411
                                  int win, int group_len, const float lambda)
412
{
413
    BandCodingPath path[120][12];
414
    int w, swb, cb, start, start2, size;
415
    int i, j;
416
    const int max_sfb  = sce->ics.max_sfb;
417
    const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
418
    const int run_esc  = (1 << run_bits) - 1;
419
    int idx, ppos, count;
420
    int stackrun[120], stackcb[120], stack_len;
421
    float next_minrd = INFINITY;
422
    int next_mincb = 0;
423
424
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
425
    start = win*128;
426
    for (cb = 0; cb < 12; cb++) {
427
        path[0][cb].cost     = run_bits+4;
428
        path[0][cb].prev_idx = -1;
429
        path[0][cb].run      = 0;
430
    }
431
    for (swb = 0; swb < max_sfb; swb++) {
432
        start2 = start;
433
        size = sce->ics.swb_sizes[swb];
434
        if (sce->zeroes[win*16 + swb]) {
435
            for (cb = 0; cb < 12; cb++) {
436
                path[swb+1][cb].prev_idx = cb;
437
                path[swb+1][cb].cost     = path[swb][cb].cost;
438
                path[swb+1][cb].run      = path[swb][cb].run + 1;
439
            }
440
        } else {
441
            float minrd = next_minrd;
442
            int mincb = next_mincb;
443
            int startcb = sce->band_type[win*16+swb];
444
            next_minrd = INFINITY;
445
            next_mincb = 0;
446
            for (cb = 0; cb < startcb; cb++) {
447
                path[swb+1][cb].cost = 61450;
448
                path[swb+1][cb].prev_idx = -1;
449
                path[swb+1][cb].run = 0;
450
            }
451
            for (cb = startcb; cb < 12; cb++) {
452
                float cost_stay_here, cost_get_here;
453
                float rd = 0.0f;
454
                for (w = 0; w < group_len; w++) {
455
                    rd += quantize_band_cost(s, sce->coeffs + start + w*128,
456
                                             s->scoefs + start + w*128, size,
457
                                             sce->sf_idx[(win+w)*16+swb], cb,
458
                                             0, INFINITY, NULL);
459
                }
460
                cost_stay_here = path[swb][cb].cost + rd;
461
                cost_get_here  = minrd              + rd + run_bits + 4;
462
                if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
463
                    != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
464
                    cost_stay_here += run_bits;
465
                if (cost_get_here < cost_stay_here) {
466
                    path[swb+1][cb].prev_idx = mincb;
467
                    path[swb+1][cb].cost     = cost_get_here;
468
                    path[swb+1][cb].run      = 1;
469
                } else {
470
                    path[swb+1][cb].prev_idx = cb;
471
                    path[swb+1][cb].cost     = cost_stay_here;
472
                    path[swb+1][cb].run      = path[swb][cb].run + 1;
473
                }
474
                if (path[swb+1][cb].cost < next_minrd) {
475
                    next_minrd = path[swb+1][cb].cost;
476
                    next_mincb = cb;
477
                }
478
            }
479
        }
480
        start += sce->ics.swb_sizes[swb];
481
    }
482
483
    //convert resulting path from backward-linked list
484
    stack_len = 0;
485
    idx       = 0;
486
    for (cb = 1; cb < 12; cb++)
487
        if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
488
            idx = cb;
489
    ppos = max_sfb;
490
    while (ppos > 0) {
491
        if (idx < 0) abort();
492
        cb = idx;
493
        stackrun[stack_len] = path[ppos][cb].run;
494
        stackcb [stack_len] = cb;
495
        idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
496
        ppos -= path[ppos][cb].run;
497
        stack_len++;
498
    }
499
    //perform actual band info encoding
500
    start = 0;
501
    for (i = stack_len - 1; i >= 0; i--) {
502
        put_bits(&s->pb, 4, stackcb[i]);
503
        count = stackrun[i];
504
        memset(sce->zeroes + win*16 + start, !stackcb[i], count);
505
        //XXX: memset when band_type is also uint8_t
506
        for (j = 0; j < count; j++) {
507
            sce->band_type[win*16 + start] =  stackcb[i];
508
            start++;
509
        }
510
        while (count >= run_esc) {
511
            put_bits(&s->pb, run_bits, run_esc);
512
            count -= run_esc;
513
        }
514
        put_bits(&s->pb, run_bits, count);
515
    }
516
}
517
518 78e65cd7 Alex Converse
typedef struct TrellisPath {
519
    float cost;
520
    int prev;
521
} TrellisPath;
522
523 f5e82fec Alex Converse
#define TRELLIS_STAGES 121
524 144c5e3d Alex Converse
#define TRELLIS_STATES (SCALE_MAX_DIFF+1)
525 f5e82fec Alex Converse
526 78e65cd7 Alex Converse
static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
527 99d61d34 Diego Biurrun
                                       SingleChannelElement *sce,
528
                                       const float lambda)
529 78e65cd7 Alex Converse
{
530
    int q, w, w2, g, start = 0;
531 9072c29e Alex Converse
    int i, j;
532 78e65cd7 Alex Converse
    int idx;
533 f5e82fec Alex Converse
    TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
534
    int bandaddr[TRELLIS_STAGES];
535 78e65cd7 Alex Converse
    int minq;
536
    float mincost;
537 144c5e3d Alex Converse
    float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
538
    int q0, q1, qcnt = 0;
539
540
    for (i = 0; i < 1024; i++) {
541
        float t = fabsf(sce->coeffs[i]);
542
        if (t > 0.0f) {
543
            q0f = FFMIN(q0f, t);
544
            q1f = FFMAX(q1f, t);
545
            qnrgf += t*t;
546
            qcnt++;
547
        }
548
    }
549
550
    if (!qcnt) {
551
        memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
552
        memset(sce->zeroes, 1, sizeof(sce->zeroes));
553
        return;
554
    }
555
556
    //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
557
    q0 = av_clip_uint8(log2(q0f)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
558
    //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
559
    q1 = av_clip_uint8(log2(q1f)*4 +  6 + SCALE_ONE_POS - SCALE_DIV_512);
560
    //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
561
    if (q1 - q0 > 60) {
562
        int q0low  = q0;
563
        int q1high = q1;
564
        //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
565
        int qnrg = av_clip_uint8(log2(sqrt(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
566
        q1 = qnrg + 30;
567
        q0 = qnrg - 30;
568
    //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
569
        if (q0 < q0low) {
570
            q1 += q0low - q0;
571
            q0  = q0low;
572
        } else if (q1 > q1high) {
573
            q0 -= q1 - q1high;
574
            q1  = q1high;
575
        }
576
    }
577
    //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
578 78e65cd7 Alex Converse
579 f5e82fec Alex Converse
    for (i = 0; i < TRELLIS_STATES; i++) {
580 9072c29e Alex Converse
        paths[0][i].cost    = 0.0f;
581
        paths[0][i].prev    = -1;
582 78e65cd7 Alex Converse
    }
583 f5e82fec Alex Converse
    for (j = 1; j < TRELLIS_STAGES; j++) {
584
        for (i = 0; i < TRELLIS_STATES; i++) {
585 9072c29e Alex Converse
            paths[j][i].cost    = INFINITY;
586
            paths[j][i].prev    = -2;
587
        }
588 78e65cd7 Alex Converse
    }
589 9072c29e Alex Converse
    idx = 1;
590 78e65cd7 Alex Converse
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
591 fd257dc4 Alex Converse
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
592 78e65cd7 Alex Converse
        start = w*128;
593 fd257dc4 Alex Converse
        for (g = 0; g < sce->ics.num_swb; g++) {
594 78e65cd7 Alex Converse
            const float *coefs = sce->coeffs + start;
595
            float qmin, qmax;
596
            int nz = 0;
597
598 9072c29e Alex Converse
            bandaddr[idx] = w * 16 + g;
599 78e65cd7 Alex Converse
            qmin = INT_MAX;
600
            qmax = 0.0f;
601 fd257dc4 Alex Converse
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
602 78e65cd7 Alex Converse
                FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
603 fd257dc4 Alex Converse
                if (band->energy <= band->threshold || band->threshold == 0.0f) {
604 78e65cd7 Alex Converse
                    sce->zeroes[(w+w2)*16+g] = 1;
605
                    continue;
606
                }
607
                sce->zeroes[(w+w2)*16+g] = 0;
608
                nz = 1;
609 fd257dc4 Alex Converse
                for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
610 78e65cd7 Alex Converse
                    float t = fabsf(coefs[w2*128+i]);
611 c8f47d8b Diego Biurrun
                    if (t > 0.0f)
612 988c1705 Alex Converse
                        qmin = FFMIN(qmin, t);
613
                    qmax = FFMAX(qmax, t);
614 78e65cd7 Alex Converse
                }
615
            }
616 fd257dc4 Alex Converse
            if (nz) {
617 78e65cd7 Alex Converse
                int minscale, maxscale;
618
                float minrd = INFINITY;
619 9069b7d3 Alex Converse
                float maxval;
620 78e65cd7 Alex Converse
                //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
621
                minscale = av_clip_uint8(log2(qmin)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
622
                //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
623
                maxscale = av_clip_uint8(log2(qmax)*4 +  6 + SCALE_ONE_POS - SCALE_DIV_512);
624 144c5e3d Alex Converse
                minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
625
                maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
626 9069b7d3 Alex Converse
                maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
627 fd257dc4 Alex Converse
                for (q = minscale; q < maxscale; q++) {
628 acc9f51f Alex Converse
                    float dist = 0;
629 0ecfa7b7 Alex Converse
                    int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
630 fd257dc4 Alex Converse
                    for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
631 78e65cd7 Alex Converse
                        FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
632 acc9f51f Alex Converse
                        dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
633 911fbc45 Alex Converse
                                                   q + q0, cb, lambda / band->threshold, INFINITY, NULL);
634 78e65cd7 Alex Converse
                    }
635 988c1705 Alex Converse
                    minrd = FFMIN(minrd, dist);
636 78e65cd7 Alex Converse
637 144c5e3d Alex Converse
                    for (i = 0; i < q1 - q0; i++) {
638 78e65cd7 Alex Converse
                        float cost;
639 9072c29e Alex Converse
                        cost = paths[idx - 1][i].cost + dist
640 78e65cd7 Alex Converse
                               + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
641 144c5e3d Alex Converse
                        if (cost < paths[idx][q].cost) {
642 9072c29e Alex Converse
                            paths[idx][q].cost    = cost;
643
                            paths[idx][q].prev    = i;
644 78e65cd7 Alex Converse
                        }
645
                    }
646
                }
647 fd257dc4 Alex Converse
            } else {
648 144c5e3d Alex Converse
                for (q = 0; q < q1 - q0; q++) {
649 911fbc45 Alex Converse
                    paths[idx][q].cost = paths[idx - 1][q].cost + 1;
650
                    paths[idx][q].prev = q;
651 78e65cd7 Alex Converse
                }
652
            }
653
            sce->zeroes[w*16+g] = !nz;
654
            start += sce->ics.swb_sizes[g];
655 9072c29e Alex Converse
            idx++;
656 78e65cd7 Alex Converse
        }
657
    }
658 9072c29e Alex Converse
    idx--;
659
    mincost = paths[idx][0].cost;
660
    minq    = 0;
661 f5e82fec Alex Converse
    for (i = 1; i < TRELLIS_STATES; i++) {
662 9072c29e Alex Converse
        if (paths[idx][i].cost < mincost) {
663
            mincost = paths[idx][i].cost;
664
            minq = i;
665 78e65cd7 Alex Converse
        }
666
    }
667 9072c29e Alex Converse
    while (idx) {
668 144c5e3d Alex Converse
        sce->sf_idx[bandaddr[idx]] = minq + q0;
669 9072c29e Alex Converse
        minq = paths[idx][minq].prev;
670
        idx--;
671 78e65cd7 Alex Converse
    }
672
    //set the same quantizers inside window groups
673 fd257dc4 Alex Converse
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
674
        for (g = 0;  g < sce->ics.num_swb; g++)
675
            for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
676 78e65cd7 Alex Converse
                sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
677
}
678
679
/**
680
 * two-loop quantizers search taken from ISO 13818-7 Appendix C
681
 */
682 99d61d34 Diego Biurrun
static void search_for_quantizers_twoloop(AVCodecContext *avctx,
683
                                          AACEncContext *s,
684
                                          SingleChannelElement *sce,
685
                                          const float lambda)
686 78e65cd7 Alex Converse
{
687
    int start = 0, i, w, w2, g;
688
    int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
689
    float dists[128], uplims[128];
690
    int fflag, minscaler;
691 99d61d34 Diego Biurrun
    int its  = 0;
692 78e65cd7 Alex Converse
    int allz = 0;
693
    float minthr = INFINITY;
694
695
    //XXX: some heuristic to determine initial quantizers will reduce search time
696
    memset(dists, 0, sizeof(dists));
697
    //determine zero bands and upper limits
698 fd257dc4 Alex Converse
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
699
        for (g = 0;  g < sce->ics.num_swb; g++) {
700 78e65cd7 Alex Converse
            int nz = 0;
701
            float uplim = 0.0f;
702 fd257dc4 Alex Converse
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
703 78e65cd7 Alex Converse
                FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
704
                uplim += band->threshold;
705 fd257dc4 Alex Converse
                if (band->energy <= band->threshold || band->threshold == 0.0f) {
706 78e65cd7 Alex Converse
                    sce->zeroes[(w+w2)*16+g] = 1;
707
                    continue;
708
                }
709
                nz = 1;
710
            }
711
            uplims[w*16+g] = uplim *512;
712
            sce->zeroes[w*16+g] = !nz;
713 fd257dc4 Alex Converse
            if (nz)
714 988c1705 Alex Converse
                minthr = FFMIN(minthr, uplim);
715 78e65cd7 Alex Converse
            allz = FFMAX(allz, nz);
716
        }
717
    }
718 fd257dc4 Alex Converse
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
719
        for (g = 0;  g < sce->ics.num_swb; g++) {
720
            if (sce->zeroes[w*16+g]) {
721 78e65cd7 Alex Converse
                sce->sf_idx[w*16+g] = SCALE_ONE_POS;
722
                continue;
723
            }
724 988c1705 Alex Converse
            sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2(uplims[w*16+g]/minthr)*4,59);
725 78e65cd7 Alex Converse
        }
726
    }
727
728 fd257dc4 Alex Converse
    if (!allz)
729 78e65cd7 Alex Converse
        return;
730
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
731
    //perform two-loop search
732
    //outer loop - improve quality
733 99d61d34 Diego Biurrun
    do {
734 78e65cd7 Alex Converse
        int tbits, qstep;
735
        minscaler = sce->sf_idx[0];
736
        //inner loop - quantize spectrum to fit into given number of bits
737
        qstep = its ? 1 : 32;
738 99d61d34 Diego Biurrun
        do {
739 78e65cd7 Alex Converse
            int prev = -1;
740
            tbits = 0;
741
            fflag = 0;
742 fd257dc4 Alex Converse
            for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
743 78e65cd7 Alex Converse
                start = w*128;
744 fd257dc4 Alex Converse
                for (g = 0;  g < sce->ics.num_swb; g++) {
745 78e65cd7 Alex Converse
                    const float *coefs = sce->coeffs + start;
746
                    const float *scaled = s->scoefs + start;
747
                    int bits = 0;
748
                    int cb;
749 04d6a54e Alex Converse
                    float dist = 0.0f;
750 78e65cd7 Alex Converse
751 a62d6cfe Alex Converse
                    if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
752
                        start += sce->ics.swb_sizes[g];
753 78e65cd7 Alex Converse
                        continue;
754 a62d6cfe Alex Converse
                    }
755 78e65cd7 Alex Converse
                    minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
756 911fbc45 Alex Converse
                    cb = find_min_book(find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled), sce->sf_idx[w*16+g]);
757
                    for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
758
                        int b;
759
                        dist += quantize_band_cost(s, coefs + w2*128,
760
                                                   scaled + w2*128,
761
                                                   sce->ics.swb_sizes[g],
762
                                                   sce->sf_idx[w*16+g],
763
                                                   cb,
764 c91dce99 Alex Converse
                                                   1.0f,
765 911fbc45 Alex Converse
                                                   INFINITY,
766
                                                   &b);
767
                        bits += b;
768
                    }
769 c91dce99 Alex Converse
                    dists[w*16+g] = dist - bits;
770 fd257dc4 Alex Converse
                    if (prev != -1) {
771 78e65cd7 Alex Converse
                        bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
772
                    }
773
                    tbits += bits;
774
                    start += sce->ics.swb_sizes[g];
775
                    prev = sce->sf_idx[w*16+g];
776
                }
777
            }
778 fd257dc4 Alex Converse
            if (tbits > destbits) {
779 c8f47d8b Diego Biurrun
                for (i = 0; i < 128; i++)
780
                    if (sce->sf_idx[i] < 218 - qstep)
781 78e65cd7 Alex Converse
                        sce->sf_idx[i] += qstep;
782 fd257dc4 Alex Converse
            } else {
783 c8f47d8b Diego Biurrun
                for (i = 0; i < 128; i++)
784
                    if (sce->sf_idx[i] > 60 - qstep)
785 78e65cd7 Alex Converse
                        sce->sf_idx[i] -= qstep;
786
            }
787
            qstep >>= 1;
788 fd257dc4 Alex Converse
            if (!qstep && tbits > destbits*1.02)
789 78e65cd7 Alex Converse
                qstep = 1;
790 c8f47d8b Diego Biurrun
            if (sce->sf_idx[0] >= 217)
791
                break;
792 99d61d34 Diego Biurrun
        } while (qstep);
793 78e65cd7 Alex Converse
794
        fflag = 0;
795
        minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
796 fd257dc4 Alex Converse
        for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
797 78e65cd7 Alex Converse
            start = w*128;
798 fd257dc4 Alex Converse
            for (g = 0; g < sce->ics.num_swb; g++) {
799 78e65cd7 Alex Converse
                int prevsc = sce->sf_idx[w*16+g];
800 804d489c Alex Converse
                const float *scaled = s->scoefs + start;
801 fd257dc4 Alex Converse
                if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
802 78e65cd7 Alex Converse
                    sce->sf_idx[w*16+g]--;
803
                sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
804
                sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
805 fd257dc4 Alex Converse
                if (sce->sf_idx[w*16+g] != prevsc)
806 78e65cd7 Alex Converse
                    fflag = 1;
807 804d489c Alex Converse
                sce->band_type[w*16+g] = find_min_book(find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled), sce->sf_idx[w*16+g]);
808
                start += sce->ics.swb_sizes[g];
809 78e65cd7 Alex Converse
            }
810
        }
811
        its++;
812 99d61d34 Diego Biurrun
    } while (fflag && its < 10);
813 78e65cd7 Alex Converse
}
814
815
static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
816 99d61d34 Diego Biurrun
                                       SingleChannelElement *sce,
817
                                       const float lambda)
818 78e65cd7 Alex Converse
{
819
    int start = 0, i, w, w2, g;
820
    float uplim[128], maxq[128];
821
    int minq, maxsf;
822
    float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
823
    int last = 0, lastband = 0, curband = 0;
824
    float avg_energy = 0.0;
825 fd257dc4 Alex Converse
    if (sce->ics.num_windows == 1) {
826 78e65cd7 Alex Converse
        start = 0;
827 fd257dc4 Alex Converse
        for (i = 0; i < 1024; i++) {
828
            if (i - start >= sce->ics.swb_sizes[curband]) {
829 78e65cd7 Alex Converse
                start += sce->ics.swb_sizes[curband];
830
                curband++;
831
            }
832 fd257dc4 Alex Converse
            if (sce->coeffs[i]) {
833 78e65cd7 Alex Converse
                avg_energy += sce->coeffs[i] * sce->coeffs[i];
834
                last = i;
835
                lastband = curband;
836
            }
837
        }
838 fd257dc4 Alex Converse
    } else {
839
        for (w = 0; w < 8; w++) {
840 78e65cd7 Alex Converse
            const float *coeffs = sce->coeffs + w*128;
841
            start = 0;
842 fd257dc4 Alex Converse
            for (i = 0; i < 128; i++) {
843
                if (i - start >= sce->ics.swb_sizes[curband]) {
844 78e65cd7 Alex Converse
                    start += sce->ics.swb_sizes[curband];
845
                    curband++;
846
                }
847 fd257dc4 Alex Converse
                if (coeffs[i]) {
848 78e65cd7 Alex Converse
                    avg_energy += coeffs[i] * coeffs[i];
849
                    last = FFMAX(last, i);
850
                    lastband = FFMAX(lastband, curband);
851
                }
852
            }
853
        }
854
    }
855
    last++;
856
    avg_energy /= last;
857 fd257dc4 Alex Converse
    if (avg_energy == 0.0f) {
858
        for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
859 78e65cd7 Alex Converse
            sce->sf_idx[i] = SCALE_ONE_POS;
860
        return;
861
    }
862 fd257dc4 Alex Converse
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
863 78e65cd7 Alex Converse
        start = w*128;
864 fd257dc4 Alex Converse
        for (g = 0; g < sce->ics.num_swb; g++) {
865 99d61d34 Diego Biurrun
            float *coefs   = sce->coeffs + start;
866 78e65cd7 Alex Converse
            const int size = sce->ics.swb_sizes[g];
867
            int start2 = start, end2 = start + size, peakpos = start;
868
            float maxval = -1, thr = 0.0f, t;
869
            maxq[w*16+g] = 0.0f;
870 fd257dc4 Alex Converse
            if (g > lastband) {
871 78e65cd7 Alex Converse
                maxq[w*16+g] = 0.0f;
872
                start += size;
873 fd257dc4 Alex Converse
                for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
874 78e65cd7 Alex Converse
                    memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
875
                continue;
876
            }
877 fd257dc4 Alex Converse
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
878
                for (i = 0; i < size; i++) {
879 78e65cd7 Alex Converse
                    float t = coefs[w2*128+i]*coefs[w2*128+i];
880 988c1705 Alex Converse
                    maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
881 78e65cd7 Alex Converse
                    thr += t;
882 fd257dc4 Alex Converse
                    if (sce->ics.num_windows == 1 && maxval < t) {
883 99d61d34 Diego Biurrun
                        maxval  = t;
884 78e65cd7 Alex Converse
                        peakpos = start+i;
885
                    }
886
                }
887
            }
888 fd257dc4 Alex Converse
            if (sce->ics.num_windows == 1) {
889 78e65cd7 Alex Converse
                start2 = FFMAX(peakpos - 2, start2);
890
                end2   = FFMIN(peakpos + 3, end2);
891 fd257dc4 Alex Converse
            } else {
892 78e65cd7 Alex Converse
                start2 -= start;
893
                end2   -= start;
894
            }
895
            start += size;
896
            thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
897 99d61d34 Diego Biurrun
            t   = 1.0 - (1.0 * start2 / last);
898 78e65cd7 Alex Converse
            uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
899
        }
900
    }
901
    memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
902
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
903 fd257dc4 Alex Converse
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
904 78e65cd7 Alex Converse
        start = w*128;
905 fd257dc4 Alex Converse
        for (g = 0;  g < sce->ics.num_swb; g++) {
906 99d61d34 Diego Biurrun
            const float *coefs  = sce->coeffs + start;
907
            const float *scaled = s->scoefs   + start;
908
            const int size      = sce->ics.swb_sizes[g];
909 78e65cd7 Alex Converse
            int scf, prev_scf, step;
910 32fa7725 Alex Converse
            int min_scf = -1, max_scf = 256;
911 78e65cd7 Alex Converse
            float curdiff;
912 fd257dc4 Alex Converse
            if (maxq[w*16+g] < 21.544) {
913 78e65cd7 Alex Converse
                sce->zeroes[w*16+g] = 1;
914
                start += size;
915
                continue;
916
            }
917
            sce->zeroes[w*16+g] = 0;
918 99d61d34 Diego Biurrun
            scf  = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
919 78e65cd7 Alex Converse
            step = 16;
920 fd257dc4 Alex Converse
            for (;;) {
921 78e65cd7 Alex Converse
                float dist = 0.0f;
922
                int quant_max;
923
924 fd257dc4 Alex Converse
                for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
925 78e65cd7 Alex Converse
                    int b;
926
                    dist += quantize_band_cost(s, coefs + w2*128,
927
                                               scaled + w2*128,
928
                                               sce->ics.swb_sizes[g],
929
                                               scf,
930
                                               ESC_BT,
931 7a4eebcd Alex Converse
                                               lambda,
932 78e65cd7 Alex Converse
                                               INFINITY,
933
                                               &b);
934
                    dist -= b;
935
                }
936 7a4eebcd Alex Converse
                dist *= 1.0f / 512.0f / lambda;
937 78e65cd7 Alex Converse
                quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
938 fd257dc4 Alex Converse
                if (quant_max >= 8191) { // too much, return to the previous quantizer
939 78e65cd7 Alex Converse
                    sce->sf_idx[w*16+g] = prev_scf;
940
                    break;
941
                }
942
                prev_scf = scf;
943
                curdiff = fabsf(dist - uplim[w*16+g]);
944 32fa7725 Alex Converse
                if (curdiff <= 1.0f)
945 78e65cd7 Alex Converse
                    step = 0;
946
                else
947 32fa7725 Alex Converse
                    step = log2(curdiff);
948 fd257dc4 Alex Converse
                if (dist > uplim[w*16+g])
949 78e65cd7 Alex Converse
                    step = -step;
950 32fa7725 Alex Converse
                scf += step;
951 46174079 Alex Converse
                scf = av_clip_uint8(scf);
952 32fa7725 Alex Converse
                step = scf - prev_scf;
953 fd257dc4 Alex Converse
                if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
954 32fa7725 Alex Converse
                    sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
955 78e65cd7 Alex Converse
                    break;
956
                }
957 fd257dc4 Alex Converse
                if (step > 0)
958 32fa7725 Alex Converse
                    min_scf = prev_scf;
959 78e65cd7 Alex Converse
                else
960 32fa7725 Alex Converse
                    max_scf = prev_scf;
961 78e65cd7 Alex Converse
            }
962
            start += size;
963
        }
964
    }
965
    minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
966 fd257dc4 Alex Converse
    for (i = 1; i < 128; i++) {
967
        if (!sce->sf_idx[i])
968 78e65cd7 Alex Converse
            sce->sf_idx[i] = sce->sf_idx[i-1];
969
        else
970
            minq = FFMIN(minq, sce->sf_idx[i]);
971
    }
972 c8f47d8b Diego Biurrun
    if (minq == INT_MAX)
973
        minq = 0;
974 78e65cd7 Alex Converse
    minq = FFMIN(minq, SCALE_MAX_POS);
975
    maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
976 fd257dc4 Alex Converse
    for (i = 126; i >= 0; i--) {
977
        if (!sce->sf_idx[i])
978 78e65cd7 Alex Converse
            sce->sf_idx[i] = sce->sf_idx[i+1];
979
        sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
980
    }
981
}
982
983
static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
984 99d61d34 Diego Biurrun
                                       SingleChannelElement *sce,
985
                                       const float lambda)
986 78e65cd7 Alex Converse
{
987
    int start = 0, i, w, w2, g;
988
    int minq = 255;
989
990
    memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
991 fd257dc4 Alex Converse
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
992 78e65cd7 Alex Converse
        start = w*128;
993 fd257dc4 Alex Converse
        for (g = 0; g < sce->ics.num_swb; g++) {
994
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
995 78e65cd7 Alex Converse
                FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
996 fd257dc4 Alex Converse
                if (band->energy <= band->threshold) {
997 78e65cd7 Alex Converse
                    sce->sf_idx[(w+w2)*16+g] = 218;
998
                    sce->zeroes[(w+w2)*16+g] = 1;
999 fd257dc4 Alex Converse
                } else {
1000 78e65cd7 Alex Converse
                    sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2(band->threshold), 80, 218);
1001
                    sce->zeroes[(w+w2)*16+g] = 0;
1002
                }
1003
                minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
1004
            }
1005
        }
1006
    }
1007 fd257dc4 Alex Converse
    for (i = 0; i < 128; i++) {
1008 c8f47d8b Diego Biurrun
        sce->sf_idx[i] = 140;
1009
        //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
1010 78e65cd7 Alex Converse
    }
1011
    //set the same quantizers inside window groups
1012 fd257dc4 Alex Converse
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
1013
        for (g = 0;  g < sce->ics.num_swb; g++)
1014
            for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
1015 78e65cd7 Alex Converse
                sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
1016
}
1017
1018 99d61d34 Diego Biurrun
static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
1019
                          const float lambda)
1020 78e65cd7 Alex Converse
{
1021
    int start = 0, i, w, w2, g;
1022
    float M[128], S[128];
1023
    float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
1024
    SingleChannelElement *sce0 = &cpe->ch[0];
1025
    SingleChannelElement *sce1 = &cpe->ch[1];
1026 fd257dc4 Alex Converse
    if (!cpe->common_window)
1027 78e65cd7 Alex Converse
        return;
1028 fd257dc4 Alex Converse
    for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
1029
        for (g = 0;  g < sce0->ics.num_swb; g++) {
1030
            if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
1031 78e65cd7 Alex Converse
                float dist1 = 0.0f, dist2 = 0.0f;
1032 fd257dc4 Alex Converse
                for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
1033 78e65cd7 Alex Converse
                    FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
1034
                    FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
1035 988c1705 Alex Converse
                    float minthr = FFMIN(band0->threshold, band1->threshold);
1036
                    float maxthr = FFMAX(band0->threshold, band1->threshold);
1037 fd257dc4 Alex Converse
                    for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1038 78e65cd7 Alex Converse
                        M[i] = (sce0->coeffs[start+w2*128+i]
1039 99d61d34 Diego Biurrun
                              + sce1->coeffs[start+w2*128+i]) * 0.5;
1040 78e65cd7 Alex Converse
                        S[i] =  sce0->coeffs[start+w2*128+i]
1041
                              - sce1->coeffs[start+w2*128+i];
1042
                    }
1043
                    abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1044
                    abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1045
                    abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
1046
                    abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
1047
                    dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
1048
                                                L34,
1049
                                                sce0->ics.swb_sizes[g],
1050
                                                sce0->sf_idx[(w+w2)*16+g],
1051
                                                sce0->band_type[(w+w2)*16+g],
1052
                                                lambda / band0->threshold, INFINITY, NULL);
1053
                    dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
1054
                                                R34,
1055
                                                sce1->ics.swb_sizes[g],
1056
                                                sce1->sf_idx[(w+w2)*16+g],
1057
                                                sce1->band_type[(w+w2)*16+g],
1058
                                                lambda / band1->threshold, INFINITY, NULL);
1059
                    dist2 += quantize_band_cost(s, M,
1060
                                                M34,
1061
                                                sce0->ics.swb_sizes[g],
1062
                                                sce0->sf_idx[(w+w2)*16+g],
1063
                                                sce0->band_type[(w+w2)*16+g],
1064
                                                lambda / maxthr, INFINITY, NULL);
1065
                    dist2 += quantize_band_cost(s, S,
1066
                                                S34,
1067
                                                sce1->ics.swb_sizes[g],
1068
                                                sce1->sf_idx[(w+w2)*16+g],
1069
                                                sce1->band_type[(w+w2)*16+g],
1070
                                                lambda / minthr, INFINITY, NULL);
1071
                }
1072
                cpe->ms_mask[w*16+g] = dist2 < dist1;
1073
            }
1074
            start += sce0->ics.swb_sizes[g];
1075
        }
1076
    }
1077
}
1078
1079
AACCoefficientsEncoder ff_aac_coders[] = {
1080
    {
1081
        search_for_quantizers_faac,
1082 7a4eebcd Alex Converse
        encode_window_bands_info,
1083 78e65cd7 Alex Converse
        quantize_and_encode_band,
1084 dd0e43e4 Alex Converse
        search_for_ms,
1085 78e65cd7 Alex Converse
    },
1086
    {
1087
        search_for_quantizers_anmr,
1088
        encode_window_bands_info,
1089
        quantize_and_encode_band,
1090 dd0e43e4 Alex Converse
        search_for_ms,
1091 78e65cd7 Alex Converse
    },
1092
    {
1093
        search_for_quantizers_twoloop,
1094 759510e6 Alex Converse
        codebook_trellis_rate,
1095 78e65cd7 Alex Converse
        quantize_and_encode_band,
1096 dd0e43e4 Alex Converse
        search_for_ms,
1097 78e65cd7 Alex Converse
    },
1098
    {
1099
        search_for_quantizers_fast,
1100
        encode_window_bands_info,
1101
        quantize_and_encode_band,
1102 dd0e43e4 Alex Converse
        search_for_ms,
1103 78e65cd7 Alex Converse
    },
1104
};