Skip to content

Commit

Permalink
opus
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlstrom-g committed Jun 2, 2024
1 parent c8d3eac commit 1467dc7
Show file tree
Hide file tree
Showing 79 changed files with 1,254 additions and 1,037 deletions.
37 changes: 20 additions & 17 deletions third_party/opus/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014 The Chromium Authors. All rights reserved.
# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

Expand All @@ -11,7 +11,7 @@ use_opus_fixed_point = current_cpu == "arm" || current_cpu == "arm64"
# If ARM optimizations shall be used to accelerate performance.
# TODO(scottmg): Disabled on Fuchsia for now, see https://crbug.com/775272.
use_opus_arm_optimization =
current_cpu == "arm" || (current_cpu == "arm64" && is_ios)
current_cpu == "arm" || (current_cpu == "arm64" && (is_ios || is_win))
&& !is_starboard

# NaCl, unlike Chrome, doesn't target SSE2 minimum, so skip optimizations for
Expand Down Expand Up @@ -626,21 +626,24 @@ test("test_opus_padding") {
deps = [ ":opus" ]
}

# Compilation fails on windows due to wstring/string mistmatch.
# This is not worth looking at it since the benchmark is tailored for android.
# It is ok to run it on linux though, for experimentation purpose.
if (is_android || is_linux || is_chromeos) {
test("opus_tests") {
sources = [ "tests/opus_benchmark.cc" ]

data = [ "tests/resources/speech_mono_32_48kHz.pcm" ]

deps = [
":opus",
"//base",
"//testing/gtest",
"//testing/gtest:gtest_main",
]
# Not all checkouts have a //base directory.
if (build_with_chromium) {
# Compilation fails on windows due to wstring/string mistmatch.
# This is not worth looking at it since the benchmark is tailored for android.
# It is ok to run it on linux though, for experimentation purpose.
if (is_android || is_linux || is_chromeos) {
test("opus_tests") {
sources = [ "tests/opus_benchmark.cc" ]

data = [ "tests/resources/speech_mono_32_48kHz.pcm" ]

deps = [
":opus",
"//base",
"//testing/gtest",
"//testing/gtest:gtest_main",
]
}
}
}
}
2 changes: 1 addition & 1 deletion third_party/opus/OWNERS
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[email protected]
henrika@chromium.org
gustaf@chromium.org
5 changes: 3 additions & 2 deletions third_party/opus/README.chromium
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Name: opus
URL: https://gitlab.xiph.org/xiph/opus
Version: 16395923
Version: 8cf872a1
License: BSD
License File: src/COPYING
Security Critical: yes
Expand All @@ -18,4 +18,5 @@ Local changes:
* add workaround to ignore some int-overflows when fuzzing (see crbug/1146174)

Opus' own unit tests are located in ./src/tests
Additional chromium tests are located in ./tests
Additional chromium tests are located in ./tests, and require chromium
checkout to run the tests.
2 changes: 1 addition & 1 deletion third_party/opus/convert_rtcd_assembler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

Expand Down
31 changes: 25 additions & 6 deletions third_party/opus/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,23 @@ if(NOT OPUS_ENABLE_FLOAT_API)
endif()

if(NOT OPUS_DISABLE_INTRINSICS)
if((OPUS_X86_MAY_HAVE_SSE AND NOT OPUS_X86_PRESUME_SSE) OR
if(((OPUS_X86_MAY_HAVE_SSE AND NOT OPUS_X86_PRESUME_SSE) OR
(OPUS_X86_MAY_HAVE_SSE2 AND NOT OPUS_X86_PRESUME_SSE2) OR
(OPUS_X86_MAY_HAVE_SSE4_1 AND NOT OPUS_X86_PRESUME_SSE4_1) OR
(OPUS_X86_MAY_HAVE_AVX AND NOT OPUS_X86_PRESUME_AVX))
(OPUS_X86_MAY_HAVE_AVX AND NOT OPUS_X86_PRESUME_AVX)) AND
RUNTIME_CPU_CAPABILITY_DETECTION)
target_compile_definitions(opus PRIVATE OPUS_HAVE_RTCD)
if(NOT MSVC)
if(CPU_INFO_BY_ASM_SUPPORTED)
target_compile_definitions(opus PRIVATE CPU_INFO_BY_ASM)
elseif(CPU_INFO_BY_C_SUPPORTED)
target_compile_definitions(opus PRIVATE CPU_INFO_BY_C)
else()
message(ERROR "Runtime cpu capability detection is enabled while CPU_INFO is not supported")
endif()
endif()
add_sources_group(opus celt ${celt_sources_x86_rtcd})
add_sources_group(opus silk ${silk_sources_x86_rtcd})
endif()

if(SSE1_SUPPORTED)
Expand Down Expand Up @@ -455,15 +467,13 @@ if(NOT OPUS_DISABLE_INTRINSICS)
endif()
endif()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "(arm|aarch64)")
add_sources_group(opus celt ${celt_sources_arm})
endif()

