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