xref: /aosp_15_r20/external/libaom/aom_dsp/flow_estimation/disflow.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_AOM_DSP_FLOW_ESTIMATION_DISFLOW_H_
13 #define AOM_AOM_DSP_FLOW_ESTIMATION_DISFLOW_H_
14 
15 #include <stdbool.h>
16 
17 #include "aom_dsp/flow_estimation/flow_estimation.h"
18 #include "aom_scale/yv12config.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 // Number of pyramid levels in disflow computation
25 #define DISFLOW_PYRAMID_LEVELS 12
26 
27 // Size of square patches in the disflow dense grid
28 // Must be a power of 2
29 #define DISFLOW_PATCH_SIZE_LOG2 3
30 #define DISFLOW_PATCH_SIZE (1 << DISFLOW_PATCH_SIZE_LOG2)
31 // Center point of square patch
32 #define DISFLOW_PATCH_CENTER ((DISFLOW_PATCH_SIZE / 2) - 1)
33 
34 // Overall scale of the `dx`, `dy` and `dt` arrays in the disflow code
35 // In other words, the various derivatives are calculated with an internal
36 // precision of (8 + DISFLOW_DERIV_SCALE_LOG2) bits, from an 8-bit input.
37 //
38 // This must be carefully synchronized with the code in sobel_filter()
39 // (which fills the dx and dy arrays) and compute_flow_error() (which
40 // fills dt); see the comments in those functions for more details
41 #define DISFLOW_DERIV_SCALE_LOG2 3
42 #define DISFLOW_DERIV_SCALE (1 << DISFLOW_DERIV_SCALE_LOG2)
43 
44 // Scale factor applied to each step in the main refinement loop
45 //
46 // This should be <= 1.0 to avoid overshoot. Values below 1.0
47 // may help in some cases, but slow convergence overall, so
48 // will require careful tuning.
49 // TODO(rachelbarker): Tune this value
50 #define DISFLOW_STEP_SIZE 1.0
51 
52 // Step size at which we should terminate iteration
53 // The idea here is that, if we take a step which is much smaller than 1px in
54 // size, then the values won't change much from iteration to iteration, so
55 // many future steps will also be small, and that won't have much effect
56 // on the ultimate result. So we can terminate early.
57 //
58 // To look at it another way, when we take a small step, that means that
59 // either we're near to convergence (so can stop), or we're stuck in a
60 // shallow valley and will take many iterations to get unstuck.
61 //
62 // Solving the latter properly requires fancier methods, such as "gradient
63 // descent with momentum". For now, we terminate to avoid wasting a ton of
64 // time on points which are either nearly-converged or stuck.
65 //
66 // Terminating at 1/8 px seems to give good results for global motion estimation
67 #define DISFLOW_STEP_SIZE_THRESOLD (1. / 8.)
68 
69 // Max number of iterations if warp convergence is not found
70 #define DISFLOW_MAX_ITR 4
71 
72 // Internal precision of cubic interpolation filters
73 // The limiting factor here is that:
74 // * Before integerizing, the maximum value of any kernel tap is 1.0
75 // * After integerizing, each tap must fit into an int16_t.
76 // Thus the largest multiplier we can get away with is 2^14 = 16384,
77 // as 2^15 = 32768 is too large to fit in an int16_t.
78 #define DISFLOW_INTERP_BITS 14
79 
80 typedef struct {
81   // Start of allocation for u and v buffers
82   double *buf0;
83 
84   // x and y directions of flow, per patch
85   double *u;
86   double *v;
87 
88   // Sizes of the above arrays
89   int width;
90   int height;
91   int stride;
92 } FlowField;
93 
94 bool av1_compute_global_motion_disflow(
95     TransformationType type, YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *ref,
96     int bit_depth, int downsample_level, MotionModel *motion_models,
97     int num_motion_models, bool *mem_alloc_failed);
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif  // AOM_AOM_DSP_FLOW_ESTIMATION_DISFLOW_H_
104