Patches attached.

- Andreas
From 749b698708288909ca14b51b51621ac837046daf Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
Date: Fri, 23 May 2025 15:58:03 +0200
Subject: [PATCH 1/5] avcodec/ac3dec: Hardcode tables to save space

The code to initialize the ungrouped bap mantissa tables
(bap 3 or 5) takes more bytes of .text than the tables itself;
they have therefore been hardcoded.

For GCC (14, -O3, albeit in an av_cold function), the initialization
code takes 99B each for the fixed and floating point decoders
(the code is currently duplicated), whereas the hardcoded tables
only take 96B. For Clang 19 it were 374B each (I don't now what
Clang was doing there).

Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
---
 libavcodec/ac3dec.c      | 16 ++--------------
 libavcodec/ac3dec_data.c | 39 ++++++++++++++++++++++++++++++++++++++-
 libavcodec/ac3dec_data.h |  9 +++++++++
 3 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index 49b170c235..dbe7f0b57c 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -55,9 +55,7 @@ static uint8_t ungroup_3_in_7_bits_tab[128][3];
 /** tables for ungrouping mantissas */
 static int b1_mantissas[32][3];
 static int b2_mantissas[128][3];
-static int b3_mantissas[8];
 static int b4_mantissas[128][2];
-static int b5_mantissas[16];
 
 /**
  * Quantization table: levels for symmetric. bits for asymmetric.
@@ -155,16 +153,6 @@ static av_cold void ac3_tables_init(void)
         b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
         b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
     }
-    /* generate ungrouped mantissa tables
-       reference: Tables 7.21 and 7.23 */
-    for (i = 0; i < 7; i++) {
-        /* bap=3 mantissas */
-        b3_mantissas[i] = symmetric_dequant(i, 7);
-    }
-    for (i = 0; i < 15; i++) {
-        /* bap=5 mantissas */
-        b5_mantissas[i] = symmetric_dequant(i, 15);
-    }
 
 #if (!USE_FIXED)
     /* generate dynamic range table
@@ -595,7 +583,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
             }
             break;
         case 3:
-            mantissa = b3_mantissas[get_bits(gbc, 3)];
+            mantissa = ff_ac3_bap3_mantissas[get_bits(gbc, 3)];
             break;
         case 4:
             if (m->b4) {
@@ -609,7 +597,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
             }
             break;
         case 5:
-            mantissa = b5_mantissas[get_bits(gbc, 4)];
+            mantissa = ff_ac3_bap5_mantissas[get_bits(gbc, 4)];
             break;
         default: /* 6 to 15 */
             /* Shift mantissa and sign-extend it. */
diff --git a/libavcodec/ac3dec_data.c b/libavcodec/ac3dec_data.c
index a3794ab223..527b3353f2 100644
--- a/libavcodec/ac3dec_data.c
+++ b/libavcodec/ac3dec_data.c
@@ -21,7 +21,7 @@
 
 /**
  * @file
- * Tables taken directly from the AC-3 spec.
+ * Tables taken directly from the AC-3 spec or derived from it.
  */
 
 #include "ac3dec_data.h"
@@ -42,6 +42,43 @@ const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3] = {
     { 3, 0, 1 }, { 3, 0, 2 }, { 3, 1, 0 }, { 3, 1, 1 }
 };
 
+/**
+ * Ungrouped mantissa tables; the extra entry is padding to avoid range checks
+ */
+#define SYMMETRIC_DEQUANT(code, levels) (((code - (levels >> 1)) * (1 << 24)) / levels)
+/**
+ * Table 7.21
+ */
+const int ff_ac3_bap3_mantissas[7 + 1] = {
+    SYMMETRIC_DEQUANT(0, 7),
+    SYMMETRIC_DEQUANT(1, 7),
+    SYMMETRIC_DEQUANT(2, 7),
+    SYMMETRIC_DEQUANT(3, 7),
+    SYMMETRIC_DEQUANT(4, 7),
+    SYMMETRIC_DEQUANT(5, 7),
+    SYMMETRIC_DEQUANT(6, 7),
+};
+/**
+ * Table 7.23
+ */
+const int ff_ac3_bap5_mantissas[15 + 1] = {
+    SYMMETRIC_DEQUANT(0,  15),
+    SYMMETRIC_DEQUANT(1,  15),
+    SYMMETRIC_DEQUANT(2,  15),
+    SYMMETRIC_DEQUANT(3,  15),
+    SYMMETRIC_DEQUANT(4,  15),
+    SYMMETRIC_DEQUANT(5,  15),
+    SYMMETRIC_DEQUANT(6,  15),
+    SYMMETRIC_DEQUANT(7,  15),
+    SYMMETRIC_DEQUANT(8,  15),
+    SYMMETRIC_DEQUANT(9,  15),
+    SYMMETRIC_DEQUANT(10, 15),
+    SYMMETRIC_DEQUANT(11, 15),
+    SYMMETRIC_DEQUANT(12, 15),
+    SYMMETRIC_DEQUANT(13, 15),
+    SYMMETRIC_DEQUANT(14, 15),
+};
+
 const uint8_t ff_eac3_hebap_tab[64] = {
     0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
     8, 8, 9, 9, 9, 10, 10, 10, 10, 11,
diff --git a/libavcodec/ac3dec_data.h b/libavcodec/ac3dec_data.h
index 975b52ef2c..4f3a23f6c7 100644
--- a/libavcodec/ac3dec_data.h
+++ b/libavcodec/ac3dec_data.h
@@ -24,9 +24,18 @@
 
 #include <stdint.h>
 
+#include "libavutil/attributes_internal.h"
+
+FF_VISIBILITY_PUSH_HIDDEN
+
 extern const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3];
 
