Coverage Report

Created: 2026-08-31 07:01

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