Coverage Report

Created: 2026-10-27 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavif/ext/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
430M
    const qm_val_t *iqmatrix, const qm_val_t *qmatrix) {
25
430M
  const int ci = scan[si];
26
430M
  const tran_low_t qc = qcoeff[ci];
27
430M
  const int is_last = si == (eob - 1);
28
430M
  const int coeff_ctx = get_lower_levels_ctx_general(
29
430M
      is_last, si, bhl, width, levels, ci, tx_size, tx_class);
30
430M
  if (qc == 0) {
31
32.2M
    *accu_rate += txb_costs->base_cost[coeff_ctx][0];
32
398M
  } else {
33
398M
    const int sign = (qc < 0) ? 1 : 0;
34
398M
    const tran_low_t abs_qc = abs(qc);
35
398M
    const tran_low_t tqc = tcoeff[ci];
36
398M
    const tran_low_t dqc = dqcoeff[ci];
37
398M
    const int rate =
38
398M
        get_coeff_cost_general(is_last, ci, abs_qc, sign, coeff_ctx,
39
398M
                               dc_sign_ctx, txb_costs, bhl, tx_class, levels);
40
41
398M
    tran_low_t qc_low = 0, dqc_low = 0;
42
398M
    tran_low_t abs_qc_low = 0;
43
398M
    int rate_low;
44
398M
    if (abs_qc == 1) {
45
44.5M
      rate_low = txb_costs->base_cost[coeff_ctx][0];
46
353M
    } else {
47
353M
      const int dqv = get_dqv(dequant, scan[si], iqmatrix);
48
353M
      get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
49
353M
      abs_qc_low = abs_qc - 1;
50
353M
      rate_low =
51
353M
          get_coeff_cost_general(is_last, ci, abs_qc_low, sign, coeff_ctx,
52
353M
                                 dc_sign_ctx, txb_costs, bhl, tx_class, levels);
53
353M
    }
54
55
398M
    int64_t dist_diff_0, dist_diff_low_0;
56
398M
    if (qmatrix == NULL) {
57
199M
      const int64_t tqc2 = (int64_t)tqc * 2;
58
199M
      dist_diff_0 = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
59
199M
      dist_diff_low_0 =
60
199M
          (abs_qc == 1)
61
199M
              ? 0
62
199M
              : (((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift)));
63
199M
    } else {
64
199M
      const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci);
65
199M
      dist_diff_0 = get_coeff_dist(tqc, dqc, shift, qmatrix, ci) - dist0;
66
199M
      dist_diff_low_0 =
67
199M
          (abs_qc == 1)
68
199M
              ? 0
69
199M
              : (get_coeff_dist(tqc, dqc_low, shift, qmatrix, ci) - dist0);
70
199M
    }
71
72
398M
    const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
73
398M
    const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
74
75
398M
    if (rd_low < rd) {
76
12.0M
      qcoeff[ci] = qc_low;
77
12.0M
      dqcoeff[ci] = dqc_low;
78
12.0M
      levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
79
12.0M
      *accu_rate += rate_low;
80
12.0M
      *accu_dist += dist_diff_low_0;
81
386M
    } else {
82
386M
      *accu_rate += rate;
83
386M
      *accu_dist += dist_diff_0;
84
386M
    }
85
398M
  }
