Coverage Report

Created: 2026-11-12 08:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/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
0
                                      int w, int h) {
41
0
  assert(w > 0 && h > 0);
42
0
  int32_t sum = 0;
43
0
  for (int row = 0; row < h; ++row) {
44
0
    for (int col = 0; col < w; ++col) {
45
0
      sum += diff[row * diff_stride + col];
46
0
    }
47
0
  }
48
0
  return (int16_t)DIVIDE_AND_ROUND_SIGNED(sum, w * h);
49
0
}
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
0
                                         int w, int h, int16_t *out) {
54
0
  assert(w > 0 && h > 0);
55
0
  for (int row = 0; row < h; ++row) {
56
0
    int32_t sum = 0;
57
0
    for (int col = 0; col < w; ++col) {
58
0
      sum += diff[row * diff_stride + col];
59
0
    }
60
0
    out[row] = (int16_t)DIVIDE_AND_ROUND_SIGNED(sum, w);
61
0
  }
62
0
}
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
0
                                        int w, int h, int16_t *out) {
67
0
  assert(w > 0 && h > 0);
68
0
  for (int col = 0; col < w; ++col) {
69
0
    int32_t sum = 0;
70
0
    for (int row = 0; row < h; ++row) {
71
0
      sum += diff[row * diff_stride + col];
72
0
    }
73
0
    out[col] = (int16_t)DIVIDE_AND_ROUND_SIGNED(sum, h);
74
0
  }
75
0
}
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
0
    int visible_tx_cols, int visible_tx_rows, TX_TYPE tx_type) {
82
0
  const int complete_block_outside =
83
0
      (visible_tx_cols == 0 || visible_tx_rows == 0);
84
85
0
  if (tx_type <= IDTX) {
86
0
    int16_t avg = 0;
87
0
    if (tx_type != IDTX && !complete_block_outside)
88
0
      avg =
89
0
          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
0
    const int right_pixels = tx_cols - visible_tx_cols;
93
0
    for (int i = 0; i < tx_rows; ++i) {
94
0
      aom_memset16(diff + i * diff_stride + visible_tx_cols, avg, right_pixels);
95
0
    }
96
97
0
    for (int i = visible_tx_rows; i < tx_rows; ++i) {
98
0
      aom_memset16(diff + i * diff_stride, avg, visible_tx_cols);
99
0
    }
100
0
  } else if (htx_tab[tx_type] == IDTX_1D) {
101
0
    if (visible_tx_rows < tx_rows) {
102
0
      int16_t out[64] = { 0 };
103
0
      if (!complete_block_outside)
104
0
        avg_wxh_block_vert_c(diff, diff_stride, visible_tx_cols,
105
0
                             visible_tx_rows, out);
106
107
0
      for (int j = 0; j < visible_tx_cols; j++) {
108
0
        for (int i = visible_tx_rows; i < tx_rows; ++i) {
109
0
          *(diff + i * diff_stride + j) = out[j];
110
0
        }
111
0
      }
112
0
    }
113
114
0
    const int right_pixels = tx_cols - visible_tx_cols;
115
0
    if (right_pixels) {
116
0
      for (int i = 0; i < tx_rows; ++i) {
117
0
        memset(diff + i * diff_stride + visible_tx_cols, 0,
118
0
               right_pixels * sizeof(*diff));
119
0
      }
120
0
    }
121
0
  } else {
122
0
    assert(vtx_tab[tx_type] == IDTX_1D);
123
124
0
    const int right_pixels = tx_cols - visible_tx_cols;
125
0
    if (right_pixels) {
126
0
      int16_t out[64] = { 0 };
127
0
      if (!complete_block_outside)
128
0
        avg_wxh_block_horiz_c(diff, diff_stride, visible_tx_cols,
129
0
                              visible_tx_rows, out);
130
131
0
      for (int i = 0; i < visible_tx_rows; ++i) {
132
0
        aom_memset16(diff + i * diff_stride + visible_tx_cols, out[i],
133
0
                     right_pixels);
134
0
      }
135
0
    }
136
137
0
    for (int i = visible_tx_rows; i < tx_rows; ++i) {
138
0
      memset(diff + i * diff_stride, 0, tx_cols * sizeof(*diff));
139
0
    }
140
0
  }
141
0
}
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
0
                        TX_TYPE tx_type, bool do_border_pad) {
149
0
  assert(rows >= 4 && cols >= 4);
150
0
  const MACROBLOCKD *const xd = &x->e_mbd;
151
0
  BitDepthInfo bd_info = get_bit_depth_info(xd);
152
0
#if CONFIG_AV1_HIGHBITDEPTH
153
0
  if (bd_info.use_highbitdepth_buf) {
154
0
    aom_highbd_subtract_block(rows, cols, diff, diff_stride, src8, src_stride,
155
0
                              pred8, pred_stride);
156
0
  } else {
157
0
    aom_subtract_block(rows, cols, diff, diff_stride, src8, src_stride, pred8,
158
0
                       pred_stride);
159
0
  }
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
0
  if (!do_border_pad) return;
166
167
0
  int visible_cols, visible_rows;
168
0
  const int is_border_block = get_visible_dimensions(
169
0
      x, plane, plane_bsize, blk_col, blk_row, cols, rows,
170
0
      /*clip_dims=*/true, &visible_cols, &visible_rows);
171
0
  if (is_border_block)
172
0
    fill_residue_outside_frame(diff, diff_stride, cols, rows, visible_cols,
173
0
                               visible_rows, tx_type);
174
0
}
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
0
                      TX_TYPE tx_type, bool do_border_pad) {
179
0
  struct macroblock_plane *const p = &x->plane[plane];
180
0
  const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
181
0
  const int diff_stride = block_size_wide[plane_bsize];
182
0
  const int src_stride = p->src.stride;
183
0
  const int dst_stride = pd->dst.stride;
184
0
  const int tx1d_width = tx_size_wide[tx_size];
185
0
  const int tx1d_height = tx_size_high[tx_size];
186
0
  uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
187
0
  uint8_t *src = &p->src.buf[(blk_row * src_stride + blk_col) << MI_SIZE_LOG2];
188
0
  int16_t *src_diff =
189
0
      &p->src_diff[(blk_row * diff_stride + blk_col) << MI_SIZE_LOG2];
190
0
  av1_subtract_block(x, tx1d_height, tx1d_width, src_diff, diff_stride, src,
191
0
                     src_stride, dst, dst_stride, plane, plane_bsize, blk_col,
192
0
                     blk_row, tx_type, do_border_pad);
193
0
}
194
195
void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE plane_bsize, int plane,
196
0
                        bool do_border_pad) {
197
0
  struct macroblock_plane *const p = &x->plane[plane];
198
0
  const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
199
0
  assert(plane_bsize < BLOCK_SIZES_ALL);
200
0
  const int bw = block_size_wide[plane_bsize];
201
0
  const int bh = block_size_high[plane_bsize];
202
203
0
  av1_subtract_block(x, bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
204
0
                     pd->dst.buf, pd->dst.stride, plane, plane_bsize, 0, 0,
205
0
                     DCT_DCT, do_border_pad);
206
0
}
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
0
                   const TXB_CTX *const txb_ctx, int *rate_cost) {
211
0
  MACROBLOCKD *const xd = &x->e_mbd;
212
0
  struct macroblock_plane *const p = &x->plane[plane];
213
0
  const int eob = p->eobs[block];
214
0
  const int segment_id = xd->mi[0]->segment_id;
215
216
0
  if (eob == 0 || !cpi->optimize_seg_arr[segment_id] ||
217
0
      xd->lossless[segment_id]) {
218
0
    *rate_cost = av1_cost_skip_txb(&x->coeff_costs, txb_ctx, plane, tx_size);
219
0
    return eob;
220
0
  }
221
222
0
  return av1_optimize_txb(cpi, x, plane, block, tx_size, tx_type, txb_ctx,
223
0
                          rate_cost, cpi->oxcf.algo_cfg.sharpness);
224
0
}
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 const 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 const 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
0
                       TxfmParam *txfm_param, int64_t per_px_mean) {
395
0
  assert(per_px_mean != INT64_MAX);
396
0
  const struct macroblock_plane *const p = &x->plane[plane];
397
0
  const int block_offset = BLOCK_OFFSET(block);
398
0
  tran_low_t *const coeff = p->coeff + block_offset;
399
0
  const int n_coeffs = av1_get_max_eob(txfm_param->tx_size);
400
0
  memset(coeff, 0, sizeof(*coeff) * n_coeffs);
401
0
  coeff[0] =
402
0
      (tran_low_t)((per_px_mean * dc_coeff_scale[txfm_param->tx_size]) >> 12);
403
0
}
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
0
                     const QUANT_PARAM *qparam) {
408
0
  av1_xform(x, plane, block, blk_row, blk_col, plane_bsize, txfm_param);
409
0
  av1_quant(x, plane, block, txfm_param, qparam);
410
0
}
411
412
void av1_xform(MACROBLOCK *x, int plane, int block, int blk_row, int blk_col,
413
0
               BLOCK_SIZE plane_bsize, TxfmParam *txfm_param) {
414
0
  const struct macroblock_plane *const p = &x->plane[plane];
415
0
  const int block_offset = BLOCK_OFFSET(block);
416
0
  tran_low_t *const coeff = p->coeff + block_offset;
417
0
  const int diff_stride = block_size_wide[plane_bsize];
418
419
0
  const int src_offset = (blk_row * diff_stride + blk_col);
420
0
  const int16_t *src_diff = &p->src_diff[src_offset << MI_SIZE_LOG2];
421
422
0
  av1_fwd_txfm(src_diff, coeff, diff_stride, txfm_param);
423
0
}
424
425
void av1_quant(MACROBLOCK *x, int plane, int block, TxfmParam *txfm_param,
426
0
               const QUANT_PARAM *qparam) {
427
0
  const struct macroblock_plane *const p = &x->plane[plane];
428
0
  const SCAN_ORDER *const scan_order =
429
0
      get_scan(txfm_param->tx_size, txfm_param->tx_type);
430
0
  const int block_offset = BLOCK_OFFSET(block);
431
0
  tran_low_t *const coeff = p->coeff + block_offset;
432
0
  tran_low_t *const qcoeff = p->qcoeff + block_offset;
433
0
  tran_low_t *const dqcoeff = p->dqcoeff + block_offset;
434
0
  uint16_t *const eob = &p->eobs[block];
435
436
0
  if (qparam->xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
437
0
    const int n_coeffs = av1_get_max_eob(txfm_param->tx_size);
438
0
    if (LIKELY(!x->seg_skip_block)) {
439
0
#if CONFIG_AV1_HIGHBITDEPTH
440
0
      if (LIKELY(!txfm_param->is_hbd &&
441
0
                 qparam->xform_quant_idx == AV1_XFORM_QUANT_FP)) {
442
0
        av1_quantize_fp_facade(coeff, n_coeffs, p, qcoeff, dqcoeff, eob,
443
0
                               scan_order, qparam);
444
0
      } else {
445
0
        quant_func_list[qparam->xform_quant_idx][txfm_param->is_hbd](
446
0
            coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, qparam);
447
0
      }
448
#else
449
      if (LIKELY(qparam->xform_quant_idx == AV1_XFORM_QUANT_FP)) {
450
        av1_quantize_fp_facade(coeff, n_coeffs, p, qcoeff, dqcoeff, eob,
451
                               scan_order, qparam);
452
      } else {
453
        quant_func_list[qparam->xform_quant_idx](
454
            coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, qparam);
455
      }
456
#endif
457
0
    } else {
458
0
      av1_quantize_skip(n_coeffs, qcoeff, dqcoeff, eob);
459
0
    }
460
0
  }
461
  // use_optimize_b is true means av1_optimze_b will be called,
462
  // thus cannot update entropy ctx now (performed in optimize_b)
463
0
  if (qparam->use_optimize_b) {
464
0
    p->txb_entropy_ctx[block] = 0;
465
0
  } else {
466
0
    p->txb_entropy_ctx[block] =
467
0
        av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
468
0
  }
469
0
}
470
471
void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size,
472
0
                     TX_TYPE tx_type, TxfmParam *txfm_param) {
473
0
  MACROBLOCKD *const xd = &x->e_mbd;
474
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
475
476
0
  txfm_param->tx_type = tx_type;
477
0
  txfm_param->tx_size = tx_size;
478
0
  txfm_param->lossless = xd->lossless[mbmi->segment_id];
479
0
  txfm_param->tx_set_type = av1_get_ext_tx_set_type(
480
0
      tx_size, is_inter_block(mbmi), cm->features.reduced_tx_set_used);
481
482
0
  txfm_param->bd = xd->bd;
483
0
  txfm_param->is_hbd = is_cur_buf_hbd(xd);
484
0
}
485
void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx,
486
0
                     int use_quant_b_adapt, QUANT_PARAM *qparam) {
487
0
  qparam->log_scale = av1_get_tx_scale(tx_size);
488
0
  qparam->tx_size = tx_size;
489
490
0
  qparam->use_quant_b_adapt = use_quant_b_adapt;
491
492
  // TODO(bohanli): optimize_b and quantization idx has relationship,
493
  // but is kind of buried and complicated in different encoding stages.
494
  // Should have a unified function to derive quant_idx, rather than
495
  // determine and pass in the quant_idx
496
0
  qparam->use_optimize_b = use_optimize_b;
497
0
  qparam->xform_quant_idx = xform_quant_idx;
498
499
0
  qparam->qmatrix = NULL;
500
0
  qparam->iqmatrix = NULL;
501
0
}
502
503
static void encode_block(int plane, int block, int blk_row, int blk_col,
504
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg,
505
0
                         RUN_TYPE dry_run) {
506
0
  (void)dry_run;
507
0
  struct encode_b_args *const args = arg;
508
0
  const AV1_COMP *const cpi = args->cpi;
509
0
  const AV1_COMMON *const cm = &cpi->common;
510
0
  MACROBLOCK *const x = args->x;
511
0
  MACROBLOCKD *const xd = &x->e_mbd;
512
0
  MB_MODE_INFO *mbmi = xd->mi[0];
513
0
  struct macroblock_plane *const p = &x->plane[plane];
514
0
  struct macroblockd_plane *const pd = &xd->plane[plane];
515
0
  tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
516
0
  uint8_t *dst;
517
0
  ENTROPY_CONTEXT *a, *l;
518
0
  int dummy_rate_cost = 0;
519
520
0
  dst = &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
521
522
0
  a = &args->ta[blk_col];
523
0
  l = &args->tl[blk_row];
524
525
0
  TX_TYPE tx_type = DCT_DCT;
526
0
  if (!mbmi->skip_mode) {
527
0
    tx_type = av1_get_tx_type(xd, pd->plane_type, blk_row, blk_col, tx_size,
528
0
                              cm->features.reduced_tx_set_used);
529
530
0
    av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size, tx_type,
531
0
                     cpi->do_border_pad);
532
0
    TxfmParam txfm_param;
533
0
    QUANT_PARAM quant_param;
534
0
    const int use_trellis = is_trellis_used(args->enable_optimize_b, dry_run);
535
0
    int quant_idx;
536
0
    if (use_trellis)
537
0
      quant_idx = AV1_XFORM_QUANT_FP;
538
0
    else
539
0
      quant_idx =
540
0
          USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
541
0
    av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
542
0
    av1_setup_quant(tx_size, use_trellis, quant_idx,
543
0
                    cpi->oxcf.q_cfg.quant_b_adapt, &quant_param);
544
0
    av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type,
545
0
                      &quant_param);
