xref: /aosp_15_r20/external/libaom/av1/encoder/global_motion.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_ENCODER_GLOBAL_MOTION_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_GLOBAL_MOTION_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/flow_estimation/flow_estimation.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/aom_pthread.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/enc_enums.h"
19*77c1e3ccSAndroid Build Coastguard Worker 
20*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
21*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
22*77c1e3ccSAndroid Build Coastguard Worker #endif
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker #define RANSAC_NUM_MOTIONS 1
25*77c1e3ccSAndroid Build Coastguard Worker #define GM_MAX_REFINEMENT_STEPS 5
26*77c1e3ccSAndroid Build Coastguard Worker #define MAX_DIRECTIONS 2
27*77c1e3ccSAndroid Build Coastguard Worker 
28*77c1e3ccSAndroid Build Coastguard Worker // The structure holds a valid reference frame type and its temporal distance
29*77c1e3ccSAndroid Build Coastguard Worker // from the source frame.
30*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
31*77c1e3ccSAndroid Build Coastguard Worker   int distance;
32*77c1e3ccSAndroid Build Coastguard Worker   MV_REFERENCE_FRAME frame;
33*77c1e3ccSAndroid Build Coastguard Worker } FrameDistPair;
34*77c1e3ccSAndroid Build Coastguard Worker 
35*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
36*77c1e3ccSAndroid Build Coastguard Worker   // Array of structure which holds the global motion parameters for a given
37*77c1e3ccSAndroid Build Coastguard Worker   // motion model. motion_models[i] holds the parameters for a given motion
38*77c1e3ccSAndroid Build Coastguard Worker   // model for the ith ransac motion.
39*77c1e3ccSAndroid Build Coastguard Worker   MotionModel motion_models[RANSAC_NUM_MOTIONS];
40*77c1e3ccSAndroid Build Coastguard Worker 
41*77c1e3ccSAndroid Build Coastguard Worker   // Pointer to hold inliers from motion model.
42*77c1e3ccSAndroid Build Coastguard Worker   uint8_t *segment_map;
43*77c1e3ccSAndroid Build Coastguard Worker } GlobalMotionData;
44*77c1e3ccSAndroid Build Coastguard Worker 
45*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
46*77c1e3ccSAndroid Build Coastguard Worker   // Holds the mapping of each thread to past/future direction.
47*77c1e3ccSAndroid Build Coastguard Worker   // thread_id_to_dir[i] indicates the direction id (past - 0/future - 1)
48*77c1e3ccSAndroid Build Coastguard Worker   // assigned to the ith thread.
49*77c1e3ccSAndroid Build Coastguard Worker   int8_t thread_id_to_dir[MAX_NUM_THREADS];
50*77c1e3ccSAndroid Build Coastguard Worker 
51*77c1e3ccSAndroid Build Coastguard Worker   // A flag which holds the early exit status based on the speed feature
52*77c1e3ccSAndroid Build Coastguard Worker   // 'prune_ref_frame_for_gm_search'. early_exit[i] will be set if the speed
53*77c1e3ccSAndroid Build Coastguard Worker   // feature based early exit happens in the direction 'i'.
54*77c1e3ccSAndroid Build Coastguard Worker   int8_t early_exit[MAX_DIRECTIONS];
55*77c1e3ccSAndroid Build Coastguard Worker 
56*77c1e3ccSAndroid Build Coastguard Worker   // Counter for the next reference frame to be processed.
57*77c1e3ccSAndroid Build Coastguard Worker   // next_frame_to_process[i] will hold the count of next reference frame to be
58*77c1e3ccSAndroid Build Coastguard Worker   // processed in the direction 'i'.
59*77c1e3ccSAndroid Build Coastguard Worker   int8_t next_frame_to_process[MAX_DIRECTIONS];
60*77c1e3ccSAndroid Build Coastguard Worker } GlobalMotionJobInfo;
61*77c1e3ccSAndroid Build Coastguard Worker 
62*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
63*77c1e3ccSAndroid Build Coastguard Worker   // Data related to assigning jobs for global motion multi-threading.
64*77c1e3ccSAndroid Build Coastguard Worker   GlobalMotionJobInfo job_info;
65*77c1e3ccSAndroid Build Coastguard Worker 
66*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
67*77c1e3ccSAndroid Build Coastguard Worker   // Mutex lock used while dispatching jobs.
68*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t *mutex_;
69*77c1e3ccSAndroid Build Coastguard Worker #endif
70*77c1e3ccSAndroid Build Coastguard Worker 
71*77c1e3ccSAndroid Build Coastguard Worker   // Initialized to false, set to true by the worker thread that encounters an
72*77c1e3ccSAndroid Build Coastguard Worker   // error in order to abort the processing of other worker threads.
73*77c1e3ccSAndroid Build Coastguard Worker   bool gm_mt_exit;
74*77c1e3ccSAndroid Build Coastguard Worker } AV1GlobalMotionSync;
75*77c1e3ccSAndroid Build Coastguard Worker 
76*77c1e3ccSAndroid Build Coastguard Worker void av1_convert_model_to_params(const double *params,
77*77c1e3ccSAndroid Build Coastguard Worker                                  WarpedMotionParams *model);
78*77c1e3ccSAndroid Build Coastguard Worker 
79*77c1e3ccSAndroid Build Coastguard Worker // Criteria for accepting a global motion model
80*77c1e3ccSAndroid Build Coastguard Worker static const double erroradv_tr = 0.65;
81*77c1e3ccSAndroid Build Coastguard Worker static const double erroradv_prod_tr = 20000;
82*77c1e3ccSAndroid Build Coastguard Worker 
83*77c1e3ccSAndroid Build Coastguard Worker // Early exit threshold for global motion refinement
84*77c1e3ccSAndroid Build Coastguard Worker // This is set slightly higher than erroradv_tr, as a compromise between
85*77c1e3ccSAndroid Build Coastguard Worker // two factors:
86*77c1e3ccSAndroid Build Coastguard Worker //
87*77c1e3ccSAndroid Build Coastguard Worker // 1) By rejecting un-promising models early, we can reduce the encode time
88*77c1e3ccSAndroid Build Coastguard Worker //    spent trying to refine them
89*77c1e3ccSAndroid Build Coastguard Worker //
90*77c1e3ccSAndroid Build Coastguard Worker // 2) When we refine a model, its error may decrease to below the acceptance
91*77c1e3ccSAndroid Build Coastguard Worker //    threshold even if the model is initially above the threshold
92*77c1e3ccSAndroid Build Coastguard Worker static const double erroradv_early_tr = 0.70;
93*77c1e3ccSAndroid Build Coastguard Worker 
94*77c1e3ccSAndroid Build Coastguard Worker int av1_is_enough_erroradvantage(double best_erroradvantage, int params_cost);
95*77c1e3ccSAndroid Build Coastguard Worker 
96*77c1e3ccSAndroid Build Coastguard Worker void av1_compute_feature_segmentation_map(uint8_t *segment_map, int width,
97*77c1e3ccSAndroid Build Coastguard Worker                                           int height, int *inliers,
98*77c1e3ccSAndroid Build Coastguard Worker                                           int num_inliers);
99*77c1e3ccSAndroid Build Coastguard Worker 
100*77c1e3ccSAndroid Build Coastguard Worker int64_t av1_segmented_frame_error(int use_hbd, int bd, const uint8_t *ref,
101*77c1e3ccSAndroid Build Coastguard Worker                                   int ref_stride, uint8_t *dst, int dst_stride,
102*77c1e3ccSAndroid Build Coastguard Worker                                   int p_width, int p_height,
103*77c1e3ccSAndroid Build Coastguard Worker                                   uint8_t *segment_map, int segment_map_stride);
104*77c1e3ccSAndroid Build Coastguard Worker 
105*77c1e3ccSAndroid Build Coastguard Worker // Returns the warp error between "dst" and the result of applying the
106*77c1e3ccSAndroid Build Coastguard Worker // motion params that result from fine-tuning "wm" to "ref". Note that "wm" is
107*77c1e3ccSAndroid Build Coastguard Worker // modified in place.
108*77c1e3ccSAndroid Build Coastguard Worker int64_t av1_refine_integerized_param(
109*77c1e3ccSAndroid Build Coastguard Worker     WarpedMotionParams *wm, TransformationType wmtype, int use_hbd, int bd,
110*77c1e3ccSAndroid Build Coastguard Worker     uint8_t *ref, int r_width, int r_height, int r_stride, uint8_t *dst,
111*77c1e3ccSAndroid Build Coastguard Worker     int d_width, int d_height, int d_stride, int n_refinements,
112*77c1e3ccSAndroid Build Coastguard Worker     int64_t ref_frame_error, uint8_t *segment_map, int segment_map_stride);
113*77c1e3ccSAndroid Build Coastguard Worker 
114*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
115*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
116*77c1e3ccSAndroid Build Coastguard Worker #endif
117*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_ENCODER_GLOBAL_MOTION_H_
118