Coverage Report

Created: 2026-08-13 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavif/ext/aom/av1/encoder/encodemb.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016, 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 "config/aom_config.h"
13
#include "config/av1_rtcd.h"
14
#include "config/aom_dsp_rtcd.h"
15
16
#include "aom_dsp/bitwriter.h"
17
#include "aom_dsp/quantize.h"
18
#include "aom_mem/aom_mem.h"
19
#include "aom_ports/mem.h"
20
21
#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
22
#include "aom_util/debug_util.h"
23
#endif  // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
24
25
#include "av1/common/cfl.h"
26
#include "av1/common/idct.h"
27
#include "av1/common/reconinter.h"
28
#include "av1/common/reconintra.h"
29
#include "av1/common/scan.h"
30
31
#include "av1/encoder/av1_quantize.h"
32
#include "av1/encoder/encodemb.h"
33
#include "av1/encoder/hybrid_fwd_txfm.h"
34
#include "av1/encoder/txb_rdopt.h"
35
#include "av1/encoder/rd.h"
36
#include "av1/encoder/rdopt.h"
37
38
void av1_subtract_block(BitDepthInfo bd_info, int rows, int cols, int16_t *diff,
39
                        ptrdiff_t diff_stride, const uint8_t *src8,
40
                        ptrdiff_t src_stride, const uint8_t *pred8,
41
475M
                        ptrdiff_t pred_stride) {
42
475M
  assert(rows >= 4 && cols >= 4);
43
475M
#if CONFIG_AV1_HIGHBITDEPTH
44
475M
  if (bd_info.use_highbitdepth_buf) {
45
111M
    aom_highbd_subtract_block(rows, cols, diff, diff_stride, src8, src_stride,
46
111M
                              pred8, pred_stride);
47
111M
    return;
48
111M
  }
49
364M
#endif
50
364M
  (void)bd_info;
51
364M
  aom_subtract_block(rows, cols, diff, diff_stride, src8, src_stride, pred8,
52
364M
                     pred_stride);
53
364M
}
54
55
void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize,
56
280M
                      int blk_col, int blk_row, TX_SIZE tx_size) {
57
280M
  MACROBLOCKD *const xd = &x->e_mbd;
58
280M
  const BitDepthInfo bd_info = get_bit_depth_info(xd);
59
280M
  struct macroblock_plane *const p = &x->plane[plane];
60
280M
  const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
61
280M
  const int diff_stride = block_size_wide[plane_bsize];
62
280M
  const int src_stride = p->src.stride;
63
280M
  const int dst_stride = pd->dst.stride;
64
280M
  const int tx1d_width = tx_size_wide[tx_size];
65
280M
  const int tx1d_height = tx_size_high[tx_size];
66
280M
  uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
67
280M
  uint8_t *src = &p->src.buf[(blk_row * src_stride + blk_col) << MI_SIZE_LOG2];
68
280M
  int16_t *src_diff =
69
280M
      &p->src_diff[(blk_row * diff_stride + blk_col) << MI_SIZE_LOG2];
70
280M
  av1_subtract_block(bd_info, tx1d_height, tx1d_width, src_diff, diff_stride,
71
280M
                     src, src_stride, dst, dst_stride);
72
280M
}
73
74
8.90M
void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE plane_bsize, int plane) {
75
8.90M
  struct macroblock_plane *const p = &x->plane[plane];
76
8.90M
  const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
77
8.90M
  assert(plane_bsize < BLOCK_SIZES_ALL);
78
8.90M
  const int bw = block_size_wide[plane_bsize];
79
8.90M
  const int bh = block_size_high[plane_bsize];
80
8.90M
  const MACROBLOCKD *xd = &x->e_mbd;
81
8.90M
  const BitDepthInfo bd_info = get_bit_depth_info(xd);
82
83
8.90M
  av1_subtract_block(bd_info, bh, bw, p->src_diff, bw, p->src.buf,
84
8.90M
                     p->src.stride, pd->dst.buf, pd->dst.stride);
85
8.90M
}
86
87
int av1_optimize_b(const struct AV1_COMP *cpi, MACROBLOCK *x, int plane,
88
                   int block, TX_SIZE tx_size, TX_TYPE tx_type,
89
231M
                   const TXB_CTX *const txb_ctx, int *rate_cost) {
90
231M
  MACROBLOCKD *const xd = &x->e_mbd;
91
231M
  struct macroblock_plane *const p = &x->plane[plane];
92
231M
  const int eob = p->eobs[block];
93
231M
  const int segment_id = xd->mi[0]->segment_id;
94
95
231M
  if (eob == 0 || !cpi->optimize_seg_arr[segment_id] ||
96
222M
      xd->lossless[segment_id]) {
97
9.30M
    *rate_cost = av1_cost_skip_txb(&x->coeff_costs, txb_ctx, plane, tx_size);
98
9.30M
    return eob;
99
9.30M
  }
100
101
222M
  return av1_optimize_txb(cpi, x, plane, block, tx_size, tx_type, txb_ctx,
102
222M
                          rate_cost, cpi->oxcf.algo_cfg.sharpness);
103
231M
}
104
105
// Hyper-parameters for dropout optimization, based on following logics.
106
// TODO(yjshen): These settings are tuned by experiments. They may still be
107
// optimized for better performance.
108
// (1) Coefficients which are large enough will ALWAYS be kept.
109
static const tran_low_t DROPOUT_COEFF_MAX = 2;  // Max dropout-able coefficient.
110
// (2) Continuous coefficients will ALWAYS be kept. Here rigorous continuity is
111
//     NOT required. For example, `5 0 0 0 7` is treated as two continuous
112
//     coefficients if three zeros do not fulfill the dropout condition.
113
static const int DROPOUT_CONTINUITY_MAX =
114
    2;  // Max dropout-able continuous coeff.