546
0
    av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
547
0
                    &quant_param);
548
0
    if (use_trellis) {
549
0
      TXB_CTX txb_ctx;
550
0
      get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
551
0
      av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
552
0
                     &dummy_rate_cost);
553
0
    }
554
0
  } else {
555
0
    p->eobs[block] = 0;
556
0
    p->txb_entropy_ctx[block] = 0;
557
0
  }
558
559
0
  av1_set_txb_context(x, plane, block, tx_size, a, l);
560
561
0
  if (p->eobs[block]) {
562
    // As long as any YUV plane has non-zero quantized transform coefficients,
563
    // mbmi->skip_txfm flag is set to 0.
564
0
    mbmi->skip_txfm = 0;
565
0
    av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
566
0
                                pd->dst.stride, p->eobs[block],
567
0
                                cm->features.reduced_tx_set_used);
568
0
  } else {
569
    // Only when YUV planes all have zero quantized transform coefficients,
570
    // mbmi->skip_txfm flag is set to 1.
571
0
    mbmi->skip_txfm &= 1;
572
0
  }
573
574
  // TODO(debargha, jingning): Temporarily disable txk_type check for eob=0
575
  // case. It is possible that certain collision in hash index would cause
576
  // the assertion failure. To further optimize the rate-distortion
577
  // performance, we need to re-visit this part and enable this assert
