blob: 772a9e2623afe0a2bce554df9e24e5f1045674ae [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
James Zerne1cbb132018-08-22 14:10:36 -070012#ifndef AOM_AV1_ENCODER_ENCODEMB_H_
13#define AOM_AV1_ENCODER_ENCODEMB_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
Tom Finegan60e653d2018-05-22 11:34:58 -070015#include "config/aom_config.h"
16
Wan-Teh Changf2d15ee2020-03-10 09:24:43 -070017#include "av1/common/av1_common_int.h"
Xing Jinbd91e942018-06-21 13:43:17 +080018#include "av1/common/txb_common.h"
Debargha Mukherjee2d565f42020-06-17 18:07:12 -070019#include "av1/encoder/av1_quantize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020#include "av1/encoder/block.h"
Angie Chiang5b5f4df2017-12-06 10:41:12 -080021#include "av1/encoder/tokenize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070022#ifdef __cplusplus
23extern "C" {
24#endif
25
chiyotsaic2f38412020-06-10 16:07:21 -070026enum {
27 AV1_XFORM_QUANT_FP = 0,
28 AV1_XFORM_QUANT_B = 1,
29 AV1_XFORM_QUANT_DC = 2,
30 AV1_XFORM_QUANT_SKIP_QUANT,
31 AV1_XFORM_QUANT_TYPES,
32} UENUM1BYTE(AV1_XFORM_QUANT);
33
34// TODO(any): Merge OPT_TYPe and TRELLLIS_OPT_TYPE
35// Available optimization types to optimize the quantized coefficients.
36enum {
37 NONE_OPT = 0, // No optimization.
38 TRELLIS_OPT = 1, // Trellis optimization. See `av1_optimize_b()`.
39 DROPOUT_OPT = 2, // Dropout optimization. See `av1_dropout_qcoeff()`.
40 TRELLIS_DROPOUT_OPT = 3 // Perform dropout after trellis optimization.
41} UENUM1BYTE(OPT_TYPE);
42
43enum {
44 NO_TRELLIS_OPT, // No trellis optimization
45 FULL_TRELLIS_OPT, // Trellis optimization in all stages
46 FINAL_PASS_TRELLIS_OPT, // Trellis optimization in only the final encode pass
47 NO_ESTIMATE_YRD_TRELLIS_OPT // Disable trellis in estimate_yrd_for_sb
48} UENUM1BYTE(TRELLIS_OPT_TYPE);
49
Yaowu Xuc27fc142016-08-22 16:08:15 -070050struct optimize_ctx {
Wan-Teh Chang660f8ed2018-05-11 17:28:08 -070051 ENTROPY_CONTEXT ta[MAX_MB_PLANE][MAX_MIB_SIZE];
52 ENTROPY_CONTEXT tl[MAX_MB_PLANE][MAX_MIB_SIZE];
Yaowu Xuc27fc142016-08-22 16:08:15 -070053};
54
55struct encode_b_args {
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080056 const struct AV1_COMP *cpi;
Yaowu Xuc27fc142016-08-22 16:08:15 -070057 MACROBLOCK *x;
58 struct optimize_ctx *ctx;
59 int8_t *skip;
60 ENTROPY_CONTEXT *ta;
61 ENTROPY_CONTEXT *tl;
Debargha Mukherjee8a191ef2020-03-24 00:47:21 -070062 RUN_TYPE dry_run;
63 TRELLIS_OPT_TYPE enable_optimize_b;
Yaowu Xuc27fc142016-08-22 16:08:15 -070064};
65
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080066void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
Hui Su95e9c0c2019-12-09 15:21:35 -080067 RUN_TYPE dry_run);
Urvang Joshi10df0322018-07-31 14:03:30 -070068
69void av1_foreach_transformed_block_in_plane(
Hui Su56e838f2019-12-04 14:48:30 -080070 const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
Urvang Joshi10df0322018-07-31 14:03:30 -070071 foreach_transformed_block_visitor visit, void *arg);
72
Urvang Joshi1de67aa2020-03-20 11:21:57 -070073void av1_encode_sby_pass1(struct AV1_COMP *cpi, MACROBLOCK *x,
74 BLOCK_SIZE bsize);
Urvang Joshi10df0322018-07-31 14:03:30 -070075
bohanlid9d8d672019-11-21 14:43:26 -080076void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size,
77 TX_TYPE tx_type, TxfmParam *txfm_param);
Urvang Joshi1de67aa2020-03-20 11:21:57 -070078void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx,
79 int use_quant_b_adapt, QUANT_PARAM *qparam);
Urvang Joshi17814622020-03-27 17:26:17 -070080void av1_setup_qmatrix(const CommonQuantParams *quant_params,
Urvang Joshif409a632020-03-28 01:03:50 -070081 const MACROBLOCKD *xd, int plane, TX_SIZE tx_size,
Urvang Joshi17814622020-03-27 17:26:17 -070082 TX_TYPE tx_type, QUANT_PARAM *qparam);
bohanlid9d8d672019-11-21 14:43:26 -080083
Ravi Chaudhary6eaea622020-08-28 10:28:24 +053084void av1_xform_dc_only(MACROBLOCK *x, int plane, int block,
85 TxfmParam *txfm_param, int64_t per_px_mean);
86
bohanlid9d8d672019-11-21 14:43:26 -080087void av1_xform_quant(MACROBLOCK *x, int plane, int block, int blk_row,
bohanlib79e6e72019-11-22 16:06:38 -080088 int blk_col, BLOCK_SIZE plane_bsize, TxfmParam *txfm_param,
bohanlid9d8d672019-11-21 14:43:26 -080089 QUANT_PARAM *qparam);
Yaowu Xuc27fc142016-08-22 16:08:15 -070090
Deepa K Gcb2fa3b2020-05-06 19:59:26 +053091void av1_xform(MACROBLOCK *x, int plane, int block, int blk_row, int blk_col,
92 BLOCK_SIZE plane_bsize, TxfmParam *txfm_param);
93
94void av1_quant(MACROBLOCK *x, int plane, int block, TxfmParam *txfm_param,
95 QUANT_PARAM *qparam);
96
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -080097int av1_optimize_b(const struct AV1_COMP *cpi, MACROBLOCK *mb, int plane,
Xing Jinbd91e942018-06-21 13:43:17 +080098 int block, TX_SIZE tx_size, TX_TYPE tx_type,
chiyotsaifc56df82020-07-09 15:53:46 -070099 const TXB_CTX *const txb_ctx, int *rate_cost);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700100
yjshen27aa8442020-01-03 16:35:34 -0800101// This function can be used as (i) a further optimization to reduce the
102// redundancy of quantized coefficients (a.k.a., `qcoeff`) after trellis
103// optimization, or (ii) an alternative to trellis optimization in high-speed
104// compression mode (e.g., real-time mode under speed-6) due to its LOW time
105// complexity. The rational behind is to drop out the may-be redundant quantized
106// coefficient which is among a bunch of zeros. NOTE: This algorithm is not as
107// accurate as trellis optimization since the hyper-parameters are hard-coded
108// instead of dynamic search. More adaptive logic may improve the performance.
109// This function should be applied to all or partical block cells.
110// Inputs:
111// mb: Pointer to the MACROBLOCK to perform dropout on.
112// plane: Index of the plane to which the target block belongs.
113// block: Index of the target block.
114// tx_size: Transform size of the target block.
115// tx_type: Transform type of the target block. This field is particularly
116// used to find out the scan order of the block.
117// qindex: Quantization index used for target block. In general, all blocks
118// in a same plane share the same quantization index. This field is
119// particularly used to determine how many zeros should be used to
120// drop out a coefficient.
121// Returns:
122// Nothing will be returned, but `qcoeff`, `dqcoeff`, `eob`, as well as
123// `txb_entropy_ctx`, which `mb` points to, may be modified by this function.
124void av1_dropout_qcoeff(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
125 TX_TYPE tx_type, int qindex);
126
Angie Chiangbb05a962021-03-31 11:34:53 -0700127void av1_subtract_block(BitDepthInfo bd_info, int rows, int cols, int16_t *diff,
128 ptrdiff_t diff_stride, const uint8_t *src8,
129 ptrdiff_t src_stride, const uint8_t *pred8,
130 ptrdiff_t pred_stride);
Jingning Hanb7b9d492019-11-18 15:59:14 -0800131
Angie Chiang62e54cd2017-04-06 10:45:56 -0700132void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize,
133 int blk_col, int blk_row, TX_SIZE tx_size);
134
Hui Su56e838f2019-12-04 14:48:30 -0800135void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE plane_bsize, int plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700136
Hui Sudb0301e2018-04-02 10:57:21 -0700137static INLINE void av1_set_txb_context(MACROBLOCK *x, int plane, int block,
138 TX_SIZE tx_size, ENTROPY_CONTEXT *a,
139 ENTROPY_CONTEXT *l) {
140 const uint8_t ctx = x->plane[plane].txb_entropy_ctx[block];
141 memset(a, ctx, tx_size_wide_unit[tx_size] * sizeof(*a));
142 memset(l, ctx, tx_size_high_unit[tx_size] * sizeof(*l));
143}
Angie Chiang36aca332017-03-23 14:16:24 -0700144
Yaowu Xuf883b422016-08-30 14:01:10 -0700145void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
146 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700147
Michelle Findlay-Olynykfbab0622017-12-13 14:10:56 -0800148void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
Debargha Mukherjee8a191ef2020-03-24 00:47:21 -0700149 BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run,
150 TRELLIS_OPT_TYPE enable_optimize_b);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151
Debargha Mukherjee8a191ef2020-03-24 00:47:21 -0700152static INLINE int is_trellis_used(TRELLIS_OPT_TYPE optimize_b,
153 RUN_TYPE dry_run) {
154 if (optimize_b == NO_TRELLIS_OPT) return false;
155 if (optimize_b == FINAL_PASS_TRELLIS_OPT && dry_run != OUTPUT_ENABLED)
156 return false;
157 return true;
158}
Ravi Chaudhary6eaea622020-08-28 10:28:24 +0530159
Ravi Chaudharyc4aff3a2020-09-10 10:45:48 +0530160// Scaling terms (precision of 12 bits) to perform tx-size specific
161// normalization that is used in DCT_DCT forward transform.
162// For transform blocks of 1:2 and 2:1 - sqrt(2) normalization is used
163// For transform blocks of 1:4 and 4:1 - factor of 2 is used
164// For transform blocks TX_8x8 and below - an additional factor of 2 is used
165// For transform blocks max(width,height)=64 - currently not supported
166
Ravi Chaudhary6eaea622020-08-28 10:28:24 +0530167static const uint16_t dc_coeff_scale[TX_SIZES_ALL] = {
168 1024, 2048, 4096, 4096, 0, 1448, 1448, 2896, 2896, 2896,
169 2896, 0, 0, 2048, 2048, 4096, 4096, 0, 0
170};
171
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172#ifdef __cplusplus
173} // extern "C"
174#endif
175
James Zerne1cbb132018-08-22 14:10:36 -0700176#endif // AOM_AV1_ENCODER_ENCODEMB_H_