Coverage Report

Created: 2026-06-10 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/aom/av1/encoder/txb_rdopt.c
Line
Count
Source
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
#include "av1/encoder/txb_rdopt.h"
13
#include "av1/encoder/txb_rdopt_utils.h"
14
15
#include "av1/common/idct.h"
16
17
static INLINE void update_coeff_general(
18
    int *accu_rate, int64_t *accu_dist, int si, int eob, TX_SIZE tx_size,
19
    TX_CLASS tx_class, int bwl, int height, int64_t rdmult, int shift,
20
    int dc_sign_ctx, const int16_t *dequant, const int16_t *scan,
21
    const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
22
    tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels,
23
10.6k
    const qm_val_t *iqmatrix) {
24
10.6k
  const int dqv = get_dqv(dequant, scan[si], iqmatrix);
25
10.6k
  const int ci = scan[si];
26
10.6k
  const tran_low_t qc = qcoeff[ci];
27
10.6k
  const int is_last = si == (eob - 1);
28
10.6k
  const int coeff_ctx = get_lower_levels_ctx_general(
29
10.6k
      is_last, si, bwl, height, levels, ci, tx_size, tx_class);
30
10.6k
  if (qc == 0) {
31
0
    *accu_rate += txb_costs->base_cost[coeff_ctx][0];
32
10.6k
  } else {
33
10.6k
    const int sign = (qc < 0) ? 1 : 0;
34
10.6k
    const tran_low_t abs_qc = abs(qc);
35
10.6k
    const tran_low_t tqc = tcoeff[ci];
36
10.6k
    const tran_low_t dqc = dqcoeff[ci];
37
10.6k
    const int64_t dist = get_coeff_dist(tqc, dqc, shift);
38
10.6k
    const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
39
10.6k
    const int rate =
40
10.6k
        get_coeff_cost_general(is_last, ci, abs_qc, sign, coeff_ctx,
41
10.6k
                               dc_sign_ctx, txb_costs, bwl, tx_class, levels);
42
10.6k
    const int64_t rd = RDCOST(rdmult, rate, dist);
43
44
10.6k
    tran_low_t qc_low, dqc_low;
45
10.6k
    tran_low_t abs_qc_low;
46
10.6k
    int64_t dist_low, rd_low;
47
10.6k
    int rate_low;
48
10.6k
    if (abs_qc == 1) {
49
0
      abs_qc_low = qc_low = dqc_low = 0;
50
0
      dist_low = dist0;
51
0
      rate_low = txb_costs->base_cost[coeff_ctx][0];
52
10.6k
    } else {
53
10.6k
      get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
54
10.6k
      abs_qc_low = abs_qc - 1;
55
10.6k
      dist_low = get_coeff_dist(tqc, dqc_low, shift);
56
10.6k
      rate_low =
57
10.6k
          get_coeff_cost_general(is_last, ci, abs_qc_low, sign, coeff_ctx,
58
10.6k
                                 dc_sign_ctx, txb_costs, bwl, tx_class, levels);
59
10.6k
    }
60
61
10.6k
    rd_low = RDCOST(rdmult, rate_low, dist_low);
62
10.6k
    if (rd_low < rd) {
63
67
      qcoeff[ci] = qc_low;
64
67
      dqcoeff[ci] = dqc_low;
65
67
      levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
66
67
      *accu_rate += rate_low;
67
67
      *accu_dist += dist_low - dist0;
68
10.5k
    } else {
69
10.5k
      *accu_rate += rate;
70
10.5k
      *accu_dist += dist - dist0;
71
10.5k
    }
72
10.6k
  }
73
10.6k
}
74
75
static AOM_FORCE_INLINE void update_coeff_simple(
76
    int *accu_rate, int si, int eob, TX_SIZE tx_size, TX_CLASS tx_class,
77
    int bwl, int64_t rdmult, int shift, const int16_t *dequant,
78
    const int16_t *scan, const LV_MAP_COEFF_COST *txb_costs,
79
    const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff,
80
11.9k
    uint8_t *levels, const qm_val_t *iqmatrix) {
81
11.9k
  const int dqv = get_dqv(dequant, scan[si], iqmatrix);
82
11.9k
  (void)eob;
83
  // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0)
84
  // and not the last (scan_idx != eob - 1)
85
11.9k
  assert(si != eob - 1);
86
11.9k
  assert(si > 0);
87
11.9k
  const int ci = scan[si];
88
11.9k
  const tran_low_t qc = qcoeff[ci];
89
11.9k
  const int coeff_ctx =
90
11.9k
      get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class);
