Coverage Report

Created: 2025-06-22 08:04

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