Statistics
| Branch: | Revision:

ffmpeg / libavcodec / aaccoder.c @ 932e6a5a

History | View | Annotate | Download (39.8 KB)

1
/*
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
 * @file libavcodec/aaccoder.c
24
 * AAC coefficients encoder
25
 */
26

    
27
/***********************************
28
 *              TODOs:
29
 * speedup quantizer selection
30
 * add sane pulse detection
31
 ***********************************/
32

    
33
#include "avcodec.h"
34
#include "put_bits.h"
35
#include "aac.h"
36
#include "aacenc.h"
37
#include "aactab.h"
38

    
39
/** bits needed to code codebook run value for long windows */
40
static const uint8_t run_value_bits_long[64] = {
41
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
42
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5, 10,
43
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
44
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
45
};
46

    
47
/** bits needed to code codebook run value for short windows */
48
static const uint8_t run_value_bits_short[16] = {
49
    3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
50
};
51

    
52
static const uint8_t *run_value_bits[2] = {
53
    run_value_bits_long, run_value_bits_short
54
};
55

    
56

    
57
/**
58
 * Quantize one coefficient.
59
 * @return absolute value of the quantized coefficient
60
 * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
61
 */
62
static av_always_inline int quant(float coef, const float Q)
63
{
64
    float a = coef * Q;
65
    return sqrtf(a * sqrtf(a)) + 0.4054;
66
}
67

    
68
static void quantize_bands(int (*out)[2], const float *in, const float *scaled,
69
                           int size, float Q34, int is_signed, int maxval)
70
{
71
    int i;
72
    double qc;
73
    for (i = 0; i < size; i++) {
74
        qc = scaled[i] * Q34;
75
        out[i][0] = (int)FFMIN(qc,          (double)maxval);
76
        out[i][1] = (int)FFMIN(qc + 0.4054, (double)maxval);
77
        if (is_signed && in[i] < 0.0f) {
78
            out[i][0] = -out[i][0];
79
            out[i][1] = -out[i][1];
80
        }
81
    }
82
}
83

    
84
static void abs_pow34_v(float *out, const float *in, const int size)
85
{
86
#ifndef USE_REALLY_FULL_SEARCH
87
    int i;
88
    for (i = 0; i < size; i++) {
89
        float a = fabsf(in[i]);
90
        out[i] = sqrtf(a * sqrtf(a));
91
    }
92
#endif /* USE_REALLY_FULL_SEARCH */
93
}
94

    
95
static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
96
static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
97

    
98
/**
99
 * Calculate rate distortion cost for quantizing with given codebook
100
 *
101
 * @return quantization distortion
102
 */
103
static float quantize_band_cost(struct AACEncContext *s, const float *in,
104
                                const float *scaled, int size, int scale_idx,
105
                                int cb, const float lambda, const float uplim,
106
                                int *bits)
107
{
108
    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];
110
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
111
    int i, j, k;
112
    float cost = 0;
113
    const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
114
    int resbits = 0;
115
#ifndef USE_REALLY_FULL_SEARCH
116
    const float  Q34 = sqrtf(Q * sqrtf(Q));
117
    const int range  = aac_cb_range[cb];
118
    const int maxval = aac_cb_maxval[cb];
119
    int offs[4];
120
#endif /* USE_REALLY_FULL_SEARCH */
121

    
122
    if (!cb) {
123
        for (i = 0; i < size; i++)
124
            cost += in[i]*in[i]*lambda;
125
        if (bits)
126
            *bits = 0;
127
        return cost;
128
    }
129
#ifndef USE_REALLY_FULL_SEARCH
130
    offs[0] = 1;
131
    for (i = 1; i < dim; i++)
132
        offs[i] = offs[i-1]*range;