91
11.9k
  if (qc == 0) {
92
6.99k
    *accu_rate += txb_costs->base_cost[coeff_ctx][0];
93
6.99k
  } else {
94
4.95k
    const tran_low_t abs_qc = abs(qc);
95
4.95k
    const tran_low_t abs_tqc = abs(tcoeff[ci]);
96
4.95k
    const tran_low_t abs_dqc = abs(dqcoeff[ci]);
97
4.95k
    int rate_low = 0;
98
4.95k
    const int rate = get_two_coeff_cost_simple(
99
4.95k
        ci, abs_qc, coeff_ctx, txb_costs, bwl, tx_class, levels, &rate_low);
100
4.95k
    if (abs_dqc < abs_tqc) {
101
2.01k
      *accu_rate += rate;
102
2.01k
      return;
103
2.01k
    }
104
105
2.93k
    const int64_t dist = get_coeff_dist(abs_tqc, abs_dqc, shift);
106
2.93k
    const int64_t rd = RDCOST(rdmult, rate, dist);
107
108
2.93k
    const tran_low_t abs_qc_low = abs_qc - 1;
109
2.93k
    const tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
110
2.93k
    const int64_t dist_low = get_coeff_dist(abs_tqc, abs_dqc_low, shift);
111
2.93k
    const int64_t rd_low = RDCOST(rdmult, rate_low, dist_low);
112
113
2.93k
    if (rd_low < rd) {
114
313
      const int sign = (qc < 0) ? 1 : 0;
115
313
      qcoeff[ci] = (-sign ^ abs_qc_low) + sign;
116
313
      dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
117
313
      levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
118
313
      *accu_rate += rate_low;
119
2.62k
    } else {
120
2.62k
      *accu_rate += rate;
121
2.62k
    }
122
2.93k
  }
123
11.9k
}
124
125
static AOM_FORCE_INLINE void update_coeff_eob(
126
    int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci,
127
    int si, TX_SIZE tx_size, TX_CLASS tx_class, int bwl, int height,
128
    int dc_sign_ctx, int64_t rdmult, int shift, const int16_t *dequant,
129
    const int16_t *scan, const LV_MAP_EOB_COST *txb_eob_costs,
130
    const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
131
    tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, int sharpness,
132
12.4k
    const qm_val_t *iqmatrix) {
133
12.4k
  const int dqv = get_dqv(dequant, scan[si], iqmatrix);
134
12.4k
  assert(si != *eob - 1);
135
12.4k
  const int ci = scan[si];
136
12.4k
  const tran_low_t qc = qcoeff[ci];
137
12.4k
  const int coeff_ctx =
138
12.4k
      get_lower_levels_ctx(levels, ci, bwl, tx_size, tx_class);
139
12.4k
  if (qc == 0) {
140
9.41k
    *accu_rate += txb_costs->base_cost[coeff_ctx][0];
141
9.41k
  } else {
142
3.07k
    int lower_level = 0;
143
3.07k
    const tran_low_t abs_qc = abs(qc);
144
3.07k
    const tran_low_t tqc = tcoeff[ci];
145
3.07k
    const tran_low_t dqc = dqcoeff[ci];
146
3.07k
    const int sign = (qc < 0) ? 1 : 0;
147
3.07k
    const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
148
3.07k
    int64_t dist = get_coeff_dist(tqc, dqc, shift) - dist0;
149
3.07k
    int rate =
150
3.07k
        get_coeff_cost_general(0, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx,
151
3.07k
                               txb_costs, bwl, tx_class, levels);
152
3.07k
    int64_t rd = RDCOST(rdmult, *accu_rate + rate, *accu_dist + dist);
153
154
3.07k
    tran_low_t qc_low, dqc_low;
155
3.07k
    tran_low_t abs_qc_low;
156
3.07k
    int64_t dist_low, rd_low;
157
3.07k
    int rate_low;
158
159
3.07k
    if (abs_qc == 1) {
160
2.87k
      abs_qc_low = 0;
161
2.87k
      dqc_low = qc_low = 0;
162
2.87k
      dist_low = 0;
163
2.87k
      rate_low = txb_costs->base_cost[coeff_ctx][0];
164
2.87k
      rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist);
165
2.87k
    } else {
166
200
      get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
167
200
      abs_qc_low = abs_qc - 1;
168
200
      dist_low = get_coeff_dist(tqc, dqc_low, shift) - dist0;
169
200
      rate_low =
170
200
          get_coeff_cost_general(0, ci, abs_qc_low, sign, coeff_ctx,
171
200
                                 dc_sign_ctx, txb_costs, bwl, tx_class, levels);
172
200
      rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist + dist_low);
173
200
    }
