Refactor inter MV/merge candidate selection

Adds struct merge_candidates_t for holding the spatial and temporal
merge candidates. Changes functions with separate parameters for each
candidate to use the struct instead.
This commit is contained in:
Arttu Ylä-Outinen 2017-02-22 14:37:20 +09:00
parent 3409748a8f
commit 094b39e7fc
2 changed files with 154 additions and 163 deletions

View file

@ -31,6 +31,14 @@
#include "videoframe.h"
typedef struct {
const cu_info_t *a[2];
const cu_info_t *b[3];
const cu_info_t *c3;
const cu_info_t *h;
} merge_candidates_t;
static void inter_recon_frac_luma(const encoder_state_t * const state,
const kvz_picture * const ref,
int32_t xpos,
@ -642,24 +650,24 @@ static bool is_b0_cand_coded(int x, int y, int width, int height)
/**
* \brief Get merge candidates for current block
* \param encoder encoder control struct to use
* \param x block x position in SCU
* \param y block y position in SCU
* \param width current block width
* \param height current block height
* \param H candidate H
* \param C1 candidate C1
*/
static void kvz_inter_get_temporal_merge_candidates(const encoder_state_t * const state,
int32_t x,
int32_t y,
int32_t width,
int32_t height,
cu_info_t **C3,
cu_info_t **H,
uint8_t ref_list,
uint8_t ref_idx) {
* \brief Get merge candidates for current block
*
* \param encoder encoder control struct to use
* \param x block x position in SCU
* \param y block y position in SCU
* \param width current block width
* \param height current block height
* \param cand_out will be filled with C3 and H candidates
*/
static void get_temporal_merge_candidates(const encoder_state_t * const state,
int32_t x,
int32_t y,
int32_t width,
int32_t height,
uint8_t ref_list,
uint8_t ref_idx,
merge_candidates_t *cand_out)
{
/*
Predictor block locations
_________
@ -670,8 +678,7 @@ static void kvz_inter_get_temporal_merge_candidates(const encoder_state_t * cons
|H|
*/
*C3 = NULL;
*H = NULL;
cand_out->c3 = cand_out->h = NULL;
// Find temporal reference
if (state->frame->ref->used_size) {
@ -707,7 +714,7 @@ static void kvz_inter_get_temporal_merge_candidates(const encoder_state_t * cons
if (H_offset >= 0) {
// Only use when it's inter block
if (ref_cu_array->data[H_offset].type == CU_INTER) {
*H = &ref_cu_array->data[H_offset];
cand_out->h = &ref_cu_array->data[H_offset];
}
}
}
@ -718,7 +725,7 @@ static void kvz_inter_get_temporal_merge_candidates(const encoder_state_t * cons
if (xColCtr < state->encoder_control->in.width && yColCtr < state->encoder_control->in.height) {
uint32_t C3_offset = ((xColCtr >> 4) << 4) / SCU_WIDTH + ((((yColCtr >> 4) << 4) / SCU_WIDTH) * cu_per_width);
if (ref_cu_array->data[C3_offset].type == CU_INTER) {
*C3 = &ref_cu_array->data[C3_offset];
cand_out->c3 = &ref_cu_array->data[C3_offset];
}
}
}
@ -737,12 +744,8 @@ static void kvz_inter_get_temporal_merge_candidates(const encoder_state_t * cons
* \param height block height in pixels
* \param picture_width tile width in pixels
* \param picture_height tile height in pixels
* \param b0 Returns the b0 candidate.
* \param b1 Returns the b1 candidate.
* \param b2 Returns the b2 candidate.
* \param a0 Returns the a0 candidate.
* \param a1 Returns the a1 candidate.
* \param lcu current LCU
* \param cand_out will be filled with A and B candidates
*/
static void get_spatial_merge_candidates(int32_t x,
int32_t y,
@ -750,12 +753,8 @@ static void get_spatial_merge_candidates(int32_t x,
int32_t height,
int32_t picture_width,
int32_t picture_height,
cu_info_t **b0,
cu_info_t **b1,
cu_info_t **b2,
cu_info_t **a0,
cu_info_t **a1,
lcu_t *lcu)
lcu_t *lcu,
merge_candidates_t *cand_out)
{
/*
Predictor block locations
@ -771,59 +770,55 @@ static void get_spatial_merge_candidates(int32_t x,
int32_t y_local = SUB_SCU(y);
// A0 and A1 availability testing
if (x != 0) {
*a1 = LCU_GET_CU_AT_PX(lcu, x_local - 1, y_local + height - 1);
// Do not check (*a1)->coded because the block above is always coded before
cu_info_t *a1 = LCU_GET_CU_AT_PX(lcu, x_local - 1, y_local + height - 1);
// Do not check a1->coded because the block above is always coded before
// the current one and the flag is not set when searching an SMP block.
if ((*a1)->type == CU_INTER) {
inter_clear_cu_unused(*a1);
} else {
*a1 = NULL;
if (a1->type == CU_INTER) {
inter_clear_cu_unused(a1);
cand_out->a[1] = a1;
}
if (y_local + height < LCU_WIDTH && y + height < picture_height) {
*a0 = LCU_GET_CU_AT_PX(lcu, x_local - 1, y_local + height);
if ((*a0)->type == CU_INTER && is_a0_cand_coded(x, y, width, height)) {
inter_clear_cu_unused(*a0);
} else {
*a0 = NULL;
cu_info_t *a0 = LCU_GET_CU_AT_PX(lcu, x_local - 1, y_local + height);
if (a0->type == CU_INTER && is_a0_cand_coded(x, y, width, height)) {
inter_clear_cu_unused(a0);
cand_out->a[0] = a0;
}
}
}
// B0, B1 and B2 availability testing
if (y != 0) {
cu_info_t *b0 = NULL;
if (x + width < picture_width) {
if (x_local + width < LCU_WIDTH) {
*b0 = LCU_GET_CU_AT_PX(lcu, x_local + width, y_local - 1);
b0 = LCU_GET_CU_AT_PX(lcu, x_local + width, y_local - 1);
} else if (y_local == 0) {
// Special case, top-right CU
*b0 = LCU_GET_TOP_RIGHT_CU(lcu);
b0 = LCU_GET_TOP_RIGHT_CU(lcu);
}
}
if ((*b0) && (*b0)->type == CU_INTER && is_b0_cand_coded(x, y, width, height)) {
inter_clear_cu_unused(*b0);
} else {
*b0 = NULL;
if (b0 && b0->type == CU_INTER && is_b0_cand_coded(x, y, width, height)) {
inter_clear_cu_unused(b0);
cand_out->b[0] = b0;
}
*b1 = LCU_GET_CU_AT_PX(lcu, x_local + width - 1, y_local - 1);
// Do not check (*b1)->coded because the block to the left is always coded
cu_info_t *b1 = LCU_GET_CU_AT_PX(lcu, x_local + width - 1, y_local - 1);
// Do not check b1->coded because the block to the left is always coded
// before the current one and the flag is not set when searching an SMP
// block.
if ((*b1)->type == CU_INTER) {
inter_clear_cu_unused(*b1);
} else {
*b1 = NULL;
if (b1->type == CU_INTER) {
inter_clear_cu_unused(b1);
cand_out->b[1] = b1;
}
if (x != 0) {
*b2 = LCU_GET_CU_AT_PX(lcu, x_local - 1, y_local - 1);
// Do not check (*b2)->coded because the block above and to the left is
cu_info_t *b2 = LCU_GET_CU_AT_PX(lcu, x_local - 1, y_local - 1);
// Do not check b2->coded because the block above and to the left is
// always coded before the current one.
if ((*b2)->type == CU_INTER) {
inter_clear_cu_unused(*b2);
} else {
*b2 = NULL;
if (b2->type == CU_INTER) {
inter_clear_cu_unused(b2);
cand_out->b[2] = b2;
}
}
}
@ -843,11 +838,7 @@ static void get_spatial_merge_candidates(int32_t x,
* \param height block height in pixels
* \param picture_width tile width in pixels
* \param picture_height tile height in pixels
* \param b0 Returns the b0 candidate.
* \param b1 Returns the b1 candidate.
* \param b2 Returns the b2 candidate.
* \param a0 Returns the a0 candidate.
* \param a1 Returns the a1 candidate.
* \param cand_out will be filled with A and B candidates
*/
static void get_spatial_merge_candidates_cua(const cu_array_t *cua,
int32_t x,
@ -856,11 +847,7 @@ static void get_spatial_merge_candidates_cua(const cu_array_t *cua,
int32_t height,
int32_t picture_width,
int32_t picture_height,
const cu_info_t **b0,
const cu_info_t **b1,
const cu_info_t **b2,
const cu_info_t **a0,
const cu_info_t **a1)
merge_candidates_t *cand_out)
{
/*
Predictor block locations
@ -876,16 +863,16 @@ static void get_spatial_merge_candidates_cua(const cu_array_t *cua,
int32_t y_local = SUB_SCU(y);
// A0 and A1 availability testing
if (x != 0) {
*a1 = kvz_cu_array_at_const(cua, x - 1, y + height - 1);
const cu_info_t *a1 = kvz_cu_array_at_const(cua, x - 1, y + height - 1);
// The block above is always coded before the current one.
if ((*a1)->type != CU_INTER) {
*a1 = NULL;
if (a1->type == CU_INTER) {
cand_out->a[1] = a1;
}
if (y_local + height < LCU_WIDTH && y + height < picture_height) {
*a0 = kvz_cu_array_at_const(cua, x - 1, y + height);
if ((*a0)->type != CU_INTER || !is_a0_cand_coded(x, y, width, height)) {
*a0 = NULL;
const cu_info_t *a0 = kvz_cu_array_at_const(cua, x - 1, y + height);
if (a0->type == CU_INTER && is_a0_cand_coded(x, y, width, height)) {
cand_out->a[0] = a0;
}
}
}
@ -893,24 +880,24 @@ static void get_spatial_merge_candidates_cua(const cu_array_t *cua,
// B0, B1 and B2 availability testing
if (y != 0) {
if (x + width < picture_width && (x_local + width < LCU_WIDTH || y_local == 0)) {
*b0 = kvz_cu_array_at_const(cua, x + width, y - 1);
if ((*b0)->type != CU_INTER || !is_b0_cand_coded(x, y, width, height)) {
*b0 = NULL;
const cu_info_t *b0 = kvz_cu_array_at_const(cua, x + width, y - 1);
if (b0->type == CU_INTER && is_b0_cand_coded(x, y, width, height)) {
cand_out->b[0] = b0;
}
}
*b1 = kvz_cu_array_at_const(cua, x + width - 1, y - 1);
const cu_info_t *b1 = kvz_cu_array_at_const(cua, x + width - 1, y - 1);
// The block to the left is always coded before the current one.
if ((*b1)->type != CU_INTER) {
*b1 = NULL;
if (b1->type == CU_INTER) {
cand_out->b[1] = b1;
}
if (x != 0) {
*b2 = kvz_cu_array_at_const(cua, x - 1, y - 1);
const cu_info_t *b2 = kvz_cu_array_at_const(cua, x - 1, y - 1);
// The block above and to the left is always coded before the current
// one.
if ((*b2)->type != CU_INTER) {
*b2 = NULL;
if (b2->type == CU_INTER) {
cand_out->b[2] = b2;
}
}
}
@ -1041,50 +1028,53 @@ static INLINE bool add_mvp_candidate(const encoder_state_t *state,
* \brief Pick two mv candidates from the spatial and temporal candidates.
*/
static void get_mv_cand_from_candidates(const encoder_state_t * const state,
int32_t x,
int32_t y,
int32_t width,
int32_t height,
const cu_info_t *b0,
const cu_info_t *b1,
const cu_info_t *b2,
const cu_info_t *a0,
const cu_info_t *a1,
const cu_info_t *c3,
const cu_info_t *h,
const cu_info_t *cur_cu,
int8_t reflist,
int16_t mv_cand[2][2])
int32_t x,
int32_t y,
int32_t width,
int32_t height,
const merge_candidates_t *merge_cand,
const cu_info_t *cur_cu,
int8_t reflist,
int16_t mv_cand[2][2])
{
const cu_info_t *const *a = merge_cand->a;
const cu_info_t *const *b = merge_cand->b;
const cu_info_t *c3 = merge_cand->c3;
const cu_info_t *h = merge_cand->h;
uint8_t candidates = 0;
uint8_t b_candidates = 0;
// Left predictors without scaling
if (add_mvp_candidate(state, cur_cu, a0, reflist, false, mv_cand[candidates])) {
candidates++;
} else if (add_mvp_candidate(state, cur_cu, a1, reflist, false, mv_cand[candidates])) {
candidates++;
for (int i = 0; i < 2; i++) {
if (add_mvp_candidate(state, cur_cu, a[i], reflist, false, mv_cand[candidates])) {
candidates++;
break;
}
}
// Left predictors with scaling
} else if (add_mvp_candidate(state, cur_cu, a0, reflist, true, mv_cand[candidates])) {
candidates++;
} else if (add_mvp_candidate(state, cur_cu, a1, reflist, true, mv_cand[candidates])) {
candidates++;
if (candidates == 0) {
for (int i = 0; i < 2; i++) {
if (add_mvp_candidate(state, cur_cu, a[i], reflist, true, mv_cand[candidates])) {
candidates++;
break;
}
}
}
// Top predictors without scaling
if (add_mvp_candidate(state, cur_cu, b0, reflist, false, mv_cand[candidates])) {
b_candidates++;
} else if (add_mvp_candidate(state, cur_cu, b1, reflist, false, mv_cand[candidates])) {
b_candidates++;
} else if (add_mvp_candidate(state, cur_cu, b2, reflist, false, mv_cand[candidates])) {
b_candidates++;
for (int i = 0; i < 3; i++) {
if (add_mvp_candidate(state, cur_cu, b[i], reflist, false, mv_cand[candidates])) {
b_candidates++;
break;
}
}
candidates += b_candidates;
// When a1 or a0 is available, we dont check for secondary B candidates.
if (a1 || a0) {
if (a[0] || a[1]) {
b_candidates = 1;
} else if (candidates != 2) {
b_candidates = 0;
@ -1092,12 +1082,11 @@ static void get_mv_cand_from_candidates(const encoder_state_t * const state,
if (!b_candidates) {
// Top predictors with scaling
if (add_mvp_candidate(state, cur_cu, b0, reflist, true, mv_cand[candidates])) {
candidates++;
} else if (add_mvp_candidate(state, cur_cu, b1, reflist, true, mv_cand[candidates])) {
candidates++;
} else if (add_mvp_candidate(state, cur_cu, b2, reflist, true, mv_cand[candidates])) {
candidates++;
for (int i = 0; i < 3; i++) {
if (add_mvp_candidate(state, cur_cu, b[i], reflist, true, mv_cand[candidates])) {
candidates++;
break;
}
}
}
@ -1155,13 +1144,15 @@ void kvz_inter_get_mv_cand(const encoder_state_t * const state,
lcu_t *lcu,
int8_t reflist)
{
cu_info_t *b0, *b1, *b2, *a0, *a1, *c3, *h;
b0 = b1 = b2 = a0 = a1 = c3 = h = NULL;
merge_candidates_t merge_cand = { {0, 0}, {0, 0, 0}, 0, 0 };
get_spatial_merge_candidates(x, y, width, height,
state->tile->frame->width, state->tile->frame->height,
&b0, &b1, &b2, &a0, &a1, lcu);
kvz_inter_get_temporal_merge_candidates(state, x, y, width, height, &c3, &h, 1, 0);
get_mv_cand_from_candidates(state, x, y, width, height, b0, b1, b2, a0, a1, c3, h, cur_cu, reflist, mv_cand);
state->tile->frame->width,
state->tile->frame->height,
lcu,
&merge_cand);
get_temporal_merge_candidates(state, x, y, width, height, 1, 0, &merge_cand);
get_mv_cand_from_candidates(state, x, y, width, height, &merge_cand, cur_cu, reflist, mv_cand);
}
/**
@ -1185,17 +1176,15 @@ void kvz_inter_get_mv_cand_cua(const encoder_state_t * const state,
const cu_info_t* cur_cu,
int8_t reflist)
{
const cu_info_t *b0, *b1, *b2, *a0, *a1;
cu_info_t *c3, *h;
b0 = b1 = b2 = a0 = a1 = c3 = h = NULL;
merge_candidates_t merge_cand = { {0, 0}, {0, 0, 0}, 0, 0 };
const cu_array_t *cua = state->tile->frame->cu_array;
get_spatial_merge_candidates_cua(cua,
x, y, width, height,
state->tile->frame->width, state->tile->frame->height,
&b0, &b1, &b2, &a0, &a1);
kvz_inter_get_temporal_merge_candidates(state, x, y, width, height, &c3, &h, 1, 0);
get_mv_cand_from_candidates(state, x, y, width, height, b0, b1, b2, a0, a1, c3, h, cur_cu, reflist, mv_cand);
&merge_cand);
get_temporal_merge_candidates(state, x, y, width, height, 1, 0, &merge_cand);
get_mv_cand_from_candidates(state, x, y, width, height, &merge_cand, cur_cu, reflist, mv_cand);
}
static bool is_duplicate_candidate(const cu_info_t* cu1, const cu_info_t* cu2)
@ -1261,22 +1250,27 @@ uint8_t kvz_inter_get_merge_cand(const encoder_state_t * const state,
{
uint8_t candidates = 0;
int8_t zero_idx = 0;
cu_info_t *b0, *b1, *b2, *a0, *a1;
b0 = b1 = b2 = a0 = a1 = NULL;
merge_candidates_t merge_cand = { {0, 0}, {0, 0, 0}, 0, 0 };
get_spatial_merge_candidates(x, y, width, height,
state->tile->frame->width,
state->tile->frame->height,
&b0, &b1, &b2, &a0, &a1, lcu);
lcu,
&merge_cand);
if (!use_a1) a1 = NULL;
if (!use_b1) b1 = NULL;
const cu_info_t **a = merge_cand.a;
const cu_info_t **b = merge_cand.b;
if (add_merge_candidate(a1, NULL, NULL, &mv_cand[candidates])) candidates++;
if (add_merge_candidate(b1, a1, NULL, &mv_cand[candidates])) candidates++;
if (add_merge_candidate(b0, b1, NULL, &mv_cand[candidates])) candidates++;
if (add_merge_candidate(a0, a1, NULL, &mv_cand[candidates])) candidates++;
if (!use_a1) a[1] = NULL;
if (!use_b1) b[1] = NULL;
if (add_merge_candidate(a[1], NULL, NULL, &mv_cand[candidates])) candidates++;
if (add_merge_candidate(b[1], a[1], NULL, &mv_cand[candidates])) candidates++;
if (add_merge_candidate(b[0], b[1], NULL, &mv_cand[candidates])) candidates++;
if (add_merge_candidate(a[0], a[1], NULL, &mv_cand[candidates])) candidates++;
if (candidates < 4 &&
add_merge_candidate(b2, a1, b1, &mv_cand[candidates])) candidates++;
add_merge_candidate(b[2], a[1], b[1], &mv_cand[candidates])) candidates++;
bool can_use_tmvp =
state->encoder_control->cfg.tmvp_enable &&
@ -1289,16 +1283,16 @@ uint8_t kvz_inter_get_merge_cand(const encoder_state_t * const state,
const int max_reflist = (state->frame->slicetype == KVZ_SLICE_B ? 1 : 0);
for (int reflist = 0; reflist <= max_reflist; reflist++) {
// Fetch temporal candidates for the current CU
cu_info_t *c3 = NULL;
cu_info_t *h = NULL;
kvz_inter_get_temporal_merge_candidates(state, x, y, width, height, &c3, &h, 1, 0);
get_temporal_merge_candidates(state, x, y, width, height, 1, 0, &merge_cand);
// TODO: enable L1 TMVP candidate
// kvz_inter_get_temporal_merge_candidates(state, x, y, width, height, &c3, &h, 2, 0);
// get_temporal_merge_candidates(state, x, y, width, height, 2, 0, &merge_cand);
const cu_info_t *temporal_cand =
(merge_cand.h != NULL) ? merge_cand.h : merge_cand.c3;
if (add_temporal_candidate(state,
ref_idx,
(h != NULL) ? h : c3,
temporal_cand,
reflist,
mv_cand[candidates].mv[reflist],
&mv_cand[candidates].ref[reflist])) {

View file

@ -31,22 +31,19 @@ TEST test_get_spatial_merge_cand(void)
lcu.cu[i].type = CU_INTER;
}
cu_info_t *mv_cand[5] = { NULL };
merge_candidates_t cand = { {0, 0}, {0, 0, 0}, 0, 0 };
get_spatial_merge_candidates(64 + 32, 64, // x, y
32, 24, // width, height
1920, 1080, // picture size
&mv_cand[0], // b0
&mv_cand[1], // b1
&mv_cand[2], // b2
&mv_cand[3], // a0
&mv_cand[4], // a1
&lcu);
&lcu,
&cand);
ASSERT_EQ(mv_cand[0], &lcu.cu[289]); // b0
ASSERT_EQ(mv_cand[1], &lcu.cu[ 16]); // b1
ASSERT_EQ(mv_cand[2], &lcu.cu[ 8]); // b2
ASSERT_EQ(mv_cand[3], &lcu.cu[127]); // a0
ASSERT_EQ(mv_cand[4], &lcu.cu[110]); // a1
ASSERT_EQ(cand.b[0], &lcu.cu[289]);
ASSERT_EQ(cand.b[1], &lcu.cu[ 16]);
ASSERT_EQ(cand.b[2], &lcu.cu[ 8]);
ASSERT_EQ(cand.a[0], &lcu.cu[127]);
ASSERT_EQ(cand.a[1], &lcu.cu[110]);
PASS();
}