chromium/third_party/opus/BUILD.gn

# 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.

import("//build/config/arm.gni")
import("//testing/test.gni")

# If ARM optimizations shall be used to accelerate performance.
use_opus_arm_optimization =
    current_cpu == "arm" ||
    (current_cpu == "arm64" && (is_fuchsia || is_ios || is_win))

# NaCl, unlike Chrome, doesn't target SSE2 minimum, so skip optimizations for
# the sake of simplicity.
use_opus_x86_optimization =
    !is_nacl && (current_cpu == "x86" || current_cpu == "x64")

# If OPUS Run Time CPU Detections (RTCD) shall be used.
# Based on the conditions in celt/arm/armcpu.c:
# defined(_MSC_VER) || defined(__linux__).
use_opus_arm_rtcd =
    current_cpu == "arm" && (is_win || is_android || is_linux || is_chromeos)

config("opus_config") {
  include_dirs = [ "src/include" ]
}

config("opus_private_config") {
  defines = [
    "OPUS_BUILD",
    "OPUS_EXPORT=",
    "ENABLE_HARDENING",

    # Prefer alloca() over variable length arrays which are often inefficient;
    # the opus code will automatically handle this correctly per-platform.
    "USE_ALLOCA",
    "HAVE_ALLOCA_H",
  ]

  include_dirs = [
    "src",
    "src/celt",
    "src/silk",
    "src/silk/fixed",
    "src/silk/float",
  ]

  cflags = []

  if (is_win) {
    defines += [ "inline=__inline" ]

    cflags += [
      "/wd4305",  # Disable truncation warning in celt/pitch.c .
      "/wd4334",  # Disable 32-bit shift warning in src/opus_encoder.c .
    ]
  } else {
    defines += [
      "HAVE_LRINT",
      "HAVE_LRINTF",
    ]
  }

  if (is_official_build) {
    # Disable assertion messages from ENABLE_HARDENING, saving 16.4 kb.
    defines += [ "CHROMIUM_NO_LOGGING" ]
  }

  if (is_debug) {
    # Turn off a warning in opus_decoder.c when compiling without optimization.
    defines += [ "OPUS_WILL_BE_SLOW" ]
  }

  if (use_opus_x86_optimization) {
    defines += [
      # Run Time CPU Detections (RTCD) is always enabled for x86.
      "OPUS_HAVE_RTCD",
      "CPU_INFO_BY_ASM",

      # Chrome always targets SSE2+.
      "OPUS_X86_MAY_HAVE_SSE",
      "OPUS_X86_MAY_HAVE_SSE2",
      "OPUS_X86_PRESUME_SSE",
      "OPUS_X86_PRESUME_SSE2",

      # Some systems may have SSE4.1+ support.
      "OPUS_X86_MAY_HAVE_SSE4_1",

      # At present libopus has no AVX functions so no sources are add for this,
      # if you see linker errors on AVX code the this flag is why.
      "OPUS_X86_MAY_HAVE_AVX",
    ]
  }

  if (use_opus_arm_optimization) {
    if (current_cpu == "arm") {
      defines += [
        "OPUS_ARM_ASM",
        "OPUS_ARM_INLINE_ASM",
        "OPUS_ARM_INLINE_EDSP",
      ]
    }

    if (use_opus_arm_rtcd) {
      defines += [
        "OPUS_ARM_MAY_HAVE_EDSP",
        "OPUS_ARM_MAY_HAVE_MEDIA",
        "OPUS_HAVE_RTCD",
      ]
    }

    if (arm_use_neon) {
      defines += [
        "OPUS_ARM_MAY_HAVE_NEON",
        "OPUS_ARM_MAY_HAVE_NEON_INTR",
      ]
    }

    if (is_ios && current_cpu == "arm64") {
      # Runtime detection of CPU features not available on iOS.
      defines += [
        "OPUS_ARM_MAY_HAVE_NEON_INTR",
        "OPUS_ARM_PRESUME_NEON_INTR",
        "OPUS_ARM_PRESUME_AARCH64_NEON_INTR",
      ]
    }
  }
}

