📄 loop.c
字号:
/*
(c) Copyright 1998, 1999 - Tord Jansson
=======================================
This file is part of the BladeEnc MP3 Encoder, based on
ISO's reference code for MPEG Layer 3 compression, and might
contain smaller or larger sections that are directly taken
from ISO's reference code.
All changes to the ISO reference code herein are either
copyrighted by Tord Jansson (tord.jansson@swipnet.se)
or sublicensed to Tord Jansson by a third party.
BladeEnc is free software; you can redistribute this file
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "system.h"
#include "l3side.h"
#include "loop.h"
#include "huffman.h"
#include "l3bitstream.h"
#include "reservoir.h"
#include "loop-pvt.h"
int bin_search_StepSize(int desired_rate, double start, int ix[576],
double xrs[576], gr_info * cod_info);
int count_bits();
float worst_xfsf_to_xmin_ratio();
int optimized_calculations_tj( int * abs_ix, gr_info *cod_info );
int bigv_bitcount_tj( int * abs_ix, gr_info *gi );
/*
Here are MPEG1 Table B.8 and MPEG2 Table B.1
-- Layer III scalefactor bands.
Index into this using a method such as:
idx = fr_ps->header->sampling_frequency
+ (fr_ps->header->version * 3)
*/
struct scalefac_struct sfBandIndex[3] =
{
{ /* Table B.8.b: 44.1 kHz */
{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
{0,4,8,12,16,22,30,40,52,66,84,106,136,192}
},
{ /* Table B.8.c: 48 kHz */
{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
{0,4,8,12,16,22,28,38,50,64,80,100,126,192}
},
{ /* Table B.8.a: 32 kHz */
{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
{0,4,8,12,16,22,30,42,58,78,104,138,180,192}
}
};
/*
The following table is used to implement the scalefactor
partitioning for MPEG2 as described in section
2.4.3.2 of the IS. The indexing corresponds to the
way the tables are presented in the IS:
[table_number][row_in_table][column of nr_of_sfb]
*/
static unsigned nr_of_sfb_block[6][3][4] =
{
{
{6, 5, 5, 5},
{9, 9, 9, 9},
{6, 9, 9, 9}
},
{
{6, 5, 7, 3},
{9, 9, 12, 6},
{6, 9, 12, 6}
},
{
{11, 10, 0, 0},
{18, 18, 0, 0},
{15,18,0,0}
},
{
{7, 7, 7, 0},
{12, 12, 12, 0},
{6, 15, 12, 0}
},
{
{6, 6, 6, 3},
{12, 9, 9, 6},
{6, 12, 9, 6}
},
{
{8, 8, 5, 0},
{15,12,9,0},
{6,18,9,0}
}
};
/* Table B.6: layer3 preemphasis */
int pretab[21] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 2, 2, 3, 3, 3, 2
};
/* This is the scfsi_band table from 2.4.2.7 of the IS */
int scfsi_band_long[5] = { 0, 6, 11, 16, 21 };
int *scalefac_band_long = &sfBandIndex[0].l[0];
int *scalefac_band_short = &sfBandIndex[0].s[0];
int fInit_iteration_loop;
int fInit_huffman_read_flag;
int tjBitOverflow1;
int tjBitOverflow2;
void fixStatic_loop( void )
{
scalefac_band_long = &sfBandIndex[0].l[0];
scalefac_band_short = &sfBandIndex[0].s[0];
}
/************************************************************************/
/* iteration_loop() */
/************************************************************************/
void
iteration_loop( double pe[][2], double xr_org[2][2][576], III_psy_ratio *ratio,
III_side_info_t *l3_side, int l3_enc[2][2][576],
int mean_bits, int stereo, double xr_dec[2][2][576],
III_scalefac_t *scalefac, frame_params *fr_ps,
int ancillary_pad, int bitsPerFrame )
{
III_psy_xmin l3_xmin;
gr_info *cod_info;
layer *info;
int *main_data_begin;
int max_bits;
int ch, gr, sfb, i, mode_gr;
double xr[2][2][576];
I576 *ix;
main_data_begin = &l3_side->main_data_begin;
l3_side->resvDrain = 0;
if ( !fInit_iteration_loop )
{
*main_data_begin = 0;
fInit_iteration_loop = 1;
}
info = fr_ps->header;
mode_gr = 2;
scalefac_band_long = &sfBandIndex[info->sampling_frequency].l[0];
scalefac_band_short = &sfBandIndex[info->sampling_frequency].s[0];
/* reading huffman code table */
if (fInit_huffman_read_flag == 0)
{
read_huffcodetab();
fInit_huffman_read_flag++;
}
for ( gr = 0; gr < mode_gr; gr++ )
{
for ( ch = 0; ch < stereo; ch++ )
{
for ( i = 0; i < 576; i++ )
xr[gr][ch][i] = xr_org[gr][ch][i];
}
}
ResvFrameBegin( fr_ps, l3_side, mean_bits, bitsPerFrame );
for ( gr = 0; gr < mode_gr; gr++ )
{
for ( ch = 0; ch < stereo; ch++ )
{
ix = (I576 *) l3_enc[gr][ch];
cod_info = (gr_info *) &(l3_side->gr[gr].ch[ch]);
gr_deco(cod_info);
calc_xmin( xr, ratio, cod_info, &l3_xmin, gr, ch );
calc_scfsi( xr[gr][ch], l3_side, &l3_xmin, ch, gr );
/* calculation of number of available bit( per granule ) */
max_bits = ResvMaxBits( fr_ps, l3_side, &pe[gr][ch], mean_bits );
/* reset of iteration variables */
for ( sfb = 0; sfb < 21; sfb++ )
scalefac->l[gr][ch][sfb] = 0;
for ( sfb = 0; sfb < 13; sfb++ )
for ( i = 0; i < 3; i++ )
scalefac->s[gr][ch][sfb][i] = 0;
for ( i = 0; i < 4; i++ )
cod_info->slen[i] = 0;
cod_info->sfb_partition_table = &nr_of_sfb_block[0][0][0];
cod_info->part2_3_length = 0;
cod_info->big_values = 0;
cod_info->count1 = 0;
cod_info->scalefac_compress = 0;
cod_info->table_select[0] = 0;
cod_info->table_select[1] = 0;
cod_info->table_select[2] = 0;
cod_info->subblock_gain[0] = 0;
cod_info->subblock_gain[1] = 0;
cod_info->subblock_gain[2] = 0;
cod_info->region0_count = 0;
cod_info->region1_count = 0;
cod_info->part2_length = 0;
cod_info->preflag = 0;
cod_info->scalefac_scale = 0;
cod_info->quantizerStepSize = 0.0;
cod_info->count1table_select= 0;
/* all spectral values zero ? */
if ( xr_max(xr[gr][ch]) != 0.0 )
{
cod_info->quantizerStepSize = (double) quantanf_init( xr[gr][ch] );
cod_info->part2_3_length = outer_loop( xr, max_bits, &l3_xmin,
l3_enc, fr_ps, scalefac,
gr, ch, l3_side );
}
ResvAdjust( fr_ps, cod_info, l3_side, mean_bits );
cod_info->global_gain = nint( cod_info->quantizerStepSize + 210.0 );
/* assert( cod_info->global_gain < 256 ); */
} /* for ch */
} /* for gr */
ResvFrameEnd( fr_ps, l3_side, mean_bits );
}
/************************************************************************/
/* quantanf_init */
/************************************************************************/
int quantanf_init( double xr[576] )
/* Function: Calculate the first quantization step quantanf. */
{
int i, tp = 0;
double system_const, minlimit;
double sfm = 0.0, sum1 = 0.0, sum2 = 0.0;
system_const = 8.0;
minlimit = -100.0;
for ( i = 0; i < 576; i++ )
{
if ( xr[i] != 0 )
{
double tpd = xr[i] * xr[i];
sum1 += log( tpd );
sum2 += tpd;
}
}
if ( sum2 != 0.0 )
{
sfm = exp( sum1 / 576.0 ) / (sum2 / 576.0);
tp = nint( system_const * log(sfm) );
if ( tp < minlimit )
tp = minlimit;
}
return(tp-70.0); /* SS 19-12-96. Starting value of
global_gain or quantizerStepSize
has to be reduced for iteration_loop
*/
}
/************************************************************************/
/* outer_loop */
/************************************************************************/
/* Function: The outer iteration loop controls the masking conditions */
/* of all scalefactorbands. It computes the best scalefac and */
/* global gain. This module calls the inner iteration loop */
/************************************************************************/
int outer_loop(
double xr[2][2][576], /* magnitudes of the spectral values */
int max_bits,
III_psy_xmin *l3_xmin, /* the allowed distortion of the scalefactor */
int l3_enc[2][2][576], /* vector of quantized values ix(0..575) */
frame_params *fr_ps,
III_scalefac_t *scalefac, /* scalefactors */
int gr, int ch, III_side_info_t *l3_side )
{
int status ;
int scalesave_l[CBLIMIT], scalesave_s[CBLIMIT][3];
int sfb, bits, huff_bits, save_preflag, save_compress;
double xfsf[4][CBLIMIT];
int i, over, iteration;
double *xrs;
int *ix;
gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;
xrs = (double *) &(xr[gr][ch][0]);
ix = (int *) &(l3_enc[gr][ch][0]);
iteration = 0;
do
{
iteration += 1;
cod_info->part2_length = part2_length( scalefac, fr_ps, gr, ch, l3_side );
huff_bits = max_bits - cod_info->part2_length;
if(iteration == 1)
{
bin_search_StepSize(max_bits,cod_info->quantizerStepSize,
ix,xrs,cod_info); /* speeds things up a bit */
}
bits = inner_loop( &xr[gr][ch][0], &l3_enc[gr][ch][0], huff_bits, cod_info );
calc_noise( &xr[gr][ch][0], &l3_enc[gr][ch][0], cod_info, xfsf ); /* distortion calculation */
for ( sfb = 0; sfb < CBLIMIT; sfb++ ) /* save scaling factors */
scalesave_l[sfb] = scalefac->l[gr][ch][sfb];
for ( sfb = 0; sfb < SFB_SMAX; sfb++ )
for ( i = 0; i < 3; i++ )
scalesave_s[sfb][i] = scalefac->s[gr][ch][sfb][i];
save_preflag = cod_info->preflag;
save_compress = cod_info->scalefac_compress;
preemphasis( &xr[gr][ch][0], xfsf, l3_xmin, gr, ch, l3_side );
over = amp_scalefac_bands( &xr[gr][ch][0], xfsf, l3_xmin,
l3_side, scalefac, gr, ch, iteration );
if ( (status = loop_break(scalefac, cod_info, gr, ch)) == 0 )
{
status = scale_bitcount( scalefac, cod_info, gr, ch );
}
}
while ( (status == 0) && (over > 0) );
cod_info->preflag = save_preflag;
cod_info->scalefac_compress = save_compress;
for ( sfb = 0; sfb < 21; sfb++ )
scalefac->l[gr][ch][sfb] = scalesave_l[sfb];
for ( i = 0; i < 3; i++ )
for ( sfb = 0; sfb < 12; sfb++ )
scalefac->s[gr][ch][sfb][i] = scalesave_s[sfb][i];
cod_info->part2_length = part2_length( scalefac, fr_ps, gr, ch, l3_side );
cod_info->part2_3_length = cod_info->part2_length + bits;
return cod_info->part2_3_length;
}
/***************************************************************************
* inner_loop *
***************************************************************************
* The code selects the best quantizerStepSize for a particular set
* of scalefacs */
int inner_loop( double* xrs, int *ix, int max_bits, gr_info *cod_info )
{
int bits;
cod_info->quantizerStepSize -= 1.0;
do
{
do
{
cod_info->quantizerStepSize += 1.0;
tjBitOverflow2 = FALSE;
quantize_tj( xrs, ix, cod_info );
}
while ( tjBitOverflow2 == TRUE ); /* within table range? */
bits = optimized_calculations_tj( ix, cod_info );
subdivide( cod_info ); /* bigvalues sfb division */
bigv_tab_select_tj( ix, cod_info ); /* codebook selection*/
bits += bigv_bitcount_tj( ix, cod_info ); /* bit count */
}
while ( bits > max_bits );
return bits;
}
/***************************************************************************/
/* calc_scfsi */
/***************************************************************************/
/* calculation of the scalefactor select information ( scfsi ) */
void calc_scfsi( double xr[576], III_side_info_t *l3_side,
III_psy_xmin *l3_xmin, int ch, int gr )
{
static int en_tot[2][2]; /* ch,gr */
static int en[2][2][21];
static int xm[2][2][21];
static int xrmax[2][2];
int en_tot_krit = 10;
int en_dif_krit = 100;
int en_scfsi_band_krit = 10;
int xm_scfsi_band_krit = 10;
int scfsi_band;
unsigned scfsi_set;
int sfb, start, end, i;
int condition = 0;
double temp, log2 = log( 2.0 );
gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;
xrmax[gr][ch] = xr_max( xr );
scfsi_set = 0;
/* the total energy of the granule */
for ( temp = 0.0, i = 0; i < 576; i++ )
temp += xr[i] * xr[i];
if ( temp == 0.0 )
en_tot[gr][ch] = 0.0;
else
en_tot[gr][ch] = log( temp ) / log2 ;
/* the energy of each scalefactor band, en */
/* the allowed distortion of each scalefactor band, xm */
if ( cod_info->window_switching_flag == 0 ||
cod_info->block_type != 2 )
for ( sfb = 0; sfb < 21; sfb++ )
{
start = scalefac_band_long[ sfb ];
end = scalefac_band_long[ sfb+1 ];
for ( temp = 0.0, i = start; i < end; i++ )
temp += xr[i] * xr[i];
if ( temp == 0.0 )
en[gr][ch][sfb] = 0.0;
else
en[gr][ch][sfb] = log( temp )/ log2;
if ( l3_xmin->l[gr][ch][sfb] == 0.0 )
xm[gr][ch][sfb] = 0.0;
else
xm[gr][ch][sfb] = log( l3_xmin->l[gr][ch][sfb] ) / log2;
}
if ( gr == 1 )
{
int gr2, tp;
for ( gr2 = 0; gr2 < 2; gr2++ )
{
/* The spectral values are not all zero */
if ( xrmax[ch][gr2] != 0.0 )
condition++;
/* None of the granules contains short blocks */
if ( (cod_info->window_switching_flag == 0) ||
(cod_info->block_type != 2) )
condition++;
}
if ( abs(en_tot[0] - en_tot[1]) < en_tot_krit )
condition++;
for ( tp = 0, sfb = 0; sfb < 21; sfb++ )
tp += abs( en[ch][0][sfb] - en[ch][1][sfb] );
if ( tp < en_dif_krit )
condition++;
if ( condition == 6 )
{
for ( scfsi_band = 0; scfsi_band < 4; scfsi_band++ )
{
int sum0 = 0, sum1 = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -