Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/opus_nsq_avx2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
webrtc-sys: patch
---

Enable AVX2 integer SILK quantization in Linux x86 WebRTC builds with upstream arithmetic fixes and CPU/OS compatibility checks.
1 change: 1 addition & 0 deletions webrtc-sys/libwebrtc/build_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ apply_patch "$COMMAND_DIR/patches/fix_pipewire_utils_compile.patch"
# See: https://crbug.com/376278218
# See: https://github.com/zed-industries/zed/pull/51433#discussion_r2944567608
apply_patch "$COMMAND_DIR/patches/disable_crel.patch" build
apply_patch "$COMMAND_DIR/patches/enable_opus_nsq_avx2.patch" third_party

cd third_party

Expand Down
196 changes: 196 additions & 0 deletions webrtc-sys/libwebrtc/patches/enable_opus_nsq_avx2.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
--- a/opus/BUILD.gn
+++ b/opus/BUILD.gn
@@ -64,6 +64,7 @@
defines += [
# Run Time CPU Detections (RTCD) is always enabled for x86.
"OPUS_HAVE_RTCD",
+ "LK_OPUS_NSQ_AVX2",
"CPU_INFO_BY_ASM",

# Chrome always targets SSE2+.
@@ -158,7 +159,22 @@
cflags = [ "-msse4.1" ]
}
}
- # TODO(dalecurtis): If libopus ever adds AVX support, add an opus_avx block.
+ # Only the integer SILK quantizer uses AVX2. Keep float dispatch unchanged.
+ source_set("opus_nsq_avx2") {
+ sources = [ "src/silk/x86/NSQ_del_dec_avx2.c" ]
+ configs -= [ "//build/config/compiler:chromium_code" ]
+ configs += [ "//build/config/compiler:no_chromium_code", ":opus_private_config", ":opus_config" ]
+ if (!is_debug) {
+ configs -= [ "//build/config/compiler:default_optimization" ]
+ configs += [ "//build/config/compiler:optimize_speed" ]
+ }
+ defines = [ "OPUS_X86_MAY_HAVE_AVX2" ]
+ if (!is_win || is_clang) {
+ cflags = [ "-mavx2", "-mfma", "-fwrapv", "-fno-strict-aliasing" ]
+ } else {
+ cflags = [ "/arch:AVX2" ]
+ }
+ }
}

# Note: Do not add any defines or include_dirs to this target, those should all
@@ -404,7 +420,7 @@
"src/silk/x86/main_sse.h",
"src/silk/x86/x86_silk_map.c",
]
- deps += [ ":opus_sse41" ]
+ deps += [ ":opus_sse41", ":opus_nsq_avx2" ]
}

if (use_opus_arm_optimization) {
--- a/opus/src/silk/x86/x86_silk_map.c
+++ b/opus/src/silk/x86/x86_silk_map.c
@@ -135,7 +135,11 @@
silk_NSQ_del_dec_c,
silk_NSQ_del_dec_c,
MAY_HAVE_SSE4_1( silk_NSQ_del_dec ), /* sse4.1 */
+#if defined(LK_OPUS_NSQ_AVX2)
+ silk_NSQ_del_dec_avx2
+#else
MAY_HAVE_AVX2( silk_NSQ_del_dec ) /* avx */
+#endif
};

#if defined(FIXED_POINT)
--- a/opus/src/silk/x86/NSQ_del_dec_avx2.c
+++ b/opus/src/silk/x86/NSQ_del_dec_avx2.c
@@ -97,16 +97,11 @@
return num;
}

