Coverage Report

Created: 2022-08-24 06:17

/src/aom/av1/encoder/encodetxb.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include "av1/encoder/encodetxb.h"
13
14
#include "aom_ports/mem.h"
15
#include "av1/common/blockd.h"
16
#include "av1/common/idct.h"
17
#include "av1/common/pred_common.h"
18
#include "av1/common/scan.h"
19
#include "av1/encoder/bitstream.h"
20
#include "av1/encoder/cost.h"
21
#include "av1/encoder/encodeframe.h"
22
#include "av1/encoder/hash.h"
23
#include "av1/encoder/rdopt.h"
24
#include "av1/encoder/tokenize.h"
25
26
2.52k
void av1_alloc_txb_buf(AV1_COMP *cpi) {
27
2.52k
  AV1_COMMON *cm = &cpi->common;
28
2.52k
  CoeffBufferPool *coeff_buf_pool = &cpi->coeff_buffer_pool;
29
2.52k
  int size = ((cm->mi_params.mi_rows >> cm->seq_params->mib_size_log2) + 1) *
30
2.52k
             ((cm->mi_params.mi_cols >> cm->seq_params->mib_size_log2) + 1);
31
2.52k
  const int num_planes = av1_num_planes(cm);
32
2.52k
  const int subsampling_x = cm->seq_params->subsampling_x;
33
2.52k
  const int subsampling_y = cm->seq_params->subsampling_y;
34
2.52k
  const int luma_max_sb_square =
35
2.52k
      1 << num_pels_log2_lookup[cm->seq_params->sb_size];
36
2.52k
  const int chroma_max_sb_square =
37
2.52k
      luma_max_sb_square >> (subsampling_x + subsampling_y);
38
2.52k
  const int num_tcoeffs =
39
2.52k
      size * (luma_max_sb_square + (num_planes - 1) * chroma_max_sb_square);
40
2.52k
  const int txb_unit_size = TX_SIZE_W_MIN * TX_SIZE_H_MIN;
41
42
2.52k
  av1_free_txb_buf(cpi);
43
  // TODO(jingning): This should be further reduced.
44
2.52k
  cpi->coeff_buffer_base = aom_malloc(sizeof(*cpi->coeff_buffer_base) * size);
45
2.52k
  CHECK_MEM_ERROR(
46
2.52k
      cm, coeff_buf_pool->tcoeff,
47
2.52k
      aom_memalign(32, sizeof(*coeff_buf_pool->tcoeff) * num_tcoeffs));
48
2.52k
  coeff_buf_pool->eobs =
49
2.52k
      aom_malloc(sizeof(*coeff_buf_pool->eobs) * num_tcoeffs / txb_unit_size);
50
2.52k
  coeff_buf_pool->entropy_ctx = aom_malloc(
51
2.52k
      sizeof(*coeff_buf_pool->entropy_ctx) * num_tcoeffs / txb_unit_size);
52
53
2.52k
  tran_low_t *tcoeff_ptr = coeff_buf_pool->tcoeff;
54
2.52k
  uint16_t *eob_ptr = coeff_buf_pool->eobs;
55
2.52k
  uint8_t *entropy_ctx_ptr = coeff_buf_pool->entropy_ctx;
56
19.3k
  for (int i = 0; i < size; i++) {
57
67.1k
    for (int plane = 0; plane < num_planes; plane++) {
58
50.3k
      const int max_sb_square =
59
50.3k
          (plane == AOM_PLANE_Y) ? luma_max_sb_square : chroma_max_sb_square;
60
50.3k
      cpi->coeff_buffer_base[i].tcoeff[plane] = tcoeff_ptr;
61
50.3k
      cpi->coeff_buffer_base[i].eobs[plane] = eob_ptr;
62
50.3k
      cpi->coeff_buffer_base[i].entropy_ctx[plane] = entropy_ctx_ptr;
63
50.3k
      tcoeff_ptr += max_sb_square;
64
50.3k
      eob_ptr += max_sb_square / txb_unit_size;
65
50.3k
      entropy_ctx_ptr += max_sb_square / txb_unit_size;
66
50.3k
    }
67
16.7k
  }
68
2.52k
}
69
70
3.78k
void av1_free_txb_buf(AV1_COMP *cpi) {
71
3.78k
  CoeffBufferPool *coeff_buf_pool = &cpi->coeff_buffer_pool;
72
3.78k
  aom_free(cpi->coeff_buffer_base);
73
3.78k
  aom_free(coeff_buf_pool->tcoeff);
74
3.78k
  aom_free(coeff_buf_pool->eobs);
75
3.78k
  aom_free(coeff_buf_pool->entropy_ctx);
76
3.78k
}
77
78
3.74k
static void write_golomb(aom_writer *w, int level) {
79
3.74k
  int x = level + 1;
80
3.74k
  int i = x;
81
3.74k
  int length = 0;
82
83
32.8k
  while (i) {
84
29.1k
    i >>= 1;
85
29.1k
    ++length;
86
29.1k
  }
87
3.74k
  assert(length > 0);
88
89
29.1k
  for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0);
