Coverage Report

Created: 2026-05-24 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/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 "aom_ports/mem.h"
16
#include "av1/common/idct.h"
17
18
static inline void update_coeff_general(
19
    int *accu_rate, int64_t *accu_dist, int si, int eob, TX_SIZE tx_size,
20
    TX_CLASS tx_class, int bhl, int width, int64_t rdmult, int shift,
21
    int dc_sign_ctx, const int16_t *dequant, const int16_t *scan,
22
    const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
23
    tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels,
24
0
    const qm_val_t *iqmatrix, const qm_val_t *qmatrix) {
25
0
  const int dqv = get_dqv(dequant, scan[si], iqmatrix);
26
0
  const int ci = scan[si];
27
0
  const tran_low_t qc = qcoeff[ci];
28
0
  const int is_last = si == (eob - 1);
29
0
  const int coeff_ctx = get_lower_levels_ctx_general(
30
0
      is_last, si, bhl, width, levels, ci, tx_size, tx_class);
31
0
  if (qc == 0) {
32
0
    *accu_rate += txb_costs->base_cost[coeff_ctx][0];
33
0
  } else {
34
0
    const int sign = (qc < 0) ? 1 : 0;
35
0
    const tran_low_t abs_qc = abs(qc);
36
0
    const tran_low_t tqc = tcoeff[ci];
37
0
    const tran_low_t dqc = dqcoeff[ci];
38
0
    const int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci);
39
0
    const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci);
40
0
    const int rate =
41
0
        get_coeff_cost_general(is_last, ci, abs_qc, sign, coeff_ctx,
42
0
                               dc_sign_ctx, txb_costs, bhl, tx_class, levels);
43
0
    const int64_t rd = RDCOST(rdmult, rate, dist);
44
45
0
    tran_low_t qc_low, dqc_low;
46
0
    tran_low_t abs_qc_low;
47
0
    int64_t dist_low, rd_low;
48
0
    int rate_low;
49
0
    if (abs_qc == 1) {
50
0
      abs_qc_low = qc_low = dqc_low = 0;
51
0
      dist_low = dist0;
52
0
      rate_low = txb_costs->base_cost[coeff_ctx][0];
53
0
    } else {
54
0
      get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
55
0
      abs_qc_low = abs_qc - 1;
56
0
      dist_low = get_coeff_dist(tqc, dqc_low, shift, qmatrix, ci);
57
0
      rate_low =
58
0
          get_coeff_cost_general(is_last, ci, abs_qc_low, sign, coeff_ctx,
59
0
                                 dc_sign_ctx, txb_costs, bhl, tx_class, levels);
60
0
    }
61
62
0
    rd_low = RDCOST(rdmult, rate_low, dist_low);
63
0
    if (rd_low < rd) {
64
0
      qcoeff[ci] = qc_low;
65
0
      dqcoeff[ci] = dqc_low;
66
0
      levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
67
0
      *accu_rate += rate_low;
68
0
      *accu_dist += dist_low - dist0;
69
0
    } else {
70
0
      *accu_rate += rate;
71
0
      *accu_dist += dist - dist0;
72
0
    }
73
0
  }
