Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: hassanhany via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: hassanhany <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PR] avcodec/opus: use precomputed NLSF weights for Silk decoder (PR #21767)
Date: Sun, 15 Feb 2026 19:45:06 -0000
Message-ID: <177118470682.25.14560419941143358218@009cbcb3d8cd> (raw)

PR #21767 opened by hassanhany
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21767
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21767.patch

Uses precomputed table of weights for NLSF decoding


>From 885f68c72b55c4dadb51e3594f9ec088fa512821 Mon Sep 17 00:00:00 2001
From: Hassan Hany <hassanhanyrashad@gmail.com>
Date: Sun, 15 Feb 2026 21:32:54 +0200
Subject: [PATCH] avcodec/opus: use precomputed NLSF weights for Silk decoder

---
 libavcodec/opus/silk.c | 14 ++-------
 libavcodec/opus/tab.c  | 70 ++++++++++++++++++++++++++++++++++++++++++
 libavcodec/opus/tab.h  |  3 +-
 3 files changed, 75 insertions(+), 12 deletions(-)

diff --git a/libavcodec/opus/silk.c b/libavcodec/opus/silk.c
index ffa2ec996b..3e07877a7c 100644
--- a/libavcodec/opus/silk.c
+++ b/libavcodec/opus/silk.c
@@ -353,20 +353,12 @@ static inline void silk_decode_lpc(SilkContext *s, SilkFrame *frame,
     for (i = 0; i < order; i++) {
         const uint8_t * codebook = s->wb ? ff_silk_lsf_codebook_wb  [lsf_i1] :
                                            ff_silk_lsf_codebook_nbmb[lsf_i1];
-        int cur, prev, next, weight_sq, weight, ipart, fpart, y, value;
+        int cur,  weight, value;
 
         /* find the weight of the residual */
-        /* TODO: precompute */
         cur = codebook[i];
-        prev = i ? codebook[i - 1] : 0;
-        next = i + 1 < order ? codebook[i + 1] : 256;
-        weight_sq = (1024 / (cur - prev) + 1024 / (next - cur)) << 16;
-
-        /* approximate square-root with mandated fixed-point arithmetic */
-        ipart = opus_ilog(weight_sq);
-        fpart = (weight_sq >> (ipart-8)) & 127;
-        y = ((ipart & 1) ? 32768 : 46214) >> ((32 - ipart)>>1);
-        weight = y + ((213 * fpart * y) >> 16);
+        weight = s->wb ? ff_silk_model_lsf_weight_wb[lsf_i1][i] :
+                         ff_silk_model_lsf_weight_nbmb[lsf_i1][i];
 
         value = cur * 128 + (lsf_res[i] * 16384) / weight;
         nlsf[i] = av_clip_uintp2(value, 15);
diff --git a/libavcodec/opus/tab.c b/libavcodec/opus/tab.c
index 5147b66b69..6a06f49161 100644
--- a/libavcodec/opus/tab.c
+++ b/libavcodec/opus/tab.c
@@ -734,6 +734,76 @@ const int8_t ff_silk_ltp_filter2_taps[32][5] = {
     {  2,   0,   9,  10,  88}
 };
 
+const uint16_t ff_silk_model_lsf_weight_nbmb[32][10] = {
+    { 2897, 2314, 2314, 2314, 2287, 2287, 2314, 2300, 2327, 2287},
+    { 2888, 2580, 2394, 2367, 2314, 2274, 2274, 2274, 2274, 2194},
+    { 2487, 2340, 2340, 2314, 2314, 2314, 2340, 2340, 2367, 2354},
+    { 3216, 2766, 2340, 2340, 2314, 2274, 2221, 2207, 2261, 2194},
+    { 2460, 2474, 2367, 2394, 2394, 2394, 2394, 2367, 2407, 2314},
+    { 3479, 3056, 2127, 2207, 2274, 2274, 2274, 2287, 2314, 2261},
+    { 3282, 3141, 2580, 2394, 2247, 2221, 2207, 2194, 2194, 2114},
+    { 4096, 3845, 2221, 2620, 2620, 2407, 2314, 2394, 2367, 2074},
+    { 3178, 3244, 2367, 2221, 2553, 2434, 2340, 2314, 2167, 2221},
+    { 3338, 3488, 2726, 2194, 2261, 2460, 2354, 2367, 2207, 2101},
+    { 2354, 2420, 2327, 2367, 2394, 2420, 2420, 2420, 2460, 2367},
+    { 3779, 3629, 2434, 2527, 2367, 2274, 2274, 2300, 2207, 2048},
+    { 3254, 3225, 2713, 2846, 2447, 2327, 2300, 2300, 2274, 2127},
+    { 3263, 3300, 2753, 2806, 2447, 2261, 2261, 2247, 2127, 2101},
+    { 2873, 2981, 2633, 2367, 2407, 2354, 2194, 2247, 2247, 2114},
+    { 3225, 3197, 2633, 2580, 2274, 2181, 2247, 2221, 2221, 2141},
+    { 3178, 3310, 2740, 2407, 2274, 2274, 2274, 2287, 2194, 2114},
+    { 3141, 3272, 2460, 2061, 2287, 2500, 2367, 2487, 2434, 2181},
+    { 3507, 3282, 2314, 2700, 2647, 2474, 2367, 2394, 2340, 2127},
+    { 3423, 3535, 3038, 3056, 2300, 1950, 2221, 2274, 2274, 2274},
+    { 3404, 3366, 2087, 2687, 2873, 2354, 2420, 2274, 2474, 2540},
+    { 3760, 3488, 1950, 2660, 2897, 2527, 2394, 2367, 2460, 2261},
+    { 3028, 3272, 2740, 2888, 2740, 2154, 2127, 2287, 2234, 2247},
+    { 3695, 3657, 2025, 1969, 2660, 2700, 2580, 2500, 2327, 2367},
+    { 3207, 3413, 2354, 2074, 2888, 2888, 2340, 2487, 2247, 2167},
+    { 3338, 3366, 2846, 2780, 2327, 2154, 2274, 2287, 2114, 2061},
+    { 2327, 2300, 2181, 2167, 2181, 2367, 2633, 2700, 2700, 2553},
+    { 2407, 2434, 2221, 2261, 2221, 2221, 2340, 2420, 2607, 2700},
+    { 3038, 3244, 2806, 2888, 2474, 2074, 2300, 2314, 2354, 2380},
+    { 2221, 2154, 2127, 2287, 2500, 2793, 2793, 2620, 2580, 2367},
+    { 3676, 3713, 2234, 1838, 2181, 2753, 2726, 2673, 2513, 2207},
+    { 2793, 3160, 2726, 2553, 2846, 2513, 2181, 2394, 2221, 2181}
+};
+
+const uint16_t ff_silk_model_lsf_weight_wb[32][16] = {  
+    { 3657, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2963, 2963, 2925, 2846},
+    { 3216, 3085, 2972, 3056, 3056, 3010, 3010, 3010, 2963, 2963, 3010, 2972, 2888, 2846, 2846, 2726},
+    { 3920, 4014, 2981, 3207, 3207, 2934, 3056, 2846, 3122, 3244, 2925, 2846, 2620, 2553, 2780, 2925},
+    { 3516, 3197, 3010, 3103, 3019, 2888, 2925, 2925, 2925, 2925, 2888, 2888, 2888, 2888, 2888, 2753},
+    { 5054, 5054, 2934, 3573, 3385, 3056, 3085, 2793, 3160, 3160, 2972, 2846, 2513, 2540, 2753, 2888},
+    { 4428, 4149, 2700, 2753, 2972, 3010, 2925, 2846, 2981, 3019, 2925, 2925, 2925, 2925, 2888, 2726},
+    { 3620, 3019, 2972, 3056, 3056, 2873, 2806, 3056, 3216, 3047, 2981, 3291, 3291, 2981, 3310, 2991},
+    { 5227, 5014, 2540, 3338, 3526, 3385, 3197, 3094, 3376, 2981, 2700, 2647, 2687, 2793, 2846, 2673},
+    { 5081, 5174, 4615, 4428, 2460, 2897, 3047, 3207, 3169, 2687, 2740, 2888, 2846, 2793, 2846, 2700},
+    { 3122, 2888, 2963, 2925, 2925, 2925, 2925, 2963, 2963, 2963, 2963, 2925, 2925, 2963, 2963, 2963},
+    { 4202, 3207, 2981, 3103, 3010, 2888, 2888, 2925, 2972, 2873, 2916, 3019, 2972, 3010, 3197, 2873},
+    { 3760, 3760, 3244, 3103, 2981, 2888, 2925, 2888, 2972, 2934, 2793, 2793, 2846, 2888, 2888, 2660},
+    { 3854, 4014, 3207, 3122, 3244, 2934, 3047, 2963, 2963, 3085, 2846, 2793, 2793, 2793, 2793, 2580},
+    { 3845, 4080, 3357, 3516, 3094, 2740, 3010, 2934, 3122, 3085, 2846, 2846, 2647, 2647, 2846, 2806},
+    { 5147, 4894, 3225, 3845, 3441, 3169, 2897, 3413, 3451, 2700, 2580, 2673, 2740, 2846, 2806, 2753},
+    { 4109, 3789, 3291, 3160, 2925, 2888, 2888, 2925, 2793, 2740, 2793, 2740, 2793, 2846, 2888, 2806},
+    { 5081, 5054, 3047, 3545, 3244, 3056, 3085, 2944, 3103, 2897, 2740, 2740, 2740, 2846, 2793, 2620},
+    { 4309, 4309, 2860, 2527, 3207, 3376, 3376, 3075, 3075, 3376, 3056, 2846, 2647, 2580, 2726, 2753},
+    { 3056, 2916, 2806, 2888, 2740, 2687, 2897, 3103, 3150, 3150, 3216, 3169, 3056, 3010, 2963, 2846},
+    { 4375, 3882, 2925, 2888, 2846, 2888, 2846, 2846, 2888, 2888, 2888, 2846, 2888, 2925, 2888, 2846},
+    { 2981, 2916, 2916, 2981, 2981, 3056, 3122, 3216, 3150, 3056, 3010, 2972, 2972, 2972, 2925, 2740},
+    { 4229, 4149, 3310, 3347, 2925, 2963, 2888, 2981, 2981, 2846, 2793, 2740, 2846, 2846, 2846, 2793},
+    { 4080, 4014, 3103, 3010, 2925, 2925, 2925, 2888, 2925, 2925, 2846, 2846, 2846, 2793, 2888, 2780},
+    { 4615, 4575, 3169, 3441, 3207, 2981, 2897, 3038, 3122, 2740, 2687, 2687, 2687, 2740, 2793, 2700},
+    { 4149, 4269, 3789, 3657, 2726, 2780, 2888, 2888, 3010, 2972, 2925, 2846, 2687, 2687, 2793, 2888},
+    { 4215, 3554, 2753, 2846, 2846, 2888, 2888, 2888, 2925, 2925, 2888, 2925, 2925, 2925, 2963, 2888},
+    { 5174, 4921, 2261, 3432, 3789, 3479, 3347, 2846, 3310, 3479, 3150, 2897, 2460, 2487, 2753, 2925},
+    { 3451, 3685, 3122, 3197, 3357, 3047, 3207, 3207, 2981, 3216, 3085, 2925, 2925, 2687, 2540, 2434},
+    { 2981, 3010, 2793, 2793, 2740, 2793, 2846, 2972, 3056, 3103, 3150, 3150, 3150, 3103, 3010, 3010},
+    { 2944, 2873, 2687, 2726, 2780, 3010, 3432, 3545, 3357, 3244, 3056, 3010, 2963, 2925, 2888, 2846},
+    { 3019, 2944, 2897, 3010, 3010, 2972, 3019, 3103, 3056, 3056, 3010, 2888, 2846, 2925, 2925, 2888},
+    { 3920, 3967, 3010, 3197, 3357, 3216, 3291, 3291, 3479, 3704, 3441, 2726, 2181, 2460, 2580, 2607}
+};
+
 const uint16_t ff_silk_ltp_scale_factor[] = {15565, 12288, 8192};
 
 const uint8_t ff_silk_shell_blocks[3][2] = {
diff --git a/libavcodec/opus/tab.h b/libavcodec/opus/tab.h
index 04d2a59bb6..c1d38f9e69 100644
--- a/libavcodec/opus/tab.h
+++ b/libavcodec/opus/tab.h
@@ -63,7 +63,8 @@ extern const uint16_t ff_silk_model_pitch_contour_nb10ms[];
 extern const uint16_t ff_silk_model_pitch_contour_nb20ms[];
 extern const uint16_t ff_silk_model_pitch_contour_mbwb10ms[];
 extern const uint16_t ff_silk_model_pitch_contour_mbwb20ms[];
-
+extern const uint16_t ff_silk_model_lsf_weight_nbmb[32][10];
+extern const uint16_t ff_silk_model_lsf_weight_wb[32][16];
 extern const uint16_t ff_silk_model_ltp_filter[];
 extern const uint16_t ff_silk_model_ltp_filter0_sel[];
 extern const uint16_t ff_silk_model_ltp_filter1_sel[];
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

                 reply	other threads:[~2026-02-15 19:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=177118470682.25.14560419941143358218@009cbcb3d8cd \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=code@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git