133
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
134
#endif /* USE_REALLY_FULL_SEARCH */
135
    for (i = 0; i < size; i += dim) {
136
        float mincost;
137
        int minidx  = 0;
138
        int minbits = 0;
139
        const float *vec;
140
#ifndef USE_REALLY_FULL_SEARCH
141
        int (*quants)[2] = &s->qcoefs[i];
142
        mincost = 0.0f;
143
        for (j = 0; j < dim; j++)
144
            mincost += in[i+j]*in[i+j]*lambda;
145
        minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
146
        minbits = ff_aac_spectral_bits[cb-1][minidx];
147
        mincost += minbits;
148
        for (j = 0; j < (1<<dim); j++) {
149
            float rd = 0.0f;
150
            int curbits;
151
            int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
152
            int same   = 0;
153
            for (k = 0; k < dim; k++) {
154
                if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
155
                    same = 1;
156
                    break;
157
                }
158
            }
159
            if (same)
160
                continue;
161
            for (k = 0; k < dim; k++)
162
                curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
163
            curbits =  ff_aac_spectral_bits[cb-1][curidx];
164
            vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
165
#else
166
        mincost = INFINITY;
167
        vec = ff_aac_codebook_vectors[cb-1];
168
        for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
169
            float rd = 0.0f;
170
            int curbits = ff_aac_spectral_bits[cb-1][j];
171
#endif /* USE_REALLY_FULL_SEARCH */
172
            if (IS_CODEBOOK_UNSIGNED(cb)) {
173
                for (k = 0; k < dim; k++) {
174
                    float t = fabsf(in[i+k]);
175
                    float di;
176
                    //do not code with escape sequence small values
177
                    if (vec[k] == 64.0f && t < 39.0f*IQ) {
178
                        rd = INFINITY;
179
                        break;
180
                    }
181
                    if (vec[k] == 64.0f) { //FIXME: slow
182
                        if (t >= CLIPPED_ESCAPE) {
183
                            di = t - CLIPPED_ESCAPE;
184
                            curbits += 21;
185
                        } else {
186
                            int c = av_clip(quant(t, Q), 0, 8191);
187
                            di = t - c*cbrt(c)*IQ;
188
                            curbits += av_log2(c)*2 - 4 + 1;
189
                        }
190
                    } else {
191
                        di = t - vec[k]*IQ;
192
                    }
193
                    if (vec[k] != 0.0f)
194
                        curbits++;
195
                    rd += di*di*lambda;
196
                }
197
            } else {
198
                for (k = 0; k < dim; k++) {
199
                    float di = in[i+k] - vec[k]*IQ;
200
                    rd += di*di*lambda;
201
                }
202
            }
203
            rd += curbits;
204
            if (rd < mincost) {
205
                mincost = rd;
206
                minidx  = j;
207
                minbits = curbits;
208
            }
209
        }
210
        cost    += mincost;
211
        resbits += minbits;
212
        if (cost >= uplim)
213
            return uplim;
214
    }
215

    
216
    if (bits)
217
        *bits = resbits;
218
    return cost;
219
}
220

    
221
static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
222
                                     const float *in, int size, int scale_idx,
223
                                     int cb, const float lambda)
224
{
225
    const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
226
    const float  Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
227
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
228
    const int dim = (cb < FIRST_PAIR_BT) ? 4 : 2;
229
    int i, j, k;
230
#ifndef USE_REALLY_FULL_SEARCH
231
    const float  Q34 = sqrtf(Q * sqrtf(Q));
232
    const int range  = aac_cb_range[cb];
233
    const int maxval = aac_cb_maxval[cb];
234
    int offs[4];
235
    float *scaled = s->scoefs;
236
#endif /* USE_REALLY_FULL_SEARCH */
237

    
238
//START_TIMER
239
    if (!cb)
240
        return;
241

    
242
#ifndef USE_REALLY_FULL_SEARCH
243
    offs[0] = 1;
244
    for (i = 1; i < dim; i++)
245
        offs[i] = offs[i-1]*range;
246
    abs_pow34_v(scaled, in, size);
247
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
248
#endif /* USE_REALLY_FULL_SEARCH */
249
    for (i = 0; i < size; i += dim) {
250
        float mincost;
251
        int minidx  = 0;
252
        int minbits = 0;
253
        const float *vec;
254
#ifndef USE_REALLY_FULL_SEARCH
255
        int (*quants)[2] = &s->qcoefs[i];
256
        mincost = 0.0f;
257
        for (j = 0; j < dim; j++)
258
            mincost += in[i+j]*in[i+j]*lambda;
259
        minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
260
        minbits = ff_aac_spectral_bits[cb-1][minidx];
261
        mincost += minbits;
262
        for (j = 0; j < (1<<dim); j++) {
263
            float rd = 0.0f;
264
            int curbits;
265
            int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
266
            int same   = 0;
267
            for (k = 0; k < dim; k++) {
268
                if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
269
                    same = 1;
270
                    break;
271
                }
272
            }
273
            if (same)
274
                continue;
275
            for (k = 0; k < dim; k++)
276
                curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
277
            curbits =  ff_aac_spectral_bits[cb-1][curidx];
278
            vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
279
#else
280
        vec = ff_aac_codebook_vectors[cb-1];
281
        mincost = INFINITY;
282
        for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
283
            float rd = 0.0f;
284
            int curbits = ff_aac_spectral_bits[cb-1][j];
285
            int curidx  = j;
286
#endif /* USE_REALLY_FULL_SEARCH */
287
            if (IS_CODEBOOK_UNSIGNED(cb)) {
288
                for (k = 0; k < dim; k++) {
289
                    float t = fabsf(in[i+k]);
290
                    float di;
291
                    //do not code with escape sequence small values
292
                    if (vec[k] == 64.0f && t < 39.0f*IQ) {
293
                        rd = INFINITY;
294
                        break;
295
                    }
296
                    if (vec[k] == 64.0f) { //FIXME: slow
297
                        if (t >= CLIPPED_ESCAPE) {
298
                            di = t - CLIPPED_ESCAPE;
299
                            curbits += 21;
300
                        } else {
301
                            int c = av_clip(quant(t, Q), 0, 8191);
302
                            di = t - c*cbrt(c)*IQ;
303
                            curbits += av_log2(c)*2 - 4 + 1;
304
                        }
305
                    } else {
306
                        di = t - vec[k]*IQ;
307
                    }
308
                    if (vec[k] != 0.0f)
309
                        curbits++;
310
                    rd += di*di*lambda;
311
                }
312
            } else {
313
                for (k = 0; k < dim; k++) {
314
                    float di = in[i+k] - vec[k]*IQ;
315
                    rd += di*di*lambda;
316
                }
317
            }
318
            rd += curbits;
319
            if (rd < mincost) {
320
                mincost = rd;
321
                minidx  = curidx;
322
                minbits = curbits;
323
            }
324
        }
325
        put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]);