115
// (3) Dropout operation is NOT applicable to blocks with large or small
116
//     quantization index.
117
static const int DROPOUT_Q_MAX = 128;
118
static const int DROPOUT_Q_MIN = 16;
119
// (4) Recall that dropout optimization will forcibly set some quantized
120
//     coefficients to zero. The key logic on determining whether a coefficient
121
//     should be dropped is to check the number of continuous zeros before AND
122
//     after this coefficient. The exact number of zeros for judgement depends
123
//     on block size and quantization index. More concretely, block size
124
//     determines the base number of zeros, while quantization index determines
125
//     the multiplier. Intuitively, larger block requires more zeros and larger
126
//     quantization index also requires more zeros (more information is lost
127
//     when using larger quantization index).
128
static const int DROPOUT_BEFORE_BASE_MAX =
129
    32;  // Max base number for leading zeros.
130
static const int DROPOUT_BEFORE_BASE_MIN =
131
    16;  // Min base number for leading zeros.
132
static const int DROPOUT_AFTER_BASE_MAX =
133
    32;  // Max base number for trailing zeros.
134
static const int DROPOUT_AFTER_BASE_MIN =
135
    16;  // Min base number for trailing zeros.
136
static const int DROPOUT_MULTIPLIER_MAX =
137
    8;  // Max multiplier on number of zeros.
138
static const int DROPOUT_MULTIPLIER_MIN =
139
    2;  // Min multiplier on number of zeros.
140
static const int DROPOUT_MULTIPLIER_Q_BASE =
141
    32;  // Base Q to compute multiplier.