174
175
3.07k
    int lower_level_new_eob = 0;
176
3.07k
    const int new_eob = si + 1;
177
3.07k
    const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bwl, height, si);
178
3.07k
    const int new_eob_cost =
179
3.07k
        get_eob_cost(new_eob, txb_eob_costs, txb_costs, tx_class);
180
3.07k
    int rate_coeff_eob =
181
3.07k
        new_eob_cost + get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx_new_eob,
182
3.07k
                                          dc_sign_ctx, txb_costs, bwl,
183
3.07k
                                          tx_class);
184
3.07k
    int64_t dist_new_eob = dist;
185
3.07k
    int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob);
186
187
3.07k
    if (abs_qc_low > 0) {
188
200
      const int rate_coeff_eob_low =
189
200
          new_eob_cost + get_coeff_cost_eob(ci, abs_qc_low, sign,
190
200
                                            coeff_ctx_new_eob, dc_sign_ctx,
191
200
                                            txb_costs, bwl, tx_class);
192
200
      const int64_t dist_new_eob_low = dist_low;
193
200
      const int64_t rd_new_eob_low =
194
200
          RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low);
195
200
      if (rd_new_eob_low < rd_new_eob) {
196
6
        lower_level_new_eob = 1;
197
6
        rd_new_eob = rd_new_eob_low;
198
6
        rate_coeff_eob = rate_coeff_eob_low;
199
6
        dist_new_eob = dist_new_eob_low;
200
6
      }
201
200
    }
202
203
3.07k
    if (sharpness == 0 || abs_qc > 1) {
204
3.07k
      if (rd_low < rd) {
205
575
        lower_level = 1;
206
575
        rd = rd_low;
207
575
        rate = rate_low;
208
575
        dist = dist_low;
209
575
      }
210
3.07k
    }
211
212
3.07k
    if (sharpness == 0 && rd_new_eob < rd) {
213
2.58k
      for (int ni = 0; ni < *nz_num; ++ni) {
214
1.32k
        int last_ci = nz_ci[ni];
215
1.32k
        levels[get_padded_idx(last_ci, bwl)] = 0;
216
1.32k
        qcoeff[last_ci] = 0;
217
1.32k
        dqcoeff[last_ci] = 0;
218
1.32k
      }
219
1.26k
      *eob = new_eob;
220
1.26k
      *nz_num = 0;
221
1.26k
      *accu_rate = rate_coeff_eob;
222
1.26k
      *accu_dist = dist_new_eob;
223
1.26k
      lower_level = lower_level_new_eob;
224
1.81k
    } else {
225
1.81k
      *accu_rate += rate;
226
1.81k
      *accu_dist += dist;
227
1.81k
    }