74
0
}
75
76
static AOM_FORCE_INLINE void update_coeff_simple(
77
    int *accu_rate, int si, int eob, TX_SIZE tx_size, TX_CLASS tx_class,
78
    int bhl, int64_t rdmult, int shift, const int16_t *dequant,
79
    const int16_t *scan, const LV_MAP_COEFF_COST *txb_costs,
80
    const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff,
81
    uint8_t *levels, int sharpness, const qm_val_t *iqmatrix,
82
0
    const qm_val_t *qmatrix) {
83
0
  (void)eob;
84
  // this simple version assumes the coeff's scan_idx is not DC (scan_idx != 0)
85
  // and not the last (scan_idx != eob - 1)
86
0
  assert(si != eob - 1);
87
0
  assert(si > 0);
88
0
  const int ci = scan[si];
89
0
  const tran_low_t qc = qcoeff[ci];
90
0
  const int coeff_ctx =
91
0
      get_lower_levels_ctx(levels, ci, bhl, tx_size, tx_class);
92
0
  if (qc == 0) {
93
0
    *accu_rate += txb_costs->base_cost[coeff_ctx][0];
94
0
  } else {
95
0
    const tran_low_t abs_qc = abs(qc);
96
0
    const tran_low_t abs_tqc = abs(tcoeff[ci]);
97
0
    const tran_low_t abs_dqc = abs(dqcoeff[ci]);
98
0
    int rate_low = 0;
99
0
    const int rate = get_two_coeff_cost_simple(
100
0
        ci, abs_qc, coeff_ctx, txb_costs, bhl, tx_class, levels, &rate_low);
101
0
    if (abs_dqc < abs_tqc) {
102
0
      *accu_rate += rate;
103
0
      return;
104
0
    }
105
106
0
    const int dqv = get_dqv(dequant, scan[si], iqmatrix);
107
0
    const int64_t dist = get_coeff_dist(abs_tqc, abs_dqc, shift, qmatrix, ci);
108
0
    const int64_t rd = RDCOST(rdmult, rate, dist);
109
110
0
    const tran_low_t abs_qc_low = abs_qc - 1;
111
0
    const tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
112
0
    const int64_t dist_low =
113
0
        get_coeff_dist(abs_tqc, abs_dqc_low, shift, qmatrix, ci);
114
0
    const int64_t rd_low = RDCOST(rdmult, rate_low, dist_low);
115
116
0
    int allow_lower_qc = sharpness ? (abs_qc > 1) : 1;
117
118
0
    if (rd_low < rd && allow_lower_qc) {
119
0
      const int sign = (qc < 0) ? 1 : 0;
120
0
      qcoeff[ci] = (-sign ^ abs_qc_low) + sign;
121
0
      dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
122
0
      levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
123
0
      *accu_rate += rate_low;
124
0
    } else {
125
0
      *accu_rate += rate;
126
0
    }
127
0
  }
128
0
}
129
130
static AOM_FORCE_INLINE void update_coeff_eob(
131
    int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci,
132
    int si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, int width,
133
    int dc_sign_ctx, int64_t rdmult, int shift, const int16_t *dequant,
134
    const int16_t *scan, const LV_MAP_EOB_COST *txb_eob_costs,
135
    const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
136
    tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, int sharpness,
137
0
    const qm_val_t *iqmatrix, const qm_val_t *qmatrix) {
138
0
  assert(si != *eob - 1);
139
0
  const int ci = scan[si];
140
0
  const tran_low_t qc = qcoeff[ci];
141
0
  const int coeff_ctx =
142
0
      get_lower_levels_ctx(levels, ci, bhl, tx_size, tx_class);
143
0
  if (qc == 0) {
144
0
    *accu_rate += txb_costs->base_cost[coeff_ctx][0];
145
0
  } else {
146
0
    const int dqv = get_dqv(dequant, scan[si], iqmatrix);
147
0
    int lower_level = 0;
148
0
    const tran_low_t abs_qc = abs(qc);
149
0
    const tran_low_t tqc = tcoeff[ci];
150
0
    const tran_low_t dqc = dqcoeff[ci];
151
0
    const int sign = (qc < 0) ? 1 : 0;
152
0
    const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci);
153
0
    int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci) - dist0;
154
0
    int rate =
155
0
        get_coeff_cost_general(0, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx,
156
0
                               txb_costs, bhl, tx_class, levels);
157
0
    int64_t rd = RDCOST(rdmult, *accu_rate + rate, *accu_dist + dist);
158
159
0
    tran_low_t qc_low, dqc_low;
160
0
    tran_low_t abs_qc_low;
161
0
    int64_t dist_low, rd_low;
162
0
    int rate_low;
163
164
0
    if (abs_qc == 1) {
165
0
      abs_qc_low = 0;
166
0
      dqc_low = qc_low = 0;
167
0
      dist_low = 0;
168
0
      rate_low = txb_costs->base_cost[coeff_ctx][0];
169
0
      rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist);