142
143
void av1_dropout_qcoeff(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
144
0
                        TX_TYPE tx_type, int qindex) {
145
0
  const int tx_width = tx_size_wide[tx_size];
146
0
  const int tx_height = tx_size_high[tx_size];
147
148
  // Early return if `qindex` is out of range.
149
0
  if (qindex > DROPOUT_Q_MAX || qindex < DROPOUT_Q_MIN) {
150
0
    return;
151
0
  }
152
153
  // Compute number of zeros used for dropout judgement.
154
0
  const int base_size = AOMMAX(tx_width, tx_height);
155
0
  const int multiplier = CLIP(qindex / DROPOUT_MULTIPLIER_Q_BASE,
156
0
                              DROPOUT_MULTIPLIER_MIN, DROPOUT_MULTIPLIER_MAX);
157
0
  const int dropout_num_before =
158
0
      multiplier *
159
0
      CLIP(base_size, DROPOUT_BEFORE_BASE_MIN, DROPOUT_BEFORE_BASE_MAX);
160
0
  const int dropout_num_after =
161
0
      multiplier *
162
0
      CLIP(base_size, DROPOUT_AFTER_BASE_MIN, DROPOUT_AFTER_BASE_MAX);
163
164
0
  av1_dropout_qcoeff_num(mb, plane, block, tx_size, tx_type, dropout_num_before,
165
0
                         dropout_num_after);
166
0
}
167
168
void av1_dropout_qcoeff_num(MACROBLOCK *mb, int plane, int block,
169
                            TX_SIZE tx_size, TX_TYPE tx_type,
170
0
                            int dropout_num_before, int dropout_num_after) {
171
0
  const struct macroblock_plane *const p = &mb->plane[plane];
172
0
  tran_low_t *const qcoeff = p->qcoeff + BLOCK_OFFSET(block);
173
0
  tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
174
0
  const int max_eob = av1_get_max_eob(tx_size);
175
0
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
176
177
  // Early return if there are not enough non-zero coefficients.
178
0
  if (p->eobs[block] == 0 || p->eobs[block] <= dropout_num_before ||
179
0
      max_eob <= dropout_num_before + dropout_num_after) {
180
0
    return;
181
0
  }
182
183
0
  int count_zeros_before = 0;
184
0
  int count_zeros_after = 0;
185
0
  int count_nonzeros = 0;
186
  // Index of the first non-zero coefficient after sufficient number of
187
  // continuous zeros. If equals to `-1`, it means number of leading zeros
188
  // hasn't reach `dropout_num_before`.
189
0
  int idx = -1;
190
0
  int eob = 0;  // New end of block.
191
192
0
  for (int i = 0; i < p->eobs[block]; ++i) {
193
0
    const int scan_idx = scan_order->scan[i];
194
0
    if (abs(qcoeff[scan_idx]) > DROPOUT_COEFF_MAX) {
195
      // Keep large coefficients.
196
0
      count_zeros_before = 0;
197
0
      count_zeros_after = 0;
198
0
      idx = -1;
199
0
      eob = i + 1;
200
0
    } else if (qcoeff[scan_idx] == 0) {  // Count zeros.
201
0
      if (idx == -1) {
202
0
        ++count_zeros_before;
203
0
      } else {
204
0
        ++count_zeros_after;
205
0
      }
206
0
    } else {  // Count non-zeros.
207
0
      if (count_zeros_before >= dropout_num_before) {
208
0
        idx = (idx == -1) ? i : idx;
209
0
        ++count_nonzeros;
210
0
      } else {
211
0
        count_zeros_before = 0;
212
0
        eob = i + 1;
213
0
      }
214
0
    }
215
216
    // Handle continuity.
217
0
    if (count_nonzeros > DROPOUT_CONTINUITY_MAX) {
218
0
      count_zeros_before = 0;
219
0
      count_zeros_after = 0;
220
0
      count_nonzeros = 0;
221
0
      idx = -1;
222
0
      eob = i + 1;
223
0
    }
224
225
    // Handle the trailing zeros after original end of block.
226
0
    if (idx != -1 && i == p->eobs[block] - 1) {
227
0
      count_zeros_after += (max_eob - p->eobs[block]);
228
0
    }
229
230
    // Set redundant coefficients to zeros if needed.
231
0
    if (count_zeros_after >= dropout_num_after) {
232
0
      for (int j = idx; j <= i; ++j) {
233
0
        qcoeff[scan_order->scan[j]] = 0;
234
0
        dqcoeff[scan_order->scan[j]] = 0;
235
0
      }
236
0
      count_zeros_before += (i - idx + 1);
237
0
      count_zeros_after = 0;
238
0
      count_nonzeros = 0;
239
0
    } else if (i == p->eobs[block] - 1) {
240
0
      eob = i + 1;
241
0
    }
242
0
  }
243
244
0
  if (eob != p->eobs[block]) {
245
0
    p->eobs[block] = eob;
246
0
    p->txb_entropy_ctx[block] =
247
0
        av1_get_txb_entropy_context(qcoeff, scan_order, eob);
248
0
  }
249
0
}
250
251
enum {
252
  QUANT_FUNC_LOWBD = 0,
253
  QUANT_FUNC_HIGHBD = 1,
254
  QUANT_FUNC_TYPES = 2
255
} UENUM1BYTE(QUANT_FUNC);
256
257
#if CONFIG_AV1_HIGHBITDEPTH
258
static AV1_QUANT_FACADE
259
    quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = {
260
      { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
261
      { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
262
      { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
263
      { NULL, NULL }
264
    };
265
#else
266
static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_TYPES] = {
267
  av1_quantize_fp_facade, av1_quantize_b_facade, av1_quantize_dc_facade, NULL
268
};
269
#endif
270
271
// Computes the transform for DC only blocks
272
void av1_xform_dc_only(MACROBLOCK *x, int plane, int block,
273
627k
                       TxfmParam *txfm_param, int64_t per_px_mean) {
274
627k
  assert(per_px_mean != INT64_MAX);
275
627k
  const struct macroblock_plane *const p = &x->plane[plane];
276
627k
  const int block_offset = BLOCK_OFFSET(block);
277
627k
  tran_low_t *const coeff = p->coeff + block_offset;
278
627k
  const int n_coeffs = av1_get_max_eob(txfm_param->tx_size);
279
627k
  memset(coeff, 0, sizeof(*coeff) * n_coeffs);
280
627k
  coeff[0] =
281
627k
      (tran_low_t)((per_px_mean * dc_coeff_scale[txfm_param->tx_size]) >> 12);
282
627k
}
283
284
void av1_xform_quant(MACROBLOCK *x, int plane, int block, int blk_row,
285
                     int blk_col, BLOCK_SIZE plane_bsize, TxfmParam *txfm_param,
286
95.6M
                     const QUANT_PARAM *qparam) {
287
95.6M
  av1_xform(x, plane, block, blk_row, blk_col, plane_bsize, txfm_param);
288
95.6M
  av1_quant(x, plane, block, txfm_param, qparam);
289
95.6M
}
290
291
void av1_xform(MACROBLOCK *x, int plane, int block, int blk_row, int blk_col,
292
411M
               BLOCK_SIZE plane_bsize, TxfmParam *txfm_param) {
293
411M
  const struct macroblock_plane *const p = &x->plane[plane];
294
411M
  const int block_offset = BLOCK_OFFSET(block);
295
411M
  tran_low_t *const coeff = p->coeff + block_offset;
296
411M
  const int diff_stride = block_size_wide[plane_bsize];
297
298
411M
  const int src_offset = (blk_row * diff_stride + blk_col);
299
411M
  const int16_t *src_diff = &p->src_diff[src_offset << MI_SIZE_LOG2];
300
301
411M
  av1_fwd_txfm(src_diff, coeff, diff_stride, txfm_param);
302
411M
}
303
304
void av1_quant(MACROBLOCK *x, int plane, int block, TxfmParam *txfm_param,
305
411M
               const QUANT_PARAM *qparam) {
306
411M
  const struct macroblock_plane *const p = &x->plane[plane];
307
411M
  const SCAN_ORDER *const scan_order =
308
411M
      get_scan(txfm_param->tx_size, txfm_param->tx_type);
309
411M
  const int block_offset = BLOCK_OFFSET(block);
310
411M
  tran_low_t *const coeff = p->coeff + block_offset;
311
411M
  tran_low_t *const qcoeff = p->qcoeff + block_offset;
312
411M
  tran_low_t *const dqcoeff = p->dqcoeff + block_offset;
313
411M
  uint16_t *const eob = &p->eobs[block];
314
315
412M
  if (qparam->xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
316
412M
    const int n_coeffs = av1_get_max_eob(txfm_param->tx_size);
317
412M
    if (LIKELY(!x->seg_skip_block)) {
318
412M
#if CONFIG_AV1_HIGHBITDEPTH
319
412M
      quant_func_list[qparam->xform_quant_idx][txfm_param->is_hbd](
320
412M
          coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, qparam);
321
#else
322
      quant_func_list[qparam->xform_quant_idx](
323
          coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, qparam);
324
#endif
325
18.4E
    } else {
326
18.4E
      av1_quantize_skip(n_coeffs, qcoeff, dqcoeff, eob);
327
18.4E
    }
328
412M
  }
329
  // use_optimize_b is true means av1_optimze_b will be called,
330
  // thus cannot update entropy ctx now (performed in optimize_b)
331
411M
  if (qparam->use_optimize_b) {
332
231M
    p->txb_entropy_ctx[block] = 0;
333
231M
  } else {
334
180M
    p->txb_entropy_ctx[block] =
335
180M
        av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
336
180M
  }
337
411M
}
338
339
void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size,
340
281M
                     TX_TYPE tx_type, TxfmParam *txfm_param) {
341
281M
  MACROBLOCKD *const xd = &x->e_mbd;
342
281M
  MB_MODE_INFO *const mbmi = xd->mi[0];
343
344
281M
  txfm_param->tx_type = tx_type;
345
281M
  txfm_param->tx_size = tx_size;
346
281M
  txfm_param->lossless = xd->lossless[mbmi->segment_id];
347
281M
  txfm_param->tx_set_type = av1_get_ext_tx_set_type(
348
281M
      tx_size, is_inter_block(mbmi), cm->features.reduced_tx_set_used);
349
350
281M
  txfm_param->bd = xd->bd;
351
281M
  txfm_param->is_hbd = is_cur_buf_hbd(xd);
352
281M
}
353
void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx,
354
304M
                     int use_quant_b_adapt, QUANT_PARAM *qparam) {
355
304M
  qparam->log_scale = av1_get_tx_scale(tx_size);
356
304M
  qparam->tx_size = tx_size;
357
358
304M
  qparam->use_quant_b_adapt = use_quant_b_adapt;
359
360
  // TODO(bohanli): optimize_b and quantization idx has relationship,
361
  // but is kind of buried and complicated in different encoding stages.
362
  // Should have a unified function to derive quant_idx, rather than
363
  // determine and pass in the quant_idx
364
304M
  qparam->use_optimize_b = use_optimize_b;
365
304M
  qparam->xform_quant_idx = xform_quant_idx;
366
367
304M
  qparam->qmatrix = NULL;
368
304M
  qparam->iqmatrix = NULL;
369
304M
}
370
void av1_setup_qmatrix(const CommonQuantParams *quant_params,
371
                       const MACROBLOCKD *xd, int plane, TX_SIZE tx_size,
372
238M
                       TX_TYPE tx_type, QUANT_PARAM *qparam) {
373
238M
  qparam->qmatrix = av1_get_qmatrix(quant_params, xd, plane, tx_size, tx_type);
374
238M
  qparam->iqmatrix =
375
238M
      av1_get_iqmatrix(quant_params, xd, plane, tx_size, tx_type);
376
238M
}
377
378
static void encode_block(int plane, int block, int blk_row, int blk_col,
379
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg,
380
1.98M
                         RUN_TYPE dry_run) {
381
1.98M
  (void)dry_run;
382
1.98M
  struct encode_b_args *const args = arg;
383
1.98M
  const AV1_COMP *const cpi = args->cpi;
384
1.98M
  const AV1_COMMON *const cm = &cpi->common;
385
1.98M
  MACROBLOCK *const x = args->x;
386
1.98M
  MACROBLOCKD *const xd = &x->e_mbd;
387
1.98M
  MB_MODE_INFO *mbmi = xd->mi[0];
388
1.98M
  struct macroblock_plane *const p = &x->plane[plane];
389
1.98M
  struct macroblockd_plane *const pd = &xd->plane[plane];
390
1.98M
  tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
391
1.98M
  uint8_t *dst;
392
1.98M
  ENTROPY_CONTEXT *a, *l;
393
1.98M
  int dummy_rate_cost = 0;
394
395
1.98M
  dst = &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
396
397
1.98M
  a = &args->ta[blk_col];
398
1.98M
  l = &args->tl[blk_row];
399
400
1.98M
  TX_TYPE tx_type = DCT_DCT;
401
1.98M
  if (!mbmi->skip_mode) {
402
1.98M
    tx_type = av1_get_tx_type(xd, pd->plane_type, blk_row, blk_col, tx_size,
403
1.98M
                              cm->features.reduced_tx_set_used);
404
1.98M
    TxfmParam txfm_param;
405
1.98M
    QUANT_PARAM quant_param;
406
1.98M
    const int use_trellis = is_trellis_used(args->enable_optimize_b, dry_run);
407
1.98M
    int quant_idx;
408
1.98M
    if (use_trellis)
409
1.13M
      quant_idx = AV1_XFORM_QUANT_FP;
410
847k
    else
411
847k
      quant_idx =
412
18.4E
          USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
413
1.98M
    av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
414
1.98M
    av1_setup_quant(tx_size, use_trellis, quant_idx,
415
1.98M
                    cpi->oxcf.q_cfg.quant_b_adapt, &quant_param);
416
1.98M
    av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type,
417
1.98M
                      &quant_param);
418
1.98M
    av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
419
1.98M
                    &quant_param);