90
91
32.8k
  for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01);
92
3.74k
}
93
94
static const int8_t eob_to_pos_small[33] = {
95
  0, 1, 2,                                        // 0-2
96
  3, 3,                                           // 3-4
97
  4, 4, 4, 4,                                     // 5-8
98
  5, 5, 5, 5, 5, 5, 5, 5,                         // 9-16
99
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6  // 17-32
100
};
101
102
static const int8_t eob_to_pos_large[17] = {
103
  6,                               // place holder
104
  7,                               // 33-64
105
  8,  8,                           // 65-128
106
  9,  9,  9,  9,                   // 129-256
107
  10, 10, 10, 10, 10, 10, 10, 10,  // 257-512
108
  11                               // 513-
109
};
110
111
520k
int av1_get_eob_pos_token(const int eob, int *const extra) {
112
520k
  int t;
113
114
520k
  if (eob < 33) {
115
445k
    t = eob_to_pos_small[eob];
116
445k
  } else {
117
75.2k
    const int e = AOMMIN((eob - 1) >> 5, 16);
118
75.2k
    t = eob_to_pos_large[e];
119
75.2k
  }
120
121
520k
  *extra = eob - av1_eob_group_start[t];
122
123
520k
  return t;
124
520k
}
125
126
#if CONFIG_ENTROPY_STATS
127
void av1_update_eob_context(int cdf_idx, int eob, TX_SIZE tx_size,
128
                            TX_CLASS tx_class, PLANE_TYPE plane,
129
                            FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts,
130
                            uint8_t allow_update_cdf) {
131
#else
132
void av1_update_eob_context(int eob, TX_SIZE tx_size, TX_CLASS tx_class,
133
                            PLANE_TYPE plane, FRAME_CONTEXT *ec_ctx,
134
5.23k
                            uint8_t allow_update_cdf) {
135
5.23k
#endif
136
5.23k
  int eob_extra;
137
5.23k
  const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra);
138
5.23k
  TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
139
140
5.23k
  const int eob_multi_size = txsize_log2_minus4[tx_size];
141
5.23k
  const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
142
143
5.23k
  switch (eob_multi_size) {
144
675
    case 0:
145
#if CONFIG_ENTROPY_STATS
146
      ++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
147
#endif
148
675
      if (allow_update_cdf)
149
675
        update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5);
150
675
      break;
151
6
    case 1:
152
#if CONFIG_ENTROPY_STATS
153
      ++counts->eob_multi32[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
154
#endif
155
6
      if (allow_update_cdf)
156
6
        update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6);
157
6
      break;
158
59
    case 2:
159
#if CONFIG_ENTROPY_STATS
160
      ++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
161
#endif
162
59
      if (allow_update_cdf)
163
59
        update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7);
164
59
      break;
165
89
    case 3:
166
#if CONFIG_ENTROPY_STATS
167
      ++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
168
#endif
169
89
      if (allow_update_cdf) {
170
89
        update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1,
171
89
                   8);
172
89
      }
173
89
      break;
174
87
    case 4:
175
#if CONFIG_ENTROPY_STATS
176
      ++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
177
#endif
178
87
      if (allow_update_cdf) {
179
87
        update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1,
180
87
                   9);
181
87
      }
182
87
      break;
183
661
    case 5:
184
#if CONFIG_ENTROPY_STATS
185
      ++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
186
#endif
187
661
      if (allow_update_cdf) {
188
661
        update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1,
189
661
                   10);
190
661
      }
191
661
      break;
192
3.66k
    case 6:
193
3.66k
    default:
194
#if CONFIG_ENTROPY_STATS
195
      ++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
196
#endif
197
3.66k
      if (allow_update_cdf) {
198
3.66k
        update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1,
199
3.66k
                   11);
200
3.66k
      }
201
3.66k
      break;
202
5.23k
  }
203
204
5.23k
  if (av1_eob_offset_bits[eob_pt] > 0) {
205
0
    int eob_ctx = eob_pt - 3;
206
0
    int eob_shift = av1_eob_offset_bits[eob_pt] - 1;
207
0
    int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
208
#if CONFIG_ENTROPY_STATS
209
    counts->eob_extra[cdf_idx][txs_ctx][plane][eob_pt][bit]++;
210
#endif  // CONFIG_ENTROPY_STATS
211
0
    if (allow_update_cdf)
212
0
      update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_ctx], bit, 2);
