summaryrefslogtreecommitdiffstats
path: root/third_party/aom/av1/encoder/encodemb.c
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/aom/av1/encoder/encodemb.c')
-rw-r--r--third_party/aom/av1/encoder/encodemb.c978
1 files changed, 224 insertions, 754 deletions
diff --git a/third_party/aom/av1/encoder/encodemb.c b/third_party/aom/av1/encoder/encodemb.c
index 7c9781533..e7f4d313d 100644
--- a/third_party/aom/av1/encoder/encodemb.c
+++ b/third_party/aom/av1/encoder/encodemb.c
@@ -112,19 +112,9 @@ void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
// These numbers are empirically obtained.
static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
-#if CONFIG_EC_ADAPT
{ 10, 7 }, { 8, 5 },
-#else
- { 10, 6 }, { 8, 6 },
-#endif
};
-#define UPDATE_RD_COST() \
- { \
- rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0); \
- rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1); \
- }
-
static INLINE unsigned int get_token_bit_costs(
unsigned int token_costs[2][COEFF_CONTEXTS][ENTROPY_TOKENS], int skip_eob,
int ctx, int token) {
@@ -133,23 +123,14 @@ static INLINE unsigned int get_token_bit_costs(
}
#if !CONFIG_LV_MAP
-#define USE_GREEDY_OPTIMIZE_B 0
-
-#if USE_GREEDY_OPTIMIZE_B
-
-typedef struct av1_token_state_greedy {
- int16_t token;
- tran_low_t qc;
- tran_low_t dqc;
-} av1_token_state_greedy;
static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
- int block, TX_SIZE tx_size, int ctx) {
+ int blk_row, int blk_col, int block,
+ TX_SIZE tx_size, int ctx) {
MACROBLOCKD *const xd = &mb->e_mbd;
struct macroblock_plane *const p = &mb->plane[plane];
struct macroblockd_plane *const pd = &xd->plane[plane];
const int ref = is_inter_block(&xd->mi[0]->mbmi);
- av1_token_state_greedy tokens[MAX_TX_SQUARE + 1][2];
uint8_t token_cache[MAX_TX_SQUARE];
const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
@@ -158,23 +139,27 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
const PLANE_TYPE plane_type = pd->plane_type;
const int16_t *const dequant_ptr = pd->dequant;
const uint8_t *const band_translate = get_band_translate(tx_size);
- TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
+ TX_TYPE tx_type =
+ av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
const SCAN_ORDER *const scan_order =
- get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
+ get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
const int16_t *const scan = scan_order->scan;
const int16_t *const nb = scan_order->neighbors;
int dqv;
const int shift = av1_get_tx_scale(tx_size);
#if CONFIG_AOM_QM
int seg_id = xd->mi[0]->mbmi.segment_id;
- const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!ref][tx_size];
+ // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
+ const qm_val_t *iqmatrix =
+ IS_2D_TRANSFORM(tx_type)
+ ? pd->seg_iqmatrix[seg_id][!ref][tx_size]
+ : cm->giqmatrix[NUM_QM_LEVELS - 1][0][0][tx_size];
#endif
#if CONFIG_NEW_QUANT
int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type);
const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
#endif // CONFIG_NEW_QUANT
int sz = 0;
- const int64_t rddiv = mb->rddiv;
int64_t rd_cost0, rd_cost1;
int16_t t0, t1;
int i, final_eob;
@@ -193,19 +178,8 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
int64_t rate0, rate1;
for (i = 0; i < eob; i++) {
const int rc = scan[i];
- int x = qcoeff[rc];
- t0 = av1_get_token(x);
-
- tokens[i][0].qc = x;
- tokens[i][0].token = t0;
- tokens[i][0].dqc = dqcoeff[rc];
-
- token_cache[rc] = av1_pt_energy_class[t0];
+ token_cache[rc] = av1_pt_energy_class[av1_get_token(qcoeff[rc])];
}
- tokens[eob][0].token = EOB_TOKEN;
- tokens[eob][0].qc = 0;
- tokens[eob][0].dqc = 0;
- tokens[eob][1] = tokens[eob][0];
unsigned int(*token_costs_ptr)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
token_costs;
@@ -213,20 +187,22 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
final_eob = 0;
int64_t eob_cost0, eob_cost1;
+ tran_low_t before_best_eob_qc = 0;
+ tran_low_t before_best_eob_dqc = 0;
const int ctx0 = ctx;
/* Record the r-d cost */
int64_t accu_rate = 0;
- int64_t accu_error = 0;
+ // Initialized to the worst possible error for the largest transform size.
+ // This ensures that it never goes negative.
+ int64_t accu_error = ((int64_t)1) << 50;
rate0 = get_token_bit_costs(*(token_costs_ptr + band_translate[0]), 0, ctx0,
EOB_TOKEN);
- int64_t best_block_rd_cost = RDCOST(rdmult, rddiv, rate0, accu_error);
+ int64_t best_block_rd_cost = RDCOST(rdmult, rate0, accu_error);
// int64_t best_block_rd_cost_all0 = best_block_rd_cost;
-
int x_prev = 1;
-
for (i = 0; i < eob; i++) {
const int rc = scan[i];
int x = qcoeff[rc];
@@ -238,9 +214,9 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
if (x == 0) {
// no need to search when x == 0
- rate0 =
- get_token_bit_costs(*(token_costs_ptr + band_cur), token_tree_sel_cur,
- ctx_cur, tokens[i][0].token);
+ int token = av1_get_token(x);
+ rate0 = get_token_bit_costs(*(token_costs_ptr + band_cur),
+ token_tree_sel_cur, ctx_cur, token);
accu_rate += rate0;
x_prev = 0;
// accu_error does not change when x==0
@@ -249,7 +225,7 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
*/
// compute the distortion for the first candidate
// and the distortion for quantizing to 0.
- int dx0 = (-coeff[rc]) * (1 << shift);
+ int dx0 = abs(coeff[rc]) * (1 << shift);
#if CONFIG_HIGHBITDEPTH
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
dx0 >>= xd->bd - 8;
@@ -273,7 +249,9 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
#if CONFIG_HIGHBITDEPTH
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
- dx >>= xd->bd - 8;
+ int dx_sign = dx < 0 ? 1 : 0;
+ dx = abs(dx) >> (xd->bd - 8);
+ if (dx_sign) dx = -dx;
}
#endif // CONFIG_HIGHBITDEPTH
d2 = (int64_t)dx * dx;
@@ -329,14 +307,16 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
if (i < default_eob - 1) {
int ctx_next, token_tree_sel_next;
int band_next = band_translate[i + 1];
+ int token_next =
+ i + 1 != eob ? av1_get_token(qcoeff[scan[i + 1]]) : EOB_TOKEN;
token_cache[rc] = av1_pt_energy_class[t0];
ctx_next = get_coef_context(nb, token_cache, i + 1);
token_tree_sel_next = (x == 0);
- next_bits0 = get_token_bit_costs(*(token_costs_ptr + band_next),
- token_tree_sel_next, ctx_next,
- tokens[i + 1][0].token);
+ next_bits0 =
+ get_token_bit_costs(*(token_costs_ptr + band_next),
+ token_tree_sel_next, ctx_next, token_next);
next_eob_bits0 =
get_token_bit_costs(*(token_costs_ptr + band_next),
token_tree_sel_next, ctx_next, EOB_TOKEN);
@@ -345,9 +325,9 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
ctx_next = get_coef_context(nb, token_cache, i + 1);
token_tree_sel_next = (x_a == 0);
- next_bits1 = get_token_bit_costs(*(token_costs_ptr + band_next),
- token_tree_sel_next, ctx_next,
- tokens[i + 1][0].token);
+ next_bits1 =
+ get_token_bit_costs(*(token_costs_ptr + band_next),
+ token_tree_sel_next, ctx_next, token_next);
if (x_a != 0) {
next_eob_bits1 =
@@ -356,16 +336,16 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
}
}
- rd_cost0 = RDCOST(rdmult, rddiv, (rate0 + next_bits0), d2);
- rd_cost1 = RDCOST(rdmult, rddiv, (rate1 + next_bits1), d2_a);
+ rd_cost0 = RDCOST(rdmult, (rate0 + next_bits0), d2);
+ rd_cost1 = RDCOST(rdmult, (rate1 + next_bits1), d2_a);
best_x = (rd_cost1 < rd_cost0);
- eob_cost0 = RDCOST(rdmult, rddiv, (accu_rate + rate0 + next_eob_bits0),
+ eob_cost0 = RDCOST(rdmult, (accu_rate + rate0 + next_eob_bits0),
(accu_error + d2 - d0));
eob_cost1 = eob_cost0;
if (x_a != 0) {
- eob_cost1 = RDCOST(rdmult, rddiv, (accu_rate + rate1 + next_eob_bits1),
+ eob_cost1 = RDCOST(rdmult, (accu_rate + rate1 + next_eob_bits1),
(accu_error + d2_a - d0));
best_eob_x = (eob_cost1 < eob_cost0);
} else {
@@ -410,38 +390,35 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
token_cache[rc] = av1_pt_energy_class[t0];
}
+ assert(accu_error >= 0);
x_prev = qcoeff[rc];
// determine whether to move the eob position to i+1
- int64_t best_eob_cost_i = eob_cost0;
-
- tokens[i][1].token = t0;
- tokens[i][1].qc = x;
- tokens[i][1].dqc = dqc;
-
- if ((x_a != 0) && (best_eob_x)) {
- best_eob_cost_i = eob_cost1;
-
- tokens[i][1].token = t1;
- tokens[i][1].qc = x_a;
- tokens[i][1].dqc = dqc_a;
- }
+ int use_a = (x_a != 0) && (best_eob_x);
+ int64_t best_eob_cost_i = use_a ? eob_cost1 : eob_cost0;
if (best_eob_cost_i < best_block_rd_cost) {
best_block_rd_cost = best_eob_cost_i;
final_eob = i + 1;
+ if (use_a) {
+ before_best_eob_qc = x_a;
+ before_best_eob_dqc = dqc_a;
+ } else {
+ before_best_eob_qc = x;
+ before_best_eob_dqc = dqc;
+ }
}
} // if (x==0)
} // for (i)
assert(final_eob <= eob);
if (final_eob > 0) {
- assert(tokens[final_eob - 1][1].qc != 0);
+ assert(before_best_eob_qc != 0);
i = final_eob - 1;
int rc = scan[i];
- qcoeff[rc] = tokens[i][1].qc;
- dqcoeff[rc] = tokens[i][1].dqc;
+ qcoeff[rc] = before_best_eob_qc;
+ dqcoeff[rc] = before_best_eob_dqc;
}
for (i = final_eob; i < eob; i++) {
@@ -453,366 +430,19 @@ static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
mb->plane[plane].eobs[block] = final_eob;
return final_eob;
}
-
-#else // USE_GREEDY_OPTIMIZE_B
-
-typedef struct av1_token_state_org {
- int64_t error;
- int rate;
- int16_t next;
- int16_t token;
- tran_low_t qc;
- tran_low_t dqc;
- uint8_t best_index;
-} av1_token_state_org;
-
-static int optimize_b_org(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
- int block, TX_SIZE tx_size, int ctx) {
- MACROBLOCKD *const xd = &mb->e_mbd;
- struct macroblock_plane *const p = &mb->plane[plane];
- struct macroblockd_plane *const pd = &xd->plane[plane];
- const int ref = is_inter_block(&xd->mi[0]->mbmi);
- av1_token_state_org tokens[MAX_TX_SQUARE + 1][2];
- uint8_t token_cache[MAX_TX_SQUARE];
- const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
- tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
- tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
- const int eob = p->eobs[block];
- const PLANE_TYPE plane_type = pd->plane_type;
- const int default_eob = tx_size_2d[tx_size];
- const int16_t *const dequant_ptr = pd->dequant;
- const uint8_t *const band_translate = get_band_translate(tx_size);
- TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
- const SCAN_ORDER *const scan_order =
- get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
- const int16_t *const scan = scan_order->scan;
- const int16_t *const nb = scan_order->neighbors;
- int dqv;
- const int shift = av1_get_tx_scale(tx_size);
-#if CONFIG_AOM_QM
- int seg_id = xd->mi[0]->mbmi.segment_id;
- const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!ref][tx_size];
-#endif
-#if CONFIG_NEW_QUANT
- int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type);
- const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
-#endif // CONFIG_NEW_QUANT
- int next = eob, sz = 0;
- const int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;
- const int64_t rddiv = mb->rddiv;
- int64_t rd_cost0, rd_cost1;
- int rate0, rate1;
- int64_t error0, error1;
- int16_t t0, t1;
- int best, band = (eob < default_eob) ? band_translate[eob]
- : band_translate[eob - 1];
- int pt, i, final_eob;
- const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
- unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
- mb->token_costs[txsize_sqr_map[tx_size]][plane_type][ref];
- const uint16_t *band_counts = &band_count_table[tx_size][band];
- uint16_t band_left = eob - band_cum_count_table[tx_size][band] + 1;
- int shortcut = 0;
- int next_shortcut = 0;
-
-#if CONFIG_EXT_DELTA_Q
- const int qindex = cm->seg.enabled
- ? av1_get_qindex(&cm->seg, xd->mi[0]->mbmi.segment_id,
- cm->base_qindex)
- : cm->base_qindex;
- assert(qindex > 0);
- (void)qindex;
-#else
- assert(mb->qindex > 0);
-#endif
-
- token_costs += band;
-
- assert((!plane_type && !plane) || (plane_type && plane));
- assert(eob <= default_eob);
-
- /* Now set up a Viterbi trellis to evaluate alternative roundings. */
- /* Initialize the sentinel node of the trellis. */
- tokens[eob][0].rate = 0;
- tokens[eob][0].error = 0;
- tokens[eob][0].next = default_eob;
- tokens[eob][0].token = EOB_TOKEN;
- tokens[eob][0].qc = 0;
- tokens[eob][1] = tokens[eob][0];
-
- for (i = 0; i < eob; i++) {
- const int rc = scan[i];
- tokens[i][0].rate = av1_get_token_cost(qcoeff[rc], &t0, cat6_bits);
- tokens[i][0].token = t0;
- token_cache[rc] = av1_pt_energy_class[t0];
- }
-
- for (i = eob; i-- > 0;) {
- int base_bits, dx;
- int64_t d2;
- const int rc = scan[i];
- int x = qcoeff[rc];
-#if CONFIG_AOM_QM
- int iwt = iqmatrix[rc];
- dqv = dequant_ptr[rc != 0];
- dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
-#else
- dqv = dequant_ptr[rc != 0];
-#endif
- next_shortcut = shortcut;
-
- /* Only add a trellis state for non-zero coefficients. */
- if (UNLIKELY(x)) {
- error0 = tokens[next][0].error;
- error1 = tokens[next][1].error;
- /* Evaluate the first possibility for this state. */
- rate0 = tokens[next][0].rate;
- rate1 = tokens[next][1].rate;
-
- if (next_shortcut) {
- /* Consider both possible successor states. */
- if (next < default_eob) {
- pt = get_coef_context(nb, token_cache, i + 1);
- rate0 +=
- get_token_bit_costs(*token_costs, 0, pt, tokens[next][0].token);
- rate1 +=
- get_token_bit_costs(*token_costs, 0, pt, tokens[next][1].token);
- }
- UPDATE_RD_COST();
- /* And pick the best. */
- best = rd_cost1 < rd_cost0;
- } else {
- if (next < default_eob) {
- pt = get_coef_context(nb, token_cache, i + 1);
- rate0 +=
- get_token_bit_costs(*token_costs, 0, pt, tokens[next][0].token);
- }
- best = 0;
- }
-
- dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
-#if CONFIG_HIGHBITDEPTH
- if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
- dx >>= xd->bd - 8;
- }
-#endif // CONFIG_HIGHBITDEPTH
- d2 = (int64_t)dx * dx;
- tokens[i][0].rate += (best ? rate1 : rate0);
- tokens[i][0].error = d2 + (best ? error1 : error0);
- tokens[i][0].next = next;
- tokens[i][0].qc = x;
- tokens[i][0].dqc = dqcoeff[rc];
- tokens[i][0].best_index = best;
-
- /* Evaluate the second possibility for this state. */
- rate0 = tokens[next][0].rate;
- rate1 = tokens[next][1].rate;
-
- // The threshold of 3 is empirically obtained.
- if (UNLIKELY(abs(x) > 3)) {
- shortcut = 0;
- } else {
-#if CONFIG_NEW_QUANT
- shortcut = ((av1_dequant_abscoeff_nuq(abs(x), dqv,
- dequant_val[band_translate[i]]) >
- (abs(coeff[rc]) << shift)) &&
- (av1_dequant_abscoeff_nuq(abs(x) - 1, dqv,
- dequant_val[band_translate[i]]) <
- (abs(coeff[rc]) << shift)));
-#else // CONFIG_NEW_QUANT
-#if CONFIG_AOM_QM
- if ((abs(x) * dequant_ptr[rc != 0] * iwt >
- ((abs(coeff[rc]) << shift) << AOM_QM_BITS)) &&
- (abs(x) * dequant_ptr[rc != 0] * iwt <
- (((abs(coeff[rc]) << shift) + dequant_ptr[rc != 0])
- << AOM_QM_BITS)))
-#else
- if ((abs(x) * dequant_ptr[rc != 0] > (abs(coeff[rc]) << shift)) &&
- (abs(x) * dequant_ptr[rc != 0] <
- (abs(coeff[rc]) << shift) + dequant_ptr[rc != 0]))
-#endif // CONFIG_AOM_QM
- shortcut = 1;
- else
- shortcut = 0;
-#endif // CONFIG_NEW_QUANT
- }
-
- if (shortcut) {
- sz = -(x < 0);
- x -= 2 * sz + 1;
- } else {
- tokens[i][1] = tokens[i][0];
- next = i;
-
- if (UNLIKELY(!(--band_left))) {
- --band_counts;
- band_left = *band_counts;
- --token_costs;
- }
- continue;
- }
-
- /* Consider both possible successor states. */
- if (!x) {
- /* If we reduced this coefficient to zero, check to see if
- * we need to move the EOB back here.
- */
- t0 = tokens[next][0].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
- t1 = tokens[next][1].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
- base_bits = 0;
- } else {
- base_bits = av1_get_token_cost(x, &t0, cat6_bits);
- t1 = t0;
- }
-
- if (next_shortcut) {
- if (LIKELY(next < default_eob)) {
- if (t0 != EOB_TOKEN) {
- token_cache[rc] = av1_pt_energy_class[t0];
- pt = get_coef_context(nb, token_cache, i + 1);
- rate0 += get_token_bit_costs(*token_costs, !x, pt,
- tokens[next][0].token);
- }
- if (t1 != EOB_TOKEN) {
- token_cache[rc] = av1_pt_energy_class[t1];
- pt = get_coef_context(nb, token_cache, i + 1);
- rate1 += get_token_bit_costs(*token_costs, !x, pt,
- tokens[next][1].token);
- }
- }
-
- UPDATE_RD_COST();
- /* And pick the best. */
- best = rd_cost1 < rd_cost0;
- } else {
- // The two states in next stage are identical.
- if (next < default_eob && t0 != EOB_TOKEN) {
- token_cache[rc] = av1_pt_energy_class[t0];
- pt = get_coef_context(nb, token_cache, i + 1);
- rate0 +=
- get_token_bit_costs(*token_costs, !x, pt, tokens[next][0].token);
- }
- best = 0;
- }
-
-#if CONFIG_NEW_QUANT
- dx = av1_dequant_coeff_nuq(x, dqv, dequant_val[band_translate[i]]) -
- (coeff[rc] << shift);
-#if CONFIG_HIGHBITDEPTH
- if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
- dx >>= xd->bd - 8;
- }
-#endif // CONFIG_HIGHBITDEPTH
-#else // CONFIG_NEW_QUANT
-#if CONFIG_HIGHBITDEPTH
- if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
- dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz;
- } else {
- dx -= (dqv + sz) ^ sz;
- }
-#else
- dx -= (dqv + sz) ^ sz;
-#endif // CONFIG_HIGHBITDEPTH
-#endif // CONFIG_NEW_QUANT
- d2 = (int64_t)dx * dx;
-
- tokens[i][1].rate = base_bits + (best ? rate1 : rate0);
- tokens[i][1].error = d2 + (best ? error1 : error0);
- tokens[i][1].next = next;
- tokens[i][1].token = best ? t1 : t0;
- tokens[i][1].qc = x;
-
- if (x) {
-#if CONFIG_NEW_QUANT
- tokens[i][1].dqc = av1_dequant_abscoeff_nuq(
- abs(x), dqv, dequant_val[band_translate[i]]);
- tokens[i][1].dqc = shift ? ROUND_POWER_OF_TWO(tokens[i][1].dqc, shift)
- : tokens[i][1].dqc;
- if (sz) tokens[i][1].dqc = -tokens[i][1].dqc;
-#else
- if (x < 0)
- tokens[i][1].dqc = -((-x * dqv) >> shift);
- else
- tokens[i][1].dqc = (x * dqv) >> shift;
-#endif // CONFIG_NEW_QUANT
- } else {
- tokens[i][1].dqc = 0;
- }
-
- tokens[i][1].best_index = best;
- /* Finally, make this the new head of the trellis. */
- next = i;
- } else {
- /* There's no choice to make for a zero coefficient, so we don't
- * add a new trellis node, but we do need to update the costs.
- */
- t0 = tokens[next][0].token;
- t1 = tokens[next][1].token;
- pt = get_coef_context(nb, token_cache, i + 1);
- /* Update the cost of each path if we're past the EOB token. */
- if (t0 != EOB_TOKEN) {
- tokens[next][0].rate += get_token_bit_costs(*token_costs, 1, pt, t0);
- tokens[next][0].token = ZERO_TOKEN;
- }
- if (t1 != EOB_TOKEN) {
- tokens[next][1].rate += get_token_bit_costs(*token_costs, 1, pt, t1);
- tokens[next][1].token = ZERO_TOKEN;
- }
- tokens[i][0].best_index = tokens[i][1].best_index = 0;
- shortcut = (tokens[next][0].rate != tokens[next][1].rate);
- /* Don't update next, because we didn't add a new node. */
- }
-
- if (UNLIKELY(!(--band_left))) {
- --band_counts;
- band_left = *band_counts;
- --token_costs;
- }
- }
-
- /* Now pick the best path through the whole trellis. */
- rate0 = tokens[next][0].rate;
- rate1 = tokens[next][1].rate;
- error0 = tokens[next][0].error;
- error1 = tokens[next][1].error;
- t0 = tokens[next][0].token;
- t1 = tokens[next][1].token;
- rate0 += get_token_bit_costs(*token_costs, 0, ctx, t0);
- rate1 += get_token_bit_costs(*token_costs, 0, ctx, t1);
- UPDATE_RD_COST();
- best = rd_cost1 < rd_cost0;
-
- final_eob = -1;
-
- for (i = next; i < eob; i = next) {
- const int x = tokens[i][best].qc;
- const int rc = scan[i];
- if (x) final_eob = i;
- qcoeff[rc] = x;
- dqcoeff[rc] = tokens[i][best].dqc;
-
- next = tokens[i][best].next;
- best = tokens[i][best].best_index;
- }
- final_eob++;
-
- mb->plane[plane].eobs[block] = final_eob;
- assert(final_eob <= default_eob);
- return final_eob;
-}
-
-#endif // USE_GREEDY_OPTIMIZE_B
#endif // !CONFIG_LV_MAP
-int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int block,
- BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
- const ENTROPY_CONTEXT *a, const ENTROPY_CONTEXT *l) {
+int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int blk_row,
+ int blk_col, int block, BLOCK_SIZE plane_bsize,
+ TX_SIZE tx_size, const ENTROPY_CONTEXT *a,
+ const ENTROPY_CONTEXT *l) {
MACROBLOCKD *const xd = &mb->e_mbd;
struct macroblock_plane *const p = &mb->plane[plane];
const int eob = p->eobs[block];
assert((mb->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
if (eob == 0) return eob;
if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return eob;
+
#if CONFIG_PVQ
(void)cm;
(void)tx_size;
@@ -823,26 +453,24 @@ int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int block,
#if !CONFIG_LV_MAP
(void)plane_bsize;
+ (void)blk_row;
+ (void)blk_col;
#if CONFIG_VAR_TX
int ctx = get_entropy_context(tx_size, a, l);
#else
int ctx = combine_entropy_contexts(*a, *l);
-#endif
-
-#if USE_GREEDY_OPTIMIZE_B
- return optimize_b_greedy(cm, mb, plane, block, tx_size, ctx);
-#else // USE_GREEDY_OPTIMIZE_B
- return optimize_b_org(cm, mb, plane, block, tx_size, ctx);
-#endif // USE_GREEDY_OPTIMIZE_B
+#endif // CONFIG_VAR_TX
+ return optimize_b_greedy(cm, mb, plane, blk_row, blk_col, block, tx_size,
+ ctx);
#else // !CONFIG_LV_MAP
TXB_CTX txb_ctx;
get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
- return av1_optimize_txb(cm, mb, plane, block, tx_size, &txb_ctx);
+ return av1_optimize_txb(cm, mb, plane, blk_row, blk_col, block, tx_size,
+ &txb_ctx);
#endif // !CONFIG_LV_MAP
}
#if !CONFIG_PVQ
-#if CONFIG_HIGHBITDEPTH
typedef enum QUANT_FUNC {
QUANT_FUNC_LOWBD = 0,
QUANT_FUNC_HIGHBD = 1,
@@ -862,29 +490,12 @@ static AV1_QUANT_FACADE
#endif // !CONFIG_NEW_QUANT
{ NULL, NULL }
};
+#endif // !CONFIG_PVQ
-#else
-
-typedef enum QUANT_FUNC {
- QUANT_FUNC_LOWBD = 0,
- QUANT_FUNC_TYPES = 1
-} QUANT_FUNC;
-
-static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_TYPES]
- [QUANT_FUNC_TYPES] = {
-#if !CONFIG_NEW_QUANT
- { av1_quantize_fp_facade },
- { av1_quantize_b_facade },
- { av1_quantize_dc_facade },
-#else // !CONFIG_NEW_QUANT
- { av1_quantize_fp_nuq_facade },
- { av1_quantize_b_nuq_facade },
- { av1_quantize_dc_nuq_facade },
-#endif // !CONFIG_NEW_QUANT
- { NULL }
- };
-#endif // CONFIG_HIGHBITDEPTH
-#endif // CONFIG_PVQ
+typedef void (*fwdTxfmFunc)(const int16_t *diff, tran_low_t *coeff, int stride,
+ TxfmParam *txfm_param);
+static const fwdTxfmFunc fwd_txfm_func[2] = { av1_fwd_txfm,
+ av1_highbd_fwd_txfm };
void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
@@ -892,7 +503,7 @@ void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
AV1_XFORM_QUANT xform_quant_idx) {
MACROBLOCKD *const xd = &x->e_mbd;
MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
-#if !(CONFIG_PVQ || CONFIG_DAALA_DIST)
+#if !(CONFIG_PVQ || CONFIG_DIST_8X8)
const struct macroblock_plane *const p = &x->plane[plane];
const struct macroblockd_plane *const pd = &xd->plane[plane];
#else
@@ -900,9 +511,14 @@ void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
struct macroblockd_plane *const pd = &xd->plane[plane];
#endif
PLANE_TYPE plane_type = get_plane_type(plane);
- TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
+ TX_TYPE tx_type =
+ av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
+
+#if CONFIG_AOM_QM || CONFIG_NEW_QUANT
const int is_inter = is_inter_block(mbmi);
- const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
+#endif
+
+ const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
@@ -910,19 +526,28 @@ void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
const int diff_stride = block_size_wide[plane_bsize];
#if CONFIG_AOM_QM
int seg_id = mbmi->segment_id;
- const qm_val_t *qmatrix = pd->seg_qmatrix[seg_id][!is_inter][tx_size];
- const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!is_inter][tx_size];
+ // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
+ const qm_val_t *qmatrix =
+ IS_2D_TRANSFORM(tx_type) ? pd->seg_qmatrix[seg_id][!is_inter][tx_size]
+ : cm->gqmatrix[NUM_QM_LEVELS - 1][0][0][tx_size];
+ const qm_val_t *iqmatrix =
+ IS_2D_TRANSFORM(tx_type)
+ ? pd->seg_iqmatrix[seg_id][!is_inter][tx_size]
+ : cm->giqmatrix[NUM_QM_LEVELS - 1][0][0][tx_size];
#endif
- FWD_TXFM_PARAM fwd_txfm_param;
+ TxfmParam txfm_param;
-#if CONFIG_PVQ || CONFIG_DAALA_DIST
+#if CONFIG_PVQ || CONFIG_DIST_8X8 || CONFIG_LGT || CONFIG_MRC_TX
uint8_t *dst;
- int16_t *pred;
const int dst_stride = pd->dst.stride;
- int tx_blk_size;
+#if CONFIG_PVQ || CONFIG_DIST_8X8
+ int16_t *pred;
+ const int txw = tx_size_wide[tx_size];
+ const int txh = tx_size_high[tx_size];
int i, j;
#endif
+#endif
#if !CONFIG_PVQ
const int tx2d_size = tx_size_2d[tx_size];
@@ -960,79 +585,68 @@ void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
src_int16 =
&p->src_int16[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
- // transform block size in pixels
- tx_blk_size = tx_size_wide[tx_size];
#if CONFIG_HIGHBITDEPTH
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
- for (j = 0; j < tx_blk_size; j++)
- for (i = 0; i < tx_blk_size; i++)
+ for (j = 0; j < txh; j++)
+ for (i = 0; i < txw; i++)
src_int16[diff_stride * j + i] =
CONVERT_TO_SHORTPTR(src)[src_stride * j + i];
} else {
#endif // CONFIG_HIGHBITDEPTH
- for (j = 0; j < tx_blk_size; j++)
- for (i = 0; i < tx_blk_size; i++)
+ for (j = 0; j < txh; j++)
+ for (i = 0; i < txw; i++)
src_int16[diff_stride * j + i] = src[src_stride * j + i];
#if CONFIG_HIGHBITDEPTH
}
#endif // CONFIG_HIGHBITDEPTH
#endif
-#if CONFIG_PVQ || CONFIG_DAALA_DIST
+#if CONFIG_PVQ || CONFIG_DIST_8X8 || CONFIG_LGT || CONFIG_MRC_TX
dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
+#if CONFIG_PVQ || CONFIG_DIST_8X8
pred = &pd->pred[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
- // transform block size in pixels
- tx_blk_size = tx_size_wide[tx_size];
-
// copy uint8 orig and predicted block to int16 buffer
// in order to use existing VP10 transform functions
#if CONFIG_HIGHBITDEPTH
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
- for (j = 0; j < tx_blk_size; j++)
- for (i = 0; i < tx_blk_size; i++)
+ for (j = 0; j < txh; j++)
+ for (i = 0; i < txw; i++)
pred[diff_stride * j + i] =
CONVERT_TO_SHORTPTR(dst)[dst_stride * j + i];
} else {
#endif // CONFIG_HIGHBITDEPTH
- for (j = 0; j < tx_blk_size; j++)
- for (i = 0; i < tx_blk_size; i++)
+ for (j = 0; j < txh; j++)
+ for (i = 0; i < txw; i++)
pred[diff_stride * j + i] = dst[dst_stride * j + i];
#if CONFIG_HIGHBITDEPTH
}
#endif // CONFIG_HIGHBITDEPTH
-#endif
+#endif // CONFIG_PVQ || CONFIG_DIST_8X8
+#endif // CONFIG_PVQ || CONFIG_DIST_8X8 || CONFIG_LGT || CONFIG_MRC_TX
(void)ctx;
- fwd_txfm_param.tx_type = tx_type;
- fwd_txfm_param.tx_size = tx_size;
- fwd_txfm_param.lossless = xd->lossless[mbmi->segment_id];
+ txfm_param.tx_type = tx_type;
+ txfm_param.tx_size = tx_size;
+ txfm_param.lossless = xd->lossless[mbmi->segment_id];
+#if CONFIG_MRC_TX || CONFIG_LGT
+ txfm_param.dst = dst;
+ txfm_param.stride = dst_stride;
+#endif // CONFIG_MRC_TX || CONFIG_LGT
+#if CONFIG_LGT
+ txfm_param.is_inter = is_inter_block(mbmi);
+ txfm_param.mode = get_prediction_mode(xd->mi[0], plane, tx_size, block);
+#endif
#if !CONFIG_PVQ
-#if CONFIG_HIGHBITDEPTH
- fwd_txfm_param.bd = xd->bd;
- if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
- av1_highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
- if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
- if (LIKELY(!x->skip_block)) {
- quant_func_list[xform_quant_idx][QUANT_FUNC_HIGHBD](
- coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
- } else {
- av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
- }
- }
-#if CONFIG_LV_MAP
- p->txb_entropy_ctx[block] =
- (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
-#endif // CONFIG_LV_MAP
- return;
- }
-#endif // CONFIG_HIGHBITDEPTH
- av1_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
+ txfm_param.bd = xd->bd;
+ const int is_hbd = get_bitdepth_data_path_index(xd);
+ fwd_txfm_func[is_hbd](src_diff, coeff, diff_stride, &txfm_param);
+
if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
if (LIKELY(!x->skip_block)) {
- quant_func_list[xform_quant_idx][QUANT_FUNC_LOWBD](
+ quant_func_list[xform_quant_idx][is_hbd](
coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
} else {
av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
@@ -1042,17 +656,18 @@ void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
p->txb_entropy_ctx[block] =
(uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
#endif // CONFIG_LV_MAP
-#else // #if !CONFIG_PVQ
+ return;
+#else // CONFIG_PVQ
(void)xform_quant_idx;
#if CONFIG_HIGHBITDEPTH
- fwd_txfm_param.bd = xd->bd;
+ txfm_param.bd = xd->bd;
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
- av1_highbd_fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param);
- av1_highbd_fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param);
+ av1_highbd_fwd_txfm(src_int16, coeff, diff_stride, &txfm_param);
+ av1_highbd_fwd_txfm(pred, ref_coeff, diff_stride, &txfm_param);
} else {
#endif
- av1_fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param);
- av1_fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param);
+ av1_fwd_txfm(src_int16, coeff, diff_stride, &txfm_param);
+ av1_fwd_txfm(pred, ref_coeff, diff_stride, &txfm_param);
#if CONFIG_HIGHBITDEPTH
}
#endif
@@ -1130,7 +745,8 @@ static void encode_block(int plane, int block, int blk_row, int blk_col,
#endif
#if !CONFIG_PVQ
- av1_optimize_b(cm, x, plane, block, plane_bsize, tx_size, a, l);
+ av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size, a,
+ l);
av1_set_txb_context(x, plane, block, tx_size, a, l);
@@ -1143,9 +759,16 @@ static void encode_block(int plane, int block, int blk_row, int blk_col,
if (x->pvq_skip[plane]) return;
#endif
- TX_TYPE tx_type = get_tx_type(pd->plane_type, xd, block, tx_size);
+ TX_TYPE tx_type =
+ av1_get_tx_type(pd->plane_type, xd, blk_row, blk_col, block, tx_size);
+#if CONFIG_LGT
+ PREDICTION_MODE mode = get_prediction_mode(xd->mi[0], plane, tx_size, block);
+ av1_inverse_transform_block(xd, dqcoeff, mode, tx_type, tx_size, dst,
+ pd->dst.stride, p->eobs[block]);
+#else
av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst,
pd->dst.stride, p->eobs[block]);
+#endif
}
#if CONFIG_VAR_TX
@@ -1174,16 +797,32 @@ static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
} else {
assert(tx_size < TX_SIZES_ALL);
+#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];
+ if (is_qttx) assert(blk_row == 0 && blk_col == 0 && block == 0);
+#else
const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
assert(sub_txs < tx_size);
+#endif
// This is the square transform block partition entry point.
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
int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
@@ -1211,6 +850,7 @@ static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
struct macroblock_plane *const p = &x->plane[plane];
struct macroblockd_plane *const pd = &xd->plane[plane];
tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
+ TxfmParam txfm_param;
uint8_t *dst;
int ctx = 0;
dst = &pd->dst
@@ -1246,22 +886,20 @@ static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
#endif // CONFIG_HIGHBITDEPTH
}
#endif // !CONFIG_PVQ
+ txfm_param.bd = xd->bd;
+ txfm_param.tx_type = DCT_DCT;
+ txfm_param.eob = p->eobs[block];
+ txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
#if CONFIG_HIGHBITDEPTH
if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
- if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
- av1_highbd_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block],
- xd->bd);
- } else {
- av1_highbd_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block],
- xd->bd);
- }
+ av1_highbd_inv_txfm_add_4x4(dqcoeff, dst, pd->dst.stride, &txfm_param);
return;
}
#endif // CONFIG_HIGHBITDEPTH
if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
- av1_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
+ av1_iwht4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
} else {
- av1_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
+ av1_idct4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
}
}
}
@@ -1316,7 +954,7 @@ void av1_encode_sb(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row,
av1_get_entropy_contexts(bsize, 0, pd, ctx.ta[plane], ctx.tl[plane]);
#else
const struct macroblockd_plane *const pd = &xd->plane[plane];
- const TX_SIZE tx_size = get_tx_size(plane, xd);
+ const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
av1_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane], ctx.tl[plane]);
#endif
@@ -1327,11 +965,27 @@ void av1_encode_sb(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row,
arg.tl = ctx.tl[plane];
#if CONFIG_VAR_TX
- for (idy = 0; idy < mi_height; idy += bh) {
- for (idx = 0; idx < mi_width; idx += bw) {
- encode_block_inter(plane, block, idy, idx, plane_bsize, max_tx_size,
- &arg);
- block += step;
+ 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(mi_width, mu_blocks_wide);
+ mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);
+
+ for (idy = 0; idy < mi_height; idy += mu_blocks_high) {
+ for (idx = 0; idx < mi_width; idx += mu_blocks_wide) {
+ int blk_row, blk_col;
+ const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);
+ const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
+ for (blk_row = idy; blk_row < unit_height; blk_row += bh) {
+ for (blk_col = idx; blk_col < unit_width; blk_col += bw) {
+ encode_block_inter(plane, block, blk_row, blk_col, plane_bsize,
+ max_tx_size, &arg);
+ block += step;
+ }
+ }
}
}
#else
@@ -1357,7 +1011,7 @@ void av1_encode_sb_supertx(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize) {
#if CONFIG_VAR_TX
const TX_SIZE tx_size = TX_4X4;
#else
- const TX_SIZE tx_size = get_tx_size(plane, xd);
+ const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
#endif
av1_subtract_plane(x, bsize, plane);
av1_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane], ctx.tl[plane]);
@@ -1435,6 +1089,24 @@ static void quantize_scaler(int coeff, int16_t zbin, int16_t round_value,
}
}
+#if CONFIG_HIGHBITDEPTH
+typedef void (*hbd_dpcm_fwd_tx_func)(const int16_t *input, int stride,
+ TX_TYPE_1D tx_type, tran_low_t *output,
+ int dir);
+
+static hbd_dpcm_fwd_tx_func get_hbd_dpcm_fwd_tx_func(int tx_length) {
+ switch (tx_length) {
+ case 4: return av1_hbd_dpcm_ft4_c;
+ case 8: return av1_hbd_dpcm_ft8_c;
+ case 16: return av1_hbd_dpcm_ft16_c;
+ case 32:
+ return av1_hbd_dpcm_ft32_c;
+ // TODO(huisu): add support for TX_64X64.
+ default: assert(0); return NULL;
+ }
+}
+#endif // CONFIG_HIGHBITDEPTH
+
typedef void (*dpcm_fwd_tx_func)(const int16_t *input, int stride,
TX_TYPE_1D tx_type, tran_low_t *output);
@@ -1539,7 +1211,7 @@ static void hbd_process_block_dpcm_vert(
int16_t *src_diff, int diff_stride, tran_low_t *coeff, tran_low_t *qcoeff,
tran_low_t *dqcoeff) {
const int tx1d_width = tx_size_wide[tx_size];
- dpcm_fwd_tx_func forward_tx = get_dpcm_fwd_tx_func(tx1d_width);
+ hbd_dpcm_fwd_tx_func forward_tx = get_hbd_dpcm_fwd_tx_func(tx1d_width);
hbd_dpcm_inv_txfm_add_func inverse_tx =
av1_get_hbd_dpcm_inv_txfm_add_func(tx1d_width);
uint16_t *src = CONVERT_TO_SHORTPTR(src8);
@@ -1553,7 +1225,7 @@ static void hbd_process_block_dpcm_vert(
// Subtraction.
for (int c = 0; c < tx1d_width; ++c) src_diff[c] = src[c] - dst[c];
// Forward transform.
- forward_tx(src_diff, 1, tx_type_1d, coeff);
+ forward_tx(src_diff, 1, tx_type_1d, coeff, 1);
// Quantization.
for (int c = 0; c < tx1d_width; ++c) {
quantize_scaler(coeff[c], p->zbin[q_idx], p->round[q_idx],
@@ -1562,7 +1234,7 @@ static void hbd_process_block_dpcm_vert(
q_idx = 1;
}
// Inverse transform.
- inverse_tx(dqcoeff, 1, tx_type_1d, bd, dst);
+ inverse_tx(dqcoeff, 1, tx_type_1d, bd, dst, 1);
// Move to the next row.
coeff += tx1d_width;
qcoeff += tx1d_width;
@@ -1580,7 +1252,7 @@ static void hbd_process_block_dpcm_horz(
int16_t *src_diff, int diff_stride, tran_low_t *coeff, tran_low_t *qcoeff,
tran_low_t *dqcoeff) {
const int tx1d_height = tx_size_high[tx_size];
- dpcm_fwd_tx_func forward_tx = get_dpcm_fwd_tx_func(tx1d_height);
+ hbd_dpcm_fwd_tx_func forward_tx = get_hbd_dpcm_fwd_tx_func(tx1d_height);
hbd_dpcm_inv_txfm_add_func inverse_tx =
av1_get_hbd_dpcm_inv_txfm_add_func(tx1d_height);
uint16_t *src = CONVERT_TO_SHORTPTR(src8);
@@ -1597,7 +1269,7 @@ static void hbd_process_block_dpcm_horz(
}
// Forward transform.
tran_low_t tx_buff[64];
- forward_tx(src_diff, diff_stride, tx_type_1d, tx_buff);
+ forward_tx(src_diff, diff_stride, tx_type_1d, tx_buff, 0);
for (int r = 0; r < tx1d_height; ++r) coeff[r * tx1d_width] = tx_buff[r];
// Quantization.
for (int r = 0; r < tx1d_height; ++r) {
@@ -1609,7 +1281,7 @@ static void hbd_process_block_dpcm_horz(
}
// Inverse transform.
for (int r = 0; r < tx1d_height; ++r) tx_buff[r] = dqcoeff[r * tx1d_width];
- inverse_tx(tx_buff, dst_stride, tx_type_1d, bd, dst);
+ inverse_tx(tx_buff, dst_stride, tx_type_1d, bd, dst, 0);
// Move to the next column.
++coeff, ++qcoeff, ++dqcoeff, ++src_diff, ++dst, ++src;
}
@@ -1631,7 +1303,8 @@ void av1_encode_block_intra_dpcm(const AV1_COMMON *cm, MACROBLOCK *x,
const int dst_stride = pd->dst.stride;
const int tx1d_width = tx_size_wide[tx_size];
const int tx1d_height = tx_size_high[tx_size];
- const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, 0);
+ const SCAN_ORDER *const scan_order =
+ get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
tran_low_t *coeff = BLOCK_OFFSET(p->coeff, block);
tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
uint8_t *dst =
@@ -1711,30 +1384,20 @@ void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
struct macroblockd_plane *const pd = &xd->plane[plane];
tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
PLANE_TYPE plane_type = get_plane_type(plane);
- const TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
+ const TX_TYPE tx_type =
+ av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
uint16_t *eob = &p->eobs[block];
const int dst_stride = pd->dst.stride;
uint8_t *dst =
&pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
-#if CONFIG_CFL
-
-#if CONFIG_EC_ADAPT
- FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
-#else
- FRAME_CONTEXT *const ec_ctx = cm->fc;
-#endif // CONFIG_EC_ADAPT
- av1_predict_intra_block_encoder_facade(x, ec_ctx, plane, block, blk_col,
- blk_row, tx_size, plane_bsize);
-#else
av1_predict_intra_block_facade(xd, plane, block, blk_col, blk_row, tx_size);
-#endif
+#if CONFIG_DPCM_INTRA || CONFIG_LGT
+ const PREDICTION_MODE mode =
+ get_prediction_mode(xd->mi[0], plane, tx_size, block);
#if CONFIG_DPCM_INTRA
- const int block_raster_idx = av1_block_index_to_raster_order(tx_size, block);
const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
- const PREDICTION_MODE mode =
- (plane == 0) ? get_y_mode(xd->mi[0], block_raster_idx) : mbmi->uv_mode;
if (av1_use_dpcm_intra(plane, mode, tx_type, mbmi)) {
av1_encode_block_intra_dpcm(cm, x, mode, plane, block, blk_row, blk_col,
plane_bsize, tx_size, tx_type, args->ta,
@@ -1742,6 +1405,7 @@ void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
return;
}
#endif // CONFIG_DPCM_INTRA
+#endif // CONFIG_DPCM_INTRA || CONFIG_LGT
av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size);
@@ -1751,7 +1415,8 @@ void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
if (args->enable_optimize_b) {
av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
ctx, AV1_XFORM_QUANT_FP);
- av1_optimize_b(cm, x, plane, block, plane_bsize, tx_size, a, l);
+ av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size,
+ a, l);
} else {
av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
ctx, AV1_XFORM_QUANT_B);
@@ -1763,220 +1428,25 @@ void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
if (x->pvq_skip[plane]) return;
#endif // CONFIG_PVQ
- av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst, dst_stride,
- *eob);
+ av1_inverse_transform_block(xd, dqcoeff,
+#if CONFIG_LGT
+ mode,
+#endif
+ tx_type, tx_size, dst, dst_stride, *eob);
#if !CONFIG_PVQ
if (*eob) *(args->skip) = 0;
#else
// Note : *(args->skip) == mbmi->skip
#endif
#if CONFIG_CFL
- MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
if (plane == AOM_PLANE_Y && x->cfl_store_y) {
- cfl_store(xd->cfl, dst, dst_stride, blk_row, blk_col, tx_size);
- }
-
- if (mbmi->uv_mode == DC_PRED) {
- // TODO(ltrudeau) find a cleaner way to detect last transform block
- if (plane == AOM_PLANE_U) {
- xd->cfl->num_tx_blk[CFL_PRED_U] =
- (blk_row == 0 && blk_col == 0) ? 1
- : xd->cfl->num_tx_blk[CFL_PRED_U] + 1;
- }
-
- if (plane == AOM_PLANE_V) {
- xd->cfl->num_tx_blk[CFL_PRED_V] =
- (blk_row == 0 && blk_col == 0) ? 1
- : xd->cfl->num_tx_blk[CFL_PRED_V] + 1;
-
- if (mbmi->skip &&
- xd->cfl->num_tx_blk[CFL_PRED_U] == xd->cfl->num_tx_blk[CFL_PRED_V]) {
- assert(plane_bsize != BLOCK_INVALID);
- const int block_width = block_size_wide[plane_bsize];
- const int block_height = block_size_high[plane_bsize];
-
- // if SKIP is chosen at the block level, and ind != 0, we must change
- // the prediction
- if (mbmi->cfl_alpha_idx != 0) {
- const struct macroblockd_plane *const pd_cb = &xd->plane[AOM_PLANE_U];
- uint8_t *const dst_cb = pd_cb->dst.buf;
- const int dst_stride_cb = pd_cb->dst.stride;
- uint8_t *const dst_cr = pd->dst.buf;
- const int dst_stride_cr = pd->dst.stride;
- for (int j = 0; j < block_height; j++) {
- for (int i = 0; i < block_width; i++) {
- dst_cb[dst_stride_cb * j + i] =
- (uint8_t)(xd->cfl->dc_pred[CFL_PRED_U] + 0.5);
- dst_cr[dst_stride_cr * j + i] =
- (uint8_t)(xd->cfl->dc_pred[CFL_PRED_V] + 0.5);
- }
- }
- mbmi->cfl_alpha_idx = 0;
- mbmi->cfl_alpha_signs[CFL_PRED_U] = CFL_SIGN_POS;
- mbmi->cfl_alpha_signs[CFL_PRED_V] = CFL_SIGN_POS;
- }
- }
- }
+ // TODO (ltrudeau) Store sub-8x8 inter blocks when bottom right block is
+ // intra predicted.
+ cfl_store(xd->cfl, dst, dst_stride, blk_row, blk_col, tx_size, plane_bsize);
}
#endif
}
-#if CONFIG_CFL
-static int cfl_alpha_dist(const uint8_t *y_pix, int y_stride, double y_avg,
- const uint8_t *src, int src_stride, int blk_width,
- int blk_height, double dc_pred, double alpha,
- int *dist_neg_out) {
- const double dc_pred_bias = dc_pred + 0.5;
- int dist = 0;
- int diff;
-
- if (alpha == 0.0) {
- const int dc_pred_i = (int)dc_pred_bias;
- for (int j = 0; j < blk_height; j++) {
- for (int i = 0; i < blk_width; i++) {
- diff = src[i] - dc_pred_i;
- dist += diff * diff;
- }
- src += src_stride;
- }
-
- if (dist_neg_out) *dist_neg_out = dist;
-
- return dist;
- }
-
- int dist_neg = 0;
- for (int j = 0; j < blk_height; j++) {
- for (int i = 0; i < blk_width; i++) {
- const double scaled_luma = alpha * (y_pix[i] - y_avg);
- const int uv = src[i];
- diff = uv - (int)(scaled_luma + dc_pred_bias);
- dist += diff * diff;
- diff = uv + (int)(scaled_luma - dc_pred_bias);
- dist_neg += diff * diff;
- }
- y_pix += y_stride;
- src += src_stride;
- }
-
- if (dist_neg_out) *dist_neg_out = dist_neg;
-
- return dist;
-}
-
-static int cfl_compute_alpha_ind(MACROBLOCK *const x, const CFL_CTX *const cfl,
- BLOCK_SIZE bsize,
- CFL_SIGN_TYPE signs_out[CFL_SIGNS]) {
- const struct macroblock_plane *const p_u = &x->plane[AOM_PLANE_U];
- const struct macroblock_plane *const p_v = &x->plane[AOM_PLANE_V];
- const uint8_t *const src_u = p_u->src.buf;
- const uint8_t *const src_v = p_v->src.buf;
- const int src_stride_u = p_u->src.stride;
- const int src_stride_v = p_v->src.stride;
- const int block_width = block_size_wide[bsize];
- const int block_height = block_size_high[bsize];
- const double dc_pred_u = cfl->dc_pred[CFL_PRED_U];
- const double dc_pred_v = cfl->dc_pred[CFL_PRED_V];
-
- // Temporary pixel buffer used to store the CfL prediction when we compute the
- // alpha index.
- uint8_t tmp_pix[MAX_SB_SQUARE];
- // Load CfL Prediction over the entire block
- const double y_avg =
- cfl_load(cfl, tmp_pix, MAX_SB_SIZE, 0, 0, block_width, block_height);
-
- int sse[CFL_PRED_PLANES][CFL_MAGS_SIZE];
- sse[CFL_PRED_U][0] =
- cfl_alpha_dist(tmp_pix, MAX_SB_SIZE, y_avg, src_u, src_stride_u,
- block_width, block_height, dc_pred_u, 0, NULL);
- sse[CFL_PRED_V][0] =
- cfl_alpha_dist(tmp_pix, MAX_SB_SIZE, y_avg, src_v, src_stride_v,
- block_width, block_height, dc_pred_v, 0, NULL);
- for (int m = 1; m < CFL_MAGS_SIZE; m += 2) {
- assert(cfl_alpha_mags[m + 1] == -cfl_alpha_mags[m]);
- sse[CFL_PRED_U][m] = cfl_alpha_dist(
- tmp_pix, MAX_SB_SIZE, y_avg, src_u, src_stride_u, block_width,
- block_height, dc_pred_u, cfl_alpha_mags[m], &sse[CFL_PRED_U][m + 1]);
- sse[CFL_PRED_V][m] = cfl_alpha_dist(
- tmp_pix, MAX_SB_SIZE, y_avg, src_v, src_stride_v, block_width,
- block_height, dc_pred_v, cfl_alpha_mags[m], &sse[CFL_PRED_V][m + 1]);
- }
-
- int dist;
- int64_t cost;
- int64_t best_cost;
-
- // Compute least squares parameter of the entire block
- // IMPORTANT: We assume that the first code is 0,0
- int ind = 0;
- signs_out[CFL_PRED_U] = CFL_SIGN_POS;
- signs_out[CFL_PRED_V] = CFL_SIGN_POS;
-
- dist = sse[CFL_PRED_U][0] + sse[CFL_PRED_V][0];
- dist *= 16;
- best_cost = RDCOST(x->rdmult, x->rddiv, cfl->costs[0], dist);
-
- for (int c = 1; c < CFL_ALPHABET_SIZE; c++) {
- const int idx_u = cfl_alpha_codes[c][CFL_PRED_U];
- const int idx_v = cfl_alpha_codes[c][CFL_PRED_V];
- for (CFL_SIGN_TYPE sign_u = idx_u == 0; sign_u < CFL_SIGNS; sign_u++) {
- for (CFL_SIGN_TYPE sign_v = idx_v == 0; sign_v < CFL_SIGNS; sign_v++) {
- dist = sse[CFL_PRED_U][idx_u + (sign_u == CFL_SIGN_NEG)] +
- sse[CFL_PRED_V][idx_v + (sign_v == CFL_SIGN_NEG)];
- dist *= 16;
- cost = RDCOST(x->rdmult, x->rddiv, cfl->costs[c], dist);
- if (cost < best_cost) {
- best_cost = cost;
- ind = c;
- signs_out[CFL_PRED_U] = sign_u;
- signs_out[CFL_PRED_V] = sign_v;
- }
- }
- }
- }
-
- return ind;
-}
-
-static inline void cfl_update_costs(CFL_CTX *cfl, FRAME_CONTEXT *ec_ctx) {
- assert(ec_ctx->cfl_alpha_cdf[CFL_ALPHABET_SIZE - 1] ==
- AOM_ICDF(CDF_PROB_TOP));
- const int prob_den = CDF_PROB_TOP;
-
- int prob_num = AOM_ICDF(ec_ctx->cfl_alpha_cdf[0]);
- cfl->costs[0] = av1_cost_zero(get_prob(prob_num, prob_den));
-
- for (int c = 1; c < CFL_ALPHABET_SIZE; c++) {
- int sign_bit_cost = (cfl_alpha_codes[c][CFL_PRED_U] != 0) +
- (cfl_alpha_codes[c][CFL_PRED_V] != 0);
- prob_num = AOM_ICDF(ec_ctx->cfl_alpha_cdf[c]) -
- AOM_ICDF(ec_ctx->cfl_alpha_cdf[c - 1]);
- cfl->costs[c] = av1_cost_zero(get_prob(prob_num, prob_den)) +
- av1_cost_literal(sign_bit_cost);
- }
-}
-
-void av1_predict_intra_block_encoder_facade(MACROBLOCK *x,
- FRAME_CONTEXT *ec_ctx, int plane,
- int block_idx, int blk_col,
- int blk_row, TX_SIZE tx_size,
- BLOCK_SIZE plane_bsize) {
- MACROBLOCKD *const xd = &x->e_mbd;
- MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
- if (plane != AOM_PLANE_Y && mbmi->uv_mode == DC_PRED) {
- if (blk_col == 0 && blk_row == 0 && plane == AOM_PLANE_U) {
- CFL_CTX *const cfl = xd->cfl;
- cfl_update_costs(cfl, ec_ctx);
- cfl_dc_pred(xd, plane_bsize, tx_size);
- mbmi->cfl_alpha_idx =
- cfl_compute_alpha_ind(x, cfl, plane_bsize, mbmi->cfl_alpha_signs);
- }
- }
- av1_predict_intra_block_facade(xd, plane, block_idx, blk_col, blk_row,
- tx_size);
-}
-#endif
-
void av1_encode_intra_block_plane(AV1_COMMON *cm, MACROBLOCK *x,
BLOCK_SIZE bsize, int plane,
int enable_optimize_b, int mi_row,
@@ -2001,7 +1471,7 @@ void av1_encode_intra_block_plane(AV1_COMMON *cm, MACROBLOCK *x,
if (enable_optimize_b) {
const struct macroblockd_plane *const pd = &xd->plane[plane];
- const TX_SIZE tx_size = get_tx_size(plane, xd);
+ const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
av1_get_entropy_contexts(bsize, tx_size, pd, ta, tl);
}
av1_foreach_transformed_block_in_plane(