420
1.98M
    if (use_trellis) {
421
1.13M
      TXB_CTX txb_ctx;
422
1.13M
      get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
423
1.13M
      av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
424
1.13M
                     &dummy_rate_cost);
425
1.13M
    }
426
1.98M
  } else {
427
762
    p->eobs[block] = 0;
428
762
    p->txb_entropy_ctx[block] = 0;
429
762
  }
430
431
1.98M
  av1_set_txb_context(x, plane, block, tx_size, a, l);
432
433
1.98M
  if (p->eobs[block]) {
434
    // As long as any YUV plane has non-zero quantized transform coefficients,
435
    // mbmi->skip_txfm flag is set to 0.
436
1.96M
    mbmi->skip_txfm = 0;
437
1.96M
    av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
438
1.96M
                                pd->dst.stride, p->eobs[block],
439
1.96M
                                cm->features.reduced_tx_set_used);
440
1.96M
  } else {
441
    // Only when YUV planes all have zero quantized transform coefficients,
442
    // mbmi->skip_txfm flag is set to 1.
443
19.9k
    mbmi->skip_txfm &= 1;
444
19.9k
  }
445
446
  // TODO(debargha, jingning): Temporarily disable txk_type check for eob=0
447
  // case. It is possible that certain collision in hash index would cause