213
0
  }
214
5.23k
}
215
216
static INLINE int get_nz_map_ctx(const uint8_t *const levels,
217
                                 const int coeff_idx, const int bwl,
218
                                 const int height, const int scan_idx,
219
                                 const int is_eob, const TX_SIZE tx_size,
220
1.34M
                                 const TX_CLASS tx_class) {
221
1.34M
  if (is_eob) {
222
164k
    if (scan_idx == 0) return 0;
223
22.0k
    if (scan_idx <= (height << bwl) / 8) return 1;
224
20.3k
    if (scan_idx <= (height << bwl) / 4) return 2;
225
20.0k
    return 3;
226
20.3k
  }
227
1.17M
  const int stats =
228
1.17M
      get_nz_mag(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class);
229
1.17M
  return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, tx_size, tx_class);
230
1.34M
}
231
232
void av1_txb_init_levels_c(const tran_low_t *const coeff, const int width,
233
195k
                           const int height, uint8_t *const levels) {
234
195k
  const int stride = width + TX_PAD_HOR;
235
195k
  uint8_t *ls = levels;
236
237
195k
  memset(levels + stride * height, 0,
238
195k
         sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END));
239
240
2.82M
  for (int i = 0; i < height; i++) {
241
55.4M
    for (int j = 0; j < width; j++) {
242
52.8M
      *ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX);
243
52.8M
    }
244
13.1M
    for (int j = 0; j < TX_PAD_HOR; j++) {
245
10.5M
      *ls++ = 0;
246
10.5M
    }
247
2.63M
  }
248
195k
}
249
250
void av1_get_nz_map_contexts_c(const uint8_t *const levels,
251
                               const int16_t *const scan, const uint16_t eob,
252
                               const TX_SIZE tx_size, const TX_CLASS tx_class,
253
164k
                               int8_t *const coeff_contexts) {
254
164k
  const int bwl = get_txb_bwl(tx_size);
255
164k
  const int height = get_txb_high(tx_size);
256
1.50M
  for (int i = 0; i < eob; ++i) {
257
1.34M
    const int pos = scan[i];
258
1.34M
    coeff_contexts[pos] = get_nz_map_ctx(levels, pos, bwl, height, i,
259
1.34M
                                         i == eob - 1, tx_size, tx_class);
260
1.34M
  }
261
164k
}
262
263
void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
264
                          aom_writer *w, int blk_row, int blk_col, int plane,
265
796k
                          int block, TX_SIZE tx_size) {
266
796k
  MACROBLOCKD *xd = &x->e_mbd;
267
796k
  const CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff;
268
796k
  const PLANE_TYPE plane_type = get_plane_type(plane);
269
796k
  const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] /
270
796k
                         (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
271
796k
  const uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset;
272
796k
  const uint16_t eob = eob_txb[block];
273
796k
  const uint8_t *entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset;
274
796k
  const int txb_skip_ctx = entropy_ctx[block] & TXB_SKIP_CTX_MASK;
275
796k
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
276
796k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
277
796k
  aom_write_symbol(w, eob == 0, ec_ctx->txb_skip_cdf[txs_ctx][txb_skip_ctx], 2);
278
796k
  if (eob == 0) return;
279
280
5.23k
  const TX_TYPE tx_type =
281
5.23k
      av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
282
5.23k
                      cm->features.reduced_tx_set_used);
283
  // Only y plane's tx_type is transmitted
284
5.23k
  if (plane == 0) {
285
1.71k
    av1_write_tx_type(cm, xd, tx_type, tx_size, w);
286
1.71k
  }
287
288
5.23k
  int eob_extra;
289
5.23k
  const int eob_pt = av1_get_eob_pos_token(eob, &eob_extra);
290
5.23k
  const int eob_multi_size = txsize_log2_minus4[tx_size];
291
5.23k
  const TX_CLASS tx_class = tx_type_to_class[tx_type];
292
5.23k
  const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
293
5.23k
  switch (eob_multi_size) {
294
675
    case 0:
295
675
      aom_write_symbol(w, eob_pt - 1,
296
675
                       ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], 5);
297
675
      break;
298
6
    case 1:
299
6
      aom_write_symbol(w, eob_pt - 1,
300
6
                       ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], 6);
301
6
      break;
302
59
    case 2:
303
59
      aom_write_symbol(w, eob_pt - 1,
304
59
                       ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], 7);
305
59
      break;
306
89
    case 3:
307
89
      aom_write_symbol(w, eob_pt - 1,
308
89
                       ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], 8);
309
89
      break;
