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