]> git.sesse.net Git - ffmpeg/blob - libavcodec/aacenc_quantization.h
aacenc: reorder coding tools
[ffmpeg] / libavcodec / aacenc_quantization.h
1 /*
2  * AAC encoder intensity stereo
3  * Copyright (C) 2015 Rostislav Pehlivanov
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 quantizer
25  * @author Rostislav Pehlivanov ( atomnuker gmail com )
26  */
27
28 #ifndef AVCODEC_AACENC_QUANTIZATION_H
29 #define AVCODEC_AACENC_QUANTIZATION_H
30
31 #include "aactab.h"
32 #include "aacenc.h"
33 #include "aacenctab.h"
34 #include "aacenc_utils.h"
35
36 /**
37  * Calculate rate distortion cost for quantizing with given codebook
38  *
39  * @return quantization distortion
40  */
41 static av_always_inline float quantize_and_encode_band_cost_template(
42                                 struct AACEncContext *s,
43                                 PutBitContext *pb, const float *in, float *out,
44                                 const float *scaled, int size, int scale_idx,
45                                 int cb, const float lambda, const float uplim,
46                                 int *bits, int BT_ZERO, int BT_UNSIGNED,
47                                 int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO,
48                                 const float ROUNDING)
49 {
50     const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
51     const float Q   = ff_aac_pow2sf_tab [q_idx];
52     const float Q34 = ff_aac_pow34sf_tab[q_idx];
53     const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
54     const float CLIPPED_ESCAPE = 165140.0f*IQ;
55     int i, j;
56     float cost = 0;
57     const int dim = BT_PAIR ? 2 : 4;
58     int resbits = 0;
59     int off;
60
61     if (BT_ZERO || BT_NOISE || BT_STEREO) {
62         for (i = 0; i < size; i++)
63             cost += in[i]*in[i];
64         if (bits)
65             *bits = 0;
66         if (out) {
67             for (i = 0; i < size; i += dim)
68                 for (j = 0; j < dim; j++)
69                     out[i+j] = 0.0f;
70         }
71         return cost * lambda;
72     }
73     if (!scaled) {
74         abs_pow34_v(s->scoefs, in, size);
75         scaled = s->scoefs;
76     }
77     quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, aac_cb_maxval[cb], ROUNDING);
78     if (BT_UNSIGNED) {
79         off = 0;
80     } else {
81         off = aac_cb_maxval[cb];
82     }
83     for (i = 0; i < size; i += dim) {
84         const float *vec;
85         int *quants = s->qcoefs + i;
86         int curidx = 0;
87         int curbits;
88         float quantized, rd = 0.0f;
89         for (j = 0; j < dim; j++) {
90             curidx *= aac_cb_range[cb];
91             curidx += quants[j] + off;
92         }
93         curbits =  ff_aac_spectral_bits[cb-1][curidx];
94         vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
95         if (BT_UNSIGNED) {
96             for (j = 0; j < dim; j++) {
97                 float t = fabsf(in[i+j]);
98                 float di;
99                 if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
100                     if (t >= CLIPPED_ESCAPE) {
101                         quantized = CLIPPED_ESCAPE;
102                         curbits += 21;
103                     } else {
104                         int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
105                         quantized = c*cbrtf(c)*IQ;
106                         curbits += av_log2(c)*2 - 4 + 1;
107                     }
108                 } else {
109                     quantized = vec[j]*IQ;
110                 }
111                 di = t - quantized;
112                 if (out)
113                     out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
114                 if (vec[j] != 0.0f)
115                     curbits++;
116                 rd += di*di;
117             }
118         } else {
119             for (j = 0; j < dim; j++) {
120                 quantized = vec[j]*IQ;
121                 if (out)
122                     out[i+j] = quantized;
123                 rd += (in[i+j] - quantized)*(in[i+j] - quantized);
124             }
125         }
126         cost    += rd * lambda + curbits;
127         resbits += curbits;
128         if (cost >= uplim)
129             return uplim;
130         if (pb) {
131             put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
132             if (BT_UNSIGNED)
133                 for (j = 0; j < dim; j++)
134                     if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
135                         put_bits(pb, 1, in[i+j] < 0.0f);
136             if (BT_ESC) {
137                 for (j = 0; j < 2; j++) {
138                     if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
139                         int coef = av_clip_uintp2(quant(fabsf(in[i+j]), Q, ROUNDING), 13);
140                         int len = av_log2(coef);
141
142                         put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
143                         put_sbits(pb, len, coef);
144                     }
145                 }
146             }
147         }
148     }
149
150     if (bits)
151         *bits = resbits;
152     return cost;
153 }
154
155 static inline float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb,
156                                                 const float *in, float *quant, const float *scaled,
157                                                 int size, int scale_idx, int cb,
158                                                 const float lambda, const float uplim,
159                                                 int *bits) {
160     av_assert0(0);
161     return 0.0f;
162 }
163
164 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
165 static float quantize_and_encode_band_cost_ ## NAME(                                         \
166                                 struct AACEncContext *s,                                     \
167                                 PutBitContext *pb, const float *in, float *quant,            \
168                                 const float *scaled, int size, int scale_idx,                \
169                                 int cb, const float lambda, const float uplim,               \
170                                 int *bits) {                                                 \
171     return quantize_and_encode_band_cost_template(                                           \
172                                 s, pb, in, quant, scaled, size, scale_idx,                   \
173                                 BT_ESC ? ESC_BT : cb, lambda, uplim, bits,                   \
174                                 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO,  \
175                                 ROUNDING);                                                   \
176 }
177
178 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO,  1, 0, 0, 0, 0, 0, ROUND_STANDARD)
179 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0, 0, 0, ROUND_STANDARD)
180 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0, 0, 0, ROUND_STANDARD)
181 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0, 0, 0, ROUND_STANDARD)
182 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0, 0, 0, ROUND_STANDARD)
183 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC,   0, 1, 1, 1, 0, 0, ROUND_STANDARD)
184 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC_RTZ, 0, 1, 1, 1, 0, 0, ROUND_TO_ZERO)
185 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NOISE, 0, 0, 0, 0, 1, 0, ROUND_STANDARD)
186 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(STEREO,0, 0, 0, 0, 0, 1, ROUND_STANDARD)
187
188 static float (*const quantize_and_encode_band_cost_arr[])(
189                                 struct AACEncContext *s,
190                                 PutBitContext *pb, const float *in, float *quant,
191                                 const float *scaled, int size, int scale_idx,
192                                 int cb, const float lambda, const float uplim,
193                                 int *bits) = {
194     quantize_and_encode_band_cost_ZERO,
195     quantize_and_encode_band_cost_SQUAD,
196     quantize_and_encode_band_cost_SQUAD,
197     quantize_and_encode_band_cost_UQUAD,
198     quantize_and_encode_band_cost_UQUAD,
199     quantize_and_encode_band_cost_SPAIR,
200     quantize_and_encode_band_cost_SPAIR,
201     quantize_and_encode_band_cost_UPAIR,
202     quantize_and_encode_band_cost_UPAIR,
203     quantize_and_encode_band_cost_UPAIR,
204     quantize_and_encode_band_cost_UPAIR,
205     quantize_and_encode_band_cost_ESC,
206     quantize_and_encode_band_cost_NONE,     /* CB 12 doesn't exist */
207     quantize_and_encode_band_cost_NOISE,
208     quantize_and_encode_band_cost_STEREO,
209     quantize_and_encode_band_cost_STEREO,
210 };
211
212 static float (*const quantize_and_encode_band_cost_rtz_arr[])(
213                                 struct AACEncContext *s,
214                                 PutBitContext *pb, const float *in, float *quant,
215                                 const float *scaled, int size, int scale_idx,
216                                 int cb, const float lambda, const float uplim,
217                                 int *bits) = {
218     quantize_and_encode_band_cost_ZERO,
219     quantize_and_encode_band_cost_SQUAD,
220     quantize_and_encode_band_cost_SQUAD,
221     quantize_and_encode_band_cost_UQUAD,
222     quantize_and_encode_band_cost_UQUAD,
223     quantize_and_encode_band_cost_SPAIR,
224     quantize_and_encode_band_cost_SPAIR,
225     quantize_and_encode_band_cost_UPAIR,
226     quantize_and_encode_band_cost_UPAIR,
227     quantize_and_encode_band_cost_UPAIR,
228     quantize_and_encode_band_cost_UPAIR,
229     quantize_and_encode_band_cost_ESC_RTZ,
230     quantize_and_encode_band_cost_NONE,     /* CB 12 doesn't exist */
231     quantize_and_encode_band_cost_NOISE,
232     quantize_and_encode_band_cost_STEREO,
233     quantize_and_encode_band_cost_STEREO,
234 };
235
236 #define quantize_and_encode_band_cost(                                  \
237                                 s, pb, in, quant, scaled, size, scale_idx, cb, \
238                                 lambda, uplim, bits, rtz)               \
239     ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \
240                                 s, pb, in, quant, scaled, size, scale_idx, cb, \
241                                 lambda, uplim, bits)
242
243 static inline float quantize_band_cost(struct AACEncContext *s, const float *in,
244                                 const float *scaled, int size, int scale_idx,
245                                 int cb, const float lambda, const float uplim,
246                                 int *bits, int rtz)
247 {
248     return quantize_and_encode_band_cost(s, NULL, in, NULL, scaled, size, scale_idx,
249                                          cb, lambda, uplim, bits, rtz);
250 }
251
252 static inline void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
253                                             const float *in, float *out, int size, int scale_idx,
254                                             int cb, const float lambda, int rtz)
255 {
256     quantize_and_encode_band_cost(s, pb, in, out, NULL, size, scale_idx, cb, lambda,
257                                   INFINITY, NULL, rtz);
258 }
259
260 #endif /* AVCODEC_AACENC_QUANTIZATION_H */