448
  // the assertion failure. To further optimize the rate-distortion
449
  // performance, we need to re-visit this part and enable this assert
450
  // again.
451
1.98M
  if (p->eobs[block] == 0 && plane == 0) {
452
#if 0
453
    if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ &&
454
        args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) {
455
      // TODO(jingning,angiebird,huisu@google.com): enable txk_check when
456
      // enable_optimize_b is true to detect potential RD bug.
457
      const uint8_t disable_txk_check = args->enable_optimize_b;
458
      if (!disable_txk_check) {
459
        assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
460
            DCT_DCT);
461
      }
462
    }
463
#endif
464
4.94k
    update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
465
4.94k
  }
466
467
#if CONFIG_MISMATCH_DEBUG
468
  if (dry_run == OUTPUT_ENABLED) {
469
    int pixel_c, pixel_r;
470
    BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
471
    int blk_w = block_size_wide[bsize];
472
    int blk_h = block_size_high[bsize];
473
    mi_to_pixel_loc(&pixel_c, &pixel_r, xd->mi_col, xd->mi_row, blk_col,
474
                    blk_row, pd->subsampling_x, pd->subsampling_y);
475
    mismatch_record_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint,
476
                             plane, pixel_c, pixel_r, blk_w, blk_h,
477
                             xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
478
  }
479
#endif
480
1.98M
}
481
482
static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
483
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
484
2.08M
                               void *arg, RUN_TYPE dry_run) {
485
2.08M
  struct encode_b_args *const args = arg;
486
2.08M
  MACROBLOCK *const x = args->x;
487
2.08M
  MACROBLOCKD *const xd = &x->e_mbd;
488
2.08M
  MB_MODE_INFO *const mbmi = xd->mi[0];
489
2.08M
  const struct macroblockd_plane *const pd = &xd->plane[plane];
490
2.08M
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
491
2.08M
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
492
493
2.08M
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
494
495
2.07M
  const TX_SIZE plane_tx_size =
496
2.07M
      plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
497
696k
                                    pd->subsampling_y)
498
2.07M
            : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
499
1.37M
                                                         blk_col)];
500
2.07M
  if (!plane) {
501
1.37M
    assert(tx_size_wide[tx_size] >= tx_size_wide[plane_tx_size] &&
502
1.37M
           tx_size_high[tx_size] >= tx_size_high[plane_tx_size]);
503
1.37M
  }
504
505
2.07M
  if (tx_size == plane_tx_size || plane) {
506
1.98M
    encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg,
507
1.98M
                 dry_run);
