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