326
        if (IS_CODEBOOK_UNSIGNED(cb))
327
            for (j = 0; j < dim; j++)
328
                if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f)
329
                    put_bits(pb, 1, in[i+j] < 0.0f);
330
        if (cb == ESC_BT) {
331
            for (j = 0; j < 2; j++) {
332
                if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) {
333
                    int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
334
                    int len = av_log2(coef);
335

    
336
                    put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
337
                    put_bits(pb, len, coef & ((1 << len) - 1));
338
                }
339
            }
340
        }
341
    }
342
//STOP_TIMER("quantize_and_encode")
343
}
344

    
345
/**
346
 * structure used in optimal codebook search
347
 */
348
typedef struct BandCodingPath {
349
    int prev_idx; ///< pointer to the previous path point
350
    int codebook; ///< codebook for coding band run
351
    float cost;   ///< path cost
352
    int run;
353
} BandCodingPath;
354

    
355
/**
356
 * Encode band info for single window group bands.
357
 */
358
static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
359
                                     int win, int group_len, const float lambda)
360
{
361
    BandCodingPath path[120][12];
362
    int w, swb, cb, start, start2, size;
363
    int i, j;
364
    const int max_sfb  = sce->ics.max_sfb;
365
    const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
366
    const int run_esc  = (1 << run_bits) - 1;
367
    int idx, ppos, count;
368
    int stackrun[120], stackcb[120], stack_len;
369
    float next_minrd = INFINITY;
370
    int next_mincb = 0;
371

    
372
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
373
    start = win*128;
374
    for (cb = 0; cb < 12; cb++) {
375
        path[0][cb].cost     = 0.0f;
376
        path[0][cb].prev_idx = -1;
377
        path[0][cb].run      = 0;
378
    }
379
    for (swb = 0; swb < max_sfb; swb++) {
380
        start2 = start;
381
        size = sce->ics.swb_sizes[swb];
382
        if (sce->zeroes[win*16 + swb]) {
383
            for (cb = 0; cb < 12; cb++) {
384
                path[swb+1][cb].prev_idx = cb;
385
                path[swb+1][cb].cost     = path[swb][cb].cost;
386
                path[swb+1][cb].run      = path[swb][cb].run + 1;
387
            }
388
        } else {
389
            float minrd = next_minrd;
390
            int mincb = next_mincb;
391
            next_minrd = INFINITY;
392
            next_mincb = 0;
393
            for (cb = 0; cb < 12; cb++) {
394
                float cost_stay_here, cost_get_here;
395
                float rd = 0.0f;
396
                for (w = 0; w < group_len; w++) {
397
                    FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
398
                    rd += quantize_band_cost(s, sce->coeffs + start + w*128,
399
                                             s->scoefs + start + w*128, size,
400
                                             sce->sf_idx[(win+w)*16+swb], cb,
401
                                             lambda / band->threshold, INFINITY, NULL);
402
                }
403
                cost_stay_here = path[swb][cb].cost + rd;
404
                cost_get_here  = minrd              + rd + run_bits + 4;
405
                if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
406
                    != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
407
                    cost_stay_here += run_bits;
408
                if (cost_get_here < cost_stay_here) {
409
                    path[swb+1][cb].prev_idx = mincb;
410
                    path[swb+1][cb].cost     = cost_get_here;
411
                    path[swb+1][cb].run      = 1;
412
                } else {
413
                    path[swb+1][cb].prev_idx = cb;
414
                    path[swb+1][cb].cost     = cost_stay_here;
415
                    path[swb+1][cb].run      = path[swb][cb].run + 1;
416
                }
417
                if (path[swb+1][cb].cost < next_minrd) {
418
                    next_minrd = path[swb+1][cb].cost;
419
                    next_mincb = cb;
420
                }
421
            }
422
        }
423
        start += sce->ics.swb_sizes[swb];
424
    }
425

    
426
    //convert resulting path from backward-linked list
427
    stack_len = 0;
428
    idx       = 0;
429
    for (cb = 1; cb < 12; cb++)
430
        if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
431
            idx = cb;
432
    ppos = max_sfb;
433
    while (ppos > 0) {
434
        cb = idx;
435
        stackrun[stack_len] = path[ppos][cb].run;
436
        stackcb [stack_len] = cb;
437
        idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
438
        ppos -= path[ppos][cb].run;
439
        stack_len++;
440
    }
441
    //perform actual band info encoding
442
    start = 0;
443
    for (i = stack_len - 1; i >= 0; i--) {
444
        put_bits(&s->pb, 4, stackcb[i]);
445
        count = stackrun[i];
446
        memset(sce->zeroes + win*16 + start, !stackcb[i], count);
447
        //XXX: memset when band_type is also uint8_t
448
        for (j = 0; j < count; j++) {
449
            sce->band_type[win*16 + start] =  stackcb[i];
450
            start++;
451
        }
452
        while (count >= run_esc) {
453
            put_bits(&s->pb, run_bits, run_esc);
454
            count -= run_esc;
455
        }
456
        put_bits(&s->pb, run_bits, count);
457
    }
458
}
459

    
460
typedef struct TrellisPath {
461
    float cost;
462
    int prev;
463
    int min_val;
464
    int max_val;
465
} TrellisPath;
466

    
467
static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
468
                                       SingleChannelElement *sce,
