~ubuntu-branches/debian/sid/ffmpeg/sid

« back to all changes in this revision

Viewing changes to libavcodec/aaccoder.c

  • Committer: Package Import Robot
  • Author(s): Andreas Cadhalpun, Fabian Greffrath, Andreas Cadhalpun
  • Date: 2015-09-22 15:15:20 UTC
  • mfrom: (0.1.29)
  • Revision ID: package-import@ubuntu.com-20150922151520-hhmd3in9ykigjvs9
Tags: 7:2.8-1
[ Fabian Greffrath ]
* Pass the --dbg-package=ffmpeg-dbg parameter only to dh_strip.
* Add alternative Depends: libavcodec-ffmpeg-extra56 to libavcodec-dev and
  ffmpeg-dbg to allow for building and debugging with this library installed.

[ Andreas Cadhalpun ]
* Import new major upstream release 2.8.
* Remove the transitional lib*-ffmpeg-dev packages.
* Drop old Breaks on kodi-bin.
* Drop workaround for sparc, which is no Debian architecture anymore.
* Re-enable x265 on alpha, as it's available again.
* Disable unavailable frei0r, opencv and x264 on mips64el.
* Disable libopenjpeg (#787275) and libschroedinger (#787957) decoders.
  (Closes: #786670)
* Disable libdc1394 on sparc64, because it links against the broken due to
  #790560 libudev1.
* Enable libsnappy support.
* Add new symbols.
* Update debian/copyright.
* Update debian/tests/encdec_list.txt.
* Add hls-only-seek-if-there-is-an-offset.patch. (Closes: #798189)
* Add 'Breaks: libavutil-ffmpeg54 (>= 8:0)' to the libraries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include "aac.h"
40
40
#include "aacenc.h"
41
41
#include "aactab.h"
 
42
#include "aacenctab.h"
 
43
#include "aacenc_utils.h"
 
44
#include "aacenc_quantization.h"
 
45
#include "aac_tablegen_decl.h"
 
46
 
 
47
#include "aacenc_is.h"
 
48
#include "aacenc_tns.h"
 
49
#include "aacenc_pred.h"
42
50
 
43
51
/** Frequency in Hz for lower limit of noise substitution **/
44
 
#define NOISE_LOW_LIMIT 4000
45
 
 
46
 
/** Total number of usable codebooks **/
47
 
#define CB_TOT 13
48
 
 
49
 
/** bits needed to code codebook run value for long windows */
50
 
static const uint8_t run_value_bits_long[64] = {
51
 
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
52
 
     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5, 10,
53
 
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
54
 
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
55
 
};
56
 
 
57
 
/** bits needed to code codebook run value for short windows */
58
 
static const uint8_t run_value_bits_short[16] = {
59
 
    3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
60
 
};
61
 
 
62
 
static const uint8_t * const run_value_bits[2] = {
63
 
    run_value_bits_long, run_value_bits_short
64
 
};
65
 
 
66
 
/** Map to convert values from BandCodingPath index to a codebook index **/
67
 
static const uint8_t aac_cb_out_map[CB_TOT]  = {0,1,2,3,4,5,6,7,8,9,10,11,13};
68
 
/** Inverse map to convert from codebooks to BandCodingPath indices **/
69
 
static const uint8_t aac_cb_in_map[CB_TOT+1] = {0,1,2,3,4,5,6,7,8,9,10,11,0,12};
70
 
 
71
 
/**
72
 
 * Quantize one coefficient.
73
 
 * @return absolute value of the quantized coefficient
74
 
 * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
75
 
 */
76
 
static av_always_inline int quant(float coef, const float Q)
77
 
{
78
 
    float a = coef * Q;
79
 
    return sqrtf(a * sqrtf(a)) + 0.4054;
80
 
}
81
 
 
82
 
static void quantize_bands(int *out, const float *in, const float *scaled,
83
 
                           int size, float Q34, int is_signed, int maxval)
84
 
{
85
 
    int i;
86
 
    double qc;
87
 
    for (i = 0; i < size; i++) {
88
 
        qc = scaled[i] * Q34;
89
 
        out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
90
 
        if (is_signed && in[i] < 0.0f) {
91
 
            out[i] = -out[i];
92
 
        }
93
 
    }
94
 
}
95
 
 
96
 
static void abs_pow34_v(float *out, const float *in, const int size)
97
 
{
98
 
#ifndef USE_REALLY_FULL_SEARCH
99
 
    int i;
100
 
    for (i = 0; i < size; i++) {
101
 
        float a = fabsf(in[i]);
102
 
        out[i] = sqrtf(a * sqrtf(a));
103
 
    }
104
 
#endif /* USE_REALLY_FULL_SEARCH */
105
 
}
106
 
 
107
 
static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
108
 
static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
109
 
 
110
 
/**
111
 
 * Calculate rate distortion cost for quantizing with given codebook
112
 
 *
113
 
 * @return quantization distortion
114
 
 */
115
 
static av_always_inline float quantize_and_encode_band_cost_template(
116
 
                                struct AACEncContext *s,
117
 
                                PutBitContext *pb, const float *in,
118
 
                                const float *scaled, int size, int scale_idx,
119
 
                                int cb, const float lambda, const float uplim,
120
 
                                int *bits, int BT_ZERO, int BT_UNSIGNED,
121
 
                                int BT_PAIR, int BT_ESC, int BT_NOISE)
122
 
{
123
 
    const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
124
 
    const float Q   = ff_aac_pow2sf_tab [q_idx];
125
 
    const float Q34 = ff_aac_pow34sf_tab[q_idx];
126
 
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
127
 
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
128
 
    int i, j;
129
 
    float cost = 0;
130
 
    const int dim = BT_PAIR ? 2 : 4;
131
 
    int resbits = 0;
132
 
    int off;
133
 
 
134
 
    if (BT_ZERO) {
135
 
        for (i = 0; i < size; i++)
136
 
            cost += in[i]*in[i];
137
 
        if (bits)
138
 
            *bits = 0;
139
 
        return cost * lambda;
140
 
    }
141
 
    if (BT_NOISE) {
142
 
        for (i = 0; i < size; i++)
143
 
            cost += in[i]*in[i];
144
 
        if (bits)
145
 
            *bits = 0;
146
 
        return cost * lambda;
147
 
    }
148
 
    if (!scaled) {
149
 
        abs_pow34_v(s->scoefs, in, size);
150
 
        scaled = s->scoefs;
151
 
    }
152
 
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, aac_cb_maxval[cb]);
153
 
    if (BT_UNSIGNED) {
154
 
        off = 0;
155
 
    } else {
156
 
        off = aac_cb_maxval[cb];
157
 
    }
158
 
    for (i = 0; i < size; i += dim) {
159
 
        const float *vec;
160
 
        int *quants = s->qcoefs + i;
161
 
        int curidx = 0;
162
 
        int curbits;
163
 
        float rd = 0.0f;
164
 
        for (j = 0; j < dim; j++) {
165
 
            curidx *= aac_cb_range[cb];
166
 
            curidx += quants[j] + off;
167
 
        }
168
 
        curbits =  ff_aac_spectral_bits[cb-1][curidx];
169
 
        vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
170
 
        if (BT_UNSIGNED) {
171
 
            for (j = 0; j < dim; j++) {
172
 
                float t = fabsf(in[i+j]);
173
 
                float di;
174
 
                if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
175
 
                    if (t >= CLIPPED_ESCAPE) {
176
 
                        di = t - CLIPPED_ESCAPE;
177
 
                        curbits += 21;
178
 
                    } else {
179
 
                        int c = av_clip_uintp2(quant(t, Q), 13);
180
 
                        di = t - c*cbrtf(c)*IQ;
181
 
                        curbits += av_log2(c)*2 - 4 + 1;
182
 
                    }
183
 
                } else {
184
 
                    di = t - vec[j]*IQ;
185
 
                }
186
 
                if (vec[j] != 0.0f)
187
 
                    curbits++;
188
 
                rd += di*di;
189
 
            }
190
 
        } else {
191
 
            for (j = 0; j < dim; j++) {
192
 
                float di = in[i+j] - vec[j]*IQ;
193
 
                rd += di*di;
194
 
            }
195
 
        }
196
 
        cost    += rd * lambda + curbits;
197
 
        resbits += curbits;
198
 
        if (cost >= uplim)
199
 
            return uplim;
200
 
        if (pb) {
201
 
            put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
202
 
            if (BT_UNSIGNED)
203
 
                for (j = 0; j < dim; j++)
204
 
                    if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
205
 
                        put_bits(pb, 1, in[i+j] < 0.0f);
206
 
            if (BT_ESC) {
207
 
                for (j = 0; j < 2; j++) {
208
 
                    if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
209
 
                        int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q), 13);
210
 
                        int len = av_log2(coef);
211
 
 
212
 
                        put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
213
 
                        put_sbits(pb, len, coef);
214
 
                    }
215
 
                }
216
 
            }
217
 
        }