86
430M
}
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
7.84G
    const qm_val_t *qmatrix) {
95
7.84G
  (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
7.84G
  assert(si != eob - 1);
99
7.84G
  assert(si > 0);
100
7.84G
  const int ci = scan[si];
101
7.84G
  const tran_low_t qc = qcoeff[ci];
102
7.84G
  const int coeff_ctx =
103
7.84G
      get_lower_levels_ctx(levels, ci, bhl, tx_size, tx_class);
104
7.84G
  if (qc == 0) {
105
3.00G
    *accu_rate += txb_costs->base_cost[coeff_ctx][0];
106
4.83G
  } else {
107
4.83G
    const tran_low_t abs_qc = abs(qc);
108
4.83G
    const tran_low_t abs_tqc = abs(tcoeff[ci]);
109
4.83G
    const tran_low_t abs_dqc = abs(dqcoeff[ci]);
110
4.83G
    if (abs_qc == 1) {
111
1.88G
      const int *base_cost = txb_costs->base_cost[coeff_ctx];
112
1.88G
      const int rate = base_cost[1] + av1_cost_literal(1);
113
1.88G
      if (abs_dqc < abs_tqc) {
114
823M
        *accu_rate += rate;
115
823M
        return;
116
823M
      }
117
118
1.06G
      const int allow_lower_qc = sharpness ? 0 : 1;
119
1.06G
      if (allow_lower_qc) {
120
466M
        int64_t dist_diff_0;
121
466M
        if (qmatrix == NULL) {
122
466M
          dist_diff_0 =
123
466M
              ((int64_t)abs_dqc * (abs_dqc - ((int64_t)abs_tqc * 2))) *
124
466M
              (1LL << (2 * shift));
125
18.4E
        } else {
126
18.4E
          const int64_t dist0 = get_coeff_dist(abs_tqc, 0, shift, qmatrix, ci);
127
18.4E
          dist_diff_0 =
128
18.4E
              get_coeff_dist(abs_tqc, abs_dqc, shift, qmatrix, ci) - dist0;
129
18.4E
        }
130
466M
        const int rate_low = rate - base_cost[5];
131
466M
        const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
132
466M
        const int64_t rd_low = RDCOST(rdmult, rate_low, 0);
133
466M
        if (rd_low < rd) {
134
63.6M
          qcoeff[ci] = 0;
135
63.6M
          dqcoeff[ci] = 0;
136
63.6M
          levels[get_padded_idx(ci, bhl)] = 0;
137
63.6M
          *accu_rate += rate_low;
138
63.6M
          return;
139
63.6M
        }
140
466M
      }
141
1.00G
      *accu_rate += rate;
142
2.95G
    } else {
143
2.95G
      int rate_low = 0;
144
2.95G
      const int rate = get_two_coeff_cost_simple(
145
2.95G
          ci, abs_qc, coeff_ctx, txb_costs, bhl, tx_class, levels, &rate_low);
146
2.95G
      if (abs_dqc < abs_tqc) {
147
1.87G
        *accu_rate += rate;
148
1.87G
        return;
149
1.87G
      }
150
151
1.07G
      const int allow_lower_qc = sharpness ? (abs_qc > 1) : 1;
152
1.71G
      if (allow_lower_qc) {
153
1.71G
        const int dqv = get_dqv(dequant, scan[si], iqmatrix);
154
1.71G
        const tran_low_t abs_qc_low = abs_qc - 1;
155
1.71G
        const tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
156
1.71G
        int64_t dist_diff_0, dist_diff_low_0;
157
1.71G
        if (qmatrix == NULL) {
158
1.09G
          const int64_t abs_tqc2 = (int64_t)abs_tqc << 1;
159
1.09G
          dist_diff_0 =
160
1.09G
              ((int64_t)abs_dqc * (abs_dqc - abs_tqc2)) * (1LL << (2 * shift));
161
1.09G
          dist_diff_low_0 = ((int64_t)abs_dqc_low * (abs_dqc_low - abs_tqc2)) *
162
1.09G
                            (1LL << (2 * shift));
163
1.09G
        } else {
164
618M
          const int64_t dist0 = get_coeff_dist(abs_tqc, 0, shift, qmatrix, ci);
165
618M
          dist_diff_0 =
166
618M
              get_coeff_dist(abs_tqc, abs_dqc, shift, qmatrix, ci) - dist0;
167
618M
          dist_diff_low_0 =
168
618M
              get_coeff_dist(abs_tqc, abs_dqc_low, shift, qmatrix, ci) - dist0;
169
618M
        }
170
1.71G
        const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
171
1.71G
        const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
172
1.71G
        if (rd_low < rd) {
173
80.0M
          const int sign = (qc < 0) ? 1 : 0;
174
80.0M
          qcoeff[ci] = (-sign ^ abs_qc_low) + sign;
175
80.0M
          dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
176
80.0M
          levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
177
80.0M
          *accu_rate += rate_low;
178
80.0M
          return;
179
80.0M
        }
180
1.71G
      }
181
991M
      *accu_rate += rate;
182
991M
    }
183
4.83G
  }
184
7.84G
}
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
2.24G
    const qm_val_t *iqmatrix, const qm_val_t *qmatrix) {
194
2.24G
  assert(si != *eob - 1);
195
2.24G
  const int ci = scan[si];
196
2.24G
  const tran_low_t qc = qcoeff[ci];
197
2.24G
  const int coeff_ctx =
198
2.24G
      get_lower_levels_ctx(levels, ci, bhl, tx_size, tx_class);
199
2.24G
  if (qc == 0) {
200
1.59G
    *accu_rate += txb_costs->base_cost[coeff_ctx][0];
201
1.59G
  } else {
202
654M
    const int dqv = get_dqv(dequant, scan[si], iqmatrix);
203
654M
    int lower_level = 0;
204
654M
    const tran_low_t abs_qc = abs(qc);
205
654M
    const tran_low_t tqc = tcoeff[ci];
206
654M
    const tran_low_t dqc = dqcoeff[ci];
207
654M
    const int sign = (qc < 0) ? 1 : 0;
208
209
654M
    tran_low_t qc_low = 0, dqc_low = 0;
210
654M
    tran_low_t abs_qc_low = 0;
211
654M
    int rate_low;
212
213
654M
    if (abs_qc == 1) {
214
390M
      rate_low = txb_costs->base_cost[coeff_ctx][0];
215
390M
    } else {
216
263M
      get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
217
263M
      abs_qc_low = abs_qc - 1;
218
263M
      rate_low =
219
263M
          get_coeff_cost_general(0, ci, abs_qc_low, sign, coeff_ctx,
220
263M
                                 dc_sign_ctx, txb_costs, bhl, tx_class, levels);
221
263M
    }
222
223
654M
    int64_t dist, dist_low;
224
654M
    if (qmatrix == NULL) {
225
357M
      const int64_t tqc2 = (int64_t)tqc * 2;
226
357M
      dist = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
227
357M
      dist_low =
228
357M
          (abs_qc == 1)
229
357M
              ? 0
230
357M
              : (((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift)));
231
357M
    } else {
232
297M
      const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci);
233
297M
      dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci) - dist0;
234
297M
      dist_low =
235
297M
          (abs_qc == 1)
236
297M
              ? 0
237
297M
              : (get_coeff_dist(tqc, dqc_low, shift, qmatrix, ci) - dist0);
238
297M
    }
