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 https://www.aomedia.org/license/software-license. 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 https://www.aomedia.org/license/patent-license.
10  */
11 
12 #ifndef AOM_AV1_ENCODER_GLOBAL_MOTION_H_
13 #define AOM_AV1_ENCODER_GLOBAL_MOTION_H_
14 
15 #include "EbDefinitions.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #if defined(__clang__) && defined(__has_warning)
22 #if __has_feature(cxx_attributes) && __has_warning("-Wimplicit-fallthrough")
23 #define AOM_FALLTHROUGH_INTENDED [[clang::fallthrough]] // NOLINT
24 #endif
25 #elif defined(__GNUC__) && __GNUC__ >= 7
26 #define AOM_FALLTHROUGH_INTENDED __attribute__((fallthrough)) // NOLINT
27 #endif
28 
29 #ifndef AOM_FALLTHROUGH_INTENDED
30 #define AOM_FALLTHROUGH_INTENDED \
31     do {                         \
32     } while (0)
33 #endif
34 
35 #define MAX_CORNERS 4096
36 #define RANSAC_NUM_MOTIONS 1
37 
38 typedef enum {
39     GLOBAL_MOTION_FEATURE_BASED,
40 } GlobalMotionEstimationType;
41 
42 enum {
43     GM_ERRORADV_TR_0,
44     GM_ERRORADV_TR_1,
45     GM_ERRORADV_TR_2,
46     GM_ERRORADV_TR_TYPES,
47 } UENUM1BYTE(GM_ERRORADV_TYPE);
48 
49 #define MAX_PARAMDIM 9
50 
51 typedef struct {
52     double params[MAX_PARAMDIM - 1];
53     int *  inliers;
54     int    num_inliers;
55 } MotionModel;
56 
57 void svt_av1_convert_model_to_params(const double *params, EbWarpedMotionParams *model);
58 
59 int svt_av1_is_enough_erroradvantage(double best_erroradvantage, int params_cost,
60                                      int erroradv_type);
61 
62 // Returns the av1_warp_error between "dst" and the result of applying the
63 // motion params that result from fine-tuning "wm" to "ref". Note that "wm" is
64 // modified in place.
65 int64_t svt_av1_refine_integerized_param(EbWarpedMotionParams *wm, TransformationType wmtype,
66                                          int use_hbd, int bd, uint8_t *ref, int r_width,
67                                          int r_height, int r_stride, uint8_t *dst, int d_width,
68                                          int d_height, int d_stride, int n_refinements,
69                                          int64_t best_frame_error);
70 
71 /*
72   Computes "num_motions" candidate global motion parameters between two frames.
73   The array "params_by_motion" should be length 8 * "num_motions". The ordering
74   of each set of parameters is best described  by the homography:
75 
76         [x'     (m2 m3 m0   [x
77     z .  y'  =   m4 m5 m1 *  y
78          1]      m6 m7 1)    1]
79 
80   where m{i} represents the ith value in any given set of parameters.
81 
82   "num_inliers" should be length "num_motions", and will be populated with the
83   number of inlier feature points for each motion. Params for which the
84   num_inliers entry is 0 should be ignored by the caller.
85 */
86 int svt_av1_compute_global_motion(TransformationType type, unsigned char *frm_buffer, int frm_width,
87                                   int frm_height, int frm_stride, int *frm_corners,
88                                   int num_frm_corners, uint8_t *ref, int ref_stride, int bit_depth,
89                                   GlobalMotionEstimationType gm_estimation_type,
90                                   int *num_inliers_by_motion, MotionModel *params_by_motion,
91                                   int num_motions);
92 #ifdef __cplusplus
93 } // extern "C"
94 #endif
95 #endif // AOM_AV1_ENCODER_GLOBAL_MOTION_H_
96