218
 
    }
219
 
 
220
 
    if (bits)
221
 
        *bits = resbits;
222
 
    return cost;
223
 
}
224
 
 
225
 
static float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
226
 
                                                const float *in, const float *scaled,
227
 
                                                int size, int scale_idx, int cb,
228
 
                                                const float lambda, const float uplim,
229
 
                                                int *bits) {
230
 
    av_assert0(0);
231
 
    return 0.0f;
232
 
}
233
 
 
234
 
#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE) \
235
 
static float quantize_and_encode_band_cost_ ## NAME(                                    \
236
 
                                struct AACEncContext *s,                                \
237
 
                                PutBitContext *pb, const float *in,                     \
238
 
                                const float *scaled, int size, int scale_idx,           \
239
 
                                int cb, const float lambda, const float uplim,          \
240
 
                                int *bits) {                                            \
241
 
    return quantize_and_encode_band_cost_template(                                      \
242
 
                                s, pb, in, scaled, size, scale_idx,                     \
243
 
                                BT_ESC ? ESC_BT : cb, lambda, uplim, bits,              \
244
 
                                BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE);       \
245
 
}
246
 
 
247
 
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO,  1, 0, 0, 0, 0)
248
 
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0)
249
 
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0)
250
 
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0)
251
 
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0)
252
 
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC,   0, 1, 1, 1, 0)
253
 
QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1)
254
 
 
255
 
static float (*const quantize_and_encode_band_cost_arr[])(
256
 
                                struct AACEncContext *s,
257
 
                                PutBitContext *pb, const float *in,
258
 
                                const float *scaled, int size, int scale_idx,
259
 
                                int cb, const float lambda, const float uplim,
260
 
                                int *bits) = {
261
 
    quantize_and_encode_band_cost_ZERO,
262
 
    quantize_and_encode_band_cost_SQUAD,
263
 
    quantize_and_encode_band_cost_SQUAD,
264
 
    quantize_and_encode_band_cost_UQUAD,
265
 
    quantize_and_encode_band_cost_UQUAD,
266
 
    quantize_and_encode_band_cost_SPAIR,
267
 
    quantize_and_encode_band_cost_SPAIR,
268
 
    quantize_and_encode_band_cost_UPAIR,
269
 
    quantize_and_encode_band_cost_UPAIR,
270
 
    quantize_and_encode_band_cost_UPAIR,
271
 
    quantize_and_encode_band_cost_UPAIR,
272
 
    quantize_and_encode_band_cost_ESC,
273
 
    quantize_and_encode_band_cost_NONE,     /* CB 12 doesn't exist */
274
 
    quantize_and_encode_band_cost_NOISE,
275
 
};
276
 
 
277
 
#define quantize_and_encode_band_cost(                                  \
278
 
                                s, pb, in, scaled, size, scale_idx, cb, \
279
 
                                lambda, uplim, bits)                    \
280
 
    quantize_and_encode_band_cost_arr[cb](                              \
281
 
                                s, pb, in, scaled, size, scale_idx, cb, \
282
 
                                lambda, uplim, bits)
283
 
 
284
 
static float quantize_band_cost(struct AACEncContext *s, const float *in,
285
 
                                const float *scaled, int size, int scale_idx,
286
 
                                int cb, const float lambda, const float uplim,
287
 
                                int *bits)
288
 
{
289
 
    return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
290
 
                                         cb, lambda, uplim, bits);
291
 
}
292
 
 
293
 
static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
294
 
                                     const float *in, int size, int scale_idx,
295
 
                                     int cb, const float lambda)
296
 
{
297
 
    quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
298
 
                                  INFINITY, NULL);
299
 
}
300
 
 
301
 
static float find_max_val(int group_len, int swb_size, const float *scaled) {
302
 
    float maxval = 0.0f;
303
 
    int w2, i;
304
 
    for (w2 = 0; w2 < group_len; w2++) {
305
 
        for (i = 0; i < swb_size; i++) {
306
 
            maxval = FFMAX(maxval, scaled[w2*128+i]);
307
 
        }
308
 
    }
309
 
    return maxval;
310
 
}
311
 
 
312
 
static int find_min_book(float maxval, int sf) {
313
 
    float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
314
 
    float Q34 = sqrtf(Q * sqrtf(Q));
315
 
    int qmaxval, cb;
316
 
    qmaxval = maxval * Q34 + 0.4054f;
317
 
    if      (qmaxval ==  0) cb = 0;
318
 
    else if (qmaxval ==  1) cb = 1;
319
 
    else if (qmaxval ==  2) cb = 3;
320
 
    else if (qmaxval <=  4) cb = 5;
321
 
    else if (qmaxval <=  7) cb = 7;
322
 
    else if (qmaxval <= 12) cb = 9;
323
 
    else                    cb = 11;
324
 
    return cb;
325
 
}
 
52
#define NOISE_LOW_LIMIT 4500
 
53
 
 
54
/* Energy spread threshold value below which no PNS is used, this corresponds to
 
55
 * typically around 17Khz, after which PNS usage decays ending at 19Khz */
 
56
#define NOISE_SPREAD_THRESHOLD 0.5f
 
57
 
 
58
/* This constant gets divided by lambda to return ~1.65 which when multiplied
 
59
 * by the band->threshold and compared to band->energy is the boundary between
 
60
 * excessive PNS and little PNS usage. */
 
61
#define NOISE_LAMBDA_NUMERATOR 252.1f
326
62
 
