1159b3361Sopenharmony_ci/*
2159b3361Sopenharmony_ci *	MP3 huffman table selecting and bit counting
3159b3361Sopenharmony_ci *
4159b3361Sopenharmony_ci *	Copyright (c) 1999-2005 Takehiro TOMINAGA
5159b3361Sopenharmony_ci *	Copyright (c) 2002-2005 Gabriel Bouvigne
6159b3361Sopenharmony_ci *
7159b3361Sopenharmony_ci * This library is free software; you can redistribute it and/or
8159b3361Sopenharmony_ci * modify it under the terms of the GNU Library General Public
9159b3361Sopenharmony_ci * License as published by the Free Software Foundation; either
10159b3361Sopenharmony_ci * version 2 of the License, or (at your option) any later version.
11159b3361Sopenharmony_ci *
12159b3361Sopenharmony_ci * This library is distributed in the hope that it will be useful,
13159b3361Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
14159b3361Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
15159b3361Sopenharmony_ci * Library General Public License for more details.
16159b3361Sopenharmony_ci *
17159b3361Sopenharmony_ci * You should have received a copy of the GNU Library General Public
18159b3361Sopenharmony_ci * License along with this library; if not, write to the
19159b3361Sopenharmony_ci * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20159b3361Sopenharmony_ci * Boston, MA 02111-1307, USA.
21159b3361Sopenharmony_ci */
22159b3361Sopenharmony_ci
23159b3361Sopenharmony_ci/* $Id$ */
24159b3361Sopenharmony_ci
25159b3361Sopenharmony_ci#ifdef HAVE_CONFIG_H
26159b3361Sopenharmony_ci# include <config.h>
27159b3361Sopenharmony_ci#endif
28159b3361Sopenharmony_ci
29159b3361Sopenharmony_ci
30159b3361Sopenharmony_ci#include "lame.h"
31159b3361Sopenharmony_ci#include "machine.h"
32159b3361Sopenharmony_ci#include "encoder.h"
33159b3361Sopenharmony_ci#include "util.h"
34159b3361Sopenharmony_ci#include "quantize_pvt.h"
35159b3361Sopenharmony_ci#include "tables.h"
36159b3361Sopenharmony_ci
37159b3361Sopenharmony_ci
38159b3361Sopenharmony_cistatic const struct {
39159b3361Sopenharmony_ci    const int region0_count;
40159b3361Sopenharmony_ci    const int region1_count;
41159b3361Sopenharmony_ci} subdv_table[23] = {
42159b3361Sopenharmony_ci    {
43159b3361Sopenharmony_ci    0, 0},              /* 0 bands */
44159b3361Sopenharmony_ci    {
45159b3361Sopenharmony_ci    0, 0},              /* 1 bands */
46159b3361Sopenharmony_ci    {
47159b3361Sopenharmony_ci    0, 0},              /* 2 bands */
48159b3361Sopenharmony_ci    {
49159b3361Sopenharmony_ci    0, 0},              /* 3 bands */
50159b3361Sopenharmony_ci    {
51159b3361Sopenharmony_ci    0, 0},              /* 4 bands */
52159b3361Sopenharmony_ci    {
53159b3361Sopenharmony_ci    0, 1},              /* 5 bands */
54159b3361Sopenharmony_ci    {
55159b3361Sopenharmony_ci    1, 1},              /* 6 bands */
56159b3361Sopenharmony_ci    {
57159b3361Sopenharmony_ci    1, 1},              /* 7 bands */
58159b3361Sopenharmony_ci    {
59159b3361Sopenharmony_ci    1, 2},              /* 8 bands */
60159b3361Sopenharmony_ci    {
61159b3361Sopenharmony_ci    2, 2},              /* 9 bands */
62159b3361Sopenharmony_ci    {
63159b3361Sopenharmony_ci    2, 3},              /* 10 bands */
64159b3361Sopenharmony_ci    {
65159b3361Sopenharmony_ci    2, 3},              /* 11 bands */
66159b3361Sopenharmony_ci    {
67159b3361Sopenharmony_ci    3, 4},              /* 12 bands */
68159b3361Sopenharmony_ci    {
69159b3361Sopenharmony_ci    3, 4},              /* 13 bands */
70159b3361Sopenharmony_ci    {
71159b3361Sopenharmony_ci    3, 4},              /* 14 bands */
72159b3361Sopenharmony_ci    {
73159b3361Sopenharmony_ci    4, 5},              /* 15 bands */
74159b3361Sopenharmony_ci    {
75159b3361Sopenharmony_ci    4, 5},              /* 16 bands */
76159b3361Sopenharmony_ci    {
77159b3361Sopenharmony_ci    4, 6},              /* 17 bands */
78159b3361Sopenharmony_ci    {
79159b3361Sopenharmony_ci    5, 6},              /* 18 bands */
80159b3361Sopenharmony_ci    {
81159b3361Sopenharmony_ci    5, 6},              /* 19 bands */
82159b3361Sopenharmony_ci    {
83159b3361Sopenharmony_ci    5, 7},              /* 20 bands */
84159b3361Sopenharmony_ci    {
85159b3361Sopenharmony_ci    6, 7},              /* 21 bands */
86159b3361Sopenharmony_ci    {
87159b3361Sopenharmony_ci    6, 7},              /* 22 bands */
88159b3361Sopenharmony_ci};
89159b3361Sopenharmony_ci
90159b3361Sopenharmony_ci
91159b3361Sopenharmony_ci
92159b3361Sopenharmony_ci
93159b3361Sopenharmony_ci
94159b3361Sopenharmony_ci/*********************************************************************
95159b3361Sopenharmony_ci * nonlinear quantization of xr
96159b3361Sopenharmony_ci * More accurate formula than the ISO formula.  Takes into account
97159b3361Sopenharmony_ci * the fact that we are quantizing xr -> ix, but we want ix^4/3 to be
98159b3361Sopenharmony_ci * as close as possible to x^4/3.  (taking the nearest int would mean
99159b3361Sopenharmony_ci * ix is as close as possible to xr, which is different.)
100159b3361Sopenharmony_ci *
101159b3361Sopenharmony_ci * From Segher Boessenkool <segher@eastsite.nl>  11/1999
102159b3361Sopenharmony_ci *
103159b3361Sopenharmony_ci * 09/2000: ASM code removed in favor of IEEE754 hack by Takehiro
104159b3361Sopenharmony_ci * Tominaga. If you need the ASM code, check CVS circa Aug 2000.
105159b3361Sopenharmony_ci *
106159b3361Sopenharmony_ci * 01/2004: Optimizations by Gabriel Bouvigne
107159b3361Sopenharmony_ci *********************************************************************/
108159b3361Sopenharmony_ci
109159b3361Sopenharmony_ci
110159b3361Sopenharmony_ci
111159b3361Sopenharmony_ci
112159b3361Sopenharmony_ci
113159b3361Sopenharmony_cistatic void
114159b3361Sopenharmony_ciquantize_lines_xrpow_01(unsigned int l, FLOAT istep, const FLOAT * xr, int *ix)
115159b3361Sopenharmony_ci{
116159b3361Sopenharmony_ci    const FLOAT compareval0 = (1.0f - 0.4054f) / istep;
117159b3361Sopenharmony_ci    unsigned int i;
118159b3361Sopenharmony_ci
119159b3361Sopenharmony_ci    assert(l > 0);
120159b3361Sopenharmony_ci    assert(l % 2 == 0);
121159b3361Sopenharmony_ci    for (i = 0; i < l; i += 2) {
122159b3361Sopenharmony_ci        FLOAT const xr_0 = xr[i+0];
123159b3361Sopenharmony_ci        FLOAT const xr_1 = xr[i+1];
124159b3361Sopenharmony_ci        int const ix_0 = (compareval0 > xr_0) ? 0 : 1;
125159b3361Sopenharmony_ci        int const ix_1 = (compareval0 > xr_1) ? 0 : 1;
126159b3361Sopenharmony_ci        ix[i+0] = ix_0;
127159b3361Sopenharmony_ci        ix[i+1] = ix_1;
128159b3361Sopenharmony_ci    }
129159b3361Sopenharmony_ci}
130159b3361Sopenharmony_ci
131159b3361Sopenharmony_ci
132159b3361Sopenharmony_ci
133159b3361Sopenharmony_ci#ifdef TAKEHIRO_IEEE754_HACK
134159b3361Sopenharmony_ci
135159b3361Sopenharmony_citypedef union {
136159b3361Sopenharmony_ci    float   f;
137159b3361Sopenharmony_ci    int     i;
138159b3361Sopenharmony_ci} fi_union;
139159b3361Sopenharmony_ci
140159b3361Sopenharmony_ci#define MAGIC_FLOAT (65536*(128))
141159b3361Sopenharmony_ci#define MAGIC_INT 0x4b000000
142159b3361Sopenharmony_ci
143159b3361Sopenharmony_ci
144159b3361Sopenharmony_cistatic void
145159b3361Sopenharmony_ciquantize_lines_xrpow(unsigned int l, FLOAT istep, const FLOAT * xp, int *pi)
146159b3361Sopenharmony_ci{
147159b3361Sopenharmony_ci    fi_union *fi;
148159b3361Sopenharmony_ci    unsigned int remaining;
149159b3361Sopenharmony_ci
150159b3361Sopenharmony_ci    assert(l > 0);
151159b3361Sopenharmony_ci
152159b3361Sopenharmony_ci    fi = (fi_union *) pi;
153159b3361Sopenharmony_ci
154159b3361Sopenharmony_ci    l = l >> 1;
155159b3361Sopenharmony_ci    remaining = l % 2;
156159b3361Sopenharmony_ci    l = l >> 1;
157159b3361Sopenharmony_ci    while (l--) {
158159b3361Sopenharmony_ci        double  x0 = istep * xp[0];
159159b3361Sopenharmony_ci        double  x1 = istep * xp[1];
160159b3361Sopenharmony_ci        double  x2 = istep * xp[2];
161159b3361Sopenharmony_ci        double  x3 = istep * xp[3];
162159b3361Sopenharmony_ci
163159b3361Sopenharmony_ci        x0 += MAGIC_FLOAT;
164159b3361Sopenharmony_ci        fi[0].f = x0;
165159b3361Sopenharmony_ci        x1 += MAGIC_FLOAT;
166159b3361Sopenharmony_ci        fi[1].f = x1;
167159b3361Sopenharmony_ci        x2 += MAGIC_FLOAT;
168159b3361Sopenharmony_ci        fi[2].f = x2;
169159b3361Sopenharmony_ci        x3 += MAGIC_FLOAT;
170159b3361Sopenharmony_ci        fi[3].f = x3;
171159b3361Sopenharmony_ci
172159b3361Sopenharmony_ci        fi[0].f = x0 + adj43asm[fi[0].i - MAGIC_INT];
173159b3361Sopenharmony_ci        fi[1].f = x1 + adj43asm[fi[1].i - MAGIC_INT];
174159b3361Sopenharmony_ci        fi[2].f = x2 + adj43asm[fi[2].i - MAGIC_INT];
175159b3361Sopenharmony_ci        fi[3].f = x3 + adj43asm[fi[3].i - MAGIC_INT];
176159b3361Sopenharmony_ci
177159b3361Sopenharmony_ci        fi[0].i -= MAGIC_INT;
178159b3361Sopenharmony_ci        fi[1].i -= MAGIC_INT;
179159b3361Sopenharmony_ci        fi[2].i -= MAGIC_INT;
180159b3361Sopenharmony_ci        fi[3].i -= MAGIC_INT;
181159b3361Sopenharmony_ci        fi += 4;
182159b3361Sopenharmony_ci        xp += 4;
183159b3361Sopenharmony_ci    };
184159b3361Sopenharmony_ci    if (remaining) {
185159b3361Sopenharmony_ci        double  x0 = istep * xp[0];
186159b3361Sopenharmony_ci        double  x1 = istep * xp[1];
187159b3361Sopenharmony_ci
188159b3361Sopenharmony_ci        x0 += MAGIC_FLOAT;
189159b3361Sopenharmony_ci        fi[0].f = x0;
190159b3361Sopenharmony_ci        x1 += MAGIC_FLOAT;
191159b3361Sopenharmony_ci        fi[1].f = x1;
192159b3361Sopenharmony_ci
193159b3361Sopenharmony_ci        fi[0].f = x0 + adj43asm[fi[0].i - MAGIC_INT];
194159b3361Sopenharmony_ci        fi[1].f = x1 + adj43asm[fi[1].i - MAGIC_INT];
195159b3361Sopenharmony_ci
196159b3361Sopenharmony_ci        fi[0].i -= MAGIC_INT;
197159b3361Sopenharmony_ci        fi[1].i -= MAGIC_INT;
198159b3361Sopenharmony_ci    }
199159b3361Sopenharmony_ci
200159b3361Sopenharmony_ci}
201159b3361Sopenharmony_ci
202159b3361Sopenharmony_ci
203159b3361Sopenharmony_ci#else
204159b3361Sopenharmony_ci
205159b3361Sopenharmony_ci/*********************************************************************
206159b3361Sopenharmony_ci * XRPOW_FTOI is a macro to convert floats to ints.
207159b3361Sopenharmony_ci * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]
208159b3361Sopenharmony_ci *                                         ROUNDFAC= -0.0946
209159b3361Sopenharmony_ci *
210159b3361Sopenharmony_ci * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]
211159b3361Sopenharmony_ci *                                   ROUNDFAC=0.4054
212159b3361Sopenharmony_ci *
213159b3361Sopenharmony_ci * Note: using floor() or (int) is extremely slow. On machines where
214159b3361Sopenharmony_ci * the TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile
215159b3361Sopenharmony_ci * to write some ASM for XRPOW_FTOI().
216159b3361Sopenharmony_ci *********************************************************************/
217159b3361Sopenharmony_ci#define XRPOW_FTOI(src,dest) ((dest) = (int)(src))
218159b3361Sopenharmony_ci#define QUANTFAC(rx)  adj43[rx]
219159b3361Sopenharmony_ci#define ROUNDFAC 0.4054
220159b3361Sopenharmony_ci
221159b3361Sopenharmony_ci
222159b3361Sopenharmony_cistatic void
223159b3361Sopenharmony_ciquantize_lines_xrpow(unsigned int l, FLOAT istep, const FLOAT * xr, int *ix)
224159b3361Sopenharmony_ci{
225159b3361Sopenharmony_ci    unsigned int remaining;
226159b3361Sopenharmony_ci
227159b3361Sopenharmony_ci    assert(l > 0);
228159b3361Sopenharmony_ci
229159b3361Sopenharmony_ci    l = l >> 1;
230159b3361Sopenharmony_ci    remaining = l % 2;
231159b3361Sopenharmony_ci    l = l >> 1;
232159b3361Sopenharmony_ci    while (l--) {
233159b3361Sopenharmony_ci        FLOAT   x0, x1, x2, x3;
234159b3361Sopenharmony_ci        int     rx0, rx1, rx2, rx3;
235159b3361Sopenharmony_ci
236159b3361Sopenharmony_ci        x0 = *xr++ * istep;
237159b3361Sopenharmony_ci        x1 = *xr++ * istep;
238159b3361Sopenharmony_ci        XRPOW_FTOI(x0, rx0);
239159b3361Sopenharmony_ci        x2 = *xr++ * istep;
240159b3361Sopenharmony_ci        XRPOW_FTOI(x1, rx1);
241159b3361Sopenharmony_ci        x3 = *xr++ * istep;
242159b3361Sopenharmony_ci        XRPOW_FTOI(x2, rx2);
243159b3361Sopenharmony_ci        x0 += QUANTFAC(rx0);
244159b3361Sopenharmony_ci        XRPOW_FTOI(x3, rx3);
245159b3361Sopenharmony_ci        x1 += QUANTFAC(rx1);
246159b3361Sopenharmony_ci        XRPOW_FTOI(x0, *ix++);
247159b3361Sopenharmony_ci        x2 += QUANTFAC(rx2);
248159b3361Sopenharmony_ci        XRPOW_FTOI(x1, *ix++);
249159b3361Sopenharmony_ci        x3 += QUANTFAC(rx3);
250159b3361Sopenharmony_ci        XRPOW_FTOI(x2, *ix++);
251159b3361Sopenharmony_ci        XRPOW_FTOI(x3, *ix++);
252159b3361Sopenharmony_ci    };
253159b3361Sopenharmony_ci    if (remaining) {
254159b3361Sopenharmony_ci        FLOAT   x0, x1;
255159b3361Sopenharmony_ci        int     rx0, rx1;
256159b3361Sopenharmony_ci
257159b3361Sopenharmony_ci        x0 = *xr++ * istep;
258159b3361Sopenharmony_ci        x1 = *xr++ * istep;
259159b3361Sopenharmony_ci        XRPOW_FTOI(x0, rx0);
260159b3361Sopenharmony_ci        XRPOW_FTOI(x1, rx1);
261159b3361Sopenharmony_ci        x0 += QUANTFAC(rx0);
262159b3361Sopenharmony_ci        x1 += QUANTFAC(rx1);
263159b3361Sopenharmony_ci        XRPOW_FTOI(x0, *ix++);
264159b3361Sopenharmony_ci        XRPOW_FTOI(x1, *ix++);
265159b3361Sopenharmony_ci    }
266159b3361Sopenharmony_ci
267159b3361Sopenharmony_ci}
268159b3361Sopenharmony_ci
269159b3361Sopenharmony_ci
270159b3361Sopenharmony_ci
271159b3361Sopenharmony_ci#endif
272159b3361Sopenharmony_ci
273159b3361Sopenharmony_ci
274159b3361Sopenharmony_ci
275159b3361Sopenharmony_ci/*********************************************************************
276159b3361Sopenharmony_ci * Quantization function
277159b3361Sopenharmony_ci * This function will select which lines to quantize and call the
278159b3361Sopenharmony_ci * proper quantization function
279159b3361Sopenharmony_ci *********************************************************************/
280159b3361Sopenharmony_ci
281159b3361Sopenharmony_cistatic void
282159b3361Sopenharmony_ciquantize_xrpow(const FLOAT * xp, int *pi, FLOAT istep, gr_info const *const cod_info,
283159b3361Sopenharmony_ci               calc_noise_data const *prev_noise)
284159b3361Sopenharmony_ci{
285159b3361Sopenharmony_ci    /* quantize on xr^(3/4) instead of xr */
286159b3361Sopenharmony_ci    int     sfb;
287159b3361Sopenharmony_ci    int     sfbmax;
288159b3361Sopenharmony_ci    int     j = 0;
289159b3361Sopenharmony_ci    int     prev_data_use;
290159b3361Sopenharmony_ci    int    *iData;
291159b3361Sopenharmony_ci    int     accumulate = 0;
292159b3361Sopenharmony_ci    int     accumulate01 = 0;
293159b3361Sopenharmony_ci    int    *acc_iData;
294159b3361Sopenharmony_ci    const FLOAT *acc_xp;
295159b3361Sopenharmony_ci
296159b3361Sopenharmony_ci    iData = pi;
297159b3361Sopenharmony_ci    acc_xp = xp;
298159b3361Sopenharmony_ci    acc_iData = iData;
299159b3361Sopenharmony_ci
300159b3361Sopenharmony_ci
301159b3361Sopenharmony_ci    /* Reusing previously computed data does not seems to work if global gain
302159b3361Sopenharmony_ci       is changed. Finding why it behaves this way would allow to use a cache of
303159b3361Sopenharmony_ci       previously computed values (let's 10 cached values per sfb) that would
304159b3361Sopenharmony_ci       probably provide a noticeable speedup */
305159b3361Sopenharmony_ci    prev_data_use = (prev_noise && (cod_info->global_gain == prev_noise->global_gain));
306159b3361Sopenharmony_ci
307159b3361Sopenharmony_ci    if (cod_info->block_type == SHORT_TYPE)
308159b3361Sopenharmony_ci        sfbmax = 38;
309159b3361Sopenharmony_ci    else
310159b3361Sopenharmony_ci        sfbmax = 21;
311159b3361Sopenharmony_ci
312159b3361Sopenharmony_ci    for (sfb = 0; sfb <= sfbmax; sfb++) {
313159b3361Sopenharmony_ci        int     step = -1;
314159b3361Sopenharmony_ci
315159b3361Sopenharmony_ci        if (prev_data_use || cod_info->block_type == NORM_TYPE) {
316159b3361Sopenharmony_ci            step =
317159b3361Sopenharmony_ci                cod_info->global_gain
318159b3361Sopenharmony_ci                - ((cod_info->scalefac[sfb] + (cod_info->preflag ? pretab[sfb] : 0))
319159b3361Sopenharmony_ci                   << (cod_info->scalefac_scale + 1))
320159b3361Sopenharmony_ci                - cod_info->subblock_gain[cod_info->window[sfb]] * 8;
321159b3361Sopenharmony_ci        }
322159b3361Sopenharmony_ci        assert(cod_info->width[sfb] >= 0);
323159b3361Sopenharmony_ci        if (prev_data_use && (prev_noise->step[sfb] == step)) {
324159b3361Sopenharmony_ci            /* do not recompute this part,
325159b3361Sopenharmony_ci               but compute accumulated lines */
326159b3361Sopenharmony_ci            if (accumulate) {
327159b3361Sopenharmony_ci                quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
328159b3361Sopenharmony_ci                accumulate = 0;
329159b3361Sopenharmony_ci            }
330159b3361Sopenharmony_ci            if (accumulate01) {
331159b3361Sopenharmony_ci                quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
332159b3361Sopenharmony_ci                accumulate01 = 0;
333159b3361Sopenharmony_ci            }
334159b3361Sopenharmony_ci        }
335159b3361Sopenharmony_ci        else {          /*should compute this part */
336159b3361Sopenharmony_ci            int     l;
337159b3361Sopenharmony_ci            l = cod_info->width[sfb];
338159b3361Sopenharmony_ci
339159b3361Sopenharmony_ci            if ((j + cod_info->width[sfb]) > cod_info->max_nonzero_coeff) {
340159b3361Sopenharmony_ci                /*do not compute upper zero part */
341159b3361Sopenharmony_ci                int     usefullsize;
342159b3361Sopenharmony_ci                usefullsize = cod_info->max_nonzero_coeff - j + 1;
343159b3361Sopenharmony_ci                memset(&pi[cod_info->max_nonzero_coeff], 0,
344159b3361Sopenharmony_ci                       sizeof(int) * (576 - cod_info->max_nonzero_coeff));
345159b3361Sopenharmony_ci                l = usefullsize;
346159b3361Sopenharmony_ci
347159b3361Sopenharmony_ci                if (l < 0) {
348159b3361Sopenharmony_ci                    l = 0;
349159b3361Sopenharmony_ci                }
350159b3361Sopenharmony_ci
351159b3361Sopenharmony_ci                /* no need to compute higher sfb values */
352159b3361Sopenharmony_ci                sfb = sfbmax + 1;
353159b3361Sopenharmony_ci            }
354159b3361Sopenharmony_ci
355159b3361Sopenharmony_ci            /*accumulate lines to quantize */
356159b3361Sopenharmony_ci            if (!accumulate && !accumulate01) {
357159b3361Sopenharmony_ci                acc_iData = iData;
358159b3361Sopenharmony_ci                acc_xp = xp;
359159b3361Sopenharmony_ci            }
360159b3361Sopenharmony_ci            if (prev_noise &&
361159b3361Sopenharmony_ci                prev_noise->sfb_count1 > 0 &&
362159b3361Sopenharmony_ci                sfb >= prev_noise->sfb_count1 &&
363159b3361Sopenharmony_ci                prev_noise->step[sfb] > 0 && step >= prev_noise->step[sfb]) {
364159b3361Sopenharmony_ci
365159b3361Sopenharmony_ci                if (accumulate) {
366159b3361Sopenharmony_ci                    quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
367159b3361Sopenharmony_ci                    accumulate = 0;
368159b3361Sopenharmony_ci                    acc_iData = iData;
369159b3361Sopenharmony_ci                    acc_xp = xp;
370159b3361Sopenharmony_ci                }
371159b3361Sopenharmony_ci                accumulate01 += l;
372159b3361Sopenharmony_ci            }
373159b3361Sopenharmony_ci            else {
374159b3361Sopenharmony_ci                if (accumulate01) {
375159b3361Sopenharmony_ci                    quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
376159b3361Sopenharmony_ci                    accumulate01 = 0;
377159b3361Sopenharmony_ci                    acc_iData = iData;
378159b3361Sopenharmony_ci                    acc_xp = xp;
379159b3361Sopenharmony_ci                }
380159b3361Sopenharmony_ci                accumulate += l;
381159b3361Sopenharmony_ci            }
382159b3361Sopenharmony_ci
383159b3361Sopenharmony_ci            if (l <= 0) {
384159b3361Sopenharmony_ci                /*  rh: 20040215
385159b3361Sopenharmony_ci                 *  may happen due to "prev_data_use" optimization
386159b3361Sopenharmony_ci                 */
387159b3361Sopenharmony_ci                if (accumulate01) {
388159b3361Sopenharmony_ci                    quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
389159b3361Sopenharmony_ci                    accumulate01 = 0;
390159b3361Sopenharmony_ci                }
391159b3361Sopenharmony_ci                if (accumulate) {
392159b3361Sopenharmony_ci                    quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
393159b3361Sopenharmony_ci                    accumulate = 0;
394159b3361Sopenharmony_ci                }
395159b3361Sopenharmony_ci
396159b3361Sopenharmony_ci                break;  /* ends for-loop */
397159b3361Sopenharmony_ci            }
398159b3361Sopenharmony_ci        }
399159b3361Sopenharmony_ci        if (sfb <= sfbmax) {
400159b3361Sopenharmony_ci            iData += cod_info->width[sfb];
401159b3361Sopenharmony_ci            xp += cod_info->width[sfb];
402159b3361Sopenharmony_ci            j += cod_info->width[sfb];
403159b3361Sopenharmony_ci        }
404159b3361Sopenharmony_ci    }
405159b3361Sopenharmony_ci    if (accumulate) {   /*last data part */
406159b3361Sopenharmony_ci        quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
407159b3361Sopenharmony_ci        accumulate = 0;
408159b3361Sopenharmony_ci    }
409159b3361Sopenharmony_ci    if (accumulate01) { /*last data part */
410159b3361Sopenharmony_ci        quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
411159b3361Sopenharmony_ci        accumulate01 = 0;
412159b3361Sopenharmony_ci    }
413159b3361Sopenharmony_ci
414159b3361Sopenharmony_ci}
415159b3361Sopenharmony_ci
416159b3361Sopenharmony_ci
417159b3361Sopenharmony_ci
418159b3361Sopenharmony_ci
419159b3361Sopenharmony_ci/*************************************************************************/
420159b3361Sopenharmony_ci/*	      ix_max							 */
421159b3361Sopenharmony_ci/*************************************************************************/
422159b3361Sopenharmony_ci
423159b3361Sopenharmony_cistatic int
424159b3361Sopenharmony_ciix_max(const int *ix, const int *end)
425159b3361Sopenharmony_ci{
426159b3361Sopenharmony_ci    int     max1 = 0, max2 = 0;
427159b3361Sopenharmony_ci
428159b3361Sopenharmony_ci    do {
429159b3361Sopenharmony_ci        int const x1 = *ix++;
430159b3361Sopenharmony_ci        int const x2 = *ix++;
431159b3361Sopenharmony_ci        if (max1 < x1)
432159b3361Sopenharmony_ci            max1 = x1;
433159b3361Sopenharmony_ci
434159b3361Sopenharmony_ci        if (max2 < x2)
435159b3361Sopenharmony_ci            max2 = x2;
436159b3361Sopenharmony_ci    } while (ix < end);
437159b3361Sopenharmony_ci    if (max1 < max2)
438159b3361Sopenharmony_ci        max1 = max2;
439159b3361Sopenharmony_ci    return max1;
440159b3361Sopenharmony_ci}
441159b3361Sopenharmony_ci
442159b3361Sopenharmony_ci
443159b3361Sopenharmony_ci
444159b3361Sopenharmony_ci
445159b3361Sopenharmony_ci
446159b3361Sopenharmony_ci
447159b3361Sopenharmony_ci
448159b3361Sopenharmony_ci
449159b3361Sopenharmony_cistatic int
450159b3361Sopenharmony_cicount_bit_ESC(const int *ix, const int *const end, int t1, const int t2, unsigned int *const s)
451159b3361Sopenharmony_ci{
452159b3361Sopenharmony_ci    /* ESC-table is used */
453159b3361Sopenharmony_ci    unsigned int const linbits = ht[t1].xlen * 65536u + ht[t2].xlen;
454159b3361Sopenharmony_ci    unsigned int sum = 0, sum2;
455159b3361Sopenharmony_ci
456159b3361Sopenharmony_ci    do {
457159b3361Sopenharmony_ci        unsigned int x = *ix++;
458159b3361Sopenharmony_ci        unsigned int y = *ix++;
459159b3361Sopenharmony_ci
460159b3361Sopenharmony_ci        if (x >= 15u) {
461159b3361Sopenharmony_ci            x = 15u;
462159b3361Sopenharmony_ci            sum += linbits;
463159b3361Sopenharmony_ci        }
464159b3361Sopenharmony_ci        if (y >= 15u) {
465159b3361Sopenharmony_ci            y = 15u;
466159b3361Sopenharmony_ci            sum += linbits;
467159b3361Sopenharmony_ci        }
468159b3361Sopenharmony_ci        x <<= 4u;
469159b3361Sopenharmony_ci        x += y;
470159b3361Sopenharmony_ci        sum += largetbl[x];
471159b3361Sopenharmony_ci    } while (ix < end);
472159b3361Sopenharmony_ci
473159b3361Sopenharmony_ci    sum2 = sum & 0xffffu;
474159b3361Sopenharmony_ci    sum >>= 16u;
475159b3361Sopenharmony_ci
476159b3361Sopenharmony_ci    if (sum > sum2) {
477159b3361Sopenharmony_ci        sum = sum2;
478159b3361Sopenharmony_ci        t1 = t2;
479159b3361Sopenharmony_ci    }
480159b3361Sopenharmony_ci
481159b3361Sopenharmony_ci    *s += sum;
482159b3361Sopenharmony_ci    return t1;
483159b3361Sopenharmony_ci}
484159b3361Sopenharmony_ci
485159b3361Sopenharmony_ci
486159b3361Sopenharmony_cistatic int
487159b3361Sopenharmony_cicount_bit_noESC(const int *ix, const int *end, int mx, unsigned int *s)
488159b3361Sopenharmony_ci{
489159b3361Sopenharmony_ci    /* No ESC-words */
490159b3361Sopenharmony_ci    unsigned int sum1 = 0;
491159b3361Sopenharmony_ci    const uint8_t *const hlen1 = ht[1].hlen;
492159b3361Sopenharmony_ci    (void) mx;
493159b3361Sopenharmony_ci
494159b3361Sopenharmony_ci    do {
495159b3361Sopenharmony_ci        unsigned int const x0 = *ix++;
496159b3361Sopenharmony_ci        unsigned int const x1 = *ix++;
497159b3361Sopenharmony_ci        sum1 += hlen1[ x0+x0 + x1 ];
498159b3361Sopenharmony_ci    } while (ix < end);
499159b3361Sopenharmony_ci
500159b3361Sopenharmony_ci    *s += sum1;
501159b3361Sopenharmony_ci    return 1;
502159b3361Sopenharmony_ci}
503159b3361Sopenharmony_ci
504159b3361Sopenharmony_ci
505159b3361Sopenharmony_cistatic const int huf_tbl_noESC[] = {
506159b3361Sopenharmony_ci    1, 2, 5, 7, 7, 10, 10, 13, 13, 13, 13, 13, 13, 13, 13
507159b3361Sopenharmony_ci};
508159b3361Sopenharmony_ci
509159b3361Sopenharmony_ci
510159b3361Sopenharmony_cistatic int
511159b3361Sopenharmony_cicount_bit_noESC_from2(const int *ix, const int *end, int max, unsigned int *s)
512159b3361Sopenharmony_ci{
513159b3361Sopenharmony_ci    int t1 = huf_tbl_noESC[max - 1];
514159b3361Sopenharmony_ci    /* No ESC-words */
515159b3361Sopenharmony_ci    const unsigned int xlen = ht[t1].xlen;
516159b3361Sopenharmony_ci    uint32_t const* table = (t1 == 2) ? &table23[0] : &table56[0];
517159b3361Sopenharmony_ci    unsigned int sum = 0, sum2;
518159b3361Sopenharmony_ci
519159b3361Sopenharmony_ci    do {
520159b3361Sopenharmony_ci        unsigned int const x0 = *ix++;
521159b3361Sopenharmony_ci        unsigned int const x1 = *ix++;
522159b3361Sopenharmony_ci        sum += table[ x0 * xlen + x1 ];
523159b3361Sopenharmony_ci    } while (ix < end);
524159b3361Sopenharmony_ci
525159b3361Sopenharmony_ci    sum2 = sum & 0xffffu;
526159b3361Sopenharmony_ci    sum >>= 16u;
527159b3361Sopenharmony_ci
528159b3361Sopenharmony_ci    if (sum > sum2) {
529159b3361Sopenharmony_ci        sum = sum2;
530159b3361Sopenharmony_ci        t1++;
531159b3361Sopenharmony_ci    }
532159b3361Sopenharmony_ci
533159b3361Sopenharmony_ci    *s += sum;
534159b3361Sopenharmony_ci    return t1;
535159b3361Sopenharmony_ci}
536159b3361Sopenharmony_ci
537159b3361Sopenharmony_ci
538159b3361Sopenharmony_ciinline static int
539159b3361Sopenharmony_cicount_bit_noESC_from3(const int *ix, const int *end, int max, unsigned int * s)
540159b3361Sopenharmony_ci{
541159b3361Sopenharmony_ci    int t1 = huf_tbl_noESC[max - 1];
542159b3361Sopenharmony_ci    /* No ESC-words */
543159b3361Sopenharmony_ci    unsigned int sum1 = 0;
544159b3361Sopenharmony_ci    unsigned int sum2 = 0;
545159b3361Sopenharmony_ci    unsigned int sum3 = 0;
546159b3361Sopenharmony_ci    const unsigned int xlen = ht[t1].xlen;
547159b3361Sopenharmony_ci    const uint8_t *const hlen1 = ht[t1].hlen;
548159b3361Sopenharmony_ci    const uint8_t *const hlen2 = ht[t1 + 1].hlen;
549159b3361Sopenharmony_ci    const uint8_t *const hlen3 = ht[t1 + 2].hlen;
550159b3361Sopenharmony_ci    int     t;
551159b3361Sopenharmony_ci
552159b3361Sopenharmony_ci    do {
553159b3361Sopenharmony_ci        unsigned int x0 = *ix++;
554159b3361Sopenharmony_ci        unsigned int x1 = *ix++;
555159b3361Sopenharmony_ci        unsigned int x = x0 * xlen + x1;
556159b3361Sopenharmony_ci        sum1 += hlen1[x];
557159b3361Sopenharmony_ci        sum2 += hlen2[x];
558159b3361Sopenharmony_ci        sum3 += hlen3[x];
559159b3361Sopenharmony_ci    } while (ix < end);
560159b3361Sopenharmony_ci
561159b3361Sopenharmony_ci    t = t1;
562159b3361Sopenharmony_ci    if (sum1 > sum2) {
563159b3361Sopenharmony_ci        sum1 = sum2;
564159b3361Sopenharmony_ci        t++;
565159b3361Sopenharmony_ci    }
566159b3361Sopenharmony_ci    if (sum1 > sum3) {
567159b3361Sopenharmony_ci        sum1 = sum3;
568159b3361Sopenharmony_ci        t = t1 + 2;
569159b3361Sopenharmony_ci    }
570159b3361Sopenharmony_ci    *s += sum1;
571159b3361Sopenharmony_ci
572159b3361Sopenharmony_ci    return t;
573159b3361Sopenharmony_ci}
574159b3361Sopenharmony_ci
575159b3361Sopenharmony_ci
576159b3361Sopenharmony_ci/*************************************************************************/
577159b3361Sopenharmony_ci/*	      choose table						 */
578159b3361Sopenharmony_ci/*************************************************************************/
579159b3361Sopenharmony_ci
580159b3361Sopenharmony_ci/*
581159b3361Sopenharmony_ci  Choose the Huffman table that will encode ix[begin..end] with
582159b3361Sopenharmony_ci  the fewest bits.
583159b3361Sopenharmony_ci
584159b3361Sopenharmony_ci  Note: This code contains knowledge about the sizes and characteristics
585159b3361Sopenharmony_ci  of the Huffman tables as defined in the IS (Table B.7), and will not work
586159b3361Sopenharmony_ci  with any arbitrary tables.
587159b3361Sopenharmony_ci*/
588159b3361Sopenharmony_cistatic int count_bit_null(const int* ix, const int* end, int max, unsigned int* s)
589159b3361Sopenharmony_ci{
590159b3361Sopenharmony_ci    (void) ix;
591159b3361Sopenharmony_ci    (void) end;
592159b3361Sopenharmony_ci    (void) max;
593159b3361Sopenharmony_ci    (void) s;
594159b3361Sopenharmony_ci    return 0;
595159b3361Sopenharmony_ci}
596159b3361Sopenharmony_ci
597159b3361Sopenharmony_citypedef int (*count_fnc)(const int* ix, const int* end, int max, unsigned int* s);
598159b3361Sopenharmony_ci
599159b3361Sopenharmony_cistatic const count_fnc count_fncs[] =
600159b3361Sopenharmony_ci{ &count_bit_null
601159b3361Sopenharmony_ci, &count_bit_noESC
602159b3361Sopenharmony_ci, &count_bit_noESC_from2
603159b3361Sopenharmony_ci, &count_bit_noESC_from2
604159b3361Sopenharmony_ci, &count_bit_noESC_from3
605159b3361Sopenharmony_ci, &count_bit_noESC_from3
606159b3361Sopenharmony_ci, &count_bit_noESC_from3
607159b3361Sopenharmony_ci, &count_bit_noESC_from3
608159b3361Sopenharmony_ci, &count_bit_noESC_from3
609159b3361Sopenharmony_ci, &count_bit_noESC_from3
610159b3361Sopenharmony_ci, &count_bit_noESC_from3
611159b3361Sopenharmony_ci, &count_bit_noESC_from3
612159b3361Sopenharmony_ci, &count_bit_noESC_from3
613159b3361Sopenharmony_ci, &count_bit_noESC_from3
614159b3361Sopenharmony_ci, &count_bit_noESC_from3
615159b3361Sopenharmony_ci, &count_bit_noESC_from3
616159b3361Sopenharmony_ci};
617159b3361Sopenharmony_ci
618159b3361Sopenharmony_cistatic int
619159b3361Sopenharmony_cichoose_table_nonMMX(const int *ix, const int *const end, int *const _s)
620159b3361Sopenharmony_ci{
621159b3361Sopenharmony_ci    unsigned int* s = (unsigned int*)_s;
622159b3361Sopenharmony_ci    unsigned int  max;
623159b3361Sopenharmony_ci    int     choice, choice2;
624159b3361Sopenharmony_ci    max = ix_max(ix, end);
625159b3361Sopenharmony_ci
626159b3361Sopenharmony_ci    if (max <= 15) {
627159b3361Sopenharmony_ci      return count_fncs[max](ix, end, max, s);
628159b3361Sopenharmony_ci    }
629159b3361Sopenharmony_ci    /* try tables with linbits */
630159b3361Sopenharmony_ci    if (max > IXMAX_VAL) {
631159b3361Sopenharmony_ci        *s = LARGE_BITS;
632159b3361Sopenharmony_ci        return -1;
633159b3361Sopenharmony_ci    }
634159b3361Sopenharmony_ci    max -= 15u;
635159b3361Sopenharmony_ci    for (choice2 = 24; choice2 < 32; choice2++) {
636159b3361Sopenharmony_ci        if (ht[choice2].linmax >= max) {
637159b3361Sopenharmony_ci            break;
638159b3361Sopenharmony_ci        }
639159b3361Sopenharmony_ci    }
640159b3361Sopenharmony_ci
641159b3361Sopenharmony_ci    for (choice = choice2 - 8; choice < 24; choice++) {
642159b3361Sopenharmony_ci        if (ht[choice].linmax >= max) {
643159b3361Sopenharmony_ci            break;
644159b3361Sopenharmony_ci        }
645159b3361Sopenharmony_ci    }
646159b3361Sopenharmony_ci    return count_bit_ESC(ix, end, choice, choice2, s);
647159b3361Sopenharmony_ci}
648159b3361Sopenharmony_ci
649159b3361Sopenharmony_ci
650159b3361Sopenharmony_ci
651159b3361Sopenharmony_ci/*************************************************************************/
652159b3361Sopenharmony_ci/*	      count_bit							 */
653159b3361Sopenharmony_ci/*************************************************************************/
654159b3361Sopenharmony_ciint
655159b3361Sopenharmony_cinoquant_count_bits(lame_internal_flags const *const gfc,
656159b3361Sopenharmony_ci                   gr_info * const gi, calc_noise_data * prev_noise)
657159b3361Sopenharmony_ci{
658159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
659159b3361Sopenharmony_ci    int     bits = 0;
660159b3361Sopenharmony_ci    int     i, a1, a2;
661159b3361Sopenharmony_ci    int const *const ix = gi->l3_enc;
662159b3361Sopenharmony_ci
663159b3361Sopenharmony_ci    i = Min(576, ((gi->max_nonzero_coeff + 2) >> 1) << 1);
664159b3361Sopenharmony_ci
665159b3361Sopenharmony_ci    if (prev_noise)
666159b3361Sopenharmony_ci        prev_noise->sfb_count1 = 0;
667159b3361Sopenharmony_ci
668159b3361Sopenharmony_ci    /* Determine count1 region */
669159b3361Sopenharmony_ci    for (; i > 1; i -= 2)
670159b3361Sopenharmony_ci        if (ix[i - 1] | ix[i - 2])
671159b3361Sopenharmony_ci            break;
672159b3361Sopenharmony_ci    gi->count1 = i;
673159b3361Sopenharmony_ci
674159b3361Sopenharmony_ci    /* Determines the number of bits to encode the quadruples. */
675159b3361Sopenharmony_ci    a1 = a2 = 0;
676159b3361Sopenharmony_ci    for (; i > 3; i -= 4) {
677159b3361Sopenharmony_ci        int x4 = ix[i-4];
678159b3361Sopenharmony_ci        int x3 = ix[i-3];
679159b3361Sopenharmony_ci        int x2 = ix[i-2];
680159b3361Sopenharmony_ci        int x1 = ix[i-1];
681159b3361Sopenharmony_ci        int     p;
682159b3361Sopenharmony_ci        /* hack to check if all values <= 1 */
683159b3361Sopenharmony_ci        if ((unsigned int) (x4 | x3 | x2 | x1) > 1)
684159b3361Sopenharmony_ci            break;
685159b3361Sopenharmony_ci
686159b3361Sopenharmony_ci        p = ((x4 * 2 + x3) * 2 + x2) * 2 + x1;
687159b3361Sopenharmony_ci        a1 += t32l[p];
688159b3361Sopenharmony_ci        a2 += t33l[p];
689159b3361Sopenharmony_ci    }
690159b3361Sopenharmony_ci
691159b3361Sopenharmony_ci    bits = a1;
692159b3361Sopenharmony_ci    gi->count1table_select = 0;
693159b3361Sopenharmony_ci    if (a1 > a2) {
694159b3361Sopenharmony_ci        bits = a2;
695159b3361Sopenharmony_ci        gi->count1table_select = 1;
696159b3361Sopenharmony_ci    }
697159b3361Sopenharmony_ci
698159b3361Sopenharmony_ci    gi->count1bits = bits;
699159b3361Sopenharmony_ci    gi->big_values = i;
700159b3361Sopenharmony_ci    if (i == 0)
701159b3361Sopenharmony_ci        return bits;
702159b3361Sopenharmony_ci
703159b3361Sopenharmony_ci    if (gi->block_type == SHORT_TYPE) {
704159b3361Sopenharmony_ci        a1 = 3 * gfc->scalefac_band.s[3];
705159b3361Sopenharmony_ci        if (a1 > gi->big_values)
706159b3361Sopenharmony_ci            a1 = gi->big_values;
707159b3361Sopenharmony_ci        a2 = gi->big_values;
708159b3361Sopenharmony_ci
709159b3361Sopenharmony_ci    }
710159b3361Sopenharmony_ci    else if (gi->block_type == NORM_TYPE) {
711159b3361Sopenharmony_ci        assert(i <= 576); /* bv_scf has 576 entries (0..575) */
712159b3361Sopenharmony_ci        a1 = gi->region0_count = gfc->sv_qnt.bv_scf[i - 2];
713159b3361Sopenharmony_ci        a2 = gi->region1_count = gfc->sv_qnt.bv_scf[i - 1];
714159b3361Sopenharmony_ci
715159b3361Sopenharmony_ci        assert(a1 + a2 + 2 < SBPSY_l);
716159b3361Sopenharmony_ci        a2 = gfc->scalefac_band.l[a1 + a2 + 2];
717159b3361Sopenharmony_ci        a1 = gfc->scalefac_band.l[a1 + 1];
718159b3361Sopenharmony_ci        if (a2 < i)
719159b3361Sopenharmony_ci            gi->table_select[2] = gfc->choose_table(ix + a2, ix + i, &bits);
720159b3361Sopenharmony_ci
721159b3361Sopenharmony_ci    }
722159b3361Sopenharmony_ci    else {
723159b3361Sopenharmony_ci        gi->region0_count = 7;
724159b3361Sopenharmony_ci        /*gi->region1_count = SBPSY_l - 7 - 1; */
725159b3361Sopenharmony_ci        gi->region1_count = SBMAX_l - 1 - 7 - 1;
726159b3361Sopenharmony_ci        a1 = gfc->scalefac_band.l[7 + 1];
727159b3361Sopenharmony_ci        a2 = i;
728159b3361Sopenharmony_ci        if (a1 > a2) {
729159b3361Sopenharmony_ci            a1 = a2;
730159b3361Sopenharmony_ci        }
731159b3361Sopenharmony_ci    }
732159b3361Sopenharmony_ci
733159b3361Sopenharmony_ci
734159b3361Sopenharmony_ci    /* have to allow for the case when bigvalues < region0 < region1 */
735159b3361Sopenharmony_ci    /* (and region0, region1 are ignored) */
736159b3361Sopenharmony_ci    a1 = Min(a1, i);
737159b3361Sopenharmony_ci    a2 = Min(a2, i);
738159b3361Sopenharmony_ci
739159b3361Sopenharmony_ci    assert(a1 >= 0);
740159b3361Sopenharmony_ci    assert(a2 >= 0);
741159b3361Sopenharmony_ci
742159b3361Sopenharmony_ci    /* Count the number of bits necessary to code the bigvalues region. */
743159b3361Sopenharmony_ci    if (0 < a1)
744159b3361Sopenharmony_ci        gi->table_select[0] = gfc->choose_table(ix, ix + a1, &bits);
745159b3361Sopenharmony_ci    if (a1 < a2)
746159b3361Sopenharmony_ci        gi->table_select[1] = gfc->choose_table(ix + a1, ix + a2, &bits);
747159b3361Sopenharmony_ci    if (cfg->use_best_huffman == 2) {
748159b3361Sopenharmony_ci        gi->part2_3_length = bits;
749159b3361Sopenharmony_ci        best_huffman_divide(gfc, gi);
750159b3361Sopenharmony_ci        bits = gi->part2_3_length;
751159b3361Sopenharmony_ci    }
752159b3361Sopenharmony_ci
753159b3361Sopenharmony_ci
754159b3361Sopenharmony_ci    if (prev_noise) {
755159b3361Sopenharmony_ci        if (gi->block_type == NORM_TYPE) {
756159b3361Sopenharmony_ci            int     sfb = 0;
757159b3361Sopenharmony_ci            while (gfc->scalefac_band.l[sfb] < gi->big_values) {
758159b3361Sopenharmony_ci                sfb++;
759159b3361Sopenharmony_ci            }
760159b3361Sopenharmony_ci            prev_noise->sfb_count1 = sfb;
761159b3361Sopenharmony_ci        }
762159b3361Sopenharmony_ci    }
763159b3361Sopenharmony_ci
764159b3361Sopenharmony_ci    return bits;
765159b3361Sopenharmony_ci}
766159b3361Sopenharmony_ci
767159b3361Sopenharmony_ciint
768159b3361Sopenharmony_cicount_bits(lame_internal_flags const *const gfc,
769159b3361Sopenharmony_ci           const FLOAT * const xr, gr_info * const gi, calc_noise_data * prev_noise)
770159b3361Sopenharmony_ci{
771159b3361Sopenharmony_ci    int    *const ix = gi->l3_enc;
772159b3361Sopenharmony_ci
773159b3361Sopenharmony_ci    /* since quantize_xrpow uses table lookup, we need to check this first: */
774159b3361Sopenharmony_ci    FLOAT const w = (IXMAX_VAL) / IPOW20(gi->global_gain);
775159b3361Sopenharmony_ci
776159b3361Sopenharmony_ci    if (gi->xrpow_max > w)
777159b3361Sopenharmony_ci        return LARGE_BITS;
778159b3361Sopenharmony_ci
779159b3361Sopenharmony_ci    quantize_xrpow(xr, ix, IPOW20(gi->global_gain), gi, prev_noise);
780159b3361Sopenharmony_ci
781159b3361Sopenharmony_ci    if (gfc->sv_qnt.substep_shaping & 2) {
782159b3361Sopenharmony_ci        int     sfb, j = 0;
783159b3361Sopenharmony_ci        /* 0.634521682242439 = 0.5946*2**(.5*0.1875) */
784159b3361Sopenharmony_ci        int const gain = gi->global_gain + gi->scalefac_scale;
785159b3361Sopenharmony_ci        const FLOAT roundfac = 0.634521682242439 / IPOW20(gain);
786159b3361Sopenharmony_ci        for (sfb = 0; sfb < gi->sfbmax; sfb++) {
787159b3361Sopenharmony_ci            int const width = gi->width[sfb];
788159b3361Sopenharmony_ci            assert(width >= 0);
789159b3361Sopenharmony_ci            if (!gfc->sv_qnt.pseudohalf[sfb]) {
790159b3361Sopenharmony_ci                j += width;
791159b3361Sopenharmony_ci            }
792159b3361Sopenharmony_ci            else {
793159b3361Sopenharmony_ci                int     k;
794159b3361Sopenharmony_ci                for (k = j, j += width; k < j; ++k) {
795159b3361Sopenharmony_ci                    ix[k] = (xr[k] >= roundfac) ? ix[k] : 0;
796159b3361Sopenharmony_ci                }
797159b3361Sopenharmony_ci            }
798159b3361Sopenharmony_ci        }
799159b3361Sopenharmony_ci    }
800159b3361Sopenharmony_ci    return noquant_count_bits(gfc, gi, prev_noise);
801159b3361Sopenharmony_ci}
802159b3361Sopenharmony_ci
803159b3361Sopenharmony_ci/***********************************************************************
804159b3361Sopenharmony_ci  re-calculate the best scalefac_compress using scfsi
805159b3361Sopenharmony_ci  the saved bits are kept in the bit reservoir.
806159b3361Sopenharmony_ci **********************************************************************/
807159b3361Sopenharmony_ci
808159b3361Sopenharmony_ci
809159b3361Sopenharmony_ciinline static void
810159b3361Sopenharmony_cirecalc_divide_init(const lame_internal_flags * const gfc,
811159b3361Sopenharmony_ci                   gr_info const *cod_info,
812159b3361Sopenharmony_ci                   int const *const ix, int r01_bits[], int r01_div[], int r0_tbl[], int r1_tbl[])
813159b3361Sopenharmony_ci{
814159b3361Sopenharmony_ci    int     r0, r1, bigv, r0t, r1t, bits;
815159b3361Sopenharmony_ci
816159b3361Sopenharmony_ci    bigv = cod_info->big_values;
817159b3361Sopenharmony_ci
818159b3361Sopenharmony_ci    for (r0 = 0; r0 <= 7 + 15; r0++) {
819159b3361Sopenharmony_ci        r01_bits[r0] = LARGE_BITS;
820159b3361Sopenharmony_ci    }
821159b3361Sopenharmony_ci
822159b3361Sopenharmony_ci    for (r0 = 0; r0 < 16; r0++) {
823159b3361Sopenharmony_ci        int const a1 = gfc->scalefac_band.l[r0 + 1];
824159b3361Sopenharmony_ci        int     r0bits;
825159b3361Sopenharmony_ci        if (a1 >= bigv)
826159b3361Sopenharmony_ci            break;
827159b3361Sopenharmony_ci        r0bits = 0;
828159b3361Sopenharmony_ci        r0t = gfc->choose_table(ix, ix + a1, &r0bits);
829159b3361Sopenharmony_ci
830159b3361Sopenharmony_ci        for (r1 = 0; r1 < 8; r1++) {
831159b3361Sopenharmony_ci            int const a2 = gfc->scalefac_band.l[r0 + r1 + 2];
832159b3361Sopenharmony_ci            if (a2 >= bigv)
833159b3361Sopenharmony_ci                break;
834159b3361Sopenharmony_ci
835159b3361Sopenharmony_ci            bits = r0bits;
836159b3361Sopenharmony_ci            r1t = gfc->choose_table(ix + a1, ix + a2, &bits);
837159b3361Sopenharmony_ci            if (r01_bits[r0 + r1] > bits) {
838159b3361Sopenharmony_ci                r01_bits[r0 + r1] = bits;
839159b3361Sopenharmony_ci                r01_div[r0 + r1] = r0;
840159b3361Sopenharmony_ci                r0_tbl[r0 + r1] = r0t;
841159b3361Sopenharmony_ci                r1_tbl[r0 + r1] = r1t;
842159b3361Sopenharmony_ci            }
843159b3361Sopenharmony_ci        }
844159b3361Sopenharmony_ci    }
845159b3361Sopenharmony_ci}
846159b3361Sopenharmony_ci
847159b3361Sopenharmony_ciinline static void
848159b3361Sopenharmony_cirecalc_divide_sub(const lame_internal_flags * const gfc,
849159b3361Sopenharmony_ci                  const gr_info * cod_info2,
850159b3361Sopenharmony_ci                  gr_info * const gi,
851159b3361Sopenharmony_ci                  const int *const ix,
852159b3361Sopenharmony_ci                  const int r01_bits[], const int r01_div[], const int r0_tbl[], const int r1_tbl[])
853159b3361Sopenharmony_ci{
854159b3361Sopenharmony_ci    int     bits, r2, a2, bigv, r2t;
855159b3361Sopenharmony_ci
856159b3361Sopenharmony_ci    bigv = cod_info2->big_values;
857159b3361Sopenharmony_ci
858159b3361Sopenharmony_ci    for (r2 = 2; r2 < SBMAX_l + 1; r2++) {
859159b3361Sopenharmony_ci        a2 = gfc->scalefac_band.l[r2];
860159b3361Sopenharmony_ci        if (a2 >= bigv)
861159b3361Sopenharmony_ci            break;
862159b3361Sopenharmony_ci
863159b3361Sopenharmony_ci        bits = r01_bits[r2 - 2] + cod_info2->count1bits;
864159b3361Sopenharmony_ci        if (gi->part2_3_length <= bits)
865159b3361Sopenharmony_ci            break;
866159b3361Sopenharmony_ci
867159b3361Sopenharmony_ci        r2t = gfc->choose_table(ix + a2, ix + bigv, &bits);
868159b3361Sopenharmony_ci        if (gi->part2_3_length <= bits)
869159b3361Sopenharmony_ci            continue;
870159b3361Sopenharmony_ci
871159b3361Sopenharmony_ci        memcpy(gi, cod_info2, sizeof(gr_info));
872159b3361Sopenharmony_ci        gi->part2_3_length = bits;
873159b3361Sopenharmony_ci        gi->region0_count = r01_div[r2 - 2];
874159b3361Sopenharmony_ci        gi->region1_count = r2 - 2 - r01_div[r2 - 2];
875159b3361Sopenharmony_ci        gi->table_select[0] = r0_tbl[r2 - 2];
876159b3361Sopenharmony_ci        gi->table_select[1] = r1_tbl[r2 - 2];
877159b3361Sopenharmony_ci        gi->table_select[2] = r2t;
878159b3361Sopenharmony_ci    }
879159b3361Sopenharmony_ci}
880159b3361Sopenharmony_ci
881159b3361Sopenharmony_ci
882159b3361Sopenharmony_ci
883159b3361Sopenharmony_ci
884159b3361Sopenharmony_civoid
885159b3361Sopenharmony_cibest_huffman_divide(const lame_internal_flags * const gfc, gr_info * const gi)
886159b3361Sopenharmony_ci{
887159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
888159b3361Sopenharmony_ci    int     i, a1, a2;
889159b3361Sopenharmony_ci    gr_info cod_info2;
890159b3361Sopenharmony_ci    int const *const ix = gi->l3_enc;
891159b3361Sopenharmony_ci
892159b3361Sopenharmony_ci    int     r01_bits[7 + 15 + 1];
893159b3361Sopenharmony_ci    int     r01_div[7 + 15 + 1];
894159b3361Sopenharmony_ci    int     r0_tbl[7 + 15 + 1];
895159b3361Sopenharmony_ci    int     r1_tbl[7 + 15 + 1];
896159b3361Sopenharmony_ci
897159b3361Sopenharmony_ci
898159b3361Sopenharmony_ci    /* SHORT BLOCK stuff fails for MPEG2 */
899159b3361Sopenharmony_ci    if (gi->block_type == SHORT_TYPE && cfg->mode_gr == 1)
900159b3361Sopenharmony_ci        return;
901159b3361Sopenharmony_ci
902159b3361Sopenharmony_ci
903159b3361Sopenharmony_ci    memcpy(&cod_info2, gi, sizeof(gr_info));
904159b3361Sopenharmony_ci    if (gi->block_type == NORM_TYPE) {
905159b3361Sopenharmony_ci        recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
906159b3361Sopenharmony_ci        recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
907159b3361Sopenharmony_ci    }
908159b3361Sopenharmony_ci
909159b3361Sopenharmony_ci    i = cod_info2.big_values;
910159b3361Sopenharmony_ci    if (i == 0 || (unsigned int) (ix[i - 2] | ix[i - 1]) > 1)
911159b3361Sopenharmony_ci        return;
912159b3361Sopenharmony_ci
913159b3361Sopenharmony_ci    i = gi->count1 + 2;
914159b3361Sopenharmony_ci    if (i > 576)
915159b3361Sopenharmony_ci        return;
916159b3361Sopenharmony_ci
917159b3361Sopenharmony_ci    /* Determines the number of bits to encode the quadruples. */
918159b3361Sopenharmony_ci    memcpy(&cod_info2, gi, sizeof(gr_info));
919159b3361Sopenharmony_ci    cod_info2.count1 = i;
920159b3361Sopenharmony_ci    a1 = a2 = 0;
921159b3361Sopenharmony_ci
922159b3361Sopenharmony_ci    assert(i <= 576);
923159b3361Sopenharmony_ci
924159b3361Sopenharmony_ci    for (; i > cod_info2.big_values; i -= 4) {
925159b3361Sopenharmony_ci        int const p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
926159b3361Sopenharmony_ci        a1 += t32l[p];
927159b3361Sopenharmony_ci        a2 += t33l[p];
928159b3361Sopenharmony_ci    }
929159b3361Sopenharmony_ci    cod_info2.big_values = i;
930159b3361Sopenharmony_ci
931159b3361Sopenharmony_ci    cod_info2.count1table_select = 0;
932159b3361Sopenharmony_ci    if (a1 > a2) {
933159b3361Sopenharmony_ci        a1 = a2;
934159b3361Sopenharmony_ci        cod_info2.count1table_select = 1;
935159b3361Sopenharmony_ci    }
936159b3361Sopenharmony_ci
937159b3361Sopenharmony_ci    cod_info2.count1bits = a1;
938159b3361Sopenharmony_ci
939159b3361Sopenharmony_ci    if (cod_info2.block_type == NORM_TYPE)
940159b3361Sopenharmony_ci        recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
941159b3361Sopenharmony_ci    else {
942159b3361Sopenharmony_ci        /* Count the number of bits necessary to code the bigvalues region. */
943159b3361Sopenharmony_ci        cod_info2.part2_3_length = a1;
944159b3361Sopenharmony_ci        a1 = gfc->scalefac_band.l[7 + 1];
945159b3361Sopenharmony_ci        if (a1 > i) {
946159b3361Sopenharmony_ci            a1 = i;
947159b3361Sopenharmony_ci        }
948159b3361Sopenharmony_ci        if (a1 > 0)
949159b3361Sopenharmony_ci            cod_info2.table_select[0] =
950159b3361Sopenharmony_ci                gfc->choose_table(ix, ix + a1, (int *) &cod_info2.part2_3_length);
951159b3361Sopenharmony_ci        if (i > a1)
952159b3361Sopenharmony_ci            cod_info2.table_select[1] =
953159b3361Sopenharmony_ci                gfc->choose_table(ix + a1, ix + i, (int *) &cod_info2.part2_3_length);
954159b3361Sopenharmony_ci        if (gi->part2_3_length > cod_info2.part2_3_length)
955159b3361Sopenharmony_ci            memcpy(gi, &cod_info2, sizeof(gr_info));
956159b3361Sopenharmony_ci    }
957159b3361Sopenharmony_ci}
958159b3361Sopenharmony_ci
959159b3361Sopenharmony_cistatic const int slen1_n[16] = { 1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16 };
960159b3361Sopenharmony_cistatic const int slen2_n[16] = { 1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8 };
961159b3361Sopenharmony_ciconst int slen1_tab[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
962159b3361Sopenharmony_ciconst int slen2_tab[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
963159b3361Sopenharmony_ci
964159b3361Sopenharmony_cistatic void
965159b3361Sopenharmony_ciscfsi_calc(int ch, III_side_info_t * l3_side)
966159b3361Sopenharmony_ci{
967159b3361Sopenharmony_ci    unsigned int i;
968159b3361Sopenharmony_ci    int     s1, s2, c1, c2;
969159b3361Sopenharmony_ci    int     sfb;
970159b3361Sopenharmony_ci    gr_info *const gi = &l3_side->tt[1][ch];
971159b3361Sopenharmony_ci    gr_info const *const g0 = &l3_side->tt[0][ch];
972159b3361Sopenharmony_ci
973159b3361Sopenharmony_ci    for (i = 0; i < (sizeof(scfsi_band) / sizeof(int)) - 1; i++) {
974159b3361Sopenharmony_ci        for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
975159b3361Sopenharmony_ci            if (g0->scalefac[sfb] != gi->scalefac[sfb]
976159b3361Sopenharmony_ci                && gi->scalefac[sfb] >= 0)
977159b3361Sopenharmony_ci                break;
978159b3361Sopenharmony_ci        }
979159b3361Sopenharmony_ci        if (sfb == scfsi_band[i + 1]) {
980159b3361Sopenharmony_ci            for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
981159b3361Sopenharmony_ci                gi->scalefac[sfb] = -1;
982159b3361Sopenharmony_ci            }
983159b3361Sopenharmony_ci            l3_side->scfsi[ch][i] = 1;
984159b3361Sopenharmony_ci        }
985159b3361Sopenharmony_ci    }
986159b3361Sopenharmony_ci
987159b3361Sopenharmony_ci    s1 = c1 = 0;
988159b3361Sopenharmony_ci    for (sfb = 0; sfb < 11; sfb++) {
989159b3361Sopenharmony_ci        if (gi->scalefac[sfb] == -1)
990159b3361Sopenharmony_ci            continue;
991159b3361Sopenharmony_ci        c1++;
992159b3361Sopenharmony_ci        if (s1 < gi->scalefac[sfb])
993159b3361Sopenharmony_ci            s1 = gi->scalefac[sfb];
994159b3361Sopenharmony_ci    }
995159b3361Sopenharmony_ci
996159b3361Sopenharmony_ci    s2 = c2 = 0;
997159b3361Sopenharmony_ci    for (; sfb < SBPSY_l; sfb++) {
998159b3361Sopenharmony_ci        if (gi->scalefac[sfb] == -1)
999159b3361Sopenharmony_ci            continue;
1000159b3361Sopenharmony_ci        c2++;
1001159b3361Sopenharmony_ci        if (s2 < gi->scalefac[sfb])
1002159b3361Sopenharmony_ci            s2 = gi->scalefac[sfb];
1003159b3361Sopenharmony_ci    }
1004159b3361Sopenharmony_ci
1005159b3361Sopenharmony_ci    for (i = 0; i < 16; i++) {
1006159b3361Sopenharmony_ci        if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
1007159b3361Sopenharmony_ci            int const c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
1008159b3361Sopenharmony_ci            if (gi->part2_length > c) {
1009159b3361Sopenharmony_ci                gi->part2_length = c;
1010159b3361Sopenharmony_ci                gi->scalefac_compress = (int)i;
1011159b3361Sopenharmony_ci            }
1012159b3361Sopenharmony_ci        }
1013159b3361Sopenharmony_ci    }
1014159b3361Sopenharmony_ci}
1015159b3361Sopenharmony_ci
1016159b3361Sopenharmony_ci/*
1017159b3361Sopenharmony_ciFind the optimal way to store the scalefactors.
1018159b3361Sopenharmony_ciOnly call this routine after final scalefactors have been
1019159b3361Sopenharmony_cichosen and the channel/granule will not be re-encoded.
1020159b3361Sopenharmony_ci */
1021159b3361Sopenharmony_civoid
1022159b3361Sopenharmony_cibest_scalefac_store(const lame_internal_flags * gfc,
1023159b3361Sopenharmony_ci                    const int gr, const int ch, III_side_info_t * const l3_side)
1024159b3361Sopenharmony_ci{
1025159b3361Sopenharmony_ci    SessionConfig_t const *const cfg = &gfc->cfg;
1026159b3361Sopenharmony_ci    /* use scalefac_scale if we can */
1027159b3361Sopenharmony_ci    gr_info *const gi = &l3_side->tt[gr][ch];
1028159b3361Sopenharmony_ci    int     sfb, i, j, l;
1029159b3361Sopenharmony_ci    int     recalc = 0;
1030159b3361Sopenharmony_ci
1031159b3361Sopenharmony_ci    /* remove scalefacs from bands with ix=0.  This idea comes
1032159b3361Sopenharmony_ci     * from the AAC ISO docs.  added mt 3/00 */
1033159b3361Sopenharmony_ci    /* check if l3_enc=0 */
1034159b3361Sopenharmony_ci    j = 0;
1035159b3361Sopenharmony_ci    for (sfb = 0; sfb < gi->sfbmax; sfb++) {
1036159b3361Sopenharmony_ci        int const width = gi->width[sfb];
1037159b3361Sopenharmony_ci        assert(width >= 0);
1038159b3361Sopenharmony_ci        for (l = j, j += width; l < j; ++l) {
1039159b3361Sopenharmony_ci            if (gi->l3_enc[l] != 0)
1040159b3361Sopenharmony_ci                break;
1041159b3361Sopenharmony_ci        }
1042159b3361Sopenharmony_ci        if (l == j)
1043159b3361Sopenharmony_ci            gi->scalefac[sfb] = recalc = -2; /* anything goes. */
1044159b3361Sopenharmony_ci        /*  only best_scalefac_store and calc_scfsi
1045159b3361Sopenharmony_ci         *  know--and only they should know--about the magic number -2.
1046159b3361Sopenharmony_ci         */
1047159b3361Sopenharmony_ci    }
1048159b3361Sopenharmony_ci
1049159b3361Sopenharmony_ci    if (!gi->scalefac_scale && !gi->preflag) {
1050159b3361Sopenharmony_ci        int     s = 0;
1051159b3361Sopenharmony_ci        for (sfb = 0; sfb < gi->sfbmax; sfb++)
1052159b3361Sopenharmony_ci            if (gi->scalefac[sfb] > 0)
1053159b3361Sopenharmony_ci                s |= gi->scalefac[sfb];
1054159b3361Sopenharmony_ci
1055159b3361Sopenharmony_ci        if (!(s & 1) && s != 0) {
1056159b3361Sopenharmony_ci            for (sfb = 0; sfb < gi->sfbmax; sfb++)
1057159b3361Sopenharmony_ci                if (gi->scalefac[sfb] > 0)
1058159b3361Sopenharmony_ci                    gi->scalefac[sfb] >>= 1;
1059159b3361Sopenharmony_ci
1060159b3361Sopenharmony_ci            gi->scalefac_scale = recalc = 1;
1061159b3361Sopenharmony_ci        }
1062159b3361Sopenharmony_ci    }
1063159b3361Sopenharmony_ci
1064159b3361Sopenharmony_ci    if (!gi->preflag && gi->block_type != SHORT_TYPE && cfg->mode_gr == 2) {
1065159b3361Sopenharmony_ci        for (sfb = 11; sfb < SBPSY_l; sfb++)
1066159b3361Sopenharmony_ci            if (gi->scalefac[sfb] < pretab[sfb] && gi->scalefac[sfb] != -2)
1067159b3361Sopenharmony_ci                break;
1068159b3361Sopenharmony_ci        if (sfb == SBPSY_l) {
1069159b3361Sopenharmony_ci            for (sfb = 11; sfb < SBPSY_l; sfb++)
1070159b3361Sopenharmony_ci                if (gi->scalefac[sfb] > 0)
1071159b3361Sopenharmony_ci                    gi->scalefac[sfb] -= pretab[sfb];
1072159b3361Sopenharmony_ci
1073159b3361Sopenharmony_ci            gi->preflag = recalc = 1;
1074159b3361Sopenharmony_ci        }
1075159b3361Sopenharmony_ci    }
1076159b3361Sopenharmony_ci
1077159b3361Sopenharmony_ci    for (i = 0; i < 4; i++)
1078159b3361Sopenharmony_ci        l3_side->scfsi[ch][i] = 0;
1079159b3361Sopenharmony_ci
1080159b3361Sopenharmony_ci    if (cfg->mode_gr == 2 && gr == 1
1081159b3361Sopenharmony_ci        && l3_side->tt[0][ch].block_type != SHORT_TYPE
1082159b3361Sopenharmony_ci        && l3_side->tt[1][ch].block_type != SHORT_TYPE) {
1083159b3361Sopenharmony_ci        scfsi_calc(ch, l3_side);
1084159b3361Sopenharmony_ci        recalc = 0;
1085159b3361Sopenharmony_ci    }
1086159b3361Sopenharmony_ci    for (sfb = 0; sfb < gi->sfbmax; sfb++) {
1087159b3361Sopenharmony_ci        if (gi->scalefac[sfb] == -2) {
1088159b3361Sopenharmony_ci            gi->scalefac[sfb] = 0; /* if anything goes, then 0 is a good choice */
1089159b3361Sopenharmony_ci        }
1090159b3361Sopenharmony_ci    }
1091159b3361Sopenharmony_ci    if (recalc) {
1092159b3361Sopenharmony_ci        (void) scale_bitcount(gfc, gi);
1093159b3361Sopenharmony_ci    }
1094159b3361Sopenharmony_ci}
1095159b3361Sopenharmony_ci
1096159b3361Sopenharmony_ci
1097159b3361Sopenharmony_ci#ifndef NDEBUG
1098159b3361Sopenharmony_cistatic int
1099159b3361Sopenharmony_ciall_scalefactors_not_negative(int const *scalefac, int n)
1100159b3361Sopenharmony_ci{
1101159b3361Sopenharmony_ci    int     i;
1102159b3361Sopenharmony_ci    for (i = 0; i < n; ++i) {
1103159b3361Sopenharmony_ci        if (scalefac[i] < 0)
1104159b3361Sopenharmony_ci            return 0;
1105159b3361Sopenharmony_ci    }
1106159b3361Sopenharmony_ci    return 1;
1107159b3361Sopenharmony_ci}
1108159b3361Sopenharmony_ci#endif
1109159b3361Sopenharmony_ci
1110159b3361Sopenharmony_ci
1111159b3361Sopenharmony_ci/* number of bits used to encode scalefacs */
1112159b3361Sopenharmony_ci
1113159b3361Sopenharmony_ci/* 18*slen1_tab[i] + 18*slen2_tab[i] */
1114159b3361Sopenharmony_cistatic const int scale_short[16] = {
1115159b3361Sopenharmony_ci    0, 18, 36, 54, 54, 36, 54, 72, 54, 72, 90, 72, 90, 108, 108, 126
1116159b3361Sopenharmony_ci};
1117159b3361Sopenharmony_ci
1118159b3361Sopenharmony_ci/* 17*slen1_tab[i] + 18*slen2_tab[i] */
1119159b3361Sopenharmony_cistatic const int scale_mixed[16] = {
1120159b3361Sopenharmony_ci    0, 18, 36, 54, 51, 35, 53, 71, 52, 70, 88, 69, 87, 105, 104, 122
1121159b3361Sopenharmony_ci};
1122159b3361Sopenharmony_ci
1123159b3361Sopenharmony_ci/* 11*slen1_tab[i] + 10*slen2_tab[i] */
1124159b3361Sopenharmony_cistatic const int scale_long[16] = {
1125159b3361Sopenharmony_ci    0, 10, 20, 30, 33, 21, 31, 41, 32, 42, 52, 43, 53, 63, 64, 74
1126159b3361Sopenharmony_ci};
1127159b3361Sopenharmony_ci
1128159b3361Sopenharmony_ci
1129159b3361Sopenharmony_ci/*************************************************************************/
1130159b3361Sopenharmony_ci/*            scale_bitcount                                             */
1131159b3361Sopenharmony_ci/*************************************************************************/
1132159b3361Sopenharmony_ci
1133159b3361Sopenharmony_ci/* Also calculates the number of bits necessary to code the scalefactors. */
1134159b3361Sopenharmony_ci
1135159b3361Sopenharmony_cistatic int
1136159b3361Sopenharmony_cimpeg1_scale_bitcount(const lame_internal_flags * gfc, gr_info * const cod_info)
1137159b3361Sopenharmony_ci{
1138159b3361Sopenharmony_ci    int     k, sfb, max_slen1 = 0, max_slen2 = 0;
1139159b3361Sopenharmony_ci
1140159b3361Sopenharmony_ci    /* maximum values */
1141159b3361Sopenharmony_ci    const int *tab;
1142159b3361Sopenharmony_ci    int    *const scalefac = cod_info->scalefac;
1143159b3361Sopenharmony_ci
1144159b3361Sopenharmony_ci    (void) gfc;
1145159b3361Sopenharmony_ci    assert(all_scalefactors_not_negative(scalefac, cod_info->sfbmax));
1146159b3361Sopenharmony_ci
1147159b3361Sopenharmony_ci    if (cod_info->block_type == SHORT_TYPE) {
1148159b3361Sopenharmony_ci        tab = scale_short;
1149159b3361Sopenharmony_ci        if (cod_info->mixed_block_flag)
1150159b3361Sopenharmony_ci            tab = scale_mixed;
1151159b3361Sopenharmony_ci    }
1152159b3361Sopenharmony_ci    else {              /* block_type == 1,2,or 3 */
1153159b3361Sopenharmony_ci        tab = scale_long;
1154159b3361Sopenharmony_ci        if (!cod_info->preflag) {
1155159b3361Sopenharmony_ci            for (sfb = 11; sfb < SBPSY_l; sfb++)
1156159b3361Sopenharmony_ci                if (scalefac[sfb] < pretab[sfb])
1157159b3361Sopenharmony_ci                    break;
1158159b3361Sopenharmony_ci
1159159b3361Sopenharmony_ci            if (sfb == SBPSY_l) {
1160159b3361Sopenharmony_ci                cod_info->preflag = 1;
1161159b3361Sopenharmony_ci                for (sfb = 11; sfb < SBPSY_l; sfb++)
1162159b3361Sopenharmony_ci                    scalefac[sfb] -= pretab[sfb];
1163159b3361Sopenharmony_ci            }
1164159b3361Sopenharmony_ci        }
1165159b3361Sopenharmony_ci    }
1166159b3361Sopenharmony_ci
1167159b3361Sopenharmony_ci    for (sfb = 0; sfb < cod_info->sfbdivide; sfb++)
1168159b3361Sopenharmony_ci        if (max_slen1 < scalefac[sfb])
1169159b3361Sopenharmony_ci            max_slen1 = scalefac[sfb];
1170159b3361Sopenharmony_ci
1171159b3361Sopenharmony_ci    for (; sfb < cod_info->sfbmax; sfb++)
1172159b3361Sopenharmony_ci        if (max_slen2 < scalefac[sfb])
1173159b3361Sopenharmony_ci            max_slen2 = scalefac[sfb];
1174159b3361Sopenharmony_ci
1175159b3361Sopenharmony_ci    /* from Takehiro TOMINAGA <tominaga@isoternet.org> 10/99
1176159b3361Sopenharmony_ci     * loop over *all* posible values of scalefac_compress to find the
1177159b3361Sopenharmony_ci     * one which uses the smallest number of bits.  ISO would stop
1178159b3361Sopenharmony_ci     * at first valid index */
1179159b3361Sopenharmony_ci    cod_info->part2_length = LARGE_BITS;
1180159b3361Sopenharmony_ci    for (k = 0; k < 16; k++) {
1181159b3361Sopenharmony_ci        if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k]
1182159b3361Sopenharmony_ci            && cod_info->part2_length > tab[k]) {
1183159b3361Sopenharmony_ci            cod_info->part2_length = tab[k];
1184159b3361Sopenharmony_ci            cod_info->scalefac_compress = k;
1185159b3361Sopenharmony_ci        }
1186159b3361Sopenharmony_ci    }
1187159b3361Sopenharmony_ci    return cod_info->part2_length == LARGE_BITS;
1188159b3361Sopenharmony_ci}
1189159b3361Sopenharmony_ci
1190159b3361Sopenharmony_ci
1191159b3361Sopenharmony_ci
1192159b3361Sopenharmony_ci/*
1193159b3361Sopenharmony_ci  table of largest scalefactor values for MPEG2
1194159b3361Sopenharmony_ci*/
1195159b3361Sopenharmony_cistatic const int max_range_sfac_tab[6][4] = {
1196159b3361Sopenharmony_ci    {15, 15, 7, 7},
1197159b3361Sopenharmony_ci    {15, 15, 7, 0},
1198159b3361Sopenharmony_ci    {7, 3, 0, 0},
1199159b3361Sopenharmony_ci    {15, 31, 31, 0},
1200159b3361Sopenharmony_ci    {7, 7, 7, 0},
1201159b3361Sopenharmony_ci    {3, 3, 0, 0}
1202159b3361Sopenharmony_ci};
1203159b3361Sopenharmony_ci
1204159b3361Sopenharmony_ci
1205159b3361Sopenharmony_ci
1206159b3361Sopenharmony_ci
1207159b3361Sopenharmony_ci/*************************************************************************/
1208159b3361Sopenharmony_ci/*            scale_bitcount_lsf                                         */
1209159b3361Sopenharmony_ci/*************************************************************************/
1210159b3361Sopenharmony_ci
1211159b3361Sopenharmony_ci/* Also counts the number of bits to encode the scalefacs but for MPEG 2 */
1212159b3361Sopenharmony_ci/* Lower sampling frequencies  (24, 22.05 and 16 kHz.)                   */
1213159b3361Sopenharmony_ci
1214159b3361Sopenharmony_ci/*  This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS,     */
1215159b3361Sopenharmony_ci/* "Audio Decoding Layer III"                                            */
1216159b3361Sopenharmony_ci
1217159b3361Sopenharmony_cistatic int
1218159b3361Sopenharmony_cimpeg2_scale_bitcount(const lame_internal_flags * gfc, gr_info * const cod_info)
1219159b3361Sopenharmony_ci{
1220159b3361Sopenharmony_ci    int     table_number, row_in_table, partition, nr_sfb, window, over;
1221159b3361Sopenharmony_ci    int     i, sfb, max_sfac[4];
1222159b3361Sopenharmony_ci    const int *partition_table;
1223159b3361Sopenharmony_ci    int const *const scalefac = cod_info->scalefac;
1224159b3361Sopenharmony_ci
1225159b3361Sopenharmony_ci    /*
1226159b3361Sopenharmony_ci       Set partition table. Note that should try to use table one,
1227159b3361Sopenharmony_ci       but do not yet...
1228159b3361Sopenharmony_ci     */
1229159b3361Sopenharmony_ci    if (cod_info->preflag)
1230159b3361Sopenharmony_ci        table_number = 2;
1231159b3361Sopenharmony_ci    else
1232159b3361Sopenharmony_ci        table_number = 0;
1233159b3361Sopenharmony_ci
1234159b3361Sopenharmony_ci    for (i = 0; i < 4; i++)
1235159b3361Sopenharmony_ci        max_sfac[i] = 0;
1236159b3361Sopenharmony_ci
1237159b3361Sopenharmony_ci    if (cod_info->block_type == SHORT_TYPE) {
1238159b3361Sopenharmony_ci        row_in_table = 1;
1239159b3361Sopenharmony_ci        partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
1240159b3361Sopenharmony_ci        for (sfb = 0, partition = 0; partition < 4; partition++) {
1241159b3361Sopenharmony_ci            nr_sfb = partition_table[partition] / 3;
1242159b3361Sopenharmony_ci            for (i = 0; i < nr_sfb; i++, sfb++)
1243159b3361Sopenharmony_ci                for (window = 0; window < 3; window++)
1244159b3361Sopenharmony_ci                    if (scalefac[sfb * 3 + window] > max_sfac[partition])
1245159b3361Sopenharmony_ci                        max_sfac[partition] = scalefac[sfb * 3 + window];
1246159b3361Sopenharmony_ci        }
1247159b3361Sopenharmony_ci    }
1248159b3361Sopenharmony_ci    else {
1249159b3361Sopenharmony_ci        row_in_table = 0;
1250159b3361Sopenharmony_ci        partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
1251159b3361Sopenharmony_ci        for (sfb = 0, partition = 0; partition < 4; partition++) {
1252159b3361Sopenharmony_ci            nr_sfb = partition_table[partition];
1253159b3361Sopenharmony_ci            for (i = 0; i < nr_sfb; i++, sfb++)
1254159b3361Sopenharmony_ci                if (scalefac[sfb] > max_sfac[partition])
1255159b3361Sopenharmony_ci                    max_sfac[partition] = scalefac[sfb];
1256159b3361Sopenharmony_ci        }
1257159b3361Sopenharmony_ci    }
1258159b3361Sopenharmony_ci
1259159b3361Sopenharmony_ci    for (over = 0, partition = 0; partition < 4; partition++) {
1260159b3361Sopenharmony_ci        if (max_sfac[partition] > max_range_sfac_tab[table_number][partition])
1261159b3361Sopenharmony_ci            over++;
1262159b3361Sopenharmony_ci    }
1263159b3361Sopenharmony_ci    if (!over) {
1264159b3361Sopenharmony_ci        /*
1265159b3361Sopenharmony_ci           Since no bands have been over-amplified, we can set scalefac_compress
1266159b3361Sopenharmony_ci           and slen[] for the formatter
1267159b3361Sopenharmony_ci         */
1268159b3361Sopenharmony_ci        static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
1269159b3361Sopenharmony_ci
1270159b3361Sopenharmony_ci        int     slen1, slen2, slen3, slen4;
1271159b3361Sopenharmony_ci
1272159b3361Sopenharmony_ci        cod_info->sfb_partition_table = nr_of_sfb_block[table_number][row_in_table];
1273159b3361Sopenharmony_ci        for (partition = 0; partition < 4; partition++)
1274159b3361Sopenharmony_ci            cod_info->slen[partition] = log2tab[max_sfac[partition]];
1275159b3361Sopenharmony_ci
1276159b3361Sopenharmony_ci        /* set scalefac_compress */
1277159b3361Sopenharmony_ci        slen1 = cod_info->slen[0];
1278159b3361Sopenharmony_ci        slen2 = cod_info->slen[1];
1279159b3361Sopenharmony_ci        slen3 = cod_info->slen[2];
1280159b3361Sopenharmony_ci        slen4 = cod_info->slen[3];
1281159b3361Sopenharmony_ci
1282159b3361Sopenharmony_ci        switch (table_number) {
1283159b3361Sopenharmony_ci        case 0:
1284159b3361Sopenharmony_ci            cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4)
1285159b3361Sopenharmony_ci                + (slen3 << 2)
1286159b3361Sopenharmony_ci                + slen4;
1287159b3361Sopenharmony_ci            break;
1288159b3361Sopenharmony_ci
1289159b3361Sopenharmony_ci        case 1:
1290159b3361Sopenharmony_ci            cod_info->scalefac_compress = 400 + (((slen1 * 5) + slen2) << 2)
1291159b3361Sopenharmony_ci                + slen3;
1292159b3361Sopenharmony_ci            break;
1293159b3361Sopenharmony_ci
1294159b3361Sopenharmony_ci        case 2:
1295159b3361Sopenharmony_ci            cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2;
1296159b3361Sopenharmony_ci            break;
1297159b3361Sopenharmony_ci
1298159b3361Sopenharmony_ci        default:
1299159b3361Sopenharmony_ci            ERRORF(gfc, "intensity stereo not implemented yet\n");
1300159b3361Sopenharmony_ci            break;
1301159b3361Sopenharmony_ci        }
1302159b3361Sopenharmony_ci    }
1303159b3361Sopenharmony_ci#ifdef DEBUG
1304159b3361Sopenharmony_ci    if (over)
1305159b3361Sopenharmony_ci        ERRORF(gfc, "---WARNING !! Amplification of some bands over limits\n");
1306159b3361Sopenharmony_ci#endif
1307159b3361Sopenharmony_ci    if (!over) {
1308159b3361Sopenharmony_ci        assert(cod_info->sfb_partition_table);
1309159b3361Sopenharmony_ci        cod_info->part2_length = 0;
1310159b3361Sopenharmony_ci        for (partition = 0; partition < 4; partition++)
1311159b3361Sopenharmony_ci            cod_info->part2_length +=
1312159b3361Sopenharmony_ci                cod_info->slen[partition] * cod_info->sfb_partition_table[partition];
1313159b3361Sopenharmony_ci    }
1314159b3361Sopenharmony_ci    return over;
1315159b3361Sopenharmony_ci}
1316159b3361Sopenharmony_ci
1317159b3361Sopenharmony_ci
1318159b3361Sopenharmony_ciint
1319159b3361Sopenharmony_ciscale_bitcount(const lame_internal_flags * gfc, gr_info * cod_info)
1320159b3361Sopenharmony_ci{
1321159b3361Sopenharmony_ci    if (gfc->cfg.mode_gr == 2) {
1322159b3361Sopenharmony_ci        return mpeg1_scale_bitcount(gfc, cod_info);
1323159b3361Sopenharmony_ci    }
1324159b3361Sopenharmony_ci    else {
1325159b3361Sopenharmony_ci        return mpeg2_scale_bitcount(gfc, cod_info);
1326159b3361Sopenharmony_ci    }
1327159b3361Sopenharmony_ci}
1328159b3361Sopenharmony_ci
1329159b3361Sopenharmony_ci
1330159b3361Sopenharmony_ci#ifdef MMX_choose_table
1331159b3361Sopenharmony_ciextern int choose_table_MMX(const int *ix, const int *const end, int *const s);
1332159b3361Sopenharmony_ci#endif
1333159b3361Sopenharmony_ci
1334159b3361Sopenharmony_civoid
1335159b3361Sopenharmony_cihuffman_init(lame_internal_flags * const gfc)
1336159b3361Sopenharmony_ci{
1337159b3361Sopenharmony_ci    int     i;
1338159b3361Sopenharmony_ci
1339159b3361Sopenharmony_ci    gfc->choose_table = choose_table_nonMMX;
1340159b3361Sopenharmony_ci
1341159b3361Sopenharmony_ci#ifdef MMX_choose_table
1342159b3361Sopenharmony_ci    if (gfc->CPU_features.MMX) {
1343159b3361Sopenharmony_ci        gfc->choose_table = choose_table_MMX;
1344159b3361Sopenharmony_ci    }
1345159b3361Sopenharmony_ci#endif
1346159b3361Sopenharmony_ci
1347159b3361Sopenharmony_ci    for (i = 2; i <= 576; i += 2) {
1348159b3361Sopenharmony_ci        int     scfb_anz = 0, bv_index;
1349159b3361Sopenharmony_ci        while (gfc->scalefac_band.l[++scfb_anz] < i);
1350159b3361Sopenharmony_ci
1351159b3361Sopenharmony_ci        bv_index = subdv_table[scfb_anz].region0_count;
1352159b3361Sopenharmony_ci        while (gfc->scalefac_band.l[bv_index + 1] > i)
1353159b3361Sopenharmony_ci            bv_index--;
1354159b3361Sopenharmony_ci
1355159b3361Sopenharmony_ci        if (bv_index < 0) {
1356159b3361Sopenharmony_ci            /* this is an indication that everything is going to
1357159b3361Sopenharmony_ci               be encoded as region0:  bigvalues < region0 < region1
1358159b3361Sopenharmony_ci               so lets set region0, region1 to some value larger
1359159b3361Sopenharmony_ci               than bigvalues */
1360159b3361Sopenharmony_ci            bv_index = subdv_table[scfb_anz].region0_count;
1361159b3361Sopenharmony_ci        }
1362159b3361Sopenharmony_ci
1363159b3361Sopenharmony_ci        gfc->sv_qnt.bv_scf[i - 2] = bv_index;
1364159b3361Sopenharmony_ci
1365159b3361Sopenharmony_ci        bv_index = subdv_table[scfb_anz].region1_count;
1366159b3361Sopenharmony_ci        while (gfc->scalefac_band.l[bv_index + gfc->sv_qnt.bv_scf[i - 2] + 2] > i)
1367159b3361Sopenharmony_ci            bv_index--;
1368159b3361Sopenharmony_ci
1369159b3361Sopenharmony_ci        if (bv_index < 0) {
1370159b3361Sopenharmony_ci            bv_index = subdv_table[scfb_anz].region1_count;
1371159b3361Sopenharmony_ci        }
1372159b3361Sopenharmony_ci
1373159b3361Sopenharmony_ci        gfc->sv_qnt.bv_scf[i - 1] = bv_index;
1374159b3361Sopenharmony_ci    }
1375159b3361Sopenharmony_ci}
1376