Coverage Report

Created: 2026-11-12 08: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
#include "av1/encoder/rdopt_utils.h"
15
16
#include "aom_ports/mem.h"
17
#include "av1/common/idct.h"
18
19
static AOM_FORCE_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, ci, 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 qm_val = qmatrix[ci];
66
0
      const int64_t dist0 = get_coeff_dist_qm(tqc, 0, shift, qm_val);
67
0
      dist_diff_0 = get_coeff_dist_qm(tqc, dqc, shift, qm_val) - dist0;
68
0
      dist_diff_low_0 =
69
0
          (abs_qc == 1)
70
0
              ? 0
71
0
              : (get_coeff_dist_qm(tqc, dqc_low, shift, qm_val) - dist0);
72
0
    }
73
74
0
    const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
75
0
    const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
76
77
0
    if (rd_low < rd) {
78
0
      qcoeff[ci] = qc_low;
79
0
      dqcoeff[ci] = dqc_low;
80
0
      levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
81
0
      *accu_rate += rate_low;
82
0
      *accu_dist += dist_diff_low_0;
83
0
    } else {
84
0
      *accu_rate += rate;
85
0
      *accu_dist += dist_diff_0;
86
0
    }
87
0
  }
88
0
}
89
90
/*!\cond */
91
typedef struct {
92
  int64_t rdmult;
93
  const LV_MAP_COEFF_COST *txb_costs;
94
  const tran_low_t *tcoeff;
95
  tran_low_t *qcoeff;
96
  tran_low_t *dqcoeff;
97
  uint8_t *levels;
98
  const qm_val_t *iqmatrix;
99
  const qm_val_t *qmatrix;
100
  int bhl;
101
  int shift;
102
  int dequant_ac;
103
  int sharpness;
104
} TxbSimpleCtx;
105
/*!\endcond */
106
107
/*!\cond */
108
typedef struct {
109
  int64_t rdmult;
110
  int64_t accu_dist;
111
  const LV_MAP_EOB_COST *txb_eob_costs;
112
  const LV_MAP_COEFF_COST *txb_costs;
113
  const int16_t *dequant;
114
  const tran_low_t *tcoeff;
115
  tran_low_t *qcoeff;
116
  tran_low_t *dqcoeff;
117
  uint8_t *levels;
118
  const qm_val_t *iqmatrix;
119
  const qm_val_t *qmatrix;
120
  int *nz_ci;
121
  int eob;
122
  int nz_num;
123
  int accu_rate;
124
  int bhl;
125
  int width;
126
  int dc_sign_ctx;
127
  int shift;
128
  int sharpness;
129
  int is_noise_pattern;
130
} TxbEobCtx;
131
/*!\endcond */
132
133
#if defined(__GNUC__) || defined(__clang__)
134
__attribute__((noinline))
135
#endif
136
static int update_coeff_simple_nz(int ci, tran_low_t qc, int coeff_ctx,
137
0
                                  TX_CLASS tx_class, const TxbSimpleCtx *ctx) {
138
0
  assert(ci > 0);
139
0
  const LV_MAP_COEFF_COST *txb_costs = ctx->txb_costs;
140
0
  const tran_low_t abs_qc = abs(qc);
141
0
  const tran_low_t abs_tqc = abs(ctx->tcoeff[ci]);
142
0
  const tran_low_t abs_dqc = abs(ctx->dqcoeff[ci]);
143
0
  if (abs_qc == 1) {
144
0
    const int *const base_cost = txb_costs->base_cost[coeff_ctx];
145
0
    const int rate = base_cost[1] + av1_cost_literal(1);
146
0
    if (ctx->sharpness || abs_dqc < abs_tqc) {
147
0
      return rate;
148
0
    }
149
150
0
    const qm_val_t *qmatrix = ctx->qmatrix;
151
0
    const int shift = ctx->shift;
152
0
    int64_t dist_diff_0;
153
0
    if (qmatrix == NULL) {
154
0
      dist_diff_0 = ((int64_t)abs_dqc * (abs_dqc - ((int64_t)abs_tqc * 2))) *
155
0
                    (1LL << (2 * shift));
156
0
    } else {
157
0
      const int64_t qm_val = qmatrix[ci];
158
0
      const int64_t dist0 = get_coeff_dist_qm(abs_tqc, 0, shift, qm_val);
159
0
      dist_diff_0 = get_coeff_dist_qm(abs_tqc, abs_dqc, shift, qm_val) - dist0;
160
0
    }
161
0
    const int rate_low = base_cost[0];
162
0
    const int64_t rdmult = ctx->rdmult;
163
0
    const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
164
0
    const int64_t rd_low = RDCOST(rdmult, rate_low, 0);
165
0
    if (rd_low < rd) {
166
0
      ctx->qcoeff[ci] = 0;
167
0
      ctx->dqcoeff[ci] = 0;
168
0
      ctx->levels[get_padded_idx(ci, ctx->bhl)] = 0;
169
0
      return rate_low;
170
0
    }
171
0
    return rate;
172
0
  } else {
173
0
    const int bhl = ctx->bhl;
174
0
    uint8_t *levels = ctx->levels;
175
0
    int rate, rate_low;
176
0
    if (abs_qc == 2) {
177
0
      const int *const base_cost = txb_costs->base_cost[coeff_ctx];
178
0
      rate = base_cost[2] + av1_cost_literal(1);
179
0
      if (abs_dqc < abs_tqc) return rate;
180
0
      rate_low = base_cost[1] + av1_cost_literal(1);
181
0
    } else {
182
0
      const int br_ctx = get_br_ctx_simple(levels, ci, bhl, tx_class);
183
0
      const int *const lps_cost = txb_costs->lps_cost[br_ctx];
184
0
      const int *const base_cost = txb_costs->base_cost[coeff_ctx];
185
0
      const int base_rate = base_cost[3] + av1_cost_literal(1);
186
0
      rate = base_rate + get_br_cost(abs_qc, lps_cost);
187
0
      if (abs_dqc < abs_tqc) return rate;
188
0
      rate_low = (abs_qc == 3)
189
0
                     ? (base_cost[2] + av1_cost_literal(1))
190
0
                     : (base_rate + get_br_cost(abs_qc - 1, lps_cost));
191
0
    }
192
193
    // update_coeff_simple_nz is only invoked for AC coefficients (si > 0), so
194
    // dequant_ac (dequant[1]) can be used directly without a (ci > 0) branch.
195
0
    int dqv = ctx->dequant_ac;
196
0
    if (ctx->iqmatrix != NULL) {
197
0
      dqv =
198
0
          ((ctx->iqmatrix[ci] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
199
0
    }
200
0
    const int shift = ctx->shift;
201
0
    const tran_low_t abs_qc_low = abs_qc - 1;
202
0
    const tran_low_t abs_dqc_low = (abs_qc_low * dqv) >> shift;
203
0
    const qm_val_t *qmatrix = ctx->qmatrix;
204
0
    int64_t dist_diff_0, dist_diff_low_0;
205
0
    if (qmatrix == NULL) {
206
0
      const int64_t abs_tqc2 = (int64_t)abs_tqc << 1;
207
0
      dist_diff_0 =
208
0
          ((int64_t)abs_dqc * (abs_dqc - abs_tqc2)) * (1LL << (2 * shift));
209
0
      dist_diff_low_0 = ((int64_t)abs_dqc_low * (abs_dqc_low - abs_tqc2)) *
210
0
                        (1LL << (2 * shift));
211
0
    } else {
212
0
      const int64_t qm_val = qmatrix[ci];
213
0
      dist_diff_0 = get_coeff_dist_qm(abs_tqc, abs_dqc, shift, qm_val);
214
0
      dist_diff_low_0 = get_coeff_dist_qm(abs_tqc, abs_dqc_low, shift, qm_val);
215
0
    }
216
0
    const int64_t rdmult = ctx->rdmult;
217
0
    const int64_t rd = RDCOST(rdmult, rate, dist_diff_0);
218
0
    const int64_t rd_low = RDCOST(rdmult, rate_low, dist_diff_low_0);
219
0
    if (rd_low < rd) {
220
0
      const int sign = (qc < 0) ? 1 : 0;
221
0
      ctx->qcoeff[ci] = (-sign ^ abs_qc_low) + sign;
222
0
      ctx->dqcoeff[ci] = (-sign ^ abs_dqc_low) + sign;
223
0
      levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
224
0
      return rate_low;
225
0
    }
226
0
    return rate;
227
0
  }
228
0
}
229
230
#if defined(__GNUC__) || defined(__clang__)
231
__attribute__((noinline))
232
#endif
233
static int update_coeff_eob_nz(int si, int ci, tran_low_t qc, int coeff_ctx,
234
0
                               TX_CLASS tx_class, TxbEobCtx *ctx) {
235
0
  const LV_MAP_COEFF_COST *txb_costs = ctx->txb_costs;
236
0
  const int bhl = ctx->bhl;
237
0
  const int dc_sign_ctx = ctx->dc_sign_ctx;
238
0
  uint8_t *levels = ctx->levels;
239
0
  const tran_low_t abs_qc = abs(qc);
240
0
  const tran_low_t tqc = ctx->tcoeff[ci];
241
0
  const tran_low_t dqc = ctx->dqcoeff[ci];
242
0
  const int sign = (qc < 0) ? 1 : 0;
243
0
  const int shift = ctx->shift;
244
0
  const qm_val_t *qmatrix = ctx->qmatrix;
245
246
0
  const int new_eob = si + 1;
247
0
  const int min_eob_cutoff = ctx->is_noise_pattern ? 8 : 5;
248
0
  const int qc_threshold = (si <= min_eob_cutoff) ? 2 : 1;
249
0
  const int sharpness = ctx->sharpness;
250
0
  const int allow_lower_qc = sharpness ? (abs_qc > qc_threshold) : 1;
251
0
  const int allow_new_eob = (sharpness == 0 || new_eob >= min_eob_cutoff);
252
253
0
  const int sign_cost = (ci == 0) ? txb_costs->dc_sign_cost[dc_sign_ctx][sign]
254
0
                                  : av1_cost_literal(1);
255
0
  const int *const base_cost = txb_costs->base_cost[coeff_ctx];
256
0
  const int *lps_cost = txb_costs->lps_cost[0];
257
0
  int rate = base_cost[AOMMIN(abs_qc, 3)] + sign_cost;
258
0
  if (abs_qc > NUM_BASE_LEVELS) {
259
0
    const int br_ctx = get_br_ctx(levels, ci, bhl, tx_class);
260
0
    lps_cost = txb_costs->lps_cost[br_ctx];
261
0
    rate += get_br_cost(abs_qc, lps_cost);
262
0
  }
263
0
  int64_t dist;
264
0
  int64_t dist0 = 0;
265
0
  int64_t qm_val = 0;
266
0
  if (qmatrix == NULL) {
267
0
    const int64_t tqc2 = (int64_t)tqc * 2;
268
0
    dist = ((int64_t)dqc * (dqc - tqc2)) * (1LL << (2 * shift));
269
0
  } else {
270
0
    qm_val = qmatrix[ci];
271
0
    dist0 = get_coeff_dist_qm(tqc, 0, shift, qm_val);
272
0
    dist = get_coeff_dist_qm(tqc, dqc, shift, qm_val) - dist0;
273
0
  }
274
275
0
  if (!allow_lower_qc && !allow_new_eob) {
276
0
    ctx->accu_dist += dist;
277
0
    ctx->nz_ci[ctx->nz_num] = ci;
278
0
    ++ctx->nz_num;
279
0
    return ctx->accu_rate + rate;
280
0
  }
281
282
0
  const int64_t dist_orig = dist;
283
0
  tran_low_t qc_low = 0, dqc_low = 0;
284
0
  tran_low_t abs_qc_low = 0;
285
0
  int64_t dist_low = 0;
286
0
  if (abs_qc > 1) {
287
0
    const int dqv = get_dqv(ctx->dequant, ci, ctx->iqmatrix);
288
0
    get_qc_dqc_low(abs_qc, sign, dqv, shift, &qc_low, &dqc_low);
289
0
    abs_qc_low = abs_qc - 1;
290
0
    if (qmatrix == NULL) {
291
0
      const int64_t tqc2 = (int64_t)tqc * 2;
292
0
      dist_low = ((int64_t)dqc_low * (dqc_low - tqc2)) * (1LL << (2 * shift));
293
0
    } else {
294
0
      dist_low = get_coeff_dist_qm(tqc, dqc_low, shift, qm_val) - dist0;
295
0
    }
296
0
  }
297
298
0
  const int64_t rdmult = ctx->rdmult;
299
0
  int accu_rate = ctx->accu_rate;
300
0
  int64_t accu_dist = ctx->accu_dist;
301
0
  int64_t rd = RDCOST(rdmult, accu_rate + rate, accu_dist + dist);
302
0
  int lower_level = 0;
303
304
0
  if (allow_lower_qc) {
305
0
    int rate_low;
306
0
    if (abs_qc == 1) {
307
0
      rate_low = base_cost[0];
308
0
    } else {
309
0
      rate_low = base_cost[AOMMIN(abs_qc_low, 3)] + sign_cost;
310
0
      if (abs_qc_low > NUM_BASE_LEVELS) {
311
0
        rate_low += get_br_cost(abs_qc_low, lps_cost);
312
0
      }
313
0
    }
314
0
    const int64_t rd_low =
315
0
        RDCOST(rdmult, accu_rate + rate_low, accu_dist + dist_low);
316
0
    if (rd_low < rd) {
317
0
      lower_level = 1;
318
0
      rd = rd_low;
319
0
      rate = rate_low;
320
0
      dist = dist_low;
321
0
    }
322
0
  }
323
324
0
  if (allow_new_eob) {
325
0
    int lower_level_new_eob = 0;
326
0
    const int coeff_ctx_new_eob = get_lower_levels_ctx_eob(bhl, ctx->width, si);
327
0
    const int new_eob_cost =
328
0
        get_eob_cost(new_eob, ctx->txb_eob_costs, txb_costs, tx_class);
329
0
    const int *const base_eob_cost =
330
0
        txb_costs->base_eob_cost[coeff_ctx_new_eob];
331
0
    const int *lps_cost_eob = txb_costs->lps_cost[0];
332
0
    int rate_coeff_eob =
333
0
        new_eob_cost + base_eob_cost[AOMMIN(abs_qc, 3) - 1] + sign_cost;
334
0
    if (abs_qc > NUM_BASE_LEVELS) {
335
0
      const int br_ctx_eob = get_br_ctx_eob(ci, bhl, tx_class);
336
0
      lps_cost_eob = txb_costs->lps_cost[br_ctx_eob];
337
0
      rate_coeff_eob += get_br_cost(abs_qc, lps_cost_eob);
338
0
    }
339
0
    int64_t dist_new_eob = dist_orig;
340
0
    int64_t rd_new_eob = RDCOST(rdmult, rate_coeff_eob, dist_new_eob);
341
342
0
    if (abs_qc_low > 0) {
343
0
      int rate_coeff_eob_low =
344
0
          new_eob_cost + base_eob_cost[AOMMIN(abs_qc_low, 3) - 1] + sign_cost;
345
0
      if (abs_qc_low > NUM_BASE_LEVELS) {
346
0
        rate_coeff_eob_low += get_br_cost(abs_qc_low, lps_cost_eob);
347
0
      }
348
0
      const int64_t dist_new_eob_low = dist_low;
349
0
      const int64_t rd_new_eob_low =
350
0
          RDCOST(rdmult, rate_coeff_eob_low, dist_new_eob_low);
351
0
      if (rd_new_eob_low < rd_new_eob) {
352
0
        lower_level_new_eob = 1;
353
0
        rd_new_eob = rd_new_eob_low;
354
0
        rate_coeff_eob = rate_coeff_eob_low;
355
0
        dist_new_eob = dist_new_eob_low;
356
0
      }
357
0
    }
358
359
0
    if (rd_new_eob < rd) {
360
0
      tran_low_t *qcoeff = ctx->qcoeff;
361
0
      tran_low_t *dqcoeff = ctx->dqcoeff;
362
0
      const int *nz_ci = ctx->nz_ci;
363
0
      const int nz_num = ctx->nz_num;
364
0
      for (int ni = 0; ni < nz_num; ++ni) {
365
0
        int last_ci = nz_ci[ni];
366
0
        levels[get_padded_idx(last_ci, bhl)] = 0;
367
0
        qcoeff[last_ci] = 0;
368
0
        dqcoeff[last_ci] = 0;
369
0
      }
370
0
      ctx->eob = new_eob;
371
0
      ctx->nz_num = 0;
372
0
      accu_rate = rate_coeff_eob;
373
0
      ctx->accu_dist = dist_new_eob;
374
0
      lower_level = lower_level_new_eob;
375
0
    } else {
376
0
      accu_rate += rate;
377
0
      ctx->accu_dist = accu_dist + dist;
378
0
    }
379
0
  } else {
380
0
    accu_rate += rate;
381
0
    ctx->accu_dist = accu_dist + dist;
382
0
  }
383
384
0
  tran_low_t *qcoeff = ctx->qcoeff;
385
0
  if (lower_level) {
386
0
    qcoeff[ci] = qc_low;
387
0
    ctx->dqcoeff[ci] = dqc_low;
388
0
    levels[get_padded_idx(ci, bhl)] = AOMMIN(abs_qc_low, INT8_MAX);
389
0
  }
390
0
  if (qcoeff[ci]) {
391
0
    ctx->nz_ci[ctx->nz_num] = ci;
392
0
    ++ctx->nz_num;
393
0
  }
394
0
  return accu_rate;
395
0
}
396
397
static inline void update_skip(int *accu_rate, int64_t accu_dist, int *eob,
398
                               int nz_num, int *nz_ci, int64_t rdmult,
399
                               int skip_cost, int non_skip_cost,
400
0
                               tran_low_t *qcoeff, tran_low_t *dqcoeff) {
401
0
  const int64_t rd = RDCOST(rdmult, *accu_rate + non_skip_cost, accu_dist);
402
0
  const int64_t rd_new_eob = RDCOST(rdmult, skip_cost, 0);
403
0
  if (rd_new_eob < rd) {
404
0
    for (int i = 0; i < nz_num; ++i) {
405
0
      const int ci = nz_ci[i];
406
0
      qcoeff[ci] = 0;
407
0
      dqcoeff[ci] = 0;
408
      // no need to set up levels because this is the last step
409
      // levels[get_padded_idx(ci, bhl)] = 0;
410
0
    }
411
0
    *accu_rate = 0;
412
0
    *eob = 0;
413
0
  }
414
0
}
415
416
// TODO(angiebird): use this function whenever it's possible
417
static inline int get_tx_type_cost(const MACROBLOCK *x, const MACROBLOCKD *xd,
418
                                   int plane, TX_SIZE tx_size, TX_TYPE tx_type,
419
0
                                   int reduced_tx_set_used) {
420
0
  if (plane > 0) return 0;
421
422
0
  const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
423
424
0
  const MB_MODE_INFO *mbmi = xd->mi[0];
425
0
  const int is_inter = is_inter_block(mbmi);
426
0
  const TxSetType set_type =
427
0
      av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used);
428
0
  if (av1_num_ext_tx_set[set_type] > 1 &&
429
0
      !xd->lossless[xd->mi[0]->segment_id]) {
430
0
    const int ext_tx_set = ext_tx_set_index[is_inter][set_type];
431
0
    if (is_inter) {
432
0
      if (ext_tx_set > 0)
433
0
        return x->mode_costs
434
0
            .inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type];
435
0
    } else {
436
0
      if (ext_tx_set > 0) {
437
0
        PREDICTION_MODE intra_dir;
438
0
        if (mbmi->filter_intra_mode_info.use_filter_intra)
439
0
          intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
440
0
                                             .filter_intra_mode];
441
0
        else
442
0
          intra_dir = mbmi->mode;
443
0
        return x->mode_costs.intra_tx_type_costs[ext_tx_set][square_tx_size]
444
0
                                                [intra_dir][tx_type];
445
0
      }
446
0
    }
447
0
  }