469
                                       const float lambda)
470
{
471
    int q, w, w2, g, start = 0;
472
    int i;
473
    int idx;
474
    TrellisPath paths[256*121];
475
    int bandaddr[121];
476
    int minq;
477
    float mincost;
478

    
479
    for (i = 0; i < 256; i++) {
480
        paths[i].cost    = 0.0f;
481
        paths[i].prev    = -1;
482
        paths[i].min_val = i;
483
        paths[i].max_val = i;
484
    }
485
    for (i = 256; i < 256*121; i++) {
486
        paths[i].cost    = INFINITY;
487
        paths[i].prev    = -2;
488
        paths[i].min_val = INT_MAX;
489
        paths[i].max_val = 0;
490
    }
491
    idx = 256;
492
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
493
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
494
        start = w*128;
495
        for (g = 0; g < sce->ics.num_swb; g++) {
496
            const float *coefs = sce->coeffs + start;
497
            float qmin, qmax;
498
            int nz = 0;
499

    
500
            bandaddr[idx >> 8] = w * 16 + g;
501
            qmin = INT_MAX;
502
            qmax = 0.0f;
503
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
504
                FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
505
                if (band->energy <= band->threshold || band->threshold == 0.0f) {
506
                    sce->zeroes[(w+w2)*16+g] = 1;
507
                    continue;
508
                }
509
                sce->zeroes[(w+w2)*16+g] = 0;
510
                nz = 1;
511
                for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
512
                    float t = fabsf(coefs[w2*128+i]);
513
                    if (t > 0.0f)
514
                        qmin = FFMIN(qmin, t);
515
                    qmax = FFMAX(qmax, t);
516
                }
517
            }
