1 /*
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <assert.h>
12 #include <limits.h>
13 #include <math.h>
14
15 #include "./vpx_dsp_rtcd.h"
16 #include "vpx_dsp/vpx_dsp_common.h"
17 #include "vpx_scale/yv12config.h"
18 #include "vpx/vpx_integer.h"
19 #include "vp9/common/vp9_reconinter.h"
20 #include "vp9/encoder/vp9_context_tree.h"
21 #include "vp9/encoder/vp9_denoiser.h"
22 #include "vp9/encoder/vp9_encoder.h"
23
24 #ifdef OUTPUT_YUV_DENOISED
25 static void make_grayscale(YV12_BUFFER_CONFIG *yuv);
26 #endif
27
absdiff_thresh(BLOCK_SIZE bs,int increase_denoising)28 static int absdiff_thresh(BLOCK_SIZE bs, int increase_denoising) {
29 (void)bs;
30 return 3 + (increase_denoising ? 1 : 0);
31 }
32
delta_thresh(BLOCK_SIZE bs,int increase_denoising)33 static int delta_thresh(BLOCK_SIZE bs, int increase_denoising) {
34 (void)bs;
35 (void)increase_denoising;
36 return 4;
37 }
38
noise_motion_thresh(BLOCK_SIZE bs,int increase_denoising)39 static int noise_motion_thresh(BLOCK_SIZE bs, int increase_denoising) {
40 (void)bs;
41 (void)increase_denoising;
42 return 625;
43 }
44
sse_thresh(BLOCK_SIZE bs,int increase_denoising)45 static unsigned int sse_thresh(BLOCK_SIZE bs, int increase_denoising) {
46 return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 80 : 40);
47 }
48
sse_diff_thresh(BLOCK_SIZE bs,int increase_denoising,int motion_magnitude)49 static int sse_diff_thresh(BLOCK_SIZE bs, int increase_denoising,
50 int motion_magnitude) {
51 if (motion_magnitude > noise_motion_thresh(bs, increase_denoising)) {
52 if (increase_denoising)
53 return (1 << num_pels_log2_lookup[bs]) << 2;
54 else
55 return 0;
56 } else {
57 return (1 << num_pels_log2_lookup[bs]) << 4;
58 }
59 }
60
total_adj_weak_thresh(BLOCK_SIZE bs,int increase_denoising)61 static int total_adj_weak_thresh(BLOCK_SIZE bs, int increase_denoising) {
62 return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 3 : 2);
63 }
64
65 // TODO(jackychen): If increase_denoising is enabled in the future,
66 // we might need to update the code for calculating 'total_adj' in
67 // case the C code is not bit-exact with corresponding sse2 code.
vp9_denoiser_filter_c(const uint8_t * sig,int sig_stride,const uint8_t * mc_avg,int mc_avg_stride,uint8_t * avg,int avg_stride,int increase_denoising,BLOCK_SIZE bs,int motion_magnitude)68 int vp9_denoiser_filter_c(const uint8_t *sig, int sig_stride,
69 const uint8_t *mc_avg, int mc_avg_stride,
70 uint8_t *avg, int avg_stride, int increase_denoising,
71 BLOCK_SIZE bs, int motion_magnitude) {
72 int r, c;
73 const uint8_t *sig_start = sig;
74 const uint8_t *mc_avg_start = mc_avg;
75 uint8_t *avg_start = avg;
76 int diff, adj, absdiff, delta;
77 int adj_val[] = { 3, 4, 6 };
78 int total_adj = 0;
79 int shift_inc = 1;
80
81 // If motion_magnitude is small, making the denoiser more aggressive by
82 // increasing the adjustment for each level. Add another increment for
83 // blocks that are labeled for increase denoising.
84 if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) {
85 if (increase_denoising) {
86 shift_inc = 2;
87 }
88 adj_val[0] += shift_inc;
89 adj_val[1] += shift_inc;
90 adj_val[2] += shift_inc;
91 }
92
93 // First attempt to apply a strong temporal denoising filter.
94 for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
95 for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
96 diff = mc_avg[c] - sig[c];
97 absdiff = abs(diff);
98
99 if (absdiff <= absdiff_thresh(bs, increase_denoising)) {
100 avg[c] = mc_avg[c];
101 total_adj += diff;
102 } else {
103 switch (absdiff) {
104 case 4:
105 case 5:
106 case 6:
107 case 7: adj = adj_val[0]; break;
108 case 8:
109 case 9:
110 case 10:
111 case 11:
112 case 12:
113 case 13:
114 case 14:
115 case 15: adj = adj_val[1]; break;
116 default: adj = adj_val[2];
117 }
118 if (diff > 0) {
119 avg[c] = VPXMIN(UINT8_MAX, sig[c] + adj);
120 total_adj += adj;
121 } else {
122 avg[c] = VPXMAX(0, sig[c] - adj);
123 total_adj -= adj;
124 }
125 }
126 }
127 sig += sig_stride;
128 avg += avg_stride;
129 mc_avg += mc_avg_stride;
130 }
131
132 // If the strong filter did not modify the signal too much, we're all set.
133 if (abs(total_adj) <= total_adj_strong_thresh(bs, increase_denoising)) {
134 return FILTER_BLOCK;
135 }
136
137 // Otherwise, we try to dampen the filter if the delta is not too high.
138 delta = ((abs(total_adj) - total_adj_strong_thresh(bs, increase_denoising)) >>
139 num_pels_log2_lookup[bs]) +
140 1;
141
142 if (delta >= delta_thresh(bs, increase_denoising)) {
143 return COPY_BLOCK;
144 }
145
146 mc_avg = mc_avg_start;
147 avg = avg_start;
148 sig = sig_start;
149 for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
150 for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
151 diff = mc_avg[c] - sig[c];
152 adj = abs(diff);
153 if (adj > delta) {
154 adj = delta;
155 }
156 if (diff > 0) {
157 // Diff positive means we made positive adjustment above
158 // (in first try/attempt), so now make negative adjustment to bring
159 // denoised signal down.
160 avg[c] = VPXMAX(0, avg[c] - adj);
161 total_adj -= adj;
162 } else {
163 // Diff negative means we made negative adjustment above
164 // (in first try/attempt), so now make positive adjustment to bring
165 // denoised signal up.
166 avg[c] = VPXMIN(UINT8_MAX, avg[c] + adj);
167 total_adj += adj;
168 }
169 }
170 sig += sig_stride;
171 avg += avg_stride;
172 mc_avg += mc_avg_stride;
173 }
174
175 // We can use the filter if it has been sufficiently dampened
176 if (abs(total_adj) <= total_adj_weak_thresh(bs, increase_denoising)) {
177 return FILTER_BLOCK;
178 }
179 return COPY_BLOCK;
180 }
181
block_start(uint8_t * framebuf,int stride,int mi_row,int mi_col)182 static uint8_t *block_start(uint8_t *framebuf, int stride, int mi_row,
183 int mi_col) {
184 return framebuf + (stride * mi_row << 3) + (mi_col << 3);
185 }
186
perform_motion_compensation(VP9_COMMON * const cm,VP9_DENOISER * denoiser,MACROBLOCK * mb,BLOCK_SIZE bs,int increase_denoising,int mi_row,int mi_col,PICK_MODE_CONTEXT * ctx,int motion_magnitude,int is_skin,int * zeromv_filter,int consec_zeromv,int num_spatial_layers,int width,int lst_fb_idx,int gld_fb_idx,int use_svc,int spatial_layer)187 static VP9_DENOISER_DECISION perform_motion_compensation(
188 VP9_COMMON *const cm, VP9_DENOISER *denoiser, MACROBLOCK *mb, BLOCK_SIZE bs,
189 int increase_denoising, int mi_row, int mi_col, PICK_MODE_CONTEXT *ctx,
190 int motion_magnitude, int is_skin, int *zeromv_filter, int consec_zeromv,
191 int num_spatial_layers, int width, int lst_fb_idx, int gld_fb_idx,
192 int use_svc, int spatial_layer) {
193 const int sse_diff = (ctx->newmv_sse == UINT_MAX)
194 ? 0
195 : ((int)ctx->zeromv_sse - (int)ctx->newmv_sse);
196 int frame;
197 int denoise_layer_idx = 0;
198 MACROBLOCKD *filter_mbd = &mb->e_mbd;
199 MODE_INFO *mi = filter_mbd->mi[0];
200 MODE_INFO saved_mi;
201 int i;
202 struct buf_2d saved_dst[MAX_MB_PLANE];
203 struct buf_2d saved_pre[MAX_MB_PLANE];
204 RefBuffer *saved_block_refs[2];
205 MV_REFERENCE_FRAME saved_frame;
206
207 frame = ctx->best_reference_frame;
208
209 saved_mi = *mi;
210
211 if (is_skin && (motion_magnitude > 0 || consec_zeromv < 4)) return COPY_BLOCK;
212
213 // Avoid denoising small blocks. When noise > kDenLow or frame width > 480,
214 // denoise 16x16 blocks.
215 if (bs == BLOCK_8X8 || bs == BLOCK_8X16 || bs == BLOCK_16X8 ||
216 (bs == BLOCK_16X16 && width > 480 &&
217 denoiser->denoising_level <= kDenLow))
218 return COPY_BLOCK;
219
220 // If the best reference frame uses inter-prediction and there is enough of a
221 // difference in sum-squared-error, use it.
222 if (frame != INTRA_FRAME && frame != ALTREF_FRAME &&
223 (frame != GOLDEN_FRAME || num_spatial_layers == 1) &&
224 sse_diff > sse_diff_thresh(bs, increase_denoising, motion_magnitude)) {
225 mi->ref_frame[0] = ctx->best_reference_frame;
226 mi->mode = ctx->best_sse_inter_mode;
227 mi->mv[0] = ctx->best_sse_mv;
228 } else {
229 // Otherwise, use the zero reference frame.
230 frame = ctx->best_zeromv_reference_frame;
231 ctx->newmv_sse = ctx->zeromv_sse;
232 // Bias to last reference.
233 if (num_spatial_layers > 1 || frame == ALTREF_FRAME ||
234 (frame != LAST_FRAME &&
235 ((ctx->zeromv_lastref_sse<(5 * ctx->zeromv_sse)>> 2) ||
236 denoiser->denoising_level >= kDenHigh))) {
237 frame = LAST_FRAME;
238 ctx->newmv_sse = ctx->zeromv_lastref_sse;
239 }
240 mi->ref_frame[0] = frame;
241 mi->mode = ZEROMV;
242 mi->mv[0].as_int = 0;
243 ctx->best_sse_inter_mode = ZEROMV;
244 ctx->best_sse_mv.as_int = 0;
245 *zeromv_filter = 1;
246 if (denoiser->denoising_level > kDenMedium) {
247 motion_magnitude = 0;
248 }
249 }
250
251 saved_frame = frame;
252 // When using SVC, we need to map REF_FRAME to the frame buffer index.
253 if (use_svc) {
254 if (frame == LAST_FRAME)
255 frame = lst_fb_idx + 1;
256 else if (frame == GOLDEN_FRAME)
257 frame = gld_fb_idx + 1;
258 // Shift for the second spatial layer.
259 if (num_spatial_layers - spatial_layer == 2)
260 frame = frame + denoiser->num_ref_frames;
261 denoise_layer_idx = num_spatial_layers - spatial_layer - 1;
262 }
263
264 if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) {
265 // Restore everything to its original state
266 *mi = saved_mi;
267 return COPY_BLOCK;
268 }
269 if (motion_magnitude > (noise_motion_thresh(bs, increase_denoising) << 3)) {
270 // Restore everything to its original state
271 *mi = saved_mi;
272 return COPY_BLOCK;
273 }
274
275 // We will restore these after motion compensation.
276 for (i = 0; i < MAX_MB_PLANE; ++i) {
277 saved_pre[i] = filter_mbd->plane[i].pre[0];
278 saved_dst[i] = filter_mbd->plane[i].dst;
279 }
280 saved_block_refs[0] = filter_mbd->block_refs[0];
281
282 // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
283 // struct.
284 filter_mbd->plane[0].pre[0].buf =
285 block_start(denoiser->running_avg_y[frame].y_buffer,
286 denoiser->running_avg_y[frame].y_stride, mi_row, mi_col);
287 filter_mbd->plane[0].pre[0].stride = denoiser->running_avg_y[frame].y_stride;
288 filter_mbd->plane[1].pre[0].buf =
289 block_start(denoiser->running_avg_y[frame].u_buffer,
290 denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
291 filter_mbd->plane[1].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
292 filter_mbd->plane[2].pre[0].buf =
293 block_start(denoiser->running_avg_y[frame].v_buffer,
294 denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
295 filter_mbd->plane[2].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
296
297 filter_mbd->plane[0].dst.buf = block_start(
298 denoiser->mc_running_avg_y[denoise_layer_idx].y_buffer,
299 denoiser->mc_running_avg_y[denoise_layer_idx].y_stride, mi_row, mi_col);
300 filter_mbd->plane[0].dst.stride =
301 denoiser->mc_running_avg_y[denoise_layer_idx].y_stride;
302 filter_mbd->plane[1].dst.buf = block_start(
303 denoiser->mc_running_avg_y[denoise_layer_idx].u_buffer,
304 denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride, mi_row, mi_col);
305 filter_mbd->plane[1].dst.stride =
306 denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride;
307 filter_mbd->plane[2].dst.buf = block_start(
308 denoiser->mc_running_avg_y[denoise_layer_idx].v_buffer,
309 denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride, mi_row, mi_col);
310 filter_mbd->plane[2].dst.stride =
311 denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride;
312
313 set_ref_ptrs(cm, filter_mbd, saved_frame, NONE);
314 vp9_build_inter_predictors_sby(filter_mbd, mi_row, mi_col, bs);
315
316 // Restore everything to its original state
317 *mi = saved_mi;
318 filter_mbd->block_refs[0] = saved_block_refs[0];
319 for (i = 0; i < MAX_MB_PLANE; ++i) {
320 filter_mbd->plane[i].pre[0] = saved_pre[i];
321 filter_mbd->plane[i].dst = saved_dst[i];
322 }
323
324 return FILTER_BLOCK;
325 }
326
vp9_denoiser_denoise(VP9_COMP * cpi,MACROBLOCK * mb,int mi_row,int mi_col,BLOCK_SIZE bs,PICK_MODE_CONTEXT * ctx,VP9_DENOISER_DECISION * denoiser_decision)327 void vp9_denoiser_denoise(VP9_COMP *cpi, MACROBLOCK *mb, int mi_row, int mi_col,
328 BLOCK_SIZE bs, PICK_MODE_CONTEXT *ctx,
329 VP9_DENOISER_DECISION *denoiser_decision) {
330 int mv_col, mv_row;
331 int motion_magnitude = 0;
332 int zeromv_filter = 0;
333 VP9_DENOISER *denoiser = &cpi->denoiser;
334 VP9_DENOISER_DECISION decision = COPY_BLOCK;
335
336 const int shift =
337 cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id == 2
338 ? denoiser->num_ref_frames
339 : 0;
340 YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME + shift];
341 const int denoise_layer_index =
342 cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id - 1;
343 YV12_BUFFER_CONFIG mc_avg = denoiser->mc_running_avg_y[denoise_layer_index];
344 uint8_t *avg_start = block_start(avg.y_buffer, avg.y_stride, mi_row, mi_col);
345
346 uint8_t *mc_avg_start =
347 block_start(mc_avg.y_buffer, mc_avg.y_stride, mi_row, mi_col);
348 struct buf_2d src = mb->plane[0].src;
349 int is_skin = 0;
350 int increase_denoising = 0;
351 int consec_zeromv = 0;
352 mv_col = ctx->best_sse_mv.as_mv.col;
353 mv_row = ctx->best_sse_mv.as_mv.row;
354 motion_magnitude = mv_row * mv_row + mv_col * mv_col;
355
356 if (cpi->use_skin_detection && bs <= BLOCK_32X32 &&
357 denoiser->denoising_level < kDenHigh) {
358 int motion_level = (motion_magnitude < 16) ? 0 : 1;
359 // If motion for current block is small/zero, compute consec_zeromv for
360 // skin detection (early exit in skin detection is done for large
361 // consec_zeromv when current block has small/zero motion).
362 consec_zeromv = 0;
363 if (motion_level == 0) {
364 VP9_COMMON *const cm = &cpi->common;
365 int j, i;
366 // Loop through the 8x8 sub-blocks.
367 const int bw = num_8x8_blocks_wide_lookup[bs];
368 const int bh = num_8x8_blocks_high_lookup[bs];
369 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
370 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
371 const int block_index = mi_row * cm->mi_cols + mi_col;
372 consec_zeromv = 100;
373 for (i = 0; i < ymis; i++) {
374 for (j = 0; j < xmis; j++) {
375 int bl_index = block_index + i * cm->mi_cols + j;
376 consec_zeromv = VPXMIN(cpi->consec_zero_mv[bl_index], consec_zeromv);
377 // No need to keep checking 8x8 blocks if any of the sub-blocks
378 // has small consec_zeromv (since threshold for no_skin based on
379 // zero/small motion in skin detection is high, i.e, > 4).
380 if (consec_zeromv < 4) {
381 i = ymis;
382 j = xmis;
383 }
384 }
385 }
386 }
387 // TODO(marpan): Compute skin detection over sub-blocks.
388 is_skin = vp9_compute_skin_block(
389 mb->plane[0].src.buf, mb->plane[1].src.buf, mb->plane[2].src.buf,
390 mb->plane[0].src.stride, mb->plane[1].src.stride, bs, consec_zeromv,
391 motion_level);
392 }
393 if (!is_skin && denoiser->denoising_level == kDenHigh) increase_denoising = 1;
394
395 if (denoiser->denoising_level >= kDenLow && !ctx->sb_skip_denoising)
396 decision = perform_motion_compensation(
397 &cpi->common, denoiser, mb, bs, increase_denoising, mi_row, mi_col, ctx,
398 motion_magnitude, is_skin, &zeromv_filter, consec_zeromv,
399 cpi->svc.number_spatial_layers, cpi->Source->y_width, cpi->lst_fb_idx,
400 cpi->gld_fb_idx, cpi->use_svc, cpi->svc.spatial_layer_id);
401
402 if (decision == FILTER_BLOCK) {
403 decision = vp9_denoiser_filter(src.buf, src.stride, mc_avg_start,
404 mc_avg.y_stride, avg_start, avg.y_stride,
405 increase_denoising, bs, motion_magnitude);
406 }
407
408 if (decision == FILTER_BLOCK) {
409 vpx_convolve_copy(avg_start, avg.y_stride, src.buf, src.stride, NULL, 0, 0,
410 0, 0, num_4x4_blocks_wide_lookup[bs] << 2,
411 num_4x4_blocks_high_lookup[bs] << 2);
412 } else { // COPY_BLOCK
413 vpx_convolve_copy(src.buf, src.stride, avg_start, avg.y_stride, NULL, 0, 0,
414 0, 0, num_4x4_blocks_wide_lookup[bs] << 2,
415 num_4x4_blocks_high_lookup[bs] << 2);
416 }
417 *denoiser_decision = decision;
418 if (decision == FILTER_BLOCK && zeromv_filter == 1)
419 *denoiser_decision = FILTER_ZEROMV_BLOCK;
420 }
421
copy_frame(YV12_BUFFER_CONFIG * const dest,const YV12_BUFFER_CONFIG * const src)422 static void copy_frame(YV12_BUFFER_CONFIG *const dest,
423 const YV12_BUFFER_CONFIG *const src) {
424 int r;
425 const uint8_t *srcbuf = src->y_buffer;
426 uint8_t *destbuf = dest->y_buffer;
427
428 assert(dest->y_width == src->y_width);
429 assert(dest->y_height == src->y_height);
430
431 for (r = 0; r < dest->y_height; ++r) {
432 memcpy(destbuf, srcbuf, dest->y_width);
433 destbuf += dest->y_stride;
434 srcbuf += src->y_stride;
435 }
436 }
437
swap_frame_buffer(YV12_BUFFER_CONFIG * const dest,YV12_BUFFER_CONFIG * const src)438 static void swap_frame_buffer(YV12_BUFFER_CONFIG *const dest,
439 YV12_BUFFER_CONFIG *const src) {
440 uint8_t *tmp_buf = dest->y_buffer;
441 assert(dest->y_width == src->y_width);
442 assert(dest->y_height == src->y_height);
443 dest->y_buffer = src->y_buffer;
444 src->y_buffer = tmp_buf;
445 }
446
vp9_denoiser_update_frame_info(VP9_DENOISER * denoiser,YV12_BUFFER_CONFIG src,FRAME_TYPE frame_type,int refresh_alt_ref_frame,int refresh_golden_frame,int refresh_last_frame,int alt_fb_idx,int gld_fb_idx,int lst_fb_idx,int resized,int svc_base_is_key,int second_spatial_layer)447 void vp9_denoiser_update_frame_info(
448 VP9_DENOISER *denoiser, YV12_BUFFER_CONFIG src, FRAME_TYPE frame_type,
449 int refresh_alt_ref_frame, int refresh_golden_frame, int refresh_last_frame,
450 int alt_fb_idx, int gld_fb_idx, int lst_fb_idx, int resized,
451 int svc_base_is_key, int second_spatial_layer) {
452 const int shift = second_spatial_layer ? denoiser->num_ref_frames : 0;
453 // Copy source into denoised reference buffers on KEY_FRAME or
454 // if the just encoded frame was resized. For SVC, copy source if the base
455 // spatial layer was key frame.
456 if (frame_type == KEY_FRAME || resized != 0 || denoiser->reset ||
457 svc_base_is_key) {
458 int i;
459 // Start at 1 so as not to overwrite the INTRA_FRAME
460 for (i = 1; i < denoiser->num_ref_frames; ++i) {
461 if (denoiser->running_avg_y[i + shift].buffer_alloc != NULL)
462 copy_frame(&denoiser->running_avg_y[i + shift], &src);
463 }
464 denoiser->reset = 0;
465 return;
466 }
467
468 // If more than one refresh occurs, must copy frame buffer.
469 if ((refresh_alt_ref_frame + refresh_golden_frame + refresh_last_frame) > 1) {
470 if (refresh_alt_ref_frame) {
471 copy_frame(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
472 &denoiser->running_avg_y[INTRA_FRAME + shift]);
473 }
474 if (refresh_golden_frame) {
475 copy_frame(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
476 &denoiser->running_avg_y[INTRA_FRAME + shift]);
477 }
478 if (refresh_last_frame) {
479 copy_frame(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
480 &denoiser->running_avg_y[INTRA_FRAME + shift]);
481 }
482 } else {
483 if (refresh_alt_ref_frame) {
484 swap_frame_buffer(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
485 &denoiser->running_avg_y[INTRA_FRAME + shift]);
486 }
487 if (refresh_golden_frame) {
488 swap_frame_buffer(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
489 &denoiser->running_avg_y[INTRA_FRAME + shift]);
490 }
491 if (refresh_last_frame) {
492 swap_frame_buffer(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
493 &denoiser->running_avg_y[INTRA_FRAME + shift]);
494 }
495 }
496 }
497
vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT * ctx)498 void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) {
499 ctx->zeromv_sse = UINT_MAX;
500 ctx->newmv_sse = UINT_MAX;
501 ctx->zeromv_lastref_sse = UINT_MAX;
502 ctx->best_sse_mv.as_int = 0;
503 }
504
vp9_denoiser_update_frame_stats(MODE_INFO * mi,unsigned int sse,PREDICTION_MODE mode,PICK_MODE_CONTEXT * ctx)505 void vp9_denoiser_update_frame_stats(MODE_INFO *mi, unsigned int sse,
506 PREDICTION_MODE mode,
507 PICK_MODE_CONTEXT *ctx) {
508 if (mi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) {
509 ctx->zeromv_sse = sse;
510 ctx->best_zeromv_reference_frame = mi->ref_frame[0];
511 if (mi->ref_frame[0] == LAST_FRAME) ctx->zeromv_lastref_sse = sse;
512 }
513
514 if (mi->mv[0].as_int != 0 && sse < ctx->newmv_sse) {
515 ctx->newmv_sse = sse;
516 ctx->best_sse_inter_mode = mode;
517 ctx->best_sse_mv = mi->mv[0];
518 ctx->best_reference_frame = mi->ref_frame[0];
519 }
520 }
521
vp9_denoiser_realloc_svc_helper(VP9_COMMON * cm,VP9_DENOISER * denoiser,int fb_idx)522 static int vp9_denoiser_realloc_svc_helper(VP9_COMMON *cm,
523 VP9_DENOISER *denoiser, int fb_idx) {
524 int fail = 0;
525 if (denoiser->running_avg_y[fb_idx].buffer_alloc == NULL) {
526 fail =
527 vpx_alloc_frame_buffer(&denoiser->running_avg_y[fb_idx], cm->width,
528 cm->height, cm->subsampling_x, cm->subsampling_y,
529 #if CONFIG_VP9_HIGHBITDEPTH
530 cm->use_highbitdepth,
531 #endif
532 VP9_ENC_BORDER_IN_PIXELS, 0);
533 if (fail) {
534 vp9_denoiser_free(denoiser);
535 return 1;
536 }
537 }
538 return 0;
539 }
540
vp9_denoiser_realloc_svc(VP9_COMMON * cm,VP9_DENOISER * denoiser,int svc_buf_shift,int refresh_alt,int refresh_gld,int refresh_lst,int alt_fb_idx,int gld_fb_idx,int lst_fb_idx)541 int vp9_denoiser_realloc_svc(VP9_COMMON *cm, VP9_DENOISER *denoiser,
542 int svc_buf_shift, int refresh_alt,
543 int refresh_gld, int refresh_lst, int alt_fb_idx,
544 int gld_fb_idx, int lst_fb_idx) {
545 int fail = 0;
546 if (refresh_alt) {
547 // Increase the frame buffer index by 1 to map it to the buffer index in the
548 // denoiser.
549 fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
550 alt_fb_idx + 1 + svc_buf_shift);
551 if (fail) return 1;
552 }
553 if (refresh_gld) {
554 fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
555 gld_fb_idx + 1 + svc_buf_shift);
556 if (fail) return 1;
557 }
558 if (refresh_lst) {
559 fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
560 lst_fb_idx + 1 + svc_buf_shift);
561 if (fail) return 1;
562 }
563 return 0;
564 }
565
vp9_denoiser_alloc(VP9_COMMON * cm,struct SVC * svc,VP9_DENOISER * denoiser,int use_svc,int noise_sen,int width,int height,int ssx,int ssy,int use_highbitdepth,int border)566 int vp9_denoiser_alloc(VP9_COMMON *cm, struct SVC *svc, VP9_DENOISER *denoiser,
567 int use_svc, int noise_sen, int width, int height,
568 int ssx, int ssy,
569 #if CONFIG_VP9_HIGHBITDEPTH
570 int use_highbitdepth,
571 #endif
572 int border) {
573 int i, layer, fail, init_num_ref_frames;
574 const int legacy_byte_alignment = 0;
575 int num_layers = 1;
576 int scaled_width = width;
577 int scaled_height = height;
578 if (use_svc) {
579 LAYER_CONTEXT *lc = &svc->layer_context[svc->spatial_layer_id *
580 svc->number_temporal_layers +
581 svc->temporal_layer_id];
582 get_layer_resolution(width, height, lc->scaling_factor_num,
583 lc->scaling_factor_den, &scaled_width, &scaled_height);
584 // For SVC: only denoise at most 2 spatial (highest) layers.
585 if (noise_sen >= 2)
586 // Denoise from one spatial layer below the top.
587 svc->first_layer_denoise = VPXMAX(svc->number_spatial_layers - 2, 0);
588 else
589 // Only denoise the top spatial layer.
590 svc->first_layer_denoise = VPXMAX(svc->number_spatial_layers - 1, 0);
591 num_layers = svc->number_spatial_layers - svc->first_layer_denoise;
592 }
593 assert(denoiser != NULL);
594 denoiser->num_ref_frames = use_svc ? SVC_REF_FRAMES : NONSVC_REF_FRAMES;
595 init_num_ref_frames = use_svc ? MAX_REF_FRAMES : NONSVC_REF_FRAMES;
596 denoiser->num_layers = num_layers;
597 CHECK_MEM_ERROR(cm, denoiser->running_avg_y,
598 vpx_calloc(denoiser->num_ref_frames * num_layers,
599 sizeof(denoiser->running_avg_y[0])));
600 CHECK_MEM_ERROR(
601 cm, denoiser->mc_running_avg_y,
602 vpx_calloc(num_layers, sizeof(denoiser->mc_running_avg_y[0])));
603
604 for (layer = 0; layer < num_layers; ++layer) {
605 const int denoise_width = (layer == 0) ? width : scaled_width;
606 const int denoise_height = (layer == 0) ? height : scaled_height;
607 for (i = 0; i < init_num_ref_frames; ++i) {
608 fail = vpx_alloc_frame_buffer(
609 &denoiser->running_avg_y[i + denoiser->num_ref_frames * layer],
610 denoise_width, denoise_height, ssx, ssy,
611 #if CONFIG_VP9_HIGHBITDEPTH
612 use_highbitdepth,
613 #endif
614 border, legacy_byte_alignment);
615 if (fail) {
616 vp9_denoiser_free(denoiser);
617 return 1;
618 }
619 #ifdef OUTPUT_YUV_DENOISED
620 make_grayscale(&denoiser->running_avg_y[i]);
621 #endif
622 }
623
624 fail = vpx_alloc_frame_buffer(&denoiser->mc_running_avg_y[layer],
625 denoise_width, denoise_height, ssx, ssy,
626 #if CONFIG_VP9_HIGHBITDEPTH
627 use_highbitdepth,
628 #endif
629 border, legacy_byte_alignment);
630 if (fail) {
631 vp9_denoiser_free(denoiser);
632 return 1;
633 }
634 }
635
636 // denoiser->last_source only used for noise_estimation, so only for top
637 // layer.
638 fail = vpx_alloc_frame_buffer(&denoiser->last_source, width, height, ssx, ssy,
639 #if CONFIG_VP9_HIGHBITDEPTH
640 use_highbitdepth,
641 #endif
642 border, legacy_byte_alignment);
643 if (fail) {
644 vp9_denoiser_free(denoiser);
645 return 1;
646 }
647 #ifdef OUTPUT_YUV_DENOISED
648 make_grayscale(&denoiser->running_avg_y[i]);
649 #endif
650 denoiser->frame_buffer_initialized = 1;
651 denoiser->denoising_level = kDenLow;
652 denoiser->prev_denoising_level = kDenLow;
653 denoiser->reset = 0;
654 return 0;
655 }
656
vp9_denoiser_free(VP9_DENOISER * denoiser)657 void vp9_denoiser_free(VP9_DENOISER *denoiser) {
658 int i;
659 if (denoiser == NULL) {
660 return;
661 }
662 denoiser->frame_buffer_initialized = 0;
663 for (i = 0; i < denoiser->num_ref_frames * denoiser->num_layers; ++i) {
664 vpx_free_frame_buffer(&denoiser->running_avg_y[i]);
665 }
666 vpx_free(denoiser->running_avg_y);
667 denoiser->running_avg_y = NULL;
668
669 for (i = 0; i < denoiser->num_layers; ++i) {
670 vpx_free_frame_buffer(&denoiser->mc_running_avg_y[i]);
671 }
672
673 vpx_free(denoiser->mc_running_avg_y);
674 denoiser->mc_running_avg_y = NULL;
675 vpx_free_frame_buffer(&denoiser->last_source);
676 }
677
vp9_denoiser_set_noise_level(VP9_DENOISER * denoiser,int noise_level)678 void vp9_denoiser_set_noise_level(VP9_DENOISER *denoiser, int noise_level) {
679 denoiser->denoising_level = noise_level;
680 if (denoiser->denoising_level > kDenLowLow &&
681 denoiser->prev_denoising_level == kDenLowLow)
682 denoiser->reset = 1;
683 else
684 denoiser->reset = 0;
685 denoiser->prev_denoising_level = denoiser->denoising_level;
686 }
687
688 // Scale/increase the partition threshold
689 // for denoiser speed-up.
vp9_scale_part_thresh(int64_t threshold,VP9_DENOISER_LEVEL noise_level,int content_state,int temporal_layer_id)690 int64_t vp9_scale_part_thresh(int64_t threshold, VP9_DENOISER_LEVEL noise_level,
691 int content_state, int temporal_layer_id) {
692 if ((content_state == kLowSadLowSumdiff) ||
693 (content_state == kHighSadLowSumdiff) ||
694 (content_state == kLowVarHighSumdiff) || (noise_level == kDenHigh) ||
695 (temporal_layer_id != 0)) {
696 int64_t scaled_thr =
697 (temporal_layer_id < 2) ? (3 * threshold) >> 1 : (7 * threshold) >> 2;
698 return scaled_thr;
699 } else {
700 return (5 * threshold) >> 2;
701 }
702 }
703
704 // Scale/increase the ac skip threshold for
705 // denoiser speed-up.
vp9_scale_acskip_thresh(int64_t threshold,VP9_DENOISER_LEVEL noise_level,int abs_sumdiff,int temporal_layer_id)706 int64_t vp9_scale_acskip_thresh(int64_t threshold,
707 VP9_DENOISER_LEVEL noise_level, int abs_sumdiff,
708 int temporal_layer_id) {
709 if (noise_level >= kDenLow && abs_sumdiff < 5)
710 return threshold *=
711 (noise_level == kDenLow) ? 2 : (temporal_layer_id == 2) ? 10 : 6;
712 else
713 return threshold;
714 }
715
716 #ifdef OUTPUT_YUV_DENOISED
make_grayscale(YV12_BUFFER_CONFIG * yuv)717 static void make_grayscale(YV12_BUFFER_CONFIG *yuv) {
718 int r, c;
719 uint8_t *u = yuv->u_buffer;
720 uint8_t *v = yuv->v_buffer;
721
722 for (r = 0; r < yuv->uv_height; ++r) {
723 for (c = 0; c < yuv->uv_width; ++c) {
724 u[c] = UINT8_MAX / 2;
725 v[c] = UINT8_MAX / 2;
726 }
727 u += yuv->uv_stride;
728 v += yuv->uv_stride;
729 }
730 }
731 #endif
732