if(COMPILER_SUPPORT_NEON)
if(OPUS_MAY_HAVE_NEON)
if(RUNTIME_CPU_CAPABILITY_DETECTION)
message(STATUS "OPUS_MAY_HAVE_NEON enabling runtime detection")
target_compile_definitions(opus PRIVATE OPUS_HAVE_RTCD)
add_sources_group(opus celt ${celt_sources_arm_rtcd})
add_sources_group(opus silk ${silk_sources_arm_rtcd})
else()
message(ERROR "Runtime cpu capability detection needed for MAY_HAVE_NEON")
endif()
Expand Down Expand Up @@ -565,18 +575,24 @@ if(OPUS_BUILD_PROGRAMS)
target_include_directories(opus_custom_demo
PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(opus_custom_demo PRIVATE opus)
target_compile_definitions(opus_custom_demo PRIVATE OPUS_BUILD)
endif()

add_executable(opus_demo ${opus_demo_sources})
target_include_directories(opus_demo PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(opus_demo PRIVATE silk) # debug.h
target_include_directories(opus_demo PRIVATE celt) # arch.h
target_link_libraries(opus_demo PRIVATE opus ${OPUS_REQUIRED_LIBRARIES})
target_compile_definitions(opus_demo PRIVATE OPUS_BUILD)

# compare
add_executable(opus_compare ${opus_compare_sources})
target_include_directories(opus_compare PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(opus_compare PRIVATE opus ${OPUS_REQUIRED_LIBRARIES})
if(MSVC)
# move cosmetic warning to level 4 for opus_compare
target_compile_options(opus_compare PRIVATE /w44244)
endif()
endif()

if(BUILD_TESTING AND NOT BUILD_SHARED_LIBS)
Expand All @@ -587,6 +603,7 @@ if(BUILD_TESTING AND NOT BUILD_SHARED_LIBS)
target_include_directories(test_opus_decode
PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(test_opus_decode PRIVATE opus)
target_compile_definitions(test_opus_decode PRIVATE OPUS_BUILD)
if(OPUS_FIXED_POINT)
target_compile_definitions(test_opus_decode PRIVATE DISABLE_FLOAT_API)
endif()
Expand All @@ -608,6 +625,7 @@ if(BUILD_TESTING AND NOT BUILD_SHARED_LIBS)
target_include_directories(test_opus_api
PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
target_link_libraries(test_opus_api PRIVATE opus)
target_compile_definitions(test_opus_api PRIVATE OPUS_BUILD)
if(OPUS_FIXED_POINT)
target_compile_definitions(test_opus_api PRIVATE DISABLE_FLOAT_API)
endif()
Expand All @@ -620,6 +638,7 @@ if(BUILD_TESTING AND NOT BUILD_SHARED_LIBS)
target_include_directories(test_opus_encode
PRIVATE ${CMAKE_CURRENT_BINARY_DIR} celt)
target_link_libraries(test_opus_encode PRIVATE opus)
target_compile_definitions(test_opus_encode PRIVATE OPUS_BUILD)
add_test(NAME test_opus_encode COMMAND ${CMAKE_COMMAND}
-DTEST_EXECUTABLE=$<TARGET_FILE:test_opus_encode>
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
Expand Down
14 changes: 12 additions & 2 deletions third_party/opus/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ else
OPUS_SOURCES += $(OPUS_SOURCES_FLOAT)
endif

if CPU_X86
if HAVE_RTCD
CELT_SOURCES += $(CELT_SOURCES_X86_RTCD)
SILK_SOURCES += $(SILK_SOURCES_X86_RTCD)
endif
if HAVE_SSE
CELT_SOURCES += $(CELT_SOURCES_SSE)
endif
Expand All @@ -45,10 +50,13 @@ endif
if HAVE_SSE4_1
CELT_SOURCES += $(CELT_SOURCES_SSE4_1)
endif
endif

if CPU_ARM
CELT_SOURCES += $(CELT_SOURCES_ARM)
SILK_SOURCES += $(SILK_SOURCES_ARM)
if HAVE_RTCD
CELT_SOURCES += $(CELT_SOURCES_ARM_RTCD)
SILK_SOURCES += $(SILK_SOURCES_ARM_RTCD)
endif

if HAVE_ARM_NEON_INTR
CELT_SOURCES += $(CELT_SOURCES_ARM_NEON_INTR)
Expand Down Expand Up @@ -225,6 +233,8 @@ EXTRA_DIST = opus.pc.in \
cmake/RunTest.cmake \
cmake/config.h.cmake.in \
cmake/vla.c \
cmake/cpu_info_by_asm.c \
cmake/cpu_info_by_c.c \
meson/get-version.py \
meson/read-sources-list.py \
meson.build \
Expand Down
9 changes: 8 additions & 1 deletion third_party/opus/src/celt/arm/armcpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ opus_uint32 opus_cpu_capabilities(void)
"your platform. Reconfigure with --disable-rtcd (or send patches)."
#endif

int opus_select_arch(void)
static int opus_select_arch_impl(void)
{
opus_uint32 flags = opus_cpu_capabilities();
int arch = 0;
Expand Down Expand Up @@ -184,4 +184,11 @@ int opus_select_arch(void)
return arch;
}

int opus_select_arch(void) {
int arch = opus_select_arch_impl();
#ifdef FUZZING
arch = rand()%(arch+1);
#endif
return arch;
}
#endif
51 changes: 21 additions & 30 deletions third_party/opus/src/celt/arm/pitch_neon_intr.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,55 +137,38 @@ void dual_inner_prod_neon(const opus_val16 *x, const opus_val16 *y01, const opus
/* celt_inner_prod_neon_float_c_simulation() simulates the floating-point */
/* operations of celt_inner_prod_neon(), and both functions should have bit */
/* exact output. */
static opus_val32 celt_inner_prod_neon_float_c_simulation(const opus_val16 *x, const opus_val16 *y, int N)
static opus_val32 celt_inner_prod_neon_float_c_simulation(const opus_val16 *x, const opus_val16 *y, float *err, int N)
{
int i;
*err = 0;
opus_val32 xy, xy0 = 0, xy1 = 0, xy2 = 0, xy3 = 0;
for (i = 0; i < N - 3; i += 4) {
xy0 = MAC16_16(xy0, x[i + 0], y[i + 0]);
xy1 = MAC16_16(xy1, x[i + 1], y[i + 1]);
xy2 = MAC16_16(xy2, x[i + 2], y[i + 2]);
xy3 = MAC16_16(xy3, x[i + 3], y[i + 3]);
*err += ABS32(xy0)+ABS32(xy1)+ABS32(xy2)+ABS32(xy3);
}
xy0 += xy2;
xy1 += xy3;
xy = xy0 + xy1;
*err += ABS32(xy1)+ABS32(xy0)+ABS32(xy);
for (; i < N; i++) {
xy = MAC16_16(xy, x[i], y[i]);
*err += ABS32(xy);
}
*err = *err*2e-7 + N*1e-37;
return xy;
}

/* dual_inner_prod_neon_float_c_simulation() simulates the floating-point */
/* operations of dual_inner_prod_neon(), and both functions should have bit */
/* exact output. */
static void dual_inner_prod_neon_float_c_simulation(const opus_val16 *x, const opus_val16 *y01, const opus_val16 *y02,
int N, opus_val32 *xy1, opus_val32 *xy2)
int N, opus_val32 *xy1, opus_val32 *xy2, float *err)
{
int i;
opus_val32 xy01, xy02, xy01_0 = 0, xy01_1 = 0, xy01_2 = 0, xy01_3 = 0, xy02_0 = 0, xy02_1 = 0, xy02_2 = 0, xy02_3 = 0;
for (i = 0; i < N - 3; i += 4) {
xy01_0 = MAC16_16(xy01_0, x[i + 0], y01[i + 0]);
xy01_1 = MAC16_16(xy01_1, x[i + 1], y01[i + 1]);
xy01_2 = MAC16_16(xy01_2, x[i + 2], y01[i + 2]);
xy01_3 = MAC16_16(xy01_3, x[i + 3], y01[i + 3]);
xy02_0 = MAC16_16(xy02_0, x[i + 0], y02[i + 0]);
xy02_1 = MAC16_16(xy02_1, x[i + 1], y02[i + 1]);
xy02_2 = MAC16_16(xy02_2, x[i + 2], y02[i + 2]);
xy02_3 = MAC16_16(xy02_3, x[i + 3], y02[i + 3]);
}
xy01_0 += xy01_2;
xy02_0 += xy02_2;
xy01_1 += xy01_3;
xy02_1 += xy02_3;
xy01 = xy01_0 + xy01_1;
xy02 = xy02_0 + xy02_1;
for (; i < N; i++) {
xy01 = MAC16_16(xy01, x[i], y01[i]);
xy02 = MAC16_16(xy02, x[i], y02[i]);
}
*xy1 = xy01;
*xy2 = xy02;
*xy1 = celt_inner_prod_neon_float_c_simulation(x, y01, &err[0], N);
*xy2 = celt_inner_prod_neon_float_c_simulation(x, y02, &err[1], N);
}

#endif /* OPUS_CHECK_ASM */
Expand Down Expand Up @@ -225,7 +208,12 @@ opus_val32 celt_inner_prod_neon(const opus_val16 *x, const opus_val16 *y, int N)
}

#ifdef OPUS_CHECK_ASM
celt_assert(ABS32(celt_inner_prod_neon_float_c_simulation(x, y, N) - xy) <= VERY_SMALL);
{
float err, res;
res = celt_inner_prod_neon_float_c_simulation(x, y, &err, N);
/*if (ABS32(res - xy) > err) fprintf(stderr, "%g %g %g\n", res, xy, err);*/
celt_assert(ABS32(res - xy) <= err);
}
#endif

return xy;
Expand Down Expand Up @@ -280,9 +268,12 @@ void dual_inner_prod_neon(const opus_val16 *x, const opus_val16 *y01, const opus
#ifdef OPUS_CHECK_ASM
{
opus_val32 xy1_c, xy2_c;
dual_inner_prod_neon_float_c_simulation(x, y01, y02, N, &xy1_c, &xy2_c);
celt_assert(ABS32(xy1_c - *xy1) <= VERY_SMALL);
celt_assert(ABS32(xy2_c - *xy2) <= VERY_SMALL);
float err[2];
dual_inner_prod_neon_float_c_simulation(x, y01, y02, N, &xy1_c, &xy2_c, err);
/*if (ABS32(xy1_c - *xy1) > err[0]) fprintf(stderr, "dual1 fail: %g %g %g\n", xy1_c, *xy1, err[0]);
if (ABS32(xy2_c - *xy2) > err[1]) fprintf(stderr, "dual2 fail: %g %g %g\n", xy2_c, *xy2, err[1]);*/
celt_assert(ABS32(xy1_c - *xy1) <= err[0]);
celt_assert(ABS32(xy2_c - *xy2) <= err[1]);
}
#endif
}
Expand Down
9 changes: 5 additions & 4 deletions third_party/opus/src/celt/bands.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx,
sctx->itheta = itheta;
sctx->qalloc = qalloc;
}
static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, int b,
static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
celt_norm *lowband_out)
{
int c;
Expand All @@ -926,7 +926,6 @@ static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
sign = ec_dec_bits(ec, 1);
}
ctx->remaining_bits -= 1<<BITRES;
b-=1<<BITRES;
}
if (ctx->resynth)
x[0] = sign ? -NORM_SCALING : NORM_SCALING;
Expand Down Expand Up @@ -1134,7 +1133,7 @@ static unsigned quant_band(struct band_ctx *ctx, celt_norm *X,
/* Special case for one sample */
if (N==1)
{
return quant_band_n1(ctx, X, NULL, b, lowband_out);
return quant_band_n1(ctx, X, NULL, lowband_out);
}

if (tf_change>0)
Expand Down Expand Up @@ -1256,7 +1255,7 @@ static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm
/* Special case for one sample */
if (N==1)
{
return quant_band_n1(ctx, X, Y, b, lowband_out);
return quant_band_n1(ctx, X, Y, lowband_out);
}

orig_fill = fill;
Expand Down Expand Up @@ -1381,6 +1380,7 @@ static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm
return cm;
}

#ifndef DISABLE_UPDATE_DRAFT
static void special_hybrid_folding(const CELTMode *m, celt_norm *norm, celt_norm *norm2, int start, int M, int dual_stereo)
{
int n1, n2;
Expand All @@ -1393,6 +1393,7 @@ static void special_hybrid_folding(const CELTMode *m, celt_norm *norm, celt_norm
if (dual_stereo)
OPUS_COPY(&norm2[n1], &norm2[2*n1 - n2], n2-n1);
}
#endif

void quant_all_bands(int encode, const CELTMode *m, int start, int end,
celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks,
Expand Down
Loading

0 comments on commit 1467dc7

Please sign in to comment.