170
0
    } else {
171
0
      get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
172
0
      abs_qc_low = abs_qc - 1;
173
0
      dist_low = get_coeff_dist(tqc, dqc_low, shift, qmatrix, ci) - dist0;
174
0
      rate_low =
175
0
          get_coeff_cost_general(0, ci, abs_qc_low, sign, coeff_ctx,
176
0
                                 dc_sign_ctx, txb_costs, bhl, tx_class, levels);
177
0
      rd_low = RDCOST(rdmult, *accu_rate + rate_low, *accu_dist + dist_low);
178
0
    }
179
180
0
    int lower_level_new_eob = 0;
181
0
    const int new_eob = si + 1;
182
0
    const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bhl, width, si);
183
0
    const int new_eob_cost =
184
0
        get_eob_cost(new_eob, txb_eob_costs, txb_costs, tx_class);
185
0
    int rate_coeff_eob =
186
0
        new_eob_cost + get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx_new_eob,
187
0
                                          dc_sign_ctx, txb_costs, bhl,
188
0
                                          tx_class);
189
0
    int64_t dist_new_eob = dist;
190
0
    int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob);
191
192
0
    if (abs_qc_low > 0) {
193
0
      const int rate_coeff_eob_low =
194
0
          new_eob_cost + get_coeff_cost_eob(ci, abs_qc_low, sign,
195
0
                                            coeff_ctx_new_eob, dc_sign_ctx,
196
0
                                            txb_costs, bhl, tx_class);
197
0
      const int64_t dist_new_eob_low = dist_low;
198
0
      const int64_t rd_new_eob_low =
199
0
          RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low);
200
0
      if (rd_new_eob_low < rd_new_eob) {
201
0
        lower_level_new_eob = 1;
202
0
        rd_new_eob = rd_new_eob_low;
203
0
        rate_coeff_eob = rate_coeff_eob_low;
204
0
        dist_new_eob = dist_new_eob_low;
205
0
      }
206
0
    }
207
208
0
    const int qc_threshold = (si <= 5) ? 2 : 1;
209
0
    const int allow_lower_qc = sharpness ? abs_qc > qc_threshold : 1;
210
211
0
    if (allow_lower_qc) {
212
0
      if (rd_low < rd) {
213
0
        lower_level = 1;
214
0
        rd = rd_low;
215
0
        rate = rate_low;
216
0
        dist = dist_low;
217
0
      }
218
0
    }
219
220
0
    if ((sharpness == 0 || new_eob >= 5) && rd_new_eob < rd) {
221
0
      for (int ni = 0; ni < *nz_num; ++ni) {
222
0
        int last_ci = nz_ci[ni];
223
0
        levels[get_padded_idx(last_ci, bhl)] = 0;
224
0
        qcoeff[last_ci] = 0;
225
0
        dqcoeff[last_ci] = 0;
226
0
      }
227
0
      *eob = new_eob;
228
0
      *nz_num = 0;
229
0
      *accu_rate = rate_coeff_eob;
230
0
      *accu_dist = dist_new_eob;
231
0
      lower_level = lower_level_new_eob;
232
0
    } else {
233
0
      *accu_rate += rate;
234
0
      *accu_dist += dist;
235
0
    }
236
237
0
    if (lower_level) {
238
0
      qcoeff[ci] = qc_low;
239
0
      dqcoeff[ci] = dqc_low;
240
0
      levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
241
0
    }
242
0
    if (qcoeff[ci]) {
243
0
      nz_ci[*nz_num] = ci;
244
0
      ++*nz_num;
245
0
    }
246
0
  }
247
0
}
248
249
static inline void update_skip(int *accu_rate, int64_t accu_dist, int *eob,
250
                               int nz_num, int *nz_ci, int64_t rdmult,
251
                               int skip_cost, int non_skip_cost,
252
0
                               tran_low_t *qcoeff, tran_low_t *dqcoeff) {
253
0
  const int64_t rd = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist);
254
0
  const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0);
255
0
  if (rd_new_eob < rd) {
256
0
    for (int i = 0; i < nz_num; ++i) {
257
0
      const int ci = nz_ci[i];
258
0
      qcoeff[ci] = 0;
259
0
      dqcoeff[ci] = 0;
260
      // no need to set up levels because this is the last step
261
      // levels[get_padded_idx(ci, bhl)] = 0;
262
0
    }
263
0
    *accu_rate = 0;
264
0
    *eob = 0;
265
0
  }
