AAC encoder: refactor to resynchronize MIPS port
[ffmpeg.git] / libavcodec / aaccoder_twoloop.h
1 /*
2  * AAC encoder twoloop coder
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
24  * AAC encoder twoloop coder
25  * @author Konstantin Shishkov
26  */
27
28 /**
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
34  *  - abs_pow34_v
35  *  - find_max_val
36  *  - find_min_book
37  */
38
39 #ifndef AVCODEC_AACCODER_TWOLOOP_H
40 #define AVCODEC_AACCODER_TWOLOOP_H
41
42 #include <float.h>
43 #include "libavutil/mathematics.h"
44 #include "avcodec.h"
45 #include "put_bits.h"
46 #include "aac.h"
47 #include "aacenc.h"
48 #include "aactab.h"
49 #include "aacenctab.h"
50 #include "aac_tablegen_decl.h"
51
52
53 /**
54  * two-loop quantizers search taken from ISO 13818-7 Appendix C
55  */
56 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
57                                           AACEncContext *s,
58                                           SingleChannelElement *sce,
59                                           const float lambda)
60 {
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 };
64     float maxvals[128];
65     int fflag, minscaler;
66     int its  = 0;
67     int allz = 0;
68     float minthr = INFINITY;
69
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++) {
77             int nz = 0;
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;
85                     continue;
86                 }
87                 nz = 1;
88             }
89             uplims[w*16+g] = uplim *512;
90             sce->zeroes[w*16+g] = !nz;
91             if (nz)
92                 minthr = FFMIN(minthr, uplim);
93             allz |= nz;
94         }
95     }
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;
100                 continue;
101             }
102             sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
103         }
104     }
105
106     if (!allz)
107         return;
108     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
109
110     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
111         start = w*128;
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];
116         }
117     }
118
119     //perform two-loop search
120     //outer loop - improve quality
121     do {
122         int tbits, qstep;
123         minscaler = sce->sf_idx[0];
124         //inner loop - quantize spectrum to fit into given number of bits
125         qstep = its ? 1 : 32;
126         do {
127             int prev = -1;
128             tbits = 0;
129             for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
130                 start = w*128;
131                 for (g = 0;  g < sce->ics.num_swb; g++) {
132                     const float *coefs = &sce->coeffs[start];
133                     const float *scaled = &s->scoefs[start];
134                     int bits = 0;
135                     int cb;
136                     float dist = 0.0f;
137
138                     if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
139                         start += sce->ics.swb_sizes[g];
140                         continue;
141                     }
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++) {
145                         int b;
146                         dist += quantize_band_cost(s, coefs + w2*128,
147                                                    scaled + w2*128,
148                                                    sce->ics.swb_sizes[g],
149                                                    sce->sf_idx[w*16+g],
150                                                    cb,
151                                                    1.0f,
152                                                    INFINITY,
153                                                    &b,
154                                                    0);
155                         bits += b;
156                     }
157                     dists[w*16+g] = dist - bits;
158                     if (prev != -1) {
159                         bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
160                     }
161                     tbits += bits;
162                     start += sce->ics.swb_sizes[g];
163                     prev = sce->sf_idx[w*16+g];
164                 }
165             }
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;
170             } else {
171                 for (i = 0; i < 128; i++)
172                     if (sce->sf_idx[i] > 60 - qstep)
173                         sce->sf_idx[i] -= qstep;
174             }
175             qstep >>= 1;
176             if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
177                 qstep = 1;
178         } while (qstep);
179
180         fflag = 0;
181         minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
182
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;
191                 }
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)
195                     fflag = 1;
196                 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
197             }
198         }
199         its++;
200     } while (fflag && its < 10);
201 }
202
203 #endif /* AVCODEC_AACCODER_TWOLOOP_H */