avcodec/lpc: Split inline functions into a header of their own

And move compute_ref_coefs() to its only user: lpc.c
There is no overlap between the users of compute_lpc_coefs()
and lpc proper.

Reviewed-by: Lynne <dev@lynne.ee>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2024-02-29 18:55:13 +01:00
parent 08aa791d20
commit aaf26cffba
10 changed files with 136 additions and 110 deletions

View File

@ -73,7 +73,7 @@ typedef int AAC_SIGNE;
#define AAC_HALF_SUM(x, y) (((x) >> 1) + ((y) >> 1))
#ifdef LPC_USE_FIXED
#error aac_defines.h must be included before lpc.h for fixed point decoder
#error aac_defines.h must be included before lpc_functions.h for fixed point decoder
#endif
#define LPC_USE_FIXED 1

View File

@ -40,7 +40,6 @@
#include "avcodec.h"
#include "codec_internal.h"
#include "get_bits.h"
#include "lpc.h"
#include "kbdwin.h"
#include "sinewin.h"

View File

@ -75,7 +75,6 @@
#include "aacdectab.h"
#include "adts_header.h"
#include "cbrt_data.h"
#include "lpc.h"
#include "sbr.h"
#include "aacsbr.h"
#include "mpeg4audio.h"

View File

@ -93,6 +93,7 @@
#include "libavutil/thread.h"
#include "decode.h"
#include "internal.h"
#include "lpc_functions.h"
static int output_configure(AACDecContext *ac,
uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,

View File

@ -30,6 +30,7 @@
#include "aacenc_tns.h"
#include "aactab.h"
#include "aacenc_utils.h"
#include "lpc_functions.h"
/* Could be set to 3 to save an additional bit at the cost of little quality */
#define TNS_Q_BITS 4

View File

@ -21,6 +21,7 @@
#include <math.h>
#include "libavutil/avassert.h"
#include "libavutil/common.h"
#include "avcodec.h"
#include "codec_internal.h"

View File

@ -25,8 +25,39 @@
#define LPC_USE_DOUBLE
#include "lpc.h"
#include "lpc_functions.h"
#include "libavutil/avassert.h"
/**
* Schur recursion.
* Produces reflection coefficients from autocorrelation data.
*/
static inline void compute_ref_coefs(const LPC_TYPE *autoc, int max_order,
LPC_TYPE *ref, LPC_TYPE *error)
{
LPC_TYPE err;
LPC_TYPE gen0[MAX_LPC_ORDER], gen1[MAX_LPC_ORDER];
for (int i = 0; i < max_order; i++)
gen0[i] = gen1[i] = autoc[i + 1];
err = autoc[0];
ref[0] = -gen1[0] / ((LPC_USE_FIXED || err) ? err : 1);
err += gen1[0] * ref[0];
if (error)
error[0] = err;
for (int i = 1; i < max_order; i++) {
for (int j = 0; j < max_order - i; j++) {
gen1[j] = gen1[j + 1] + ref[i - 1] * gen0[j];
gen0[j] = gen1[j + 1] * ref[i - 1] + gen0[j];
}
ref[i] = -gen1[0] / ((LPC_USE_FIXED || err) ? err : 1);
err += gen1[0] * ref[i];
if (error)
error[i] = err;
}
}
/**
* Apply Welch window function to audio block

View File

@ -24,7 +24,6 @@
#include <stdint.h>
#include <stddef.h>
#include "libavutil/avassert.h"
#include "libavutil/lls.h"
#define ORDER_METHOD_EST 0
@ -116,109 +115,4 @@ void ff_lpc_init_x86(LPCContext *s);
*/
void ff_lpc_end(LPCContext *s);
#ifndef LPC_USE_FIXED
#define LPC_USE_FIXED 0
#endif
#if LPC_USE_FIXED
typedef int LPC_TYPE;
typedef unsigned LPC_TYPE_U;
#else
#ifndef LPC_SRA_R
#define LPC_SRA_R(x, y) (x)
#define LPC_MUL26(x, y) ((x) * (y))
#define LPC_FIXR(x) ((float)(x))
#endif
#ifdef LPC_USE_DOUBLE
typedef double LPC_TYPE;
typedef double LPC_TYPE_U;
#else
typedef float LPC_TYPE;
typedef float LPC_TYPE_U;
#endif
#endif // USE_FIXED
/**
* Schur recursion.
* Produces reflection coefficients from autocorrelation data.
*/
static inline void compute_ref_coefs(const LPC_TYPE *autoc, int max_order,
LPC_TYPE *ref, LPC_TYPE *error)
{
int i, j;
LPC_TYPE err;
LPC_TYPE gen0[MAX_LPC_ORDER], gen1[MAX_LPC_ORDER];
for (i = 0; i < max_order; i++)
gen0[i] = gen1[i] = autoc[i + 1];
err = autoc[0];
ref[0] = -gen1[0] / ((LPC_USE_FIXED || err) ? err : 1);
err += gen1[0] * ref[0];
if (error)
error[0] = err;
for (i = 1; i < max_order; i++) {
for (j = 0; j < max_order - i; j++) {
gen1[j] = gen1[j + 1] + ref[i - 1] * gen0[j];
gen0[j] = gen1[j + 1] * ref[i - 1] + gen0[j];
}
ref[i] = -gen1[0] / ((LPC_USE_FIXED || err) ? err : 1);
err += gen1[0] * ref[i];
if (error)
error[i] = err;
}
}
/**
* Levinson-Durbin recursion.
* Produce LPC coefficients from autocorrelation data.
*/
static inline int compute_lpc_coefs(const LPC_TYPE *autoc, int max_order,
LPC_TYPE *lpc, int lpc_stride, int fail,
int normalize)
{
int i, j;
LPC_TYPE err = 0;
LPC_TYPE *lpc_last = lpc;
av_assert2(normalize || !fail);
if (normalize)
err = *autoc++;
if (fail && (autoc[max_order - 1] == 0 || err <= 0))
return -1;
for(i=0; i<max_order; i++) {
LPC_TYPE r = LPC_SRA_R(-autoc[i], 5);
if (normalize) {
for(j=0; j<i; j++)
r -= lpc_last[j] * autoc[i-j-1];
if (err)
r /= err;
err *= LPC_FIXR(1.0) - (r * r);
}
lpc[i] = r;
for(j=0; j < (i+1)>>1; j++) {
LPC_TYPE f = lpc_last[ j];
LPC_TYPE b = lpc_last[i-1-j];
lpc[ j] = f + (LPC_TYPE_U)LPC_MUL26(r, b);
lpc[i-1-j] = b + (LPC_TYPE_U)LPC_MUL26(r, f);
}
if (fail && err < 0)
return -1;
lpc_last = lpc;
lpc += lpc_stride;
}
return 0;
}
#endif /* AVCODEC_LPC_H */

100
libavcodec/lpc_functions.h Normal file
View File

@ -0,0 +1,100 @@
/*
* LPC utility functions
* Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com>
*
* 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_LPC_FUNCTIONS_H
#define AVCODEC_LPC_FUNCTIONS_H
#include "libavutil/avassert.h"
#ifndef LPC_USE_FIXED
#define LPC_USE_FIXED 0
#endif
#if LPC_USE_FIXED
typedef int LPC_TYPE;
typedef unsigned LPC_TYPE_U;
#else
#ifndef LPC_SRA_R
#define LPC_SRA_R(x, y) (x)
#define LPC_MUL26(x, y) ((x) * (y))
#define LPC_FIXR(x) ((float)(x))
#endif
#ifdef LPC_USE_DOUBLE
typedef double LPC_TYPE;
typedef double LPC_TYPE_U;
#else
typedef float LPC_TYPE;
typedef float LPC_TYPE_U;
#endif
#endif // USE_FIXED
/**
* Levinson-Durbin recursion.
* Produce LPC coefficients from autocorrelation data.
*/
static inline int compute_lpc_coefs(const LPC_TYPE *autoc, int max_order,
LPC_TYPE *lpc, int lpc_stride, int fail,
int normalize)
{
LPC_TYPE err = 0;
LPC_TYPE *lpc_last = lpc;
av_assert2(normalize || !fail);
if (normalize)
err = *autoc++;
if (fail && (autoc[max_order - 1] == 0 || err <= 0))
return -1;
for(int i = 0; i < max_order; i++) {
LPC_TYPE r = LPC_SRA_R(-autoc[i], 5);
if (normalize) {
for(int j = 0; j < i; j++)
r -= lpc_last[j] * autoc[i-j-1];
if (err)
r /= err;
err *= LPC_FIXR(1.0) - (r * r);
}
lpc[i] = r;
for(int j = 0; j < (i + 1) >> 1; j++) {
LPC_TYPE f = lpc_last[ j];
LPC_TYPE b = lpc_last[i-1-j];
lpc[ j] = f + (LPC_TYPE_U)LPC_MUL26(r, b);
lpc[i-1-j] = b + (LPC_TYPE_U)LPC_MUL26(r, f);
}
if (fail && err < 0)
return -1;
lpc_last = lpc;
lpc += lpc_stride;
}
return 0;
}
#endif /* AVCODEC_LPC_FUNCTIONS_H */

View File

@ -30,7 +30,7 @@
#include "codec_internal.h"
#include "decode.h"
#include "get_bits.h"
#include "lpc.h"
#include "lpc_functions.h"
#include "ra288.h"
#define MAX_BACKWARD_FILTER_ORDER 36