annotate aaccoder.c @ 9936:7f42ae22c351 libavcodec

Cosmetics: Pretty print the AAC encoder.
author alexc
date Wed, 08 Jul 2009 20:36:45 +0000
parents d09283aeeef8
children 3e39dbd2d9eb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1 /*
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
2 * AAC coefficients encoder
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
3 * Copyright (C) 2008-2009 Konstantin Shishkov
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
4 *
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
5 * This file is part of FFmpeg.
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
6 *
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
11 *
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
15 * Lesser General Public License for more details.
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
16 *
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
20 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
21
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
22 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
23 * @file libavcodec/aaccoder.c
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
24 * AAC coefficients encoder
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
25 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
26
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
27 /***********************************
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
28 * TODOs:
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
29 * speedup quantizer selection
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
30 * add sane pulse detection
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
31 ***********************************/
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
32
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
33 #include "avcodec.h"
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
34 #include "put_bits.h"
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
35 #include "aac.h"
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
36 #include "aacenc.h"
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
37 #include "aactab.h"
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
38
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
39 /** bits needed to code codebook run value for long windows */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
40 static const uint8_t run_value_bits_long[64] = {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
41 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
42 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
43 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
44 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
45 };
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
46
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
47 /** bits needed to code codebook run value for short windows */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
48 static const uint8_t run_value_bits_short[16] = {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
49 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
50 };
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
51
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
52 static const uint8_t* run_value_bits[2] = {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
53 run_value_bits_long, run_value_bits_short
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
54 };
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
55
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
56
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
57 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
58 * Quantize one coefficient.
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
59 * @return absolute value of the quantized coefficient
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
60 * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
61 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
62 static av_always_inline int quant(float coef, const float Q)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
63 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
64 return pow(coef * Q, 0.75) + 0.4054;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
65 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
66
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
67 static void quantize_bands(int (*out)[2], const float *in, const float *scaled, int size, float Q34, int is_signed, int maxval)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
68 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
69 int i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
70 double qc;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
71 for (i = 0; i < size; i++) {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
72 qc = scaled[i] * Q34;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
73 out[i][0] = (int)FFMIN((int)qc, maxval);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
74 out[i][1] = (int)FFMIN((int)(qc + 0.4054), maxval);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
75 if (is_signed && in[i] < 0.0f) {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
76 out[i][0] = -out[i][0];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
77 out[i][1] = -out[i][1];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
78 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
79 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
80 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
81
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
82 static void abs_pow34_v(float *out, const float* in, const int size)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
83 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
84 #ifndef USE_REALLY_FULL_SEARCH
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
85 int i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
86 for (i = 0; i < size; i++) {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
87 out[i] = pow(fabsf(in[i]), 0.75);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
88 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
89 #endif /* USE_REALLY_FULL_SEARCH */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
90 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
91
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
92 static av_always_inline int quant2(float coef, const float Q)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
93 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
94 return pow(coef * Q, 0.75);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
95 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
96
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
97 static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
98 static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
99
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
100 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
101 * Calculate rate distortion cost for quantizing with given codebook
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
102 *
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
103 * @return quantization distortion
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
104 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
105 static float quantize_band_cost(struct AACEncContext *s, const float *in, const float *scaled, int size, int scale_idx, int cb,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
106 const float lambda, const float uplim, int *bits)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
107 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
108 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
109 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
110 const float CLIPPED_ESCAPE = 165140.0f*IQ;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
111 int i, j, k;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
112 float cost = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
113 const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
114 int resbits = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
115 #ifndef USE_REALLY_FULL_SEARCH
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
116 const float Q34 = pow(Q, 0.75);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
117 const int range = aac_cb_range[cb];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
118 const int maxval = aac_cb_maxval[cb];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
119 int offs[4];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
120 #endif /* USE_REALLY_FULL_SEARCH */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
121
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
122 if (!cb) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
123 for (i = 0; i < size; i++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
124 cost += in[i]*in[i]*lambda;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
125 return cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
126 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
127 #ifndef USE_REALLY_FULL_SEARCH
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
128 offs[0] = 1;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
129 for (i = 1; i < dim; i++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
130 offs[i] = offs[i-1]*range;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
131 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
132 #endif /* USE_REALLY_FULL_SEARCH */
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
133 for (i = 0; i < size; i += dim) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
134 float mincost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
135 int minidx = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
136 int minbits = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
137 const float *vec;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
138 #ifndef USE_REALLY_FULL_SEARCH
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
139 int (*quants)[2] = &s->qcoefs[i];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
140 mincost = 0.0f;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
141 for (j = 0; j < dim; j++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
142 mincost += in[i+j]*in[i+j]*lambda;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
143 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
144 minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
145 minbits = ff_aac_spectral_bits[cb-1][minidx];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
146 mincost += minbits;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
147 for (j = 0; j < (1<<dim); j++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
148 float rd = 0.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
149 int curbits;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
150 int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
151 int same = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
152 for (k = 0; k < dim; k++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
153 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
154 same = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
155 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
156 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
157 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
158 if (same)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
159 continue;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
160 for (k = 0; k < dim; k++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
161 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
162 curbits = ff_aac_spectral_bits[cb-1][curidx];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
163 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
164 #else
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
165 mincost = INFINITY;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
166 vec = ff_aac_codebook_vectors[cb-1];
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
167 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
168 float rd = 0.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
169 int curbits = ff_aac_spectral_bits[cb-1][j];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
170 #endif /* USE_REALLY_FULL_SEARCH */
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
171 if (IS_CODEBOOK_UNSIGNED(cb)) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
172 for (k = 0; k < dim; k++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
173 float t = fabsf(in[i+k]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
174 float di;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
175 //do not code with escape sequence small values
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
176 if (vec[k] == 64.0f && t < 39.0f*IQ) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
177 rd = INFINITY;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
178 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
179 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
180 if (vec[k] == 64.0f) {//FIXME: slow
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
181 if (t >= CLIPPED_ESCAPE) {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
182 di = t - CLIPPED_ESCAPE;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
183 curbits += 21;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
184 } else {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
185 int c = av_clip(quant(t, Q), 0, 8191);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
186 di = t - c*cbrt(c)*IQ;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
187 curbits += av_log2(c)*2 - 4 + 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
188 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
189 } else {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
190 di = t - vec[k]*IQ;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
191 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
192 if (vec[k] != 0.0f)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
193 curbits++;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
194 rd += di*di*lambda;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
195 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
196 } else {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
197 for (k = 0; k < dim; k++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
198 float di = in[i+k] - vec[k]*IQ;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
199 rd += di*di*lambda;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
200 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
201 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
202 rd += curbits;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
203 if (rd < mincost) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
204 mincost = rd;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
205 minidx = j;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
206 minbits = curbits;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
207 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
208 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
209 cost += mincost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
210 resbits += minbits;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
211 if (cost >= uplim)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
212 return uplim;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
213 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
214
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
215 if (bits)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
216 *bits = resbits;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
217 return cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
218 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
219
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
220 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, const float *in, int size,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
221 int scale_idx, int cb, const float lambda)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
222 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
223 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
224 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
225 const float CLIPPED_ESCAPE = 165140.0f*IQ;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
226 const int dim = (cb < FIRST_PAIR_BT) ? 4 : 2;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
227 int i, j, k;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
228 #ifndef USE_REALLY_FULL_SEARCH
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
229 const float Q34 = pow(Q, 0.75);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
230 const int range = aac_cb_range[cb];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
231 const int maxval = aac_cb_maxval[cb];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
232 int offs[4];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
233 float *scaled = s->scoefs;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
234 #endif /* USE_REALLY_FULL_SEARCH */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
235
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
236 //START_TIMER
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
237 if (!cb)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
238 return;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
239
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
240 #ifndef USE_REALLY_FULL_SEARCH
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
241 offs[0] = 1;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
242 for (i = 1; i < dim; i++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
243 offs[i] = offs[i-1]*range;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
244 abs_pow34_v(scaled, in, size);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
245 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
246 #endif /* USE_REALLY_FULL_SEARCH */
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
247 for (i = 0; i < size; i += dim) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
248 float mincost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
249 int minidx = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
250 int minbits = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
251 const float *vec;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
252 #ifndef USE_REALLY_FULL_SEARCH
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
253 int (*quants)[2] = &s->qcoefs[i];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
254 mincost = 0.0f;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
255 for (j = 0; j < dim; j++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
256 mincost += in[i+j]*in[i+j]*lambda;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
257 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
258 minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
259 minbits = ff_aac_spectral_bits[cb-1][minidx];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
260 mincost += minbits;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
261 for (j = 0; j < (1<<dim); j++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
262 float rd = 0.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
263 int curbits;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
264 int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
265 int same = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
266 for (k = 0; k < dim; k++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
267 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
268 same = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
269 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
270 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
271 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
272 if (same)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
273 continue;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
274 for (k = 0; k < dim; k++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
275 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
276 curbits = ff_aac_spectral_bits[cb-1][curidx];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
277 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
278 #else
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
279 vec = ff_aac_codebook_vectors[cb-1];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
280 mincost = INFINITY;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
281 for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
282 float rd = 0.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
283 int curbits = ff_aac_spectral_bits[cb-1][j];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
284 int curidx = j;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
285 #endif /* USE_REALLY_FULL_SEARCH */
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
286 if (IS_CODEBOOK_UNSIGNED(cb)) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
287 for (k = 0; k < dim; k++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
288 float t = fabsf(in[i+k]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
289 float di;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
290 //do not code with escape sequence small values
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
291 if (vec[k] == 64.0f && t < 39.0f*IQ) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
292 rd = INFINITY;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
293 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
294 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
295 if (vec[k] == 64.0f) {//FIXME: slow
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
296 if (t >= CLIPPED_ESCAPE) {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
297 di = t - CLIPPED_ESCAPE;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
298 curbits += 21;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
299 } else {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
300 int c = av_clip(quant(t, Q), 0, 8191);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
301 di = t - c*cbrt(c)*IQ;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
302 curbits += av_log2(c)*2 - 4 + 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
303 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
304 } else {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
305 di = t - vec[k]*IQ;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
306 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
307 if (vec[k] != 0.0f)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
308 curbits++;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
309 rd += di*di*lambda;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
310 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
311 } else {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
312 for (k = 0; k < dim; k++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
313 float di = in[i+k] - vec[k]*IQ;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
314 rd += di*di*lambda;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
315 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
316 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
317 rd += curbits;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
318 if (rd < mincost) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
319 mincost = rd;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
320 minidx = curidx;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
321 minbits = curbits;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
322 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
323 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
324 put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
325 if (IS_CODEBOOK_UNSIGNED(cb))
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
326 for (j = 0; j < dim; j++)
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
327 if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
328 put_bits(pb, 1, in[i+j] < 0.0f);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
329 if (cb == ESC_BT) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
330 for (j = 0; j < 2; j++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
331 if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
332 int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
333 int len = av_log2(coef);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
334
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
335 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
336 put_bits(pb, len, coef & ((1 << len) - 1));
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
337 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
338 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
339 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
340 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
341 //STOP_TIMER("quantize_and_encode")
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
342 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
343
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
344 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
345 * structure used in optimal codebook search
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
346 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
347 typedef struct BandCodingPath {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
348 int prev_idx; ///< pointer to the previous path point
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
349 int codebook; ///< codebook for coding band run
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
350 float cost; ///< path cost
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
351 int run;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
352 } BandCodingPath;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
353
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
354 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
355 * Encode band info for single window group bands.
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
356 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
357 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
358 int win, int group_len, const float lambda)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
359 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
360 BandCodingPath path[120][12];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
361 int w, swb, cb, start, start2, size;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
362 int i, j;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
363 const int max_sfb = sce->ics.max_sfb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
364 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
365 const int run_esc = (1 << run_bits) - 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
366 int idx, ppos, count;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
367 int stackrun[120], stackcb[120], stack_len;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
368 float next_minrd = INFINITY;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
369 int next_mincb = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
370
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
371 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
372 start = win*128;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
373 for (cb = 0; cb < 12; cb++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
374 path[0][cb].cost = 0.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
375 path[0][cb].prev_idx = -1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
376 path[0][cb].run = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
377 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
378 for (swb = 0; swb < max_sfb; swb++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
379 start2 = start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
380 size = sce->ics.swb_sizes[swb];
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
381 if (sce->zeroes[win*16 + swb]) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
382 for (cb = 0; cb < 12; cb++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
383 path[swb+1][cb].prev_idx = cb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
384 path[swb+1][cb].cost = path[swb][cb].cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
385 path[swb+1][cb].run = path[swb][cb].run + 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
386 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
387 } else {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
388 float minrd = next_minrd;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
389 int mincb = next_mincb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
390 next_minrd = INFINITY;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
391 next_mincb = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
392 for (cb = 0; cb < 12; cb++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
393 float cost_stay_here, cost_get_here;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
394 float rd = 0.0f;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
395 for (w = 0; w < group_len; w++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
396 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
397 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
398 s->scoefs + start + w*128, size,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
399 sce->sf_idx[(win+w)*16+swb], cb,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
400 lambda / band->threshold, INFINITY, NULL);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
401 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
402 cost_stay_here = path[swb][cb].cost + rd;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
403 cost_get_here = minrd + rd + run_bits + 4;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
404 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
405 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
406 cost_stay_here += run_bits;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
407 if (cost_get_here < cost_stay_here) {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
408 path[swb+1][cb].prev_idx = mincb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
409 path[swb+1][cb].cost = cost_get_here;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
410 path[swb+1][cb].run = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
411 } else {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
412 path[swb+1][cb].prev_idx = cb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
413 path[swb+1][cb].cost = cost_stay_here;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
414 path[swb+1][cb].run = path[swb][cb].run + 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
415 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
416 if (path[swb+1][cb].cost < next_minrd) {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
417 next_minrd = path[swb+1][cb].cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
418 next_mincb = cb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
419 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
420 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
421 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
422 start += sce->ics.swb_sizes[swb];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
423 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
424
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
425 //convert resulting path from backward-linked list
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
426 stack_len = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
427 idx = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
428 for (cb = 1; cb < 12; cb++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
429 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
430 idx = cb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
431 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
432 ppos = max_sfb;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
433 while(ppos > 0) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
434 cb = idx;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
435 stackrun[stack_len] = path[ppos][cb].run;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
436 stackcb [stack_len] = cb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
437 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
438 ppos -= path[ppos][cb].run;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
439 stack_len++;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
440 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
441 //perform actual band info encoding
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
442 start = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
443 for (i = stack_len - 1; i >= 0; i--) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
444 put_bits(&s->pb, 4, stackcb[i]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
445 count = stackrun[i];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
446 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
447 //XXX: memset when band_type is also uint8_t
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
448 for (j = 0; j < count; j++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
449 sce->band_type[win*16 + start] = stackcb[i];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
450 start++;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
451 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
452 while(count >= run_esc) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
453 put_bits(&s->pb, run_bits, run_esc);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
454 count -= run_esc;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
455 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
456 put_bits(&s->pb, run_bits, count);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
457 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
458 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
459
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
460 static void encode_window_bands_info_fixed(AACEncContext *s, SingleChannelElement *sce,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
461 int win, int group_len, const float lambda)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
462 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
463 encode_window_bands_info(s, sce, win, group_len, 1.0f);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
464 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
465
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
466
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
467 typedef struct TrellisPath {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
468 float cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
469 int prev;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
470 int min_val;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
471 int max_val;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
472 } TrellisPath;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
473
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
474 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
475 SingleChannelElement *sce, const float lambda)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
476 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
477 int q, w, w2, g, start = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
478 int i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
479 int idx;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
480 TrellisPath paths[256*121];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
481 int bandaddr[121];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
482 int minq;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
483 float mincost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
484
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
485 for (i = 0; i < 256; i++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
486 paths[i].cost = 0.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
487 paths[i].prev = -1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
488 paths[i].min_val = i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
489 paths[i].max_val = i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
490 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
491 for (i = 256; i < 256*121; i++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
492 paths[i].cost = INFINITY;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
493 paths[i].prev = -2;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
494 paths[i].min_val = INT_MAX;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
495 paths[i].max_val = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
496 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
497 idx = 256;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
498 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
499 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
500 start = w*128;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
501 for (g = 0; g < sce->ics.num_swb; g++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
502 const float *coefs = sce->coeffs + start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
503 float qmin, qmax;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
504 int nz = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
505
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
506 bandaddr[idx >> 8] = w*16+g;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
507 qmin = INT_MAX;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
508 qmax = 0.0f;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
509 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
510 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
511 if (band->energy <= band->threshold || band->threshold == 0.0f) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
512 sce->zeroes[(w+w2)*16+g] = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
513 continue;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
514 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
515 sce->zeroes[(w+w2)*16+g] = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
516 nz = 1;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
517 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
518 float t = fabsf(coefs[w2*128+i]);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
519 if (t > 0.0f) qmin = fminf(qmin, t);
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
520 qmax = fmaxf(qmax, t);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
521 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
522 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
523 if (nz) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
524 int minscale, maxscale;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
525 float minrd = INFINITY;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
526 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
527 minscale = av_clip_uint8(log2(qmin)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
528 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
529 maxscale = av_clip_uint8(log2(qmax)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
530 for (q = minscale; q < maxscale; q++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
531 float dists[12], dist;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
532 memset(dists, 0, sizeof(dists));
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
533 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
534 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
535 int cb;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
536 for (cb = 0; cb <= ESC_BT; cb++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
537 dists[cb] += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
538 q, cb, lambda / band->threshold, INFINITY, NULL);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
539 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
540 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
541 dist = dists[0];
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
542 for (i = 1; i <= ESC_BT; i++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
543 dist = fminf(dist, dists[i]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
544 minrd = fminf(minrd, dist);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
545
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
546 for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, 256); i++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
547 float cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
548 int minv, maxv;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
549 if (isinf(paths[idx - 256 + i].cost))
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
550 continue;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
551 cost = paths[idx - 256 + i].cost + dist
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
552 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
553 minv = FFMIN(paths[idx - 256 + i].min_val, q);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
554 maxv = FFMAX(paths[idx - 256 + i].max_val, q);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
555 if (cost < paths[idx + q].cost && maxv-minv < SCALE_MAX_DIFF) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
556 paths[idx + q].cost = cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
557 paths[idx + q].prev = idx - 256 + i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
558 paths[idx + q].min_val = minv;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
559 paths[idx + q].max_val = maxv;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
560 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
561 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
562 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
563 } else {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
564 for (q = 0; q < 256; q++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
565 if (!isinf(paths[idx - 256 + q].cost)) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
566 paths[idx + q].cost = paths[idx - 256 + q].cost + 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
567 paths[idx + q].prev = idx - 256 + q;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
568 paths[idx + q].min_val = FFMIN(paths[idx - 256 + q].min_val, q);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
569 paths[idx + q].max_val = FFMAX(paths[idx - 256 + q].max_val, q);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
570 continue;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
571 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
572 for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, 256); i++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
573 float cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
574 int minv, maxv;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
575 if (isinf(paths[idx - 256 + i].cost))
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
576 continue;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
577 cost = paths[idx - 256 + i].cost + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
578 minv = FFMIN(paths[idx - 256 + i].min_val, q);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
579 maxv = FFMAX(paths[idx - 256 + i].max_val, q);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
580 if (cost < paths[idx + q].cost && maxv-minv < SCALE_MAX_DIFF) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
581 paths[idx + q].cost = cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
582 paths[idx + q].prev = idx - 256 + i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
583 paths[idx + q].min_val = minv;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
584 paths[idx + q].max_val = maxv;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
585 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
586 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
587 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
588 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
589 sce->zeroes[w*16+g] = !nz;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
590 start += sce->ics.swb_sizes[g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
591 idx += 256;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
592 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
593 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
594 idx -= 256;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
595 mincost = paths[idx].cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
596 minq = idx;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
597 for (i = 1; i < 256; i++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
598 if (paths[idx + i].cost < mincost) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
599 mincost = paths[idx + i].cost;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
600 minq = idx + i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
601 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
602 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
603 while(minq >= 256) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
604 sce->sf_idx[bandaddr[minq>>8]] = minq & 0xFF;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
605 minq = paths[minq].prev;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
606 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
607 //set the same quantizers inside window groups
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
608 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
609 for (g = 0; g < sce->ics.num_swb; g++)
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
610 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
611 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
612 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
613
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
614 /**
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
615 * two-loop quantizers search taken from ISO 13818-7 Appendix C
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
616 */
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
617 static void search_for_quantizers_twoloop(AVCodecContext *avctx, AACEncContext *s,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
618 SingleChannelElement *sce, const float lambda)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
619 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
620 int start = 0, i, w, w2, g;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
621 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
622 float dists[128], uplims[128];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
623 int fflag, minscaler;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
624 int its = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
625 int allz = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
626 float minthr = INFINITY;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
627
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
628 //XXX: some heuristic to determine initial quantizers will reduce search time
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
629 memset(dists, 0, sizeof(dists));
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
630 //determine zero bands and upper limits
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
631 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
632 for (g = 0; g < sce->ics.num_swb; g++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
633 int nz = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
634 float uplim = 0.0f;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
635 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
636 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
637 uplim += band->threshold;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
638 if (band->energy <= band->threshold || band->threshold == 0.0f) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
639 sce->zeroes[(w+w2)*16+g] = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
640 continue;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
641 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
642 nz = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
643 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
644 uplims[w*16+g] = uplim *512;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
645 sce->zeroes[w*16+g] = !nz;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
646 if (nz)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
647 minthr = fminf(minthr, uplim);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
648 allz = FFMAX(allz, nz);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
649 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
650 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
651 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
652 for (g = 0; g < sce->ics.num_swb; g++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
653 if (sce->zeroes[w*16+g]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
654 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
655 continue;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
656 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
657 sce->sf_idx[w*16+g] = SCALE_ONE_POS + fminf(log2(uplims[w*16+g]/minthr)*4,59);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
658 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
659 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
660
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
661 if (!allz)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
662 return;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
663 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
664 //perform two-loop search
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
665 //outer loop - improve quality
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
666 do{
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
667 int tbits, qstep;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
668 minscaler = sce->sf_idx[0];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
669 //inner loop - quantize spectrum to fit into given number of bits
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
670 qstep = its ? 1 : 32;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
671 do{
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
672 int prev = -1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
673 tbits = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
674 fflag = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
675 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
676 start = w*128;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
677 for (g = 0; g < sce->ics.num_swb; g++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
678 const float *coefs = sce->coeffs + start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
679 const float *scaled = s->scoefs + start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
680 int bits = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
681 int cb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
682 float mindist = INFINITY;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
683 int minbits = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
684
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
685 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
686 continue;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
687 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
688 for (cb = 0; cb <= ESC_BT; cb++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
689 float dist = 0.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
690 int bb = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
691 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
692 int b;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
693 dist += quantize_band_cost(s, coefs + w2*128,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
694 scaled + w2*128,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
695 sce->ics.swb_sizes[g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
696 sce->sf_idx[w*16+g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
697 ESC_BT,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
698 1.0,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
699 INFINITY,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
700 &b);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
701 bb += b;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
702 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
703 if (dist < mindist) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
704 mindist = dist;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
705 minbits = bb;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
706 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
707 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
708 dists[w*16+g] = mindist - minbits;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
709 bits = minbits;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
710 if (prev != -1) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
711 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
712 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
713 tbits += bits;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
714 start += sce->ics.swb_sizes[g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
715 prev = sce->sf_idx[w*16+g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
716 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
717 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
718 if (tbits > destbits) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
719 for (i = 0; i < 128; i++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
720 if (sce->sf_idx[i] < 218 - qstep) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
721 sce->sf_idx[i] += qstep;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
722 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
723 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
724 } else {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
725 for (i = 0; i < 128; i++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
726 if (sce->sf_idx[i] > 60 - qstep) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
727 sce->sf_idx[i] -= qstep;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
728 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
729 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
730 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
731 qstep >>= 1;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
732 if (!qstep && tbits > destbits*1.02)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
733 qstep = 1;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
734 if (sce->sf_idx[0] >= 217)break;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
735 }while(qstep);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
736
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
737 fflag = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
738 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
739 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
740 start = w*128;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
741 for (g = 0; g < sce->ics.num_swb; g++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
742 int prevsc = sce->sf_idx[w*16+g];
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
743 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
744 sce->sf_idx[w*16+g]--;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
745 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
746 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
747 if (sce->sf_idx[w*16+g] != prevsc)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
748 fflag = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
749 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
750 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
751 its++;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
752 }while(fflag && its < 10);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
753 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
754
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
755 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
756 SingleChannelElement *sce, const float lambda)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
757 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
758 int start = 0, i, w, w2, g;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
759 float uplim[128], maxq[128];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
760 int minq, maxsf;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
761 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
762 int last = 0, lastband = 0, curband = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
763 float avg_energy = 0.0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
764 if (sce->ics.num_windows == 1) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
765 start = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
766 for (i = 0; i < 1024; i++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
767 if (i - start >= sce->ics.swb_sizes[curband]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
768 start += sce->ics.swb_sizes[curband];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
769 curband++;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
770 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
771 if (sce->coeffs[i]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
772 avg_energy += sce->coeffs[i] * sce->coeffs[i];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
773 last = i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
774 lastband = curband;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
775 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
776 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
777 } else {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
778 for (w = 0; w < 8; w++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
779 const float *coeffs = sce->coeffs + w*128;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
780 start = 0;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
781 for (i = 0; i < 128; i++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
782 if (i - start >= sce->ics.swb_sizes[curband]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
783 start += sce->ics.swb_sizes[curband];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
784 curband++;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
785 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
786 if (coeffs[i]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
787 avg_energy += coeffs[i] * coeffs[i];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
788 last = FFMAX(last, i);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
789 lastband = FFMAX(lastband, curband);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
790 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
791 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
792 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
793 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
794 last++;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
795 avg_energy /= last;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
796 if (avg_energy == 0.0f) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
797 for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
798 sce->sf_idx[i] = SCALE_ONE_POS;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
799 return;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
800 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
801 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
802 start = w*128;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
803 for (g = 0; g < sce->ics.num_swb; g++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
804 float *coefs = sce->coeffs + start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
805 const int size = sce->ics.swb_sizes[g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
806 int start2 = start, end2 = start + size, peakpos = start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
807 float maxval = -1, thr = 0.0f, t;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
808 maxq[w*16+g] = 0.0f;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
809 if (g > lastband) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
810 maxq[w*16+g] = 0.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
811 start += size;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
812 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
813 memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
814 continue;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
815 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
816 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
817 for (i = 0; i < size; i++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
818 float t = coefs[w2*128+i]*coefs[w2*128+i];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
819 maxq[w*16+g] = fmaxf(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
820 thr += t;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
821 if (sce->ics.num_windows == 1 && maxval < t) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
822 maxval = t;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
823 peakpos = start+i;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
824 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
825 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
826 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
827 if (sce->ics.num_windows == 1) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
828 start2 = FFMAX(peakpos - 2, start2);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
829 end2 = FFMIN(peakpos + 3, end2);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
830 } else {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
831 start2 -= start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
832 end2 -= start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
833 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
834 start += size;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
835 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
836 t = 1.0 - (1.0 * start2 / last);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
837 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
838 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
839 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
840 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
841 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
842 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
843 start = w*128;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
844 for (g = 0; g < sce->ics.num_swb; g++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
845 const float *coefs = sce->coeffs + start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
846 const float *scaled = s->scoefs + start;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
847 const int size = sce->ics.swb_sizes[g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
848 int scf, prev_scf, step;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
849 int min_scf = 0, max_scf = 255;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
850 float curdiff;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
851 if (maxq[w*16+g] < 21.544) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
852 sce->zeroes[w*16+g] = 1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
853 start += size;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
854 continue;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
855 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
856 sce->zeroes[w*16+g] = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
857 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
858 step = 16;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
859 for (;;) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
860 float dist = 0.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
861 int quant_max;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
862
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
863 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
864 int b;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
865 dist += quantize_band_cost(s, coefs + w2*128,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
866 scaled + w2*128,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
867 sce->ics.swb_sizes[g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
868 scf,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
869 ESC_BT,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
870 1.0,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
871 INFINITY,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
872 &b);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
873 dist -= b;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
874 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
875 dist *= 1.0f/512.0f;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
876 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
877 if (quant_max >= 8191) { // too much, return to the previous quantizer
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
878 sce->sf_idx[w*16+g] = prev_scf;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
879 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
880 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
881 prev_scf = scf;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
882 curdiff = fabsf(dist - uplim[w*16+g]);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
883 if (curdiff == 0.0f)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
884 step = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
885 else
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
886 step = fabsf(log2(curdiff));
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
887 if (dist > uplim[w*16+g])
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
888 step = -step;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
889 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
890 sce->sf_idx[w*16+g] = scf;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
891 break;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
892 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
893 scf += step;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
894 if (step > 0)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
895 min_scf = scf;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
896 else
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
897 max_scf = scf;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
898 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
899 start += size;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
900 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
901 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
902 minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
903 for (i = 1; i < 128; i++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
904 if (!sce->sf_idx[i])
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
905 sce->sf_idx[i] = sce->sf_idx[i-1];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
906 else
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
907 minq = FFMIN(minq, sce->sf_idx[i]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
908 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
909 if (minq == INT_MAX) minq = 0;
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
910 minq = FFMIN(minq, SCALE_MAX_POS);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
911 maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
912 for (i = 126; i >= 0; i--) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
913 if (!sce->sf_idx[i])
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
914 sce->sf_idx[i] = sce->sf_idx[i+1];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
915 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
916 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
917 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
918
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
919 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
920 SingleChannelElement *sce, const float lambda)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
921 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
922 int start = 0, i, w, w2, g;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
923 int minq = 255;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
924
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
925 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
926 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
927 start = w*128;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
928 for (g = 0; g < sce->ics.num_swb; g++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
929 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
930 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
931 if (band->energy <= band->threshold) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
932 sce->sf_idx[(w+w2)*16+g] = 218;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
933 sce->zeroes[(w+w2)*16+g] = 1;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
934 } else {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
935 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2(band->threshold), 80, 218);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
936 sce->zeroes[(w+w2)*16+g] = 0;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
937 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
938 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
939 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
940 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
941 }
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
942 for (i = 0; i < 128; i++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
943 sce->sf_idx[i] = 140;//av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
944 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
945 //set the same quantizers inside window groups
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
946 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
947 for (g = 0; g < sce->ics.num_swb; g++)
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
948 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
949 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
950 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
951
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
952 static void search_for_ms(AACEncContext *s, ChannelElement *cpe, const float lambda)
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
953 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
954 int start = 0, i, w, w2, g;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
955 float M[128], S[128];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
956 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
957 SingleChannelElement *sce0 = &cpe->ch[0];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
958 SingleChannelElement *sce1 = &cpe->ch[1];
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
959 if (!cpe->common_window)
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
960 return;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
961 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
962 for (g = 0; g < sce0->ics.num_swb; g++) {
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
963 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
964 float dist1 = 0.0f, dist2 = 0.0f;
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
965 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
966 FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
967 FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
968 float minthr = fminf(band0->threshold, band1->threshold);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
969 float maxthr = fmaxf(band0->threshold, band1->threshold);
9936
7f42ae22c351 Cosmetics: Pretty print the AAC encoder.
alexc
parents: 9935
diff changeset
970 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
9935
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
971 M[i] = (sce0->coeffs[start+w2*128+i]
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
972 + sce1->coeffs[start+w2*128+i])*0.5;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
973 S[i] = sce0->coeffs[start+w2*128+i]
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
974 - sce1->coeffs[start+w2*128+i];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
975 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
976 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
977 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
978 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
979 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
980 dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
981 L34,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
982 sce0->ics.swb_sizes[g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
983 sce0->sf_idx[(w+w2)*16+g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
984 sce0->band_type[(w+w2)*16+g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
985 lambda / band0->threshold, INFINITY, NULL);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
986 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
987 R34,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
988 sce1->ics.swb_sizes[g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
989 sce1->sf_idx[(w+w2)*16+g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
990 sce1->band_type[(w+w2)*16+g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
991 lambda / band1->threshold, INFINITY, NULL);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
992 dist2 += quantize_band_cost(s, M,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
993 M34,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
994 sce0->ics.swb_sizes[g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
995 sce0->sf_idx[(w+w2)*16+g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
996 sce0->band_type[(w+w2)*16+g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
997 lambda / maxthr, INFINITY, NULL);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
998 dist2 += quantize_band_cost(s, S,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
999 S34,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1000 sce1->ics.swb_sizes[g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1001 sce1->sf_idx[(w+w2)*16+g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1002 sce1->band_type[(w+w2)*16+g],
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1003 lambda / minthr, INFINITY, NULL);
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1004 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1005 cpe->ms_mask[w*16+g] = dist2 < dist1;
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1006 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1007 start += sce0->ics.swb_sizes[g];
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1008 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1009 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1010 }
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1011
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1012 AACCoefficientsEncoder ff_aac_coders[] = {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1013 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1014 search_for_quantizers_faac,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1015 encode_window_bands_info_fixed,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1016 quantize_and_encode_band,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1017 // search_for_ms,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1018 },
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1019 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1020 search_for_quantizers_anmr,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1021 encode_window_bands_info,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1022 quantize_and_encode_band,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1023 // search_for_ms,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1024 },
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1025 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1026 search_for_quantizers_twoloop,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1027 encode_window_bands_info,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1028 quantize_and_encode_band,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1029 // search_for_ms,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1030 },
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1031 {
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1032 search_for_quantizers_fast,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1033 encode_window_bands_info,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1034 quantize_and_encode_band,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1035 // search_for_ms,
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1036 },
d09283aeeef8 Merge the AAC encoder from SoC svn. It is still considered experimental.
alexc
parents:
diff changeset
1037 };