266
0
}
267
268
// TODO(angiebird): use this function whenever it's possible
269
static int get_tx_type_cost(const MACROBLOCK *x, const MACROBLOCKD *xd,
270
                            int plane, TX_SIZE tx_size, TX_TYPE tx_type,
271
0
                            int reduced_tx_set_used) {
272
0
  if (plane > 0) return 0;
273
274
0
  const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
275
276
0
  const MB_MODE_INFO *mbmi = xd->mi[0];
277
0
  const int is_inter = is_inter_block(mbmi);
278
0
  const TxSetType set_type =
279
0
      av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used);
280
0
  if (av1_num_ext_tx_set[set_type] > 1 &&
281
0
      !xd->lossless[xd->mi[0]->segment_id]) {
282
0
    const int ext_tx_set = ext_tx_set_index[is_inter][set_type];
283
0
    if (is_inter) {
284
0
      if (ext_tx_set > 0)
285
0
        return x->mode_costs
286
0
            .inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type];
287
0
    } else {
288
0
      if (ext_tx_set > 0) {
289
0
        PREDICTION_MODE intra_dir;
290
0
        if (mbmi->filter_intra_mode_info.use_filter_intra)
291
0
          intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
292
0
                                             .filter_intra_mode];
293
0
        else
294
0
          intra_dir = mbmi->mode;
295
0
        return x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size]
296
0
                                                [intra_dir][tx_type];
297
0
      }
298
0
    }
299
0
  }
300
0
  return 0;
301
0
}
302
303
static AOM_FORCE_INLINE void update_coeff_eob_facade(
304
    int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci,
305
    int *si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, int width,
306
    int dc_sign_ctx, int64_t rdmult, int shift, const int16_t *dequant,
307
    const int16_t *scan, const LV_MAP_EOB_COST *txb_eob_costs,
308
    const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
309
    tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, int sharpness,
310
0
    const qm_val_t *iqmatrix, const qm_val_t *qmatrix, int max_nz_num) {
311
0
  for (; *si >= 0 && *nz_num <= max_nz_num; --*si) {
312
0
    update_coeff_eob(accu_rate, accu_dist, eob, nz_num, nz_ci, *si, tx_size,
313
0
                     tx_class, bhl, width, dc_sign_ctx, rdmult, shift, dequant,
314
0
                     scan, txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,
315
0
                     levels, sharpness, iqmatrix, qmatrix);
316
0
  }
317
0
}
318
319
static AOM_FORCE_INLINE void update_coeff_simple_facade(
320
    int *accu_rate, int *si, int eob, TX_SIZE tx_size, TX_CLASS tx_class,
321
    int bhl, int64_t rdmult, int shift, const int16_t *dequant,
322
    const int16_t *scan, const LV_MAP_COEFF_COST *txb_costs,
323
    const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff,
324
    uint8_t *levels, int sharpness, const qm_val_t *iqmatrix,
325
0
    const qm_val_t *qmatrix) {
326
0
  for (; *si >= 1; --*si) {
327
0
    update_coeff_simple(accu_rate, *si, eob, tx_size, tx_class, bhl, rdmult,
328
0
                        shift, dequant, scan, txb_costs, tcoeff, qcoeff,
329
0
                        dqcoeff, levels, sharpness, iqmatrix, qmatrix);
330
0
  }
331
0
}
332
333
int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
334
                     int block, TX_SIZE tx_size, TX_TYPE tx_type,
335
                     const TXB_CTX *const txb_ctx, int *rate_cost,