448
0
  return 0;
449
0
}
450
451
static AOM_FORCE_INLINE void update_coeff_eob_facade(
452
    int *accu_rate, int64_t *accu_dist, int *eob, int *nz_num, int *nz_ci,
453
    int *si, TX_SIZE tx_size, TX_CLASS tx_class, int bhl, int width,
454
    int dc_sign_ctx, int64_t rdmult, int shift, const int16_t *dequant,
455
    const int16_t *__restrict scan,
456
    const LV_MAP_EOB_COST *__restrict txb_eob_costs,
457
    const LV_MAP_COEFF_COST *__restrict txb_costs,
458
    const tran_low_t *__restrict tcoeff, tran_low_t *__restrict qcoeff,
459
    tran_low_t *__restrict dqcoeff, uint8_t *__restrict levels, int sharpness,
460
    const qm_val_t *iqmatrix, const qm_val_t *qmatrix, int max_nz_num,
461
0
    int is_noise_pattern) {
462
0
  int s = *si;
463
0
  if (s < 0) return;
464
0
  int local_rate = *accu_rate;
465
0
  TxbEobCtx ctx = {
466
0
    rdmult, *accu_dist, txb_eob_costs,   txb_costs, dequant, tcoeff,
467
0
    qcoeff, dqcoeff,    levels,          iqmatrix,  qmatrix, nz_ci,
468
0
    *eob,   *nz_num,    local_rate,      bhl,       width,   dc_sign_ctx,
469
0
    shift,  sharpness,  is_noise_pattern
470
0
  };
471
0
  const intptr_t stride = (1 << bhl) + TX_PAD_HOR;
472
0
  const int8_t *const nz_map_ctx =
473
0
      (tx_class == TX_CLASS_2D) ? av1_nz_map_ctx_offset[tx_size] : NULL;
474
0
  const int (*base_cost)[8] = txb_costs->base_cost;
475
0
  while (s >= 0 && ctx.nz_num <= max_nz_num) {
476
0
    int ci;
477
0
    tran_low_t qc;
478
0
    int coeff_ctx;
479
0
    do {
480
0
      ci = scan[s];
481
0
      coeff_ctx = get_lower_levels_ctx_fast(levels, ci, bhl, stride, nz_map_ctx,
482
0
                                            tx_class);
483
0
      qc = qcoeff[ci];
484
0
      if (qc != 0) break;
485
0
      local_rate += base_cost[coeff_ctx][0];
486
0
    } while (--s >= 0);
487
0
    if (s < 0) break;
488
0
    ctx.accu_rate = local_rate;
489
0
    local_rate = update_coeff_eob_nz(s, ci, qc, coeff_ctx, tx_class, &ctx);
490
0
    --s;
491
0
  }
492
0
  *accu_rate = local_rate;
493
0
  *accu_dist = ctx.accu_dist;
494
0
  *eob = ctx.eob;
495
0
  *nz_num = ctx.nz_num;
496
0
  *si = s;
497
0
}
498
499
static AOM_FORCE_INLINE void update_coeff_simple_facade(
500
    int *accu_rate, int *si, int eob, TX_SIZE tx_size, TX_CLASS tx_class,
501
    int bhl, int64_t rdmult, int shift, const int16_t *dequant,
502
    const int16_t *__restrict scan,
503
    const LV_MAP_COEFF_COST *__restrict txb_costs,
504
    const tran_low_t *__restrict tcoeff, tran_low_t *__restrict qcoeff,
505
    tran_low_t *__restrict dqcoeff, uint8_t *__restrict levels, int sharpness,
506
0
    const qm_val_t *iqmatrix, const qm_val_t *qmatrix) {
507
0
  (void)eob;
508
0
  int s = *si;
509
0
  if (s < 1) return;
510
0
  int local_rate = *accu_rate;
511
0
  const TxbSimpleCtx ctx = { rdmult,  txb_costs, tcoeff,     qcoeff,
512
0
                             dqcoeff, levels,    iqmatrix,   qmatrix,
513
0
                             bhl,     shift,     dequant[1], sharpness };
514
0
  const intptr_t stride = (1 << bhl) + TX_PAD_HOR;
515
0
  const int8_t *const nz_map_ctx =
516
0
      (tx_class == TX_CLASS_2D) ? av1_nz_map_ctx_offset[tx_size] : NULL;
517
0
  const int (*base_cost)[8] = txb_costs->base_cost;
518
0
  while (s >= 1) {
519
0
    int ci;
520
0
    tran_low_t qc;
521
0
    int coeff_ctx;
522
0
    do {
523
0
      ci = scan[s];
524
0
      assert(ci > 0);
525
0
      coeff_ctx = get_lower_levels_ctx_simple_fast(levels, ci, bhl, stride,
526
0
                                                   nz_map_ctx, tx_class);
527
0
      qc = qcoeff[ci];
528
0
      if (qc != 0) break;
529
0
      local_rate += base_cost[coeff_ctx][0];
530
0
    } while (--s >= 1);
531
0
    if (s < 1) break;
532
0
    if (sharpness != 0 && (unsigned int)(qc + 1) <= 2) {
533
0
      local_rate += base_cost[coeff_ctx][1] + av1_cost_literal(1);
534
0
      --s;
535
0
      continue;
536
0
    }
537
0
    local_rate += update_coeff_simple_nz(ci, qc, coeff_ctx, tx_class, &ctx);
538
0
    --s;
539
0
  }
540
0
  *accu_rate = local_rate;
541
0
  *si = 0;
542
0
}
543
544
int av1_optimize_txb(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
545
                     int block, TX_SIZE tx_size, TX_TYPE tx_type,
546
                     const TXB_CTX *const txb_ctx, int *rate_cost,
547
0
                     int sharpness) {
548
0
  MACROBLOCKD *xd = &x->e_mbd;
549
0
  const struct macroblock_plane *p = &x->plane[plane];
550
0
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
551
0
  const int16_t *scan = scan_order->scan;
552
0
  const int shift = av1_get_tx_scale(tx_size);
553
0
  int eob = p->eobs[block];
554
0
  const int16_t *dequant = p->dequant_QTX;
555
0
  const qm_val_t *iqmatrix =
556
0
      tx_type < IDTX
557
0
          ? xd->plane[plane].seg_iqmatrix[xd->mi[0]->segment_id][tx_size]
558
0
          : NULL;
559
0
  const qm_val_t *qmatrix =
560
0
      (cpi->oxcf.tune_cfg.dist_metric == AOM_DIST_METRIC_QM_PSNR &&
561
0
       tx_type < IDTX)
562
0
          ? xd->plane[plane].seg_qmatrix[xd->mi[0]->segment_id][tx_size]
563
0
          : NULL;
564
0
  const int block_offset = BLOCK_OFFSET(block);
565
0
  tran_low_t *qcoeff = p->qcoeff + block_offset;
566
0
  tran_low_t *dqcoeff = p->dqcoeff + block_offset;
567
0
  const tran_low_t *tcoeff = p->coeff + block_offset;
568
0
  const CoeffCosts *coeff_costs = &x->coeff_costs;
569
570
  // This function is not called if eob = 0.
571
0
  assert(eob > 0);
572
573
0
  const AV1_COMMON *cm = &cpi->common;
574
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
575
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
576
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
577
0
  const MB_MODE_INFO *mbmi = xd->mi[0];
578
0
  const int bhl = get_txb_bhl(tx_size);
579
0
  const int width = get_txb_wide(tx_size);
580
0
  const int height = get_txb_high(tx_size);
581
0
  assert(height == (1 << bhl));
582
0
  const int is_inter = is_inter_block(mbmi);
583
0
  const LV_MAP_COEFF_COST *txb_costs =
584
0
      &coeff_costs->coeff_costs[txs_ctx][plane_type];
585
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
586
0
  const LV_MAP_EOB_COST *txb_eob_costs =
587
0
      &coeff_costs->eob_costs[eob_multi_size][plane_type];
588
589
0
  int is_noise_pattern = 0;
590
0
#if CONFIG_AV1_HIGHBITDEPTH
591
0
  if (xd->bd > 8 && sharpness == 3 && plane == AOM_PLANE_Y) {
592
0
    int64_t src_var, rec_var;
593
0
    av1_get_variance_stats(x, &src_var, &rec_var);
594
0
    if (src_var > rec_var) {
595
0
      const int num_pixels =
596
0
          block_size_wide[mbmi->bsize] * block_size_high[mbmi->bsize];
597
0
      const int64_t src_var_per_px = src_var / num_pixels;
598
0
      if (src_var_per_px < 64) {
599
0
        is_noise_pattern = 1;
600
0
      }
601
0
    }
602
0
  }
603
0
#endif
604
605
  // For the IQ and SSIMULACRA 2 tunings, increase rshift from 2 to 4.
606
  // This biases trellis quantization towards keeping more coefficients, and
607
  // together with the IQ and SSIMULACRA2 rdmult adjustment in
608
  // av1_compute_rd_mult_based_on_qindex(), this helps preserve image
609
  // features (like repeating patterns and camera noise/film grain), which
610
  // improves SSIMULACRA 2 scores.
611
0
  const int rshift = (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_IQ ||
612
0
                      cpi->oxcf.tune_cfg.tuning == AOM_TUNE_SSIMULACRA2)
613
0
                         ? 7
614
0
                         : (is_noise_pattern ? 6 : 5);
615
616
0
  const int (*trellis_rd_mult)[2] = cpi->sf.tx_sf.use_chroma_trellis_rd_mult
617
0
                                        ? plane_rd_mult_chroma
618
0
                                        : plane_rd_mult;
619
0
  const int64_t rdmult = ROUND_POWER_OF_TWO(
620
0
      (int64_t)x->rdmult * (8 - sharpness) *
621
0
          (trellis_rd_mult[is_inter][plane_type] << (2 * (xd->bd - 8))),
622
0
      rshift);
623
624
0
  uint8_t levels_buf[TX_PAD_2D];
625
0
  uint8_t *const levels = set_levels(levels_buf, height);
626
627
0
  if (eob > 1) av1_txb_init_levels(qcoeff, width, height, levels);
628
629
  // TODO(angirbird): check iqmatrix
630
631
0
  const int non_skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][0];
