Statistics
| Branch: | Revision:

ffmpeg / libavcodec / aaccoder.c @ 78e65cd7

History | View | Annotate | Download (39.3 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
    return pow(coef * Q, 0.75) + 0.4054;
65
}
66

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

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

    
92
static av_always_inline int quant2(float coef, const float Q)
93
{
94
    return pow(coef * Q, 0.75);
95
}
96

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

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

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

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

    
236
//START_TIMER
237
    if(!cb)
238
        return;
239

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

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

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

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

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

    
425
    //convert resulting path from backward-linked list
426
    stack_len = 0;
427
    idx = 0;
428
    for(cb = 1; cb < 12; cb++){
429
        if(path[max_sfb][cb].cost < path[max_sfb][idx].cost)
430
            idx = cb;
431
    }
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
static void encode_window_bands_info_fixed(AACEncContext *s, SingleChannelElement *sce,
461
                                           int win, int group_len, const float lambda)
462
{
463
    encode_window_bands_info(s, sce, win, group_len, 1.0f);
464
}
465

    
466

    
467
typedef struct TrellisPath {
468
    float cost;
469
    int prev;
470
    int min_val;
471
    int max_val;
472
} TrellisPath;
473

    
474
static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
475
                                       SingleChannelElement *sce, const float lambda)
476
{
477
    int q, w, w2, g, start = 0;
478
    int i;
479
    int idx;
480
    TrellisPath paths[256*121];
481
    int bandaddr[121];
482
    int minq;
483
    float mincost;
484

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

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

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

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

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

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

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

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

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

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

    
919
static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
920
                                       SingleChannelElement *sce, const float lambda)
921
{
922
    int start = 0, i, w, w2, g;
923
    int minq = 255;
924

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

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

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