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