508
1.98M
  } else {
509
86.9k
    assert(tx_size < TX_SIZES_ALL);
510
86.9k
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
511
86.9k
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
512
86.9k
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
513
    // This is the square transform block partition entry point.
514
86.9k
    const int bsw = tx_size_wide_unit[sub_txs];
515
86.9k
    const int bsh = tx_size_high_unit[sub_txs];
516
86.9k
    const int step = bsh * bsw;
517
86.9k
    const int row_end =
518
86.9k
        AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
519
86.9k
    const int col_end =
520
86.9k
        AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
521
86.9k
    assert(bsw > 0 && bsh > 0);
522
523
252k
    for (int row = 0; row < row_end; row += bsh) {
524
165k
      const int offsetr = blk_row + row;
525
483k
      for (int col = 0; col < col_end; col += bsw) {
526
317k
        const int offsetc = blk_col + col;
527
528
317k
        encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
529
317k
                           arg, dry_run);
530
317k
        block += step;
531
317k
      }
532
165k
    }
533
86.9k
  }
534
2.07M
}
535
536
void av1_foreach_transformed_block_in_plane(
537
    const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
538
282M
    foreach_transformed_block_visitor visit, void *arg) {
539
282M
  const struct macroblockd_plane *const pd = &xd->plane[plane];
540
  // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
541
  // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
542
  // transform size varies per plane, look it up in a common way.
543
282M
  const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
544
282M
  const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size];
545
  // Call visit() directly with zero offsets if the current block size is the
546
  // same as the transform block size.
547
282M
  if (plane_bsize == tx_bsize) {
548
254M
    visit(plane, 0, 0, 0, plane_bsize, tx_size, arg);
549
254M
    return;
550
254M
  }
551
28.0M
  const uint8_t txw_unit = tx_size_wide_unit[tx_size];
552
28.0M
  const uint8_t txh_unit = tx_size_high_unit[tx_size];
553
28.0M
  const int step = txw_unit * txh_unit;
554
555
  // If mb_to_right_edge is < 0 we are in a situation in which
556
  // the current block size extends into the UMV and we won't
557
  // visit the sub blocks that are wholly within the UMV.
558
28.0M
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
559
28.0M
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
560
28.0M
  const BLOCK_SIZE max_unit_bsize =
561
28.0M
      get_plane_block_size(BLOCK_64X64, pd->subsampling_x, pd->subsampling_y);
562
28.0M
  const int mu_blocks_wide =
563
28.0M
      AOMMIN(mi_size_wide[max_unit_bsize], max_blocks_wide);
564
28.0M
  const int mu_blocks_high =
565
28.0M
      AOMMIN(mi_size_high[max_unit_bsize], max_blocks_high);
566
567
  // Keep track of the row and column of the blocks we use so that we know
568
  // if we are in the unrestricted motion border.
569
28.0M
  int i = 0;
570
56.0M
  for (int r = 0; r < max_blocks_high; r += mu_blocks_high) {
571
28.0M
    const int unit_height = AOMMIN(mu_blocks_high + r, max_blocks_high);
572
    // Skip visiting the sub blocks that are wholly within the UMV.
573
56.0M
    for (int c = 0; c < max_blocks_wide; c += mu_blocks_wide) {
574
28.0M
      const int unit_width = AOMMIN(mu_blocks_wide + c, max_blocks_wide);
575
85.9M
      for (int blk_row = r; blk_row < unit_height; blk_row += txh_unit) {
576
183M
        for (int blk_col = c; blk_col < unit_width; blk_col += txw_unit) {
577
125M
          visit(plane, i, blk_row, blk_col, plane_bsize, tx_size, arg);
578
125M
          i += step;
579
125M
        }
580
57.8M
      }
581
28.0M
    }
582
28.0M
  }
583
  // Check if visit() is invoked at least once.
584
28.0M
  assert(i >= 1);
585
28.0M
}
586
587
typedef struct encode_block_pass1_args {
588
  AV1_COMP *cpi;
589
  MACROBLOCK *x;
590
} encode_block_pass1_args;
591
592
static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
593
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
594
0
                               void *arg) {
595
0
  encode_block_pass1_args *args = (encode_block_pass1_args *)arg;
596
0
  AV1_COMP *cpi = args->cpi;
597
0
  AV1_COMMON *cm = &cpi->common;
598
0
  MACROBLOCK *const x = args->x;
599
0
  MACROBLOCKD *const xd = &x->e_mbd;
600
0
  struct macroblock_plane *const p = &x->plane[plane];
601
0
  struct macroblockd_plane *const pd = &xd->plane[plane];
602
0
  tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
603
604
0
  uint8_t *dst;
605
0
  dst = &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
606
607
0
  TxfmParam txfm_param;
608
0
  QUANT_PARAM quant_param;
609
610
0
  av1_setup_xform(cm, x, tx_size, DCT_DCT, &txfm_param);
611
0
  av1_setup_quant(tx_size, 0, AV1_XFORM_QUANT_B, cpi->oxcf.q_cfg.quant_b_adapt,
612
0
                  &quant_param);
613
0
  av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, DCT_DCT,
614
0
                    &quant_param);
615
616
0
  av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
617
0
                  &quant_param);
618
619
0
  if (p->eobs[block] > 0) {
620
0
    txfm_param.eob = p->eobs[block];
621
0
    if (txfm_param.is_hbd) {
622
0
      av1_highbd_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
623
0
      return;
624
0
    }
625
0
    av1_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
626
0
  }
