Coverage Report

Created: 2026-04-01 07:49

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
  if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 &&
279
0
      !xd->lossless[xd->mi[0]->segment_id]) {
280
0
    const int ext_tx_set =
281
0
        get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used);
282
0
    if (is_inter) {
283
0
      if (ext_tx_set > 0)
284
0
        return x->mode_costs
285
0
            .inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type];
286
0
    } else {
287
0
      if (ext_tx_set > 0) {
288
0
        PREDICTION_MODE intra_dir;
289
0
        if (mbmi->filter_intra_mode_info.use_filter_intra)
290
0
          intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
291
0
                                             .filter_intra_mode];
292
0
        else
293
0
          intra_dir = mbmi->mode;
294
0
        return x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size]
295
0
                                                [intra_dir][tx_type];
296
0
      }
297
0
    }
298
0
  }
299
0
  return 0;
300
0
}
301
302
static AOM_FORCE_INLINE void update_coeff_eob_facade(
303
    int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci,
304
    int *si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, int width,
305
    int dc_sign_ctx, int64_t rdmult, int shift, const int16_t *dequant,
306
    const int16_t *scan, const LV_MAP_EOB_COST *txb_eob_costs,
307
    const LV_MAP_COEFF_COST *txb_costs, const tran_low_t *tcoeff,
308
    tran_low_t *qcoeff, tran_low_t *dqcoeff, uint8_t *levels, int sharpness,
309
0
    const qm_val_t *iqmatrix, const qm_val_t *qmatrix, int max_nz_num) {
310
0
  for (; *si >= 0 && *nz_num <= max_nz_num; --*si) {
311
0
    update_coeff_eob(accu_rate, accu_dist, eob, nz_num, nz_ci, *si, tx_size,
312
0
                     tx_class, bhl, width, dc_sign_ctx, rdmult, shift, dequant,
313
0
                     scan, txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,
314
0
                     levels, sharpness, iqmatrix, qmatrix);
315
0
  }
316
0
}
317
318
static AOM_FORCE_INLINE void update_coeff_simple_facade(
319
    int *accu_rate, int *si, int eob, TX_SIZE tx_size, TX_CLASS tx_class,
320
    int bhl, int64_t rdmult, int shift, const int16_t *dequant,
321
    const int16_t *scan, const LV_MAP_COEFF_COST *txb_costs,
322
    const tran_low_t *tcoeff, tran_low_t *qcoeff, tran_low_t *dqcoeff,
323
    uint8_t *levels, int sharpness, const qm_val_t *iqmatrix,
324
0
    const qm_val_t *qmatrix) {
325
0
  for (; *si >= 1; --*si) {
326
0
    update_coeff_simple(accu_rate, *si, eob, tx_size, tx_class, bhl, rdmult,
327
0
                        shift, dequant, scan, txb_costs, tcoeff, qcoeff,
328
0
                        dqcoeff, levels, sharpness, iqmatrix, qmatrix);
329
0
  }
330
0
}
331
332
int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
333
                     int block, TX_SIZE tx_size, TX_TYPE tx_type,
334
                     const TXB_CTX *const txb_ctx, int *rate_cost,