+extern const int     ff_ac3_bap3_mantissas[ 7 + 1];
+extern const int     ff_ac3_bap5_mantissas[15 + 1];
+
 extern const uint8_t ff_eac3_hebap_tab[64];
 extern const uint8_t ff_eac3_default_spx_band_struct[17];
 
+FF_VISIBILITY_POP_HIDDEN
+
 #endif /* AVCODEC_AC3DEC_DATA_H */
-- 
2.45.2

From 8606b8f1e9a6570135e4e1e17bd5ae1e19224814 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
Date: Sat, 24 May 2025 18:39:26 +0200
Subject: [PATCH 2/5] avcodec/ac3dec: Deduplicate mantissas and their init code

Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
---
 libavcodec/ac3dec.c      | 92 ++++++++++------------------------------
 libavcodec/ac3dec_data.c | 60 +++++++++++++++++++++++++-
 libavcodec/ac3dec_data.h |  8 ++++
 3 files changed, 90 insertions(+), 70 deletions(-)

diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index dbe7f0b57c..0e18403793 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -46,17 +46,6 @@
 #include "decode.h"
 #include "kbdwin.h"
 
-/**
- * table for ungrouping 3 values in 7 bits.
- * used for exponents and bap=2 mantissas
- */
-static uint8_t ungroup_3_in_7_bits_tab[128][3];
-
-/** tables for ungrouping mantissas */
-static int b1_mantissas[32][3];
-static int b2_mantissas[128][3];
-static int b4_mantissas[128][2];
-
 /**
  * Quantization table: levels for symmetric. bits for asymmetric.
  * reference: Table 7.18 Mapping of bap to Quantizer
@@ -109,67 +98,28 @@ static const uint8_t ac3_default_coeffs[8][5][2] = {
     { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, },
 };
 
-/**
- * Symmetrical Dequantization
- * reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization
- *            Tables 7.19 to 7.23
- */
-static inline int
-symmetric_dequant(int code, int levels)
-{
-    return ((code - (levels >> 1)) * (1 << 24)) / levels;
-}
-
+#if (!USE_FIXED)
 /*
  * Initialize tables at runtime.
  */
-static av_cold void ac3_tables_init(void)
+static av_cold void ac3_float_tables_init(void)
 {
-    int i;
-
-    /* generate table for ungrouping 3 values in 7 bits
-       reference: Section 7.1.3 Exponent Decoding */
-    for (i = 0; i < 128; i++) {
-        ungroup_3_in_7_bits_tab[i][0] =  i / 25;
-        ungroup_3_in_7_bits_tab[i][1] = (i % 25) / 5;
-        ungroup_3_in_7_bits_tab[i][2] = (i % 25) % 5;
-    }
-
-    /* generate grouped mantissa tables
-       reference: Section 7.3.5 Ungrouping of Mantissas */
-    for (i = 0; i < 32; i++) {
-        /* bap=1 mantissas */
-        b1_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][0], 3);
-        b1_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][1], 3);
-        b1_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][2], 3);
-    }
-    for (i = 0; i < 128; i++) {
-        /* bap=2 mantissas */
-        b2_mantissas[i][0] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][0], 5);
-        b2_mantissas[i][1] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][1], 5);
-        b2_mantissas[i][2] = symmetric_dequant(ungroup_3_in_7_bits_tab[i][2], 5);
-
-        /* bap=4 mantissas */
-        b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
-        b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
-    }
-
-#if (!USE_FIXED)
     /* generate dynamic range table
        reference: Section 7.7.1 Dynamic Range Control */
