blob: ebb8011ada06b7f402549006d66819cfbe7fd158 [file] [log] [blame]
/*
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include "aom/aom_encoder.h"
#include "aom_dsp/aom_dsp_common.h"
#include "aom_dsp/binary_codes_writer.h"
#include "aom_dsp/bitwriter_buffer.h"
#include "aom_mem/aom_mem.h"
#include "aom_ports/mem_ops.h"
#include "aom_ports/system_state.h"
#if CONFIG_BITSTREAM_DEBUG
#include "aom_util/debug_util.h"
#endif // CONFIG_BITSTREAM_DEBUG
#if CONFIG_CDEF
#include "av1/common/cdef.h"
#endif // CONFIG_CDEF
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/entropymv.h"
#include "av1/common/mvref_common.h"
#include "av1/common/odintrin.h"
#include "av1/common/pred_common.h"
#include "av1/common/reconinter.h"
#if CONFIG_EXT_INTRA
#include "av1/common/reconintra.h"
#endif // CONFIG_EXT_INTRA
#include "av1/common/seg_common.h"
#include "av1/common/tile_common.h"
#if CONFIG_LV_MAP
#include "av1/encoder/encodetxb.h"
#endif // CONFIG_LV_MAP
#include "av1/encoder/bitstream.h"
#include "av1/encoder/cost.h"
#include "av1/encoder/encodemv.h"
#include "av1/encoder/mcomp.h"
#if CONFIG_PALETTE_DELTA_ENCODING
#include "av1/encoder/palette.h"
#endif // CONFIG_PALETTE_DELTA_ENCODING
#include "av1/encoder/segmentation.h"
#include "av1/encoder/subexp.h"
#include "av1/encoder/tokenize.h"
#define ENC_MISMATCH_DEBUG 0
#if CONFIG_COMPOUND_SINGLEREF
static struct av1_token
inter_singleref_comp_mode_encodings[INTER_SINGLEREF_COMP_MODES];
#endif // CONFIG_COMPOUND_SINGLEREF
static INLINE void write_uniform(aom_writer *w, int n, int v) {
const int l = get_unsigned_bits(n);
const int m = (1 << l) - n;
if (l == 0) return;
if (v < m) {
aom_write_literal(w, v, l - 1);
} else {
aom_write_literal(w, m + ((v - m) >> 1), l - 1);
aom_write_literal(w, (v - m) & 1, 1);
}
}
static struct av1_token interintra_mode_encodings[INTERINTRA_MODES];
static struct av1_token compound_type_encodings[COMPOUND_TYPES];
#if CONFIG_LOOP_RESTORATION
static void loop_restoration_write_sb_coeffs(const AV1_COMMON *const cm,
MACROBLOCKD *xd,
const RestorationUnitInfo *rui,
aom_writer *const w, int plane);
#endif // CONFIG_LOOP_RESTORATION
#if CONFIG_OBU
static void write_uncompressed_header_obu(AV1_COMP *cpi,
struct aom_write_bit_buffer *wb);
#else
static void write_uncompressed_header_frame(AV1_COMP *cpi,
struct aom_write_bit_buffer *wb);
#endif
static uint32_t write_compressed_header(AV1_COMP *cpi, uint8_t *data);
#if !CONFIG_OBU || CONFIG_EXT_TILE
static int remux_tiles(const AV1_COMMON *const cm, uint8_t *dst,
const uint32_t data_size, const uint32_t max_tile_size,
const uint32_t max_tile_col_size,
int *const tile_size_bytes,
int *const tile_col_size_bytes);
#endif
void av1_encode_token_init(void) {
av1_tokens_from_tree(interintra_mode_encodings, av1_interintra_mode_tree);
#if CONFIG_COMPOUND_SINGLEREF
av1_tokens_from_tree(inter_singleref_comp_mode_encodings,
av1_inter_singleref_comp_mode_tree);
#endif // CONFIG_COMPOUND_SINGLEREF
av1_tokens_from_tree(compound_type_encodings, av1_compound_type_tree);
}
static void write_intra_mode_kf(const AV1_COMMON *cm, FRAME_CONTEXT *frame_ctx,
const MODE_INFO *mi, const MODE_INFO *above_mi,
const MODE_INFO *left_mi, int block,
PREDICTION_MODE mode, aom_writer *w) {
#if CONFIG_INTRABC
assert(!is_intrabc_block(&mi->mbmi));
#endif // CONFIG_INTRABC
aom_write_symbol(w, mode,
get_y_mode_cdf(frame_ctx, mi, above_mi, left_mi, block),
INTRA_MODES);
(void)cm;
}
static void write_inter_mode(aom_writer *w, PREDICTION_MODE mode,
FRAME_CONTEXT *ec_ctx, const int16_t mode_ctx) {
const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, mode != NEWMV, ec_ctx->newmv_cdf[newmv_ctx], 2);
#else
aom_write(w, mode != NEWMV, ec_ctx->newmv_prob[newmv_ctx]);
#endif
if (mode != NEWMV) {
if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) {
assert(mode == GLOBALMV);
return;
}
const int16_t zeromv_ctx =
(mode_ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, mode != GLOBALMV, ec_ctx->zeromv_cdf[zeromv_ctx], 2);
#else
aom_write(w, mode != GLOBALMV, ec_ctx->zeromv_prob[zeromv_ctx]);
#endif
if (mode != GLOBALMV) {
int16_t refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6;
if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7;
if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8;
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, mode != NEARESTMV, ec_ctx->refmv_cdf[refmv_ctx], 2);
#else
aom_write(w, mode != NEARESTMV, ec_ctx->refmv_prob[refmv_ctx]);
#endif
}
}
}
static void write_drl_idx(FRAME_CONTEXT *ec_ctx, const MB_MODE_INFO *mbmi,
const MB_MODE_INFO_EXT *mbmi_ext, aom_writer *w) {
uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
assert(mbmi->ref_mv_idx < 3);
#if CONFIG_COMPOUND_SINGLEREF
if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
mbmi->mode == SR_NEW_NEWMV) {
#else // !CONFIG_COMPOUND_SINGLEREF
if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) {
#endif // CONFIG_COMPOUND_SINGLEREF
int idx;
for (idx = 0; idx < 2; ++idx) {
if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
uint8_t drl_ctx =
av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx);
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, mbmi->ref_mv_idx != idx, ec_ctx->drl_cdf[drl_ctx],
2);
#else
aom_write(w, mbmi->ref_mv_idx != idx, ec_ctx->drl_prob[drl_ctx]);
#endif
if (mbmi->ref_mv_idx == idx) return;
}
}
return;
}
if (have_nearmv_in_inter_mode(mbmi->mode)) {
int idx;
// TODO(jingning): Temporary solution to compensate the NEARESTMV offset.
for (idx = 1; idx < 3; ++idx) {
if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
uint8_t drl_ctx =
av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx);
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, mbmi->ref_mv_idx != (idx - 1),
ec_ctx->drl_cdf[drl_ctx], 2);
#else
aom_write(w, mbmi->ref_mv_idx != (idx - 1), ec_ctx->drl_prob[drl_ctx]);
#endif
if (mbmi->ref_mv_idx == (idx - 1)) return;
}
}
return;
}
}
static void write_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
aom_writer *w, PREDICTION_MODE mode,
const int16_t mode_ctx) {
assert(is_inter_compound_mode(mode));
(void)cm;
aom_write_symbol(w, INTER_COMPOUND_OFFSET(mode),
xd->tile_ctx->inter_compound_mode_cdf[mode_ctx],
INTER_COMPOUND_MODES);
}
#if CONFIG_COMPOUND_SINGLEREF
static void write_inter_singleref_comp_mode(MACROBLOCKD *xd, aom_writer *w,
PREDICTION_MODE mode,
const int16_t mode_ctx) {
assert(is_inter_singleref_comp_mode(mode));
aom_cdf_prob *const inter_singleref_comp_cdf =
xd->tile_ctx->inter_singleref_comp_mode_cdf[mode_ctx];
aom_write_symbol(w, INTER_SINGLEREF_COMP_OFFSET(mode),
inter_singleref_comp_cdf, INTER_SINGLEREF_COMP_MODES);
}
#endif // CONFIG_COMPOUND_SINGLEREF
static void encode_unsigned_max(struct aom_write_bit_buffer *wb, int data,
int max) {
aom_wb_write_literal(wb, data, get_unsigned_bits(max));
}
static void write_tx_size_vartx(const AV1_COMMON *cm, MACROBLOCKD *xd,
const MB_MODE_INFO *mbmi, TX_SIZE tx_size,
int depth, int blk_row, int blk_col,
aom_writer *w) {
#if CONFIG_NEW_MULTISYMBOL
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
(void)cm;
#endif
const int tx_row = blk_row >> 1;
const int tx_col = blk_col >> 1;
const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0);
const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0);
int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
xd->left_txfm_context + blk_row,
mbmi->sb_type, tx_size);
if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
if (depth == MAX_VARTX_DEPTH) {
txfm_partition_update(xd->above_txfm_context + blk_col,
xd->left_txfm_context + blk_row, tx_size, tx_size);
return;
}
#if CONFIG_RECT_TX_EXT
if (tx_size == mbmi->inter_tx_size[tx_row][tx_col] ||
mbmi->tx_size == quarter_txsize_lookup[mbmi->sb_type]) {
#else
if (tx_size == mbmi->inter_tx_size[tx_row][tx_col]) {
#endif
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, 0, ec_ctx->txfm_partition_cdf[ctx], 2);
#else
aom_write(w, 0, cm->fc->txfm_partition_prob[ctx]);
#endif
txfm_partition_update(xd->above_txfm_context + blk_col,
xd->left_txfm_context + blk_row, tx_size, tx_size);
// TODO(yuec): set correct txfm partition update for qttx
} else {
const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
const int bsl = tx_size_wide_unit[sub_txs];
int i;
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, 1, ec_ctx->txfm_partition_cdf[ctx], 2);
#else
aom_write(w, 1, cm->fc->txfm_partition_prob[ctx]);
#endif
if (sub_txs == TX_4X4) {
txfm_partition_update(xd->above_txfm_context + blk_col,
xd->left_txfm_context + blk_row, sub_txs, tx_size);
return;
}
assert(bsl > 0);
for (i = 0; i < 4; ++i) {
int offsetr = blk_row + (i >> 1) * bsl;
int offsetc = blk_col + (i & 0x01) * bsl;
write_tx_size_vartx(cm, xd, mbmi, sub_txs, depth + 1, offsetr, offsetc,
w);
}
}
}
#if !CONFIG_NEW_MULTISYMBOL
static void update_txfm_partition_probs(AV1_COMMON *cm, aom_writer *w,
FRAME_COUNTS *counts, int probwt) {
int k;
for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k)
av1_cond_prob_diff_update(w, &cm->fc->txfm_partition_prob[k],
counts->txfm_partition[k], probwt);
}
#endif // CONFIG_NEW_MULTISYMBOL
static void write_selected_tx_size(const AV1_COMMON *cm, const MACROBLOCKD *xd,
aom_writer *w) {
const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
const BLOCK_SIZE bsize = mbmi->sb_type;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
(void)cm;
if (block_signals_txsize(bsize)) {
const TX_SIZE tx_size = mbmi->tx_size;
const int is_inter = is_inter_block(mbmi);
const int tx_size_ctx = get_tx_size_context(xd);
const int32_t tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
: intra_tx_size_cat_lookup[bsize];
const TX_SIZE coded_tx_size = txsize_sqr_up_map[tx_size];
const int depth = tx_size_to_depth(coded_tx_size);
assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed(xd, mbmi)));
aom_write_symbol(w, depth, ec_ctx->tx_size_cdf[tx_size_cat][tx_size_ctx],
tx_size_cat + 2);
#if CONFIG_RECT_TX_EXT
if (is_quarter_tx_allowed(xd, mbmi, is_inter) && tx_size != coded_tx_size)
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, tx_size == quarter_txsize_lookup[bsize],
cm->fc->quarter_tx_size_cdf, 2);
#else
aom_write(w, tx_size == quarter_txsize_lookup[bsize],
cm->fc->quarter_tx_size_prob);
#endif
#endif
}
}
#if !CONFIG_NEW_MULTISYMBOL
static void update_inter_mode_probs(AV1_COMMON *cm, aom_writer *w,
FRAME_COUNTS *counts) {
int i;
const int probwt = cm->num_tg;
for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i)
av1_cond_prob_diff_update(w, &cm->fc->newmv_prob[i], counts->newmv_mode[i],
probwt);
for (i = 0; i < GLOBALMV_MODE_CONTEXTS; ++i)
av1_cond_prob_diff_update(w, &cm->fc->zeromv_prob[i],
counts->zeromv_mode[i], probwt);
for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
av1_cond_prob_diff_update(w, &cm->fc->refmv_prob[i], counts->refmv_mode[i],
probwt);
for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
av1_cond_prob_diff_update(w, &cm->fc->drl_prob[i], counts->drl_mode[i],
probwt);
}
#endif
static int write_skip(const AV1_COMMON *cm, const MACROBLOCKD *xd,
int segment_id, const MODE_INFO *mi, aom_writer *w) {
if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
return 1;
} else {
const int skip = mi->mbmi.skip;
const int ctx = av1_get_skip_context(xd);
#if CONFIG_NEW_MULTISYMBOL
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
aom_write_symbol(w, skip, ec_ctx->skip_cdfs[ctx], 2);
#else
aom_write(w, skip, cm->fc->skip_probs[ctx]);
#endif
return skip;
}
}
static void write_is_inter(const AV1_COMMON *cm, const MACROBLOCKD *xd,
int segment_id, aom_writer *w, const int is_inter) {
if (!segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
const int ctx = av1_get_intra_inter_context(xd);
#if CONFIG_NEW_MULTISYMBOL
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
aom_write_symbol(w, is_inter, ec_ctx->intra_inter_cdf[ctx], 2);
#else
aom_write(w, is_inter, cm->fc->intra_inter_prob[ctx]);
#endif
}
}
static void write_motion_mode(const AV1_COMMON *cm, MACROBLOCKD *xd,
const MODE_INFO *mi, aom_writer *w) {
const MB_MODE_INFO *mbmi = &mi->mbmi;
MOTION_MODE last_motion_mode_allowed =
motion_mode_allowed(0, cm->global_motion, xd, mi);
if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return;
#if CONFIG_NCOBMC_ADAPT_WEIGHT
if (last_motion_mode_allowed == NCOBMC_ADAPT_WEIGHT) {
aom_write_symbol(w, mbmi->motion_mode,
xd->tile_ctx->ncobmc_cdf[mbmi->sb_type],
OBMC_FAMILY_MODES);
} else if (last_motion_mode_allowed == OBMC_CAUSAL) {
aom_write_symbol(w, mbmi->motion_mode == OBMC_CAUSAL,
xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2);
} else {
#else
if (last_motion_mode_allowed == OBMC_CAUSAL) {
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, mbmi->motion_mode == OBMC_CAUSAL,
xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2);
#else
aom_write(w, mbmi->motion_mode == OBMC_CAUSAL,
cm->fc->obmc_prob[mbmi->sb_type]);
#endif
} else {
#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
aom_write_symbol(w, mbmi->motion_mode,
xd->tile_ctx->motion_mode_cdf[mbmi->sb_type],
MOTION_MODES);
}
}
#if CONFIG_NCOBMC_ADAPT_WEIGHT
static void write_ncobmc_mode(MACROBLOCKD *xd, const MODE_INFO *mi,
aom_writer *w) {
const MB_MODE_INFO *mbmi = &mi->mbmi;
ADAPT_OVERLAP_BLOCK ao_block = adapt_overlap_block_lookup[mbmi->sb_type];
if (mbmi->motion_mode != NCOBMC_ADAPT_WEIGHT) return;
aom_write_symbol(w, mbmi->ncobmc_mode[0],
xd->tile_ctx->ncobmc_mode_cdf[ao_block], MAX_NCOBMC_MODES);
if (mi_size_wide[mbmi->sb_type] != mi_size_high[mbmi->sb_type]) {
aom_write_symbol(w, mbmi->ncobmc_mode[1],
xd->tile_ctx->ncobmc_mode_cdf[ao_block], MAX_NCOBMC_MODES);
}
}
#endif
static void write_delta_qindex(const AV1_COMMON *cm, const MACROBLOCKD *xd,
int delta_qindex, aom_writer *w) {
int sign = delta_qindex < 0;
int abs = sign ? -delta_qindex : delta_qindex;
int rem_bits, thr;
int smallval = abs < DELTA_Q_SMALL ? 1 : 0;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
(void)cm;
aom_write_symbol(w, AOMMIN(abs, DELTA_Q_SMALL), ec_ctx->delta_q_cdf,
DELTA_Q_PROBS + 1);
if (!smallval) {
rem_bits = OD_ILOG_NZ(abs - 1) - 1;
thr = (1 << rem_bits) + 1;
aom_write_literal(w, rem_bits - 1, 3);
aom_write_literal(w, abs - thr, rem_bits);
}
if (abs > 0) {
aom_write_bit(w, sign);
}
}
#if CONFIG_EXT_DELTA_Q
static void write_delta_lflevel(const AV1_COMMON *cm, const MACROBLOCKD *xd,
#if CONFIG_LOOPFILTER_LEVEL
int lf_id,
#endif
int delta_lflevel, aom_writer *w) {
int sign = delta_lflevel < 0;
int abs = sign ? -delta_lflevel : delta_lflevel;
int rem_bits, thr;
int smallval = abs < DELTA_LF_SMALL ? 1 : 0;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
(void)cm;
#if CONFIG_LOOPFILTER_LEVEL
if (cm->delta_lf_multi) {
assert(lf_id >= 0 && lf_id < FRAME_LF_COUNT);
aom_write_symbol(w, AOMMIN(abs, DELTA_LF_SMALL),
ec_ctx->delta_lf_multi_cdf[lf_id], DELTA_LF_PROBS + 1);
} else {
aom_write_symbol(w, AOMMIN(abs, DELTA_LF_SMALL), ec_ctx->delta_lf_cdf,
DELTA_LF_PROBS + 1);
}
#else
aom_write_symbol(w, AOMMIN(abs, DELTA_LF_SMALL), ec_ctx->delta_lf_cdf,
DELTA_LF_PROBS + 1);
#endif // CONFIG_LOOPFILTER_LEVEL
if (!smallval) {
rem_bits = OD_ILOG_NZ(abs - 1) - 1;
thr = (1 << rem_bits) + 1;
aom_write_literal(w, rem_bits - 1, 3);
aom_write_literal(w, abs - thr, rem_bits);
}
if (abs > 0) {
aom_write_bit(w, sign);
}
}
#endif // CONFIG_EXT_DELTA_Q
#if !CONFIG_NEW_MULTISYMBOL
static void update_skip_probs(AV1_COMMON *cm, aom_writer *w,
FRAME_COUNTS *counts) {
int k;
const int probwt = cm->num_tg;
for (k = 0; k < SKIP_CONTEXTS; ++k) {
av1_cond_prob_diff_update(w, &cm->fc->skip_probs[k], counts->skip[k],
probwt);
}
}
#endif
static void pack_map_tokens(aom_writer *w, const TOKENEXTRA **tp, int n,
int num) {
const TOKENEXTRA *p = *tp;
write_uniform(w, n, p->token); // The first color index.
++p;
--num;
for (int i = 0; i < num; ++i) {
aom_write_symbol(w, p->token, p->color_map_cdf, n);
++p;
}
*tp = p;
}
#if !CONFIG_LV_MAP
#if CONFIG_NEW_MULTISYMBOL
static INLINE void write_coeff_extra(const aom_cdf_prob *const *cdf, int val,
int n, aom_writer *w) {
// Code the extra bits from LSB to MSB in groups of 4
int i = 0;
int count = 0;
while (count < n) {
const int size = AOMMIN(n - count, 4);
const int mask = (1 << size) - 1;
aom_write_cdf(w, val & mask, cdf[i++], 1 << size);
val >>= size;
count += size;
}
}
#else
static INLINE void write_coeff_extra(const aom_prob *pb, int value,
int num_bits, int skip_bits, aom_writer *w,
TOKEN_STATS *token_stats) {
// Code the extra bits from MSB to LSB 1 bit at a time
int index;
for (index = skip_bits; index < num_bits; ++index) {
const int shift = num_bits - index - 1;
const int bb = (value >> shift) & 1;
aom_write_record(w, bb, pb[index], token_stats);
}
}
#endif // CONFIG_NEW_MULTISYMBOL
static void pack_mb_tokens(aom_writer *w, const TOKENEXTRA **tp,
const TOKENEXTRA *const stop,
aom_bit_depth_t bit_depth, const TX_SIZE tx_size,
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
TX_TYPE tx_type, int is_inter,
#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
TOKEN_STATS *token_stats) {
const TOKENEXTRA *p = *tp;
int count = 0;
const int seg_eob = tx_size_2d[tx_size];
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
if (tx_type == MRC_DCT && ((is_inter && SIGNAL_MRC_MASK_INTER) ||
(!is_inter && SIGNAL_MRC_MASK_INTRA))) {
int rows = tx_size_high[tx_size];
int cols = tx_size_wide[tx_size];
assert(tx_size == TX_32X32);
assert(p < stop);
pack_map_tokens(w, &p, 2, rows * cols);
}
#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
while (p < stop && p->token != EOSB_TOKEN) {
const int token = p->token;
const int8_t eob_val = p->eob_val;
if (token == BLOCK_Z_TOKEN) {
aom_write_symbol(w, 0, *p->head_cdf, HEAD_TOKENS + 1);
p++;
break;
continue;
}
const av1_extra_bit *const extra_bits = &av1_extra_bits[token];
if (eob_val == LAST_EOB) {
// Just code a flag indicating whether the value is >1 or 1.
aom_write_bit(w, token != ONE_TOKEN);
} else {
int comb_symb = 2 * AOMMIN(token, TWO_TOKEN) - eob_val + p->first_val;
aom_write_symbol(w, comb_symb, *p->head_cdf, HEAD_TOKENS + p->first_val);
}
if (token > ONE_TOKEN) {
aom_write_symbol(w, token - TWO_TOKEN, *p->tail_cdf, TAIL_TOKENS);
}
if (extra_bits->base_val) {
const int bit_string = p->extra;
const int bit_string_length = extra_bits->len; // Length of extra bits to
const int is_cat6 = (extra_bits->base_val == CAT6_MIN_VAL);
// be written excluding
// the sign bit.
int skip_bits = is_cat6
? (int)sizeof(av1_cat6_prob) -
av1_get_cat6_extrabits_size(tx_size, bit_depth)
: 0;
assert(!(bit_string >> (bit_string_length - skip_bits + 1)));
if (bit_string_length > 0)
#if CONFIG_NEW_MULTISYMBOL
write_coeff_extra(extra_bits->cdf, bit_string >> 1,
bit_string_length - skip_bits, w);
#else
write_coeff_extra(extra_bits->prob, bit_string >> 1, bit_string_length,
skip_bits, w, token_stats);
#endif
aom_write_bit_record(w, bit_string & 1, token_stats);
}
++p;
++count;
if (eob_val == EARLY_EOB || count == seg_eob) break;
}
*tp = p;
}
#endif // !CONFIG_LV_MAP
#if CONFIG_LV_MAP
static void pack_txb_tokens(aom_writer *w, AV1_COMMON *cm, MACROBLOCK *const x,
const TOKENEXTRA **tp,
const TOKENEXTRA *const tok_end, MACROBLOCKD *xd,
MB_MODE_INFO *mbmi, int plane,
BLOCK_SIZE plane_bsize, aom_bit_depth_t bit_depth,
int block, int blk_row, int blk_col,
TX_SIZE tx_size, TOKEN_STATS *token_stats) {
const struct macroblockd_plane *const pd = &xd->plane[plane];
const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
const int tx_row = blk_row >> (1 - pd->subsampling_y);
const int tx_col = blk_col >> (1 - pd->subsampling_x);
TX_SIZE plane_tx_size;
const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
plane_tx_size =
plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
: mbmi->inter_tx_size[tx_row][tx_col];
if (tx_size == plane_tx_size) {
TOKEN_STATS tmp_token_stats;
init_token_stats(&tmp_token_stats);
tran_low_t *tcoeff = BLOCK_OFFSET(x->mbmi_ext->tcoeff[plane], block);
uint16_t eob = x->mbmi_ext->eobs[plane][block];
TXB_CTX txb_ctx = { x->mbmi_ext->txb_skip_ctx[plane][block],
x->mbmi_ext->dc_sign_ctx[plane][block] };
av1_write_coeffs_txb(cm, xd, w, blk_row, blk_col, block, plane, tx_size,
tcoeff, eob, &txb_ctx);
#if CONFIG_RD_DEBUG
token_stats->txb_coeff_cost_map[blk_row][blk_col] = tmp_token_stats.cost;
token_stats->cost += tmp_token_stats.cost;
#endif
} else {
const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
const int bsl = tx_size_wide_unit[sub_txs];
int i;
assert(bsl > 0);
for (i = 0; i < 4; ++i) {
const int offsetr = blk_row + (i >> 1) * bsl;
const int offsetc = blk_col + (i & 0x01) * bsl;
const int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
pack_txb_tokens(w, cm, x, tp, tok_end, xd, mbmi, plane, plane_bsize,
bit_depth, block, offsetr, offsetc, sub_txs, token_stats);
block += step;
}
}
}
#else // CONFIG_LV_MAP
static void pack_txb_tokens(aom_writer *w, const TOKENEXTRA **tp,
const TOKENEXTRA *const tok_end, MACROBLOCKD *xd,
MB_MODE_INFO *mbmi, int plane,
BLOCK_SIZE plane_bsize, aom_bit_depth_t bit_depth,
int block, int blk_row, int blk_col,
TX_SIZE tx_size, TOKEN_STATS *token_stats) {
const struct macroblockd_plane *const pd = &xd->plane[plane];
const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
const int tx_row = blk_row >> (1 - pd->subsampling_y);
const int tx_col = blk_col >> (1 - pd->subsampling_x);
TX_SIZE plane_tx_size;
const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
TX_TYPE tx_type = av1_get_tx_type(plane ? PLANE_TYPE_UV : PLANE_TYPE_Y, xd,
blk_row, blk_col, block, tx_size);
#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
plane_tx_size =
plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
: mbmi->inter_tx_size[tx_row][tx_col];
if (tx_size == plane_tx_size) {
TOKEN_STATS tmp_token_stats;
init_token_stats(&tmp_token_stats);
pack_mb_tokens(w, tp, tok_end, bit_depth, tx_size,
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
tx_type, is_inter_block(mbmi),
#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
&tmp_token_stats);
#if CONFIG_RD_DEBUG
token_stats->txb_coeff_cost_map[blk_row][blk_col] = tmp_token_stats.cost;
token_stats->cost += tmp_token_stats.cost;
#endif
} else {
#if CONFIG_RECT_TX_EXT
int is_qttx = plane_tx_size == quarter_txsize_lookup[plane_bsize];
const TX_SIZE sub_txs = is_qttx ? plane_tx_size : sub_tx_size_map[tx_size];
#else
const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
#endif
const int bsl = tx_size_wide_unit[sub_txs];
int i;
assert(bsl > 0);
for (i = 0; i < 4; ++i) {
#if CONFIG_RECT_TX_EXT
int is_wide_tx = tx_size_wide_unit[sub_txs] > tx_size_high_unit[sub_txs];
const int offsetr =
is_qttx ? (is_wide_tx ? i * tx_size_high_unit[sub_txs] : 0)
: blk_row + (i >> 1) * bsl;
const int offsetc =
is_qttx ? (is_wide_tx ? 0 : i * tx_size_wide_unit[sub_txs])
: blk_col + (i & 0x01) * bsl;
#else
const int offsetr = blk_row + (i >> 1) * bsl;
const int offsetc = blk_col + (i & 0x01) * bsl;
#endif
const int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
pack_txb_tokens(w, tp, tok_end, xd, mbmi, plane, plane_bsize, bit_depth,
block, offsetr, offsetc, sub_txs, token_stats);
block += step;
}
}
}
#endif // CONFIG_LV_MAP
static void write_segment_id(aom_writer *w, const struct segmentation *seg,
struct segmentation_probs *segp, int segment_id) {
if (seg->enabled && seg->update_map) {
aom_write_symbol(w, segment_id, segp->tree_cdf, MAX_SEGMENTS);
}
}
#if CONFIG_NEW_MULTISYMBOL
#define WRITE_REF_BIT(bname, pname) \
aom_write_symbol(w, bname, av1_get_pred_cdf_##pname(cm, xd), 2)
#define WRITE_REF_BIT2(bname, pname) \
aom_write_symbol(w, bname, av1_get_pred_cdf_##pname(xd), 2)
#else
#define WRITE_REF_BIT(bname, pname) \
aom_write(w, bname, av1_get_pred_prob_##pname(cm, xd))
#define WRITE_REF_BIT2(bname, pname) \
aom_write(w, bname, av1_get_pred_prob_##pname(cm, xd))
#endif
// This function encodes the reference frame
static void write_ref_frames(const AV1_COMMON *cm, const MACROBLOCKD *xd,
aom_writer *w) {
const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
const int is_compound = has_second_ref(mbmi);
const int segment_id = mbmi->segment_id;
// If segment level coding of this signal is disabled...
// or the segment allows multiple reference frame options
if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
assert(!is_compound);
assert(mbmi->ref_frame[0] ==
get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME));
}
#if CONFIG_SEGMENT_GLOBALMV
else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) ||
segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV))
#else
else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP))
#endif
{
assert(!is_compound);
assert(mbmi->ref_frame[0] == LAST_FRAME);
} else {
// does the feature use compound prediction or not
// (if not specified at the frame/segment level)
if (cm->reference_mode == REFERENCE_MODE_SELECT) {
if (is_comp_ref_allowed(mbmi->sb_type))
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, is_compound, av1_get_reference_mode_cdf(cm, xd), 2);
#else
aom_write(w, is_compound, av1_get_reference_mode_prob(cm, xd));
#endif // CONFIG_NEW_MULTISYMBOL
} else {
assert((!is_compound) == (cm->reference_mode == SINGLE_REFERENCE));
}
if (is_compound) {
#if CONFIG_EXT_COMP_REFS
const COMP_REFERENCE_TYPE comp_ref_type = has_uni_comp_refs(mbmi)
? UNIDIR_COMP_REFERENCE
: BIDIR_COMP_REFERENCE;
#if CONFIG_VAR_REFS
if ((L_OR_L2(cm) || L3_OR_G(cm)) && BWD_OR_ALT(cm))
if (L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm) || BWD_AND_ALT(cm))
#endif // CONFIG_VAR_REFS
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, comp_ref_type,
av1_get_comp_reference_type_cdf(xd), 2);
#else
aom_write(w, comp_ref_type, av1_get_comp_reference_type_prob(cm, xd));
#endif
#if CONFIG_VAR_REFS
else
assert(comp_ref_type == BIDIR_COMP_REFERENCE);
else
assert(comp_ref_type == UNIDIR_COMP_REFERENCE);
#endif // CONFIG_VAR_REFS
if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
const int bit = mbmi->ref_frame[0] == BWDREF_FRAME;
#if CONFIG_VAR_REFS
if ((L_AND_L2(cm) || L_AND_L3(cm) || L_AND_G(cm)) && BWD_AND_ALT(cm))
#endif // CONFIG_VAR_REFS
WRITE_REF_BIT2(bit, uni_comp_ref_p);
if (!bit) {
assert(mbmi->ref_frame[0] == LAST_FRAME);
#if CONFIG_VAR_REFS
if (L_AND_L2(cm) && (L_AND_L3(cm) || L_AND_G(cm))) {
#endif // CONFIG_VAR_REFS
const int bit1 = mbmi->ref_frame[1] == LAST3_FRAME ||
mbmi->ref_frame[1] == GOLDEN_FRAME;
WRITE_REF_BIT2(bit1, uni_comp_ref_p1);
if (bit1) {
#if CONFIG_VAR_REFS
if (L_AND_L3(cm) && L_AND_G(cm)) {
#endif // CONFIG_VAR_REFS
const int bit2 = mbmi->ref_frame[1] == GOLDEN_FRAME;
WRITE_REF_BIT2(bit2, uni_comp_ref_p2);
#if CONFIG_VAR_REFS
}
#endif // CONFIG_VAR_REFS
}
#if CONFIG_VAR_REFS
}
#endif // CONFIG_VAR_REFS
} else {
assert(mbmi->ref_frame[1] == ALTREF_FRAME);
}
return;
}
assert(comp_ref_type == BIDIR_COMP_REFERENCE);
#endif // CONFIG_EXT_COMP_REFS
const int bit = (mbmi->ref_frame[0] == GOLDEN_FRAME ||
mbmi->ref_frame[0] == LAST3_FRAME);
#if CONFIG_VAR_REFS
// Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
if (L_OR_L2(cm) && L3_OR_G(cm))
#endif // CONFIG_VAR_REFS
WRITE_REF_BIT(bit, comp_ref_p);
if (!bit) {
#if CONFIG_VAR_REFS
// Test need to explicitly code (L) vs (L2) branch node in tree
if (L_AND_L2(cm)) {
#endif // CONFIG_VAR_REFS
const int bit1 = mbmi->ref_frame[0] == LAST_FRAME;
WRITE_REF_BIT(bit1, comp_ref_p1);
#if CONFIG_VAR_REFS
}
#endif // CONFIG_VAR_REFS
} else {
#if CONFIG_VAR_REFS
// Test need to explicitly code (L3) vs (G) branch node in tree
if (L3_AND_G(cm)) {
#endif // CONFIG_VAR_REFS
const int bit2 = mbmi->ref_frame[0] == GOLDEN_FRAME;
WRITE_REF_BIT(bit2, comp_ref_p2);
#if CONFIG_VAR_REFS
}
#endif // CONFIG_VAR_REFS
}
#if CONFIG_VAR_REFS
// Test need to explicitly code (BWD,ALT2) vs (ALT) branch node in tree
if (BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm)) {
#endif // CONFIG_VAR_REFS
const int bit_bwd = mbmi->ref_frame[1] == ALTREF_FRAME;
WRITE_REF_BIT(bit_bwd, comp_bwdref_p);
if (!bit_bwd) {
#if CONFIG_VAR_REFS
// Test need to explicitly code (BWD,ALT2) vs (ALT) branch node in
// tree
if (BWD_AND_ALT2(cm))
#endif // CONFIG_VAR_REFS
WRITE_REF_BIT(mbmi->ref_frame[1] == ALTREF2_FRAME, comp_bwdref_p1);
}
#if CONFIG_VAR_REFS
}
#endif // CONFIG_VAR_REFS
} else {
const int bit0 = (mbmi->ref_frame[0] <= ALTREF_FRAME &&
mbmi->ref_frame[0] >= BWDREF_FRAME);
#if CONFIG_VAR_REFS
// Test need to explicitly code (L,L2,L3,G) vs (BWD,ALT2,ALT) branch node
// in tree
if ((L_OR_L2(cm) || L3_OR_G(cm)) &&
(BWD_OR_ALT2(cm) || ALTREF_IS_VALID(cm)))
#endif // CONFIG_VAR_REFS
WRITE_REF_BIT(bit0, single_ref_p1);
if (bit0) {
#if CONFIG_VAR_REFS
// Test need to explicitly code (BWD,ALT2) vs (ALT) branch node in tree
if (BWD_OR_ALT2(cm) && ALTREF_IS_VALID(cm)) {
#endif // CONFIG_VAR_REFS
const int bit1 = mbmi->ref_frame[0] == ALTREF_FRAME;
WRITE_REF_BIT(bit1, single_ref_p2);
if (!bit1) {
#if CONFIG_VAR_REFS
// Test need to explicitly code (BWD) vs (ALT2) branch node in tree
if (BWD_AND_ALT2(cm))
#endif // CONFIG_VAR_REFS
WRITE_REF_BIT(mbmi->ref_frame[0] == ALTREF2_FRAME, single_ref_p6);
}
#if CONFIG_VAR_REFS
}
#endif // CONFIG_VAR_REFS
} else {
const int bit2 = (mbmi->ref_frame[0] == LAST3_FRAME ||
mbmi->ref_frame[0] == GOLDEN_FRAME);
#if CONFIG_VAR_REFS
// Test need to explicitly code (L,L2) vs (L3,G) branch node in tree
if (L_OR_L2(cm) && L3_OR_G(cm))
#endif // CONFIG_VAR_REFS
WRITE_REF_BIT(bit2, single_ref_p3);
if (!bit2) {
#if CONFIG_VAR_REFS
// Test need to explicitly code (L) vs (L2) branch node in tree
if (L_AND_L2(cm)) {
#endif // CONFIG_VAR_REFS
const int bit3 = mbmi->ref_frame[0] != LAST_FRAME;
WRITE_REF_BIT(bit3, single_ref_p4);
#if CONFIG_VAR_REFS
}
#endif // CONFIG_VAR_REFS
} else {
#if CONFIG_VAR_REFS
// Test need to explicitly code (L3) vs (G) branch node in tree
if (L3_AND_G(cm)) {
#endif // CONFIG_VAR_REFS
const int bit4 = mbmi->ref_frame[0] != LAST3_FRAME;
WRITE_REF_BIT(bit4, single_ref_p5);
#if CONFIG_VAR_REFS
}
#endif // CONFIG_VAR_REFS
}
}
}
}
}
#if CONFIG_FILTER_INTRA
static void write_filter_intra_mode_info(const AV1_COMMON *const cm,
const MACROBLOCKD *xd,
const MB_MODE_INFO *const mbmi,
aom_writer *w) {
if (mbmi->mode == DC_PRED && mbmi->palette_mode_info.palette_size[0] == 0) {
aom_write(w, mbmi->filter_intra_mode_info.use_filter_intra_mode[0],
cm->fc->filter_intra_probs[0]);
if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) {
const FILTER_INTRA_MODE mode =
mbmi->filter_intra_mode_info.filter_intra_mode[0];
aom_write_symbol(w, mode, xd->tile_ctx->filter_intra_mode_cdf[0],
FILTER_INTRA_MODES);
}
}
}
#endif // CONFIG_FILTER_INTRA
#if CONFIG_EXT_INTRA
static void write_intra_angle_info(const MACROBLOCKD *xd,
FRAME_CONTEXT *const ec_ctx, aom_writer *w) {
const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
const BLOCK_SIZE bsize = mbmi->sb_type;
if (!av1_use_angle_delta(bsize)) return;
if (av1_is_directional_mode(mbmi->mode, bsize)) {
#if CONFIG_EXT_INTRA_MOD
aom_write_symbol(w, mbmi->angle_delta[0] + MAX_ANGLE_DELTA,
ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED],
2 * MAX_ANGLE_DELTA + 1);
#else
(void)ec_ctx;
write_uniform(w, 2 * MAX_ANGLE_DELTA + 1,
MAX_ANGLE_DELTA + mbmi->angle_delta[0]);
#endif // CONFIG_EXT_INTRA_MOD
}
if (av1_is_directional_mode(get_uv_mode(mbmi->uv_mode), bsize)) {
#if CONFIG_EXT_INTRA_MOD
aom_write_symbol(w, mbmi->angle_delta[1] + MAX_ANGLE_DELTA,
ec_ctx->angle_delta_cdf[mbmi->uv_mode - V_PRED],
2 * MAX_ANGLE_DELTA + 1);
#else
write_uniform(w, 2 * MAX_ANGLE_DELTA + 1,
MAX_ANGLE_DELTA + mbmi->angle_delta[1]);
#endif
}
}
#endif // CONFIG_EXT_INTRA
static void write_mb_interp_filter(AV1_COMP *cpi, const MACROBLOCKD *xd,
aom_writer *w) {
AV1_COMMON *const cm = &cpi->common;
const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
if (!av1_is_interp_needed(xd)) {
assert(mbmi->interp_filters ==
av1_broadcast_interp_filter(
av1_unswitchable_filter(cm->interp_filter)));
return;
}
if (cm->interp_filter == SWITCHABLE) {
#if CONFIG_DUAL_FILTER
int dir;
for (dir = 0; dir < 2; ++dir) {
if (has_subpel_mv_component(xd->mi[0], xd, dir) ||
(mbmi->ref_frame[1] > INTRA_FRAME &&
has_subpel_mv_component(xd->mi[0], xd, dir + 2))) {
const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
InterpFilter filter =
av1_extract_interp_filter(mbmi->interp_filters, dir);
aom_write_symbol(w, filter, ec_ctx->switchable_interp_cdf[ctx],
SWITCHABLE_FILTERS);
++cpi->interp_filter_selected[0][filter];
} else {
assert(av1_extract_interp_filter(mbmi->interp_filters, dir) ==
EIGHTTAP_REGULAR);
}
}
#else
{
const int ctx = av1_get_pred_context_switchable_interp(xd);
InterpFilter filter = av1_extract_interp_filter(mbmi->interp_filters, 0);
aom_write_symbol(w, filter, ec_ctx->switchable_interp_cdf[ctx],
SWITCHABLE_FILTERS);
++cpi->interp_filter_selected[0][filter];
}
#endif // CONFIG_DUAL_FILTER
}
}
#if CONFIG_PALETTE_DELTA_ENCODING
// Transmit color values with delta encoding. Write the first value as
// literal, and the deltas between each value and the previous one. "min_val" is
// the smallest possible value of the deltas.
static void delta_encode_palette_colors(const int *colors, int num,
int bit_depth, int min_val,
aom_writer *w) {
if (num <= 0) return;
assert(colors[0] < (1 << bit_depth));
aom_write_literal(w, colors[0], bit_depth);
if (num == 1) return;
int max_delta = 0;
int deltas[PALETTE_MAX_SIZE];
memset(deltas, 0, sizeof(deltas));
for (int i = 1; i < num; ++i) {
assert(colors[i] < (1 << bit_depth));
const int delta = colors[i] - colors[i - 1];
deltas[i - 1] = delta;
assert(delta >= min_val);
if (delta > max_delta) max_delta = delta;
}
const int min_bits = bit_depth - 3;
int bits = AOMMAX(av1_ceil_log2(max_delta + 1 - min_val), min_bits);
assert(bits <= bit_depth);
int range = (1 << bit_depth) - colors[0] - min_val;
aom_write_literal(w, bits - min_bits, 2);
for (int i = 0; i < num - 1; ++i) {
aom_write_literal(w, deltas[i] - min_val, bits);
range -= deltas[i];
bits = AOMMIN(bits, av1_ceil_log2(range));
}
}
// Transmit luma palette color values. First signal if each color in the color
// cache is used. Those colors that are not in the cache are transmitted with
// delta encoding.
static void write_palette_colors_y(const MACROBLOCKD *const xd,
const PALETTE_MODE_INFO *const pmi,
int bit_depth, aom_writer *w) {
const int n = pmi->palette_size[0];
uint16_t color_cache[2 * PALETTE_MAX_SIZE];
const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
int out_cache_colors[PALETTE_MAX_SIZE];
uint8_t cache_color_found[2 * PALETTE_MAX_SIZE];
const int n_out_cache =
av1_index_color_cache(color_cache, n_cache, pmi->palette_colors, n,
cache_color_found, out_cache_colors);
int n_in_cache = 0;
for (int i = 0; i < n_cache && n_in_cache < n; ++i) {
const int found = cache_color_found[i];
aom_write_bit(w, found);
n_in_cache += found;
}
assert(n_in_cache + n_out_cache == n);
delta_encode_palette_colors(out_cache_colors, n_out_cache, bit_depth, 1, w);
}
// Write chroma palette color values. U channel is handled similarly to the luma
// channel. For v channel, either use delta encoding or transmit raw values
// directly, whichever costs less.
static void write_palette_colors_uv(const MACROBLOCKD *const xd,
const PALETTE_MODE_INFO *const pmi,
int bit_depth, aom_writer *w) {
const int n = pmi->palette_size[1];
const uint16_t *colors_u = pmi->palette_colors + PALETTE_MAX_SIZE;
const uint16_t *colors_v = pmi->palette_colors + 2 * PALETTE_MAX_SIZE;
// U channel colors.
uint16_t color_cache[2 * PALETTE_MAX_SIZE];
const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
int out_cache_colors[PALETTE_MAX_SIZE];
uint8_t cache_color_found[2 * PALETTE_MAX_SIZE];
const int n_out_cache = av1_index_color_cache(
color_cache, n_cache, colors_u, n, cache_color_found, out_cache_colors);
int n_in_cache = 0;
for (int i = 0; i < n_cache && n_in_cache < n; ++i) {
const int found = cache_color_found[i];
aom_write_bit(w, found);
n_in_cache += found;
}
delta_encode_palette_colors(out_cache_colors, n_out_cache, bit_depth, 0, w);
// V channel colors. Don't use color cache as the colors are not sorted.
const int max_val = 1 << bit_depth;
int zero_count = 0, min_bits_v = 0;
int bits_v =
av1_get_palette_delta_bits_v(pmi, bit_depth, &zero_count, &min_bits_v);
const int rate_using_delta =
2 + bit_depth + (bits_v + 1) * (n - 1) - zero_count;
const int rate_using_raw = bit_depth * n;
if (rate_using_delta < rate_using_raw) { // delta encoding
assert(colors_v[0] < (1 << bit_depth));
aom_write_bit(w, 1);
aom_write_literal(w, bits_v - min_bits_v, 2);
aom_write_literal(w, colors_v[0], bit_depth);
for (int i = 1; i < n; ++i) {
assert(colors_v[i] < (1 << bit_depth));
if (colors_v[i] == colors_v[i - 1]) { // No need to signal sign bit.
aom_write_literal(w, 0, bits_v);
continue;
}
const int delta = abs((int)colors_v[i] - colors_v[i - 1]);
const int sign_bit = colors_v[i] < colors_v[i - 1];
if (delta <= max_val - delta) {
aom_write_literal(w, delta, bits_v);
aom_write_bit(w, sign_bit);
} else {
aom_write_literal(w, max_val - delta, bits_v);
aom_write_bit(w, !sign_bit);
}
}
} else { // Transmit raw values.
aom_write_bit(w, 0);
for (int i = 0; i < n; ++i) {
assert(colors_v[i] < (1 << bit_depth));
aom_write_literal(w, colors_v[i], bit_depth);
}
}
}
#endif // CONFIG_PALETTE_DELTA_ENCODING
static void write_palette_mode_info(const AV1_COMMON *cm, const MACROBLOCKD *xd,
const MODE_INFO *const mi, aom_writer *w) {
const MB_MODE_INFO *const mbmi = &mi->mbmi;
const MODE_INFO *const above_mi = xd->above_mi;
const MODE_INFO *const left_mi = xd->left_mi;
const BLOCK_SIZE bsize = mbmi->sb_type;
const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
assert(bsize >= BLOCK_8X8 && bsize <= BLOCK_LARGEST);
const int block_palette_idx = bsize - BLOCK_8X8;
if (mbmi->mode == DC_PRED) {
const int n = pmi->palette_size[0];
int palette_y_mode_ctx = 0;
if (above_mi) {
palette_y_mode_ctx +=
(above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
}
if (left_mi) {
palette_y_mode_ctx +=
(left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
}
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(
w, n > 0,
xd->tile_ctx->palette_y_mode_cdf[block_palette_idx][palette_y_mode_ctx],
2);
#else
aom_write(
w, n > 0,
av1_default_palette_y_mode_prob[block_palette_idx][palette_y_mode_ctx]);
#endif
if (n > 0) {
aom_write_symbol(w, n - PALETTE_MIN_SIZE,
xd->tile_ctx->palette_y_size_cdf[block_palette_idx],
PALETTE_SIZES);
#if CONFIG_PALETTE_DELTA_ENCODING
write_palette_colors_y(xd, pmi, cm->bit_depth, w);
#else
for (int i = 0; i < n; ++i) {
assert(pmi->palette_colors[i] < (1 << cm->bit_depth));
aom_write_literal(w, pmi->palette_colors[i], cm->bit_depth);
}
#endif // CONFIG_PALETTE_DELTA_ENCODING
}
}
if (mbmi->uv_mode == UV_DC_PRED) {
const int n = pmi->palette_size[1];
const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, n > 0,
xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2);
#else
aom_write(w, n > 0, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx]);
#endif
if (n > 0) {
aom_write_symbol(w, n - PALETTE_MIN_SIZE,
xd->tile_ctx->palette_uv_size_cdf[block_palette_idx],
PALETTE_SIZES);
#if CONFIG_PALETTE_DELTA_ENCODING
write_palette_colors_uv(xd, pmi, cm->bit_depth, w);
#else
for (int i = 0; i < n; ++i) {
assert(pmi->palette_colors[PALETTE_MAX_SIZE + i] <
(1 << cm->bit_depth));
assert(pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] <
(1 << cm->bit_depth));
aom_write_literal(w, pmi->palette_colors[PALETTE_MAX_SIZE + i],
cm->bit_depth);
aom_write_literal(w, pmi->palette_colors[2 * PALETTE_MAX_SIZE + i],
cm->bit_depth);
}
#endif // CONFIG_PALETTE_DELTA_ENCODING
}
}
}
void av1_write_tx_type(const AV1_COMMON *const cm, const MACROBLOCKD *xd,
#if CONFIG_TXK_SEL
int blk_row, int blk_col, int block, int plane,
TX_SIZE tx_size,
#endif
aom_writer *w) {
MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
const int is_inter = is_inter_block(mbmi);
#if !CONFIG_TXK_SEL
const TX_SIZE sqr_up_tx_size =
txsize_sqr_up_map[max_txsize_rect_lookup[xd->mi[0]->mbmi.sb_type]];
const TX_SIZE tx_size =
is_inter ? AOMMAX(sub_tx_size_map[sqr_up_tx_size], mbmi->min_tx_size)
: mbmi->tx_size;
#endif // !CONFIG_TXK_SEL
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
#if !CONFIG_TXK_SEL
TX_TYPE tx_type = mbmi->tx_type;
#else
// Only y plane's tx_type is transmitted
if (plane > 0) return;
PLANE_TYPE plane_type = get_plane_type(plane);
TX_TYPE tx_type =
av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
#endif
if (!FIXED_TX_TYPE) {
const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
const BLOCK_SIZE bsize = mbmi->sb_type;
if (get_ext_tx_types(tx_size, bsize, is_inter, cm->reduced_tx_set_used) >
1 &&
((!cm->seg.enabled && cm->base_qindex > 0) ||
(cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
!mbmi->skip &&
!segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
#if CONFIG_MRC_TX
if (tx_type == MRC_DCT)
assert(mbmi->valid_mrc_mask && "Invalid MRC mask");
#endif // CONFIG_MRC_TX
const TxSetType tx_set_type = get_ext_tx_set_type(
tx_size, bsize, is_inter, cm->reduced_tx_set_used);
const int eset =
get_ext_tx_set(tx_size, bsize, is_inter, cm->reduced_tx_set_used);
// eset == 0 should correspond to a set with only DCT_DCT and there
// is no need to send the tx_type
assert(eset > 0);
assert(av1_ext_tx_used[tx_set_type][tx_type]);
#if !CONFIG_LGT_FROM_PRED
if (is_inter) {
aom_write_symbol(w, av1_ext_tx_ind[tx_set_type][tx_type],
ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
av1_num_ext_tx_set[tx_set_type]);
} else if (ALLOW_INTRA_EXT_TX) {
#if CONFIG_FILTER_INTRA
PREDICTION_MODE intra_dir;
if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0])
intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
.filter_intra_mode[0]];
else
intra_dir = mbmi->mode;
aom_write_symbol(
w, av1_ext_tx_ind[tx_set_type][tx_type],
ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][intra_dir],
av1_num_ext_tx_set[tx_set_type]);
#else
aom_write_symbol(
w, av1_ext_tx_ind[tx_set_type][tx_type],
ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
av1_num_ext_tx_set[tx_set_type]);
#endif
}
#else
// only signal tx_type when lgt is not allowed or not selected
if (is_inter) {
if (LGT_FROM_PRED_INTER) {
if (is_lgt_allowed(mbmi->mode, tx_size) && !cm->reduced_tx_set_used)
aom_write(w, mbmi->use_lgt, ec_ctx->inter_lgt_prob[square_tx_size]);
if (!mbmi->use_lgt)
aom_write_symbol(w, av1_ext_tx_ind[tx_set_type][tx_type],
ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
av1_num_ext_tx_set[tx_set_type]);
} else {
aom_write_symbol(w, av1_ext_tx_ind[tx_set_type][tx_type],
ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
av1_num_ext_tx_set[tx_set_type]);
}
} else if (ALLOW_INTRA_EXT_TX) {
if (LGT_FROM_PRED_INTRA) {
if (is_lgt_allowed(mbmi->mode, tx_size) && !cm->reduced_tx_set_used)
aom_write(w, mbmi->use_lgt,
ec_ctx->intra_lgt_prob[square_tx_size][mbmi->mode]);
if (!mbmi->use_lgt)
aom_write_symbol(
w, av1_ext_tx_ind[tx_set_type][tx_type],
ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
av1_num_ext_tx_set[tx_set_type]);
} else {
aom_write_symbol(
w, av1_ext_tx_ind[tx_set_type][tx_type],
ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
av1_num_ext_tx_set[tx_set_type]);
}
}
#endif // CONFIG_LGT_FROM_PRED
}
}
}
static void write_intra_mode(FRAME_CONTEXT *frame_ctx, BLOCK_SIZE bsize,
PREDICTION_MODE mode, aom_writer *w) {
aom_write_symbol(w, mode, frame_ctx->y_mode_cdf[size_group_lookup[bsize]],
INTRA_MODES);
}
static void write_intra_uv_mode(FRAME_CONTEXT *frame_ctx,
UV_PREDICTION_MODE uv_mode,
PREDICTION_MODE y_mode, aom_writer *w) {
#if !CONFIG_CFL
uv_mode = get_uv_mode(uv_mode);
#endif
aom_write_symbol(w, uv_mode, frame_ctx->uv_mode_cdf[y_mode], UV_INTRA_MODES);
}
#if CONFIG_CFL
static void write_cfl_alphas(FRAME_CONTEXT *const ec_ctx, int idx,
int joint_sign, aom_writer *w) {
aom_write_symbol(w, joint_sign, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS);
// Magnitudes are only signaled for nonzero codes.
if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
aom_write_symbol(w, CFL_IDX_U(idx), cdf_u, CFL_ALPHABET_SIZE);
}
if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
aom_write_symbol(w, CFL_IDX_V(idx), cdf_v, CFL_ALPHABET_SIZE);
}
}
#endif
static void pack_inter_mode_mvs(AV1_COMP *cpi, const int mi_row,
const int mi_col, aom_writer *w) {
AV1_COMMON *const cm = &cpi->common;
MACROBLOCK *const x = &cpi->td.mb;
MACROBLOCKD *const xd = &x->e_mbd;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
const MODE_INFO *mi = xd->mi[0];
const struct segmentation *const seg = &cm->seg;
struct segmentation_probs *const segp = &ec_ctx->seg;
const MB_MODE_INFO *const mbmi = &mi->mbmi;
const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
const PREDICTION_MODE mode = mbmi->mode;
const int segment_id = mbmi->segment_id;
const BLOCK_SIZE bsize = mbmi->sb_type;
const int allow_hp = cm->allow_high_precision_mv;
const int is_inter = is_inter_block(mbmi);
const int is_compound = has_second_ref(mbmi);
int skip, ref;
(void)mi_row;
(void)mi_col;
if (seg->update_map) {
if (seg->temporal_update) {
const int pred_flag = mbmi->seg_id_predicted;
#if CONFIG_NEW_MULTISYMBOL
aom_cdf_prob *pred_cdf = av1_get_pred_cdf_seg_id(segp, xd);
aom_write_symbol(w, pred_flag, pred_cdf, 2);
#else
aom_prob pred_prob = av1_get_pred_prob_seg_id(segp, xd);
aom_write(w, pred_flag, pred_prob);
#endif
if (!pred_flag) write_segment_id(w, seg, segp, segment_id);
} else {
write_segment_id(w, seg, segp, segment_id);
}
}
skip = write_skip(cm, xd, segment_id, mi, w);
if (cm->delta_q_present_flag) {
int super_block_upper_left = ((mi_row & (cm->mib_size - 1)) == 0) &&
((mi_col & (cm->mib_size - 1)) == 0);
if ((bsize != cm->sb_size || skip == 0) && super_block_upper_left) {
assert(mbmi->current_q_index > 0);
int reduced_delta_qindex =
(mbmi->current_q_index - xd->prev_qindex) / cm->delta_q_res;
write_delta_qindex(cm, xd, reduced_delta_qindex, w);
xd->prev_qindex = mbmi->current_q_index;
#if CONFIG_EXT_DELTA_Q
#if CONFIG_LOOPFILTER_LEVEL
if (cm->delta_lf_present_flag) {
if (cm->delta_lf_multi) {
for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
int reduced_delta_lflevel =
(mbmi->curr_delta_lf[lf_id] - xd->prev_delta_lf[lf_id]) /
cm->delta_lf_res;
write_delta_lflevel(cm, xd, lf_id, reduced_delta_lflevel, w);
xd->prev_delta_lf[lf_id] = mbmi->curr_delta_lf[lf_id];
}
} else {
int reduced_delta_lflevel =
(mbmi->current_delta_lf_from_base - xd->prev_delta_lf_from_base) /
cm->delta_lf_res;
write_delta_lflevel(cm, xd, -1, reduced_delta_lflevel, w);
xd->prev_delta_lf_from_base = mbmi->current_delta_lf_from_base;
}
}
#else
if (cm->delta_lf_present_flag) {
int reduced_delta_lflevel =
(mbmi->current_delta_lf_from_base - xd->prev_delta_lf_from_base) /
cm->delta_lf_res;
write_delta_lflevel(cm, xd, reduced_delta_lflevel, w);
xd->prev_delta_lf_from_base = mbmi->current_delta_lf_from_base;
}
#endif // CONFIG_LOOPFILTER_LEVEL
#endif // CONFIG_EXT_DELTA_Q
}
}
write_is_inter(cm, xd, mbmi->segment_id, w, is_inter);
if (cm->tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
!(is_inter && skip) && !xd->lossless[segment_id]) {
if (is_inter) { // This implies skip flag is 0.
const TX_SIZE max_tx_size = get_vartx_max_txsize(mbmi, bsize, 0);
const int bh = tx_size_high_unit[max_tx_size];
const int bw = tx_size_wide_unit[max_tx_size];
const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
int idx, idy;
for (idy = 0; idy < height; idy += bh)
for (idx = 0; idx < width; idx += bw)
write_tx_size_vartx(cm, xd, mbmi, max_tx_size, 0, idy, idx, w);
#if CONFIG_RECT_TX_EXT
if (is_quarter_tx_allowed(xd, mbmi, is_inter_block(mbmi)) &&
quarter_txsize_lookup[bsize] != max_tx_size &&
(mbmi->tx_size == quarter_txsize_lookup[bsize] ||
mbmi->tx_size == max_tx_size)) {
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, mbmi->tx_size != max_tx_size,
cm->fc->quarter_tx_size_cdf, 2);
#else
aom_write(w, mbmi->tx_size != max_tx_size,
cm->fc->quarter_tx_size_prob);
#endif
}
#endif
} else {
set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, skip, xd);
write_selected_tx_size(cm, xd, w);
}
} else {
set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, skip, xd);
}
if (!is_inter) {
write_intra_mode(ec_ctx, bsize, mode, w);
if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
xd->plane[1].subsampling_y)) {
write_intra_uv_mode(ec_ctx, mbmi->uv_mode, mode, w);
#if CONFIG_CFL
if (mbmi->uv_mode == UV_CFL_PRED) {
write_cfl_alphas(ec_ctx, mbmi->cfl_alpha_idx, mbmi->cfl_alpha_signs, w);
}
#endif
}
#if CONFIG_EXT_INTRA
write_intra_angle_info(xd, ec_ctx, w);
#endif // CONFIG_EXT_INTRA
if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
write_palette_mode_info(cm, xd, mi, w);
#if CONFIG_FILTER_INTRA
write_filter_intra_mode_info(cm, xd, mbmi, w);
#endif // CONFIG_FILTER_INTRA
} else {
int16_t mode_ctx;
write_ref_frames(cm, xd, w);
#if CONFIG_JNT_COMP
if (has_second_ref(mbmi)) {
const int comp_index_ctx = get_comp_index_context(cm, xd);
aom_write(w, mbmi->compound_idx,
ec_ctx->compound_index_probs[comp_index_ctx]);
}
#endif // CONFIG_JNT_COMP
#if CONFIG_COMPOUND_SINGLEREF
if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) {
// NOTE: Handle single ref comp mode
if (!is_compound)
aom_write(w, is_inter_singleref_comp_mode(mode),
av1_get_inter_mode_prob(cm, xd));
}
#endif // CONFIG_COMPOUND_SINGLEREF
#if CONFIG_COMPOUND_SINGLEREF
if (is_compound || is_inter_singleref_comp_mode(mode))
#else // !CONFIG_COMPOUND_SINGLEREF
if (is_compound)
#endif // CONFIG_COMPOUND_SINGLEREF
mode_ctx = mbmi_ext->compound_mode_context[mbmi->ref_frame[0]];
else
mode_ctx = av1_mode_context_analyzer(mbmi_ext->mode_context,
mbmi->ref_frame, bsize, -1);
// If segment skip is not enabled code the mode.
if (!segfeature_active(seg, segment_id, SEG_LVL_SKIP)) {
if (is_inter_compound_mode(mode))
write_inter_compound_mode(cm, xd, w, mode, mode_ctx);
#if CONFIG_COMPOUND_SINGLEREF
else if (is_inter_singleref_comp_mode(mode))
write_inter_singleref_comp_mode(xd, w, mode, mode_ctx);
#endif // CONFIG_COMPOUND_SINGLEREF
else if (is_inter_singleref_mode(mode))
write_inter_mode(w, mode, ec_ctx, mode_ctx);
if (mode == NEWMV || mode == NEW_NEWMV ||
#if CONFIG_COMPOUND_SINGLEREF
mbmi->mode == SR_NEW_NEWMV ||
#endif // CONFIG_COMPOUND_SINGLEREF
have_nearmv_in_inter_mode(mode))
write_drl_idx(ec_ctx, mbmi, mbmi_ext, w);
else
assert(mbmi->ref_mv_idx == 0);
}
if (mode == NEWMV || mode == NEW_NEWMV) {
int_mv ref_mv;
for (ref = 0; ref < 1 + is_compound; ++ref) {
int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
int nmv_ctx =
av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
mbmi_ext->ref_mv_stack[rf_type], ref, mbmi->ref_mv_idx);
nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx];
ref_mv = mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0];
av1_encode_mv(cpi, w, &mbmi->mv[ref].as_mv, &ref_mv.as_mv, nmvc,
allow_hp);
}
} else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
int nmv_ctx =
av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
mbmi_ext->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx];
av1_encode_mv(cpi, w, &mbmi->mv[1].as_mv,
&mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0].as_mv, nmvc,
allow_hp);
} else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
int nmv_ctx =
av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx];
av1_encode_mv(cpi, w, &mbmi->mv[0].as_mv,
&mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_mv, nmvc,
allow_hp);
#if CONFIG_COMPOUND_SINGLEREF
} else if ( // mode == SR_NEAREST_NEWMV ||
mode == SR_NEAR_NEWMV || mode == SR_ZERO_NEWMV ||
mode == SR_NEW_NEWMV) {
int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
int nmv_ctx =
av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx];
int_mv ref_mv = mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0];
if (mode == SR_NEW_NEWMV)
av1_encode_mv(cpi, w, &mbmi->mv[0].as_mv, &ref_mv.as_mv, nmvc,
allow_hp);
av1_encode_mv(cpi, w, &mbmi->mv[1].as_mv, &ref_mv.as_mv, nmvc, allow_hp);
#endif // CONFIG_COMPOUND_SINGLEREF
}
if (cpi->common.reference_mode != COMPOUND_REFERENCE &&
cpi->common.allow_interintra_compound && is_interintra_allowed(mbmi)) {
const int interintra = mbmi->ref_frame[1] == INTRA_FRAME;
const int bsize_group = size_group_lookup[bsize];
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, interintra, ec_ctx->interintra_cdf[bsize_group], 2);
#else
aom_write(w, interintra, cm->fc->interintra_prob[bsize_group]);
#endif
if (interintra) {
aom_write_symbol(w, mbmi->interintra_mode,
ec_ctx->interintra_mode_cdf[bsize_group],
INTERINTRA_MODES);
if (is_interintra_wedge_used(bsize)) {
#if CONFIG_NEW_MULTISYMBOL
aom_write_symbol(w, mbmi->use_wedge_interintra,
ec_ctx->wedge_interintra_cdf[bsize], 2);
#else
aom_write(w, mbmi->use_wedge_interintra,
cm->fc->wedge_interintra_prob[bsize]);
#endif
if (mbmi->use_wedge_interintra) {
aom_write_literal(w, mbmi->interintra_wedge_index,
get_wedge_bits_lookup(bsize));
assert(mbmi->interintra_wedge_sign == 0);
}
}
}
}
if (mbmi->ref_frame[1] != INTRA_FRAME) write_motion_mode(cm, xd, mi, w);
#if CONFIG_NCOBMC_ADAPT_WEIGHT
write_ncobmc_mode(xd, mi, w);
#endif
if (
#if CONFIG_COMPOUND_SINGLEREF
is_inter_anyref_comp_mode(mbmi->mode) &&
#else // !CONFIG_COMPOUND_SINGLEREF
cpi->common.reference_mode != SINGLE_REFERENCE &&
is_inter_compound_mode(mbmi->mode) &&
#endif // CONFIG_COMPOUND_SINGLEREF
mbmi->motion_mode == SIMPLE_TRANSLATION &&
is_any_masked_compound_used(bsize)) {
#if CONFIG_JNT_COMP
if (cm->allow_masked_compound && mbmi->compound_idx)
#else
if (cm->allow_masked_compound)
#endif // CONFIG_JNT_COMP
{
if (!is_interinter_compound_used(COMPOUND_WEDGE, bsize))
aom_write_bit(w, mbmi->interinter_compound_type == COMPOUND_AVERAGE);
else
aom_write_symbol(w, mbmi->interinter_compound_type,
ec_ctx->compound_type_cdf[bsize], COMPOUND_TYPES);
if (is_interinter_compound_used(COMPOUND_WEDGE, bsize) &&
mbmi->interinter_compound_type == COMPOUND_WEDGE) {
aom_write_literal(w, mbmi->wedge_index, get_wedge_bits_lookup(bsize));
aom_write_bit(w, mbmi->wedge_sign);
}
if (mbmi->interinter_compound_type == COMPOUND_SEG) {
aom_write_literal(w, mbmi->mask_type, MAX_SEG_MASK_BITS);
}
}
}
write_mb_interp_filter(cpi, xd, w);
}
#if !CONFIG_TXK_SEL
av1_write_tx_type(cm, xd, w);
#endif // !CONFIG_TXK_SEL
}
#if CONFIG_INTRABC
static void write_intrabc_info(AV1_COMMON *cm, MACROBLOCKD *xd,
const MB_MODE_INFO_EXT *mbmi_ext,
int enable_tx_size, aom_writer *w) {
const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
int use_intrabc = is_intrabc_block(mbmi);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
aom_write_symbol(w, use_intrabc, ec_ctx->intrabc_cdf, 2);
if (use_intrabc) {
assert(mbmi->mode == DC_PRED);
assert(mbmi->uv_mode == UV_DC_PRED);
if ((enable_tx_size && !mbmi->skip)) {
const BLOCK_SIZE bsize = mbmi->sb_type;
const TX_SIZE max_tx_size = get_vartx_max_txsize(mbmi, bsize, 0);
const int bh = tx_size_high_unit[max_tx_size];
const int bw = tx_size_wide_unit[max_tx_size];
const int width = block_size_wide[bsize] >> tx_size_wide_log2[0];
const int height = block_size_high[bsize] >> tx_size_wide_log2[0];
int idx, idy;
for (idy = 0; idy < height; idy += bh) {
for (idx = 0; idx < width; idx += bw) {
write_tx_size_vartx(cm, xd, mbmi, max_tx_size, 0, idy, idx, w);
}
}
} else {
set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
}
int_mv dv_ref = mbmi_ext->ref_mvs[INTRA_FRAME][0];
av1_encode_dv(w, &mbmi->mv[0].as_mv, &dv_ref.as_mv, &ec_ctx->ndvc);
#if !CONFIG_TXK_SEL
av1_write_tx_type(cm, xd, w);
#endif // !CONFIG_TXK_SEL
}
}
#endif // CONFIG_INTRABC
static void write_mb_modes_kf(AV1_COMMON *cm, MACROBLOCKD *xd,
#if CONFIG_INTRABC
const MB_MODE_INFO_EXT *mbmi_ext,
#endif // CONFIG_INTRABC
const int mi_row, const int mi_col,
aom_writer *w) {
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
const struct segmentation *const seg = &cm->seg;
struct segmentation_probs *const segp = &ec_ctx->seg;
const MODE_INFO *const mi = xd->mi[0];
const MODE_INFO *const above_mi = xd->above_mi;
const MODE_INFO *const left_mi = xd->left_mi;
const MB_MODE_INFO *const mbmi = &mi->mbmi;
const BLOCK_SIZE bsize = mbmi->sb_type;
(void)mi_row;
(void)mi_col;
if (seg->update_map) write_segment_id(w, seg, segp, mbmi->segment_id);
const int skip = write_skip(cm, xd, mbmi->segment_id, mi, w);
if (cm->delta_q_present_flag) {
int super_block_upper_left = ((mi_row & (cm->mib_size - 1)) == 0) &&
((mi_col & (cm->mib_size - 1)) == 0);
if ((bsize != cm->sb_size || skip == 0) && super_block_upper_left) {
assert(mbmi->current_q_index > 0);
int reduced_delta_qindex =
(mbmi->current_q_index - xd->prev_qindex) / cm->delta_q_res;
write_delta_qindex(cm, xd, reduced_delta_qindex, w);
xd->prev_qindex = mbmi->current_q_index;
#if CONFIG_EXT_DELTA_Q
#if CONFIG_LOOPFILTER_LEVEL
if (cm->delta_lf_present_flag) {
if (cm->delta_lf_multi) {
for (int lf_id = 0; lf_id < FRAME_LF_COUNT; ++lf_id) {
int reduced_delta_lflevel =
(mbmi->curr_delta_lf[lf_id] - xd->prev_delta_lf[lf_id]) /
cm->delta_lf_res;
write_delta_lflevel(cm, xd, lf_id, reduced_delta_lflevel, w);
xd->prev_delta_lf[lf_id] = mbmi->curr_delta_lf[lf_id];
}
} else {
int reduced_delta_lflevel =
(mbmi->current_delta_lf_from_base - xd->prev_delta_lf_from_base) /
cm->delta_lf_res;
write_delta_lflevel(cm, xd, -1, reduced_delta_lflevel, w);
xd->prev_delta_lf_from_base = mbmi->current_delta_lf_from_base;
}
}
#else
if (cm->delta_lf_present_flag) {
int reduced_delta_lflevel =
(mbmi->current_delta_lf_from_base - xd->prev_delta_lf_from_base) /
cm->delta_lf_res;
write_delta_lflevel(cm, xd, reduced_delta_lflevel, w);
xd->prev_delta_lf_from_base = mbmi->current_delta_lf_from_base;
}
#endif // CONFIG_LOOPFILTER_LEVEL
#endif // CONFIG_EXT_DELTA_Q
}
}
int enable_tx_size = cm->tx_mode == TX_MODE_SELECT &&
block_signals_txsize(bsize) &&
!xd->lossless[mbmi->segment_id];
#if CONFIG_INTRABC
if (av1_allow_intrabc(bsize, cm)) {
write_intrabc_info(cm, xd, mbmi_ext, enable_tx_size, w);
if (is_intrabc_block(mbmi)) return;
}
#endif // CONFIG_INTRABC
if (enable_tx_size) write_selected_tx_size(cm, xd, w);
#if CONFIG_INTRABC
if (cm->allow_screen_content_tools)
set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, mbmi->skip, xd);
#endif // CONFIG_INTRABC
write_intra_mode_kf(cm, ec_ctx, mi, above_mi, left_mi, 0, mbmi->mode, w);
if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x,
xd->plane[1].subsampling_y)) {
write_intra_uv_mode(ec_ctx, mbmi->uv_mode, mbmi->mode, w);
#if CONFIG_CFL
if (mbmi->uv_mode == UV_CFL_PRED) {
write_cfl_alphas(ec_ctx, mbmi->cfl_alpha_idx, mbmi->cfl_alpha_signs, w);
}
#endif
}
#if CONFIG_EXT_INTRA
write_intra_angle_info(xd, ec_ctx, w);
#endif // CONFIG_EXT_INTRA
if (av1_allow_palette(cm->allow_screen_content_tools, bsize))
write_palette_mode_info(cm, xd, mi, w);
#if CONFIG_FILTER_INTRA
write_filter_intra_mode_info(cm, xd, mbmi, w);
#endif // CONFIG_FILTER_INTRA
#if !CONFIG_TXK_SEL
av1_write_tx_type(cm, xd, w);
#endif // !CONFIG_TXK_SEL
}
#if CONFIG_RD_DEBUG
static void dump_mode_info(MODE_INFO *mi) {
printf("\nmi->mbmi.mi_row == %d\n", mi->mbmi.mi_row);
printf("&& mi->mbmi.mi_col == %d\n", mi->mbmi.mi_col);
printf("&& mi->mbmi.sb_type == %d\n", mi->mbmi.sb_type);
printf("&& mi->mbmi.tx_size == %d\n", mi->mbmi.tx_size);
if (mi->mbmi.sb_type >= BLOCK_8X8) {
printf("&& mi->mbmi.mode == %d\n", mi->mbmi.mode);
} else {
printf("&& mi->bmi[0].as_mode == %d\n", mi->bmi[0].as_mode);
}
}
static int rd_token_stats_mismatch(RD_STATS *rd_stats, TOKEN_STATS *token_stats,
int plane) {
if (rd_stats->txb_coeff_cost[plane] != token_stats->cost) {
int r, c;
printf("\nplane %d rd_stats->txb_coeff_cost %d token_stats->cost %d\n",
plane, rd_stats->txb_coeff_cost[plane], token_stats->cost);
printf("rd txb_coeff_cost_map\n");
for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) {
for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) {
printf("%d ", rd_stats->txb_coeff_cost_map[plane][r][c]);
}
printf("\n");
}
printf("pack txb_coeff_cost_map\n");
for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) {
for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) {
printf("%d ", token_stats->txb_coeff_cost_map[r][c]);
}
printf("\n");
}
return 1;
}
return 0;
}
#endif
#if ENC_MISMATCH_DEBUG
static void enc_dump_logs(AV1_COMP *cpi, int mi_row, int mi_col) {
AV1_COMMON *const cm = &cpi->common;
MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
MODE_INFO *m;
xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col);
m = xd->mi[0];
if (is_inter_block(&m->mbmi)) {
#define FRAME_TO_CHECK 1
if (cm->current_video_frame == FRAME_TO_CHECK && cm->show_frame == 1) {
const MB_MODE_INFO *const mbmi = &m->mbmi;
const BLOCK_SIZE bsize = mbmi->sb_type;
int_mv mv[2];
int is_comp_ref = has_second_ref(&m->mbmi);
int ref;
for (ref = 0; ref < 1 + is_comp_ref; ++ref)
mv[ref].as_mv = m->mbmi.mv[ref].as_mv;
if (!is_comp_ref) {
#if CONFIG_COMPOUND_SINGLEREF
if (is_inter_singleref_comp_mode(m->mbmi.mode))
mv[1].as_mv = m->mbmi.mv[1].as_mv;
else
#endif // CONFIG_COMPOUND_SINGLEREF
mv[1].as_int = 0;
}
MACROBLOCK *const x = &cpi->td.mb;
const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
const int16_t mode_ctx = av1_mode_context_analyzer(
mbmi_ext->mode_context, mbmi->ref_frame, bsize, -1);
const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
int16_t zeromv_ctx = -1;
int16_t refmv_ctx = -1;
if (mbmi->mode != NEWMV) {
zeromv_ctx = (mode_ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) {
assert(mbmi->mode == GLOBALMV);
}
if (mbmi->mode != GLOBALMV) {
refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6;
if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7;
if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8;
}
}
int8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
printf(
"=== ENCODER ===: "
"Frame=%d, (mi_row,mi_col)=(%d,%d), mode=%d, bsize=%d, "
"show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
"ref[1]=%d, motion_mode=%d, inter_mode_ctx=%d, mode_ctx=%d, "
"newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d\n",
cm->current_video_frame, mi_row, mi_col, mbmi->mode, bsize,
cm->show_frame, mv[0].as_mv.row, mv[0].as_mv.col, mv[1].as_mv.row,
mv[1].as_mv.col, mbmi->ref_frame[0], mbmi->ref_frame[1],
mbmi->motion_mode, mbmi_ext->mode_context[ref_frame_type], mode_ctx,
newmv_ctx, zeromv_ctx, refmv_ctx);
}
}
}
#endif // ENC_MISMATCH_DEBUG
static void write_mbmi_b(AV1_COMP *cpi, const TileInfo *const tile,
aom_writer *w, int mi_row, int mi_col) {
AV1_COMMON *const cm = &cpi->common;
MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
MODE_INFO *m;
int bh, bw;
xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col);
m = xd->mi[0];
assert(m->mbmi.sb_type <= cm->sb_size ||
(m->mbmi.sb_type >= BLOCK_SIZES && m->mbmi.sb_type < BLOCK_SIZES_ALL));
bh = mi_size_high[m->mbmi.sb_type];
bw = mi_size_wide[m->mbmi.sb_type];
cpi->td.mb.mbmi_ext = cpi->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
#if CONFIG_DEPENDENT_HORZTILES
cm->dependent_horz_tiles,
#endif // CONFIG_DEPENDENT_HORZTILES
cm->mi_rows, cm->mi_cols);
if (frame_is_intra_only(cm)) {
#if CONFIG_INTRABC
if (cm->allow_screen_content_tools) {
xd->above_txfm_context =
cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
xd->left_txfm_context = xd->left_txfm_context_buffer +
((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
}
#endif // CONFIG_INTRABC
write_mb_modes_kf(cm, xd,
#if CONFIG_INTRABC
cpi->td.mb.mbmi_ext,
#endif // CONFIG_INTRABC
mi_row, mi_col, w);
} else {
xd->above_txfm_context =
cm->above_txfm_context + (mi_col << TX_UNIT_WIDE_LOG2);
xd->left_txfm_context = xd->left_txfm_context_buffer +
((mi_row & MAX_MIB_MASK) << TX_UNIT_HIGH_LOG2);
// has_subpel_mv_component needs the ref frame buffers set up to look
// up if they are scaled. has_subpel_mv_component is in turn needed by
// write_switchable_interp_filter, which is called by pack_inter_mode_mvs.
set_ref_ptrs(cm, xd, m->mbmi.ref_frame[0], m->mbmi.ref_frame[1]);
#if CONFIG_COMPOUND_SINGLEREF
if (!has_second_ref(&m->mbmi) && is_inter_singleref_comp_mode(m->mbmi.mode))
xd->block_refs[1] = xd->block_refs[0];
#endif // CONFIG_COMPOUND_SINGLEREF
#if ENC_MISMATCH_DEBUG
enc_dump_logs(cpi, mi_row, mi_col);
#endif // ENC_MISMATCH_DEBUG
pack_inter_mode_mvs(cpi, mi_row, mi_col, w);
}
}
static void write_tokens_b(AV1_COMP *cpi, const TileInfo *const tile,
aom_writer *w, const TOKENEXTRA **tok,
const TOKENEXTRA *const tok_end, int mi_row,
int mi_col) {
AV1_COMMON *const cm = &cpi->common;
MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
const int mi_offset = mi_row * cm->mi_stride + mi_col;
MODE_INFO *const m = *(cm->mi_grid_visible + mi_offset);
MB_MODE_INFO *const mbmi = &m->mbmi;
int plane;
int bh, bw;
#if CONFIG_LV_MAP
MACROBLOCK *const x = &cpi->td.mb;
(void)tok;
(void)tok_end;
#endif
xd->mi = cm->mi_grid_visible + mi_offset;
assert(mbmi->sb_type <= cm->sb_size ||
(mbmi->sb_type >= BLOCK_SIZES && mbmi->sb_type < BLOCK_SIZES_ALL));
bh = mi_size_high[mbmi->sb_type];
bw = mi_size_wide[mbmi->sb_type];
cpi->td.mb.mbmi_ext = cpi->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw,
#if CONFIG_DEPENDENT_HORZTILES
cm->dependent_horz_tiles,
#endif // CONFIG_DEPENDENT_HORZTILES
cm->mi_rows, cm->mi_cols);
for (plane = 0; plane <= 1; ++plane) {
const uint8_t palette_size_plane =
mbmi->palette_mode_info.palette_size[plane];
if (palette_size_plane > 0) {
#if CONFIG_INTRABC
assert(mbmi->use_intrabc == 0);
#endif
int rows, cols;
assert(mbmi->sb_type >= BLOCK_8X8);
av1_get_block_dimensions(mbmi->sb_type, plane, xd, NULL, NULL, &rows,
&cols);
assert(*tok < tok_end);
pack_map_tokens(w, tok, palette_size_plane, rows * cols);
#if !CONFIG_LV_MAP
assert(*tok < tok_end + mbmi->skip);
#endif // !CONFIG_LV_MAP
}
}
if (!mbmi->skip) {
#if !CONFIG_LV_MAP
assert(*tok < tok_end);
#endif
for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
if (!is_chroma_reference(mi_row, mi_col, mbmi->sb_type,
xd->plane[plane].subsampling_x,
xd->plane[plane].subsampling_y)) {
#if !CONFIG_LV_MAP
(*tok)++;
#endif // !CONFIG_LV_MAP
continue;
}
const struct macroblockd_plane *const pd = &xd->plane[plane];
BLOCK_SIZE bsize = mbmi->sb_type;
const BLOCK_SIZE plane_bsize =
AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
const int num_4x4_w =
block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
const int num_4x4_h =
block_size_high[plane_bsize] >> tx_size_wide_log2[0];
int row, col;
TOKEN_STATS token_stats;
init_token_stats(&token_stats);
const BLOCK_SIZE max_unit_bsize = get_plane_block_size(BLOCK_64X64, pd);
int mu_blocks_wide =
block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
int mu_blocks_high =
block_size_high[max_unit_bsize] >> tx_size_high_log2[0];
mu_blocks_wide = AOMMIN(num_4x4_w, mu_blocks_wide);
mu_blocks_high = AOMMIN(num_4x4_h, mu_blocks_high);
if (is_inter_block(mbmi)) {
const TX_SIZE max_tx_size = get_vartx_max_txsize(
mbmi, plane_bsize, pd->subsampling_x || pd->subsampling_y);
int block = 0;
const int step =
tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
const int bkw = tx_size_wide_unit[max_tx_size];
const int bkh = tx_size_high_unit[max_tx_size];
assert(bkw <= mu_blocks_wide);
assert(bkh <= mu_blocks_high);
for (row = 0; row < num_4x4_h; row += mu_blocks_high) {
const int unit_height = AOMMIN(mu_blocks_high + row, num_4x4_h);
for (col = 0; col < num_4x4_w; col += mu_blocks_wide) {
int blk_row, blk_col;
const int unit_width = AOMMIN(mu_blocks_wide + col, num_4x4_w);
for (blk_row = row; blk_row < unit_height; blk_row += bkh) {
for (blk_col = col; blk_col < unit_width; blk_col += bkw) {
pack_txb_tokens(w,
#if CONFIG_LV_MAP
cm, x,
#endif
tok, tok_end, xd, mbmi, plane, plane_bsize,
cm->bit_depth, block, blk_row, blk_col,
max_tx_size, &token_stats);
block += step;
}
}
}
}
#if CONFIG_RD_DEBUG
if (mbmi->sb_type >= BLOCK_8X8 &&
rd_token_stats_mismatch(&mbmi->rd_stats, &token_stats, plane)) {
dump_mode_info(m);
assert(0);
}
#endif // CONFIG_RD_DEBUG
} else {
#if CONFIG_LV_MAP
av1_write_coeffs_mb(cm, x, w, plane);
#else
const TX_SIZE tx = av1_get_tx_size(plane, xd);
const int bkw = tx_size_wide_unit[tx];
const int bkh = tx_size_high_unit[tx];
int blk_row, blk_col;
for (row = 0; row < num_4x4_h; row += mu_blocks_high) {
for (col = 0; col < num_4x4_w; col += mu_blocks_wide) {
const int unit_height = AOMMIN(mu_blocks_high + row, num_4x4_h);
const int unit_width = AOMMIN(mu_blocks_wide + col, num_4x4_w);
for (blk_row = row; blk_row < unit_height; blk_row += bkh) {
for (blk_col = col; blk_col < unit_width; blk_col += bkw) {
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
TX_TYPE tx_type =
av1_get_tx_type(plane ? PLANE_TYPE_UV : PLANE_TYPE_Y, xd,
blk_row, blk_col, 0, tx);
#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
pack_mb_tokens(w, tok, tok_end, cm->bit_depth, tx,
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
tx_type, is_inter_block(mbmi),
#endif // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
&token_stats);
}
}
}
}
#endif // CONFIG_LV_MAP
}
#if !CONFIG_LV_MAP
assert(*tok < tok_end && (*tok)->token == EOSB_TOKEN);
(*tok)++;
#endif
}
}
}
#if NC_MODE_INFO
static void write_tokens_sb(AV1_COMP *cpi, const TileInfo *const tile,
aom_writer *w, const TOKENEXTRA **tok,
const TOKENEXTRA *const tok_end, int mi_row,
int mi_col, BLOCK_SIZE bsize) {
const AV1_COMMON *const cm = &cpi->common;
const int hbs = mi_size_wide[bsize] / 2;
PARTITION_TYPE partition;
BLOCK_SIZE subsize;
if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
partition = get_partition(cm, mi_row, mi_col, bsize);
subsize = get_subsize(bsize, partition);
switch (partition) {
case PARTITION_NONE:
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
break;
case PARTITION_HORZ:
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
if (mi_row + hbs < cm->mi_rows)
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col);
break;
case PARTITION_VERT:
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
if (mi_col + hbs < cm->mi_cols)
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs);
break;
case PARTITION_SPLIT:
write_tokens_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, subsize);
write_tokens_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs,
subsize);
write_tokens_sb(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col,
subsize);
write_tokens_sb(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs,
subsize);
break;
#if CONFIG_EXT_PARTITION_TYPES
#if CONFIG_EXT_PARTITION_TYPES_AB
#error NC_MODE_INFO+MOTION_VAR not yet supported for new HORZ/VERT_AB partitions
#endif
case PARTITION_HORZ_A:
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs);
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col);
break;
case PARTITION_HORZ_B:
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col);
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs);
break;
case PARTITION_VERT_A:
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col);
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs);
break;
case PARTITION_VERT_B:
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs);
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs);
break;
#endif // CONFIG_EXT_PARTITION_TYPES
default: assert(0);
}
}
#endif
static void write_modes_b(AV1_COMP *cpi, const TileInfo *const tile,
aom_writer *w, const TOKENEXTRA **tok,
const TOKENEXTRA *const tok_end, int mi_row,
int mi_col) {
write_mbmi_b(cpi, tile, w, mi_row, mi_col);
#if NC_MODE_INFO
(void)tok;
(void)tok_end;
#else
write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
#endif
}
static void write_partition(const AV1_COMMON *const cm,
const MACROBLOCKD *const xd, int hbs, int mi_row,
int mi_col, PARTITION_TYPE p, BLOCK_SIZE bsize,
aom_writer *w) {
const int has_rows = (mi_row + hbs) < cm->mi_rows;
const int has_cols = (mi_col + hbs) < cm->mi_cols;
const int is_partition_point = bsize >= BLOCK_8X8;
const int ctx = is_partition_point
? partition_plane_context(xd, mi_row, mi_col,
#if CONFIG_UNPOISON_PARTITION_CTX
has_rows, has_cols,
#endif
bsize)
: 0;
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
(void)cm;
if (!is_partition_point) return;
if (has_rows && has_cols) {
#if CONFIG_EXT_PARTITION_TYPES
const int num_partition_types =
(mi_width_log2_lookup[bsize] > mi_width_log2_lookup[BLOCK_8X8])
? EXT_PARTITION_TYPES
: PARTITION_TYPES;
#else
const int num_partition_types = PARTITION_TYPES;
#endif
aom_write_symbol(w, p, ec_ctx->partition_cdf[ctx], num_partition_types);
} else if (!has_rows && has_cols) {
assert(p == PARTITION_SPLIT || p == PARTITION_HORZ);
assert(bsize > BLOCK_8X8);
aom_cdf_prob cdf[2];
partition_gather_vert_alike(cdf, ec_ctx->partition_cdf[ctx]);
aom_write_cdf(w, p == PARTITION_SPLIT, cdf, 2);
} else if (has_rows && !has_cols) {
assert(p == PARTITION_SPLIT || p == PARTITION_VERT);
assert(bsize > BLOCK_8X8);
aom_cdf_prob cdf[2];
partition_gather_horz_alike(cdf, ec_ctx->partition_cdf[ctx]);
aom_write_cdf(w, p == PARTITION_SPLIT, cdf, 2);
} else {
assert(p == PARTITION_SPLIT);
}
}
static void write_modes_sb(AV1_COMP *const cpi, const TileInfo *const tile,
aom_writer *const w, const TOKENEXTRA **tok,
const TOKENEXTRA *const tok_end, int mi_row,
int mi_col, BLOCK_SIZE bsize) {
const AV1_COMMON *const cm = &cpi->common;
MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
const int hbs = mi_size_wide[bsize] / 2;
#if CONFIG_EXT_PARTITION_TYPES
const int quarter_step = mi_size_wide[bsize] / 4;
int i;
#if CONFIG_EXT_PARTITION_TYPES_AB
const int qbs = mi_size_wide[bsize] / 4;
#endif // CONFIG_EXT_PARTITION_TYPES_AB
#endif // CONFIG_EXT_PARTITION_TYPES
const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize);
const BLOCK_SIZE subsize = get_subsize(bsize, partition);
if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
write_partition(cm, xd, hbs, mi_row, mi_col, partition, bsize, w);
switch (partition) {
case PARTITION_NONE:
write_modes_b