327
63
/**
328
64
 * structure used in optimal codebook search
339
75
static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
340
76
                                     int win, int group_len, const float lambda)
341
77
{
342
 
    BandCodingPath path[120][CB_TOT];
 
78
    BandCodingPath path[120][CB_TOT_ALL];
343
79
    int w, swb, cb, start, size;
344
80
    int i, j;
345
81
    const int max_sfb  = sce->ics.max_sfb;
352
88
 
353
89
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
354
90
    start = win*128;
355
 
    for (cb = 0; cb < CB_TOT; cb++) {
 
91
    for (cb = 0; cb < CB_TOT_ALL; cb++) {
356
92
        path[0][cb].cost     = 0.0f;
357
93
        path[0][cb].prev_idx = -1;
358
94
        path[0][cb].run      = 0;
360
96
    for (swb = 0; swb < max_sfb; swb++) {
361
97
        size = sce->ics.swb_sizes[swb];
362
98
        if (sce->zeroes[win*16 + swb]) {
363
 
            for (cb = 0; cb < CB_TOT; cb++) {
 
99
            for (cb = 0; cb < CB_TOT_ALL; cb++) {
364
100
                path[swb+1][cb].prev_idx = cb;
365
101
                path[swb+1][cb].cost     = path[swb][cb].cost;
366
102
                path[swb+1][cb].run      = path[swb][cb].run + 1;
370
106
            int mincb = next_mincb;
371
107
            next_minrd = INFINITY;
372
108
            next_mincb = 0;
373
 
            for (cb = 0; cb < CB_TOT; cb++) {
 
109
            for (cb = 0; cb < CB_TOT_ALL; cb++) {
374
110
                float cost_stay_here, cost_get_here;
375
111
                float rd = 0.0f;
 
112
                if (cb >= 12 && sce->band_type[win*16+swb] < aac_cb_out_map[cb] ||
 
113
                    cb  < aac_cb_in_map[sce->band_type[win*16+swb]] && sce->band_type[win*16+swb] > aac_cb_out_map[cb]) {
 
114
                    path[swb+1][cb].prev_idx = -1;
 
115
                    path[swb+1][cb].cost     = INFINITY;
 
116
                    path[swb+1][cb].run      = path[swb][cb].run + 1;
 
117
                    continue;
 
118
                }
376
119
                for (w = 0; w < group_len; w++) {
377
120
                    FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb];
378
 
                    rd += quantize_band_cost(s, sce->coeffs + start + w*128,
379
 
                                             s->scoefs + start + w*128, size,
 
121
                    rd += quantize_band_cost(s, &sce->coeffs[start + w*128],
 
122
                                             &s->scoefs[start + w*128], size,
380
123
                                             sce->sf_idx[(win+w)*16+swb], aac_cb_out_map[cb],
381
 
                                             lambda / band->threshold, INFINITY, NULL);
 
124
                                             lambda / band->threshold, INFINITY, NULL, 0);
382
125
                }
383
126
                cost_stay_here = path[swb][cb].cost + rd;
384
127
                cost_get_here  = minrd              + rd + run_bits + 4;
406
149
    //convert resulting path from backward-linked list
407
150
    stack_len = 0;
408
151
    idx       = 0;
409
 
    for (cb = 1; cb < CB_TOT; cb++)
 
152
    for (cb = 1; cb < CB_TOT_ALL; cb++)
410
153
        if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
411
154
            idx = cb;
412
155
    ppos = max_sfb;
413
156
    while (ppos > 0) {
 
157
        av_assert1(idx >= 0);
414
158
        cb = idx;
415
159
        stackrun[stack_len] = path[ppos][cb].run;
416
160
        stackcb [stack_len] = cb;
441
185
static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
442
186
                                  int win, int group_len, const float lambda)
443
187
{
444
 
    BandCodingPath path[120][CB_TOT];
 
188
    BandCodingPath path[120][CB_TOT_ALL];
445
189
    int w, swb, cb, start, size;
446
190
    int i, j;
447
191
    const int max_sfb  = sce->ics.max_sfb;
454
198
 
455
199
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
456
200
    start = win*128;
457
 
    for (cb = 0; cb < CB_TOT; cb++) {
 
201
    for (cb = 0; cb < CB_TOT_ALL; cb++) {
458
202
        path[0][cb].cost     = run_bits+4;
459
203
        path[0][cb].prev_idx = -1;
460
204
        path[0][cb].run      = 0;
478
222
            }
479
223
            next_minbits = path[swb+1][0].cost;
480
224
            next_mincb = 0;
481
 
            for (cb = 1; cb < CB_TOT; cb++) {
 
225
            for (cb = 1; cb < CB_TOT_ALL; cb++) {
482
226
                path[swb+1][cb].cost = 61450;
483
227
                path[swb+1][cb].prev_idx = -1;
484
228
                path[swb+1][cb].run = 0;
495
239
                path[swb+1][cb].prev_idx = -1;
496
240
                path[swb+1][cb].run = 0;
497
241
            }
498
 
            for (cb = startcb; cb < CB_TOT; cb++) {
 
242
            for (cb = startcb; cb < CB_TOT_ALL; cb++) {
499
243
                float cost_stay_here, cost_get_here;
500
244
                float bits = 0.0f;
501
 
                if (cb == 12 && sce->band_type[win*16+swb] != NOISE_BT) {
 
245
                if (cb >= 12 && sce->band_type[win*16+swb] != aac_cb_out_map[cb]) {
502
246
                    path[swb+1][cb].cost = 61450;
503
247
                    path[swb+1][cb].prev_idx = -1;
504
248
                    path[swb+1][cb].run = 0;
505
249
                    continue;
506
250
                }
507
251
                for (w = 0; w < group_len; w++) {
508
 
                    bits += quantize_band_cost(s, sce->coeffs + start + w*128,
509
 
                                               s->scoefs + start + w*128, size,
510
 
                                               sce->sf_idx[(win+w)*16+swb],
 
252
                    bits += quantize_band_cost(s, &sce->coeffs[start + w*128],
 
253
                                               &s->scoefs[start + w*128], size,
 
254
                                               sce->sf_idx[win*16+swb],
511
255
                                               aac_cb_out_map[cb],
512
 
                                               0, INFINITY, NULL);
 
256
                                               0, INFINITY, NULL, 0);
513
257
                }
514
258
                cost_stay_here = path[swb][cb].cost + bits;
515
259
                cost_get_here  = minbits            + bits + run_bits + 4;
537
281
    //convert resulting path from backward-linked list
538
282
    stack_len = 0;
539
283
    idx       = 0;
540
 
    for (cb = 1; cb < CB_TOT; cb++)
 
284
    for (cb = 1; cb < CB_TOT_ALL; cb++)
541
285
        if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
542
286
            idx = cb;
543
287
    ppos = max_sfb;
570
314
    }
571
315
}
572
316
 
573
 
/** Return the minimum scalefactor where the quantized coef does not clip. */
574
 
