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 #define TXB_SKIP_CTX_MASK 15
29 #define DC_SIGN_CTX_SHIFT 4
30 #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 uint8_t av1_get_txb_entropy_context(const tran_low_t *qcoeff,
127 const SCAN_ORDER *scan_order, int eob);
128
129 /*!\brief Update the probability model (cdf) and the entropy context related to
130 * coefficient coding for all transform blocks in the intra macroblock.
131 *
132 * \ingroup coefficient_coding
133 *
134 * This function will go through each transform block in the intra macorblock
135 * and call \ref av1_update_and_record_txb_context to update the probability
136 * model and entropy context properly.
137 *
138 * \param[in] cpi Top-level encoder structure
139 * \param[in] td Top-level multithreading structure
140 * \param[in] dry_run Whether this is a dry run.
141 * \param[in] bsize Block size of the current macroblock
142 * \param[in] allow_update_cdf Allowed to update probability model (cdf) or
143 * not.
144 */
145 void av1_update_intra_mb_txb_context(const AV1_COMP *cpi, ThreadData *td,
146 RUN_TYPE dry_run, BLOCK_SIZE bsize,
147 uint8_t allow_update_cdf);
148
149 /*!\brief Update the probability model (cdf) and the entropy context related to
150 * coefficient coding for a transform block.
151 *
152 * \ingroup coefficient_coding
153 *
154 * There are regular mode and dry run for this funtion.
155 *
156 * Regular mode:
157 *
158 * The probability model (cdf) for each coding symbol in the
159 * transform block will be updated.
160 *
161 * The entropy context of this transform block will be updated.
162 *
163 * Dry run:
164 *
165 * The probability model update will be skipped.
166 *
167 * The entropy context of this transform block will be updated.
168 *
169 * \param[in] plane The index of the current plane.
170 * \param[in] block The index of the current transform block in the
171 * macroblock. It's defined by number of 4x4 units that have been coded before
172 * the currernt transform block.
173 * \param[in] blk_row The row index of the current transform block
174 * in the macroblock. Each unit has 4 pixels in y plane.
175 * \param[in] blk_col The col index of the current transform block
176 * in the macroblock. Each unit has 4 pixels in y plane.
177 * \param[in] plane_bsize Block size for this plane. When the video source
178 * uses chroma subsampling, the block size of UV planes will be smaller than the
179 * block size of Y plane.
180 * \param[in] tx_size The given transform size.
181 * \param[in] arg This parameter will be translated into
182 * tokenize_b_args, in which RUN_TYPE indicates using regular mode or dry run.
183 */
184 void av1_update_and_record_txb_context(int plane, int block, int blk_row,
185 int blk_col, BLOCK_SIZE plane_bsize,
186 TX_SIZE tx_size, void *arg);
187
188 /*!\brief Update the entropy context related to coefficient coding for a
189 * transform block.
190 *
191 * \ingroup coefficient_coding
192 *
193 * There are regular mode and dry run for this function.
194 *
195 * Regular mode:
196 *
197 * The entropy context of this transform block will be updated.
198 *
199 * Dry run:
200 *
201 * The probability model update will be skipped.
202 *
203 * The entropy context of this transform block will be updated.
204 *
205 * \param[in] plane The index of the current plane.
206 * \param[in] block The index of the current transform block in the
207 * macroblock. It's defined by number of 4x4 units that have been coded before
208 * the currernt transform block.
209 * \param[in] blk_row The row index of the current transform block
210 * in the macroblock. Each unit has 4 pixels in y plane.
211 * \param[in] blk_col The col index of the current transform block
212 * in the macroblock. Each unit has 4 pixels in y plane.
213 * \param[in] plane_bsize Block size for this plane. When the video source
214 * uses chroma subsampling, the block size of UV planes will be smaller than the
215 * block size of Y plane.
216 * \param[in] tx_size The given transform size.
217 * \param[in] arg This parameter will be translated into
218 * tokenize_b_args, in which RUN_TYPE indicates using regular mode or dry run.
219 */
220 void av1_record_txb_context(int plane, int block, int blk_row, int blk_col,
221 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg);
222
223 /*!\brief Get the corresponding \ref CB_COEFF_BUFFER of the current macro block.
224 *
225 * \ingroup coefficient_coding
226 *
227 * The macroblock's location is described by mi_row and mi_col, row and column
228 * mi indexes in the coding frame.
229 *
230 * Each mi unit is a 4x4 pixel block.
231 *
232 * \param[in] cpi Top-level encoder structure.
233 * \param[in] mi_row Row mi index of the current transform block
234 * in the frame.
235 * \param[in] mi_col Column mi index of the current transform
236 * block in the frame.
237 * \return CB_COEFF_BUFFER* Pointer of \ref CB_COEFF_BUFFER associated
238 * to this macroblock.
239 */
240 CB_COEFF_BUFFER *av1_get_cb_coeff_buffer(const struct AV1_COMP *cpi, int mi_row,
241 int mi_col);
242
243 /*!\brief Returns the entropy cost associated with skipping the current
244 * transform block.
245 *
246 * \ingroup coefficient_coding
247 *
248 * \param[in] coeff_costs Table of entropy cost for coefficient coding.
249 * \param[in] txb_ctx Context info for entropy coding transform block
250 * skip flag (tx_skip) and the sign of DC coefficient (dc_sign).
251 * \param[in] plane The index of the current plane
252 * \param[in] tx_size The transform size
253 */
av1_cost_skip_txb(const CoeffCosts * coeff_costs,const TXB_CTX * const txb_ctx,int plane,TX_SIZE tx_size)254 static inline int av1_cost_skip_txb(const CoeffCosts *coeff_costs,
255 const TXB_CTX *const txb_ctx, int plane,
256 TX_SIZE tx_size) {
257 const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
258 const PLANE_TYPE plane_type = get_plane_type(plane);
259 const LV_MAP_COEFF_COST *const coeff_costs_ =
260 &coeff_costs->coeff_costs[txs_ctx][plane_type];
261 return coeff_costs_->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
262 }
263
264 /*!\cond */
265 // These numbers are empirically obtained.
266 static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
267 { 17, 13 },
268 { 16, 10 },
269 };
270 /*!\endcond */
271
272 #ifdef __cplusplus
273 }
274 #endif
275
276 #endif // AOM_AV1_ENCODER_ENCODETXB_H_
277