310
87
    case 4:
311
87
      aom_write_symbol(w, eob_pt - 1,
312
87
                       ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], 9);
313
87
      break;
314
661
    case 5:
315
661
      aom_write_symbol(w, eob_pt - 1,
316
661
                       ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], 10);
317
661
      break;
318
3.66k
    default:
319
3.66k
      aom_write_symbol(w, eob_pt - 1,
320
3.66k
                       ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11);
321
3.66k
      break;
322
5.23k
  }
323
324
5.23k
  const int eob_offset_bits = av1_eob_offset_bits[eob_pt];
325
5.23k
  if (eob_offset_bits > 0) {
326
0
    const int eob_ctx = eob_pt - 3;
327
0
    int eob_shift = eob_offset_bits - 1;
328
0
    int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
329
0
    aom_write_symbol(w, bit,
330
0
                     ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2);
331
0
    for (int i = 1; i < eob_offset_bits; i++) {
332
0
      eob_shift = eob_offset_bits - 1 - i;
333
0
      bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
334
0
      aom_write_bit(w, bit);
335
0
    }
336
0
  }
337
338
5.23k
  const int width = get_txb_wide(tx_size);
339
5.23k
  const int height = get_txb_high(tx_size);
340
5.23k
  uint8_t levels_buf[TX_PAD_2D];
341
5.23k
  uint8_t *const levels = set_levels(levels_buf, width);
342
5.23k
  const tran_low_t *tcoeff_txb =
343
5.23k
      cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
344
5.23k
  const tran_low_t *tcoeff = tcoeff_txb + BLOCK_OFFSET(block);
345
5.23k
  av1_txb_init_levels(tcoeff, width, height, levels);
346
5.23k
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
347
5.23k
  const int16_t *const scan = scan_order->scan;
348
5.23k
  DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
349
5.23k
  av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
350
351
5.23k
  const int bwl = get_txb_bwl(tx_size);
352
10.4k
  for (int c = eob - 1; c >= 0; --c) {
353
5.23k
    const int pos = scan[c];
354
5.23k
    const int coeff_ctx = coeff_contexts[pos];
355
5.23k
    const tran_low_t v = tcoeff[pos];
356
5.23k
    const tran_low_t level = abs(v);
357
358
5.23k
    if (c == eob - 1) {
359
5.23k
      aom_write_symbol(
360
5.23k
          w, AOMMIN(level, 3) - 1,
361
5.23k
          ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3);
362
5.23k
    } else {
363
0
      aom_write_symbol(w, AOMMIN(level, 3),
364
0
                       ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx],
365
0
                       4);
366
0
    }
367
5.23k
    if (level > NUM_BASE_LEVELS) {
368
      // level is above 1.
369
3.78k
      const int base_range = level - 1 - NUM_BASE_LEVELS;
370
3.78k
      const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
371
3.78k
      aom_cdf_prob *cdf =
372
3.78k
          ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx];
373
18.8k
      for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
374
15.1k
        const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
375
15.1k
        aom_write_symbol(w, k, cdf, BR_CDF_SIZE);
376
15.1k
        if (k < BR_CDF_SIZE - 1) break;
377
15.1k
      }
378
3.78k
    }
379
5.23k
  }
380
381
  // Loop to code all signs in the transform block,
382
  // starting with the sign of DC (if applicable)
383
10.4k
  for (int c = 0; c < eob; ++c) {
384
5.23k
    const tran_low_t v = tcoeff[scan[c]];
385
5.23k
    const tran_low_t level = abs(v);
386
5.23k
    const int sign = (v < 0) ? 1 : 0;
387
5.23k
    if (level) {
388
5.23k
      if (c == 0) {
389
5.23k
        const int dc_sign_ctx =
390
5.23k
            (entropy_ctx[block] >> DC_SIGN_CTX_SHIFT) & DC_SIGN_CTX_MASK;
391
5.23k
        aom_write_symbol(w, sign, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx],
392
5.23k
                         2);
393
5.23k
      } else {
394
0
        aom_write_bit(w, sign);
395
0
      }
396
5.23k
      if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS)
397
3.74k
        write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS);
398
5.23k
    }
399
5.23k
  }
400
5.23k
}
401
402
void av1_write_intra_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x,
403
11.8k
                               aom_writer *w, BLOCK_SIZE bsize) {
404
11.8k
  MACROBLOCKD *xd = &x->e_mbd;
405
11.8k
  const int num_planes = av1_num_planes(cm);
406
11.8k
  int block[MAX_MB_PLANE] = { 0 };
407
11.8k
  int row, col;
408
11.8k
  assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
409
11.8k
                                       xd->plane[0].subsampling_y));
