2 * AAC encoder twoloop coder
3 * Copyright (C) 2008-2009 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
24 * AAC encoder twoloop coder
25 * @author Konstantin Shishkov
29 * This file contains a template for the twoloop coder function.
30 * It needs to be provided, externally, as an already included declaration,
31 * the following functions from aacenc_quantization/util.h. They're not included
32 * explicitly here to make it possible to provide alternative implementations:
33 * - quantize_band_cost
39 #ifndef AVCODEC_AACCODER_TWOLOOP_H
40 #define AVCODEC_AACCODER_TWOLOOP_H
43 #include "libavutil/mathematics.h"
49 #include "aacenctab.h"
50 #include "aac_tablegen_decl.h"
54 * two-loop quantizers search taken from ISO 13818-7 Appendix C
56 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
58 SingleChannelElement *sce,
61 int start = 0, i, w, w2, g;
62 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels * (lambda / 120.f);
63 float dists[128] = { 0 }, uplims[128] = { 0 };
68 float minthr = INFINITY;
70 // for values above this the decoder might end up in an endless loop
71 // due to always having more bits than what can be encoded.
72 destbits = FFMIN(destbits, 5800);
73 //XXX: some heuristic to determine initial quantizers will reduce search time
74 //determine zero bands and upper limits
75 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
76 for (g = 0; g < sce->ics.num_swb; g++) {
78 float uplim = 0.0f, energy = 0.0f;
79 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
80 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
81 uplim += band->threshold;
82 energy += band->energy;
83 if (band->energy <= band->threshold || band->threshold == 0.0f) {
84 sce->zeroes[(w+w2)*16+g] = 1;
89 uplims[w*16+g] = uplim *512;
90 sce->zeroes[w*16+g] = !nz;
92 minthr = FFMIN(minthr, uplim);
96 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
97 for (g = 0; g < sce->ics.num_swb; g++) {
98 if (sce->zeroes[w*16+g]) {
99 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
102 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
108 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
110 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
112 for (g = 0; g < sce->ics.num_swb; g++) {
113 const float *scaled = s->scoefs + start;
114 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
115 start += sce->ics.swb_sizes[g];
119 //perform two-loop search
120 //outer loop - improve quality
123 minscaler = sce->sf_idx[0];
124 //inner loop - quantize spectrum to fit into given number of bits
125 qstep = its ? 1 : 32;
129 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
131 for (g = 0; g < sce->ics.num_swb; g++) {
132 const float *coefs = &sce->coeffs[start];
133 const float *scaled = &s->scoefs[start];
138 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
139 start += sce->ics.swb_sizes[g];
142 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
143 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
144 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
146 dist += quantize_band_cost(s, coefs + w2*128,
148 sce->ics.swb_sizes[g],
157 dists[w*16+g] = dist - bits;
159 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
162 start += sce->ics.swb_sizes[g];
163 prev = sce->sf_idx[w*16+g];
166 if (tbits > destbits) {
167 for (i = 0; i < 128; i++)
168 if (sce->sf_idx[i] < 218 - qstep)
169 sce->sf_idx[i] += qstep;
171 for (i = 0; i < 128; i++)
172 if (sce->sf_idx[i] > 60 - qstep)
173 sce->sf_idx[i] -= qstep;
176 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
181 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
183 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
184 for (g = 0; g < sce->ics.num_swb; g++) {
185 int prevsc = sce->sf_idx[w*16+g];
186 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
187 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
188 sce->sf_idx[w*16+g]--;
189 else //Try to make sure there is some energy in every band
190 sce->sf_idx[w*16+g]-=2;
192 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
193 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
194 if (sce->sf_idx[w*16+g] != prevsc)
196 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
200 } while (fflag && its < 10);
203 #endif /* AVCODEC_AACCODER_TWOLOOP_H */