Coverage Report

Created: 2026-09-03 06:27

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