578
  // again.
579
0
  if (p->eobs[block] == 0 && plane == 0) {
580
#if 0
581
    if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ &&
582
        args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) {
583
      // TODO(jingning,angiebird,huisu@google.com): enable txk_check when
584
      // enable_optimize_b is true to detect potential RD bug.
585
      const uint8_t disable_txk_check = args->enable_optimize_b;
586
      if (!disable_txk_check) {
587
        assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
588
            DCT_DCT);
589
      }
590
    }
591
#endif
592
0
    update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
593
0
  }
594
595
#if CONFIG_MISMATCH_DEBUG
596
  if (dry_run == OUTPUT_ENABLED) {
597
    int pixel_c, pixel_r;
598
    BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
599
    int blk_w = block_size_wide[bsize];
600
    int blk_h = block_size_high[bsize];
601
    mi_to_pixel_loc(&pixel_c, &pixel_r, xd->mi_col, xd->mi_row, blk_col,
602
                    blk_row, pd->subsampling_x, pd->subsampling_y);
603
    mismatch_record_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint,
604
                             plane, pixel_c, pixel_r, blk_w, blk_h,
605
                             xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
606
  }
607
#endif
608
0
}
609
610
static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
611
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
612
0
                               void *arg, RUN_TYPE dry_run) {
613
0
  struct encode_b_args *const args = arg;
614
0
  MACROBLOCK *const x = args->x;
615
0
  MACROBLOCKD *const xd = &x->e_mbd;
616
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
617
0
  const struct macroblockd_plane *const pd = &xd->plane[plane];
618
0
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
619
0
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
620
621
0
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
622
623
0
  const TX_SIZE plane_tx_size =
624
0
      plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
625
0
                                    pd->subsampling_y)