-static OPUS_INLINE opus_int32 silk_sar_round_32(opus_int32 a, int bits)
-{
- silk_assert(bits > 0 && bits < 31);
- a += 1 << (bits-1);
- return a >> bits;
-}
-
static OPUS_INLINE opus_int64 silk_sar_round_smulww(opus_int32 a, opus_int32 b, int bits)
{
+#ifndef OPUS_CHECK_ASM
opus_int64 t;
+#endif
silk_assert(bits > 0 && bits < 63);
#ifdef OPUS_CHECK_ASM
return silk_RSHIFT_ROUND(silk_SMULWW(a, b), bits);
@@ -133,7 +128,7 @@
static OPUS_INLINE __m128i silk_mm_srai_round_epi32(__m128i a, int bits)
{
silk_assert(bits > 0 && bits < 31);
- return _mm_srai_epi32(_mm_add_epi32(a, _mm_set1_epi32(1 << (bits - 1))), bits);
+ return _mm_srai_epi32(_mm_add_epi32(_mm_srai_epi32(a, bits - 1), _mm_set1_epi32(1)), 1);
}

/* add/subtract with output saturated */
@@ -513,7 +508,7 @@
last_smple_idx = (last_smple_idx + DECISION_DELAY - 1) % DECISION_DELAY;
psSample = &psDelDec.Samples[last_smple_idx];
pulses[i - decisionDelay] =
- (opus_int8)silk_sar_round_32(silk_select_winner(psSample->Q_Q10, Winner_selector), 10);
+ (opus_int8)silk_RSHIFT_ROUND(silk_select_winner(psSample->Q_Q10, Winner_selector), 10);
pxq[i - decisionDelay] =
silk_sat16((opus_int32)silk_sar_round_smulww(silk_select_winner(psSample->Xq_Q14, Winner_selector), Gains_Q16[1], 14));
NSQ->sLTP_shp_Q14[NSQ->sLTP_shp_buf_idx - decisionDelay + i] =
@@ -562,7 +557,7 @@
psSample = &psDelDec.Samples[last_smple_idx];

pulses[i - decisionDelay] =
- (opus_int8)silk_sar_round_32(silk_select_winner(psSample->Q_Q10, Winner_selector), 10);
+ (opus_int8)silk_RSHIFT_ROUND(silk_select_winner(psSample->Q_Q10, Winner_selector), 10);
pxq[i - decisionDelay] =
silk_sat16((opus_int32)silk_sar_round_smulww(silk_select_winner(psSample->Xq_Q14, Winner_selector), Gain_Q10, 8));
NSQ->sLTP_shp_Q14[NSQ->sLTP_shp_buf_idx - decisionDelay + i] =
@@ -896,7 +891,7 @@
if (subfr > 0 || i >= decisionDelay)
{
pulses[i - decisionDelay] =
- (opus_int8)silk_sar_round_32(silk_select_winner(psLastSample->Q_Q10, Winner_selector), 10);
+ (opus_int8)silk_RSHIFT_ROUND(silk_select_winner(psLastSample->Q_Q10, Winner_selector), 10);
xq[i - decisionDelay] =
silk_sat16((opus_int32)silk_sar_round_smulww(silk_select_winner(psLastSample->Xq_Q14, Winner_selector), delayedGain_Q10[last_smple_idx], 8));
NSQ->sLTP_shp_Q14[NSQ->sLTP_shp_buf_idx - decisionDelay] =
@@ -954,12 +949,12 @@
silk_assert(inv_gain_Q31 != 0);

/* Scale input */
- inv_gain_Q26 = silk_sar_round_32(inv_gain_Q31, 5);
+ inv_gain_Q26 = silk_RSHIFT_ROUND(inv_gain_Q31, 5);
for (i = 0; i < psEncC->subfr_length; i+=4)
{
__m256i x = _mm256_cvtepi16_epi64(_mm_loadu_si64(&x16[i]));
x = _mm256_slli_epi64(_mm256_mul_epi32(x, _mm256_set1_epi32(inv_gain_Q26)), 16);
- _mm_storeu_si128((__m128i*)&x_sc_Q10[i], silk_cvtepi64_epi32_high(x));
+ _mm_storeu_si128((__m128i*)(void*)&x_sc_Q10[i], silk_cvtepi64_epi32_high(x));
}

/* After rewhitening the LTP state is un-scaled, so scale with inv_gain_Q16 */
@@ -986,7 +981,7 @@
for (i = NSQ->sLTP_shp_buf_idx - psEncC->ltp_mem_length; i < NSQ->sLTP_shp_buf_idx; i+=4)
{
opus_int32 *p = &NSQ->sLTP_shp_Q14[i];
- _mm_storeu_si128((__m128i*)p, silk_mm_smulww_epi32(_mm_loadu_si128((__m128i*)p), gain_adj_Q16));
+ _mm_storeu_si128((__m128i*)(void*)p, silk_mm_smulww_epi32(_mm_loadu_si128((__m128i*)(void*)p), gain_adj_Q16));
}

/* Scale long-term prediction state */
@@ -1041,13 +1036,13 @@
/* Allowing wrap around so that two wraps can cancel each other. The rare
cases where the result wraps around can only be triggered by invalid streams*/

- __m256i in_v = _mm256_cvtepi16_epi32(_mm_loadu_si128((__m128i*)&in_ptr[-8]));
- __m256i B_v = _mm256_cvtepi16_epi32(_mm_loadu_si128((__m128i*)& B[0]));
+ __m256i in_v = _mm256_cvtepi16_epi32(_mm_loadu_si128((__m128i*)(void*)&in_ptr[-8]));
+ __m256i B_v = _mm256_cvtepi16_epi32(_mm_loadu_si128((__m128i*)(void*)& B[0]));
__m256i sum = _mm256_mullo_epi32(in_v, silk_mm256_reverse_epi32(B_v));
if (order > 10)
{
- in_v = _mm256_cvtepi16_epi32(_mm_loadu_si128((__m128i*)&in_ptr[-16]));
- B_v = _mm256_cvtepi16_epi32(_mm_loadu_si128((__m128i*)&B [8]));
+ in_v = _mm256_cvtepi16_epi32(_mm_loadu_si128((__m128i*)(void*)&in_ptr[-16]));
+ B_v = _mm256_cvtepi16_epi32(_mm_loadu_si128((__m128i*)(void*)&B [8]));
B_v = silk_mm256_reverse_epi32(B_v);
}
else
@@ -1064,7 +1059,7 @@
out32_Q12 = silk_SUB32_ovflw( silk_LSHIFT( (opus_int32)*in_ptr, 12 ), out32_Q12 );

/* Scale to Q0 */
- out32 = silk_sar_round_32(out32_Q12, 12);
+ out32 = silk_RSHIFT_ROUND(out32_Q12, 12);

/* Saturate output */
out[ i ] = silk_sat16(out32);
--- a/opus/src/celt/x86/x86cpu.c
+++ b/opus/src/celt/x86/x86cpu.c
@@ -99,6 +99,18 @@

#endif

+/* Call only after CPUID.OSXSAVE: XGETBV otherwise faults on older machines. */
+static int opus_os_supports_avx(void)
+{
+#if defined(_MSC_VER)
+ return (_xgetbv(0) & 6) == 6;
+#else
+ unsigned int lo, hi;
+ __asm__ __volatile__("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0));
+ return (lo & 6) == 6;
+#endif
+}
+
typedef struct CPU_Feature{
/* SIMD: 128-bit */
int HW_SSE;
@@ -121,7 +133,8 @@
cpu_feature->HW_SSE = (info[3] & (1 << 25)) != 0;
cpu_feature->HW_SSE2 = (info[3] & (1 << 26)) != 0;
cpu_feature->HW_SSE41 = (info[2] & (1 << 19)) != 0;
- cpu_feature->HW_AVX2 = (info[2] & (1 << 28)) != 0 && (info[2] & (1 << 12)) != 0;
+ cpu_feature->HW_AVX2 = (info[2] & (1 << 28)) != 0 && (info[2] & (1 << 12)) != 0
+ && (info[2] & (1 << 27)) != 0 && opus_os_supports_avx();
if (cpu_feature->HW_AVX2 && nIds >= 7) {
cpuid(info, 7);
cpu_feature->HW_AVX2 = cpu_feature->HW_AVX2 && (info[1] & (1 << 5)) != 0;
Loading