336
0
                     int sharpness) {
337
0
  MACROBLOCKD *xd = &x->e_mbd;
338
0
  const struct macroblock_plane *p = &x->plane[plane];
339
0
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
340
0
  const int16_t *scan = scan_order->scan;
341
0
  const int shift = av1_get_tx_scale(tx_size);
342
0
  int eob = p->eobs[block];
343
0
  const int16_t *dequant = p->dequant_QTX;
344
0
  const qm_val_t *iqmatrix =
345
0
      av1_get_iqmatrix(&cpi->common.quant_params, xd, plane, tx_size, tx_type);
346
0
  const qm_val_t *qmatrix =
347
0
      cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR
348
0
          ? av1_get_qmatrix(&cpi->common.quant_params, xd, plane, tx_size,
349
0
                            tx_type)
350
0
          : NULL;
351
0
  const int block_offset = BLOCK_OFFSET(block);
352
0
  tran_low_t *qcoeff = p->qcoeff + block_offset;
353
0
  tran_low_t *dqcoeff = p->dqcoeff + block_offset;
354
0
  const tran_low_t *tcoeff = p->coeff + block_offset;
355
0
  const CoeffCosts *coeff_costs = &x->coeff_costs;
356
357
  // This function is not called if eob = 0.
358
0
  assert(eob > 0);
359
360
0
  const AV1_COMMON *cm = &cpi->common;
361
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
362
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
363
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
364
0
  const MB_MODE_INFO *mbmi = xd->mi[0];
365
0
  const int bhl = get_txb_bhl(tx_size);
366
0
  const int width = get_txb_wide(tx_size);
367
0
  const int height = get_txb_high(tx_size);
368
0
  assert(height == (1 << bhl));
369
0
  const int is_inter = is_inter_block(mbmi);
370
0
  const LV_MAP_COEFF_COST *txb_costs =
371
0
      &coeff_costs->coeff_costs[txs_ctx][plane_type];
372
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
373
0
  const LV_MAP_EOB_COST *txb_eob_costs =
374
0
      &coeff_costs->eob_costs[eob_multi_size][plane_type];
375
376
  // For the IQ and SSIMULACRA 2 tunings, increase rshift from 2 to 4.
377
  // This biases trellis quantization towards keeping more coefficients, and
378
  // together with the IQ and SSIMULACRA2 rdmult adjustment in
379
  // av1_compute_rd_mult_based_on_qindex(), this helps preserve image
380
  // features (like repeating patterns and camera noise/film grain), which
381
  // improves SSIMULACRA 2 scores.
382
0
  const int rshift = (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_IQ ||
383
0
                      cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIMULACRA2)
384
0
                         ? 7
385
0
                         : 5;
386
387
0
  const int(*trellis_rd_mult)[2] = cpi->sf.tx_sf.use_chroma_trellis_rd_mult
388
0
                                       ? plane_rd_mult_chroma
389
0
                                       : plane_rd_mult;
390
0
  const int64_t rdmult = ROUND_POWER_OF_TWO(
391
0
      (int64_t)x->rdmult * (8 - sharpness) *
392
0
          (trellis_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8))),
393
0
      rshift);
394
395
0
  uint8_t levels_buf[TX_PAD_2D];
396
0
  uint8_t *const levels = set_levels(levels_buf, height);
397
398
0
  if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels);
399
400
  // TODO(angirbird): check iqmatrix
401
402
0
  const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0];
403
0
  const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
404
0
  const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class);
405
0
  int accu_rate = eob_cost;
406
0
  int64_t accu_dist = 0;
407
0
  int si = eob - 1;
408
0
  const int ci = scan[si];
409
0
  const tran_low_t qc = qcoeff[ci];
410
0
  const tran_low_t abs_qc = abs(qc);
411
0
  const int sign = qc < 0;
412
0
  const int max_nz_num = 2;
413
0
  int nz_num = 1;
414
0
  int nz_ci[3] = { ci, 0, 0 };
415
0
  if (abs_qc >= 2) {
416
0
    update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class,
417
0
                         bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
418
0
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
419
0
                         levels, iqmatrix, qmatrix);
420
0
    --si;
421
0
  } else {
422
0
    assert(abs_qc == 1);
423
0
    const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
424
0
    accu_rate +=
425
0
        get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, txb_ctx->dc_sign_ctx,
426
0
                           txb_costs, bhl, tx_class);
427
0
    const tran_low_t tqc = tcoeff[ci];
428
0
    const tran_low_t dqc = dqcoeff[ci];
429
0
    const int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci);
430
0
    const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci);
431
0
    accu_dist += dist - dist0;
432
0
    --si;
433
0
  }
434
435
0
#define UPDATE_COEFF_EOB_CASE(tx_class_literal)                            \
436
0
  case tx_class_literal:                                                   \
