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