228
229
3.07k
    if (lower_level) {
230
149
      qcoeff[ci] = qc_low;
231
149
      dqcoeff[ci] = dqc_low;
232
149
      levels[get_padded_idx(ci, bwl)] = AOMMIN(abs_qc_low, INT8_MAX);
233
149
    }
234
3.07k
    if (qcoeff[ci]) {
235
2.93k
      nz_ci[*nz_num] = ci;
236
2.93k
      ++*nz_num;
237
2.93k
    }
238
3.07k
  }
239
12.4k
}
240
241
static INLINE void update_skip(int *accu_rate, int64_t accu_dist, int *eob,
242
                               int nz_num, int *nz_ci, int64_t rdmult,
243
                               int skip_cost, int non_skip_cost,
244
46.1k
                               tran_low_t *qcoeff, tran_low_t *dqcoeff) {
245
46.1k
  const int64_t rd = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist);
246
46.1k
  const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0);
247
46.1k
  if (rd_new_eob < rd) {
248
46.8k
    for (int i = 0; i < nz_num; ++i) {
249
23.4k
      const int ci = nz_ci[i];
250
23.4k
      qcoeff[ci] = 0;
251
23.4k
      dqcoeff[ci] = 0;
252
      // no need to set up levels because this is the last step
253
      // levels[get_padded_idx(ci, bwl)] = 0;
254
23.4k
    }
255
23.4k
    *accu_rate = 0;
256
23.4k
    *eob = 0;
257
23.4k
  }
258
46.1k
}
259
260
// TODO(angiebird): use this function whenever it's possible
261
static int get_tx_type_cost(const MACROBLOCK *x, const MACROBLOCKD *xd,
262
                            int plane, TX_SIZE tx_size, TX_TYPE tx_type,
263
67.6k
                            int reduced_tx_set_used) {
264
67.6k
  if (plane > 0) return 0;
265
266
29.1k
  const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
267
268
29.1k
  const MB_MODE_INFO *mbmi = xd->mi[0];
269
29.1k
  const int is_inter = is_inter_block(mbmi);
270
29.1k
  if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 &&
271
16.3k
      !xd->lossless[xd->mi[0]->segment_id]) {
272
12.7k
    const int ext_tx_set =
273
12.7k
        get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used);
274
12.7k
    if (is_inter) {
275
0
      if (ext_tx_set > 0)
276
0
        return x->mode_costs
277
0
            .inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type];
278
12.7k
    } else {
279
12.7k
      if (ext_tx_set > 0) {
280
12.7k
        PREDICTION_MODE intra_dir;
281
12.7k
        if (mbmi->filter_intra_mode_info.use_filter_intra)
282
0
          intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
283
0
                                             .filter_intra_mode];
284
12.7k
        else
285
12.7k
          intra_dir = mbmi->mode;
286
12.7k
        return x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size]
287
12.7k
                                                [intra_dir][tx_type];
288
12.7k
      }
289
12.7k
    }
290
12.7k
  }
291
16.4k
  return 0;
292
29.1k
}
293
294
int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
295
                     int block, TX_SIZE tx_size, TX_TYPE tx_type,
296
                     const TXB_CTX *const txb_ctx, int *rate_cost,