239
240
654M
    int rate =
241
654M
        get_coeff_cost_general(0, ci, abs_qc, sign, coeff_ctx, dc_sign_ctx,
242
654M
                               txb_costs, bhl, tx_class, levels);
243
654M
    int64_t rd = RDCOST(rdmult, *accu_rate + rate, *accu_dist + dist);
244
654M
    int64_t rd_low =
245
654M
        RDCOST(rdmult, *accu_rate + rate_low, *accu_dist + dist_low);
246
247
654M
    int lower_level_new_eob = 0;
248
654M
    const int new_eob = si + 1;
249
654M
    const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bhl, width, si);
250
654M
    const int new_eob_cost =
251
654M
        get_eob_cost(new_eob, txb_eob_costs, txb_costs, tx_class);
252
654M
    int rate_coeff_eob =
253
654M
        new_eob_cost + get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx_new_eob,
254
654M
                                          dc_sign_ctx, txb_costs, bhl,
255
654M
                                          tx_class);
256
654M
    int64_t dist_new_eob = dist;
257
654M
    int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob);
258
259
654M
    if (abs_qc_low > 0) {
260
304M
      const int rate_coeff_eob_low =
261
304M
          new_eob_cost + get_coeff_cost_eob(ci, abs_qc_low, sign,
262
304M
                                            coeff_ctx_new_eob, dc_sign_ctx,
263
304M
                                            txb_costs, bhl, tx_class);
264
304M
      const int64_t dist_new_eob_low = dist_low;
265
304M
      const int64_t rd_new_eob_low =
266
304M
          RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low);
267
304M
      if (rd_new_eob_low < rd_new_eob) {
268
13.1M
        lower_level_new_eob = 1;
269
13.1M
        rd_new_eob = rd_new_eob_low;
270
13.1M
        rate_coeff_eob = rate_coeff_eob_low;
271
13.1M
        dist_new_eob = dist_new_eob_low;
272
13.1M
      }
273
304M
    }
274
275
654M
    const int qc_threshold = (si <= 5) ? 2 : 1;
276
654M
    const int allow_lower_qc = sharpness ? abs_qc > qc_threshold : 1;
