/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 AV1_QUANT_FACADE |
380 | | quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = { |
381 | | { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade }, |
382 | | { av1_quantize_b_facade, av1_highbd_quantize_b_facade }, |
383 | | { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade }, |
384 | | { NULL, NULL } |
385 | | }; |
386 | | #else |
387 | | static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_TYPES] = { |
388 | | av1_quantize_fp_facade, av1_quantize_b_facade, av1_quantize_dc_facade, NULL |
389 | | }; |
390 | | #endif |
391 | | |
392 | | // Computes the transform for DC only blocks |
393 | | void av1_xform_dc_only(MACROBLOCK *x, int plane, int block, |
394 | 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 | quant_func_list[qparam->xform_quant_idx][txfm_param->is_hbd]( |
441 | 0 | coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, qparam); |
442 | | #else |
443 | | quant_func_list[qparam->xform_quant_idx]( |
444 | | coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, qparam); |
445 | | #endif |
446 | 0 | } else { |
447 | 0 | av1_quantize_skip(n_coeffs, qcoeff, dqcoeff, eob); |
448 | 0 | } |
449 | 0 | } |
450 | | // use_optimize_b is true means av1_optimze_b will be called, |
451 | | // thus cannot update entropy ctx now (performed in optimize_b) |
452 | 0 | if (qparam->use_optimize_b) { |
453 | 0 | p->txb_entropy_ctx[block] = 0; |
454 | 0 | } else { |
455 | 0 | p->txb_entropy_ctx[block] = |
456 | 0 | av1_get_txb_entropy_context(qcoeff, scan_order, *eob); |
457 | 0 | } |
458 | 0 | } |
459 | | |
460 | | void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size, |
461 | 0 | TX_TYPE tx_type, TxfmParam *txfm_param) { |
462 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
463 | 0 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
464 | |
|
465 | 0 | txfm_param->tx_type = tx_type; |
466 | 0 | txfm_param->tx_size = tx_size; |
467 | 0 | txfm_param->lossless = xd->lossless[mbmi->segment_id]; |
468 | 0 | txfm_param->tx_set_type = av1_get_ext_tx_set_type( |
469 | 0 | tx_size, is_inter_block(mbmi), cm->features.reduced_tx_set_used); |
470 | |
|
471 | 0 | txfm_param->bd = xd->bd; |
472 | 0 | txfm_param->is_hbd = is_cur_buf_hbd(xd); |
473 | 0 | } |
474 | | void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx, |
475 | 0 | int use_quant_b_adapt, QUANT_PARAM *qparam) { |
476 | 0 | qparam->log_scale = av1_get_tx_scale(tx_size); |
477 | 0 | qparam->tx_size = tx_size; |
478 | |
|
479 | 0 | qparam->use_quant_b_adapt = use_quant_b_adapt; |
480 | | |
481 | | // TODO(bohanli): optimize_b and quantization idx has relationship, |
482 | | // but is kind of buried and complicated in different encoding stages. |
483 | | // Should have a unified function to derive quant_idx, rather than |
484 | | // determine and pass in the quant_idx |
485 | 0 | qparam->use_optimize_b = use_optimize_b; |
486 | 0 | qparam->xform_quant_idx = xform_quant_idx; |
487 | |
|
488 | 0 | qparam->qmatrix = NULL; |
489 | 0 | qparam->iqmatrix = NULL; |
490 | 0 | } |
491 | | void av1_setup_qmatrix(const CommonQuantParams *quant_params, |
492 | | const MACROBLOCKD *xd, int plane, TX_SIZE tx_size, |
493 | 0 | TX_TYPE tx_type, QUANT_PARAM *qparam) { |
494 | 0 | qparam->qmatrix = av1_get_qmatrix(quant_params, xd, plane, tx_size, tx_type); |
495 | 0 | qparam->iqmatrix = |
496 | 0 | av1_get_iqmatrix(quant_params, xd, plane, tx_size, tx_type); |
497 | 0 | } |
498 | | |
499 | | static void encode_block(int plane, int block, int blk_row, int blk_col, |
500 | | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg, |
501 | 0 | RUN_TYPE dry_run) { |
502 | 0 | (void)dry_run; |
503 | 0 | struct encode_b_args *const args = arg; |
504 | 0 | const AV1_COMP *const cpi = args->cpi; |
505 | 0 | const AV1_COMMON *const cm = &cpi->common; |
506 | 0 | MACROBLOCK *const x = args->x; |
507 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
508 | 0 | MB_MODE_INFO *mbmi = xd->mi[0]; |
509 | 0 | struct macroblock_plane *const p = &x->plane[plane]; |
510 | 0 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
511 | 0 | tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block); |
512 | 0 | uint8_t *dst; |
513 | 0 | ENTROPY_CONTEXT *a, *l; |
514 | 0 | int dummy_rate_cost = 0; |
515 | |
|
516 | 0 | dst = &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2]; |
517 | |
|
518 | 0 | a = &args->ta[blk_col]; |
519 | 0 | l = &args->tl[blk_row]; |
520 | |
|
521 | 0 | TX_TYPE tx_type = DCT_DCT; |
522 | 0 | if (!mbmi->skip_mode) { |
523 | 0 | tx_type = av1_get_tx_type(xd, pd->plane_type, blk_row, blk_col, tx_size, |
524 | 0 | cm->features.reduced_tx_set_used); |
525 | |
|
526 | 0 | av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size, tx_type, |
527 | 0 | cpi->do_border_pad); |
528 | 0 | TxfmParam txfm_param; |
529 | 0 | QUANT_PARAM quant_param; |
530 | 0 | const int use_trellis = is_trellis_used(args->enable_optimize_b, dry_run); |
531 | 0 | int quant_idx; |
532 | 0 | if (use_trellis) |
533 | 0 | quant_idx = AV1_XFORM_QUANT_FP; |
534 | 0 | else |
535 | 0 | quant_idx = |
536 | 0 | USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP; |
537 | 0 | av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param); |
538 | 0 | av1_setup_quant(tx_size, use_trellis, quant_idx, |
539 | 0 | cpi->oxcf.q_cfg.quant_b_adapt, &quant_param); |
540 | 0 | av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type, |
541 | 0 | &quant_param); |
542 | 0 | av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param, |
543 | 0 | &quant_param); |
544 | 0 | if (use_trellis) { |
545 | 0 | TXB_CTX txb_ctx; |
546 | 0 | get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx); |
547 | 0 | av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx, |
548 | 0 | &dummy_rate_cost); |
549 | 0 | } |
550 | 0 | } else { |
551 | 0 | p->eobs[block] = 0; |
552 | 0 | p->txb_entropy_ctx[block] = 0; |
553 | 0 | } |
554 | |
|
555 | 0 | av1_set_txb_context(x, plane, block, tx_size, a, l); |
556 | |
|
557 | 0 | if (p->eobs[block]) { |
558 | | // As long as any YUV plane has non-zero quantized transform coefficients, |
559 | | // mbmi->skip_txfm flag is set to 0. |
560 | 0 | mbmi->skip_txfm = 0; |
561 | 0 | av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst, |
562 | 0 | pd->dst.stride, p->eobs[block], |
563 | 0 | cm->features.reduced_tx_set_used); |
564 | 0 | } else { |
565 | | // Only when YUV planes all have zero quantized transform coefficients, |
566 | | // mbmi->skip_txfm flag is set to 1. |
567 | 0 | mbmi->skip_txfm &= 1; |
568 | 0 | } |
569 | | |
570 | | // TODO(debargha, jingning): Temporarily disable txk_type check for eob=0 |
571 | | // case. It is possible that certain collision in hash index would cause |
572 | | // the assertion failure. To further optimize the rate-distortion |
573 | | // performance, we need to re-visit this part and enable this assert |
574 | | // again. |
575 | 0 | if (p->eobs[block] == 0 && plane == 0) { |
576 | | #if 0 |
577 | | if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ && |
578 | | args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) { |
579 | | // TODO(jingning,angiebird,huisu@google.com): enable txk_check when |
580 | | // enable_optimize_b is true to detect potential RD bug. |
581 | | const uint8_t disable_txk_check = args->enable_optimize_b; |
582 | | if (!disable_txk_check) { |
583 | | assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] == |
584 | | DCT_DCT); |
585 | | } |
586 | | } |
587 | | #endif |
588 | 0 | update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT); |
589 | 0 | } |
590 | |
|
591 | | #if CONFIG_MISMATCH_DEBUG |
592 | | if (dry_run == OUTPUT_ENABLED) { |
593 | | int pixel_c, pixel_r; |
594 | | BLOCK_SIZE bsize = txsize_to_bsize[tx_size]; |
595 | | int blk_w = block_size_wide[bsize]; |
596 | | int blk_h = block_size_high[bsize]; |
597 | | mi_to_pixel_loc(&pixel_c, &pixel_r, xd->mi_col, xd->mi_row, blk_col, |
598 | | blk_row, pd->subsampling_x, pd->subsampling_y); |
599 | | mismatch_record_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint, |
600 | | plane, pixel_c, pixel_r, blk_w, blk_h, |
601 | | xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH); |
602 | | } |
603 | | #endif |
604 | 0 | } |
605 | | |
606 | | static void encode_block_inter(int plane, int block, int blk_row, int blk_col, |
607 | | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, |
608 | 0 | void *arg, RUN_TYPE dry_run) { |
609 | 0 | struct encode_b_args *const args = arg; |
610 | 0 | MACROBLOCK *const x = args->x; |
611 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
612 | 0 | MB_MODE_INFO *const mbmi = xd->mi[0]; |
613 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
614 | 0 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
615 | 0 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
616 | |
|
617 | 0 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
618 | | |
619 | 0 | const TX_SIZE plane_tx_size = |
620 | 0 | plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x, |
621 | 0 | pd->subsampling_y) |
622 | 0 | : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row, |
623 | 0 | blk_col)]; |
624 | 0 | if (!plane) { |
625 | 0 | assert(tx_size_wide[tx_size] >= tx_size_wide[plane_tx_size] && |
626 | 0 | tx_size_high[tx_size] >= tx_size_high[plane_tx_size]); |
627 | 0 | } |
628 | | |
629 | 0 | if (tx_size == plane_tx_size || plane) { |
630 | 0 | encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg, |
631 | 0 | dry_run); |
632 | 0 | } else { |
633 | 0 | assert(tx_size < TX_SIZES_ALL); |
634 | 0 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
635 | 0 | assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size)); |
636 | 0 | assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size)); |
637 | | // This is the square transform block partition entry point. |
638 | 0 | const int bsw = tx_size_wide_unit[sub_txs]; |
639 | 0 | const int bsh = tx_size_high_unit[sub_txs]; |
640 | 0 | const int step = bsh * bsw; |
641 | 0 | const int row_end = |
642 | 0 | AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row); |
643 | 0 | const int col_end = |
644 | 0 | AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col); |
645 | 0 | assert(bsw > 0 && bsh > 0); |
646 | | |
647 | 0 | for (int row = 0; row < row_end; row += bsh) { |
648 | 0 | const int offsetr = blk_row + row; |
649 | 0 | for (int col = 0; col < col_end; col += bsw) { |
650 | 0 | const int offsetc = blk_col + col; |
651 | |
|
652 | 0 | encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs, |
653 | 0 | arg, dry_run); |
654 | 0 | block += step; |
655 | 0 | } |
656 | 0 | } |
657 | 0 | } |
658 | 0 | } |
659 | | |
660 | | void av1_foreach_transformed_block_in_plane( |
661 | | const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane, |
662 | 0 | foreach_transformed_block_visitor visit, void *arg) { |
663 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
664 | | // block and transform sizes, in number of 4x4 blocks log 2 ("*_b") |
665 | | // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8 |
666 | | // transform size varies per plane, look it up in a common way. |
667 | 0 | const TX_SIZE tx_size = av1_get_tx_size(plane, xd); |
668 | 0 | const BLOCK_SIZE tx_bsize = txsize_to_bsize[tx_size]; |
669 | | // Call visit() directly with zero offsets if the current block size is the |
670 | | // same as the transform block size. |
671 | 0 | if (plane_bsize == tx_bsize) { |
672 | 0 | visit(plane, 0, 0, 0, plane_bsize, tx_size, arg); |
673 | 0 | return; |
674 | 0 | } |
675 | 0 | const uint8_t txw_unit = tx_size_wide_unit[tx_size]; |
676 | 0 | const uint8_t txh_unit = tx_size_high_unit[tx_size]; |
677 | 0 | const int step = txw_unit * txh_unit; |
678 | | |
679 | | // If mb_to_right_edge is < 0 we are in a situation in which |
680 | | // the current block size extends into the UMV and we won't |
681 | | // visit the sub blocks that are wholly within the UMV. |
682 | 0 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
683 | 0 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
684 | 0 | const BLOCK_SIZE max_unit_bsize = |
685 | 0 | get_plane_block_size(BLOCK_64X64, pd->subsampling_x, pd->subsampling_y); |
686 | 0 | const int mu_blocks_wide = |
687 | 0 | AOMMIN(mi_size_wide[max_unit_bsize], max_blocks_wide); |
688 | 0 | const int mu_blocks_high = |
689 | 0 | AOMMIN(mi_size_high[max_unit_bsize], max_blocks_high); |
690 | | |
691 | | // Keep track of the row and column of the blocks we use so that we know |
692 | | // if we are in the unrestricted motion border. |
693 | 0 | int i = 0; |
694 | 0 | for (int r = 0; r < max_blocks_high; r += mu_blocks_high) { |
695 | 0 | const int unit_height = AOMMIN(mu_blocks_high + r, max_blocks_high); |
696 | | // Skip visiting the sub blocks that are wholly within the UMV. |
697 | 0 | for (int c = 0; c < max_blocks_wide; c += mu_blocks_wide) { |
698 | 0 | const int unit_width = AOMMIN(mu_blocks_wide + c, max_blocks_wide); |
699 | 0 | for (int blk_row = r; blk_row < unit_height; blk_row += txh_unit) { |
700 | 0 | for (int blk_col = c; blk_col < unit_width; blk_col += txw_unit) { |
701 | 0 | visit(plane, i, blk_row, blk_col, plane_bsize, tx_size, arg); |
702 | 0 | i += step; |
703 | 0 | } |
704 | 0 | } |
705 | 0 | } |
706 | 0 | } |
707 | | // Check if visit() is invoked at least once. |
708 | 0 | assert(i >= 1); |
709 | 0 | } |
710 | | |
711 | | typedef struct encode_block_pass1_args { |
712 | | AV1_COMP *cpi; |
713 | | MACROBLOCK *x; |
714 | | } encode_block_pass1_args; |
715 | | |
716 | | static void encode_block_pass1(int plane, int block, int blk_row, int blk_col, |
717 | | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, |
718 | 0 | void *arg) { |
719 | 0 | encode_block_pass1_args *args = (encode_block_pass1_args *)arg; |
720 | 0 | AV1_COMP *cpi = args->cpi; |
721 | 0 | AV1_COMMON *cm = &cpi->common; |
722 | 0 | MACROBLOCK *const x = args->x; |
723 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
724 | 0 | struct macroblock_plane *const p = &x->plane[plane]; |
725 | 0 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
726 | 0 | tran_low_t *const dqcoeff = p->dqcoeff + BLOCK_OFFSET(block); |
727 | |
|
728 | 0 | uint8_t *dst; |
729 | 0 | dst = &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2]; |
730 | |
|
731 | 0 | TxfmParam txfm_param; |
732 | 0 | QUANT_PARAM quant_param; |
733 | |
|
734 | 0 | av1_setup_xform(cm, x, tx_size, DCT_DCT, &txfm_param); |
735 | 0 | av1_setup_quant(tx_size, 0, AV1_XFORM_QUANT_B, cpi->oxcf.q_cfg.quant_b_adapt, |
736 | 0 | &quant_param); |
737 | 0 | av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, DCT_DCT, |
738 | 0 | &quant_param); |
739 | |
|
740 | 0 | av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param, |
741 | 0 | &quant_param); |
742 | |
|
743 | 0 | if (p->eobs[block] > 0) { |
744 | 0 | txfm_param.eob = p->eobs[block]; |
745 | 0 | if (txfm_param.is_hbd) { |
746 | 0 | av1_highbd_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param); |
747 | 0 | return; |
748 | 0 | } |
749 | 0 | av1_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param); |
750 | 0 | } |
751 | 0 | } |
752 | | |
753 | 0 | void av1_encode_sby_pass1(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize) { |
754 | 0 | encode_block_pass1_args args = { cpi, x }; |
755 | 0 | av1_subtract_plane(x, bsize, PLANE_TYPE_Y, cpi->do_border_pad); |
756 | 0 | av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0, |
757 | 0 | encode_block_pass1, &args); |
758 | 0 | } |
759 | | |
760 | | void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, |
761 | 0 | RUN_TYPE dry_run) { |
762 | 0 | assert(bsize < BLOCK_SIZES_ALL); |
763 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
764 | 0 | MB_MODE_INFO *mbmi = xd->mi[0]; |
765 | | // In the current encoder implementation, for inter blocks, |
766 | | // only when YUV planes all have zero quantized transform coefficients, |
767 | | // mbmi->skip_txfm flag is set to 1. |
768 | | // For intra blocks, this flag is set to 0 since skipped blocks are so rare |
769 | | // that transmitting skip_txfm = 1 is very expensive. |
770 | | // mbmi->skip_txfm is init to 1, and will be modified in encode_block() based |
771 | | // on transform, quantization, and (if exists) trellis optimization. |
772 | 0 | mbmi->skip_txfm = 1; |
773 | 0 | if (x->txfm_search_info.skip_txfm) return; |
774 | | |
775 | 0 | struct optimize_ctx ctx; |
776 | 0 | struct encode_b_args arg = { |
777 | 0 | cpi, x, &ctx, NULL, NULL, dry_run, cpi->optimize_seg_arr[mbmi->segment_id] |
778 | 0 | }; |
779 | 0 | const AV1_COMMON *const cm = &cpi->common; |
780 | 0 | const int num_planes = av1_num_planes(cm); |
781 | 0 | for (int plane = 0; plane < num_planes; ++plane) { |
782 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
783 | 0 | const int subsampling_x = pd->subsampling_x; |
784 | 0 | const int subsampling_y = pd->subsampling_y; |
785 | 0 | if (plane && !xd->is_chroma_ref) break; |
786 | 0 | const BLOCK_SIZE plane_bsize = |
787 | 0 | get_plane_block_size(bsize, subsampling_x, subsampling_y); |
788 | 0 | assert(plane_bsize < BLOCK_SIZES_ALL); |
789 | 0 | const int mi_width = mi_size_wide[plane_bsize]; |
790 | 0 | const int mi_height = mi_size_high[plane_bsize]; |
791 | 0 | const TX_SIZE max_tx_size = get_vartx_max_txsize(xd, plane_bsize, plane); |
792 | 0 | const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size]; |
793 | 0 | const int bw = mi_size_wide[txb_size]; |
794 | 0 | const int bh = mi_size_high[txb_size]; |
795 | 0 | int block = 0; |
796 | 0 | const int step = |
797 | 0 | tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size]; |
798 | 0 | av1_get_entropy_contexts(plane_bsize, pd, ctx.ta[plane], ctx.tl[plane]); |
799 | 0 | arg.ta = ctx.ta[plane]; |
800 | 0 | arg.tl = ctx.tl[plane]; |
801 | 0 | const BLOCK_SIZE max_unit_bsize = |
802 | 0 | get_plane_block_size(BLOCK_64X64, subsampling_x, subsampling_y); |
803 | 0 | int mu_blocks_wide = mi_size_wide[max_unit_bsize]; |
804 | 0 | int mu_blocks_high = mi_size_high[max_unit_bsize]; |
805 | 0 | mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide); |
806 | 0 | mu_blocks_high = AOMMIN(mi_height, mu_blocks_high); |
807 | |
|
808 | 0 | for (int idy = 0; idy < mi_height; idy += mu_blocks_high) { |
809 | 0 | for (int idx = 0; idx < mi_width; idx += mu_blocks_wide) { |
810 | 0 | int blk_row, blk_col; |
811 | 0 | const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height); |
812 | 0 | const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width); |
813 | 0 | for (blk_row = idy; blk_row < unit_height; blk_row += bh) { |
814 | 0 | for (blk_col = idx; blk_col < unit_width; blk_col += bw) { |
815 | 0 | encode_block_inter(plane, block, blk_row, blk_col, plane_bsize, |
816 | 0 | max_tx_size, &arg, dry_run); |
817 | 0 | block += step; |
818 | 0 | } |
819 | 0 | } |
820 | 0 | } |
821 | 0 | } |
822 | 0 | } |
823 | 0 | } |
824 | | |
825 | | static void encode_block_intra(int plane, int block, int blk_row, int blk_col, |
826 | | BLOCK_SIZE plane_bsize, TX_SIZE tx_size, |
827 | 0 | void *arg) { |
828 | 0 | struct encode_b_args *const args = arg; |
829 | 0 | const AV1_COMP *const cpi = args->cpi; |
830 | 0 | const AV1_COMMON *const cm = &cpi->common; |
831 | 0 | MACROBLOCK *const x = args->x; |
832 | 0 | MACROBLOCKD *const xd = &x->e_mbd; |
833 | 0 | struct macroblock_plane *const p = &x->plane[plane]; |
834 | 0 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
835 | 0 | tran_low_t *dqcoeff = p->dqcoeff + BLOCK_OFFSET(block); |
836 | 0 | PLANE_TYPE plane_type = get_plane_type(plane); |
837 | 0 | uint16_t *eob = &p->eobs[block]; |
838 | 0 | const int dst_stride = pd->dst.stride; |
839 | 0 | uint8_t *dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << MI_SIZE_LOG2]; |
840 | 0 | int dummy_rate_cost = 0; |
841 | |
|
842 | 0 | av1_predict_intra_block_facade(cm, xd, plane, blk_col, blk_row, tx_size); |
843 | |
|
844 | 0 | TX_TYPE tx_type = DCT_DCT; |
845 | 0 | if (xd->mi[0]->skip_txfm) { |
846 | 0 | *eob = 0; |
847 | 0 | p->txb_entropy_ctx[block] = 0; |
848 | 0 | } else { |
849 | 0 | const ENTROPY_CONTEXT *a = &args->ta[blk_col]; |
850 | 0 | const ENTROPY_CONTEXT *l = &args->tl[blk_row]; |
851 | 0 | tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size, |
852 | 0 | cm->features.reduced_tx_set_used); |
853 | 0 | TX_TYPE primary_tx_type = is_stat_generation_stage(cpi) ? DCT_DCT : tx_type; |
854 | 0 | av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size, |
855 | 0 | primary_tx_type, cpi->do_border_pad); |
856 | 0 | TxfmParam txfm_param; |
857 | 0 | QUANT_PARAM quant_param; |
858 | 0 | const int use_trellis = |
859 | 0 | is_trellis_used(args->enable_optimize_b, args->dry_run); |
860 | 0 | int quant_idx; |
861 | 0 | if (use_trellis) |
862 | 0 | quant_idx = AV1_XFORM_QUANT_FP; |
863 | 0 | else |
864 | 0 | quant_idx = |
865 | 0 | USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP; |
866 | |
|
867 | 0 | av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param); |
868 | 0 | av1_setup_quant(tx_size, use_trellis, quant_idx, |
869 | 0 | cpi->oxcf.q_cfg.quant_b_adapt, &quant_param); |
870 | 0 | av1_setup_qmatrix(&cm->quant_params, xd, plane, tx_size, tx_type, |
871 | 0 | &quant_param); |
872 | |
|
873 | 0 | av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param, |
874 | 0 | &quant_param); |
875 | 0 | if (use_trellis) { |
876 | 0 | TXB_CTX txb_ctx; |
877 | 0 | get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx); |
878 | 0 | av1_optimize_b(args->cpi, x, plane, block, tx_size, tx_type, &txb_ctx, |
879 | 0 | &dummy_rate_cost); |
880 | 0 | } |
881 | 0 | } |
882 | |
|
883 | 0 | if (*eob) { |
884 | 0 | av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst, |
885 | 0 | dst_stride, *eob, |
886 | 0 | cm->features.reduced_tx_set_used); |
887 | 0 | } |
888 | | |
889 | | // TODO(jingning): Temporarily disable txk_type check for eob=0 case. |
890 | | // It is possible that certain collision in hash index would cause |
891 | | // the assertion failure. To further optimize the rate-distortion |
892 | | // performance, we need to re-visit this part and enable this assert |
893 | | // again. |
894 | 0 | if (*eob == 0 && plane == 0) { |
895 | | #if 0 |
896 | | if (args->cpi->oxcf.q_cfg.aq_mode == NO_AQ |
897 | | && args->cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q) { |
898 | | assert(xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col)] == |
899 | | DCT_DCT); |
900 | | } |
901 | | #endif |
902 | 0 | update_txk_array(xd, blk_row, blk_col, tx_size, DCT_DCT); |
903 | 0 | } |
904 | |
|
905 | 0 | #if !CONFIG_REALTIME_ONLY |
906 | 0 | if (plane == AOM_PLANE_Y && xd->cfl.store_y) { |
907 | 0 | cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize); |
908 | 0 | } |
909 | 0 | #endif |
910 | 0 | } |
911 | | |
912 | | static void encode_block_intra_and_set_context(int plane, int block, |
913 | | int blk_row, int blk_col, |
914 | | BLOCK_SIZE plane_bsize, |
915 | 0 | TX_SIZE tx_size, void *arg) { |
916 | 0 | encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg); |
917 | |
|
918 | 0 | struct encode_b_args *const args = arg; |
919 | 0 | MACROBLOCK *x = args->x; |
920 | 0 | ENTROPY_CONTEXT *a = &args->ta[blk_col]; |
921 | 0 | ENTROPY_CONTEXT *l = &args->tl[blk_row]; |
922 | 0 | av1_set_txb_context(x, plane, block, tx_size, a, l); |
923 | 0 | } |
924 | | |
925 | | void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x, |
926 | | BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run, |
927 | 0 | TRELLIS_OPT_TYPE enable_optimize_b) { |
928 | 0 | assert(bsize < BLOCK_SIZES_ALL); |
929 | 0 | const MACROBLOCKD *const xd = &x->e_mbd; |
930 | 0 | if (plane && !xd->is_chroma_ref) return; |
931 | | |
932 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
933 | 0 | const int ss_x = pd->subsampling_x; |
934 | 0 | const int ss_y = pd->subsampling_y; |
935 | 0 | ENTROPY_CONTEXT ta[MAX_MIB_SIZE] = { 0 }; |
936 | 0 | ENTROPY_CONTEXT tl[MAX_MIB_SIZE] = { 0 }; |
937 | 0 | struct encode_b_args arg = { |
938 | 0 | cpi, x, NULL, ta, tl, dry_run, enable_optimize_b |
939 | 0 | }; |
940 | 0 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, ss_x, ss_y); |
941 | 0 | if (enable_optimize_b) { |
942 | 0 | av1_get_entropy_contexts(plane_bsize, pd, ta, tl); |
943 | 0 | } |
944 | 0 | av1_foreach_transformed_block_in_plane( |
945 | 0 | xd, plane_bsize, plane, encode_block_intra_and_set_context, &arg); |
946 | 0 | } |