410
11.8k
  const int max_blocks_wide = max_block_wide(xd, bsize, 0);
411
11.8k
  const int max_blocks_high = max_block_high(xd, bsize, 0);
412
11.8k
  const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
413
11.8k
  int mu_blocks_wide = mi_size_wide[max_unit_bsize];
414
11.8k
  int mu_blocks_high = mi_size_high[max_unit_bsize];
415
11.8k
  mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
416
11.8k
  mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
417
418
23.6k
  for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
419
23.6k
    for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
420
47.2k
      for (int plane = 0; plane < num_planes; ++plane) {
421
35.4k
        if (plane && !xd->is_chroma_ref) break;
422
35.4k
        const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
423
35.4k
        const int stepr = tx_size_high_unit[tx_size];
424
35.4k
        const int stepc = tx_size_wide_unit[tx_size];
425
35.4k
        const int step = stepr * stepc;
426
35.4k
        const struct macroblockd_plane *const pd = &xd->plane[plane];
427
35.4k
        const int unit_height = ROUND_POWER_OF_TWO(
428
35.4k
            AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
429
35.4k
        const int unit_width = ROUND_POWER_OF_TWO(
430
35.4k
            AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
431
141k
        for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
432
105k
             blk_row += stepr) {
433
901k
          for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
434
796k
               blk_col += stepc) {
435
796k
            av1_write_coeffs_txb(cm, x, w, blk_row, blk_col, plane,
436
796k
                                 block[plane], tx_size);
437
796k
            block[plane] += step;
438
796k
          }
439
105k
        }
440
35.4k
      }
441
11.8k
    }
442
11.8k
  }
443
11.8k
}
444
445
uint8_t av1_get_txb_entropy_context(const tran_low_t *qcoeff,
446
42.3M
                                    const SCAN_ORDER *scan_order, int eob) {
447
42.3M
  const int16_t *const scan = scan_order->scan;
448
42.3M
  int cul_level = 0;
449
42.3M
  int c;
450
451
42.3M
  if (eob == 0) return 0;
452
497k
  for (c = 0; c < eob; ++c) {
453
380k
    cul_level += abs(qcoeff[scan[c]]);
454
380k
    if (cul_level > COEFF_CONTEXT_MASK) break;
455
380k
  }
456
457
370k
  cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
458
370k
  set_dc_sign(&cul_level, qcoeff[0]);
459
460
370k
  return (uint8_t)cul_level;
461
42.3M
}
462
463
static void update_tx_type_count(const AV1_COMP *cpi, const AV1_COMMON *cm,
464
                                 MACROBLOCKD *xd, int blk_row, int blk_col,
465
                                 int plane, TX_SIZE tx_size,
466
                                 FRAME_COUNTS *counts,
467
5.23k
                                 uint8_t allow_update_cdf) {
468
5.23k
  MB_MODE_INFO *mbmi = xd->mi[0];
469
5.23k
  int is_inter = is_inter_block(mbmi);
470
5.23k
  const int reduced_tx_set_used = cm->features.reduced_tx_set_used;
471
5.23k
  FRAME_CONTEXT *fc = xd->tile_ctx;
472
5.23k
#if !CONFIG_ENTROPY_STATS
473
5.23k
  (void)counts;
474
5.23k
#endif  // !CONFIG_ENTROPY_STATS
475
476
  // Only y plane's tx_type is updated
477
5.23k
  if (plane > 0) return;
478
1.71k
  const TX_TYPE tx_type = av1_get_tx_type(xd, PLANE_TYPE_Y, blk_row, blk_col,
479
1.71k
                                          tx_size, reduced_tx_set_used);
480
1.71k
  if (is_inter) {
481
0
    if (cpi->oxcf.txfm_cfg.use_inter_dct_only) {
482
0
      assert(tx_type == DCT_DCT);
483
0
    }
484
1.71k
  } else {
485
1.71k
    if (cpi->oxcf.txfm_cfg.use_intra_dct_only) {
486
0
      assert(tx_type == DCT_DCT);
487
1.71k
    } else if (cpi->oxcf.txfm_cfg.use_intra_default_tx_only) {
488
0
      const TX_TYPE default_type = get_default_tx_type(
489
0
          PLANE_TYPE_Y, xd, tx_size, cpi->use_screen_content_tools);
490
0
      (void)default_type;
491
      // TODO(kyslov): We don't always respect use_intra_default_tx_only flag in
492
      // NonRD and REALTIME case. Specifically we ignore it in hybrid inta mode
493
      // search, when picking up intra mode in nonRD inter mode search and in RD
494
      // REALTIME mode when we limit TX type usage.
495
      // We need to fix txfm cfg for these cases. Meanwhile relieving the
496
      // assert.
497
0
      assert(tx_type == default_type || cpi->sf.rt_sf.use_nonrd_pick_mode ||
498
0
             cpi->oxcf.mode == REALTIME);
499
0
    }
500
1.71k
  }
501
502
1.71k
  if (get_ext_tx_types(tx_size, is_inter, reduced_tx_set_used) > 1 &&
503
1.71k
      cm->quant_params.base_qindex > 0 && !mbmi->skip_txfm &&
504
1.71k
      !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
505
33
    const int eset = get_ext_tx_set(tx_size, is_inter, reduced_tx_set_used);
506
33
    if (eset > 0) {
507
33
      const TxSetType tx_set_type =
508
33
          av1_get_ext_tx_set_type(tx_size, is_inter, reduced_tx_set_used);
509
33
      if (is_inter) {
510
0
        if (allow_update_cdf) {
511
0
          update_cdf(fc->inter_ext_tx_cdf[eset][txsize_sqr_map[tx_size]],
512
0
                     av1_ext_tx_ind[tx_set_type][tx_type],
513
0
                     av1_num_ext_tx_set[tx_set_type]);
514
0
        }
515
#if CONFIG_ENTROPY_STATS
516
        ++counts->inter_ext_tx[eset][txsize_sqr_map[tx_size]]
517
                              [av1_ext_tx_ind[tx_set_type][tx_type]];
518
#endif  // CONFIG_ENTROPY_STATS
519
33
      } else {
520
33
        PREDICTION_MODE intra_dir;
521
33
        if (mbmi->filter_intra_mode_info.use_filter_intra)
522
1
          intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
523
1
                                             .filter_intra_mode];
524
32
        else
525
32
          intra_dir = mbmi->mode;
526
#if CONFIG_ENTROPY_STATS
527
        ++counts->intra_ext_tx[eset][txsize_sqr_map[tx_size]][intra_dir]
528
                              [av1_ext_tx_ind[tx_set_type][tx_type]];
529
#endif  // CONFIG_ENTROPY_STATS
530
33
        if (allow_update_cdf) {
531
33
          update_cdf(
532
33
              fc->intra_ext_tx_cdf[eset][txsize_sqr_map[tx_size]][intra_dir],
533
33
              av1_ext_tx_ind[tx_set_type][tx_type],
534
33
              av1_num_ext_tx_set[tx_set_type]);
535
33
        }
536
33
      }
537
33
    }
538
33
  }
