1 /*
2 * Copyright (c) 2019, 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 "av1/encoder/tune_vmaf.h"
13
14 #include "aom_dsp/psnr.h"
15 #include "av1/encoder/extend.h"
16 #include "av1/encoder/rdopt.h"
17 #include "config/aom_scale_rtcd.h"
18
19 static const double kBaselineVmaf = 97.42773;
20
get_layer_value(const double * array,int layer)21 static double get_layer_value(const double *array, int layer) {
22 while (array[layer] < 0.0 && layer > 0) layer--;
23 return AOMMAX(array[layer], 0.0);
24 }
25
motion_search(AV1_COMP * cpi,const YV12_BUFFER_CONFIG * src,const YV12_BUFFER_CONFIG * ref,const BLOCK_SIZE block_size,const int mb_row,const int mb_col,FULLPEL_MV * ref_mv)26 static void motion_search(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *src,
27 const YV12_BUFFER_CONFIG *ref,
28 const BLOCK_SIZE block_size, const int mb_row,
29 const int mb_col, FULLPEL_MV *ref_mv) {
30 // Block information (ONLY Y-plane is used for motion search).
31 const int mb_height = block_size_high[block_size];
32 const int mb_width = block_size_wide[block_size];
33 const int y_stride = src->y_stride;
34 assert(y_stride == ref->y_stride);
35 const int y_offset = mb_row * mb_height * y_stride + mb_col * mb_width;
36
37 // Save input state.
38 MACROBLOCK *const mb = &cpi->td.mb;
39 MACROBLOCKD *const mbd = &mb->e_mbd;
40 const struct buf_2d ori_src_buf = mb->plane[0].src;
41 const struct buf_2d ori_pre_buf = mbd->plane[0].pre[0];
42
43 // Parameters used for motion search.
44 FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
45 const SEARCH_METHODS search_method = NSTEP;
46 const search_site_config *search_site_cfg =
47 cpi->mv_search_params.search_site_cfg[SS_CFG_FPF];
48 const int step_param =
49 av1_init_search_range(AOMMAX(src->y_crop_width, src->y_crop_height));
50
51 // Baseline position for motion search (used for rate distortion comparison).
52 const MV baseline_mv = kZeroMv;
53
54 // Setup.
55 mb->plane[0].src.buf = src->y_buffer + y_offset;
56 mb->plane[0].src.stride = y_stride;
57 mbd->plane[0].pre[0].buf = ref->y_buffer + y_offset;
58 mbd->plane[0].pre[0].stride = y_stride;
59
60 // Unused intermediate results for motion search.
61 int cost_list[5];
62
63 // Do motion search.
64 // Only do full search on the entire block.
65 av1_make_default_fullpel_ms_params(&full_ms_params, cpi, mb, block_size,
66 &baseline_mv, search_site_cfg,
67 /*fine_search_interval=*/0);
68 av1_set_mv_search_method(&full_ms_params, search_site_cfg, search_method);
69 av1_full_pixel_search(*ref_mv, &full_ms_params, step_param,
70 cond_cost_list(cpi, cost_list), ref_mv, NULL);
71
72 // Restore input state.
73 mb->plane[0].src = ori_src_buf;
74 mbd->plane[0].pre[0] = ori_pre_buf;
75 }
76
residual_variance(const AV1_COMP * cpi,const YV12_BUFFER_CONFIG * src,const YV12_BUFFER_CONFIG * ref,const BLOCK_SIZE block_size,const int mb_row,const int mb_col,FULLPEL_MV ref_mv,unsigned int * sse)77 static unsigned int residual_variance(const AV1_COMP *cpi,
78 const YV12_BUFFER_CONFIG *src,
79 const YV12_BUFFER_CONFIG *ref,
80 const BLOCK_SIZE block_size,
81 const int mb_row, const int mb_col,
82 FULLPEL_MV ref_mv, unsigned int *sse) {
83 const int mb_height = block_size_high[block_size];
84 const int mb_width = block_size_wide[block_size];
85 const int y_stride = src->y_stride;
86 assert(y_stride == ref->y_stride);
87 const int y_offset = mb_row * mb_height * y_stride + mb_col * mb_width;
88 const int mv_offset = ref_mv.row * y_stride + ref_mv.col;
89 const unsigned int var = cpi->ppi->fn_ptr[block_size].vf(
90 ref->y_buffer + y_offset + mv_offset, y_stride, src->y_buffer + y_offset,
91 y_stride, sse);
92 return var;
93 }
94
frame_average_variance(const AV1_COMP * const cpi,const YV12_BUFFER_CONFIG * const frame)95 static double frame_average_variance(const AV1_COMP *const cpi,
96 const YV12_BUFFER_CONFIG *const frame) {
97 const uint8_t *const y_buffer = frame->y_buffer;
98 const int y_stride = frame->y_stride;
99 const BLOCK_SIZE block_size = BLOCK_64X64;
100
101 const int block_w = mi_size_wide[block_size] * 4;
102 const int block_h = mi_size_high[block_size] * 4;
103 int row, col;
104 const int bit_depth = cpi->td.mb.e_mbd.bd;
105 double var = 0.0, var_count = 0.0;
106
107 // Loop through each block.
108 for (row = 0; row < frame->y_height / block_h; ++row) {
109 for (col = 0; col < frame->y_width / block_w; ++col) {
110 struct buf_2d buf;
111 const int row_offset_y = row * block_h;
112 const int col_offset_y = col * block_w;
113
114 buf.buf = (uint8_t *)y_buffer + row_offset_y * y_stride + col_offset_y;
115 buf.stride = y_stride;
116
117 if (cpi->common.seq_params->use_highbitdepth) {
118 assert(frame->flags & YV12_FLAG_HIGHBITDEPTH);
119 var += av1_high_get_sby_perpixel_variance(cpi, &buf, block_size,
120 bit_depth);
121 } else {
122 var += av1_get_sby_perpixel_variance(cpi, &buf, block_size);
123 }
124 var_count += 1.0;
125 }
126 }
127 var /= var_count;
128 return var;
129 }
130
residual_frame_average_variance(AV1_COMP * cpi,const YV12_BUFFER_CONFIG * src,const YV12_BUFFER_CONFIG * ref,FULLPEL_MV * mvs)131 static double residual_frame_average_variance(AV1_COMP *cpi,
132 const YV12_BUFFER_CONFIG *src,
133 const YV12_BUFFER_CONFIG *ref,
134 FULLPEL_MV *mvs) {
135 if (ref == NULL) return frame_average_variance(cpi, src);
136 const BLOCK_SIZE block_size = BLOCK_16X16;
137 const int frame_height = src->y_height;
138 const int frame_width = src->y_width;
139 const int mb_height = block_size_high[block_size];
140 const int mb_width = block_size_wide[block_size];
141 const int mb_rows = (frame_height + mb_height - 1) / mb_height;
142 const int mb_cols = (frame_width + mb_width - 1) / mb_width;
143 const int num_planes = av1_num_planes(&cpi->common);
144 const int mi_h = mi_size_high_log2[block_size];
145 const int mi_w = mi_size_wide_log2[block_size];
146 assert(num_planes >= 1 && num_planes <= MAX_MB_PLANE);
147
148 // Save input state.
149 MACROBLOCK *const mb = &cpi->td.mb;
150 MACROBLOCKD *const mbd = &mb->e_mbd;
151 uint8_t *input_buffer[MAX_MB_PLANE];
152 for (int i = 0; i < num_planes; i++) {
153 input_buffer[i] = mbd->plane[i].pre[0].buf;
154 }
155 MB_MODE_INFO **input_mb_mode_info = mbd->mi;
156
157 bool do_motion_search = false;
158 if (mvs == NULL) {
159 do_motion_search = true;
160 mvs = (FULLPEL_MV *)aom_malloc(sizeof(*mvs) * mb_rows * mb_cols);
161 memset(mvs, 0, sizeof(*mvs) * mb_rows * mb_cols);
162 }
163
164 unsigned int variance = 0;
165 // Perform temporal filtering block by block.
166 for (int mb_row = 0; mb_row < mb_rows; mb_row++) {
167 av1_set_mv_row_limits(&cpi->common.mi_params, &mb->mv_limits,
168 (mb_row << mi_h), (mb_height >> MI_SIZE_LOG2),
169 cpi->oxcf.border_in_pixels);
170 for (int mb_col = 0; mb_col < mb_cols; mb_col++) {
171 av1_set_mv_col_limits(&cpi->common.mi_params, &mb->mv_limits,
172 (mb_col << mi_w), (mb_width >> MI_SIZE_LOG2),
173 cpi->oxcf.border_in_pixels);
174 FULLPEL_MV *ref_mv = &mvs[mb_col + mb_row * mb_cols];
175 if (do_motion_search) {
176 motion_search(cpi, src, ref, block_size, mb_row, mb_col, ref_mv);
177 }
178 unsigned int mv_sse;
179 const unsigned int blk_var = residual_variance(
180 cpi, src, ref, block_size, mb_row, mb_col, *ref_mv, &mv_sse);
181 variance += blk_var;
182 }
183 }
184
185 // Restore input state
186 for (int i = 0; i < num_planes; i++) {
187 mbd->plane[i].pre[0].buf = input_buffer[i];
188 }
189 mbd->mi = input_mb_mode_info;
190 return (double)variance / (double)(mb_rows * mb_cols);
191 }
192
193 // TODO(sdeng): Add the SIMD implementation.
highbd_unsharp_rect(const uint16_t * source,int source_stride,const uint16_t * blurred,int blurred_stride,uint16_t * dst,int dst_stride,int w,int h,double amount,int bit_depth)194 static AOM_INLINE void highbd_unsharp_rect(const uint16_t *source,
195 int source_stride,
196 const uint16_t *blurred,
197 int blurred_stride, uint16_t *dst,
198 int dst_stride, int w, int h,
199 double amount, int bit_depth) {
200 const int max_value = (1 << bit_depth) - 1;
201 for (int i = 0; i < h; ++i) {
202 for (int j = 0; j < w; ++j) {
203 const double val =
204 (double)source[j] + amount * ((double)source[j] - (double)blurred[j]);
205 dst[j] = (uint16_t)clamp((int)(val + 0.5), 0, max_value);
206 }
207 source += source_stride;
208 blurred += blurred_stride;
209 dst += dst_stride;
210 }
211 }
212
unsharp_rect(const uint8_t * source,int source_stride,const uint8_t * blurred,int blurred_stride,uint8_t * dst,int dst_stride,int w,int h,double amount)213 static AOM_INLINE void unsharp_rect(const uint8_t *source, int source_stride,
214 const uint8_t *blurred, int blurred_stride,
215 uint8_t *dst, int dst_stride, int w, int h,
216 double amount) {
217 for (int i = 0; i < h; ++i) {
218 for (int j = 0; j < w; ++j) {
219 const double val =
220 (double)source[j] + amount * ((double)source[j] - (double)blurred[j]);
221 dst[j] = (uint8_t)clamp((int)(val + 0.5), 0, 255);
222 }
223 source += source_stride;
224 blurred += blurred_stride;
225 dst += dst_stride;
226 }
227 }
228
unsharp(const AV1_COMP * const cpi,const YV12_BUFFER_CONFIG * source,const YV12_BUFFER_CONFIG * blurred,const YV12_BUFFER_CONFIG * dst,double amount)229 static AOM_INLINE void unsharp(const AV1_COMP *const cpi,
230 const YV12_BUFFER_CONFIG *source,
231 const YV12_BUFFER_CONFIG *blurred,
232 const YV12_BUFFER_CONFIG *dst, double amount) {
233 const int bit_depth = cpi->td.mb.e_mbd.bd;
234 if (cpi->common.seq_params->use_highbitdepth) {
235 assert(source->flags & YV12_FLAG_HIGHBITDEPTH);
236 assert(blurred->flags & YV12_FLAG_HIGHBITDEPTH);
237 assert(dst->flags & YV12_FLAG_HIGHBITDEPTH);
238 highbd_unsharp_rect(CONVERT_TO_SHORTPTR(source->y_buffer), source->y_stride,
239 CONVERT_TO_SHORTPTR(blurred->y_buffer),
240 blurred->y_stride, CONVERT_TO_SHORTPTR(dst->y_buffer),
241 dst->y_stride, source->y_width, source->y_height,
242 amount, bit_depth);
243 } else {
244 unsharp_rect(source->y_buffer, source->y_stride, blurred->y_buffer,
245 blurred->y_stride, dst->y_buffer, dst->y_stride,
246 source->y_width, source->y_height, amount);
247 }
248 }
249
250 // 8-tap Gaussian convolution filter with sigma = 1.0, sums to 128,
251 // all co-efficients must be even.
252 DECLARE_ALIGNED(16, static const int16_t, gauss_filter[8]) = { 0, 8, 30, 52,
253 30, 8, 0, 0 };
gaussian_blur(const int bit_depth,const YV12_BUFFER_CONFIG * source,const YV12_BUFFER_CONFIG * dst)254 static AOM_INLINE void gaussian_blur(const int bit_depth,
255 const YV12_BUFFER_CONFIG *source,
256 const YV12_BUFFER_CONFIG *dst) {
257 const int block_size = BLOCK_128X128;
258 const int block_w = mi_size_wide[block_size] * 4;
259 const int block_h = mi_size_high[block_size] * 4;
260 const int num_cols = (source->y_width + block_w - 1) / block_w;
261 const int num_rows = (source->y_height + block_h - 1) / block_h;
262 int row, col;
263
264 ConvolveParams conv_params = get_conv_params(0, 0, bit_depth);
265 InterpFilterParams filter = { .filter_ptr = gauss_filter,
266 .taps = 8,
267 .interp_filter = EIGHTTAP_REGULAR };
268
269 for (row = 0; row < num_rows; ++row) {
270 for (col = 0; col < num_cols; ++col) {
271 const int row_offset_y = row * block_h;
272 const int col_offset_y = col * block_w;
273
274 uint8_t *src_buf =
275 source->y_buffer + row_offset_y * source->y_stride + col_offset_y;
276 uint8_t *dst_buf =
277 dst->y_buffer + row_offset_y * dst->y_stride + col_offset_y;
278
279 if (source->flags & YV12_FLAG_HIGHBITDEPTH) {
280 av1_highbd_convolve_2d_sr(
281 CONVERT_TO_SHORTPTR(src_buf), source->y_stride,
282 CONVERT_TO_SHORTPTR(dst_buf), dst->y_stride, block_w, block_h,
283 &filter, &filter, 0, 0, &conv_params, bit_depth);
284 } else {
285 av1_convolve_2d_sr(src_buf, source->y_stride, dst_buf, dst->y_stride,
286 block_w, block_h, &filter, &filter, 0, 0,
287 &conv_params);
288 }
289 }
290 }
291 }
292
cal_approx_vmaf(const AV1_COMP * const cpi,double source_variance,YV12_BUFFER_CONFIG * const source,YV12_BUFFER_CONFIG * const sharpened)293 static AOM_INLINE double cal_approx_vmaf(const AV1_COMP *const cpi,
294 double source_variance,
295 YV12_BUFFER_CONFIG *const source,
296 YV12_BUFFER_CONFIG *const sharpened) {
297 const int bit_depth = cpi->td.mb.e_mbd.bd;
298 const bool cal_vmaf_neg =
299 cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN;
300 double new_vmaf;
301
302 aom_calc_vmaf(cpi->vmaf_info.vmaf_model, source, sharpened, bit_depth,
303 cal_vmaf_neg, &new_vmaf);
304
305 const double sharpened_var = frame_average_variance(cpi, sharpened);
306 return source_variance / sharpened_var * (new_vmaf - kBaselineVmaf);
307 }
308
find_best_frame_unsharp_amount_loop(const AV1_COMP * const cpi,YV12_BUFFER_CONFIG * const source,YV12_BUFFER_CONFIG * const blurred,YV12_BUFFER_CONFIG * const sharpened,double best_vmaf,const double baseline_variance,const double unsharp_amount_start,const double step_size,const int max_loop_count,const double max_amount)309 static double find_best_frame_unsharp_amount_loop(
310 const AV1_COMP *const cpi, YV12_BUFFER_CONFIG *const source,
311 YV12_BUFFER_CONFIG *const blurred, YV12_BUFFER_CONFIG *const sharpened,
312 double best_vmaf, const double baseline_variance,
313 const double unsharp_amount_start, const double step_size,
314 const int max_loop_count, const double max_amount) {
315 const double min_amount = 0.0;
316 int loop_count = 0;
317 double approx_vmaf = best_vmaf;
318 double unsharp_amount = unsharp_amount_start;
319 do {
320 best_vmaf = approx_vmaf;
321 unsharp_amount += step_size;
322 if (unsharp_amount > max_amount || unsharp_amount < min_amount) break;
323 unsharp(cpi, source, blurred, sharpened, unsharp_amount);
324 approx_vmaf = cal_approx_vmaf(cpi, baseline_variance, source, sharpened);
325
326 loop_count++;
327 } while (approx_vmaf > best_vmaf && loop_count < max_loop_count);
328 unsharp_amount =
329 approx_vmaf > best_vmaf ? unsharp_amount : unsharp_amount - step_size;
330 return AOMMIN(max_amount, AOMMAX(unsharp_amount, min_amount));
331 }
332
find_best_frame_unsharp_amount(const AV1_COMP * const cpi,YV12_BUFFER_CONFIG * const source,YV12_BUFFER_CONFIG * const blurred,const double unsharp_amount_start,const double step_size,const int max_loop_count,const double max_filter_amount)333 static double find_best_frame_unsharp_amount(const AV1_COMP *const cpi,
334 YV12_BUFFER_CONFIG *const source,
335 YV12_BUFFER_CONFIG *const blurred,
336 const double unsharp_amount_start,
337 const double step_size,
338 const int max_loop_count,
339 const double max_filter_amount) {
340 const AV1_COMMON *const cm = &cpi->common;
341 const int width = source->y_width;
342 const int height = source->y_height;
343 YV12_BUFFER_CONFIG sharpened;
344 memset(&sharpened, 0, sizeof(sharpened));
345 aom_alloc_frame_buffer(
346 &sharpened, width, height, source->subsampling_x, source->subsampling_y,
347 cm->seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
348 cm->features.byte_alignment);
349
350 const double baseline_variance = frame_average_variance(cpi, source);
351 double unsharp_amount;
352 if (unsharp_amount_start <= step_size) {
353 unsharp_amount = find_best_frame_unsharp_amount_loop(
354 cpi, source, blurred, &sharpened, 0.0, baseline_variance, 0.0,
355 step_size, max_loop_count, max_filter_amount);
356 } else {
357 double a0 = unsharp_amount_start - step_size, a1 = unsharp_amount_start;
358 double v0, v1;
359 unsharp(cpi, source, blurred, &sharpened, a0);
360 v0 = cal_approx_vmaf(cpi, baseline_variance, source, &sharpened);
361 unsharp(cpi, source, blurred, &sharpened, a1);
362 v1 = cal_approx_vmaf(cpi, baseline_variance, source, &sharpened);
363 if (fabs(v0 - v1) < 0.01) {
364 unsharp_amount = a0;
365 } else if (v0 > v1) {
366 unsharp_amount = find_best_frame_unsharp_amount_loop(
367 cpi, source, blurred, &sharpened, v0, baseline_variance, a0,
368 -step_size, max_loop_count, max_filter_amount);
369 } else {
370 unsharp_amount = find_best_frame_unsharp_amount_loop(
371 cpi, source, blurred, &sharpened, v1, baseline_variance, a1,
372 step_size, max_loop_count, max_filter_amount);
373 }
374 }
375
376 aom_free_frame_buffer(&sharpened);
377 return unsharp_amount;
378 }
379
av1_vmaf_neg_preprocessing(AV1_COMP * const cpi,YV12_BUFFER_CONFIG * const source)380 void av1_vmaf_neg_preprocessing(AV1_COMP *const cpi,
381 YV12_BUFFER_CONFIG *const source) {
382 const AV1_COMMON *const cm = &cpi->common;
383 const int bit_depth = cpi->td.mb.e_mbd.bd;
384 const int width = source->y_width;
385 const int height = source->y_height;
386
387 const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
388 const int layer_depth =
389 AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], MAX_ARF_LAYERS - 1);
390 const double best_frame_unsharp_amount =
391 get_layer_value(cpi->vmaf_info.last_frame_unsharp_amount, layer_depth);
392
393 if (best_frame_unsharp_amount <= 0.0) return;
394
395 YV12_BUFFER_CONFIG blurred;
396 memset(&blurred, 0, sizeof(blurred));
397 aom_alloc_frame_buffer(
398 &blurred, width, height, source->subsampling_x, source->subsampling_y,
399 cm->seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
400 cm->features.byte_alignment);
401
402 gaussian_blur(bit_depth, source, &blurred);
403 unsharp(cpi, source, &blurred, source, best_frame_unsharp_amount);
404 aom_free_frame_buffer(&blurred);
405 }
406
av1_vmaf_frame_preprocessing(AV1_COMP * const cpi,YV12_BUFFER_CONFIG * const source)407 void av1_vmaf_frame_preprocessing(AV1_COMP *const cpi,
408 YV12_BUFFER_CONFIG *const source) {
409 const AV1_COMMON *const cm = &cpi->common;
410 const int bit_depth = cpi->td.mb.e_mbd.bd;
411 const int width = source->y_width;
412 const int height = source->y_height;
413
414 YV12_BUFFER_CONFIG source_extended, blurred;
415 memset(&source_extended, 0, sizeof(source_extended));
416 memset(&blurred, 0, sizeof(blurred));
417 aom_alloc_frame_buffer(
418 &source_extended, width, height, source->subsampling_x,
419 source->subsampling_y, cm->seq_params->use_highbitdepth,
420 cpi->oxcf.border_in_pixels, cm->features.byte_alignment);
421 aom_alloc_frame_buffer(
422 &blurred, width, height, source->subsampling_x, source->subsampling_y,
423 cm->seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
424 cm->features.byte_alignment);
425
426 av1_copy_and_extend_frame(source, &source_extended);
427 gaussian_blur(bit_depth, &source_extended, &blurred);
428 aom_free_frame_buffer(&source_extended);
429
430 const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
431 const int layer_depth =
432 AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], MAX_ARF_LAYERS - 1);
433 const double last_frame_unsharp_amount =
434 get_layer_value(cpi->vmaf_info.last_frame_unsharp_amount, layer_depth);
435
436 const double best_frame_unsharp_amount = find_best_frame_unsharp_amount(
437 cpi, source, &blurred, last_frame_unsharp_amount, 0.05, 20, 1.01);
438
439 cpi->vmaf_info.last_frame_unsharp_amount[layer_depth] =
440 best_frame_unsharp_amount;
441
442 unsharp(cpi, source, &blurred, source, best_frame_unsharp_amount);
443 aom_free_frame_buffer(&blurred);
444 }
445
av1_vmaf_blk_preprocessing(AV1_COMP * const cpi,YV12_BUFFER_CONFIG * const source)446 void av1_vmaf_blk_preprocessing(AV1_COMP *const cpi,
447 YV12_BUFFER_CONFIG *const source) {
448 const AV1_COMMON *const cm = &cpi->common;
449 const int width = source->y_width;
450 const int height = source->y_height;
451 const int bit_depth = cpi->td.mb.e_mbd.bd;
452 const int ss_x = source->subsampling_x;
453 const int ss_y = source->subsampling_y;
454
455 YV12_BUFFER_CONFIG source_extended, blurred;
456 memset(&blurred, 0, sizeof(blurred));
457 memset(&source_extended, 0, sizeof(source_extended));
458 aom_alloc_frame_buffer(
459 &blurred, width, height, ss_x, ss_y, cm->seq_params->use_highbitdepth,
460 cpi->oxcf.border_in_pixels, cm->features.byte_alignment);
461 aom_alloc_frame_buffer(&source_extended, width, height, ss_x, ss_y,
462 cm->seq_params->use_highbitdepth,
463 cpi->oxcf.border_in_pixels,
464 cm->features.byte_alignment);
465
466 av1_copy_and_extend_frame(source, &source_extended);
467 gaussian_blur(bit_depth, &source_extended, &blurred);
468 aom_free_frame_buffer(&source_extended);
469
470 const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
471 const int layer_depth =
472 AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], MAX_ARF_LAYERS - 1);
473 const double last_frame_unsharp_amount =
474 get_layer_value(cpi->vmaf_info.last_frame_unsharp_amount, layer_depth);
475
476 const double best_frame_unsharp_amount = find_best_frame_unsharp_amount(
477 cpi, source, &blurred, last_frame_unsharp_amount, 0.05, 20, 1.01);
478
479 cpi->vmaf_info.last_frame_unsharp_amount[layer_depth] =
480 best_frame_unsharp_amount;
481
482 const int block_size = BLOCK_64X64;
483 const int block_w = mi_size_wide[block_size] * 4;
484 const int block_h = mi_size_high[block_size] * 4;
485 const int num_cols = (source->y_width + block_w - 1) / block_w;
486 const int num_rows = (source->y_height + block_h - 1) / block_h;
487 double *best_unsharp_amounts =
488 aom_malloc(sizeof(*best_unsharp_amounts) * num_cols * num_rows);
489 memset(best_unsharp_amounts, 0,
490 sizeof(*best_unsharp_amounts) * num_cols * num_rows);
491
492 YV12_BUFFER_CONFIG source_block, blurred_block;
493 memset(&source_block, 0, sizeof(source_block));
494 memset(&blurred_block, 0, sizeof(blurred_block));
495 aom_alloc_frame_buffer(&source_block, block_w, block_h, ss_x, ss_y,
496 cm->seq_params->use_highbitdepth,
497 cpi->oxcf.border_in_pixels,
498 cm->features.byte_alignment);
499 aom_alloc_frame_buffer(&blurred_block, block_w, block_h, ss_x, ss_y,
500 cm->seq_params->use_highbitdepth,
501 cpi->oxcf.border_in_pixels,
502 cm->features.byte_alignment);
503
504 for (int row = 0; row < num_rows; ++row) {
505 for (int col = 0; col < num_cols; ++col) {
506 const int row_offset_y = row * block_h;
507 const int col_offset_y = col * block_w;
508 const int block_width = AOMMIN(width - col_offset_y, block_w);
509 const int block_height = AOMMIN(height - row_offset_y, block_h);
510 const int index = col + row * num_cols;
511
512 if (cm->seq_params->use_highbitdepth) {
513 assert(source->flags & YV12_FLAG_HIGHBITDEPTH);
514 assert(blurred.flags & YV12_FLAG_HIGHBITDEPTH);
515 uint16_t *frame_src_buf = CONVERT_TO_SHORTPTR(source->y_buffer) +
516 row_offset_y * source->y_stride +
517 col_offset_y;
518 uint16_t *frame_blurred_buf = CONVERT_TO_SHORTPTR(blurred.y_buffer) +
519 row_offset_y * blurred.y_stride +
520 col_offset_y;
521 uint16_t *blurred_dst = CONVERT_TO_SHORTPTR(blurred_block.y_buffer);
522 uint16_t *src_dst = CONVERT_TO_SHORTPTR(source_block.y_buffer);
523
524 // Copy block from source frame.
525 for (int i = 0; i < block_h; ++i) {
526 for (int j = 0; j < block_w; ++j) {
527 if (i >= block_height || j >= block_width) {
528 src_dst[j] = 0;
529 blurred_dst[j] = 0;
530 } else {
531 src_dst[j] = frame_src_buf[j];
532 blurred_dst[j] = frame_blurred_buf[j];
533 }
534 }
535 frame_src_buf += source->y_stride;
536 frame_blurred_buf += blurred.y_stride;
537 src_dst += source_block.y_stride;
538 blurred_dst += blurred_block.y_stride;
539 }
540 } else {
541 uint8_t *frame_src_buf =
542 source->y_buffer + row_offset_y * source->y_stride + col_offset_y;
543 uint8_t *frame_blurred_buf =
544 blurred.y_buffer + row_offset_y * blurred.y_stride + col_offset_y;
545 uint8_t *blurred_dst = blurred_block.y_buffer;
546 uint8_t *src_dst = source_block.y_buffer;
547
548 // Copy block from source frame.
549 for (int i = 0; i < block_h; ++i) {
550 for (int j = 0; j < block_w; ++j) {
551 if (i >= block_height || j >= block_width) {
552 src_dst[j] = 0;
553 blurred_dst[j] = 0;
554 } else {
555 src_dst[j] = frame_src_buf[j];
556 blurred_dst[j] = frame_blurred_buf[j];
557 }
558 }
559 frame_src_buf += source->y_stride;
560 frame_blurred_buf += blurred.y_stride;
561 src_dst += source_block.y_stride;
562 blurred_dst += blurred_block.y_stride;
563 }
564 }
565
566 best_unsharp_amounts[index] = find_best_frame_unsharp_amount(
567 cpi, &source_block, &blurred_block, best_frame_unsharp_amount, 0.1, 3,
568 1.5);
569 }
570 }
571
572 // Apply best blur amounts
573 for (int row = 0; row < num_rows; ++row) {
574 for (int col = 0; col < num_cols; ++col) {
575 const int row_offset_y = row * block_h;
576 const int col_offset_y = col * block_w;
577 const int block_width = AOMMIN(source->y_width - col_offset_y, block_w);
578 const int block_height = AOMMIN(source->y_height - row_offset_y, block_h);
579 const int index = col + row * num_cols;
580
581 if (cm->seq_params->use_highbitdepth) {
582 assert(source->flags & YV12_FLAG_HIGHBITDEPTH);
583 assert(blurred.flags & YV12_FLAG_HIGHBITDEPTH);
584 uint16_t *src_buf = CONVERT_TO_SHORTPTR(source->y_buffer) +
585 row_offset_y * source->y_stride + col_offset_y;
586 uint16_t *blurred_buf = CONVERT_TO_SHORTPTR(blurred.y_buffer) +
587 row_offset_y * blurred.y_stride + col_offset_y;
588 highbd_unsharp_rect(src_buf, source->y_stride, blurred_buf,
589 blurred.y_stride, src_buf, source->y_stride,
590 block_width, block_height,
591 best_unsharp_amounts[index], bit_depth);
592 } else {
593 uint8_t *src_buf =
594 source->y_buffer + row_offset_y * source->y_stride + col_offset_y;
595 uint8_t *blurred_buf =
596 blurred.y_buffer + row_offset_y * blurred.y_stride + col_offset_y;
597 unsharp_rect(src_buf, source->y_stride, blurred_buf, blurred.y_stride,
598 src_buf, source->y_stride, block_width, block_height,
599 best_unsharp_amounts[index]);
600 }
601 }
602 }
603
604 aom_free_frame_buffer(&source_block);
605 aom_free_frame_buffer(&blurred_block);
606 aom_free_frame_buffer(&blurred);
607 aom_free(best_unsharp_amounts);
608 }
609
av1_set_mb_vmaf_rdmult_scaling(AV1_COMP * cpi)610 void av1_set_mb_vmaf_rdmult_scaling(AV1_COMP *cpi) {
611 AV1_COMMON *cm = &cpi->common;
612 const int y_width = cpi->source->y_width;
613 const int y_height = cpi->source->y_height;
614 const int resized_block_size = BLOCK_32X32;
615 const int resize_factor = 2;
616 const int bit_depth = cpi->td.mb.e_mbd.bd;
617 const int ss_x = cpi->source->subsampling_x;
618 const int ss_y = cpi->source->subsampling_y;
619
620 YV12_BUFFER_CONFIG resized_source;
621 memset(&resized_source, 0, sizeof(resized_source));
622 aom_alloc_frame_buffer(
623 &resized_source, y_width / resize_factor, y_height / resize_factor, ss_x,
624 ss_y, cm->seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
625 cm->features.byte_alignment);
626 av1_resize_and_extend_frame_nonnormative(cpi->source, &resized_source,
627 bit_depth, av1_num_planes(cm));
628
629 const int resized_y_width = resized_source.y_width;
630 const int resized_y_height = resized_source.y_height;
631 const int resized_block_w = mi_size_wide[resized_block_size] * 4;
632 const int resized_block_h = mi_size_high[resized_block_size] * 4;
633 const int num_cols =
634 (resized_y_width + resized_block_w - 1) / resized_block_w;
635 const int num_rows =
636 (resized_y_height + resized_block_h - 1) / resized_block_h;
637
638 YV12_BUFFER_CONFIG blurred;
639 memset(&blurred, 0, sizeof(blurred));
640 aom_alloc_frame_buffer(&blurred, resized_y_width, resized_y_height, ss_x,
641 ss_y, cm->seq_params->use_highbitdepth,
642 cpi->oxcf.border_in_pixels,
643 cm->features.byte_alignment);
644 gaussian_blur(bit_depth, &resized_source, &blurred);
645
646 YV12_BUFFER_CONFIG recon;
647 memset(&recon, 0, sizeof(recon));
648 aom_alloc_frame_buffer(&recon, resized_y_width, resized_y_height, ss_x, ss_y,
649 cm->seq_params->use_highbitdepth,
650 cpi->oxcf.border_in_pixels,
651 cm->features.byte_alignment);
652 aom_yv12_copy_frame(&resized_source, &recon, 1);
653
654 VmafContext *vmaf_context;
655 const bool cal_vmaf_neg =
656 cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN;
657 aom_init_vmaf_context(&vmaf_context, cpi->vmaf_info.vmaf_model, cal_vmaf_neg);
658 unsigned int *sses = aom_malloc(sizeof(*sses) * (num_rows * num_cols));
659 memset(sses, 0, sizeof(*sses) * (num_rows * num_cols));
660
661 // Loop through each 'block_size' block.
662 for (int row = 0; row < num_rows; ++row) {
663 for (int col = 0; col < num_cols; ++col) {
664 const int index = row * num_cols + col;
665 const int row_offset_y = row * resized_block_h;
666 const int col_offset_y = col * resized_block_w;
667
668 uint8_t *const orig_buf = resized_source.y_buffer +
669 row_offset_y * resized_source.y_stride +
670 col_offset_y;
671 uint8_t *const blurred_buf =
672 blurred.y_buffer + row_offset_y * blurred.y_stride + col_offset_y;
673
674 cpi->ppi->fn_ptr[resized_block_size].vf(orig_buf, resized_source.y_stride,
675 blurred_buf, blurred.y_stride,
676 &sses[index]);
677
678 uint8_t *const recon_buf =
679 recon.y_buffer + row_offset_y * recon.y_stride + col_offset_y;
680 // Set recon buf
681 if (cpi->common.seq_params->use_highbitdepth) {
682 highbd_unsharp_rect(CONVERT_TO_SHORTPTR(blurred_buf), blurred.y_stride,
683 CONVERT_TO_SHORTPTR(blurred_buf), blurred.y_stride,
684 CONVERT_TO_SHORTPTR(recon_buf), recon.y_stride,
685 resized_block_w, resized_block_h, 0.0, bit_depth);
686 } else {
687 unsharp_rect(blurred_buf, blurred.y_stride, blurred_buf,
688 blurred.y_stride, recon_buf, recon.y_stride,
689 resized_block_w, resized_block_h, 0.0);
690 }
691
692 aom_read_vmaf_image(vmaf_context, &resized_source, &recon, bit_depth,
693 index);
694
695 // Restore recon buf
696 if (cpi->common.seq_params->use_highbitdepth) {
697 highbd_unsharp_rect(
698 CONVERT_TO_SHORTPTR(orig_buf), resized_source.y_stride,
699 CONVERT_TO_SHORTPTR(orig_buf), resized_source.y_stride,
700 CONVERT_TO_SHORTPTR(recon_buf), recon.y_stride, resized_block_w,
701 resized_block_h, 0.0, bit_depth);
702 } else {
703 unsharp_rect(orig_buf, resized_source.y_stride, orig_buf,
704 resized_source.y_stride, recon_buf, recon.y_stride,
705 resized_block_w, resized_block_h, 0.0);
706 }
707 }
708 }
709 aom_flush_vmaf_context(vmaf_context);
710 for (int row = 0; row < num_rows; ++row) {
711 for (int col = 0; col < num_cols; ++col) {
712 const int index = row * num_cols + col;
713 const double vmaf = aom_calc_vmaf_at_index(
714 vmaf_context, cpi->vmaf_info.vmaf_model, index);
715 const double dvmaf = kBaselineVmaf - vmaf;
716
717 const double mse =
718 (double)sses[index] / (double)(resized_y_width * resized_y_height);
719 double weight;
720 const double eps = 0.01 / (num_rows * num_cols);
721 if (dvmaf < eps || mse < eps) {
722 weight = 1.0;
723 } else {
724 weight = mse / dvmaf;
725 }
726
727 // Normalize it with a data fitted model.
728 weight = 6.0 * (1.0 - exp(-0.05 * weight)) + 0.8;
729 cpi->vmaf_info.rdmult_scaling_factors[index] = weight;
730 }
731 }
732
733 aom_free_frame_buffer(&resized_source);
734 aom_free_frame_buffer(&blurred);
735 aom_close_vmaf_context(vmaf_context);
736 aom_free(sses);
737 }
738
av1_set_vmaf_rdmult(const AV1_COMP * const cpi,MACROBLOCK * const x,const BLOCK_SIZE bsize,const int mi_row,const int mi_col,int * const rdmult)739 void av1_set_vmaf_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x,
740 const BLOCK_SIZE bsize, const int mi_row,
741 const int mi_col, int *const rdmult) {
742 const AV1_COMMON *const cm = &cpi->common;
743
744 const int bsize_base = BLOCK_64X64;
745 const int num_mi_w = mi_size_wide[bsize_base];
746 const int num_mi_h = mi_size_high[bsize_base];
747 const int num_cols = (cm->mi_params.mi_cols + num_mi_w - 1) / num_mi_w;
748 const int num_rows = (cm->mi_params.mi_rows + num_mi_h - 1) / num_mi_h;
749 const int num_bcols = (mi_size_wide[bsize] + num_mi_w - 1) / num_mi_w;
750 const int num_brows = (mi_size_high[bsize] + num_mi_h - 1) / num_mi_h;
751 int row, col;
752 double num_of_mi = 0.0;
753 double geom_mean_of_scale = 0.0;
754
755 for (row = mi_row / num_mi_w;
756 row < num_rows && row < mi_row / num_mi_w + num_brows; ++row) {
757 for (col = mi_col / num_mi_h;
758 col < num_cols && col < mi_col / num_mi_h + num_bcols; ++col) {
759 const int index = row * num_cols + col;
760 geom_mean_of_scale += log(cpi->vmaf_info.rdmult_scaling_factors[index]);
761 num_of_mi += 1.0;
762 }
763 }
764 geom_mean_of_scale = exp(geom_mean_of_scale / num_of_mi);
765
766 *rdmult = (int)((double)(*rdmult) * geom_mean_of_scale + 0.5);
767 *rdmult = AOMMAX(*rdmult, 0);
768 av1_set_error_per_bit(&x->errorperbit, *rdmult);
769 }
770
771 // TODO(sdeng): replace them with the SIMD versions.
highbd_image_sad_c(const uint16_t * src,int src_stride,const uint16_t * ref,int ref_stride,int w,int h)772 static AOM_INLINE double highbd_image_sad_c(const uint16_t *src, int src_stride,
773 const uint16_t *ref, int ref_stride,
774 int w, int h) {
775 double accum = 0.0;
776 int i, j;
777
778 for (i = 0; i < h; ++i) {
779 for (j = 0; j < w; ++j) {
780 double img1px = src[i * src_stride + j];
781 double img2px = ref[i * ref_stride + j];
782
783 accum += fabs(img1px - img2px);
784 }
785 }
786
787 return accum / (double)(h * w);
788 }
789
image_sad_c(const uint8_t * src,int src_stride,const uint8_t * ref,int ref_stride,int w,int h)790 static AOM_INLINE double image_sad_c(const uint8_t *src, int src_stride,
791 const uint8_t *ref, int ref_stride, int w,
792 int h) {
793 double accum = 0.0;
794 int i, j;
795
796 for (i = 0; i < h; ++i) {
797 for (j = 0; j < w; ++j) {
798 double img1px = src[i * src_stride + j];
799 double img2px = ref[i * ref_stride + j];
800
801 accum += fabs(img1px - img2px);
802 }
803 }
804
805 return accum / (double)(h * w);
806 }
807
calc_vmaf_motion_score(const AV1_COMP * const cpi,const AV1_COMMON * const cm,const YV12_BUFFER_CONFIG * const cur,const YV12_BUFFER_CONFIG * const last,const YV12_BUFFER_CONFIG * const next)808 static double calc_vmaf_motion_score(const AV1_COMP *const cpi,
809 const AV1_COMMON *const cm,
810 const YV12_BUFFER_CONFIG *const cur,
811 const YV12_BUFFER_CONFIG *const last,
812 const YV12_BUFFER_CONFIG *const next) {
813 const int y_width = cur->y_width;
814 const int y_height = cur->y_height;
815 YV12_BUFFER_CONFIG blurred_cur, blurred_last, blurred_next;
816 const int bit_depth = cpi->td.mb.e_mbd.bd;
817 const int ss_x = cur->subsampling_x;
818 const int ss_y = cur->subsampling_y;
819
820 memset(&blurred_cur, 0, sizeof(blurred_cur));
821 memset(&blurred_last, 0, sizeof(blurred_last));
822 memset(&blurred_next, 0, sizeof(blurred_next));
823
824 aom_alloc_frame_buffer(&blurred_cur, y_width, y_height, ss_x, ss_y,
825 cm->seq_params->use_highbitdepth,
826 cpi->oxcf.border_in_pixels,
827 cm->features.byte_alignment);
828 aom_alloc_frame_buffer(&blurred_last, y_width, y_height, ss_x, ss_y,
829 cm->seq_params->use_highbitdepth,
830 cpi->oxcf.border_in_pixels,
831 cm->features.byte_alignment);
832 aom_alloc_frame_buffer(&blurred_next, y_width, y_height, ss_x, ss_y,
833 cm->seq_params->use_highbitdepth,
834 cpi->oxcf.border_in_pixels,
835 cm->features.byte_alignment);
836
837 gaussian_blur(bit_depth, cur, &blurred_cur);
838 gaussian_blur(bit_depth, last, &blurred_last);
839 if (next) gaussian_blur(bit_depth, next, &blurred_next);
840
841 double motion1, motion2 = 65536.0;
842 if (cm->seq_params->use_highbitdepth) {
843 assert(blurred_cur.flags & YV12_FLAG_HIGHBITDEPTH);
844 assert(blurred_last.flags & YV12_FLAG_HIGHBITDEPTH);
845 const float scale_factor = 1.0f / (float)(1 << (bit_depth - 8));
846 motion1 = highbd_image_sad_c(CONVERT_TO_SHORTPTR(blurred_cur.y_buffer),
847 blurred_cur.y_stride,
848 CONVERT_TO_SHORTPTR(blurred_last.y_buffer),
849 blurred_last.y_stride, y_width, y_height) *
850 scale_factor;
851 if (next) {
852 assert(blurred_next.flags & YV12_FLAG_HIGHBITDEPTH);
853 motion2 = highbd_image_sad_c(CONVERT_TO_SHORTPTR(blurred_cur.y_buffer),
854 blurred_cur.y_stride,
855 CONVERT_TO_SHORTPTR(blurred_next.y_buffer),
856 blurred_next.y_stride, y_width, y_height) *
857 scale_factor;
858 }
859 } else {
860 motion1 = image_sad_c(blurred_cur.y_buffer, blurred_cur.y_stride,
861 blurred_last.y_buffer, blurred_last.y_stride, y_width,
862 y_height);
863 if (next) {
864 motion2 = image_sad_c(blurred_cur.y_buffer, blurred_cur.y_stride,
865 blurred_next.y_buffer, blurred_next.y_stride,
866 y_width, y_height);
867 }
868 }
869
870 aom_free_frame_buffer(&blurred_cur);
871 aom_free_frame_buffer(&blurred_last);
872 aom_free_frame_buffer(&blurred_next);
873
874 return AOMMIN(motion1, motion2);
875 }
876
get_neighbor_frames(const AV1_COMP * const cpi,YV12_BUFFER_CONFIG ** last,YV12_BUFFER_CONFIG ** next)877 static AOM_INLINE void get_neighbor_frames(const AV1_COMP *const cpi,
878 YV12_BUFFER_CONFIG **last,
879 YV12_BUFFER_CONFIG **next) {
880 const AV1_COMMON *const cm = &cpi->common;
881 const GF_GROUP *gf_group = &cpi->ppi->gf_group;
882 const int src_index =
883 cm->show_frame != 0 ? 0 : gf_group->arf_src_offset[cpi->gf_frame_index];
884 struct lookahead_entry *last_entry = av1_lookahead_peek(
885 cpi->ppi->lookahead, src_index - 1, cpi->compressor_stage);
886 struct lookahead_entry *next_entry = av1_lookahead_peek(
887 cpi->ppi->lookahead, src_index + 1, cpi->compressor_stage);
888 *next = &next_entry->img;
889 *last = cm->show_frame ? cpi->last_source : &last_entry->img;
890 }
891
892 // Calculates the new qindex from the VMAF motion score. This is based on the
893 // observation: when the motion score becomes higher, the VMAF score of the
894 // same source and distorted frames would become higher.
av1_get_vmaf_base_qindex(const AV1_COMP * const cpi,int current_qindex)895 int av1_get_vmaf_base_qindex(const AV1_COMP *const cpi, int current_qindex) {
896 const AV1_COMMON *const cm = &cpi->common;
897 if (cm->current_frame.frame_number == 0 || cpi->oxcf.pass == 1) {
898 return current_qindex;
899 }
900 const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
901 const int layer_depth =
902 AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], MAX_ARF_LAYERS - 1);
903 const double last_frame_ysse =
904 get_layer_value(cpi->vmaf_info.last_frame_ysse, layer_depth);
905 const double last_frame_vmaf =
906 get_layer_value(cpi->vmaf_info.last_frame_vmaf, layer_depth);
907 const int bit_depth = cpi->td.mb.e_mbd.bd;
908 const double approx_sse = last_frame_ysse / (double)((1 << (bit_depth - 8)) *
909 (1 << (bit_depth - 8)));
910 const double approx_dvmaf = kBaselineVmaf - last_frame_vmaf;
911 const double sse_threshold =
912 0.01 * cpi->source->y_width * cpi->source->y_height;
913 const double vmaf_threshold = 0.01;
914 if (approx_sse < sse_threshold || approx_dvmaf < vmaf_threshold) {
915 return current_qindex;
916 }
917 YV12_BUFFER_CONFIG *cur_buf = cpi->source;
918 if (cm->show_frame == 0) {
919 const int src_index = gf_group->arf_src_offset[cpi->gf_frame_index];
920 struct lookahead_entry *cur_entry = av1_lookahead_peek(
921 cpi->ppi->lookahead, src_index, cpi->compressor_stage);
922 cur_buf = &cur_entry->img;
923 }
924 assert(cur_buf);
925
926 YV12_BUFFER_CONFIG *next_buf, *last_buf;
927 get_neighbor_frames(cpi, &last_buf, &next_buf);
928 assert(last_buf);
929
930 const double motion =
931 calc_vmaf_motion_score(cpi, cm, cur_buf, last_buf, next_buf);
932
933 // Get dVMAF through a data fitted model.
934 const double dvmaf = 26.11 * (1.0 - exp(-0.06 * motion));
935 const double dsse = dvmaf * approx_sse / approx_dvmaf;
936
937 const double beta = approx_sse / (dsse + approx_sse);
938 const int offset =
939 av1_get_deltaq_offset(cm->seq_params->bit_depth, current_qindex, beta);
940 int qindex = current_qindex + offset;
941
942 qindex = AOMMIN(qindex, MAXQ);
943 qindex = AOMMAX(qindex, MINQ);
944
945 return qindex;
946 }
947
cal_approx_score(AV1_COMP * const cpi,double src_variance,double new_variance,double src_score,YV12_BUFFER_CONFIG * const src,YV12_BUFFER_CONFIG * const recon_sharpened)948 static AOM_INLINE double cal_approx_score(
949 AV1_COMP *const cpi, double src_variance, double new_variance,
950 double src_score, YV12_BUFFER_CONFIG *const src,
951 YV12_BUFFER_CONFIG *const recon_sharpened) {
952 double score;
953 const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
954 const bool cal_vmaf_neg =
955 cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN;
956 aom_calc_vmaf(cpi->vmaf_info.vmaf_model, src, recon_sharpened, bit_depth,
957 cal_vmaf_neg, &score);
958 return src_variance / new_variance * (score - src_score);
959 }
960
find_best_frame_unsharp_amount_loop_neg(AV1_COMP * const cpi,double src_variance,double base_score,YV12_BUFFER_CONFIG * const src,YV12_BUFFER_CONFIG * const recon,YV12_BUFFER_CONFIG * const ref,YV12_BUFFER_CONFIG * const src_blurred,YV12_BUFFER_CONFIG * const recon_blurred,YV12_BUFFER_CONFIG * const src_sharpened,YV12_BUFFER_CONFIG * const recon_sharpened,FULLPEL_MV * mvs,double best_score,const double unsharp_amount_start,const double step_size,const int max_loop_count,const double max_amount)961 static double find_best_frame_unsharp_amount_loop_neg(
962 AV1_COMP *const cpi, double src_variance, double base_score,
963 YV12_BUFFER_CONFIG *const src, YV12_BUFFER_CONFIG *const recon,
964 YV12_BUFFER_CONFIG *const ref, YV12_BUFFER_CONFIG *const src_blurred,
965 YV12_BUFFER_CONFIG *const recon_blurred,
966 YV12_BUFFER_CONFIG *const src_sharpened,
967 YV12_BUFFER_CONFIG *const recon_sharpened, FULLPEL_MV *mvs,
968 double best_score, const double unsharp_amount_start,
969 const double step_size, const int max_loop_count, const double max_amount) {
970 const double min_amount = 0.0;
971 int loop_count = 0;
972 double approx_score = best_score;
973 double unsharp_amount = unsharp_amount_start;
974
975 do {
976 best_score = approx_score;
977 unsharp_amount += step_size;
978 if (unsharp_amount > max_amount || unsharp_amount < min_amount) break;
979 unsharp(cpi, recon, recon_blurred, recon_sharpened, unsharp_amount);
980 unsharp(cpi, src, src_blurred, src_sharpened, unsharp_amount);
981 const double new_variance =
982 residual_frame_average_variance(cpi, src_sharpened, ref, mvs);
983 approx_score = cal_approx_score(cpi, src_variance, new_variance, base_score,
984 src, recon_sharpened);
985
986 loop_count++;
987 } while (approx_score > best_score && loop_count < max_loop_count);
988 unsharp_amount =
989 approx_score > best_score ? unsharp_amount : unsharp_amount - step_size;
990
991 return AOMMIN(max_amount, AOMMAX(unsharp_amount, min_amount));
992 }
993
find_best_frame_unsharp_amount_neg(AV1_COMP * const cpi,YV12_BUFFER_CONFIG * const src,YV12_BUFFER_CONFIG * const recon,YV12_BUFFER_CONFIG * const ref,double base_score,const double unsharp_amount_start,const double step_size,const int max_loop_count,const double max_filter_amount)994 static double find_best_frame_unsharp_amount_neg(
995 AV1_COMP *const cpi, YV12_BUFFER_CONFIG *const src,
996 YV12_BUFFER_CONFIG *const recon, YV12_BUFFER_CONFIG *const ref,
997 double base_score, const double unsharp_amount_start,
998 const double step_size, const int max_loop_count,
999 const double max_filter_amount) {
1000 FULLPEL_MV *mvs = NULL;
1001 const double src_variance =
1002 residual_frame_average_variance(cpi, src, ref, mvs);
1003
1004 const AV1_COMMON *const cm = &cpi->common;
1005 const int width = recon->y_width;
1006 const int height = recon->y_height;
1007 const int bit_depth = cpi->td.mb.e_mbd.bd;
1008 const int ss_x = recon->subsampling_x;
1009 const int ss_y = recon->subsampling_y;
1010
1011 YV12_BUFFER_CONFIG src_blurred, recon_blurred, src_sharpened, recon_sharpened;
1012 memset(&recon_sharpened, 0, sizeof(recon_sharpened));
1013 memset(&src_sharpened, 0, sizeof(src_sharpened));
1014 memset(&recon_blurred, 0, sizeof(recon_blurred));
1015 memset(&src_blurred, 0, sizeof(src_blurred));
1016 aom_alloc_frame_buffer(&recon_sharpened, width, height, ss_x, ss_y,
1017 cm->seq_params->use_highbitdepth,
1018 cpi->oxcf.border_in_pixels,
1019 cm->features.byte_alignment);
1020 aom_alloc_frame_buffer(&src_sharpened, width, height, ss_x, ss_y,
1021 cm->seq_params->use_highbitdepth,
1022 cpi->oxcf.border_in_pixels,
1023 cm->features.byte_alignment);
1024 aom_alloc_frame_buffer(&recon_blurred, width, height, ss_x, ss_y,
1025 cm->seq_params->use_highbitdepth,
1026 cpi->oxcf.border_in_pixels,
1027 cm->features.byte_alignment);
1028 aom_alloc_frame_buffer(
1029 &src_blurred, width, height, ss_x, ss_y, cm->seq_params->use_highbitdepth,
1030 cpi->oxcf.border_in_pixels, cm->features.byte_alignment);
1031
1032 gaussian_blur(bit_depth, recon, &recon_blurred);
1033 gaussian_blur(bit_depth, src, &src_blurred);
1034
1035 unsharp(cpi, recon, &recon_blurred, &recon_sharpened, unsharp_amount_start);
1036 unsharp(cpi, src, &src_blurred, &src_sharpened, unsharp_amount_start);
1037 const double variance_start =
1038 residual_frame_average_variance(cpi, &src_sharpened, ref, mvs);
1039 const double score_start = cal_approx_score(
1040 cpi, src_variance, variance_start, base_score, src, &recon_sharpened);
1041
1042 const double unsharp_amount_next = unsharp_amount_start + step_size;
1043 unsharp(cpi, recon, &recon_blurred, &recon_sharpened, unsharp_amount_next);
1044 unsharp(cpi, src, &src_blurred, &src_sharpened, unsharp_amount_next);
1045 const double variance_next =
1046 residual_frame_average_variance(cpi, &src_sharpened, ref, mvs);
1047 const double score_next = cal_approx_score(cpi, src_variance, variance_next,
1048 base_score, src, &recon_sharpened);
1049
1050 double unsharp_amount;
1051 if (score_next > score_start) {
1052 unsharp_amount = find_best_frame_unsharp_amount_loop_neg(
1053 cpi, src_variance, base_score, src, recon, ref, &src_blurred,
1054 &recon_blurred, &src_sharpened, &recon_sharpened, mvs, score_next,
1055 unsharp_amount_next, step_size, max_loop_count, max_filter_amount);
1056 } else {
1057 unsharp_amount = find_best_frame_unsharp_amount_loop_neg(
1058 cpi, src_variance, base_score, src, recon, ref, &src_blurred,
1059 &recon_blurred, &src_sharpened, &recon_sharpened, mvs, score_start,
1060 unsharp_amount_start, -step_size, max_loop_count, max_filter_amount);
1061 }
1062
1063 aom_free_frame_buffer(&recon_sharpened);
1064 aom_free_frame_buffer(&src_sharpened);
1065 aom_free_frame_buffer(&recon_blurred);
1066 aom_free_frame_buffer(&src_blurred);
1067 aom_free(mvs);
1068 return unsharp_amount;
1069 }
1070
av1_update_vmaf_curve(AV1_COMP * cpi)1071 void av1_update_vmaf_curve(AV1_COMP *cpi) {
1072 YV12_BUFFER_CONFIG *source = cpi->source;
1073 YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
1074 const int bit_depth = cpi->td.mb.e_mbd.bd;
1075 const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
1076 const int layer_depth =
1077 AOMMIN(gf_group->layer_depth[cpi->gf_frame_index], MAX_ARF_LAYERS - 1);
1078 double base_score;
1079 const bool cal_vmaf_neg =
1080 cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN;
1081 aom_calc_vmaf(cpi->vmaf_info.vmaf_model, source, recon, bit_depth,
1082 cal_vmaf_neg, &base_score);
1083 cpi->vmaf_info.last_frame_vmaf[layer_depth] = base_score;
1084 if (cpi->common.seq_params->use_highbitdepth) {
1085 assert(source->flags & YV12_FLAG_HIGHBITDEPTH);
1086 assert(recon->flags & YV12_FLAG_HIGHBITDEPTH);
1087 cpi->vmaf_info.last_frame_ysse[layer_depth] =
1088 (double)aom_highbd_get_y_sse(source, recon);
1089 } else {
1090 cpi->vmaf_info.last_frame_ysse[layer_depth] =
1091 (double)aom_get_y_sse(source, recon);
1092 }
1093
1094 if (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
1095 YV12_BUFFER_CONFIG *last, *next;
1096 get_neighbor_frames(cpi, &last, &next);
1097 double best_unsharp_amount_start =
1098 get_layer_value(cpi->vmaf_info.last_frame_unsharp_amount, layer_depth);
1099 const int max_loop_count = 5;
1100 cpi->vmaf_info.last_frame_unsharp_amount[layer_depth] =
1101 find_best_frame_unsharp_amount_neg(cpi, source, recon, last, base_score,
1102 best_unsharp_amount_start, 0.025,
1103 max_loop_count, 1.01);
1104 }
1105 }
1106