626
0
            : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
627
0
                                                         blk_col)];
628
0
  if (!plane) {
629
0
    assert(tx_size_wide[tx_size] >= tx_size_wide[plane_tx_size] &&
630
0
           tx_size_high[tx_size] >= tx_size_high[plane_tx_size]);
631
0
  }
632
633
0
  if (tx_size == plane_tx_size || plane) {
634
0
    encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg,
635
0
                 dry_run);
636
0
  } else {
637
0
    assert(tx_size < TX_SIZES_ALL);
638
0
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
639
0
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
640
0
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
641
    // This is the square transform block partition entry point.
642
0
    const int bsw = tx_size_wide_unit[sub_txs];
643
0
    const int bsh = tx_size_high_unit[sub_txs];
644
0
    const int step = bsh * bsw;
645
0
    const int row_end =
646
0
        AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
647
0
    const int col_end =
648
0
        AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
649
0
    assert(bsw > 0 && bsh > 0);
650
651
0
    for (int row = 0; row < row_end; row += bsh) {
652
0
      const int offsetr = blk_row + row;
653
0
      for (int col = 0; col < col_end; col += bsw) {
654
0
        const int offsetc = blk_col + col;
655
656
0
        encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
657
0
                           arg, dry_run);
658
0
        block += step;
659
0
      }
660
0
    }