static av_always_inline uint8_t coef2minsf(float coef) {
575
 
    return av_clip_uint8(log2f(coef)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
576
 
}
577
 
 
578
 
/** Return the maximum scalefactor where the quantized coef is not zero. */
579
 
static av_always_inline uint8_t coef2maxsf(float coef) {
580
 
    return av_clip_uint8(log2f(coef)*4 +  6 + SCALE_ONE_POS - SCALE_DIV_512);
581
 
}
582
 
 
583
317
typedef struct TrellisPath {
584
318
    float cost;
585
319
    int prev;
588
322
#define TRELLIS_STAGES 121
589
323
#define TRELLIS_STATES (SCALE_MAX_DIFF+1)
590
324
 
 
325
static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
 
326
{
 
327
    int w, g, start = 0;
 
328
    int minscaler_n = sce->sf_idx[0], minscaler_i = sce->sf_idx[0];
 
329
    int bands = 0;
 
330
 
 
331
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
 
332
        start = 0;
 
333
        for (g = 0;  g < sce->ics.num_swb; g++) {
 
334
            if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
 
335
                sce->sf_idx[w*16+g] = av_clip(ceilf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
 
336
                minscaler_i = FFMIN(minscaler_i, sce->sf_idx[w*16+g]);
 
337
                bands++;
 
338
            } else if (sce->band_type[w*16+g] == NOISE_BT) {
 
339
                sce->sf_idx[w*16+g] = av_clip(4+log2f(sce->pns_ener[w*16+g])*2, -100, 155);
 
340
                minscaler_n = FFMIN(minscaler_n, sce->sf_idx[w*16+g]);
 
341
                bands++;
 
342
            }
 
343
            start += sce->ics.swb_sizes[g];
 
344
        }
 
345
    }
 
346
 
 
347
    if (!bands)
 
348
        return;
 
349
 
 
350
    /* Clip the scalefactor indices */
 
351
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
 
352
        for (g = 0;  g < sce->ics.num_swb; g++) {
 
353
            if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
 
354
                sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_i, minscaler_i + SCALE_MAX_DIFF);
 
355
            } else if (sce->band_type[w*16+g] == NOISE_BT) {
 
356
                sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_n, minscaler_n + SCALE_MAX_DIFF);
 
357
            }
 
358
        }
 
359
    }
 
360
}
 
361
 
591
362
static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
592
363
                                       SingleChannelElement *sce,
593
364
                                       const float lambda)
653
424
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
654
425
        start = w*128;
655
426
        for (g = 0; g < sce->ics.num_swb; g++) {
656
 
            const float *coefs = sce->coeffs + start;
 
427
            const float *coefs = &sce->coeffs[start];
657
428
            float qmin, qmax;
658
429
            int nz = 0;
659
430
 
692
463
                    for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
693
464
                        FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
694
465
                        dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
695
 
                                                   q + q0, cb, lambda / band->threshold, INFINITY, NULL);
 
