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_scale/yv12config.h" 17 #include "aom_util/aom_thread.h" 18 19 #include "av1/common/mv.h" 20 #include "av1/common/warped_motion.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 #define MAX_CORNERS 4096 27 #define RANSAC_NUM_MOTIONS 1 28 #define GM_REFINEMENT_COUNT 5 29 #define MAX_DIRECTIONS 2 30 31 typedef enum { 32 GLOBAL_MOTION_FEATURE_BASED, 33 GLOBAL_MOTION_DISFLOW_BASED, 34 } GlobalMotionEstimationType; 35 36 unsigned char *av1_downconvert_frame(YV12_BUFFER_CONFIG *frm, int bit_depth); 37 38 typedef struct { 39 double params[MAX_PARAMDIM - 1]; 40 int *inliers; 41 int num_inliers; 42 } MotionModel; 43 44 // The structure holds a valid reference frame type and its temporal distance 45 // from the source frame. 46 typedef struct { 47 int distance; 48 MV_REFERENCE_FRAME frame; 49 } FrameDistPair; 50 51 typedef struct { 52 // Array of structure which holds the global motion parameters for a given 53 // motion model. params_by_motion[i] holds the parameters for a given motion 54 // model for the ith ransac motion. 55 MotionModel params_by_motion[RANSAC_NUM_MOTIONS]; 56 57 // Pointer to hold inliers from motion model. 58 uint8_t *segment_map; 59 } GlobalMotionThreadData; 60 61 typedef struct { 62 // Holds the mapping of each thread to past/future direction. 63 // thread_id_to_dir[i] indicates the direction id (past - 0/future - 1) 64 // assigned to the ith thread. 65 int8_t thread_id_to_dir[MAX_NUM_THREADS]; 66 67 // A flag which holds the early exit status based on the speed feature 68 // 'prune_ref_frame_for_gm_search'. early_exit[i] will be set if the speed 69 // feature based early exit happens in the direction 'i'. 70 int8_t early_exit[MAX_DIRECTIONS]; 71 72 // Counter for the next reference frame to be processed. 73 // next_frame_to_process[i] will hold the count of next reference frame to be 74 // processed in the direction 'i'. 75 int8_t next_frame_to_process[MAX_DIRECTIONS]; 76 } JobInfo; 77 78 typedef struct { 79 // Data related to assigning jobs for global motion multi-threading. 80 JobInfo job_info; 81 82 // Data specific to each worker in global motion multi-threading. 83 // thread_data[i] stores the thread specific data for worker 'i'. 84 GlobalMotionThreadData *thread_data; 85 86 #if CONFIG_MULTITHREAD 87 // Mutex lock used while dispatching jobs. 88 pthread_mutex_t *mutex_; 89 #endif 90 91 // Width and height for which segment_map is allocated for each thread. 92 int allocated_width; 93 int allocated_height; 94 95 // Number of workers for which thread_data is allocated. 96 int8_t allocated_workers; 97 } AV1GlobalMotionSync; 98 99 void av1_convert_model_to_params(const double *params, 100 WarpedMotionParams *model); 101 102 // TODO(sarahparker) These need to be retuned for speed 0 and 1 to 103 // maximize gains from segmented error metric 104 static const double erroradv_tr = 0.65; 105 static const double erroradv_prod_tr = 20000; 106 107 int av1_is_enough_erroradvantage(double best_erroradvantage, int params_cost); 108 109 void av1_compute_feature_segmentation_map(uint8_t *segment_map, int width, 110 int height, int *inliers, 111 int num_inliers); 112 113 // Returns the error between the result of applying motion 'wm' to the frame 114 // described by 'ref' and the frame described by 'dst'. 115 int64_t av1_warp_error(WarpedMotionParams *wm, int use_hbd, int bd, 116 const uint8_t *ref, int width, int height, int stride, 117 uint8_t *dst, int p_col, int p_row, int p_width, 118 int p_height, int p_stride, int subsampling_x, 119 int subsampling_y, int64_t best_error, 120 uint8_t *segment_map, int segment_map_stride); 121 122 // Returns the av1_warp_error between "dst" and the result of applying the 123 // motion params that result from fine-tuning "wm" to "ref". Note that "wm" is 124 // modified in place. 125 int64_t av1_refine_integerized_param( 126 WarpedMotionParams *wm, TransformationType wmtype, int use_hbd, int bd, 127 uint8_t *ref, int r_width, int r_height, int r_stride, uint8_t *dst, 128 int d_width, int d_height, int d_stride, int n_refinements, 129 int64_t best_frame_error, uint8_t *segment_map, int segment_map_stride, 130 int64_t erroradv_threshold); 131 132 /* 133 Computes "num_motions" candidate global motion parameters between two frames. 134 The array "params_by_motion" should be length 8 * "num_motions". The ordering 135 of each set of parameters is best described by the homography: 136 137 [x' (m2 m3 m0 [x 138 z . y' = m4 m5 m1 * y 139 1] m6 m7 1) 1] 140 141 where m{i} represents the ith value in any given set of parameters. 142 143 "num_inliers" should be length "num_motions", and will be populated with the 144 number of inlier feature points for each motion. Params for which the 145 num_inliers entry is 0 should be ignored by the caller. 146 */ 147 int av1_compute_global_motion(TransformationType type, 148 unsigned char *src_buffer, int src_width, 149 int src_height, int src_stride, int *src_corners, 150 int num_src_corners, YV12_BUFFER_CONFIG *ref, 151 int bit_depth, 152 GlobalMotionEstimationType gm_estimation_type, 153 int *num_inliers_by_motion, 154 MotionModel *params_by_motion, int num_motions); 155 #ifdef __cplusplus 156 } // extern "C" 157 #endif 158 #endif // AOM_AV1_ENCODER_GLOBAL_MOTION_H_ 159