518
            if (nz) {
519
                int minscale, maxscale;
520
                float minrd = INFINITY;
521
                //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
522
                minscale = av_clip_uint8(log2(qmin)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
523
                //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
524
                maxscale = av_clip_uint8(log2(qmax)*4 +  6 + SCALE_ONE_POS - SCALE_DIV_512);
525
                for (q = minscale; q < maxscale; q++) {
526
                    float dists[12], dist;
527
                    memset(dists, 0, sizeof(dists));
528
                    for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
529
                        FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
530
                        int cb;
531
                        for (cb = 0; cb <= ESC_BT; cb++)
532
                            dists[cb] += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
533
                                                            q, cb, lambda / band->threshold, INFINITY, NULL);
534
                    }
535
                    dist = dists[0];
536
                    for (i = 1; i <= ESC_BT; i++)
537
                        dist = FFMIN(dist, dists[i]);
538
                    minrd = FFMIN(minrd, dist);
539

    
540
                    for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, 256); i++) {
541
                        float cost;
542
                        int minv, maxv;
543
                        if (isinf(paths[idx - 256 + i].cost))
544
                            continue;
545
                        cost = paths[idx - 256 + i].cost + dist
546
                               + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
547
                        minv = FFMIN(paths[idx - 256 + i].min_val, q);
548
                        maxv = FFMAX(paths[idx - 256 + i].max_val, q);
549
                        if (cost < paths[idx + q].cost && maxv-minv < SCALE_MAX_DIFF) {
550
                            paths[idx + q].cost    = cost;
551
                            paths[idx + q].prev    = idx - 256 + i;
552
                            paths[idx + q].min_val = minv;
553
                            paths[idx + q].max_val = maxv;
554
                        }
555
                    }
556
                }
557
            } else {
558
                for (q = 0; q < 256; q++) {
559
                    if (!isinf(paths[idx - 256 + q].cost)) {
560
                        paths[idx + q].cost = paths[idx - 256 + q].cost + 1;
561
                        paths[idx + q].prev = idx - 256 + q;
562
                        paths[idx + q].min_val = FFMIN(paths[idx - 256 + q].min_val, q);
563
                        paths[idx + q].max_val = FFMAX(paths[idx - 256 + q].max_val, q);
564
                        continue;
565
                    }
566
                    for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, 256); i++) {
567
                        float cost;
568
                        int minv, maxv;
569
                        if (isinf(paths[idx - 256 + i].cost))
570
                            continue;
571
                        cost = paths[idx - 256 + i].cost + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
572
                        minv = FFMIN(paths[idx - 256 + i].min_val, q);
573
                        maxv = FFMAX(paths[idx - 256 + i].max_val, q);
574
                        if (cost < paths[idx + q].cost && maxv-minv < SCALE_MAX_DIFF) {
575
                            paths[idx + q].cost    = cost;
576
                            paths[idx + q].prev    = idx - 256 + i;
577
                            paths[idx + q].min_val = minv;
578
                            paths[idx + q].max_val = maxv;
579
                        }
580
                    }
581
                }
582
            }
583
            sce->zeroes[w*16+g] = !nz;
584
            start += sce->ics.swb_sizes[g];
585
            idx   += 256;
586
        }
587
    }
588
    idx -= 256;
589
    mincost = paths[idx].cost;
590
    minq    = idx;
591
    for (i = 1; i < 256; i++) {
592
        if (paths[idx + i].cost < mincost) {
593
            mincost = paths[idx + i].cost;
594
            minq = idx + i;
595
        }
596
    }
597
    while (minq >= 256) {
598
        sce->sf_idx[bandaddr[minq>>8]] = minq & 0xFF;
599
        minq = paths[minq].prev;
600
    }
601
    //set the same quantizers inside window groups
602
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
603
        for (g = 0;  g < sce->ics.num_swb; g++)
604
            for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
605
                sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
606
}
607

    
608
/**
609
 * two-loop quantizers search taken from ISO 13818-7 Appendix C
610
 */
611
static void search_for_quantizers_twoloop(AVCodecContext *avctx,
612
                                          AACEncContext *s,
613
                                          SingleChannelElement *sce,
614
                                          const float lambda)
615
{
616
    int start = 0, i, w, w2, g;
617
    int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
618
    float dists[128], uplims[128];
619
    int fflag, minscaler;
620
    int its  = 0;
621
    int allz = 0;
622
    float minthr = INFINITY;
623

    
624
    //XXX: some heuristic to determine initial quantizers will reduce search time
625
    memset(dists, 0, sizeof(dists));
626
    //determine zero bands and upper limits
627
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
628
        for (g = 0;  g < sce->ics.num_swb; g++) {
629
            int nz = 0;
630
            float uplim = 0.0f;
631
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
632
                FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
633
                uplim += band->threshold;
634
                if (band->energy <= band->threshold || band->threshold == 0.0f) {
635
                    sce->zeroes[(w+w2)*16+g] = 1;
636
                    continue;
637
                }
638
                nz = 1;
639
            }
640
            uplims[w*16+g] = uplim *512;
641
            sce->zeroes[w*16+g] = !nz;
642
            if (nz)
643
                minthr = FFMIN(minthr, uplim);
644
            allz = FFMAX(allz, nz);
645
        }
