aacdec: move prediction to separate files

This commit is contained in:
Lynne 2024-03-16 05:16:50 +01:00
parent b1718ce0f9
commit 49e7be1e37
No known key found for this signature in database
GPG Key ID: A2FEA5F03F034464
9 changed files with 306 additions and 230 deletions

View File

@ -569,6 +569,52 @@ static void AAC_RENAME(clip_output)(AACDecContext *ac, ChannelElement *che,
#endif
}
static inline void reset_all_predictors(PredictorState *ps)
{
int i;
for (i = 0; i < MAX_PREDICTORS; i++)
reset_predict_state(&ps[i]);
}
static inline void reset_predictor_group(PredictorState *ps, int group_num)
{
int i;
for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
reset_predict_state(&ps[i]);
}
/**
* Apply AAC-Main style frequency domain prediction.
*/
static void AAC_RENAME(apply_prediction)(AACDecContext *ac, SingleChannelElement *sce)
{
int sfb, k;
if (!sce->ics.predictor_initialized) {
reset_all_predictors(sce->AAC_RENAME(predictor_state));
sce->ics.predictor_initialized = 1;
}
if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
for (sfb = 0;
sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
sfb++) {
for (k = sce->ics.swb_offset[sfb];
k < sce->ics.swb_offset[sfb + 1];
k++) {
predict(&sce->AAC_RENAME(predictor_state)[k],
&sce->AAC_RENAME(coeffs)[k],
sce->ics.predictor_present &&
sce->ics.prediction_used[sfb]);
}
}
if (sce->ics.predictor_reset_group)
reset_predictor_group(sce->AAC_RENAME(predictor_state),
sce->ics.predictor_reset_group);
} else
reset_all_predictors(sce->AAC_RENAME(predictor_state));
}
const AACDecDSP AAC_RENAME(aac_dsp) = {
.init_tables = &AAC_RENAME(init_tables),
@ -579,6 +625,8 @@ const AACDecDSP AAC_RENAME(aac_dsp) = {
.apply_ltp = &AAC_RENAME(apply_ltp),
.update_ltp = &AAC_RENAME(update_ltp),
.apply_prediction = AAC_RENAME(apply_prediction),
.imdct_and_windowing = AAC_RENAME(imdct_and_windowing),
.imdct_and_windowing_960 = AAC_RENAME(imdct_and_windowing_960),
.imdct_and_windowing_ld = AAC_RENAME(imdct_and_windowing_ld),

View File

@ -79,5 +79,6 @@ static const int cce_scale_fixed[8] = {
#include "aacdec_fixed_dequant.h"
#include "aacdec_fixed_coupling.h"
#include "aacdec_fixed_prediction.h"
#include "aacdec_dsp_template.c"
#include "aacdec_proc_template.c"

View File

@ -0,0 +1,151 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
*
* AAC decoder fixed-point implementation
* Copyright (c) 2013
* MIPS Technologies, Inc., California.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it 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.
*
* FFmpeg 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_AAC_AACDEC_FIXED_PREDICTION_H
#define AVCODEC_AAC_AACDEC_FIXED_PREDICTION_H
static av_always_inline SoftFloat flt16_round(SoftFloat pf)
{
SoftFloat tmp;
int s;
tmp.exp = pf.exp;
s = pf.mant >> 31;
tmp.mant = (pf.mant ^ s) - s;
tmp.mant = (tmp.mant + 0x00200000U) & 0xFFC00000U;
tmp.mant = (tmp.mant ^ s) - s;
return tmp;
}
static av_always_inline SoftFloat flt16_even(SoftFloat pf)
{
SoftFloat tmp;
int s;
tmp.exp = pf.exp;
s = pf.mant >> 31;
tmp.mant = (pf.mant ^ s) - s;
tmp.mant = (tmp.mant + 0x001FFFFFU + (tmp.mant & 0x00400000U >> 16)) & 0xFFC00000U;
tmp.mant = (tmp.mant ^ s) - s;
return tmp;
}
static av_always_inline SoftFloat flt16_trunc(SoftFloat pf)
{
SoftFloat pun;
int s;
pun.exp = pf.exp;
s = pf.mant >> 31;
pun.mant = (pf.mant ^ s) - s;
pun.mant = pun.mant & 0xFFC00000U;
pun.mant = (pun.mant ^ s) - s;
return pun;
}
static av_always_inline void predict(PredictorState *ps, int *coef,
int output_enable)
{
const SoftFloat a = { 1023410176, 0 }; // 61.0 / 64
const SoftFloat alpha = { 973078528, 0 }; // 29.0 / 32
SoftFloat e0, e1;
SoftFloat pv;
SoftFloat k1, k2;
SoftFloat r0 = ps->r0, r1 = ps->r1;
SoftFloat cor0 = ps->cor0, cor1 = ps->cor1;
SoftFloat var0 = ps->var0, var1 = ps->var1;
SoftFloat tmp;
if (var0.exp > 1 || (var0.exp == 1 && var0.mant > 0x20000000)) {
k1 = av_mul_sf(cor0, flt16_even(av_div_sf(a, var0)));
}
else {
k1.mant = 0;
k1.exp = 0;
}
if (var1.exp > 1 || (var1.exp == 1 && var1.mant > 0x20000000)) {
k2 = av_mul_sf(cor1, flt16_even(av_div_sf(a, var1)));
}
else {
k2.mant = 0;
k2.exp = 0;
}
tmp = av_mul_sf(k1, r0);
pv = flt16_round(av_add_sf(tmp, av_mul_sf(k2, r1)));
if (output_enable) {
int shift = 28 - pv.exp;
if (shift < 31) {
if (shift > 0) {
*coef += (unsigned)((pv.mant + (1 << (shift - 1))) >> shift);
} else
*coef += (unsigned)pv.mant << -shift;
}
}
e0 = av_int2sf(*coef, 2);
e1 = av_sub_sf(e0, tmp);
ps->cor1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor1), av_mul_sf(r1, e1)));
tmp = av_add_sf(av_mul_sf(r1, r1), av_mul_sf(e1, e1));
tmp.exp--;
ps->var1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var1), tmp));
ps->cor0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor0), av_mul_sf(r0, e0)));
tmp = av_add_sf(av_mul_sf(r0, r0), av_mul_sf(e0, e0));
tmp.exp--;
ps->var0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var0), tmp));
ps->r1 = flt16_trunc(av_mul_sf(a, av_sub_sf(r0, av_mul_sf(k1, e0))));
ps->r0 = flt16_trunc(av_mul_sf(a, e0));
}
static av_always_inline void reset_predict_state(PredictorState *ps)
{
ps->r0.mant = 0;
ps->r0.exp = 0;
ps->r1.mant = 0;
ps->r1.exp = 0;
ps->cor0.mant = 0;
ps->cor0.exp = 0;
ps->cor1.mant = 0;
ps->cor1.exp = 0;
ps->var0.mant = 0x20000000;
ps->var0.exp = 1;
ps->var1.mant = 0x20000000;
ps->var1.exp = 1;
}
#endif /* AVCODEC_AAC_AACDEC_FIXED_PREDICTION_H */

