blob: af72c4e49d22cb0ffc720a3ce0fb3a621bbe7349 [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 "av1/common/blockd.h"
#include "av1/common/cdef.h"
#include "av1/common/cdef_block.h"
#include "av1/common/cfl.h"
#include "av1/common/common.h"
#include "av1/common/txb_common.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/entropymv.h"
#include "av1/common/mvref_common.h"
#include "av1/common/pred_common.h"
#include "av1/common/reconinter.h"
#include "av1/common/reconintra.h"
#include "av1/common/seg_common.h"
#include "av1/common/warped_motion.h"
#include "av1/decoder/decodeframe.h"
#include "av1/decoder/decodemv.h"
#include "aom_dsp/aom_dsp_common.h"
#if CONFIG_VQ_MVD_CODING
#include "aom_dsp/binary_codes_reader.h"
#endif // CONFIG_VQ_MVD_CODING
#define DEC_MISMATCH_DEBUG 0
#if !CONFIG_AIMC
static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
return (PREDICTION_MODE)aom_read_symbol(r, cdf, INTRA_MODES, ACCT_INFO());
}
#endif // !CONFIG_AIMC
static void read_cdef(AV1_COMMON *cm, aom_reader *r, MACROBLOCKD *const xd) {
assert(xd->tree_type != CHROMA_PART);
const int skip_txfm = xd->mi[0]->skip_txfm[0];
if (cm->features.coded_lossless) return;
if (is_global_intrabc_allowed(cm)) {
#if CONFIG_FIX_CDEF_SYNTAX
assert(cm->cdef_info.cdef_frame_enable == 0);
#else
assert(cm->cdef_info.cdef_bits == 0);
#endif // CONFIG_FIX_CDEF_SYNTAX
return;
}
#if CONFIG_FIX_CDEF_SYNTAX
if (!cm->cdef_info.cdef_frame_enable) return;
#endif // CONFIG_FIX_CDEF_SYNTAX
// At the start of a superblock, mark that we haven't yet read CDEF strengths
// for any of the CDEF units contained in this superblock.
const int sb_mask = (cm->mib_size - 1);
const int mi_row_in_sb = (xd->mi_row & sb_mask);
const int mi_col_in_sb = (xd->mi_col & sb_mask);
if (mi_row_in_sb == 0 && mi_col_in_sb == 0) {
av1_zero(xd->cdef_transmitted);
}
// CDEF unit size is 64x64 irrespective of the superblock size.
const int cdef_size = 1 << MI_IN_CDEF_LINEAR_LOG2;
// Find index of this CDEF unit in this superblock.
const int index = av1_get_cdef_transmitted_index(xd->mi_row, xd->mi_col);
// Read CDEF strength from the first non-skip coding block in this CDEF unit.
if (!xd->cdef_transmitted[index] && !skip_txfm) {
// CDEF strength for this CDEF unit needs to be read into the MB_MODE_INFO
// of the 1st block in this CDEF unit.
const int first_block_mask = ~(cdef_size - 1);
CommonModeInfoParams *const mi_params = &cm->mi_params;
const int grid_idx =
get_mi_grid_idx(mi_params, xd->mi_row & first_block_mask,
xd->mi_col & first_block_mask);
MB_MODE_INFO *const mbmi = mi_params->mi_grid_base[grid_idx];
mbmi->cdef_strength = aom_read_literal(r, cm->cdef_info.cdef_bits,
ACCT_INFO("cdef_strength"));
xd->cdef_transmitted[index] = true;
}
}
#if CONFIG_CCSO
static void read_ccso(AV1_COMMON *cm, aom_reader *r, MACROBLOCKD *const xd) {
if (cm->features.coded_lossless) return;
if (is_global_intrabc_allowed(cm)) return;
const CommonModeInfoParams *const mi_params = &cm->mi_params;
const int mi_row = xd->mi_row;
const int mi_col = xd->mi_col;
const int blk_size_y =
(1 << (CCSO_BLK_SIZE + xd->plane[1].subsampling_y - MI_SIZE_LOG2)) - 1;
const int blk_size_x =
(1 << (CCSO_BLK_SIZE + xd->plane[1].subsampling_x - MI_SIZE_LOG2)) - 1;
#if CONFIG_CCSO_EXT
if (!(mi_row & blk_size_y) && !(mi_col & blk_size_x) &&
cm->ccso_info.ccso_enable[0]) {
const int blk_idc =
aom_read_symbol(r, xd->tile_ctx->ccso_cdf[0], 2, ACCT_INFO("blk_idc"));
xd->ccso_blk_y = blk_idc;
mi_params
->mi_grid_base[(mi_row & ~blk_size_y) * mi_params->mi_stride +
(mi_col & ~blk_size_x)]
->ccso_blk_y = blk_idc;
}
#endif
if (!(mi_row & blk_size_y) && !(mi_col & blk_size_x) &&
#if CONFIG_CCSO_EXT
cm->ccso_info.ccso_enable[1]) {
const int blk_idc =
aom_read_symbol(r, xd->tile_ctx->ccso_cdf[1], 2, ACCT_INFO("blk_idc"));
#else
cm->ccso_info.ccso_enable[0]) {
const int blk_idc = aom_read_bit(r, ACCT_INFO("blk_idc"));
#endif
xd->ccso_blk_u = blk_idc;
mi_params
->mi_grid_base[(mi_row & ~blk_size_y) * mi_params->mi_stride +
(mi_col & ~blk_size_x)]
->ccso_blk_u = blk_idc;
}
if (!(mi_row & blk_size_y) && !(mi_col & blk_size_x) &&
#if CONFIG_CCSO_EXT
cm->ccso_info.ccso_enable[2]) {
const int blk_idc =
aom_read_symbol(r, xd->tile_ctx->ccso_cdf[2], 2, ACCT_INFO("blk_idc"));
#else
cm->ccso_info.ccso_enable[1]) {
const int blk_idc = aom_read_bit(r, ACCT_INFO("blk_idc"));
#endif
xd->ccso_blk_v = blk_idc;
mi_params
->mi_grid_base[(mi_row & ~blk_size_y) * mi_params->mi_stride +
(mi_col & ~blk_size_x)]
->ccso_blk_v = blk_idc;
}
}
#endif
static int read_delta_qindex(AV1_COMMON *cm, const MACROBLOCKD *xd,
aom_reader *r, MB_MODE_INFO *const mbmi) {
int sign, abs, reduced_delta_qindex = 0;
BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART];
const int b_col = xd->mi_col & (cm->mib_size - 1);
const int b_row = xd->mi_row & (cm->mib_size - 1);
const int read_delta_q_flag = (b_col == 0 && b_row == 0);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
if ((bsize != cm->sb_size ||
mbmi->skip_txfm[xd->tree_type == CHROMA_PART] == 0) &&
read_delta_q_flag) {
abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1,
ACCT_INFO("abs"));
const int smallval = (abs < DELTA_Q_SMALL);
if (!smallval) {
const int rem_bits = aom_read_literal(r, 3, ACCT_INFO("rem_bits")) + 1;
const int thr = (1 << rem_bits) + 1;
abs = aom_read_literal(r, rem_bits, ACCT_INFO("abs")) + thr;
}
if (abs) {
sign = aom_read_bit(r, ACCT_INFO("sign"));
} else {
sign = 1;
}
reduced_delta_qindex = sign ? -abs : abs;
}
return reduced_delta_qindex;
}
static int read_delta_lflevel(const AV1_COMMON *const cm, aom_reader *r,
aom_cdf_prob *const cdf,
const MB_MODE_INFO *const mbmi, int mi_col,
int mi_row, int tree_type) {
int reduced_delta_lflevel = 0;
const int plane_type = (tree_type == CHROMA_PART);
const BLOCK_SIZE bsize = mbmi->sb_type[plane_type];
const int b_col = mi_col & (cm->mib_size - 1);
const int b_row = mi_row & (cm->mib_size - 1);
const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
if ((bsize != cm->sb_size || mbmi->skip_txfm[plane_type] == 0) &&
read_delta_lf_flag) {
int abs = aom_read_symbol(r, cdf, DELTA_LF_PROBS + 1, ACCT_INFO("abs"));
const int smallval = (abs < DELTA_LF_SMALL);
if (!smallval) {
const int rem_bits = aom_read_literal(r, 3, ACCT_INFO("rem_bits")) + 1;
const int thr = (1 << rem_bits) + 1;
abs = aom_read_literal(r, rem_bits, ACCT_INFO("abs")) + thr;
}
const int sign = abs ? aom_read_bit(r, ACCT_INFO("sign")) : 1;
reduced_delta_lflevel = sign ? -abs : abs;
}
return reduced_delta_lflevel;
}
static uint8_t read_mrl_index(FRAME_CONTEXT *ec_ctx, aom_reader *r
#if CONFIG_IMPROVED_INTRA_DIR_PRED
,
const MB_MODE_INFO *neighbor0,
const MB_MODE_INFO *neighbor1
#endif // CONFIG_IMPROVED_INTRA_DIR_PRED
) {
#if CONFIG_IMPROVED_INTRA_DIR_PRED
int ctx = get_mrl_index_ctx(neighbor0, neighbor1);
aom_cdf_prob *mrl_cdf = ec_ctx->mrl_index_cdf[ctx];
const uint8_t mrl_index =
aom_read_symbol(r, mrl_cdf, MRL_LINE_NUMBER, ACCT_INFO());
#else
const uint8_t mrl_index =
aom_read_symbol(r, ec_ctx->mrl_index_cdf, MRL_LINE_NUMBER, ACCT_INFO());
#endif // CONFIG_IMPROVED_INTRA_DIR_PRED
return mrl_index;
}
#if CONFIG_LOSSLESS_DPCM
// read if dpcm lossless mode is used for luma
static uint8_t read_dpcm_mode(FRAME_CONTEXT *ec_ctx, aom_reader *r) {
const uint8_t dpcm_mode =
aom_read_symbol(r, ec_ctx->dpcm_cdf, 2, ACCT_INFO());
return dpcm_mode;
}
// read dpcm lossless direction for luma
static uint8_t read_dpcm_vert_horz_mode(FRAME_CONTEXT *ec_ctx, aom_reader *r) {
const uint8_t dpcm_vert_horz_mode =
aom_read_symbol(r, ec_ctx->dpcm_vert_horz_cdf, 2, ACCT_INFO());
return dpcm_vert_horz_mode;
}
// read if dpcm lossless mode is used for chroma
static uint8_t read_dpcm_uv_mode(FRAME_CONTEXT *ec_ctx, aom_reader *r) {
const uint8_t dpcm_uv_mode =
aom_read_symbol(r, ec_ctx->dpcm_uv_cdf, 2, ACCT_INFO());
return dpcm_uv_mode;
}
// read dpcm lossless direction for chroma
static uint8_t read_dpcm_uv_vert_horz_mode(FRAME_CONTEXT *ec_ctx,
aom_reader *r) {
const uint8_t dpcm_uv_vert_horz_mode =
aom_read_symbol(r, ec_ctx->dpcm_uv_vert_horz_cdf, 2, ACCT_INFO());
return dpcm_uv_vert_horz_mode;
}
#endif // CONFIG_LOSSLESS_DPCM
static uint8_t read_fsc_mode(aom_reader *r, aom_cdf_prob *fsc_cdf) {
const uint8_t fsc_mode = aom_read_symbol(r, fsc_cdf, FSC_MODES, ACCT_INFO());
return fsc_mode;
}
#if CONFIG_IMPROVED_CFL
static uint8_t read_cfl_index(FRAME_CONTEXT *ec_ctx, aom_reader *r) {
#if CONFIG_ENABLE_MHCCP
uint8_t cfl_index = aom_read_symbol(r, ec_ctx->cfl_index_cdf,
CFL_TYPE_COUNT - 1, ACCT_INFO());
#else
uint8_t cfl_index =
aom_read_symbol(r, ec_ctx->cfl_index_cdf, CFL_TYPE_COUNT, ACCT_INFO());
#endif // CONFIG_ENABLE_MHCCP
return cfl_index;
}
#endif
#if CONFIG_ENABLE_MHCCP
// Read multi hypothesis cross component prediction filter direction
static uint8_t read_mh_dir(aom_cdf_prob *mh_dir_cdf, aom_reader *r) {
uint8_t mh_dir = aom_read_symbol(r, mh_dir_cdf, MHCCP_MODE_NUM, ACCT_INFO());
return mh_dir;
}
#endif // CONFIG_ENABLE_MHCCP
#if !CONFIG_AIMC
static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
aom_reader *r,
CFL_ALLOWED_TYPE cfl_allowed,
PREDICTION_MODE y_mode) {
const UV_PREDICTION_MODE uv_mode =
aom_read_symbol(r, ec_ctx->uv_mode_cdf[cfl_allowed][y_mode],
UV_INTRA_MODES - !cfl_allowed, ACCT_INFO());
return uv_mode;
}
#endif // !CONFIG_AIMC
static uint8_t read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
int8_t *signs_out) {
const int8_t joint_sign = aom_read_symbol(
r, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS, ACCT_INFO("cfl:signs"));
uint8_t idx = 0;
// Magnitudes are only coded for nonzero values
if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
idx = (uint8_t)aom_read_symbol(r, cdf_u, CFL_ALPHABET_SIZE,
ACCT_INFO("cfl:alpha_u"))
<< CFL_ALPHABET_SIZE_LOG2;
}
if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
idx += (uint8_t)aom_read_symbol(r, cdf_v, CFL_ALPHABET_SIZE,
ACCT_INFO("cfl:alpha_v"));
}
*signs_out = joint_sign;
return idx;
}
static INTERINTRA_MODE read_interintra_mode(MACROBLOCKD *xd, aom_reader *r,
int size_group) {
const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
ACCT_INFO());
return ii_mode;
}
static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, aom_reader *r,
int16_t ctx
#if CONFIG_EXTENDED_WARP_PREDICTION
,
const AV1_COMMON *const cm,
const MACROBLOCKD *xd,
const MB_MODE_INFO *mbmi,
BLOCK_SIZE bsize
#endif // CONFIG_EXTENDED_WARP_PREDICTION
) {
const int16_t ismode_ctx = inter_single_mode_ctx(ctx);
#if CONFIG_EXTENDED_WARP_PREDICTION
int is_warpmv = 0;
if (is_warpmv_mode_allowed(cm, mbmi, bsize)) {
const int16_t iswarpmvmode_ctx = inter_warpmv_mode_ctx(cm, xd, mbmi);
is_warpmv =
aom_read_symbol(r, ec_ctx->inter_warp_mode_cdf[iswarpmvmode_ctx], 2,
ACCT_INFO("is_warpmv"));
if (is_warpmv) {
return WARPMV;
}
}
#endif // CONFIG_EXTENDED_WARP_PREDICTION
return SINGLE_INTER_MODE_START +
aom_read_symbol(r, ec_ctx->inter_single_mode_cdf[ismode_ctx],
INTER_SINGLE_MODES, ACCT_INFO("inter_single_mode"));
}
static void read_drl_idx(int max_drl_bits, const int16_t mode_ctx,
FRAME_CONTEXT *ec_ctx, DecoderCodingBlock *dcb,
MB_MODE_INFO *mbmi, aom_reader *r) {
MACROBLOCKD *const xd = &dcb->xd;
uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
#if CONFIG_SEP_COMP_DRL
mbmi->ref_mv_idx[0] = 0;
mbmi->ref_mv_idx[1] = 0;
#if !CONFIG_SKIP_MODE_ENHANCEMENT
assert(!mbmi->skip_mode);
#endif // CONFIG_SKIP_MODE_ENHANCEMENT
if (has_second_drl(mbmi)) {
if (mbmi->mode == NEAR_NEWMV)
max_drl_bits = AOMMIN(max_drl_bits, SEP_COMP_DRL_SIZE);
else
assert(mbmi->mode == NEAR_NEARMV);
}
for (int ref = 0; ref < 1 + has_second_drl(mbmi); ref++) {
for (int idx = 0; idx < max_drl_bits; ++idx) {
#if CONFIG_IMPROVED_SAME_REF_COMPOUND
if (ref && !mbmi->skip_mode && mbmi->ref_frame[0] == mbmi->ref_frame[1] &&
mbmi->mode == NEAR_NEARMV && idx <= mbmi->ref_mv_idx[0]) {
mbmi->ref_mv_idx[ref] = idx + 1;
continue;
}
#endif // CONFIG_IMPROVED_SAME_REF_COMPOUND
const uint16_t *weight = has_second_drl(mbmi)
? xd->weight[mbmi->ref_frame[ref]]
: xd->weight[ref_frame_type];
aom_cdf_prob *drl_cdf =
#if CONFIG_SKIP_MODE_ENHANCEMENT
mbmi->skip_mode ? ec_ctx->skip_drl_cdf[AOMMIN(idx, 2)]
: av1_get_drl_cdf(ec_ctx, weight, mode_ctx, idx);
#else
av1_get_drl_cdf(ec_ctx, xd->weight[ref_frame_type], mode_ctx, idx);
#endif // CONFIG_SKIP_MODE_ENHANCEMENT
int drl_idx = aom_read_symbol(r, drl_cdf, 2, ACCT_INFO("drl_idx"));
mbmi->ref_mv_idx[ref] = idx + drl_idx;
if (!drl_idx) break;
}
assert(mbmi->ref_mv_idx[ref] < max_drl_bits + 1);
}
#if CONFIG_IMPROVED_SAME_REF_COMPOUND
if (!mbmi->skip_mode && mbmi->ref_frame[0] == mbmi->ref_frame[1] &&
has_second_drl(mbmi) && mbmi->mode == NEAR_NEARMV &&
mbmi->ref_mv_idx[0] < max_drl_bits)
assert(mbmi->ref_mv_idx[0] < mbmi->ref_mv_idx[1]);
#endif // CONFIG_IMPROVED_SAME_REF_COMPOUND
#else
mbmi->ref_mv_idx = 0;
#if !CONFIG_SKIP_MODE_ENHANCEMENT
assert(!mbmi->skip_mode);
#endif // CONFIG_SKIP_MODE_ENHANCEMENT
for (int idx = 0; idx < max_drl_bits; ++idx) {
aom_cdf_prob *drl_cdf =
#if CONFIG_SKIP_MODE_ENHANCEMENT
mbmi->skip_mode ? ec_ctx->skip_drl_cdf[AOMMIN(idx, 2)]
: av1_get_drl_cdf(ec_ctx, xd->weight[ref_frame_type],
mode_ctx, idx);
#else
av1_get_drl_cdf(ec_ctx, xd->weight[ref_frame_type], mode_ctx, idx);
#endif // CONFIG_SKIP_MODE_ENHANCEMENT
int drl_idx = aom_read_symbol(r, drl_cdf, 2, ACCT_INFO("drl_idx"));
mbmi->ref_mv_idx = idx + drl_idx;
if (!drl_idx) break;
}
assert(mbmi->ref_mv_idx < max_drl_bits + 1);
#endif // CONFIG_SEP_COMP_DRL
}
#if CONFIG_WEDGE_MOD_EXT
static int8_t read_wedge_mode(aom_reader *r, FRAME_CONTEXT *ec_ctx,
const BLOCK_SIZE bsize) {
#if CONFIG_D149_CTX_MODELING_OPT
(void)bsize;
#endif // CONFIG_D149_CTX_MODELING_OPT
#if CONFIG_D149_CTX_MODELING_OPT
int wedge_angle_dir = aom_read_symbol(r, ec_ctx->wedge_angle_dir_cdf, 2,
ACCT_INFO("wedge_angle_dir"));
#else
int wedge_angle_dir = aom_read_symbol(r, ec_ctx->wedge_angle_dir_cdf[bsize],
2, ACCT_INFO("wedge_angle_dir"));
#endif // CONFIG_D149_CTX_MODELING_OPT
int wedge_angle = WEDGE_ANGLES;
if (wedge_angle_dir == 0) {
#if CONFIG_D149_CTX_MODELING_OPT
wedge_angle =
aom_read_symbol(r, ec_ctx->wedge_angle_0_cdf, H_WEDGE_ANGLES,
ACCT_INFO("wedge_angle", "wedge_angle_0_cdf"));
#else
wedge_angle =
aom_read_symbol(r, ec_ctx->wedge_angle_0_cdf[bsize], H_WEDGE_ANGLES,
ACCT_INFO("wedge_angle", "wedge_angle_0_cdf"));
#endif // CONFIG_D149_CTX_MODELING_OPT
} else {
wedge_angle =
H_WEDGE_ANGLES +
#if CONFIG_D149_CTX_MODELING_OPT
aom_read_symbol(r, ec_ctx->wedge_angle_1_cdf, H_WEDGE_ANGLES,
ACCT_INFO("wedge_angle", "wedge_angle_1_cdf"));
#else
aom_read_symbol(r, ec_ctx->wedge_angle_1_cdf[bsize], H_WEDGE_ANGLES,
ACCT_INFO("wedge_angle", "wedge_angle_1_cdf"));
#endif // CONFIG_D149_CTX_MODELING_OPT
}
int wedge_dist = 0;
if ((wedge_angle >= H_WEDGE_ANGLES) ||
(wedge_angle == WEDGE_90 || wedge_angle == WEDGE_180)) {
#if CONFIG_D149_CTX_MODELING_OPT
wedge_dist = aom_read_symbol(r, ec_ctx->wedge_dist_cdf2, NUM_WEDGE_DIST - 1,
ACCT_INFO("wedge_dist", "wedge_dist_cdf2")) +
1;
#else
wedge_dist =
aom_read_symbol(r, ec_ctx->wedge_dist_cdf2[bsize], NUM_WEDGE_DIST - 1,
ACCT_INFO("wedge_dist", "wedge_dist_cdf2")) +
1;
#endif // CONFIG_D149_CTX_MODELING_OPT
} else {
assert(wedge_angle < H_WEDGE_ANGLES);
#if CONFIG_D149_CTX_MODELING_OPT
wedge_dist = aom_read_symbol(r, ec_ctx->wedge_dist_cdf, NUM_WEDGE_DIST,
ACCT_INFO("wedge_dist", "wedge_dist_cdf"));
#else
wedge_dist =
aom_read_symbol(r, ec_ctx->wedge_dist_cdf[bsize], NUM_WEDGE_DIST,
ACCT_INFO("wedge_dist", "wedge_dist_cdf"));
#endif // CONFIG_D149_CTX_MODELING_OPT
}
return wedge_angle_dist_2_index[wedge_angle][wedge_dist];
}
#endif // CONFIG_WEDGE_MOD_EXT
#if CONFIG_EXTENDED_WARP_PREDICTION
// read the reference index warp_ref_idx of WRL
static void read_warp_ref_idx(FRAME_CONTEXT *ec_ctx, MB_MODE_INFO *mbmi,
aom_reader *r) {
if (mbmi->max_num_warp_candidates <= 1) {
mbmi->warp_ref_idx = 0;
return;
}
int max_idx_bits = mbmi->max_num_warp_candidates - 1;
for (int bit_idx = 0; bit_idx < max_idx_bits; ++bit_idx) {
aom_cdf_prob *warp_ref_idx_cdf = av1_get_warp_ref_idx_cdf(ec_ctx, bit_idx);
int warp_idx =
aom_read_symbol(r, warp_ref_idx_cdf, 2, ACCT_INFO("warp_idx"));
mbmi->warp_ref_idx = bit_idx + warp_idx;
if (!warp_idx) break;
}
}
static void read_warpmv_with_mvd_flag(FRAME_CONTEXT *ec_ctx, MB_MODE_INFO *mbmi,
aom_reader *r) {
mbmi->warpmv_with_mvd_flag =
#if CONFIG_D149_CTX_MODELING_OPT
aom_read_symbol(r, ec_ctx->warpmv_with_mvd_flag_cdf, 2,
ACCT_INFO("warpmv_with_mvd_flag"));
#else
aom_read_symbol(
r, ec_ctx->warpmv_with_mvd_flag_cdf[mbmi->sb_type[PLANE_TYPE_Y]], 2,
ACCT_INFO("warpmv_with_mvd_flag"));
#endif // CONFIG_D149_CTX_MODELING_OPT
}
// Read the delta for a single warp parameter
// Each delta is coded as a symbol in the range
// -WARP_DELTA_CODED_MAX, ..., 0, ..., +WARP_DELTA_CODED_MAX
static int read_warp_delta_param(const MACROBLOCKD *xd, int index,
aom_reader *r) {
assert(2 <= index && index <= 5);
int index_type = (index == 2 || index == 5) ? 0 : 1;
int coded_value =
aom_read_symbol(r, xd->tile_ctx->warp_delta_param_cdf[index_type],
WARP_DELTA_NUM_SYMBOLS, ACCT_INFO());
return (coded_value - WARP_DELTA_CODED_MAX) * WARP_DELTA_STEP;
}
static void read_warp_delta(const AV1_COMMON *cm, const MACROBLOCKD *xd,
MB_MODE_INFO *mbmi, aom_reader *r,
WARP_CANDIDATE *warp_param_stack) {
WarpedMotionParams *params = &mbmi->wm_params[0];
int mi_row = xd->mi_row;
int mi_col = xd->mi_col;
const BLOCK_SIZE bsize = mbmi->sb_type[PLANE_TYPE_Y];
// Figure out what parameters to use as a base
WarpedMotionParams base_params;
int_mv center_mv;
av1_get_warp_base_params(cm, mbmi, &base_params, &center_mv,
warp_param_stack);
// TODO(rachelbarker): Allow signaling warp type?
if (allow_warp_parameter_signaling(cm, mbmi)) {
params->wmtype = ROTZOOM;
params->wmmat[2] = base_params.wmmat[2] + read_warp_delta_param(xd, 2, r);
params->wmmat[3] = base_params.wmmat[3] + read_warp_delta_param(xd, 3, r);
params->wmmat[4] = -params->wmmat[3];
params->wmmat[5] = params->wmmat[2];
} else {
*params = base_params;
}
av1_reduce_warp_model(params);
int valid = av1_get_shear_params(params);
params->invalid = !valid;
if (!valid) {
#if WARPED_MOTION_DEBUG
printf("Warning: unexpected WARP_DELTA model from aomenc\n");
#endif
return;
}
av1_set_warp_translation(mi_row, mi_col, bsize, center_mv.as_mv, params);
#if CONFIG_C071_SUBBLK_WARPMV
assign_warpmv(cm, xd->submi, bsize, params, mi_row, mi_col
#if CONFIG_COMPOUND_WARP_CAUSAL
,
0
#endif // CONFIG_COMPOUND_WARP_CAUSAL
);
#endif // CONFIG_C071_SUBBLK_WARPMV
}
static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
MB_MODE_INFO *mbmi, aom_reader *r) {
const BLOCK_SIZE bsize = mbmi->sb_type[PLANE_TYPE_Y];
mbmi->max_num_warp_candidates = 0;
const int allowed_motion_modes =
motion_mode_allowed(cm, xd, xd->ref_mv_stack[mbmi->ref_frame[0]], mbmi);
if (mbmi->mode == WARPMV) {
if (allowed_motion_modes & (1 << WARPED_CAUSAL)) {
int use_warped_causal =
#if CONFIG_D149_CTX_MODELING_OPT
aom_read_symbol(r, xd->tile_ctx->warped_causal_warpmv_cdf, 2,
ACCT_INFO("use_warped_causal"));
#else
aom_read_symbol(r, xd->tile_ctx->warped_causal_warpmv_cdf[bsize], 2,
ACCT_INFO("use_warped_causal"));
#endif // CONFIG_D149_CTX_MODELING_OPT
return use_warped_causal ? WARPED_CAUSAL : WARP_DELTA;
}
return WARP_DELTA;
}
mbmi->use_wedge_interintra = 0;
if (allowed_motion_modes & (1 << INTERINTRA)) {
const int bsize_group = size_group_lookup[bsize];
const int use_interintra =
aom_read_symbol(r, xd->tile_ctx->interintra_cdf[bsize_group], 2,
ACCT_INFO("use_interintra"));
assert(mbmi->ref_frame[1] == NONE_FRAME);
if (use_interintra) {
const INTERINTRA_MODE interintra_mode =
read_interintra_mode(xd, r, bsize_group);
#if !CONFIG_INTERINTRA_IMPROVEMENT
mbmi->ref_frame[1] = INTRA_FRAME;
#endif // !CONFIG_INTERINTRA_IMPROVEMENT
mbmi->interintra_mode = interintra_mode;
mbmi->angle_delta[PLANE_TYPE_Y] = 0;
mbmi->angle_delta[PLANE_TYPE_UV] = 0;
#if CONFIG_LOSSLESS_DPCM
mbmi->use_dpcm_y = 0;
mbmi->dpcm_mode_y = 0;
mbmi->use_dpcm_uv = 0;
mbmi->dpcm_mode_uv = 0;
#endif // CONFIG_LOSSLESS_DPCM
mbmi->filter_intra_mode_info.use_filter_intra = 0;
if (av1_is_wedge_used(bsize)) {
mbmi->use_wedge_interintra =
#if CONFIG_D149_CTX_MODELING_OPT
aom_read_symbol(r, xd->tile_ctx->wedge_interintra_cdf, 2,
ACCT_INFO("use_wedge_interintra"));
#else
aom_read_symbol(r, xd->tile_ctx->wedge_interintra_cdf[bsize], 2,
ACCT_INFO("use_wedge_interintra"));
#endif // CONFIG_D149_CTX_MODELING_OPT
if (mbmi->use_wedge_interintra) {
#if CONFIG_WEDGE_MOD_EXT
mbmi->interintra_wedge_index =
read_wedge_mode(r, xd->tile_ctx, bsize);
assert(mbmi->interintra_wedge_index != -1);
#else
mbmi->interintra_wedge_index = (int8_t)aom_read_symbol(
r, xd->tile_ctx->wedge_idx_cdf[bsize], MAX_WEDGE_TYPES,
ACCT_INFO("interintra_wedge_index"));
#endif
}
}
return INTERINTRA;
}
}
if (allowed_motion_modes & (1 << OBMC_CAUSAL)) {
#if CONFIG_D149_CTX_MODELING_OPT
int use_obmc =
aom_read_symbol(r, xd->tile_ctx->obmc_cdf, 2, ACCT_INFO("use_obmc"));
#else
int use_obmc = aom_read_symbol(r, xd->tile_ctx->obmc_cdf[bsize], 2,
ACCT_INFO("use_obmc"));
#endif // CONFIG_D149_CTX_MODELING_OPT
if (use_obmc) {
return OBMC_CAUSAL;
}
}
if (allowed_motion_modes & (1 << WARP_EXTEND)) {
const int ctx1 = av1_get_warp_extend_ctx1(xd, mbmi);
const int ctx2 = av1_get_warp_extend_ctx2(xd, mbmi);
int use_warp_extend =
aom_read_symbol(r, xd->tile_ctx->warp_extend_cdf[ctx1][ctx2], 2,
ACCT_INFO("use_warp_extend"));
if (use_warp_extend) {
return WARP_EXTEND;
}
}
if (allowed_motion_modes & (1 << WARPED_CAUSAL)) {
#if CONFIG_D149_CTX_MODELING_OPT && !NO_D149_FOR_WARPED_CAUSAL
int use_warped_causal = aom_read_symbol(r, xd->tile_ctx->warped_causal_cdf,
2, ACCT_INFO("use_warped_causal"));
#else
int use_warped_causal =
aom_read_symbol(r, xd->tile_ctx->warped_causal_cdf[bsize], 2,
ACCT_INFO("use_warped_causal"));
#endif // CONFIG_D149_CTX_MODELING_OPT && !NO_D149_FOR_WARPED_CAUSAL
if (use_warped_causal) {
return WARPED_CAUSAL;
}
}
if (allowed_motion_modes & (1 << WARP_DELTA)) {
#if CONFIG_D149_CTX_MODELING_OPT
int use_warp_delta = aom_read_symbol(r, xd->tile_ctx->warp_delta_cdf, 2,
ACCT_INFO("use_warp_delta"));
#else
int use_warp_delta = aom_read_symbol(r, xd->tile_ctx->warp_delta_cdf[bsize],
2, ACCT_INFO("use_warp_delta"));
#endif // CONFIG_D149_CTX_MODELING_OPT
if (use_warp_delta) {
mbmi->motion_mode = WARP_DELTA;
return WARP_DELTA;
}
}
return SIMPLE_TRANSLATION;
}
#else
static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
MB_MODE_INFO *mbmi, aom_reader *r) {
if (mbmi->skip_mode) return SIMPLE_TRANSLATION;
if (is_tip_ref_frame(mbmi->ref_frame[0])) return SIMPLE_TRANSLATION;
const MOTION_MODE last_motion_mode_allowed =
motion_mode_allowed(cm, xd, mbmi);
int motion_mode;
if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
if (last_motion_mode_allowed == OBMC_CAUSAL) {
#if !CONFIG_D149_CTX_MODELING_OPT
const int bsize = mbmi->sb_type[PLANE_TYPE_Y];
#endif // !CONFIG_D149_CTX_MODELING_OPT
motion_mode = aom_read_symbol(r,
#if CONFIG_D149_CTX_MODELING_OPT
xd->tile_ctx->obmc_cdf,
#else
xd->tile_ctx->obmc_cdf[bsize],
#endif // CONFIG_D149_CTX_MODELING_OPT
2, ACCT_INFO("motion_mode", "obmc_cdf"));
return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
} else {
motion_mode = aom_read_symbol(
r, xd->tile_ctx->motion_mode_cdf[mbmi->sb_type[PLANE_TYPE_Y]],
MOTION_MODES, ACCT_INFO("motion_mode", "motion_mode_cdf"));
return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
}
}
#endif // CONFIG_EXTENDED_WARP_PREDICTION
// Read scale mode flag for joint mvd coding mode
static PREDICTION_MODE read_jmvd_scale_mode(MACROBLOCKD *xd, aom_reader *r,
MB_MODE_INFO *const mbmi) {
if (!is_joint_mvd_coding_mode(mbmi->mode)) return 0;
const int is_joint_amvd_mode = is_joint_amvd_coding_mode(mbmi->mode);
aom_cdf_prob *jmvd_scale_mode_cdf =
is_joint_amvd_mode ? xd->tile_ctx->jmvd_amvd_scale_mode_cdf
: xd->tile_ctx->jmvd_scale_mode_cdf;
const int jmvd_scale_cnt = is_joint_amvd_mode ? JOINT_AMVD_SCALE_FACTOR_CNT
: JOINT_NEWMV_SCALE_FACTOR_CNT;
const int jmvd_scale_mode = aom_read_symbol(
r, jmvd_scale_mode_cdf, jmvd_scale_cnt, ACCT_INFO("jmvd_scale_mode"));
return jmvd_scale_mode;
}
// Read index for the weighting factor of compound weighted prediction
static int read_cwp_idx(MACROBLOCKD *xd, aom_reader *r, const AV1_COMMON *cm,
MB_MODE_INFO *const mbmi) {
int8_t cwp_idx = 0;
int bit_cnt = 0;
const int ctx = 0;
for (int idx = 0; idx < MAX_CWP_NUM - 1; ++idx) {
const int tmp_idx = aom_read_symbol(
r, xd->tile_ctx->cwp_idx_cdf[ctx][bit_cnt], 2, ACCT_INFO());
cwp_idx = idx + tmp_idx;
if (!tmp_idx) break;
++bit_cnt;
}
assert(cwp_idx <= CWP_MAX);
// convert index to weight
return get_cwp_coding_idx(cwp_idx, 0, cm, mbmi);
}
static PREDICTION_MODE read_inter_compound_mode(MACROBLOCKD *xd, aom_reader *r,
#if CONFIG_OPTFLOW_REFINEMENT
const AV1_COMMON *cm,
MB_MODE_INFO *const mbmi,
#endif // CONFIG_OPTFLOW_REFINEMNET
int16_t ctx) {
#if CONFIG_AFFINE_REFINEMENT
mbmi->comp_refine_type = cm->features.opfl_refine_type == REFINE_ALL
? COMP_REFINE_TYPE_FOR_REFINE_ALL
: COMP_REFINE_NONE;
#endif // CONFIG_AFFINE_REFINEMENT
#if CONFIG_OPTFLOW_REFINEMENT
int use_optical_flow = 0;
const int mode = aom_read_symbol(
r, xd->tile_ctx->inter_compound_mode_cdf[ctx], INTER_COMPOUND_REF_TYPES,
ACCT_INFO("inter_compound_mode_cdf"));
if (cm->features.opfl_refine_type == REFINE_SWITCHABLE &&
opfl_allowed_for_cur_refs(cm, mbmi)) {
#if CONFIG_AFFINE_REFINEMENT
const int allow_translational =
is_translational_refinement_allowed(cm, comp_idx_to_opfl_mode[mode]);
const int allow_affine =
is_affine_refinement_allowed(cm, xd, comp_idx_to_opfl_mode[mode]);
if (allow_affine || allow_translational)
use_optical_flow = aom_read_symbol(r, xd->tile_ctx->use_optflow_cdf[ctx],
2, ACCT_INFO("use_optical_flow"));
mbmi->comp_refine_type = use_optical_flow
? COMP_REFINE_SUBBLK2P + allow_affine
: COMP_REFINE_NONE;
#else
use_optical_flow = aom_read_symbol(r, xd->tile_ctx->use_optflow_cdf[ctx], 2,
ACCT_INFO("use_optical_flow"));
#endif // CONFIG_AFFINE_REFINEMENT
if (use_optical_flow) {
assert(is_inter_compound_mode(comp_idx_to_opfl_mode[mode]));
return comp_idx_to_opfl_mode[mode];
}
}
#else
const int mode = aom_read_symbol(
r, xd->tile_ctx->inter_compound_mode_cdf[ctx], INTER_COMPOUND_MODES,
ACCT_INFO("inter_compound_mode_cdf"));
#endif // CONFIG_OPTFLOW_REFINEMENT
assert(is_inter_compound_mode(NEAR_NEARMV + mode));
return NEAR_NEARMV + mode;
}
int av1_neg_deinterleave(int diff, int ref, int max) {
if (!ref) return diff;
if (ref >= (max - 1)) return max - diff - 1;
if (2 * ref < max) {
if (diff <= 2 * ref) {
if (diff & 1)
return ref + ((diff + 1) >> 1);
else
return ref - (diff >> 1);
}
return diff;
} else {
if (diff <= 2 * (max - ref - 1)) {
if (diff & 1)
return ref + ((diff + 1) >> 1);
else
return ref - (diff >> 1);
}
return max - (diff + 1);
}
}
static int read_segment_id(AV1_COMMON *const cm, const MACROBLOCKD *const xd,
aom_reader *r, int skip) {
int cdf_num;
const int pred = av1_get_spatial_seg_pred(cm, xd, &cdf_num);
if (skip) return pred;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
struct segmentation *const seg = &cm->seg;
struct segmentation_probs *const segp = &ec_ctx->seg;
aom_cdf_prob *pred_cdf = segp->spatial_pred_seg_cdf[cdf_num];
const int coded_id =
aom_read_symbol(r, pred_cdf, MAX_SEGMENTS, ACCT_INFO("coded_id"));
const int segment_id =
av1_neg_deinterleave(coded_id, pred, seg->last_active_segid + 1);
if (segment_id < 0 || segment_id > seg->last_active_segid) {
aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
"Corrupted segment_ids");
}
return segment_id;
}
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
int mi_offset, int x_inside_boundary,
int y_inside_boundary) {
int segment_id = INT_MAX;
for (int y = 0; y < y_inside_boundary; y++)
for (int x = 0; x < x_inside_boundary; x++)
segment_id = AOMMIN(
segment_id, segment_ids[mi_offset + y * cm->mi_params.mi_cols + x]);
assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
return segment_id;
}
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_inside_boundary,
int y_inside_boundary, int segment_id) {
assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
for (int y = 0; y < y_inside_boundary; y++)
for (int x = 0; x < x_inside_boundary; x++)
cm->cur_frame->seg_map[mi_offset + y * cm->mi_params.mi_cols + x] =
segment_id;
}
static int read_intra_segment_id(AV1_COMMON *const cm,
const MACROBLOCKD *const xd, BLOCK_SIZE bsize,
aom_reader *r, int skip) {
struct segmentation *const seg = &cm->seg;
if (!seg->enabled) return 0; // Default for disabled segmentation
#if CONFIG_EXTENDED_SDP
if (frame_is_intra_only(cm))
#endif // CONFIG_EXTENDED_SDP
assert(seg->update_map && !seg->temporal_update);
const CommonModeInfoParams *const mi_params = &cm->mi_params;
const int mi_row = xd->mi_row;
const int mi_col = xd->mi_col;
const int mi_offset = mi_row * mi_params->mi_cols + mi_col;
const int bw = mi_size_wide[bsize];
const int bh = mi_size_high[bsize];
const int x_inside_boundary = AOMMIN(mi_params->mi_cols - mi_col, bw);
const int y_inside_boundary = AOMMIN(mi_params->mi_rows - mi_row, bh);
const int segment_id = read_segment_id(cm, xd, r, skip);
set_segment_id(cm, mi_offset, x_inside_boundary, y_inside_boundary,
segment_id);
return segment_id;
}
static void copy_segment_id(const CommonModeInfoParams *const mi_params,
const uint8_t *last_segment_ids,
uint8_t *current_segment_ids, int mi_offset,
int x_inside_boundary, int y_inside_boundary) {
for (int y = 0; y < y_inside_boundary; y++)
for (int x = 0; x < x_inside_boundary; x++)
current_segment_ids[mi_offset + y * mi_params->mi_cols + x] =
last_segment_ids
? last_segment_ids[mi_offset + y * mi_params->mi_cols + x]
: 0;
}
static int get_predicted_segment_id(AV1_COMMON *const cm, int mi_offset,
int x_inside_boundary,
int y_inside_boundary) {
return cm->last_frame_seg_map
? dec_get_segment_id(cm, cm->last_frame_seg_map, mi_offset,
x_inside_boundary, y_inside_boundary)
: 0;
}
static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
int preskip, aom_reader *r) {
struct segmentation *const seg = &cm->seg;
const CommonModeInfoParams *const mi_params = &cm->mi_params;
MB_MODE_INFO *const mbmi = xd->mi[0];
const int mi_row = xd->mi_row;
const int mi_col = xd->mi_col;
const int mi_offset = mi_row * mi_params->mi_cols + mi_col;
const int bw = mi_size_wide[mbmi->sb_type[PLANE_TYPE_Y]];
const int bh = mi_size_high[mbmi->sb_type[PLANE_TYPE_Y]];
// TODO(slavarnway): move x_inside_boundary, y_inside_boundary into xd ?????
const int x_inside_boundary = AOMMIN(mi_params->mi_cols - mi_col, bw);
const int y_inside_boundary = AOMMIN(mi_params->mi_rows - mi_row, bh);
if (!seg->enabled) return 0; // Default for disabled segmentation
if (!seg->update_map) {
copy_segment_id(mi_params, cm->last_frame_seg_map, cm->cur_frame->seg_map,
mi_offset, x_inside_boundary, y_inside_boundary);
return get_predicted_segment_id(cm, mi_offset, x_inside_boundary,
y_inside_boundary);
}
int segment_id;
if (preskip) {
if (!seg->segid_preskip) return 0;
} else {
if (mbmi->skip_txfm[xd->tree_type == CHROMA_PART]) {
if (seg->temporal_update) {
mbmi->seg_id_predicted = 0;
}
segment_id = read_segment_id(cm, xd, r, 1);
set_segment_id(cm, mi_offset, x_inside_boundary, y_inside_boundary,
segment_id);
return segment_id;
}
}
if (seg->temporal_update) {
const int ctx = av1_get_pred_context_seg_id(xd);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
struct segmentation_probs *const segp = &ec_ctx->seg;
aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
mbmi->seg_id_predicted =
aom_read_symbol(r, pred_cdf, 2, ACCT_INFO("seg_id_predicted"));
if (mbmi->seg_id_predicted) {
segment_id = get_predicted_segment_id(cm, mi_offset, x_inside_boundary,
y_inside_boundary);
} else {
segment_id = read_segment_id(cm, xd, r, 0);
}
} else {
segment_id = read_segment_id(cm, xd, r, 0);
}
set_segment_id(cm, mi_offset, x_inside_boundary, y_inside_boundary,
segment_id);
return segment_id;
}
static int read_skip_mode(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
aom_reader *r) {
if (!cm->current_frame.skip_mode_info.skip_mode_flag) return 0;
if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
return 0;
}
if (!is_comp_ref_allowed(xd->mi[0]->sb_type[xd->tree_type == CHROMA_PART]))
return 0;
if (segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
// These features imply single-reference mode, while skip mode implies
// compound reference. Hence, the two are mutually exclusive.
// In other words, skip_mode is implicitly 0 here.
return 0;
}
const int ctx = av1_get_skip_mode_context(xd);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
const int skip_mode = aom_read_symbol(r, ec_ctx->skip_mode_cdfs[ctx], 2,
ACCT_INFO("skip_mode"));
return skip_mode;
}
static int read_skip_txfm(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
aom_reader *r) {
if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
return 1;
} else {
const int ctx = av1_get_skip_txfm_context(xd);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
const int skip_txfm = aom_read_symbol(r, ec_ctx->skip_txfm_cdfs[ctx], 2,
ACCT_INFO("skip_txfm"));
return skip_txfm;
}
}
#if !CONFIG_PALETTE_IMPROVEMENTS
// Merge the sorted list of cached colors(cached_colors[0...n_cached_colors-1])
// and the sorted list of transmitted colors(colors[n_cached_colors...n-1]) into
// one single sorted list(colors[...]).
static void merge_colors(uint16_t *colors, uint16_t *cached_colors,
int n_colors, int n_cached_colors) {
if (n_cached_colors == 0) return;
int cache_idx = 0, trans_idx = n_cached_colors;
for (int i = 0; i < n_colors; ++i) {
if (cache_idx < n_cached_colors &&
(trans_idx >= n_colors ||
cached_colors[cache_idx] <= colors[trans_idx])) {
colors[i] = cached_colors[cache_idx++];
} else {
assert(trans_idx < n_colors);
colors[i] = colors[trans_idx++];
}
}
}
#endif //! CONFIG_PALETTE_IMPROVEMENTS
static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
PALETTE_MODE_INFO *const pmi, aom_reader *r) {
#if CONFIG_PALETTE_IMPROVEMENTS
uint16_t color_cache[2 * PALETTE_MAX_SIZE];
const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
const int n = pmi->palette_size[0];
int idx = 0;
for (int i = 0; i < n_cache && idx < n; ++i) {
if (aom_read_bit(r, ACCT_INFO("color_cache")))
pmi->palette_colors[idx++] = color_cache[i];
}
if (idx < n) {
pmi->palette_colors[idx++] =
aom_read_literal(r, bit_depth, ACCT_INFO("palette_colors"));
if (idx < n) {
const int min_bits = bit_depth - 3;
int bits = min_bits + aom_read_literal(r, 2, ACCT_INFO("bits"));
int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
for (; idx < n; ++idx) {
assert(range >= 0);
const int delta = aom_read_literal(r, bits, ACCT_INFO("delta")) + 1;
pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
0, (1 << bit_depth) - 1);
range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
bits = AOMMIN(bits, av1_ceil_log2(range));
}
}
}
// Sort Y palette
for (int i = 0; i < n; i++) {
for (int j = 1; j < n - i; j++) {
if (pmi->palette_colors[j - 1] > pmi->palette_colors[j]) {
const uint16_t tmp = pmi->palette_colors[j - 1];
pmi->palette_colors[j - 1] = pmi->palette_colors[j];
pmi->palette_colors[j] = tmp;
}
}
}
#else
uint16_t color_cache[2 * PALETTE_MAX_SIZE];
uint16_t cached_colors[PALETTE_MAX_SIZE];
const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
const int n = pmi->palette_size[0];
int idx = 0;
for (int i = 0; i < n_cache && idx < n; ++i)
if (aom_read_bit(r, ACCT_INFO("color_cache")))
cached_colors[idx++] = color_cache[i];
if (idx < n) {
const int n_cached_colors = idx;
pmi->palette_colors[idx++] =
aom_read_literal(r, bit_depth, ACCT_INFO("palette_colors"));
if (idx < n) {
const int min_bits = bit_depth - 3;
int bits = min_bits + aom_read_literal(r, 2, ACCT_INFO("bits"));
int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
for (; idx < n; ++idx) {
assert(range >= 0);
const int delta = aom_read_literal(r, bits, ACCT_INFO("delta")) + 1;
pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
0, (1 << bit_depth) - 1);
range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
bits = AOMMIN(bits, av1_ceil_log2(range));
}
}
merge_colors(pmi->palette_colors, cached_colors, n, n_cached_colors);
} else {
memcpy(pmi->palette_colors, cached_colors, n * sizeof(cached_colors[0]));
}
#endif // CONFIG_PALETTE_IMPROVEMENTS
}
static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
PALETTE_MODE_INFO *const pmi,
aom_reader *r) {
#if CONFIG_PALETTE_IMPROVEMENTS
const int n = pmi->palette_size[1];
// U channel colors.
uint16_t color_cache[2 * PALETTE_MAX_SIZE];
const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
int idx = PALETTE_MAX_SIZE;
for (int i = 0; i < n_cache && idx < PALETTE_MAX_SIZE + n; ++i)
if (aom_read_bit(r, ACCT_INFO("color_cache")))
pmi->palette_colors[idx++] = color_cache[i];
if (idx < PALETTE_MAX_SIZE + n) {
pmi->palette_colors[idx++] =
aom_read_literal(r, bit_depth, ACCT_INFO("palette_colors"));
if (idx < PALETTE_MAX_SIZE + n) {
const int min_bits = bit_depth - 3;
int bits = min_bits + aom_read_literal(r, 2, ACCT_INFO("bits"));
int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
assert(range >= 0);
const int delta = aom_read_literal(r, bits, ACCT_INFO("delta"));
pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
0, (1 << bit_depth) - 1);
range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
bits = AOMMIN(bits, av1_ceil_log2(range));
}
}
}
// Sort U palette
for (int i = 0; i < n; i++) {
for (int j = 1; j < n - i; j++) {
if (pmi->palette_colors[PALETTE_MAX_SIZE + j - 1] >
pmi->palette_colors[PALETTE_MAX_SIZE + j]) {
const uint16_t tmp = pmi->palette_colors[PALETTE_MAX_SIZE + j - 1];
pmi->palette_colors[PALETTE_MAX_SIZE + j - 1] =
pmi->palette_colors[PALETTE_MAX_SIZE + j];
pmi->palette_colors[PALETTE_MAX_SIZE + j] = tmp;
}
}
}
#else
const int n = pmi->palette_size[1];
// U channel colors.
uint16_t color_cache[2 * PALETTE_MAX_SIZE];
uint16_t cached_colors[PALETTE_MAX_SIZE];
const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
int idx = 0;
for (int i = 0; i < n_cache && idx < n; ++i)
if (aom_read_bit(r, ACCT_INFO("color_cache")))
cached_colors[idx++] = color_cache[i];
if (idx < n) {
const int n_cached_colors = idx;
idx += PALETTE_MAX_SIZE;
pmi->palette_colors[idx++] =
aom_read_literal(r, bit_depth, ACCT_INFO("palette_colors"));
if (idx < PALETTE_MAX_SIZE + n) {
const int min_bits = bit_depth - 3;
int bits = min_bits + aom_read_literal(r, 2, ACCT_INFO("bits"));
int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
assert(range >= 0);
const int delta = aom_read_literal(r, bits, ACCT_INFO("delta"));
pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
0, (1 << bit_depth) - 1);
range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
bits = AOMMIN(bits, av1_ceil_log2(range));
}
}
merge_colors(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors, n,
n_cached_colors);
} else {
memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors,
n * sizeof(cached_colors[0]));
}
#endif // CONFIG_PALETTE_IMPROVEMENTS
// V channel colors.
if (aom_read_bit(r, ACCT_INFO("use_delta"))) { // Delta encoding.
const int min_bits_v = bit_depth - 4;
const int max_val = 1 << bit_depth;
int bits = min_bits_v + aom_read_literal(r, 2, ACCT_INFO("bits"));
pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
aom_read_literal(r, bit_depth, ACCT_INFO("palette_colors"));
for (int i = 1; i < n; ++i) {
int delta = aom_read_literal(r, bits, ACCT_INFO("delta"));
if (delta && aom_read_bit(r, ACCT_INFO("negate"))) delta = -delta;
int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
if (val < 0) val += max_val;
if (val >= max_val) val -= max_val;
pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
}
} else {
for (int i = 0; i < n; ++i) {
pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
aom_read_literal(r, bit_depth, ACCT_INFO("palette_colors"));
}
}
}
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
aom_reader *r) {
const int num_planes = av1_num_planes(cm);
MB_MODE_INFO *const mbmi = xd->mi[0];
const BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART];
assert(av1_allow_palette(cm->features.allow_screen_content_tools, bsize));
PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
if (mbmi->mode == DC_PRED && xd->tree_type != CHROMA_PART) {
const int palette_mode_ctx = av1_get_palette_mode_ctx(xd);
const int modev = aom_read_symbol(
r, xd->tile_ctx->palette_y_mode_cdf[bsize_ctx][palette_mode_ctx], 2,
ACCT_INFO("modev", "luma"));
if (modev) {
pmi->palette_size[0] =
aom_read_symbol(r, xd->tile_ctx->palette_y_size_cdf[bsize_ctx],
PALETTE_SIZES, ACCT_INFO("palette_size", "luma")) +
2;
read_palette_colors_y(xd, cm->seq_params.bit_depth, pmi, r);
}
}
if (num_planes > 1 && xd->tree_type != LUMA_PART &&
mbmi->uv_mode == UV_DC_PRED && xd->is_chroma_ref) {
const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
const int modev = aom_read_symbol(
r, xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2,
ACCT_INFO("modev", "chroma"));
if (modev) {
pmi->palette_size[1] =
aom_read_symbol(r, xd->tile_ctx->palette_uv_size_cdf[bsize_ctx],
PALETTE_SIZES, ACCT_INFO("palette_size", "chroma")) +
2;
read_palette_colors_uv(xd, cm->seq_params.bit_depth, pmi, r);
}
}
}
#if !CONFIG_AIMC
static int read_angle_delta(aom_reader *r, aom_cdf_prob *cdf) {
const int sym = aom_read_symbol(r, cdf, 2 * MAX_ANGLE_DELTA + 1, ACCT_INFO());
return sym - MAX_ANGLE_DELTA;
}
#endif // !CONFIG_AIMC
static void read_filter_intra_mode_info(const AV1_COMMON *const cm,
MACROBLOCKD *const xd, aom_reader *r) {
MB_MODE_INFO *const mbmi = xd->mi[0];
FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
&mbmi->filter_intra_mode_info;
if (av1_filter_intra_allowed(cm, mbmi
#if CONFIG_LOSSLESS_DPCM
,
xd
#endif
) &&
xd->tree_type != CHROMA_PART) {
filter_intra_mode_info->use_filter_intra =
#if CONFIG_D149_CTX_MODELING_OPT
aom_read_symbol(r, xd->tile_ctx->filter_intra_cdfs, 2,
ACCT_INFO("use_filter_intra"));
#else
aom_read_symbol(
r, xd->tile_ctx->filter_intra_cdfs[mbmi->sb_type[PLANE_TYPE_Y]], 2,
ACCT_INFO("use_filter_intra"));
#endif // CONFIG_D149_CTX_MODELING_OPT
if (filter_intra_mode_info->use_filter_intra) {
filter_intra_mode_info->filter_intra_mode =
aom_read_symbol(r, xd->tile_ctx->filter_intra_mode_cdf,
FILTER_INTRA_MODES, ACCT_INFO("filter_intra_mode"));
}
} else {
filter_intra_mode_info->use_filter_intra = 0;
}
}
void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd, int blk_row,
int blk_col, TX_SIZE tx_size, aom_reader *r,
const int plane, const int eob, const int dc_skip) {
if (plane != PLANE_TYPE_Y) return;
MB_MODE_INFO *mbmi = xd->mi[0];
TX_TYPE *tx_type =
&xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col];
*tx_type = DCT_DCT;
if (dc_skip == 1) return;
// No need to read transform type if block is skipped.
if (mbmi->skip_txfm[xd->tree_type == CHROMA_PART] ||
segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
return;
// No need to read transform type for lossless mode(qindex==0).
const int qindex = xd->qindex[mbmi->segment_id];
if (qindex == 0) return;
const int inter_block = is_inter_block(mbmi, xd->tree_type);
if (get_ext_tx_types(tx_size, inter_block, cm->features.reduced_tx_set_used) >
1) {
const TxSetType tx_set_type = av1_get_ext_tx_set_type(
tx_size, inter_block, cm->features.reduced_tx_set_used);
const int eset =
get_ext_tx_set(tx_size, inter_block, cm->features.reduced_tx_set_used);
// eset == 0 should correspond to a set with only DCT_DCT and
// there is no need to read the tx_type
assert(eset != 0);
const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
if (inter_block) {
const int eob_tx_ctx = get_lp2tx_ctx(tx_size, get_txb_bwl(tx_size), eob);
*tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
r, ec_ctx->inter_ext_tx_cdf[eset][eob_tx_ctx][square_tx_size],
av1_num_ext_tx_set[tx_set_type], ACCT_INFO("tx_type"))];
} else {
if (mbmi->fsc_mode[xd->tree_type == CHROMA_PART]) {
*tx_type = IDTX;
return;
}
const PREDICTION_MODE intra_mode =
mbmi->filter_intra_mode_info.use_filter_intra
? fimode_to_intradir[mbmi->filter_intra_mode_info
.filter_intra_mode]
: get_intra_mode(mbmi, PLANE_TYPE_Y);
const int size_info = av1_size_class[tx_size];
#if CONFIG_INTRA_TX_IST_PARSE
*tx_type = av1_tx_idx_to_type(
aom_read_symbol(
r,
ec_ctx->intra_ext_tx_cdf[eset + cm->features.reduced_tx_set_used]
[square_tx_size],
cm->features.reduced_tx_set_used
? av1_num_reduced_tx_set
: av1_num_ext_tx_set_intra[tx_set_type],
ACCT_INFO("tx_type")),
tx_set_type, intra_mode, size_info);
#else
*tx_type = av1_tx_idx_to_type(
aom_read_symbol(
r,
ec_ctx->intra_ext_tx_cdf[eset + cm->features.reduced_tx_set_used]
[square_tx_size][intra_mode],
cm->features.reduced_tx_set_used
? av1_num_reduced_tx_set
: av1_num_ext_tx_set_intra[tx_set_type],
ACCT_INFO("tx_type")),
tx_set_type, intra_mode, size_info);
#endif // CONFIG_INTRA_TX_IST_PARSE
}
}
}
void av1_read_cctx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
int blk_row, int blk_col, TX_SIZE tx_size,
aom_reader *r) {
MB_MODE_INFO *mbmi = xd->mi[0];
// If it is a sub 8x8 chroma block, derive the mi_row and mi_col of the
// parent block area. Then apply cctx type update to this area w.r.t the
// offsets derived
int row_offset, col_offset;
#if CONFIG_EXT_RECUR_PARTITIONS
get_chroma_mi_offsets(xd, &row_offset, &col_offset);
#else
get_chroma_mi_offsets(xd, tx_size, &row_offset, &col_offset);
#endif // CONFIG_EXT_RECUR_PARTITIONS
update_cctx_array(xd, blk_row, blk_col, row_offset, col_offset, tx_size,
CCTX_NONE);
// No need to read transform type if block is skipped.
if (mbmi->skip_txfm[xd->tree_type == CHROMA_PART] ||
segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
return;
// No need to read transform type for lossless mode(qindex==0).
const int qindex = xd->qindex[mbmi->segment_id];
if (qindex == 0) return;
CctxType cctx_type = CCTX_NONE;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
int above_cctx, left_cctx;
#if CONFIG_EXT_RECUR_PARTITIONS
get_above_and_left_cctx_type(cm, xd, &above_cctx, &left_cctx);
#else
get_above_and_left_cctx_type(cm, xd, tx_size, &above_cctx, &left_cctx);
#endif // CONFIG_EXT_RECUR_PARTITIONS
const int cctx_ctx = get_cctx_context(xd, &above_cctx, &left_cctx);
cctx_type =
aom_read_symbol(r, ec_ctx->cctx_type_cdf[square_tx_size][cctx_ctx],
CCTX_TYPES, ACCT_INFO("cctx_type"));
update_cctx_array(xd, blk_row, blk_col, row_offset, col_offset, tx_size,
cctx_type);
}
// This function reads a 'secondary tx set' from the bitstream
#if CONFIG_INTER_IST
static void read_secondary_tx_set(MACROBLOCKD *xd, FRAME_CONTEXT *ec_ctx,
aom_reader *r, MB_MODE_INFO *mbmi,
TX_TYPE *tx_type) {
const int inter_block = is_inter_block(mbmi, xd->tree_type);
TX_TYPE stx_set_flag = DC_PRED;
if (!inter_block) {
uint8_t intra_mode = get_intra_mode(mbmi, AOM_PLANE_Y);
#if CONFIG_INTRA_TX_IST_PARSE
const TX_TYPE reordered_stx_set_flag =
aom_read_symbol(r, ec_ctx->most_probable_stx_set_cdf, IST_DIR_SIZE,
ACCT_INFO("stx_set_flag"));
stx_set_flag =
inv_most_probable_stx_mapping[intra_mode][reordered_stx_set_flag];
#else
uint8_t stx_set_ctx = stx_transpose_mapping[intra_mode];
assert(stx_set_ctx < IST_DIR_SIZE);
stx_set_flag = aom_read_symbol(r, ec_ctx->stx_set_cdf[stx_set_ctx],
IST_DIR_SIZE, ACCT_INFO("stx_set_flag"));
#endif // CONFIG_INTRA_TX_IST_PARSE
assert(stx_set_flag < IST_DIR_SIZE);
}
if (get_primary_tx_type(*tx_type) == ADST_ADST) stx_set_flag += IST_DIR_SIZE;
set_secondary_tx_set(tx_type, stx_set_flag);
}
#else
static void read_secondary_tx_set(FRAME_CONTEXT *ec_ctx, aom_reader *r,
MB_MODE_INFO *mbmi, TX_TYPE *tx_type) {
uint8_t intra_mode = get_intra_mode(mbmi, PLANE_TYPE_Y);
#if CONFIG_INTRA_TX_IST_PARSE
const TX_TYPE reordered_stx_set_flag =
aom_read_symbol(r, ec_ctx->most_probable_stx_set_cdf, IST_DIR_SIZE,
ACCT_INFO("stx_set_flag"));
TX_TYPE stx_set_flag =
inv_most_probable_stx_mapping[intra_mode][reordered_stx_set_flag];
#else
uint8_t stx_set_ctx = stx_transpose_mapping[intra_mode];
assert(stx_set_ctx < IST_DIR_SIZE);
TX_TYPE stx_set_flag =
aom_read_symbol(r, ec_ctx->stx_set_cdf[stx_set_ctx], IST_DIR_SIZE,
ACCT_INFO("stx_set_flag"));
#endif // CONFIG_INTRA_TX_IST_PARSE
assert(stx_set_flag < IST_DIR_SIZE);
if (get_primary_tx_type(*tx_type) == ADST_ADST) stx_set_flag += IST_DIR_SIZE;
set_secondary_tx_set(tx_type, stx_set_flag);
}
#endif // CONFIG_INTER_IST
void av1_read_sec_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
int blk_row, int blk_col, TX_SIZE tx_size,
uint16_t *eob, aom_reader *r) {
MB_MODE_INFO *mbmi = xd->mi[0];
TX_TYPE *tx_type =
&xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col];
// No need to read transform type if block is skipped.
if (mbmi->skip_txfm[xd->tree_type == CHROMA_PART] ||
segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
return;
// No need to read transform type for lossless mode(qindex==0).
const int qindex = xd->qindex[mbmi->segment_id];
if (qindex == 0) return;
const int inter_block = is_inter_block(mbmi, xd->tree_type);
if (get_ext_tx_types(tx_size, inter_block, cm->features.reduced_tx_set_used) >
1) {
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
#if !CONFIG_INTER_IST
if (!inter_block) {
#endif // CONFIG_INTER_IST
if (block_signals_sec_tx_type(xd, tx_size, *tx_type, *eob)) {
#if CONFIG_INTER_IST
const uint8_t stx_flag =
aom_read_symbol(r, ec_ctx->stx_cdf[inter_block][square_tx_size],
STX_TYPES, ACCT_INFO("stx_flag"));
#else
const uint8_t stx_flag = aom_read_symbol(
r, ec_ctx->stx_cdf[square_tx_size], STX_TYPES, ACCT_INFO("stx_flag"));
#endif // CONFIG_INTER_IST
*tx_type |= (stx_flag << 4);
#if CONFIG_IST_SET_FLAG
#if CONFIG_INTER_IST
if (stx_flag > 0) read_secondary_tx_set(xd, ec_ctx, r, mbmi, tx_type);
#else
if (stx_flag > 0) read_secondary_tx_set(ec_ctx, r, mbmi, tx_type);
#endif // CONFIG_INTER_IST
#endif // CONFIG_IST_SET_FLAG
}
#if !CONFIG_INTER_IST
}
#endif // CONFIG_INTER_IST
#if CONFIG_INTER_IST
} else {
#else
} else if (!inter_block) {
#endif // CONFIG_INTER_IST
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
if (block_signals_sec_tx_type(xd, tx_size, *tx_type, *eob)) {
#if CONFIG_INTER_IST
const uint8_t stx_flag =
aom_read_symbol(r, ec_ctx->stx_cdf[inter_block][square_tx_size],
STX_TYPES, ACCT_INFO("stx_flag"));
#else
const uint8_t stx_flag = aom_read_symbol(
r, ec_ctx->stx_cdf[square_tx_size], STX_TYPES, ACCT_INFO("stx_flag"));
#endif // // CONFIG_INTER_IST
*tx_type |= (stx_flag << 4);
#if CONFIG_IST_SET_FLAG
#if CONFIG_INTER_IST
if (stx_flag > 0) read_secondary_tx_set(xd, ec_ctx, r, mbmi, tx_type);
#else
if (stx_flag > 0) read_secondary_tx_set(ec_ctx, r, mbmi, tx_type);
#endif // CONFIG_INTER_IST
#endif // CONFIG_IST_SET_FLAG
}
}
}
#if !CONFIG_VQ_MVD_CODING
static INLINE void read_mv(aom_reader *r,
#if CONFIG_DERIVED_MVD_SIGN
MV *mv_diff,
#else
MV *mv, MV ref,
#endif // CONFIG_DERIVED_MVD_SIGN
int is_adaptive_mvd, nmv_context *ctx,
MvSubpelPrecision precision);
#else
static INLINE void read_mv(aom_reader *r, MV *mv_diff, int skip_sign_coding,
nmv_context *ctx, MvSubpelPrecision precision,
int is_adaptive_mvd
#if !CONFIG_DERIVED_MVD_SIGN
,
MV *mv, MV ref
#endif
);
#endif // !CONFIG_VQ_MVD_CODING
static INLINE int is_mv_valid(const MV *mv);
static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
const int_mv *ref_mv, int mi_row, int mi_col,
BLOCK_SIZE bsize, aom_reader *r) {
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
#if CONFIG_IBC_BV_IMPROVEMENT
const MB_MODE_INFO *const mbmi = xd->mi[0];
if (mbmi->intrabc_mode == 1) {
mv->as_int = ref_mv->as_int;
} else {
#endif // CONFIG_IBC_BV_IMPROVEMENT
#if CONFIG_DERIVED_MVD_SIGN || CONFIG_VQ_MVD_CODING
MV mv_diff = kZeroMv;
#endif
#if CONFIG_VQ_MVD_CODING
read_mv(r, &mv_diff, 1, &ec_ctx->ndvc, MV_PRECISION_ONE_PEL, 0
#if !CONFIG_DERIVED_MVD_SIGN
,
&mv->as_mv, ref_mv->as_mv
#endif
);
#else
read_mv(r,
#if CONFIG_DERIVED_MVD_SIGN
&mv_diff,
#else
&mv->as_mv, ref_mv->as_mv,
#endif // CONFIG_DERIVED_MVD_SIGN
0, &ec_ctx->ndvc, MV_PRECISION_ONE_PEL);
#endif // CONFIG_VQ_MVD_CODING
#if CONFIG_DERIVED_MVD_SIGN
// Encode sign
if (mv_diff.row) {
int sign = aom_read_symbol(r, ec_ctx->ndvc.comps[0].sign_cdf, 2,
ACCT_INFO("sign"));
if (sign) mv_diff.row = -mv_diff.row;
}
if (mv_diff.col) {
int sign = aom_read_symbol(r, ec_ctx->ndvc.comps[1].sign_cdf, 2,
ACCT_INFO("sign"));
if (sign) mv_diff.col = -mv_diff.col;
}
mv->as_mv.row = ref_mv->as_mv.row + mv_diff.row;
mv->as_mv.col = ref_mv->as_mv.col + mv_diff.col;
#endif // CONFIG_DERIVED_MVD_SIGN
#if CONFIG_IBC_BV_IMPROVEMENT
}
#endif // CONFIG_IBC_BV_IMPROVEMENT
// DV should not have sub-pel.
assert((mv->as_mv.col & 7) == 0);
assert((mv->as_mv.row & 7) == 0);
mv->as_mv.col = (mv->as_mv.col >> 3) * 8;
mv->as_mv.row = (mv->as_mv.row >> 3) * 8;
int valid = is_mv_valid(&mv->as_mv) &&
av1_is_dv_valid(mv->as_mv, cm, xd, mi_row, mi_col, bsize,
cm->mib_size_log2);
return valid;
}
#if CONFIG_IBC_BV_IMPROVEMENT
static void read_intrabc_drl_idx(int max_ref_bv_cnt, FRAME_CONTEXT *ec_ctx,
MB_MODE_INFO *mbmi, aom_reader *r) {
mbmi->intrabc_drl_idx = 0;
int bit_cnt = 0;
for (int idx = 0; idx < max_ref_bv_cnt - 1; ++idx) {
const int intrabc_drl_idx = aom_read_symbol(
r, ec_ctx->intrabc_drl_idx_cdf[bit_cnt], 2, ACCT_INFO());
mbmi->intrabc_drl_idx = idx + intrabc_drl_idx;
if (!intrabc_drl_idx) break;
++bit_cnt;
}
assert(mbmi->intrabc_drl_idx < max_ref_bv_cnt);
}
#endif // CONFIG_IBC_BV_IMPROVEMENT
static void read_intrabc_info(AV1_COMMON *const cm, DecoderCodingBlock *dcb,
aom_reader *r) {
MACROBLOCKD *const xd = &dcb->xd;
MB_MODE_INFO *const mbmi = xd->mi[0];
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
assert(xd->tree_type != CHROMA_PART);
#if !CONFIG_SKIP_TXFM_OPT
#if CONFIG_NEW_CONTEXT_MODELING
mbmi->use_intrabc[0] = 0;
mbmi->use_intrabc[1] = 0;
const int intrabc_ctx = get_intrabc_ctx(xd);
mbmi->use_intrabc[xd->tree_type == CHROMA_PART] =
aom_read_symbol(r, ec_ctx->intrabc_cdf[intrabc_ctx], 2, ACCT_INFO());
#else
mbmi->use_intrabc[xd->tree_type == CHROMA_PART] =
aom_read_symbol(r, ec_ctx->intrabc_cdf, 2, ACCT_INFO());
#endif // CONFIG_NEW_CONTEXT_MODELING
#endif // !CONFIG_SKIP_TXFM_OPT
if (xd->tree_type == CHROMA_PART)
assert(mbmi->use_intrabc[PLANE_TYPE_UV] == 0);
if (mbmi->use_intrabc[xd->tree_type == CHROMA_PART]) {
BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART];
mbmi->mode = DC_PRED;
mbmi->fsc_mode[PLANE_TYPE_Y] = 0;
mbmi->fsc_mode[PLANE_TYPE_UV] = 0;
mbmi->uv_mode = UV_DC_PRED;
mbmi->interp_fltr = BILINEAR;
mbmi->motion_mode = SIMPLE_TRANSLATION;
// CHECK(cm->features.fr_mv_precision != MV_PRECISION_ONE_PEL, "
// fr_mv_precision is not same as MV_PRECISION_ONE_PEL for intra-bc
// blocks");
set_default_max_mv_precision(mbmi, xd->sbi->sb_mv_precision);
set_mv_precision(mbmi, MV_PRECISION_ONE_PEL);
set_default_precision_set(cm, mbmi, bsize);
set_most_probable_mv_precision(cm, mbmi, bsize);
#if CONFIG_REFINEMV
mbmi->refinemv_flag = 0;
#endif // CONFIG_REFINEMV
#if CONFIG_BAWP
#if CONFIG_BAWP_CHROMA
for (int plane = 0; plane < 2; ++plane) {
mbmi->bawp_flag[plane] = 0;
}
#else
mbmi->bawp_flag = 0;
#endif // CONFIG_BAWP_CHROMA
#endif
#if !CONFIG_C076_INTER_MOD_CTX
int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
#endif // !CONFIG_C076_INTER_MOD_CTX
// TODO(kslu): Rework av1_find_mv_refs to avoid having this big array
// ref_mvs
int_mv ref_mvs[INTRA_FRAME + 1][MAX_MV_REF_CANDIDATES];
#if CONFIG_IBC_BV_IMPROVEMENT
#if CONFIG_IBC_MAX_DRL
for (int i = 0; i < cm->features.max_bvp_drl_bits + 1; ++i) {
#else
for (int i = 0; i < MAX_REF_BV_STACK_SIZE; ++i) {
#endif // CONFIG_IBC_MAX_DRL
xd->ref_mv_stack[INTRA_FRAME][i].this_mv.as_int = 0;
xd->ref_mv_stack[INTRA_FRAME][i].comp_mv.as_int = 0;
#if CONFIG_EXTENDED_WARP_PREDICTION
xd->ref_mv_stack[INTRA_FRAME][i].row_offset = OFFSET_NONSPATIAL;
xd->ref_mv_stack[INTRA_FRAME][i].col_offset = OFFSET_NONSPATIAL;
#endif // CONFIG_EXTENDED_WARP_PREDICTION
xd->ref_mv_stack[INTRA_FRAME][i].cwp_idx = CWP_EQUAL;
}
#endif // CONFIG_IBC_BV_IMPROVEMENT
av1_find_mv_refs(cm, xd, mbmi, INTRA_FRAME, dcb->ref_mv_count,
xd->ref_mv_stack, xd->weight, ref_mvs, /*global_mvs=*/NULL
#if !CONFIG_C076_INTER_MOD_CTX
,
inter_mode_ctx
#endif // !CONFIG_C076_INTER_MOD_CTX
#if CONFIG_EXTENDED_WARP_PREDICTION
,
NULL, 0, NULL
#endif // CONFIG_EXTENDED_WARP_PREDICTION
);
#if CONFIG_IBC_BV_IMPROVEMENT
mbmi->intrabc_mode =
aom_read_symbol(r, ec_ctx->intrabc_mode_cdf, 2, ACCT_INFO());
#if CONFIG_IBC_MAX_DRL
read_intrabc_drl_idx(cm->features.max_bvp_drl_bits + 1, ec_ctx, mbmi, r);
#else
read_intrabc_drl_idx(MAX_REF_BV_STACK_SIZE, ec_ctx, mbmi, r);
#endif // CONFIG_IBC_MAX_DRL
int_mv dv_ref =
xd->ref_mv_stack[INTRA_FRAME][mbmi->intrabc_drl_idx].this_mv;
#else
int_mv nearestmv, nearmv;
av1_find_best_ref_mvs(ref_mvs[INTRA_FRAME], &nearestmv, &nearmv,
mbmi->pb_mv_precision);
assert(cm->features.fr_mv_precision == MV_PRECISION_ONE_PEL &&
mbmi->max_mv_precision == MV_PRECISION_ONE_PEL);
int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
#endif // CONFIG_IBC_BV_IMPROVEMENT
if (dv_ref.as_int == 0)
av1_find_ref_dv(&dv_ref, &xd->tile, cm->mib_size, xd->mi_row);
// Ref DV should not have sub-pel.
int valid_dv = (dv_ref.as_mv.col & 7) == 0 && (dv_ref.as_mv.row & 7) == 0;
dv_ref.as_mv.col = (dv_ref.as_mv.col >> 3) * 8;
dv_ref.as_mv.row = (dv_ref.as_mv.row >> 3) * 8;
valid_dv = valid_dv && assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, xd->mi_row,
xd->mi_col, bsize, r);
if (!valid_dv) {
// Intra bc motion vectors are not valid - signal corrupt frame
aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
"Invalid intrabc dv");
}
#if CONFIG_MORPH_PRED
const int morph_pred_ctx = get_morph_pred_ctx(xd);
mbmi->morph_pred = aom_read_symbol(
r, ec_ctx->morph_pred_cdf[morph_pred_ctx], 2, ACCT_INFO());
#endif // CONFIG_MORPH_PRED
}
}
// If delta q is present, reads delta_q index.
// Also reads delta_q loop filter levels, if present.
static void read_delta_q_params(AV1_COMMON *const cm, MACROBLOCKD *const xd,
aom_reader *r) {
DeltaQInfo *const delta_q_info = &cm->delta_q_info;
if (delta_q_info->delta_q_present_flag) {
MB_MODE_INFO *const mbmi = xd->mi[0];
xd->current_base_qindex +=
read_delta_qindex(cm, xd, r, mbmi) * delta_q_info->delta_q_res;
/* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
xd->current_base_qindex =
clamp(xd->current_base_qindex, 1,
cm->seq_params.bit_depth == AOM_BITS_8 ? MAXQ_8_BITS
: cm->seq_params.bit_depth == AOM_BITS_10 ? MAXQ_10_BITS
: MAXQ);
FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
if (delta_q_info->delta_lf_present_flag) {
const int mi_row = xd->mi_row;
const int mi_col = xd->mi_col;
if (delta_q_info->delta_lf_multi) {
const int frame_lf_count =
av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
const int tmp_lvl =
xd->delta_lf[lf_id] +
read_delta_lflevel(cm, r, ec_ctx->delta_lf_multi_cdf[lf_id], mbmi,
mi_col, mi_row, xd->tree_type) *
delta_q_info->delta_lf_res;
mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id] =
clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
}
} else {
const int tmp_lvl =
xd->delta_lf_from_base +
read_delta_lflevel(cm, r, ec_ctx->delta_lf_cdf, mbmi, mi_col,
mi_row, xd->tree_type) *
delta_q_info->delta_lf_res;
mbmi->delta_lf_from_base = xd->delta_lf_from_base =
clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
}
}
}
}
#if CONFIG_AIMC
// read mode set index and mode index in set for y component,
// and map it to y mode and delta angle
static void read_intra_luma_mode(MACROBLOCKD *const xd, aom_reader *r) {
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
MB_MODE_INFO *const mbmi = xd->mi[0];
uint8_t mode_idx = 0;
const int context = get_y_mode_idx_ctx(xd);
int mode_set_index =
aom_read_symbol(r, ec_ctx->y_mode_set_cdf, INTRA_MODE_SETS,
ACCT_INFO("mode_set_index", "y_mode_set_cdf"));
if (mode_set_index == 0) {
mode_idx =
aom_read_symbol(r, ec_ctx->y_mode_idx_cdf_0[context], FIRST_MODE_COUNT,
ACCT_INFO("mode_idx", "y_mode_idx_cdf_0"));
} else {
mode_idx =
FIRST_MODE_COUNT + (mode_set_index - 1) * SECOND_MODE_COUNT +
aom_read_symbol(r, ec_ctx->y_mode_idx_cdf_1[context], SECOND_MODE_COUNT,
ACCT_INFO("mode_idx", "y_mode_idx_cdf_1"));
}
assert(mode_idx < LUMA_MODE_COUNT);
get_y_intra_mode_set(mbmi, xd);
mbmi->joint_y_mode_delta_angle = mbmi->y_intra_mode_list[mode_idx];
set_y_mode_and_delta_angle(mbmi->joint_y_mode_delta_angle, mbmi);
mbmi->y_mode_idx = mode_idx;
if (mbmi->joint_y_mode_delta_angle < NON_DIRECTIONAL_MODES_COUNT)
assert(mbmi->joint_y_mode_delta_angle == mbmi->y_mode_idx);
}
#if CONFIG_UV_CFL
// Read mode index for uv component and map it to uv mode and delta angle.
// First we read if the uv mode is UV_CFL_PRED.
// If yes, uv mode is set to UV_CFL_PRED, delta angle is set to 0. Done.
// If not, we read mode index and map it to uv mode and delta angle.
static void read_intra_uv_mode(MACROBLOCKD *const xd,
CFL_ALLOWED_TYPE cfl_allowed, aom_reader *r) {
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
MB_MODE_INFO *const mbmi = xd->mi[0];
int is_cfl_mode = 0;
if (cfl_allowed) {
const int cfl_ctx = get_cfl_ctx(xd);
is_cfl_mode = aom_read_symbol(r, ec_ctx->cfl_cdf[cfl_ctx], 2,
ACCT_INFO("is_cfl_idx"));
}
if (is_cfl_mode) {
mbmi->uv_mode = UV_CFL_PRED;
mbmi->angle_delta[PLANE_TYPE_UV] = 0;
return;
}
const int context = av1_is_directional_mode(mbmi->mode) ? 1 : 0;
const int uv_mode_idx =
aom_read_symbol(r, ec_ctx->uv_mode_cdf[context], UV_INTRA_MODES - 1,
ACCT_INFO("uv_mode_idx"));
assert(uv_mode_idx >= 0 && uv_mode_idx < UV_INTRA_MODES);
get_uv_intra_mode_set(mbmi);
mbmi->uv_mode = mbmi->uv_intra_mode_list[uv_mode_idx];
if (mbmi->uv_mode == mbmi->mode)
mbmi->angle_delta[PLANE_TYPE_UV] = mbmi->angle_delta[PLANE_TYPE_Y];
else
mbmi->angle_delta[PLANE_TYPE_UV] = 0;
}
#else
// read mode index for uv component and map it to uv mode and delta angle
static void read_intra_uv_mode(MACROBLOCKD *const xd,
CFL_ALLOWED_TYPE cfl_allowed, aom_reader *r) {
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
MB_MODE_INFO *const mbmi = xd->mi[0];
const int context = av1_is_directional_mode(mbmi->mode) ? 1 : 0;
const int uv_mode_idx =
aom_read_symbol(r, ec_ctx->uv_mode_cdf[cfl_allowed][context],
UV_INTRA_MODES - !cfl_allowed, ACCT_INFO("uv_mode_idx"));
assert(uv_mode_idx >= 0 && uv_mode_idx < UV_INTRA_MODES);
get_uv_intra_mode_set(mbmi);
mbmi->uv_mode = mbmi->uv_intra_mode_list[uv_mode_idx];
if (mbmi->uv_mode == mbmi->mode)
mbmi->angle_delta[PLANE_TYPE_UV] = mbmi->angle_delta[PLANE_TYPE_Y];
else
mbmi->angle_delta[PLANE_TYPE_UV] = 0;
}
#endif // CONFIG_UV_CFL
#endif // CONFIG_AIMC
static void read_intra_frame_mode_info(AV1_COMMON *const cm,
DecoderCodingBlock *dcb, aom_reader *r) {
MACROBLOCKD *const xd = &dcb->xd;
MB_MODE_INFO *const mbmi = xd->mi[0];
const BLOCK_SIZE bsize = mbmi->sb_type[xd->tree_type == CHROMA_PART];
struct segmentation *const seg = &cm->seg;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
if (seg->segid_preskip
#if CONFIG_EXTENDED_SDP
&& !(!frame_is_intra_only(cm) && xd->tree_type == CHROMA_PART)
#endif // CONFIG_EXTENDED_SDP
)
mbmi->segment_id = read_intra_segment_id(cm, xd, bsize, r, 0);
#if CONFIG_SKIP_MODE_ENHANCEMENT
mbmi->skip_mode = 0;
#endif // CONFIG_SKIP_MODE_ENHANCEMENT
#if CONFIG_MORPH_PRED
mbmi->morph_pred = 0;
#endif // CONFIG_MORPH_PRED
#if CONFIG_SKIP_TXFM_OPT
if (av1_allow_intrabc(cm, xd) && xd->tree_type != CHROMA_PART) {
#if CONFIG_NEW_CONTEXT_MODELING
mbmi->use_intrabc[0] = 0;
mbmi->use_intrabc[1] = 0;
const int intrabc_ctx = get_intrabc_ctx(xd);
mbmi->use_intrabc[xd->tree_type == CHROMA_PART] =
aom_read_symbol(r, ec_ctx->intrabc_cdf[intrabc_ctx], 2,
ACCT_INFO("use_intrabc", "chroma"));
#else
mbmi->use_intrabc[xd->tree_type == CHROMA_PART] = aom_read_symbol(
r, ec_ctx->intrabc_cdf, 2, ACCT_INFO("use_intrabc", "chroma"));
#endif // CONFIG_NEW_CONTEXT_MODELING
}
if (is_intrabc_block(mbmi, xd->tree_type)) {
mbmi->skip_txfm[xd->tree_type == CHROMA_PART] =
read_skip_txfm(cm, xd, mbmi->segment_id, r);
} else {
mbmi->skip_txfm[xd->tree_type == CHROMA_PART] = 0;
}
#else
mbmi->skip_txfm[xd->tree_type == CHROMA_PART] =
read_skip_txfm(cm, xd, mbmi->segment_id, r);
#endif // CONFIG_SKIP_TXFM_OPT
if (!seg->segid_preskip
#if CONFIG_EXTENDED_SDP
&& !(!frame_is_intra_only(cm) && xd->tree_type == CHROMA_PART)
#endif // CONFIG_EXTENDED_SDP
)
mbmi->segment_id = read_intra_segment_id(
cm, xd, bsize, r, mbmi->skip_txfm[xd->tree_type == CHROMA_PART]);
#if CONFIG_EXTENDED_SDP
mbmi->seg_id_predicted = 0;
#endif // CONFIG_EXTENDED_SDP
if (xd->tree_type != CHROMA_PART) read_cdef(cm, r, xd);
#if CONFIG_CCSO
if (cm->seq_params.enable_ccso
#if CONFIG_CCSO_EXT
&& xd->tree_type != CHROMA_PART
#else
&& xd->tree_type != LUMA_PART
#endif
)
read_ccso(cm, r, xd);
#endif
read_delta_q_params(cm, xd, r);
mbmi->current_qindex = xd->current_base_qindex;
mbmi->ref_frame[0] = INTRA_FRAME;
mbmi->ref_frame[1] = NONE_FRAME;
if (xd->tree_type != CHROMA_PART) mbmi->palette_mode_info.palette_size[0] = 0;
mbmi->palette_mode_info.palette_size[1] = 0;
if (xd->tree_type != CHROMA_PART)
mbmi->filter_intra_mode_info.use_filter_intra = 0;
#if !CONFIG_TX_PARTITION_CTX
const int mi_row = xd->mi_row;
const int mi_col = xd->mi_col;
xd->above_txfm_context = cm->above_contexts.txfm[xd->tile.tile_row] + mi_col;
xd->left_txfm_context =
xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
#endif // !CONFIG_TX_PARTITION_CTX
if (av1_allow_intrabc(cm, xd) && xd->tree_type != CHROMA_PART) {
read_intrabc_info(cm, dcb, r);
if (is_intrabc_block(mbmi, xd->tree_type)) {
#if CONFIG_LOSSLESS_DPCM
mbmi->use_dpcm_y = 0;
mbmi->dpcm_mode_y = 0;
#endif // CONFIG_LOSSLESS_DPCM
return;
}
}
#if !CONFIG_AIMC
const int use_angle_delta = av1_use_angle_delta(bsize);
#endif // !CONFIG_AIMC
if (xd->tree_type != CHROMA_PART) {
#if CONFIG_AIMC
#if CONFIG_LOSSLESS_DPCM
if (xd->lossless[mbmi->segment_id]) {
mbmi->use_dpcm_y = read_dpcm_mode(ec_ctx, r);
if (mbmi->use_dpcm_y == 0) {
read_intra_luma_mode(xd, r);
} else {
mbmi->dpcm_mode_y = read_dpcm_vert_horz_mode(ec_ctx, r);
if (mbmi->dpcm_mode_y == 0) {
mbmi->joint_y_mode_delta_angle = 22;
mbmi->mode = V_PRED;
mbmi->angle_delta[0] = 0;
} else {
mbmi->joint_y_mode_delta_angle = 50;
mbmi->mode = H_PRED;
mbmi->angle_delta[0] = 0;
}
}
} else {
mbmi->use_dpcm_y = 0;
mbmi->dpcm_mode_y = 0;
read_intra_luma_mode(xd, r);
}
#else // CONFIG_LOSSLESS_DPCM
read_intra_luma_mode(xd, r);
#endif // CONFIG_LOSSLESS_DPCM
if (allow_fsc_intra(cm,
#if !CONFIG_LOSSLESS_DPCM
xd,
#endif // CONFIG_LOSSLESS_DPCM
bsize, mbmi)) {
aom_cdf_prob *fsc_cdf = get_fsc_mode_cdf(xd, bsize, 1);
mbmi->fsc_mode[xd->tree_type == CHROMA_PART] = read_fsc_mode(r, fsc_cdf);
} else {
mbmi->fsc_mode[xd->tree_type == CHROMA_PART] = 0;
}
#else
mbmi->mode = read_intra_mode(
r, get_y_mode_cdf(ec_ctx, xd->neighbors[0], xd->neighbors[1]));
if (allow_fsc_intra(cm,
#if !CONFIG_LOSSLESS_DPCM
xd,
#endif // CONFIG_LOSSLESS_DPCM
bsize, mbmi)) {
aom_cdf_prob *fsc_cdf = get_fsc_mode_cdf(xd, bsize, 1);
mbmi->fsc_mode[xd->tree_type == CHROMA_PART] = read_fsc_mode(r, fsc_cdf);
} else {
mbmi->fsc_mode[xd->tree_type == CHROMA_PART] = 0;
}
mbmi->angle_delta[PLANE_TYPE_Y] =
(use_angle_delta && av1_is_directional_mode(mbmi->mode))
? read_angle_delta(
r, ec_ctx->angle_delta_cdf[PLANE_TYPE_Y][mbmi->mode - V_PRED])
: 0;
#endif // CONFIG_AIMC
#if CONFIG_LOSSLESS_DPCM
if (xd->lossless[mbmi->segment_id]) {
if (mbmi->use_dpcm_y == 0) {
mbmi->mrl_index =
(cm->seq_params.enable_mrls && av1_is_directional_mode(mbmi->mode))
#if CONFIG_IMPROVED_INTRA_DIR_PRED
? read_mrl_index(ec_ctx, r, xd->neighbors[0], xd->neighbors[1])
#else
? read_mrl_index(ec_ctx, r)
#endif // CONFIG_IMPROVED_INTRA_DIR_PRED
: 0;
} else {
mbmi->mrl_index = 0;
}
} else {
mbmi->mrl_index =
(cm->seq_params.enable_mrls && av1_is_directional_mode(mbmi->mode))
#if CONFIG_IMPROVED_INTRA_DIR_PRED
? read_mrl_index(ec_ctx, r, xd->neighbors[0], xd->neighbors[1])
#else
? read_mrl_index(ec_ctx, r)
#endif // CONFIG_IMPROVED_INTRA_DIR_PRED
: 0;
}
#else // CONFIG_LOSSLESS_DPCM
mbmi->mrl_index =
(cm->seq_params.enable_mrls && av1_is_directional_mode(mbmi->mode))
#if CONFIG_IMPROVED_INTRA_DIR_PRED
? read_mrl_index(ec_ctx, r, xd->neighbors[0], xd->neighbors[1])
#else
? read_mrl_index(ec_ctx, r)
#endif // CONFIG_IMPROVED_INTRA_DIR_PRED
: 0;
#endif // CONFIG_LOSSLESS_DPCM
}
if (xd->tree_type != LUMA_PART) {
if (!cm->seq_params.monochrome && xd->is_chroma_ref) {
#if CONFIG_AIMC
#if CONFIG_LOSSLESS_DPCM
if (xd->lossless[mbmi->segment_id]) {
mbmi->use_dpcm_uv = read_dpcm_uv_mode(ec_ctx, r);
if (mbmi->use_dpcm_uv == 0) {
read_intra_uv_mode(xd, is_cfl_allowed(xd), r);
mbmi->dpcm_mode_uv = 0;
} else {
get_uv_intra_mode_set(mbmi);
mbmi->dpcm_mode_uv = read_dpcm_uv_vert_horz_mode(ec_ctx, r);
mbmi->uv_mode = mbmi->dpcm_mode_uv + 1;
if (mbmi->uv_mode == mbmi->mode)
mbmi->angle_delta[PLANE_TYPE_UV] = mbmi->angle_delta[PLANE_TYPE_Y];
else
mbmi->angle_delta[PLANE_TYPE_UV] = 0;
}
} else {
read_intra_uv_mode(xd, is_cfl_allowed(xd), r);
mbmi->use_dpcm_uv = 0;
mbmi->dpcm_mode_uv = 0;
}
#else // CONFIG_LOSSLESS_DPCM
read_intra_uv_mode(xd, is_cfl_allowed(xd), r);
#endif // CONFIG_LOSSLESS_DPCM
#else
mbmi->uv_mode =
read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode);
if (cm->seq_params.enable_sdp) {
mbmi->angle_delta[PLANE_TYPE_UV] =
(use_angle_delta &&
av1_is_directional_mode(get_uv_mode(mbmi->uv_mode)))
? read_angle_delta(
r, ec_ctx->angle_delta_cdf[PLANE_TYPE_UV]
[mbmi->uv_mode - V_PRED])
: 0;
} else {
mbmi->angle_delta[PLANE_TYPE_UV] =
(use_angle_delta &&
av1_is_directional_mode(get_uv_mode(mbmi->uv_mode)))
? read_angle_delta(
r, ec_ctx->angle_delta_cdf[PLANE_TYPE_Y]
[mbmi->uv_mode - V_PRED])
: 0;
}
#endif // CONFIG_AIMC
if (mbmi->uv_mode == UV_CFL_PRED) {
#if CONFIG_IMPROVED_CFL
{
mbmi->cfl_idx = read_cfl_index(ec_ctx, r);
#if CONFIG_ENABLE_MHCCP
if (mbmi->cfl_idx == CFL_MULTI_PARAM_V) {
const uint8_t mh_size_group = fsc_bsize_groups[bsize];
#if CONFIG_CFL_64x64
assert(mh_size_group < MHCCP_CONTEXT_GROUP_SIZE);
#else
assert(mh_size_group < FSC_BSIZE_CONTEXTS);
#endif // CONFIG_CFL_64x64
aom_cdf_prob *mh_dir_cdf = ec_ctx->filter_dir_cdf[mh_size_group];
mbmi->mh_dir = read_mh_dir(mh_dir_cdf, r);
}
#endif // CONFIG_ENABLE_MHCCP
}
if (mbmi->cfl_idx == 0)
#endif
mbmi->cfl_alpha_idx =
read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
}
} else {
// Avoid decoding angle_info if there is is no chroma prediction
mbmi->uv_mode = UV_DC_PRED;
}
xd->cfl.store_y = store_cfl_required(cm, xd);
} else {
// Avoid decoding angle_info if there is is no chroma prediction
mbmi->uv_mode = UV_DC_PRED;
}
if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize))
read_palette_mode_info(cm, xd, r);
if (xd->tree_type != CHROMA_PART) read_filter_intra_mode_info(cm, xd, r);
}
#if !CONFIG_VQ_MVD_CODING
// Read the MVD for the lower precision
// this function is executed when the precision is less than integer pixel
// precision
static int read_mv_component_low_precision(aom_reader *r, nmv_component *mvcomp,
MvSubpelPrecision precision) {
int offset, mag;
#if CONFIG_DERIVED_MVD_SIGN
const int sign = 0;
#else
const int sign = aom_read_symbol(r, mvcomp->sign_cdf, 2, ACCT_INFO("sign"));
#endif // CONFIG_DERIVED_MVD_SIGN
const int num_mv_classes = MV_CLASSES - (precision <= MV_PRECISION_FOUR_PEL) -
(precision <= MV_PRECISION_8_PEL);
int mv_class = aom_read_symbol(
r, mvcomp->classes_cdf[av1_get_mv_class_context(precision)],
num_mv_classes, ACCT_INFO("mv_class"));
if (precision <= MV_PRECISION_FOUR_PEL && mv_class >= MV_CLASS_1)
mv_class += (precision == MV_PRECISION_FOUR_PEL ? 1 : 2);
int has_offset = (mv_class >= min_class_with_offset[precision]);
assert(MV_PRECISION_ONE_PEL >= precision);
const int precision_diff = MV_PRECISION_ONE_PEL - precision;
const uint8_t start_lsb = (precision_diff >= 0) ? (uint8_t)precision_diff : 0;
// Integer part
if (!has_offset) {
mag = mv_class ? (1 << mv_class) : 0; // int mv data
} else {
const int n = (mv_class == MV_CLASS_0) ? 1 : mv_class;
offset = 0;
for (int i = start_lsb; i < n; ++i)
offset |= aom_read_symbol(r, mvcomp->bits_cdf[i], 2, ACCT_INFO("offset"))
<< i;
const int base = mv_class ? (1 << mv_class) : 0;
mag = (offset + base); // int mv data
}
const int nonZero_offset = (1 << start_lsb);
mag = (mag + nonZero_offset) << 3;
return sign ? -mag : mag;
}
static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
int is_adaptive_mvd, MvSubpelPrecision precision) {
if (precision < MV_PRECISION_ONE_PEL) {
assert(!is_adaptive_mvd);
return read_mv_component_low_precision(r, mvcomp, precision);
}
int mag, d, fr, hp;
#if CONFIG_DERIVED_MVD_SIGN
const int sign = 0;
#else
const int sign = aom_read_symbol(r, mvcomp->sign_cdf, 2, ACCT_INFO("sign"));
#endif // CONFIG_DERIVED_MVD_SIGN
const int mv_class =
is_adaptive_mvd
? aom_read_symbol(r, mvcomp->amvd_classes_cdf, MV_CLASSES,
ACCT_INFO("mv_class", "amvd_classes_cdf"))
: aom_read_symbol(
r, mvcomp->classes_cdf[av1_get_mv_class_context(precision)],
MV_CLASSES, ACCT_INFO("mv_class", "classes_cdf"));
const int class0 = mv_class == MV_CLASS_0;
int use_mv_class_offset = 1;
if (mv_class > MV_CLASS_0 && is_adaptive_mvd) use_mv_class_offset = 0;
if (use_mv_class_offset) {
// Integer part
if (class0) {
d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE,
ACCT_INFO("class0_cdf"));
mag = 0;
} else {
const int n = mv_class + CLASS0_BITS - 1; // number of bits
d = 0;
for (int i = 0; i < n; ++i)
d |= aom_read_symbol(r, mvcomp->bits_cdf[i], 2, ACCT_INFO("bits_cdf"))
<< i;
mag = CLASS0_SIZE << (mv_class + 2);
}
} else {
const int n = mv_class + CLASS0_BITS - 1; // number of bits
d = 0;
for (int i = 0; i < n; ++i) d |= 1 << i;
mag = CLASS0_SIZE << (mv_class + 2);
}
int use_subpel = 1;
if (is_adaptive_mvd) {
use_subpel &= class0;
use_subpel &= (d == 0);
}
if (precision > MV_PRECISION_ONE_PEL && use_subpel) {
// Fractional part
// 1/2 and 1/4 pel parts
fr = aom_read_symbol(
r, class0 ? mvcomp->class0_fp_cdf[d][0] : mvcomp->fp_cdf[0], 2,
ACCT_INFO("class0_fp_cdf"))
<< 1;
fr +=
precision > MV_PRECISION_HALF_PEL
? aom_read_symbol(r,
class0 ? mvcomp->class0_fp_cdf[d][1 + (fr >> 1)]
: mvcomp->fp_cdf[1 + (fr >> 1)],
2, ACCT_INFO(class0 ? "class0_fp_cdf" : "fp_cdf"))
: 1;
// 1/8 pel part (if hp is not used, the default value of the hp is 1)
hp = (precision > MV_PRECISION_QTR_PEL)
? aom_read_symbol(
r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
ACCT_INFO(class0 ? "class0_hp_cdf" : "hp_cdf"))
: 1;
} else {
fr = 3;
hp = 1;
}
// Result
mag += ((d << 3) | (fr << 1) | hp) + 1;
return sign ? -mag : mag;
}
static INLINE void read_mv(aom_reader *r,
#if CONFIG_DERIVED_MVD_SIGN
MV *mv_diff,
#else
MV *mv, MV ref,
#endif // CONFIG_DERIVED_MVD_SIGN
int is_adaptive_mvd, nmv_context *ctx,
MvSubpelPrecision precision) {
MV diff = kZeroMv;
const MV_JOINT_TYPE joint_type =
is_adaptive_mvd ? (MV_JOINT_TYPE)aom_read_symbol(
r, ctx->amvd_joints_cdf, MV_JOINTS,
ACCT_INFO("joint_type", "amvd_joints_cdf"))
: (MV_JOINT_TYPE)aom_read_symbol(
r, ctx->joints_cdf, MV_JOINTS,
ACCT_INFO("joint_type", "joints_cdf"));
if (mv_joint_vertical(joint_type))
diff.row = read_mv_component(r, &ctx->comps[0], is_adaptive_mvd, precision);
if (mv_joint_horizontal(joint_type))
diff.col = read_mv_component(r, &ctx->comps[1], is_adaptive_mvd, precision);
#if CONFIG_DERIVED_MVD_SIGN
mv_diff->row = diff.row;
mv_diff->col = diff.col;
#else
#if BUGFIX_AMVD_AMVR
if (!is_adaptive_mvd)
#endif // BUGFIX_AMVD_AMVR
#if CONFIG_C071_SUBBLK_WARPMV
if (precision < MV_PRECISION_HALF_PEL)
#endif // CONFIG_C071_SUBBLK_WARPMV
lower_mv_precision(&ref, precision);
mv->row = ref.row + diff.row;
mv->col = ref.col + diff.col;
#endif // CONFIG_DERIVED_MVD_SIGN
}
#else
// Coding of col mvd for shell class 2
static void read_truncated_unary_mvd(aom_reader *r, nmv_context *ctx,
const int max_coded_value, int num_of_ctx,
int *decoded_value) {
int col = 0;
int max_idx_bits = max_coded_value;
for (int bit_idx = 0; bit_idx < max_idx_bits; ++bit_idx) {
int context_index = bit_idx < num_of_ctx ? bit_idx : num_of_ctx - 1;
assert(context_index < num_of_ctx);
aom_cdf_prob *cdf = ctx->shell_offset_class2_cdf[context_index];
int this_bit = aom_read_symbol(
r, cdf, 2, ACCT_INFO("greater_flags", "col_mv_greter_flags_cdf"));
col = bit_idx + this_bit;
if (!this_bit) break;
}
*decoded_value = col;
}
// Decoding of truncated unary followed by quasi uniform code
static void read_tu_quasi_uniform(aom_reader *r, nmv_context *ctx,
const int max_coded_value, int *scaled_mv_col,
int max_trunc_unary_value) {
int col = 0;
int max_idx_bits = AOMMIN(max_coded_value, max_trunc_unary_value);
int max_num_of_ctx = NUM_CTX_COL_MV_GTX;
for (int bit_idx = 0; bit_idx < max_idx_bits; ++bit_idx) {
int context_index = bit_idx < max_num_of_ctx ? bit_idx : max_num_of_ctx - 1;
assert(context_index < max_num_of_ctx);
int this_bit =
aom_read_symbol(r, ctx->col_mv_greter_flags_cdf[context_index], 2,
ACCT_INFO("greater_flags", "col_mv_greter_flags_cdf"));
col = bit_idx + this_bit;
if (!this_bit) break;
}
if (max_coded_value > max_trunc_unary_value && col == max_trunc_unary_value) {
int remainder_max_value = max_coded_value - max_trunc_unary_value;
int remainder = aom_read_primitive_quniform(r, remainder_max_value + 1,
ACCT_INFO("remainder"));
col = remainder + max_trunc_unary_value;
}
*scaled_mv_col = col;
}
// Read MVD for AMVD mode
static INLINE void read_vq_amvd(aom_reader *r, MV *mv_diff, nmv_context *ctx
#if !CONFIG_DERIVED_MVD_SIGN
,
MV *mv, MV ref
#endif
) {
MV diff_index = kZeroMv;
const MV_JOINT_TYPE joint_type = (MV_JOINT_TYPE)aom_read_symbol(
r, ctx->amvd_joints_cdf, MV_JOINTS,
ACCT_INFO("joint_type", "amvd_joints_cdf"));
int code_row = mv_joint_vertical(joint_type);
int code_col = mv_joint_horizontal(joint_type);
if (code_row) {
diff_index.row =
1 + aom_read_symbol(r, ctx->comps[0].amvd_indices_cdf, MAX_AMVD_INDEX,
ACCT_INFO("amvd_index", "amvd_indices_cdf"));
}
if (code_col) {
diff_index.col =
1 + aom_read_symbol(r, ctx->comps[1].amvd_indices_cdf, MAX_AMVD_INDEX,
ACCT_INFO("amvd_index", "amvd_indices_cdf"));
}
MV diff = { get_mvd_from_amvd_index(diff_index.row),
get_mvd_from_amvd_index(diff_index.col) };
mv_diff->row = diff.row;
mv_diff->col = diff.col;
#if !CONFIG_DERIVED_MVD_SIGN
// Decode signs
for (int component = 0; component < 2; component++) {
int value = component == 0 ? mv_diff->row : mv_diff->col;
if (value) {
int sign = aom_read_symbol(r, ctx->comps[component].sign_cdf, 2,
ACCT_INFO("sign"));
if (component == 0) {
mv_diff->row = sign ? -value : value;
} else {
mv_diff->col = sign ? -value : value;
}
}
}
mv->row = ref.row + mv_diff->row;
mv->col = ref.col + mv_diff->col;
#endif // CONFIG_DERIVED_MVD_SIGN
}
static INLINE void read_mv(aom_reader *r, MV *mv_diff, int skip_sign_coding,
nmv_context *ctx, MvSubpelPrecision precision,
int is_adaptive_mvd
#if !CONFIG_DERIVED_MVD_SIGN
,
MV *mv, MV ref
#endif
) {
if (is_adaptive_mvd) {
read_vq_amvd(r, mv_diff, ctx
#if !CONFIG_DERIVED_MVD_SIGN
,
mv, ref
#endif
);
return;
}
MV scaled_mv_diff;
// Read shell class
int num_mv_class = get_default_num_shell_class(precision);
const int shell_class =
aom_read_symbol(r, ctx->joint_shell_class_cdf[precision], num_mv_class,
ACCT_INFO("shell_class", "joint_shell_class_cdf"));
assert(shell_class < num_mv_class);
// Decode shell class offset
int shell_cls_offset = 0;
if (shell_class < 2) {
shell_cls_offset = aom_read_symbol(
r, ctx->shell_offset_low_class_cdf[shell_class], 2,
ACCT_INFO("shell_cls_offset", "shell_offset_low_class_cdf"));
assert(shell_cls_offset == 0 || shell_cls_offset == 1);
} else if (shell_class == 2) {
read_truncated_unary_mvd(r, ctx, 3, 3, &shell_cls_offset);
} else {
const int num_of_bits_for_this_offset =
(shell_class == 0) ? 1 : shell_class;
for (int i = 0; i < num_of_bits_for_this_offset; ++i) {
shell_cls_offset |=
aom_read_symbol(r, ctx->shell_offset_other_class_cdf[0][i], 2,
ACCT_INFO("offset"))
<< i;
}
}
// Reconstruct shell index
const int shell_class_base_index =
(shell_class == 0) ? 0 : (1 << (shell_class));
const int shell_index = shell_cls_offset + shell_class_base_index;
if (shell_index > 0) {
// Coding the col here
int scaled_mv_col;
int max_trunc_unary_value = MAX_COL_TRUNCATED_UNARY_VAL;
int this_pair_index = 0;
int maximum_pair_index = shell_index >> 1;
if (maximum_pair_index > 0) {
read_tu_quasi_uniform(r, ctx, maximum_pair_index, &this_pair_index,
max_trunc_unary_value);
}
assert(this_pair_index <= maximum_pair_index);
int skip_coding_col_bit =
(this_pair_index == maximum_pair_index) && ((shell_index % 2 == 0));
if (skip_coding_col_bit) {
scaled_mv_col = maximum_pair_index;
} else {
// int bit = aom_read_literal(r, 1, ACCT_INFO());
int context_index = shell_class < NUM_CTX_COL_MV_INDEX
? shell_class
: NUM_CTX_COL_MV_INDEX - 1;
assert(context_index < NUM_CTX_COL_MV_INDEX);
int this_bit = aom_read_symbol(
r, ctx->col_mv_index_cdf[context_index], 2,
ACCT_INFO("greater_flags", "col_mv_greter_flags_cdf"));
if (!this_bit)
scaled_mv_col = this_pair_index;
else
scaled_mv_col = shell_index - this_pair_index;
}
scaled_mv_diff.col = scaled_mv_col;
scaled_mv_diff.row = shell_index - scaled_mv_diff.col;
assert(scaled_mv_diff.row >= 0 && scaled_mv_diff.col >= 0);
} else {
scaled_mv_diff.row = 0;
scaled_mv_diff.col = 0;
}
int start_lsb = (MV_PRECISION_ONE_EIGHTH_PEL - precision);
mv_diff->row = scaled_mv_diff.row << start_lsb;
mv_diff->col = scaled_mv_diff.col << start_lsb;
// Decode signs
#if !CONFIG_DERIVED_MVD_SIGN
for (int component = 0; component < 2; component++) {
int value = component == 0 ? mv_diff->row : mv_diff->col;
if (value) {
int sign = aom_read_symbol(r, ctx->comps[component].sign_cdf, 2,
ACCT_INFO("sign"));
if (component == 0) {
mv_diff->row = sign ? -value : value;
} else {
mv_diff->col = sign ? -value : value;
}
}
}
#if BUGFIX_AMVD_AMVR
if (!is_adaptive_mvd)
#endif // BUGFIX_AMVD_AMVR
#if CONFIG_C071_SUBBLK_WARPMV
if (precision < MV_PRECISION_HALF_PEL)
#endif // CONFIG_C071_SUBBLK_WARPMV
lower_mv_precision(&ref, precision);
mv->row = ref.row + mv_diff->row;
mv->col = ref.col + mv_diff->col;
#endif
#if 0
static int counter = 0;
printf(" Decoder: counter = %d shell_index = %d \n", counter, shell_index);
counter++;
#endif
(void)skip_sign_coding;
}
#endif // !CONFIG_VQ_MVD_CODING
static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
const MACROBLOCKD *xd,
aom_reader *r) {
if (!is_comp_ref_allowed(xd->mi[0]->sb_type[PLANE_TYPE_Y]))
return SINGLE_REFERENCE;
if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT) {
const int ctx = av1_get_reference_mode_context(cm, xd);
const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_INFO());
return mode; // SINGLE_REFERENCE or COMPOUND_REFERENCE
} else {
assert(cm->current_frame.reference_mode == SINGLE_REFERENCE);
return cm->current_frame.reference_mode;
}
}
static AOM_INLINE void read_single_ref(
MACROBLOCKD *const xd, MV_REFERENCE_FRAME ref_frame[2],
const RefFramesInfo *const ref_frames_info, aom_reader *r) {
const int n_refs = ref_frames_info->num_total_refs;
for (int i = 0; i < n_refs - 1; i++) {
const int bit = aom_read_symbol(
r, av1_get_pred_cdf_single_ref(xd, i, n_refs), 2, ACCT_INFO());
if (bit) {
ref_frame[0] = i;