-    for (i = 0; i < 256; i++) {
+    for (int i = 0; i < 256; i++) {
         int v = (i >> 5) - ((i >> 7) << 3) - 5;
         dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
     }
 
     /* generate compr dynamic range table
        reference: Section 7.7.2 Heavy Compression */
-    for (i = 0; i < 256; i++) {
+    for (int i = 0; i < 256; i++) {
         int v = (i >> 4) - ((i >> 7) << 4) - 4;
         ff_ac3_heavy_dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0xF) | 0x10);
     }
-#endif
+    ff_ac3_init_static();
 }
+#endif
 
 static void ac3_downmix(AVCodecContext *avctx)
 {
@@ -194,7 +144,6 @@ static void ac3_downmix(AVCodecContext *avctx)
  */
 static av_cold int ac3_decode_init(AVCodecContext *avctx)
 {
-    static AVOnce init_static_once = AV_ONCE_INIT;
     AC3DecodeContext *s = avctx->priv_data;
     const float scale = 1.0f;
     int i, ret;
@@ -235,7 +184,12 @@ static av_cold int ac3_decode_init(AVCodecContext *avctx)
         s->dlyptr[i] = s->delay[i];
     }
 
-    ff_thread_once(&init_static_once, ac3_tables_init);
+#if USE_FIXED
+    ff_ac3_init_static();
+#else
+    static AVOnce init_static_once = AV_ONCE_INIT;
+    ff_thread_once(&init_static_once, ac3_float_tables_init);
+#endif
 
     return 0;
 }
@@ -467,9 +421,9 @@ static int decode_exponents(AC3DecodeContext *s,
             av_log(s->avctx, AV_LOG_ERROR, "expacc %d is out-of-range\n", expacc);
             return AVERROR_INVALIDDATA;
         }
-        dexp[i++] = ungroup_3_in_7_bits_tab[expacc][0];
-        dexp[i++] = ungroup_3_in_7_bits_tab[expacc][1];
-        dexp[i++] = ungroup_3_in_7_bits_tab[expacc][2];
+        dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][0];
+        dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][1];
+        dexp[i++] = ff_ac3_ungroup_3_in_7_bits_tab[expacc][2];
     }
 
     /* convert to absolute exps and expand groups */
@@ -564,9 +518,9 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
                 mantissa = m->b1_mant[m->b1];
             } else {
                 int bits      = get_bits(gbc, 5);
-                mantissa      = b1_mantissas[bits][0];
-                m->b1_mant[1] = b1_mantissas[bits][1];
-                m->b1_mant[0] = b1_mantissas[bits][2];
+                mantissa      = ff_ac3_bap1_mantissas[bits][0];
+                m->b1_mant[1] = ff_ac3_bap1_mantissas[bits][1];
+                m->b1_mant[0] = ff_ac3_bap1_mantissas[bits][2];
                 m->b1         = 2;
             }
             break;
@@ -576,9 +530,9 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
                 mantissa = m->b2_mant[m->b2];
             } else {
                 int bits      = get_bits(gbc, 7);
-                mantissa      = b2_mantissas[bits][0];
-                m->b2_mant[1] = b2_mantissas[bits][1];
-                m->b2_mant[0] = b2_mantissas[bits][2];
+                mantissa      = ff_ac3_bap2_mantissas[bits][0];
+                m->b2_mant[1] = ff_ac3_bap2_mantissas[bits][1];
+                m->b2_mant[0] = ff_ac3_bap2_mantissas[bits][2];
                 m->b2         = 2;
             }
             break;
@@ -591,8 +545,8 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
                 mantissa = m->b4_mant;
             } else {
                 int bits   = get_bits(gbc, 7);
-                mantissa   = b4_mantissas[bits][0];
-                m->b4_mant = b4_mantissas[bits][1];
+                mantissa   = ff_ac3_bap4_mantissas[bits][0];
+                m->b4_mant = ff_ac3_bap4_mantissas[bits][1];
                 m->b4      = 1;
             }
             break;
diff --git a/libavcodec/ac3dec_data.c b/libavcodec/ac3dec_data.c
index 527b3353f2..7ef64f1f84 100644
--- a/libavcodec/ac3dec_data.c
+++ b/libavcodec/ac3dec_data.c
@@ -25,6 +25,7 @@
  */
 
 #include "ac3dec_data.h"
+#include "libavutil/thread.h"
 
 /**
  * Table used to ungroup 3 values stored in 5 bits.
@@ -43,9 +44,20 @@ const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3] = {
 };
 
 /**
- * Ungrouped mantissa tables; the extra entry is padding to avoid range checks
+ * table for ungrouping 3 values in 7 bits.
+ * used for exponents and bap=2 mantissas
+ */
+uint8_t ff_ac3_ungroup_3_in_7_bits_tab[128][3];
+
+/**
+ * Symmetrical Dequantization
+ * reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization
+ *            Tables 7.19 to 7.23
  */
 #define SYMMETRIC_DEQUANT(code, levels) (((code - (levels >> 1)) * (1 << 24)) / levels)