277
278
654M
    if (allow_lower_qc) {
279
459M
      if (rd_low < rd) {
280
69.2M
        lower_level = 1;
281
69.2M
        rd = rd_low;
282
69.2M
        rate = rate_low;
283
69.2M
        dist = dist_low;
284
69.2M
      }
285
459M
    }
286
287
654M
    if ((sharpness == 0 || new_eob >= 5) && rd_new_eob < rd) {
288
108M
      for (int ni = 0; ni < *nz_num; ++ni) {
289
55.5M
        int last_ci = nz_ci[ni];
290
55.5M
        levels[get_padded_idx(last_ci, bhl)] = 0;
291
55.5M
        qcoeff[last_ci] = 0;
292
55.5M
        dqcoeff[last_ci] = 0;
293
55.5M
      }
294
52.4M
      *eob = new_eob;
295
52.4M
      *nz_num = 0;
296
52.4M
      *accu_rate = rate_coeff_eob;
297
52.4M
      *accu_dist = dist_new_eob;
298
52.4M
      lower_level = lower_level_new_eob;
299
602M
    } else {
300
602M
      *accu_rate += rate;
301
602M
      *accu_dist += dist;
302
602M
    }
303
304
654M
    if (lower_level) {
305
47.5M
      qcoeff[ci] = qc_low;
306
47.5M
      dqcoeff[ci] = dqc_low;
307
47.5M
      levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
308
47.5M
    }
309
660M
    if (qcoeff[ci]) {
310
660M
      nz_ci[*nz_num] = ci;
311
660M
      ++*nz_num;
312
660M
    }
313
654M
  }
314
2.24G
}
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
11.7M
                               tran_low_t *qcoeff, tran_low_t *dqcoeff) {
320
11.7M
  const int64_t rd = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist);
321
11.7M
  const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0);
322
11.7M
  if (rd_new_eob < rd) {
323
8.95M
    for (int i = 0; i < nz_num; ++i) {
324
4.67M
      const int ci = nz_ci[i];
325
4.67M
      qcoeff[ci] = 0;
326
4.67M
      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
4.67M
    }
330
4.27M
    *accu_rate = 0;
331
4.27M
    *eob = 0;
332
4.27M
  }
333
11.7M
}
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
645M
                            int reduced_tx_set_used) {
339
645M
  if (plane > 0) return 0;
340
341
490M
  const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
342
343
490M
  const MB_MODE_INFO *mbmi = xd->mi[0];
344
490M
  const int is_inter = is_inter_block(mbmi);
345
490M
  const TxSetType set_type =
346
490M
      av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used);
347
490M
  if (av1_num_ext_tx_set[set_type] > 1 &&
348
490M
      !xd->lossless[xd->mi[0]->segment_id]) {
349
446M
    const int ext_tx_set = ext_tx_set_index[is_inter][set_type];
350
446M
    if (is_inter) {
351
21.0M
      if (ext_tx_set > 0)
352
21.0M
        return x->mode_costs
353
21.0M
            .inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type];
354
425M
    } else {
355
425M
      if (ext_tx_set > 0) {
356
425M
        PREDICTION_MODE intra_dir;
357
425M
        if (mbmi->filter_intra_mode_info.use_filter_intra)
358
114M
          intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
359
114M
                                             .filter_intra_mode];
360
311M
        else
361
311M
          intra_dir = mbmi->mode;
362
425M
        return x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size]
363
425M
                                                [intra_dir][tx_type];
364
425M
      }
365
425M
    }
366
446M
  }
367
43.3M
  return 0;