View File

@ -147,5 +147,6 @@ static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
#endif
#include "aacdec_float_coupling.h"
#include "aacdec_float_prediction.h"
#include "aacdec_dsp_template.c"
#include "aacdec_proc_template.c"

View File

@ -0,0 +1,100 @@
/*
* AAC decoder
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
*
* AAC LATM decoder
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
* Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
*
* AAC decoder fixed-point implementation
* Copyright (c) 2013
* MIPS Technologies, Inc., California.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it 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.
*
* FFmpeg 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_AAC_AACDEC_FLOAT_PREDICTION_H
#define AVCODEC_AAC_AACDEC_FLOAT_PREDICTION_H
static av_always_inline float flt16_round(float pf)
{
union av_intfloat32 tmp;
tmp.f = pf;
tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
return tmp.f;
}
static av_always_inline float flt16_even(float pf)
{
union av_intfloat32 tmp;
tmp.f = pf;
tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
return tmp.f;
}
static av_always_inline float flt16_trunc(float pf)
{
union av_intfloat32 pun;
pun.f = pf;
pun.i &= 0xFFFF0000U;
return pun.f;
}
static av_always_inline void predict(PredictorState *ps, float *coef,
int output_enable)
{
const float a = 0.953125; // 61.0 / 64
const float alpha = 0.90625; // 29.0 / 32
float e0, e1;
float pv;
float k1, k2;
float r0 = ps->r0, r1 = ps->r1;
float cor0 = ps->cor0, cor1 = ps->cor1;
float var0 = ps->var0, var1 = ps->var1;
k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
pv = flt16_round(k1 * r0 + k2 * r1);
if (output_enable)
*coef += pv;
e0 = *coef;
e1 = e0 - k1 * r0;
ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
ps->r0 = flt16_trunc(a * e0);
}
static av_always_inline void reset_predict_state(PredictorState *ps)
{
ps->r0 = 0.0f;
ps->r1 = 0.0f;
ps->cor0 = 0.0f;
ps->cor1 = 0.0f;
ps->var0 = 1.0f;
ps->var1 = 1.0f;
}
#endif /* AVCODEC_AAC_AACDEC_FLOAT_PREDICTION_H */

