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 #ifndef AOM_AV1_ENCODER_ENCODEMB_H_
13 #define AOM_AV1_ENCODER_ENCODEMB_H_
14
15 #include "config/aom_config.h"
16
17 #include "av1/common/av1_common_int.h"
18 #include "av1/common/txb_common.h"
19 #include "av1/encoder/av1_quantize.h"
20 #include "av1/encoder/block.h"
21 #include "av1/encoder/tokenize.h"
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 enum {
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.
36 enum {
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
43 enum {
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
50 struct optimize_ctx {
51 ENTROPY_CONTEXT ta[MAX_MB_PLANE][MAX_MIB_SIZE];
52 ENTROPY_CONTEXT tl[MAX_MB_PLANE][MAX_MIB_SIZE];
53 };
54
55 struct encode_b_args {
56 const struct AV1_COMP *cpi;
57 MACROBLOCK *x;
58 struct optimize_ctx *ctx;
59 ENTROPY_CONTEXT *ta;
60 ENTROPY_CONTEXT *tl;
61 RUN_TYPE dry_run;
62 TRELLIS_OPT_TYPE enable_optimize_b;
63 };
64
65 void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
66 RUN_TYPE dry_run);
67
68 void av1_foreach_transformed_block_in_plane(
69 const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
70 foreach_transformed_block_visitor visit, void *arg);
71
72 void av1_encode_sby_pass1(struct AV1_COMP *cpi, MACROBLOCK *x,
73 BLOCK_SIZE bsize);
74
75 void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size,
76 TX_TYPE tx_type, TxfmParam *txfm_param);
77 void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx,
78 int use_quant_b_adapt, QUANT_PARAM *qparam);
79 void av1_setup_qmatrix(const CommonQuantParams *quant_params,
80 const MACROBLOCKD *xd, int plane, TX_SIZE tx_size,
81 TX_TYPE tx_type, QUANT_PARAM *qparam);
82
83 void av1_xform_dc_only(MACROBLOCK *x, int plane, int block,
84 TxfmParam *txfm_param, int64_t per_px_mean);
85
86 void av1_xform_quant(MACROBLOCK *x, int plane, int block, int blk_row,
87 int blk_col, BLOCK_SIZE plane_bsize, TxfmParam *txfm_param,
88 const QUANT_PARAM *qparam);
89
90 void av1_xform(MACROBLOCK *x, int plane, int block, int blk_row, int blk_col,
91 BLOCK_SIZE plane_bsize, TxfmParam *txfm_param);
92
93 void av1_quant(MACROBLOCK *x, int plane, int block, TxfmParam *txfm_param,
94 const QUANT_PARAM *qparam);
95
96 int av1_optimize_b(const struct AV1_COMP *cpi, MACROBLOCK *mb, int plane,
97 int block, TX_SIZE tx_size, TX_TYPE tx_type,
98 const TXB_CTX *const txb_ctx, int *rate_cost);
99
100 // This function can be used as (i) a further optimization to reduce the
101 // redundancy of quantized coefficients (a.k.a., `qcoeff`) after trellis
102 // optimization, or (ii) an alternative to trellis optimization in high-speed
103 // compression mode (e.g., real-time mode under speed-6) due to its LOW time
104 // complexity. The rational behind is to drop out the may-be redundant quantized
105 // coefficient which is among a bunch of zeros. NOTE: This algorithm is not as
106 // accurate as trellis optimization since the hyper-parameters are hard-coded
107 // instead of dynamic search. More adaptive logic may improve the performance.
108 // This function should be applied to all or partical block cells.
109 // Inputs:
110 // mb: Pointer to the MACROBLOCK to perform dropout on.
111 // plane: Index of the plane to which the target block belongs.
112 // block: Index of the target block.
113 // tx_size: Transform size of the target block.
114 // tx_type: Transform type of the target block. This field is particularly
115 // used to find out the scan order of the block.
116 // qindex: Quantization index used for target block. In general, all blocks
117 // in a same plane share the same quantization index. This field is
118 // particularly used to determine how many zeros should be used to
119 // drop out a coefficient.
120 // Returns:
121 // Nothing will be returned, but `qcoeff`, `dqcoeff`, `eob`, as well as
122 // `txb_entropy_ctx`, which `mb` points to, may be modified by this function.
123 void av1_dropout_qcoeff(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
124 TX_TYPE tx_type, int qindex);
125 // Same as above, with the number of zeroes needed before/after a coeff to drop
126 // it explicitly passed in, instead of being derived from qindex.
127 void av1_dropout_qcoeff_num(MACROBLOCK *mb, int plane, int block,
128 TX_SIZE tx_size, TX_TYPE tx_type,
129 int dropout_num_before, int dropout_num_after);
130
131 void av1_subtract_block(BitDepthInfo bd_info, int rows, int cols, int16_t *diff,
132 ptrdiff_t diff_stride, const uint8_t *src8,
133 ptrdiff_t src_stride, const uint8_t *pred8,
134 ptrdiff_t pred_stride);
135
136 void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize,
137 int blk_col, int blk_row, TX_SIZE tx_size);
138
139 void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE plane_bsize, int plane);
140
av1_set_txb_context(MACROBLOCK * x,int plane,int block,TX_SIZE tx_size,ENTROPY_CONTEXT * a,ENTROPY_CONTEXT * l)141 static inline void av1_set_txb_context(MACROBLOCK *x, int plane, int block,
142 TX_SIZE tx_size, ENTROPY_CONTEXT *a,
143 ENTROPY_CONTEXT *l) {
144 const uint8_t ctx = x->plane[plane].txb_entropy_ctx[block];
145 memset(a, ctx, tx_size_wide_unit[tx_size] * sizeof(*a));
146 memset(l, ctx, tx_size_high_unit[tx_size] * sizeof(*l));
147 }
148
149 void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
150 BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run,
151 TRELLIS_OPT_TYPE enable_optimize_b);
152
is_trellis_used(TRELLIS_OPT_TYPE optimize_b,RUN_TYPE dry_run)153 static inline int is_trellis_used(TRELLIS_OPT_TYPE optimize_b,
154 RUN_TYPE dry_run) {
155 if (optimize_b == NO_TRELLIS_OPT) return false;
156 if (optimize_b == FINAL_PASS_TRELLIS_OPT && dry_run != OUTPUT_ENABLED)
157 return false;
158 return true;
159 }
160
161 // Scaling terms (precision of 12 bits) to perform tx-size specific
162 // normalization that is used in DCT_DCT forward transform.
163 // For transform blocks of 1:2 and 2:1 - sqrt(2) normalization is used
164 // For transform blocks of 1:4 and 4:1 - factor of 2 is used
165 // For transform blocks TX_8x8 and below - an additional factor of 2 is used
166 // For transform blocks max(width,height)=64 - currently not supported
167
168 static const uint16_t dc_coeff_scale[TX_SIZES_ALL] = {
169 1024, 2048, 4096, 4096, 0, 1448, 1448, 2896, 2896, 2896,
170 2896, 0, 0, 2048, 2048, 4096, 4096, 0, 0
171 };
172
173 #ifdef __cplusplus
174 } // extern "C"
175 #endif
176
177 #endif // AOM_AV1_ENCODER_ENCODEMB_H_
178