config("opus_test_config") {
  include_dirs = [
    "src/celt",
    "src/silk",
  ]

  if (is_win) {
    defines = [ "inline=__inline" ]
  }
  if (is_android) {
    libs = [ "log" ]
  }
  if (is_clang) {
    cflags = [ "-Wno-absolute-value" ]
  }
}

# GN orders flags on a target before flags from configs. The default config
# adds -Wall, and this flag has to be after -Wall -- so they need to
# come from a config and can't be on the target directly.
config("opus_test_no_nonnull_config") {
  # tests may pass a null pointer to functions for an argument marked as
  # requiring a non-null value by the nonnull function attribute, and expects
  # the function call to fail. Disable the -Wnonnull option to avoid a
  # compilation error if -Werror is specified.
  if (is_clang) {
    cflags = [ "-Wno-nonnull" ]
  }
}

if (use_opus_arm_rtcd) {
  action("convert_rtcd_assembler") {
    script = "convert_rtcd_assembler.py"
    outputs = [ "$target_gen_dir/celt_pitch_xcorr_arm_gnu.S" ]
    args = [
      rebase_path("//third_party/opus/src/celt/arm/arm2gnu.pl", root_build_dir),
      rebase_path("//third_party/opus/src/celt/arm/celt_pitch_xcorr_arm.s",
                  root_build_dir),
      rebase_path("$target_gen_dir/celt_pitch_xcorr_arm_gnu.S", root_build_dir),
    ]
  }
}

if (use_opus_x86_optimization) {
  source_set("opus_sse41") {
    sources = [
      "src/celt/x86/pitch_sse4_1.c",
      "src/silk/x86/NSQ_del_dec_sse4_1.c",
      "src/silk/x86/NSQ_sse4_1.c",
      "src/silk/x86/VAD_sse4_1.c",
      "src/silk/x86/VQ_WMat_EC_sse4_1.c",
    ]

    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [ "//build/config/compiler:no_chromium_code" ]
    configs += [
      ":opus_private_config",
      ":opus_config",
    ]

    if (!is_win || is_clang) {
      cflags = [ "-msse4.1" ]
    }
  }
  # TODO(dalecurtis): If libopus ever adds AVX support, add an opus_avx block.
}

