Coverage Report

Created: 2026-10-28 07:26

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