646
    }
647
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
648
        for (g = 0;  g < sce->ics.num_swb; g++) {
649
            if (sce->zeroes[w*16+g]) {
650
                sce->sf_idx[w*16+g] = SCALE_ONE_POS;
651
                continue;
652
            }
653
            sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2(uplims[w*16+g]/minthr)*4,59);
654
        }
655
    }
656

    
657
    if (!allz)
658
        return;
659
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
660
    //perform two-loop search
661
    //outer loop - improve quality
662
    do {
663
        int tbits, qstep;
664
        minscaler = sce->sf_idx[0];
665
        //inner loop - quantize spectrum to fit into given number of bits
666
        qstep = its ? 1 : 32;
667
        do {
668
            int prev = -1;
669
            tbits = 0;
670
            fflag = 0;
671
            for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
672
                start = w*128;
673
                for (g = 0;  g < sce->ics.num_swb; g++) {
674
                    const float *coefs = sce->coeffs + start;
675
                    const float *scaled = s->scoefs + start;
676
                    int bits = 0;
677
                    int cb;
678
                    float mindist = INFINITY;
679
                    int minbits = 0;
680

    
681
                    if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218)
682
                        continue;
683
                    minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
684
                    for (cb = 0; cb <= ESC_BT; cb++) {
685
                        float dist = 0.0f;
686
                        int bb = 0;
687
                        for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
688
                            int b;
689
                            dist += quantize_band_cost(s, coefs + w2*128,
690
                                                       scaled + w2*128,
691
                                                       sce->ics.swb_sizes[g],
692
                                                       sce->sf_idx[w*16+g],
693
                                                       ESC_BT,
694
                                                       lambda,
695
                                                       INFINITY,
696
                                                       &b);
697
                            bb += b;
698
                        }
699
                        if (dist < mindist) {
700
                            mindist = dist;
701
                            minbits = bb;
702
                        }
703
                    }
704
                    dists[w*16+g] = (mindist - minbits) / lambda;
705
                    bits = minbits;
706
                    if (prev != -1) {
707
                        bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
708
                    }
709
                    tbits += bits;
710
                    start += sce->ics.swb_sizes[g];
711
                    prev = sce->sf_idx[w*16+g];
712
                }
713
            }
714
            if (tbits > destbits) {
715
                for (i = 0; i < 128; i++)
716
                    if (sce->sf_idx[i] < 218 - qstep)
717
                        sce->sf_idx[i] += qstep;
718
            } else {
719
                for (i = 0; i < 128; i++)
720
                    if (sce->sf_idx[i] > 60 - qstep)
721
                        sce->sf_idx[i] -= qstep;
722
            }
723
            qstep >>= 1;
724
            if (!qstep && tbits > destbits*1.02)
725
                qstep = 1;
726
            if (sce->sf_idx[0] >= 217)
727
                break;
728
        } while (qstep);
729

    
730
        fflag = 0;
731
        minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
732
        for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
733
            start = w*128;
734
            for (g = 0; g < sce->ics.num_swb; g++) {
735
                int prevsc = sce->sf_idx[w*16+g];
736
                if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
737
                    sce->sf_idx[w*16+g]--;
738
                sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
739
                sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
740
                if (sce->sf_idx[w*16+g] != prevsc)
741
                    fflag = 1;
742
            }
743
        }
744
        its++;
745
    } while (fflag && its < 10);
746
}
747

    
748
static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
749
                                       SingleChannelElement *sce,
750
                                       const float lambda)
