Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Alliance for Open Media. All rights reserved |
| 3 | * |
| 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. |
| 10 | */ |
| 11 | |
Linfeng Zhang | ae7b2f3 | 2017-11-08 15:46:57 -0800 | [diff] [blame] | 12 | #include "aom_ports/mem.h" |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 13 | #include "av1/common/scan.h" |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 14 | #include "av1/common/idct.h" |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 15 | #include "av1/common/txb_common.h" |
Angie Chiang | de58e49 | 2017-04-13 17:10:14 -0700 | [diff] [blame] | 16 | #include "av1/decoder/decodemv.h" |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 17 | #include "av1/decoder/decodetxb.h" |
Angie Chiang | 85e3b96 | 2017-10-01 16:04:43 -0700 | [diff] [blame] | 18 | #include "av1/decoder/symbolrate.h" |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 19 | |
| 20 | #define ACCT_STR __func__ |
| 21 | |
Angie Chiang | 85e3b96 | 2017-10-01 16:04:43 -0700 | [diff] [blame] | 22 | static int read_golomb(MACROBLOCKD *xd, aom_reader *r, FRAME_COUNTS *counts) { |
| 23 | #if !CONFIG_SYMBOLRATE |
| 24 | (void)counts; |
| 25 | #endif |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 26 | int x = 1; |
| 27 | int length = 0; |
| 28 | int i = 0; |
| 29 | |
| 30 | while (!i) { |
Angie Chiang | 85e3b96 | 2017-10-01 16:04:43 -0700 | [diff] [blame] | 31 | i = av1_read_record_bit(counts, r, ACCT_STR); |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 32 | ++length; |
Angie Chiang | f1880e1 | 2017-04-12 15:40:44 -0700 | [diff] [blame] | 33 | if (length >= 32) { |
| 34 | aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME, |
| 35 | "Invalid length in read_golomb"); |
| 36 | break; |
| 37 | } |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | for (i = 0; i < length - 1; ++i) { |
| 41 | x <<= 1; |
Angie Chiang | 85e3b96 | 2017-10-01 16:04:43 -0700 | [diff] [blame] | 42 | x += av1_read_record_bit(counts, r, ACCT_STR); |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | return x - 1; |
| 46 | } |
| 47 | |
Linfeng Zhang | 0c72b2f | 2017-12-04 10:59:28 -0800 | [diff] [blame] | 48 | static INLINE int rec_eob_pos(const int eob_token, const int extra) { |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 49 | int eob = k_eob_group_start[eob_token]; |
| 50 | if (eob > 2) { |
| 51 | eob += extra; |
| 52 | } |
| 53 | return eob; |
| 54 | } |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 55 | |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 56 | uint8_t av1_read_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *const xd, |
| 57 | aom_reader *const r, const int blk_row, |
Luc Trudeau | 2eb9b84 | 2017-12-13 11:19:16 -0500 | [diff] [blame] | 58 | const int blk_col, const int plane, |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 59 | const TXB_CTX *const txb_ctx, const TX_SIZE tx_size, |
| 60 | int16_t *const max_scan_line, int *const eob) { |
| 61 | FRAME_CONTEXT *const ec_ctx = xd->tile_ctx; |
Jingning Han | 5880758 | 2017-11-16 22:24:15 -0800 | [diff] [blame] | 62 | #if TXCOEFF_TIMER |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 63 | FRAME_COUNTS *const counts = NULL; |
Jingning Han | 5880758 | 2017-11-16 22:24:15 -0800 | [diff] [blame] | 64 | #else |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 65 | FRAME_COUNTS *const counts = xd->counts; |
Jingning Han | 5880758 | 2017-11-16 22:24:15 -0800 | [diff] [blame] | 66 | #endif |
Debargha Mukherjee | b3eda2f | 2017-11-28 16:00:20 -0800 | [diff] [blame] | 67 | const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size); |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 68 | const PLANE_TYPE plane_type = get_plane_type(plane); |
| 69 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Urvang Joshi | 8089315 | 2017-10-27 11:51:14 -0700 | [diff] [blame] | 70 | const int seg_eob = av1_get_max_eob(tx_size); |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 71 | int c = 0; |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 72 | int num_updates = 0; |
Linfeng Zhang | 0ff69c6 | 2017-12-04 10:35:38 -0800 | [diff] [blame] | 73 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 74 | const int16_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id]; |
| 75 | tran_low_t *const tcoeffs = pd->dqcoeff; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 76 | #if !CONFIG_DAALA_TX |
Jingning Han | ff70545 | 2017-04-27 11:32:15 -0700 | [diff] [blame] | 77 | const int shift = av1_get_tx_scale(tx_size); |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 78 | #endif |
Angie Chiang | a9ba58e | 2017-12-01 19:22:43 -0800 | [diff] [blame] | 79 | const int bwl = get_txb_bwl(tx_size); |
| 80 | const int width = get_txb_wide(tx_size); |
| 81 | const int height = get_txb_high(tx_size); |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 82 | int cul_level = 0; |
Linfeng Zhang | 679d81e | 2017-10-31 15:27:42 -0700 | [diff] [blame] | 83 | uint8_t levels_buf[TX_PAD_2D]; |
| 84 | uint8_t *const levels = set_levels(levels_buf, width); |
Linfeng Zhang | ae7b2f3 | 2017-11-08 15:46:57 -0800 | [diff] [blame] | 85 | DECLARE_ALIGNED(16, uint8_t, level_counts[MAX_TX_SQUARE]); |
Linfeng Zhang | 1015a34 | 2017-10-24 16:20:41 -0700 | [diff] [blame] | 86 | int8_t signs[MAX_TX_SQUARE]; |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 87 | uint16_t update_pos[MAX_TX_SQUARE]; |
Linfeng Zhang | ce065ca | 2017-10-17 16:49:30 -0700 | [diff] [blame] | 88 | |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 89 | const int all_zero = av1_read_record_bin( |
Angie Chiang | 85e3b96 | 2017-10-01 16:04:43 -0700 | [diff] [blame] | 90 | counts, r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2, |
| 91 | ACCT_STR); |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 92 | // printf("txb_skip: %d %2d\n", txs_ctx, txb_ctx->txb_skip_ctx); |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 93 | if (xd->counts) |
Jingning Han | 48be0e1 | 2017-06-13 12:12:01 -0700 | [diff] [blame] | 94 | ++xd->counts->txb_skip[txs_ctx][txb_ctx->txb_skip_ctx][all_zero]; |
Angie Chiang | 29b0fad | 2017-03-20 16:18:45 -0700 | [diff] [blame] | 95 | *eob = 0; |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 96 | if (all_zero) { |
| 97 | *max_scan_line = 0; |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 98 | #if CONFIG_TXK_SEL |
Angie Chiang | bce07f1 | 2017-12-01 16:34:31 -0800 | [diff] [blame] | 99 | if (plane == 0) |
| 100 | mbmi->txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col] = DCT_DCT; |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 101 | #endif |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Linfeng Zhang | 679d81e | 2017-10-31 15:27:42 -0700 | [diff] [blame] | 105 | memset(levels_buf, 0, |
Linfeng Zhang | 1122d7d | 2017-10-31 15:30:28 -0700 | [diff] [blame] | 106 | sizeof(*levels_buf) * |
| 107 | ((width + TX_PAD_HOR) * (height + TX_PAD_VER) + TX_PAD_END)); |
Linfeng Zhang | ce065ca | 2017-10-17 16:49:30 -0700 | [diff] [blame] | 108 | |
Jingning Han | 7eab9ff | 2017-07-06 10:12:54 -0700 | [diff] [blame] | 109 | (void)blk_row; |
| 110 | (void)blk_col; |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 111 | #if CONFIG_TXK_SEL |
Luc Trudeau | 2eb9b84 | 2017-12-13 11:19:16 -0500 | [diff] [blame] | 112 | av1_read_tx_type(cm, xd, blk_row, blk_col, plane, get_min_tx_size(tx_size), |
| 113 | r); |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 114 | #endif |
Jingning Han | 19b5c8f | 2017-07-06 15:10:12 -0700 | [diff] [blame] | 115 | const TX_TYPE tx_type = |
Luc Trudeau | 2eb9b84 | 2017-12-13 11:19:16 -0500 | [diff] [blame] | 116 | av1_get_tx_type(plane_type, xd, blk_row, blk_col, tx_size); |
Angie Chiang | bd99b38 | 2017-06-20 15:11:16 -0700 | [diff] [blame] | 117 | const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi); |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 118 | const int16_t *const scan = scan_order->scan; |
Linfeng Zhang | 0c72b2f | 2017-12-04 10:59:28 -0800 | [diff] [blame] | 119 | int dummy; |
| 120 | const int max_eob_pt = get_eob_pos_token(seg_eob, &dummy); |
| 121 | int eob_extra = 0; |
| 122 | int eob_pt = 1; |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 123 | |
Linfeng Zhang | 0c72b2f | 2017-12-04 10:59:28 -0800 | [diff] [blame] | 124 | for (eob_pt = 1; eob_pt < max_eob_pt; eob_pt++) { |
| 125 | const int eob_pos_ctx = av1_get_eob_pos_ctx(tx_type, eob_pt); |
| 126 | const int is_equal = av1_read_record_bin( |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 127 | counts, r, ec_ctx->eob_flag_cdf[txs_ctx][plane_type][eob_pos_ctx], 2, |
| 128 | ACCT_STR); |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 129 | // printf("eob_flag_cdf: %d %d %2d\n", txs_ctx, plane_type, eob_pos_ctx); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 130 | // aom_read_symbol(r, |
| 131 | // ec_ctx->eob_flag_cdf[AOMMIN(txs_ctx,3)][plane_type][eob_pos_ctx], 2, |
| 132 | // ACCT_STR); |
| 133 | if (counts) ++counts->eob_flag[txs_ctx][plane_type][eob_pos_ctx][is_equal]; |
| 134 | |
| 135 | if (is_equal) { |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | } |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 139 | |
| 140 | // printf("Dec: "); |
| 141 | if (k_eob_offset_bits[eob_pt] > 0) { |
Angie Chiang | 7ab884e | 2017-10-18 15:57:12 -0700 | [diff] [blame] | 142 | int bit = av1_read_record_bin( |
| 143 | counts, r, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_pt], 2, |
| 144 | ACCT_STR); |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 145 | // printf("eob_extra_cdf: %d %d %2d\n", txs_ctx, plane_type, eob_pt); |
Angie Chiang | 0d04f57 | 2017-10-22 18:19:51 -0700 | [diff] [blame] | 146 | if (counts) ++counts->eob_extra[txs_ctx][plane_type][eob_pt][bit]; |
Angie Chiang | 7ab884e | 2017-10-18 15:57:12 -0700 | [diff] [blame] | 147 | if (bit) { |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 148 | eob_extra += (1 << (k_eob_offset_bits[eob_pt] - 1)); |
Angie Chiang | 7ab884e | 2017-10-18 15:57:12 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) { |
Angie Chiang | 7ab884e | 2017-10-18 15:57:12 -0700 | [diff] [blame] | 152 | bit = av1_read_record_bit(counts, r, ACCT_STR); |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 153 | // printf("eob_bit:\n"); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 154 | if (bit) { |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 155 | eob_extra += (1 << (k_eob_offset_bits[eob_pt] - 1 - i)); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 156 | } |
| 157 | // printf("%d ", bit); |
| 158 | } |
| 159 | } |
| 160 | *eob = rec_eob_pos(eob_pt, eob_extra); |
| 161 | // printf("=>[%d, %d], (%d, %d)\n", seg_eob, *eob, eob_pt, eob_extra); |
| 162 | |
| 163 | for (int i = 0; i < *eob; ++i) { |
| 164 | c = *eob - 1 - i; |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 165 | const int pos = scan[c]; |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 166 | #if CONFIG_LV_MAP_MULTI |
Linfeng Zhang | f6d730a | 2017-12-06 14:49:44 -0800 | [diff] [blame] | 167 | const int coeff_ctx = get_nz_map_ctx(levels, pos, bwl, height, c, |
| 168 | c == *eob - 1, tx_size, tx_type); |
Dake He | 3fe369c | 2017-11-16 17:56:44 -0800 | [diff] [blame] | 169 | #if USE_BASE_EOB_ALPHABET |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 170 | aom_cdf_prob *cdf; |
| 171 | int nsymbs; |
Dake He | 3fe369c | 2017-11-16 17:56:44 -0800 | [diff] [blame] | 172 | if (c == *eob - 1) { |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 173 | cdf = ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type] |
| 174 | [coeff_ctx - SIG_COEF_CONTEXTS + |
| 175 | SIG_COEF_CONTEXTS_EOB]; |
| 176 | nsymbs = 3; |
Dake He | 3fe369c | 2017-11-16 17:56:44 -0800 | [diff] [blame] | 177 | } else { |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 178 | cdf = ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx]; |
| 179 | nsymbs = 4; |
Dake He | 3fe369c | 2017-11-16 17:56:44 -0800 | [diff] [blame] | 180 | } |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 181 | const int level = av1_read_record_symbol(counts, r, cdf, nsymbs, ACCT_STR) + |
| 182 | (c == *eob - 1); |
Dake He | 5988177 | 2017-11-24 07:00:02 -0800 | [diff] [blame] | 183 | if (counts) { |
| 184 | if (c < *eob - 1) { |
| 185 | ++counts->nz_map[txs_ctx][plane_type][coeff_ctx][level != 0]; |
| 186 | } |
| 187 | if (level != 0) { |
| 188 | for (int k = 0; k < NUM_BASE_LEVELS; ++k) { |
| 189 | ++counts |
| 190 | ->coeff_base[txs_ctx][plane_type][k][coeff_ctx][level > k + 1]; |
| 191 | if (level == k + 1) break; |
| 192 | } |
| 193 | } |
| 194 | } |
Dake He | 3fe369c | 2017-11-16 17:56:44 -0800 | [diff] [blame] | 195 | #else |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 196 | const int level = av1_read_record_symbol( |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 197 | counts, r, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx], 4, |
| 198 | ACCT_STR); |
Dake He | 3fe369c | 2017-11-16 17:56:44 -0800 | [diff] [blame] | 199 | #endif |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 200 | // printf("base_cdf: %d %d %2d\n", txs_ctx, plane_type, coeff_ctx); |
| 201 | // printf("base_cdf: %d %d %2d : %3d %3d %3d\n", txs_ctx, plane_type, |
| 202 | // coeff_ctx, |
| 203 | // ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx][0]>>7, |
| 204 | // ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx][1]>>7, |
| 205 | // ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx][2]>>7); |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 206 | if (level) { |
Linfeng Zhang | d564737 | 2017-12-05 17:06:07 -0800 | [diff] [blame] | 207 | levels[get_padded_idx(pos, bwl)] = level; |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 208 | *max_scan_line = AOMMAX(*max_scan_line, pos); |
| 209 | if (level < 3) { |
| 210 | cul_level += level; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 211 | #if CONFIG_DAALA_TX |
| 212 | tcoeffs[pos] = level * dequant[!!c]; |
| 213 | #else |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 214 | tcoeffs[pos] = (level * dequant[!!c]) >> shift; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 215 | #endif |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 216 | } else { |
| 217 | update_pos[num_updates++] = pos; |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 218 | } |
Jingning Han | 8682cd9 | 2017-11-16 20:08:31 -0800 | [diff] [blame] | 219 | } |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 220 | #else |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 221 | int is_nz; |
Linfeng Zhang | f6d730a | 2017-12-06 14:49:44 -0800 | [diff] [blame] | 222 | const int coeff_ctx = get_nz_map_ctx(levels, pos, bwl, tx_size, tx_type); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 223 | |
| 224 | if (c < *eob - 1) { |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 225 | is_nz = av1_read_record_bin( |
| 226 | counts, r, ec_ctx->nz_map_cdf[txs_ctx][plane_type][coeff_ctx], 2, |
| 227 | ACCT_STR); |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 228 | } else { |
| 229 | is_nz = 1; |
| 230 | } |
| 231 | |
Dake He | 03a3292 | 2017-10-31 08:06:45 -0700 | [diff] [blame] | 232 | if (is_nz) { |
| 233 | int k; |
| 234 | for (k = 0; k < NUM_BASE_LEVELS; ++k) { |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 235 | const int ctx = coeff_ctx; |
| 236 | const int is_k = av1_read_record_bin( |
Dake He | 03a3292 | 2017-10-31 08:06:45 -0700 | [diff] [blame] | 237 | counts, r, ec_ctx->coeff_base_cdf[txs_ctx][plane_type][k][ctx], 2, |
| 238 | ACCT_STR); |
| 239 | if (counts) ++counts->coeff_base[txs_ctx][plane_type][k][ctx][is_k]; |
| 240 | |
Dake He | bd47bfa | 2017-11-02 13:44:47 -0700 | [diff] [blame] | 241 | // semantic: is_k = 1 if level > (k+1) |
Dake He | 03a3292 | 2017-10-31 08:06:45 -0700 | [diff] [blame] | 242 | if (is_k == 0) { |
| 243 | cul_level += k + 1; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 244 | #if CONFIG_DAALA_TX |
| 245 | tcoeffs[pos] = (k + 1) * dequant[!!c]; |
| 246 | #else |
Jingning Han | cf3a082 | 2017-11-16 22:19:39 -0800 | [diff] [blame] | 247 | tcoeffs[pos] = ((k + 1) * dequant[!!c]) >> shift; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 248 | #endif |
Dake He | 03a3292 | 2017-10-31 08:06:45 -0700 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | } |
Linfeng Zhang | d564737 | 2017-12-05 17:06:07 -0800 | [diff] [blame] | 252 | levels[get_padded_idx(pos, bwl)] = k + 1; |
Jingning Han | cf3a082 | 2017-11-16 22:19:39 -0800 | [diff] [blame] | 253 | *max_scan_line = AOMMAX(*max_scan_line, pos); |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 254 | if (k == NUM_BASE_LEVELS) { |
| 255 | update_pos[num_updates++] = pos; |
| 256 | } |
Dake He | 03a3292 | 2017-10-31 08:06:45 -0700 | [diff] [blame] | 257 | } |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 258 | #endif |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 259 | } |
Dake He | a47cd6c | 2017-10-13 18:09:58 -0700 | [diff] [blame] | 260 | |
Dake He | 43edb76 | 2017-10-26 10:29:46 -0700 | [diff] [blame] | 261 | // Loop to decode all signs in the transform block, |
| 262 | // starting with the sign of the DC (if applicable) |
| 263 | for (c = 0; c < *eob; ++c) { |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 264 | const int pos = scan[c]; |
Jingning Han | cf3a082 | 2017-11-16 22:19:39 -0800 | [diff] [blame] | 265 | int8_t *const sign = &signs[pos]; |
Linfeng Zhang | d564737 | 2017-12-05 17:06:07 -0800 | [diff] [blame] | 266 | if (levels[get_padded_idx(pos, bwl)] == 0) continue; |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 267 | if (c == 0) { |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 268 | const int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
Dake He | 43edb76 | 2017-10-26 10:29:46 -0700 | [diff] [blame] | 269 | #if LV_MAP_PROB |
Linfeng Zhang | ce065ca | 2017-10-17 16:49:30 -0700 | [diff] [blame] | 270 | *sign = av1_read_record_bin( |
Angie Chiang | 85e3b96 | 2017-10-01 16:04:43 -0700 | [diff] [blame] | 271 | counts, r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], 2, ACCT_STR); |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 272 | // printf("dc_sign: %d %d\n", plane_type, dc_sign_ctx); |
Dake He | 43edb76 | 2017-10-26 10:29:46 -0700 | [diff] [blame] | 273 | #else |
| 274 | *sign = aom_read(r, ec_ctx->dc_sign[plane_type][dc_sign_ctx], ACCT_STR); |
| 275 | #endif |
Linfeng Zhang | ce065ca | 2017-10-17 16:49:30 -0700 | [diff] [blame] | 276 | if (counts) ++counts->dc_sign[plane_type][dc_sign_ctx][*sign]; |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 277 | } else { |
Linfeng Zhang | ce065ca | 2017-10-17 16:49:30 -0700 | [diff] [blame] | 278 | *sign = av1_read_record_bit(counts, r, ACCT_STR); |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 279 | } |
Jingning Han | ad2c178 | 2017-11-18 14:30:46 -0800 | [diff] [blame] | 280 | if (*sign) tcoeffs[pos] = -tcoeffs[pos]; |
Dake He | 43edb76 | 2017-10-26 10:29:46 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 283 | if (num_updates) { |
Dake He | 7d01ab5 | 2017-11-24 17:53:28 -0800 | [diff] [blame] | 284 | #if !CONFIG_LV_MAP_MULTI |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 285 | av1_get_br_level_counts(levels, width, height, level_counts); |
Dake He | 7d01ab5 | 2017-11-24 17:53:28 -0800 | [diff] [blame] | 286 | #endif |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 287 | for (c = 0; c < num_updates; ++c) { |
| 288 | const int pos = update_pos[c]; |
Linfeng Zhang | d564737 | 2017-12-05 17:06:07 -0800 | [diff] [blame] | 289 | uint8_t *const level = &levels[get_padded_idx(pos, bwl)]; |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 290 | int idx; |
| 291 | int ctx; |
Dake He | 43edb76 | 2017-10-26 10:29:46 -0700 | [diff] [blame] | 292 | |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 293 | assert(*level > NUM_BASE_LEVELS); |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 294 | |
Dake He | 7d01ab5 | 2017-11-24 17:53:28 -0800 | [diff] [blame] | 295 | #if !CONFIG_LV_MAP_MULTI |
Jingning Han | cf3a082 | 2017-11-16 22:19:39 -0800 | [diff] [blame] | 296 | ctx = get_br_ctx(levels, pos, bwl, level_counts[pos]); |
Dake He | 7d01ab5 | 2017-11-24 17:53:28 -0800 | [diff] [blame] | 297 | #endif |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 298 | |
Ola Hugosson | e72a209 | 2017-11-12 09:11:53 +0100 | [diff] [blame] | 299 | #if CONFIG_LV_MAP_MULTI |
Dake He | 7d01ab5 | 2017-11-24 17:53:28 -0800 | [diff] [blame] | 300 | #if USE_CAUSAL_BR_CTX |
| 301 | ctx = get_br_ctx(levels, pos, bwl, level_counts[pos], tx_type); |
| 302 | #else |
| 303 | ctx = get_br_ctx(levels, pos, bwl, level_counts[pos]); |
| 304 | #endif |
Ola Hugosson | e72a209 | 2017-11-12 09:11:53 +0100 | [diff] [blame] | 305 | for (idx = 0; idx < COEFF_BASE_RANGE / (BR_CDF_SIZE - 1); ++idx) { |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 306 | int k = av1_read_record_symbol( |
Dake He | 7d01ab5 | 2017-11-24 17:53:28 -0800 | [diff] [blame] | 307 | counts, r, |
| 308 | ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_16X16)][plane_type][ctx], |
Ola Hugosson | e72a209 | 2017-11-12 09:11:53 +0100 | [diff] [blame] | 309 | BR_CDF_SIZE, ACCT_STR); |
| 310 | *level += k; |
Dake He | 5988177 | 2017-11-24 07:00:02 -0800 | [diff] [blame] | 311 | if (counts) { |
Dake He | 7d01ab5 | 2017-11-24 17:53:28 -0800 | [diff] [blame] | 312 | for (int lps = 0; lps < BR_CDF_SIZE - 1; lps++) { |
| 313 | ++counts->coeff_lps[AOMMIN(txs_ctx, TX_16X16)][plane_type][lps][ctx] |
| 314 | [lps == k]; |
Dake He | 5988177 | 2017-11-24 07:00:02 -0800 | [diff] [blame] | 315 | if (lps == k) break; |
| 316 | } |
| 317 | } |
Ola Hugosson | e72a209 | 2017-11-12 09:11:53 +0100 | [diff] [blame] | 318 | if (k < BR_CDF_SIZE - 1) break; |
| 319 | } |
| 320 | if (*level <= NUM_BASE_LEVELS + COEFF_BASE_RANGE) { |
| 321 | cul_level += *level; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 322 | #if CONFIG_DAALA_TX |
| 323 | tran_low_t t = *level * dequant[!!pos]; |
| 324 | #else |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 325 | tran_low_t t = (*level * dequant[!!pos]) >> shift; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 326 | #endif |
Jingning Han | cf3a082 | 2017-11-16 22:19:39 -0800 | [diff] [blame] | 327 | if (signs[pos]) t = -t; |
| 328 | tcoeffs[pos] = t; |
Ola Hugosson | e72a209 | 2017-11-12 09:11:53 +0100 | [diff] [blame] | 329 | continue; |
| 330 | } |
| 331 | #else |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 332 | for (idx = 0; idx < BASE_RANGE_SETS; ++idx) { |
Ola Hugosson | 1389210 | 2017-11-06 08:01:44 +0100 | [diff] [blame] | 333 | // printf("br: %d %d %d %d\n", txs_ctx, plane_type, idx, ctx); |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 334 | if (av1_read_record_bin( |
| 335 | counts, r, ec_ctx->coeff_br_cdf[txs_ctx][plane_type][idx][ctx], |
| 336 | 2, ACCT_STR)) { |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 337 | const int extra_bits = (1 << br_extra_bits[idx]) - 1; |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 338 | // int br_offset = aom_read_literal(r, extra_bits, ACCT_STR); |
| 339 | int br_offset = 0; |
| 340 | int tok; |
| 341 | if (counts) ++counts->coeff_br[txs_ctx][plane_type][idx][ctx][1]; |
| 342 | for (tok = 0; tok < extra_bits; ++tok) { |
| 343 | if (av1_read_record_bin( |
| 344 | counts, r, ec_ctx->coeff_lps_cdf[txs_ctx][plane_type][ctx], |
| 345 | 2, ACCT_STR)) { |
| 346 | br_offset = tok; |
| 347 | if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][1]; |
| 348 | break; |
| 349 | } |
| 350 | if (counts) ++counts->coeff_lps[txs_ctx][plane_type][ctx][0]; |
Jingning Han | 87b01b5 | 2017-08-31 12:07:20 -0700 | [diff] [blame] | 351 | } |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 352 | if (tok == extra_bits) br_offset = extra_bits; |
| 353 | |
Linfeng Zhang | 9a7cc0e | 2017-11-20 12:37:28 -0800 | [diff] [blame] | 354 | const int br_base = br_index_to_coeff[idx]; |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 355 | |
| 356 | *level = NUM_BASE_LEVELS + 1 + br_base + br_offset; |
| 357 | cul_level += *level; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 358 | #if CONFIG_DAALA_TX |
| 359 | tran_low_t t = *level * dequant[!!pos]; |
| 360 | #else |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 361 | tran_low_t t = (*level * dequant[!!pos]) >> shift; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 362 | #endif |
Jingning Han | cf3a082 | 2017-11-16 22:19:39 -0800 | [diff] [blame] | 363 | if (signs[pos]) t = -t; |
| 364 | tcoeffs[pos] = t; |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 365 | break; |
Jingning Han | 87b01b5 | 2017-08-31 12:07:20 -0700 | [diff] [blame] | 366 | } |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 367 | if (counts) ++counts->coeff_br[txs_ctx][plane_type][idx][ctx][0]; |
Jingning Han | 87b01b5 | 2017-08-31 12:07:20 -0700 | [diff] [blame] | 368 | } |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 369 | |
| 370 | if (idx < BASE_RANGE_SETS) continue; |
Ola Hugosson | e72a209 | 2017-11-12 09:11:53 +0100 | [diff] [blame] | 371 | #endif |
Linfeng Zhang | 97fc474 | 2017-11-07 12:57:25 -0800 | [diff] [blame] | 372 | // decode 0-th order Golomb code |
| 373 | *level = COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS; |
| 374 | // Save golomb in tcoeffs because adding it to level may incur overflow |
Jingning Han | cf3a082 | 2017-11-16 22:19:39 -0800 | [diff] [blame] | 375 | tran_low_t t = *level + read_golomb(xd, r, counts); |
| 376 | cul_level += t; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 377 | #if CONFIG_DAALA_TX |
| 378 | t = t * dequant[!!pos]; |
| 379 | #else |
Linfeng Zhang | 530bee2 | 2017-11-28 16:42:27 -0800 | [diff] [blame] | 380 | t = (t * dequant[!!pos]) >> shift; |
Monty | bca9e9e | 2017-12-14 06:23:29 -0600 | [diff] [blame] | 381 | #endif |
Jingning Han | cf3a082 | 2017-11-16 22:19:39 -0800 | [diff] [blame] | 382 | if (signs[pos]) t = -t; |
| 383 | tcoeffs[pos] = t; |
Jingning Han | 87b01b5 | 2017-08-31 12:07:20 -0700 | [diff] [blame] | 384 | } |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Angie Chiang | cea11f2 | 2017-02-24 12:30:40 -0800 | [diff] [blame] | 387 | cul_level = AOMMIN(63, cul_level); |
| 388 | |
| 389 | // DC value |
| 390 | set_dc_sign(&cul_level, tcoeffs[0]); |
| 391 | |
| 392 | return cul_level; |
| 393 | } |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 394 | |
Luc Trudeau | 2eb9b84 | 2017-12-13 11:19:16 -0500 | [diff] [blame] | 395 | uint8_t av1_read_coeffs_txb_facade(const AV1_COMMON *const cm, |
| 396 | MACROBLOCKD *const xd, aom_reader *const r, |
| 397 | const int row, const int col, |
| 398 | const int plane, const TX_SIZE tx_size, |
| 399 | int16_t *const max_scan_line, |
| 400 | int *const eob) { |
Linfeng Zhang | 0ff69c6 | 2017-12-04 10:35:38 -0800 | [diff] [blame] | 401 | MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 402 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 403 | |
| 404 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 405 | const BLOCK_SIZE plane_bsize = |
| 406 | AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd)); |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 407 | |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 408 | TXB_CTX txb_ctx; |
| 409 | get_txb_ctx(plane_bsize, tx_size, plane, pd->above_context + col, |
| 410 | pd->left_context + row, &txb_ctx); |
Luc Trudeau | 2eb9b84 | 2017-12-13 11:19:16 -0500 | [diff] [blame] | 411 | uint8_t cul_level = av1_read_coeffs_txb(cm, xd, r, row, col, plane, &txb_ctx, |
| 412 | tx_size, max_scan_line, eob); |
Angie Chiang | 5c0568a | 2017-03-21 16:00:39 -0700 | [diff] [blame] | 413 | #if CONFIG_ADAPT_SCAN |
| 414 | PLANE_TYPE plane_type = get_plane_type(plane); |
Luc Trudeau | 2eb9b84 | 2017-12-13 11:19:16 -0500 | [diff] [blame] | 415 | TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, row, col, tx_size); |
Jingning Han | 025c6c4 | 2017-11-22 12:06:03 -0800 | [diff] [blame] | 416 | const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2); |
| 417 | |
Angie Chiang | 5c0568a | 2017-03-21 16:00:39 -0700 | [diff] [blame] | 418 | if (xd->counts && *eob > 0) |
Jingning Han | 0bd3bf6 | 2017-11-28 17:11:51 -0800 | [diff] [blame] | 419 | av1_update_scan_count_facade(cm, xd, mi_row, tx_size, tx_type, pd->dqcoeff, |
| 420 | *eob); |
Angie Chiang | 5c0568a | 2017-03-21 16:00:39 -0700 | [diff] [blame] | 421 | #endif |
Angie Chiang | 29b0fad | 2017-03-20 16:18:45 -0700 | [diff] [blame] | 422 | av1_set_contexts(xd, pd, plane, tx_size, cul_level, col, row); |
| 423 | return cul_level; |
Angie Chiang | 133733c | 2017-03-17 12:50:20 -0700 | [diff] [blame] | 424 | } |