335
0
                     int sharpness) {
336
0
  MACROBLOCKD *xd = &x->e_mbd;
337
0
  const struct macroblock_plane *p = &x->plane[plane];
338
0
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
339
0
  const int16_t *scan = scan_order->scan;
340
0
  const int shift = av1_get_tx_scale(tx_size);
341
0
  int eob = p->eobs[block];
342
0
  const int16_t *dequant = p->dequant_QTX;
343
0
  const qm_val_t *iqmatrix =
344
0
      av1_get_iqmatrix(&cpi->common.quant_params, xd, plane, tx_size, tx_type);
345
0
  const qm_val_t *qmatrix =
346
0
      cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR
347
0
          ? av1_get_qmatrix(&cpi->common.quant_params, xd, plane, tx_size,
348
0
                            tx_type)
349
0
          : NULL;
350
0
  const int block_offset = BLOCK_OFFSET(block);
351
0
  tran_low_t *qcoeff = p->qcoeff + block_offset;
352
0
  tran_low_t *dqcoeff = p->dqcoeff + block_offset;
353
0
  const tran_low_t *tcoeff = p->coeff + block_offset;
354
0
  const CoeffCosts *coeff_costs = &x->coeff_costs;
355
356
  // This function is not called if eob = 0.
357
0
  assert(eob > 0);
358
359
0
  const AV1_COMMON *cm = &cpi->common;
360
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
361
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
362
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
363
0
  const MB_MODE_INFO *mbmi = xd->mi[0];
364
0
  const int bhl = get_txb_bhl(tx_size);
365
0
  const int width = get_txb_wide(tx_size);
366
0
  const int height = get_txb_high(tx_size);
367
0
  assert(height == (1 << bhl));
368
0
  const int is_inter = is_inter_block(mbmi);
369
0
  const LV_MAP_COEFF_COST *txb_costs =
370
0
      &coeff_costs->coeff_costs[txs_ctx][plane_type];
371
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
372
0
  const LV_MAP_EOB_COST *txb_eob_costs =
373
0
      &coeff_costs->eob_costs[eob_multi_size][plane_type];
374
375
  // For the IQ and SSIMULACRA 2 tunings, increase rshift from 2 to 4.
376
  // This biases trellis quantization towards keeping more coefficients, and
377
  // together with the IQ and SSIMULACRA2 rdmult adjustment in
378
  // av1_compute_rd_mult_based_on_qindex(), this helps preserve image
379
  // features (like repeating patterns and camera noise/film grain), which
380
  // improves SSIMULACRA 2 scores.
381
0
  const int rshift = (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_IQ ||
382
0
                      cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIMULACRA2)
383
0
                         ? 7
384
0
                         : 5;
385
386
0
  const int64_t rdmult = ROUND_POWER_OF_TWO(
387
0
      (int64_t)x->rdmult * (8 - sharpness) *
388
0
          (plane_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8))),
389
0
      rshift);
390
391
0
  uint8_t levels_buf[TX_PAD_2D];
392
0
  uint8_t *const levels = set_levels(levels_buf, height);
393
394
0
  if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels);
395
396
  // TODO(angirbird): check iqmatrix
397
398
0
  const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0];
399
0
  const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
400
0
  const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class);
401
0
  int accu_rate = eob_cost;
402
0
  int64_t accu_dist = 0;
403
0
  int si = eob - 1;
404
0
  const int ci = scan[si];
405
0
  const tran_low_t qc = qcoeff[ci];
406
0
  const tran_low_t abs_qc = abs(qc);
407
0
  const int sign = qc < 0;
408
0
  const int max_nz_num = 2;
409
0
  int nz_num = 1;
410
0
  int nz_ci[3] = { ci, 0, 0 };
411
0
  if (abs_qc >= 2) {
412
0
    update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class,
413
0
                         bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
414
0
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
415
0
                         levels, iqmatrix, qmatrix);
416
0
    --si;
417
0
  } else {
418
0
    assert(abs_qc == 1);
419
0
    const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
420
0
    accu_rate +=
421
0
        get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, txb_ctx->dc_sign_ctx,
422
0
                           txb_costs, bhl, tx_class);
423
0
    const tran_low_t tqc = tcoeff[ci];
424
0
    const tran_low_t dqc = dqcoeff[ci];
425
0
    const int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci);
426
0
    const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci);
427
0
    accu_dist += dist - dist0;
428
0
    --si;
429
0
  }
430
431
0
#define UPDATE_COEFF_EOB_CASE(tx_class_literal)                            \
432
0
  case tx_class_literal:                                                   \