466
                                                   q + q0, cb, lambda / band->threshold, INFINITY, NULL, 0);
696
467
                    }
697
468
                    minrd = FFMIN(minrd, dist);
698
469
 
748
519
{
749
520
    int start = 0, i, w, w2, g;
750
521
    int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels * (lambda / 120.f);
751
 
    const float freq_mult = avctx->sample_rate/(1024.0f/sce->ics.num_windows)/2.0f;
752
522
    float dists[128] = { 0 }, uplims[128] = { 0 };
753
523
    float maxvals[128];
754
 
    int noise_sf[128] = { 0 };
755
 
    int fflag, minscaler, minscaler_n;
 
524
    int fflag, minscaler;
756
525
    int its  = 0;
757
526
    int allz = 0;
758
527
    float minthr = INFINITY;
763
532
    //XXX: some heuristic to determine initial quantizers will reduce search time
764
533
    //determine zero bands and upper limits
765
534
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
766
 
        start = 0;
767
535
        for (g = 0;  g < sce->ics.num_swb; g++) {
768
536
            int nz = 0;
769
537
            float uplim = 0.0f, energy = 0.0f;
770
538
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
771
539
                FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
772
 
                uplim += band->threshold;
 
540
                uplim  += band->threshold;
773
541
                energy += band->energy;
774
542
                if (band->energy <= band->threshold || band->threshold == 0.0f) {
775
543
                    sce->zeroes[(w+w2)*16+g] = 1;
778
546
                nz = 1;
779
547
            }
780
548
            uplims[w*16+g] = uplim *512;
781
 
            if (s->options.pns && start*freq_mult > NOISE_LOW_LIMIT && energy < uplim * 1.2f) {
782
 
                noise_sf[w*16+g] = av_clip(4+FFMIN(log2f(energy)*2,255), -100, 155);
783
 
                sce->band_type[w*16+g] = NOISE_BT;
784
 
                nz= 1;
785
 
            } else { /** Band type will be determined by the twoloop algorithm */
786
 
                sce->band_type[w*16+g] = 0;
787
 
            }
788
549
            sce->zeroes[w*16+g] = !nz;
789
550
            if (nz)
790
551
                minthr = FFMIN(minthr, uplim);
791
552
            allz |= nz;
792
 
            start += sce->ics.swb_sizes[g];
793
553
        }
794
554
    }
795
555
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
820
580
    do {
821
581
        int tbits, qstep;
822
582
        minscaler = sce->sf_idx[0];
823
 
        minscaler_n = sce->sf_idx[0];
824
583
        //inner loop - quantize spectrum to fit into given number of bits
825
584
        qstep = its ? 1 : 32;
826
585
        do {
829
588
            for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
830
589
                start = w*128;
831
590
                for (g = 0;  g < sce->ics.num_swb; g++) {
832
 
                    const float *coefs = sce->coeffs + start;
833
 
                    const float *scaled = s->scoefs + start;
 
591
                    const float *coefs = &sce->coeffs[start];
 
592
                    const float *scaled = &s->scoefs[start];
834
593
                    int bits = 0;
835
594
                    int cb;
836
595
                    float dist = 0.0f;
837
596
 
838
 
                    if (sce->band_type[w*16+g] == NOISE_BT) {
839
 
                        minscaler_n = FFMIN(minscaler_n, noise_sf[w*16+g]);
840
 
                        start += sce->ics.swb_sizes[g];
841
 
                        continue;
842
 
                    } else if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
 
597
                    if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
843
598
                        start += sce->ics.swb_sizes[g];
844
599
                        continue;
845
600
                    }
854
609
                                                   cb,
855
610
                                                   1.0f,
856
611
                                                   INFINITY,
857
 
                                                   &b);
 
612
                                                   &b,
 
613
                                                   0);
858
614
                        bits += b;
859
615
                    }
860
616
                    dists[w*16+g] = dist - bits;
883
639
        fflag = 0;
884
640
        minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
885
641
 
886
 
        for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
887
 
            for (g = 0; g < sce->ics.num_swb; g++)
888
 
                if (sce->band_type[w*16+g] == NOISE_BT)
889
 
                    sce->sf_idx[w*16+g] = av_clip(noise_sf[w*16+g], minscaler_n, minscaler_n + SCALE_MAX_DIFF);
890
 
 
891
642
        for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
892
643
            for (g = 0; g < sce->ics.num_swb; g++) {
893
644
                int prevsc = sce->sf_idx[w*16+g];
894
 
                if (sce->band_type[w*16+g] == NOISE_BT)
895
 
                    continue;
896
645
                if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
897
646
                    if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
898
647
                        sce->sf_idx[w*16+g]--;
935
684
        }