437
0
    update_coeff_eob_facade(                                               \
438
0
        &accu_rate, &accu_dist, &eob, &nz_num, nz_ci, &si, tx_size,        \
439
0
        tx_class_literal, bhl, width, txb_ctx->dc_sign_ctx, rdmult, shift, \
440
0
        dequant, scan, txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,  \
441
0
        levels, sharpness, iqmatrix, qmatrix, max_nz_num);                 \
442
0
    break
443
0
  switch (tx_class) {
444
0
    UPDATE_COEFF_EOB_CASE(TX_CLASS_2D);
445
0
    UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ);
446
0
    UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT);
447
0
#undef UPDATE_COEFF_EOB_CASE
448
0
    default: assert(false);
449
0
  }
450
451
0
  if (si == -1 && nz_num <= max_nz_num && sharpness == 0) {
452
0
    update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost,
453
0
                non_skip_cost, qcoeff, dqcoeff);
454
0
  }
455
456
0
#define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal)                            \
457
0
  case tx_class_literal:                                                      \
458
0
    update_coeff_simple_facade(&accu_rate, &si, eob, tx_size,                 \
459
0
                               tx_class_literal, bhl, rdmult, shift, dequant, \
460
0
                               scan, txb_costs, tcoeff, qcoeff, dqcoeff,      \
461
0
                               levels, sharpness, iqmatrix, qmatrix);         \
462
0
    break
463
0
  switch (tx_class) {
464
0
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D);
465
0
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ);
466
0
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT);
467
0
#undef UPDATE_COEFF_SIMPLE_CASE
468
0
    default: assert(false);
469
0
  }
470
471
  // DC position
472
0
  if (si == 0) {
473
    // no need to update accu_dist because it's not used after this point
474
0
    int64_t dummy_dist = 0;
475
0
    update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class,
476
0
                         bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
477
0
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
478
0
                         levels, iqmatrix, qmatrix);
479
0
  }
480
481
0
  const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
482
0
                                            cm->features.reduced_tx_set_used);
483
0
  if (eob == 0)
484
0
    accu_rate += skip_cost;
485
0
  else
486
0
    accu_rate += non_skip_cost + tx_type_cost;
487
488
0
  p->eobs[block] = eob;
489
0
  p->txb_entropy_ctx[block] =
490
0
      av1_get_txb_entropy_context(qcoeff, scan_order, p->eobs[block]);
491
492
0
  *rate_cost = accu_rate;
493
0
  return eob;
494
0
}
495
496
static AOM_FORCE_INLINE int warehouse_efficients_txb(
497
    const MACROBLOCK *x, const int plane, const int block,
498
    const TX_SIZE tx_size, const TXB_CTX *const txb_ctx,
499
    const struct macroblock_plane *p, const int eob,
500
    const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
501
    const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class,
502
0
    int reduced_tx_set_used) {
503
0
  const tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block);
504
0
  const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
505
0
  const int bhl = get_txb_bhl(tx_size);
506
0
  const int width = get_txb_wide(tx_size);
507
0
  const int height = get_txb_high(tx_size);
508
0
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
509
0
  const int16_t *const scan = scan_order->scan;
510
0
  uint8_t levels_buf[TX_PAD_2D];
511
0
  uint8_t *const levels = set_levels(levels_buf, height);
512
0
  DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
513
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
514
0
  const LV_MAP_EOB_COST *const eob_costs =
515
0
      &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
516
0
  int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
517
518
0
  if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels);
519
520
0
  cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
521
522
0
  cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
523
524
0
  av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
525
526
0
  const int(*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] =
527
0
      coeff_costs->lps_cost;
528
0
  int c = eob - 1;
529
0
  {
530
0
    const int pos = scan[c];
531
0
    const tran_low_t v = qcoeff[pos];
532
533
0
    if (v) {
534
0
      const int sign = AOMSIGN(v);
535
0
      const int level = (v ^ sign) - sign;
536
0
      const int coeff_ctx = coeff_contexts[pos];
537
0
      cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
538
      // sign bit cost
539
0
      if (level > NUM_BASE_LEVELS) {
540
0
        const int ctx = get_br_ctx_eob(pos, bhl, tx_class);
541
0
        cost += get_br_cost(level, lps_cost[ctx]);
542
0
      }
543
0
      if (c) {
544
0
        cost += av1_cost_literal(1);
545
0
      } else {
546
0
        const int sign01 = (sign ^ sign) - sign;
547
0
        const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
548
0
        cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
549
0
        return cost;
550
0
      }
551
0
    }
552
0
  }