632
0
  const int skip_cost = txb_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
633
0
  const int eob_cost = get_eob_cost(eob, txb_eob_costs, txb_costs, tx_class);
634
0
  int accu_rate = eob_cost;
635
0
  int64_t accu_dist = 0;
636
0
  int si = eob - 1;
637
0
  const int ci = scan[si];
638
0
  const tran_low_t qc = qcoeff[ci];
639
0
  const tran_low_t abs_qc = abs(qc);
640
0
  const int sign = qc < 0;
641
0
  const int max_nz_num = 2;
642
0
  int nz_num = 1;
643
0
  int nz_ci[3] = { ci, 0, 0 };
644
0
  if (abs_qc >= 2) {
645
0
    update_coeff_general(&accu_rate, &accu_dist, si, eob, tx_size, tx_class,
646
0
                         bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
647
0
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
648
0
                         levels, iqmatrix, qmatrix);
649
0
    --si;
650
0
  } else {
651
0
    assert(abs_qc == 1);
652
0
    const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, si);
653
0
    accu_rate +=
654
0
        get_coeff_cost_eob(ci, abs_qc, sign, coeff_ctx, txb_ctx->dc_sign_ctx,
655
0
                           txb_costs, bhl, tx_class);
656
0
    const tran_low_t tqc = tcoeff[ci];