661
0
  }
662
0
}
663
664
void av1_foreach_transformed_block_in_plane(
665
    const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
666
0
    foreach_transformed_block_visitor visit, void *arg) {
667
0
  const struct macroblockd_plane *const pd = &xd->plane[plane];
668
  // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
669
  // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
670
  // transform size varies per plane, look it up in a common way.
671
0
  const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
672
0
  const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size];
673
  // Call visit() directly with zero offsets if the current block size is the
674
  // same as the transform block size.
675
0
  if (plane_bsize == tx_bsize) {
676
0
    visit(plane, 0, 0, 0, plane_bsize, tx_size, arg);
677
0
    return;
678
0
  }
679
0
  const uint8_t txw_unit = tx_size_wide_unit[tx_size];
680
0
  const uint8_t txh_unit = tx_size_high_unit[tx_size];
681
0
  const int step = txw_unit * txh_unit;
682
683
  // If mb_to_right_edge is < 0 we are in a situation in which
684
  // the current block size extends into the UMV and we won't
685
  // visit the sub blocks that are wholly within the UMV.
686
0
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
687
0
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
688
0
  const BLOCK_SIZE max_unit_bsize =
689
0
      get_plane_block_size(BLOCK_64X64, pd->subsampling_x, pd->subsampling_y);