751
{
752
    int start = 0, i, w, w2, g;
753
    float uplim[128], maxq[128];
754
    int minq, maxsf;
755
    float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
756
    int last = 0, lastband = 0, curband = 0;
757
    float avg_energy = 0.0;
758
    if (sce->ics.num_windows == 1) {
759
        start = 0;
760
        for (i = 0; i < 1024; i++) {
761
            if (i - start >= sce->ics.swb_sizes[curband]) {
762
                start += sce->ics.swb_sizes[curband];
763
                curband++;
764
            }
765
            if (sce->coeffs[i]) {
766
                avg_energy += sce->coeffs[i] * sce->coeffs[i];
767
                last = i;
768
                lastband = curband;
769
            }
770
        }
771
    } else {
772
        for (w = 0; w < 8; w++) {
773
            const float *coeffs = sce->coeffs + w*128;
774
            start = 0;
775
            for (i = 0; i < 128; i++) {
776
                if (i - start >= sce->ics.swb_sizes[curband]) {
777
                    start += sce->ics.swb_sizes[curband];
778
                    curband++;
779
                }
780
                if (coeffs[i]) {
781
                    avg_energy += coeffs[i] * coeffs[i];
782
                    last = FFMAX(last, i);
783
                    lastband = FFMAX(lastband, curband);
784
                }
785
            }
786
        }
787
    }
788
    last++;
789
    avg_energy /= last;
790
    if (avg_energy == 0.0f) {
791
        for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
792
            sce->sf_idx[i] = SCALE_ONE_POS;
793
        return;
794
    }
795
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
796
        start = w*128;
797
        for (g = 0; g < sce->ics.num_swb; g++) {
798
            float *coefs   = sce->coeffs + start;
799
            const int size = sce->ics.swb_sizes[g];
800
            int start2 = start, end2 = start + size, peakpos = start;
801
            float maxval = -1, thr = 0.0f, t;
802
            maxq[w*16+g] = 0.0f;
803
            if (g > lastband) {
804
                maxq[w*16+g] = 0.0f;
805
                start += size;
806
                for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
807
                    memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
808
                continue;
809
            }
810
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
811
                for (i = 0; i < size; i++) {
812
                    float t = coefs[w2*128+i]*coefs[w2*128+i];
813
                    maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
814
                    thr += t;
815
                    if (sce->ics.num_windows == 1 && maxval < t) {
816
                        maxval  = t;
817
                        peakpos = start+i;
818
                    }
819
                }
820
            }
821
            if (sce->ics.num_windows == 1) {
822
                start2 = FFMAX(peakpos - 2, start2);
823
                end2   = FFMIN(peakpos + 3, end2);
824
            } else {
825
                start2 -= start;
826
                end2   -= start;
827
            }
828
            start += size;
829
            thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
830
            t   = 1.0 - (1.0 * start2 / last);
831
            uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
832
        }
833
    }
834
    memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
835
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
836
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
837
        start = w*128;
838
        for (g = 0;  g < sce->ics.num_swb; g++) {
839
            const float *coefs  = sce->coeffs + start;
840
            const float *scaled = s->scoefs   + start;
841
            const int size      = sce->ics.swb_sizes[g];
842
            int scf, prev_scf, step;
843
            int min_scf = 0, max_scf = 255;
844
            float curdiff;
845
            if (maxq[w*16+g] < 21.544) {
846
                sce->zeroes[w*16+g] = 1;
847
                start += size;
848
                continue;
849
            }
850
            sce->zeroes[w*16+g] = 0;
851
            scf  = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
852
            step = 16;
853
            for (;;) {
854
                float dist = 0.0f;
855
                int quant_max;
856

    
857
                for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
858
                    int b;
859
                    dist += quantize_band_cost(s, coefs + w2*128,
860
                                               scaled + w2*128,
861
                                               sce->ics.swb_sizes[g],
862
                                               scf,
863
                                               ESC_BT,
864
                                               lambda,
865
                                               INFINITY,
866
                                               &b);
867
                    dist -= b;
868
                }
869
                dist *= 1.0f / 512.0f / lambda;
870
                quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
871
                if (quant_max >= 8191) { // too much, return to the previous quantizer
872
                    sce->sf_idx[w*16+g] = prev_scf;
873
                    break;
874
                }
875
                prev_scf = scf;
876
                curdiff = fabsf(dist - uplim[w*16+g]);
877
                if (curdiff == 0.0f)
878
                    step = 0;
879
                else
880
                    step = fabsf(log2(curdiff));
881
                if (dist > uplim[w*16+g])
882
                    step = -step;
883
                if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
884
                    sce->sf_idx[w*16+g] = scf;
885
                    break;
886
                }
887
                scf += step;
888
                if (step > 0)
889
                    min_scf = scf;
890
                else
891
                    max_scf = scf;
892
            }
893
            start += size;
894
        }
895
    }
896
    minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
897
    for (i = 1; i < 128; i++) {
898
        if (!sce->sf_idx[i])
899
            sce->sf_idx[i] = sce->sf_idx[i-1];
900
        else
901
            minq = FFMIN(minq, sce->sf_idx[i]);
902
    }
903
    if (minq == INT_MAX)
904
        minq = 0;
905
    minq = FFMIN(minq, SCALE_MAX_POS);
906
    maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
907
    for (i = 126; i >= 0; i--) {
908
        if (!sce->sf_idx[i])
909
            sce->sf_idx[i] = sce->sf_idx[i+1];
910
        sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
911
    }
