📄 sbr_hfgen.c
字号:
/*
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
** Any non-GPL usage of this software or parts of this software is strictly
** forbidden.
**
** Commercial non-GPL licensing of this software is possible.
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
**
** $Id: sbr_hfgen.c,v 1.22 2004/09/08 09:43:11 gcp Exp $
**/
/* High Frequency generation */
#include "common.h"
#include "structs.h"
#ifdef SBR_DEC
#include "sbr_syntax.h"
#include "sbr_hfgen.h"
#include "sbr_fbt.h"
/* static function declarations */
#ifdef SBR_LOW_POWER
static void calc_prediction_coef_lp(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64],
complex_t *alpha_0, complex_t *alpha_1, real_t *rxx);
static void calc_aliasing_degree(sbr_info *sbr, real_t *rxx, real_t *deg);
#else
static void calc_prediction_coef(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64],
complex_t *alpha_0, complex_t *alpha_1, uint8_t k);
#endif
static void calc_chirp_factors(sbr_info *sbr, uint8_t ch);
static void patch_construction(sbr_info *sbr);
void hf_generation(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64],
qmf_t Xhigh[MAX_NTSRHFG][64]
#ifdef SBR_LOW_POWER
,real_t *deg
#endif
,uint8_t ch)
{
uint8_t l, i, x;
ALIGN complex_t alpha_0[64], alpha_1[64];
#ifdef SBR_LOW_POWER
ALIGN real_t rxx[64];
#endif
uint8_t offset = sbr->tHFAdj;
uint8_t first = sbr->t_E[ch][0];
uint8_t last = sbr->t_E[ch][sbr->L_E[ch]];
calc_chirp_factors(sbr, ch);
#ifdef SBR_LOW_POWER
memset(deg, 0, 64*sizeof(real_t));
#endif
if ((ch == 0) && (sbr->Reset))
patch_construction(sbr);
/* calculate the prediction coefficients */
#ifdef SBR_LOW_POWER
calc_prediction_coef_lp(sbr, Xlow, alpha_0, alpha_1, rxx);
calc_aliasing_degree(sbr, rxx, deg);
#endif
/* actual HF generation */
for (i = 0; i < sbr->noPatches; i++)
{
for (x = 0; x < sbr->patchNoSubbands[i]; x++)
{
real_t a0_r, a0_i, a1_r, a1_i;
real_t bw, bw2;
uint8_t q, p, k, g;
/* find the low and high band for patching */
k = sbr->kx + x;
for (q = 0; q < i; q++)
{
k += sbr->patchNoSubbands[q];
}
p = sbr->patchStartSubband[i] + x;
#ifdef SBR_LOW_POWER
if (x != 0 /*x < sbr->patchNoSubbands[i]-1*/)
deg[k] = deg[p];
else
deg[k] = 0;
#endif
g = sbr->table_map_k_to_g[k];
bw = sbr->bwArray[ch][g];
bw2 = MUL_C(bw, bw);
/* do the patching */
/* with or without filtering */
if (bw2 > 0)
{
real_t temp1_r, temp2_r, temp3_r;
#ifndef SBR_LOW_POWER
real_t temp1_i, temp2_i, temp3_i;
calc_prediction_coef(sbr, Xlow, alpha_0, alpha_1, p);
#endif
a0_r = MUL_C(RE(alpha_0[p]), bw);
a1_r = MUL_C(RE(alpha_1[p]), bw2);
#ifndef SBR_LOW_POWER
a0_i = MUL_C(IM(alpha_0[p]), bw);
a1_i = MUL_C(IM(alpha_1[p]), bw2);
#endif
temp2_r = QMF_RE(Xlow[first - 2 + offset][p]);
temp3_r = QMF_RE(Xlow[first - 1 + offset][p]);
#ifndef SBR_LOW_POWER
temp2_i = QMF_IM(Xlow[first - 2 + offset][p]);
temp3_i = QMF_IM(Xlow[first - 1 + offset][p]);
#endif
for (l = first; l < last; l++)
{
temp1_r = temp2_r;
temp2_r = temp3_r;
temp3_r = QMF_RE(Xlow[l + offset][p]);
#ifndef SBR_LOW_POWER
temp1_i = temp2_i;
temp2_i = temp3_i;
temp3_i = QMF_IM(Xlow[l + offset][p]);
#endif
#ifdef SBR_LOW_POWER
QMF_RE(Xhigh[l + offset][k]) =
temp3_r
+(MUL_R(a0_r, temp2_r) +
MUL_R(a1_r, temp1_r));
#else
QMF_RE(Xhigh[l + offset][k]) =
temp3_r
+(MUL_R(a0_r, temp2_r) -
MUL_R(a0_i, temp2_i) +
MUL_R(a1_r, temp1_r) -
MUL_R(a1_i, temp1_i));
QMF_IM(Xhigh[l + offset][k]) =
temp3_i
+(MUL_R(a0_i, temp2_r) +
MUL_R(a0_r, temp2_i) +
MUL_R(a1_i, temp1_r) +
MUL_R(a1_r, temp1_i));
#endif
}
} else {
for (l = first; l < last; l++)
{
QMF_RE(Xhigh[l + offset][k]) = QMF_RE(Xlow[l + offset][p]);
#ifndef SBR_LOW_POWER
QMF_IM(Xhigh[l + offset][k]) = QMF_IM(Xlow[l + offset][p]);
#endif
}
}
}
}
if (sbr->Reset)
{
limiter_frequency_table(sbr);
}
}
typedef struct
{
complex_t r01;
complex_t r02;
complex_t r11;
complex_t r12;
complex_t r22;
real_t det;
} acorr_coef;
#ifdef SBR_LOW_POWER
static void auto_correlation(sbr_info *sbr, acorr_coef *ac,
qmf_t buffer[MAX_NTSRHFG][64],
uint8_t bd, uint8_t len)
{
real_t r01 = 0, r02 = 0, r11 = 0;
int8_t j;
uint8_t offset = sbr->tHFAdj;
#ifdef FIXED_POINT
const real_t rel = FRAC_CONST(0.999999); // 1 / (1 + 1e-6f);
uint32_t maxi = 0;
uint32_t pow2, exp;
#else
const real_t rel = 1 / (1 + 1e-6f);
#endif
#ifdef FIXED_POINT
mask = 0;
for (j = (offset-2); j < (len + offset); j++)
{
real_t x;
x = QMF_RE(buffer[j][bd])>>REAL_BITS;
mask |= x ^ (x >> 31);
}
exp = wl_min_lzc(mask);
/* improves accuracy */
if (exp > 0)
exp -= 1;
for (j = offset; j < len + offset; j++)
{
real_t buf_j = ((QMF_RE(buffer[j][bd])+(1<<(exp-1)))>>exp);
real_t buf_j_1 = ((QMF_RE(buffer[j-1][bd])+(1<<(exp-1)))>>exp);
real_t buf_j_2 = ((QMF_RE(buffer[j-2][bd])+(1<<(exp-1)))>>exp);
/* normalisation with rounding */
r01 += MUL_R(buf_j, buf_j_1);
r02 += MUL_R(buf_j, buf_j_2);
r11 += MUL_R(buf_j_1, buf_j_1);
}
RE(ac->r12) = r01 -
MUL_R(((QMF_RE(buffer[len+offset-1][bd])+(1<<(exp-1)))>>exp), ((QMF_RE(buffer[len+offset-2][bd])+(1<<(exp-1)))>>exp)) +
MUL_R(((QMF_RE(buffer[offset-1][bd])+(1<<(exp-1)))>>exp), ((QMF_RE(buffer[offset-2][bd])+(1<<(exp-1)))>>exp));
RE(ac->r22) = r11 -
MUL_R(((QMF_RE(buffer[len+offset-2][bd])+(1<<(exp-1)))>>exp), ((QMF_RE(buffer[len+offset-2][bd])+(1<<(exp-1)))>>exp)) +
MUL_R(((QMF_RE(buffer[offset-2][bd])+(1<<(exp-1)))>>exp), ((QMF_RE(buffer[offset-2][bd])+(1<<(exp-1)))>>exp));
#else
for (j = offset; j < len + offset; j++)
{
r01 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-1][bd]);
r02 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-2][bd]);
r11 += QMF_RE(buffer[j-1][bd]) * QMF_RE(buffer[j-1][bd]);
}
RE(ac->r12) = r01 -
QMF_RE(buffer[len+offset-1][bd]) * QMF_RE(buffer[len+offset-2][bd]) +
QMF_RE(buffer[offset-1][bd]) * QMF_RE(buffer[offset-2][bd]);
RE(ac->r22) = r11 -
QMF_RE(buffer[len+offset-2][bd]) * QMF_RE(buffer[len+offset-2][bd]) +
QMF_RE(buffer[offset-2][bd]) * QMF_RE(buffer[offset-2][bd]);
#endif
RE(ac->r01) = r01;
RE(ac->r02) = r02;
RE(ac->r11) = r11;
ac->det = MUL_R(RE(ac->r11), RE(ac->r22)) - MUL_F(MUL_R(RE(ac->r12), RE(ac->r12)), rel);
}
#else
static void auto_correlation(sbr_info *sbr, acorr_coef *ac, qmf_t buffer[MAX_NTSRHFG][64],
uint8_t bd, uint8_t len)
{
real_t r01r = 0, r01i = 0, r02r = 0, r02i = 0, r11r = 0;
real_t temp1_r, temp1_i, temp2_r, temp2_i, temp3_r, temp3_i, temp4_r, temp4_i, temp5_r, temp5_i;
#ifdef FIXED_POINT
const real_t rel = FRAC_CONST(0.999999); // 1 / (1 + 1e-6f);
uint32_t mask, exp;
real_t pow2_to_exp;
#else
const real_t rel = 1 / (1 + 1e-6f);
#endif
int8_t j;
uint8_t offset = sbr->tHFAdj;
#ifdef FIXED_POINT
mask = 0;
for (j = (offset-2); j < (len + offset); j++)
{
real_t x;
x = QMF_RE(buffer[j][bd])>>REAL_BITS;
mask |= x ^ (x >> 31);
x = QMF_IM(buffer[j][bd])>>REAL_BITS;
mask |= x ^ (x >> 31);
}
exp = wl_min_lzc(mask);
/* improves accuracy */
if (exp > 0)
exp -= 1;
pow2_to_exp = 1<<(exp-1);
temp2_r = (QMF_RE(buffer[offset-2][bd]) + pow2_to_exp) >> exp;
temp2_i = (QMF_IM(buffer[offset-2][bd]) + pow2_to_exp) >> exp;
temp3_r = (QMF_RE(buffer[offset-1][bd]) + pow2_to_exp) >> exp;
temp3_i = (QMF_IM(buffer[offset-1][bd]) + pow2_to_exp) >> exp;
// Save these because they are needed after loop
temp4_r = temp2_r;
temp4_i = temp2_i;
temp5_r = temp3_r;
temp5_i = temp3_i;
for (j = offset; j < len + offset; j++)
{
temp1_r = temp2_r; // temp1_r = (QMF_RE(buffer[offset-2][bd] + (1<<(exp-1))) >> exp;
temp1_i = temp2_i; // temp1_i = (QMF_IM(buffer[offset-2][bd] + (1<<(exp-1))) >> exp;
temp2_r = temp3_r; // temp2_r = (QMF_RE(buffer[offset-1][bd] + (1<<(exp-1))) >> exp;
temp2_i = temp3_i; // temp2_i = (QMF_IM(buffer[offset-1][bd] + (1<<(exp-1))) >> exp;
temp3_r = (QMF_RE(buffer[j][bd]) + pow2_to_exp) >> exp;
temp3_i = (QMF_IM(buffer[j][bd]) + pow2_to_exp) >> exp;
r01r += MUL_R(temp3_r, temp2_r) + MUL_R(temp3_i, temp2_i);
r01i += MUL_R(temp3_i, temp2_r) - MUL_R(temp3_r, temp2_i);
r02r += MUL_R(temp3_r, temp1_r) + MUL_R(temp3_i, temp1_i);
r02i += MUL_R(temp3_i, temp1_r) - MUL_R(temp3_r, temp1_i);
r11r += MUL_R(temp2_r, temp2_r) + MUL_R(temp2_i, temp2_i);
}
// These are actual values in temporary variable at this point
// temp1_r = (QMF_RE(buffer[len+offset-1-2][bd] + (1<<(exp-1))) >> exp;
// temp1_i = (QMF_IM(buffer[len+offset-1-2][bd] + (1<<(exp-1))) >> exp;
// temp2_r = (QMF_RE(buffer[len+offset-1-1][bd] + (1<<(exp-1))) >> exp;
// temp2_i = (QMF_IM(buffer[len+offset-1-1][bd] + (1<<(exp-1))) >> exp;
// temp3_r = (QMF_RE(buffer[len+offset-1][bd]) + (1<<(exp-1))) >> exp;
// temp3_i = (QMF_IM(buffer[len+offset-1][bd]) + (1<<(exp-1))) >> exp;
// temp4_r = (QMF_RE(buffer[offset-2][bd]) + (1<<(exp-1))) >> exp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -