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