657
0
    const tran_low_t dqc = dqcoeff[ci];
658
0
    if (qmatrix == NULL) {
659
0
      accu_dist +=
660
0
          ((int64_t)dqc * (dqc - (int64_t)tqc * 2)) * (1LL << (2 * shift));
661
0
    } else {
662
0
      const int64_t dist = get_coeff_dist(tqc, dqc, shift, qmatrix, ci);
663
0
      const int64_t dist0 = get_coeff_dist(tqc, 0, shift, qmatrix, ci);
664
0
      accu_dist += dist - dist0;
665
0
    }
666
0
    --si;
667
0
  }
668
669
0
#define UPDATE_COEFF_EOB_CASE(tx_class_literal)                              \
670
0
  case tx_class_literal:                                                     \
671
0
    update_coeff_eob_facade(                                                 \
672
0
        &accu_rate, &accu_dist, &eob, &nz_num, nz_ci, &si, tx_size,          \
673
0
        tx_class_literal, bhl, width, txb_ctx->dc_sign_ctx, rdmult, shift,   \
674
0
        dequant, scan, txb_eob_costs, txb_costs, tcoeff, qcoeff, dqcoeff,    \
675
0
        levels, sharpness, iqmatrix, qmatrix, max_nz_num, is_noise_pattern); \
