• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   int8_t *skip;
60   ENTROPY_CONTEXT *ta;
61   ENTROPY_CONTEXT *tl;
62   RUN_TYPE dry_run;
63   TRELLIS_OPT_TYPE enable_optimize_b;
64 };
65 
66 void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
67                    RUN_TYPE dry_run);
68 
69 void av1_foreach_transformed_block_in_plane(
70     const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
71     foreach_transformed_block_visitor visit, void *arg);
72 
73 void av1_encode_sby_pass1(struct AV1_COMP *cpi, MACROBLOCK *x,
74                           BLOCK_SIZE bsize);
75 
76 void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size,
77                      TX_TYPE tx_type, TxfmParam *txfm_param);
78 void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx,
79                      int use_quant_b_adapt, QUANT_PARAM *qparam);
80 void av1_setup_qmatrix(const CommonQuantParams *quant_params,
81                        const MACROBLOCKD *xd, int plane, TX_SIZE tx_size,
82                        TX_TYPE tx_type, QUANT_PARAM *qparam);
83 
84 void av1_xform_dc_only(MACROBLOCK *x, int plane, int block,
85                        TxfmParam *txfm_param, int64_t per_px_mean);
86 
87 void av1_xform_quant(MACROBLOCK *x, int plane, int block, int blk_row,
88                      int blk_col, BLOCK_SIZE plane_bsize, TxfmParam *txfm_param,
89                      QUANT_PARAM *qparam);
90 
91 void av1_xform(MACROBLOCK *x, int plane, int block, int blk_row, int blk_col,
92                BLOCK_SIZE plane_bsize, TxfmParam *txfm_param);
93 
94 void av1_quant(MACROBLOCK *x, int plane, int block, TxfmParam *txfm_param,
95                QUANT_PARAM *qparam);
96 
97 int av1_optimize_b(const struct AV1_COMP *cpi, MACROBLOCK *mb, int plane,
98                    int block, TX_SIZE tx_size, TX_TYPE tx_type,
99                    const TXB_CTX *const txb_ctx, int *rate_cost);
100 
101 // 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.
124 void av1_dropout_qcoeff(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
125                         TX_TYPE tx_type, int qindex);
126 // Same as above, with the number of zeroes needed before/after a coeff to drop
127 // it explicitly passed in, instead of being derived from qindex.
128 void av1_dropout_qcoeff_num(MACROBLOCK *mb, int plane, int block,
129                             TX_SIZE tx_size, TX_TYPE tx_type,
130                             int dropout_num_before, int dropout_num_after);
131 
132 void av1_subtract_block(BitDepthInfo bd_info, int rows, int cols, int16_t *diff,
133                         ptrdiff_t diff_stride, const uint8_t *src8,
134                         ptrdiff_t src_stride, const uint8_t *pred8,
135                         ptrdiff_t pred_stride);
136 
137 void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize,
138                       int blk_col, int blk_row, TX_SIZE tx_size);
139 
140 void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE plane_bsize, int plane);
141 
av1_set_txb_context(MACROBLOCK * x,int plane,int block,TX_SIZE tx_size,ENTROPY_CONTEXT * a,ENTROPY_CONTEXT * l)142 static INLINE void av1_set_txb_context(MACROBLOCK *x, int plane, int block,
143                                        TX_SIZE tx_size, ENTROPY_CONTEXT *a,
144                                        ENTROPY_CONTEXT *l) {
145   const uint8_t ctx = x->plane[plane].txb_entropy_ctx[block];
146   memset(a, ctx, tx_size_wide_unit[tx_size] * sizeof(*a));
147   memset(l, ctx, tx_size_high_unit[tx_size] * sizeof(*l));
148 }
149 
150 void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
151                             BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg);
152 
153 void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
154                                   BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run,
155                                   TRELLIS_OPT_TYPE enable_optimize_b);
156 
is_trellis_used(TRELLIS_OPT_TYPE optimize_b,RUN_TYPE dry_run)157 static INLINE int is_trellis_used(TRELLIS_OPT_TYPE optimize_b,
158                                   RUN_TYPE dry_run) {
159   if (optimize_b == NO_TRELLIS_OPT) return false;
160   if (optimize_b == FINAL_PASS_TRELLIS_OPT && dry_run != OUTPUT_ENABLED)
161     return false;
162   return true;
163 }
164 
165 // Scaling terms (precision of 12 bits) to perform tx-size specific
166 // normalization that is used in DCT_DCT forward transform.
167 // For transform blocks of 1:2 and 2:1       - sqrt(2) normalization is used
168 // For transform blocks of 1:4 and 4:1       - factor of 2 is used
169 // For transform blocks TX_8x8 and below     - an additional factor of 2 is used
170 // For transform blocks max(width,height)=64 - currently not supported
171 
172 static const uint16_t dc_coeff_scale[TX_SIZES_ALL] = {
173   1024, 2048, 4096, 4096, 0,    1448, 1448, 2896, 2896, 2896,
174   2896, 0,    0,    2048, 2048, 4096, 4096, 0,    0
175 };
176 
177 #ifdef __cplusplus
178 }  // extern "C"
179 #endif
180 
181 #endif  // AOM_AV1_ENCODER_ENCODEMB_H_
182