Coverage Report

Created: 2026-11-12 08:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/av1/encoder/encodetxb.h
Line
Count
Source
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
#ifndef AOM_AV1_ENCODER_ENCODETXB_H_
13
#define AOM_AV1_ENCODER_ENCODETXB_H_
14
15
#include "config/aom_config.h"
16
17
#include "av1/common/av1_common_int.h"
18
#include "av1/common/blockd.h"
19
#include "av1/common/txb_common.h"
20
#include "av1/encoder/block.h"
21
#include "av1/encoder/encoder.h"
22
#include "aom_dsp/bitwriter.h"
23
#ifdef __cplusplus
24
extern "C" {
25
#endif
26
27
/*!\cond */
28
0
#define TXB_SKIP_CTX_MASK 15
29
0
#define DC_SIGN_CTX_SHIFT 4
30
0
#define DC_SIGN_CTX_MASK 3
31
32
int av1_get_eob_pos_token(const int eob, int *const extra);
33
34
/*!\endcond */
35
/*!\brief Allocate the memory resources for all the macro blocks in the current
36
 * coding frame.
37
 * \ingroup coefficient_coding
38
 *
39
 * Each macro block will need a \ref CB_COEFF_BUFFER to store information for
40
 * rate-distortion optimization and entropy coding of transform coefficients.
41
 *
42
 * \param[in]    cpi            Top-level encoder structure
43
 */
44
void av1_alloc_txb_buf(AV1_COMP *cpi);
45
/*!\brief Free the memory resources for all the macro blocks in the current
46
 * coding frame.
47
 * \ingroup coefficient_coding
48
 *
49
 * See \ref av1_alloc_txb_buf and \ref CB_COEFF_BUFFER for more details.
50
 *
51
 * \param[in]    cpi            Top-level encoder structure
52
 */
53
void av1_free_txb_buf(AV1_COMP *cpi);
54
55
/*!\brief Write quantized coefficients in a transform block into bitstream using
56
 * entropy coding.
57
 *
58
 * \ingroup coefficient_coding
59
 *
60
 * This function will write the quantized coefficients in a transform block into
61
 * the bitstream using entropy coding.
62
 *
63
 * The coding steps are as follows.
64
 *
65
 * 1) Code the end of block position "eob", which is the scan index of the
66
 * last non-zero coefficient plus one.
67
 *
68
 * 2) Code the lower magnitude level (<= COEFF_BASE_RANGE + NUM_BASE_LEVELS)
69
 * for each coefficient in reversed scan order.
70
 *
71
 * 3) Code the sign and higher magnitude level
72
 * (> COEFF_BASE_RANGE + NUM_BASE_LEVELS) in forward scan order.
73
 *
74
 * \param[in]    cm             Top-level structure shared by encoder and
75
 * decoder
76
 * \param[in]    x              Pointer to structure holding the data for the
77
                                current encoding macroblock
78
 * \param[in]    w              Entropy coding write pointer
79
 * \param[in]    blk_row      The row index of the current transform block
80
 * in the macroblock. Each unit has 4 pixels in y plane
81
 * \param[in]    blk_col      The col index of the current transform block
82
 * in the macroblock. Each unit has 4 pixels in y plane
83
 * \param[in]    plane          The index of the current plane
84
 * \param[in]    block          The index of the current transform block in the
85
 * macroblock. It's defined by number of 4x4 units that have been coded before
86
 * the currernt transform block
87
 * \param[in]    tx_size        The given transform size
88
 */
89
void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCK *const x,
90
                          aom_writer *w, int blk_row, int blk_col, int plane,
91
                          int block, TX_SIZE tx_size);
92
93
/*!\brief Write quantized coefficients of all transform blocks in an intra
94
 * macroblock into the bitstream using entropy coding.
95
 *
96
 * \ingroup coefficient_coding
97
 *
98
 * All transform blocks in the intra macroblock share the same transform size.
99
 *
100
 * This function use \ref av1_write_coeffs_txb() to code each transform block in
101
 * raster order.
102
 *
103
 * \param[in]    cm             Top-level structure shared by encoder and
104
 * decoder
105
 * \param[in]    x              Pointer to structure holding the data for the
106
                                current encoding macroblock
107
 * \param[in]    w              Entropy coding write pointer
108
 * \param[in]    bsize          Block size of the current macroblock
109
 */
110
void av1_write_intra_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x,
111
                               aom_writer *w, BLOCK_SIZE bsize);