676
0
    break
677
0
  if (si >= 0) {
678
0
    switch (tx_class) {
679
0
      UPDATE_COEFF_EOB_CASE(TX_CLASS_2D);
680
0
      UPDATE_COEFF_EOB_CASE(TX_CLASS_HORIZ);
681
0
      UPDATE_COEFF_EOB_CASE(TX_CLASS_VERT);
682
0
#undef UPDATE_COEFF_EOB_CASE
683
0
      default: assert(false);
684
0
    }
685
0
  }
686
687
0
  if (si == -1 && nz_num <= max_nz_num && sharpness == 0) {
688
0
    update_skip(&accu_rate, accu_dist, &eob, nz_num, nz_ci, rdmult, skip_cost,
689
0
                non_skip_cost, qcoeff, dqcoeff);
690
0
  } else if (si >= 1) {
691
0
#define UPDATE_COEFF_SIMPLE_CASE(tx_class_literal)                            \
692
0
  case tx_class_literal:                                                      \
693
0
    update_coeff_simple_facade(&accu_rate, &si, eob, tx_size,                 \
694
0
                               tx_class_literal, bhl, rdmult, shift, dequant, \
695
0
                               scan, txb_costs, tcoeff, qcoeff, dqcoeff,      \
696
0
                               levels, sharpness, iqmatrix, qmatrix);         \
697
0
    break
698
0
    switch (tx_class) {
699
0
      UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_2D);
700
0
      UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_HORIZ);