539
1.71k
}
540
541
void av1_update_and_record_txb_context(int plane, int block, int blk_row,
542
                                       int blk_col, BLOCK_SIZE plane_bsize,
543
1.27M
                                       TX_SIZE tx_size, void *arg) {
544
1.27M
  struct tokenize_b_args *const args = arg;
545
1.27M
  const AV1_COMP *cpi = args->cpi;
546
1.27M
  const AV1_COMMON *cm = &cpi->common;
547
1.27M
  ThreadData *const td = args->td;
548
1.27M
  MACROBLOCK *const x = &td->mb;
549
1.27M
  MACROBLOCKD *const xd = &x->e_mbd;
550
1.27M
  struct macroblock_plane *p = &x->plane[plane];
551
1.27M
  struct macroblockd_plane *pd = &xd->plane[plane];
552
1.27M
  const int eob = p->eobs[block];
553
1.27M
  const int block_offset = BLOCK_OFFSET(block);
554
1.27M
  tran_low_t *qcoeff = p->qcoeff + block_offset;
555
1.27M
  const PLANE_TYPE plane_type = pd->plane_type;
556
1.27M
  const TX_TYPE tx_type =
557
1.27M
      av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
558
1.27M
                      cm->features.reduced_tx_set_used);
559
1.27M
  const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
560
1.27M
  tran_low_t *tcoeff;
561
1.27M
  assert(args->dry_run != DRY_RUN_COSTCOEFFS);
