blob: a0b014db0127fb26cffccf9034cd232f9b8bbc2c [file]
/*
* Copyright (c) 2026, Alliance for Open Media. All rights reserved.
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <tuple>
#include <vector>
#include "config/aom_config.h"
#include "config/av1_rtcd.h"
#include "aom_dsp/prob.h"
#include "aom_ports/aom_timer.h"
#include "av1/common/av1_common_int.h"
#include "av1/common/blockd.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/reconinter.h"
#include "av1/common/reconintra.h"
#include "av1/encoder/block.h"
#include "av1/encoder/cost.h"
#include "av1/encoder/rd.h"
#include "test/acm_random.h"
#include "test/register_state_check.h"
#include "test/util.h"
#include "gtest/gtest.h"
namespace {
using libaom_test::ACMRandom;
using CdfBatchFn = void (*)(int *costs, int costs_stride, const uint16_t *cdf,
int cdf_stride, int n);
using CdfGenBatchFn = void (*)(int *costs, int costs_stride,
const uint16_t *cdf, int cdf_stride, int nsyms,
int n);
using CdfInvBatchFn = void (*)(int *costs, int costs_stride,
const uint16_t *cdf, int cdf_stride,
const int *inv_map, int nsyms, int n);
using CdfBatchTestParams =
std::tuple<CdfBatchFn, CdfBatchFn, CdfGenBatchFn, CdfInvBatchFn>;
// Fixture that fills av1_cost_lut_low once before exercising the cost helpers.
class CostTokensFromCdf4Test : public ::testing::Test {
protected:
void SetUp() override { av1_init_cost_luts(); }
};
class CostTokensFromCdfBatchTest
: public ::testing::TestWithParam<CdfBatchTestParams> {
protected:
void SetUp() override { av1_init_cost_luts(); }
};
// Builds a valid stored 4-symbol CDF from monotonic cumulative probs.
void BuildCdf4(ACMRandom *rnd, aom_cdf_prob cdf[5]) {
uint16_t v[3];
for (int k = 0; k < 3; ++k) v[k] = 1 + (rnd->Rand16() % (CDF_PROB_TOP - 1));
// Keep it monotonic; leave duplicates in so the EC_MIN_PROB clamp gets
// exercised.
std::sort(v, v + 3);
cdf[0] = AOM_ICDF(v[0]);
cdf[1] = AOM_ICDF(v[1]);
cdf[2] = AOM_ICDF(v[2]);
cdf[3] = AOM_ICDF(CDF_PROB_TOP);
cdf[4] = 0; // CDF adaptation counter slot; not read by the cost path.
}
// Builds a valid stored 2-symbol CDF.
void BuildCdf2(ACMRandom *rnd, aom_cdf_prob cdf[3]) {
cdf[0] = AOM_ICDF(1 + (rnd->Rand16() % (CDF_PROB_TOP - 1)));
cdf[1] = AOM_ICDF(CDF_PROB_TOP);
cdf[2] = 0; // CDF adaptation counter slot; not read by the cost path.
}
// Builds a valid stored `nsyms`-symbol CDF into `cdf[0..nsyms-1]`, leaving
// any stride padding `[nsyms, cdf_stride)` untouched for ASan/MSan checks.
void BuildCdfN(ACMRandom *rnd, aom_cdf_prob *cdf, int nsyms) {
std::vector<uint16_t> v(nsyms - 1);
for (int k = 0; k < nsyms - 1; ++k) {
v[k] = 1 + (rnd->Rand16() % (CDF_PROB_TOP - 1));
}
std::sort(v.begin(), v.end());
for (int k = 0; k < nsyms - 1; ++k) cdf[k] = AOM_ICDF(v[k]);
cdf[nsyms - 1] = AOM_ICDF(CDF_PROB_TOP);
}
// The hybrid LUT+arithmetic path must equal the pure-arithmetic
// av1_cost_symbol for every p15, including the COST_LUT_LOW boundary and the
// fallback range.
TEST_F(CostTokensFromCdf4Test, HybridMatchesArithmetic) {
for (int p = 1; p < CDF_PROB_TOP; ++p)
ASSERT_EQ(av1_cost_symbol((aom_cdf_prob)p), av1_cost_symbol_lut(p))
<< "p15=" << p;
}
// The N=4 specialization must be bit-identical to the generic path.
TEST_F(CostTokensFromCdf4Test, MatchesGenericRandom) {
ACMRandom rnd(ACMRandom::DeterministicSeed());
for (int iter = 0; iter < 200000; ++iter) {
aom_cdf_prob cdf[5];
BuildCdf4(&rnd, cdf);
int ref[4], spec[4];
av1_cost_tokens_from_cdf(ref, cdf, nullptr);
av1_cost_tokens_from_cdf_4(spec, cdf);
for (int i = 0; i < 4; ++i)
ASSERT_EQ(ref[i], spec[i]) << "iter=" << iter << " i=" << i;
}
}
// Degenerate CDFs (near-zero probs) must clamp identically.
TEST_F(CostTokensFromCdf4Test, MatchesGenericEdge) {
const aom_cdf_prob edge[][5] = {
{ AOM_ICDF(1), AOM_ICDF(2), AOM_ICDF(3), AOM_ICDF(CDF_PROB_TOP), 0 },
{ AOM_ICDF(1), AOM_ICDF(1), AOM_ICDF(1), AOM_ICDF(CDF_PROB_TOP), 0 },
{ AOM_ICDF(CDF_PROB_TOP - 3), AOM_ICDF(CDF_PROB_TOP - 2),
AOM_ICDF(CDF_PROB_TOP - 1), AOM_ICDF(CDF_PROB_TOP), 0 },
{ AOM_ICDF(8192), AOM_ICDF(16384), AOM_ICDF(24576), AOM_ICDF(CDF_PROB_TOP),
0 },
};
for (const auto &cdf : edge) {
int ref[4], spec[4];
av1_cost_tokens_from_cdf(ref, cdf, nullptr);
av1_cost_tokens_from_cdf_4(spec, cdf);
for (int i = 0; i < 4; ++i) ASSERT_EQ(ref[i], spec[i]) << "i=" << i;
}
}
// Runs `fn` for a range of context counts and output strides and checks each
// filled entry against the generic scalar path. Exercises the SIMD main loop
// plus every tail length (n % 8) and both output strides (8 = base_cost,
// 4 = br_rate).
void CheckBatchAgainstGeneric(CdfBatchFn fn) {
ACMRandom rnd(ACMRandom::DeterministicSeed());
const int kCdfStride = CDF_SIZE(4); // 5
constexpr int kSentinel = -1234567;
constexpr int kCanary = 0x5A5A5A5A;
constexpr int kGuard = 8;
for (const int costs_stride : { 4, 8 }) {
{
std::vector<int> out_buf(2 * kGuard + costs_stride, kCanary);
aom_cdf_prob dummy_cdf[1] = { 0 };
API_REGISTER_STATE_CHECK(
fn(out_buf.data() + kGuard, costs_stride, dummy_cdf, kCdfStride, 0));
for (const int val : out_buf) ASSERT_EQ(kCanary, val);
}
for (int n = 1; n <= 45; ++n) {
std::vector<aom_cdf_prob> cdf(n * kCdfStride);
for (int k = 0; k < n; ++k) BuildCdf4(&rnd, &cdf[k * kCdfStride]);
std::vector<int> out_buf(2 * kGuard + n * costs_stride, kCanary);
int *const out = out_buf.data() + kGuard;
std::fill(out, out + n * costs_stride, kSentinel);
std::vector<int> ref(n * 4);
for (int k = 0; k < n; ++k)
av1_cost_tokens_from_cdf(&ref[k * 4], &cdf[k * kCdfStride], nullptr);
API_REGISTER_STATE_CHECK(
fn(out, costs_stride, cdf.data(), kCdfStride, n));
for (int g = 0; g < kGuard; ++g) {
ASSERT_EQ(kCanary, out_buf[g]) << "underflow guard g=" << g;
ASSERT_EQ(kCanary, out_buf[kGuard + n * costs_stride + g])
<< "overflow guard g=" << g;
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < 4; ++i)
ASSERT_EQ(ref[k * 4 + i], out[k * costs_stride + i])
<< "stride=" << costs_stride << " n=" << n << " k=" << k
<< " i=" << i;
for (int i = 4; i < costs_stride; ++i)
ASSERT_EQ(kSentinel, out[k * costs_stride + i])
<< "stride=" << costs_stride << " n=" << n << " k=" << k
<< " i=" << i;
}
}
}
}
// Same as CheckBatchAgainstGeneric but for the N=2 batch primitive.
void CheckBatch2AgainstGeneric(CdfBatchFn fn) {
ACMRandom rnd(ACMRandom::DeterministicSeed());
const int kCdfStride = CDF_SIZE(2); // 3
constexpr int kSentinel = -1234567;
constexpr int kCanary = 0x5A5A5A5A;
constexpr int kGuard = 8;
for (const int costs_stride : { 2, 4 }) {
{
std::vector<int> out_buf(2 * kGuard + costs_stride, kCanary);
aom_cdf_prob dummy_cdf[1] = { 0 };
API_REGISTER_STATE_CHECK(
fn(out_buf.data() + kGuard, costs_stride, dummy_cdf, kCdfStride, 0));
for (const int val : out_buf) ASSERT_EQ(kCanary, val);
}
for (int n = 1; n <= 45; ++n) {
std::vector<aom_cdf_prob> cdf(n * kCdfStride);
for (int k = 0; k < n; ++k) BuildCdf2(&rnd, &cdf[k * kCdfStride]);
std::vector<int> out_buf(2 * kGuard + n * costs_stride, kCanary);
int *const out = out_buf.data() + kGuard;
std::fill(out, out + n * costs_stride, kSentinel);
std::vector<int> ref(n * 2);
for (int k = 0; k < n; ++k)
av1_cost_tokens_from_cdf(&ref[k * 2], &cdf[k * kCdfStride], nullptr);
API_REGISTER_STATE_CHECK(
fn(out, costs_stride, cdf.data(), kCdfStride, n));
for (int g = 0; g < kGuard; ++g) {
ASSERT_EQ(kCanary, out_buf[g]) << "underflow guard g=" << g;
ASSERT_EQ(kCanary, out_buf[kGuard + n * costs_stride + g])
<< "overflow guard g=" << g;
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < 2; ++i)
ASSERT_EQ(ref[k * 2 + i], out[k * costs_stride + i])
<< "stride=" << costs_stride << " n=" << n << " k=" << k
<< " i=" << i;
for (int i = 2; i < costs_stride; ++i)
ASSERT_EQ(kSentinel, out[k * costs_stride + i])
<< "stride=" << costs_stride << " n=" << n << " k=" << k
<< " i=" << i;
}
}
}
}
// Times the C vs target batch primitive on the real av1_fill_coeff_costs call
// shapes and prints ns/call and the speedup. DISABLED_ so it runs only on
// request (--gtest_also_run_disabled_tests).
void RunBatchSpeed(const char *name, CdfBatchFn c_fn, CdfBatchFn simd_fn,
int cdf_stride, int syms, const char *label, int n,
int costs_stride) {
ACMRandom rnd(ACMRandom::DeterministicSeed());
std::vector<aom_cdf_prob> cdf(n * cdf_stride);
for (int k = 0; k < n; ++k) {
if (syms == 4)
BuildCdf4(&rnd, &cdf[k * cdf_stride]);
else
BuildCdf2(&rnd, &cdf[k * cdf_stride]);
}
std::vector<int> out(n * costs_stride);
const CdfBatchFn funcs[2] = { c_fn, simd_fn };
const int kLoops = 1000000;
double ns[2];
for (int i = 0; i < 2; ++i) {
aom_usec_timer timer;
aom_usec_timer_start(&timer);
for (int j = 0; j < kLoops; ++j)
funcs[i](out.data(), costs_stride, cdf.data(), cdf_stride, n);
aom_usec_timer_mark(&timer);
ns[i] =
1000.0 * static_cast<double>(aom_usec_timer_elapsed(&timer)) / kLoops;
}
printf("%s %-9s n=%2d stride=%d: c %7.1f ns tst %7.1f ns (%4.2fx)\n", name,
label, n, costs_stride, ns[0], ns[1], ns[0] / ns[1]);
}
void CheckGeneralBatchAgainstGeneric(CdfGenBatchFn fn) {
ACMRandom rnd(ACMRandom::DeterministicSeed());
constexpr int kSentinel = -1234567;
constexpr int kCanary = 0x5A5A5A5A;
constexpr int kGuard = 8;
for (int nsyms = 2; nsyms <= 17; ++nsyms) {
for (const int cdf_stride : { nsyms, nsyms + 1, nsyms + 4 }) {
for (const int costs_stride : { nsyms, nsyms + 3 }) {
// Verify n == 0 leaves output and guards untouched.
{
std::vector<int> out_buf(2 * kGuard + costs_stride, kCanary);
aom_cdf_prob dummy_cdf[1] = { 0 };
API_REGISTER_STATE_CHECK(fn(out_buf.data() + kGuard, costs_stride,
dummy_cdf, cdf_stride, nsyms, 0));
for (const int val : out_buf) ASSERT_EQ(kCanary, val);
}
for (const int n : { 1, 2, 3, 4, 5, 7, 8, 9, 10, 13, 19 }) {
// Allocate exact tight tail so ASan catches any overread past
// cdf[(n - 1) * cdf_stride + nsyms - 1] and MSan catches reading
// uninitialized padding in [nsyms, cdf_stride).
const int cdf_elems = (n - 1) * cdf_stride + nsyms;
std::unique_ptr<aom_cdf_prob[]> cdf(new aom_cdf_prob[cdf_elems]);
for (int k = 0; k < n; ++k) {
aom_cdf_prob *const row = &cdf[k * cdf_stride];
if (k == 0) {
// Exercise p15 == CDF_PROB_TOP - 1 on symbol 0 (prob_idx 128)
// and minimum-prob duplicates.
for (int i = 0; i < nsyms - 1; ++i) row[i] = 1;
row[nsyms - 1] = 0;
} else if (k == 1) {
// Exercise p15 == CDF_PROB_TOP - 1 on the last symbol
// (cost_cdf_tail8_avx2 prob_idx 128 clamp).
for (int i = 0; i < nsyms - 1; ++i) row[i] = CDF_PROB_TOP - 1;
row[nsyms - 1] = 0;
} else {
BuildCdfN(&rnd, row, nsyms);
}
}
std::vector<int> out_buf(2 * kGuard + n * costs_stride, kCanary);
int *const out = out_buf.data() + kGuard;
std::fill(out, out + n * costs_stride, kSentinel);
std::vector<int> ref(n * nsyms);
for (int k = 0; k < n; ++k) {
av1_cost_tokens_from_cdf(&ref[k * nsyms], &cdf[k * cdf_stride],
nullptr);
}
API_REGISTER_STATE_CHECK(
fn(out, costs_stride, cdf.get(), cdf_stride, nsyms, n));
for (int g = 0; g < kGuard; ++g) {
ASSERT_EQ(kCanary, out_buf[g]) << "underflow guard g=" << g;
ASSERT_EQ(kCanary, out_buf[kGuard + n * costs_stride + g])
<< "overflow guard g=" << g;
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < nsyms; ++i) {
ASSERT_EQ(ref[k * nsyms + i], out[k * costs_stride + i])
<< "nsyms=" << nsyms << " cdf_stride=" << cdf_stride
<< " costs_stride=" << costs_stride << " n=" << n
<< " k=" << k << " i=" << i;
}
for (int i = nsyms; i < costs_stride; ++i) {
ASSERT_EQ(kSentinel, out[k * costs_stride + i])
<< "overwrote padding: nsyms=" << nsyms << " k=" << k
<< " i=" << i;
}
}
}
}
}
}
}
void CheckInvBatchAgainstGeneric(CdfInvBatchFn fn) {
ACMRandom rnd(ACMRandom::DeterministicSeed());
constexpr int kSentinel = -7654321;
constexpr int kCanary = 0x5A5A5A5A;
constexpr int kGuard = 8;
// Test all real extended transform inverse maps plus synthetic permutations.
for (int set_type = 1; set_type < EXT_TX_SET_TYPES; ++set_type) {
const int nsyms = av1_num_ext_tx_set[set_type];
const int *const inv_map = av1_ext_tx_inv[set_type];
for (const int cdf_stride :
{ nsyms, CDF_SIZE(nsyms), CDF_SIZE(TX_TYPES) }) {
{
std::vector<int> out_buf(2 * kGuard + TX_TYPES, kCanary);
aom_cdf_prob dummy_cdf[1] = { 0 };
API_REGISTER_STATE_CHECK(fn(out_buf.data() + kGuard, TX_TYPES,
dummy_cdf, cdf_stride, inv_map, nsyms, 0));
for (const int val : out_buf) ASSERT_EQ(kCanary, val);
}
for (const int n : { 1, 2, 5, 13 }) {
const int cdf_elems = (n - 1) * cdf_stride + nsyms;
std::unique_ptr<aom_cdf_prob[]> cdf(new aom_cdf_prob[cdf_elems]);
for (int k = 0; k < n; ++k) {
aom_cdf_prob *const row = &cdf[k * cdf_stride];
if (k == 0) {
for (int i = 0; i < nsyms - 1; ++i) row[i] = CDF_PROB_TOP - 1;
row[nsyms - 1] = 0;
} else {
BuildCdfN(&rnd, row, nsyms);
}
}
std::vector<int> out_buf(2 * kGuard + n * TX_TYPES, kCanary);
int *const out = out_buf.data() + kGuard;
std::fill(out, out + n * TX_TYPES, kSentinel);
std::vector<int> ref(n * TX_TYPES, kSentinel);
for (int k = 0; k < n; ++k) {
av1_cost_tokens_from_cdf(&ref[k * TX_TYPES], &cdf[k * cdf_stride],
inv_map);
}
API_REGISTER_STATE_CHECK(
fn(out, TX_TYPES, cdf.get(), cdf_stride, inv_map, nsyms, n));
for (int g = 0; g < kGuard; ++g) {
ASSERT_EQ(kCanary, out_buf[g]) << "underflow guard g=" << g;
ASSERT_EQ(kCanary, out_buf[kGuard + n * TX_TYPES + g])
<< "overflow guard g=" << g;
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < TX_TYPES; ++i) {
ASSERT_EQ(ref[k * TX_TYPES + i], out[k * TX_TYPES + i])
<< "set_type=" << set_type << " nsyms=" << nsyms
<< " cdf_stride=" << cdf_stride << " n=" << n << " k=" << k
<< " i=" << i;
}
}
}
}
}
// Exercise all nsyms in [2..17] with reversed mapping, tight/padded strides,
// and extreme CDFs.
for (int nsyms = 2; nsyms <= 17; ++nsyms) {
std::vector<int> inv_map(nsyms);
for (int i = 0; i < nsyms; ++i) inv_map[i] = nsyms - 1 - i;
for (const int cdf_stride : { nsyms, nsyms + 4 }) {
for (const int costs_stride : { nsyms, nsyms + 3 }) {
for (const int n : { 1, 7 }) {
const int cdf_elems = (n - 1) * cdf_stride + nsyms;
std::unique_ptr<aom_cdf_prob[]> cdf(new aom_cdf_prob[cdf_elems]);
for (int k = 0; k < n; ++k) {
aom_cdf_prob *const row = &cdf[k * cdf_stride];
if (k == 0) {
for (int i = 0; i < nsyms - 1; ++i) row[i] = 1;
row[nsyms - 1] = 0;
} else if (k == 1) {
for (int i = 0; i < nsyms - 1; ++i) row[i] = CDF_PROB_TOP - 1;
row[nsyms - 1] = 0;
} else {
BuildCdfN(&rnd, row, nsyms);
}
}
std::vector<int> out_buf(2 * kGuard + n * costs_stride, kCanary);
int *const out = out_buf.data() + kGuard;
std::fill(out, out + n * costs_stride, kSentinel);
std::vector<int> ref(n * costs_stride, kSentinel);
for (int k = 0; k < n; ++k) {
av1_cost_tokens_from_cdf(&ref[k * costs_stride],
&cdf[k * cdf_stride], inv_map.data());
}
API_REGISTER_STATE_CHECK(fn(out, costs_stride, cdf.get(), cdf_stride,
inv_map.data(), nsyms, n));
for (int g = 0; g < kGuard; ++g) {
ASSERT_EQ(kCanary, out_buf[g]) << "underflow guard g=" << g;
ASSERT_EQ(kCanary, out_buf[kGuard + n * costs_stride + g])
<< "overflow guard g=" << g;
}
for (int i = 0; i < n * costs_stride; ++i) {
ASSERT_EQ(ref[i], out[i])
<< "nsyms=" << nsyms << " cdf_stride=" << cdf_stride
<< " costs_stride=" << costs_stride << " idx=" << i;
}
}
}
}
}
}
void RunGeneralBatchSpeed(CdfGenBatchFn c_fn, CdfGenBatchFn simd_fn,
const char *label, int nsyms, int n, int cdf_stride,
int costs_stride) {
ACMRandom rnd(ACMRandom::DeterministicSeed());
std::vector<aom_cdf_prob> cdf(n * cdf_stride, 0);
for (int k = 0; k < n; ++k) BuildCdfN(&rnd, &cdf[k * cdf_stride], nsyms);
std::vector<int> out(n * costs_stride);
const CdfGenBatchFn funcs[2] = { c_fn, simd_fn };
const int kLoops = 500000;
double ns[2];
volatile int sink = 0;
for (int i = 0; i < 2; ++i) {
aom_usec_timer timer;
aom_usec_timer_start(&timer);
for (int j = 0; j < kLoops; ++j)
funcs[i](out.data(), costs_stride, cdf.data(), cdf_stride, nsyms, n);
aom_usec_timer_mark(&timer);
sink += out[0];
ns[i] =
1000.0 * static_cast<double>(aom_usec_timer_elapsed(&timer)) / kLoops;
}
(void)sink;
printf(
"cdf_batch %-11s nsyms=%2d n=%2d: c %7.1f ns tst %7.1f ns (%4.2fx)\n",
label, nsyms, n, ns[0], ns[1], ns[0] / ns[1]);
}
void RunInvBatchSpeed(CdfInvBatchFn c_fn, CdfInvBatchFn simd_fn,
const char *label, int set_type, int n) {
ACMRandom rnd(ACMRandom::DeterministicSeed());
const int nsyms = av1_num_ext_tx_set[set_type];
const int *const inv_map = av1_ext_tx_inv[set_type];
const int cdf_stride = CDF_SIZE(TX_TYPES);
std::vector<aom_cdf_prob> cdf(n * cdf_stride, 0);
for (int k = 0; k < n; ++k) BuildCdfN(&rnd, &cdf[k * cdf_stride], nsyms);
std::vector<int> out(n * TX_TYPES);
const CdfInvBatchFn funcs[2] = { c_fn, simd_fn };
const int kLoops = 500000;
double ns[2];
volatile int sink = 0;
for (int i = 0; i < 2; ++i) {
aom_usec_timer timer;
aom_usec_timer_start(&timer);
for (int j = 0; j < kLoops; ++j)
funcs[i](out.data(), TX_TYPES, cdf.data(), cdf_stride, inv_map, nsyms, n);
aom_usec_timer_mark(&timer);
sink += out[0];
ns[i] =
1000.0 * static_cast<double>(aom_usec_timer_elapsed(&timer)) / kLoops;
}
(void)sink;
printf(
"inv_batch %-11s nsyms=%2d n=%2d: c %7.1f ns tst %7.1f ns (%4.2fx)\n",
label, nsyms, n, ns[0], ns[1], ns[0] / ns[1]);
}
TEST_P(CostTokensFromCdfBatchTest, Batch4MatchesGeneric) {
CheckBatchAgainstGeneric(GET_PARAM(0));
}
TEST_P(CostTokensFromCdfBatchTest, Batch2MatchesGeneric) {
CheckBatch2AgainstGeneric(GET_PARAM(1));
}
TEST_P(CostTokensFromCdfBatchTest, BatchGeneralMatchesGeneric) {
CheckGeneralBatchAgainstGeneric(GET_PARAM(2));
}
TEST_P(CostTokensFromCdfBatchTest, BatchInvMatchesGeneric) {
CheckInvBatchAgainstGeneric(GET_PARAM(3));
}
TEST_P(CostTokensFromCdfBatchTest, DISABLED_Batch4Speed) {
RunBatchSpeed("cdf_4_batch", av1_cost_tokens_from_cdf_4_batch_c, GET_PARAM(0),
CDF_SIZE(4), 4, "base_cost", SIG_COEF_CONTEXTS, 8);
RunBatchSpeed("cdf_4_batch", av1_cost_tokens_from_cdf_4_batch_c, GET_PARAM(0),
CDF_SIZE(4), 4, "br_rate", LEVEL_CONTEXTS, BR_CDF_SIZE);
}
TEST_P(CostTokensFromCdfBatchTest, DISABLED_Batch2Speed) {
RunBatchSpeed("cdf_2_batch", av1_cost_tokens_from_cdf_2_batch_c, GET_PARAM(1),
CDF_SIZE(2), 2, "txb_skip", TXB_SKIP_CONTEXTS, 2);
RunBatchSpeed("cdf_2_batch", av1_cost_tokens_from_cdf_2_batch_c, GET_PARAM(1),
CDF_SIZE(2), 2, "eob_extra", EOB_COEF_CONTEXTS, 2);
}
TEST_P(CostTokensFromCdfBatchTest, DISABLED_BatchGeneralSpeed) {
RunGeneralBatchSpeed(av1_cost_tokens_from_cdf_batch_c, GET_PARAM(2),
"base_eob", 3, SIG_COEF_CONTEXTS_EOB, CDF_SIZE(3), 4);
RunGeneralBatchSpeed(av1_cost_tokens_from_cdf_batch_c, GET_PARAM(2),
"angle_d", 7, DIRECTIONAL_MODES, CDF_SIZE(7), 7);
RunGeneralBatchSpeed(av1_cost_tokens_from_cdf_batch_c, GET_PARAM(2),
"partition", EXT_PARTITION_TYPES, 12,
CDF_SIZE(EXT_PARTITION_TYPES), EXT_PARTITION_TYPES);
RunGeneralBatchSpeed(av1_cost_tokens_from_cdf_batch_c, GET_PARAM(2),
"intra_y", INTRA_MODES, KF_MODE_CONTEXTS,
CDF_SIZE(INTRA_MODES), INTRA_MODES);
RunGeneralBatchSpeed(av1_cost_tokens_from_cdf_batch_c, GET_PARAM(2),
"cfl_alpha", CFL_ALPHABET_SIZE, CFL_ALPHA_CONTEXTS,
CDF_SIZE(CFL_ALPHABET_SIZE), CFL_ALPHABET_SIZE);
// Shuffled mixed-shape schedule to exercise dispatch branch prediction.
ACMRandom rnd(ACMRandom::DeterministicSeed());
constexpr int kSchedule = 4096;
const int kShapes[] = { 2, 3, 4, 5, 6, 7, 8, 10, 13, 16 };
constexpr int kNumShapes = static_cast<int>(sizeof(kShapes) / sizeof(int));
std::vector<int> shape_idx(kSchedule);
for (int i = 0; i < kSchedule; ++i) {
shape_idx[i] = rnd.PseudoUniform(kNumShapes);
}
std::vector<std::vector<aom_cdf_prob>> cdfs(kNumShapes);
for (int si = 0; si < kNumShapes; ++si) {
cdfs[si].assign(16 * CDF_SIZE(16), 0);
for (int k = 0; k < 16; ++k) {
BuildCdfN(&rnd, &cdfs[si][k * CDF_SIZE(16)], kShapes[si]);
}
}
std::vector<int> out(16 * 16);
const CdfGenBatchFn funcs[2] = { av1_cost_tokens_from_cdf_batch_c,
GET_PARAM(2) };
double ns[2];
volatile int sink = 0;
for (int f = 0; f < 2; ++f) {
aom_usec_timer timer;
aom_usec_timer_start(&timer);
for (int rep = 0; rep < 200; ++rep) {
for (int i = 0; i < kSchedule; ++i) {
const int si = shape_idx[i];
funcs[f](out.data(), 16, cdfs[si].data(), CDF_SIZE(16), kShapes[si], 8);
}
}
aom_usec_timer_mark(&timer);
sink += out[0];
ns[f] = 1000.0 * static_cast<double>(aom_usec_timer_elapsed(&timer)) /
(200.0 * kSchedule);
}
(void)sink;
printf(
"cdf_batch mixed_4096 : c %7.1f ns tst %7.1f ns (%4.2fx)\n",
ns[0], ns[1], ns[0] / ns[1]);
}
TEST_P(CostTokensFromCdfBatchTest, DISABLED_BatchInvSpeed) {
RunInvBatchSpeed(av1_cost_tokens_from_cdf_inv_batch_c, GET_PARAM(3),
"dtt4_idtx", EXT_TX_SET_DTT4_IDTX, INTRA_MODES);
RunInvBatchSpeed(av1_cost_tokens_from_cdf_inv_batch_c, GET_PARAM(3),
"intra_dtt7", EXT_TX_SET_DTT4_IDTX_1DDCT, INTRA_MODES);
RunInvBatchSpeed(av1_cost_tokens_from_cdf_inv_batch_c, GET_PARAM(3),
"inter_dtt9", EXT_TX_SET_DTT9_IDTX_1DDCT, 1);
RunInvBatchSpeed(av1_cost_tokens_from_cdf_inv_batch_c, GET_PARAM(3),
"inter_all16", EXT_TX_SET_ALL16, 1);
}
INSTANTIATE_TEST_SUITE_P(C, CostTokensFromCdfBatchTest,
::testing::Values(CdfBatchTestParams(
av1_cost_tokens_from_cdf_4_batch_c,
av1_cost_tokens_from_cdf_2_batch_c,
av1_cost_tokens_from_cdf_batch_c,
av1_cost_tokens_from_cdf_inv_batch_c)));
#if HAVE_AVX2
INSTANTIATE_TEST_SUITE_P(AVX2, CostTokensFromCdfBatchTest,
::testing::Values(CdfBatchTestParams(
av1_cost_tokens_from_cdf_4_batch_avx2,
av1_cost_tokens_from_cdf_2_batch_avx2,
av1_cost_tokens_from_cdf_batch_avx2,
av1_cost_tokens_from_cdf_inv_batch_avx2)));
#endif // HAVE_AVX2
constexpr int kParityUseIntraExtTx[EXT_TX_SETS_INTRA][EXT_TX_SIZES] = {
{ 1, 1, 1, 1 },
{ 1, 1, 0, 0 },
{ 0, 0, 1, 0 },
};
constexpr int kParityUseInterExtTx[EXT_TX_SETS_INTER][EXT_TX_SIZES] = {
{ 1, 1, 1, 1 },
{ 1, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 1, 1, 1 },
};
constexpr int kParityExtTxSetIdxToType[2][AOMMAX(EXT_TX_SETS_INTRA,
EXT_TX_SETS_INTER)] = {
{ EXT_TX_SET_DCTONLY, EXT_TX_SET_DTT4_IDTX_1DDCT, EXT_TX_SET_DTT4_IDTX },
{ EXT_TX_SET_DCTONLY, EXT_TX_SET_ALL16, EXT_TX_SET_DTT9_IDTX_1DDCT,
EXT_TX_SET_DCT_IDTX },
};
void PopulateRandomFrameContext(ACMRandom *rnd, FRAME_CONTEXT *fc) {
std::memset(fc, 0, sizeof(*fc));
for (int i = 0; i < PARTITION_CONTEXTS; ++i) {
const int nsyms = (i < PARTITION_PLOFFSET) ? PARTITION_TYPES
: (i < 4 * PARTITION_PLOFFSET) ? EXT_PARTITION_TYPES
: EXT_PARTITION_TYPES - 2;
BuildCdfN(rnd, fc->partition_cdf[i], nsyms);
}
for (int i = 0; i < SKIP_MODE_CONTEXTS; ++i)
BuildCdfN(rnd, fc->skip_mode_cdfs[i], 2);
for (int i = 0; i < SKIP_CONTEXTS; ++i)
BuildCdfN(rnd, fc->skip_txfm_cdfs[i], 2);
for (int i = 0; i < KF_MODE_CONTEXTS; ++i)
for (int j = 0; j < KF_MODE_CONTEXTS; ++j)
BuildCdfN(rnd, fc->kf_y_cdf[i][j], INTRA_MODES);
for (int i = 0; i < BLOCK_SIZE_GROUPS; ++i)
BuildCdfN(rnd, fc->y_mode_cdf[i], INTRA_MODES);
for (int cfl = 0; cfl < CFL_ALLOWED_TYPES; ++cfl) {
const int nsyms =
(cfl == CFL_DISALLOWED) ? UV_INTRA_MODES - 1 : UV_INTRA_MODES;
for (int j = 0; j < INTRA_MODES; ++j)
BuildCdfN(rnd, fc->uv_mode_cdf[cfl][j], nsyms);
}
BuildCdfN(rnd, fc->filter_intra_mode_cdf, FILTER_INTRA_MODES);
for (int i = 0; i < BLOCK_SIZES_ALL; ++i)
BuildCdfN(rnd, fc->filter_intra_cdfs[i], 2);
for (int i = 0; i < PALATTE_BSIZE_CTXS; ++i) {
BuildCdfN(rnd, fc->palette_y_size_cdf[i], PALETTE_SIZES);
BuildCdfN(rnd, fc->palette_uv_size_cdf[i], PALETTE_SIZES);
for (int j = 0; j < PALETTE_Y_MODE_CONTEXTS; ++j)
BuildCdfN(rnd, fc->palette_y_mode_cdf[i][j], 2);
}
for (int i = 0; i < PALETTE_UV_MODE_CONTEXTS; ++i)
BuildCdfN(rnd, fc->palette_uv_mode_cdf[i], 2);
for (int i = 0; i < PALETTE_SIZES; ++i) {
const int nsyms = i + PALETTE_MIN_SIZE;
for (int j = 0; j < PALETTE_COLOR_INDEX_CONTEXTS; ++j) {
BuildCdfN(rnd, fc->palette_y_color_index_cdf[i][j], nsyms);
BuildCdfN(rnd, fc->palette_uv_color_index_cdf[i][j], nsyms);
}
}
BuildCdfN(rnd, fc->cfl_sign_cdf, CFL_JOINT_SIGNS);
for (int i = 0; i < CFL_ALPHA_CONTEXTS; ++i)
BuildCdfN(rnd, fc->cfl_alpha_cdf[i], CFL_ALPHABET_SIZE);
for (int i = 0; i < MAX_TX_CATS; ++i) {
const int nsyms = (i == 0) ? 2 : MAX_TX_DEPTH + 1;
for (int j = 0; j < TX_SIZE_CONTEXTS; ++j)
BuildCdfN(rnd, fc->tx_size_cdf[i][j], nsyms);
}
for (int i = 0; i < TXFM_PARTITION_CONTEXTS; ++i)
BuildCdfN(rnd, fc->txfm_partition_cdf[i], 2);
BuildCdfN(rnd, fc->intrabc_cdf, 2);
for (int i = TX_4X4; i < EXT_TX_SIZES; ++i) {
for (int s = 1; s < EXT_TX_SETS_INTRA; ++s) {
if (kParityUseIntraExtTx[s][i]) {
const int set_type = kParityExtTxSetIdxToType[0][s];
const int nsyms = av1_num_ext_tx_set[set_type];
for (int j = 0; j < INTRA_MODES; ++j)
BuildCdfN(rnd, fc->intra_ext_tx_cdf[s][i][j], nsyms);
}
}
for (int s = 1; s < EXT_TX_SETS_INTER; ++s) {
if (kParityUseInterExtTx[s][i]) {
const int set_type = kParityExtTxSetIdxToType[1][s];
const int nsyms = av1_num_ext_tx_set[set_type];
BuildCdfN(rnd, fc->inter_ext_tx_cdf[s][i], nsyms);
}
}
}
for (int i = 0; i < DIRECTIONAL_MODES; ++i)
BuildCdfN(rnd, fc->angle_delta_cdf[i], 2 * MAX_ANGLE_DELTA + 1);
for (int i = 0; i < SPATIAL_PREDICTION_PROBS; ++i)
BuildCdfN(rnd, fc->seg.spatial_pred_seg_cdf[i], MAX_SEGMENTS);
for (int i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
BuildCdfN(rnd, fc->switchable_interp_cdf[i], SWITCHABLE_FILTERS);
for (int i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i)
BuildCdfN(rnd, fc->seg.pred_cdf[i], 2);
for (int i = 0; i < COMP_INTER_CONTEXTS; ++i)
BuildCdfN(rnd, fc->comp_inter_cdf[i], 2);
for (int i = 0; i < REF_CONTEXTS; ++i) {
for (int j = 0; j < SINGLE_REFS - 1; ++j)
BuildCdfN(rnd, fc->single_ref_cdf[i][j], 2);
for (int j = 0; j < FWD_REFS - 1; ++j)
BuildCdfN(rnd, fc->comp_ref_cdf[i][j], 2);
for (int j = 0; j < BWD_REFS - 1; ++j)
BuildCdfN(rnd, fc->comp_bwdref_cdf[i][j], 2);
}
for (int i = 0; i < COMP_REF_TYPE_CONTEXTS; ++i)
BuildCdfN(rnd, fc->comp_ref_type_cdf[i], COMP_REFERENCE_TYPES);
for (int i = 0; i < UNI_COMP_REF_CONTEXTS; ++i)
for (int j = 0; j < UNIDIR_COMP_REFS - 1; ++j)
BuildCdfN(rnd, fc->uni_comp_ref_cdf[i][j], 2);
for (int i = 0; i < INTRA_INTER_CONTEXTS; ++i)
BuildCdfN(rnd, fc->intra_inter_cdf[i], 2);
for (int i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
BuildCdfN(rnd, fc->newmv_cdf[i], 2);
for (int i = 0; i < GLOBALMV_MODE_CONTEXTS; ++i)
BuildCdfN(rnd, fc->zeromv_cdf[i], 2);
for (int i = 0; i < REFMV_MODE_CONTEXTS; ++i)
BuildCdfN(rnd, fc->refmv_cdf[i], 2);
for (int i = 0; i < DRL_MODE_CONTEXTS; ++i) BuildCdfN(rnd, fc->drl_cdf[i], 2);
for (int i = 0; i < INTER_MODE_CONTEXTS; ++i)
BuildCdfN(rnd, fc->inter_compound_mode_cdf[i], INTER_COMPOUND_MODES);
for (int i = 0; i < BLOCK_SIZES_ALL; ++i) {
BuildCdfN(rnd, fc->compound_type_cdf[i], MASKED_COMPOUND_TYPES);
BuildCdfN(rnd, fc->wedge_idx_cdf[i], 16);
BuildCdfN(rnd, fc->wedge_interintra_cdf[i], 2);
BuildCdfN(rnd, fc->motion_mode_cdf[i], MOTION_MODES);
BuildCdfN(rnd, fc->obmc_cdf[i], 2);
}
for (int i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
BuildCdfN(rnd, fc->interintra_cdf[i], 2);
BuildCdfN(rnd, fc->interintra_mode_cdf[i], INTERINTRA_MODES);
}
for (int i = 0; i < COMP_INDEX_CONTEXTS; ++i)
BuildCdfN(rnd, fc->compound_index_cdf[i], 2);
for (int i = 0; i < COMP_GROUP_IDX_CONTEXTS; ++i)
BuildCdfN(rnd, fc->comp_group_idx_cdf[i], 2);
for (int p = 0; p < PLANE_TYPES; ++p) {
for (int c = 0; c < 2; ++c) {
BuildCdfN(rnd, fc->eob_flag_cdf16[p][c], 5);
BuildCdfN(rnd, fc->eob_flag_cdf32[p][c], 6);
BuildCdfN(rnd, fc->eob_flag_cdf64[p][c], 7);
BuildCdfN(rnd, fc->eob_flag_cdf128[p][c], 8);
BuildCdfN(rnd, fc->eob_flag_cdf256[p][c], 9);
BuildCdfN(rnd, fc->eob_flag_cdf512[p][c], 10);
BuildCdfN(rnd, fc->eob_flag_cdf1024[p][c], 11);
}
for (int c = 0; c < DC_SIGN_CONTEXTS; ++c)
BuildCdfN(rnd, fc->dc_sign_cdf[p][c], 2);
}
for (int t = 0; t < TX_SIZES; ++t) {
for (int c = 0; c < TXB_SKIP_CONTEXTS; ++c)
BuildCdfN(rnd, fc->txb_skip_cdf[t][c], 2);
for (int p = 0; p < PLANE_TYPES; ++p) {
for (int c = 0; c < SIG_COEF_CONTEXTS_EOB; ++c)
BuildCdfN(rnd, fc->coeff_base_eob_cdf[t][p][c], 3);
for (int c = 0; c < SIG_COEF_CONTEXTS; ++c)
BuildCdfN(rnd, fc->coeff_base_cdf[t][p][c], 4);
for (int c = 0; c < EOB_COEF_CONTEXTS; ++c)
BuildCdfN(rnd, fc->eob_extra_cdf[t][p][c], 2);
for (int c = 0; c < LEVEL_CONTEXTS; ++c)
BuildCdfN(rnd, fc->coeff_br_cdf[t][p][c], BR_CDF_SIZE);
}
}
}
void FillInterTxTypeCostsScalar(ModeCosts *mode_costs,
const FRAME_CONTEXT *fc) {
for (int i = TX_4X4; i < EXT_TX_SIZES; ++i) {
for (int s = 1; s < EXT_TX_SETS_INTER; ++s) {
if (kParityUseInterExtTx[s][i]) {
av1_cost_tokens_from_cdf(
mode_costs->inter_tx_type_costs[s][i], fc->inter_ext_tx_cdf[s][i],
av1_ext_tx_inv[kParityExtTxSetIdxToType[1][s]]);
}
}
}
}
void FillModeRatesScalar(const AV1_COMMON *cm, ModeCosts *mode_costs,
const FRAME_CONTEXT *fc) {
const int is_intra_only = frame_is_intra_only(cm);
for (int i = 0; i < PARTITION_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->partition_cost[i],
fc->partition_cdf[i], nullptr);
if (cm->current_frame.skip_mode_info.skip_mode_flag) {
for (int i = 0; i < SKIP_MODE_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->skip_mode_cost[i],
fc->skip_mode_cdfs[i], nullptr);
} else {
std::memset(mode_costs->skip_mode_cost, 0,
sizeof(mode_costs->skip_mode_cost));
}
for (int i = 0; i < SKIP_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->skip_txfm_cost[i],
fc->skip_txfm_cdfs[i], nullptr);
for (int i = 0; i < KF_MODE_CONTEXTS; ++i)
for (int j = 0; j < KF_MODE_CONTEXTS; ++j)
av1_cost_tokens_from_cdf(mode_costs->y_mode_costs[i][j],
fc->kf_y_cdf[i][j], nullptr);
for (int i = 0; i < BLOCK_SIZE_GROUPS; ++i)
av1_cost_tokens_from_cdf(mode_costs->mbmode_cost[i], fc->y_mode_cdf[i],
nullptr);
for (int i = 0; i < CFL_ALLOWED_TYPES; ++i)
for (int j = 0; j < INTRA_MODES; ++j)
av1_cost_tokens_from_cdf(mode_costs->intra_uv_mode_cost[i][j],
fc->uv_mode_cdf[i][j], nullptr);
av1_cost_tokens_from_cdf(mode_costs->filter_intra_mode_cost,
fc->filter_intra_mode_cdf, nullptr);
for (int i = 0; i < BLOCK_SIZES_ALL; ++i) {
if (av1_filter_intra_allowed_bsize(cm, static_cast<BLOCK_SIZE>(i)))
av1_cost_tokens_from_cdf(mode_costs->filter_intra_cost[i],
fc->filter_intra_cdfs[i], nullptr);
}
for (int i = 0; i < PALATTE_BSIZE_CTXS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->palette_y_size_cost[i],
fc->palette_y_size_cdf[i], nullptr);
av1_cost_tokens_from_cdf(mode_costs->palette_uv_size_cost[i],
fc->palette_uv_size_cdf[i], nullptr);
for (int j = 0; j < PALETTE_Y_MODE_CONTEXTS; ++j)
av1_cost_tokens_from_cdf(mode_costs->palette_y_mode_cost[i][j],
fc->palette_y_mode_cdf[i][j], nullptr);
}
for (int i = 0; i < PALETTE_UV_MODE_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->palette_uv_mode_cost[i],
fc->palette_uv_mode_cdf[i], nullptr);
for (int i = 0; i < PALETTE_SIZES; ++i) {
for (int j = 0; j < PALETTE_COLOR_INDEX_CONTEXTS; ++j) {
av1_cost_tokens_from_cdf(mode_costs->palette_y_color_cost[i][j],
fc->palette_y_color_index_cdf[i][j], nullptr);
av1_cost_tokens_from_cdf(mode_costs->palette_uv_color_cost[i][j],
fc->palette_uv_color_index_cdf[i][j], nullptr);
}
}
int sign_cost[CFL_JOINT_SIGNS];
av1_cost_tokens_from_cdf(sign_cost, fc->cfl_sign_cdf, nullptr);
for (int joint_sign = 0; joint_sign < CFL_JOINT_SIGNS; ++joint_sign) {
int *cost_u = mode_costs->cfl_cost[joint_sign][CFL_PRED_U];
int *cost_v = mode_costs->cfl_cost[joint_sign][CFL_PRED_V];
if (CFL_SIGN_U(joint_sign) == CFL_SIGN_ZERO) {
std::memset(cost_u, 0, CFL_ALPHABET_SIZE * sizeof(*cost_u));
} else {
av1_cost_tokens_from_cdf(
cost_u, fc->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)], nullptr);
}
if (CFL_SIGN_V(joint_sign) == CFL_SIGN_ZERO) {
std::memset(cost_v, 0, CFL_ALPHABET_SIZE * sizeof(*cost_v));
} else {
av1_cost_tokens_from_cdf(
cost_v, fc->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)], nullptr);
}
for (int u = 0; u < CFL_ALPHABET_SIZE; ++u)
cost_u[u] += sign_cost[joint_sign];
}
for (int i = 0; i < MAX_TX_CATS; ++i)
for (int j = 0; j < TX_SIZE_CONTEXTS; ++j)
av1_cost_tokens_from_cdf(mode_costs->tx_size_cost[i][j],
fc->tx_size_cdf[i][j], nullptr);
for (int i = 0; i < TXFM_PARTITION_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->txfm_partition_cost[i],
fc->txfm_partition_cdf[i], nullptr);
const int allow_intrabc = av1_allow_intrabc(cm);
if (allow_intrabc) {
FillInterTxTypeCostsScalar(mode_costs, fc);
av1_cost_tokens_from_cdf(mode_costs->intrabc_cost, fc->intrabc_cdf,
nullptr);
}
for (int i = TX_4X4; i < EXT_TX_SIZES; ++i) {
for (int s = 1; s < EXT_TX_SETS_INTRA; ++s) {
if (kParityUseIntraExtTx[s][i]) {
for (int j = 0; j < INTRA_MODES; ++j) {
av1_cost_tokens_from_cdf(
mode_costs->intra_tx_type_costs[s][i][j],
fc->intra_ext_tx_cdf[s][i][j],
av1_ext_tx_inv[kParityExtTxSetIdxToType[0][s]]);
}
}
}
}
for (int i = 0; i < DIRECTIONAL_MODES; ++i)
av1_cost_tokens_from_cdf(mode_costs->angle_delta_cost[i],
fc->angle_delta_cdf[i], nullptr);
for (int i = 0; i < SPATIAL_PREDICTION_PROBS; ++i)
av1_cost_tokens_from_cdf(mode_costs->spatial_pred_cost[i],
fc->seg.spatial_pred_seg_cdf[i], nullptr);
if (is_intra_only) {
mode_costs->intra_only_skipped_inter_costs = 1;
mode_costs->intra_only_skipped_inter_tx_type =
static_cast<uint8_t>(!allow_intrabc);
} else {
mode_costs->intra_only_skipped_inter_costs = 0;
mode_costs->intra_only_skipped_inter_tx_type = 0;
for (int i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->switchable_interp_costs[i],
fc->switchable_interp_cdf[i], nullptr);
FillInterTxTypeCostsScalar(mode_costs, fc);
for (int i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i)
av1_cost_tokens_from_cdf(mode_costs->tmp_pred_cost[i],
fc->seg.pred_cdf[i], nullptr);
for (int i = 0; i < COMP_INTER_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->comp_inter_cost[i],
fc->comp_inter_cdf[i], nullptr);
for (int i = 0; i < REF_CONTEXTS; ++i)
for (int j = 0; j < SINGLE_REFS - 1; ++j)
av1_cost_tokens_from_cdf(mode_costs->single_ref_cost[i][j],
fc->single_ref_cdf[i][j], nullptr);
for (int i = 0; i < COMP_REF_TYPE_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->comp_ref_type_cost[i],
fc->comp_ref_type_cdf[i], nullptr);
for (int i = 0; i < UNI_COMP_REF_CONTEXTS; ++i)
for (int j = 0; j < UNIDIR_COMP_REFS - 1; ++j)
av1_cost_tokens_from_cdf(mode_costs->uni_comp_ref_cost[i][j],
fc->uni_comp_ref_cdf[i][j], nullptr);
for (int i = 0; i < REF_CONTEXTS; ++i)
for (int j = 0; j < FWD_REFS - 1; ++j)
av1_cost_tokens_from_cdf(mode_costs->comp_ref_cost[i][j],
fc->comp_ref_cdf[i][j], nullptr);
for (int i = 0; i < REF_CONTEXTS; ++i)
for (int j = 0; j < BWD_REFS - 1; ++j)
av1_cost_tokens_from_cdf(mode_costs->comp_bwdref_cost[i][j],
fc->comp_bwdref_cdf[i][j], nullptr);
for (int i = 0; i < INTRA_INTER_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->intra_inter_cost[i],
fc->intra_inter_cdf[i], nullptr);
for (int i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->newmv_mode_cost[i], fc->newmv_cdf[i],
nullptr);
for (int i = 0; i < GLOBALMV_MODE_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->zeromv_mode_cost[i],
fc->zeromv_cdf[i], nullptr);
for (int i = 0; i < REFMV_MODE_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->refmv_mode_cost[i], fc->refmv_cdf[i],
nullptr);
for (int i = 0; i < DRL_MODE_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->drl_mode_cost0[i], fc->drl_cdf[i],
nullptr);
for (int i = 0; i < INTER_MODE_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->inter_compound_mode_cost[i],
fc->inter_compound_mode_cdf[i], nullptr);
for (int i = 0; i < BLOCK_SIZES_ALL; ++i)
av1_cost_tokens_from_cdf(mode_costs->compound_type_cost[i],
fc->compound_type_cdf[i], nullptr);
for (int i = 0; i < BLOCK_SIZES_ALL; ++i) {
if (av1_is_wedge_used(static_cast<BLOCK_SIZE>(i)))
av1_cost_tokens_from_cdf(mode_costs->wedge_idx_cost[i],
fc->wedge_idx_cdf[i], nullptr);
}
for (int i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->interintra_cost[i],
fc->interintra_cdf[i], nullptr);
av1_cost_tokens_from_cdf(mode_costs->interintra_mode_cost[i],
fc->interintra_mode_cdf[i], nullptr);
}
for (int i = 0; i < BLOCK_SIZES_ALL; ++i)
av1_cost_tokens_from_cdf(mode_costs->wedge_interintra_cost[i],
fc->wedge_interintra_cdf[i], nullptr);
for (int i = BLOCK_8X8; i < BLOCK_SIZES_ALL; ++i)
av1_cost_tokens_from_cdf(mode_costs->motion_mode_cost[i],
fc->motion_mode_cdf[i], nullptr);
for (int i = BLOCK_8X8; i < BLOCK_SIZES_ALL; ++i)
av1_cost_tokens_from_cdf(mode_costs->motion_mode_cost1[i],
fc->obmc_cdf[i], nullptr);
for (int i = 0; i < COMP_INDEX_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->comp_idx_cost[i],
fc->compound_index_cdf[i], nullptr);
for (int i = 0; i < COMP_GROUP_IDX_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->comp_group_idx_cost[i],
fc->comp_group_idx_cdf[i], nullptr);
}
}
void FillCoeffCostsScalar(CoeffCosts *coeff_costs, const FRAME_CONTEXT *fc,
int num_planes) {
const int nplanes = AOMMIN(num_planes, PLANE_TYPES);
for (int eob_multi_size = 0; eob_multi_size < 7; ++eob_multi_size) {
for (int plane = 0; plane < nplanes; ++plane) {
LV_MAP_EOB_COST *pcost = &coeff_costs->eob_costs[eob_multi_size][plane];
for (int ctx = 0; ctx < 2; ++ctx) {
const aom_cdf_prob *pcdf = nullptr;
switch (eob_multi_size) {
case 0: pcdf = fc->eob_flag_cdf16[plane][ctx]; break;
case 1: pcdf = fc->eob_flag_cdf32[plane][ctx]; break;
case 2: pcdf = fc->eob_flag_cdf64[plane][ctx]; break;
case 3: pcdf = fc->eob_flag_cdf128[plane][ctx]; break;
case 4: pcdf = fc->eob_flag_cdf256[plane][ctx]; break;
case 5: pcdf = fc->eob_flag_cdf512[plane][ctx]; break;
case 6:
default: pcdf = fc->eob_flag_cdf1024[plane][ctx]; break;
}
av1_cost_tokens_from_cdf(pcost->eob_cost[ctx], pcdf, nullptr);
}
}
}
for (int tx_size = 0; tx_size < TX_SIZES; ++tx_size) {
for (int plane = 0; plane < nplanes; ++plane) {
LV_MAP_COEFF_COST *pcost = &coeff_costs->coeff_costs[tx_size][plane];
for (int ctx = 0; ctx < TXB_SKIP_CONTEXTS; ++ctx)
av1_cost_tokens_from_cdf(pcost->txb_skip_cost[ctx],
fc->txb_skip_cdf[tx_size][ctx], nullptr);
for (int ctx = 0; ctx < SIG_COEF_CONTEXTS_EOB; ++ctx)
av1_cost_tokens_from_cdf(pcost->base_eob_cost[ctx],
fc->coeff_base_eob_cdf[tx_size][plane][ctx],
nullptr);
for (int ctx = 0; ctx < SIG_COEF_CONTEXTS; ++ctx) {
av1_cost_tokens_from_cdf(pcost->base_cost[ctx],
fc->coeff_base_cdf[tx_size][plane][ctx],
nullptr);
pcost->base_cost[ctx][4] = pcost->base_cost[ctx][0];
pcost->base_cost[ctx][5] =
pcost->base_cost[ctx][1] + av1_cost_literal(1);
pcost->base_cost[ctx][6] =
pcost->base_cost[ctx][2] + av1_cost_literal(1);
pcost->base_cost[ctx][7] =
pcost->base_cost[ctx][3] + av1_cost_literal(1);
}
for (int ctx = 0; ctx < EOB_COEF_CONTEXTS; ++ctx)
av1_cost_tokens_from_cdf(pcost->eob_extra_cost[ctx],
fc->eob_extra_cdf[tx_size][plane][ctx],
nullptr);
for (int ctx = 0; ctx < DC_SIGN_CONTEXTS; ++ctx)
av1_cost_tokens_from_cdf(pcost->dc_sign_cost[ctx],
fc->dc_sign_cdf[plane][ctx], nullptr);
if (tx_size <= TX_32X32) {
for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
int br_rate[BR_CDF_SIZE];
int prev_cost = 0;
int i;
av1_cost_tokens_from_cdf(
br_rate, fc->coeff_br_cdf[tx_size][plane][ctx], nullptr);
for (i = 0; i < COEFF_BASE_RANGE; i += BR_CDF_SIZE - 1) {
for (int j = 0; j < BR_CDF_SIZE - 1; ++j)
pcost->lps_cost[ctx][i + j] = prev_cost + br_rate[j];
prev_cost += br_rate[BR_CDF_SIZE - 1];
}
pcost->lps_cost[ctx][i] = prev_cost;
}
} else {
std::memcpy(pcost->lps_cost,
coeff_costs->coeff_costs[TX_32X32][plane].lps_cost,
sizeof(pcost->lps_cost));
}
}
}
}
template <size_t N>
void ExpectIntsEqual(const char *name, const int (&ref)[N],
const int (&tst)[N]) {
for (size_t i = 0; i < N; ++i)
ASSERT_EQ(ref[i], tst[i]) << name << "[" << i << "]";
}
template <size_t M, size_t N>
void ExpectIntsEqual(const char *name, const int (&ref)[M][N],
const int (&tst)[M][N]) {
for (size_t i = 0; i < M; ++i)
for (size_t j = 0; j < N; ++j)
ASSERT_EQ(ref[i][j], tst[i][j]) << name << "[" << i << "][" << j << "]";
}
template <size_t L, size_t M, size_t N>
void ExpectIntsEqual(const char *name, const int (&ref)[L][M][N],
const int (&tst)[L][M][N]) {
for (size_t i = 0; i < L; ++i)
for (size_t j = 0; j < M; ++j)
for (size_t k = 0; k < N; ++k)
ASSERT_EQ(ref[i][j][k], tst[i][j][k])
<< name << "[" << i << "][" << j << "][" << k << "]";
}
void ExpectModeCostsEqual(const ModeCosts &ref, const ModeCosts &tst) {
ExpectIntsEqual("partition_cost", ref.partition_cost, tst.partition_cost);
ExpectIntsEqual("mbmode_cost", ref.mbmode_cost, tst.mbmode_cost);
ExpectIntsEqual("y_mode_costs", ref.y_mode_costs, tst.y_mode_costs);
ExpectIntsEqual("intra_uv_mode_cost", ref.intra_uv_mode_cost,
tst.intra_uv_mode_cost);
ExpectIntsEqual("filter_intra_cost", ref.filter_intra_cost,
tst.filter_intra_cost);
ExpectIntsEqual("filter_intra_mode_cost", ref.filter_intra_mode_cost,
tst.filter_intra_mode_cost);
ExpectIntsEqual("angle_delta_cost", ref.angle_delta_cost,
tst.angle_delta_cost);
ExpectIntsEqual("cfl_cost", ref.cfl_cost, tst.cfl_cost);
ExpectIntsEqual("intrabc_cost", ref.intrabc_cost, tst.intrabc_cost);
ExpectIntsEqual("palette_y_size_cost", ref.palette_y_size_cost,
tst.palette_y_size_cost);
ExpectIntsEqual("palette_uv_size_cost", ref.palette_uv_size_cost,
tst.palette_uv_size_cost);
ExpectIntsEqual("palette_y_color_cost", ref.palette_y_color_cost,
tst.palette_y_color_cost);
ExpectIntsEqual("palette_uv_color_cost", ref.palette_uv_color_cost,
tst.palette_uv_color_cost);
ExpectIntsEqual("palette_y_mode_cost", ref.palette_y_mode_cost,
tst.palette_y_mode_cost);
ExpectIntsEqual("palette_uv_mode_cost", ref.palette_uv_mode_cost,
tst.palette_uv_mode_cost);
ExpectIntsEqual("skip_mode_cost", ref.skip_mode_cost, tst.skip_mode_cost);
ExpectIntsEqual("newmv_mode_cost", ref.newmv_mode_cost, tst.newmv_mode_cost);
ExpectIntsEqual("zeromv_mode_cost", ref.zeromv_mode_cost,
tst.zeromv_mode_cost);
ExpectIntsEqual("refmv_mode_cost", ref.refmv_mode_cost, tst.refmv_mode_cost);
ExpectIntsEqual("drl_mode_cost0", ref.drl_mode_cost0, tst.drl_mode_cost0);
ExpectIntsEqual("single_ref_cost", ref.single_ref_cost, tst.single_ref_cost);
ExpectIntsEqual("comp_inter_cost", ref.comp_inter_cost, tst.comp_inter_cost);
ExpectIntsEqual("comp_ref_type_cost", ref.comp_ref_type_cost,
tst.comp_ref_type_cost);
ExpectIntsEqual("uni_comp_ref_cost", ref.uni_comp_ref_cost,
tst.uni_comp_ref_cost);
ExpectIntsEqual("comp_ref_cost", ref.comp_ref_cost, tst.comp_ref_cost);
ExpectIntsEqual("comp_bwdref_cost", ref.comp_bwdref_cost,
tst.comp_bwdref_cost);
ExpectIntsEqual("intra_inter_cost", ref.intra_inter_cost,
tst.intra_inter_cost);
ExpectIntsEqual("inter_compound_mode_cost", ref.inter_compound_mode_cost,
tst.inter_compound_mode_cost);
ExpectIntsEqual("compound_type_cost", ref.compound_type_cost,
tst.compound_type_cost);
ExpectIntsEqual("wedge_idx_cost", ref.wedge_idx_cost, tst.wedge_idx_cost);
ExpectIntsEqual("interintra_cost", ref.interintra_cost, tst.interintra_cost);
ExpectIntsEqual("wedge_interintra_cost", ref.wedge_interintra_cost,
tst.wedge_interintra_cost);
ExpectIntsEqual("interintra_mode_cost", ref.interintra_mode_cost,
tst.interintra_mode_cost);
ExpectIntsEqual("comp_idx_cost", ref.comp_idx_cost, tst.comp_idx_cost);
ExpectIntsEqual("comp_group_idx_cost", ref.comp_group_idx_cost,
tst.comp_group_idx_cost);
ExpectIntsEqual("motion_mode_cost", ref.motion_mode_cost,
tst.motion_mode_cost);
ExpectIntsEqual("motion_mode_cost1", ref.motion_mode_cost1,
tst.motion_mode_cost1);
ExpectIntsEqual("switchable_interp_costs", ref.switchable_interp_costs,
tst.switchable_interp_costs);
ExpectIntsEqual("skip_txfm_cost", ref.skip_txfm_cost, tst.skip_txfm_cost);
ExpectIntsEqual("tx_size_cost", ref.tx_size_cost, tst.tx_size_cost);
ExpectIntsEqual("txfm_partition_cost", ref.txfm_partition_cost,
tst.txfm_partition_cost);
ExpectIntsEqual("inter_tx_type_costs", ref.inter_tx_type_costs,
tst.inter_tx_type_costs);
for (int s = 0; s < EXT_TX_SETS_INTRA; ++s) {
ExpectIntsEqual("intra_tx_type_costs", ref.intra_tx_type_costs[s],
tst.intra_tx_type_costs[s]);
}
ExpectIntsEqual("switchable_restore_cost", ref.switchable_restore_cost,
tst.switchable_restore_cost);
ExpectIntsEqual("wiener_restore_cost", ref.wiener_restore_cost,
tst.wiener_restore_cost);
ExpectIntsEqual("sgrproj_restore_cost", ref.sgrproj_restore_cost,
tst.sgrproj_restore_cost);
ExpectIntsEqual("tmp_pred_cost", ref.tmp_pred_cost, tst.tmp_pred_cost);
ExpectIntsEqual("spatial_pred_cost", ref.spatial_pred_cost,
tst.spatial_pred_cost);
ASSERT_EQ(ref.intra_only_skipped_inter_costs,
tst.intra_only_skipped_inter_costs);
ASSERT_EQ(ref.intra_only_skipped_inter_tx_type,
tst.intra_only_skipped_inter_tx_type);
}
void ExpectCoeffCostsEqual(const CoeffCosts &ref, const CoeffCosts &tst) {
for (int i = 0; i < 7; ++i)
for (int p = 0; p < PLANE_TYPES; ++p)
ExpectIntsEqual("eob_cost", ref.eob_costs[i][p].eob_cost,
tst.eob_costs[i][p].eob_cost);
for (int t = 0; t < TX_SIZES; ++t) {
for (int p = 0; p < PLANE_TYPES; ++p) {
const LV_MAP_COEFF_COST &r = ref.coeff_costs[t][p];
const LV_MAP_COEFF_COST &c = tst.coeff_costs[t][p];
ExpectIntsEqual("txb_skip_cost", r.txb_skip_cost, c.txb_skip_cost);
ExpectIntsEqual("base_eob_cost", r.base_eob_cost, c.base_eob_cost);
ExpectIntsEqual("base_cost", r.base_cost, c.base_cost);
ExpectIntsEqual("eob_extra_cost", r.eob_extra_cost, c.eob_extra_cost);
ExpectIntsEqual("dc_sign_cost", r.dc_sign_cost, c.dc_sign_cost);
ExpectIntsEqual("lps_cost", r.lps_cost, c.lps_cost);
}
}
}
TEST_F(CostTokensFromCdf4Test, FillModeAndCoeffCostsParity) {
ACMRandom rnd(ACMRandom::DeterministicSeed());
auto cm = std::make_unique<AV1_COMMON>();
auto seq_params = std::make_unique<SequenceHeader>();
auto fc = std::make_unique<FRAME_CONTEXT>();
auto ref_mode_costs = std::make_unique<ModeCosts>();
auto tst_mode_costs = std::make_unique<ModeCosts>();
auto ref_coeff_costs = std::make_unique<CoeffCosts>();
auto tst_coeff_costs = std::make_unique<CoeffCosts>();
for (int use_default : { 1, 0 }) {
std::memset(cm.get(), 0, sizeof(*cm));
std::memset(seq_params.get(), 0, sizeof(*seq_params));
cm->seq_params = seq_params.get();
cm->fc = fc.get();
if (use_default) {
std::memset(fc.get(), 0, sizeof(*fc));
cm->quant_params.base_qindex = 100;
av1_default_coef_probs(cm.get());
av1_init_mode_probs(fc.get());
} else {
PopulateRandomFrameContext(&rnd, fc.get());
}
// Inter frame with skip_mode and filter_intra enabled.
cm->current_frame.frame_type = INTER_FRAME;
cm->current_frame.skip_mode_info.skip_mode_flag = 1;
seq_params->enable_filter_intra = 1;
cm->features.allow_screen_content_tools = 0;
cm->features.allow_intrabc = 0;
std::memset(ref_mode_costs.get(), 0x5A, sizeof(*ref_mode_costs));
std::memset(tst_mode_costs.get(), 0x5A, sizeof(*tst_mode_costs));
FillModeRatesScalar(cm.get(), ref_mode_costs.get(), fc.get());
API_REGISTER_STATE_CHECK(
av1_fill_mode_rates(cm.get(), tst_mode_costs.get(), fc.get()));
ExpectModeCostsEqual(*ref_mode_costs, *tst_mode_costs);
// Keyframe with intrabc enabled and filter_intra/skip_mode disabled.
cm->current_frame.frame_type = KEY_FRAME;
cm->current_frame.skip_mode_info.skip_mode_flag = 0;
seq_params->enable_filter_intra = 0;
cm->features.allow_screen_content_tools = 1;
cm->features.allow_intrabc = 1;
std::memset(ref_mode_costs.get(), 0x5A, sizeof(*ref_mode_costs));
std::memset(tst_mode_costs.get(), 0x5A, sizeof(*tst_mode_costs));
FillModeRatesScalar(cm.get(), ref_mode_costs.get(), fc.get());
API_REGISTER_STATE_CHECK(
av1_fill_mode_rates(cm.get(), tst_mode_costs.get(), fc.get()));
ExpectModeCostsEqual(*ref_mode_costs, *tst_mode_costs);
// Keyframe with intrabc disabled (verifies inter_tx_type_costs and
// intrabc_cost stay untouched).
cm->features.allow_intrabc = 0;
std::memset(ref_mode_costs.get(), 0x5A, sizeof(*ref_mode_costs));
std::memset(tst_mode_costs.get(), 0x5A, sizeof(*tst_mode_costs));
FillModeRatesScalar(cm.get(), ref_mode_costs.get(), fc.get());
API_REGISTER_STATE_CHECK(
av1_fill_mode_rates(cm.get(), tst_mode_costs.get(), fc.get()));
ExpectModeCostsEqual(*ref_mode_costs, *tst_mode_costs);
for (const int num_planes : { 1, MAX_MB_PLANE }) {
std::memset(ref_coeff_costs.get(), 0x5A, sizeof(*ref_coeff_costs));
std::memset(tst_coeff_costs.get(), 0x5A, sizeof(*tst_coeff_costs));
FillCoeffCostsScalar(ref_coeff_costs.get(), fc.get(), num_planes);
API_REGISTER_STATE_CHECK(
av1_fill_coeff_costs(tst_coeff_costs.get(), fc.get(), num_planes));
ExpectCoeffCostsEqual(*ref_coeff_costs, *tst_coeff_costs);
}
}
}
} // namespace