vbrquantize.c

Go to the documentation of this file.
00001 /*
00002  *      MP3 quantization
00003  *
00004  *      Copyright (c) 1999-2000 Mark Taylor
00005  *      Copyright (c) 2000-2007 Robert Hegemann
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the
00019  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 
00023 /* $Id: vbrquantize.c,v 1.116 2007/08/12 00:07:24 robert Exp $ */
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #  include <config.h>
00027 #endif
00028 
00029 
00030 #include "lame.h"
00031 #include "machine.h"
00032 #include "encoder.h"
00033 #include "util.h"
00034 #include "vbrquantize.h"
00035 #include "quantize_pvt.h"
00036 
00037 
00038 
00039 
00040 struct algo_s;
00041 typedef struct algo_s algo_t;
00042 
00043 typedef void (*quantize_f) (const algo_t *);
00044 
00045 typedef uint8_t(*find_f) (const FLOAT *, const FLOAT *, FLOAT, unsigned int, uint8_t);
00046 
00047 typedef int (*alloc_sf_f) (const algo_t *, int *, const int *, int);
00048 
00049 struct algo_s {
00050     find_f  find;
00051     quantize_f quantize;
00052     alloc_sf_f alloc;
00053     const FLOAT *xr34orig;
00054     lame_internal_flags *gfc;
00055     gr_info *cod_info;
00056     int     mingain_l;
00057     int     mingain_s[3];
00058 };
00059 
00060 
00061 
00062 /*  Remarks on optimizing compilers:
00063  *
00064  *  the MSVC compiler may get into aliasing problems when accessing
00065  *  memory through the fi_union. declaring it volatile does the trick here
00066  *
00067  *  the calc_sfb_noise_* functions are not inlined because the intel compiler
00068  *  optimized executeables won't work as expected anymore
00069  */
00070 
00071 #ifdef _MSC_VER
00072 #  define VOLATILE volatile
00073 #else
00074 #  define VOLATILE
00075 #endif
00076 
00077 typedef VOLATILE union {
00078     float   f;
00079     int     i;
00080 } fi_union;
00081 
00082 
00083 
00084 #define DOUBLEX double
00085 
00086 #define MAGIC_FLOAT_def (65536*(128))
00087 #define MAGIC_INT_def    0x4b000000
00088 
00089 #ifdef TAKEHIRO_IEEE754_HACK
00090 #  define ROUNDFAC_def -0.0946f
00091 #else
00092 /*********************************************************************
00093  * XRPOW_FTOI is a macro to convert floats to ints.
00094  * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]
00095  *                                         ROUNDFAC= -0.0946
00096  *
00097  * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]
00098  *                                   ROUNDFAC=0.4054
00099  *********************************************************************/
00100 #  define QUANTFAC(rx)  adj43[rx]
00101 #  define ROUNDFAC_def 0.4054f
00102 #  define XRPOW_FTOI(src,dest) ((dest) = (int)(src))
00103 #endif
00104 
00105 static int const MAGIC_INT = MAGIC_INT_def;
00106 #ifndef TAKEHIRO_IEEE754_HACK
00107 static DOUBLEX const ROUNDFAC = ROUNDFAC_def;
00108 #endif
00109 static DOUBLEX const MAGIC_FLOAT = MAGIC_FLOAT_def;
00110 static DOUBLEX const ROUNDFAC_plus_MAGIC_FLOAT = ROUNDFAC_def + MAGIC_FLOAT_def;
00111 
00112 
00113 
00114 
00115 static  FLOAT
00116 max_x34(const FLOAT * xr34, unsigned int bw)
00117 {
00118     FLOAT   xfsf = 0;
00119     unsigned int j = bw >> 1;
00120     unsigned int const remaining = (j & 0x01u);
00121 
00122     for (j >>= 1; j > 0; --j) {
00123         if (xfsf < xr34[0]) {
00124             xfsf = xr34[0];
00125         }
00126         if (xfsf < xr34[1]) {
00127             xfsf = xr34[1];
00128         }
00129         if (xfsf < xr34[2]) {
00130             xfsf = xr34[2];
00131         }
00132         if (xfsf < xr34[3]) {
00133             xfsf = xr34[3];
00134         }
00135         xr34 += 4;
00136     }
00137     if (remaining) {
00138         if (xfsf < xr34[0]) {
00139             xfsf = xr34[0];
00140         }
00141         if (xfsf < xr34[1]) {
00142             xfsf = xr34[1];
00143         }
00144     }
00145     return xfsf;
00146 }
00147 
00148 
00149 
00150 static  uint8_t
00151 find_lowest_scalefac(const FLOAT xr34)
00152 {
00153     uint8_t sf_ok = 255;
00154     uint8_t sf = 128, delsf = 64;
00155     uint8_t i;
00156     for (i = 0; i < 8; ++i) {
00157         FLOAT const xfsf = ipow20[sf] * xr34;
00158         if (xfsf <= IXMAX_VAL) {
00159             sf_ok = sf;
00160             sf -= delsf;
00161         }
00162         else {
00163             sf += delsf;
00164         }
00165         delsf >>= 1;
00166     }
00167     return sf_ok;
00168 }
00169 
00170 
00171 
00172 static void
00173 k_34_4(DOUBLEX x[4], int l3[4])
00174 {
00175 #ifdef TAKEHIRO_IEEE754_HACK
00176     fi_union fi[4];
00177 
00178     assert(x[0] <= IXMAX_VAL && x[1] <= IXMAX_VAL && x[2] <= IXMAX_VAL && x[3] <= IXMAX_VAL);
00179     x[0] += MAGIC_FLOAT;
00180     fi[0].f = x[0];
00181     x[1] += MAGIC_FLOAT;
00182     fi[1].f = x[1];
00183     x[2] += MAGIC_FLOAT;
00184     fi[2].f = x[2];
00185     x[3] += MAGIC_FLOAT;
00186     fi[3].f = x[3];
00187     fi[0].f = x[0] + adj43asm[fi[0].i - MAGIC_INT];
00188     fi[1].f = x[1] + adj43asm[fi[1].i - MAGIC_INT];
00189     fi[2].f = x[2] + adj43asm[fi[2].i - MAGIC_INT];
00190     fi[3].f = x[3] + adj43asm[fi[3].i - MAGIC_INT];
00191     l3[0] = fi[0].i - MAGIC_INT;
00192     l3[1] = fi[1].i - MAGIC_INT;
00193     l3[2] = fi[2].i - MAGIC_INT;
00194     l3[3] = fi[3].i - MAGIC_INT;
00195 #else
00196     assert(x[0] <= IXMAX_VAL && x[1] <= IXMAX_VAL && x[2] <= IXMAX_VAL && x[3] <= IXMAX_VAL);
00197     XRPOW_FTOI(x[0], l3[0]);
00198     XRPOW_FTOI(x[1], l3[1]);
00199     XRPOW_FTOI(x[2], l3[2]);
00200     XRPOW_FTOI(x[3], l3[3]);
00201     x[0] += QUANTFAC(l3[0]);
00202     x[1] += QUANTFAC(l3[1]);
00203     x[2] += QUANTFAC(l3[2]);
00204     x[3] += QUANTFAC(l3[3]);
00205     XRPOW_FTOI(x[0], l3[0]);
00206     XRPOW_FTOI(x[1], l3[1]);
00207     XRPOW_FTOI(x[2], l3[2]);
00208     XRPOW_FTOI(x[3], l3[3]);
00209 #endif
00210 }
00211 
00212 
00213 
00214 static void
00215 k_34_2(DOUBLEX x[2], int l3[2])
00216 {
00217 #ifdef TAKEHIRO_IEEE754_HACK
00218     fi_union fi[2];
00219 
00220     assert(x[0] <= IXMAX_VAL && x[1] <= IXMAX_VAL);
00221     x[0] += MAGIC_FLOAT;
00222     fi[0].f = x[0];
00223     x[1] += MAGIC_FLOAT;
00224     fi[1].f = x[1];
00225     fi[0].f = x[0] + adj43asm[fi[0].i - MAGIC_INT];
00226     fi[1].f = x[1] + adj43asm[fi[1].i - MAGIC_INT];
00227     l3[0] = fi[0].i - MAGIC_INT;
00228     l3[1] = fi[1].i - MAGIC_INT;
00229 #else
00230     assert(x[0] <= IXMAX_VAL && x[1] <= IXMAX_VAL);
00231     XRPOW_FTOI(x[0], l3[0]);
00232     XRPOW_FTOI(x[1], l3[1]);
00233     x[0] += QUANTFAC(l3[0]);
00234     x[1] += QUANTFAC(l3[1]);
00235     XRPOW_FTOI(x[0], l3[0]);
00236     XRPOW_FTOI(x[1], l3[1]);
00237 #endif
00238 }
00239 
00240 
00241 
00242 static void
00243 k_iso_4(DOUBLEX x[4], int l3[4])
00244 {
00245 #ifdef TAKEHIRO_IEEE754_HACK
00246     fi_union fi[4];
00247 
00248     assert(x[0] <= IXMAX_VAL && x[1] <= IXMAX_VAL && x[2] <= IXMAX_VAL && x[3] <= IXMAX_VAL);
00249     x[0] += ROUNDFAC_plus_MAGIC_FLOAT;
00250     fi[0].f = x[0];
00251     x[1] += ROUNDFAC_plus_MAGIC_FLOAT;
00252     fi[1].f = x[1];
00253     x[2] += ROUNDFAC_plus_MAGIC_FLOAT;
00254     fi[2].f = x[2];
00255     x[3] += ROUNDFAC_plus_MAGIC_FLOAT;
00256     fi[3].f = x[3];
00257     l3[0] = fi[0].i - MAGIC_INT;
00258     l3[1] = fi[1].i - MAGIC_INT;
00259     l3[2] = fi[2].i - MAGIC_INT;
00260     l3[3] = fi[3].i - MAGIC_INT;
00261 #else
00262     l3[0] = x[0] + ROUNDFAC;
00263     l3[1] = x[1] + ROUNDFAC;
00264     l3[2] = x[2] + ROUNDFAC;
00265     l3[3] = x[3] + ROUNDFAC;
00266 #endif
00267 }
00268 
00269 
00270 
00271 static void
00272 k_iso_2(DOUBLEX x[2], int l3[2])
00273 {
00274 #ifdef TAKEHIRO_IEEE754_HACK
00275     fi_union fi[2];
00276 
00277     assert(x[0] <= IXMAX_VAL && x[1] <= IXMAX_VAL);
00278     x[0] += ROUNDFAC_plus_MAGIC_FLOAT;
00279     fi[0].f = x[0];
00280     x[1] += ROUNDFAC_plus_MAGIC_FLOAT;
00281     fi[1].f = x[1];
00282     l3[0] = fi[0].i - MAGIC_INT;
00283     l3[1] = fi[1].i - MAGIC_INT;
00284 #else
00285     l3[0] = x[0] + ROUNDFAC;
00286     l3[1] = x[1] + ROUNDFAC;
00287 #endif
00288 }
00289 
00290 
00291 
00292 /*  do call the calc_sfb_noise_* functions only with sf values
00293  *  for which holds: sfpow34*xr34 <= IXMAX_VAL
00294  */
00295 
00296 static  FLOAT
00297 calc_sfb_noise_x34(const FLOAT * xr, const FLOAT * xr34, unsigned int bw, uint8_t sf)
00298 {
00299     DOUBLEX x[4];
00300     int     l3[4];
00301     const FLOAT sfpow = pow20[sf + Q_MAX2]; /*pow(2.0,sf/4.0); */
00302     const FLOAT sfpow34 = ipow20[sf]; /*pow(sfpow,-3.0/4.0); */
00303 
00304     FLOAT   xfsf = 0;
00305     unsigned int j = bw >> 1;
00306     unsigned int const remaining = (j & 0x01u);
00307 
00308     for (j >>= 1; j > 0; --j) {
00309         x[0] = sfpow34 * xr34[0];
00310         x[1] = sfpow34 * xr34[1];
00311         x[2] = sfpow34 * xr34[2];
00312         x[3] = sfpow34 * xr34[3];
00313 
00314         k_34_4(x, l3);
00315 
00316         x[0] = fabs(xr[0]) - sfpow * pow43[l3[0]];
00317         x[1] = fabs(xr[1]) - sfpow * pow43[l3[1]];
00318         x[2] = fabs(xr[2]) - sfpow * pow43[l3[2]];
00319         x[3] = fabs(xr[3]) - sfpow * pow43[l3[3]];
00320         xfsf += (x[0] * x[0] + x[1] * x[1]) + (x[2] * x[2] + x[3] * x[3]);
00321 
00322         xr += 4;
00323         xr34 += 4;
00324     }
00325     if (remaining) {
00326         x[0] = sfpow34 * xr34[0];
00327         x[1] = sfpow34 * xr34[1];
00328 
00329         k_34_2(x, l3);
00330 
00331         x[0] = fabs(xr[0]) - sfpow * pow43[l3[0]];
00332         x[1] = fabs(xr[1]) - sfpow * pow43[l3[1]];
00333         xfsf += x[0] * x[0] + x[1] * x[1];
00334     }
00335     return xfsf;
00336 }
00337 
00338 
00339 
00340 static  FLOAT
00341 calc_sfb_noise_ISO(const FLOAT * xr, const FLOAT * xr34, unsigned int bw, uint8_t sf)
00342 {
00343     DOUBLEX x[4];
00344     int     l3[4];
00345     const FLOAT sfpow = pow20[sf + Q_MAX2]; /*pow(2.0,sf/4.0); */
00346     const FLOAT sfpow34 = ipow20[sf]; /*pow(sfpow,-3.0/4.0); */
00347 
00348     FLOAT   xfsf = 0;
00349     unsigned int j = bw >> 1;
00350     unsigned int const remaining = (j & 0x01u);
00351 
00352     for (j >>= 1; j > 0; --j) {
00353         x[0] = sfpow34 * xr34[0];
00354         x[1] = sfpow34 * xr34[1];
00355         x[2] = sfpow34 * xr34[2];
00356         x[3] = sfpow34 * xr34[3];
00357 
00358         k_iso_4(x, l3);
00359 
00360         x[0] = fabs(xr[0]) - sfpow * pow43[l3[0]];
00361         x[1] = fabs(xr[1]) - sfpow * pow43[l3[1]];
00362         x[2] = fabs(xr[2]) - sfpow * pow43[l3[2]];
00363         x[3] = fabs(xr[3]) - sfpow * pow43[l3[3]];
00364 
00365         xfsf += (x[0] * x[0] + x[1] * x[1]) + (x[2] * x[2] + x[3] * x[3]);
00366 
00367         xr += 4;
00368         xr34 += 4;
00369     }
00370     if (remaining) {
00371         x[0] = sfpow34 * xr34[0];
00372         x[1] = sfpow34 * xr34[1];
00373 
00374         k_iso_2(x, l3);
00375 
00376         x[0] = fabs(xr[0]) - sfpow * pow43[l3[0]];
00377         x[1] = fabs(xr[1]) - sfpow * pow43[l3[1]];
00378         xfsf += x[0] * x[0] + x[1] * x[1];
00379     }
00380     return xfsf;
00381 }
00382 
00383 
00384 
00385 struct calc_noise_cache {
00386     int     valid;
00387     FLOAT   value;
00388 };
00389 
00390 typedef struct calc_noise_cache calc_noise_cache_t;
00391 
00392 
00393 static  uint8_t
00394 tri_calc_sfb_noise_x34(const FLOAT * xr, const FLOAT * xr34, FLOAT l3_xmin, unsigned int bw,
00395                        uint8_t sf, calc_noise_cache_t * did_it)
00396 {
00397     if (did_it[sf].valid == 0) {
00398         did_it[sf].valid = 1;
00399         did_it[sf].value = calc_sfb_noise_x34(xr, xr34, bw, sf);
00400     }
00401     if (l3_xmin < did_it[sf].value) {
00402         return 1;
00403     }
00404     if (sf < 255) {
00405         uint8_t const sf_x = sf + 1;
00406         if (did_it[sf_x].valid == 0) {
00407             did_it[sf_x].valid = 1;
00408             did_it[sf_x].value = calc_sfb_noise_x34(xr, xr34, bw, sf_x);
00409         }
00410         if (l3_xmin < did_it[sf_x].value) {
00411             return 1;
00412         }
00413     }
00414     if (sf > 0) {
00415         uint8_t const sf_x = sf - 1;
00416         if (did_it[sf_x].valid == 0) {
00417             did_it[sf_x].valid = 1;
00418             did_it[sf_x].value = calc_sfb_noise_x34(xr, xr34, bw, sf_x);
00419         }
00420         if (l3_xmin < did_it[sf_x].value) {
00421             return 1;
00422         }
00423     }
00424     return 0;
00425 }
00426 
00427 
00428 static  uint8_t
00429 tri_calc_sfb_noise_ISO(const FLOAT * xr, const FLOAT * xr34, FLOAT l3_xmin, unsigned int bw,
00430                        uint8_t sf, calc_noise_cache_t * did_it)
00431 {
00432     if (did_it[sf].valid == 0) {
00433         did_it[sf].valid = 1;
00434         did_it[sf].value = calc_sfb_noise_ISO(xr, xr34, bw, sf);
00435     }
00436     if (l3_xmin < did_it[sf].value) {
00437         return 1;
00438     }
00439     if (sf < 255) {
00440         uint8_t const sf_x = sf + 1;
00441         if (did_it[sf_x].valid == 0) {
00442             did_it[sf_x].valid = 1;
00443             did_it[sf_x].value = calc_sfb_noise_ISO(xr, xr34, bw, sf_x);
00444         }
00445         if (l3_xmin < did_it[sf_x].value) {
00446             return 1;
00447         }
00448     }
00449     if (sf > 0) {
00450         uint8_t const sf_x = sf - 1;
00451         if (did_it[sf_x].valid == 0) {
00452             did_it[sf_x].valid = 1;
00453             did_it[sf_x].value = calc_sfb_noise_ISO(xr, xr34, bw, sf_x);
00454         }
00455         if (l3_xmin < did_it[sf_x].value) {
00456             return 1;
00457         }
00458     }
00459     return 0;
00460 }
00461 
00462 
00463 
00464 /* the find_scalefac* routines calculate
00465  * a quantization step size which would
00466  * introduce as much noise as is allowed.
00467  * The larger the step size the more
00468  * quantization noise we'll get. The
00469  * scalefactors are there to lower the
00470  * global step size, allowing limited
00471  * differences in quantization step sizes
00472  * per band (shaping the noise).
00473  */
00474 
00475 static  uint8_t
00476 find_scalefac_x34(const FLOAT * xr, const FLOAT * xr34, FLOAT l3_xmin, unsigned int bw,
00477                   uint8_t sf_min)
00478 {
00479     calc_noise_cache_t did_it[256];
00480     uint8_t sf = 128, sf_ok = 255, delsf = 128, i;
00481     memset(did_it, 0, sizeof(did_it));
00482     for (i = 0; i < 8; ++i) {
00483         delsf >>= 1;
00484         if (sf <= sf_min) {
00485             sf += delsf;
00486         }
00487         else {
00488             uint8_t const bad = tri_calc_sfb_noise_x34(xr, xr34, l3_xmin, bw, sf, did_it);
00489             if (bad) {  /* distortion.  try a smaller scalefactor */
00490                 sf -= delsf;
00491             }
00492             else {
00493                 sf_ok = sf;
00494                 sf += delsf;
00495             }
00496         }
00497     }
00498     /*  returning a scalefac without distortion, if possible
00499      */
00500     return sf_ok;
00501 }
00502 
00503 
00504 
00505 static  uint8_t
00506 find_scalefac_ISO(const FLOAT * xr, const FLOAT * xr34, FLOAT l3_xmin, unsigned int bw,
00507                   uint8_t sf_min)
00508 {
00509     calc_noise_cache_t did_it[256];
00510     uint8_t sf = 128, sf_ok = 255, delsf = 128, i;
00511     memset(did_it, 0, sizeof(did_it));
00512     for (i = 0; i < 8; ++i) {
00513         delsf >>= 1;
00514         if (sf <= sf_min) {
00515             sf += delsf;
00516         }
00517         else {
00518             uint8_t const bad = tri_calc_sfb_noise_ISO(xr, xr34, l3_xmin, bw, sf, did_it);
00519             if (bad) {  /* distortion.  try a smaller scalefactor */
00520                 sf -= delsf;
00521             }
00522             else {
00523                 sf_ok = sf;
00524                 sf += delsf;
00525             }
00526         }
00527     }
00528     /*  returning a scalefac without distortion, if possible
00529      */
00530     return sf_ok;
00531 }
00532 
00533 
00534 
00535 /***********************************************************************
00536  *
00537  *      calc_short_block_vbr_sf()
00538  *      calc_long_block_vbr_sf()
00539  *
00540  *  Mark Taylor 2000-??-??
00541  *  Robert Hegemann 2000-10-25 made functions of it
00542  *
00543  ***********************************************************************/
00544 
00545 /* a variation for vbr-mtrh */
00546 static int
00547 block_sf(algo_t * that, const FLOAT l3_xmin[SFBMAX], int vbrsf[SFBMAX], int vbrsfmin[SFBMAX])
00548 {
00549     FLOAT   max_xr34;
00550     const FLOAT *const xr = &that->cod_info->xr[0];
00551     const FLOAT *const xr34_orig = &that->xr34orig[0];
00552     const int *const width = &that->cod_info->width[0];
00553     unsigned int const max_nonzero_coeff = (unsigned int) that->cod_info->max_nonzero_coeff;
00554     uint8_t maxsf = 0;
00555     int     sfb = 0;
00556     unsigned int j = 0, i = 0;
00557     int const psymax = that->cod_info->psymax;
00558 
00559     assert(that->cod_info->max_nonzero_coeff >= 0);
00560 
00561     that->mingain_l = 0;
00562     that->mingain_s[0] = 0;
00563     that->mingain_s[1] = 0;
00564     that->mingain_s[2] = 0;
00565     while (j <= max_nonzero_coeff) {
00566         unsigned int const w = (unsigned int) width[sfb];
00567         unsigned int const m = (unsigned int) (max_nonzero_coeff - j + 1);
00568         unsigned int l = w;
00569         uint8_t m1, m2;
00570         if (l > m) {
00571             l = m;
00572         }
00573         max_xr34 = max_x34(&xr34_orig[j], l);
00574 
00575         m1 = find_lowest_scalefac(max_xr34);
00576         vbrsfmin[sfb] = m1;
00577         if (that->mingain_l < m1) {
00578             that->mingain_l = m1;
00579         }
00580         if (that->mingain_s[i] < m1) {
00581             that->mingain_s[i] = m1;
00582         }
00583         if (++i > 2) {
00584             i = 0;
00585         }
00586         if (sfb < psymax) {
00587             m2 = that->find(&xr[j], &xr34_orig[j], l3_xmin[sfb], l, m1);
00588             if (maxsf < m2) {
00589                 maxsf = m2;
00590             }
00591         }
00592         else {
00593             if (maxsf < m1) {
00594                 maxsf = m1;
00595             }
00596             m2 = maxsf;
00597         }
00598         vbrsf[sfb] = m2;
00599         ++sfb;
00600         j += w;
00601     }
00602     for (; sfb < SFBMAX; ++sfb) {
00603         vbrsf[sfb] = maxsf;
00604         vbrsfmin[sfb] = 0;
00605     }
00606     return maxsf;
00607 }
00608 
00609 
00610 
00611 /***********************************************************************
00612  *
00613  *  quantize xr34 based on scalefactors
00614  *
00615  *  block_xr34
00616  *
00617  *  Mark Taylor 2000-??-??
00618  *  Robert Hegemann 2000-10-20 made functions of them
00619  *
00620  ***********************************************************************/
00621 
00622 static void
00623 quantize_x34(const algo_t * that)
00624 {
00625     DOUBLEX x[4];
00626     const FLOAT *xr34_orig = that->xr34orig;
00627     gr_info *const cod_info = that->cod_info;
00628     int const ifqstep = (cod_info->scalefac_scale == 0) ? 2 : 4;
00629     int    *l3 = cod_info->l3_enc;
00630     unsigned int j = 0, sfb = 0;
00631     unsigned int const max_nonzero_coeff = (unsigned int) cod_info->max_nonzero_coeff;
00632 
00633     assert(cod_info->max_nonzero_coeff >= 0);
00634     assert(cod_info->max_nonzero_coeff < 576);
00635 
00636     while (j <= max_nonzero_coeff) {
00637         int const s =
00638             (cod_info->scalefac[sfb] + (cod_info->preflag ? pretab[sfb] : 0)) * ifqstep
00639             + cod_info->subblock_gain[cod_info->window[sfb]] * 8;
00640         uint8_t const sfac = (uint8_t) (cod_info->global_gain - s);
00641         FLOAT const sfpow34 = ipow20[sfac];
00642         unsigned int const w = (unsigned int) cod_info->width[sfb];
00643         unsigned int const m = (unsigned int) (max_nonzero_coeff - j + 1);
00644         unsigned int l = w;
00645         unsigned int remaining;
00646 
00647         assert((cod_info->global_gain - s) >= 0);
00648         assert(cod_info->width[sfb] >= 0);
00649 
00650         if (l > m) {
00651             l = m;
00652         }
00653         j += w;
00654         ++sfb;
00655         l >>= 1;
00656         remaining = (l & 1);
00657 
00658         for (l >>= 1; l > 0; --l) {
00659             x[0] = sfpow34 * xr34_orig[0];
00660             x[1] = sfpow34 * xr34_orig[1];
00661             x[2] = sfpow34 * xr34_orig[2];
00662             x[3] = sfpow34 * xr34_orig[3];
00663 
00664             k_34_4(x, l3);
00665 
00666             l3 += 4;
00667             xr34_orig += 4;
00668         }
00669         if (remaining) {
00670             x[0] = sfpow34 * xr34_orig[0];
00671             x[1] = sfpow34 * xr34_orig[1];
00672 
00673             k_34_2(x, l3);
00674 
00675             l3 += 2;
00676             xr34_orig += 2;
00677         }
00678     }
00679 }
00680 
00681 
00682 
00683 static void
00684 quantize_ISO(const algo_t * that)
00685 {
00686     DOUBLEX x[4];
00687     const FLOAT *xr34_orig = that->xr34orig;
00688     gr_info *const cod_info = that->cod_info;
00689     int const ifqstep = (cod_info->scalefac_scale == 0) ? 2 : 4;
00690     int    *l3 = cod_info->l3_enc;
00691     unsigned int j = 0, sfb = 0;
00692     unsigned int const max_nonzero_coeff = (unsigned int) cod_info->max_nonzero_coeff;
00693 
00694     assert(cod_info->max_nonzero_coeff >= 0);
00695     assert(cod_info->max_nonzero_coeff < 576);
00696 
00697     while (j <= max_nonzero_coeff) {
00698         int const s =
00699             (cod_info->scalefac[sfb] + (cod_info->preflag ? pretab[sfb] : 0)) * ifqstep
00700             + cod_info->subblock_gain[cod_info->window[sfb]] * 8;
00701         uint8_t const sfac = (uint8_t) (cod_info->global_gain - s);
00702         FLOAT const sfpow34 = ipow20[sfac];
00703         unsigned int const w = (unsigned int) cod_info->width[sfb];
00704         unsigned int const m = (unsigned int) (max_nonzero_coeff - j + 1);
00705         unsigned int l = w;
00706         unsigned int remaining;
00707 
00708         assert((cod_info->global_gain - s) >= 0);
00709         assert(cod_info->width[sfb] >= 0);
00710 
00711         if (l > m) {
00712             l = m;
00713         }
00714         j += w;
00715         ++sfb;
00716         l >>= 1;
00717         remaining = (l & 1);
00718 
00719         for (l >>= 1; l > 0; --l) {
00720             x[0] = sfpow34 * xr34_orig[0];
00721             x[1] = sfpow34 * xr34_orig[1];
00722             x[2] = sfpow34 * xr34_orig[2];
00723             x[3] = sfpow34 * xr34_orig[3];
00724 
00725             k_iso_4(x, l3);
00726 
00727             l3 += 4;
00728             xr34_orig += 4;
00729         }
00730         if (remaining) {
00731             x[0] = sfpow34 * xr34_orig[0];
00732             x[1] = sfpow34 * xr34_orig[1];
00733 
00734             k_iso_2(x, l3);
00735 
00736             l3 += 2;
00737             xr34_orig += 2;
00738         }
00739     }
00740 }
00741 
00742 
00743 
00744 
00745 static const uint8_t max_range_short[SBMAX_s * 3] = {
00746     15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
00747     7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
00748     0, 0, 0
00749 };
00750 
00751 static const uint8_t max_range_long[SBMAX_l] = {
00752     15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0
00753 };
00754 
00755 static const uint8_t max_range_long_lsf_pretab[SBMAX_l] = {
00756     7, 7, 7, 7, 7, 7, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00757 };
00758 
00759 
00760 
00761 /*
00762     sfb=0..5  scalefac < 16
00763     sfb>5     scalefac < 8
00764 
00765     ifqstep = ( cod_info->scalefac_scale == 0 ) ? 2 : 4;
00766     ol_sf =  (cod_info->global_gain-210.0);
00767     ol_sf -= 8*cod_info->subblock_gain[i];
00768     ol_sf -= ifqstep*scalefac[gr][ch].s[sfb][i];
00769 */
00770 
00771 static void
00772 set_subblock_gain(gr_info * cod_info, const int mingain_s[3], int sf[])
00773 {
00774     const int maxrange1 = 15, maxrange2 = 7;
00775     const int ifqstepShift = (cod_info->scalefac_scale == 0) ? 1 : 2;
00776     int    *const sbg = cod_info->subblock_gain;
00777     unsigned int const psymax = (unsigned int) cod_info->psymax;
00778     unsigned int psydiv = 18;
00779     int     sbg0, sbg1, sbg2;
00780     unsigned int sfb, i;
00781 
00782     if (psydiv > psymax) {
00783         psydiv = psymax;
00784     }
00785     for (i = 0; i < 3; ++i) {
00786         int     maxsf1 = 0, maxsf2 = 0, minsf = 1000;
00787         /* see if we should use subblock gain */
00788         for (sfb = i; sfb < psydiv; sfb += 3) { /* part 1 */
00789             int const v = -sf[sfb];
00790             if (maxsf1 < v) {
00791                 maxsf1 = v;
00792             }
00793             if (minsf > v) {
00794                 minsf = v;
00795             }
00796         }
00797         for (; sfb < SFBMAX; sfb += 3) { /* part 2 */
00798             int const v = -sf[sfb];
00799             if (maxsf2 < v) {
00800                 maxsf2 = v;
00801             }
00802             if (minsf > v) {
00803                 minsf = v;
00804             }
00805         }
00806 
00807         /* boost subblock gain as little as possible so we can
00808          * reach maxsf1 with scalefactors
00809          * 8*sbg >= maxsf1
00810          */
00811         {
00812             int const m1 = maxsf1 - (maxrange1 << ifqstepShift);
00813             int const m2 = maxsf2 - (maxrange2 << ifqstepShift);
00814 
00815             maxsf1 = Max(m1, m2);
00816         }
00817         if (minsf > 0) {
00818             sbg[i] = minsf >> 3;
00819         }
00820         else {
00821             sbg[i] = 0;
00822         }
00823         if (maxsf1 > 0) {
00824             int const m1 = sbg[i];
00825             int const m2 = (maxsf1 + 7) >> 3;
00826             sbg[i] = Max(m1, m2);
00827         }
00828         if (sbg[i] > 0 && mingain_s[i] > (cod_info->global_gain - sbg[i] * 8)) {
00829             sbg[i] = (cod_info->global_gain - mingain_s[i]) >> 3;
00830         }
00831         if (sbg[i] > 7) {
00832             sbg[i] = 7;
00833         }
00834     }
00835     sbg0 = sbg[0] * 8;
00836     sbg1 = sbg[1] * 8;
00837     sbg2 = sbg[2] * 8;
00838     for (sfb = 0; sfb < SFBMAX; sfb += 3) {
00839         sf[sfb + 0] += sbg0;
00840         sf[sfb + 1] += sbg1;
00841         sf[sfb + 2] += sbg2;
00842     }
00843 }
00844 
00845 
00846 
00847 /*
00848           ifqstep = ( cod_info->scalefac_scale == 0 ) ? 2 : 4;
00849           ol_sf =  (cod_info->global_gain-210.0);
00850           ol_sf -= ifqstep*scalefac[gr][ch].l[sfb];
00851           if (cod_info->preflag && sfb>=11)
00852           ol_sf -= ifqstep*pretab[sfb];
00853 */
00854 static void
00855 set_scalefacs(gr_info * cod_info, const int *vbrsfmin, int sf[], const uint8_t * max_range)
00856 {
00857     const int ifqstep = (cod_info->scalefac_scale == 0) ? 2 : 4;
00858     const int ifqstepShift = (cod_info->scalefac_scale == 0) ? 1 : 2;
00859     int    *const scalefac = cod_info->scalefac;
00860     int const sfbmax = cod_info->sfbmax;
00861     int     sfb;
00862     int const *const sbg = cod_info->subblock_gain;
00863     int const *const window = cod_info->window;
00864     int const preflag = cod_info->preflag;
00865 
00866     if (preflag) {
00867         for (sfb = 11; sfb < sfbmax; ++sfb) {
00868             sf[sfb] += pretab[sfb] * ifqstep;
00869         }
00870     }
00871     for (sfb = 0; sfb < sfbmax; ++sfb) {
00872         int const gain = cod_info->global_gain - (sbg[window[sfb]] * 8)
00873             - ((preflag ? pretab[sfb] : 0) * ifqstep);
00874 
00875         if (sf[sfb] < 0) {
00876             int const m = gain - vbrsfmin[sfb];
00877             /* ifqstep*scalefac >= -sf[sfb], so round UP */
00878             scalefac[sfb] = (ifqstep - 1 - sf[sfb]) >> ifqstepShift;
00879 
00880             if (scalefac[sfb] > max_range[sfb]) {
00881                 scalefac[sfb] = max_range[sfb];
00882             }
00883             if (scalefac[sfb] > 0 && (scalefac[sfb] << ifqstepShift) > m) {
00884                 scalefac[sfb] = m >> ifqstepShift;
00885             }
00886         }
00887         else {
00888             scalefac[sfb] = 0;
00889         }
00890     }
00891     for (; sfb < SFBMAX; ++sfb) {
00892         scalefac[sfb] = 0; /* sfb21 */
00893     }
00894 }
00895 
00896 
00897 
00898 static int
00899 checkScalefactor(const gr_info * cod_info, const int vbrsfmin[SFBMAX])
00900 {
00901     int const ifqstep = cod_info->scalefac_scale == 0 ? 2 : 4;
00902     int     sfb;
00903     for (sfb = 0; sfb < cod_info->psymax; ++sfb) {
00904         const int s =
00905             ((cod_info->scalefac[sfb] +
00906               (cod_info->preflag ? pretab[sfb] : 0)) * ifqstep) +
00907             cod_info->subblock_gain[cod_info->window[sfb]] * 8;
00908 
00909         if ((cod_info->global_gain - s) < vbrsfmin[sfb]) {
00910             /*
00911                fprintf( stdout, "sf %d\n", sfb );
00912                fprintf( stdout, "min %d\n", vbrsfmin[sfb] );
00913                fprintf( stdout, "ggain %d\n", cod_info->global_gain );
00914                fprintf( stdout, "scalefac %d\n", cod_info->scalefac[sfb] );
00915                fprintf( stdout, "pretab %d\n", (cod_info->preflag ? pretab[sfb] : 0) );
00916                fprintf( stdout, "scale %d\n", (cod_info->scalefac_scale + 1) );
00917                fprintf( stdout, "subgain %d\n", cod_info->subblock_gain[cod_info->window[sfb]] * 8 );
00918                fflush( stdout );
00919                exit(-1);
00920              */
00921             return 0;
00922         }
00923     }
00924     return 1;
00925 }
00926 
00927 
00928 
00929 /******************************************************************
00930  *
00931  *  short block scalefacs
00932  *
00933  ******************************************************************/
00934 
00935 static int
00936 short_block_constrain(const algo_t * that, int vbrsf[SFBMAX],
00937                       const int vbrsfmin[SFBMAX], int vbrmax)
00938 {
00939     gr_info *const cod_info = that->cod_info;
00940     lame_internal_flags const *const gfc = that->gfc;
00941     int const maxminsfb = that->mingain_l;
00942     int     mover, maxover0 = 0, maxover1 = 0, delta = 0;
00943     int     v, v0, v1;
00944     int     sfb;
00945     int const psymax = cod_info->psymax;
00946 
00947     for (sfb = 0; sfb < psymax; ++sfb) {
00948         assert(vbrsf[sfb] >= vbrsfmin[sfb]);
00949         v = vbrmax - vbrsf[sfb];
00950         if (delta < v) {
00951             delta = v;
00952         }
00953         v0 = v - (4 * 14 + 2 * max_range_short[sfb]);
00954         v1 = v - (4 * 14 + 4 * max_range_short[sfb]);
00955         if (maxover0 < v0) {
00956             maxover0 = v0;
00957         }
00958         if (maxover1 < v1) {
00959             maxover1 = v1;
00960         }
00961     }
00962     if (gfc->noise_shaping == 2) {
00963         /* allow scalefac_scale=1 */
00964         mover = Min(maxover0, maxover1);
00965     }
00966     else {
00967         mover = maxover0;
00968     }
00969     if (delta > mover) {
00970         delta = mover;
00971     }
00972     vbrmax -= delta;
00973     maxover0 -= mover;
00974     maxover1 -= mover;
00975 
00976     if (maxover0 == 0) {
00977         cod_info->scalefac_scale = 0;
00978     }
00979     else if (maxover1 == 0) {
00980         cod_info->scalefac_scale = 1;
00981     }
00982     if (vbrmax < maxminsfb) {
00983         vbrmax = maxminsfb;
00984     }
00985     cod_info->global_gain = vbrmax;
00986 
00987     if (cod_info->global_gain < 0) {
00988         cod_info->global_gain = 0;
00989     }
00990     else if (cod_info->global_gain > 255) {
00991         cod_info->global_gain = 255;
00992     }
00993     for (sfb = 0; sfb < SFBMAX; ++sfb) {
00994         vbrsf[sfb] -= vbrmax;
00995     }
00996     set_subblock_gain(cod_info, &that->mingain_s[0], vbrsf);
00997     set_scalefacs(cod_info, vbrsfmin, vbrsf, max_range_short);
00998     assert(checkScalefactor(cod_info, vbrsfmin));
00999     return checkScalefactor(cod_info, vbrsfmin);
01000 }
01001 
01002 
01003 
01004 /******************************************************************
01005  *
01006  *  long block scalefacs
01007  *
01008  ******************************************************************/
01009 
01010 static int
01011 long_block_constrain(const algo_t * that, int vbrsf[SFBMAX], const int vbrsfmin[SFBMAX], int vbrmax)
01012 {
01013     gr_info *const cod_info = that->cod_info;
01014     lame_internal_flags const *const gfc = that->gfc;
01015     uint8_t const *max_rangep;
01016     int const maxminsfb = that->mingain_l;
01017     int     sfb;
01018     int     maxover0, maxover1, maxover0p, maxover1p, mover, delta = 0;
01019     int     v, v0, v1, v0p, v1p, vm0p = 1, vm1p = 1;
01020     int const psymax = cod_info->psymax;
01021 
01022     max_rangep = gfc->mode_gr == 2 ? max_range_long : max_range_long_lsf_pretab;
01023 
01024     maxover0 = 0;
01025     maxover1 = 0;
01026     maxover0p = 0;      /* pretab */
01027     maxover1p = 0;      /* pretab */
01028 
01029     for (sfb = 0; sfb < psymax; ++sfb) {
01030         assert(vbrsf[sfb] >= vbrsfmin[sfb]);
01031         v = vbrmax - vbrsf[sfb];
01032         if (delta < v) {
01033             delta = v;
01034         }
01035         v0 = v - 2 * max_range_long[sfb];
01036         v1 = v - 4 * max_range_long[sfb];
01037         v0p = v - 2 * (max_rangep[sfb] + pretab[sfb]);
01038         v1p = v - 4 * (max_rangep[sfb] + pretab[sfb]);
01039         if (maxover0 < v0) {
01040             maxover0 = v0;
01041         }
01042         if (maxover1 < v1) {
01043             maxover1 = v1;
01044         }
01045         if (maxover0p < v0p) {
01046             maxover0p = v0p;
01047         }
01048         if (maxover1p < v1p) {
01049             maxover1p = v1p;
01050         }
01051     }
01052     if (vm0p == 1) {
01053         int     gain = vbrmax - maxover0p;
01054         if (gain < maxminsfb) {
01055             gain = maxminsfb;
01056         }
01057         for (sfb = 0; sfb < psymax; ++sfb) {
01058             int const a = (gain - vbrsfmin[sfb]) - 2 * pretab[sfb];
01059             if (a <= 0) {
01060                 vm0p = 0;
01061                 vm1p = 0;
01062                 break;
01063             }
01064         }
01065     }
01066     if (vm1p == 1) {
01067         int     gain = vbrmax - maxover1p;
01068         if (gain < maxminsfb) {
01069             gain = maxminsfb;
01070         }
01071         for (sfb = 0; sfb < psymax; ++sfb) {
01072             int const b = (gain - vbrsfmin[sfb]) - 4 * pretab[sfb];
01073             if (b <= 0) {
01074                 vm1p = 0;
01075                 break;
01076             }
01077         }
01078     }
01079     if (vm0p == 0) {
01080         maxover0p = maxover0;
01081     }
01082     if (vm1p == 0) {
01083         maxover1p = maxover1;
01084     }
01085     if (gfc->noise_shaping != 2) {
01086         maxover1 = maxover0;
01087         maxover1p = maxover0p;
01088     }
01089     mover = Min(maxover0, maxover0p);
01090     mover = Min(mover, maxover1);
01091     mover = Min(mover, maxover1p);
01092 
01093     if (delta > mover) {
01094         delta = mover;
01095     }
01096     vbrmax -= delta;
01097     if (vbrmax < maxminsfb) {
01098         vbrmax = maxminsfb;
01099     }
01100     maxover0 -= mover;
01101     maxover0p -= mover;
01102     maxover1 -= mover;
01103     maxover1p -= mover;
01104 
01105     if (maxover0 == 0) {
01106         cod_info->scalefac_scale = 0;
01107         cod_info->preflag = 0;
01108         max_rangep = max_range_long;
01109     }
01110     else if (maxover0p == 0) {
01111         cod_info->scalefac_scale = 0;
01112         cod_info->preflag = 1;
01113     }
01114     else if (maxover1 == 0) {
01115         cod_info->scalefac_scale = 1;
01116         cod_info->preflag = 0;
01117         max_rangep = max_range_long;
01118     }
01119     else if (maxover1p == 0) {
01120         cod_info->scalefac_scale = 1;
01121         cod_info->preflag = 1;
01122     }
01123     else {
01124         assert(0);      /* this should not happen */
01125     }
01126     cod_info->global_gain = vbrmax;
01127     if (cod_info->global_gain < 0) {
01128         cod_info->global_gain = 0;
01129     }
01130     else if (cod_info->global_gain > 255) {
01131         cod_info->global_gain = 255;
01132     }
01133     for (sfb = 0; sfb < SFBMAX; ++sfb) {
01134         vbrsf[sfb] -= vbrmax;
01135     }
01136     set_scalefacs(cod_info, vbrsfmin, vbrsf, max_rangep);
01137     assert(checkScalefactor(cod_info, vbrsfmin));
01138     return checkScalefactor(cod_info, vbrsfmin);
01139 }
01140 
01141 
01142 
01143 static int
01144 bitcount(const algo_t * that)
01145 {
01146     if (that->gfc->mode_gr == 2) {
01147         return scale_bitcount(that->cod_info);
01148     }
01149     else {
01150         return scale_bitcount_lsf(that->gfc, that->cod_info);
01151     }
01152 }
01153 
01154 
01155 
01156 static int
01157 quantizeAndCountBits(const algo_t * that)
01158 {
01159     that->quantize(that);
01160     that->cod_info->part2_3_length = noquant_count_bits(that->gfc, that->cod_info, 0);
01161     return that->cod_info->part2_3_length;
01162 }
01163 
01164 
01165 #if 0
01166 static int
01167 tryScalefacColor(const algo_t * that, int vbrsf[SFBMAX],
01168                  const int vbrsf2[SFBMAX], const int vbrsfmin[SFBMAX], int I, int M, int target)
01169 {
01170     FLOAT const xrpow_max = that->cod_info->xrpow_max;
01171     int     i, nbits;
01172     int     gain, vbrmax = 0;
01173 
01174     for (i = 0; i < SFBMAX; ++i) {
01175         gain = target + (vbrsf2[i] - target) * I / M;
01176         if (gain < vbrsfmin[i]) {
01177             gain = vbrsfmin[i];
01178         }
01179         if (gain > 255) {
01180             gain = 255;
01181         }
01182         if (vbrmax < gain) {
01183             vbrmax = gain;
01184         }
01185         vbrsf[i] = gain;
01186     }
01187     if (!that->alloc(that, vbrsf, vbrsfmin, vbrmax)) {
01188         return LARGE_BITS;
01189     }
01190     (void) bitcount(that);
01191     nbits = quantizeAndCountBits(that);
01192     that->cod_info->xrpow_max = xrpow_max;
01193     return nbits;
01194 }
01195 
01196 
01197 
01198 static void
01199 searchScalefacColorMax(const algo_t that_[2], int sfwork[2][SFBMAX],
01200                        const int sfcalc[2][SFBMAX], const int vbrsfmin[2][SFBMAX], int maxbits[2])
01201 {
01202     int     M[2], vbrmin[2], vbrmax[2];
01203     int     ch;
01204     int     max_bits = 0;
01205     int const nch = that_[0].gfc->channels_out;
01206     int     last[2], i[2], ok[2], l[2], r[2], looping = 0;
01207 
01208     for (ch = 0; ch < nch; ch++) {
01209         algo_t const *that = &that_[ch];
01210 
01211         if (maxbits[ch] != 0) {
01212             int const psymax = that->cod_info->psymax;
01213             int     j;
01214             vbrmin[ch] = 255;
01215             vbrmax[ch] = 0;
01216             for (j = 0; j < psymax; ++j) {
01217                 if (vbrmin[ch] > sfcalc[ch][j]) {
01218                     vbrmin[ch] = sfcalc[ch][j];
01219                 }
01220                 if (vbrmax[ch] < sfcalc[ch][j]) {
01221                     vbrmax[ch] = sfcalc[ch][j];
01222                 }
01223             }
01224             M[ch] = vbrmax[ch] - vbrmin[ch];
01225             max_bits += maxbits[ch];
01226         }
01227     }
01228 
01229     for (ch = 0; ch < nch; ch++) {
01230         if (maxbits[ch] != 0) {
01231             ok[ch] = -1;
01232             l[ch] = 0;
01233             r[ch] = M[ch];
01234             last[ch] = i[ch] = M[ch] / 2;
01235             if (l[ch] <= r[ch]) {
01236                 looping += 1 + ch;
01237             }
01238         }
01239     }
01240     while (looping > 0) {
01241         int     nbits = 0;
01242         looping = 0;
01243         for (ch = 0; ch < nch; ch++) {
01244             if (maxbits[ch] != 0) {
01245                 if (M[ch] == 0) {
01246                     continue;
01247                 }
01248                 nbits +=
01249                     tryScalefacColor(&that_[ch], sfwork[ch], sfcalc[ch], vbrsfmin[ch], i[ch], M[ch],
01250                                      vbrmax[ch]);
01251             }
01252         }
01253         for (ch = 0; ch < nch; ch++) {
01254             if (maxbits[ch] != 0) {
01255                 if (nbits < max_bits) {
01256                     ok[ch] = i[ch];
01257                     l[ch] = i[ch] + 1;
01258                     if (l[ch] <= r[ch]) {
01259                         looping += 1 + ch;
01260                     }
01261                     else {
01262                         l[ch] = r[ch];
01263                     }
01264                 }
01265                 else {
01266                     r[ch] = i[ch] - 1;
01267                     if (l[ch] <= r[ch]) {
01268                         looping += 1 + ch;
01269                     }
01270                     else {
01271                         r[ch] = l[ch];
01272                     }
01273                 }
01274                 last[ch] = i[ch];
01275                 i[ch] = (l[ch] + r[ch]) / 2;
01276             }
01277         }
01278     }
01279     for (ch = 0; ch < nch; ch++) {
01280         if (maxbits[ch] != 0) {
01281             if (M[ch] == 0) {
01282                 continue;
01283             }
01284             if (last[ch] != ok[ch]) {
01285                 if (ok[ch] == -1) {
01286                     ok[ch] = 0;
01287                 }
01288                 tryScalefacColor(&that_[ch], sfwork[ch], sfcalc[ch], vbrsfmin[ch], ok[ch], M[ch],
01289                                  vbrmax[ch]);
01290             }
01291         }
01292     }
01293 }
01294 #endif
01295 
01296 
01297 
01298 static int
01299 tryGlobalStepsize(const algo_t * that, const int sfwork[SFBMAX],
01300                   const int vbrsfmin[SFBMAX], int delta)
01301 {
01302     FLOAT const xrpow_max = that->cod_info->xrpow_max;
01303     int     sftemp[SFBMAX], i, nbits;
01304     int     gain, vbrmax = 0;
01305     for (i = 0; i < SFBMAX; ++i) {
01306         gain = sfwork[i] + delta;
01307         if (gain < vbrsfmin[i]) {
01308             gain = vbrsfmin[i];
01309         }
01310         if (gain > 255) {
01311             gain = 255;
01312         }
01313         if (vbrmax < gain) {
01314             vbrmax = gain;
01315         }
01316         sftemp[i] = gain;
01317     }
01318     if (!that->alloc(that, sftemp, vbrsfmin, vbrmax)) {
01319         return LARGE_BITS;
01320     }
01321     (void) bitcount(that);
01322     nbits = quantizeAndCountBits(that);
01323     that->cod_info->xrpow_max = xrpow_max;
01324     return nbits;
01325 }
01326 
01327 
01328 
01329 static void
01330 searchGlobalStepsizeMax(const algo_t * that, const int sfwork[SFBMAX],
01331                         const int vbrsfmin[SFBMAX], int target)
01332 {
01333     gr_info const *const cod_info = that->cod_info;
01334     const int gain = cod_info->global_gain;
01335     int     curr = gain;
01336     int     gain_ok = 1024;
01337     int     nbits = LARGE_BITS;
01338     int     l = gain, r = 512;
01339 
01340     assert(gain >= 0);
01341     while (l <= r) {
01342         curr = (l + r) >> 1;
01343         nbits = tryGlobalStepsize(that, sfwork, vbrsfmin, curr - gain);
01344         if (nbits == 0 || (nbits + cod_info->part2_length) < target) {
01345             r = curr - 1;
01346             gain_ok = curr;
01347         }
01348         else {
01349             l = curr + 1;
01350             if (gain_ok == 1024) {
01351                 gain_ok = curr;
01352             }
01353         }
01354     }
01355     if (gain_ok != curr) {
01356         curr = gain_ok;
01357         nbits = tryGlobalStepsize(that, sfwork, vbrsfmin, curr - gain);
01358     }
01359 }
01360 
01361 
01362 
01363 static void
01364 reduce_bit_usage(lame_internal_flags * gfc, int gr, int ch
01365 #if 0
01366                  , const FLOAT xr34orig[576], const FLOAT l3_xmin[SFBMAX], int maxbits
01367 #endif
01368     )
01369 {
01370     gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
01371     /*  try some better scalefac storage
01372      */
01373     best_scalefac_store(gfc, gr, ch, &gfc->l3_side);
01374 
01375     /*  best huffman_divide may save some bits too
01376      */
01377     if (gfc->use_best_huffman == 1)
01378         best_huffman_divide(gfc, cod_info);
01379 #if 0
01380     /* truncate small spectrum seems to introduce pops, disabled(RH 050918) */
01381     if (gfc->substep_shaping & 1) {
01382         trancate_smallspectrums(gfc, cod_info, l3_xmin, xr34orig);
01383     }
01384     else if (cod_info->part2_3_length > maxbits - cod_info->part2_length) {
01385         trancate_smallspectrums(gfc, cod_info, l3_xmin, xr34orig);
01386     }
01387 #endif
01388 }
01389 
01390 #if 0
01391 static int frame_num = -1;
01392 static void
01393 debug_bit_usage(algo_t that[2][2], int maxbits[2][2], char *s)
01394 {
01395     lame_internal_flags* gfc = that[0][0].gfc;
01396     int     ngr, nch;
01397     int     gr, ch;
01398     int     fr_sum = 0, fr_max = 0;
01399 
01400     ngr = gfc->mode_gr;
01401     nch = gfc->channels_out;
01402     DEBUGF(gfc, "%4d %s\t", frame_num, s);
01403     for (gr = 0; gr < ngr; ++gr) {
01404         int     gr_sum = 0, gr_max = 0;
01405         for (ch = 0; ch < nch; ++ch) {
01406             int     use = 0;
01407             if (maxbits[gr][ch] > 0) {
01408                 gr_max += maxbits[gr][ch];
01409                 use += that[gr][ch].cod_info->part2_3_length + that[gr][ch].cod_info->part2_length;
01410                 gr_sum += use;
01411             }
01412             DEBUGF(gfc, "<%d%d> %4d %c%4d\t", gr, ch, maxbits[gr][ch],
01413                 maxbits[gr][ch] < use ? '<' : ' ', use);
01414         }
01415         DEBUGF(gfc, "max%4d%csum=%4d\t", gr_max, gr_max < gr_sum ? '<' : ' ', gr_sum);
01416     }
01417     DEBUGF(gfc, "fr_max=%4d%cfr_sum=%4d\n", fr_max, fr_max < fr_sum ? '<' : ' ', fr_sum);
01418 }
01419 #endif
01420 
01421 /************************************************************************
01422  *
01423  *  VBR_noise_shaping()
01424  *
01425  *  may result in a need of too many bits, then do it CBR like
01426  *
01427  *  Robert Hegemann 2000-10-25
01428  *
01429  ***********************************************************************/
01430 #if 0
01431 static int
01432 VBR_noise_shaping(lame_internal_flags * gfc, const FLOAT xr34orig[2][576],
01433                   const FLOAT l3_xmin[2][SFBMAX], int const maxbits[2], int gr)
01434 {
01435     int     sfwork_[2][SFBMAX];
01436     int     sfcalc_[2][SFBMAX];
01437     int     vbrsfmin_[2][SFBMAX];
01438     algo_t  that_[2];
01439     int     ch;
01440     int     nbits, max_bits = 0;
01441 
01442 
01443 #if 0
01444     searchScalefacColorMax(that_, sfwork_, sfcalc_, vbrsfmin_, maxbits);
01445     nbits = 0;
01446     for (ch = 0; ch < gfc->channels_out; ch++) {
01447         if (maxbits[ch] != 0) {
01448             reduce_bit_usage(gfc, gr, ch /*, xr34orig, l3_xmin, maxbits */ );
01449             algo_t *that = &that_[ch];
01450             nbits += that->cod_info->part2_3_length + that->cod_info->part2_length;
01451         }
01452     }
01453     debug_bit_usage(that_, maxbits, "B");
01454     if (nbits <= max_bits) {
01455         return nbits;
01456     }
01457 #endif
01458     nbits = 0;
01459     for (ch = 0; ch < gfc->channels_out; ch++) {
01460         int    *sfwork = sfwork_[ch];
01461         int    *vbrsfmin = vbrsfmin_[ch];
01462         algo_t *that = &that_[ch];
01463         if (maxbits[ch] > 0) {
01464             searchGlobalStepsizeMax(that, sfwork, vbrsfmin, maxbits[ch]);
01465             reduce_bit_usage(gfc, gr, ch /*, xr34orig, l3_xmin, maxbits */ );
01466             nbits += that->cod_info->part2_3_length + that->cod_info->part2_length;
01467         }
01468     }                   /* for ch */
01469     /*debug_bit_usage(that_,maxbits,"C"); */
01470     if (nbits <= max_bits /* && that->cod_info->global_gain < 256 */ ) {
01471         return nbits;
01472     }
01473     ERRORF(gfc, "INTERNAL ERROR IN VBR NEW CODE (1313), please send bug report\n"
01474            "maxbits=%d usedbits=%d\n", maxbits[ch], nbits);
01475     exit(-1);
01476 }
01477 #endif
01478 
01479 
01480 int
01481 VBR_encode_frame(lame_internal_flags * gfc, FLOAT const xr34orig[2][2][576],
01482                  FLOAT const l3_xmin[2][2][SFBMAX], int const max_bits[2][2])
01483 {
01484     int     sfwork_[2][2][SFBMAX];
01485     int     vbrsfmin_[2][2][SFBMAX];
01486     algo_t  that_[2][2];
01487     int const ngr = gfc->mode_gr;
01488     int const nch = gfc->channels_out;
01489     int     max_nbits_ch[2][2];
01490     int     max_nbits_gr[2];
01491     int     max_nbits_fr = 0;
01492     int     use_nbits_ch[2][2];
01493     int     use_nbits_gr[2];
01494     int     use_nbits_fr = 0;
01495     int     gr, ch;
01496     int     ok, sum_fr;
01497 /*++frame_num;*/
01498     /* set up some encoding parameters
01499      */
01500     for (gr = 0; gr < ngr; ++gr) {
01501         max_nbits_gr[gr] = 0;
01502         for (ch = 0; ch < nch; ++ch) {
01503             max_nbits_ch[gr][ch] = max_bits[gr][ch];
01504             use_nbits_ch[gr][ch] = 0;
01505             max_nbits_gr[gr] += max_bits[gr][ch];
01506             max_nbits_fr += max_bits[gr][ch];
01507             that_[gr][ch].gfc = gfc;
01508             that_[gr][ch].cod_info = &gfc->l3_side.tt[gr][ch];
01509             that_[gr][ch].xr34orig = xr34orig[gr][ch];
01510             if (gfc->quantization) {
01511                 that_[gr][ch].find = find_scalefac_x34;
01512                 that_[gr][ch].quantize = quantize_x34;
01513             }
01514             else {
01515                 that_[gr][ch].find = find_scalefac_ISO;
01516                 that_[gr][ch].quantize = quantize_ISO;
01517             }
01518             if (that_[gr][ch].cod_info->block_type == SHORT_TYPE) {
01519                 that_[gr][ch].alloc = short_block_constrain;
01520             }
01521             else {
01522                 that_[gr][ch].alloc = long_block_constrain;
01523             }
01524         }               /* for ch */
01525     }
01526 
01527     /* searches scalefactors
01528      */
01529     for (gr = 0; gr < ngr; ++gr) {
01530         for (ch = 0; ch < nch; ++ch) {
01531             if (max_bits[gr][ch] > 0) {
01532                 algo_t *that = &that_[gr][ch];
01533                 int    *sfwork = sfwork_[gr][ch];
01534                 int    *vbrsfmin = vbrsfmin_[gr][ch];
01535                 int     vbrmax;
01536 
01537                 vbrmax = block_sf(that, l3_xmin[gr][ch], sfwork, vbrsfmin);
01538                 (void) that->alloc(that, sfwork, vbrsfmin, vbrmax);
01539                 if (0 != bitcount(that)) {
01540                     /*  this should not happen due to the way the scalefactors are selected
01541                      */
01542                     ERRORF(gfc, "INTERNAL ERROR IN VBR NEW CODE (1319), please send bug report\n");
01543                     exit(-1);
01544                 }
01545             }
01546             else {
01547                 /*  xr contains no energy 
01548                  *  l3_enc, our encoding data, will be quantized to zero
01549                  *  continue with next channel
01550                  */
01551             }
01552         }               /* for ch */
01553     }
01554 
01555     /* encode 'as is'
01556      */
01557     use_nbits_fr = 0;
01558     for (gr = 0; gr < ngr; ++gr) {
01559         use_nbits_gr[gr] = 0;
01560         for (ch = 0; ch < nch; ++ch) {
01561             if (max_bits[gr][ch] > 0) {
01562                 algo_t *that = &that_[gr][ch];
01563                 unsigned int const max_nonzero_coeff =
01564                         (unsigned int) that->cod_info->max_nonzero_coeff;
01565 
01566                 assert(max_nonzero_coeff < 576);
01567                 memset(&that->cod_info->l3_enc[max_nonzero_coeff], 0,
01568                         (576u - max_nonzero_coeff) * sizeof(that->cod_info->l3_enc[0]));
01569 
01570                 (void) quantizeAndCountBits(that);
01571                 reduce_bit_usage(gfc, gr, ch);
01572                 use_nbits_ch[gr][ch] =
01573                     that->cod_info->part2_3_length + that->cod_info->part2_length;
01574                 use_nbits_gr[gr] += use_nbits_ch[gr][ch];
01575             }
01576             else {
01577                 /*  xr contains no energy 
01578                  *  l3_enc, our encoding data, will be quantized to zero
01579                  *  continue with next channel
01580                  */
01581             }
01582         }               /* for ch */
01583         use_nbits_fr += use_nbits_gr[gr];
01584     }
01585 
01586     /* check bit constrains
01587      */
01588     if (use_nbits_fr <= max_nbits_fr) {
01589         ok = 1;
01590         for (gr = 0; gr < ngr; ++gr) {
01591             if (use_nbits_gr[gr] > MAX_BITS_PER_GRANULE) {
01592                 /* violates the rule that every granule has to use no more
01593                  * bits than MAX_BITS_PER_GRANULE
01594                  */
01595                 ok = 0;
01596             }
01597             for (ch = 0; ch < nch; ++ch) {
01598                 if (use_nbits_ch[gr][ch] > MAX_BITS_PER_CHANNEL) {
01599                     /* violates the rule that every gr_ch has to use no more
01600                      * bits than MAX_BITS_PER_CHANNEL
01601                      *
01602                      * This isn't explicitly stated in the ISO docs, but the
01603                      * part2_3_length field has only 12 bits, that makes it
01604                      * up to a maximum size of 4095 bits!!!
01605                      */
01606                     ok = 0;
01607                 }
01608             }
01609         }
01610         if (ok) {
01611             /*debug_bit_usage(that_,max_nbits_ch,"+");*/
01612             return use_nbits_fr;
01613         }
01614     }
01615     /*debug_bit_usage(that_,max_nbits_ch,"?");*/
01616 
01617     /* OK, we are in trouble and have to define how many bits are
01618      * to be used for each granule
01619      */
01620     {
01621         ok = 1;
01622         sum_fr = 0;
01623 
01624         for (gr = 0; gr < ngr; ++gr) {
01625             max_nbits_gr[gr] = 0;
01626             for (ch = 0; ch < nch; ++ch) {
01627                 if (use_nbits_ch[gr][ch] > MAX_BITS_PER_CHANNEL) {
01628                     max_nbits_ch[gr][ch] = MAX_BITS_PER_CHANNEL;
01629                 }
01630                 else {
01631                     max_nbits_ch[gr][ch] = use_nbits_ch[gr][ch];
01632                 }
01633                 max_nbits_gr[gr] += max_nbits_ch[gr][ch];
01634             }
01635             if (max_nbits_gr[gr] > MAX_BITS_PER_GRANULE) {
01636 #if 1
01637                 float f[2], s = 0;
01638                 for (ch = 0; ch < nch; ++ch) {
01639                     if (max_nbits_ch[gr][ch] > 0) {
01640                         f[ch] = sqrt(sqrt(max_nbits_ch[gr][ch]));
01641                         s += f[ch];
01642                     }
01643                     else {
01644                         f[ch] = 0;
01645                     }
01646                 }
01647                 for (ch = 0; ch < nch; ++ch) {
01648                     if (s > 0) {
01649                         max_nbits_ch[gr][ch] = MAX_BITS_PER_GRANULE * f[ch]/s;
01650                     }
01651                     else {
01652                         max_nbits_ch[gr][ch] = 0;
01653                     }
01654                 }
01655                 if (nch > 1) {
01656                     if (max_nbits_ch[gr][0] > use_nbits_ch[gr][0]+32) {
01657                         max_nbits_ch[gr][1] += max_nbits_ch[gr][0];
01658                         max_nbits_ch[gr][1] -= use_nbits_ch[gr][0]+32;
01659                         max_nbits_ch[gr][0] = use_nbits_ch[gr][0]+32;
01660                     }
01661                     if (max_nbits_ch[gr][1] > use_nbits_ch[gr][1]+32) {
01662                         max_nbits_ch[gr][0] += max_nbits_ch[gr][1];
01663                         max_nbits_ch[gr][0] -= use_nbits_ch[gr][1]+32;
01664                         max_nbits_ch[gr][1] = use_nbits_ch[gr][1]+32;
01665                     }
01666                     if (max_nbits_ch[gr][0] > MAX_BITS_PER_CHANNEL) {
01667                         max_nbits_ch[gr][0] = MAX_BITS_PER_CHANNEL;
01668                     }
01669                     if (max_nbits_ch[gr][1] > MAX_BITS_PER_CHANNEL) {
01670                         max_nbits_ch[gr][1] = MAX_BITS_PER_CHANNEL;
01671                     }
01672                 }
01673 #else
01674                 for (ch = 0; ch < nch; ++ch) {
01675                     max_nbits_ch[gr][ch] *= MAX_BITS_PER_GRANULE;
01676                     max_nbits_ch[gr][ch] /= max_nbits_gr[gr];
01677                 }
01678 #endif
01679                 max_nbits_gr[gr] = 0;
01680                 for (ch = 0; ch < nch; ++ch) {
01681                     max_nbits_gr[gr] += max_nbits_ch[gr][ch]; 
01682                 }
01683             }
01684             sum_fr += max_nbits_gr[gr];
01685         }
01686         if (sum_fr > max_nbits_fr) {
01687 #if 1
01688             {
01689                 float f[2], s = 0;
01690                 for (gr = 0; gr < ngr; ++gr) {
01691                     if (max_nbits_gr[gr] > 0) {
01692                         f[gr] = sqrt(max_nbits_gr[gr]);
01693                         s += f[gr];
01694                     }
01695                     else {
01696                         f[gr] = 0;
01697                     }
01698                 }
01699                 for (gr = 0; gr < ngr; ++gr) {
01700                     if (s > 0) {
01701                         max_nbits_gr[gr] = max_nbits_fr * f[gr]/s;
01702                     }
01703                     else {
01704                         max_nbits_gr[gr] = 0;
01705                     }
01706                 }
01707             }
01708             if (ngr > 1) {
01709                 if (max_nbits_gr[0] > use_nbits_gr[0]+125) {                    
01710                     max_nbits_gr[1] += max_nbits_gr[0];
01711                     max_nbits_gr[1] -= use_nbits_gr[0]+125;
01712                     max_nbits_gr[0] = use_nbits_gr[0]+125;
01713                 }
01714                 if (max_nbits_gr[1] > use_nbits_gr[1]+125) {
01715                     max_nbits_gr[0] += max_nbits_gr[1];
01716                     max_nbits_gr[0] -= use_nbits_gr[1]+125;
01717                     max_nbits_gr[1] = use_nbits_gr[1]+125;
01718                 }
01719                 for (gr = 0; gr < ngr; ++gr) {
01720                     if (max_nbits_gr[gr] > MAX_BITS_PER_GRANULE) {
01721                         max_nbits_gr[gr] = MAX_BITS_PER_GRANULE;
01722                     }
01723                 }
01724             }
01725             for (gr = 0; gr < ngr; ++gr) {
01726                 float f[2], s = 0;
01727                 for (ch = 0; ch < nch; ++ch) {
01728                     if (max_nbits_ch[gr][ch] > 0) {
01729                         f[ch] = sqrt(max_nbits_ch[gr][ch]);
01730                         s += f[ch];
01731                     }
01732                     else {
01733                         f[ch] = 0;
01734                     }
01735                 }
01736                 for (ch = 0; ch < nch; ++ch) {
01737                     if (s > 0) {
01738                         max_nbits_ch[gr][ch] = max_nbits_gr[gr] * f[ch] / s;
01739                     }
01740                     else {
01741                         max_nbits_ch[gr][ch] = 0;
01742                     }
01743                 }
01744                 if (nch > 1) {
01745                     if (max_nbits_ch[gr][0] > use_nbits_ch[gr][0]+32) {
01746                         max_nbits_ch[gr][1] += max_nbits_ch[gr][0];
01747                         max_nbits_ch[gr][1] -= use_nbits_ch[gr][0]+32;
01748                         max_nbits_ch[gr][0] = use_nbits_ch[gr][0]+32;
01749                     }
01750                     if (max_nbits_ch[gr][1] > use_nbits_ch[gr][1]+32) {
01751                         max_nbits_ch[gr][0] += max_nbits_ch[gr][1];
01752                         max_nbits_ch[gr][0] -= use_nbits_ch[gr][1]+32;
01753                         max_nbits_ch[gr][1] = use_nbits_ch[gr][1]+32;
01754                     }
01755                     for (ch = 0; ch < nch; ++ch) {
01756                         if (max_nbits_ch[gr][ch] > MAX_BITS_PER_CHANNEL) {
01757                             max_nbits_ch[gr][ch] = MAX_BITS_PER_CHANNEL;
01758                         }
01759                     }
01760                 }
01761             }
01762 #else
01763             for (gr = 0; gr < ngr; ++gr) {
01764                 for (ch = 0; ch < nch; ++ch) {
01765                     max_nbits_ch[gr][ch] *= max_nbits_fr;
01766                     max_nbits_ch[gr][ch] /= sum_fr;
01767                 }
01768             }
01769 #endif
01770         }
01771         /* sanity check */
01772         sum_fr = 0;
01773         for (gr = 0; gr < ngr; ++gr) {
01774             int     sum_gr = 0;
01775             for (ch = 0; ch < nch; ++ch) {
01776                 sum_gr += max_nbits_ch[gr][ch];
01777                 if (max_nbits_ch[gr][ch] > MAX_BITS_PER_CHANNEL) {
01778                     ok = 0;
01779                 }
01780             }
01781             sum_fr += sum_gr;
01782             if (sum_gr > MAX_BITS_PER_GRANULE) {
01783                 ok = 0;
01784             }
01785         }
01786         if (sum_fr > max_nbits_fr) {
01787             ok = 0;
01788         }
01789         if (!ok) {
01790             /* we must have done something wrong, fallback to 'on_pe' based constrain */
01791             for (gr = 0; gr < ngr; ++gr) {
01792                 for (ch = 0; ch < nch; ++ch) {
01793                     max_nbits_ch[gr][ch] = max_bits[gr][ch];
01794                 }
01795             }
01796         }
01797     }
01798 
01799     /* we already called the 'best_scalefac_store' function, so we need to reset some
01800      * variables before we can do it again.
01801      */
01802     for (ch = 0; ch < nch; ++ch) {
01803         gfc->l3_side.scfsi[ch][0] = 0;
01804         gfc->l3_side.scfsi[ch][1] = 0;
01805         gfc->l3_side.scfsi[ch][2] = 0;
01806         gfc->l3_side.scfsi[ch][3] = 0;
01807     }
01808     for (gr = 0; gr < ngr; ++gr) {
01809         for (ch = 0; ch < nch; ++ch) {
01810             gfc->l3_side.tt[gr][ch].scalefac_compress = 0;
01811         }
01812     }
01813 
01814     /* alter our encoded data, until it fits into the target bitrate
01815      */
01816     use_nbits_fr = 0;
01817     for (gr = 0; gr < ngr; ++gr) {
01818         use_nbits_gr[gr] = 0;
01819         for (ch = 0; ch < nch; ++ch) {
01820             algo_t *that = &that_[gr][ch];
01821             use_nbits_ch[gr][ch] = 0;
01822             if (max_bits[gr][ch] > 0) {
01823                 int    *sfwork = sfwork_[gr][ch];
01824                 int    *vbrsfmin = vbrsfmin_[gr][ch];
01825                 searchGlobalStepsizeMax(that, sfwork, vbrsfmin, max_nbits_ch[gr][ch]);
01826                 reduce_bit_usage(gfc, gr, ch);
01827                 use_nbits_ch[gr][ch] =
01828                     that->cod_info->part2_3_length + that->cod_info->part2_length;
01829                 assert(use_nbits_ch[gr][ch] <= max_nbits_ch[gr][ch]);
01830                 use_nbits_gr[gr] += use_nbits_ch[gr][ch];
01831             }
01832         }               /* for ch */
01833         use_nbits_fr += use_nbits_gr[gr];
01834     }
01835 
01836     /* check bit constrains, but it should always be ok, iff there are no bugs ;-)
01837      */
01838     if (use_nbits_fr <= max_nbits_fr) {
01839         /*debug_bit_usage(that_,max_nbits_ch,"-");*/
01840         return use_nbits_fr;
01841     }
01842 
01843     ERRORF(gfc, "INTERNAL ERROR IN VBR NEW CODE (1313), please send bug report\n"
01844            "maxbits=%d usedbits=%d\n", max_nbits_fr, use_nbits_fr);
01845     exit(-1);
01846 
01847     return use_nbits_fr;
01848 }

Generated on Sun Dec 2 11:34:21 2007 for LAME by  doxygen 1.5.2