701
0
      UPDATE_COEFF_SIMPLE_CASE(TX_CLASS_VERT);
702
0
#undef UPDATE_COEFF_SIMPLE_CASE
703
0
      default: assert(false);
704
0
    }
705
0
  }
706
707
  // DC position
708
0
  if (si == 0) {
709
    // no need to update accu_dist because it's not used after this point
710
0
    int64_t dummy_dist = 0;
711
0
    update_coeff_general(&accu_rate, &dummy_dist, si, eob, tx_size, tx_class,
712
0
                         bhl, width, rdmult, shift, txb_ctx->dc_sign_ctx,
713
0
                         dequant, scan, txb_costs, tcoeff, qcoeff, dqcoeff,
714
0
                         levels, iqmatrix, qmatrix);
715
0
  }
716
717
0
  p->eobs[block] = eob;
718
0
  if (eob == 0) {
719
0
    accu_rate += skip_cost;
720
0
    p->txb_entropy_ctx[block] = 0;
721
0
  } else {
722
0
    const int tx_type_cost = get_tx_type_cost(x, xd, plane, tx_size, tx_type,
723
0
                                              cm->features.reduced_tx_set_used);
724
0
    accu_rate += non_skip_cost + tx_type_cost;
725
0
    p->txb_entropy_ctx[block] =
726
0
        av1_get_txb_entropy_context(qcoeff, scan_order, eob);
727
0
  }
728
729
0
  *rate_cost = accu_rate;
730
0
  return eob;
731
0
}
732
733
static AOM_FORCE_INLINE int warehouse_efficients_txb_loop(
734
    const tran_low_t *const __restrict qcoeff,
735
    const int16_t *const __restrict scan, const int eob,
736
    const uint8_t *const __restrict levels, const TX_SIZE tx_size,
737
    const int bhl, const int width, const TXB_CTX *const txb_ctx,
738
    const LV_MAP_COEFF_COST *const coeff_costs, const TX_CLASS tx_class,
739
0
    int cost) {
740
0
  const int (*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] =
741
0
      coeff_costs->lps_cost;
742
0
  const int (*base_cost)[8] = coeff_costs->base_cost;
743
0
  int c = eob - 1;
744
0
  {
745
0
    const int pos = scan[c];
746
0
    const tran_low_t v = qcoeff[pos];
747
748
0
    if (v) {
749
0
      const int sign = AOMSIGN(v);
750
0
      const int level = (v ^ sign) - sign;
751
0
      const int coeff_ctx = get_lower_levels_ctx_eob(bhl, width, c);
752
0
      cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
753
0
      if (level > NUM_BASE_LEVELS) {
754
0
        const int ctx = get_br_ctx_eob(pos, bhl, tx_class);
755
0
        cost += get_br_cost(level, lps_cost[ctx]);
756
0
      }
757
0
      cost += av1_cost_literal(1);
758
0
    }
759
0
  }
760
761
0
  if (eob <= ((width << bhl) >> 4) + 3) {
762
0
    const intptr_t stride = (1 << bhl) + TX_PAD_HOR;
763
0
    const int8_t *const nz_map_ctx =
764
0
        (tx_class == TX_CLASS_2D) ? av1_nz_map_ctx_offset[tx_size] : NULL;
765
0
    for (c = eob - 2; c >= 1; --c) {
766
0
      const int pos = scan[c];
767
0
      const int coeff_ctx = get_lower_levels_ctx_simple_fast(
768
0
          levels, pos, bhl, stride, nz_map_ctx, tx_class);
769
0
      const int level = abs(qcoeff[pos]);
770
0
      if (level <= NUM_BASE_LEVELS) {
771
0
        cost +=
772
0
            base_cost[coeff_ctx][level] + ((level > 0) << AV1_PROB_COST_SHIFT);
773
0
      } else {
774
0
        cost += base_cost[coeff_ctx][3] + av1_cost_literal(1);
775
0
        const int ctx = get_br_ctx_simple(levels, pos, bhl, tx_class);
776
0
        cost += get_br_cost(level, lps_cost[ctx]);
777
0
      }
778
0
    }
779
0
    const tran_low_t v = qcoeff[0];
780
0
    const int coeff_ctx =
781
0
        (tx_class == TX_CLASS_2D)
782
0
            ? 0
783
0
            : get_lower_levels_ctx(levels, 0, bhl, tx_size, tx_class);
784
0
    if (!v) {
785
0
      cost += base_cost[coeff_ctx][0];
786
0
    } else {
787
0
      const int sign = AOMSIGN(v);
788
0
      const int level = (v ^ sign) - sign;
789
0
      cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
790
0
      const int sign01 = -sign;
791
0
      cost += coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign01];
792
0
      if (level > NUM_BASE_LEVELS) {
793
0
        const int ctx = get_br_ctx(levels, 0, bhl, tx_class);
794
0
        cost += get_br_cost(level, lps_cost[ctx]);
795
0
      }
796
0
    }
797
0
    return cost;
798
0
  }
799
800
0
  DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
801
0
  av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
