Coverage Report

Created: 2026-07-25 07:06

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