433
0
    update_coeff_eob_facade(                                               \
434
0
        &accu_rate, &accu_dist, &eob, &nz_num, nz_ci, &si, tx_size,        \
435
0
        tx_class_literal, bhl, width, txb_ctx->dc_sign_ctx, rdmult, shift, \
436
0
        dequant, scan, txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,  \
437
0
        levels, sharpness, iqmatrix, qmatrix, max_nz_num);                 \
438
0
    break
439
0
  switch (tx_class) {
440
0
    UPDATE_COEFF_EOB_CASE(TX_CLASS_2D);
441
0
    UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ);
442
0
    UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT);
443
0
#undef UPDATE_COEFF_EOB_CASE
444
0
    default: assert(false);
445
0
  }
446
447
0
  if (si == -1 && nz_num <= max_nz_num && sharpness == 0) {
448
0
    update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost,
449
0
                non_skip_cost, qcoeff, dqcoeff);
450
0
  }
451
452
0
#define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal)                            \
453
0
  case tx_class_literal:                                                      \
454
0
    update_coeff_simple_facade(&accu_rate, &si, eob, tx_size,                 \
455
0
                               tx_class_literal, bhl, rdmult, shift, dequant, \
456
0
                               scan, txb_costs, tcoeff, qcoeff, dqcoeff,      \
457
0
                               levels, sharpness, iqmatrix, qmatrix);         \
458
0
    break
459
0
  switch (tx_class) {
460
0
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D);
461
0
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ);
462
0
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT);
463
0
#undef UPDATE_COEFF_SIMPLE_CASE
464
0
    default: assert(false);
465
0
  }
466
467
  // DC position
468
0
  if (si == 0) {
469
    // no need to update accu_dist because it's not used after this point
470
0
    int64_t dummy_dist = 0;
471
0
    update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class,
472
0
                         bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
473
0
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
474
0
                         levels, iqmatrix, qmatrix);
475
0
  }
476
477
0
  const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
478
0
                                            cm->features.reduced_tx_set_used);
479
0
  if (eob == 0)
480
0
    accu_rate += skip_cost;
481
0
  else
482
0
    accu_rate += non_skip_cost + tx_type_cost;
483
484
0
  p->eobs[block] = eob;
485
0
  p->txb_entropy_ctx[block] =
486
0
      av1_get_txb_entropy_context(qcoeff, scan_order, p->eobs[block]);
487
488
0
  *rate_cost = accu_rate;
489
0
  return eob;
490
0
}
491
492
static AOM_FORCE_INLINE int warehouse_efficients_txb(
493
    const MACROBLOCK *x, const int plane, const int block,
494
    const TX_SIZE tx_size, const TXB_CTX *const txb_ctx,
495
    const struct macroblock_plane *p, const int eob,
496
    const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
497
    const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class,
498
0
    int reduced_tx_set_used) {
499
0
  const tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block);
500
0
  const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
501
0
  const int bhl = get_txb_bhl(tx_size);
502
0
  const int width = get_txb_wide(tx_size);
503
0
  const int height = get_txb_high(tx_size);
504
0
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
505
0
  const int16_t *const scan = scan_order->scan;
506
0
  uint8_t levels_buf[TX_PAD_2D];
507
0
  uint8_t *const levels = set_levels(levels_buf, height);
508
0
  DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
509
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
510
0
  const LV_MAP_EOB_COST *const eob_costs =
511
0
      &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
512
0
  int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
513
514
0
  av1_txb_init_levels(qcoeff, width, height, levels);
515
516
0
  cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
517
518
0
  cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
519
520
0
  av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
521
522
0
  const int(*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] =
523
0
      coeff_costs->lps_cost;
524
0
  int c = eob - 1;