112
113
/*!\brief Pack the context info of the current transform block into an uint8_t.
114
 * \ingroup coefficient_coding
115
 *
116
 * This context info will be collected and consolidated by its neighbor
117
 * transform blocks for coding transform block skip flag (tx_skip) and
118
 * the sign of DC coefficient (dc_sign).
119
 *
120
 * \param[in]    qcoeff         Buffer of quantized coefficients
121
 * \param[in]    scan_order     Coding order of coefficients in the transform
122
 * block
123
 * \param[in]    eob            The scan index of last non-zero coefficient plus
124
 * one
125
 */
126
static inline uint8_t av1_get_txb_entropy_context(const tran_low_t *qcoeff,
127
                                                  const SCAN_ORDER *scan_order,
128
0
                                                  int eob) {
129
0
  if (eob == 0) return 0;
130
0
  const int32_t dc_val = qcoeff[0];
131
0
  int cul_level = abs(dc_val);
132
0
  if (cul_level < COEFF_CONTEXT_MASK && eob > 1) {
133
0
    const int16_t *const scan = scan_order->scan;
134
0
    int c = 1;
135
0
    for (; c + 3 < eob; c += 4) {
136
0
      cul_level += abs(qcoeff[scan[c]]) + abs(qcoeff[scan[c + 1]]) +
137
0
                   abs(qcoeff[scan[c + 2]]) + abs(qcoeff[scan[c + 3]]);
138
0
      if (cul_level >= COEFF_CONTEXT_MASK) break;
139
0
    }
140
0
    if (cul_level < COEFF_CONTEXT_MASK) {
141
0
      for (; c < eob; ++c) {
142
0
        cul_level += abs(qcoeff[scan[c]]);
143
0
      }
144
0
    }
145
0
  }
146
147
0
  cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
148
0
  set_dc_sign(&cul_level, dc_val);
149
150
0
  return (uint8_t)cul_level;
151
0
}
Unexecuted instantiation: encodetxb.c:av1_get_txb_entropy_context
Unexecuted instantiation: rd.c:av1_get_txb_entropy_context
Unexecuted instantiation: rdopt.c:av1_get_txb_entropy_context
Unexecuted instantiation: tokenize.c:av1_get_txb_entropy_context
Unexecuted instantiation: tpl_model.c:av1_get_txb_entropy_context
Unexecuted instantiation: txb_rdopt.c:av1_get_txb_entropy_context
Unexecuted instantiation: var_based_part.c:av1_get_txb_entropy_context
Unexecuted instantiation: rdopt_sse4.c:av1_get_txb_entropy_context
Unexecuted instantiation: rdopt_avx2.c:av1_get_txb_entropy_context
Unexecuted instantiation: bitstream.c:av1_get_txb_entropy_context
Unexecuted instantiation: compound_type.c:av1_get_txb_entropy_context
Unexecuted instantiation: encodeframe.c:av1_get_txb_entropy_context
Unexecuted instantiation: encodeframe_utils.c:av1_get_txb_entropy_context
Unexecuted instantiation: encodemb.c:av1_get_txb_entropy_context
Unexecuted instantiation: encode_strategy.c:av1_get_txb_entropy_context
Unexecuted instantiation: encoder.c:av1_get_txb_entropy_context
Unexecuted instantiation: encoder_utils.c:av1_get_txb_entropy_context
Unexecuted instantiation: ethread.c:av1_get_txb_entropy_context
Unexecuted instantiation: firstpass.c:av1_get_txb_entropy_context
Unexecuted instantiation: global_motion_facade.c:av1_get_txb_entropy_context
Unexecuted instantiation: mcomp.c:av1_get_txb_entropy_context
Unexecuted instantiation: motion_search_facade.c:av1_get_txb_entropy_context
Unexecuted instantiation: partition_search.c:av1_get_txb_entropy_context
Unexecuted instantiation: partition_strategy.c:av1_get_txb_entropy_context
Unexecuted instantiation: ratectrl.c:av1_get_txb_entropy_context
Unexecuted instantiation: nonrd_pickmode.c:av1_get_txb_entropy_context
Unexecuted instantiation: nonrd_opt.c:av1_get_txb_entropy_context
Unexecuted instantiation: speed_features.c:av1_get_txb_entropy_context
Unexecuted instantiation: superres_scale.c:av1_get_txb_entropy_context
Unexecuted instantiation: svc_layercontext.c:av1_get_txb_entropy_context
Unexecuted instantiation: aq_cyclicrefresh.c:av1_get_txb_entropy_context
Unexecuted instantiation: av1_cx_iface.c:av1_get_txb_entropy_context
152
153
/*!\brief Update the probability model (cdf) and the entropy context related to
154
 * coefficient coding for all transform blocks in the intra macroblock.
155
 *
156
 * \ingroup coefficient_coding
157
 *
158
 * This function will go through each transform block in the intra macorblock
159
 * and call \ref av1_update_and_record_txb_context to update the probability
160
 * model and entropy context properly.
161
 *
162
 * \param[in]    cpi               Top-level encoder structure
163
 * \param[in]    td                Top-level multithreading structure
164
 * \param[in]    dry_run           Whether this is a dry run.
165
 * \param[in]    bsize             Block size of the current macroblock
166
 * \param[in]    allow_update_cdf  Allowed to update probability model (cdf) or
167
 * not.
168
 */