# Note: Do not add any defines or include_dirs to this target, those should all
# go in the opus_private_config so they can be shared with intrinsic targets.
static_library("opus") {
  sources = [
    "src/celt/_kiss_fft_guts.h",
    "src/celt/arch.h",
    "src/celt/bands.c",
    "src/celt/bands.h",
    "src/celt/celt.c",
    "src/celt/celt.h",
    "src/celt/celt_decoder.c",
    "src/celt/celt_encoder.c",
    "src/celt/celt_lpc.c",
    "src/celt/celt_lpc.h",
    "src/celt/cpu_support.h",
    "src/celt/cwrs.c",
    "src/celt/cwrs.h",
    "src/celt/ecintrin.h",
    "src/celt/entcode.c",
    "src/celt/entcode.h",
    "src/celt/entdec.c",
    "src/celt/entdec.h",
    "src/celt/entenc.c",
    "src/celt/entenc.h",
    "src/celt/fixed_debug.h",
    "src/celt/fixed_generic.h",
    "src/celt/float_cast.h",
    "src/celt/kiss_fft.c",
    "src/celt/kiss_fft.h",
    "src/celt/laplace.c",
    "src/celt/laplace.h",
    "src/celt/mathops.c",
    "src/celt/mathops.h",
    "src/celt/mdct.c",
    "src/celt/mdct.h",
    "src/celt/mfrngcod.h",
    "src/celt/modes.c",
    "src/celt/modes.h",
    "src/celt/os_support.h",
    "src/celt/pitch.c",
    "src/celt/pitch.h",
    "src/celt/quant_bands.c",
    "src/celt/quant_bands.h",
    "src/celt/rate.c",
    "src/celt/rate.h",
    "src/celt/stack_alloc.h",
    "src/celt/static_modes_fixed.h",
    "src/celt/static_modes_float.h",
    "src/celt/vq.c",
    "src/celt/vq.h",
    "src/include/opus.h",
    "src/include/opus_custom.h",
    "src/include/opus_defines.h",
    "src/include/opus_multistream.h",
    "src/include/opus_projection.h",
    "src/include/opus_types.h",
    "src/silk/A2NLSF.c",
    "src/silk/API.h",
    "src/silk/CNG.c",
    "src/silk/HP_variable_cutoff.c",
    "src/silk/Inlines.h",
    "src/silk/LPC_analysis_filter.c",
    "src/silk/LPC_fit.c",
    "src/silk/LPC_inv_pred_gain.c",
    "src/silk/LP_variable_cutoff.c",
    "src/silk/MacroCount.h",
    "src/silk/MacroDebug.h",
    "src/silk/NLSF2A.c",
    "src/silk/NLSF_VQ.c",
    "src/silk/NLSF_VQ_weights_laroia.c",
    "src/silk/NLSF_decode.c",
    "src/silk/NLSF_del_dec_quant.c",
    "src/silk/NLSF_encode.c",
    "src/silk/NLSF_stabilize.c",
    "src/silk/NLSF_unpack.c",
    "src/silk/NSQ.c",
    "src/silk/NSQ.h",
    "src/silk/NSQ_del_dec.c",
    "src/silk/PLC.c",
    "src/silk/PLC.h",
    "src/silk/SigProc_FIX.h",
    "src/silk/VAD.c",
    "src/silk/VQ_WMat_EC.c",
    "src/silk/ana_filt_bank_1.c",
    "src/silk/biquad_alt.c",
    "src/silk/bwexpander.c",
    "src/silk/bwexpander_32.c",
    "src/silk/check_control_input.c",
    "src/silk/code_signs.c",
    "src/silk/control.h",
    "src/silk/control_SNR.c",
    "src/silk/control_audio_bandwidth.c",
    "src/silk/control_codec.c",
    "src/silk/debug.c",
    "src/silk/debug.h",
    "src/silk/dec_API.c",
    "src/silk/decode_core.c",
    "src/silk/decode_frame.c",
    "src/silk/decode_indices.c",
    "src/silk/decode_parameters.c",
    "src/silk/decode_pitch.c",
    "src/silk/decode_pulses.c",
    "src/silk/decoder_set_fs.c",
    "src/silk/define.h",
    "src/silk/enc_API.c",
    "src/silk/encode_indices.c",
    "src/silk/encode_pulses.c",
    "src/silk/errors.h",
    "src/silk/gain_quant.c",
    "src/silk/init_decoder.c",
    "src/silk/init_encoder.c",
    "src/silk/inner_prod_aligned.c",
    "src/silk/interpolate.c",
    "src/silk/lin2log.c",
    "src/silk/log2lin.c",
    "src/silk/macros.h",
    "src/silk/main.h",
    "src/silk/pitch_est_defines.h",
    "src/silk/pitch_est_tables.c",
    "src/silk/process_NLSFs.c",
    "src/silk/quant_LTP_gains.c",
    "src/silk/resampler.c",
    "src/silk/resampler_down2.c",
    "src/silk/resampler_down2_3.c",
    "src/silk/resampler_private.h",
    "src/silk/resampler_private_AR2.c",
    "src/silk/resampler_private_IIR_FIR.c",
    "src/silk/resampler_private_down_FIR.c",
    "src/silk/resampler_private_up2_HQ.c",
    "src/silk/resampler_rom.c",
    "src/silk/resampler_rom.h",
    "src/silk/resampler_structs.h",
    "src/silk/shell_coder.c",
    "src/silk/sigm_Q15.c",
    "src/silk/sort.c",
    "src/silk/stereo_LR_to_MS.c",
    "src/silk/stereo_MS_to_LR.c",
    "src/silk/stereo_decode_pred.c",
    "src/silk/stereo_encode_pred.c",
    "src/silk/stereo_find_predictor.c",
    "src/silk/stereo_quant_pred.c",
    "src/silk/structs.h",
    "src/silk/sum_sqr_shift.c",
    "src/silk/table_LSF_cos.c",
    "src/silk/tables.h",
    "src/silk/tables_LTP.c",
    "src/silk/tables_NLSF_CB_NB_MB.c",
    "src/silk/tables_NLSF_CB_WB.c",
    "src/silk/tables_gain.c",
    "src/silk/tables_other.c",
    "src/silk/tables_pitch_lag.c",
    "src/silk/tables_pulses_per_block.c",
    "src/silk/tuning_parameters.h",
    "src/silk/typedef.h",
    "src/src/analysis.c",
    "src/src/analysis.h",
    "src/src/mapping_matrix.c",
    "src/src/mapping_matrix.h",
    "src/src/mlp.c",
    "src/src/mlp.h",
    "src/src/mlp_data.c",
    "src/src/opus.c",
    "src/src/opus_decoder.c",
    "src/src/opus_encoder.c",
    "src/src/opus_multistream.c",
    "src/src/opus_multistream_decoder.c",
    "src/src/opus_multistream_encoder.c",
    "src/src/opus_private.h",
    "src/src/opus_projection_decoder.c",
    "src/src/opus_projection_encoder.c",
    "src/src/repacketizer.c",
    "src/src/tansig_table.h",
  ]

  # Silk floating-point
  sources += [
    "src/silk/float/LPC_analysis_filter_FLP.c",
    "src/silk/float/LPC_inv_pred_gain_FLP.c",
    "src/silk/float/LTP_analysis_filter_FLP.c",
    "src/silk/float/LTP_scale_ctrl_FLP.c",
    "src/silk/float/SigProc_FLP.h",
    "src/silk/float/apply_sine_window_FLP.c",
    "src/silk/float/autocorrelation_FLP.c",
    "src/silk/float/burg_modified_FLP.c",
    "src/silk/float/bwexpander_FLP.c",
    "src/silk/float/corrMatrix_FLP.c",
    "src/silk/float/encode_frame_FLP.c",
    "src/silk/float/energy_FLP.c",
    "src/silk/float/find_LPC_FLP.c",
    "src/silk/float/find_LTP_FLP.c",
    "src/silk/float/find_pitch_lags_FLP.c",
    "src/silk/float/find_pred_coefs_FLP.c",
    "src/silk/float/inner_product_FLP.c",
    "src/silk/float/k2a_FLP.c",
    "src/silk/float/main_FLP.h",
    "src/silk/float/noise_shape_analysis_FLP.c",
    "src/silk/float/pitch_analysis_core_FLP.c",
    "src/silk/float/process_gains_FLP.c",
    "src/silk/float/regularize_correlations_FLP.c",
    "src/silk/float/residual_energy_FLP.c",
    "src/silk/float/scale_copy_vector_FLP.c",
    "src/silk/float/scale_vector_FLP.c",
    "src/silk/float/schur_FLP.c",
    "src/silk/float/sort_FLP.c",
    "src/silk/float/structs_FLP.h",
    "src/silk/float/warped_autocorrelation_FLP.c",
    "src/silk/float/wrappers_FLP.c",
  ]

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [
    "//build/config/compiler:no_chromium_code",
    ":opus_private_config",
  ]
  public_configs = [ ":opus_config" ]

  if (!is_debug) {
    configs -= [ "//build/config/compiler:default_optimization" ]

    # TODO(crbug.com/621335) Rework this so that we don't have the confusion
    # between "optimize_speed" and "optimize_max".
    configs += [ "//build/config/compiler:optimize_speed" ]
  }

  deps = []

  if (use_opus_x86_optimization) {
    sources += [
      "src/celt/x86/celt_lpc_sse.h",
      "src/celt/x86/celt_lpc_sse4_1.c",
      "src/celt/x86/pitch_sse.c",
      "src/celt/x86/pitch_sse.h",
      "src/celt/x86/pitch_sse2.c",
      "src/celt/x86/vq_sse.h",
      "src/celt/x86/vq_sse2.c",
      "src/celt/x86/x86_celt_map.c",
      "src/celt/x86/x86cpu.c",
      "src/celt/x86/x86cpu.h",
      "src/silk/x86/SigProc_FIX_sse.h",
      "src/silk/x86/main_sse.h",
      "src/silk/x86/x86_silk_map.c",
    ]
    deps += [ ":opus_sse41" ]
  }

  if (use_opus_arm_optimization) {
    sources += [
      "src/celt/arm/fixed_arm64.h",
      "src/celt/arm/fixed_armv4.h",
      "src/celt/arm/fixed_armv5e.h",
      "src/celt/arm/kiss_fft_armv4.h",
      "src/celt/arm/kiss_fft_armv5e.h",
      "src/celt/arm/pitch_arm.h",
      "src/silk/arm/SigProc_FIX_armv4.h",
      "src/silk/arm/SigProc_FIX_armv5e.h",
      "src/silk/arm/macros_arm64.h",
      "src/silk/arm/macros_armv4.h",
      "src/silk/arm/macros_armv5e.h",
    ]

    if (use_opus_arm_rtcd) {
      sources += [
        "$target_gen_dir/celt_pitch_xcorr_arm_gnu.S",
        "src/celt/arm/arm_celt_map.c",
        "src/celt/arm/armcpu.c",
        "src/celt/arm/armcpu.h",
        "src/celt/arm/fft_arm.h",
        "src/celt/arm/mdct_arm.h",
        "src/celt/arm/pitch_arm.h",
        "src/silk/arm/arm_silk_map.c",
        "src/silk/fixed/main_FIX.h",
      ]
      deps += [ ":convert_rtcd_assembler" ]
    }

    if (arm_use_neon) {
      sources += [
        "src/celt/arm/celt_neon_intr.c",
        "src/celt/arm/pitch_neon_intr.c",
        "src/silk/arm/LPC_inv_pred_gain_arm.h",
        "src/silk/arm/LPC_inv_pred_gain_neon_intr.c",
        "src/silk/arm/NSQ_del_dec_arm.h",
        "src/silk/arm/NSQ_del_dec_neon_intr.c",
        "src/silk/arm/NSQ_neon.c",
        "src/silk/arm/NSQ_neon.h",
        "src/silk/arm/biquad_alt_arm.h",
        "src/silk/arm/biquad_alt_neon_intr.c",
      ]
    }
  }
}