297
46.9k
                     int sharpness) {
298
46.9k
  MACROBLOCKD *xd = &x->e_mbd;
299
46.9k
  const struct macroblock_plane *p = &x->plane[plane];
300
46.9k
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
301
46.9k
  const int16_t *scan = scan_order->scan;
302
46.9k
  const int shift = av1_get_tx_scale(tx_size);
303
46.9k
  int eob = p->eobs[block];
304
46.9k
  const int16_t *dequant = p->dequant_QTX;
305
46.9k
  const qm_val_t *iqmatrix =
306
46.9k
      av1_get_iqmatrix(&cpi->common.quant_params, xd, plane, tx_size, tx_type);
307
46.9k
  const int block_offset = BLOCK_OFFSET(block);
308
46.9k
  tran_low_t *qcoeff = p->qcoeff + block_offset;
309
46.9k
  tran_low_t *dqcoeff = p->dqcoeff + block_offset;
310
46.9k
  const tran_low_t *tcoeff = p->coeff + block_offset;
311
46.9k
  const CoeffCosts *coeff_costs = &x->coeff_costs;
312
313
  // This function is not called if eob = 0.
314
46.9k
  assert(eob > 0);
315
316
46.9k
  const AV1_COMMON *cm = &cpi->common;
317
46.9k
  const PLANE_TYPE plane_type = get_plane_type(plane);
318
46.9k
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
319
46.9k
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
320
46.9k
  const MB_MODE_INFO *mbmi = xd->mi[0];
321
46.9k
  const int bwl = get_txb_bwl(tx_size);
322
46.9k
  const int width = get_txb_wide(tx_size);
323
46.9k
  const int height = get_txb_high(tx_size);
324
46.9k
  assert(width == (1 << bwl));
325
46.9k
  const int is_inter = is_inter_block(mbmi);
326
46.9k
  const LV_MAP_COEFF_COST *txb_costs =
327
46.9k
      &coeff_costs->coeff_costs[txs_ctx][plane_type];
328
46.9k
  const int eob_multi_size = txsize_log2_minus4[tx_size];
329
46.9k
  const LV_MAP_EOB_COST *txb_eob_costs =
330
46.9k
      &coeff_costs->eob_costs[eob_multi_size][plane_type];
331
332
46.9k
  const int rshift = 2;
333
334
46.9k
  const int64_t rdmult =
335
46.9k
      (((int64_t)x->rdmult *
336
46.9k
        (plane_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8)))) +
337
46.9k
       2) >>
338
46.9k
      rshift;
339
340
46.9k
  uint8_t levels_buf[TX_PAD_2D];
341
46.9k
  uint8_t *const levels = set_levels(levels_buf, width);
342
343
46.9k
  if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels);
344
345
  // TODO(angirbird): check iqmatrix
346
347
46.9k
  const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0];
348
46.9k
  const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
349
46.9k
  const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class);
350
46.9k
  int accu_rate = eob_cost;
351
46.9k
  int64_t accu_dist = 0;
352
46.9k
  int si = eob - 1;
353
46.9k
  const int ci = scan[si];
354
46.9k
  const tran_low_t qc = qcoeff[ci];
355
46.9k
  const tran_low_t abs_qc = abs(qc);
356
46.9k
  const int sign = qc < 0;
357
46.9k
  const int max_nz_num = 2;
358
46.9k
  int nz_num = 1;
359
46.9k
  int nz_ci[3] = { ci, 0, 0 };
360
46.9k
  if (abs_qc >= 2) {
361
9.99k
    update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class,
362
9.99k
                         bwl, height, rdmult, shift, txb_ctx->dc_sign_ctx,
363
9.99k
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
364
9.99k
                         levels, iqmatrix);
365
9.99k
    --si;
366
36.9k
  } else {
367
36.9k
    assert(abs_qc == 1);
368
36.9k
    const int coeff_ctx = get_lower_levels_ctx_eob(bwl, height, si);
369
36.9k
    accu_rate +=
370
36.9k
        get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, txb_ctx->dc_sign_ctx,
371
36.9k
                           txb_costs, bwl, tx_class);
372
36.9k
    const tran_low_t tqc = tcoeff[ci];
373
36.9k
    const tran_low_t dqc = dqcoeff[ci];
374
36.9k
    const int64_t dist = get_coeff_dist(tqc, dqc, shift);
375
36.9k
    const int64_t dist0 = get_coeff_dist(tqc, 0, shift);
376
36.9k
    accu_dist += dist - dist0;
377
36.9k
    --si;
378
36.9k
  }