368
490M
}
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
349M
    const qm_val_t *iqmatrix, const qm_val_t *qmatrix, int max_nz_num) {
378
2.60G
  for (; *si >= 0 && *nz_num <= max_nz_num; --*si) {
379
2.25G
    update_coeff_eob(accu_rate, accu_dist, eob, nz_num, nz_ci, *si, tx_size,
380
2.25G
                     tx_class, bhl, width, dc_sign_ctx, rdmult, shift, dequant,
381
2.25G
                     scan, txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,
382
2.25G
                     levels, sharpness, iqmatrix, qmatrix);
383
2.25G
  }
384
349M
}
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
347M
    const qm_val_t *qmatrix) {
393
8.19G
  for (; *si >= 1; --*si) {
394
7.84G
    update_coeff_simple(accu_rate, *si, eob, tx_size, tx_class, bhl, rdmult,
395
7.84G
                        shift, dequant, scan, txb_costs, tcoeff, qcoeff,
396
7.84G
                        dqcoeff, levels, sharpness, iqmatrix, qmatrix);
397
7.84G
  }
398
347M
}
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
353M
                     int sharpness) {
404
353M
  MACROBLOCKD *xd = &x->e_mbd;
405
353M
  const struct macroblock_plane *p = &x->plane[plane];
406
353M
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
407
353M
  const int16_t *scan = scan_order->scan;
408
353M
  const int shift = av1_get_tx_scale(tx_size);
409
353M
  int eob = p->eobs[block];
410
353M
  const int16_t *dequant = p->dequant_QTX;
411
353M
  const qm_val_t *iqmatrix =
412
353M
      av1_get_iqmatrix(&cpi->common.quant_params, xd, plane, tx_size, tx_type);
413
353M
  const qm_val_t *qmatrix =
414
353M
      cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR
415
353M
          ? av1_get_qmatrix(&cpi->common.quant_params, xd, plane, tx_size,
416
237M
                            tx_type)
417
353M
          : NULL;
418
353M
  const int block_offset = BLOCK_OFFSET(block);
419
353M
  tran_low_t *qcoeff = p->qcoeff + block_offset;
420
353M
  tran_low_t *dqcoeff = p->dqcoeff + block_offset;
421
353M
  const tran_low_t *tcoeff = p->coeff + block_offset;
422
353M
  const CoeffCosts *coeff_costs = &x->coeff_costs;
423
424
  // This function is not called if eob = 0.
425
353M
  assert(eob > 0);
426
427
353M
  const AV1_COMMON *cm = &cpi->common;
428
353M
  const PLANE_TYPE plane_type = get_plane_type(plane);
429
353M
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
430
353M
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
431
353M
  const MB_MODE_INFO *mbmi = xd->mi[0];
432
353M
  const int bhl = get_txb_bhl(tx_size);
433
353M
  const int width = get_txb_wide(tx_size);
434
353M
  const int height = get_txb_high(tx_size);
435
353M
  assert(height == (1 << bhl));
436
353M
  const int is_inter = is_inter_block(mbmi);
437
353M
  const LV_MAP_COEFF_COST *txb_costs =
438
353M
      &coeff_costs->coeff_costs[txs_ctx][plane_type];
439
353M
  const int eob_multi_size = txsize_log2_minus4[tx_size];
440
353M
  const LV_MAP_EOB_COST *txb_eob_costs =
441
353M
      &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
353M
  const int rshift = (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_IQ ||
450
115M
                      cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIMULACRA2)
451
353M
                         ? 7
452
353M
                         : 5;
453
454
353M
  const int(*trellis_rd_mult)[2] = cpi->sf.tx_sf.use_chroma_trellis_rd_mult
455
353M
                                       ? plane_rd_mult_chroma
456
353M
                                       : plane_rd_mult;
457
353M
  const int64_t rdmult = ROUND_POWER_OF_TWO(
458
353M
      (int64_t)x->rdmult * (8 - sharpness) *
459
353M
          (trellis_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8))),
460
353M
      rshift);
461
462
353M
  uint8_t levels_buf[TX_PAD_2D];
463
353M
  uint8_t *const levels = set_levels(levels_buf, height);
464
465
353M
  if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels);
466
467
  // TODO(angirbird): check iqmatrix
468
469
353M
  const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0];
470
353M
  const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
471
353M
  const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class);
472
353M
  int accu_rate = eob_cost;
473
353M
  int64_t accu_dist = 0;
474
353M
  int si = eob - 1;
475
353M
  const int ci = scan[si];
476
353M
  const tran_low_t qc = qcoeff[ci];
477
353M
  const tran_low_t abs_qc = abs(qc);
478
353M
  const int sign = qc < 0;
479
353M
  const int max_nz_num = 2;
480
353M
  int nz_num = 1;
481
353M
  int nz_ci[3] = { ci, 0, 0 };
482
353M
  if (abs_qc >= 2) {
483
146M
    update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class,
484
146M
                         bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
485
146M
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
486
146M
                         levels, iqmatrix, qmatrix);
487
146M
    --si;
488
206M
  } else {
489
206M
    assert(abs_qc == 1);
490
206M
    const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
491
206M
    accu_rate +=
492
206M
        get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, txb_ctx->dc_sign_ctx,
493
206M
                           txb_costs, bhl, tx_class);
