1 /*
2 * Copyright (c) 2020, 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 "aom_dsp/binary_codes_writer.h"
13
14 #include "aom_dsp/flow_estimation/corner_detect.h"
15 #include "aom_dsp/flow_estimation/flow_estimation.h"
16 #include "aom_dsp/pyramid.h"
17 #include "av1/common/warped_motion.h"
18 #include "av1/encoder/encoder.h"
19 #include "av1/encoder/ethread.h"
20 #include "av1/encoder/rdopt.h"
21 #include "av1/encoder/global_motion_facade.h"
22
23 // Range of model types to search
24 #define FIRST_GLOBAL_TRANS_TYPE ROTZOOM
25 #define LAST_GLOBAL_TRANS_TYPE ROTZOOM
26
27 // Computes the cost for the warp parameters.
gm_get_params_cost(const WarpedMotionParams * gm,const WarpedMotionParams * ref_gm,int allow_hp)28 static int gm_get_params_cost(const WarpedMotionParams *gm,
29 const WarpedMotionParams *ref_gm, int allow_hp) {
30 int params_cost = 0;
31 int trans_bits, trans_prec_diff;
32 switch (gm->wmtype) {
33 case AFFINE:
34 case ROTZOOM:
35 params_cost += aom_count_signed_primitive_refsubexpfin(
36 GM_ALPHA_MAX + 1, SUBEXPFIN_K,
37 (ref_gm->wmmat[2] >> GM_ALPHA_PREC_DIFF) - (1 << GM_ALPHA_PREC_BITS),
38 (gm->wmmat[2] >> GM_ALPHA_PREC_DIFF) - (1 << GM_ALPHA_PREC_BITS));
39 params_cost += aom_count_signed_primitive_refsubexpfin(
40 GM_ALPHA_MAX + 1, SUBEXPFIN_K,
41 (ref_gm->wmmat[3] >> GM_ALPHA_PREC_DIFF),
42 (gm->wmmat[3] >> GM_ALPHA_PREC_DIFF));
43 if (gm->wmtype >= AFFINE) {
44 params_cost += aom_count_signed_primitive_refsubexpfin(
45 GM_ALPHA_MAX + 1, SUBEXPFIN_K,
46 (ref_gm->wmmat[4] >> GM_ALPHA_PREC_DIFF),
47 (gm->wmmat[4] >> GM_ALPHA_PREC_DIFF));
48 params_cost += aom_count_signed_primitive_refsubexpfin(
49 GM_ALPHA_MAX + 1, SUBEXPFIN_K,
50 (ref_gm->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
51 (1 << GM_ALPHA_PREC_BITS),
52 (gm->wmmat[5] >> GM_ALPHA_PREC_DIFF) - (1 << GM_ALPHA_PREC_BITS));
53 }
54 AOM_FALLTHROUGH_INTENDED;
55 case TRANSLATION:
56 trans_bits = (gm->wmtype == TRANSLATION)
57 ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
58 : GM_ABS_TRANS_BITS;
59 trans_prec_diff = (gm->wmtype == TRANSLATION)
60 ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
61 : GM_TRANS_PREC_DIFF;
62 params_cost += aom_count_signed_primitive_refsubexpfin(
63 (1 << trans_bits) + 1, SUBEXPFIN_K,
64 (ref_gm->wmmat[0] >> trans_prec_diff),
65 (gm->wmmat[0] >> trans_prec_diff));
66 params_cost += aom_count_signed_primitive_refsubexpfin(
67 (1 << trans_bits) + 1, SUBEXPFIN_K,
68 (ref_gm->wmmat[1] >> trans_prec_diff),
69 (gm->wmmat[1] >> trans_prec_diff));
70 AOM_FALLTHROUGH_INTENDED;
71 case IDENTITY: break;
72 default: assert(0);
73 }
74 return (params_cost << AV1_PROB_COST_SHIFT);
75 }
76
77 // For the given reference frame, computes the global motion parameters for
78 // different motion models and finds the best.
compute_global_motion_for_ref_frame(AV1_COMP * cpi,struct aom_internal_error_info * error_info,YV12_BUFFER_CONFIG * ref_buf[REF_FRAMES],int frame,MotionModel * motion_models,uint8_t * segment_map,const int segment_map_w,const int segment_map_h,const WarpedMotionParams * ref_params)79 static inline void compute_global_motion_for_ref_frame(
80 AV1_COMP *cpi, struct aom_internal_error_info *error_info,
81 YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES], int frame,
82 MotionModel *motion_models, uint8_t *segment_map, const int segment_map_w,
83 const int segment_map_h, const WarpedMotionParams *ref_params) {
84 AV1_COMMON *const cm = &cpi->common;
85 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
86 int src_width = cpi->source->y_crop_width;
87 int src_height = cpi->source->y_crop_height;
88 int src_stride = cpi->source->y_stride;
89 assert(ref_buf[frame] != NULL);
90 int bit_depth = cpi->common.seq_params->bit_depth;
91 GlobalMotionMethod global_motion_method = default_global_motion_method;
92 int downsample_level = cpi->sf.gm_sf.downsample_level;
93 int num_refinements = cpi->sf.gm_sf.num_refinement_steps;
94 bool mem_alloc_failed = false;
95
96 // Select the best model based on fractional error reduction.
97 // By initializing this to erroradv_tr, the same logic which is used to
98 // select the best model will automatically filter out any model which
99 // doesn't meet the required quality threshold
100 double best_erroradv = erroradv_tr;
101 for (TransformationType model = FIRST_GLOBAL_TRANS_TYPE;
102 model <= LAST_GLOBAL_TRANS_TYPE; ++model) {
103 if (!aom_compute_global_motion(model, cpi->source, ref_buf[frame],
104 bit_depth, global_motion_method,
105 downsample_level, motion_models,
106 RANSAC_NUM_MOTIONS, &mem_alloc_failed)) {
107 if (mem_alloc_failed) {
108 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
109 "Failed to allocate global motion buffers");
110 }
111 continue;
112 }
113
114 for (int i = 0; i < RANSAC_NUM_MOTIONS; ++i) {
115 if (motion_models[i].num_inliers == 0) continue;
116
117 WarpedMotionParams tmp_wm_params;
118 av1_convert_model_to_params(motion_models[i].params, &tmp_wm_params);
119
120 // Check that the generated model is warp-able
121 if (!av1_get_shear_params(&tmp_wm_params)) continue;
122
123 // Skip models that we won't use (IDENTITY or TRANSLATION)
124 //
125 // For IDENTITY type models, we don't need to evaluate anything because
126 // all the following logic is effectively comparing the estimated model
127 // to an identity model.
128 //
129 // For TRANSLATION type global motion models, gm_get_motion_vector() gives
130 // the wrong motion vector (see comments in that function for details).
131 // As translation-type models do not give much gain, we can avoid this bug
132 // by never choosing a TRANSLATION type model
133 if (tmp_wm_params.wmtype <= TRANSLATION) continue;
134
135 av1_compute_feature_segmentation_map(
136 segment_map, segment_map_w, segment_map_h, motion_models[i].inliers,
137 motion_models[i].num_inliers);
138
139 int64_t ref_frame_error = av1_segmented_frame_error(
140 is_cur_buf_hbd(xd), xd->bd, ref_buf[frame]->y_buffer,
141 ref_buf[frame]->y_stride, cpi->source->y_buffer, src_stride,
142 src_width, src_height, segment_map, segment_map_w);
143
144 if (ref_frame_error == 0) continue;
145
146 const int64_t warp_error = av1_refine_integerized_param(
147 &tmp_wm_params, tmp_wm_params.wmtype, is_cur_buf_hbd(xd), xd->bd,
148 ref_buf[frame]->y_buffer, ref_buf[frame]->y_crop_width,
149 ref_buf[frame]->y_crop_height, ref_buf[frame]->y_stride,
150 cpi->source->y_buffer, src_width, src_height, src_stride,
151 num_refinements, ref_frame_error, segment_map, segment_map_w);
152
153 // av1_refine_integerized_param() can return a simpler model type than
154 // its input, so re-check model type here
155 if (tmp_wm_params.wmtype <= TRANSLATION) continue;
156
157 double erroradvantage = (double)warp_error / ref_frame_error;
158
159 // Check that the model signaling cost is not too high
160 if (!av1_is_enough_erroradvantage(
161 erroradvantage,
162 gm_get_params_cost(&tmp_wm_params, ref_params,
163 cm->features.allow_high_precision_mv))) {
164 continue;
165 }
166
167 if (erroradvantage < best_erroradv) {
168 best_erroradv = erroradvantage;
169 // Save the wm_params modified by
170 // av1_refine_integerized_param() rather than motion index to
171 // avoid rerunning refine() below.
172 memcpy(&(cm->global_motion[frame]), &tmp_wm_params,
173 sizeof(WarpedMotionParams));
174 }
175 }
176 }
177 }
178
179 // Computes global motion for the given reference frame.
av1_compute_gm_for_valid_ref_frames(AV1_COMP * cpi,struct aom_internal_error_info * error_info,YV12_BUFFER_CONFIG * ref_buf[REF_FRAMES],int frame,MotionModel * motion_models,uint8_t * segment_map,int segment_map_w,int segment_map_h)180 void av1_compute_gm_for_valid_ref_frames(
181 AV1_COMP *cpi, struct aom_internal_error_info *error_info,
182 YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES], int frame,
183 MotionModel *motion_models, uint8_t *segment_map, int segment_map_w,
184 int segment_map_h) {
185 AV1_COMMON *const cm = &cpi->common;
186 const WarpedMotionParams *ref_params =
187 cm->prev_frame ? &cm->prev_frame->global_motion[frame]
188 : &default_warp_params;
189
190 compute_global_motion_for_ref_frame(cpi, error_info, ref_buf, frame,
191 motion_models, segment_map, segment_map_w,
192 segment_map_h, ref_params);
193 }
194
195 // Loops over valid reference frames and computes global motion estimation.
compute_global_motion_for_references(AV1_COMP * cpi,YV12_BUFFER_CONFIG * ref_buf[REF_FRAMES],FrameDistPair reference_frame[REF_FRAMES-1],int num_ref_frames,MotionModel * motion_models,uint8_t * segment_map,const int segment_map_w,const int segment_map_h)196 static inline void compute_global_motion_for_references(
197 AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES],
198 FrameDistPair reference_frame[REF_FRAMES - 1], int num_ref_frames,
199 MotionModel *motion_models, uint8_t *segment_map, const int segment_map_w,
200 const int segment_map_h) {
201 AV1_COMMON *const cm = &cpi->common;
202 struct aom_internal_error_info *const error_info =
203 cpi->td.mb.e_mbd.error_info;
204 // Compute global motion w.r.t. reference frames starting from the nearest ref
205 // frame in a given direction.
206 for (int frame = 0; frame < num_ref_frames; frame++) {
207 int ref_frame = reference_frame[frame].frame;
208 av1_compute_gm_for_valid_ref_frames(cpi, error_info, ref_buf, ref_frame,
209 motion_models, segment_map,
210 segment_map_w, segment_map_h);
211 // If global motion w.r.t. current ref frame is
212 // INVALID/TRANSLATION/IDENTITY, skip the evaluation of global motion w.r.t
213 // the remaining ref frames in that direction.
214 if (cpi->sf.gm_sf.prune_ref_frame_for_gm_search &&
215 cm->global_motion[ref_frame].wmtype <= TRANSLATION)
216 break;
217 }
218 }
219
220 // Compares the distance in 'a' and 'b'. Returns 1 if the frame corresponding to
221 // 'a' is farther, -1 if the frame corresponding to 'b' is farther, 0 otherwise.
compare_distance(const void * a,const void * b)222 static int compare_distance(const void *a, const void *b) {
223 const int diff =
224 ((FrameDistPair *)a)->distance - ((FrameDistPair *)b)->distance;
225 if (diff > 0)
226 return 1;
227 else if (diff < 0)
228 return -1;
229 return 0;
230 }
231
disable_gm_search_based_on_stats(const AV1_COMP * const cpi)232 static int disable_gm_search_based_on_stats(const AV1_COMP *const cpi) {
233 int is_gm_present = 1;
234
235 // Check number of GM models only in GF groups with ARF frames. GM param
236 // estimation is always done in the case of GF groups with no ARF frames (flat
237 // gops)
238 if (cpi->ppi->gf_group.arf_index > -1) {
239 // valid_gm_model_found is initialized to INT32_MAX in the beginning of
240 // every GF group.
241 // Therefore, GM param estimation is always done for all frames until
242 // at least 1 frame each of ARF_UPDATE, INTNL_ARF_UPDATE and LF_UPDATE are
243 // encoded in a GF group For subsequent frames, GM param estimation is
244 // disabled, if no valid models have been found in all the three update
245 // types.
246 is_gm_present = (cpi->ppi->valid_gm_model_found[ARF_UPDATE] != 0) ||
247 (cpi->ppi->valid_gm_model_found[INTNL_ARF_UPDATE] != 0) ||
248 (cpi->ppi->valid_gm_model_found[LF_UPDATE] != 0);
249 }
250 return !is_gm_present;
251 }
252
253 // Prunes reference frames for global motion estimation based on the speed
254 // feature 'gm_search_type'.
do_gm_search_logic(SPEED_FEATURES * const sf,int frame)255 static int do_gm_search_logic(SPEED_FEATURES *const sf, int frame) {
256 (void)frame;
257 switch (sf->gm_sf.gm_search_type) {
258 case GM_FULL_SEARCH: return 1;
259 case GM_REDUCED_REF_SEARCH_SKIP_L2_L3:
260 return !(frame == LAST2_FRAME || frame == LAST3_FRAME);
261 case GM_REDUCED_REF_SEARCH_SKIP_L2_L3_ARF2:
262 return !(frame == LAST2_FRAME || frame == LAST3_FRAME ||
263 (frame == ALTREF2_FRAME));
264 case GM_SEARCH_CLOSEST_REFS_ONLY: return 1;
265 case GM_DISABLE_SEARCH: return 0;
266 default: assert(0);
267 }
268 return 1;
269 }
270
271 // Populates valid reference frames in past/future directions in
272 // 'reference_frames' and their count in 'num_ref_frames'.
update_valid_ref_frames_for_gm(AV1_COMP * cpi,YV12_BUFFER_CONFIG * ref_buf[REF_FRAMES],FrameDistPair reference_frames[MAX_DIRECTIONS][REF_FRAMES-1],int * num_ref_frames)273 static inline void update_valid_ref_frames_for_gm(
274 AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES],
275 FrameDistPair reference_frames[MAX_DIRECTIONS][REF_FRAMES - 1],
276 int *num_ref_frames) {
277 AV1_COMMON *const cm = &cpi->common;
278 int *num_past_ref_frames = &num_ref_frames[0];
279 int *num_future_ref_frames = &num_ref_frames[1];
280 const GF_GROUP *gf_group = &cpi->ppi->gf_group;
281 int ref_pruning_enabled = is_frame_eligible_for_ref_pruning(
282 gf_group, cpi->sf.inter_sf.selective_ref_frame, 1, cpi->gf_frame_index);
283 int cur_frame_gm_disabled = 0;
284 int pyr_lvl = cm->cur_frame->pyramid_level;
285
286 if (cpi->sf.gm_sf.disable_gm_search_based_on_stats) {
287 cur_frame_gm_disabled = disable_gm_search_based_on_stats(cpi);
288 }
289
290 for (int frame = ALTREF_FRAME; frame >= LAST_FRAME; --frame) {
291 const MV_REFERENCE_FRAME ref_frame[2] = { frame, NONE_FRAME };
292 RefCntBuffer *buf = get_ref_frame_buf(cm, frame);
293 const int ref_disabled =
294 !(cpi->ref_frame_flags & av1_ref_frame_flag_list[frame]);
295 ref_buf[frame] = NULL;
296 cm->global_motion[frame] = default_warp_params;
297 // Skip global motion estimation for invalid ref frames
298 if (buf == NULL ||
299 (ref_disabled && cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE)) {
300 continue;
301 } else {
302 ref_buf[frame] = &buf->buf;
303 }
304
305 int prune_ref_frames =
306 ref_pruning_enabled &&
307 prune_ref_by_selective_ref_frame(cpi, NULL, ref_frame,
308 cm->cur_frame->ref_display_order_hint);
309 int ref_pyr_lvl = buf->pyramid_level;
310
311 if (ref_buf[frame]->y_crop_width == cpi->source->y_crop_width &&
312 ref_buf[frame]->y_crop_height == cpi->source->y_crop_height &&
313 do_gm_search_logic(&cpi->sf, frame) && !prune_ref_frames &&
314 ref_pyr_lvl <= pyr_lvl && !cur_frame_gm_disabled) {
315 assert(ref_buf[frame] != NULL);
316 const int relative_frame_dist = av1_encoder_get_relative_dist(
317 buf->display_order_hint, cm->cur_frame->display_order_hint);
318 // Populate past and future ref frames.
319 // reference_frames[0][] indicates past direction and
320 // reference_frames[1][] indicates future direction.
321 if (relative_frame_dist == 0) {
322 // Skip global motion estimation for frames at the same nominal instant.
323 // This will generally be either a "real" frame coded against a
324 // temporal filtered version, or a higher spatial layer coded against
325 // a lower spatial layer. In either case, the optimal motion model will
326 // be IDENTITY, so we don't need to search explicitly.
327 } else if (relative_frame_dist < 0) {
328 reference_frames[0][*num_past_ref_frames].distance =
329 abs(relative_frame_dist);
330 reference_frames[0][*num_past_ref_frames].frame = frame;
331 (*num_past_ref_frames)++;
332 } else {
333 reference_frames[1][*num_future_ref_frames].distance =
334 abs(relative_frame_dist);
335 reference_frames[1][*num_future_ref_frames].frame = frame;
336 (*num_future_ref_frames)++;
337 }
338 }
339 }
340 }
341
342 // Initializes parameters used for computing global motion.
setup_global_motion_info_params(AV1_COMP * cpi)343 static inline void setup_global_motion_info_params(AV1_COMP *cpi) {
344 GlobalMotionInfo *const gm_info = &cpi->gm_info;
345 YV12_BUFFER_CONFIG *source = cpi->source;
346
347 gm_info->segment_map_w =
348 (source->y_crop_width + WARP_ERROR_BLOCK - 1) >> WARP_ERROR_BLOCK_LOG;
349 gm_info->segment_map_h =
350 (source->y_crop_height + WARP_ERROR_BLOCK - 1) >> WARP_ERROR_BLOCK_LOG;
351
352 memset(gm_info->reference_frames, -1,
353 sizeof(gm_info->reference_frames[0][0]) * MAX_DIRECTIONS *
354 (REF_FRAMES - 1));
355 av1_zero(gm_info->num_ref_frames);
356
357 // Populate ref_buf for valid ref frames in global motion
358 update_valid_ref_frames_for_gm(cpi, gm_info->ref_buf,
359 gm_info->reference_frames,
360 gm_info->num_ref_frames);
361
362 // Sort the past and future ref frames in the ascending order of their
363 // distance from the current frame. reference_frames[0] => past direction
364 // and reference_frames[1] => future direction.
365 qsort(gm_info->reference_frames[0], gm_info->num_ref_frames[0],
366 sizeof(gm_info->reference_frames[0][0]), compare_distance);
367 qsort(gm_info->reference_frames[1], gm_info->num_ref_frames[1],
368 sizeof(gm_info->reference_frames[1][0]), compare_distance);
369
370 if (cpi->sf.gm_sf.gm_search_type == GM_SEARCH_CLOSEST_REFS_ONLY) {
371 // Filter down to the nearest two ref frames.
372 // Prefer one past and one future ref over two past refs, even if
373 // the second past ref is closer
374 if (gm_info->num_ref_frames[1] > 0) {
375 gm_info->num_ref_frames[0] = AOMMIN(gm_info->num_ref_frames[0], 1);
376 gm_info->num_ref_frames[1] = AOMMIN(gm_info->num_ref_frames[1], 1);
377 } else {
378 gm_info->num_ref_frames[0] = AOMMIN(gm_info->num_ref_frames[0], 2);
379 }
380 }
381 }
382
383 // Computes global motion w.r.t. valid reference frames.
global_motion_estimation(AV1_COMP * cpi)384 static inline void global_motion_estimation(AV1_COMP *cpi) {
385 GlobalMotionInfo *const gm_info = &cpi->gm_info;
386 GlobalMotionData *gm_data = &cpi->td.gm_data;
387
388 // Compute global motion w.r.t. past reference frames and future reference
389 // frames
390 for (int dir = 0; dir < MAX_DIRECTIONS; dir++) {
391 if (gm_info->num_ref_frames[dir] > 0)
392 compute_global_motion_for_references(
393 cpi, gm_info->ref_buf, gm_info->reference_frames[dir],
394 gm_info->num_ref_frames[dir], gm_data->motion_models,
395 gm_data->segment_map, gm_info->segment_map_w, gm_info->segment_map_h);
396 }
397 }
398
399 // Global motion estimation for the current frame is computed.This computation
400 // happens once per frame and the winner motion model parameters are stored in
401 // cm->cur_frame->global_motion.
av1_compute_global_motion_facade(AV1_COMP * cpi)402 void av1_compute_global_motion_facade(AV1_COMP *cpi) {
403 AV1_COMMON *const cm = &cpi->common;
404 GlobalMotionInfo *const gm_info = &cpi->gm_info;
405
406 if (cpi->oxcf.tool_cfg.enable_global_motion) {
407 if (cpi->gf_frame_index == 0) {
408 for (int i = 0; i < FRAME_UPDATE_TYPES; i++) {
409 cpi->ppi->valid_gm_model_found[i] = INT32_MAX;
410 #if CONFIG_FPMT_TEST
411 if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE)
412 cpi->ppi->temp_valid_gm_model_found[i] = INT32_MAX;
413 #endif
414 }
415 }
416 }
417
418 if (cpi->common.current_frame.frame_type == INTER_FRAME && cpi->source &&
419 cpi->oxcf.tool_cfg.enable_global_motion && !gm_info->search_done &&
420 cpi->sf.gm_sf.gm_search_type != GM_DISABLE_SEARCH) {
421 setup_global_motion_info_params(cpi);
422 // Terminate early if the total number of reference frames is zero.
423 if (cpi->gm_info.num_ref_frames[0] || cpi->gm_info.num_ref_frames[1]) {
424 gm_alloc_data(cpi, &cpi->td.gm_data);
425 if (cpi->mt_info.num_workers > 1)
426 av1_global_motion_estimation_mt(cpi);
427 else
428 global_motion_estimation(cpi);
429 gm_dealloc_data(&cpi->td.gm_data);
430 gm_info->search_done = 1;
431 }
432 }
433 memcpy(cm->cur_frame->global_motion, cm->global_motion,
434 sizeof(cm->cur_frame->global_motion));
435 }
436