553
0
  const int(*base_cost)[8] = coeff_costs->base_cost;
554
0
  for (c = eob - 2; c >= 1; --c) {
555
0
    const int pos = scan[c];
556
0
    const int coeff_ctx = coeff_contexts[pos];
557
0
    const tran_low_t v = qcoeff[pos];
558
0
    if (!v) {
559
0
      cost += base_cost[coeff_ctx][0];
560
0
      continue;
561
0
    }
562
0
    const int level = abs(v);
563
0
    cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
564
    // sign bit cost
565
0
    cost += av1_cost_literal(1);
566
0
    if (level > NUM_BASE_LEVELS) {
567
0
      const int ctx = get_br_ctx(levels, pos, bhl, tx_class);
568
0
      cost += get_br_cost(level, lps_cost[ctx]);
569
0
    }
570
0
  }
571
  // c == 0 after previous loop
572
0
  {
573
0
    const int pos = scan[c];
574
0
    const tran_low_t v = qcoeff[pos];
575
0
    const int coeff_ctx = coeff_contexts[pos];
576
0
    if (!v) {
577
0
      cost += base_cost[coeff_ctx][0];
578
0
    } else {
579
0
      const int sign = AOMSIGN(v);
580
0
      const int level = (v ^ sign) - sign;
581
0
      cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
582
      // sign bit cost
583
0
      const int sign01 = (sign ^ sign) - sign;
584
0
      const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
585
0
      cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
586
0
      if (level > NUM_BASE_LEVELS) {
587
0
        const int ctx = get_br_ctx(levels, pos, bhl, tx_class);
588
0
        cost += get_br_cost(level, lps_cost[ctx]);
589
0
      }
590
0
    }
591
0
  }
592
0
  return cost;
593
0
}
594
595
/*!\brief Estimate the entropy cost of transform coefficients using Laplacian
596
 * distribution.
597
 *
598
 * \ingroup coefficient_coding
599
 *
600
 * This function assumes each transform coefficient is of its own Laplacian
601
 * distribution and the coefficient is the only observation of the Laplacian
602
 * distribution.
603
 *
604
 * Based on that, each coefficient's coding cost can be estimated by computing
605
 * the entropy of the corresponding Laplacian distribution.
606
 *
607
 * This function then return the sum of the estimated entropy cost for all
608
 * coefficients in the transform block.
609
 *
610
 * Note that the entropy cost of end of block (eob) and transform type (tx_type)
611
 * are not included.
612
 *
613
 * \param[in]    x              Pointer to structure holding the data for the
614
                                current encoding macroblock
615
 * \param[in]    plane          The index of the current plane
616
 * \param[in]    block          The index of the current transform block in the
617
 * macroblock. It's defined by number of 4x4 units that have been coded before
618
 * the currernt transform block
619
 * \param[in]    tx_size        The transform size
620
 * \param[in]    tx_type        The transform type
621
 * \return       int            Estimated entropy cost of coefficients in the
622
 * transform block.
623
 */
