/src/aom/av1/encoder/global_motion.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include <stdio.h> |
13 | | #include <stdlib.h> |
14 | | #include <stdbool.h> |
15 | | #include <memory.h> |
16 | | #include <math.h> |
17 | | #include <assert.h> |
18 | | |
19 | | #include "config/aom_dsp_rtcd.h" |
20 | | |
21 | | #include "av1/encoder/global_motion.h" |
22 | | |
23 | | #include "av1/common/convolve.h" |
24 | | #include "av1/common/warped_motion.h" |
25 | | |
26 | | #include "av1/encoder/segmentation.h" |
27 | | |
28 | | #define MIN_TRANS_THRESH (1 * GM_TRANS_DECODE_FACTOR) |
29 | | |
30 | | // Border over which to compute the global motion |
31 | 0 | #define ERRORADV_BORDER 0 |
32 | | |
33 | 0 | int av1_is_enough_erroradvantage(double best_erroradvantage, int params_cost) { |
34 | 0 | return best_erroradvantage < erroradv_tr && |
35 | 0 | best_erroradvantage * params_cost < erroradv_prod_tr; |
36 | 0 | } |
37 | | |
38 | 0 | static void convert_to_params(const double *params, int32_t *model) { |
39 | 0 | int i; |
40 | 0 | model[0] = (int32_t)floor(params[0] * (1 << GM_TRANS_PREC_BITS) + 0.5); |
41 | 0 | model[1] = (int32_t)floor(params[1] * (1 << GM_TRANS_PREC_BITS) + 0.5); |
42 | 0 | model[0] = (int32_t)clamp(model[0], GM_TRANS_MIN, GM_TRANS_MAX) * |
43 | 0 | GM_TRANS_DECODE_FACTOR; |
44 | 0 | model[1] = (int32_t)clamp(model[1], GM_TRANS_MIN, GM_TRANS_MAX) * |
45 | 0 | GM_TRANS_DECODE_FACTOR; |
46 | |
|
47 | 0 | for (i = 2; i < 6; ++i) { |
48 | 0 | const int diag_value = ((i == 2 || i == 5) ? (1 << GM_ALPHA_PREC_BITS) : 0); |
49 | 0 | model[i] = (int32_t)floor(params[i] * (1 << GM_ALPHA_PREC_BITS) + 0.5); |
50 | 0 | model[i] = |
51 | 0 | (int32_t)clamp(model[i] - diag_value, GM_ALPHA_MIN, GM_ALPHA_MAX); |
52 | 0 | model[i] = (model[i] + diag_value) * GM_ALPHA_DECODE_FACTOR; |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | | void av1_convert_model_to_params(const double *params, |
57 | 0 | WarpedMotionParams *model) { |
58 | 0 | convert_to_params(params, model->wmmat); |
59 | 0 | model->wmtype = get_wmtype(model); |
60 | 0 | model->invalid = 0; |
61 | 0 | } |
62 | | |
63 | | // Adds some offset to a global motion parameter and handles |
64 | | // all of the necessary precision shifts, clamping, and |
65 | | // zero-centering. |
66 | | static int32_t add_param_offset(int param_index, int32_t param_value, |
67 | 0 | int32_t offset) { |
68 | 0 | const int scale_vals[2] = { GM_TRANS_PREC_DIFF, GM_ALPHA_PREC_DIFF }; |
69 | 0 | const int clamp_vals[2] = { GM_TRANS_MAX, GM_ALPHA_MAX }; |
70 | | // type of param: 0 - translation, 1 - affine |
71 | 0 | const int param_type = (param_index < 2 ? 0 : 1); |
72 | 0 | const int is_one_centered = (param_index == 2 || param_index == 5); |
73 | | |
74 | | // Make parameter zero-centered and offset the shift that was done to make |
75 | | // it compatible with the warped model |
76 | 0 | param_value = (param_value - (is_one_centered << WARPEDMODEL_PREC_BITS)) >> |
77 | 0 | scale_vals[param_type]; |
78 | | // Add desired offset to the rescaled/zero-centered parameter |
79 | 0 | param_value += offset; |
80 | | // Clamp the parameter so it does not overflow the number of bits allotted |
81 | | // to it in the bitstream |
82 | 0 | param_value = (int32_t)clamp(param_value, -clamp_vals[param_type], |
83 | 0 | clamp_vals[param_type]); |
84 | | // Rescale the parameter to WARPEDMODEL_PRECISION_BITS so it is compatible |
85 | | // with the warped motion library |
86 | 0 | param_value *= (1 << scale_vals[param_type]); |
87 | | |
88 | | // Undo the zero-centering step if necessary |
89 | 0 | return param_value + (is_one_centered << WARPEDMODEL_PREC_BITS); |
90 | 0 | } |
91 | | |
92 | 0 | static void force_wmtype(WarpedMotionParams *wm, TransformationType wmtype) { |
93 | 0 | switch (wmtype) { |
94 | 0 | case IDENTITY: |
95 | 0 | wm->wmmat[0] = 0; |
96 | 0 | wm->wmmat[1] = 0; |
97 | 0 | AOM_FALLTHROUGH_INTENDED; |
98 | 0 | case TRANSLATION: |
99 | 0 | wm->wmmat[2] = 1 << WARPEDMODEL_PREC_BITS; |
100 | 0 | wm->wmmat[3] = 0; |
101 | 0 | AOM_FALLTHROUGH_INTENDED; |
102 | 0 | case ROTZOOM: |
103 | 0 | wm->wmmat[4] = -wm->wmmat[3]; |
104 | 0 | wm->wmmat[5] = wm->wmmat[2]; |
105 | 0 | AOM_FALLTHROUGH_INTENDED; |
106 | 0 | case AFFINE: break; |
107 | 0 | default: assert(0); |
108 | 0 | } |
109 | 0 | wm->wmtype = wmtype; |
110 | 0 | } |
111 | | |
112 | | #if CONFIG_AV1_HIGHBITDEPTH |
113 | | static inline int generic_sad_highbd(const uint16_t *const ref, int ref_stride, |
114 | | const uint16_t *const dst, int dst_stride, |
115 | 0 | int p_width, int p_height) { |
116 | | // This function should only be called for patches smaller than |
117 | | // WARP_ERROR_BLOCK x WARP_ERROR_BLOCK. This keeps the number of pixels |
118 | | // small enough that we don't need a 64-bit accumulator |
119 | 0 | assert(p_width <= WARP_ERROR_BLOCK && p_height <= WARP_ERROR_BLOCK); |
120 | |
|
121 | 0 | int sad = 0; |
122 | 0 | for (int i = 0; i < p_height; ++i) { |
123 | 0 | for (int j = 0; j < p_width; ++j) { |
124 | 0 | sad += abs(dst[j + i * dst_stride] - ref[j + i * ref_stride]); |
125 | 0 | } |
126 | 0 | } |
127 | 0 | return sad; |
128 | 0 | } |
129 | | |
130 | | #if WARP_ERROR_BLOCK != 32 |
131 | | #error "Need to change SAD call size in highbd_segmented_frame_error" |
132 | | #endif // WARP_ERROR_BLOCK != 32 |
133 | | static int64_t highbd_segmented_frame_error( |
134 | | const uint16_t *const ref, int ref_stride, const uint16_t *const dst, |
135 | | int dst_stride, int p_width, int p_height, int bd, uint8_t *segment_map, |
136 | 0 | int segment_map_stride) { |
137 | 0 | (void)bd; |
138 | 0 | int patch_w, patch_h; |
139 | 0 | const int error_bsize_w = AOMMIN(p_width, WARP_ERROR_BLOCK); |
140 | 0 | const int error_bsize_h = AOMMIN(p_height, WARP_ERROR_BLOCK); |
141 | 0 | int64_t sum_error = 0; |
142 | 0 | for (int i = 0; i < p_height; i += WARP_ERROR_BLOCK) { |
143 | 0 | for (int j = 0; j < p_width; j += WARP_ERROR_BLOCK) { |
144 | 0 | int seg_x = j >> WARP_ERROR_BLOCK_LOG; |
145 | 0 | int seg_y = i >> WARP_ERROR_BLOCK_LOG; |
146 | | // Only compute the error if this block contains inliers from the motion |
147 | | // model |
148 | 0 | if (!segment_map[seg_y * segment_map_stride + seg_x]) continue; |
149 | | |
150 | | // avoid computing error into the frame padding |
151 | 0 | patch_w = AOMMIN(error_bsize_w, p_width - j); |
152 | 0 | patch_h = AOMMIN(error_bsize_h, p_height - i); |
153 | |
|
154 | 0 | if (patch_w == WARP_ERROR_BLOCK && patch_h == WARP_ERROR_BLOCK) { |
155 | 0 | sum_error += aom_highbd_sad32x32( |
156 | 0 | CONVERT_TO_BYTEPTR(ref + j + i * ref_stride), ref_stride, |
157 | 0 | CONVERT_TO_BYTEPTR(dst + j + i * dst_stride), dst_stride); |
158 | 0 | } else { |
159 | 0 | sum_error += generic_sad_highbd(ref + j + i * ref_stride, ref_stride, |
160 | 0 | dst + j + i * dst_stride, dst_stride, |
161 | 0 | patch_w, patch_h); |
162 | 0 | } |
163 | 0 | } |
164 | 0 | } |
165 | 0 | return sum_error; |
166 | 0 | } |
167 | | |
168 | | #if WARP_ERROR_BLOCK != 32 |
169 | | #error "Need to change SAD call size in highbd_warp_error" |
170 | | #endif // WARP_ERROR_BLOCK != 32 |
171 | | static int64_t highbd_warp_error(WarpedMotionParams *wm, |
172 | | const uint16_t *const ref, int ref_width, |
173 | | int ref_height, int ref_stride, |
174 | | const uint16_t *const dst, int dst_stride, |
175 | | int p_col, int p_row, int p_width, |
176 | | int p_height, int subsampling_x, |
177 | | int subsampling_y, int bd, int64_t best_error, |
178 | 0 | uint8_t *segment_map, int segment_map_stride) { |
179 | 0 | int64_t gm_sumerr = 0; |
180 | 0 | const int error_bsize_w = AOMMIN(p_width, WARP_ERROR_BLOCK); |
181 | 0 | const int error_bsize_h = AOMMIN(p_height, WARP_ERROR_BLOCK); |
182 | 0 | DECLARE_ALIGNED(32, uint16_t, tmp[WARP_ERROR_BLOCK * WARP_ERROR_BLOCK]); |
183 | |
|
184 | 0 | ConvolveParams conv_params = get_conv_params(0, 0, bd); |
185 | 0 | conv_params.use_dist_wtd_comp_avg = 0; |
186 | 0 | for (int i = p_row; i < p_row + p_height; i += WARP_ERROR_BLOCK) { |
187 | 0 | for (int j = p_col; j < p_col + p_width; j += WARP_ERROR_BLOCK) { |
188 | 0 | int seg_x = j >> WARP_ERROR_BLOCK_LOG; |
189 | 0 | int seg_y = i >> WARP_ERROR_BLOCK_LOG; |
190 | | // Only compute the error if this block contains inliers from the motion |
191 | | // model |
192 | 0 | if (!segment_map[seg_y * segment_map_stride + seg_x]) continue; |
193 | | // avoid warping extra 8x8 blocks in the padded region of the frame |
194 | | // when p_width and p_height are not multiples of WARP_ERROR_BLOCK |
195 | 0 | const int warp_w = AOMMIN(error_bsize_w, p_col + ref_width - j); |
196 | 0 | const int warp_h = AOMMIN(error_bsize_h, p_row + ref_height - i); |
197 | 0 | highbd_warp_plane(wm, ref, ref_width, ref_height, ref_stride, tmp, j, i, |
198 | 0 | warp_w, warp_h, WARP_ERROR_BLOCK, subsampling_x, |
199 | 0 | subsampling_y, bd, &conv_params); |
200 | |
|
201 | 0 | if (warp_w == WARP_ERROR_BLOCK && warp_h == WARP_ERROR_BLOCK) { |
202 | 0 | gm_sumerr += aom_highbd_sad32x32( |
203 | 0 | CONVERT_TO_BYTEPTR(tmp), WARP_ERROR_BLOCK, |
204 | 0 | CONVERT_TO_BYTEPTR(dst + j + i * dst_stride), dst_stride); |
205 | 0 | } else { |
206 | 0 | gm_sumerr += |
207 | 0 | generic_sad_highbd(tmp, WARP_ERROR_BLOCK, dst + j + i * dst_stride, |
208 | 0 | dst_stride, warp_w, warp_h); |
209 | 0 | } |
210 | |
|
211 | 0 | if (gm_sumerr > best_error) return INT64_MAX; |
212 | 0 | } |
213 | 0 | } |
214 | 0 | return gm_sumerr; |
215 | 0 | } |
216 | | #endif |
217 | | |
218 | | static inline int generic_sad(const uint8_t *const ref, int ref_stride, |
219 | | const uint8_t *const dst, int dst_stride, |
220 | 0 | int p_width, int p_height) { |
221 | | // This function should only be called for patches smaller than |
222 | | // WARP_ERROR_BLOCK x WARP_ERROR_BLOCK. This keeps the number of pixels |
223 | | // small enough that we don't need a 64-bit accumulator |
224 | 0 | assert(p_width <= WARP_ERROR_BLOCK && p_height <= WARP_ERROR_BLOCK); |
225 | |
|
226 | 0 | int sad = 0; |
227 | 0 | for (int i = 0; i < p_height; ++i) { |
228 | 0 | for (int j = 0; j < p_width; ++j) { |
229 | 0 | sad += abs(dst[j + i * dst_stride] - ref[j + i * ref_stride]); |
230 | 0 | } |
231 | 0 | } |
232 | 0 | return sad; |
233 | 0 | } |
234 | | |
235 | | #if WARP_ERROR_BLOCK != 32 |
236 | | #error "Need to change SAD call size in segmented_warp_error" |
237 | | #endif // WARP_ERROR_BLOCK != 32 |
238 | | static int64_t segmented_frame_error(const uint8_t *const ref, int ref_stride, |
239 | | const uint8_t *const dst, int dst_stride, |
240 | | int p_width, int p_height, |
241 | | uint8_t *segment_map, |
242 | 0 | int segment_map_stride) { |
243 | 0 | int patch_w, patch_h; |
244 | 0 | const int error_bsize_w = AOMMIN(p_width, WARP_ERROR_BLOCK); |
245 | 0 | const int error_bsize_h = AOMMIN(p_height, WARP_ERROR_BLOCK); |
246 | 0 | int64_t sum_error = 0; |
247 | 0 | for (int i = 0; i < p_height; i += WARP_ERROR_BLOCK) { |
248 | 0 | for (int j = 0; j < p_width; j += WARP_ERROR_BLOCK) { |
249 | 0 | int seg_x = j >> WARP_ERROR_BLOCK_LOG; |
250 | 0 | int seg_y = i >> WARP_ERROR_BLOCK_LOG; |
251 | | // Only compute the error if this block contains inliers from the motion |
252 | | // model |
253 | 0 | if (!segment_map[seg_y * segment_map_stride + seg_x]) continue; |
254 | | |
255 | | // avoid computing error into the frame padding |
256 | 0 | patch_w = AOMMIN(error_bsize_w, p_width - j); |
257 | 0 | patch_h = AOMMIN(error_bsize_h, p_height - i); |
258 | |
|
259 | 0 | if (patch_w == WARP_ERROR_BLOCK && patch_h == WARP_ERROR_BLOCK) { |
260 | 0 | sum_error += aom_sad32x32(ref + j + i * ref_stride, ref_stride, |
261 | 0 | dst + j + i * dst_stride, dst_stride); |
262 | 0 | } else { |
263 | 0 | sum_error += |
264 | 0 | generic_sad(ref + j + i * ref_stride, ref_stride, |
265 | 0 | dst + j + i * dst_stride, dst_stride, patch_w, patch_h); |
266 | 0 | } |
267 | 0 | } |
268 | 0 | } |
269 | 0 | return sum_error; |
270 | 0 | } |
271 | | |
272 | | #if WARP_ERROR_BLOCK != 32 |
273 | | #error "Need to change SAD call size in warp_error" |
274 | | #endif // WARP_ERROR_BLOCK != 32 |
275 | | static int64_t warp_error(WarpedMotionParams *wm, const uint8_t *const ref, |
276 | | int ref_width, int ref_height, int ref_stride, |
277 | | const uint8_t *const dst, int dst_stride, int p_col, |
278 | | int p_row, int p_width, int p_height, |
279 | | int subsampling_x, int subsampling_y, |
280 | | int64_t best_error, uint8_t *segment_map, |
281 | 0 | int segment_map_stride) { |
282 | 0 | int64_t gm_sumerr = 0; |
283 | 0 | int warp_w, warp_h; |
284 | 0 | const int error_bsize_w = AOMMIN(p_width, WARP_ERROR_BLOCK); |
285 | 0 | const int error_bsize_h = AOMMIN(p_height, WARP_ERROR_BLOCK); |
286 | 0 | DECLARE_ALIGNED(16, uint8_t, tmp[WARP_ERROR_BLOCK * WARP_ERROR_BLOCK]); |
287 | 0 | ConvolveParams conv_params = get_conv_params(0, 0, 8); |
288 | 0 | conv_params.use_dist_wtd_comp_avg = 0; |
289 | |
|
290 | 0 | for (int i = p_row; i < p_row + p_height; i += WARP_ERROR_BLOCK) { |
291 | 0 | for (int j = p_col; j < p_col + p_width; j += WARP_ERROR_BLOCK) { |
292 | 0 | int seg_x = j >> WARP_ERROR_BLOCK_LOG; |
293 | 0 | int seg_y = i >> WARP_ERROR_BLOCK_LOG; |
294 | | // Only compute the error if this block contains inliers from the motion |
295 | | // model |
296 | 0 | if (!segment_map[seg_y * segment_map_stride + seg_x]) continue; |
297 | | // avoid warping extra 8x8 blocks in the padded region of the frame |
298 | | // when p_width and p_height are not multiples of WARP_ERROR_BLOCK |
299 | 0 | warp_w = AOMMIN(error_bsize_w, p_col + ref_width - j); |
300 | 0 | warp_h = AOMMIN(error_bsize_h, p_row + ref_height - i); |
301 | 0 | warp_plane(wm, ref, ref_width, ref_height, ref_stride, tmp, j, i, warp_w, |
302 | 0 | warp_h, WARP_ERROR_BLOCK, subsampling_x, subsampling_y, |
303 | 0 | &conv_params); |
304 | |
|
305 | 0 | if (warp_w == WARP_ERROR_BLOCK && warp_h == WARP_ERROR_BLOCK) { |
306 | 0 | gm_sumerr += aom_sad32x32(tmp, WARP_ERROR_BLOCK, |
307 | 0 | dst + j + i * dst_stride, dst_stride); |
308 | 0 | } else { |
309 | 0 | gm_sumerr += |
310 | 0 | generic_sad(tmp, WARP_ERROR_BLOCK, dst + j + i * dst_stride, |
311 | 0 | dst_stride, warp_w, warp_h); |
312 | 0 | } |
313 | |
|
314 | 0 | if (gm_sumerr > best_error) return INT64_MAX; |
315 | 0 | } |
316 | 0 | } |
317 | 0 | return gm_sumerr; |
318 | 0 | } |
319 | | |
320 | | int64_t av1_segmented_frame_error(int use_hbd, int bd, const uint8_t *ref, |
321 | | int ref_stride, uint8_t *dst, int dst_stride, |
322 | | int p_width, int p_height, |
323 | | uint8_t *segment_map, |
324 | 0 | int segment_map_stride) { |
325 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
326 | 0 | if (use_hbd) { |
327 | 0 | return highbd_segmented_frame_error( |
328 | 0 | CONVERT_TO_SHORTPTR(ref), ref_stride, CONVERT_TO_SHORTPTR(dst), |
329 | 0 | dst_stride, p_width, p_height, bd, segment_map, segment_map_stride); |
330 | 0 | } |
331 | 0 | #endif |
332 | 0 | (void)use_hbd; |
333 | 0 | (void)bd; |
334 | 0 | return segmented_frame_error(ref, ref_stride, dst, dst_stride, p_width, |
335 | 0 | p_height, segment_map, segment_map_stride); |
336 | 0 | } |
337 | | |
338 | | // Returns the error between the result of applying motion 'wm' to the frame |
339 | | // described by 'ref' and the frame described by 'dst'. |
340 | | static int64_t get_warp_error(WarpedMotionParams *wm, int use_hbd, int bd, |
341 | | const uint8_t *ref, int ref_width, int ref_height, |
342 | | int ref_stride, uint8_t *dst, int dst_stride, |
343 | | int p_col, int p_row, int p_width, int p_height, |
344 | | int subsampling_x, int subsampling_y, |
345 | | int64_t best_error, uint8_t *segment_map, |
346 | 0 | int segment_map_stride) { |
347 | 0 | if (!av1_get_shear_params(wm)) return INT64_MAX; |
348 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
349 | 0 | if (use_hbd) |
350 | 0 | return highbd_warp_error(wm, CONVERT_TO_SHORTPTR(ref), ref_width, |
351 | 0 | ref_height, ref_stride, CONVERT_TO_SHORTPTR(dst), |
352 | 0 | dst_stride, p_col, p_row, p_width, p_height, |
353 | 0 | subsampling_x, subsampling_y, bd, best_error, |
354 | 0 | segment_map, segment_map_stride); |
355 | 0 | #endif |
356 | 0 | (void)use_hbd; |
357 | 0 | (void)bd; |
358 | 0 | return warp_error(wm, ref, ref_width, ref_height, ref_stride, dst, dst_stride, |
359 | 0 | p_col, p_row, p_width, p_height, subsampling_x, |
360 | 0 | subsampling_y, best_error, segment_map, segment_map_stride); |
361 | 0 | } |
362 | | |
363 | | int64_t av1_refine_integerized_param( |
364 | | WarpedMotionParams *wm, TransformationType wmtype, int use_hbd, int bd, |
365 | | uint8_t *ref, int r_width, int r_height, int r_stride, uint8_t *dst, |
366 | | int d_width, int d_height, int d_stride, int n_refinements, |
367 | 0 | int64_t ref_frame_error, uint8_t *segment_map, int segment_map_stride) { |
368 | 0 | static const int max_trans_model_params[TRANS_TYPES] = { 0, 2, 4, 6 }; |
369 | 0 | const int border = ERRORADV_BORDER; |
370 | 0 | int i = 0, p; |
371 | 0 | int n_params = max_trans_model_params[wmtype]; |
372 | 0 | int32_t *param_mat = wm->wmmat; |
373 | 0 | int64_t step_error, best_error; |
374 | 0 | int32_t step; |
375 | 0 | int32_t *param; |
376 | 0 | int32_t curr_param; |
377 | 0 | int32_t best_param; |
378 | |
|
379 | 0 | force_wmtype(wm, wmtype); |
380 | 0 | wm->wmtype = get_wmtype(wm); |
381 | |
|
382 | 0 | if (n_refinements == 0) { |
383 | | // Compute the maximum error value that will be accepted, so that |
384 | | // get_warp_error can terminate early if it proves the model will not |
385 | | // be accepted. |
386 | 0 | int64_t selection_threshold = (int64_t)lrint(ref_frame_error * erroradv_tr); |
387 | 0 | return get_warp_error(wm, use_hbd, bd, ref, r_width, r_height, r_stride, |
388 | 0 | dst + border * d_stride + border, d_stride, border, |
389 | 0 | border, d_width - 2 * border, d_height - 2 * border, |
390 | 0 | 0, 0, selection_threshold, segment_map, |
391 | 0 | segment_map_stride); |
392 | 0 | } |
393 | | |
394 | | // When refining, use a slightly higher threshold for the initial error |
395 | | // calculation - see comment above erroradv_early_tr for why. |
396 | 0 | int64_t selection_threshold = |
397 | 0 | (int64_t)lrint(ref_frame_error * erroradv_early_tr); |
398 | 0 | best_error = |
399 | 0 | get_warp_error(wm, use_hbd, bd, ref, r_width, r_height, r_stride, |
400 | 0 | dst + border * d_stride + border, d_stride, border, border, |
401 | 0 | d_width - 2 * border, d_height - 2 * border, 0, 0, |
402 | 0 | selection_threshold, segment_map, segment_map_stride); |
403 | |
|
404 | 0 | if (best_error > selection_threshold) { |
405 | 0 | return INT64_MAX; |
406 | 0 | } |
407 | | |
408 | 0 | step = 1 << (n_refinements - 1); |
409 | 0 | for (i = 0; i < n_refinements; i++, step >>= 1) { |
410 | 0 | for (p = 0; p < n_params; ++p) { |
411 | 0 | int step_dir = 0; |
412 | 0 | param = param_mat + p; |
413 | 0 | curr_param = *param; |
414 | 0 | best_param = curr_param; |
415 | | // look to the left |
416 | | // Note: We have to use force_wmtype() to keep the proper symmetry for |
417 | | // ROTZOOM type models |
418 | 0 | *param = add_param_offset(p, curr_param, -step); |
419 | 0 | force_wmtype(wm, wmtype); |
420 | 0 | step_error = |
421 | 0 | get_warp_error(wm, use_hbd, bd, ref, r_width, r_height, r_stride, |
422 | 0 | dst + border * d_stride + border, d_stride, border, |
423 | 0 | border, d_width - 2 * border, d_height - 2 * border, 0, |
424 | 0 | 0, best_error, segment_map, segment_map_stride); |
425 | 0 | if (step_error < best_error) { |
426 | 0 | best_error = step_error; |
427 | 0 | best_param = *param; |
428 | 0 | step_dir = -1; |
429 | 0 | } |
430 | | |
431 | | // look to the right |
432 | 0 | *param = add_param_offset(p, curr_param, step); |
433 | 0 | force_wmtype(wm, wmtype); |
434 | 0 | step_error = |
435 | 0 | get_warp_error(wm, use_hbd, bd, ref, r_width, r_height, r_stride, |
436 | 0 | dst + border * d_stride + border, d_stride, border, |
437 | 0 | border, d_width - 2 * border, d_height - 2 * border, 0, |
438 | 0 | 0, best_error, segment_map, segment_map_stride); |
439 | 0 | if (step_error < best_error) { |
440 | 0 | best_error = step_error; |
441 | 0 | best_param = *param; |
442 | 0 | step_dir = 1; |
443 | 0 | } |
444 | | |
445 | | // look to the direction chosen above repeatedly until error increases |
446 | | // for the biggest step size |
447 | 0 | while (step_dir) { |
448 | 0 | *param = add_param_offset(p, best_param, step * step_dir); |
449 | 0 | force_wmtype(wm, wmtype); |
450 | 0 | step_error = |
451 | 0 | get_warp_error(wm, use_hbd, bd, ref, r_width, r_height, r_stride, |
452 | 0 | dst + border * d_stride + border, d_stride, border, |
453 | 0 | border, d_width - 2 * border, d_height - 2 * border, |
454 | 0 | 0, 0, best_error, segment_map, segment_map_stride); |
455 | 0 | if (step_error < best_error) { |
456 | 0 | best_error = step_error; |
457 | 0 | best_param = *param; |
458 | 0 | } else { |
459 | 0 | step_dir = 0; |
460 | 0 | } |
461 | 0 | } |
462 | | |
463 | | // Restore best parameter value so far |
464 | 0 | *param = best_param; |
465 | 0 | force_wmtype(wm, wmtype); |
466 | 0 | } |
467 | 0 | } |
468 | |
|
469 | 0 | wm->wmtype = get_wmtype(wm); |
470 | | // Recompute shear params for the refined model |
471 | | // This should never fail, because we only ever consider warp-able models |
472 | 0 | if (!av1_get_shear_params(wm)) { |
473 | 0 | assert(0); |
474 | 0 | } |
475 | 0 | return best_error; |
476 | 0 | } |
477 | | |
478 | 0 | #define FEAT_COUNT_TR 3 |
479 | 0 | #define SEG_COUNT_TR 48 |
480 | | void av1_compute_feature_segmentation_map(uint8_t *segment_map, int width, |
481 | | int height, int *inliers, |
482 | 0 | int num_inliers) { |
483 | 0 | int seg_count = 0; |
484 | 0 | memset(segment_map, 0, sizeof(*segment_map) * width * height); |
485 | |
|
486 | 0 | for (int i = 0; i < num_inliers; i++) { |
487 | 0 | int x = inliers[i * 2]; |
488 | 0 | int y = inliers[i * 2 + 1]; |
489 | 0 | int seg_x = x >> WARP_ERROR_BLOCK_LOG; |
490 | 0 | int seg_y = y >> WARP_ERROR_BLOCK_LOG; |
491 | 0 | segment_map[seg_y * width + seg_x] += 1; |
492 | 0 | } |
493 | |
|
494 | 0 | for (int i = 0; i < height; i++) { |
495 | 0 | for (int j = 0; j < width; j++) { |
496 | 0 | uint8_t feat_count = segment_map[i * width + j]; |
497 | 0 | segment_map[i * width + j] = (feat_count >= FEAT_COUNT_TR); |
498 | 0 | seg_count += (segment_map[i * width + j]); |
499 | 0 | } |
500 | 0 | } |
501 | | |
502 | | // If this motion does not make up a large enough portion of the frame, |
503 | | // use the unsegmented version of the error metric |
504 | 0 | if (seg_count < SEG_COUNT_TR) |
505 | 0 | memset(segment_map, 1, width * height * sizeof(*segment_map)); |
506 | 0 | } |