525
0
  {
526
0
    const int pos = scan[c];
527
0
    const tran_low_t v = qcoeff[pos];
528
0
    const int sign = AOMSIGN(v);
529
0
    const int level = (v ^ sign) - sign;
530
0
    const int coeff_ctx = coeff_contexts[pos];
531
0
    cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
532
533
0
    if (v) {
534
      // sign bit cost
535
0
      if (level > NUM_BASE_LEVELS) {
536
0
        const int ctx = get_br_ctx_eob(pos, bhl, tx_class);
537
0
        cost += get_br_cost(level, lps_cost[ctx]);
538
0
      }
539
0
      if (c) {
540
0
        cost += av1_cost_literal(1);
541
0
      } else {
542
0
        const int sign01 = (sign ^ sign) - sign;
543
0
        const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
544
0
        cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
545
0
        return cost;
546
0
      }
547
0
    }
548
0
  }
549
0
  const int(*base_cost)[8] = coeff_costs->base_cost;
550
0
  for (c = eob - 2; c >= 1; --c) {
551
0
    const int pos = scan[c];
552
0
    const int coeff_ctx = coeff_contexts[pos];
553
0
    const tran_low_t v = qcoeff[pos];
554
0
    const int level = abs(v);
555
0
    cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
556
0
    if (v) {
557
      // sign bit cost
558
0
      cost += av1_cost_literal(1);
559
0
      if (level > NUM_BASE_LEVELS) {
560
0
        const int ctx = get_br_ctx(levels, pos, bhl, tx_class);
561
0
        cost += get_br_cost(level, lps_cost[ctx]);
562
0
      }
563
0
    }
564
0
  }
565
  // c == 0 after previous loop
566
0
  {
567
0
    const int pos = scan[c];
568
0
    const tran_low_t v = qcoeff[pos];
569
0
    const int coeff_ctx = coeff_contexts[pos];
570
0
    const int sign = AOMSIGN(v);
571
0
    const int level = (v ^ sign) - sign;
572
0
    cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
573
574
0
    if (v) {
575
      // sign bit cost
576
0
      const int sign01 = (sign ^ sign) - sign;
577
0
      const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
578
0
      cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
579
0
      if (level > NUM_BASE_LEVELS) {
580
0
        const int ctx = get_br_ctx(levels, pos, bhl, tx_class);
581
0
        cost += get_br_cost(level, lps_cost[ctx]);
582
0
      }
583
0
    }
584
0
  }
585
0
  return cost;
586
0
}
587
588
/*!\brief Estimate the entropy cost of transform coefficients using Laplacian
589
 * distribution.
590
 *
591
 * \ingroup coefficient_coding
592
 *
593
 * This function assumes each transform coefficient is of its own Laplacian
594
 * distribution and the coefficient is the only observation of the Laplacian
595
 * distribution.
596
 *
597
 * Based on that, each coefficient's coding cost can be estimated by computing
598
 * the entropy of the corresponding Laplacian distribution.
599
 *
600
 * This function then return the sum of the estimated entropy cost for all
601
 * coefficients in the transform block.
602
 *
603
 * Note that the entropy cost of end of block (eob) and transform type (tx_type)
604
 * are not included.
605
 *
606
 * \param[in]    x              Pointer to structure holding the data for the
607
                                current encoding macroblock
608
 * \param[in]    plane          The index of the current plane
609
 * \param[in]    block          The index of the current transform block in the
610
 * macroblock. It's defined by number of 4x4 units that have been coded before
611
 * the currernt transform block
612
 * \param[in]    tx_size        The transform size
613
 * \param[in]    tx_type        The transform type
614
 * \return       int            Estimated entropy cost of coefficients in the
615
 * transform block.
616
 */