379
380
46.9k
#define UPDATE_COEFF_EOB_CASE(tx_class_literal)                            \
381
46.9k
  case tx_class_literal:                                                   \
382
59.4k
    for (; si >= 0 && nz_num <= max_nz_num; --si) {                        \
383
12.4k
      update_coeff_eob(&accu_rate, &accu_dist, &eob, &nz_num, nz_ci, si,   \
384
12.4k
                       tx_size, tx_class_literal, bwl, height,             \
385
12.4k
                       txb_ctx->dc_sign_ctx, rdmult, shift, dequant, scan, \
386
12.4k
                       txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,  \
387
12.4k
                       levels, sharpness, iqmatrix);                       \
388
12.4k
    }                                                                      \
389
46.9k
    break;
390
46.9k
  switch (tx_class) {
391
46.9k
    UPDATE_COEFF_EOB_CASE(TX_CLASS_2D);
392
0
    UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ);
393
0
    UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT);
394
0
#undef UPDATE_COEFF_EOB_CASE
395
0
    default: assert(false);
396
46.9k
  }
397
398
46.9k
  if (si == -1 && nz_num <= max_nz_num && sharpness == 0) {
399
46.1k
    update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost,
400
46.1k
                non_skip_cost, qcoeff, dqcoeff);
401
46.1k
  }
402
403
46.9k
#define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal)                             \
404
46.9k
  case tx_class_literal:                                                       \
405
58.9k
    for (; si >= 1; --si) {                                                    \
406
11.9k
      update_coeff_simple(&accu_rate, si, eob, tx_size, tx_class_literal, bwl, \
407
11.9k
                          rdmult, shift, dequant, scan, txb_costs, tcoeff,     \
408
11.9k
                          qcoeff, dqcoeff, levels, iqmatrix);                  \
409
11.9k
    }                                                                          \
410
46.9k
    break;
411
46.9k
  switch (tx_class) {
412
46.9k
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D);
413
0
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ);
414
0
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT);
415
0
#undef UPDATE_COEFF_SIMPLE_CASE
416
0
    default: assert(false);
417
46.9k
  }
418
419
  // DC position
420
46.9k
  if (si == 0) {
421
    // no need to update accu_dist because it's not used after this point
422
605
    int64_t dummy_dist = 0;
423
605
    update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class,
424
605
                         bwl, height, rdmult, shift, txb_ctx->dc_sign_ctx,
425
605
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
426
605
                         levels, iqmatrix);
427
605
  }
428
429
46.9k
  const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
430
46.9k
                                            cm->features.reduced_tx_set_used);
431
46.9k
  if (eob == 0)
432
23.4k
    accu_rate += skip_cost;
433
23.5k
  else
434
23.5k
    accu_rate += non_skip_cost + tx_type_cost;
435
436
46.9k
  p->eobs[block] = eob;
437
46.9k
  p->txb_entropy_ctx[block] =
438
46.9k
      av1_get_txb_entropy_context(qcoeff, scan_order, p->eobs[block]);
439
440
46.9k
  *rate_cost = accu_rate;
441
46.9k
  return eob;
442
46.9k
}
443
444
static AOM_FORCE_INLINE int warehouse_efficients_txb(
445
    const MACROBLOCK *x, const int plane, const int block,
446
    const TX_SIZE tx_size, const TXB_CTX *const txb_ctx,
447
    const struct macroblock_plane *p, const int eob,
448
    const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
449
    const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class,
450
20.6k
    int reduced_tx_set_used) {
451
20.6k
  const tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block);
452
20.6k
  const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
453
20.6k
  const int bwl = get_txb_bwl(tx_size);
454
20.6k
  const int width = get_txb_wide(tx_size);
455
20.6k
  const int height = get_txb_high(tx_size);
456
20.6k
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
457
20.6k
  const int16_t *const scan = scan_order->scan;
458
20.6k
  uint8_t levels_buf[TX_PAD_2D];