627
0
}
628
629
0
void av1_encode_sby_pass1(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize) {
630
0
  encode_block_pass1_args args = { cpi, x };
631
0
  av1_subtract_plane(x, bsize, 0);
632
0
  av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
633
0
                                         encode_block_pass1, &args);
634
0
}
635
636
void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
637
1.38M
                   RUN_TYPE dry_run) {
638
1.38M
  assert(bsize < BLOCK_SIZES_ALL);
639
1.38M
  MACROBLOCKD *const xd = &x->e_mbd;
640
1.38M
  MB_MODE_INFO *mbmi = xd->mi[0];
641
  // In the current encoder implementation, for inter blocks,
642
  // only when YUV planes all have zero quantized transform coefficients,
643
  // mbmi->skip_txfm flag is set to 1.
644
  // For intra blocks, this flag is set to 0 since skipped blocks are so rare
645
  // that transmitting skip_txfm = 1 is very expensive.
646
  // mbmi->skip_txfm is init to 1, and will be modified in encode_block() based
647
  // on transform, quantization, and (if exists) trellis optimization.
648
1.38M
  mbmi->skip_txfm = 1;
649
1.38M
  if (x->txfm_search_info.skip_txfm) return;
650
651
899k
  struct optimize_ctx ctx;
652
899k
  struct encode_b_args arg = {
653
899k
    cpi, x, &ctx, NULL, NULL, dry_run, cpi->optimize_seg_arr[mbmi->segment_id]
654
899k
  };
655
899k
  const AV1_COMMON *const cm = &cpi->common;
656
899k
  const int num_planes = av1_num_planes(cm);
657
2.46M
  for (int plane = 0; plane < num_planes; ++plane) {
658
1.57M
    const struct macroblockd_plane *const pd = &xd->plane[plane];
659
1.57M
    const int subsampling_x = pd->subsampling_x;
660
1.57M
    const int subsampling_y = pd->subsampling_y;
661
1.57M
    if (plane && !xd->is_chroma_ref) break;
662
1.56M
    const BLOCK_SIZE plane_bsize =
663
1.56M
        get_plane_block_size(bsize, subsampling_x, subsampling_y);
664
1.56M
    assert(plane_bsize < BLOCK_SIZES_ALL);
665
1.56M
    const int mi_width = mi_size_wide[plane_bsize];
666
1.56M
    const int mi_height = mi_size_high[plane_bsize];
667
1.56M
    const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane);
668
1.56M
    const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
669
1.56M
    const int bw = mi_size_wide[txb_size];
670
1.56M
    const int bh = mi_size_high[txb_size];
671
1.56M
    int block = 0;
672
1.56M
    const int step =
673
1.56M
        tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
674
1.56M
    av1_get_entropy_contexts(plane_bsize, pd, ctx.ta[plane], ctx.tl[plane]);
675
1.56M
    av1_subtract_plane(x, plane_bsize, plane);
676
1.56M
    arg.ta = ctx.ta[plane];
677
1.56M
    arg.tl = ctx.tl[plane];
678
1.56M
    const BLOCK_SIZE max_unit_bsize =
679
1.56M
        get_plane_block_size(BLOCK_64X64, subsampling_x, subsampling_y);
680
1.56M
    int mu_blocks_wide = mi_size_wide[max_unit_bsize];
681
1.56M
    int mu_blocks_high = mi_size_high[max_unit_bsize];
682
1.56M
    mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide);
683
1.56M
    mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);
684
685
3.13M
    for (int idy = 0; idy < mi_height; idy += mu_blocks_high) {
686
3.13M
      for (int idx = 0; idx < mi_width; idx += mu_blocks_wide) {
687
1.56M
        int blk_row, blk_col;
688
1.56M
        const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);
689
1.56M
        const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
690
3.20M
        for (blk_row = idy; blk_row < unit_height; blk_row += bh) {
691
3.39M
          for (blk_col = idx; blk_col < unit_width; blk_col += bw) {
692
1.76M
            encode_block_inter(plane, block, blk_row, blk_col, plane_bsize,
693
1.76M
                               max_tx_size, &arg, dry_run);
694
1.76M
            block += step;
695
1.76M
          }
696
1.63M
        }
697
1.56M
      }
698
1.56M
    }
699
1.56M
  }