690
0
  const int mu_blocks_wide =
691
0
      AOMMIN(mi_size_wide[max_unit_bsize], max_blocks_wide);
692
0
  const int mu_blocks_high =
693
0
      AOMMIN(mi_size_high[max_unit_bsize], max_blocks_high);
694
695
  // Keep track of the row and column of the blocks we use so that we know
696
  // if we are in the unrestricted motion border.
697
0
  int i = 0;
698
0
  for (int r = 0; r < max_blocks_high; r += mu_blocks_high) {
699
0
    const int unit_height = AOMMIN(mu_blocks_high + r, max_blocks_high);
700
    // Skip visiting the sub blocks that are wholly within the UMV.
701
0
    for (int c = 0; c < max_blocks_wide; c += mu_blocks_wide) {
702
0
      const int unit_width = AOMMIN(mu_blocks_wide + c, max_blocks_wide);
703
0
      for (int blk_row = r; blk_row < unit_height; blk_row += txh_unit) {
704
0
        for (int blk_col = c; blk_col < unit_width; blk_col += txw_unit) {
705
0
          visit(plane, i, blk_row, blk_col, plane_bsize, tx_size, arg);
706
0
          i += step;
707
0
        }
708
0
      }
709
0
    }
710
0
  }
711
  // Check if visit() is invoked at least once.
712
0
  assert(i >= 1);
713
0
}
714
715
typedef struct encode_block_pass1_args {
716
  AV1_COMP *cpi;
717
  MACROBLOCK *x;
718
} encode_block_pass1_args;
719
720
static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
721
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
722
0
                               void *arg) {
723
0
  encode_block_pass1_args *args = (encode_block_pass1_args *)arg;
724
0
  AV1_COMP *cpi = args->cpi;
725
0
  AV1_COMMON *cm = &cpi->common;
726
0
  MACROBLOCK *const x = args->x;
727
0
  MACROBLOCKD *const xd = &x->e_mbd;
728
0
  struct macroblock_plane *const p = &x->plane[plane];
729
0
  struct macroblockd_plane *const pd = &xd->plane[plane];
730
0
  tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
731
732
0
  uint8_t *dst;
733
0
  dst = &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
734
735
0
  TxfmParam txfm_param;
736
0
  QUANT_PARAM quant_param;
737
738
0
  av1_setup_xform(cm, x, tx_size, DCT_DCT, &txfm_param);
739
0
  av1_setup_quant(tx_size, 0, AV1_XFORM_QUANT_B, cpi->oxcf.q_cfg.quant_b_adapt,
740
0
                  &quant_param);
741
0
  av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, DCT_DCT,
742
0
                    &quant_param);
743
744
0
  av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
745
0
                  &quant_param);
746
747
0
  if (p->eobs[block] > 0) {
748
0
    txfm_param.eob = p->eobs[block];
749
0
    if (txfm_param.is_hbd) {
750
0
      av1_highbd_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
751
0
      return;
752
0
    }
753
0
    av1_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
754
0
  }
755
0
}
756
757
0
void av1_encode_sby_pass1(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize) {
758
0
  encode_block_pass1_args args = { cpi, x };
759
0
  av1_subtract_plane(x, bsize, PLANE_TYPE_Y, cpi->do_border_pad);
760
0
  av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
761
0
                                         encode_block_pass1, &args);