View File

@ -63,71 +63,6 @@
# include "mips/aacdec_mips.h"
#endif
static av_always_inline void reset_predict_state(PredictorState *ps)
{
ps->r0 = 0.0f;
ps->r1 = 0.0f;
ps->cor0 = 0.0f;
ps->cor1 = 0.0f;
ps->var0 = 1.0f;
ps->var1 = 1.0f;
}
static av_always_inline float flt16_round(float pf)
{
union av_intfloat32 tmp;
tmp.f = pf;
tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
return tmp.f;
}
static av_always_inline float flt16_even(float pf)
{
union av_intfloat32 tmp;
tmp.f = pf;
tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
return tmp.f;
}
static av_always_inline float flt16_trunc(float pf)
{
union av_intfloat32 pun;
pun.f = pf;
pun.i &= 0xFFFF0000U;
return pun.f;
}
static av_always_inline void predict(PredictorState *ps, float *coef,
int output_enable)
{
const float a = 0.953125; // 61.0 / 64
const float alpha = 0.90625; // 29.0 / 32
float e0, e1;
float pv;
float k1, k2;
float r0 = ps->r0, r1 = ps->r1;
float cor0 = ps->cor0, cor1 = ps->cor1;
float var0 = ps->var0, var1 = ps->var1;
k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
pv = flt16_round(k1 * r0 + k2 * r1);
if (output_enable)
*coef += pv;
e0 = *coef;
e1 = e0 - k1 * r0;
ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
ps->r0 = flt16_trunc(a * e0);
}
#include "aacdec_template.c"
#define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word

View File