459
20.6k
  uint8_t *const levels = set_levels(levels_buf, width);
460
20.6k
  DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
461
20.6k
  const int eob_multi_size = txsize_log2_minus4[tx_size];
462
20.6k
  const LV_MAP_EOB_COST *const eob_costs =
463
20.6k
      &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
464
20.6k
  int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
465
466
20.6k
  av1_txb_init_levels(qcoeff, width, height, levels);
467
468
20.6k
  cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
469
470
20.6k
  cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
471
472
20.6k
  av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
473
474
20.6k
  const int(*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] =
475
20.6k
      coeff_costs->lps_cost;
476
20.6k
  int c = eob - 1;
477
20.6k
  {
478
20.6k
    const int pos = scan[c];
479
20.6k
    const tran_low_t v = qcoeff[pos];
480
20.6k
    const int sign = AOMSIGN(v);
481
20.6k
    const int level = (v ^ sign) - sign;
482
20.6k
    const int coeff_ctx = coeff_contexts[pos];
483
20.6k
    cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
484
485
20.6k
    if (v) {
486
      // sign bit cost
487
20.6k
      if (level > NUM_BASE_LEVELS) {
488
16.9k
        const int ctx = get_br_ctx_eob(pos, bwl, tx_class);
489
16.9k
        cost += get_br_cost(level, lps_cost[ctx]);
490
16.9k
      }
491
20.6k
      if (c) {
492
5.00k
        cost += av1_cost_literal(1);
493
15.6k
      } else {
494
15.6k
        const int sign01 = (sign ^ sign) - sign;
495
15.6k
        const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
496
15.6k
        cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
497
15.6k
        return cost;
498
15.6k
      }
499
20.6k
    }
500
20.6k
  }
501
5.00k
  const int(*base_cost)[8] = coeff_costs->base_cost;
502
326k
  for (c = eob - 2; c >= 1; --c) {
503
321k
    const int pos = scan[c];
504
321k
    const int coeff_ctx = coeff_contexts[pos];
505
321k
    const tran_low_t v = qcoeff[pos];
506
321k
    const int level = abs(v);
507
321k
    cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
508
321k
    if (v) {
509
      // sign bit cost
510
115k
      cost += av1_cost_literal(1);
511
115k
      if (level > NUM_BASE_LEVELS) {
512
55.9k
        const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
513
55.9k
        cost += get_br_cost(level, lps_cost[ctx]);
514
55.9k
      }
515
115k
    }
516
321k
  }
517
  // c == 0 after previous loop
518
5.00k
  {
519
5.00k
    const int pos = scan[c];
520
5.00k
    const tran_low_t v = qcoeff[pos];
521
5.00k
    const int coeff_ctx = coeff_contexts[pos];
522
5.00k
    const int sign = AOMSIGN(v);
523
5.00k
    const int level = (v ^ sign) - sign;
524
5.00k
    cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
525
526
5.00k
    if (v) {
527
      // sign bit cost
528
5.00k
      const int sign01 = (sign ^ sign) - sign;
529
5.00k
      const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
530
5.00k
      cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
531
5.00k
      if (level > NUM_BASE_LEVELS) {
532
5.00k
        const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
533
5.00k
        cost += get_br_cost(level, lps_cost[ctx]);
534
5.00k
      }
535
5.00k
    }
536
5.00k
  }
537
5.00k
  return cost;