936
685
    } else {
937
686
        for (w = 0; w < 8; w++) {
938
 
            const float *coeffs = sce->coeffs + w*128;
 
687
            const float *coeffs = &sce->coeffs[w*128];
939
688
            curband = start = 0;
940
689
            for (i = 0; i < 128; i++) {
941
690
                if (i - start >= sce->ics.swb_sizes[curband]) {
960
709
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
961
710
        start = w*128;
962
711
        for (g = 0; g < sce->ics.num_swb; g++) {
963
 
            float *coefs   = sce->coeffs + start;
 
712
            float *coefs   = &sce->coeffs[start];
964
713
            const int size = sce->ics.swb_sizes[g];
965
714
            int start2 = start, end2 = start + size, peakpos = start;
966
715
            float maxval = -1, thr = 0.0f, t;
1001
750
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
1002
751
        start = w*128;
1003
752
        for (g = 0;  g < sce->ics.num_swb; g++) {
1004
 
            const float *coefs  = sce->coeffs + start;
1005
 
            const float *scaled = s->scoefs   + start;
 
753
            const float *coefs  = &sce->coeffs[start];
 
754
            const float *scaled = &s->scoefs[start];
1006
755
            const int size      = sce->ics.swb_sizes[g];
1007
756
            int scf, prev_scf, step;
1008
757
            int min_scf = -1, max_scf = 256;
1027
776
                                               ESC_BT,
1028
777
                                               lambda,
1029
778
                                               INFINITY,
1030
 
                                               &b);
 
779
                                               &b,
 
780
                                               0);
1031
781
                    dist -= b;
1032
782
                }
1033
783
                dist *= 1.0f / 512.0f / lambda;
1034
 
                quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
 
784
                quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512], ROUND_STANDARD);
1035
785
                if (quant_max >= 8191) { // too much, return to the previous quantizer
1036
786
                    sce->sf_idx[w*16+g] = prev_scf;
1037
787
                    break;
1111
861
                sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
1112
862
}
1113
863
 
1114
 
static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
1115
 
                          const float lambda)
 
864
static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
 
865
{
 
866
    int start = 0, w, w2, g;
 
867
    const float lambda = s->lambda;
 
868
    const float freq_mult = avctx->sample_rate/(1024.0f/sce->ics.num_windows)/2.0f;
 
869
    const float spread_threshold = NOISE_SPREAD_THRESHOLD*(lambda/120.f);
 
870
    const float thr_mult = NOISE_LAMBDA_NUMERATOR/lambda;
 
871
 
 
872
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
 
873
        start = 0;
 
874
        for (g = 0;  g < sce->ics.num_swb; g++) {
 
875
            if (start*freq_mult > NOISE_LOW_LIMIT*(lambda/170.0f)) {
 
876
                float energy = 0.0f, threshold = 0.0f, spread = 0.0f;
 
877
                for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
 
878
                    FFPsyBand *band = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
 
879
                    energy += band->energy;
 
880
                    threshold += band->threshold;
 
881
                    spread += band->spread;
 
882
                }
 
883
                if (spread > spread_threshold*sce->ics.group_len[w] &&
 
884
                    ((sce->zeroes[w*16+g] && energy >= threshold) ||
 
885
                    energy < threshold*thr_mult*sce->ics.group_len[w])) {
 
886
                    sce->band_type[w*16+g] = NOISE_BT;
 
887
                    sce->pns_ener[w*16+g] = energy / sce->ics.group_len[w];
 
888
                    sce->zeroes[w*16+g] = 0;
 
889
                }
 
890
            }
 
891
            start += sce->ics.swb_sizes[g];
 
892
        }
 
893
    }
 
894
}
 
895
 
 
896
static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
1116
897
{
1117
898
    int start = 0, i, w, w2, g;
1118
899
    float M[128], S[128];
1119
900
    float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
 
901
    const float lambda = s->lambda;
1120
902
    SingleChannelElement *sce0 = &cpe->ch[0];
1121
903
    SingleChannelElement *sce1 = &cpe->ch[1];
1122
904
    if (!cpe->common_window)
1123
905
        return;
1124
906
    for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
 
907
        start = 0;
1125
908
        for (g = 0;  g < sce0->ics.num_swb; g++) {
1126
909
            if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
1127
910
                float dist1 = 0.0f, dist2 = 0.0f;
1131
914
                    float minthr = FFMIN(band0->threshold, band1->threshold);
1132
915
                    float maxthr = FFMAX(band0->threshold, band1->threshold);
1133
916
                    for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1134
 
                        M[i] = (sce0->pcoeffs[start+w2*128+i]
1135
 
                              + sce1->pcoeffs[start+w2*128+i]) * 0.5;
 
917
                        M[i] = (sce0->coeffs[start+(w+w2)*128+i]
 
918
                              + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
1136
919
                        S[i] =  M[i]
1137
 
                              - sce1->pcoeffs[start+w2*128+i];
 
920
                              - sce1->coeffs[start+(w+w2)*128+i];
1138
921
                    }
1139
 
                    abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1140
 
                    abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
 
922
                    abs_pow34_v(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
 
923
                    abs_pow34_v(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
1141
924
                    abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
1142
925
                    abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
1143
 
                    dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
 
926
                    dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
1144
927
                                                L34,
1145
928
                                                sce0->ics.swb_sizes[g],
1146
929
                                                sce0->sf_idx[(w+w2)*16+g],
1147
930
                                                sce0->band_type[(w+w2)*16+g],
1148
 
                                                lambda / band0->threshold, INFINITY, NULL);
1149
 
                    dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
 
931
                                                lambda / band0->threshold, INFINITY, NULL, 0);
 
932
                    dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
1150
933
                                                R34,
1151
934
                                                sce1->ics.swb_sizes[g],
1152
935
                                                sce1->sf_idx[(w+w2)*16+g],
1153
936
                                                sce1->band_type[(w+w2)*16+g],
1154
 
                                                lambda / band1->threshold, INFINITY, NULL);
 
937
                                                lambda / band1->threshold, INFINITY, NULL, 0);
