Coverage Report

Created: 2022-08-24 06:11

/src/aom/av1/encoder/txb_rdopt_utils.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021, 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
#ifndef AOM_AV1_ENCODER_TXB_RDOPT_UTILS_H_
13
#define AOM_AV1_ENCODER_TXB_RDOPT_UTILS_H_
14
15
#include "av1/encoder/encodetxb.h"
16
17
static const int golomb_bits_cost[32] = {
18
  0,       512,     512 * 3, 512 * 3, 512 * 5, 512 * 5, 512 * 5, 512 * 5,
19
  512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7, 512 * 7,
20
  512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9,
21
  512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9, 512 * 9
22
};
23
24
static const int golomb_cost_diff[32] = {
25
  0,       512, 512 * 2, 0, 512 * 2, 0, 0, 0, 512 * 2, 0, 0, 0, 0, 0, 0, 0,
26
  512 * 2, 0,   0,       0, 0,       0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0
27
};
28
29
// Look up table of individual cost of coefficient by its quantization level.
30
// determined based on Laplacian distribution conditioned on estimated context
31
static const int costLUT[15] = { -1143, 53,   545,  825,  1031,
32
                                 1209,  1393, 1577, 1763, 1947,
33
                                 2132,  2317, 2501, 2686, 2871 };
34
35
static const int const_term = (1 << AV1_PROB_COST_SHIFT);
36
37
static const int loge_par = ((14427 << AV1_PROB_COST_SHIFT) + 5000) / 10000;
38
39
static INLINE int get_dqv(const int16_t *dequant, int coeff_idx,
40
0
                          const qm_val_t *iqmatrix) {
41
0
  int dqv = dequant[!!coeff_idx];
42
0
  if (iqmatrix != NULL)
43
0
    dqv =
44
0
        ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
45
0
  return dqv;
46
0
}
47
48
static INLINE int64_t get_coeff_dist(tran_low_t tcoeff, tran_low_t dqcoeff,
49
0
                                     int shift) {
50
0
  const int64_t diff = (tcoeff - dqcoeff) * (1 << shift);
51
0
  const int64_t error = diff * diff;
52
0
  return error;
53
0
}
54
55
static int get_eob_cost(int eob, const LV_MAP_EOB_COST *txb_eob_costs,
56
0
                        const LV_MAP_COEFF_COST *txb_costs, TX_CLASS tx_class) {
57
0
  int eob_extra;
58
0
  const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra);
59
0
  int eob_cost = 0;
60
0
  const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
61
0
  eob_cost = txb_eob_costs->eob_cost[eob_multi_ctx][eob_pt - 1];
62
63
0
  if (av1_eob_offset_bits[eob_pt] > 0) {
64
0
    const int eob_ctx = eob_pt - 3;
65
0
    const int eob_shift = av1_eob_offset_bits[eob_pt] - 1;
66
0
    const int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
67
0
    eob_cost += txb_costs->eob_extra_cost[eob_ctx][bit];
68
0
    const int offset_bits = av1_eob_offset_bits[eob_pt];
69
0
    if (offset_bits > 1) eob_cost += av1_cost_literal(offset_bits - 1);
70
0
  }
71
0
  return eob_cost;
72
0
}
73
74
0
static INLINE int get_golomb_cost(int abs_qc) {
75
0
  if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
76
0
    const int r = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
77
0
    const int length = get_msb(r) + 1;
78
0
    return av1_cost_literal(2 * length - 1);
79
0
  }
80
0
  return 0;
81
0
}
82
83
0
static INLINE int get_br_cost(tran_low_t level, const int *coeff_lps) {
84
0
  const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
85
0
  return coeff_lps[base_range] + get_golomb_cost(level);
86
0
}
87
88
static INLINE int get_br_cost_with_diff(tran_low_t level, const int *coeff_lps,
89
0
                                        int *diff) {
90
0
  const int base_range = AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
91
0
  int golomb_bits = 0;
92
0
  if (level <= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS)
93
0
    *diff += coeff_lps[base_range + COEFF_BASE_RANGE + 1];
94
95
0
  if (level >= COEFF_BASE_RANGE + 1 + NUM_BASE_LEVELS) {
96
0
    int r = level - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
97
0
    if (r < 32) {
98
0
      golomb_bits = golomb_bits_cost[r];
99
0
      *diff += golomb_cost_diff[r];
100
0
    } else {
101
0
      golomb_bits = get_golomb_cost(level);
102
0
      *diff += (r & (r - 1)) == 0 ? 1024 : 0;
103
0
    }
104
0
  }
105
106
0
  return coeff_lps[base_range] + golomb_bits;
107
0
}
108
109
static AOM_FORCE_INLINE int get_two_coeff_cost_simple(
110
    int ci, tran_low_t abs_qc, int coeff_ctx,
111
    const LV_MAP_COEFF_COST *txb_costs, int bwl, TX_CLASS tx_class,
112
0
    const uint8_t *levels, int *cost_low) {
113
  // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0)
114
  // and not the last (scan_idx != eob - 1)
115
0
  assert(ci > 0);
116
0
  int cost = txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
117
0
  int diff = 0;
118
0
  if (abs_qc <= 3) diff = txb_costs->base_cost[coeff_ctx][abs_qc + 4];
119
0
  if (abs_qc) {
120
0
    cost += av1_cost_literal(1);
121
0
    if (abs_qc > NUM_BASE_LEVELS) {
122
0
      const int br_ctx = get_br_ctx(levels, ci, bwl, tx_class);
123
0
      int brcost_diff = 0;
124
0
      cost += get_br_cost_with_diff(abs_qc, txb_costs->lps_cost[br_ctx],
125
0
                                    &brcost_diff);
126
0
      diff += brcost_diff;
127
0
    }
128
0
  }
129
0
  *cost_low = cost - diff;
130
131
0
  return cost;
132
0
}
133
134
static INLINE int get_coeff_cost_eob(int ci, tran_low_t abs_qc, int sign,
135
                                     int coeff_ctx, int dc_sign_ctx,
136
                                     const LV_MAP_COEFF_COST *txb_costs,
137
0
                                     int bwl, TX_CLASS tx_class) {
138
0
  int cost = 0;
139
0
  cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
140
0
  if (abs_qc != 0) {
141
0
    if (ci == 0) {
142
0
      cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign];
143
0
    } else {
144
0
      cost += av1_cost_literal(1);
145
0
    }
146
0
    if (abs_qc > NUM_BASE_LEVELS) {
147
0
      int br_ctx;
148
0
      br_ctx = get_br_ctx_eob(ci, bwl, tx_class);
149
0
      cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]);
