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 | |
| 12 | #include "av1/common/scan.h" |
| 13 | #include "av1/common/txb_common.h" |
| 14 | #include "av1/decoder/decodetxb.h" |
| 15 | |
| 16 | #define ACCT_STR __func__ |
| 17 | |
| 18 | static int read_golomb(aom_reader *r) { |
| 19 | int x = 1; |
| 20 | int length = 0; |
| 21 | int i = 0; |
| 22 | |
| 23 | while (!i) { |
| 24 | i = aom_read_bit(r, ACCT_STR); |
| 25 | ++length; |
| 26 | } |
| 27 | |
| 28 | for (i = 0; i < length - 1; ++i) { |
| 29 | x <<= 1; |
| 30 | x += aom_read_bit(r, ACCT_STR); |
| 31 | } |
| 32 | |
| 33 | return x - 1; |
| 34 | } |
| 35 | |
| 36 | uint8_t av1_read_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *xd, |
| 37 | aom_reader *r, int block, int plane, |
| 38 | tran_low_t *tcoeffs, TXB_CTX *txb_ctx) { |
| 39 | FRAME_COUNTS *counts = xd->counts; |
| 40 | TX_SIZE tx_size = get_tx_size(plane, xd, block); |
| 41 | PLANE_TYPE plane_type = get_plane_type(plane); |
| 42 | aom_prob *nz_map = cm->fc->nz_map[tx_size][plane_type]; |
| 43 | aom_prob *eob_flag = cm->fc->eob_flag[tx_size][plane_type]; |
| 44 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 45 | const TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size); |
| 46 | const SCAN_ORDER *const scan_order = |
| 47 | get_scan(cm, tx_size, tx_type, is_inter_block(mbmi)); |
| 48 | const int16_t *scan = scan_order->scan; |
| 49 | const int seg_eob = 16 << (tx_size << 1); |
| 50 | int c = 0; |
| 51 | int eob = 0, update_eob = -1; |
| 52 | const int16_t *const dequant = xd->plane[plane].seg_dequant[mbmi->segment_id]; |
| 53 | const int shift = (tx_size == TX_32X32); |
| 54 | const int bwl = b_width_log2_lookup[txsize_to_bsize[tx_size]] + 2; |
| 55 | int cul_level = 0; |
| 56 | unsigned int(*nz_map_count)[SIG_COEF_CONTEXTS][2]; |
| 57 | uint8_t txb_mask[32 * 32] = { 0 }; |
| 58 | |
| 59 | nz_map_count = (counts) ? &counts->nz_map[tx_size][plane_type] : NULL; |
| 60 | |
| 61 | memset(tcoeffs, 0, sizeof(*tcoeffs) * seg_eob); |
| 62 | |
| 63 | for (c = 0; c < seg_eob; ++c) { |
| 64 | int is_nz; |
| 65 | int coeff_ctx = get_nz_map_ctx(tcoeffs, txb_mask, scan[c], bwl); |
| 66 | int eob_ctx = get_eob_ctx(tcoeffs, scan[c], bwl); |
| 67 | |
| 68 | if (c < seg_eob - 1) |
| 69 | is_nz = aom_read(r, nz_map[coeff_ctx], tx_size); |
| 70 | else |
| 71 | is_nz = 1; |
| 72 | |
| 73 | // set non-zero coefficient map. |
| 74 | tcoeffs[scan[c]] = is_nz; |
| 75 | |
| 76 | if (c == seg_eob - 1) { |
| 77 | ++c; |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | if (counts) ++(*nz_map_count)[coeff_ctx][is_nz]; |
| 82 | |
| 83 | if (is_nz) { |
| 84 | int is_eob = aom_read(r, eob_flag[eob_ctx], tx_size); |
| 85 | if (counts) ++counts->eob_flag[tx_size][plane_type][eob_ctx][is_eob]; |
| 86 | if (is_eob) break; |
| 87 | } |
| 88 | txb_mask[scan[c]] = 1; |
| 89 | } |
| 90 | |
| 91 | eob = AOMMIN(seg_eob, c + 1); |
| 92 | |
| 93 | int i; |
| 94 | for (i = 0; i < NUM_BASE_LEVELS; ++i) { |
| 95 | aom_prob *coeff_base = cm->fc->coeff_base[tx_size][plane_type][i]; |
| 96 | |
| 97 | update_eob = 0; |
| 98 | for (c = eob - 1; c >= 0; --c) { |
| 99 | tran_low_t *v = &tcoeffs[scan[c]]; |
| 100 | int sign; |
| 101 | int ctx; |
| 102 | |
| 103 | if (*v <= i) continue; |
| 104 | |
| 105 | ctx = get_base_ctx(tcoeffs, scan[c], bwl, i + 1); |
| 106 | |
| 107 | if (aom_read(r, coeff_base[ctx], tx_size)) { |
| 108 | *v = i + 1; |
| 109 | cul_level += i + 1; |
| 110 | |
| 111 | if (counts) ++counts->coeff_base[tx_size][plane_type][i][ctx][1]; |
| 112 | |
| 113 | if (c == 0) { |
| 114 | int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
| 115 | sign = aom_read(r, cm->fc->dc_sign[plane_type][dc_sign_ctx], tx_size); |
| 116 | if (counts) ++counts->dc_sign[plane_type][dc_sign_ctx][sign]; |
| 117 | } else { |
| 118 | sign = aom_read_bit(r, ACCT_STR); |
| 119 | } |
| 120 | if (sign) *v = -(*v); |
| 121 | continue; |
| 122 | } |
| 123 | *v = i + 2; |
| 124 | if (counts) ++counts->coeff_base[tx_size][plane_type][i][ctx][0]; |
| 125 | |
| 126 | // update the eob flag for coefficients with magnitude above 1. |
| 127 | update_eob = AOMMAX(update_eob, c); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | for (c = update_eob; c >= 0; --c) { |
| 132 | tran_low_t *v = &tcoeffs[scan[c]]; |
| 133 | int sign; |
| 134 | int idx; |
| 135 | int ctx; |
| 136 | |
| 137 | if (*v <= NUM_BASE_LEVELS) continue; |
| 138 | |
| 139 | if (c == 0) { |
| 140 | int dc_sign_ctx = txb_ctx->dc_sign_ctx; |
| 141 | sign = aom_read(r, cm->fc->dc_sign[plane_type][dc_sign_ctx], tx_size); |
| 142 | if (counts) ++counts->dc_sign[plane_type][dc_sign_ctx][sign]; |
| 143 | } else { |
| 144 | sign = aom_read_bit(r, ACCT_STR); |
| 145 | } |
| 146 | |
| 147 | ctx = get_level_ctx(tcoeffs, scan[c], bwl); |
| 148 | |
| 149 | if (cm->fc->coeff_lps[tx_size][plane_type][ctx] == 0) exit(0); |
| 150 | |
| 151 | for (idx = 0; idx < COEFF_BASE_RANGE; ++idx) { |
| 152 | if (aom_read(r, cm->fc->coeff_lps[tx_size][plane_type][ctx], tx_size)) { |
| 153 | *v = (idx + 1 + NUM_BASE_LEVELS); |
| 154 | if (sign) *v = -(*v); |
| 155 | cul_level += abs(*v); |
| 156 | |
| 157 | if (counts) ++counts->coeff_lps[tx_size][plane_type][ctx][1]; |
| 158 | break; |
| 159 | } |
| 160 | if (counts) ++counts->coeff_lps[tx_size][plane_type][ctx][0]; |
| 161 | } |
| 162 | if (idx < COEFF_BASE_RANGE) continue; |
| 163 | |
| 164 | // decode 0-th order Golomb code |
| 165 | *v = read_golomb(r) + COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS; |
| 166 | if (sign) *v = -(*v); |
| 167 | cul_level += abs(*v); |
| 168 | } |
| 169 | |
| 170 | for (c = 0; c < eob; ++c) { |
| 171 | int16_t dqv = (c == 0) ? dequant[0] : dequant[1]; |
| 172 | tran_low_t *v = &tcoeffs[scan[c]]; |
| 173 | int sign = (*v) < 0; |
| 174 | *v = (abs(*v) * dqv) >> shift; |
| 175 | if (sign) *v = -(*v); |
| 176 | } |
| 177 | |
| 178 | cul_level = AOMMIN(63, cul_level); |
| 179 | |
| 180 | // DC value |
| 181 | set_dc_sign(&cul_level, tcoeffs[0]); |
| 182 | |
| 183 | return cul_level; |
| 184 | } |