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