624
static int av1_cost_coeffs_txb_estimate(const MACROBLOCK *x, const int plane,
625
                                        const int block, const TX_SIZE tx_size,
626
0
                                        const TX_TYPE tx_type) {
627
0
  assert(plane == 0);
628
629
0
  int cost = 0;
630
0
  const struct macroblock_plane *p = &x->plane[plane];
631
0
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
632
0
  const int16_t *scan = scan_order->scan;
633
0
  tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
634
635
0
  int eob = p->eobs[block];
636
637
  // coeffs
638
0
  int c = eob - 1;
639
  // eob
640
0
  {
641
0
    const int pos = scan[c];
642
0
    const tran_low_t v = abs(qcoeff[pos]) - 1;
643
0
    cost += (v << (AV1_PROB_COST_SHIFT + 2));
644
0
  }
645
  // other coeffs
646
0
  for (c = eob - 2; c >= 0; c--) {
647
0
    const int pos = scan[c];
648
0
    const tran_low_t v = abs(qcoeff[pos]);
649
0
    const int idx = AOMMIN(v, 14);
650
651
0
    cost += costLUT[idx];
652
0
  }
653
654
  // const_term does not contain DC, and log(e) does not contain eob, so both
655
  // (eob-1)
656
0
  cost += (const_term + loge_par) * (eob - 1);
657
658
0
  return cost;
659
0
}
660
661
static AOM_FORCE_INLINE int warehouse_efficients_txb_laplacian(
662
    const MACROBLOCK *x, const int plane, const int block,
663
    const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, const int eob,
664
    const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
665
    const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class,
666
0
    int reduced_tx_set_used) {
667
0
  const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
668
669
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
670
0
  const LV_MAP_EOB_COST *const eob_costs =
671
0
      &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
672
0
  int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
673
674
0
  cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
675
676
0
  cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
677
678
0
  cost += av1_cost_coeffs_txb_estimate(x, plane, block, tx_size, tx_type);
679
0
  return cost;
680
0
}
681
682
int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block,
683
                        const TX_SIZE tx_size, const TX_TYPE tx_type,
684
0
                        const TXB_CTX *const txb_ctx, int reduced_tx_set_used) {
685
0
  const struct macroblock_plane *p = &x->plane[plane];
686
0
  const int eob = p->eobs[block];
687
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
688
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
689
0
  const LV_MAP_COEFF_COST *const coeff_costs =
690
0
      &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
691
0
  if (eob == 0) {
692
0
    return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
693
0
  }
694
695
0
  const MACROBLOCKD *const xd = &x->e_mbd;
696
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
697
698
0
  switch (tx_class) {
699
0
    case TX_CLASS_2D:
700
0
      return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob,
701
0
                                      plane_type, coeff_costs, xd, tx_type,
702
0
                                      TX_CLASS_2D, reduced_tx_set_used);
703
704
0
    case TX_CLASS_VERT:
705
0
      return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob,
706
0
                                      plane_type, coeff_costs, xd, tx_type,
707
0
                                      TX_CLASS_VERT, reduced_tx_set_used);
708
709
0
    case TX_CLASS_HORIZ:
710
0
      return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob,
711
0
                                      plane_type, coeff_costs, xd, tx_type,
712
0
                                      TX_CLASS_HORIZ, reduced_tx_set_used);
713
714
0
    default: assert(0 && "Invalid TX_CLASS"); return 0;
715
0
  }
716
0
}
717
718
int av1_cost_coeffs_txb_laplacian(const MACROBLOCK *x, const int plane,
719
                                  const int block, const TX_SIZE tx_size,
720
                                  const TX_TYPE tx_type,
721
                                  const TXB_CTX *const txb_ctx,
722
                                  const int reduced_tx_set_used,
723
0
                                  const int adjust_eob) {
724
0
  const struct macroblock_plane *p = &x->plane[plane];
725
0
  int eob = p->eobs[block];
726
727
0
  if (adjust_eob) {
728
0
    const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
729
0
    const int16_t *scan = scan_order->scan;
730
0
    tran_low_t *tcoeff = p->coeff + BLOCK_OFFSET(block);
731
0
    tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
732
0
    tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
733
0
    update_coeff_eob_fast(&eob, av1_get_tx_scale(tx_size), p->dequant_QTX, scan,
734
0
                          tcoeff, qcoeff, dqcoeff);
735
0
    p->eobs[block] = eob;
736
0
  }
737
738
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
739
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
740
0
  const LV_MAP_COEFF_COST *const coeff_costs =
741
0
      &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
742
0
  if (eob == 0) {
743
0
    return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
744
0
  }
745
746
0
  const MACROBLOCKD *const xd = &x->e_mbd;
747
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
748
749
0
  return warehouse_efficients_txb_laplacian(
750
0
      x, plane, block, tx_size, txb_ctx, eob, plane_type, coeff_costs, xd,
751
0
      tx_type, tx_class, reduced_tx_set_used);
752
0
}