executable("opus_compare") {
  sources = [ "src/src/opus_compare.c" ]

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [
    "//build/config/compiler:no_chromium_code",
    ":opus_test_config",
  ]

  deps = [
    ":opus",
    "//build/win:default_exe_manifest",
  ]
}

executable("opus_demo") {
  sources = [ "src/src/opus_demo.c" ]

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [
    "//build/config/compiler:no_chromium_code",
    ":opus_test_config",
  ]

  deps = [
    ":opus",
    "//build/win:default_exe_manifest",
  ]
}

test("test_opus_api") {
  sources = [ "src/tests/test_opus_api.c" ]

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [
    "//build/config/compiler:no_chromium_code",
    ":opus_test_config",
    ":opus_test_no_nonnull_config",
  ]

  deps = [ ":opus" ]
}

test("test_opus_encode") {
  sources = [
    "src/tests/opus_encode_regressions.c",
    "src/tests/test_opus_encode.c",
  ]

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [
    "//build/config/compiler:no_chromium_code",
    ":opus_test_config",
  ]

  deps = [ ":opus" ]
}

test("test_opus_decode") {
  sources = [ "src/tests/test_opus_decode.c" ]

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [
    "//build/config/compiler:no_chromium_code",
    ":opus_test_config",
    ":opus_test_no_nonnull_config",
  ]

  deps = [ ":opus" ]
}

test("test_opus_padding") {
  sources = [ "src/tests/test_opus_padding.c" ]

  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [
    "//build/config/compiler:no_chromium_code",
    ":opus_test_config",
  ]

  deps = [ ":opus" ]
}

# 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",
      ]
    }
  }
}