blob: 113bf17973fc03ee76b96bf75308205ab8b2e525 [file] [log] [blame]
/*
* Copyright (c) 2021, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. 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
* aomedia.org/license/patent-license/.
*/
#include <assert.h>
#include <limits.h>
#include "config/aom_scale_rtcd.h"
#include "aom_dsp/aom_dsp_common.h"
#include "aom_dsp/psnr.h"
#include "aom_mem/aom_mem.h"
#include "aom_ports/mem.h"
#include "av1/common/av1_common_int.h"
#include "av1/common/av1_loopfilter.h"
#include "av1/common/quant_common.h"
#include "av1/encoder/av1_quantize.h"
#include "av1/encoder/encoder.h"
#include "av1/encoder/picklpf.h"
#include <float.h>
#define CHROMA_LAMBDA_MULT 6
static void yv12_copy_plane(const YV12_BUFFER_CONFIG *src_bc,
YV12_BUFFER_CONFIG *dst_bc, int plane) {
switch (plane) {
case 0: aom_yv12_copy_y(src_bc, dst_bc); break;
case 1: aom_yv12_copy_u(src_bc, dst_bc); break;
case 2: aom_yv12_copy_v(src_bc, dst_bc); break;
default: assert(plane >= 0 && plane <= 2); break;
}
}
static int64_t try_filter_frame(const YV12_BUFFER_CONFIG *sd,
AV1_COMP *const cpi, int q_offset,
int side_offset, int partial_frame, int plane,
int dir) {
MultiThreadInfo *const mt_info = &cpi->mt_info;
int num_workers = mt_info->num_workers;
AV1_COMMON *const cm = &cpi->common;
int64_t filt_err;
assert(plane >= 0 && plane <= 2);
// set base filters for use of av1_get_filter_level when in DELTA_LF mode
switch (plane) {
case 0:
#if DF_DUAL
switch (dir) {
case 2:
cm->lf.delta_q_luma[0] = cm->lf.delta_q_luma[1] = q_offset;
cm->lf.delta_side_luma[0] = cm->lf.delta_side_luma[1] = side_offset;
break;
case 1:
case 0:
cm->lf.delta_q_luma[dir] = q_offset;
cm->lf.delta_side_luma[dir] = side_offset;
break;
}
#else
cm->lf.delta_q_luma = q_offset;
cm->lf.delta_side_luma = side_offset;
#endif // DF_DUAL
break;
case 1:
cm->lf.delta_q_u = q_offset;
cm->lf.delta_side_u = side_offset;
break;
case 2:
cm->lf.delta_q_v = q_offset;
cm->lf.delta_side_v = side_offset;
break;
}
// TODO(any): please enable multi-thread and remove the flag when loop
// filter mask is compatible with multi-thread.
if (num_workers > 1)
av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, &cpi->td.mb.e_mbd, plane,
plane + 1, partial_frame,
#if CONFIG_LPF_MASK
0,
#endif
mt_info->workers, num_workers,
&mt_info->lf_row_sync);
else
av1_loop_filter_frame(&cm->cur_frame->buf, cm, &cpi->td.mb.e_mbd,
#if CONFIG_LPF_MASK
0,
#endif
plane, plane + 1, partial_frame);
filt_err = aom_get_sse_plane(sd, &cm->cur_frame->buf, plane);
// Re-instate the unfiltered frame
yv12_copy_plane(&cpi->last_frame_uf, &cm->cur_frame->buf, plane);
return filt_err;
}
static int search_filter_offsets(const YV12_BUFFER_CONFIG *sd, AV1_COMP *cpi,
int partial_frame,
const int *last_frame_offsets,
double *best_cost_ret, int plane,
int search_side_offset
#if DF_DUAL
,
int dir
#endif
) {
const AV1_COMMON *const cm = &cpi->common;
const int min_filter_offset = DF_PAR_MIN_VAL;
const int max_filter_offset = DF_PAR_MAX_VAL;
int filt_direction = 0;
int64_t best_err, start_err;
int offset_best;
MACROBLOCK *x = &cpi->td.mb;
int offsets[2];
int off_ind = search_side_offset;
int temp_offsets[2];
assert(plane >= 0 && plane <= 2);
// Start the search at the previous frame filter level unless it is now out of
// range.
#if DF_DUAL
switch (plane) {
case 0:
switch (dir) {
case 2:
offsets[0] = (last_frame_offsets[0] + last_frame_offsets[2] + 1) >> 1;
offsets[1] = (last_frame_offsets[1] + last_frame_offsets[3] + 1) >> 1;
break;
case 0:
offsets[0] = last_frame_offsets[0];
offsets[1] = last_frame_offsets[1];
break;
case 1:
offsets[0] = last_frame_offsets[2];
offsets[1] = last_frame_offsets[3];
break;
default: assert(dir >= 0 && dir <= 2); return 0;
}
break;
case 1:
offsets[0] = last_frame_offsets[4];
offsets[1] = last_frame_offsets[5];
break;
case 2:
offsets[0] = last_frame_offsets[6];
offsets[1] = last_frame_offsets[7];
break;
default: assert(plane >= 0 && plane <= 2); return 0;
}
#else
offsets[0] = last_frame_offsets[plane << 1];
offsets[1] = last_frame_offsets[(plane << 1) + 1];
#endif // DF_DUAL
int offset_mid =
clamp(offsets[off_ind], min_filter_offset, max_filter_offset);
// int filter_step = abs(offset_mid) < 16 ? 8 : abs(offset_mid) / 2; // is
// this a good number?
int filter_step = DF_SEARCH_STEP_SIZE; // is this a good number?
// Sum squared error at each filter level
int64_t ss_err[MAX_DF_OFFSETS + 1];
// Set each entry to -1
memset(ss_err, 0xFF, sizeof(ss_err));
yv12_copy_plane(&cm->cur_frame->buf, &cpi->last_frame_uf, plane);
#if DF_TWO_PARAM
temp_offsets[0] = offsets[0];
temp_offsets[1] = offsets[1];
temp_offsets[off_ind] = offset_mid;
#else
temp_offsets[0] = temp_offsets[1] = offset_mid;
#endif // DF_TWO_PARAM
#if DF_DUAL
start_err = best_err = try_filter_frame(
sd, cpi, temp_offsets[0], temp_offsets[1], partial_frame, plane, dir);
#else
start_err = best_err = try_filter_frame(
sd, cpi, temp_offsets[0], temp_offsets[1], partial_frame, plane, 0);
#endif // DF_DUAL
offset_best = offset_mid;
ss_err[offset_mid + ZERO_DF_OFFSET] = best_err;
while (filter_step > 0) {
const int offset_high = AOMMIN(offset_mid + filter_step, max_filter_offset);
const int offset_low = AOMMAX(offset_mid - filter_step, min_filter_offset);
int64_t bias = 0;
// yx, bias less for large block size
if (cm->features.tx_mode != ONLY_4X4) bias >>= 1;
if (filt_direction <= 0 && offset_low != offset_mid) {
// Get Low filter error score
if (ss_err[offset_low + ZERO_DF_OFFSET] < 0) {
#if DF_TWO_PARAM
temp_offsets[off_ind] = offset_low;
#else
temp_offsets[0] = temp_offsets[1] = offset_low;
#endif // DF_TWO_PARAM
ss_err[offset_low + ZERO_DF_OFFSET] =
#if DF_DUAL
try_filter_frame(sd, cpi, temp_offsets[0], temp_offsets[1],
partial_frame, plane, dir);
#else
try_filter_frame(sd, cpi, temp_offsets[0], temp_offsets[1],
partial_frame, plane, 0);
#endif // DF_TWO_PARAM
}
// If value is close to the best so far then bias towards a lower loop
// filter value.
if (ss_err[offset_low + ZERO_DF_OFFSET] < (best_err + bias)) {
// Was it actually better than the previous best?
if (ss_err[offset_low + ZERO_DF_OFFSET] < best_err) {
best_err = ss_err[offset_low + ZERO_DF_OFFSET];
}
offset_best = offset_low;
}
}
// Now look at filt_high
if (filt_direction >= 0 && offset_high != offset_mid) {
if (ss_err[offset_high + ZERO_DF_OFFSET] < 0) {
#if DF_TWO_PARAM
temp_offsets[off_ind] = offset_high;
#else
temp_offsets[0] = temp_offsets[1] = offset_high;
#endif // DF_TWO_PARAM
ss_err[offset_high + ZERO_DF_OFFSET] =
#if DF_DUAL
try_filter_frame(sd, cpi, temp_offsets[0], temp_offsets[1],
partial_frame, plane, dir);
#else
try_filter_frame(sd, cpi, temp_offsets[0], temp_offsets[1],
partial_frame, plane, 0);
#endif // DF_DUAL
}
// If value is significantly better than previous best, bias added against
// raising filter value
if (ss_err[offset_high + ZERO_DF_OFFSET] < (best_err - bias)) {
best_err = ss_err[offset_high + ZERO_DF_OFFSET];
offset_best = offset_high;
}
}
// Half the step distance if the best filter value was the same as last time
if (offset_best == offset_mid) {
filter_step /= 2;
filt_direction = 0;
} else {
filt_direction = (offset_best < offset_mid) ? -1 : 1;
offset_mid = offset_best;
}
}
// Update best error
best_err = ss_err[offset_best + ZERO_DF_OFFSET];
int chroma_lambda_mult = plane ? CHROMA_LAMBDA_MULT : 1;
#if DF_DUAL
int best_bits = 0;
int start_bits = 0;
if (dir == 2) {
start_bits = offsets[off_ind] ? DF_PAR_BITS : 0;
best_bits = offset_best ? DF_PAR_BITS : 0;
} else if (dir == 0) {
start_bits = 0; // To not bias the first dual search, assumed that dir == 1
// will be run later.
best_bits = 0;
} else { // dir == 1
int vert_q_ind = 0; // offset for the vert dir
int vert_offset = last_frame_offsets[vert_q_ind + off_ind];
int vert_bits = vert_offset ? DF_PAR_BITS : 0;
start_bits =
vert_bits + (offsets[off_ind] == vert_offset ? 0 : DF_PAR_BITS);
best_bits = vert_bits + (offset_best == vert_offset ? 0 : DF_PAR_BITS);
}
double best_cost =
RDCOST_DBL_WITH_NATIVE_BD_DIST(x->rdmult * chroma_lambda_mult, best_bits,
best_err, cm->seq_params.bit_depth);
double start_cost =
RDCOST_DBL_WITH_NATIVE_BD_DIST(x->rdmult * chroma_lambda_mult, start_bits,
start_err, cm->seq_params.bit_depth);
#else
double best_cost = RDCOST_DBL_WITH_NATIVE_BD_DIST(
x->rdmult * chroma_lambda_mult, offset_best ? DF_PAR_BITS : 0, best_err,
cm->seq_params.bit_depth);
double start_cost = RDCOST_DBL_WITH_NATIVE_BD_DIST(
x->rdmult * chroma_lambda_mult, offsets[off_ind] ? DF_PAR_BITS : 0,
start_err, cm->seq_params.bit_depth);
#endif // DF_DUAL
if (best_cost_ret) *best_cost_ret = AOMMIN(best_cost, start_cost);
return best_cost < start_cost ? offset_best : offsets[off_ind];
}
void av1_pick_filter_level(const YV12_BUFFER_CONFIG *sd, AV1_COMP *cpi,
LPF_PICK_METHOD method) {
AV1_COMMON *const cm = &cpi->common;
const int num_planes = av1_num_planes(cm);
struct loopfilter *const lf = &cm->lf;
(void)sd;
cpi->td.mb.rdmult = cpi->rd.RDMULT;
if (method == LPF_PICK_MINIMAL_LPF) {
lf->filter_level[0] = 0;
lf->filter_level[1] = 0;
lf->filter_level_u = lf->filter_level_v = 0;
} else if (method >= LPF_PICK_FROM_Q) {
// TODO(chengchen): retrain the model for Y, U, V filter levels
lf->filter_level[0] = lf->filter_level[1] = 1;
if (num_planes > 1) {
lf->filter_level_u = lf->filter_level_v = 1;
} else {
lf->filter_level_u = lf->filter_level_v = 0;
}
#if DF_DUAL
lf->delta_q_luma[0] = lf->delta_q_luma[1] = lf->delta_q_u = lf->delta_q_v =
0;
lf->delta_side_luma[0] = lf->delta_side_luma[1] = lf->delta_side_u =
lf->delta_side_v = 0;
#else
lf->delta_q_luma = lf->delta_q_u = lf->delta_q_v = 0;
lf->delta_side_luma = lf->delta_side_u = lf->delta_side_v = 0;
#endif // DF_DUAL
} else {
// To make sure the df filters are run
lf->filter_level[0] = 1;
lf->filter_level[1] = 1;
if (num_planes > 1) {
lf->filter_level_u = lf->filter_level_v = 1;
} else {
lf->filter_level_u = lf->filter_level_v = 0;
}
// TODO(anyone): What are good initial levels for keyframes?
#if DF_DUAL
lf->delta_q_luma[0] = lf->delta_q_luma[1] = lf->delta_q_u = lf->delta_q_v =
0;
lf->delta_side_luma[0] = lf->delta_side_luma[1] = lf->delta_side_u =
lf->delta_side_v = 0;
#else
lf->delta_q_luma = lf->delta_q_u = lf->delta_q_v = 0;
lf->delta_side_luma = lf->delta_side_u = lf->delta_side_v = 0;
#endif // DF_DUAL
#if DF_DUAL
int last_frame_offsets[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
#else
int last_frame_offsets[6] = { 0, 0, 0, 0, 0, 0 };
#endif // DF_DUAL
// double best_cost[3] = { DBL_MAX, DBL_MAX, DBL_MAX };
#if DF_DUAL
int dir = 0;
double best_single_cost = DBL_MAX;
double best_dual_cost = DBL_MAX;
int best_single_offsets[4] = { 0, 0, 0, 0 };
// luma
last_frame_offsets[1] = last_frame_offsets[3] = lf->delta_side_luma[0] =
lf->delta_side_luma[1] = search_filter_offsets(
sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, last_frame_offsets,
&best_single_cost, 0, 1, 2);
#if DF_TWO_PARAM
last_frame_offsets[0] = last_frame_offsets[2] = lf->delta_q_luma[0] =
lf->delta_q_luma[1] = search_filter_offsets(
sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, last_frame_offsets,
&best_single_cost, 0, 0, 2);
#else
last_frame_offsets[0] = last_frame_offsets[2] = lf->delta_q_luma[0] =
lf->delta_q_luma[1] = lf->delta_side_luma[0];
#endif // DF_TWO_PARAM
best_single_offsets[0] = last_frame_offsets[0];
best_single_offsets[1] = last_frame_offsets[1];
best_single_offsets[2] = last_frame_offsets[2];
best_single_offsets[3] = last_frame_offsets[3];
last_frame_offsets[1] = lf->delta_side_luma[0] =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, &best_dual_cost, 0, 1, 0);
#if DF_TWO_PARAM
last_frame_offsets[0] = lf->delta_q_luma[0] =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, &best_dual_cost, 0, 0, 0);
#else
last_frame_offsets[0] = lf->delta_q_luma[0] = lf->delta_side_luma[0];
#endif // DF_TWO_PARAM
last_frame_offsets[3] = lf->delta_side_luma[1] =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, &best_dual_cost, 0, 1, 1);
#if DF_TWO_PARAM
last_frame_offsets[2] = lf->delta_q_luma[1] =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, &best_dual_cost, 0, 0, 1);
#else
last_frame_offsets[2] = lf->delta_q_luma[1] = lf->delta_side_luma[1];
#endif // DF_TWO_PARAM
if (best_single_cost < best_dual_cost) {
lf->delta_q_luma[0] = last_frame_offsets[0] = best_single_offsets[0];
lf->delta_side_luma[0] = last_frame_offsets[1] = best_single_offsets[1];
lf->delta_q_luma[1] = last_frame_offsets[2] = best_single_offsets[2];
lf->delta_side_luma[1] = last_frame_offsets[3] = best_single_offsets[3];
}
if (num_planes > 1) {
// Cb
last_frame_offsets[5] = lf->delta_side_u =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, NULL, 1, 1, dir);
#if DF_TWO_PARAM
last_frame_offsets[4] = lf->delta_q_u =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, NULL, 1, 0, dir);
#else
last_frame_offsets[4] = lf->delta_q_u = lf->delta_side_u;
#endif // DF_TWO_PARAM
last_frame_offsets[5] = lf->delta_side_u =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, NULL, 1, 1, dir);
#if DF_TWO_PARAM
last_frame_offsets[4] = lf->delta_q_u =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, NULL, 1, 0, dir);
#else
last_frame_offsets[4] = lf->delta_q_u = lf->delta_side_u;
#endif // DF_TWO_PARAM
// Cr
last_frame_offsets[7] = lf->delta_side_v =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, NULL, 2, 1, dir);
#if DF_TWO_PARAM
last_frame_offsets[6] = lf->delta_q_v =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, NULL, 2, 0, dir);
#else
last_frame_offsets[6] = lf->delta_q_v = lf->delta_side_v;
#endif // DF_TWO_PARAM
last_frame_offsets[7] = lf->delta_side_v =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, NULL, 2, 1, dir);
#if DF_TWO_PARAM
last_frame_offsets[6] = lf->delta_q_v =
search_filter_offsets(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE,
last_frame_offsets, NULL, 2, 0, dir);
#else
last_frame_offsets[6] = lf->delta_q_v = lf->delta_side_v;
#endif // DF_TWO_PARAM
// to switch off filters if offsets are zero
if (!df_quant_from_qindex(cm->quant_params.base_qindex +
cm->lf.delta_q_luma[0] * DF_DELTA_SCALE,
cm->seq_params.bit_depth) ||
!df_side_from_qindex(cm->quant_params.base_qindex +
cm->lf.delta_side_luma[0] * DF_DELTA_SCALE,
cm->seq_params.bit_depth)) {
lf->filter_level[0] = 0;
cm->lf.delta_q_luma[0] = 0;
cm->lf.delta_side_luma[0] = 0;
}
if (!df_quant_from_qindex(cm->quant_params.base_qindex +
cm->lf.delta_q_luma[1] * DF_DELTA_SCALE,
cm->seq_params.bit_depth) ||
!df_side_from_qindex(cm->quant_params.base_qindex +
cm->lf.delta_side_luma[1] * DF_DELTA_SCALE,
cm->seq_params.bit_depth)) {
lf->filter_level[1] = 0;
cm->lf.delta_q_luma[1] = 0;
cm->lf.delta_side_luma[1] = 0;
}
if (lf->filter_level[0] == 0 && lf->filter_level[1] == 0) {
lf->filter_level_u = 0;
lf->filter_level_v = 0;
cm->lf.delta_q_u = 0;
cm->lf.delta_side_u = 0;
cm->lf.delta_q_v = 0;
cm->lf.delta_side_v = 0;
} else {
if (!df_quant_from_qindex(cm->quant_params.base_qindex +
cm->quant_params.u_ac_delta_q +
cm->lf.delta_q_u * DF_DELTA_SCALE,
cm->seq_params.bit_depth) ||
!df_side_from_qindex(cm->quant_params.base_qindex +
cm->quant_params.u_ac_delta_q +
cm->lf.delta_side_u * DF_DELTA_SCALE,
cm->seq_params.bit_depth)) {
lf->filter_level_u = 0;
cm->lf.delta_q_u = 0;
cm->lf.delta_side_u = 0;
}
if (!df_quant_from_qindex(cm->quant_params.base_qindex +
cm->quant_params.v_ac_delta_q +
cm->lf.delta_q_v * DF_DELTA_SCALE,
cm->seq_params.bit_depth) ||
!df_side_from_qindex(cm->quant_params.base_qindex +
cm->quant_params.v_ac_delta_q +
cm->lf.delta_side_v * DF_DELTA_SCALE,
cm->seq_params.bit_depth)) {
lf->filter_level_v = 0;
cm->lf.delta_q_v = 0;
cm->lf.delta_side_v = 0;
}
}
// to switch off filters if offsets are zero
}
#else // not DUAL
for (int plane = 0; plane < num_planes; ++plane) {
last_frame_offsets[(plane << 1) + 1] = search_filter_offsets(
sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, last_frame_offsets,
&best_cost[plane], plane, 1);
#if DF_TWO_PARAM
last_frame_offsets[plane << 1] = search_filter_offsets(
sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, last_frame_offsets,
&best_cost[plane], plane, 0);
#else
last_frame_offsets[plane << 1] = last_frame_offsets[(plane << 1) + 1];
#endif // DF_TWO_PARAM
last_frame_offsets[(plane << 1) + 1] = search_filter_offsets(
sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, last_frame_offsets,
&best_cost[plane], plane, 1);
#if DF_TWO_PARAM
last_frame_offsets[plane << 1] = search_filter_offsets(
sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, last_frame_offsets,
&best_cost[plane], plane, 0);
#else
last_frame_offsets[plane << 1] = last_frame_offsets[(plane << 1) + 1];
#endif // DF_TWO_PARAM
}
lf->delta_q_luma = last_frame_offsets[0];
lf->delta_side_luma = last_frame_offsets[1];
if (num_planes > 1) {
lf->delta_q_u = last_frame_offsets[2];
lf->delta_side_u = last_frame_offsets[3];
lf->delta_q_v = last_frame_offsets[4];
lf->delta_side_v = last_frame_offsets[5];
}
#endif // DF_DUAL
}
}