xref: /aosp_15_r20/external/libaom/av1/encoder/interp_search.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2020, 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_INTERP_FILTER_SEARCH_H_
13 #define AOM_AV1_ENCODER_INTERP_FILTER_SEARCH_H_
14 
15 #include "av1/encoder/block.h"
16 #include "av1/encoder/encoder.h"
17 #include "av1/encoder/rdopt_utils.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /*!\cond */
24 #define MAX_INTERP_FILTER_STATS 128
25 #define DUAL_FILTER_SET_SIZE (SWITCHABLE_FILTERS * SWITCHABLE_FILTERS)
26 
27 typedef struct {
28   int_interpfilters filters;
29   int_mv mv[2];
30   int8_t ref_frames[2];
31   COMPOUND_TYPE comp_type;
32   int compound_idx;
33   int64_t rd;
34   unsigned int pred_sse;
35 } INTERPOLATION_FILTER_STATS;
36 /*!\endcond */
37 
38 /*!\brief Miscellaneous arguments for inter mode search.
39  */
40 typedef struct HandleInterModeArgs {
41   /*!
42    * Buffer for the above predictor in OBMC
43    */
44   uint8_t *above_pred_buf[MAX_MB_PLANE];
45   /*!
46    * Stride for the above predictor in OBMC
47    */
48   int above_pred_stride[MAX_MB_PLANE];
49   /*!
50    * Buffer for the left predictor in OBMC
51    */
52   uint8_t *left_pred_buf[MAX_MB_PLANE];
53   /*!
54    * Stride for the left predictor in OBMC
55    */
56   int left_pred_stride[MAX_MB_PLANE];
57   /*!
58    * Pointer to the first member in a 2D array which holds
59    * single reference mode motion vectors to be used as a starting
60    * point in the mv search for compound modes. Each array is length REF_FRAMES,
61    * meaning there is a slot for a single reference motion vector for
62    * each possible reference frame. The 2D array consists of N of these arrays,
63    * where N is the length of the reference mv stack computed for the single
64    * reference case for that particular reference frame.
65    */
66   int_mv (*single_newmv)[REF_FRAMES];
67   /*!
68    * Pointer to the first array of a 2D array with the same setup as
69    * single_newmv array above. This is a 2D array to hold the rate
70    * corresponding to each of the single reference mode motion vectors
71    * held in single_newmv.
72    */
73   int (*single_newmv_rate)[REF_FRAMES];
74   /*!
75    * Pointer to the first array of a 2D array with the same setup as
76    * single_newmv array above. This is a 2D array to hold a 0 or 1
77    * validity value corresponding to each of the single reference mode motion
78    * vectors held in single_newmv.
79    */
80   int (*single_newmv_valid)[REF_FRAMES];
81   /*!
82    * Pointer to the first array in a 3D array of predicted rate-distortion.
83    * The dimensions of this structure are:
84    * (number of possible inter modes) X
85    * (number of reference MVs) X
86    * (number of reference frames).
87    */
88   int64_t (*modelled_rd)[MAX_REF_MV_SEARCH][REF_FRAMES];
89   /*!
90    * Holds an estimated entropy cost for picking the current reference frame.
91    * This is used to compute an rd estimate.
92    */
93   int ref_frame_cost;
94   /*!
95    * Holds an estimated entropy cost for picking single or compound
96    * reference. This is used to compute an rd estimate.
97    */
98   int single_comp_cost;
99   /*!
100    * Pointer to the first element in a 3D array holding rd's of
101    * SIMPLE_TRANSLATION used to prune out the motion mode search in single ref
102    * modes used to determine compound ref modes. The full structure is:
103    * (number of inter modes) X (length of refmv list) X (number of ref frames)
104    */
105   int64_t (*simple_rd)[MAX_REF_MV_SEARCH][REF_FRAMES];
106   /*!
107    * An integer value 0 or 1 which indicates whether or not to skip the motion
108    * mode search and default to SIMPLE_TRANSLATION as a speed feature.
109    */
110   int skip_motion_mode;
111   /*!
112    * Initialized to false. If true, skips interpolation filter search and uses
113    * the default EIGHTTAP_REGULAR.
114    */
115   bool skip_ifs;
116   /*!
117    * A pointer to the first element in an array of INTERINTRA_MODE types. This
118    * contains the best inter_intra mode for each reference frame.
119    */
120   INTERINTRA_MODE *inter_intra_mode;
121   /*!
122    * Array of saved interpolation filter stats collected to avoid repeating
123    * an interpolation filter search when the mv and ref_frame are the same
124    * as a previous search.
125    */
126   INTERPOLATION_FILTER_STATS interp_filter_stats[MAX_INTERP_FILTER_STATS];
127 
128   /*!
129    * Stack to store full pixel search start mv of NEWMV mode.
130    */
131   FULLPEL_MV start_mv_stack[(MAX_REF_MV_SEARCH - 1) * 2];
132 
133   /*!
134    * Stack to store ref_mv_idx of NEWMV mode.
135    */
136   uint8_t ref_mv_idx_stack[(MAX_REF_MV_SEARCH - 1) * 2];
137 
138   /*!
139    * Count of mvs in start mv stack.
140    */
141   int start_mv_cnt;
142 
143   /*!
144    * Index of the last set of saved stats in the interp_filter_stats array.
145    */
146   int interp_filter_stats_idx;
147   /*!
148    * Estimated wedge index.
149    */
150   int wedge_index;
151   /*!
152    * Estimated wedge sign.
153    */
154   int wedge_sign;
155   /*!
156    * Estimated diff wtd index.
157    */
158   int diffwtd_index;
159   /*!
160    * Estimated cmp mode.
161    */
162   int cmp_mode[MODE_CTX_REF_FRAMES];
163   /*!
164    * The best sse during single new_mv search. Note that the sse here comes from
165    * single_motion_search, and not from interpolation_filter_search. This has
166    * two implications:
167    * 1. The mv used to calculate the sse here does not have to be the best sse
168    *    found in handle_inter_mode.
169    * 2. Even if the mvs agree, the sse here can differ from the sse in \ref
170    *    MACROBLOCK::pred_sse due to different interpolation filter used.
171    */
172   unsigned int best_single_sse_in_refs[REF_FRAMES];
173   /*!
174    * Holds the sse of best mode so far in the mode evaluation process. This is
175    * used in intermediate termination of NEWMV mode evaluation.
176    */
177   unsigned int best_pred_sse;
178 } HandleInterModeArgs;
179 
180 /*!\cond */
181 static const int_interpfilters filter_sets[DUAL_FILTER_SET_SIZE] = {
182   { 0x00000000 }, { 0x00010000 }, { 0x00020000 },  // y = 0
183   { 0x00000001 }, { 0x00010001 }, { 0x00020001 },  // y = 1
184   { 0x00000002 }, { 0x00010002 }, { 0x00020002 },  // y = 2
185 };
186 
187 int64_t av1_interpolation_filter_search(
188     MACROBLOCK *const x, const AV1_COMP *const cpi,
189     const TileDataEnc *tile_data, BLOCK_SIZE bsize,
190     const BUFFER_SET *const tmp_dst, const BUFFER_SET *const orig_dst,
191     int64_t *const rd, int *const switchable_rate, int *skip_build_pred,
192     HandleInterModeArgs *args, int64_t ref_best_rd);
193 
194 /*!\endcond */
195 #ifdef __cplusplus
196 }  // extern "C"
197 #endif
198 
199 #endif  // AOM_AV1_ENCODER_INTERP_FILTER_SEARCH_H_
200