762
0
}
763
764
void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
765
0
                   RUN_TYPE dry_run) {
766
0
  assert(bsize < BLOCK_SIZES_ALL);
767
0
  MACROBLOCKD *const xd = &x->e_mbd;
768
0
  MB_MODE_INFO *mbmi = xd->mi[0];
769
  // In the current encoder implementation, for inter blocks,
770
  // only when YUV planes all have zero quantized transform coefficients,
771
  // mbmi->skip_txfm flag is set to 1.
772
  // For intra blocks, this flag is set to 0 since skipped blocks are so rare
773
  // that transmitting skip_txfm = 1 is very expensive.
774
  // mbmi->skip_txfm is init to 1, and will be modified in encode_block() based
775
  // on transform, quantization, and (if exists) trellis optimization.
776
0
  mbmi->skip_txfm = 1;
777
0
  if (x->txfm_search_info.skip_txfm) return;
778
779
0
  struct optimize_ctx ctx;
780
0
  struct encode_b_args arg = {
781
0
    cpi, x, &ctx, NULL, NULL, dry_run, cpi->optimize_seg_arr[mbmi->segment_id]
782
0
  };
783
0
  const AV1_COMMON *const cm = &cpi->common;
784
0
  const int num_planes = av1_num_planes(cm);
785
0
  for (int plane = 0; plane < num_planes; ++plane) {
786
0
    const struct macroblockd_plane *const pd = &xd->plane[plane];
787
0
    const int subsampling_x = pd->subsampling_x;
788
0
    const int subsampling_y = pd->subsampling_y;
789
0
    if (plane && !xd->is_chroma_ref) break;
790
0
    const BLOCK_SIZE plane_bsize =
791
0
        get_plane_block_size(bsize, subsampling_x, subsampling_y);
792
0
    assert(plane_bsize < BLOCK_SIZES_ALL);
793
0
    const int mi_width = mi_size_wide[plane_bsize];
794
0
    const int mi_height = mi_size_high[plane_bsize];
795
0
    const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane);
796
0
    const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
797
0
    const int bw = mi_size_wide[txb_size];
798
0
    const int bh = mi_size_high[txb_size];
799
0
    int block = 0;
800
0
    const int step =
801
0
        tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
802
0
    av1_get_entropy_contexts(plane_bsize, pd, ctx.ta[plane], ctx.tl[plane]);
803
0
    arg.ta = ctx.ta[plane];
804
0
    arg.tl = ctx.tl[plane];
805
0
    const BLOCK_SIZE max_unit_bsize =
806
0
        get_plane_block_size(BLOCK_64X64, subsampling_x, subsampling_y);
807
0
    int mu_blocks_wide = mi_size_wide[max_unit_bsize];
808
0
    int mu_blocks_high = mi_size_high[max_unit_bsize];
809
0
    mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide);
810
0
    mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);
811
812
0
    for (int idy = 0; idy < mi_height; idy += mu_blocks_high) {
813
0
      for (int idx = 0; idx < mi_width; idx += mu_blocks_wide) {
814
0
        int blk_row, blk_col;
815
0
        const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);
816
0
        const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
817
0
        for (blk_row = idy; blk_row < unit_height; blk_row += bh) {
818
0
          for (blk_col = idx; blk_col < unit_width; blk_col += bw) {
819
0
            encode_block_inter(plane, block, blk_row, blk_col, plane_bsize,
820
0
                               max_tx_size, &arg, dry_run);
821
0
            block += step;
822
0
          }
823
0
        }
824
0
      }
825
0
    }
826
0
  }
827
0
}
828
829
static void encode_block_intra(int plane, int block, int blk_row, int blk_col,
830
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
831
0
                               void *arg) {
832
0
  struct encode_b_args *const args = arg;
833
0
  const AV1_COMP *const cpi = args->cpi;
834
0
  const AV1_COMMON *const cm = &cpi->common;
835
0
  MACROBLOCK *const x = args->x;
836
0
  MACROBLOCKD *const xd = &x->e_mbd;
837
0
  struct macroblock_plane *const p = &x->plane[plane];
838
0
  struct macroblockd_plane *const pd = &xd->plane[plane];
839
0
  tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block);
840
0
  PLANE_TYPE plane_type = get_plane_type(plane);
841
0
  uint16_t *eob = &p->eobs[block];
842
0
  const int dst_stride = pd->dst.stride;
843
0
  uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2];
844
0
  int dummy_rate_cost = 0;
845
846
0
  av1_predict_intra_block_facade(cm, xd, plane, blk_col, blk_row, tx_size);
847
848
0
  TX_TYPE tx_type = DCT_DCT;