562
1.27M
  if (args->dry_run == OUTPUT_ENABLED) {
563
792k
    MB_MODE_INFO *mbmi = xd->mi[0];
564
792k
    TXB_CTX txb_ctx;
565
792k
    get_txb_ctx(plane_bsize, tx_size, plane,
566
792k
                pd->above_entropy_context + blk_col,
567
792k
                pd->left_entropy_context + blk_row, &txb_ctx);
568
792k
    const int bwl = get_txb_bwl(tx_size);
569
792k
    const int width = get_txb_wide(tx_size);
570
792k
    const int height = get_txb_high(tx_size);
571
792k
    const uint8_t allow_update_cdf = args->allow_update_cdf;
572
792k
    const TX_SIZE txsize_ctx = get_txsize_entropy_ctx(tx_size);
573
792k
    FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
574
#if CONFIG_ENTROPY_STATS
575
    int cdf_idx = cm->coef_cdf_category;
576
    ++td->counts->txb_skip[cdf_idx][txsize_ctx][txb_ctx.txb_skip_ctx][eob == 0];
577
#endif  // CONFIG_ENTROPY_STATS
578
792k
    if (allow_update_cdf) {
579
792k
      update_cdf(ec_ctx->txb_skip_cdf[txsize_ctx][txb_ctx.txb_skip_ctx],
580
792k
                 eob == 0, 2);
581
792k
    }
582
583
792k
    CB_COEFF_BUFFER *cb_coef_buff = x->cb_coef_buff;
584
792k
    const int txb_offset = x->mbmi_ext_frame->cb_offset[plane_type] /
585
792k
                           (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
586
792k
    uint16_t *eob_txb = cb_coef_buff->eobs[plane] + txb_offset;
587
792k
    uint8_t *const entropy_ctx = cb_coef_buff->entropy_ctx[plane] + txb_offset;
588
792k
    entropy_ctx[block] = txb_ctx.txb_skip_ctx;
589
792k
    eob_txb[block] = eob;
590
591
792k
    if (eob == 0) {
592
786k
      av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, 0, blk_col,
593
786k
                               blk_row);
594
786k
      return;
595
786k
    }
596
5.54k
    const int segment_id = mbmi->segment_id;
597
5.54k
    const int seg_eob = av1_get_tx_eob(&cpi->common.seg, segment_id, tx_size);
598
5.54k
    tran_low_t *tcoeff_txb =
599
5.54k
        cb_coef_buff->tcoeff[plane] + x->mbmi_ext_frame->cb_offset[plane_type];
600
5.54k
    tcoeff = tcoeff_txb + block_offset;
601
5.54k
    memcpy(tcoeff, qcoeff, sizeof(*tcoeff) * seg_eob);
602
603
5.54k
    uint8_t levels_buf[TX_PAD_2D];
604
5.54k
    uint8_t *const levels = set_levels(levels_buf, width);
605
5.54k
    av1_txb_init_levels(tcoeff, width, height, levels);
606
5.54k
    update_tx_type_count(cpi, cm, xd, blk_row, blk_col, plane, tx_size,
607
5.54k
                         td->counts, allow_update_cdf);
608
609
5.54k
    const TX_CLASS tx_class = tx_type_to_class[tx_type];
610
5.54k
    const int16_t *const scan = scan_order->scan;
611
612
    // record tx type usage
613
5.54k
    td->rd_counts.tx_type_used[tx_size][tx_type]++;
614
615
#if CONFIG_ENTROPY_STATS
616
    av1_update_eob_context(cdf_idx, eob, tx_size, tx_class, plane_type, ec_ctx,
617
                           td->counts, allow_update_cdf);
618
#else
619
5.54k
    av1_update_eob_context(eob, tx_size, tx_class, plane_type, ec_ctx,
620
5.54k
                           allow_update_cdf);
621
5.54k
#endif
622
623
5.54k
    DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
624
5.54k
    av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class,
625
5.54k
                            coeff_contexts);
626
627
10.7k
    for (int c = eob - 1; c >= 0; --c) {
628
5.23k
      const int pos = scan[c];
629
5.23k
      const int coeff_ctx = coeff_contexts[pos];
630
5.23k
      const tran_low_t v = qcoeff[pos];
631
5.23k
      const tran_low_t level = abs(v);
632
5.23k
      td->abs_sum_level += level;
633
634
5.23k
      if (allow_update_cdf) {
635
5.23k
        if (c == eob - 1) {
636
5.23k
          assert(coeff_ctx < 4);
637
5.23k
          update_cdf(
638
5.23k
              ec_ctx->coeff_base_eob_cdf[txsize_ctx][plane_type][coeff_ctx],
639
5.23k
              AOMMIN(level, 3) - 1, 3);
640
5.23k
        } else {
641
0
          update_cdf(ec_ctx->coeff_base_cdf[txsize_ctx][plane_type][coeff_ctx],
642
0
                     AOMMIN(level, 3), 4);
643
0
        }
644
5.23k
      }
645
5.23k
      if (c == eob - 1) {
646
5.23k
        assert(coeff_ctx < 4);
647
#if CONFIG_ENTROPY_STATS
648
        ++td->counts->coeff_base_eob_multi[cdf_idx][txsize_ctx][plane_type]
649
                                          [coeff_ctx][AOMMIN(level, 3) - 1];
650
      } else {
651
        ++td->counts->coeff_base_multi[cdf_idx][txsize_ctx][plane_type]
652
                                      [coeff_ctx][AOMMIN(level, 3)];
653
#endif
654
5.23k
      }
655
5.23k
      if (level > NUM_BASE_LEVELS) {
656
3.78k
        const int base_range = level - 1 - NUM_BASE_LEVELS;
657
3.78k
        const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
658
18.8k
        for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
659
15.1k
          const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
660
15.1k
          if (allow_update_cdf) {
661
15.1k
            update_cdf(ec_ctx->coeff_br_cdf[AOMMIN(txsize_ctx, TX_32X32)]
662
15.1k
                                           [plane_type][br_ctx],
663
15.1k
                       k, BR_CDF_SIZE);
664
15.1k
          }
665
60.3k
          for (int lps = 0; lps < BR_CDF_SIZE - 1; lps++) {
666
#if CONFIG_ENTROPY_STATS
667
            ++td->counts->coeff_lps[AOMMIN(txsize_ctx, TX_32X32)][plane_type]
668
                                   [lps][br_ctx][lps == k];
669
#endif  // CONFIG_ENTROPY_STATS
670
45.2k
            if (lps == k) break;
671
45.2k
          }
672
#if CONFIG_ENTROPY_STATS
673
          ++td->counts->coeff_lps_multi[cdf_idx][AOMMIN(txsize_ctx, TX_32X32)]
674
                                       [plane_type][br_ctx][k];
675
#endif
676
15.1k
          if (k < BR_CDF_SIZE - 1) break;
677
15.1k
        }
678
3.78k
      }