+/**
+ * Ungrouped mantissa tables; the extra entry is padding to avoid range checks
+ */
 /**
  * Table 7.21
  */
@@ -79,6 +91,52 @@ const int ff_ac3_bap5_mantissas[15 + 1] = {
     SYMMETRIC_DEQUANT(14, 15),
 };
 
+int ff_ac3_bap1_mantissas[32][3];
+int ff_ac3_bap2_mantissas[128][3];
+int ff_ac3_bap4_mantissas[128][2];
+
+static inline int
+symmetric_dequant(int code, int levels)
+{
+    return SYMMETRIC_DEQUANT(code, levels);
+}
+
+static av_cold void ac3_init_static(void)
+{
+    /* generate table for ungrouping 3 values in 7 bits
+       reference: Section 7.1.3 Exponent Decoding */
+    for (int i = 0; i < 128; ++i) {
+        ff_ac3_ungroup_3_in_7_bits_tab[i][0] =  i / 25;
+        ff_ac3_ungroup_3_in_7_bits_tab[i][1] = (i % 25) / 5;
+        ff_ac3_ungroup_3_in_7_bits_tab[i][2] = (i % 25) % 5;
+    }
+
+    /* generate grouped mantissa tables
+       reference: Section 7.3.5 Ungrouping of Mantissas */
+    for (int i = 0; i < 32; ++i) {
+        /* bap=1 mantissas */
+        ff_ac3_bap1_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][0], 3);
+        ff_ac3_bap1_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][1], 3);
+        ff_ac3_bap1_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_5_bits_tab[i][2], 3);
+    }
+    for (int i = 0; i < 128; ++i) {
+        /* bap=2 mantissas */
+        ff_ac3_bap2_mantissas[i][0] = symmetric_dequant(ff_ac3_ungroup_3_in_7_bits_tab[i][0], 5);
+        ff_ac3_bap2_mantissas[i][1] = symmetric_dequant(ff_ac3_ungroup_3_in_7_bits_tab[i][1], 5);
+        ff_ac3_bap2_mantissas[i][2] = symmetric_dequant(ff_ac3_ungroup_3_in_7_bits_tab[i][2], 5);
+
+        /* bap=4 mantissas */
+        ff_ac3_bap4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
+        ff_ac3_bap4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
+    }
+}
+
+av_cold void ff_ac3_init_static(void)
+{
+    static AVOnce ac3_init_static_once = AV_ONCE_INIT;
+    ff_thread_once(&ac3_init_static_once, ac3_init_static);
+}
+
 const uint8_t ff_eac3_hebap_tab[64] = {
     0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
     8, 8, 9, 9, 9, 10, 10, 10, 10, 11,
diff --git a/libavcodec/ac3dec_data.h b/libavcodec/ac3dec_data.h
index 4f3a23f6c7..1bbfa8b71e 100644
--- a/libavcodec/ac3dec_data.h
+++ b/libavcodec/ac3dec_data.h
@@ -29,13 +29,21 @@
 FF_VISIBILITY_PUSH_HIDDEN
 
 extern const uint8_t ff_ac3_ungroup_3_in_5_bits_tab[32][3];
+extern       uint8_t ff_ac3_ungroup_3_in_7_bits_tab[128][3];
 
 extern const int     ff_ac3_bap3_mantissas[ 7 + 1];
 extern const int     ff_ac3_bap5_mantissas[15 + 1];
 
+/** tables for ungrouping mantissas */
+extern int ff_ac3_bap1_mantissas[32][3];
+extern int ff_ac3_bap2_mantissas[128][3];
+extern int ff_ac3_bap4_mantissas[128][2];
+
 extern const uint8_t ff_eac3_hebap_tab[64];
 extern const uint8_t ff_eac3_default_spx_band_struct[17];
 
+void ff_ac3_init_static(void);
+
 FF_VISIBILITY_POP_HIDDEN
 
 #endif /* AVCODEC_AC3DEC_DATA_H */
-- 
2.45.2

From b9f448056848dcedbe9e5ba93e8af868fea2e782 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
Date: Sat, 24 May 2025 18:41:17 +0200
Subject: [PATCH 3/5] avcodec/ac3: Move gain value defines to ac3defs.h

Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
---
 libavcodec/ac3.h     | 11 -----------
 libavcodec/ac3defs.h | 11 +++++++++++
 2 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/libavcodec/ac3.h b/libavcodec/ac3.h
index 2386c15ad0..ccd437f700 100644
--- a/libavcodec/ac3.h
+++ b/libavcodec/ac3.h
@@ -81,17 +81,6 @@ typedef float                   SHORTFLOAT;
 
 #define AC3_LEVEL(x)            ROUND15((x) * FIXR15(M_SQRT1_2))
 
-/* pre-defined gain values */
-#define LEVEL_PLUS_3DB          M_SQRT2
-#define LEVEL_PLUS_1POINT5DB    1.1892071150027209
-#define LEVEL_MINUS_1POINT5DB   0.8408964152537145
-#define LEVEL_MINUS_3DB         M_SQRT1_2
-#define LEVEL_MINUS_4POINT5DB   0.5946035575013605
-#define LEVEL_MINUS_6DB         0.5000000000000000
-#define LEVEL_MINUS_9DB         0.3535533905932738
-#define LEVEL_ZERO              0.0000000000000000
-#define LEVEL_ONE               1.0000000000000000
-
 typedef struct AC3BitAllocParameters {
     int sr_code;
     int sr_shift;
diff --git a/libavcodec/ac3defs.h b/libavcodec/ac3defs.h
index ff92f0ac4a..f9b1be059f 100644
--- a/libavcodec/ac3defs.h
+++ b/libavcodec/ac3defs.h
@@ -34,6 +34,17 @@
 #define AC3_CRITICAL_BANDS 50
 #define AC3_MAX_CPL_BANDS  18
 
+/* pre-defined gain values */
+#define LEVEL_PLUS_3DB          M_SQRT2
+#define LEVEL_PLUS_1POINT5DB    1.1892071150027209
+#define LEVEL_MINUS_1POINT5DB   0.8408964152537145
+#define LEVEL_MINUS_3DB         M_SQRT1_2
+#define LEVEL_MINUS_4POINT5DB   0.5946035575013605
+#define LEVEL_MINUS_6DB         0.5000000000000000
+#define LEVEL_MINUS_9DB         0.3535533905932738
+#define LEVEL_ZERO              0.0000000000000000
+#define LEVEL_ONE               1.0000000000000000
+
 /* exponent encoding strategy */
 #define EXP_REUSE 0
 #define EXP_NEW   1
-- 
2.45.2

From fe2bc54990a06ef64e814491d53e1f00a20113d3 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
Date: Sat, 24 May 2025 18:58:24 +0200
Subject: [PATCH 4/5] avcodec/ac3{dec,enc}: Deduplicate gain levels table

(I don't know why the encoder only uses eight of the nine values.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
---
 libavcodec/ac3dec.c | 36 ++++++++++++------------------------
 libavcodec/ac3enc.c |  5 +----
 libavcodec/ac3tab.c | 14 ++++++++++++++
 libavcodec/ac3tab.h |  5 +++++
 4 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index 0e18403793..2b87c7a5f9 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -61,18 +61,6 @@ static float dynamic_range_tab[256];
 float ff_ac3_heavy_dynamic_range_tab[256];
 #endif
 
-/** Adjustments in dB gain */
-static const float gain_levels[9] = {
-    LEVEL_PLUS_3DB,
-    LEVEL_PLUS_1POINT5DB,
-    LEVEL_ONE,
-    LEVEL_MINUS_1POINT5DB,
-    LEVEL_MINUS_3DB,
-    LEVEL_MINUS_4POINT5DB,
-    LEVEL_MINUS_6DB,
-    LEVEL_ZERO,
-    LEVEL_MINUS_9DB
-};
 
 /** Adjustments in dB gain (LFE, +10 to -21 dB) */
 static const float gain_levels_lfe[32] = {
@@ -346,8 +334,8 @@ static int parse_frame_header(AC3DecodeContext *s)
 static int set_downmix_coeffs(AC3DecodeContext *s)
 {
     int i;
-    float cmix = gain_levels[s->  center_mix_level];
-    float smix = gain_levels[s->surround_mix_level];
+    float cmix = ff_ac3_gain_levels[s->  center_mix_level];
+    float smix = ff_ac3_gain_levels[s->surround_mix_level];
     float norm0, norm1;
     float downmix_coeffs[2][AC3_MAX_CHANNELS];
 
@@ -360,8 +348,8 @@ static int set_downmix_coeffs(AC3DecodeContext *s)
     }
 
     for (i = 0; i < s->fbw_channels; i++) {
-        downmix_coeffs[0][i] = gain_levels[ac3_default_coeffs[s->channel_mode][i][0]];
-        downmix_coeffs[1][i] = gain_levels[ac3_default_coeffs[s->channel_mode][i][1]];
+        downmix_coeffs[0][i] = ff_ac3_gain_levels[ac3_default_coeffs[s->channel_mode][i][0]];
+        downmix_coeffs[1][i] = ff_ac3_gain_levels[ac3_default_coeffs[s->channel_mode][i][1]];
     }
     if (s->channel_mode > 1 && s->channel_mode & 1) {
         downmix_coeffs[0][1] = downmix_coeffs[1][1] = cmix;
@@ -1562,10 +1550,10 @@ dependent_frame:
             s->output_mode  = AC3_CHMODE_STEREO;
         }
 
-        s->loro_center_mix_level   = gain_levels[s->  center_mix_level];
-        s->loro_surround_mix_level = gain_levels[s->surround_mix_level];
-        s->ltrt_center_mix_level   = gain_levels[s->  center_mix_level_ltrt];
-        s->ltrt_surround_mix_level = gain_levels[s->surround_mix_level_ltrt];
+        s->loro_center_mix_level   = ff_ac3_gain_levels[s->  center_mix_level];
+        s->loro_surround_mix_level = ff_ac3_gain_levels[s->surround_mix_level];
+        s->ltrt_center_mix_level   = ff_ac3_gain_levels[s->  center_mix_level_ltrt];
+        s->ltrt_surround_mix_level = ff_ac3_gain_levels[s->surround_mix_level_ltrt];
         switch (s->preferred_downmix) {
         case AC3_DMIXMOD_LTRT:
             s->preferred_stereo_downmix = AV_DOWNMIX_TYPE_LTRT;
@@ -1804,10 +1792,10 @@ skip:
             downmix_info->preferred_downmix_type = AV_DOWNMIX_TYPE_UNKNOWN;
             break;
         }
-        downmix_info->center_mix_level        = gain_levels[s->       center_mix_level];
-        downmix_info->center_mix_level_ltrt   = gain_levels[s->  center_mix_level_ltrt];
-        downmix_info->surround_mix_level      = gain_levels[s->     surround_mix_level];
-        downmix_info->surround_mix_level_ltrt = gain_levels[s->surround_mix_level_ltrt];
+        downmix_info->center_mix_level        = ff_ac3_gain_levels[s->       center_mix_level];
+        downmix_info->center_mix_level_ltrt   = ff_ac3_gain_levels[s->  center_mix_level_ltrt];
+        downmix_info->surround_mix_level      = ff_ac3_gain_levels[s->     surround_mix_level];
+        downmix_info->surround_mix_level_ltrt = ff_ac3_gain_levels[s->surround_mix_level_ltrt];
         if (s->lfe_mix_level_exists)
             downmix_info->lfe_mix_level       = gain_levels_lfe[s->lfe_mix_level];
         else
diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index a1783577c5..a316d4e4d7 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -71,10 +71,7 @@ static const float surmixlev_options[SURMIXLEV_NUM_OPTIONS] = {
 };
 
 #define EXTMIXLEV_NUM_OPTIONS 8
-static const float extmixlev_options[EXTMIXLEV_NUM_OPTIONS] = {
-    LEVEL_PLUS_3DB,  LEVEL_PLUS_1POINT5DB,  LEVEL_ONE,       LEVEL_MINUS_1POINT5DB,
-    LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, LEVEL_MINUS_6DB, LEVEL_ZERO
-};
+#define extmixlev_options ff_ac3_gain_levels
 
 /* The first two options apply only to the AC-3 encoders;
  * the rest is also valid for EAC-3. When modifying it,
diff --git a/libavcodec/ac3tab.c b/libavcodec/ac3tab.c
index 48c89a8ba0..b38e7237b3 100644
--- a/libavcodec/ac3tab.c
+++ b/libavcodec/ac3tab.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mathematics.h"
 
 #include "ac3tab.h"
 
@@ -147,6 +148,19 @@ const uint16_t ff_ac3_fast_gain_tab[8]= {
     0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400,
 };
 
+/** Adjustments in dB gain */
+const float ff_ac3_gain_levels[9] = {
+    LEVEL_PLUS_3DB,
+    LEVEL_PLUS_1POINT5DB,
+    LEVEL_ONE,
+    LEVEL_MINUS_1POINT5DB,
+    LEVEL_MINUS_3DB,
+    LEVEL_MINUS_4POINT5DB,
+    LEVEL_MINUS_6DB,
+    LEVEL_ZERO,
+    LEVEL_MINUS_9DB
+};
+
 const uint64_t ff_eac3_custom_channel_map_locations[16][2] = {
     { 1, AV_CH_FRONT_LEFT },
     { 1, AV_CH_FRONT_CENTER },
diff --git a/libavcodec/ac3tab.h b/libavcodec/ac3tab.h
index dcef643acb..3f83ce7b8c 100644
--- a/libavcodec/ac3tab.h
+++ b/libavcodec/ac3tab.h
@@ -26,6 +26,9 @@
 
 #include "ac3defs.h"
 
+#include "libavutil/attributes_internal.h"
+
+FF_VISIBILITY_PUSH_HIDDEN
 extern const uint16_t ff_ac3_frame_size_tab[38][3];
 extern const uint8_t  ff_ac3_channels_tab[8];
 extern const uint16_t ff_ac3_channel_layout_tab[8];
@@ -43,7 +46,9 @@ extern const int16_t  ff_ac3_floor_tab[8];
 extern const uint16_t ff_ac3_fast_gain_tab[8];
 extern const uint8_t  ff_ac3_band_start_tab[AC3_CRITICAL_BANDS+1];
 extern const uint8_t  ff_ac3_bin_to_band_tab[253];
+extern const float    ff_ac3_gain_levels[9];
 extern const uint64_t ff_eac3_custom_channel_map_locations[16][2];
+FF_VISIBILITY_POP_HIDDEN
 
 #define COMMON_CHANNEL_MAP \
     { { 0, 1,          }, { 0, 1, 2,         } },\
-- 
2.45.2

From 9322be841b7b12ea9e7a102b5e91a815cc53b649 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
Date: Fri, 30 May 2025 00:20:15 +0200
Subject: [PATCH 5/5] avcodec/ac3dec: Deduplicate tables

Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@outlook.com>
---
 libavcodec/ac3dec.c      | 44 ++++------------------------------------
 libavcodec/ac3dec_data.c | 33 ++++++++++++++++++++++++++++++
 libavcodec/ac3dec_data.h |  5 +++++
 3 files changed, 42 insertions(+), 40 deletions(-)

diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index 2b87c7a5f9..5eacab4475 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -46,47 +46,11 @@
 #include "decode.h"
 #include "kbdwin.h"
 
-/**
- * Quantization table: levels for symmetric. bits for asymmetric.
- * reference: Table 7.18 Mapping of bap to Quantizer
- */
-static const uint8_t quantization_tab[16] = {
-    0, 3, 5, 7, 11, 15,
-    5, 6, 7, 8, 9, 10, 11, 12, 14, 16
-};
-
 #if (!USE_FIXED)
 /** dynamic range table. converts codes to scale factors. */
 static float dynamic_range_tab[256];
 float ff_ac3_heavy_dynamic_range_tab[256];
-#endif
-
-
-/** Adjustments in dB gain (LFE, +10 to -21 dB) */
-static const float gain_levels_lfe[32] = {
-    3.162275, 2.818382, 2.511886, 2.238719, 1.995261, 1.778278, 1.584893,
-    1.412536, 1.258924, 1.122018, 1.000000, 0.891251, 0.794328, 0.707946,
-    0.630957, 0.562341, 0.501187, 0.446683, 0.398107, 0.354813, 0.316227,
-    0.281838, 0.251188, 0.223872, 0.199526, 0.177828, 0.158489, 0.141253,
-    0.125892, 0.112201, 0.100000, 0.089125
-};
 
-/**
- * Table for default stereo downmixing coefficients
- * reference: Section 7.8.2 Downmixing Into Two Channels
- */
-static const uint8_t ac3_default_coeffs[8][5][2] = {
-    { { 2, 7 }, { 7, 2 },                               },
-    { { 4, 4 },                                         },
-    { { 2, 7 }, { 7, 2 },                               },
-    { { 2, 7 }, { 5, 5 }, { 7, 2 },                     },
-    { { 2, 7 }, { 7, 2 }, { 6, 6 },                     },
-    { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 8, 8 },           },
-    { { 2, 7 }, { 7, 2 }, { 6, 7 }, { 7, 6 },           },
-    { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, },
-};
-
-#if (!USE_FIXED)
 /*
  * Initialize tables at runtime.
  */
@@ -348,8 +312,8 @@ static int set_downmix_coeffs(AC3DecodeContext *s)
     }
 
     for (i = 0; i < s->fbw_channels; i++) {
-        downmix_coeffs[0][i] = ff_ac3_gain_levels[ac3_default_coeffs[s->channel_mode][i][0]];
-        downmix_coeffs[1][i] = ff_ac3_gain_levels[ac3_default_coeffs[s->channel_mode][i][1]];
+        downmix_coeffs[0][i] = ff_ac3_gain_levels[ff_ac3_default_coeffs[s->channel_mode][i][0]];
+        downmix_coeffs[1][i] = ff_ac3_gain_levels[ff_ac3_default_coeffs[s->channel_mode][i][1]];
     }
     if (s->channel_mode > 1 && s->channel_mode & 1) {
         downmix_coeffs[0][1] = downmix_coeffs[1][1] = cmix;
@@ -547,7 +511,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
                 av_log(s->avctx, AV_LOG_ERROR, "bap %d is invalid in plain AC-3\n", bap);
                 bap = 15;
             }
-            mantissa = (unsigned)get_sbits(gbc, quantization_tab[bap]) << (24 - quantization_tab[bap]);
+            mantissa = (unsigned)get_sbits(gbc, ff_ac3_quantization_tab[bap]) << (24 - ff_ac3_quantization_tab[bap]);
             break;
         }
         coeffs[freq] = mantissa >> exps[freq];
@@ -1797,7 +1761,7 @@ skip:
         downmix_info->surround_mix_level      = ff_ac3_gain_levels[s->     surround_mix_level];
         downmix_info->surround_mix_level_ltrt = ff_ac3_gain_levels[s->surround_mix_level_ltrt];
         if (s->lfe_mix_level_exists)
-            downmix_info->lfe_mix_level       = gain_levels_lfe[s->lfe_mix_level];
+            downmix_info->lfe_mix_level       = ff_eac3_gain_levels_lfe[s->lfe_mix_level];
         else
             downmix_info->lfe_mix_level       = 0.0; // -inf dB
     }
diff --git a/libavcodec/ac3dec_data.c b/libavcodec/ac3dec_data.c
index 7ef64f1f84..0f5402c335 100644
--- a/libavcodec/ac3dec_data.c
+++ b/libavcodec/ac3dec_data.c
@@ -137,6 +137,30 @@ av_cold void ff_ac3_init_static(void)
     ff_thread_once(&ac3_init_static_once, ac3_init_static);
 }
 