849
0
  if (xd->mi[0]->skip_txfm) {
850
0
    *eob = 0;
851
0
    p->txb_entropy_ctx[block] = 0;
852
0
  } else {
853
0
    const ENTROPY_CONTEXT *a = &args->ta[blk_col];
854
0
    const ENTROPY_CONTEXT *l = &args->tl[blk_row];
855
0
    tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
856
0
                              cm->features.reduced_tx_set_used);
857
0
    TX_TYPE primary_tx_type = is_stat_generation_stage(cpi) ? DCT_DCT : tx_type;
858
0
    av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size,
859
0
                     primary_tx_type, cpi->do_border_pad);
860
0
    TxfmParam txfm_param;
861
0
    QUANT_PARAM quant_param;
862
0
    const int use_trellis =
863
0
        is_trellis_used(args->enable_optimize_b, args->dry_run);
864
0
    int quant_idx;
865
0
    if (use_trellis)
866
0
      quant_idx = AV1_XFORM_QUANT_FP;
867
0
    else
868
0
      quant_idx =
869
0
          USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
870
871
0
    av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
872
0
    av1_setup_quant(tx_size, use_trellis, quant_idx,
873
0
                    cpi->oxcf.q_cfg.quant_b_adapt, &quant_param);
874
0
    av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type,
875
0
                      &quant_param);
876
877
0
    av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
878
0
                    &quant_param);
879
0
    if (use_trellis) {
880
0
      TXB_CTX txb_ctx;
881
0
      get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
882
0
      av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx,
883
0
                     &dummy_rate_cost);
884
0
    }
885
0
  }
886
887
0
  if (*eob) {
888
0
    av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
889
0
                                dst_stride, *eob,
890
0
                                cm->features.reduced_tx_set_used);
891
0
  }
892
893
  // TODO(jingning): Temporarily disable txk_type check for eob=0 case.
894
  // It is possible that certain collision in hash index would cause
895
  // the assertion failure. To further optimize the rate-distortion
896
  // performance, we need to re-visit this part and enable this assert
897
  // again.
898
0
  if (*eob == 0 && plane == 0) {
899
#if 0
900
    if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ
901
        && args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) {
902
      assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] ==
903
          DCT_DCT);
904
    }
905
#endif
906
0
    update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT);
907
0
  }
908
909
0
#if !CONFIG_REALTIME_ONLY
910
0
  if (plane == AOM_PLANE_Y && xd->cfl.store_y) {
911
0
    cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize);
912
0
  }
913
0
#endif
914
0
}
915
916
static void encode_block_intra_and_set_context(int plane, int block,
917
                                               int blk_row, int blk_col,
918
                                               BLOCK_SIZE plane_bsize,
919
0
                                               TX_SIZE tx_size, void *arg) {
920
0
  encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
921
922
0
  struct encode_b_args *const args = arg;
923
0
  MACROBLOCK *x = args->x;
924
0
  ENTROPY_CONTEXT *a = &args->ta[blk_col];
925
0
  ENTROPY_CONTEXT *l = &args->tl[blk_row];
926
0
  av1_set_txb_context(x, plane, block, tx_size, a, l);
927
0
}
928
929
void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
930
                                  BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run,
931
0
                                  TRELLIS_OPT_TYPE enable_optimize_b) {
932
0
  assert(bsize < BLOCK_SIZES_ALL);
933
0
  const MACROBLOCKD *const xd = &x->e_mbd;
934
0
  if (plane && !xd->is_chroma_ref) return;
935
936
0
  const struct macroblockd_plane *const pd = &xd->plane[plane];
937
0
  const int ss_x = pd->subsampling_x;
938
0
  const int ss_y = pd->subsampling_y;
939
0
  ENTROPY_CONTEXT ta[MAX_MIB_SIZE] = { 0 };
940
0
  ENTROPY_CONTEXT tl[MAX_MIB_SIZE] = { 0 };
941
0
  struct encode_b_args arg = {
942
0
    cpi, x, NULL, ta, tl, dry_run, enable_optimize_b
943
0
  };
944
0
  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
945
0
  if (enable_optimize_b) {
946
0
    av1_get_entropy_contexts(plane_bsize, pd, ta, tl);
947
0
  }
948
0
  av1_foreach_transformed_block_in_plane(
949
0
      xd, plane_bsize, plane, encode_block_intra_and_set_context, &arg);
950
0
}