679
5.23k
    }
680
    // Update the context needed to code the DC sign (if applicable)
681
5.54k
    if (tcoeff[0] != 0) {
682
5.23k
      const int dc_sign = (tcoeff[0] < 0) ? 1 : 0;
683
5.23k
      const int dc_sign_ctx = txb_ctx.dc_sign_ctx;
684
#if CONFIG_ENTROPY_STATS
685
      ++td->counts->dc_sign[plane_type][dc_sign_ctx][dc_sign];
686
#endif  // CONFIG_ENTROPY_STATS
687
5.23k
      if (allow_update_cdf)
688
5.23k
        update_cdf(ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx], dc_sign, 2);
689
5.23k
      entropy_ctx[block] |= dc_sign_ctx << DC_SIGN_CTX_SHIFT;
690
5.23k
    }
691
485k
  } else {
692
485k
    tcoeff = qcoeff;
693
485k
  }
694
490k
  const uint8_t cul_level =
695
490k
      av1_get_txb_entropy_context(tcoeff, scan_order, eob);
696
490k
  av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level,
697
490k
                           blk_col, blk_row);
698
490k
}
699
700
void av1_update_intra_mb_txb_context(const AV1_COMP *cpi, ThreadData *td,
701
                                     RUN_TYPE dry_run, BLOCK_SIZE bsize,
702
61.6k
                                     uint8_t allow_update_cdf) {
703
61.6k
  const AV1_COMMON *const cm = &cpi->common;
704
61.6k
  const int num_planes = av1_num_planes(cm);
705
61.6k
  MACROBLOCK *const x = &td->mb;
706
61.6k
  MACROBLOCKD *const xd = &x->e_mbd;
707
61.6k
  MB_MODE_INFO *const mbmi = xd->mi[0];
708
61.6k
  struct tokenize_b_args arg = { cpi, td, 0, allow_update_cdf, dry_run };
709
61.6k
  if (mbmi->skip_txfm) {
710
0
    av1_reset_entropy_context(xd, bsize, num_planes);
711
0
    return;
712
0
  }
713
714
237k
  for (int plane = 0; plane < num_planes; ++plane) {
715
180k
    if (plane && !xd->is_chroma_ref) break;
716
175k
    const struct macroblockd_plane *const pd = &xd->plane[plane];
717
175k
    const int ss_x = pd->subsampling_x;
718
175k
    const int ss_y = pd->subsampling_y;
719
175k
    const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y);
720
175k
    av1_foreach_transformed_block_in_plane(
721
175k
        xd, plane_bsize, plane, av1_update_and_record_txb_context, &arg);
722
175k
  }
723
61.6k
}
724
725
CB_COEFF_BUFFER *av1_get_cb_coeff_buffer(const struct AV1_COMP *cpi, int mi_row,
726
23.5k
                                         int mi_col) {
727
23.5k
  const AV1_COMMON *const cm = &cpi->common;
728
23.5k
  const int mib_size_log2 = cm->seq_params->mib_size_log2;
729
23.5k
  const int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1;
730
23.5k
  const int offset =
731
23.5k
      (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
732
23.5k
  return cpi->coeff_buffer_base + offset;
733
23.5k
}