+/**
+ * Quantization table: levels for symmetric. bits for asymmetric.
+ * reference: Table 7.18 Mapping of bap to Quantizer
+ */
+const uint8_t ff_ac3_quantization_tab[16] = {
+    0, 3, 5, 7, 11, 15,
+    5, 6, 7, 8, 9, 10, 11, 12, 14, 16
+};
+
+/**
+ * Table for default stereo downmixing coefficients
+ * reference: Section 7.8.2 Downmixing Into Two Channels
+ */
+const uint8_t ff_ac3_default_coeffs[8][5][2] = {
+    { { 2, 7 }, { 7, 2 },                               },
+    { { 4, 4 },                                         },
+    { { 2, 7 }, { 7, 2 },                               },
+    { { 2, 7 }, { 5, 5 }, { 7, 2 },                     },
+    { { 2, 7 }, { 7, 2 }, { 6, 6 },                     },
+    { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 8, 8 },           },
+    { { 2, 7 }, { 7, 2 }, { 6, 7 }, { 7, 6 },           },
+    { { 2, 7 }, { 5, 5 }, { 7, 2 }, { 6, 7 }, { 7, 6 }, },
+};
+
 const uint8_t ff_eac3_hebap_tab[64] = {
     0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
     8, 8, 9, 9, 9, 10, 10, 10, 10, 11,
@@ -152,3 +176,12 @@ const uint8_t ff_eac3_hebap_tab[64] = {
  */
 const uint8_t ff_eac3_default_spx_band_struct[17] =
 { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 };
+
+/** Adjustments in dB gain (LFE, +10 to -21 dB) */
+const float ff_eac3_gain_levels_lfe[32] = {
+    3.162275, 2.818382, 2.511886, 2.238719, 1.995261, 1.778278, 1.584893,
+    1.412536, 1.258924, 1.122018, 1.000000, 0.891251, 0.794328, 0.707946,
+    0.630957, 0.562341, 0.501187, 0.446683, 0.398107, 0.354813, 0.316227,
+    0.281838, 0.251188, 0.223872, 0.199526, 0.177828, 0.158489, 0.141253,
+    0.125892, 0.112201, 0.100000, 0.089125
+};
diff --git a/libavcodec/ac3dec_data.h b/libavcodec/ac3dec_data.h
index 1bbfa8b71e..613871627b 100644
--- a/libavcodec/ac3dec_data.h
+++ b/libavcodec/ac3dec_data.h
@@ -39,8 +39,13 @@ extern int ff_ac3_bap1_mantissas[32][3];
 extern int ff_ac3_bap2_mantissas[128][3];
 extern int ff_ac3_bap4_mantissas[128][2];
 
+extern const uint8_t ff_ac3_quantization_tab[16];
+
+extern const uint8_t ff_ac3_default_coeffs[8][5][2];
+
 extern const uint8_t ff_eac3_hebap_tab[64];
 extern const uint8_t ff_eac3_default_spx_band_struct[17];
+extern const float   ff_eac3_gain_levels_lfe[32];
 
 void ff_ac3_init_static(void);
 
-- 
2.45.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to