538
20.6k
}
539
540
int av1_cost_coeffs_txb_estimate(const MACROBLOCK *x, const int plane,
541
                                 const int block, const TX_SIZE tx_size,
542
0
                                 const TX_TYPE tx_type) {
543
0
  assert(plane == 0);
544
545
0
  int cost = 0;
546
0
  const struct macroblock_plane *p = &x->plane[plane];
547
0
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
548
0
  const int16_t *scan = scan_order->scan;
549
0
  tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
550
551
0
  int eob = p->eobs[block];
552
553
  // coeffs
554
0
  int c = eob - 1;
555
  // eob
556
0
  {
557
0
    const int pos = scan[c];
558
0
    const tran_low_t v = abs(qcoeff[pos]) - 1;
559
0
    cost += (v << (AV1_PROB_COST_SHIFT + 2));
560
0
  }
561
  // other coeffs
562
0
  for (c = eob - 2; c >= 0; c--) {
563
0
    const int pos = scan[c];
564
0
    const tran_low_t v = abs(qcoeff[pos]);
565
0
    const int idx = AOMMIN(v, 14);
566
567
0
    cost += costLUT[idx];
568
0
  }
569
570
  // const_term does not contain DC, and log(e) does not contain eob, so both
571
  // (eob-1)
572
0
  cost += (const_term + loge_par) * (eob - 1);
573
574
0
  return cost;
575
0
}
576
577
static AOM_FORCE_INLINE int warehouse_efficients_txb_laplacian(
578
    const MACROBLOCK *x, const int plane, const int block,
579
    const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, const int eob,
580
    const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
581
    const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class,
582
0
    int reduced_tx_set_used) {
583
0
  const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
584
585
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
586
0
  const LV_MAP_EOB_COST *const eob_costs =
587
0
      &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
588
0
  int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
589
590
0
  cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
591
592
0
  cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
593
594
0
  cost += av1_cost_coeffs_txb_estimate(x, plane, block, tx_size, tx_type);
595
0
  return cost;
596
0
}
597
598
int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block,
599
                        const TX_SIZE tx_size, const TX_TYPE tx_type,
600
10.9M
                        const TXB_CTX *const txb_ctx, int reduced_tx_set_used) {
601
10.9M
  const struct macroblock_plane *p = &x->plane[plane];
602
10.9M
  const int eob = p->eobs[block];
603
10.9M
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
604
10.9M
  const PLANE_TYPE plane_type = get_plane_type(plane);
605
10.9M
  const LV_MAP_COEFF_COST *const coeff_costs =
606
10.9M
      &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
607
10.9M
  if (eob == 0) {
608
10.8M
    return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
609
10.8M
  }
610
611
18.8k
  const MACROBLOCKD *const xd = &x->e_mbd;
612
18.8k
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
613
614
18.8k
  return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob,
615
18.8k
                                  plane_type, coeff_costs, xd, tx_type,
616
18.8k
                                  tx_class, reduced_tx_set_used);
617
10.9M
}
618
619
int av1_cost_coeffs_txb_laplacian(const MACROBLOCK *x, const int plane,
620
                                  const int block, const TX_SIZE tx_size,
621
                                  const TX_TYPE tx_type,
622
                                  const TXB_CTX *const txb_ctx,
623
                                  const int reduced_tx_set_used,
624
0
                                  const int adjust_eob) {
625
0
  const struct macroblock_plane *p = &x->plane[plane];
626
0
  int eob = p->eobs[block];
627
628
0
  if (adjust_eob) {
629
0
    const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
630
0
    const int16_t *scan = scan_order->scan;
631
0
    tran_low_t *tcoeff = p->coeff + BLOCK_OFFSET(block);
632
0
    tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
633
0
    tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
634
0
    update_coeff_eob_fast(&eob, av1_get_tx_scale(tx_size), p->dequant_QTX, scan,
635
0
                          tcoeff, qcoeff, dqcoeff);
636
0
    p->eobs[block] = eob;
637
0
  }
638
639
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
640
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
641
0
  const LV_MAP_COEFF_COST *const coeff_costs =
642
0
      &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
643
0
  if (eob == 0) {
644
0
    return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
645
0
  }
646
647
0
  const MACROBLOCKD *const xd = &x->e_mbd;
648
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
649
650
0
  return warehouse_efficients_txb_laplacian(
651
0
      x, plane, block, tx_size, txb_ctx, eob, plane_type, coeff_costs, xd,
652
0
      tx_type, tx_class, reduced_tx_set_used);
653
0
}