From aaf26cffbac108cc3a4916e3284590e68b816865 Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Thu, 29 Feb 2024 18:55:13 +0100 Subject: [PATCH] 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 Signed-off-by: Andreas Rheinhardt --- libavcodec/aac_defines.h | 2 +- libavcodec/aacdec.c | 1 - libavcodec/aacdec_fixed.c | 1 - libavcodec/aacdec_template.c | 1 + libavcodec/aacenc_tns.c | 1 + libavcodec/cngenc.c | 1 + libavcodec/lpc.c | 31 ++++++++++ libavcodec/lpc.h | 106 ----------------------------------- libavcodec/lpc_functions.h | 100 +++++++++++++++++++++++++++++++++ libavcodec/ra288.c | 2 +- 10 files changed, 136 insertions(+), 110 deletions(-) create mode 100644 libavcodec/lpc_functions.h diff --git a/libavcodec/aac_defines.h b/libavcodec/aac_defines.h index 8765939731..a0c23c33ff 100644 --- a/libavcodec/aac_defines.h +++ b/libavcodec/aac_defines.h @@ -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 diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c index b1bce7542b..fa2187b61a 100644 --- a/libavcodec/aacdec.c +++ b/libavcodec/aacdec.c @@ -40,7 +40,6 @@ #include "avcodec.h" #include "codec_internal.h" #include "get_bits.h" -#include "lpc.h" #include "kbdwin.h" #include "sinewin.h" diff --git a/libavcodec/aacdec_fixed.c b/libavcodec/aacdec_fixed.c index 08343bf157..2abe6acb6b 100644 --- a/libavcodec/aacdec_fixed.c +++ b/libavcodec/aacdec_fixed.c @@ -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" diff --git a/libavcodec/aacdec_template.c b/libavcodec/aacdec_template.c index fb3a954aad..6561abb14e 100644 --- a/libavcodec/aacdec_template.c +++ b/libavcodec/aacdec_template.c @@ -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, diff --git a/libavcodec/aacenc_tns.c b/libavcodec/aacenc_tns.c index 8dc6dfcf62..b2418a0236 100644 --- a/libavcodec/aacenc_tns.c +++ b/libavcodec/aacenc_tns.c @@ -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 diff --git a/libavcodec/cngenc.c b/libavcodec/cngenc.c index 596d6f8c2a..15a410ea50 100644 --- a/libavcodec/cngenc.c +++ b/libavcodec/cngenc.c @@ -21,6 +21,7 @@ #include +#include "libavutil/avassert.h" #include "libavutil/common.h" #include "avcodec.h" #include "codec_internal.h" diff --git a/libavcodec/lpc.c b/libavcodec/lpc.c index 9e2fd0f128..53f5c3d379 100644 --- a/libavcodec/lpc.c +++ b/libavcodec/lpc.c @@ -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 diff --git a/libavcodec/lpc.h b/libavcodec/lpc.h index 907fab7508..6d62707a59 100644 --- a/libavcodec/lpc.h +++ b/libavcodec/lpc.h @@ -24,7 +24,6 @@ #include #include -#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>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 */ diff --git a/libavcodec/lpc_functions.h b/libavcodec/lpc_functions.h new file mode 100644 index 0000000000..57bcfab900 --- /dev/null +++ b/libavcodec/lpc_functions.h @@ -0,0 +1,100 @@ +/* + * LPC utility functions + * Copyright (c) 2006 Justin Ruggles + * + * 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 */ diff --git a/libavcodec/ra288.c b/libavcodec/ra288.c index c8c20e4884..e4b14ef2a2 100644 --- a/libavcodec/ra288.c +++ b/libavcodec/ra288.c @@ -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