1155
938
                    dist2 += quantize_band_cost(s, M,
1156
939
                                                M34,
1157
940
                                                sce0->ics.swb_sizes[g],
1158
941
                                                sce0->sf_idx[(w+w2)*16+g],
1159
942
                                                sce0->band_type[(w+w2)*16+g],
1160
 
                                                lambda / maxthr, INFINITY, NULL);
 
943
                                                lambda / maxthr, INFINITY, NULL, 0);
1161
944
                    dist2 += quantize_band_cost(s, S,
1162
945
                                                S34,
1163
946
                                                sce1->ics.swb_sizes[g],
1164
947
                                                sce1->sf_idx[(w+w2)*16+g],
1165
948
                                                sce1->band_type[(w+w2)*16+g],
1166
 
                                                lambda / minthr, INFINITY, NULL);
 
949
                                                lambda / minthr, INFINITY, NULL, 0);
1167
950
                }
1168
951
                cpe->ms_mask[w*16+g] = dist2 < dist1;
1169
952
            }
1177
960
        search_for_quantizers_faac,
1178
961
        encode_window_bands_info,
1179
962
        quantize_and_encode_band,
 
963
        ff_aac_encode_tns_info,
 
964
        ff_aac_encode_main_pred,
 
965
        ff_aac_adjust_common_prediction,
 
966
        ff_aac_apply_main_pred,
 
967
        ff_aac_apply_tns,
 
968
        set_special_band_scalefactors,
 
969
        search_for_pns,
 
970
        ff_aac_search_for_tns,
1180
971
        search_for_ms,
 
972
        ff_aac_search_for_is,
 
973
        ff_aac_search_for_pred,
1181
974
    },
1182
975
    [AAC_CODER_ANMR] = {
1183
976
        search_for_quantizers_anmr,
1184
977
        encode_window_bands_info,
1185
978
        quantize_and_encode_band,
 
979
        ff_aac_encode_tns_info,
 
980
        ff_aac_encode_main_pred,
 
981
        ff_aac_adjust_common_prediction,
 
982
        ff_aac_apply_main_pred,
 
983
        ff_aac_apply_tns,
 
984
        set_special_band_scalefactors,
 
985
        search_for_pns,
 
986
        ff_aac_search_for_tns,
1186
987
        search_for_ms,
 
988
        ff_aac_search_for_is,
 
989
        ff_aac_search_for_pred,
1187
990
    },
1188
991
    [AAC_CODER_TWOLOOP] = {
1189
992
        search_for_quantizers_twoloop,
1190
993
        codebook_trellis_rate,
1191
994
        quantize_and_encode_band,
 
995
        ff_aac_encode_tns_info,
 
996
        ff_aac_encode_main_pred,
 
997
        ff_aac_adjust_common_prediction,
 
998
        ff_aac_apply_main_pred,
 
999
        ff_aac_apply_tns,
 
1000
        set_special_band_scalefactors,
 
1001
        search_for_pns,
 
1002
        ff_aac_search_for_tns,
1192
1003
        search_for_ms,
 
1004
        ff_aac_search_for_is,
 
1005
        ff_aac_search_for_pred,
1193
1006
    },
1194
1007
    [AAC_CODER_FAST] = {
1195
1008
        search_for_quantizers_fast,
1196
1009
        encode_window_bands_info,
1197
1010
        quantize_and_encode_band,
 
1011
        ff_aac_encode_tns_info,
 
1012
        ff_aac_encode_main_pred,
 
1013
        ff_aac_adjust_common_prediction,
 
1014
        ff_aac_apply_main_pred,
 
1015
        ff_aac_apply_tns,
 
1016
        set_special_band_scalefactors,
 
1017
        search_for_pns,
 
1018
        ff_aac_search_for_tns,
1198
1019
        search_for_ms,
 
1020
        ff_aac_search_for_is,
 
1021
        ff_aac_search_for_pred,
1199
1022
    },
1200
1023
};