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/block.h"
20 #include "av1/encoder/tokenize.h"
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 struct optimize_ctx {
26   ENTROPY_CONTEXT ta[MAX_MB_PLANE][MAX_MIB_SIZE];
27   ENTROPY_CONTEXT tl[MAX_MB_PLANE][MAX_MIB_SIZE];
28 };
29 
30 struct encode_b_args {
31   const struct AV1_COMP *cpi;
32   MACROBLOCK *x;
33   struct optimize_ctx *ctx;
34   int8_t *skip;
35   ENTROPY_CONTEXT *ta;
36   ENTROPY_CONTEXT *tl;
37   RUN_TYPE dry_run;
38   TRELLIS_OPT_TYPE enable_optimize_b;
39 };
40 
41 enum {
42   AV1_XFORM_QUANT_FP = 0,
43   AV1_XFORM_QUANT_B = 1,
44   AV1_XFORM_QUANT_DC = 2,
45   AV1_XFORM_QUANT_SKIP_QUANT,
46   AV1_XFORM_QUANT_TYPES,
47 } UENUM1BYTE(AV1_XFORM_QUANT);
48 
49 // Available optimization types to optimize the quantized coefficients.
50 enum {
51   NONE_OPT = 0,            // No optimization.
52   TRELLIS_OPT = 1,         // Trellis optimization. See `av1_optimize_b()`.
53   DROPOUT_OPT = 2,         // Dropout optimization. See `av1_dropout_qcoeff()`.
54   TRELLIS_DROPOUT_OPT = 3  // Perform dropout after trellis optimization.
55 } UENUM1BYTE(OPT_TYPE);
56 
57 void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
58                    RUN_TYPE dry_run);
59 
60 void av1_foreach_transformed_block_in_plane(
61     const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
62     foreach_transformed_block_visitor visit, void *arg);
63 
64 void av1_encode_sby_pass1(struct AV1_COMP *cpi, MACROBLOCK *x,
65                           BLOCK_SIZE bsize);
66 
67 void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size,
68                      TX_TYPE tx_type, TxfmParam *txfm_param);
69 void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx,
70                      int use_quant_b_adapt, QUANT_PARAM *qparam);
71 void av1_setup_qmatrix(const CommonQuantParams *quant_params,
72                        const MACROBLOCKD *xd, int plane, TX_SIZE tx_size,
73                        TX_TYPE tx_type, QUANT_PARAM *qparam);
74 
75 void av1_xform_quant(MACROBLOCK *x, int plane, int block, int blk_row,
76                      int blk_col, BLOCK_SIZE plane_bsize, TxfmParam *txfm_param,
77                      QUANT_PARAM *qparam);
78 
79 int av1_optimize_b(const struct AV1_COMP *cpi, MACROBLOCK *mb, int plane,
80                    int block, TX_SIZE tx_size, TX_TYPE tx_type,
81                    const TXB_CTX *const txb_ctx, int fast_mode, int *rate_cost);
82 
83 // This function can be used as (i) a further optimization to reduce the
84 // redundancy of quantized coefficients (a.k.a., `qcoeff`) after trellis
85 // optimization, or (ii) an alternative to trellis optimization in high-speed
86 // compression mode (e.g., real-time mode under speed-6) due to its LOW time
87 // complexity. The rational behind is to drop out the may-be redundant quantized
88 // coefficient which is among a bunch of zeros. NOTE: This algorithm is not as
89 // accurate as trellis optimization since the hyper-parameters are hard-coded
90 // instead of dynamic search. More adaptive logic may improve the performance.
91 // This function should be applied to all or partical block cells.
92 // Inputs:
93 //   mb: Pointer to the MACROBLOCK to perform dropout on.
94 //   plane: Index of the plane to which the target block belongs.
95 //   block: Index of the target block.
96 //   tx_size: Transform size of the target block.
97 //   tx_type: Transform type of the target block. This field is particularly
98 //            used to find out the scan order of the block.
99 //   qindex: Quantization index used for target block. In general, all blocks
100 //           in a same plane share the same quantization index. This field is
101 //           particularly used to determine how many zeros should be used to
102 //           drop out a coefficient.
103 // Returns:
104 //   Nothing will be returned, but `qcoeff`, `dqcoeff`, `eob`, as well as
105 //   `txb_entropy_ctx`, which `mb` points to, may be modified by this function.
106 void av1_dropout_qcoeff(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
107                         TX_TYPE tx_type, int qindex);
108 
109 void av1_subtract_block(const MACROBLOCKD *xd, int rows, int cols,
110                         int16_t *diff, ptrdiff_t diff_stride,
111                         const uint8_t *src8, ptrdiff_t src_stride,
112                         const uint8_t *pred8, ptrdiff_t pred_stride);
113 
114 void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize,
115                       int blk_col, int blk_row, TX_SIZE tx_size);
116 
117 void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE plane_bsize, int plane);
118 
av1_set_txb_context(MACROBLOCK * x,int plane,int block,TX_SIZE tx_size,ENTROPY_CONTEXT * a,ENTROPY_CONTEXT * l)119 static INLINE void av1_set_txb_context(MACROBLOCK *x, int plane, int block,
120                                        TX_SIZE tx_size, ENTROPY_CONTEXT *a,
121                                        ENTROPY_CONTEXT *l) {
122   const uint8_t ctx = x->plane[plane].txb_entropy_ctx[block];
123   memset(a, ctx, tx_size_wide_unit[tx_size] * sizeof(*a));
124   memset(l, ctx, tx_size_high_unit[tx_size] * sizeof(*l));
125 }
126 
127 void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
128                             BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg);
129 
130 void av1_encode_intra_block_plane(const struct AV1_COMP *cpi, MACROBLOCK *x,
131                                   BLOCK_SIZE bsize, int plane, RUN_TYPE dry_run,
132                                   TRELLIS_OPT_TYPE enable_optimize_b);
133 
is_trellis_used(TRELLIS_OPT_TYPE optimize_b,RUN_TYPE dry_run)134 static INLINE int is_trellis_used(TRELLIS_OPT_TYPE optimize_b,
135                                   RUN_TYPE dry_run) {
136   if (optimize_b == NO_TRELLIS_OPT) return false;
137   if (optimize_b == FINAL_PASS_TRELLIS_OPT && dry_run != OUTPUT_ENABLED)
138     return false;
139   return true;
140 }
141 #ifdef __cplusplus
142 }  // extern "C"
143 #endif
144 
145 #endif  // AOM_AV1_ENCODER_ENCODEMB_H_
146