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 #include "av1/encoder/rdopt_utils.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define COMP_TYPE_RD_THRESH_SCALE 11
31 #define COMP_TYPE_RD_THRESH_SHIFT 4
32 #define MAX_WINNER_MOTION_MODES 10
33 
34 struct TileInfo;
35 struct macroblock;
36 struct RD_STATS;
37 
38 // Returns the number of colors in 'src'.
39 int av1_count_colors(const uint8_t *src, int stride, int rows, int cols,
40                      int *val_count);
41 // Same as av1_count_colors(), but for high-bitdepth mode.
42 int av1_count_colors_highbd(const uint8_t *src8, int stride, int rows, int cols,
43                             int bit_depth, int *val_count);
44 
av1_cost_skip_txb(MACROBLOCK * x,const TXB_CTX * const txb_ctx,int plane,TX_SIZE tx_size)45 static INLINE int av1_cost_skip_txb(MACROBLOCK *x, const TXB_CTX *const txb_ctx,
46                                     int plane, TX_SIZE tx_size) {
47   const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
48   const PLANE_TYPE plane_type = get_plane_type(plane);
49   const LV_MAP_COEFF_COST *const coeff_costs =
50       &x->coeff_costs[txs_ctx][plane_type];
51   return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
52 }
53 
54 void av1_rd_pick_intra_mode_sb(const struct AV1_COMP *cpi, struct macroblock *x,
55                                struct RD_STATS *rd_cost, BLOCK_SIZE bsize,
56                                PICK_MODE_CONTEXT *ctx, int64_t best_rd);
57 
58 unsigned int av1_get_sby_perpixel_variance(const struct AV1_COMP *cpi,
59                                            const struct buf_2d *ref,
60                                            BLOCK_SIZE bs);
61 unsigned int av1_high_get_sby_perpixel_variance(const struct AV1_COMP *cpi,
62                                                 const struct buf_2d *ref,
63                                                 BLOCK_SIZE bs, int bd);
64 
65 void av1_rd_pick_inter_mode_sb(struct AV1_COMP *cpi,
66                                struct TileDataEnc *tile_data,
67                                struct macroblock *x, struct RD_STATS *rd_cost,
68                                BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
69                                int64_t best_rd_so_far);
70 
71 void av1_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
72                          BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx);
73 
74 void av1_nonrd_pick_inter_mode_sb(struct AV1_COMP *cpi,
75                                   struct TileDataEnc *tile_data,
76                                   struct macroblock *x,
77                                   struct RD_STATS *rd_cost, BLOCK_SIZE bsize,
78                                   PICK_MODE_CONTEXT *ctx,
79                                   int64_t best_rd_so_far);
80 
81 void av1_rd_pick_inter_mode_sb_seg_skip(
82     const struct AV1_COMP *cpi, struct TileDataEnc *tile_data,
83     struct macroblock *x, int mi_row, int mi_col, struct RD_STATS *rd_cost,
84     BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, int64_t best_rd_so_far);
85 
86 // The best edge strength seen in the block, as well as the best x and y
87 // components of edge strength seen.
88 typedef struct {
89   uint16_t magnitude;
90   uint16_t x;
91   uint16_t y;
92 } EdgeInfo;
93 
94 /** Returns an integer indicating the strength of the edge.
95  * 0 means no edge found, 556 is the strength of a solid black/white edge,
96  * and the number may range higher if the signal is even stronger (e.g., on a
97  * corner). high_bd is a bool indicating the source should be treated
98  * as a 16-bit array. bd is the bit depth.
99  */
100 EdgeInfo av1_edge_exists(const uint8_t *src, int src_stride, int w, int h,
101                          bool high_bd, int bd);
102 
103 /** Applies a Gaussian blur with sigma = 1.3. Used by av1_edge_exists and
104  * tests.
105  */
106 void av1_gaussian_blur(const uint8_t *src, int src_stride, int w, int h,
107                        uint8_t *dst, bool high_bd, int bd);
108 
109 /* Applies standard 3x3 Sobel matrix. */
110 typedef struct {
111   int16_t x;
112   int16_t y;
113 } sobel_xy;
114 
115 sobel_xy av1_sobel(const uint8_t *input, int stride, int i, int j,
116                    bool high_bd);
117 
118 void av1_inter_mode_data_init(struct TileDataEnc *tile_data);
119 void av1_inter_mode_data_fit(TileDataEnc *tile_data, int rdmult);
120 
121 #if !CONFIG_REALTIME_ONLY
coded_to_superres_mi(int mi_col,int denom)122 static INLINE int coded_to_superres_mi(int mi_col, int denom) {
123   return (mi_col * denom + SCALE_NUMERATOR / 2) / SCALE_NUMERATOR;
124 }
125 #endif
126 
av1_encoder_get_relative_dist(const OrderHintInfo * oh,int a,int b)127 static INLINE int av1_encoder_get_relative_dist(const OrderHintInfo *oh, int a,
128                                                 int b) {
129   if (!oh->enable_order_hint) return 0;
130 
131   assert(a >= 0 && b >= 0);
132   return (a - b);
133 }
134 
135 // This function will return number of mi's in a superblock.
av1_get_sb_mi_size(const AV1_COMMON * const cm)136 static INLINE int av1_get_sb_mi_size(const AV1_COMMON *const cm) {
137   const int mi_alloc_size_1d = mi_size_wide[cm->mi_params.mi_alloc_bsize];
138   int sb_mi_rows =
139       (mi_size_wide[cm->seq_params.sb_size] + mi_alloc_size_1d - 1) /
140       mi_alloc_size_1d;
141   assert(mi_size_wide[cm->seq_params.sb_size] ==
142          mi_size_high[cm->seq_params.sb_size]);
143   int sb_mi_size = sb_mi_rows * sb_mi_rows;
144 
145   return sb_mi_size;
146 }
147 
148 // This function will copy usable ref_mv_stack[ref_frame][4] and
149 // weight[ref_frame][4] information from ref_mv_stack[ref_frame][8] and
150 // weight[ref_frame][8].
av1_copy_usable_ref_mv_stack_and_weight(const MACROBLOCKD * xd,MB_MODE_INFO_EXT * const mbmi_ext,MV_REFERENCE_FRAME ref_frame)151 static INLINE void av1_copy_usable_ref_mv_stack_and_weight(
152     const MACROBLOCKD *xd, MB_MODE_INFO_EXT *const mbmi_ext,
153     MV_REFERENCE_FRAME ref_frame) {
154   memcpy(mbmi_ext->weight[ref_frame], xd->weight[ref_frame],
155          USABLE_REF_MV_STACK_SIZE * sizeof(xd->weight[0][0]));
156   memcpy(mbmi_ext->ref_mv_stack[ref_frame], xd->ref_mv_stack[ref_frame],
157          USABLE_REF_MV_STACK_SIZE * sizeof(xd->ref_mv_stack[0][0]));
158 }
159 
160 // This function prunes the mode if either of the reference frame falls in the
161 // pruning list
prune_ref(const MV_REFERENCE_FRAME * const ref_frame,const OrderHintInfo * const order_hint_info,const unsigned int * const ref_display_order_hint,const unsigned int frame_display_order_hint,const int * ref_frame_list)162 static INLINE int prune_ref(const MV_REFERENCE_FRAME *const ref_frame,
163                             const OrderHintInfo *const order_hint_info,
164                             const unsigned int *const ref_display_order_hint,
165                             const unsigned int frame_display_order_hint,
166                             const int *ref_frame_list) {
167   for (int i = 0; i < 2; i++) {
168     if (ref_frame_list[i] == NONE_FRAME) continue;
169 
170     if (ref_frame[0] == ref_frame_list[i] ||
171         ref_frame[1] == ref_frame_list[i]) {
172       if (av1_encoder_get_relative_dist(
173               order_hint_info,
174               ref_display_order_hint[ref_frame_list[i] - LAST_FRAME],
175               frame_display_order_hint) < 0)
176         return 1;
177     }
178   }
179   return 0;
180 }
181 
prune_ref_by_selective_ref_frame(const AV1_COMP * const cpi,const MACROBLOCK * const x,const MV_REFERENCE_FRAME * const ref_frame,const unsigned int * const ref_display_order_hint)182 static INLINE int prune_ref_by_selective_ref_frame(
183     const AV1_COMP *const cpi, const MACROBLOCK *const x,
184     const MV_REFERENCE_FRAME *const ref_frame,
185     const unsigned int *const ref_display_order_hint) {
186   const SPEED_FEATURES *const sf = &cpi->sf;
187   if (!sf->inter_sf.selective_ref_frame) return 0;
188 
189   const AV1_COMMON *const cm = &cpi->common;
190   const OrderHintInfo *const order_hint_info = &cm->seq_params.order_hint_info;
191   const int comp_pred = ref_frame[1] > INTRA_FRAME;
192 
193   if (sf->inter_sf.selective_ref_frame >= 2 ||
194       (sf->inter_sf.selective_ref_frame == 1 && comp_pred)) {
195     int ref_frame_list[2] = { LAST3_FRAME, LAST2_FRAME };
196 
197     if (x != NULL) {
198       if (x->search_ref_frame[LAST3_FRAME]) ref_frame_list[0] = NONE_FRAME;
199       if (x->search_ref_frame[LAST2_FRAME]) ref_frame_list[1] = NONE_FRAME;
200     }
201 
202     if (prune_ref(ref_frame, order_hint_info, ref_display_order_hint,
203                   ref_display_order_hint[GOLDEN_FRAME - LAST_FRAME],
204                   ref_frame_list))
205       return 1;
206   }
207 
208   if (sf->inter_sf.selective_ref_frame >= 3) {
209     int ref_frame_list[2] = { ALTREF2_FRAME, BWDREF_FRAME };
210 
211     if (x != NULL) {
212       if (x->search_ref_frame[ALTREF2_FRAME]) ref_frame_list[0] = NONE_FRAME;
213       if (x->search_ref_frame[BWDREF_FRAME]) ref_frame_list[1] = NONE_FRAME;
214     }
215 
216     if (prune_ref(ref_frame, order_hint_info, ref_display_order_hint,
217                   ref_display_order_hint[LAST_FRAME - LAST_FRAME],
218                   ref_frame_list))
219       return 1;
220   }
221 
222   return 0;
223 }
224 
225 // This function will copy the best reference mode information from
226 // MB_MODE_INFO_EXT to MB_MODE_INFO_EXT_FRAME.
av1_copy_mbmi_ext_to_mbmi_ext_frame(MB_MODE_INFO_EXT_FRAME * mbmi_ext_best,const MB_MODE_INFO_EXT * const mbmi_ext,uint8_t ref_frame_type)227 static INLINE void av1_copy_mbmi_ext_to_mbmi_ext_frame(
228     MB_MODE_INFO_EXT_FRAME *mbmi_ext_best,
229     const MB_MODE_INFO_EXT *const mbmi_ext, uint8_t ref_frame_type) {
230   memcpy(mbmi_ext_best->ref_mv_stack, mbmi_ext->ref_mv_stack[ref_frame_type],
231          sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE]));
232   memcpy(mbmi_ext_best->weight, mbmi_ext->weight[ref_frame_type],
233          sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE]));
234   mbmi_ext_best->mode_context = mbmi_ext->mode_context[ref_frame_type];
235   mbmi_ext_best->ref_mv_count = mbmi_ext->ref_mv_count[ref_frame_type];
236   memcpy(mbmi_ext_best->global_mvs, mbmi_ext->global_mvs,
237          sizeof(mbmi_ext->global_mvs));
238 }
239 
240 #ifdef __cplusplus
241 }  // extern "C"
242 #endif
243 
244 #endif  // AOM_AV1_ENCODER_RDOPT_H_
245