912
}
913

    
914
static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
915
                                       SingleChannelElement *sce,
916
                                       const float lambda)
917
{
918
    int start = 0, i, w, w2, g;
919
    int minq = 255;
920

    
921
    memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
922
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
923
        start = w*128;
924
        for (g = 0; g < sce->ics.num_swb; g++) {
925
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
926
                FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
927
                if (band->energy <= band->threshold) {
928
                    sce->sf_idx[(w+w2)*16+g] = 218;
929
                    sce->zeroes[(w+w2)*16+g] = 1;
930
                } else {
931
                    sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2(band->threshold), 80, 218);
932
                    sce->zeroes[(w+w2)*16+g] = 0;
933
                }
934
                minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
935
            }
936
        }
937
    }
938
    for (i = 0; i < 128; i++) {
939
        sce->sf_idx[i] = 140;
940
        //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
941
    }
942
    //set the same quantizers inside window groups
943
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
944
        for (g = 0;  g < sce->ics.num_swb; g++)
945
            for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
946
                sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
947
}
948

    
949
static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
950
                          const float lambda)
951
{
952
    int start = 0, i, w, w2, g;
953
    float M[128], S[128];
954
    float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
955
    SingleChannelElement *sce0 = &cpe->ch[0];
956
    SingleChannelElement *sce1 = &cpe->ch[1];
957
    if (!cpe->common_window)
958
        return;
959
    for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
960
        for (g = 0;  g < sce0->ics.num_swb; g++) {
961
            if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
962
                float dist1 = 0.0f, dist2 = 0.0f;
963
                for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
964
                    FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
965
                    FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
966
                    float minthr = FFMIN(band0->threshold, band1->threshold);
967
                    float maxthr = FFMAX(band0->threshold, band1->threshold);
968
                    for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
969
                        M[i] = (sce0->coeffs[start+w2*128+i]
970
                              + sce1->coeffs[start+w2*128+i]) * 0.5;
971
                        S[i] =  sce0->coeffs[start+w2*128+i]
972
                              - sce1->coeffs[start+w2*128+i];
973
                    }
974
                    abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
975
                    abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
976
                    abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
977
                    abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
978
                    dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
979
                                                L34,
980
                                                sce0->ics.swb_sizes[g],
981
                                                sce0->sf_idx[(w+w2)*16+g],
982
                                                sce0->band_type[(w+w2)*16+g],
983
                                                lambda / band0->threshold, INFINITY, NULL);
984
                    dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
985
                                                R34,
986
                                                sce1->ics.swb_sizes[g],
987
                                                sce1->sf_idx[(w+w2)*16+g],
988
                                                sce1->band_type[(w+w2)*16+g],
989
                                                lambda / band1->threshold, INFINITY, NULL);
990
                    dist2 += quantize_band_cost(s, M,
991
                                                M34,
992
                                                sce0->ics.swb_sizes[g],
993
                                                sce0->sf_idx[(w+w2)*16+g],
994
                                                sce0->band_type[(w+w2)*16+g],
995
                                                lambda / maxthr, INFINITY, NULL);
996
                    dist2 += quantize_band_cost(s, S,
997
                                                S34,
998
                                                sce1->ics.swb_sizes[g],
999
                                                sce1->sf_idx[(w+w2)*16+g],
1000
                                                sce1->band_type[(w+w2)*16+g],
1001
                                                lambda / minthr, INFINITY, NULL);
1002
                }
1003
                cpe->ms_mask[w*16+g] = dist2 < dist1;
1004
            }
1005
            start += sce0->ics.swb_sizes[g];
1006
        }
1007
    }
1008
}
1009

    
1010
AACCoefficientsEncoder ff_aac_coders[] = {
1011
    {
1012
        search_for_quantizers_faac,
1013
        encode_window_bands_info,
1014
        quantize_and_encode_band,
1015
//        search_for_ms,
1016
    },
1017
    {
1018
        search_for_quantizers_anmr,
1019
        encode_window_bands_info,
1020
        quantize_and_encode_band,
1021
//        search_for_ms,
1022
    },
1023
    {
1024
        search_for_quantizers_twoloop,
1025
        encode_window_bands_info,
1026
        quantize_and_encode_band,
1027
//        search_for_ms,
1028
    },
1029
    {
1030
        search_for_quantizers_fast,
1031
        encode_window_bands_info,
1032
        quantize_and_encode_band,
1033
//        search_for_ms,
1034
    },
1035
};