494
206M
    const tran_low_t tqc = tcoeff[ci];
495
206M
    const tran_low_t dqc = dqcoeff[ci];
496
206M
    const int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci);
497
206M
    const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci);
498
206M
    accu_dist += dist - dist0;
499
206M
    --si;
500
206M
  }
501
502
353M
#define UPDATE_COEFF_EOB_CASE(tx_class_literal)                            \
503
353M
  case tx_class_literal:                                                   \
504
352M
    update_coeff_eob_facade(                                               \
505
352M
        &accu_rate, &accu_dist, &eob, &nz_num, nz_ci, &si, tx_size,        \
506
352M
        tx_class_literal, bhl, width, txb_ctx->dc_sign_ctx, rdmult, shift, \
507
352M
        dequant, scan, txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,  \
508
352M
        levels, sharpness, iqmatrix, qmatrix, max_nz_num);                 \
509
352M
    break
510
353M
  switch (tx_class) {
511
295M
    UPDATE_COEFF_EOB_CASE(TX_CLASS_2D);
512
50.2M
    UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ);
513
6.13M
    UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT);
514
0
#undef UPDATE_COEFF_EOB_CASE
515
0
    default: assert(false);
516
353M
  }
517
518
347M
  if (si == -1 && nz_num <= max_nz_num && sharpness == 0) {
519
11.7M
    update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost,
520
11.7M
                non_skip_cost, qcoeff, dqcoeff);
521
11.7M
  }
522
523
347M
#define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal)                            \
524
350M
  case tx_class_literal:                                                      \
525
350M
    update_coeff_simple_facade(&accu_rate, &si, eob, tx_size,                 \
526
350M
                               tx_class_literal, bhl, rdmult, shift, dequant, \
527
350M
                               scan, txb_costs, tcoeff, qcoeff, dqcoeff,      \
528
350M
                               levels, sharpness, iqmatrix, qmatrix);         \
529
350M
    break
530
347M
  switch (tx_class) {
531
294M
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D);
532
50.1M
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ);
533
6.13M
    UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT);
534
0
#undef UPDATE_COEFF_SIMPLE_CASE
535
0
    default: assert(false);
536
347M
  }
537
538
  // DC position
539
339M
  if (si == 0) {
540
    // no need to update accu_dist because it's not used after this point
541
279M
    int64_t dummy_dist = 0;
542
279M
    update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class,
543
279M
                         bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
544
279M
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
545
279M
                         levels, iqmatrix, qmatrix);
546
279M
  }
547
548
339M
  const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
549
339M
                                            cm->features.reduced_tx_set_used);
550
339M
  if (eob == 0)
551
4.28M
    accu_rate += skip_cost;
552
335M
  else
553
335M
    accu_rate += non_skip_cost + tx_type_cost;
554
555
339M
  p->eobs[block] = eob;
556
339M
  p->txb_entropy_ctx[block] =
557
339M
      av1_get_txb_entropy_context(qcoeff, scan_order, p->eobs[block]);
558
559
339M
  *rate_cost = accu_rate;
560
339M
  return eob;
561
347M
}
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
300M
    int reduced_tx_set_used) {
570
300M
  const tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block);
571
300M
  const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
572
300M
  const int bhl = get_txb_bhl(tx_size);
573
300M
  const int width = get_txb_wide(tx_size);
574
300M
  const int height = get_txb_high(tx_size);
575
300M
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
576
300M
  const int16_t *const scan = scan_order->scan;
577
300M
  uint8_t levels_buf[TX_PAD_2D];
578
300M
  uint8_t *const levels = set_levels(levels_buf, height);
579
300M
  DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
580
300M
  const int eob_multi_size = txsize_log2_minus4[tx_size];
581
300M
  const LV_MAP_EOB_COST *const eob_costs =
582
300M
      &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
583
300M
  int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
584
585
300M
  if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels);
586
587
300M
  cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
588
589
300M
  cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
590
591
300M
  av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
592
593
300M
  const int(*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] =
594
300M
      coeff_costs->lps_cost;
595
300M
  int c = eob - 1;
