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_RDOPT_H_
13 #define AOM_AV1_ENCODER_RDOPT_H_
14 
15 #include <stdbool.h>
16 
17 #include "av1/common/blockd.h"
18 #include "av1/common/txb_common.h"
19 
20 #include "av1/encoder/block.h"
21 #include "av1/encoder/context_tree.h"
22 #include "av1/encoder/encoder.h"
23 #include "av1/encoder/encodetxb.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define MAX_REF_MV_SERCH 3
30 #define DEFAULT_LUMA_INTERP_SKIP_FLAG 1
31 #define DEFAULT_CHROMA_INTERP_SKIP_FLAG 2
32 #define DEFAULT_INTERP_SKIP_FLAG \
33   (DEFAULT_LUMA_INTERP_SKIP_FLAG | DEFAULT_CHROMA_INTERP_SKIP_FLAG)
34 
35 struct TileInfo;
36 struct macroblock;
37 struct RD_STATS;
38 
39 #if CONFIG_RD_DEBUG
av1_update_txb_coeff_cost(RD_STATS * rd_stats,int plane,TX_SIZE tx_size,int blk_row,int blk_col,int txb_coeff_cost)40 static INLINE void av1_update_txb_coeff_cost(RD_STATS *rd_stats, int plane,
41                                              TX_SIZE tx_size, int blk_row,
42                                              int blk_col, int txb_coeff_cost) {
43   (void)blk_row;
44   (void)blk_col;
45   (void)tx_size;
46   rd_stats->txb_coeff_cost[plane] += txb_coeff_cost;
47 
48   {
49     const int txb_h = tx_size_high_unit[tx_size];
50     const int txb_w = tx_size_wide_unit[tx_size];
51     int idx, idy;
52     for (idy = 0; idy < txb_h; ++idy)
53       for (idx = 0; idx < txb_w; ++idx)
54         rd_stats->txb_coeff_cost_map[plane][blk_row + idy][blk_col + idx] = 0;
55 
56     rd_stats->txb_coeff_cost_map[plane][blk_row][blk_col] = txb_coeff_cost;
57   }
58   assert(blk_row < TXB_COEFF_COST_MAP_SIZE);
59   assert(blk_col < TXB_COEFF_COST_MAP_SIZE);
60 }
61 #endif
62 
63 // Returns the number of colors in 'src'.
64 int av1_count_colors(const uint8_t *src, int stride, int rows, int cols,
65                      int *val_count);
66 // Same as av1_count_colors(), but for high-bitdepth mode.
67 int av1_count_colors_highbd(const uint8_t *src8, int stride, int rows, int cols,
68                             int bit_depth, int *val_count);
69 
70 #if CONFIG_DIST_8X8
71 int64_t av1_dist_8x8(const struct AV1_COMP *const cpi, const MACROBLOCK *x,
72                      const uint8_t *src, int src_stride, const uint8_t *dst,
73                      int dst_stride, const BLOCK_SIZE tx_bsize, int bsw,
74                      int bsh, int visible_w, int visible_h, int qindex);
75 #endif
76 
av1_cost_skip_txb(MACROBLOCK * x,const TXB_CTX * const txb_ctx,int plane,TX_SIZE tx_size)77 static INLINE int av1_cost_skip_txb(MACROBLOCK *x, const TXB_CTX *const txb_ctx,
78                                     int plane, TX_SIZE tx_size) {
79   const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
80   const PLANE_TYPE plane_type = get_plane_type(plane);
81   const LV_MAP_COEFF_COST *const coeff_costs =
82       &x->coeff_costs[txs_ctx][plane_type];
83   return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
84 }
85 
av1_cost_coeffs(const AV1_COMMON * const cm,MACROBLOCK * x,int plane,int block,TX_SIZE tx_size,const TX_TYPE tx_type,const TXB_CTX * const txb_ctx,int use_fast_coef_costing)86 static INLINE int av1_cost_coeffs(const AV1_COMMON *const cm, MACROBLOCK *x,
87                                   int plane, int block, TX_SIZE tx_size,
88                                   const TX_TYPE tx_type,
89                                   const TXB_CTX *const txb_ctx,
90                                   int use_fast_coef_costing) {
91 #if TXCOEFF_COST_TIMER
92   struct aom_usec_timer timer;
93   aom_usec_timer_start(&timer);
94 #endif
95   (void)use_fast_coef_costing;
96   const int cost =
97       av1_cost_coeffs_txb(cm, x, plane, block, tx_size, tx_type, txb_ctx);
98 #if TXCOEFF_COST_TIMER
99   AV1_COMMON *tmp_cm = (AV1_COMMON *)&cpi->common;
100   aom_usec_timer_mark(&timer);
101   const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
102   tmp_cm->txcoeff_cost_timer += elapsed_time;
103   ++tmp_cm->txcoeff_cost_count;
104 #endif
105   return cost;
106 }
107 
108 void av1_rd_pick_intra_mode_sb(const struct AV1_COMP *cpi, struct macroblock *x,
109                                int mi_row, int mi_col, struct RD_STATS *rd_cost,
110                                BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
111                                int64_t best_rd);
112 
113 unsigned int av1_get_sby_perpixel_variance(const struct AV1_COMP *cpi,
114                                            const struct buf_2d *ref,
115                                            BLOCK_SIZE bs);
116 unsigned int av1_high_get_sby_perpixel_variance(const struct AV1_COMP *cpi,
117                                                 const struct buf_2d *ref,
118                                                 BLOCK_SIZE bs, int bd);
119 
120 void av1_rd_pick_inter_mode_sb(struct AV1_COMP *cpi,
121                                struct TileDataEnc *tile_data,
122                                struct macroblock *x, int mi_row, int mi_col,
123                                struct RD_STATS *rd_cost, BLOCK_SIZE bsize,
124                                PICK_MODE_CONTEXT *ctx, int64_t best_rd_so_far);
125 
126 void av1_nonrd_pick_inter_mode_sb(struct AV1_COMP *cpi,
127                                   struct TileDataEnc *tile_data,
128                                   struct macroblock *x, int mi_row, int mi_col,
129                                   struct RD_STATS *rd_cost, BLOCK_SIZE bsize,
130                                   PICK_MODE_CONTEXT *ctx,
131                                   int64_t best_rd_so_far);
132 
133 void av1_rd_pick_inter_mode_sb_seg_skip(
134     const struct AV1_COMP *cpi, struct TileDataEnc *tile_data,
135     struct macroblock *x, int mi_row, int mi_col, struct RD_STATS *rd_cost,
136     BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, int64_t best_rd_so_far);
137 
138 // The best edge strength seen in the block, as well as the best x and y
139 // components of edge strength seen.
140 typedef struct {
141   uint16_t magnitude;
142   uint16_t x;
143   uint16_t y;
144 } EdgeInfo;
145 
146 /** Returns an integer indicating the strength of the edge.
147  * 0 means no edge found, 556 is the strength of a solid black/white edge,
148  * and the number may range higher if the signal is even stronger (e.g., on a
149  * corner). high_bd is a bool indicating the source should be treated
150  * as a 16-bit array. bd is the bit depth.
151  */
152 EdgeInfo av1_edge_exists(const uint8_t *src, int src_stride, int w, int h,
153                          bool high_bd, int bd);
154 
155 /** Applies a Gaussian blur with sigma = 1.3. Used by av1_edge_exists and
156  * tests.
157  */
158 void gaussian_blur(const uint8_t *src, int src_stride, int w, int h,
159                    uint8_t *dst, bool high_bd, int bd);
160 
161 /* Applies standard 3x3 Sobel matrix. */
162 typedef struct {
163   int16_t x;
164   int16_t y;
165 } sobel_xy;
166 
167 sobel_xy sobel(const uint8_t *input, int stride, int i, int j, bool high_bd);
168 
169 void av1_inter_mode_data_init(struct TileDataEnc *tile_data);
170 void av1_inter_mode_data_fit(TileDataEnc *tile_data, int rdmult);
171 
172 #ifdef __cplusplus
173 }  // extern "C"
174 #endif
175 
176 #endif  // AOM_AV1_ENCODER_RDOPT_H_
177