Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12 | #include "./av1_rtcd.h" |
| 13 | #include "./aom_config.h" |
| 14 | #include "./aom_dsp_rtcd.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 15 | |
Nathan E. Egge | 6675be0 | 2016-12-21 13:02:43 -0500 | [diff] [blame] | 16 | #include "aom_dsp/bitwriter.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 17 | #include "aom_dsp/quantize.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 18 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 19 | #include "aom_ports/mem.h" |
| 20 | |
| 21 | #include "av1/common/idct.h" |
| 22 | #include "av1/common/reconinter.h" |
| 23 | #include "av1/common/reconintra.h" |
| 24 | #include "av1/common/scan.h" |
| 25 | |
Tom Finegan | 17ce8b1 | 2017-02-08 12:46:31 -0800 | [diff] [blame] | 26 | #include "av1/encoder/av1_quantize.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 27 | #include "av1/encoder/encodemb.h" |
| 28 | #include "av1/encoder/hybrid_fwd_txfm.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 29 | #include "av1/encoder/rd.h" |
| 30 | #include "av1/encoder/tokenize.h" |
| 31 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 32 | #if CONFIG_PVQ |
| 33 | #include "av1/encoder/encint.h" |
| 34 | #include "av1/common/partition.h" |
| 35 | #include "av1/encoder/pvq_encoder.h" |
| 36 | #endif |
| 37 | |
Jingning Han | e325abd | 2016-12-01 09:35:10 -0800 | [diff] [blame] | 38 | // Check if one needs to use c version subtraction. |
| 39 | static int check_subtract_block_size(int w, int h) { return w < 4 || h < 4; } |
| 40 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 41 | void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 42 | struct macroblock_plane *const p = &x->plane[plane]; |
| 43 | const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane]; |
| 44 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 45 | const int bw = block_size_wide[plane_bsize]; |
| 46 | const int bh = block_size_high[plane_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 47 | |
Jingning Han | e325abd | 2016-12-01 09:35:10 -0800 | [diff] [blame] | 48 | if (check_subtract_block_size(bw, bh)) { |
| 49 | #if CONFIG_AOM_HIGHBITDEPTH |
| 50 | if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 51 | aom_highbd_subtract_block_c(bh, bw, p->src_diff, bw, p->src.buf, |
| 52 | p->src.stride, pd->dst.buf, pd->dst.stride, |
| 53 | x->e_mbd.bd); |
| 54 | return; |
| 55 | } |
| 56 | #endif // CONFIG_AOM_HIGHBITDEPTH |
| 57 | aom_subtract_block_c(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride, |
| 58 | pd->dst.buf, pd->dst.stride); |
| 59 | |
| 60 | return; |
| 61 | } |
| 62 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 63 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 64 | if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 65 | aom_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 66 | p->src.stride, pd->dst.buf, pd->dst.stride, |
| 67 | x->e_mbd.bd); |
| 68 | return; |
| 69 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 70 | #endif // CONFIG_AOM_HIGHBITDEPTH |
| 71 | aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 72 | pd->dst.buf, pd->dst.stride); |
| 73 | } |
| 74 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 75 | typedef struct av1_token_state { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 76 | int rate; |
| 77 | int64_t error; |
| 78 | int next; |
| 79 | int16_t token; |
| 80 | tran_low_t qc; |
| 81 | tran_low_t dqc; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 82 | } av1_token_state; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 83 | |
| 84 | // These numbers are empirically obtained. |
| 85 | static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = { |
| 86 | { 10, 6 }, { 8, 5 }, |
| 87 | }; |
| 88 | |
| 89 | #define UPDATE_RD_COST() \ |
| 90 | { \ |
| 91 | rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0); \ |
| 92 | rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1); \ |
| 93 | } |
| 94 | |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 95 | static inline int64_t get_token_bit_costs( |
| 96 | unsigned int token_costs[2][COEFF_CONTEXTS][ENTROPY_TOKENS], int skip_eob, |
| 97 | int ctx, int token) { |
| 98 | #if CONFIG_NEW_TOKENSET |
| 99 | (void)skip_eob; |
| 100 | return token_costs[token == ZERO_TOKEN || token == EOB_TOKEN][ctx][token]; |
| 101 | #else |
| 102 | return token_costs[skip_eob][ctx][token]; |
| 103 | #endif |
| 104 | } |
| 105 | |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 106 | int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int block, |
| 107 | TX_SIZE tx_size, int ctx) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 108 | MACROBLOCKD *const xd = &mb->e_mbd; |
| 109 | struct macroblock_plane *const p = &mb->plane[plane]; |
| 110 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 111 | const int ref = is_inter_block(&xd->mi[0]->mbmi); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 112 | av1_token_state tokens[MAX_TX_SQUARE + 1][2]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 113 | unsigned best_index[MAX_TX_SQUARE + 1][2]; |
| 114 | uint8_t token_cache[MAX_TX_SQUARE]; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 115 | const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 116 | tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block); |
| 117 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 118 | const int eob = p->eobs[block]; |
Debargha Mukherjee | 3c42c09 | 2016-09-29 09:17:36 -0700 | [diff] [blame] | 119 | const PLANE_TYPE plane_type = pd->plane_type; |
Jingning Han | de953b9 | 2016-10-25 12:35:43 -0700 | [diff] [blame] | 120 | const int default_eob = tx_size_2d[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 121 | const int16_t *const dequant_ptr = pd->dequant; |
| 122 | const uint8_t *const band_translate = get_band_translate(tx_size); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 123 | const int block_raster_idx = av1_block_index_to_raster_order(tx_size, block); |
| 124 | TX_TYPE tx_type = get_tx_type(plane_type, xd, block_raster_idx, tx_size); |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 125 | const SCAN_ORDER *const scan_order = |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 126 | get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi)); |
Urvang Joshi | 03f6fdc | 2016-10-14 15:53:39 -0700 | [diff] [blame] | 127 | const int16_t *const scan = scan_order->scan; |
| 128 | const int16_t *const nb = scan_order->neighbors; |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 129 | int dqv; |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 130 | const int shift = get_tx_scale(tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 131 | #if CONFIG_AOM_QM |
| 132 | int seg_id = xd->mi[0]->mbmi.segment_id; |
Debargha Mukherjee | 3c42c09 | 2016-09-29 09:17:36 -0700 | [diff] [blame] | 133 | const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!ref][tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 134 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 135 | #if CONFIG_NEW_QUANT |
David Barker | d7d78c8 | 2016-10-24 10:55:35 +0100 | [diff] [blame] | 136 | int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 137 | const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq]; |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 138 | #elif !CONFIG_AOM_QM |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 139 | const int dq_step[2] = { dequant_ptr[0] >> shift, dequant_ptr[1] >> shift }; |
| 140 | #endif // CONFIG_NEW_QUANT |
| 141 | int next = eob, sz = 0; |
Debargha Mukherjee | 3c42c09 | 2016-09-29 09:17:36 -0700 | [diff] [blame] | 142 | const int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 143 | const int64_t rddiv = mb->rddiv; |
| 144 | int64_t rd_cost0, rd_cost1; |
| 145 | int rate0, rate1; |
| 146 | int64_t error0, error1; |
| 147 | int16_t t0, t1; |
| 148 | int best, band = (eob < default_eob) ? band_translate[eob] |
| 149 | : band_translate[eob - 1]; |
| 150 | int pt, i, final_eob; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 151 | #if CONFIG_AOM_HIGHBITDEPTH |
| 152 | const int *cat6_high_cost = av1_get_high_cost_table(xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 153 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 154 | const int *cat6_high_cost = av1_get_high_cost_table(8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 155 | #endif |
| 156 | unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] = |
Debargha Mukherjee | 3c42c09 | 2016-09-29 09:17:36 -0700 | [diff] [blame] | 157 | mb->token_costs[txsize_sqr_map[tx_size]][plane_type][ref]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 158 | const uint16_t *band_counts = &band_count_table[tx_size][band]; |
| 159 | uint16_t band_left = eob - band_cum_count_table[tx_size][band] + 1; |
| 160 | int shortcut = 0; |
| 161 | int next_shortcut = 0; |
| 162 | |
David Barker | d7d78c8 | 2016-10-24 10:55:35 +0100 | [diff] [blame] | 163 | assert((mb->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0)); |
Debargha Mukherjee | 3c42c09 | 2016-09-29 09:17:36 -0700 | [diff] [blame] | 164 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 165 | token_costs += band; |
| 166 | |
Debargha Mukherjee | 3c42c09 | 2016-09-29 09:17:36 -0700 | [diff] [blame] | 167 | assert((!plane_type && !plane) || (plane_type && plane)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 168 | assert(eob <= default_eob); |
| 169 | |
| 170 | /* Now set up a Viterbi trellis to evaluate alternative roundings. */ |
| 171 | /* Initialize the sentinel node of the trellis. */ |
| 172 | tokens[eob][0].rate = 0; |
| 173 | tokens[eob][0].error = 0; |
| 174 | tokens[eob][0].next = default_eob; |
| 175 | tokens[eob][0].token = EOB_TOKEN; |
| 176 | tokens[eob][0].qc = 0; |
| 177 | tokens[eob][1] = tokens[eob][0]; |
| 178 | |
| 179 | for (i = 0; i < eob; i++) { |
| 180 | const int rc = scan[i]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 181 | tokens[i][0].rate = av1_get_token_cost(qcoeff[rc], &t0, cat6_high_cost); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 182 | tokens[i][0].token = t0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 183 | token_cache[rc] = av1_pt_energy_class[t0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | for (i = eob; i-- > 0;) { |
| 187 | int base_bits, dx; |
| 188 | int64_t d2; |
| 189 | const int rc = scan[i]; |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 190 | int x = qcoeff[rc]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 191 | #if CONFIG_AOM_QM |
| 192 | int iwt = iqmatrix[rc]; |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 193 | dqv = dequant_ptr[rc != 0]; |
| 194 | dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS; |
| 195 | #else |
| 196 | dqv = dequant_ptr[rc != 0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 197 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 198 | next_shortcut = shortcut; |
| 199 | |
| 200 | /* Only add a trellis state for non-zero coefficients. */ |
| 201 | if (UNLIKELY(x)) { |
| 202 | error0 = tokens[next][0].error; |
| 203 | error1 = tokens[next][1].error; |
| 204 | /* Evaluate the first possibility for this state. */ |
| 205 | rate0 = tokens[next][0].rate; |
| 206 | rate1 = tokens[next][1].rate; |
| 207 | |
| 208 | if (next_shortcut) { |
| 209 | /* Consider both possible successor states. */ |
| 210 | if (next < default_eob) { |
| 211 | pt = get_coef_context(nb, token_cache, i + 1); |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 212 | rate0 += |
| 213 | get_token_bit_costs(*token_costs, 0, pt, tokens[next][0].token); |
| 214 | rate1 += |
| 215 | get_token_bit_costs(*token_costs, 0, pt, tokens[next][1].token); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 216 | } |
| 217 | UPDATE_RD_COST(); |
| 218 | /* And pick the best. */ |
| 219 | best = rd_cost1 < rd_cost0; |
| 220 | } else { |
| 221 | if (next < default_eob) { |
| 222 | pt = get_coef_context(nb, token_cache, i + 1); |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 223 | rate0 += |
| 224 | get_token_bit_costs(*token_costs, 0, pt, tokens[next][0].token); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 225 | } |
| 226 | best = 0; |
| 227 | } |
| 228 | |
| 229 | dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 230 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 231 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 232 | dx >>= xd->bd - 8; |
| 233 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 234 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 235 | d2 = (int64_t)dx * dx; |
| 236 | tokens[i][0].rate += (best ? rate1 : rate0); |
| 237 | tokens[i][0].error = d2 + (best ? error1 : error0); |
| 238 | tokens[i][0].next = next; |
| 239 | tokens[i][0].qc = x; |
| 240 | tokens[i][0].dqc = dqcoeff[rc]; |
| 241 | best_index[i][0] = best; |
| 242 | |
| 243 | /* Evaluate the second possibility for this state. */ |
| 244 | rate0 = tokens[next][0].rate; |
| 245 | rate1 = tokens[next][1].rate; |
| 246 | |
| 247 | // The threshold of 3 is empirically obtained. |
| 248 | if (UNLIKELY(abs(x) > 3)) { |
| 249 | shortcut = 0; |
| 250 | } else { |
| 251 | #if CONFIG_NEW_QUANT |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 252 | shortcut = ((av1_dequant_abscoeff_nuq(abs(x), dqv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 253 | dequant_val[band_translate[i]]) > |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 254 | (abs(coeff[rc]) << shift)) && |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 255 | (av1_dequant_abscoeff_nuq(abs(x) - 1, dqv, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 256 | dequant_val[band_translate[i]]) < |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 257 | (abs(coeff[rc]) << shift))); |
| 258 | #else // CONFIG_NEW_QUANT |
| 259 | #if CONFIG_AOM_QM |
| 260 | if ((abs(x) * dequant_ptr[rc != 0] * iwt > |
| 261 | ((abs(coeff[rc]) << shift) << AOM_QM_BITS)) && |
| 262 | (abs(x) * dequant_ptr[rc != 0] * iwt < |
| 263 | (((abs(coeff[rc]) << shift) + dequant_ptr[rc != 0]) |
| 264 | << AOM_QM_BITS))) |
| 265 | #else |
| 266 | if ((abs(x) * dequant_ptr[rc != 0] > (abs(coeff[rc]) << shift)) && |
| 267 | (abs(x) * dequant_ptr[rc != 0] < |
| 268 | (abs(coeff[rc]) << shift) + dequant_ptr[rc != 0])) |
| 269 | #endif // CONFIG_AOM_QM |
| 270 | shortcut = 1; |
| 271 | else |
| 272 | shortcut = 0; |
| 273 | #endif // CONFIG_NEW_QUANT |
| 274 | } |
| 275 | |
| 276 | if (shortcut) { |
| 277 | sz = -(x < 0); |
| 278 | x -= 2 * sz + 1; |
| 279 | } else { |
| 280 | tokens[i][1] = tokens[i][0]; |
| 281 | best_index[i][1] = best_index[i][0]; |
| 282 | next = i; |
| 283 | |
| 284 | if (UNLIKELY(!(--band_left))) { |
| 285 | --band_counts; |
| 286 | band_left = *band_counts; |
| 287 | --token_costs; |
| 288 | } |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | /* Consider both possible successor states. */ |
| 293 | if (!x) { |
| 294 | /* If we reduced this coefficient to zero, check to see if |
| 295 | * we need to move the EOB back here. |
| 296 | */ |
| 297 | t0 = tokens[next][0].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN; |
| 298 | t1 = tokens[next][1].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN; |
| 299 | base_bits = 0; |
| 300 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 301 | base_bits = av1_get_token_cost(x, &t0, cat6_high_cost); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 302 | t1 = t0; |
| 303 | } |
| 304 | |
| 305 | if (next_shortcut) { |
| 306 | if (LIKELY(next < default_eob)) { |
| 307 | if (t0 != EOB_TOKEN) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 308 | token_cache[rc] = av1_pt_energy_class[t0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 309 | pt = get_coef_context(nb, token_cache, i + 1); |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 310 | rate0 += get_token_bit_costs(*token_costs, !x, pt, |
| 311 | tokens[next][0].token); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 312 | } |
| 313 | if (t1 != EOB_TOKEN) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 314 | token_cache[rc] = av1_pt_energy_class[t1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 315 | pt = get_coef_context(nb, token_cache, i + 1); |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 316 | rate1 += get_token_bit_costs(*token_costs, !x, pt, |
| 317 | tokens[next][1].token); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | UPDATE_RD_COST(); |
| 322 | /* And pick the best. */ |
| 323 | best = rd_cost1 < rd_cost0; |
| 324 | } else { |
| 325 | // The two states in next stage are identical. |
| 326 | if (next < default_eob && t0 != EOB_TOKEN) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 327 | token_cache[rc] = av1_pt_energy_class[t0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 328 | pt = get_coef_context(nb, token_cache, i + 1); |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 329 | rate0 += |
| 330 | get_token_bit_costs(*token_costs, !x, pt, tokens[next][0].token); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 331 | } |
| 332 | best = 0; |
| 333 | } |
| 334 | |
| 335 | #if CONFIG_NEW_QUANT |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 336 | dx = av1_dequant_coeff_nuq(x, dqv, dequant_val[band_translate[i]]) - |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 337 | (coeff[rc] << shift); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 338 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 339 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 340 | dx >>= xd->bd - 8; |
| 341 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 342 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 343 | #else // CONFIG_NEW_QUANT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 344 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 345 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 346 | dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 347 | } else { |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 348 | dx -= (dqv + sz) ^ sz; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 349 | } |
| 350 | #else |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 351 | dx -= (dqv + sz) ^ sz; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 352 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 353 | #endif // CONFIG_NEW_QUANT |
| 354 | d2 = (int64_t)dx * dx; |
| 355 | |
| 356 | tokens[i][1].rate = base_bits + (best ? rate1 : rate0); |
| 357 | tokens[i][1].error = d2 + (best ? error1 : error0); |
| 358 | tokens[i][1].next = next; |
| 359 | tokens[i][1].token = best ? t1 : t0; |
| 360 | tokens[i][1].qc = x; |
| 361 | |
| 362 | if (x) { |
| 363 | #if CONFIG_NEW_QUANT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 364 | tokens[i][1].dqc = av1_dequant_abscoeff_nuq( |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 365 | abs(x), dqv, dequant_val[band_translate[i]]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 366 | tokens[i][1].dqc = shift ? ROUND_POWER_OF_TWO(tokens[i][1].dqc, shift) |
| 367 | : tokens[i][1].dqc; |
| 368 | if (sz) tokens[i][1].dqc = -tokens[i][1].dqc; |
| 369 | #else |
Debargha Mukherjee | b98a702 | 2016-11-15 16:07:12 -0800 | [diff] [blame] | 370 | // The 32x32 transform coefficient uses half quantization step size. |
| 371 | // Account for the rounding difference in the dequantized coefficeint |
| 372 | // value when the quantization index is dropped from an even number |
| 373 | // to an odd number. |
Thomas Davies | e0f8c55 | 2016-11-15 17:28:03 +0000 | [diff] [blame] | 374 | |
| 375 | #if CONFIG_AOM_QM |
| 376 | tran_low_t offset = dqv >> shift; |
| 377 | #else |
| 378 | tran_low_t offset = dq_step[rc != 0]; |
| 379 | #endif |
| 380 | if (shift & x) offset += (dqv & 0x01); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 381 | |
| 382 | if (sz == 0) |
| 383 | tokens[i][1].dqc = dqcoeff[rc] - offset; |
| 384 | else |
| 385 | tokens[i][1].dqc = dqcoeff[rc] + offset; |
| 386 | #endif // CONFIG_NEW_QUANT |
| 387 | } else { |
| 388 | tokens[i][1].dqc = 0; |
| 389 | } |
| 390 | |
| 391 | best_index[i][1] = best; |
| 392 | /* Finally, make this the new head of the trellis. */ |
| 393 | next = i; |
| 394 | } else { |
| 395 | /* There's no choice to make for a zero coefficient, so we don't |
| 396 | * add a new trellis node, but we do need to update the costs. |
| 397 | */ |
| 398 | t0 = tokens[next][0].token; |
| 399 | t1 = tokens[next][1].token; |
| 400 | pt = get_coef_context(nb, token_cache, i + 1); |
| 401 | /* Update the cost of each path if we're past the EOB token. */ |
| 402 | if (t0 != EOB_TOKEN) { |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 403 | tokens[next][0].rate += get_token_bit_costs(*token_costs, 1, pt, t0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 404 | tokens[next][0].token = ZERO_TOKEN; |
| 405 | } |
| 406 | if (t1 != EOB_TOKEN) { |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 407 | tokens[next][1].rate += get_token_bit_costs(*token_costs, 1, pt, t1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 408 | tokens[next][1].token = ZERO_TOKEN; |
| 409 | } |
| 410 | best_index[i][0] = best_index[i][1] = 0; |
| 411 | shortcut = (tokens[next][0].rate != tokens[next][1].rate); |
| 412 | /* Don't update next, because we didn't add a new node. */ |
| 413 | } |
| 414 | |
| 415 | if (UNLIKELY(!(--band_left))) { |
| 416 | --band_counts; |
| 417 | band_left = *band_counts; |
| 418 | --token_costs; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /* Now pick the best path through the whole trellis. */ |
| 423 | rate0 = tokens[next][0].rate; |
| 424 | rate1 = tokens[next][1].rate; |
| 425 | error0 = tokens[next][0].error; |
| 426 | error1 = tokens[next][1].error; |
| 427 | t0 = tokens[next][0].token; |
| 428 | t1 = tokens[next][1].token; |
Thomas Davies | ed8e2d2 | 2017-01-04 16:42:09 +0000 | [diff] [blame] | 429 | rate0 += get_token_bit_costs(*token_costs, 0, ctx, t0); |
| 430 | rate1 += get_token_bit_costs(*token_costs, 0, ctx, t1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 431 | UPDATE_RD_COST(); |
| 432 | best = rd_cost1 < rd_cost0; |
| 433 | |
| 434 | final_eob = -1; |
| 435 | |
| 436 | for (i = next; i < eob; i = next) { |
| 437 | const int x = tokens[i][best].qc; |
| 438 | const int rc = scan[i]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 439 | if (x) final_eob = i; |
| 440 | qcoeff[rc] = x; |
| 441 | dqcoeff[rc] = tokens[i][best].dqc; |
| 442 | |
| 443 | next = tokens[i][best].next; |
| 444 | best = best_index[i][best]; |
| 445 | } |
| 446 | final_eob++; |
| 447 | |
| 448 | mb->plane[plane].eobs[block] = final_eob; |
| 449 | assert(final_eob <= default_eob); |
| 450 | return final_eob; |
| 451 | } |
| 452 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 453 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 454 | typedef enum QUANT_FUNC { |
| 455 | QUANT_FUNC_LOWBD = 0, |
| 456 | QUANT_FUNC_HIGHBD = 1, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 457 | QUANT_FUNC_TYPES = 2 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 458 | } QUANT_FUNC; |
| 459 | |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 460 | static AV1_QUANT_FACADE |
| 461 | quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = { |
| 462 | { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade }, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 463 | { av1_quantize_b_facade, av1_highbd_quantize_b_facade }, |
| 464 | { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade }, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 465 | #if CONFIG_NEW_QUANT |
| 466 | { av1_quantize_fp_nuq_facade, av1_highbd_quantize_fp_nuq_facade }, |
| 467 | { av1_quantize_b_nuq_facade, av1_highbd_quantize_b_nuq_facade }, |
| 468 | { av1_quantize_dc_nuq_facade, av1_highbd_quantize_dc_nuq_facade }, |
| 469 | #endif // CONFIG_NEW_QUANT |
| 470 | { NULL, NULL } |
| 471 | }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 472 | |
Yaowu Xu | 02d4c3b | 2016-11-07 10:45:56 -0800 | [diff] [blame] | 473 | #elif !CONFIG_PVQ |
| 474 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 475 | typedef enum QUANT_FUNC { |
| 476 | QUANT_FUNC_LOWBD = 0, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 477 | QUANT_FUNC_TYPES = 1 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 478 | } QUANT_FUNC; |
| 479 | |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 480 | static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_TYPES] |
| 481 | [QUANT_FUNC_TYPES] = { |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 482 | { av1_quantize_fp_facade }, |
| 483 | { av1_quantize_b_facade }, |
| 484 | { av1_quantize_dc_facade }, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 485 | #if CONFIG_NEW_QUANT |
| 486 | { av1_quantize_fp_nuq_facade }, |
| 487 | { av1_quantize_b_nuq_facade }, |
| 488 | { av1_quantize_dc_nuq_facade }, |
| 489 | #endif // CONFIG_NEW_QUANT |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 490 | { NULL } |
| 491 | }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 492 | #endif |
| 493 | |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 494 | void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block, |
| 495 | int blk_row, int blk_col, BLOCK_SIZE plane_bsize, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 496 | TX_SIZE tx_size, int ctx, |
| 497 | AV1_XFORM_QUANT xform_quant_idx) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 498 | MACROBLOCKD *const xd = &x->e_mbd; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 499 | #if !(CONFIG_PVQ || CONFIG_DAALA_DIST) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 500 | const struct macroblock_plane *const p = &x->plane[plane]; |
| 501 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 502 | #else |
| 503 | struct macroblock_plane *const p = &x->plane[plane]; |
| 504 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 505 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 506 | PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 507 | const int block_raster_idx = av1_block_index_to_raster_order(tx_size, block); |
| 508 | TX_TYPE tx_type = get_tx_type(plane_type, xd, block_raster_idx, tx_size); |
Debargha Mukherjee | 3c42c09 | 2016-09-29 09:17:36 -0700 | [diff] [blame] | 509 | const int is_inter = is_inter_block(&xd->mi[0]->mbmi); |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 510 | const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 511 | tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block); |
| 512 | tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block); |
| 513 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 514 | uint16_t *const eob = &p->eobs[block]; |
Jingning Han | ae5cfde | 2016-11-30 12:01:44 -0800 | [diff] [blame] | 515 | const int diff_stride = block_size_wide[plane_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 516 | #if CONFIG_AOM_QM |
| 517 | int seg_id = xd->mi[0]->mbmi.segment_id; |
Debargha Mukherjee | 3c42c09 | 2016-09-29 09:17:36 -0700 | [diff] [blame] | 518 | const qm_val_t *qmatrix = pd->seg_qmatrix[seg_id][!is_inter][tx_size]; |
| 519 | const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!is_inter][tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 520 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 521 | |
| 522 | FWD_TXFM_PARAM fwd_txfm_param; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 523 | |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 524 | #if CONFIG_PVQ || CONFIG_DAALA_DIST |
| 525 | uint8_t *dst; |
| 526 | int16_t *pred; |
| 527 | const int dst_stride = pd->dst.stride; |
| 528 | int tx_blk_size; |
| 529 | int i, j; |
| 530 | #endif |
| 531 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 532 | #if !CONFIG_PVQ |
| 533 | const int tx2d_size = tx_size_2d[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 534 | QUANT_PARAM qparam; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 535 | const int16_t *src_diff; |
| 536 | |
Jingning Han | 8149226 | 2016-12-05 15:48:47 -0800 | [diff] [blame] | 537 | src_diff = |
| 538 | &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]]; |
Debargha Mukherjee | 0e11912 | 2016-11-04 12:10:23 -0700 | [diff] [blame] | 539 | qparam.log_scale = get_tx_scale(tx_size); |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 540 | #if CONFIG_NEW_QUANT |
| 541 | qparam.tx_size = tx_size; |
| 542 | qparam.dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type); |
| 543 | #endif // CONFIG_NEW_QUANT |
| 544 | #if CONFIG_AOM_QM |
| 545 | qparam.qmatrix = qmatrix; |
| 546 | qparam.iqmatrix = iqmatrix; |
| 547 | #endif // CONFIG_AOM_QM |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 548 | #else |
| 549 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 550 | tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 551 | int skip = 1; |
| 552 | PVQ_INFO *pvq_info = NULL; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 553 | uint8_t *src; |
| 554 | int16_t *src_int16; |
| 555 | const int src_stride = p->src.stride; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 556 | |
| 557 | (void)scan_order; |
| 558 | (void)qcoeff; |
| 559 | |
| 560 | if (x->pvq_coded) { |
| 561 | assert(block < MAX_PVQ_BLOCKS_IN_SB); |
| 562 | pvq_info = &x->pvq[block][plane]; |
| 563 | } |
Jingning Han | 8149226 | 2016-12-05 15:48:47 -0800 | [diff] [blame] | 564 | src = &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]]; |
| 565 | src_int16 = |
| 566 | &p->src_int16[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]]; |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 567 | |
| 568 | // transform block size in pixels |
| 569 | tx_blk_size = tx_size_wide[tx_size]; |
| 570 | |
| 571 | for (j = 0; j < tx_blk_size; j++) |
| 572 | for (i = 0; i < tx_blk_size; i++) |
| 573 | src_int16[diff_stride * j + i] = src[src_stride * j + i]; |
| 574 | #endif |
| 575 | |
| 576 | #if CONFIG_PVQ || CONFIG_DAALA_DIST |
| 577 | dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]]; |
Jingning Han | 8149226 | 2016-12-05 15:48:47 -0800 | [diff] [blame] | 578 | pred = &pd->pred[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 579 | |
| 580 | // transform block size in pixels |
| 581 | tx_blk_size = tx_size_wide[tx_size]; |
| 582 | |
| 583 | // copy uint8 orig and predicted block to int16 buffer |
| 584 | // in order to use existing VP10 transform functions |
| 585 | for (j = 0; j < tx_blk_size; j++) |
| 586 | for (i = 0; i < tx_blk_size; i++) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 587 | pred[diff_stride * j + i] = dst[dst_stride * j + i]; |
| 588 | } |
| 589 | #endif |
Yushin Cho | 7a428ba | 2017-01-12 16:28:49 -0800 | [diff] [blame] | 590 | |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 591 | (void)ctx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 592 | |
| 593 | fwd_txfm_param.tx_type = tx_type; |
| 594 | fwd_txfm_param.tx_size = tx_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 595 | fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id]; |
| 596 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 597 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 598 | fwd_txfm_param.bd = xd->bd; |
| 599 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 600 | highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 601 | if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 602 | if (LIKELY(!x->skip_block)) { |
| 603 | quant_func_list[xform_quant_idx][QUANT_FUNC_HIGHBD]( |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 604 | coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 605 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 606 | av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | return; |
| 610 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 611 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 612 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 613 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 614 | fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 615 | if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 616 | if (LIKELY(!x->skip_block)) { |
| 617 | quant_func_list[xform_quant_idx][QUANT_FUNC_LOWBD]( |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 618 | coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 619 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 620 | av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 621 | } |
| 622 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 623 | #else // #if !CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 624 | |
Angie Chiang | 2cc057c | 2017-01-03 18:31:47 -0800 | [diff] [blame] | 625 | (void)xform_quant_idx; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 626 | fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param); |
| 627 | fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param); |
| 628 | |
| 629 | // PVQ for inter mode block |
ltrudeau | 472f63f | 2017-01-12 15:22:32 -0500 | [diff] [blame] | 630 | if (!x->skip_block) { |
ltrudeau | e1c0929 | 2017-01-20 15:42:13 -0500 | [diff] [blame] | 631 | PVQ_SKIP_TYPE ac_dc_coded = |
| 632 | av1_pvq_encode_helper(&x->daala_enc, |
| 633 | coeff, // target original vector |
| 634 | ref_coeff, // reference vector |
| 635 | dqcoeff, // de-quantized vector |
| 636 | eob, // End of Block marker |
| 637 | pd->dequant, // aom's quantizers |
| 638 | plane, // image plane |
| 639 | tx_size, // block size in log_2 - 2 |
| 640 | tx_type, |
| 641 | &x->rate, // rate measured |
| 642 | x->pvq_speed, |
| 643 | pvq_info); // PVQ info for a block |
| 644 | skip = ac_dc_coded == PVQ_SKIP; |
ltrudeau | 472f63f | 2017-01-12 15:22:32 -0500 | [diff] [blame] | 645 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 646 | x->pvq_skip[plane] = skip; |
| 647 | |
| 648 | if (!skip) mbmi->skip = 0; |
| 649 | #endif // #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 652 | static void encode_block(int plane, int block, int blk_row, int blk_col, |
| 653 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) { |
| 654 | struct encode_b_args *const args = arg; |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 655 | AV1_COMMON *cm = args->cm; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 656 | MACROBLOCK *const x = args->x; |
| 657 | MACROBLOCKD *const xd = &x->e_mbd; |
| 658 | int ctx; |
| 659 | struct macroblock_plane *const p = &x->plane[plane]; |
| 660 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 661 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 662 | uint8_t *dst; |
| 663 | ENTROPY_CONTEXT *a, *l; |
| 664 | INV_TXFM_PARAM inv_txfm_param; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 665 | const int block_raster_idx = av1_block_index_to_raster_order(tx_size, block); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 666 | #if CONFIG_PVQ |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 667 | int tx_width_pixels, tx_height_pixels; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 668 | int i, j; |
| 669 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 670 | #if CONFIG_VAR_TX |
| 671 | int i; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 672 | int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 673 | #endif |
Jingning Han | 8149226 | 2016-12-05 15:48:47 -0800 | [diff] [blame] | 674 | dst = &pd->dst |
| 675 | .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 676 | a = &args->ta[blk_col]; |
| 677 | l = &args->tl[blk_row]; |
| 678 | #if CONFIG_VAR_TX |
| 679 | ctx = get_entropy_context(tx_size, a, l); |
| 680 | #else |
| 681 | ctx = combine_entropy_contexts(*a, *l); |
| 682 | #endif |
| 683 | |
| 684 | #if CONFIG_VAR_TX |
Yue Chen | a1e48dc | 2016-08-29 17:29:33 -0700 | [diff] [blame] | 685 | // Assert not magic number (uninitialized). |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 686 | assert(x->blk_skip[plane][blk_row * bw + blk_col] != 234); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 687 | |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 688 | if (x->blk_skip[plane][blk_row * bw + blk_col] == 0) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 689 | #else |
| 690 | { |
| 691 | #endif |
| 692 | #if CONFIG_NEW_QUANT |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 693 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 694 | ctx, AV1_XFORM_QUANT_FP_NUQ); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 695 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 696 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 697 | ctx, AV1_XFORM_QUANT_FP); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 698 | #endif // CONFIG_NEW_QUANT |
| 699 | } |
| 700 | #if CONFIG_VAR_TX |
| 701 | else { |
| 702 | p->eobs[block] = 0; |
| 703 | } |
| 704 | #endif |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 705 | #if !CONFIG_PVQ |
Jingning Han | db4c9ba | 2016-11-30 10:00:25 -0800 | [diff] [blame] | 706 | if (p->eobs[block] && !xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 707 | *a = *l = av1_optimize_b(cm, x, plane, block, tx_size, ctx) > 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 708 | } else { |
| 709 | *a = *l = p->eobs[block] > 0; |
| 710 | } |
| 711 | |
| 712 | #if CONFIG_VAR_TX |
Jingning Han | de953b9 | 2016-10-25 12:35:43 -0700 | [diff] [blame] | 713 | for (i = 0; i < tx_size_wide_unit[tx_size]; ++i) a[i] = a[0]; |
| 714 | |
| 715 | for (i = 0; i < tx_size_high_unit[tx_size]; ++i) l[i] = l[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 716 | #endif |
| 717 | |
| 718 | if (p->eobs[block]) *(args->skip) = 0; |
| 719 | |
| 720 | if (p->eobs[block] == 0) return; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 721 | #else |
| 722 | (void)ctx; |
| 723 | *a = *l = !x->pvq_skip[plane]; |
| 724 | |
| 725 | if (!x->pvq_skip[plane]) *(args->skip) = 0; |
| 726 | |
| 727 | if (x->pvq_skip[plane]) return; |
| 728 | |
| 729 | // transform block size in pixels |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 730 | tx_width_pixels = tx_size_wide[tx_size]; |
| 731 | tx_height_pixels = tx_size_high[tx_size]; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 732 | |
| 733 | // Since av1 does not have separate function which does inverse transform |
| 734 | // but av1_inv_txfm_add_*x*() also does addition of predicted image to |
| 735 | // inverse transformed image, |
| 736 | // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 737 | for (j = 0; j < tx_height_pixels; j++) |
| 738 | for (i = 0; i < tx_width_pixels; i++) dst[j * pd->dst.stride + i] = 0; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 739 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 740 | |
| 741 | // inverse transform parameters |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 742 | inv_txfm_param.tx_type = |
| 743 | get_tx_type(pd->plane_type, xd, block_raster_idx, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 744 | inv_txfm_param.tx_size = tx_size; |
| 745 | inv_txfm_param.eob = p->eobs[block]; |
| 746 | inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id]; |
| 747 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 748 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 749 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 750 | inv_txfm_param.bd = xd->bd; |
| 751 | highbd_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &inv_txfm_param); |
| 752 | return; |
| 753 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 754 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 755 | inv_txfm_add(dqcoeff, dst, pd->dst.stride, &inv_txfm_param); |
| 756 | } |
| 757 | |
| 758 | #if CONFIG_VAR_TX |
| 759 | static void encode_block_inter(int plane, int block, int blk_row, int blk_col, |
| 760 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, |
| 761 | void *arg) { |
| 762 | struct encode_b_args *const args = arg; |
| 763 | MACROBLOCK *const x = args->x; |
| 764 | MACROBLOCKD *const xd = &x->e_mbd; |
| 765 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 766 | const BLOCK_SIZE bsize = txsize_to_bsize[tx_size]; |
| 767 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 768 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 769 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
| 770 | TX_SIZE plane_tx_size; |
Jingning Han | f65b870 | 2016-10-31 12:13:20 -0700 | [diff] [blame] | 771 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 772 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 773 | |
| 774 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 775 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 776 | plane_tx_size = |
| 777 | plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0] |
| 778 | : mbmi->inter_tx_size[tx_row][tx_col]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 779 | |
| 780 | if (tx_size == plane_tx_size) { |
| 781 | encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg); |
| 782 | } else { |
Jingning Han | a933632 | 2016-11-02 15:45:07 -0700 | [diff] [blame] | 783 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
| 784 | // This is the square transform block partition entry point. |
| 785 | int bsl = tx_size_wide_unit[sub_txs]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 786 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 787 | assert(bsl > 0); |
Jingning Han | d3fada8 | 2016-11-22 10:46:55 -0800 | [diff] [blame] | 788 | assert(tx_size < TX_SIZES_ALL); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 789 | |
| 790 | for (i = 0; i < 4; ++i) { |
Jingning Han | de953b9 | 2016-10-25 12:35:43 -0700 | [diff] [blame] | 791 | const int offsetr = blk_row + ((i >> 1) * bsl); |
| 792 | const int offsetc = blk_col + ((i & 0x01) * bsl); |
Jingning Han | de953b9 | 2016-10-25 12:35:43 -0700 | [diff] [blame] | 793 | int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 794 | |
| 795 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
| 796 | |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 797 | encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs, |
| 798 | arg); |
| 799 | block += step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
| 803 | #endif |
| 804 | |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 805 | typedef struct encode_block_pass1_args { |
| 806 | AV1_COMMON *cm; |
| 807 | MACROBLOCK *x; |
| 808 | } encode_block_pass1_args; |
| 809 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 810 | static void encode_block_pass1(int plane, int block, int blk_row, int blk_col, |
| 811 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, |
| 812 | void *arg) { |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 813 | encode_block_pass1_args *args = (encode_block_pass1_args *)arg; |
| 814 | AV1_COMMON *cm = args->cm; |
| 815 | MACROBLOCK *const x = args->x; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 816 | MACROBLOCKD *const xd = &x->e_mbd; |
| 817 | struct macroblock_plane *const p = &x->plane[plane]; |
| 818 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 819 | tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 820 | uint8_t *dst; |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 821 | int ctx = 0; |
Jingning Han | 8149226 | 2016-12-05 15:48:47 -0800 | [diff] [blame] | 822 | dst = &pd->dst |
| 823 | .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 824 | |
| 825 | #if CONFIG_NEW_QUANT |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 826 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 827 | ctx, AV1_XFORM_QUANT_B_NUQ); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 828 | #else |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 829 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 830 | ctx, AV1_XFORM_QUANT_B); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 831 | #endif // CONFIG_NEW_QUANT |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 832 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 833 | if (p->eobs[block] > 0) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 834 | #else |
| 835 | if (!x->pvq_skip[plane]) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 836 | { |
| 837 | int tx_blk_size; |
| 838 | int i, j; |
| 839 | // transform block size in pixels |
| 840 | tx_blk_size = tx_size_wide[tx_size]; |
| 841 | |
| 842 | // Since av1 does not have separate function which does inverse transform |
| 843 | // but av1_inv_txfm_add_*x*() also does addition of predicted image to |
| 844 | // inverse transformed image, |
| 845 | // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros |
| 846 | for (j = 0; j < tx_blk_size; j++) |
| 847 | for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0; |
| 848 | } |
Jingning Han | df07264 | 2016-11-08 16:43:38 -0800 | [diff] [blame] | 849 | #endif // !CONFIG_PVQ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 850 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 851 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 852 | if (xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 853 | av1_highbd_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block], |
| 854 | xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 855 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 856 | av1_highbd_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block], |
| 857 | xd->bd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 858 | } |
| 859 | return; |
| 860 | } |
Jingning Han | e38d63c | 2016-11-08 12:05:29 -0800 | [diff] [blame] | 861 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 862 | if (xd->lossless[xd->mi[0]->mbmi.segment_id]) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 863 | av1_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 864 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 865 | av1_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 870 | void av1_encode_sby_pass1(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize) { |
| 871 | encode_block_pass1_args args = { cm, x }; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 872 | av1_subtract_plane(x, bsize, 0); |
| 873 | av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0, |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 874 | encode_block_pass1, &args); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Jingning Han | 18c53c8 | 2017-02-17 14:49:57 -0800 | [diff] [blame^] | 877 | void av1_encode_sb(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize, |
| 878 | const int mi_row, const int mi_col) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 879 | MACROBLOCKD *const xd = &x->e_mbd; |
| 880 | struct optimize_ctx ctx; |
| 881 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 882 | struct encode_b_args arg = { cm, x, &ctx, &mbmi->skip, NULL, NULL, 1 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 883 | int plane; |
| 884 | |
| 885 | mbmi->skip = 1; |
| 886 | |
| 887 | if (x->skip) return; |
| 888 | |
| 889 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 890 | #if CONFIG_VAR_TX |
| 891 | // TODO(jingning): Clean this up. |
| 892 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 893 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
Jingning Han | de953b9 | 2016-10-25 12:35:43 -0700 | [diff] [blame] | 894 | const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 895 | const int mi_height = block_size_high[plane_bsize] >> tx_size_wide_log2[0]; |
Jingning Han | 70e5f3f | 2016-11-09 17:03:07 -0800 | [diff] [blame] | 896 | const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 897 | const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size]; |
Jingning Han | de953b9 | 2016-10-25 12:35:43 -0700 | [diff] [blame] | 898 | const int bw = block_size_wide[txb_size] >> tx_size_wide_log2[0]; |
| 899 | const int bh = block_size_high[txb_size] >> tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 900 | int idx, idy; |
| 901 | int block = 0; |
Jingning Han | de953b9 | 2016-10-25 12:35:43 -0700 | [diff] [blame] | 902 | int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size]; |
Jingning Han | 581d169 | 2017-01-05 16:03:54 -0800 | [diff] [blame] | 903 | av1_get_entropy_contexts(bsize, 0, pd, ctx.ta[plane], ctx.tl[plane]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 904 | #else |
| 905 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 906 | const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 907 | av1_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane], ctx.tl[plane]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 908 | #endif |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 909 | #if !CONFIG_PVQ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 910 | av1_subtract_plane(x, bsize, plane); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 911 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 912 | arg.ta = ctx.ta[plane]; |
| 913 | arg.tl = ctx.tl[plane]; |
| 914 | |
Jingning Han | 18c53c8 | 2017-02-17 14:49:57 -0800 | [diff] [blame^] | 915 | #if CONFIG_CB4X4 |
| 916 | if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col)) |
| 917 | continue; |
| 918 | #else |
| 919 | (void)mi_row; |
| 920 | (void)mi_col; |
| 921 | #endif |
| 922 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 923 | #if CONFIG_VAR_TX |
Jingning Han | fe45b21 | 2016-11-22 10:30:23 -0800 | [diff] [blame] | 924 | for (idy = 0; idy < mi_height; idy += bh) { |
| 925 | for (idx = 0; idx < mi_width; idx += bw) { |
| 926 | encode_block_inter(plane, block, idy, idx, plane_bsize, max_tx_size, |
| 927 | &arg); |
| 928 | block += step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 929 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 930 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 931 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 932 | av1_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block, |
| 933 | &arg); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 934 | #endif |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | #if CONFIG_SUPERTX |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 939 | void av1_encode_sb_supertx(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 940 | MACROBLOCKD *const xd = &x->e_mbd; |
| 941 | struct optimize_ctx ctx; |
| 942 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 943 | struct encode_b_args arg = { cm, x, &ctx, &mbmi->skip, NULL, NULL, 1 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 944 | int plane; |
| 945 | |
| 946 | mbmi->skip = 1; |
| 947 | if (x->skip) return; |
| 948 | |
| 949 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
| 950 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 951 | #if CONFIG_VAR_TX |
| 952 | const TX_SIZE tx_size = TX_4X4; |
| 953 | #else |
| 954 | const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi, pd) : mbmi->tx_size; |
| 955 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 956 | av1_subtract_plane(x, bsize, plane); |
| 957 | av1_get_entropy_contexts(bsize, tx_size, pd, ctx.ta[plane], ctx.tl[plane]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 958 | arg.ta = ctx.ta[plane]; |
| 959 | arg.tl = ctx.tl[plane]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 960 | av1_foreach_transformed_block_in_plane(xd, bsize, plane, encode_block, |
| 961 | &arg); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | #endif // CONFIG_SUPERTX |
| 965 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 966 | void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col, |
| 967 | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, |
| 968 | void *arg) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 969 | struct encode_b_args *const args = arg; |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 970 | AV1_COMMON *cm = args->cm; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 971 | MACROBLOCK *const x = args->x; |
| 972 | MACROBLOCKD *const xd = &x->e_mbd; |
| 973 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 974 | struct macroblock_plane *const p = &x->plane[plane]; |
| 975 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 976 | tran_low_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); |
| 977 | PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 978 | const int block_raster_idx = av1_block_index_to_raster_order(tx_size, block); |
| 979 | const TX_TYPE tx_type = |
| 980 | get_tx_type(plane_type, xd, block_raster_idx, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 981 | PREDICTION_MODE mode; |
Jingning Han | 62a2b9e | 2016-10-24 10:32:37 -0700 | [diff] [blame] | 982 | const int diff_stride = block_size_wide[plane_bsize]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 983 | uint8_t *src, *dst; |
| 984 | int16_t *src_diff; |
| 985 | uint16_t *eob = &p->eobs[block]; |
| 986 | const int src_stride = p->src.stride; |
| 987 | const int dst_stride = pd->dst.stride; |
Jingning Han | 62a2b9e | 2016-10-24 10:32:37 -0700 | [diff] [blame] | 988 | const int tx1d_width = tx_size_wide[tx_size]; |
| 989 | const int tx1d_height = tx_size_high[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 990 | ENTROPY_CONTEXT *a = NULL, *l = NULL; |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 991 | int ctx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 992 | INV_TXFM_PARAM inv_txfm_param; |
Yushin Cho | 3827fdd | 2016-11-09 15:50:23 -0800 | [diff] [blame] | 993 | #if CONFIG_PVQ |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 994 | int tx_blk_size; |
| 995 | int i, j; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 996 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 997 | |
Jingning Han | 8149226 | 2016-12-05 15:48:47 -0800 | [diff] [blame] | 998 | dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]]; |
| 999 | src = &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]]; |
| 1000 | src_diff = |
| 1001 | &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]]; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 1002 | mode = (plane == 0) ? get_y_mode(xd->mi[0], block_raster_idx) : mbmi->uv_mode; |
David Barker | 839467f | 2017-01-19 11:06:15 +0000 | [diff] [blame] | 1003 | av1_predict_intra_block(xd, pd->width, pd->height, txsize_to_bsize[tx_size], |
| 1004 | mode, dst, dst_stride, dst, dst_stride, blk_col, |
| 1005 | blk_row, plane); |
Jingning Han | e325abd | 2016-12-01 09:35:10 -0800 | [diff] [blame] | 1006 | |
| 1007 | if (check_subtract_block_size(tx1d_width, tx1d_height)) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1008 | #if CONFIG_AOM_HIGHBITDEPTH |
Jingning Han | e325abd | 2016-12-01 09:35:10 -0800 | [diff] [blame] | 1009 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1010 | aom_highbd_subtract_block_c(tx1d_height, tx1d_width, src_diff, |
| 1011 | diff_stride, src, src_stride, dst, dst_stride, |
| 1012 | xd->bd); |
| 1013 | } else { |
| 1014 | aom_subtract_block_c(tx1d_height, tx1d_width, src_diff, diff_stride, src, |
| 1015 | src_stride, dst, dst_stride); |
| 1016 | } |
| 1017 | #else |
| 1018 | aom_subtract_block_c(tx1d_height, tx1d_width, src_diff, diff_stride, src, |
| 1019 | src_stride, dst, dst_stride); |
| 1020 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1021 | } else { |
Jingning Han | e325abd | 2016-12-01 09:35:10 -0800 | [diff] [blame] | 1022 | #if CONFIG_AOM_HIGHBITDEPTH |
| 1023 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1024 | aom_highbd_subtract_block(tx1d_height, tx1d_width, src_diff, diff_stride, |
| 1025 | src, src_stride, dst, dst_stride, xd->bd); |
| 1026 | } else { |
| 1027 | aom_subtract_block(tx1d_height, tx1d_width, src_diff, diff_stride, src, |
| 1028 | src_stride, dst, dst_stride); |
| 1029 | } |
| 1030 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1031 | aom_subtract_block(tx1d_height, tx1d_width, src_diff, diff_stride, src, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1032 | src_stride, dst, dst_stride); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1033 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Jingning Han | e325abd | 2016-12-01 09:35:10 -0800 | [diff] [blame] | 1034 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1035 | |
| 1036 | a = &args->ta[blk_col]; |
| 1037 | l = &args->tl[blk_row]; |
Yushin Cho | 3827fdd | 2016-11-09 15:50:23 -0800 | [diff] [blame] | 1038 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1039 | ctx = combine_entropy_contexts(*a, *l); |
| 1040 | |
| 1041 | if (args->enable_optimize_b) { |
| 1042 | #if CONFIG_NEW_QUANT |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 1043 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 1044 | ctx, AV1_XFORM_QUANT_FP_NUQ); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1045 | #else // CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1046 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 1047 | ctx, AV1_XFORM_QUANT_FP); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1048 | #endif // CONFIG_NEW_QUANT |
| 1049 | if (p->eobs[block]) { |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1050 | *a = *l = av1_optimize_b(cm, x, plane, block, tx_size, ctx) > 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1051 | } else { |
| 1052 | *a = *l = 0; |
| 1053 | } |
| 1054 | } else { |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 1055 | #if CONFIG_NEW_QUANT |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1056 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 1057 | ctx, AV1_XFORM_QUANT_B_NUQ); |
| 1058 | #else // CONFIG_NEW_QUANT |
| 1059 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 1060 | ctx, AV1_XFORM_QUANT_B); |
| 1061 | #endif // CONFIG_NEW_QUANT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1062 | *a = *l = p->eobs[block] > 0; |
| 1063 | } |
| 1064 | |
| 1065 | if (*eob) { |
| 1066 | // inverse transform |
| 1067 | inv_txfm_param.tx_type = tx_type; |
| 1068 | inv_txfm_param.tx_size = tx_size; |
| 1069 | inv_txfm_param.eob = *eob; |
| 1070 | inv_txfm_param.lossless = xd->lossless[mbmi->segment_id]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1071 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1072 | inv_txfm_param.bd = xd->bd; |
| 1073 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1074 | highbd_inv_txfm_add(dqcoeff, dst, dst_stride, &inv_txfm_param); |
| 1075 | } else { |
| 1076 | inv_txfm_add(dqcoeff, dst, dst_stride, &inv_txfm_param); |
| 1077 | } |
| 1078 | #else |
| 1079 | inv_txfm_add(dqcoeff, dst, dst_stride, &inv_txfm_param); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1080 | #endif // CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1081 | |
| 1082 | *(args->skip) = 0; |
| 1083 | } |
Yaowu Xu | 859a527 | 2016-11-10 15:32:21 -0800 | [diff] [blame] | 1084 | #else // #if !CONFIG_PVQ |
Yushin Cho | 3827fdd | 2016-11-09 15:50:23 -0800 | [diff] [blame] | 1085 | |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 1086 | #if CONFIG_NEW_QUANT |
Yushin Cho | 3827fdd | 2016-11-09 15:50:23 -0800 | [diff] [blame] | 1087 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
Debargha Mukherjee | f030558 | 2016-11-24 09:55:34 -0800 | [diff] [blame] | 1088 | ctx, AV1_XFORM_QUANT_FP_NUQ); |
| 1089 | #else |
| 1090 | av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size, |
| 1091 | ctx, AV1_XFORM_QUANT_FP); |
| 1092 | #endif // CONFIG_NEW_QUANT |
Yushin Cho | 3827fdd | 2016-11-09 15:50:23 -0800 | [diff] [blame] | 1093 | |
| 1094 | *a = *l = !x->pvq_skip[plane]; |
| 1095 | |
| 1096 | // *(args->skip) == mbmi->skip |
| 1097 | if (!x->pvq_skip[plane]) *(args->skip) = 0; |
| 1098 | |
| 1099 | if (x->pvq_skip[plane]) return; |
| 1100 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1101 | // transform block size in pixels |
| 1102 | tx_blk_size = tx_size_wide[tx_size]; |
| 1103 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1104 | // Since av1 does not have separate function which does inverse transform |
| 1105 | // but av1_inv_txfm_add_*x*() also does addition of predicted image to |
| 1106 | // inverse transformed image, |
| 1107 | // pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros |
| 1108 | |
Yushin Cho | 3827fdd | 2016-11-09 15:50:23 -0800 | [diff] [blame] | 1109 | for (j = 0; j < tx_blk_size; j++) |
| 1110 | for (i = 0; i < tx_blk_size; i++) dst[j * dst_stride + i] = 0; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1111 | |
Yushin Cho | 3827fdd | 2016-11-09 15:50:23 -0800 | [diff] [blame] | 1112 | inv_txfm_param.tx_type = tx_type; |
| 1113 | inv_txfm_param.tx_size = tx_size; |
| 1114 | inv_txfm_param.eob = *eob; |
| 1115 | inv_txfm_param.lossless = xd->lossless[mbmi->segment_id]; |
| 1116 | #if CONFIG_AOM_HIGHBITDEPTH |
Yaowu Xu | 859a527 | 2016-11-10 15:32:21 -0800 | [diff] [blame] | 1117 | #error |
Yushin Cho | 3827fdd | 2016-11-09 15:50:23 -0800 | [diff] [blame] | 1118 | |
| 1119 | #else |
| 1120 | inv_txfm_add(dqcoeff, dst, dst_stride, &inv_txfm_param); |
| 1121 | #endif |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1122 | #endif // #if !CONFIG_PVQ |
| 1123 | |
| 1124 | #if !CONFIG_PVQ |
| 1125 | if (*eob) *(args->skip) = 0; |
| 1126 | #else |
| 1127 | // Note : *(args->skip) == mbmi->skip |
| 1128 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1131 | void av1_encode_intra_block_plane(AV1_COMMON *cm, MACROBLOCK *x, |
| 1132 | BLOCK_SIZE bsize, int plane, |
Jingning Han | 18c53c8 | 2017-02-17 14:49:57 -0800 | [diff] [blame^] | 1133 | int enable_optimize_b, const int mi_row, |
| 1134 | const int mi_col) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1135 | const MACROBLOCKD *const xd = &x->e_mbd; |
Guillaume Martres | e50d9dd | 2016-12-18 18:26:47 +0100 | [diff] [blame] | 1136 | ENTROPY_CONTEXT ta[2 * MAX_MIB_SIZE] = { 0 }; |
| 1137 | ENTROPY_CONTEXT tl[2 * MAX_MIB_SIZE] = { 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1138 | |
Angie Chiang | ff6d890 | 2016-10-21 11:02:09 -0700 | [diff] [blame] | 1139 | struct encode_b_args arg = { |
| 1140 | cm, x, NULL, &xd->mi[0]->mbmi.skip, ta, tl, enable_optimize_b |
| 1141 | }; |
Jingning Han | 18c53c8 | 2017-02-17 14:49:57 -0800 | [diff] [blame^] | 1142 | |
| 1143 | #if CONFIG_CB4X4 |
| 1144 | if (bsize < BLOCK_8X8 && plane && !is_chroma_reference(mi_row, mi_col)) |
| 1145 | return; |
| 1146 | #else |
| 1147 | (void)mi_row; |
| 1148 | (void)mi_col; |
| 1149 | #endif |
| 1150 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1151 | if (enable_optimize_b) { |
| 1152 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1153 | const TX_SIZE tx_size = |
| 1154 | plane ? get_uv_tx_size(&xd->mi[0]->mbmi, pd) : xd->mi[0]->mbmi.tx_size; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1155 | av1_get_entropy_contexts(bsize, tx_size, pd, ta, tl); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1156 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1157 | av1_foreach_transformed_block_in_plane(xd, bsize, plane, |
| 1158 | av1_encode_block_intra, &arg); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1159 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1160 | |
| 1161 | #if CONFIG_PVQ |
ltrudeau | e1c0929 | 2017-01-20 15:42:13 -0500 | [diff] [blame] | 1162 | PVQ_SKIP_TYPE av1_pvq_encode_helper( |
| 1163 | daala_enc_ctx *daala_enc, tran_low_t *const coeff, tran_low_t *ref_coeff, |
| 1164 | tran_low_t *const dqcoeff, uint16_t *eob, const int16_t *quant, int plane, |
| 1165 | int tx_size, TX_TYPE tx_type, int *rate, int speed, PVQ_INFO *pvq_info) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1166 | const int tx_blk_size = tx_size_wide[tx_size]; |
ltrudeau | e1c0929 | 2017-01-20 15:42:13 -0500 | [diff] [blame] | 1167 | PVQ_SKIP_TYPE ac_dc_coded; |
Timothy B. Terriberry | e93acb2 | 2017-02-06 13:55:53 -0800 | [diff] [blame] | 1168 | /*TODO(tterribe): Handle CONFIG_AOM_HIGHBITDEPTH.*/ |
| 1169 | int coeff_shift = 3 - get_tx_scale(tx_size); |
| 1170 | int rounding_mask; |
Yushin Cho | 7066912 | 2016-12-08 09:53:14 -1000 | [diff] [blame] | 1171 | int pvq_dc_quant; |
| 1172 | int use_activity_masking = daala_enc->use_activity_masking; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1173 | int tell; |
| 1174 | int has_dc_skip = 1; |
| 1175 | int i; |
| 1176 | int off = od_qm_offset(tx_size, plane ? 1 : 0); |
Yushin Cho | 7066912 | 2016-12-08 09:53:14 -1000 | [diff] [blame] | 1177 | |
Thomas Daede | 1dbda1b | 2017-02-06 16:06:29 -0800 | [diff] [blame] | 1178 | DECLARE_ALIGNED(16, tran_low_t, coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]); |
| 1179 | DECLARE_ALIGNED(16, tran_low_t, ref_coeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]); |
| 1180 | DECLARE_ALIGNED(16, tran_low_t, dqcoeff_pvq[OD_TXSIZE_MAX * OD_TXSIZE_MAX]); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1181 | |
Yushin Cho | 48f84db | 2016-11-07 21:20:17 -0800 | [diff] [blame] | 1182 | DECLARE_ALIGNED(16, int32_t, in_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX]); |
| 1183 | DECLARE_ALIGNED(16, int32_t, ref_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX]); |
| 1184 | DECLARE_ALIGNED(16, int32_t, out_int32[OD_TXSIZE_MAX * OD_TXSIZE_MAX]); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1185 | |
Timothy B. Terriberry | e93acb2 | 2017-02-06 13:55:53 -0800 | [diff] [blame] | 1186 | assert(OD_COEFF_SHIFT >= 3); |
Yushin Cho | 7066912 | 2016-12-08 09:53:14 -1000 | [diff] [blame] | 1187 | // DC quantizer for PVQ |
| 1188 | if (use_activity_masking) |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 1189 | pvq_dc_quant = |
| 1190 | OD_MAXI(1, (quant[0] << (OD_COEFF_SHIFT - 3)) * |
| 1191 | daala_enc->state |
| 1192 | .pvq_qm_q4[plane][od_qm_get_index(tx_size, 0)] >> |
| 1193 | 4); |
Yushin Cho | 7066912 | 2016-12-08 09:53:14 -1000 | [diff] [blame] | 1194 | else |
Timothy B. Terriberry | e93acb2 | 2017-02-06 13:55:53 -0800 | [diff] [blame] | 1195 | pvq_dc_quant = OD_MAXI(1, quant[0] << (OD_COEFF_SHIFT - 3)); |
Yushin Cho | 7066912 | 2016-12-08 09:53:14 -1000 | [diff] [blame] | 1196 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1197 | *eob = 0; |
| 1198 | |
Nathan E. Egge | 6675be0 | 2016-12-21 13:02:43 -0500 | [diff] [blame] | 1199 | #if CONFIG_DAALA_EC |
| 1200 | tell = od_ec_enc_tell_frac(&daala_enc->w.ec); |
| 1201 | #else |
| 1202 | #error "CONFIG_PVQ currently requires CONFIG_DAALA_EC." |
| 1203 | #endif |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1204 | |
| 1205 | // Change coefficient ordering for pvq encoding. |
| 1206 | od_raster_to_coding_order(coeff_pvq, tx_blk_size, tx_type, coeff, |
| 1207 | tx_blk_size); |
| 1208 | od_raster_to_coding_order(ref_coeff_pvq, tx_blk_size, tx_type, ref_coeff, |
| 1209 | tx_blk_size); |
| 1210 | |
| 1211 | // copy int16 inputs to int32 |
| 1212 | for (i = 0; i < tx_blk_size * tx_blk_size; i++) { |
Timothy B. Terriberry | e93acb2 | 2017-02-06 13:55:53 -0800 | [diff] [blame] | 1213 | ref_int32[i] = ref_coeff_pvq[i] << (OD_COEFF_SHIFT - coeff_shift); |
| 1214 | in_int32[i] = coeff_pvq[i] << (OD_COEFF_SHIFT - coeff_shift); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1217 | if (abs(in_int32[0] - ref_int32[0]) < pvq_dc_quant * 141 / 256) { /* 0.55 */ |
| 1218 | out_int32[0] = 0; |
| 1219 | } else { |
| 1220 | out_int32[0] = OD_DIV_R0(in_int32[0] - ref_int32[0], pvq_dc_quant); |
| 1221 | } |
| 1222 | |
ltrudeau | 472f63f | 2017-01-12 15:22:32 -0500 | [diff] [blame] | 1223 | ac_dc_coded = od_pvq_encode( |
| 1224 | daala_enc, ref_int32, in_int32, out_int32, |
Timothy B. Terriberry | e93acb2 | 2017-02-06 13:55:53 -0800 | [diff] [blame] | 1225 | quant[0] << (OD_COEFF_SHIFT - 3), // scale/quantizer |
| 1226 | quant[1] << (OD_COEFF_SHIFT - 3), // scale/quantizer |
ltrudeau | 472f63f | 2017-01-12 15:22:32 -0500 | [diff] [blame] | 1227 | plane, tx_size, OD_PVQ_BETA[use_activity_masking][plane][tx_size], |
| 1228 | OD_ROBUST_STREAM, |
| 1229 | 0, // is_keyframe, |
| 1230 | 0, 0, 0, // q_scaling, bx, by, |
| 1231 | daala_enc->state.qm + off, daala_enc->state.qm_inv + off, |
| 1232 | speed, // speed |
| 1233 | pvq_info); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1234 | |
| 1235 | // Encode residue of DC coeff, if required. |
| 1236 | if (!has_dc_skip || out_int32[0]) { |
Nathan E. Egge | 760c27f | 2016-12-22 12:30:00 -0500 | [diff] [blame] | 1237 | generic_encode(&daala_enc->w, &daala_enc->state.adapt.model_dc[plane], |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1238 | abs(out_int32[0]) - has_dc_skip, -1, |
| 1239 | &daala_enc->state.adapt.ex_dc[plane][tx_size][0], 2); |
| 1240 | } |
| 1241 | if (out_int32[0]) { |
Nathan E. Egge | 6675be0 | 2016-12-21 13:02:43 -0500 | [diff] [blame] | 1242 | aom_write_bit(&daala_enc->w, out_int32[0] < 0); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | // need to save quantized residue of DC coeff |
| 1246 | // so that final pvq bitstream writing can know whether DC is coded. |
| 1247 | if (pvq_info) pvq_info->dq_dc_residue = out_int32[0]; |
| 1248 | |
| 1249 | out_int32[0] = out_int32[0] * pvq_dc_quant; |
| 1250 | out_int32[0] += ref_int32[0]; |
| 1251 | |
| 1252 | // copy int32 result back to int16 |
Timothy B. Terriberry | e93acb2 | 2017-02-06 13:55:53 -0800 | [diff] [blame] | 1253 | assert(OD_COEFF_SHIFT > coeff_shift); |
| 1254 | rounding_mask = (1 << (OD_COEFF_SHIFT - coeff_shift - 1)) - 1; |
| 1255 | for (i = 0; i < tx_blk_size * tx_blk_size; i++) { |
| 1256 | dqcoeff_pvq[i] = (out_int32[i] + (out_int32[i] < 0) + rounding_mask) >> |
| 1257 | (OD_COEFF_SHIFT - coeff_shift); |
| 1258 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1259 | |
| 1260 | // Back to original coefficient order |
| 1261 | od_coding_order_to_raster(dqcoeff, tx_blk_size, tx_type, dqcoeff_pvq, |
| 1262 | tx_blk_size); |
| 1263 | |
| 1264 | *eob = tx_blk_size * tx_blk_size; |
| 1265 | |
Nathan E. Egge | 6675be0 | 2016-12-21 13:02:43 -0500 | [diff] [blame] | 1266 | #if CONFIG_DAALA_EC |
| 1267 | *rate = (od_ec_enc_tell_frac(&daala_enc->w.ec) - tell) |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1268 | << (AV1_PROB_COST_SHIFT - OD_BITRES); |
Nathan E. Egge | 6675be0 | 2016-12-21 13:02:43 -0500 | [diff] [blame] | 1269 | #else |
| 1270 | #error "CONFIG_PVQ currently requires CONFIG_DAALA_EC." |
| 1271 | #endif |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1272 | assert(*rate >= 0); |
Yushin Cho | 5c20729 | 2017-02-16 15:01:33 -0800 | [diff] [blame] | 1273 | |
ltrudeau | 472f63f | 2017-01-12 15:22:32 -0500 | [diff] [blame] | 1274 | return ac_dc_coded; |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | void av1_store_pvq_enc_info(PVQ_INFO *pvq_info, int *qg, int *theta, |
| 1278 | int *max_theta, int *k, od_coeff *y, int nb_bands, |
| 1279 | const int *off, int *size, int skip_rest, |
| 1280 | int skip_dir, |
| 1281 | int bs) { // block size in log_2 -2 |
| 1282 | int i; |
| 1283 | const int tx_blk_size = tx_size_wide[bs]; |
| 1284 | |
| 1285 | for (i = 0; i < nb_bands; i++) { |
| 1286 | pvq_info->qg[i] = qg[i]; |
| 1287 | pvq_info->theta[i] = theta[i]; |
| 1288 | pvq_info->max_theta[i] = max_theta[i]; |
| 1289 | pvq_info->k[i] = k[i]; |
| 1290 | pvq_info->off[i] = off[i]; |
| 1291 | pvq_info->size[i] = size[i]; |
| 1292 | } |
| 1293 | |
| 1294 | memcpy(pvq_info->y, y, tx_blk_size * tx_blk_size * sizeof(od_coeff)); |
| 1295 | |
| 1296 | pvq_info->nb_bands = nb_bands; |
| 1297 | pvq_info->skip_rest = skip_rest; |
| 1298 | pvq_info->skip_dir = skip_dir; |
| 1299 | pvq_info->bs = bs; |
| 1300 | } |
| 1301 | #endif |