802
0
  for (c = eob - 2; c >= 1; --c) {
803
0
    const int pos = scan[c];
804
0
    const int coeff_ctx = coeff_contexts[pos];
805
0
    const tran_low_t v = qcoeff[pos];
806
0
    int c_cost = base_cost[coeff_ctx][0];
807
0
    if (v) {
808
0
      const int level = abs(v);
809
0
      c_cost = base_cost[coeff_ctx][AOMMIN(level, 3)] + av1_cost_literal(1);
810
0
      if (level > NUM_BASE_LEVELS) {
811
0
        const int ctx = get_br_ctx_simple(levels, pos, bhl, tx_class);
812
0
        c_cost += get_br_cost(level, lps_cost[ctx]);
813
0
      }
814
0
    }
815
0
    cost += c_cost;
816
0
  }
817
  // c == 0 after previous loop
818
0
  {
819
0
    const int pos = scan[c];
820
0
    const tran_low_t v = qcoeff[pos];
821
0
    const int coeff_ctx = coeff_contexts[pos];
822
0
    if (!v) {
823
0
      cost += base_cost[coeff_ctx][0];
824
0
    } else {
825
0
      const int sign = AOMSIGN(v);
826
0
      const int level = (v ^ sign) - sign;
827
0
      cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
828
      // sign bit cost
829
0
      const int sign01 = -sign;
830
0
      cost += coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign01];
831
0
      if (level > NUM_BASE_LEVELS) {
832
0
        const int ctx = get_br_ctx(levels, pos, bhl, tx_class);
833
0
        cost += get_br_cost(level, lps_cost[ctx]);
834
0
      }
835
0
    }
836
0
  }
837
0
  return cost;
838
0
}
839
840
#if defined(__GNUC__) || defined(__clang__)
841
__attribute__((noinline))
842
#endif
843
static int warehouse_efficients_txb_multi(
844
    const tran_low_t *const qcoeff, const TX_SIZE tx_size,
845
    const TXB_CTX *const txb_ctx, const int eob,
846
    const LV_MAP_COEFF_COST *const coeff_costs, const TX_TYPE tx_type,
847
0
    const TX_CLASS tx_class, int cost) {
848
0
  const int bhl = get_txb_bhl(tx_size);
849
0
  const int width = get_txb_wide(tx_size);
850
0
  const int height = get_txb_high(tx_size);
851
0
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
852
0
  const int16_t *const scan = scan_order->scan;
853
0
  uint8_t levels_buf[TX_PAD_2D];
854
0
  uint8_t *const levels = set_levels(levels_buf, height);
855
856
0
  av1_txb_init_levels(qcoeff, width, height, levels);
857
858
0
  switch (tx_class) {
859
0
    case TX_CLASS_2D:
860
0
      return warehouse_efficients_txb_loop(qcoeff, scan, eob, levels, tx_size,
861
0
                                           bhl, width, txb_ctx, coeff_costs,
862
0
                                           TX_CLASS_2D, cost);
863
0
    case TX_CLASS_VERT:
864
0
      return warehouse_efficients_txb_loop(qcoeff, scan, eob, levels, tx_size,
865
0
                                           bhl, width, txb_ctx, coeff_costs,
866
0
                                           TX_CLASS_VERT, cost);
867
0
    case TX_CLASS_HORIZ:
868
0
    default:
869
0
      return warehouse_efficients_txb_loop(qcoeff, scan, eob, levels, tx_size,
870
0
                                           bhl, width, txb_ctx, coeff_costs,
871
0
                                           TX_CLASS_HORIZ, cost);
872
0
  }
873
0
}
874
875
/*!\brief Estimate the entropy cost of transform coefficients using Laplacian
876
 * distribution.
877
 *
878
 * \ingroup coefficient_coding
879
 *
880
 * This function assumes each transform coefficient is of its own Laplacian
881
 * distribution and the coefficient is the only observation of the Laplacian
882
 * distribution.
883
 *
884
 * Based on that, each coefficient's coding cost can be estimated by computing
885
 * the entropy of the corresponding Laplacian distribution.
886
 *
887
 * This function then return the sum of the estimated entropy cost for all
888
 * coefficients in the transform block.
889
 *
890
 * Note that the entropy cost of end of block (eob) and transform type (tx_type)
891
 * are not included.
892
 *
893
 * \param[in]    x              Pointer to structure holding the data for the
894
                                current encoding macroblock
895
 * \param[in]    plane          The index of the current plane
896
 * \param[in]    block          The index of the current transform block in the
897
 * macroblock. It's defined by number of 4x4 units that have been coded before
898
 * the currernt transform block
899
 * \param[in]    tx_size        The transform size
900
 * \param[in]    tx_type        The transform type
901
 * \return       int            Estimated entropy cost of coefficients in the
902
 * transform block.
903
 */