617
static int av1_cost_coeffs_txb_estimate(const MACROBLOCK *x, const int plane,
618
                                        const int block, const TX_SIZE tx_size,
619
0
                                        const TX_TYPE tx_type) {
620
0
  assert(plane == 0);
621
622
0
  int cost = 0;
623
0
  const struct macroblock_plane *p = &x->plane[plane];
624
0
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
625
0
  const int16_t *scan = scan_order->scan;
626
0
  tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
627
628
0
  int eob = p->eobs[block];
629
630
  // coeffs
631
0
  int c = eob - 1;
632
  // eob
633
0
  {
634
0
    const int pos = scan[c];
635
0
    const tran_low_t v = abs(qcoeff[pos]) - 1;
636
0
    cost += (v << (AV1_PROB_COST_SHIFT + 2));
637
0
  }
638
  // other coeffs
639
0
  for (c = eob - 2; c >= 0; c--) {
640
0
    const int pos = scan[c];
641
0
    const tran_low_t v = abs(qcoeff[pos]);
642
0
    const int idx = AOMMIN(v, 14);
643
644
0
    cost += costLUT[idx];
645
0
  }
646
647
  // const_term does not contain DC, and log(e) does not contain eob, so both
648
  // (eob-1)
649
0
  cost += (const_term + loge_par) * (eob - 1);
650
651
0
  return cost;
652
0
}
653
654
static AOM_FORCE_INLINE int warehouse_efficients_txb_laplacian(
655
    const MACROBLOCK *x, const int plane, const int block,
656
    const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, const int eob,
657
    const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
658
    const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class,
659
0
    int reduced_tx_set_used) {
660
0
  const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
661
662
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
663
0
  const LV_MAP_EOB_COST *const eob_costs =
664
0
      &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
665
0
  int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
666
667
0
  cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
668
669
0
  cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
670
671
0
  cost += av1_cost_coeffs_txb_estimate(x, plane, block, tx_size, tx_type);
672
0
  return cost;
673
0
}
674
675
int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block,
676
                        const TX_SIZE tx_size, const TX_TYPE tx_type,
677
0
                        const TXB_CTX *const txb_ctx, int reduced_tx_set_used) {
678
0
  const struct macroblock_plane *p = &x->plane[plane];
679
0
  const int eob = p->eobs[block];
680
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
681
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
682
0
  const LV_MAP_COEFF_COST *const coeff_costs =
683
0
      &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
684
0
  if (eob == 0) {
685
0
    return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
686
0
  }
687
688
0
  const MACROBLOCKD *const xd = &x->e_mbd;
689
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
690
691
0
  return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob,
692
0
                                  plane_type, coeff_costs, xd, tx_type,
693
0
                                  tx_class, reduced_tx_set_used);
694
0
}
695
696
int av1_cost_coeffs_txb_laplacian(const MACROBLOCK *x, const int plane,
697
                                  const int block, const TX_SIZE tx_size,
698
                                  const TX_TYPE tx_type,
699
                                  const TXB_CTX *const txb_ctx,
700
                                  const int reduced_tx_set_used,
701
0
                                  const int adjust_eob) {
702
0
  const struct macroblock_plane *p = &x->plane[plane];
703
0
  int eob = p->eobs[block];
704
705
0
  if (adjust_eob) {
706
0
    const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
707
0
    const int16_t *scan = scan_order->scan;
708
0
    tran_low_t *tcoeff = p->coeff + BLOCK_OFFSET(block);
709
0
    tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
710
0
    tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
711
0
    update_coeff_eob_fast(&eob, av1_get_tx_scale(tx_size), p->dequant_QTX, scan,
712
0
                          tcoeff, qcoeff, dqcoeff);
713
0
    p->eobs[block] = eob;
714
0
  }
715
716
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
717
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
718
0
  const LV_MAP_COEFF_COST *const coeff_costs =
719
0
      &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
720
0
  if (eob == 0) {
721
0
    return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
722
0
  }
723
724
0
  const MACROBLOCKD *const xd = &x->e_mbd;
725
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
726
727
0
  return warehouse_efficients_txb_laplacian(
728
0
      x, plane, block, tx_size, txb_ctx, eob, plane_type, coeff_costs, xd,
729
0
      tx_type, tx_class, reduced_tx_set_used);
730
0
}