Coverage Report

Created: 2026-06-16 07:20

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