904
static int av1_cost_coeffs_txb_estimate(const MACROBLOCK *x, const int plane,
905
                                        const int block, const TX_SIZE tx_size,
906
0
                                        const TX_TYPE tx_type) {
907
0
  assert(plane == 0);
908
909
0
  int cost = 0;
910
0
  const struct macroblock_plane *p = &x->plane[plane];
911
0
  const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
912
0
  const int16_t *scan = scan_order->scan;
913
0
  tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
914
915
0
  int eob = p->eobs[block];
916
917
  // coeffs
918
0
  int c = eob - 1;
919
  // eob
920
0
  {
921
0
    const int pos = scan[c];
922
0
    const tran_low_t v = abs(qcoeff[pos]) - 1;
923
0
    cost += (v << (AV1_PROB_COST_SHIFT + 2));
924
0
  }
925
  // other coeffs
926
0
  for (c = eob - 2; c >= 0; c--) {
927
0
    const int pos = scan[c];
928
0
    const tran_low_t v = abs(qcoeff[pos]);
929
0
    const int idx = AOMMIN(v, 14);
930
931
0
    cost += costLUT[idx];
932
0
  }
933
934
  // const_term does not contain DC, and log(e) does not contain eob, so both
935
  // (eob-1)
936
0
  cost += (const_term + loge_par) * (eob - 1);
937
938
0
  return cost;
939
0
}
940
941
static AOM_FORCE_INLINE int warehouse_efficients_txb_laplacian(
942
    const MACROBLOCK *x, const int plane, const int block,
943
    const TX_SIZE tx_size, const TXB_CTX *const txb_ctx, const int eob,
944
    const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
945
    const MACROBLOCKD *const xd, const TX_TYPE tx_type, const TX_CLASS tx_class,
946
0
    int reduced_tx_set_used) {
947
0
  const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
948
949
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
950
0
  const LV_MAP_EOB_COST *const eob_costs =
951
0
      &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
952
0
  int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
953
954
0
  cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
955
956
0
  cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
957
958
0
  cost += av1_cost_coeffs_txb_estimate(x, plane, block, tx_size, tx_type);
959
0
  return cost;
960
0
}
961
962
int av1_cost_coeffs_txb(const MACROBLOCK *x, const int plane, const int block,
963
                        const TX_SIZE tx_size, const TX_TYPE tx_type,
964
0
                        const TXB_CTX *const txb_ctx, int reduced_tx_set_used) {
965
0
  const struct macroblock_plane *p = &x->plane[plane];
966
0
  const int eob = p->eobs[block];
967
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
968
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
969
0
  const LV_MAP_COEFF_COST *const coeff_costs =
970
0
      &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
971
0
  if (eob == 0) {
972
0
    return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
973
0
  }
974
975
0
  const MACROBLOCKD *const xd = &x->e_mbd;
976
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
977
0
  const tran_low_t *const __restrict qcoeff = p->qcoeff + BLOCK_OFFSET(block);
978
0
  const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
979
0
  const int eob_multi_size = txsize_log2_minus4[tx_size];
980
0
  const LV_MAP_EOB_COST *const eob_costs =
981
0
      &x->coeff_costs.eob_costs[eob_multi_size][plane_type];
982
0
  int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
983
0
  cost += get_tx_type_cost(x, xd, plane, tx_size, tx_type, reduced_tx_set_used);
984
0
  cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
985
986
0
  if (eob == 1) {
987
0
    const tran_low_t v = qcoeff[0];
988
0
    if (v) {
989
0
      const int sign = AOMSIGN(v);
990
0
      const int level = (v ^ sign) - sign;
991
0
      cost += coeff_costs->base_eob_cost[0][AOMMIN(level, 3) - 1];
992
0
      if (level > NUM_BASE_LEVELS) {
993
0
        cost += get_br_cost(level, coeff_costs->lps_cost[0]);
994
0
      }
995
0
      const int sign01 = -sign;
996
0
      cost += coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign01];
997
0
    }
998
0
    return cost;
999
0
  }
1000
1001
0
  if (eob == 2 && tx_class == TX_CLASS_2D) {
1002
0
    const int bhl = get_txb_bhl(tx_size);
1003
0
    const int width = get_txb_wide(tx_size);
1004
0
    const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
1005
0
    const int (*lps_cost)[COEFF_BASE_RANGE + 1 + COEFF_BASE_RANGE + 1] =
1006
0
        coeff_costs->lps_cost;
1007
0
    const int (*base_cost)[8] = coeff_costs->base_cost;
1008
0
    const int pos1 = scan_order->scan[1];
1009
0
    const tran_low_t v1 = qcoeff[pos1];
1010
0
    int level1 = 0;
1011
0
    if (v1) {
1012
0
      const int sign1 = AOMSIGN(v1);
1013
0
      level1 = (v1 ^ sign1) - sign1;
1014
0
      const int coeff_ctx1 = get_lower_levels_ctx_eob(bhl, width, 1);
1015
0
      cost += coeff_costs->base_eob_cost[coeff_ctx1][AOMMIN(level1, 3) - 1];
1016
0
      if (level1 > NUM_BASE_LEVELS) {
1017
0
        const int ctx1 = get_br_ctx_eob(pos1, bhl, TX_CLASS_2D);
1018
0
        cost += get_br_cost(level1, lps_cost[ctx1]);
1019
0
      }
1020
0
      cost += av1_cost_literal(1);
1021
0
    }
1022
0
    const tran_low_t v0 = qcoeff[0];
1023
0
    if (!v0) {
1024
0
      cost += base_cost[0][0];
1025
0
    } else {
1026
0
      const int sign0 = AOMSIGN(v0);
1027
0
      const int level0 = (v0 ^ sign0) - sign0;
1028
0
      cost += base_cost[0][AOMMIN(level0, 3)];
1029
0
      const int sign01 = -sign0;
1030
0
      cost += coeff_costs->dc_sign_cost[txb_ctx->dc_sign_ctx][sign01];
1031
0
      if (level0 > NUM_BASE_LEVELS) {
1032
0
        const int mag =
1033
0
            (pos1 == 1 || pos1 == (1 << bhl) || pos1 == (1 << bhl) + 1)
1034
0
                ? AOMMIN(level1, INT8_MAX)
1035
0
                : 0;
1036
0
        const int ctx0 = AOMMIN((mag + 1) >> 1, 6);
1037
0
        cost += get_br_cost(level0, lps_cost[ctx0]);
1038
0
      }
1039
0
    }
1040
0
    return cost;
1041
0
  }
1042
1043
0
  return warehouse_efficients_txb_multi(qcoeff, tx_size, txb_ctx, eob,
1044
0
                                        coeff_costs, tx_type, tx_class, cost);
1045
0
}
1046
1047
int av1_cost_coeffs_txb_laplacian(const MACROBLOCK *x, const int plane,
1048
                                  const int block, const TX_SIZE tx_size,
1049
                                  const TX_TYPE tx_type,
1050
                                  const TXB_CTX *const txb_ctx,
1051
                                  const int reduced_tx_set_used,
1052
0
                                  const int adjust_eob) {
1053
0
  const struct macroblock_plane *p = &x->plane[plane];
1054
0
  int eob = p->eobs[block];
1055
1056
0
  if (adjust_eob) {
1057
0
    const SCAN_ORDER *scan_order = get_scan(tx_size, tx_type);
1058
0
    const int16_t *scan = scan_order->scan;
1059
0
    tran_low_t *tcoeff = p->coeff + BLOCK_OFFSET(block);
1060
0
    tran_low_t *qcoeff = p->qcoeff + BLOCK_OFFSET(block);
1061
0
    tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
1062
0
    update_coeff_eob_fast(&eob, av1_get_tx_scale(tx_size), p->dequant_QTX, scan,
1063
0
                          tcoeff, qcoeff, dqcoeff);
1064
0
    p->eobs[block] = eob;
1065
0
  }
1066
1067
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
1068
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
1069
0
  const LV_MAP_COEFF_COST *const coeff_costs =
1070
0
      &x->coeff_costs.coeff_costs[txs_ctx][plane_type];
1071
0
  if (eob == 0) {
1072
0
    return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
1073
0
  }
1074
1075
0
  const MACROBLOCKD *const xd = &x->e_mbd;
1076
0
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
1077
1078
0
  return warehouse_efficients_txb_laplacian(
1079
0
      x, plane, block, tx_size, txb_ctx, eob, plane_type, coeff_costs, xd,
1080
0
      tx_type, tx_class, reduced_tx_set_used);
1081
0
}