169
void av1_update_intra_mb_txb_context(const AV1_COMP *cpi, ThreadData *td,
170
                                     RUN_TYPE dry_run, BLOCK_SIZE bsize,
171
                                     uint8_t allow_update_cdf);
172
173
/*!\brief Update the probability model (cdf) and the entropy context related to
174
 * coefficient coding for a transform block.
175
 *
176
 * \ingroup coefficient_coding
177
 *
178
 * There are regular mode and dry run for this funtion.
179
 *
180
 * Regular mode:
181
 *
182
 * The probability model (cdf) for each coding symbol in the
183
 * transform block will be updated.
184
 *
185
 * The entropy context of this transform block will be updated.
186
 *
187
 * Dry run:
188
 *
189
 * The probability model update will be skipped.
190
 *
191
 * The entropy context of this transform block will be updated.
192
 *
193
 * \param[in]    plane        The index of the current plane.
194
 * \param[in]    block        The index of the current transform block in the
195
 * macroblock. It's defined by number of 4x4 units that have been coded before
196
 * the currernt transform block.
197
 * \param[in]    blk_row      The row index of the current transform block
198
 * in the macroblock. Each unit has 4 pixels in y plane.
199
 * \param[in]    blk_col      The col index of the current transform block
200
 * in the macroblock. Each unit has 4 pixels in y plane.
201
 * \param[in]    plane_bsize  Block size for this plane. When the video source
202
 * uses chroma subsampling, the block size of UV planes will be smaller than the
203
 * block size of Y plane.
204
 * \param[in]    tx_size      The given transform size.
205
 * \param[in]    arg          This parameter will be translated into
206
 * tokenize_b_args, in which RUN_TYPE indicates using regular mode or dry run.
207
 */
208
void av1_update_and_record_txb_context(int plane, int block, int blk_row,
209
                                       int blk_col, BLOCK_SIZE plane_bsize,
210
                                       TX_SIZE tx_size, void *arg);
211
212
/*!\brief Update the entropy context related to coefficient coding for a
213
 * transform block.
214
 *
215
 * \ingroup coefficient_coding
216
 *
217
 * There are regular mode and dry run for this function.
218
 *
219
 * Regular mode:
220
 *
221
 * The entropy context of this transform block will be updated.
222
 *
223
 * Dry run:
224
 *
225
 * The probability model update will be skipped.
226
 *
227
 * The entropy context of this transform block will be updated.
228
 *
229
 * \param[in]    plane        The index of the current plane.
230
 * \param[in]    block        The index of the current transform block in the
231
 * macroblock. It's defined by number of 4x4 units that have been coded before
232
 * the currernt transform block.
233
 * \param[in]    blk_row      The row index of the current transform block
234
 * in the macroblock. Each unit has 4 pixels in y plane.
235
 * \param[in]    blk_col      The col index of the current transform block
236
 * in the macroblock. Each unit has 4 pixels in y plane.
237
 * \param[in]    plane_bsize  Block size for this plane. When the video source
238
 * uses chroma subsampling, the block size of UV planes will be smaller than the
239
 * block size of Y plane.
240
 * \param[in]    tx_size      The given transform size.
241
 * \param[in]    arg          This parameter will be translated into
242
 * tokenize_b_args, in which RUN_TYPE indicates using regular mode or dry run.
243
 */
244
void av1_record_txb_context(int plane, int block, int blk_row, int blk_col,
245
                            BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg);
246
247
/*!\brief Get the corresponding \ref CB_COEFF_BUFFER of the current macro block.
248
 *
249
 * \ingroup coefficient_coding
250
 *
251
 * The macroblock's location is described by mi_row and mi_col, row and column
252
 * mi indexes in the coding frame.
253
 *
254
 * Each mi unit is a 4x4 pixel block.
255
 *
256
 * \param[in]    cpi               Top-level encoder structure.
257
 * \param[in]    mi_row            Row mi index of the current transform block
258
 * in the frame.
259
 * \param[in]    mi_col           Column mi index of the current transform
260
 * block in the frame.
261
 * \return       CB_COEFF_BUFFER*  Pointer of \ref CB_COEFF_BUFFER associated
262
 * to this macroblock.
263
 */