596
300M
  {
597
300M
    const int pos = scan[c];
598
300M
    const tran_low_t v = qcoeff[pos];
599
600
300M
    if (v) {
601
298M
      const int sign = AOMSIGN(v);
602
298M
      const int level = (v ^ sign) - sign;
603
298M
      const int coeff_ctx = coeff_contexts[pos];
604
298M
      cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
605
      // sign bit cost
606
298M
      if (level > NUM_BASE_LEVELS) {
607
143M
        const int ctx = get_br_ctx_eob(pos, bhl, tx_class);
608
143M
        cost += get_br_cost(level, lps_cost[ctx]);
609
143M
      }
610
298M
      if (c) {
611
290M
        cost += av1_cost_literal(1);
612
290M
      } else {
613
7.26M
        const int sign01 = (sign ^ sign) - sign;
614
7.26M
        const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
615
7.26M
        cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
616
7.26M
        return cost;
617
7.26M
      }
618
298M
    }
619
300M
  }
620
293M
  const int(*base_cost)[8] = coeff_costs->base_cost;
621
12.6G
  for (c = eob - 2; c >= 1; --c) {
622
12.3G
    const int pos = scan[c];
623
12.3G
    const int coeff_ctx = coeff_contexts[pos];
624
12.3G
    const tran_low_t v = qcoeff[pos];
625
12.3G
    if (!v) {
626
4.34G
      cost += base_cost[coeff_ctx][0];
627
4.34G
      continue;
628
4.34G
    }
629
7.99G
    const int level = abs(v);
630
7.99G
    cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
631
    // sign bit cost
632
7.99G
    cost += av1_cost_literal(1);
633
7.99G
    if (level > NUM_BASE_LEVELS) {
634
4.78G
      const int ctx = get_br_ctx(levels, pos, bhl, tx_class);
635
4.78G
      cost += get_br_cost(level, lps_cost[ctx]);
636
4.78G
    }
637
7.99G
  }
638
  // c == 0 after previous loop
639
293M
  {
640
293M
    const int pos = scan[c];
641
293M
    const tran_low_t v = qcoeff[pos];
642
293M
    const int coeff_ctx = coeff_contexts[pos];
643
293M
    if (!v) {
644
26.1M
      cost += base_cost[coeff_ctx][0];
645
266M
    } else {
646
266M
      const int sign = AOMSIGN(v);
647
266M
      const int level = (v ^ sign) - sign;
648
266M
      cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
649
      // sign bit cost
650
266M
      const int sign01 = (sign ^ sign) - sign;
651
266M
      const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
652
266M
      cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
653
266M
      if (level > NUM_BASE_LEVELS) {
654
220M
        const int ctx = get_br_ctx(levels, pos, bhl, tx_class);
655
220M
        cost += get_br_cost(level, lps_cost[ctx]);
656
220M
      }
657
266M
    }
658
293M
  }
659
293M
  return cost;
660
300M
}
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
307M
                        const TXB_CTX *const txb_ctx, int reduced_tx_set_used) {
752
307M
  const struct macroblock_plane *p = &x->plane[plane];
753
307M
  const int eob = p->eobs[block];
754
307M
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
755
307M
  const PLANE_TYPE plane_type = get_plane_type(plane);
756
307M
  const LV_MAP_COEFF_COST *const coeff_costs =
757
307M
      &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
758
307M
  if (eob == 0) {
759
7.17M
    return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
760
7.17M
  }
761
762
300M
  const MACROBLOCKD *const xd = &x->e_mbd;
763
300M
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
764
765
300M
  switch (tx_class) {
766
280M
    case TX_CLASS_2D:
767
280M
      return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob,
768
280M
                                      plane_type, coeff_costs, xd, tx_type,
769
280M
                                      TX_CLASS_2D, reduced_tx_set_used);
770
771
7.39M
    case TX_CLASS_VERT:
772
7.39M
      return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob,
773
7.39M
                                      plane_type, coeff_costs, xd, tx_type,
774
7.39M
                                      TX_CLASS_VERT, reduced_tx_set_used);
775
776
13.1M
    case TX_CLASS_HORIZ:
777
13.1M
      return warehouse_efficients_txb(x, plane, block, tx_size, txb_ctx, p, eob,
778
13.1M
                                      plane_type, coeff_costs, xd, tx_type,
779
13.1M
                                      TX_CLASS_HORIZ, reduced_tx_set_used);
780
781
0
    default: assert(0 && "Invalid TX_CLASS"); return 0;
782
300M
  }
783
300M
}
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
}