@ -230,6 +230,8 @@ typedef struct AACDecDSP {
void (*apply_ltp)(AACDecContext *ac, SingleChannelElement *sce);
void (*update_ltp)(AACDecContext *ac, SingleChannelElement *sce);
void (*apply_prediction)(AACDecContext *ac, SingleChannelElement *sce);
void (*apply_dependent_coupling)(AACDecContext *ac,
SingleChannelElement *target,
ChannelElement *cce, int index);

View File

@ -127,124 +127,8 @@ static const int * const tns_tmp2_map_fixed[4] = {
};
// @}
static av_always_inline void reset_predict_state(PredictorState *ps)
{
ps->r0.mant = 0;
ps->r0.exp = 0;
ps->r1.mant = 0;
ps->r1.exp = 0;
ps->cor0.mant = 0;
ps->cor0.exp = 0;
ps->cor1.mant = 0;
ps->cor1.exp = 0;
ps->var0.mant = 0x20000000;
ps->var0.exp = 1;
ps->var1.mant = 0x20000000;
ps->var1.exp = 1;
}
static const int exp2tab[4] = { Q31(1.0000000000/2), Q31(1.1892071150/2), Q31(1.4142135624/2), Q31(1.6817928305/2) }; // 2^0, 2^0.25, 2^0.5, 2^0.75
static av_always_inline SoftFloat flt16_round(SoftFloat pf)
{
SoftFloat tmp;
int s;
tmp.exp = pf.exp;
s = pf.mant >> 31;
tmp.mant = (pf.mant ^ s) - s;
tmp.mant = (tmp.mant + 0x00200000U) & 0xFFC00000U;
tmp.mant = (tmp.mant ^ s) - s;
return tmp;
}
static av_always_inline SoftFloat flt16_even(SoftFloat pf)
{
SoftFloat tmp;
int s;
tmp.exp = pf.exp;
s = pf.mant >> 31;
tmp.mant = (pf.mant ^ s) - s;
tmp.mant = (tmp.mant + 0x001FFFFFU + (tmp.mant & 0x00400000U >> 16)) & 0xFFC00000U;
tmp.mant = (tmp.mant ^ s) - s;
return tmp;
}
static av_always_inline SoftFloat flt16_trunc(SoftFloat pf)
{
SoftFloat pun;
int s;
pun.exp = pf.exp;
s = pf.mant >> 31;
pun.mant = (pf.mant ^ s) - s;
pun.mant = pun.mant & 0xFFC00000U;
pun.mant = (pun.mant ^ s) - s;
return pun;
}
static av_always_inline void predict(PredictorState *ps, int *coef,
int output_enable)
{
const SoftFloat a = { 1023410176, 0 }; // 61.0 / 64
const SoftFloat alpha = { 973078528, 0 }; // 29.0 / 32
SoftFloat e0, e1;
SoftFloat pv;
SoftFloat k1, k2;
SoftFloat r0 = ps->r0, r1 = ps->r1;
SoftFloat cor0 = ps->cor0, cor1 = ps->cor1;
SoftFloat var0 = ps->var0, var1 = ps->var1;
SoftFloat tmp;
if (var0.exp > 1 || (var0.exp == 1 && var0.mant > 0x20000000)) {
k1 = av_mul_sf(cor0, flt16_even(av_div_sf(a, var0)));
}
else {
k1.mant = 0;
k1.exp = 0;
}
if (var1.exp > 1 || (var1.exp == 1 && var1.mant > 0x20000000)) {
k2 = av_mul_sf(cor1, flt16_even(av_div_sf(a, var1)));
}
else {
k2.mant = 0;
k2.exp = 0;
}
tmp = av_mul_sf(k1, r0);
pv = flt16_round(av_add_sf(tmp, av_mul_sf(k2, r1)));
if (output_enable) {
int shift = 28 - pv.exp;
if (shift < 31) {
if (shift > 0) {
*coef += (unsigned)((pv.mant + (1 << (shift - 1))) >> shift);
} else
*coef += (unsigned)pv.mant << -shift;
}
}
e0 = av_int2sf(*coef, 2);
e1 = av_sub_sf(e0, tmp);
ps->cor1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor1), av_mul_sf(r1, e1)));
tmp = av_add_sf(av_mul_sf(r1, r1), av_mul_sf(e1, e1));
tmp.exp--;
ps->var1 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var1), tmp));
ps->cor0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, cor0), av_mul_sf(r0, e0)));
tmp = av_add_sf(av_mul_sf(r0, r0), av_mul_sf(e0, e0));
tmp.exp--;
ps->var0 = flt16_trunc(av_add_sf(av_mul_sf(alpha, var0), tmp));
ps->r1 = flt16_trunc(av_mul_sf(a, av_sub_sf(r0, av_mul_sf(k1, e0))));
ps->r0 = flt16_trunc(av_mul_sf(a, e0));
}
#include "aacdec_template.c"
const FFCodec ff_aac_fixed_decoder = {

View File

@ -1072,13 +1072,6 @@ static int decode_audio_specific_config(AACDecContext *ac,
sync_extension);
}
static void reset_all_predictors(PredictorState *ps)
{
int i;
for (i = 0; i < MAX_PREDICTORS; i++)
reset_predict_state(&ps[i]);
}
static int sample_rate_idx (int rate)
{
if (92017 <= rate) return 0;
@ -1095,13 +1088,6 @@ static int sample_rate_idx (int rate)
else return 11;
}
static void reset_predictor_group(PredictorState *ps, int group_num)
{
int i;
for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
reset_predict_state(&ps[i]);
}
static void aacdec_init(AACDecContext *ac);
static av_cold void aac_static_table_init(void)
@ -1573,38 +1559,6 @@ static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
}
}
/**
* Apply AAC-Main style frequency domain prediction.
*/
static void apply_prediction(AACDecContext *ac, SingleChannelElement *sce)
{
int sfb, k;
if (!sce->ics.predictor_initialized) {
reset_all_predictors(sce->AAC_RENAME(predictor_state));
sce->ics.predictor_initialized = 1;
}
if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
for (sfb = 0;
sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
sfb++) {
for (k = sce->ics.swb_offset[sfb];
k < sce->ics.swb_offset[sfb + 1];
k++) {
predict(&sce->AAC_RENAME(predictor_state)[k],
&sce->AAC_RENAME(coeffs)[k],
sce->ics.predictor_present &&
sce->ics.prediction_used[sfb]);
}
}
if (sce->ics.predictor_reset_group)
reset_predictor_group(sce->AAC_RENAME(predictor_state),
sce->ics.predictor_reset_group);
} else
reset_all_predictors(sce->AAC_RENAME(predictor_state));
}
static void decode_gain_control(SingleChannelElement * sce, GetBitContext * gb)
{
// wd_num, wd_test, aloc_size
@ -1722,7 +1676,7 @@ int AAC_RENAME(ff_aac_decode_ics)(AACDecContext *ac, SingleChannelElement *sce,
goto fail;
if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
apply_prediction(ac, sce);
ac->dsp.apply_prediction(ac, sce);
return 0;
fail:
@ -1767,8 +1721,8 @@ static int decode_cpe(AACDecContext *ac, GetBitContext *gb, ChannelElement *cpe)
if (ms_present)
ac->dsp.apply_mid_side_stereo(ac, cpe);
if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
apply_prediction(ac, &cpe->ch[0]);
apply_prediction(ac, &cpe->ch[1]);
ac->dsp.apply_prediction(ac, &cpe->ch[0]);
ac->dsp.apply_prediction(ac, &cpe->ch[1]);
}
}