264
CB_COEFF_BUFFER *av1_get_cb_coeff_buffer(const struct AV1_COMP *cpi, int mi_row,
265
                                         int mi_col);
266
267
/*!\brief Returns the entropy cost associated with skipping the current
268
 * transform block.
269
 *
270
 * \ingroup coefficient_coding
271
 *
272
 * \param[in]    coeff_costs    Table of entropy cost for coefficient coding.
273
 * \param[in]    txb_ctx        Context info for entropy coding transform block
274
 * skip flag (tx_skip) and the sign of DC coefficient (dc_sign).
275
 * \param[in]    plane          The index of the current plane
276
 * \param[in]    tx_size        The transform size
277
 */
278
static inline int av1_cost_skip_txb(const CoeffCosts *coeff_costs,
279
                                    const TXB_CTX *const txb_ctx, int plane,
280
0
                                    TX_SIZE tx_size) {
281
0
  const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
282
0
  const PLANE_TYPE plane_type = get_plane_type(plane);
283
0
  const LV_MAP_COEFF_COST *const coeff_costs_ =
284
0
      &coeff_costs->coeff_costs[txs_ctx][plane_type];
285
0
  return coeff_costs_->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
286
0
}
Unexecuted instantiation: encodetxb.c:av1_cost_skip_txb
Unexecuted instantiation: rd.c:av1_cost_skip_txb
Unexecuted instantiation: rdopt.c:av1_cost_skip_txb
Unexecuted instantiation: tokenize.c:av1_cost_skip_txb
Unexecuted instantiation: tpl_model.c:av1_cost_skip_txb
Unexecuted instantiation: txb_rdopt.c:av1_cost_skip_txb
Unexecuted instantiation: var_based_part.c:av1_cost_skip_txb
Unexecuted instantiation: rdopt_sse4.c:av1_cost_skip_txb
Unexecuted instantiation: rdopt_avx2.c:av1_cost_skip_txb
Unexecuted instantiation: bitstream.c:av1_cost_skip_txb
Unexecuted instantiation: compound_type.c:av1_cost_skip_txb
Unexecuted instantiation: encodeframe.c:av1_cost_skip_txb
Unexecuted instantiation: encodeframe_utils.c:av1_cost_skip_txb
Unexecuted instantiation: encodemb.c:av1_cost_skip_txb
Unexecuted instantiation: encode_strategy.c:av1_cost_skip_txb
Unexecuted instantiation: encoder.c:av1_cost_skip_txb
Unexecuted instantiation: encoder_utils.c:av1_cost_skip_txb
Unexecuted instantiation: ethread.c:av1_cost_skip_txb
Unexecuted instantiation: firstpass.c:av1_cost_skip_txb
Unexecuted instantiation: global_motion_facade.c:av1_cost_skip_txb
Unexecuted instantiation: mcomp.c:av1_cost_skip_txb
Unexecuted instantiation: motion_search_facade.c:av1_cost_skip_txb
Unexecuted instantiation: partition_search.c:av1_cost_skip_txb
Unexecuted instantiation: partition_strategy.c:av1_cost_skip_txb
Unexecuted instantiation: ratectrl.c:av1_cost_skip_txb
Unexecuted instantiation: nonrd_pickmode.c:av1_cost_skip_txb
Unexecuted instantiation: nonrd_opt.c:av1_cost_skip_txb
Unexecuted instantiation: speed_features.c:av1_cost_skip_txb
Unexecuted instantiation: superres_scale.c:av1_cost_skip_txb
Unexecuted instantiation: svc_layercontext.c:av1_cost_skip_txb
Unexecuted instantiation: aq_cyclicrefresh.c:av1_cost_skip_txb
Unexecuted instantiation: av1_cx_iface.c:av1_cost_skip_txb
287
288
/*!\cond */
289
// These numbers are empirically obtained.
290
static const int plane_rd_mult_chroma[REF_TYPES][PLANE_TYPES] = {
291
  { 17, 13 },
292
  { 16, 10 },
293
};
294
static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
295
  { 17, 20 },
296
  { 16, 20 },
297
};
298
/*!\endcond */
299
300
#ifdef __cplusplus
301
}
302
#endif
303
304
#endif  // AOM_AV1_ENCODER_ENCODETXB_H_