Blame libmp3lame/quantize.c

Packit 47f805
/*
Packit 47f805
 * MP3 quantization
Packit 47f805
 *
Packit 47f805
 *      Copyright (c) 1999-2000 Mark Taylor
Packit 47f805
 *      Copyright (c) 1999-2003 Takehiro Tominaga
Packit 47f805
 *      Copyright (c) 2000-2011 Robert Hegemann
Packit 47f805
 *      Copyright (c) 2001-2005 Gabriel Bouvigne
Packit 47f805
 *
Packit 47f805
 * This library is free software; you can redistribute it and/or
Packit 47f805
 * modify it under the terms of the GNU Library General Public
Packit 47f805
 * License as published by the Free Software Foundation; either
Packit 47f805
 * version 2 of the License, or (at your option) any later version.
Packit 47f805
 *
Packit 47f805
 * This library is distributed in the hope that it will be useful,
Packit 47f805
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 47f805
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the GNU
Packit 47f805
 * Library General Public License for more details.
Packit 47f805
 *
Packit 47f805
 * You should have received a copy of the GNU Library General Public
Packit 47f805
 * License along with this library; if not, write to the
Packit 47f805
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 47f805
 * Boston, MA 02111-1307, USA.
Packit 47f805
 */
Packit 47f805
Packit 47f805
/* $Id: quantize.c,v 1.219 2017/08/02 19:48:05 robert Exp $ */
Packit 47f805
Packit 47f805
#ifdef HAVE_CONFIG_H
Packit 47f805
# include <config.h>
Packit 47f805
#endif
Packit 47f805
Packit 47f805
#include "lame.h"
Packit 47f805
#include "machine.h"
Packit 47f805
#include "encoder.h"
Packit 47f805
#include "util.h"
Packit 47f805
#include "quantize_pvt.h"
Packit 47f805
#include "reservoir.h"
Packit 47f805
#include "bitstream.h"
Packit 47f805
#include "vbrquantize.h"
Packit 47f805
#include "quantize.h"
Packit 47f805
#ifdef HAVE_XMMINTRIN_H
Packit 47f805
#include "vector/lame_intrin.h"
Packit 47f805
#endif
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/* convert from L/R <-> Mid/Side */
Packit 47f805
static void
Packit 47f805
ms_convert(III_side_info_t * l3_side, int gr)
Packit 47f805
{
Packit 47f805
    int     i;
Packit 47f805
    for (i = 0; i < 576; ++i) {
Packit 47f805
        FLOAT   l, r;
Packit 47f805
        l = l3_side->tt[gr][0].xr[i];
Packit 47f805
        r = l3_side->tt[gr][1].xr[i];
Packit 47f805
        l3_side->tt[gr][0].xr[i] = (l + r) * (FLOAT) (SQRT2 * 0.5);
Packit 47f805
        l3_side->tt[gr][1].xr[i] = (l - r) * (FLOAT) (SQRT2 * 0.5);
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
/************************************************************************
Packit 47f805
 *
Packit 47f805
 *      init_outer_loop()
Packit 47f805
 *  mt 6/99
Packit 47f805
 *
Packit 47f805
 *  initializes cod_info, scalefac and xrpow
Packit 47f805
 *
Packit 47f805
 *  returns 0 if all energies in xr are zero, else 1
Packit 47f805
 *
Packit 47f805
 ************************************************************************/
Packit 47f805
Packit 47f805
static void
Packit 47f805
init_xrpow_core_c(gr_info * const cod_info, FLOAT xrpow[576], int upper, FLOAT * sum)
Packit 47f805
{
Packit 47f805
    int     i;
Packit 47f805
    FLOAT   tmp;
Packit 47f805
    *sum = 0;
Packit 47f805
    for (i = 0; i <= upper; ++i) {
Packit 47f805
        tmp = fabs(cod_info->xr[i]);
Packit 47f805
        *sum += tmp;
Packit 47f805
        xrpow[i] = sqrt(tmp * sqrt(tmp));
Packit 47f805
Packit 47f805
        if (xrpow[i] > cod_info->xrpow_max)
Packit 47f805
            cod_info->xrpow_max = xrpow[i];
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
void
Packit 47f805
init_xrpow_core_init(lame_internal_flags * const gfc)
Packit 47f805
{
Packit 47f805
    gfc->init_xrpow_core = init_xrpow_core_c;
Packit 47f805
Packit 47f805
#if defined(HAVE_XMMINTRIN_H)
Packit 47f805
    if (gfc->CPU_features.SSE)
Packit 47f805
        gfc->init_xrpow_core = init_xrpow_core_sse;
Packit 47f805
#endif
Packit 47f805
#ifndef HAVE_NASM
Packit 47f805
#ifdef MIN_ARCH_SSE
Packit 47f805
    gfc->init_xrpow_core = init_xrpow_core_sse;
Packit 47f805
#endif
Packit 47f805
#endif
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
static int
Packit 47f805
init_xrpow(lame_internal_flags * gfc, gr_info * const cod_info, FLOAT xrpow[576])
Packit 47f805
{
Packit 47f805
    FLOAT   sum = 0;
Packit 47f805
    int     i;
Packit 47f805
    int const upper = cod_info->max_nonzero_coeff;
Packit 47f805
Packit 47f805
    assert(xrpow != NULL);
Packit 47f805
    cod_info->xrpow_max = 0;
Packit 47f805
Packit 47f805
    /*  check if there is some energy we have to quantize
Packit 47f805
     *  and calculate xrpow matching our fresh scalefactors
Packit 47f805
     */
Packit 47f805
    assert(0 <= upper && upper <= 575);
Packit 47f805
    memset(&(xrpow[upper]), 0, (576 - upper) * sizeof(xrpow[0]));
Packit 47f805
Packit 47f805
Packit 47f805
    gfc->init_xrpow_core(cod_info, xrpow, upper, &sum);
Packit 47f805
Packit 47f805
    /*  return 1 if we have something to quantize, else 0
Packit 47f805
     */
Packit 47f805
    if (sum > (FLOAT) 1E-20) {
Packit 47f805
        int     j = 0;
Packit 47f805
        if (gfc->sv_qnt.substep_shaping & 2)
Packit 47f805
            j = 1;
Packit 47f805
Packit 47f805
        for (i = 0; i < cod_info->psymax; i++)
Packit 47f805
            gfc->sv_qnt.pseudohalf[i] = j;
Packit 47f805
Packit 47f805
        return 1;
Packit 47f805
    }
Packit 47f805
Packit 47f805
    memset(&cod_info->l3_enc[0], 0, sizeof(int) * 576);
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/*
Packit 47f805
Gabriel Bouvigne feb/apr 2003
Packit 47f805
Analog silence detection in partitionned sfb21
Packit 47f805
or sfb12 for short blocks
Packit 47f805
Packit 47f805
From top to bottom of sfb, changes to 0
Packit 47f805
coeffs which are below ath. It stops on the first
Packit 47f805
coeff higher than ath.
Packit 47f805
*/
Packit 47f805
static void
Packit 47f805
psfb21_analogsilence(lame_internal_flags const *gfc, gr_info * const cod_info)
Packit 47f805
{
Packit 47f805
    ATH_t const *const ATH = gfc->ATH;
Packit 47f805
    FLOAT  *const xr = cod_info->xr;
Packit 47f805
Packit 47f805
    if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type, but not SHORT blocks */
Packit 47f805
        int     gsfb;
Packit 47f805
        int     stop = 0;
Packit 47f805
        for (gsfb = PSFB21 - 1; gsfb >= 0 && !stop; gsfb--) {
Packit 47f805
            int const start = gfc->scalefac_band.psfb21[gsfb];
Packit 47f805
            int const end = gfc->scalefac_band.psfb21[gsfb + 1];
Packit 47f805
            int     j;
Packit 47f805
            FLOAT   ath21;
Packit 47f805
            ath21 = athAdjust(ATH->adjust_factor, ATH->psfb21[gsfb], ATH->floor, 0);
Packit 47f805
Packit 47f805
            if (gfc->sv_qnt.longfact[21] > 1e-12f)
Packit 47f805
                ath21 *= gfc->sv_qnt.longfact[21];
Packit 47f805
Packit 47f805
            for (j = end - 1; j >= start; j--) {
Packit 47f805
                if (fabs(xr[j]) < ath21)
Packit 47f805
                    xr[j] = 0;
Packit 47f805
                else {
Packit 47f805
                    stop = 1;
Packit 47f805
                    break;
Packit 47f805
                }
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        /*note: short blocks coeffs are reordered */
Packit 47f805
        int     block;
Packit 47f805
        for (block = 0; block < 3; block++) {
Packit 47f805
Packit 47f805
            int     gsfb;
Packit 47f805
            int     stop = 0;
Packit 47f805
            for (gsfb = PSFB12 - 1; gsfb >= 0 && !stop; gsfb--) {
Packit 47f805
                int const start = gfc->scalefac_band.s[12] * 3 +
Packit 47f805
                    (gfc->scalefac_band.s[13] - gfc->scalefac_band.s[12]) * block +
Packit 47f805
                    (gfc->scalefac_band.psfb12[gsfb] - gfc->scalefac_band.psfb12[0]);
Packit 47f805
                int const end =
Packit 47f805
                    start + (gfc->scalefac_band.psfb12[gsfb + 1] - gfc->scalefac_band.psfb12[gsfb]);
Packit 47f805
                int     j;
Packit 47f805
                FLOAT   ath12;
Packit 47f805
                ath12 = athAdjust(ATH->adjust_factor, ATH->psfb12[gsfb], ATH->floor, 0);
Packit 47f805
Packit 47f805
                if (gfc->sv_qnt.shortfact[12] > 1e-12f)
Packit 47f805
                    ath12 *= gfc->sv_qnt.shortfact[12];
Packit 47f805
Packit 47f805
                for (j = end - 1; j >= start; j--) {
Packit 47f805
                    if (fabs(xr[j]) < ath12)
Packit 47f805
                        xr[j] = 0;
Packit 47f805
                    else {
Packit 47f805
                        stop = 1;
Packit 47f805
                        break;
Packit 47f805
                    }
Packit 47f805
                }
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
static void
Packit 47f805
init_outer_loop(lame_internal_flags const *gfc, gr_info * const cod_info)
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    int     sfb, j;
Packit 47f805
    /*  initialize fresh cod_info
Packit 47f805
     */
Packit 47f805
    cod_info->part2_3_length = 0;
Packit 47f805
    cod_info->big_values = 0;
Packit 47f805
    cod_info->count1 = 0;
Packit 47f805
    cod_info->global_gain = 210;
Packit 47f805
    cod_info->scalefac_compress = 0;
Packit 47f805
    /* mixed_block_flag, block_type was set in psymodel.c */
Packit 47f805
    cod_info->table_select[0] = 0;
Packit 47f805
    cod_info->table_select[1] = 0;
Packit 47f805
    cod_info->table_select[2] = 0;
Packit 47f805
    cod_info->subblock_gain[0] = 0;
Packit 47f805
    cod_info->subblock_gain[1] = 0;
Packit 47f805
    cod_info->subblock_gain[2] = 0;
Packit 47f805
    cod_info->subblock_gain[3] = 0; /* this one is always 0 */
Packit 47f805
    cod_info->region0_count = 0;
Packit 47f805
    cod_info->region1_count = 0;
Packit 47f805
    cod_info->preflag = 0;
Packit 47f805
    cod_info->scalefac_scale = 0;
Packit 47f805
    cod_info->count1table_select = 0;
Packit 47f805
    cod_info->part2_length = 0;
Packit 47f805
    if (cfg->samplerate_out <= 8000) {
Packit 47f805
      cod_info->sfb_lmax = 17;
Packit 47f805
      cod_info->sfb_smin = 9;
Packit 47f805
      cod_info->psy_lmax = 17;
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
      cod_info->sfb_lmax = SBPSY_l;
Packit 47f805
      cod_info->sfb_smin = SBPSY_s;
Packit 47f805
      cod_info->psy_lmax = gfc->sv_qnt.sfb21_extra ? SBMAX_l : SBPSY_l;
Packit 47f805
    }
Packit 47f805
    cod_info->psymax = cod_info->psy_lmax;
Packit 47f805
    cod_info->sfbmax = cod_info->sfb_lmax;
Packit 47f805
    cod_info->sfbdivide = 11;
Packit 47f805
    for (sfb = 0; sfb < SBMAX_l; sfb++) {
Packit 47f805
        cod_info->width[sfb]
Packit 47f805
            = gfc->scalefac_band.l[sfb + 1] - gfc->scalefac_band.l[sfb];
Packit 47f805
        cod_info->window[sfb] = 3; /* which is always 0. */
Packit 47f805
    }
Packit 47f805
    if (cod_info->block_type == SHORT_TYPE) {
Packit 47f805
        FLOAT   ixwork[576];
Packit 47f805
        FLOAT  *ix;
Packit 47f805
Packit 47f805
        cod_info->sfb_smin = 0;
Packit 47f805
        cod_info->sfb_lmax = 0;
Packit 47f805
        if (cod_info->mixed_block_flag) {
Packit 47f805
            /*
Packit 47f805
             *  MPEG-1:      sfbs 0-7 long block, 3-12 short blocks
Packit 47f805
             *  MPEG-2(.5):  sfbs 0-5 long block, 3-12 short blocks
Packit 47f805
             */
Packit 47f805
            cod_info->sfb_smin = 3;
Packit 47f805
            cod_info->sfb_lmax = cfg->mode_gr * 2 + 4;
Packit 47f805
        }
Packit 47f805
        if (cfg->samplerate_out <= 8000) {
Packit 47f805
            cod_info->psymax
Packit 47f805
                = cod_info->sfb_lmax
Packit 47f805
                + 3 * (9 - cod_info->sfb_smin);
Packit 47f805
            cod_info->sfbmax = cod_info->sfb_lmax + 3 * (9 - cod_info->sfb_smin);
Packit 47f805
        }
Packit 47f805
        else {
Packit 47f805
            cod_info->psymax
Packit 47f805
                = cod_info->sfb_lmax
Packit 47f805
                + 3 * ((gfc->sv_qnt.sfb21_extra ? SBMAX_s : SBPSY_s) - cod_info->sfb_smin);
Packit 47f805
            cod_info->sfbmax = cod_info->sfb_lmax + 3 * (SBPSY_s - cod_info->sfb_smin);
Packit 47f805
        }
Packit 47f805
        cod_info->sfbdivide = cod_info->sfbmax - 18;
Packit 47f805
        cod_info->psy_lmax = cod_info->sfb_lmax;
Packit 47f805
        /* re-order the short blocks, for more efficient encoding below */
Packit 47f805
        /* By Takehiro TOMINAGA */
Packit 47f805
        /*
Packit 47f805
           Within each scalefactor band, data is given for successive
Packit 47f805
           time windows, beginning with window 0 and ending with window 2.
Packit 47f805
           Within each window, the quantized values are then arranged in
Packit 47f805
           order of increasing frequency...
Packit 47f805
         */
Packit 47f805
        ix = &cod_info->xr[gfc->scalefac_band.l[cod_info->sfb_lmax]];
Packit 47f805
        memcpy(ixwork, cod_info->xr, 576 * sizeof(FLOAT));
Packit 47f805
        for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
Packit 47f805
            int const start = gfc->scalefac_band.s[sfb];
Packit 47f805
            int const end = gfc->scalefac_band.s[sfb + 1];
Packit 47f805
            int     window, l;
Packit 47f805
            for (window = 0; window < 3; window++) {
Packit 47f805
                for (l = start; l < end; l++) {
Packit 47f805
                    *ix++ = ixwork[3 * l + window];
Packit 47f805
                }
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
Packit 47f805
        j = cod_info->sfb_lmax;
Packit 47f805
        for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
Packit 47f805
            cod_info->width[j] = cod_info->width[j + 1] = cod_info->width[j + 2]
Packit 47f805
                = gfc->scalefac_band.s[sfb + 1] - gfc->scalefac_band.s[sfb];
Packit 47f805
            cod_info->window[j] = 0;
Packit 47f805
            cod_info->window[j + 1] = 1;
Packit 47f805
            cod_info->window[j + 2] = 2;
Packit 47f805
            j += 3;
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
Packit 47f805
    cod_info->count1bits = 0;
Packit 47f805
    cod_info->sfb_partition_table = nr_of_sfb_block[0][0];
Packit 47f805
    cod_info->slen[0] = 0;
Packit 47f805
    cod_info->slen[1] = 0;
Packit 47f805
    cod_info->slen[2] = 0;
Packit 47f805
    cod_info->slen[3] = 0;
Packit 47f805
Packit 47f805
    cod_info->max_nonzero_coeff = 575;
Packit 47f805
Packit 47f805
    /*  fresh scalefactors are all zero
Packit 47f805
     */
Packit 47f805
    memset(cod_info->scalefac, 0, sizeof(cod_info->scalefac));
Packit 47f805
Packit 47f805
    if (cfg->vbr != vbr_mt && cfg->vbr != vbr_mtrh && cfg->vbr != vbr_abr && cfg->vbr != vbr_off) {
Packit 47f805
        psfb21_analogsilence(gfc, cod_info);
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/************************************************************************
Packit 47f805
 *
Packit 47f805
 *      bin_search_StepSize()
Packit 47f805
 *
Packit 47f805
 *  author/date??
Packit 47f805
 *
Packit 47f805
 *  binary step size search
Packit 47f805
 *  used by outer_loop to get a quantizer step size to start with
Packit 47f805
 *
Packit 47f805
 ************************************************************************/
Packit 47f805
Packit 47f805
typedef enum {
Packit 47f805
    BINSEARCH_NONE,
Packit 47f805
    BINSEARCH_UP,
Packit 47f805
    BINSEARCH_DOWN
Packit 47f805
} binsearchDirection_t;
Packit 47f805
Packit 47f805
static int
Packit 47f805
bin_search_StepSize(lame_internal_flags * const gfc, gr_info * const cod_info,
Packit 47f805
                    int desired_rate, const int ch, const FLOAT xrpow[576])
Packit 47f805
{
Packit 47f805
    int     nBits;
Packit 47f805
    int     CurrentStep = gfc->sv_qnt.CurrentStep[ch];
Packit 47f805
    int     flag_GoneOver = 0;
Packit 47f805
    int const start = gfc->sv_qnt.OldValue[ch];
Packit 47f805
    binsearchDirection_t Direction = BINSEARCH_NONE;
Packit 47f805
    cod_info->global_gain = start;
Packit 47f805
    desired_rate -= cod_info->part2_length;
Packit 47f805
Packit 47f805
    assert(CurrentStep);
Packit 47f805
    for (;;) {
Packit 47f805
        int     step;
Packit 47f805
        nBits = count_bits(gfc, xrpow, cod_info, 0);
Packit 47f805
Packit 47f805
        if (CurrentStep == 1 || nBits == desired_rate)
Packit 47f805
            break;      /* nothing to adjust anymore */
Packit 47f805
Packit 47f805
        if (nBits > desired_rate) {
Packit 47f805
            /* increase Quantize_StepSize */
Packit 47f805
            if (Direction == BINSEARCH_DOWN)
Packit 47f805
                flag_GoneOver = 1;
Packit 47f805
Packit 47f805
            if (flag_GoneOver)
Packit 47f805
                CurrentStep /= 2;
Packit 47f805
            Direction = BINSEARCH_UP;
Packit 47f805
            step = CurrentStep;
Packit 47f805
        }
Packit 47f805
        else {
Packit 47f805
            /* decrease Quantize_StepSize */
Packit 47f805
            if (Direction == BINSEARCH_UP)
Packit 47f805
                flag_GoneOver = 1;
Packit 47f805
Packit 47f805
            if (flag_GoneOver)
Packit 47f805
                CurrentStep /= 2;
Packit 47f805
            Direction = BINSEARCH_DOWN;
Packit 47f805
            step = -CurrentStep;
Packit 47f805
        }
Packit 47f805
        cod_info->global_gain += step;
Packit 47f805
        if (cod_info->global_gain < 0) {
Packit 47f805
            cod_info->global_gain = 0;
Packit 47f805
            flag_GoneOver = 1;
Packit 47f805
        }
Packit 47f805
        if (cod_info->global_gain > 255) {
Packit 47f805
            cod_info->global_gain = 255;
Packit 47f805
            flag_GoneOver = 1;
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
Packit 47f805
    assert(cod_info->global_gain >= 0);
Packit 47f805
    assert(cod_info->global_gain < 256);
Packit 47f805
Packit 47f805
    while (nBits > desired_rate && cod_info->global_gain < 255) {
Packit 47f805
        cod_info->global_gain++;
Packit 47f805
        nBits = count_bits(gfc, xrpow, cod_info, 0);
Packit 47f805
    }
Packit 47f805
    gfc->sv_qnt.CurrentStep[ch] = (start - cod_info->global_gain >= 4) ? 4 : 2;
Packit 47f805
    gfc->sv_qnt.OldValue[ch] = cod_info->global_gain;
Packit 47f805
    cod_info->part2_3_length = nBits;
Packit 47f805
    return nBits;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/************************************************************************
Packit 47f805
 *
Packit 47f805
 *      trancate_smallspectrums()
Packit 47f805
 *
Packit 47f805
 *  Takehiro TOMINAGA 2002-07-21
Packit 47f805
 *
Packit 47f805
 *  trancate smaller nubmers into 0 as long as the noise threshold is allowed.
Packit 47f805
 *
Packit 47f805
 ************************************************************************/
Packit 47f805
static int
Packit 47f805
floatcompare(const void *v1, const void *v2)
Packit 47f805
{
Packit 47f805
    const FLOAT *const a = v1, *const b = v2;
Packit 47f805
    if (*a > *b)
Packit 47f805
        return 1;
Packit 47f805
    if (*a < *b)
Packit 47f805
        return -1;
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
static void
Packit 47f805
trancate_smallspectrums(lame_internal_flags const *gfc,
Packit 47f805
                        gr_info * const gi, const FLOAT * const l3_xmin, FLOAT * const work)
Packit 47f805
{
Packit 47f805
    int     sfb, j, width;
Packit 47f805
    FLOAT   distort[SFBMAX];
Packit 47f805
    calc_noise_result dummy;
Packit 47f805
Packit 47f805
    if ((!(gfc->sv_qnt.substep_shaping & 4) && gi->block_type == SHORT_TYPE)
Packit 47f805
        || gfc->sv_qnt.substep_shaping & 0x80)
Packit 47f805
        return;
Packit 47f805
    (void) calc_noise(gi, l3_xmin, distort, &dummy, 0);
Packit 47f805
    for (j = 0; j < 576; j++) {
Packit 47f805
        FLOAT   xr = 0.0;
Packit 47f805
        if (gi->l3_enc[j] != 0)
Packit 47f805
            xr = fabs(gi->xr[j]);
Packit 47f805
        work[j] = xr;
Packit 47f805
    }
Packit 47f805
Packit 47f805
    j = 0;
Packit 47f805
    sfb = 8;
Packit 47f805
    if (gi->block_type == SHORT_TYPE)
Packit 47f805
        sfb = 6;
Packit 47f805
    do {
Packit 47f805
        FLOAT   allowedNoise, trancateThreshold;
Packit 47f805
        int     nsame, start;
Packit 47f805
Packit 47f805
        width = gi->width[sfb];
Packit 47f805
        j += width;
Packit 47f805
        if (distort[sfb] >= 1.0)
Packit 47f805
            continue;
Packit 47f805
Packit 47f805
        qsort(&work[j - width], width, sizeof(FLOAT), floatcompare);
Packit 47f805
        if (EQ(work[j - 1], 0.0))
Packit 47f805
            continue;   /* all zero sfb */
Packit 47f805
Packit 47f805
        allowedNoise = (1.0 - distort[sfb]) * l3_xmin[sfb];
Packit 47f805
        trancateThreshold = 0.0;
Packit 47f805
        start = 0;
Packit 47f805
        do {
Packit 47f805
            FLOAT   noise;
Packit 47f805
            for (nsame = 1; start + nsame < width; nsame++)
Packit 47f805
                if (NEQ(work[start + j - width], work[start + j + nsame - width]))
Packit 47f805
                    break;
Packit 47f805
Packit 47f805
            noise = work[start + j - width] * work[start + j - width] * nsame;
Packit 47f805
            if (allowedNoise < noise) {
Packit 47f805
                if (start != 0)
Packit 47f805
                    trancateThreshold = work[start + j - width - 1];
Packit 47f805
                break;
Packit 47f805
            }
Packit 47f805
            allowedNoise -= noise;
Packit 47f805
            start += nsame;
Packit 47f805
        } while (start < width);
Packit 47f805
        if (EQ(trancateThreshold, 0.0))
Packit 47f805
            continue;
Packit 47f805
Packit 47f805
/*      printf("%e %e %e\n", */
Packit 47f805
/*             trancateThreshold/l3_xmin[sfb], */
Packit 47f805
/*             trancateThreshold/(l3_xmin[sfb]*start), */
Packit 47f805
/*             trancateThreshold/(l3_xmin[sfb]*(start+width)) */
Packit 47f805
/*          ); */
Packit 47f805
/*      if (trancateThreshold > 1000*l3_xmin[sfb]*start) */
Packit 47f805
/*          trancateThreshold = 1000*l3_xmin[sfb]*start; */
Packit 47f805
Packit 47f805
        do {
Packit 47f805
            if (fabs(gi->xr[j - width]) <= trancateThreshold)
Packit 47f805
                gi->l3_enc[j - width] = 0;
Packit 47f805
        } while (--width > 0);
Packit 47f805
    } while (++sfb < gi->psymax);
Packit 47f805
Packit 47f805
    gi->part2_3_length = noquant_count_bits(gfc, gi, 0);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
/*************************************************************************
Packit 47f805
 *
Packit 47f805
 *      loop_break()
Packit 47f805
 *
Packit 47f805
 *  author/date??
Packit 47f805
 *
Packit 47f805
 *  Function: Returns zero if there is a scalefac which has not been
Packit 47f805
 *            amplified. Otherwise it returns one.
Packit 47f805
 *
Packit 47f805
 *************************************************************************/
Packit 47f805
Packit 47f805
inline static int
Packit 47f805
loop_break(const gr_info * const cod_info)
Packit 47f805
{
Packit 47f805
    int     sfb;
Packit 47f805
Packit 47f805
    for (sfb = 0; sfb < cod_info->sfbmax; sfb++)
Packit 47f805
        if (cod_info->scalefac[sfb]
Packit 47f805
            + cod_info->subblock_gain[cod_info->window[sfb]] == 0)
Packit 47f805
            return 0;
Packit 47f805
Packit 47f805
    return 1;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/*  mt 5/99:  Function: Improved calc_noise for a single channel   */
Packit 47f805
Packit 47f805
/*************************************************************************
Packit 47f805
 *
Packit 47f805
 *      quant_compare()
Packit 47f805
 *
Packit 47f805
 *  author/date??
Packit 47f805
 *
Packit 47f805
 *  several different codes to decide which quantization is better
Packit 47f805
 *
Packit 47f805
 *************************************************************************/
Packit 47f805
Packit 47f805
static double
Packit 47f805
penalties(double noise)
Packit 47f805
{
Packit 47f805
    return FAST_LOG10(0.368 + 0.632 * noise * noise * noise);
Packit 47f805
}
Packit 47f805
Packit 47f805
static double
Packit 47f805
get_klemm_noise(const FLOAT * distort, const gr_info * const gi)
Packit 47f805
{
Packit 47f805
    int     sfb;
Packit 47f805
    double  klemm_noise = 1E-37;
Packit 47f805
    for (sfb = 0; sfb < gi->psymax; sfb++)
Packit 47f805
        klemm_noise += penalties(distort[sfb]);
Packit 47f805
Packit 47f805
    return Max(1e-20, klemm_noise);
Packit 47f805
}
Packit 47f805
Packit 47f805
inline static int
Packit 47f805
quant_compare(const int quant_comp,
Packit 47f805
              const calc_noise_result * const best,
Packit 47f805
              calc_noise_result * const calc, const gr_info * const gi, const FLOAT * distort)
Packit 47f805
{
Packit 47f805
    /*
Packit 47f805
       noise is given in decibels (dB) relative to masking thesholds.
Packit 47f805
Packit 47f805
       over_noise:  ??? (the previous comment is fully wrong)
Packit 47f805
       tot_noise:   ??? (the previous comment is fully wrong)
Packit 47f805
       max_noise:   max quantization noise
Packit 47f805
Packit 47f805
     */
Packit 47f805
    int     better;
Packit 47f805
Packit 47f805
    switch (quant_comp) {
Packit 47f805
    default:
Packit 47f805
    case 9:{
Packit 47f805
            if (best->over_count > 0) {
Packit 47f805
                /* there are distorted sfb */
Packit 47f805
                better = calc->over_SSD <= best->over_SSD;
Packit 47f805
                if (calc->over_SSD == best->over_SSD)
Packit 47f805
                    better = calc->bits < best->bits;
Packit 47f805
            }
Packit 47f805
            else {
Packit 47f805
                /* no distorted sfb */
Packit 47f805
                better = ((calc->max_noise < 0) &&
Packit 47f805
                          ((calc->max_noise * 10 + calc->bits) <=
Packit 47f805
                           (best->max_noise * 10 + best->bits)));
Packit 47f805
            }
Packit 47f805
            break;
Packit 47f805
        }
Packit 47f805
Packit 47f805
    case 0:
Packit 47f805
        better = calc->over_count < best->over_count
Packit 47f805
            || (calc->over_count == best->over_count && calc->over_noise < best->over_noise)
Packit 47f805
            || (calc->over_count == best->over_count &&
Packit 47f805
                EQ(calc->over_noise, best->over_noise) && calc->tot_noise < best->tot_noise);
Packit 47f805
        break;
Packit 47f805
Packit 47f805
    case 8:
Packit 47f805
        calc->max_noise = get_klemm_noise(distort, gi);
Packit 47f805
        /*lint --fallthrough */
Packit 47f805
    case 1:
Packit 47f805
        better = calc->max_noise < best->max_noise;
Packit 47f805
        break;
Packit 47f805
    case 2:
Packit 47f805
        better = calc->tot_noise < best->tot_noise;
Packit 47f805
        break;
Packit 47f805
    case 3:
Packit 47f805
        better = (calc->tot_noise < best->tot_noise)
Packit 47f805
            && (calc->max_noise < best->max_noise);
Packit 47f805
        break;
Packit 47f805
    case 4:
Packit 47f805
        better = (calc->max_noise <= 0.0 && best->max_noise > 0.2)
Packit 47f805
            || (calc->max_noise <= 0.0 &&
Packit 47f805
                best->max_noise < 0.0 &&
Packit 47f805
                best->max_noise > calc->max_noise - 0.2 && calc->tot_noise < best->tot_noise)
Packit 47f805
            || (calc->max_noise <= 0.0 &&
Packit 47f805
                best->max_noise > 0.0 &&
Packit 47f805
                best->max_noise > calc->max_noise - 0.2 &&
Packit 47f805
                calc->tot_noise < best->tot_noise + best->over_noise)
Packit 47f805
            || (calc->max_noise > 0.0 &&
Packit 47f805
                best->max_noise > -0.05 &&
Packit 47f805
                best->max_noise > calc->max_noise - 0.1 &&
Packit 47f805
                calc->tot_noise + calc->over_noise < best->tot_noise + best->over_noise)
Packit 47f805
            || (calc->max_noise > 0.0 &&
Packit 47f805
                best->max_noise > -0.1 &&
Packit 47f805
                best->max_noise > calc->max_noise - 0.15 &&
Packit 47f805
                calc->tot_noise + calc->over_noise + calc->over_noise <
Packit 47f805
                best->tot_noise + best->over_noise + best->over_noise);
Packit 47f805
        break;
Packit 47f805
    case 5:
Packit 47f805
        better = calc->over_noise < best->over_noise
Packit 47f805
            || (EQ(calc->over_noise, best->over_noise) && calc->tot_noise < best->tot_noise);
Packit 47f805
        break;
Packit 47f805
    case 6:
Packit 47f805
        better = calc->over_noise < best->over_noise
Packit 47f805
            || (EQ(calc->over_noise, best->over_noise) &&
Packit 47f805
                (calc->max_noise < best->max_noise
Packit 47f805
                 || (EQ(calc->max_noise, best->max_noise) && calc->tot_noise <= best->tot_noise)
Packit 47f805
                ));
Packit 47f805
        break;
Packit 47f805
    case 7:
Packit 47f805
        better = calc->over_count < best->over_count || calc->over_noise < best->over_noise;
Packit 47f805
        break;
Packit 47f805
    }
Packit 47f805
Packit 47f805
Packit 47f805
    if (best->over_count == 0) {
Packit 47f805
        /*
Packit 47f805
           If no distorted bands, only use this quantization
Packit 47f805
           if it is better, and if it uses less bits.
Packit 47f805
           Unfortunately, part2_3_length is sometimes a poor
Packit 47f805
           estimator of the final size at low bitrates.
Packit 47f805
         */
Packit 47f805
        better = better && calc->bits < best->bits;
Packit 47f805
    }
Packit 47f805
Packit 47f805
Packit 47f805
    return better;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/*************************************************************************
Packit 47f805
 *
Packit 47f805
 *          amp_scalefac_bands()
Packit 47f805
 *
Packit 47f805
 *  author/date??
Packit 47f805
 *
Packit 47f805
 *  Amplify the scalefactor bands that violate the masking threshold.
Packit 47f805
 *  See ISO 11172-3 Section C.1.5.4.3.5
Packit 47f805
 *
Packit 47f805
 *  distort[] = noise/masking
Packit 47f805
 *  distort[] > 1   ==> noise is not masked
Packit 47f805
 *  distort[] < 1   ==> noise is masked
Packit 47f805
 *  max_dist = maximum value of distort[]
Packit 47f805
 *
Packit 47f805
 *  Three algorithms:
Packit 47f805
 *  noise_shaping_amp
Packit 47f805
 *        0             Amplify all bands with distort[]>1.
Packit 47f805
 *
Packit 47f805
 *        1             Amplify all bands with distort[] >= max_dist^(.5);
Packit 47f805
 *                     ( 50% in the db scale)
Packit 47f805
 *
Packit 47f805
 *        2             Amplify first band with distort[] >= max_dist;
Packit 47f805
 *
Packit 47f805
 *
Packit 47f805
 *  For algorithms 0 and 1, if max_dist < 1, then amplify all bands
Packit 47f805
 *  with distort[] >= .95*max_dist.  This is to make sure we always
Packit 47f805
 *  amplify at least one band.
Packit 47f805
 *
Packit 47f805
 *
Packit 47f805
 *************************************************************************/
Packit 47f805
static void
Packit 47f805
amp_scalefac_bands(lame_internal_flags * gfc,
Packit 47f805
                   gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    int     j, sfb;
Packit 47f805
    FLOAT   ifqstep34, trigger;
Packit 47f805
    int     noise_shaping_amp;
Packit 47f805
Packit 47f805
    if (cod_info->scalefac_scale == 0) {
Packit 47f805
        ifqstep34 = 1.29683955465100964055; /* 2**(.75*.5) */
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        ifqstep34 = 1.68179283050742922612; /* 2**(.75*1) */
Packit 47f805
    }
Packit 47f805
Packit 47f805
    /* compute maximum value of distort[]  */
Packit 47f805
    trigger = 0;
Packit 47f805
    for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
Packit 47f805
        if (trigger < distort[sfb])
Packit 47f805
            trigger = distort[sfb];
Packit 47f805
    }
Packit 47f805
Packit 47f805
    noise_shaping_amp = cfg->noise_shaping_amp;
Packit 47f805
    if (noise_shaping_amp == 3) {
Packit 47f805
        if (bRefine == 1)
Packit 47f805
            noise_shaping_amp = 2;
Packit 47f805
        else
Packit 47f805
            noise_shaping_amp = 1;
Packit 47f805
    }
Packit 47f805
    switch (noise_shaping_amp) {
Packit 47f805
    case 2:
Packit 47f805
        /* amplify exactly 1 band */
Packit 47f805
        break;
Packit 47f805
Packit 47f805
    case 1:
Packit 47f805
        /* amplify bands within 50% of max (on db scale) */
Packit 47f805
        if (trigger > 1.0)
Packit 47f805
            trigger = pow(trigger, .5);
Packit 47f805
        else
Packit 47f805
            trigger *= .95;
Packit 47f805
        break;
Packit 47f805
Packit 47f805
    case 0:
Packit 47f805
    default:
Packit 47f805
        /* ISO algorithm.  amplify all bands with distort>1 */
Packit 47f805
        if (trigger > 1.0)
Packit 47f805
            trigger = 1.0;
Packit 47f805
        else
Packit 47f805
            trigger *= .95;
Packit 47f805
        break;
Packit 47f805
    }
Packit 47f805
Packit 47f805
    j = 0;
Packit 47f805
    for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
Packit 47f805
        int const width = cod_info->width[sfb];
Packit 47f805
        int     l;
Packit 47f805
        j += width;
Packit 47f805
        if (distort[sfb] < trigger)
Packit 47f805
            continue;
Packit 47f805
Packit 47f805
        if (gfc->sv_qnt.substep_shaping & 2) {
Packit 47f805
            gfc->sv_qnt.pseudohalf[sfb] = !gfc->sv_qnt.pseudohalf[sfb];
Packit 47f805
            if (!gfc->sv_qnt.pseudohalf[sfb] && cfg->noise_shaping_amp == 2)
Packit 47f805
                return;
Packit 47f805
        }
Packit 47f805
        cod_info->scalefac[sfb]++;
Packit 47f805
        for (l = -width; l < 0; l++) {
Packit 47f805
            xrpow[j + l] *= ifqstep34;
Packit 47f805
            if (xrpow[j + l] > cod_info->xrpow_max)
Packit 47f805
                cod_info->xrpow_max = xrpow[j + l];
Packit 47f805
        }
Packit 47f805
Packit 47f805
        if (cfg->noise_shaping_amp == 2)
Packit 47f805
            return;
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
/*************************************************************************
Packit 47f805
 *
Packit 47f805
 *      inc_scalefac_scale()
Packit 47f805
 *
Packit 47f805
 *  Takehiro Tominaga 2000-xx-xx
Packit 47f805
 *
Packit 47f805
 *  turns on scalefac scale and adjusts scalefactors
Packit 47f805
 *
Packit 47f805
 *************************************************************************/
Packit 47f805
Packit 47f805
static void
Packit 47f805
inc_scalefac_scale(gr_info * const cod_info, FLOAT xrpow[576])
Packit 47f805
{
Packit 47f805
    int     l, j, sfb;
Packit 47f805
    const FLOAT ifqstep34 = 1.29683955465100964055;
Packit 47f805
Packit 47f805
    j = 0;
Packit 47f805
    for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
Packit 47f805
        int const width = cod_info->width[sfb];
Packit 47f805
        int     s = cod_info->scalefac[sfb];
Packit 47f805
        if (cod_info->preflag)
Packit 47f805
            s += pretab[sfb];
Packit 47f805
        j += width;
Packit 47f805
        if (s & 1) {
Packit 47f805
            s++;
Packit 47f805
            for (l = -width; l < 0; l++) {
Packit 47f805
                xrpow[j + l] *= ifqstep34;
Packit 47f805
                if (xrpow[j + l] > cod_info->xrpow_max)
Packit 47f805
                    cod_info->xrpow_max = xrpow[j + l];
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
        cod_info->scalefac[sfb] = s >> 1;
Packit 47f805
    }
Packit 47f805
    cod_info->preflag = 0;
Packit 47f805
    cod_info->scalefac_scale = 1;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/*************************************************************************
Packit 47f805
 *
Packit 47f805
 *      inc_subblock_gain()
Packit 47f805
 *
Packit 47f805
 *  Takehiro Tominaga 2000-xx-xx
Packit 47f805
 *
Packit 47f805
 *  increases the subblock gain and adjusts scalefactors
Packit 47f805
 *
Packit 47f805
 *************************************************************************/
Packit 47f805
Packit 47f805
static int
Packit 47f805
inc_subblock_gain(const lame_internal_flags * const gfc, gr_info * const cod_info, FLOAT xrpow[576])
Packit 47f805
{
Packit 47f805
    int     sfb, window;
Packit 47f805
    int    *const scalefac = cod_info->scalefac;
Packit 47f805
Packit 47f805
    /* subbloc_gain can't do anything in the long block region */
Packit 47f805
    for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++) {
Packit 47f805
        if (scalefac[sfb] >= 16)
Packit 47f805
            return 1;
Packit 47f805
    }
Packit 47f805
Packit 47f805
    for (window = 0; window < 3; window++) {
Packit 47f805
        int     s1, s2, l, j;
Packit 47f805
        s1 = s2 = 0;
Packit 47f805
Packit 47f805
        for (sfb = cod_info->sfb_lmax + window; sfb < cod_info->sfbdivide; sfb += 3) {
Packit 47f805
            if (s1 < scalefac[sfb])
Packit 47f805
                s1 = scalefac[sfb];
Packit 47f805
        }
Packit 47f805
        for (; sfb < cod_info->sfbmax; sfb += 3) {
Packit 47f805
            if (s2 < scalefac[sfb])
Packit 47f805
                s2 = scalefac[sfb];
Packit 47f805
        }
Packit 47f805
Packit 47f805
        if (s1 < 16 && s2 < 8)
Packit 47f805
            continue;
Packit 47f805
Packit 47f805
        if (cod_info->subblock_gain[window] >= 7)
Packit 47f805
            return 1;
Packit 47f805
Packit 47f805
        /* even though there is no scalefactor for sfb12
Packit 47f805
         * subblock gain affects upper frequencies too, that's why
Packit 47f805
         * we have to go up to SBMAX_s
Packit 47f805
         */
Packit 47f805
        cod_info->subblock_gain[window]++;
Packit 47f805
        j = gfc->scalefac_band.l[cod_info->sfb_lmax];
Packit 47f805
        for (sfb = cod_info->sfb_lmax + window; sfb < cod_info->sfbmax; sfb += 3) {
Packit 47f805
            FLOAT   amp;
Packit 47f805
            int const width = cod_info->width[sfb];
Packit 47f805
            int     s = scalefac[sfb];
Packit 47f805
            assert(s >= 0);
Packit 47f805
            s = s - (4 >> cod_info->scalefac_scale);
Packit 47f805
            if (s >= 0) {
Packit 47f805
                scalefac[sfb] = s;
Packit 47f805
                j += width * 3;
Packit 47f805
                continue;
Packit 47f805
            }
Packit 47f805
Packit 47f805
            scalefac[sfb] = 0;
Packit 47f805
            {
Packit 47f805
                int const gain = 210 + (s << (cod_info->scalefac_scale + 1));
Packit 47f805
                amp = IPOW20(gain);
Packit 47f805
            }
Packit 47f805
            j += width * (window + 1);
Packit 47f805
            for (l = -width; l < 0; l++) {
Packit 47f805
                xrpow[j + l] *= amp;
Packit 47f805
                if (xrpow[j + l] > cod_info->xrpow_max)
Packit 47f805
                    cod_info->xrpow_max = xrpow[j + l];
Packit 47f805
            }
Packit 47f805
            j += width * (3 - window - 1);
Packit 47f805
        }
Packit 47f805
Packit 47f805
        {
Packit 47f805
            FLOAT const amp = IPOW20(202);
Packit 47f805
            j += cod_info->width[sfb] * (window + 1);
Packit 47f805
            for (l = -cod_info->width[sfb]; l < 0; l++) {
Packit 47f805
                xrpow[j + l] *= amp;
Packit 47f805
                if (xrpow[j + l] > cod_info->xrpow_max)
Packit 47f805
                    cod_info->xrpow_max = xrpow[j + l];
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
    return 0;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/********************************************************************
Packit 47f805
 *
Packit 47f805
 *      balance_noise()
Packit 47f805
 *
Packit 47f805
 *  Takehiro Tominaga /date??
Packit 47f805
 *  Robert Hegemann 2000-09-06: made a function of it
Packit 47f805
 *
Packit 47f805
 *  amplifies scalefactor bands,
Packit 47f805
 *   - if all are already amplified returns 0
Packit 47f805
 *   - if some bands are amplified too much:
Packit 47f805
 *      * try to increase scalefac_scale
Packit 47f805
 *      * if already scalefac_scale was set
Packit 47f805
 *          try on short blocks to increase subblock gain
Packit 47f805
 *
Packit 47f805
 ********************************************************************/
Packit 47f805
inline static int
Packit 47f805
balance_noise(lame_internal_flags * gfc,
Packit 47f805
              gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    int     status;
Packit 47f805
Packit 47f805
    amp_scalefac_bands(gfc, cod_info, distort, xrpow, bRefine);
Packit 47f805
Packit 47f805
    /* check to make sure we have not amplified too much
Packit 47f805
     * loop_break returns 0 if there is an unamplified scalefac
Packit 47f805
     * scale_bitcount returns 0 if no scalefactors are too large
Packit 47f805
     */
Packit 47f805
Packit 47f805
    status = loop_break(cod_info);
Packit 47f805
Packit 47f805
    if (status)
Packit 47f805
        return 0;       /* all bands amplified */
Packit 47f805
Packit 47f805
    /* not all scalefactors have been amplified.  so these
Packit 47f805
     * scalefacs are possibly valid.  encode them:
Packit 47f805
     */
Packit 47f805
    status = scale_bitcount(gfc, cod_info);
Packit 47f805
Packit 47f805
    if (!status)
Packit 47f805
        return 1;       /* amplified some bands not exceeding limits */
Packit 47f805
Packit 47f805
    /*  some scalefactors are too large.
Packit 47f805
     *  lets try setting scalefac_scale=1
Packit 47f805
     */
Packit 47f805
    if (cfg->noise_shaping > 1) {
Packit 47f805
        memset(&gfc->sv_qnt.pseudohalf[0], 0, sizeof(gfc->sv_qnt.pseudohalf));
Packit 47f805
        if (!cod_info->scalefac_scale) {
Packit 47f805
            inc_scalefac_scale(cod_info, xrpow);
Packit 47f805
            status = 0;
Packit 47f805
        }
Packit 47f805
        else {
Packit 47f805
            if (cod_info->block_type == SHORT_TYPE && cfg->subblock_gain > 0) {
Packit 47f805
                status = inc_subblock_gain(gfc, cod_info, xrpow)
Packit 47f805
                    || loop_break(cod_info);
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
Packit 47f805
    if (!status) {
Packit 47f805
        status = scale_bitcount(gfc, cod_info);
Packit 47f805
    }
Packit 47f805
    return !status;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/************************************************************************
Packit 47f805
 *
Packit 47f805
 *  outer_loop ()
Packit 47f805
 *
Packit 47f805
 *  Function: The outer iteration loop controls the masking conditions
Packit 47f805
 *  of all scalefactorbands. It computes the best scalefac and
Packit 47f805
 *  global gain. This module calls the inner iteration loop
Packit 47f805
 *
Packit 47f805
 *  mt 5/99 completely rewritten to allow for bit reservoir control,
Packit 47f805
 *  mid/side channels with L/R or mid/side masking thresholds,
Packit 47f805
 *  and chooses best quantization instead of last quantization when
Packit 47f805
 *  no distortion free quantization can be found.
Packit 47f805
 *
Packit 47f805
 *  added VBR support mt 5/99
Packit 47f805
 *
Packit 47f805
 *  some code shuffle rh 9/00
Packit 47f805
 ************************************************************************/
Packit 47f805
Packit 47f805
static int
Packit 47f805
outer_loop(lame_internal_flags * gfc, gr_info * const cod_info, const FLOAT * const l3_xmin, /* allowed distortion */
Packit 47f805
           FLOAT xrpow[576], /* coloured magnitudes of spectral */
Packit 47f805
           const int ch, const int targ_bits)
Packit 47f805
{                       /* maximum allowed bits */
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    gr_info cod_info_w;
Packit 47f805
    FLOAT   save_xrpow[576];
Packit 47f805
    FLOAT   distort[SFBMAX];
Packit 47f805
    calc_noise_result best_noise_info;
Packit 47f805
    int     huff_bits;
Packit 47f805
    int     better;
Packit 47f805
    int     age;
Packit 47f805
    calc_noise_data prev_noise;
Packit 47f805
    int     best_part2_3_length = 9999999;
Packit 47f805
    int     bEndOfSearch = 0;
Packit 47f805
    int     bRefine = 0;
Packit 47f805
    int     best_ggain_pass1 = 0;
Packit 47f805
Packit 47f805
    (void) bin_search_StepSize(gfc, cod_info, targ_bits, ch, xrpow);
Packit 47f805
Packit 47f805
    if (!cfg->noise_shaping)
Packit 47f805
        /* fast mode, no noise shaping, we are ready */
Packit 47f805
        return 100;     /* default noise_info.over_count */
Packit 47f805
Packit 47f805
    memset(&prev_noise, 0, sizeof(calc_noise_data));
Packit 47f805
Packit 47f805
Packit 47f805
    /* compute the distortion in this quantization */
Packit 47f805
    /* coefficients and thresholds both l/r (or both mid/side) */
Packit 47f805
    (void) calc_noise(cod_info, l3_xmin, distort, &best_noise_info, &prev_noise);
Packit 47f805
    best_noise_info.bits = cod_info->part2_3_length;
Packit 47f805
Packit 47f805
    cod_info_w = *cod_info;
Packit 47f805
    age = 0;
Packit 47f805
    /* if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh) */
Packit 47f805
    memcpy(save_xrpow, xrpow, sizeof(FLOAT) * 576);
Packit 47f805
Packit 47f805
    while (!bEndOfSearch) {
Packit 47f805
        /* BEGIN MAIN LOOP */
Packit 47f805
        do {
Packit 47f805
            calc_noise_result noise_info;
Packit 47f805
            int     search_limit;
Packit 47f805
            int     maxggain = 255;
Packit 47f805
Packit 47f805
            /* When quantization with no distorted bands is found,
Packit 47f805
             * allow up to X new unsuccesful tries in serial. This
Packit 47f805
             * gives us more possibilities for different quant_compare modes.
Packit 47f805
             * Much more than 3 makes not a big difference, it is only slower.
Packit 47f805
             */
Packit 47f805
Packit 47f805
            if (gfc->sv_qnt.substep_shaping & 2) {
Packit 47f805
                search_limit = 20;
Packit 47f805
            }
Packit 47f805
            else {
Packit 47f805
                search_limit = 3;
Packit 47f805
            }
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
            /* Check if the last scalefactor band is distorted.
Packit 47f805
             * in VBR mode we can't get rid of the distortion, so quit now
Packit 47f805
             * and VBR mode will try again with more bits.
Packit 47f805
             * (makes a 10% speed increase, the files I tested were
Packit 47f805
             * binary identical, 2000/05/20 Robert Hegemann)
Packit 47f805
             * distort[] > 1 means noise > allowed noise
Packit 47f805
             */
Packit 47f805
            if (gfc->sv_qnt.sfb21_extra) {
Packit 47f805
                if (distort[cod_info_w.sfbmax] > 1.0)
Packit 47f805
                    break;
Packit 47f805
                if (cod_info_w.block_type == SHORT_TYPE
Packit 47f805
                    && (distort[cod_info_w.sfbmax + 1] > 1.0
Packit 47f805
                        || distort[cod_info_w.sfbmax + 2] > 1.0))
Packit 47f805
                    break;
Packit 47f805
            }
Packit 47f805
Packit 47f805
            /* try a new scalefactor conbination on cod_info_w */
Packit 47f805
            if (balance_noise(gfc, &cod_info_w, distort, xrpow, bRefine) == 0)
Packit 47f805
                break;
Packit 47f805
            if (cod_info_w.scalefac_scale)
Packit 47f805
                maxggain = 254;
Packit 47f805
Packit 47f805
            /* inner_loop starts with the initial quantization step computed above
Packit 47f805
             * and slowly increases until the bits < huff_bits.
Packit 47f805
             * Thus it is important not to start with too large of an inital
Packit 47f805
             * quantization step.  Too small is ok, but inner_loop will take longer
Packit 47f805
             */
Packit 47f805
            huff_bits = targ_bits - cod_info_w.part2_length;
Packit 47f805
            if (huff_bits <= 0)
Packit 47f805
                break;
Packit 47f805
Packit 47f805
            /*  increase quantizer stepsize until needed bits are below maximum
Packit 47f805
             */
Packit 47f805
            while ((cod_info_w.part2_3_length
Packit 47f805
                    = count_bits(gfc, xrpow, &cod_info_w, &prev_noise)) > huff_bits
Packit 47f805
                   && cod_info_w.global_gain <= maxggain)
Packit 47f805
                cod_info_w.global_gain++;
Packit 47f805
Packit 47f805
            if (cod_info_w.global_gain > maxggain)
Packit 47f805
                break;
Packit 47f805
Packit 47f805
            if (best_noise_info.over_count == 0) {
Packit 47f805
Packit 47f805
                while ((cod_info_w.part2_3_length
Packit 47f805
                        = count_bits(gfc, xrpow, &cod_info_w, &prev_noise)) > best_part2_3_length
Packit 47f805
                       && cod_info_w.global_gain <= maxggain)
Packit 47f805
                    cod_info_w.global_gain++;
Packit 47f805
Packit 47f805
                if (cod_info_w.global_gain > maxggain)
Packit 47f805
                    break;
Packit 47f805
            }
Packit 47f805
Packit 47f805
            /* compute the distortion in this quantization */
Packit 47f805
            (void) calc_noise(&cod_info_w, l3_xmin, distort, &noise_info, &prev_noise);
Packit 47f805
            noise_info.bits = cod_info_w.part2_3_length;
Packit 47f805
Packit 47f805
            /* check if this quantization is better
Packit 47f805
             * than our saved quantization */
Packit 47f805
            if (cod_info->block_type != SHORT_TYPE) /* NORM, START or STOP type */
Packit 47f805
                better = cfg->quant_comp;
Packit 47f805
            else
Packit 47f805
                better = cfg->quant_comp_short;
Packit 47f805
Packit 47f805
Packit 47f805
            better = quant_compare(better, &best_noise_info, &noise_info, &cod_info_w, distort);
Packit 47f805
Packit 47f805
Packit 47f805
            /* save data so we can restore this quantization later */
Packit 47f805
            if (better) {
Packit 47f805
                best_part2_3_length = cod_info->part2_3_length;
Packit 47f805
                best_noise_info = noise_info;
Packit 47f805
                *cod_info = cod_info_w;
Packit 47f805
                age = 0;
Packit 47f805
                /* save data so we can restore this quantization later */
Packit 47f805
                /*if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh) */  {
Packit 47f805
                    /* store for later reuse */
Packit 47f805
                    memcpy(save_xrpow, xrpow, sizeof(FLOAT) * 576);
Packit 47f805
                }
Packit 47f805
            }
Packit 47f805
            else {
Packit 47f805
                /* early stop? */
Packit 47f805
                if (cfg->full_outer_loop == 0) {
Packit 47f805
                    if (++age > search_limit && best_noise_info.over_count == 0)
Packit 47f805
                        break;
Packit 47f805
                    if ((cfg->noise_shaping_amp == 3) && bRefine && age > 30)
Packit 47f805
                        break;
Packit 47f805
                    if ((cfg->noise_shaping_amp == 3) && bRefine &&
Packit 47f805
                        (cod_info_w.global_gain - best_ggain_pass1) > 15)
Packit 47f805
                        break;
Packit 47f805
                }
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
        while ((cod_info_w.global_gain + cod_info_w.scalefac_scale) < 255);
Packit 47f805
Packit 47f805
        if (cfg->noise_shaping_amp == 3) {
Packit 47f805
            if (!bRefine) {
Packit 47f805
                /* refine search */
Packit 47f805
                cod_info_w = *cod_info;
Packit 47f805
                memcpy(xrpow, save_xrpow, sizeof(FLOAT) * 576);
Packit 47f805
                age = 0;
Packit 47f805
                best_ggain_pass1 = cod_info_w.global_gain;
Packit 47f805
Packit 47f805
                bRefine = 1;
Packit 47f805
            }
Packit 47f805
            else {
Packit 47f805
                /* search already refined, stop */
Packit 47f805
                bEndOfSearch = 1;
Packit 47f805
            }
Packit 47f805
Packit 47f805
        }
Packit 47f805
        else {
Packit 47f805
            bEndOfSearch = 1;
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
Packit 47f805
    assert((cod_info->global_gain + cod_info->scalefac_scale) <= 255);
Packit 47f805
    /*  finish up
Packit 47f805
     */
Packit 47f805
    if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh || cfg->vbr == vbr_mt)
Packit 47f805
        /* restore for reuse on next try */
Packit 47f805
        memcpy(xrpow, save_xrpow, sizeof(FLOAT) * 576);
Packit 47f805
    /*  do the 'substep shaping'
Packit 47f805
     */
Packit 47f805
    else if (gfc->sv_qnt.substep_shaping & 1)
Packit 47f805
        trancate_smallspectrums(gfc, cod_info, l3_xmin, xrpow);
Packit 47f805
Packit 47f805
    return best_noise_info.over_count;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/************************************************************************
Packit 47f805
 *
Packit 47f805
 *      iteration_finish_one()
Packit 47f805
 *
Packit 47f805
 *  Robert Hegemann 2000-09-06
Packit 47f805
 *
Packit 47f805
 *  update reservoir status after FINAL quantization/bitrate
Packit 47f805
 *
Packit 47f805
 ************************************************************************/
Packit 47f805
Packit 47f805
static void
Packit 47f805
iteration_finish_one(lame_internal_flags * gfc, int gr, int ch)
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    III_side_info_t *const l3_side = &gfc->l3_side;
Packit 47f805
    gr_info *const cod_info = &l3_side->tt[gr][ch];
Packit 47f805
Packit 47f805
    /*  try some better scalefac storage
Packit 47f805
     */
Packit 47f805
    best_scalefac_store(gfc, gr, ch, l3_side);
Packit 47f805
Packit 47f805
    /*  best huffman_divide may save some bits too
Packit 47f805
     */
Packit 47f805
    if (cfg->use_best_huffman == 1)
Packit 47f805
        best_huffman_divide(gfc, cod_info);
Packit 47f805
Packit 47f805
    /*  update reservoir status after FINAL quantization/bitrate
Packit 47f805
     */
Packit 47f805
    ResvAdjust(gfc, cod_info);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/*********************************************************************
Packit 47f805
 *
Packit 47f805
 *      VBR_encode_granule()
Packit 47f805
 *
Packit 47f805
 *  2000-09-04 Robert Hegemann
Packit 47f805
 *
Packit 47f805
 *********************************************************************/
Packit 47f805
Packit 47f805
static void
Packit 47f805
VBR_encode_granule(lame_internal_flags * gfc, gr_info * const cod_info, const FLOAT * const l3_xmin, /* allowed distortion of the scalefactor */
Packit 47f805
                   FLOAT xrpow[576], /* coloured magnitudes of spectral values */
Packit 47f805
                   const int ch, int min_bits, int max_bits)
Packit 47f805
{
Packit 47f805
    gr_info bst_cod_info;
Packit 47f805
    FLOAT   bst_xrpow[576];
Packit 47f805
    int const Max_bits = max_bits;
Packit 47f805
    int     real_bits = max_bits + 1;
Packit 47f805
    int     this_bits = (max_bits + min_bits) / 2;
Packit 47f805
    int     dbits, over, found = 0;
Packit 47f805
    int const sfb21_extra = gfc->sv_qnt.sfb21_extra;
Packit 47f805
Packit 47f805
    assert(Max_bits <= MAX_BITS_PER_CHANNEL);
Packit 47f805
    memset(bst_cod_info.l3_enc, 0, sizeof(bst_cod_info.l3_enc));
Packit 47f805
Packit 47f805
    /*  search within round about 40 bits of optimal
Packit 47f805
     */
Packit 47f805
    do {
Packit 47f805
        assert(this_bits >= min_bits);
Packit 47f805
        assert(this_bits <= max_bits);
Packit 47f805
        assert(min_bits <= max_bits);
Packit 47f805
Packit 47f805
        if (this_bits > Max_bits - 42)
Packit 47f805
            gfc->sv_qnt.sfb21_extra = 0;
Packit 47f805
        else
Packit 47f805
            gfc->sv_qnt.sfb21_extra = sfb21_extra;
Packit 47f805
Packit 47f805
        over = outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, this_bits);
Packit 47f805
Packit 47f805
        /*  is quantization as good as we are looking for ?
Packit 47f805
         *  in this case: is no scalefactor band distorted?
Packit 47f805
         */
Packit 47f805
        if (over <= 0) {
Packit 47f805
            found = 1;
Packit 47f805
            /*  now we know it can be done with "real_bits"
Packit 47f805
             *  and maybe we can skip some iterations
Packit 47f805
             */
Packit 47f805
            real_bits = cod_info->part2_3_length;
Packit 47f805
Packit 47f805
            /*  store best quantization so far
Packit 47f805
             */
Packit 47f805
            bst_cod_info = *cod_info;
Packit 47f805
            memcpy(bst_xrpow, xrpow, sizeof(FLOAT) * 576);
Packit 47f805
Packit 47f805
            /*  try with fewer bits
Packit 47f805
             */
Packit 47f805
            max_bits = real_bits - 32;
Packit 47f805
            dbits = max_bits - min_bits;
Packit 47f805
            this_bits = (max_bits + min_bits) / 2;
Packit 47f805
        }
Packit 47f805
        else {
Packit 47f805
            /*  try with more bits
Packit 47f805
             */
Packit 47f805
            min_bits = this_bits + 32;
Packit 47f805
            dbits = max_bits - min_bits;
Packit 47f805
            this_bits = (max_bits + min_bits) / 2;
Packit 47f805
Packit 47f805
            if (found) {
Packit 47f805
                found = 2;
Packit 47f805
                /*  start again with best quantization so far
Packit 47f805
                 */
Packit 47f805
                *cod_info = bst_cod_info;
Packit 47f805
                memcpy(xrpow, bst_xrpow, sizeof(FLOAT) * 576);
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
    } while (dbits > 12);
Packit 47f805
Packit 47f805
    gfc->sv_qnt.sfb21_extra = sfb21_extra;
Packit 47f805
Packit 47f805
    /*  found=0 => nothing found, use last one
Packit 47f805
     *  found=1 => we just found the best and left the loop
Packit 47f805
     *  found=2 => we restored a good one and have now l3_enc to restore too
Packit 47f805
     */
Packit 47f805
    if (found == 2) {
Packit 47f805
        memcpy(cod_info->l3_enc, bst_cod_info.l3_enc, sizeof(int) * 576);
Packit 47f805
    }
Packit 47f805
    assert(cod_info->part2_3_length <= Max_bits);
Packit 47f805
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/************************************************************************
Packit 47f805
 *
Packit 47f805
 *      get_framebits()
Packit 47f805
 *
Packit 47f805
 *  Robert Hegemann 2000-09-05
Packit 47f805
 *
Packit 47f805
 *  calculates
Packit 47f805
 *  * how many bits are available for analog silent granules
Packit 47f805
 *  * how many bits to use for the lowest allowed bitrate
Packit 47f805
 *  * how many bits each bitrate would provide
Packit 47f805
 *
Packit 47f805
 ************************************************************************/
Packit 47f805
Packit 47f805
static void
Packit 47f805
get_framebits(lame_internal_flags * gfc, int frameBits[15])
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    EncResult_t *const eov = &gfc->ov_enc;
Packit 47f805
    int     bitsPerFrame, i;
Packit 47f805
Packit 47f805
    /*  always use at least this many bits per granule per channel
Packit 47f805
     *  unless we detect analog silence, see below
Packit 47f805
     */
Packit 47f805
    eov->bitrate_index = cfg->vbr_min_bitrate_index;
Packit 47f805
    bitsPerFrame = getframebits(gfc);
Packit 47f805
Packit 47f805
    /*  bits for analog silence
Packit 47f805
     */
Packit 47f805
    eov->bitrate_index = 1;
Packit 47f805
    bitsPerFrame = getframebits(gfc);
Packit 47f805
Packit 47f805
    for (i = 1; i <= cfg->vbr_max_bitrate_index; i++) {
Packit 47f805
        eov->bitrate_index = i;
Packit 47f805
        frameBits[i] = ResvFrameBegin(gfc, &bitsPerFrame);
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/*********************************************************************
Packit 47f805
 *
Packit 47f805
 *      VBR_prepare()
Packit 47f805
 *
Packit 47f805
 *  2000-09-04 Robert Hegemann
Packit 47f805
 *
Packit 47f805
 *  * converts LR to MS coding when necessary
Packit 47f805
 *  * calculates allowed/adjusted quantization noise amounts
Packit 47f805
 *  * detects analog silent frames
Packit 47f805
 *
Packit 47f805
 *  some remarks:
Packit 47f805
 *  - lower masking depending on Quality setting
Packit 47f805
 *  - quality control together with adjusted ATH MDCT scaling
Packit 47f805
 *    on lower quality setting allocate more noise from
Packit 47f805
 *    ATH masking, and on higher quality setting allocate
Packit 47f805
 *    less noise from ATH masking.
Packit 47f805
 *  - experiments show that going more than 2dB over GPSYCHO's
Packit 47f805
 *    limits ends up in very annoying artefacts
Packit 47f805
 *
Packit 47f805
 *********************************************************************/
Packit 47f805
Packit 47f805
/* RH: this one needs to be overhauled sometime */
Packit 47f805
Packit 47f805
static int
Packit 47f805
VBR_old_prepare(lame_internal_flags * gfc,
Packit 47f805
                const FLOAT pe[2][2], FLOAT const ms_ener_ratio[2],
Packit 47f805
                const III_psy_ratio ratio[2][2],
Packit 47f805
                FLOAT l3_xmin[2][2][SFBMAX],
Packit 47f805
                int frameBits[16], int min_bits[2][2], int max_bits[2][2], int bands[2][2])
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    EncResult_t *const eov = &gfc->ov_enc;
Packit 47f805
Packit 47f805
    FLOAT   masking_lower_db, adjust = 0.0;
Packit 47f805
    int     gr, ch;
Packit 47f805
    int     analog_silence = 1;
Packit 47f805
    int     avg, mxb, bits = 0;
Packit 47f805
Packit 47f805
    eov->bitrate_index = cfg->vbr_max_bitrate_index;
Packit 47f805
    avg = ResvFrameBegin(gfc, &avg) / cfg->mode_gr;
Packit 47f805
Packit 47f805
    get_framebits(gfc, frameBits);
Packit 47f805
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
        mxb = on_pe(gfc, pe, max_bits[gr], avg, gr, 0);
Packit 47f805
        if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
Packit 47f805
            ms_convert(&gfc->l3_side, gr);
Packit 47f805
            reduce_side(max_bits[gr], ms_ener_ratio[gr], avg, mxb);
Packit 47f805
        }
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ++ch) {
Packit 47f805
            gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
Packit 47f805
Packit 47f805
            if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type */
Packit 47f805
                adjust = 1.28 / (1 + exp(3.5 - pe[gr][ch] / 300.)) - 0.05;
Packit 47f805
                masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
Packit 47f805
            }
Packit 47f805
            else {
Packit 47f805
                adjust = 2.56 / (1 + exp(3.5 - pe[gr][ch] / 300.)) - 0.14;
Packit 47f805
                masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
Packit 47f805
            }
Packit 47f805
            gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
Packit 47f805
Packit 47f805
            init_outer_loop(gfc, cod_info);
Packit 47f805
            bands[gr][ch] = calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin[gr][ch]);
Packit 47f805
            if (bands[gr][ch])
Packit 47f805
                analog_silence = 0;
Packit 47f805
Packit 47f805
            min_bits[gr][ch] = 126;
Packit 47f805
Packit 47f805
            bits += max_bits[gr][ch];
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
            if (bits > frameBits[cfg->vbr_max_bitrate_index] && bits > 0) {
Packit 47f805
                max_bits[gr][ch] *= frameBits[cfg->vbr_max_bitrate_index];
Packit 47f805
                max_bits[gr][ch] /= bits;
Packit 47f805
            }
Packit 47f805
            if (min_bits[gr][ch] > max_bits[gr][ch])
Packit 47f805
                min_bits[gr][ch] = max_bits[gr][ch];
Packit 47f805
Packit 47f805
        }               /* for ch */
Packit 47f805
    }                   /* for gr */
Packit 47f805
Packit 47f805
    return analog_silence;
Packit 47f805
}
Packit 47f805
Packit 47f805
static void
Packit 47f805
bitpressure_strategy(lame_internal_flags const *gfc,
Packit 47f805
                     FLOAT l3_xmin[2][2][SFBMAX], const int min_bits[2][2], int max_bits[2][2])
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    int     gr, ch, sfb;
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
            gr_info const *const gi = &gfc->l3_side.tt[gr][ch];
Packit 47f805
            FLOAT  *pxmin = l3_xmin[gr][ch];
Packit 47f805
            for (sfb = 0; sfb < gi->psy_lmax; sfb++)
Packit 47f805
                *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_l / SBMAX_l;
Packit 47f805
Packit 47f805
            if (gi->block_type == SHORT_TYPE) {
Packit 47f805
                for (sfb = gi->sfb_smin; sfb < SBMAX_s; sfb++) {
Packit 47f805
                    *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_s / SBMAX_s;
Packit 47f805
                    *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_s / SBMAX_s;
Packit 47f805
                    *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_s / SBMAX_s;
Packit 47f805
                }
Packit 47f805
            }
Packit 47f805
            max_bits[gr][ch] = Max(min_bits[gr][ch], 0.9 * max_bits[gr][ch]);
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
/************************************************************************
Packit 47f805
 *
Packit 47f805
 *      VBR_iteration_loop()
Packit 47f805
 *
Packit 47f805
 *  tries to find out how many bits are needed for each granule and channel
Packit 47f805
 *  to get an acceptable quantization. An appropriate bitrate will then be
Packit 47f805
 *  choosed for quantization.  rh 8/99
Packit 47f805
 *
Packit 47f805
 *  Robert Hegemann 2000-09-06 rewrite
Packit 47f805
 *
Packit 47f805
 ************************************************************************/
Packit 47f805
Packit 47f805
void
Packit 47f805
VBR_old_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
Packit 47f805
                       const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    EncResult_t *const eov = &gfc->ov_enc;
Packit 47f805
    FLOAT   l3_xmin[2][2][SFBMAX];
Packit 47f805
Packit 47f805
    FLOAT   xrpow[576];
Packit 47f805
    int     bands[2][2];
Packit 47f805
    int     frameBits[15];
Packit 47f805
    int     used_bits;
Packit 47f805
    int     bits;
Packit 47f805
    int     min_bits[2][2], max_bits[2][2];
Packit 47f805
    int     mean_bits;
Packit 47f805
    int     ch, gr, analog_silence;
Packit 47f805
    III_side_info_t *const l3_side = &gfc->l3_side;
Packit 47f805
Packit 47f805
    analog_silence = VBR_old_prepare(gfc, pe, ms_ener_ratio, ratio,
Packit 47f805
                                     l3_xmin, frameBits, min_bits, max_bits, bands);
Packit 47f805
Packit 47f805
    /*---------------------------------*/
Packit 47f805
    for (;;) {
Packit 47f805
Packit 47f805
        /*  quantize granules with lowest possible number of bits
Packit 47f805
         */
Packit 47f805
Packit 47f805
        used_bits = 0;
Packit 47f805
Packit 47f805
        for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
            for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
                int     ret;
Packit 47f805
                gr_info *const cod_info = &l3_side->tt[gr][ch];
Packit 47f805
Packit 47f805
                /*  init_outer_loop sets up cod_info, scalefac and xrpow
Packit 47f805
                 */
Packit 47f805
                ret = init_xrpow(gfc, cod_info, xrpow);
Packit 47f805
                if (ret == 0 || max_bits[gr][ch] == 0) {
Packit 47f805
                    /*  xr contains no energy
Packit 47f805
                     *  l3_enc, our encoding data, will be quantized to zero
Packit 47f805
                     */
Packit 47f805
                    continue; /* with next channel */
Packit 47f805
                }
Packit 47f805
Packit 47f805
                VBR_encode_granule(gfc, cod_info, l3_xmin[gr][ch], xrpow,
Packit 47f805
                                   ch, min_bits[gr][ch], max_bits[gr][ch]);
Packit 47f805
Packit 47f805
                /*  do the 'substep shaping'
Packit 47f805
                 */
Packit 47f805
                if (gfc->sv_qnt.substep_shaping & 1) {
Packit 47f805
                    trancate_smallspectrums(gfc, &l3_side->tt[gr][ch], l3_xmin[gr][ch], xrpow);
Packit 47f805
                }
Packit 47f805
Packit 47f805
                ret = cod_info->part2_3_length + cod_info->part2_length;
Packit 47f805
                used_bits += ret;
Packit 47f805
            }           /* for ch */
Packit 47f805
        }               /* for gr */
Packit 47f805
Packit 47f805
        /*  find lowest bitrate able to hold used bits
Packit 47f805
         */
Packit 47f805
        if (analog_silence && !cfg->enforce_min_bitrate)
Packit 47f805
            /*  we detected analog silence and the user did not specify
Packit 47f805
             *  any hard framesize limit, so start with smallest possible frame
Packit 47f805
             */
Packit 47f805
            eov->bitrate_index = 1;
Packit 47f805
        else
Packit 47f805
            eov->bitrate_index = cfg->vbr_min_bitrate_index;
Packit 47f805
Packit 47f805
        for (; eov->bitrate_index < cfg->vbr_max_bitrate_index; eov->bitrate_index++) {
Packit 47f805
            if (used_bits <= frameBits[eov->bitrate_index])
Packit 47f805
                break;
Packit 47f805
        }
Packit 47f805
        bits = ResvFrameBegin(gfc, &mean_bits);
Packit 47f805
Packit 47f805
        if (used_bits <= bits)
Packit 47f805
            break;
Packit 47f805
Packit 47f805
        bitpressure_strategy(gfc, l3_xmin, (const int (*)[2])min_bits, max_bits);
Packit 47f805
Packit 47f805
    }                   /* breaks adjusted */
Packit 47f805
    /*--------------------------------------*/
Packit 47f805
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
            iteration_finish_one(gfc, gr, ch);
Packit 47f805
        }               /* for ch */
Packit 47f805
    }                   /* for gr */
Packit 47f805
    ResvFrameEnd(gfc, mean_bits);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
static int
Packit 47f805
VBR_new_prepare(lame_internal_flags * gfc,
Packit 47f805
                const FLOAT pe[2][2], const III_psy_ratio ratio[2][2],
Packit 47f805
                FLOAT l3_xmin[2][2][SFBMAX], int frameBits[16], int max_bits[2][2],
Packit 47f805
                int* max_resv)
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    EncResult_t *const eov = &gfc->ov_enc;
Packit 47f805
Packit 47f805
    int     gr, ch;
Packit 47f805
    int     analog_silence = 1;
Packit 47f805
    int     avg, bits = 0;
Packit 47f805
    int     maximum_framebits;
Packit 47f805
Packit 47f805
    if (!cfg->free_format) {
Packit 47f805
        eov->bitrate_index = cfg->vbr_max_bitrate_index;
Packit 47f805
        (void) ResvFrameBegin(gfc, &avg);
Packit 47f805
        *max_resv = gfc->sv_enc.ResvMax;
Packit 47f805
Packit 47f805
        get_framebits(gfc, frameBits);
Packit 47f805
        maximum_framebits = frameBits[cfg->vbr_max_bitrate_index];
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        eov->bitrate_index = 0;
Packit 47f805
        maximum_framebits = ResvFrameBegin(gfc, &avg);
Packit 47f805
        frameBits[0] = maximum_framebits;
Packit 47f805
        *max_resv = gfc->sv_enc.ResvMax;
Packit 47f805
    }
Packit 47f805
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
        (void) on_pe(gfc, pe, max_bits[gr], avg, gr, 0);
Packit 47f805
        if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
Packit 47f805
            ms_convert(&gfc->l3_side, gr);
Packit 47f805
        }
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ++ch) {
Packit 47f805
            gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
Packit 47f805
Packit 47f805
            gfc->sv_qnt.masking_lower = pow(10.0, gfc->sv_qnt.mask_adjust * 0.1);
Packit 47f805
Packit 47f805
            init_outer_loop(gfc, cod_info);
Packit 47f805
            if (0 != calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin[gr][ch]))
Packit 47f805
                analog_silence = 0;
Packit 47f805
Packit 47f805
            bits += max_bits[gr][ch];
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
            if (bits > maximum_framebits && bits > 0) {
Packit 47f805
                max_bits[gr][ch] *= maximum_framebits;
Packit 47f805
                max_bits[gr][ch] /= bits;
Packit 47f805
            }
Packit 47f805
Packit 47f805
        }               /* for ch */
Packit 47f805
    }                   /* for gr */
Packit 47f805
    if (analog_silence) {
Packit 47f805
        *max_resv = 0;
Packit 47f805
    }
Packit 47f805
    return analog_silence;
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
void
Packit 47f805
VBR_new_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
Packit 47f805
                       const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    EncResult_t *const eov = &gfc->ov_enc;
Packit 47f805
    FLOAT   l3_xmin[2][2][SFBMAX];
Packit 47f805
Packit 47f805
    FLOAT   xrpow[2][2][576];
Packit 47f805
    int     frameBits[15];
Packit 47f805
    int     used_bits;
Packit 47f805
    int     max_bits[2][2];
Packit 47f805
    int     ch, gr, analog_silence, pad;
Packit 47f805
    III_side_info_t *const l3_side = &gfc->l3_side;
Packit 47f805
Packit 47f805
    const FLOAT (*const_l3_xmin)[2][SFBMAX] = (const FLOAT (*)[2][SFBMAX])l3_xmin;
Packit 47f805
    const FLOAT (*const_xrpow)[2][576] = (const FLOAT (*)[2][576])xrpow;
Packit 47f805
    const int (*const_max_bits)[2] = (const int (*)[2])max_bits;
Packit 47f805
    
Packit 47f805
    (void) ms_ener_ratio; /* not used */
Packit 47f805
Packit 47f805
    memset(xrpow, 0, sizeof(xrpow));
Packit 47f805
Packit 47f805
    analog_silence = VBR_new_prepare(gfc, pe, ratio, l3_xmin, frameBits, max_bits, &pad;;
Packit 47f805
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
            gr_info *const cod_info = &l3_side->tt[gr][ch];
Packit 47f805
Packit 47f805
            /*  init_outer_loop sets up cod_info, scalefac and xrpow
Packit 47f805
             */
Packit 47f805
            if (0 == init_xrpow(gfc, cod_info, xrpow[gr][ch])) {
Packit 47f805
                max_bits[gr][ch] = 0; /* silent granule needs no bits */
Packit 47f805
            }
Packit 47f805
        }               /* for ch */
Packit 47f805
    }                   /* for gr */
Packit 47f805
Packit 47f805
    /*  quantize granules with lowest possible number of bits
Packit 47f805
     */
Packit 47f805
Packit 47f805
    used_bits = VBR_encode_frame(gfc, const_xrpow, const_l3_xmin, const_max_bits);
Packit 47f805
Packit 47f805
    if (!cfg->free_format) {
Packit 47f805
        int     i, j;
Packit 47f805
Packit 47f805
        /*  find lowest bitrate able to hold used bits
Packit 47f805
         */
Packit 47f805
        if (analog_silence && !cfg->enforce_min_bitrate) {
Packit 47f805
            /*  we detected analog silence and the user did not specify
Packit 47f805
             *  any hard framesize limit, so start with smallest possible frame
Packit 47f805
             */
Packit 47f805
            i = 1;
Packit 47f805
        }
Packit 47f805
        else {
Packit 47f805
            i = cfg->vbr_min_bitrate_index;
Packit 47f805
        }
Packit 47f805
Packit 47f805
        for (; i < cfg->vbr_max_bitrate_index; i++) {
Packit 47f805
            if (used_bits <= frameBits[i]) 
Packit 47f805
                break;
Packit 47f805
        }
Packit 47f805
        if (i > cfg->vbr_max_bitrate_index) {
Packit 47f805
            i = cfg->vbr_max_bitrate_index;
Packit 47f805
        }
Packit 47f805
        if (pad > 0) {
Packit 47f805
            for (j = cfg->vbr_max_bitrate_index; j > i; --j) {
Packit 47f805
                int const unused = frameBits[j] - used_bits;
Packit 47f805
                if (unused <= pad) 
Packit 47f805
                    break;
Packit 47f805
            }
Packit 47f805
            eov->bitrate_index = j;
Packit 47f805
        }
Packit 47f805
        else {
Packit 47f805
            eov->bitrate_index = i;
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
#if 0
Packit 47f805
        static int mmm = 0;
Packit 47f805
        int     fff = getFramesize_kbps(gfc, used_bits);
Packit 47f805
        int     hhh = getFramesize_kbps(gfc, MAX_BITS_PER_GRANULE * cfg->mode_gr);
Packit 47f805
        if (mmm < fff)
Packit 47f805
            mmm = fff;
Packit 47f805
        printf("demand=%3d kbps  max=%3d kbps   limit=%3d kbps\n", fff, mmm, hhh);
Packit 47f805
#endif
Packit 47f805
        eov->bitrate_index = 0;
Packit 47f805
    }
Packit 47f805
    if (used_bits <= frameBits[eov->bitrate_index]) {
Packit 47f805
        /* update Reservoire status */
Packit 47f805
        int     mean_bits, fullframebits;
Packit 47f805
        fullframebits = ResvFrameBegin(gfc, &mean_bits);
Packit 47f805
        assert(used_bits <= fullframebits);
Packit 47f805
        for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
            for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
                gr_info const *const cod_info = &l3_side->tt[gr][ch];
Packit 47f805
                ResvAdjust(gfc, cod_info);
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
        ResvFrameEnd(gfc, mean_bits);
Packit 47f805
    }
Packit 47f805
    else {
Packit 47f805
        /* SHOULD NOT HAPPEN INTERNAL ERROR
Packit 47f805
         */
Packit 47f805
        ERRORF(gfc, "INTERNAL ERROR IN VBR NEW CODE, please send bug report\n");
Packit 47f805
        exit(-1);
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/********************************************************************
Packit 47f805
 *
Packit 47f805
 *  calc_target_bits()
Packit 47f805
 *
Packit 47f805
 *  calculates target bits for ABR encoding
Packit 47f805
 *
Packit 47f805
 *  mt 2000/05/31
Packit 47f805
 *
Packit 47f805
 ********************************************************************/
Packit 47f805
Packit 47f805
static void
Packit 47f805
calc_target_bits(lame_internal_flags * gfc,
Packit 47f805
                 const FLOAT pe[2][2],
Packit 47f805
                 FLOAT const ms_ener_ratio[2],
Packit 47f805
                 int targ_bits[2][2], int *analog_silence_bits, int *max_frame_bits)
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    EncResult_t *const eov = &gfc->ov_enc;
Packit 47f805
    III_side_info_t const *const l3_side = &gfc->l3_side;
Packit 47f805
    FLOAT   res_factor;
Packit 47f805
    int     gr, ch, totbits, mean_bits;
Packit 47f805
    int     framesize = 576 * cfg->mode_gr;
Packit 47f805
Packit 47f805
    eov->bitrate_index = cfg->vbr_max_bitrate_index;
Packit 47f805
    *max_frame_bits = ResvFrameBegin(gfc, &mean_bits);
Packit 47f805
Packit 47f805
    eov->bitrate_index = 1;
Packit 47f805
    mean_bits = getframebits(gfc) - cfg->sideinfo_len * 8;
Packit 47f805
    *analog_silence_bits = mean_bits / (cfg->mode_gr * cfg->channels_out);
Packit 47f805
Packit 47f805
    mean_bits = cfg->vbr_avg_bitrate_kbps * framesize * 1000;
Packit 47f805
    if (gfc->sv_qnt.substep_shaping & 1)
Packit 47f805
        mean_bits *= 1.09;
Packit 47f805
    mean_bits /= cfg->samplerate_out;
Packit 47f805
    mean_bits -= cfg->sideinfo_len * 8;
Packit 47f805
    mean_bits /= (cfg->mode_gr * cfg->channels_out);
Packit 47f805
Packit 47f805
    /*
Packit 47f805
       res_factor is the percentage of the target bitrate that should
Packit 47f805
       be used on average.  the remaining bits are added to the
Packit 47f805
       bitreservoir and used for difficult to encode frames.
Packit 47f805
Packit 47f805
       Since we are tracking the average bitrate, we should adjust
Packit 47f805
       res_factor "on the fly", increasing it if the average bitrate
Packit 47f805
       is greater than the requested bitrate, and decreasing it
Packit 47f805
       otherwise.  Reasonable ranges are from .9 to 1.0
Packit 47f805
Packit 47f805
       Until we get the above suggestion working, we use the following
Packit 47f805
       tuning:
Packit 47f805
       compression ratio    res_factor
Packit 47f805
       5.5  (256kbps)         1.0      no need for bitreservoir
Packit 47f805
       11   (128kbps)         .93      7% held for reservoir
Packit 47f805
Packit 47f805
       with linear interpolation for other values.
Packit 47f805
Packit 47f805
     */
Packit 47f805
    res_factor = .93 + .07 * (11.0 - cfg->compression_ratio) / (11.0 - 5.5);
Packit 47f805
    if (res_factor < .90)
Packit 47f805
        res_factor = .90;
Packit 47f805
    if (res_factor > 1.00)
Packit 47f805
        res_factor = 1.00;
Packit 47f805
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
        int     sum = 0;
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
            targ_bits[gr][ch] = res_factor * mean_bits;
Packit 47f805
Packit 47f805
            if (pe[gr][ch] > 700) {
Packit 47f805
                int     add_bits = (pe[gr][ch] - 700) / 1.4;
Packit 47f805
Packit 47f805
                gr_info const *const cod_info = &l3_side->tt[gr][ch];
Packit 47f805
                targ_bits[gr][ch] = res_factor * mean_bits;
Packit 47f805
Packit 47f805
                /* short blocks use a little extra, no matter what the pe */
Packit 47f805
                if (cod_info->block_type == SHORT_TYPE) {
Packit 47f805
                    if (add_bits < mean_bits / 2)
Packit 47f805
                        add_bits = mean_bits / 2;
Packit 47f805
                }
Packit 47f805
                /* at most increase bits by 1.5*average */
Packit 47f805
                if (add_bits > mean_bits * 3 / 2)
Packit 47f805
                    add_bits = mean_bits * 3 / 2;
Packit 47f805
                else if (add_bits < 0)
Packit 47f805
                    add_bits = 0;
Packit 47f805
Packit 47f805
                targ_bits[gr][ch] += add_bits;
Packit 47f805
            }
Packit 47f805
            if (targ_bits[gr][ch] > MAX_BITS_PER_CHANNEL) {
Packit 47f805
                targ_bits[gr][ch] = MAX_BITS_PER_CHANNEL;
Packit 47f805
            }
Packit 47f805
            sum += targ_bits[gr][ch];
Packit 47f805
        }               /* for ch */
Packit 47f805
        if (sum > MAX_BITS_PER_GRANULE) {
Packit 47f805
            for (ch = 0; ch < cfg->channels_out; ++ch) {
Packit 47f805
                targ_bits[gr][ch] *= MAX_BITS_PER_GRANULE;
Packit 47f805
                targ_bits[gr][ch] /= sum;
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
    }                   /* for gr */
Packit 47f805
Packit 47f805
    if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR)
Packit 47f805
        for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
            reduce_side(targ_bits[gr], ms_ener_ratio[gr], mean_bits * cfg->channels_out,
Packit 47f805
                        MAX_BITS_PER_GRANULE);
Packit 47f805
        }
Packit 47f805
Packit 47f805
    /*  sum target bits
Packit 47f805
     */
Packit 47f805
    totbits = 0;
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
            if (targ_bits[gr][ch] > MAX_BITS_PER_CHANNEL)
Packit 47f805
                targ_bits[gr][ch] = MAX_BITS_PER_CHANNEL;
Packit 47f805
            totbits += targ_bits[gr][ch];
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
Packit 47f805
    /*  repartion target bits if needed
Packit 47f805
     */
Packit 47f805
    if (totbits > *max_frame_bits && totbits > 0) {
Packit 47f805
        for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
            for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
                targ_bits[gr][ch] *= *max_frame_bits;
Packit 47f805
                targ_bits[gr][ch] /= totbits;
Packit 47f805
            }
Packit 47f805
        }
Packit 47f805
    }
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/********************************************************************
Packit 47f805
 *
Packit 47f805
 *  ABR_iteration_loop()
Packit 47f805
 *
Packit 47f805
 *  encode a frame with a disired average bitrate
Packit 47f805
 *
Packit 47f805
 *  mt 2000/05/31
Packit 47f805
 *
Packit 47f805
 ********************************************************************/
Packit 47f805
Packit 47f805
void
Packit 47f805
ABR_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
Packit 47f805
                   const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    EncResult_t *const eov = &gfc->ov_enc;
Packit 47f805
    FLOAT   l3_xmin[SFBMAX];
Packit 47f805
    FLOAT   xrpow[576];
Packit 47f805
    int     targ_bits[2][2];
Packit 47f805
    int     mean_bits, max_frame_bits;
Packit 47f805
    int     ch, gr, ath_over;
Packit 47f805
    int     analog_silence_bits;
Packit 47f805
    gr_info *cod_info;
Packit 47f805
    III_side_info_t *const l3_side = &gfc->l3_side;
Packit 47f805
Packit 47f805
    mean_bits = 0;
Packit 47f805
Packit 47f805
    calc_target_bits(gfc, pe, ms_ener_ratio, targ_bits, &analog_silence_bits, &max_frame_bits);
Packit 47f805
Packit 47f805
    /*  encode granules
Packit 47f805
     */
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
Packit 47f805
        if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
Packit 47f805
            ms_convert(&gfc->l3_side, gr);
Packit 47f805
        }
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
            FLOAT   adjust, masking_lower_db;
Packit 47f805
            cod_info = &l3_side->tt[gr][ch];
Packit 47f805
Packit 47f805
            if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type */
Packit 47f805
                /* adjust = 1.28/(1+exp(3.5-pe[gr][ch]/300.))-0.05; */
Packit 47f805
                adjust = 0;
Packit 47f805
                masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
Packit 47f805
            }
Packit 47f805
            else {
Packit 47f805
                /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14; */
Packit 47f805
                adjust = 0;
Packit 47f805
                masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
Packit 47f805
            }
Packit 47f805
            gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
Packit 47f805
Packit 47f805
Packit 47f805
            /*  cod_info, scalefac and xrpow get initialized in init_outer_loop
Packit 47f805
             */
Packit 47f805
            init_outer_loop(gfc, cod_info);
Packit 47f805
            if (init_xrpow(gfc, cod_info, xrpow)) {
Packit 47f805
                /*  xr contains energy we will have to encode
Packit 47f805
                 *  calculate the masking abilities
Packit 47f805
                 *  find some good quantization in outer_loop
Packit 47f805
                 */
Packit 47f805
                ath_over = calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin);
Packit 47f805
                if (0 == ath_over) /* analog silence */
Packit 47f805
                    targ_bits[gr][ch] = analog_silence_bits;
Packit 47f805
Packit 47f805
                (void) outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, targ_bits[gr][ch]);
Packit 47f805
            }
Packit 47f805
            iteration_finish_one(gfc, gr, ch);
Packit 47f805
        }               /* ch */
Packit 47f805
    }                   /* gr */
Packit 47f805
Packit 47f805
    /*  find a bitrate which can refill the resevoir to positive size.
Packit 47f805
     */
Packit 47f805
    for (eov->bitrate_index = cfg->vbr_min_bitrate_index;
Packit 47f805
         eov->bitrate_index <= cfg->vbr_max_bitrate_index; eov->bitrate_index++) {
Packit 47f805
        if (ResvFrameBegin(gfc, &mean_bits) >= 0)
Packit 47f805
            break;
Packit 47f805
    }
Packit 47f805
    assert(eov->bitrate_index <= cfg->vbr_max_bitrate_index);
Packit 47f805
Packit 47f805
    ResvFrameEnd(gfc, mean_bits);
Packit 47f805
}
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
Packit 47f805
/************************************************************************
Packit 47f805
 *
Packit 47f805
 *      CBR_iteration_loop()
Packit 47f805
 *
Packit 47f805
 *  author/date??
Packit 47f805
 *
Packit 47f805
 *  encodes one frame of MP3 data with constant bitrate
Packit 47f805
 *
Packit 47f805
 ************************************************************************/
Packit 47f805
Packit 47f805
void
Packit 47f805
CBR_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
Packit 47f805
                   const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
Packit 47f805
{
Packit 47f805
    SessionConfig_t const *const cfg = &gfc->cfg;
Packit 47f805
    FLOAT   l3_xmin[SFBMAX];
Packit 47f805
    FLOAT   xrpow[576];
Packit 47f805
    int     targ_bits[2];
Packit 47f805
    int     mean_bits, max_bits;
Packit 47f805
    int     gr, ch;
Packit 47f805
    III_side_info_t *const l3_side = &gfc->l3_side;
Packit 47f805
    gr_info *cod_info;
Packit 47f805
Packit 47f805
    (void) ResvFrameBegin(gfc, &mean_bits);
Packit 47f805
Packit 47f805
    /* quantize! */
Packit 47f805
    for (gr = 0; gr < cfg->mode_gr; gr++) {
Packit 47f805
Packit 47f805
        /*  calculate needed bits
Packit 47f805
         */
Packit 47f805
        max_bits = on_pe(gfc, pe, targ_bits, mean_bits, gr, gr);
Packit 47f805
Packit 47f805
        if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
Packit 47f805
            ms_convert(&gfc->l3_side, gr);
Packit 47f805
            reduce_side(targ_bits, ms_ener_ratio[gr], mean_bits, max_bits);
Packit 47f805
        }
Packit 47f805
Packit 47f805
        for (ch = 0; ch < cfg->channels_out; ch++) {
Packit 47f805
            FLOAT   adjust, masking_lower_db;
Packit 47f805
            cod_info = &l3_side->tt[gr][ch];
Packit 47f805
Packit 47f805
            if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type */
Packit 47f805
                /* adjust = 1.28/(1+exp(3.5-pe[gr][ch]/300.))-0.05; */
Packit 47f805
                adjust = 0;
Packit 47f805
                masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
Packit 47f805
            }
Packit 47f805
            else {
Packit 47f805
                /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14; */
Packit 47f805
                adjust = 0;
Packit 47f805
                masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
Packit 47f805
            }
Packit 47f805
            gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
Packit 47f805
Packit 47f805
            /*  init_outer_loop sets up cod_info, scalefac and xrpow
Packit 47f805
             */
Packit 47f805
            init_outer_loop(gfc, cod_info);
Packit 47f805
            if (init_xrpow(gfc, cod_info, xrpow)) {
Packit 47f805
                /*  xr contains energy we will have to encode
Packit 47f805
                 *  calculate the masking abilities
Packit 47f805
                 *  find some good quantization in outer_loop
Packit 47f805
                 */
Packit 47f805
                (void) calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin);
Packit 47f805
                (void) outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, targ_bits[ch]);
Packit 47f805
            }
Packit 47f805
Packit 47f805
            iteration_finish_one(gfc, gr, ch);
Packit 47f805
            assert(cod_info->part2_3_length <= MAX_BITS_PER_CHANNEL);
Packit 47f805
            assert(cod_info->part2_3_length <= targ_bits[ch]);
Packit 47f805
        }               /* for ch */
Packit 47f805
    }                   /* for gr */
Packit 47f805
Packit 47f805
    ResvFrameEnd(gfc, mean_bits);
Packit 47f805
}