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_COMMON_WARPED_MOTION_H_
13 #define AOM_AV1_COMMON_WARPED_MOTION_H_
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <memory.h>
18 #include <math.h>
19 #include <assert.h>
20
21 #include "config/aom_config.h"
22
23 #include "aom_ports/mem.h"
24 #include "aom_dsp/aom_dsp_common.h"
25 #include "av1/common/mv.h"
26 #include "av1/common/convolve.h"
27
28 #define LEAST_SQUARES_SAMPLES_MAX_BITS 3
29 #define LEAST_SQUARES_SAMPLES_MAX (1 << LEAST_SQUARES_SAMPLES_MAX_BITS)
30 #define SAMPLES_ARRAY_SIZE (LEAST_SQUARES_SAMPLES_MAX * 2)
31 #define WARPED_MOTION_DEBUG 0
32 #define DEFAULT_WMTYPE AFFINE
33 #define WARP_ERROR_BLOCK_LOG 5
34 #define WARP_ERROR_BLOCK (1 << WARP_ERROR_BLOCK_LOG)
35
36 extern const int16_t av1_warped_filter[WARPEDPIXEL_PREC_SHIFTS * 3 + 1][8];
37
38 DECLARE_ALIGNED(8, extern const int8_t,
39 av1_filter_8bit[WARPEDPIXEL_PREC_SHIFTS * 3 + 1][8]);
40
41 /* clang-format off */
42 static const int error_measure_lut[512] = {
43 // pow 0.7
44 16384, 16339, 16294, 16249, 16204, 16158, 16113, 16068,
45 16022, 15977, 15932, 15886, 15840, 15795, 15749, 15703,
46 15657, 15612, 15566, 15520, 15474, 15427, 15381, 15335,
47 15289, 15242, 15196, 15149, 15103, 15056, 15010, 14963,
48 14916, 14869, 14822, 14775, 14728, 14681, 14634, 14587,
49 14539, 14492, 14445, 14397, 14350, 14302, 14254, 14206,
50 14159, 14111, 14063, 14015, 13967, 13918, 13870, 13822,
51 13773, 13725, 13676, 13628, 13579, 13530, 13481, 13432,
52 13383, 13334, 13285, 13236, 13187, 13137, 13088, 13038,
53 12988, 12939, 12889, 12839, 12789, 12739, 12689, 12639,
54 12588, 12538, 12487, 12437, 12386, 12335, 12285, 12234,
55 12183, 12132, 12080, 12029, 11978, 11926, 11875, 11823,
56 11771, 11719, 11667, 11615, 11563, 11511, 11458, 11406,
57 11353, 11301, 11248, 11195, 11142, 11089, 11036, 10982,
58 10929, 10875, 10822, 10768, 10714, 10660, 10606, 10552,
59 10497, 10443, 10388, 10333, 10279, 10224, 10168, 10113,
60 10058, 10002, 9947, 9891, 9835, 9779, 9723, 9666,
61 9610, 9553, 9497, 9440, 9383, 9326, 9268, 9211,
62 9153, 9095, 9037, 8979, 8921, 8862, 8804, 8745,
63 8686, 8627, 8568, 8508, 8449, 8389, 8329, 8269,
64 8208, 8148, 8087, 8026, 7965, 7903, 7842, 7780,
65 7718, 7656, 7593, 7531, 7468, 7405, 7341, 7278,
66 7214, 7150, 7086, 7021, 6956, 6891, 6826, 6760,
67 6695, 6628, 6562, 6495, 6428, 6361, 6293, 6225,
68 6157, 6089, 6020, 5950, 5881, 5811, 5741, 5670,
69 5599, 5527, 5456, 5383, 5311, 5237, 5164, 5090,
70 5015, 4941, 4865, 4789, 4713, 4636, 4558, 4480,
71 4401, 4322, 4242, 4162, 4080, 3998, 3916, 3832,
72 3748, 3663, 3577, 3490, 3402, 3314, 3224, 3133,
73 3041, 2948, 2854, 2758, 2661, 2562, 2461, 2359,
74 2255, 2148, 2040, 1929, 1815, 1698, 1577, 1452,
75 1323, 1187, 1045, 894, 731, 550, 339, 0,
76 339, 550, 731, 894, 1045, 1187, 1323, 1452,
77 1577, 1698, 1815, 1929, 2040, 2148, 2255, 2359,
78 2461, 2562, 2661, 2758, 2854, 2948, 3041, 3133,
79 3224, 3314, 3402, 3490, 3577, 3663, 3748, 3832,
80 3916, 3998, 4080, 4162, 4242, 4322, 4401, 4480,
81 4558, 4636, 4713, 4789, 4865, 4941, 5015, 5090,
82 5164, 5237, 5311, 5383, 5456, 5527, 5599, 5670,
83 5741, 5811, 5881, 5950, 6020, 6089, 6157, 6225,
84 6293, 6361, 6428, 6495, 6562, 6628, 6695, 6760,
85 6826, 6891, 6956, 7021, 7086, 7150, 7214, 7278,
86 7341, 7405, 7468, 7531, 7593, 7656, 7718, 7780,
87 7842, 7903, 7965, 8026, 8087, 8148, 8208, 8269,
88 8329, 8389, 8449, 8508, 8568, 8627, 8686, 8745,
89 8804, 8862, 8921, 8979, 9037, 9095, 9153, 9211,
90 9268, 9326, 9383, 9440, 9497, 9553, 9610, 9666,
91 9723, 9779, 9835, 9891, 9947, 10002, 10058, 10113,
92 10168, 10224, 10279, 10333, 10388, 10443, 10497, 10552,
93 10606, 10660, 10714, 10768, 10822, 10875, 10929, 10982,
94 11036, 11089, 11142, 11195, 11248, 11301, 11353, 11406,
95 11458, 11511, 11563, 11615, 11667, 11719, 11771, 11823,
96 11875, 11926, 11978, 12029, 12080, 12132, 12183, 12234,
97 12285, 12335, 12386, 12437, 12487, 12538, 12588, 12639,
98 12689, 12739, 12789, 12839, 12889, 12939, 12988, 13038,
99 13088, 13137, 13187, 13236, 13285, 13334, 13383, 13432,
100 13481, 13530, 13579, 13628, 13676, 13725, 13773, 13822,
101 13870, 13918, 13967, 14015, 14063, 14111, 14159, 14206,
102 14254, 14302, 14350, 14397, 14445, 14492, 14539, 14587,
103 14634, 14681, 14728, 14775, 14822, 14869, 14916, 14963,
104 15010, 15056, 15103, 15149, 15196, 15242, 15289, 15335,
105 15381, 15427, 15474, 15520, 15566, 15612, 15657, 15703,
106 15749, 15795, 15840, 15886, 15932, 15977, 16022, 16068,
107 16113, 16158, 16204, 16249, 16294, 16339, 16384, 16384,
108 };
109 /* clang-format on */
110
111 static const uint8_t warp_pad_left[14][16] = {
112 { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
113 { 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
114 { 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
115 { 4, 4, 4, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
116 { 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
117 { 6, 6, 6, 6, 6, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
118 { 7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
119 { 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 10, 11, 12, 13, 14, 15 },
120 { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 11, 12, 13, 14, 15 },
121 { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 12, 13, 14, 15 },
122 { 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 13, 14, 15 },
123 { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 14, 15 },
124 { 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 15 },
125 { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15 },
126 };
127
128 static const uint8_t warp_pad_right[14][16] = {
129 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14 },
130 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 13, 13 },
131 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12 },
132 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11, 11, 11 },
133 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 10, 10 },
134 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9 },
135 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8 },
136 { 0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7 },
137 { 0, 1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
138 { 0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 },
139 { 0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
140 { 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
141 { 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
142 { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
143 };
144
error_measure(int err)145 static INLINE int error_measure(int err) {
146 return error_measure_lut[255 + err];
147 }
148
149 // Returns the error between the frame described by 'ref' and the frame
150 // described by 'dst'.
151 int64_t av1_frame_error(int use_hbd, int bd, const uint8_t *ref, int stride,
152 uint8_t *dst, int p_width, int p_height, int p_stride);
153
154 int64_t av1_segmented_frame_error(int use_hbd, int bd, const uint8_t *ref,
155 int stride, uint8_t *dst, int p_width,
156 int p_height, int p_stride,
157 uint8_t *segment_map, int segment_map_stride);
158
159 int64_t av1_calc_highbd_frame_error(const uint16_t *const ref, int stride,
160 const uint16_t *const dst, int p_width,
161 int p_height, int p_stride, int bd);
162
163 void highbd_warp_plane(WarpedMotionParams *wm, const uint16_t *const ref,
164 int width, int height, int stride, uint16_t *const pred,
165 int p_col, int p_row, int p_width, int p_height,
166 int p_stride, int subsampling_x, int subsampling_y,
167 int bd, ConvolveParams *conv_params);
168
169 void warp_plane(WarpedMotionParams *wm, const uint8_t *const ref, int width,
170 int height, int stride, uint8_t *pred, int p_col, int p_row,
171 int p_width, int p_height, int p_stride, int subsampling_x,
172 int subsampling_y, ConvolveParams *conv_params);
173
174 void av1_warp_plane(WarpedMotionParams *wm, int use_hbd, int bd,
175 const uint8_t *ref, int width, int height, int stride,
176 uint8_t *pred, int p_col, int p_row, int p_width,
177 int p_height, int p_stride, int subsampling_x,
178 int subsampling_y, ConvolveParams *conv_params);
179
180 int av1_find_projection(int np, const int *pts1, const int *pts2,
181 BLOCK_SIZE bsize, int mvy, int mvx,
182 WarpedMotionParams *wm_params, int mi_row, int mi_col);
183
184 int av1_get_shear_params(WarpedMotionParams *wm);
185 #endif // AOM_AV1_COMMON_WARPED_MOTION_H_
186