150
0
    }
151
0
  }
152
0
  return cost;
153
0
}
154
155
static INLINE int get_coeff_cost_general(int is_last, int ci, tran_low_t abs_qc,
156
                                         int sign, int coeff_ctx,
157
                                         int dc_sign_ctx,
158
                                         const LV_MAP_COEFF_COST *txb_costs,
159
                                         int bwl, TX_CLASS tx_class,
160
0
                                         const uint8_t *levels) {
161
0
  int cost = 0;
162
0
  if (is_last) {
163
0
    cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
164
0
  } else {
165
0
    cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
166
0
  }
167
0
  if (abs_qc != 0) {
168
0
    if (ci == 0) {
169
0
      cost += txb_costs->dc_sign_cost[dc_sign_ctx][sign];
170
0
    } else {
171
0
      cost += av1_cost_literal(1);
172
0
    }
173
0
    if (abs_qc > NUM_BASE_LEVELS) {
174
0
      int br_ctx;
175
0
      if (is_last)
176
0
        br_ctx = get_br_ctx_eob(ci, bwl, tx_class);
177
0
      else
178
0
        br_ctx = get_br_ctx(levels, ci, bwl, tx_class);
179
0
      cost += get_br_cost(abs_qc, txb_costs->lps_cost[br_ctx]);
180
0
    }
181
0
  }
182
0
  return cost;
183
0
}
184
185
static INLINE void get_qc_dqc_low(tran_low_t abs_qc, int sign, int dqv,
186
                                  int shift, tran_low_t *qc_low,
187
0
                                  tran_low_t *dqc_low) {
188
0
  tran_low_t abs_qc_low = abs_qc - 1;
189
0
  *qc_low = (-sign ^ abs_qc_low) + sign;
190
0
  assert((sign ? -abs_qc_low : abs_qc_low) == *qc_low);
191
0
  tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
192
0
  *dqc_low = (-sign ^ abs_dqc_low) + sign;
193
0
  assert((sign ? -abs_dqc_low : abs_dqc_low) == *dqc_low);
194
0
}
195
196
static INLINE void update_coeff_eob_fast(int *eob, int shift,
197
                                         const int16_t *dequant_ptr,
198
                                         const int16_t *scan,
199
                                         const tran_low_t *coeff_ptr,
200
                                         tran_low_t *qcoeff_ptr,
201
0
                                         tran_low_t *dqcoeff_ptr) {
202
  // TODO(sarahparker) make this work for aomqm
203
0
  int eob_out = *eob;
204
0
  int zbin[2] = { dequant_ptr[0] + ROUND_POWER_OF_TWO(dequant_ptr[0] * 70, 7),
205
0
                  dequant_ptr[1] + ROUND_POWER_OF_TWO(dequant_ptr[1] * 70, 7) };
206
207
0
  for (int i = *eob - 1; i >= 0; i--) {
208
0
    const int rc = scan[i];
209
0
    const int qcoeff = qcoeff_ptr[rc];
210
0
    const int coeff = coeff_ptr[rc];
211
0
    const int coeff_sign = AOMSIGN(coeff);
212
0
    int64_t abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
213
214
0
    if (((abs_coeff << (1 + shift)) < zbin[rc != 0]) || (qcoeff == 0)) {
215
0
      eob_out--;
216
0
      qcoeff_ptr[rc] = 0;
217
0
      dqcoeff_ptr[rc] = 0;
218
0
    } else {
219
0
      break;
220
0
    }
221
0
  }
222
223
0
  *eob = eob_out;
224
0
}
225
#endif  // AOM_AV1_ENCODER_TXB_RDOPT_UTILS_H_