700
899k
}
701
702
static void encode_block_intra(int plane, int block, int blk_row, int blk_col,
703
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
704
93.7M
                               void *arg) {
705
93.7M
  struct encode_b_args *const args = arg;
706
93.7M
  const AV1_COMP *const cpi = args->cpi;
707
93.7M
  const AV1_COMMON *const cm = &cpi->common;
708
93.7M
  MACROBLOCK *const x = args->x;
709
93.7M
  MACROBLOCKD *const xd = &x->e_mbd;
710
93.7M
  struct macroblock_plane *const p = &x->plane[plane];
711
93.7M
  struct macroblockd_plane *const pd = &xd->plane[plane];
712
93.7M
  tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
713
93.7M
  PLANE_TYPE plane_type = get_plane_type(plane);
714
93.7M
  uint16_t *eob = &p->eobs[block];
715
93.7M
  const int dst_stride = pd->dst.stride;
716
93.7M
  uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
717
93.7M
  int dummy_rate_cost = 0;
718
719
93.7M
  av1_predict_intra_block_facade(cm, xd, plane, blk_col, blk_row, tx_size);
720
721
93.7M
  TX_TYPE tx_type = DCT_DCT;
722
93.7M
  if (xd->mi[0]->skip_txfm) {
723
46
    *eob = 0;
724
46
    p->txb_entropy_ctx[block] = 0;
725
93.7M
  } else {
726
93.7M
    av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size);
727
728
93.7M
    const ENTROPY_CONTEXT *a = &args->ta[blk_col];
729
93.7M
    const ENTROPY_CONTEXT *l = &args->tl[blk_row];
730
93.7M
    tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
731
93.7M
                              cm->features.reduced_tx_set_used);
732
93.7M
    TxfmParam txfm_param;
733
93.7M
    QUANT_PARAM quant_param;
734
93.7M
    const int use_trellis =
735
93.7M
        is_trellis_used(args->enable_optimize_b, args->dry_run);
736
93.7M
    int quant_idx;
737
93.7M
    if (use_trellis)
738
66.0M
      quant_idx = AV1_XFORM_QUANT_FP;
739
27.6M
    else
740
27.6M
      quant_idx =
741
27.6M
          USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
742
743
93.7M
    av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
744
93.7M
    av1_setup_quant(tx_size, use_trellis, quant_idx,
745
93.7M
                    cpi->oxcf.q_cfg.quant_b_adapt, &quant_param);
746
93.7M
    av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type,
747
93.7M
                      &quant_param);
748
749
93.7M
    av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
750
93.7M
                    &quant_param);
751
93.7M
    if (use_trellis) {
752
66.0M
      TXB_CTX txb_ctx;
753
66.0M
      get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
754
66.0M
      av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
755
66.0M
                     &dummy_rate_cost);
756
66.0M
    }
757
93.7M
  }
758
759
93.7M
  if (*eob) {
760
86.5M
    av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
761
86.5M
                                dst_stride, *eob,
762
86.5M
                                cm->features.reduced_tx_set_used);
763
86.5M
  }
764
765
  // TODO(jingning): Temporarily disable txk_type check for eob=0 case.
766
  // It is possible that certain collision in hash index would cause
767
  // the assertion failure. To further optimize the rate-distortion
768
  // performance, we need to re-visit this part and enable this assert
769
  // again.
770
93.7M
  if (*eob == 0 && plane == 0) {
771
#if 0
772
    if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ
773
        && args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) {
774
      assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
775
          DCT_DCT);
776
    }
777
#endif
778
5.42M
    update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
779
5.42M
  }
780
781
93.7M
#if !CONFIG_REALTIME_ONLY
782
93.7M
  if (plane == AOM_PLANE_Y && xd->cfl.store_y) {
783
18.7M
    cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize);
784
18.7M
  }
785
93.7M
#endif
786
93.7M
}
787
788
static void encode_block_intra_and_set_context(int plane, int block,
789
                                               int blk_row, int blk_col,
790
                                               BLOCK_SIZE plane_bsize,
791
93.7M
                                               TX_SIZE tx_size, void *arg) {
792
93.7M
  encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
793
794
93.7M
  struct encode_b_args *const args = arg;
795
93.7M
  MACROBLOCK *x = args->x;
796
93.7M
  ENTROPY_CONTEXT *a = &args->ta[blk_col];
797
93.7M
  ENTROPY_CONTEXT *l = &args->tl[blk_row];
798
93.7M
  av1_set_txb_context(x, plane, block, tx_size, a, l);
799
93.7M
}
800
801
void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
802
                                  BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run,
803
73.9M
                                  TRELLIS_OPT_TYPE enable_optimize_b) {
804
73.9M
  assert(bsize < BLOCK_SIZES_ALL);
805
73.9M
  const MACROBLOCKD *const xd = &x->e_mbd;
806
73.9M
  if (plane && !xd->is_chroma_ref) return;
807
808
67.7M
  const struct macroblockd_plane *const pd = &xd->plane[plane];
809
67.7M
  const int ss_x = pd->subsampling_x;
810
67.7M
  const int ss_y = pd->subsampling_y;
811
67.7M
  ENTROPY_CONTEXT ta[MAX_MIB_SIZE] = { 0 };
812
67.7M
  ENTROPY_CONTEXT tl[MAX_MIB_SIZE] = { 0 };
813
67.7M
  struct encode_b_args arg = {
814
67.7M
    cpi, x, NULL, ta, tl, dry_run, enable_optimize_b
815
67.7M
  };
816
67.7M
  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
817
67.7M
  if (enable_optimize_b) {
818
51.1M
    av1_get_entropy_contexts(plane_bsize, pd, ta, tl);
819
51.1M
  }
820
67.7M
  av1_foreach_transformed_block_in_plane(
821
67.7M
      xd, plane_bsize, plane, encode_block_intra_and_set_context, &arg);
822
67.7M
}