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 // OUTPUT_YUV_DENOISED
25
26 #ifdef OUTPUT_YUV_DENOISED
27 static void make_grayscale(YV12_BUFFER_CONFIG *yuv);
28 #endif
29
absdiff_thresh(BLOCK_SIZE bs,int increase_denoising)30 static int absdiff_thresh(BLOCK_SIZE bs, int increase_denoising) {
31 (void)bs;
32 return 3 + (increase_denoising ? 1 : 0);
33 }
34
delta_thresh(BLOCK_SIZE bs,int increase_denoising)35 static int delta_thresh(BLOCK_SIZE bs, int increase_denoising) {
36 (void)bs;
37 (void)increase_denoising;
38 return 4;
39 }
40
noise_motion_thresh(BLOCK_SIZE bs,int increase_denoising)41 static int noise_motion_thresh(BLOCK_SIZE bs, int increase_denoising) {
42 (void)bs;
43 (void)increase_denoising;
44 return 625;
45 }
46
sse_thresh(BLOCK_SIZE bs,int increase_denoising)47 static unsigned int sse_thresh(BLOCK_SIZE bs, int increase_denoising) {
48 return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 80 : 40);
49 }
50
sse_diff_thresh(BLOCK_SIZE bs,int increase_denoising,int motion_magnitude)51 static int sse_diff_thresh(BLOCK_SIZE bs, int increase_denoising,
52 int motion_magnitude) {
53 if (motion_magnitude > noise_motion_thresh(bs, increase_denoising)) {
54 if (increase_denoising)
55 return (1 << num_pels_log2_lookup[bs]) << 2;
56 else
57 return 0;
58 } else {
59 return (1 << num_pels_log2_lookup[bs]) << 4;
60 }
61 }
62
total_adj_weak_thresh(BLOCK_SIZE bs,int increase_denoising)63 static int total_adj_weak_thresh(BLOCK_SIZE bs, int increase_denoising) {
64 return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 3 : 2);
65 }
66
67 // TODO(jackychen): If increase_denoising is enabled in the future,
68 // we might need to update the code for calculating 'total_adj' in
69 // 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)70 int vp9_denoiser_filter_c(const uint8_t *sig, int sig_stride,
71 const uint8_t *mc_avg, int mc_avg_stride,
72 uint8_t *avg, int avg_stride, int increase_denoising,
73 BLOCK_SIZE bs, int motion_magnitude) {
74 int r, c;
75 const uint8_t *sig_start = sig;
76 const uint8_t *mc_avg_start = mc_avg;
77 uint8_t *avg_start = avg;
78 int diff, adj, absdiff, delta;
79 int adj_val[] = { 3, 4, 6 };
80 int total_adj = 0;
81 int shift_inc = 1;
82
83 // If motion_magnitude is small, making the denoiser more aggressive by
84 // increasing the adjustment for each level. Add another increment for
85 // blocks that are labeled for increase denoising.
86 if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) {
87 if (increase_denoising) {
88 shift_inc = 2;
89 }
90 adj_val[0] += shift_inc;
91 adj_val[1] += shift_inc;
92 adj_val[2] += shift_inc;
93 }
94
95 // First attempt to apply a strong temporal denoising filter.
96 for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
97 for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
98 diff = mc_avg[c] - sig[c];
99 absdiff = abs(diff);
100
101 if (absdiff <= absdiff_thresh(bs, increase_denoising)) {
102 avg[c] = mc_avg[c];
103 total_adj += diff;
104 } else {
105 switch (absdiff) {
106 case 4:
107 case 5:
108 case 6:
109 case 7: adj = adj_val[0]; break;
110 case 8:
111 case 9:
112 case 10:
113 case 11:
114 case 12:
115 case 13:
116 case 14:
117 case 15: adj = adj_val[1]; break;
118 default: adj = adj_val[2];
119 }
120 if (diff > 0) {
121 avg[c] = VPXMIN(UINT8_MAX, sig[c] + adj);
122 total_adj += adj;
123 } else {
124 avg[c] = VPXMAX(0, sig[c] - adj);
125 total_adj -= adj;
126 }
127 }
128 }
129 sig += sig_stride;
130 avg += avg_stride;
131 mc_avg += mc_avg_stride;
132 }
133
134 // If the strong filter did not modify the signal too much, we're all set.
135 if (abs(total_adj) <= total_adj_strong_thresh(bs, increase_denoising)) {
136 return FILTER_BLOCK;
137 }
138
139 // Otherwise, we try to dampen the filter if the delta is not too high.
140 delta = ((abs(total_adj) - total_adj_strong_thresh(bs, increase_denoising)) >>
141 num_pels_log2_lookup[bs]) +
142 1;
143
144 if (delta >= delta_thresh(bs, increase_denoising)) {
145 return COPY_BLOCK;
146 }
147
148 mc_avg = mc_avg_start;
149 avg = avg_start;
150 sig = sig_start;
151 for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
152 for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
153 diff = mc_avg[c] - sig[c];
154 adj = abs(diff);
155 if (adj > delta) {
156 adj = delta;
157 }
158 if (diff > 0) {
159 // Diff positive means we made positive adjustment above
160 // (in first try/attempt), so now make negative adjustment to bring
161 // denoised signal down.
162 avg[c] = VPXMAX(0, avg[c] - adj);
163 total_adj -= adj;
164 } else {
165 // Diff negative means we made negative adjustment above
166 // (in first try/attempt), so now make positive adjustment to bring
167 // denoised signal up.
168 avg[c] = VPXMIN(UINT8_MAX, avg[c] + adj);
169 total_adj += adj;
170 }
171 }
172 sig += sig_stride;
173 avg += avg_stride;
174 mc_avg += mc_avg_stride;
175 }
176
177 // We can use the filter if it has been sufficiently dampened
178 if (abs(total_adj) <= total_adj_weak_thresh(bs, increase_denoising)) {
179 return FILTER_BLOCK;
180 }
181 return COPY_BLOCK;
182 }
183
block_start(uint8_t * framebuf,int stride,int mi_row,int mi_col)184 static uint8_t *block_start(uint8_t *framebuf, int stride, int mi_row,
185 int mi_col) {
186 return framebuf + (stride * mi_row << 3) + (mi_col << 3);
187 }
188
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)189 static VP9_DENOISER_DECISION perform_motion_compensation(
190 VP9_COMMON *const cm, VP9_DENOISER *denoiser, MACROBLOCK *mb, BLOCK_SIZE bs,
191 int increase_denoising, int mi_row, int mi_col, PICK_MODE_CONTEXT *ctx,
192 int motion_magnitude, int is_skin, int *zeromv_filter, int consec_zeromv,
193 int num_spatial_layers, int width) {
194 const int sse_diff = (ctx->newmv_sse == UINT_MAX)
195 ? 0
196 : ((int)ctx->zeromv_sse - (int)ctx->newmv_sse);
197 MV_REFERENCE_FRAME frame;
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
206 frame = ctx->best_reference_frame;
207 saved_mi = *mi;
208
209 if (is_skin && (motion_magnitude > 0 || consec_zeromv < 4)) return COPY_BLOCK;
210
211 // Avoid denoising small blocks. When noise > kDenLow or frame width > 480,
212 // denoise 16x16 blocks.
213 if (bs == BLOCK_8X8 || bs == BLOCK_8X16 || bs == BLOCK_16X8 ||
214 (bs == BLOCK_16X16 && width > 480 &&
215 denoiser->denoising_level <= kDenLow))
216 return COPY_BLOCK;
217
218 // If the best reference frame uses inter-prediction and there is enough of a
219 // difference in sum-squared-error, use it.
220 if (frame != INTRA_FRAME &&
221 (frame != GOLDEN_FRAME || num_spatial_layers == 1) &&
222 sse_diff > sse_diff_thresh(bs, increase_denoising, motion_magnitude)) {
223 mi->ref_frame[0] = ctx->best_reference_frame;
224 mi->mode = ctx->best_sse_inter_mode;
225 mi->mv[0] = ctx->best_sse_mv;
226 } else {
227 // Otherwise, use the zero reference frame.
228 frame = ctx->best_zeromv_reference_frame;
229 ctx->newmv_sse = ctx->zeromv_sse;
230 // Bias to last reference.
231 if (num_spatial_layers > 1 ||
232 (frame != LAST_FRAME &&
233 ((ctx->zeromv_lastref_sse<(5 * ctx->zeromv_sse)>> 2) ||
234 denoiser->denoising_level >= kDenHigh))) {
235 frame = LAST_FRAME;
236 ctx->newmv_sse = ctx->zeromv_lastref_sse;
237 }
238 mi->ref_frame[0] = frame;
239 mi->mode = ZEROMV;
240 mi->mv[0].as_int = 0;
241 ctx->best_sse_inter_mode = ZEROMV;
242 ctx->best_sse_mv.as_int = 0;
243 *zeromv_filter = 1;
244 if (denoiser->denoising_level > kDenMedium) {
245 motion_magnitude = 0;
246 }
247 }
248
249 if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) {
250 // Restore everything to its original state
251 *mi = saved_mi;
252 return COPY_BLOCK;
253 }
254 if (motion_magnitude > (noise_motion_thresh(bs, increase_denoising) << 3)) {
255 // Restore everything to its original state
256 *mi = saved_mi;
257 return COPY_BLOCK;
258 }
259
260 // We will restore these after motion compensation.
261 for (i = 0; i < MAX_MB_PLANE; ++i) {
262 saved_pre[i] = filter_mbd->plane[i].pre[0];
263 saved_dst[i] = filter_mbd->plane[i].dst;
264 }
265 saved_block_refs[0] = filter_mbd->block_refs[0];
266
267 // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
268 // struct.
269 filter_mbd->plane[0].pre[0].buf =
270 block_start(denoiser->running_avg_y[frame].y_buffer,
271 denoiser->running_avg_y[frame].y_stride, mi_row, mi_col);
272 filter_mbd->plane[0].pre[0].stride = denoiser->running_avg_y[frame].y_stride;
273 filter_mbd->plane[1].pre[0].buf =
274 block_start(denoiser->running_avg_y[frame].u_buffer,
275 denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
276 filter_mbd->plane[1].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
277 filter_mbd->plane[2].pre[0].buf =
278 block_start(denoiser->running_avg_y[frame].v_buffer,
279 denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
280 filter_mbd->plane[2].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
281
282 filter_mbd->plane[0].dst.buf =
283 block_start(denoiser->mc_running_avg_y.y_buffer,
284 denoiser->mc_running_avg_y.y_stride, mi_row, mi_col);
285 filter_mbd->plane[0].dst.stride = denoiser->mc_running_avg_y.y_stride;
286 filter_mbd->plane[1].dst.buf =
287 block_start(denoiser->mc_running_avg_y.u_buffer,
288 denoiser->mc_running_avg_y.uv_stride, mi_row, mi_col);
289 filter_mbd->plane[1].dst.stride = denoiser->mc_running_avg_y.uv_stride;
290 filter_mbd->plane[2].dst.buf =
291 block_start(denoiser->mc_running_avg_y.v_buffer,
292 denoiser->mc_running_avg_y.uv_stride, mi_row, mi_col);
293 filter_mbd->plane[2].dst.stride = denoiser->mc_running_avg_y.uv_stride;
294
295 set_ref_ptrs(cm, filter_mbd, frame, NONE);
296 vp9_build_inter_predictors_sby(filter_mbd, mi_row, mi_col, bs);
297
298 // Restore everything to its original state
299 *mi = saved_mi;
300 filter_mbd->block_refs[0] = saved_block_refs[0];
301 for (i = 0; i < MAX_MB_PLANE; ++i) {
302 filter_mbd->plane[i].pre[0] = saved_pre[i];
303 filter_mbd->plane[i].dst = saved_dst[i];
304 }
305
306 return FILTER_BLOCK;
307 }
308
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)309 void vp9_denoiser_denoise(VP9_COMP *cpi, MACROBLOCK *mb, int mi_row, int mi_col,
310 BLOCK_SIZE bs, PICK_MODE_CONTEXT *ctx,
311 VP9_DENOISER_DECISION *denoiser_decision) {
312 int mv_col, mv_row;
313 int motion_magnitude = 0;
314 int zeromv_filter = 0;
315 VP9_DENOISER *denoiser = &cpi->denoiser;
316 VP9_DENOISER_DECISION decision = COPY_BLOCK;
317 YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME];
318 YV12_BUFFER_CONFIG mc_avg = denoiser->mc_running_avg_y;
319 uint8_t *avg_start = block_start(avg.y_buffer, avg.y_stride, mi_row, mi_col);
320 uint8_t *mc_avg_start =
321 block_start(mc_avg.y_buffer, mc_avg.y_stride, mi_row, mi_col);
322 struct buf_2d src = mb->plane[0].src;
323 int is_skin = 0;
324 int increase_denoising = 0;
325 int consec_zeromv = 0;
326 mv_col = ctx->best_sse_mv.as_mv.col;
327 mv_row = ctx->best_sse_mv.as_mv.row;
328 motion_magnitude = mv_row * mv_row + mv_col * mv_col;
329
330 if (cpi->use_skin_detection && bs <= BLOCK_32X32 &&
331 denoiser->denoising_level < kDenHigh) {
332 int motion_level = (motion_magnitude < 16) ? 0 : 1;
333 // If motion for current block is small/zero, compute consec_zeromv for
334 // skin detection (early exit in skin detection is done for large
335 // consec_zeromv when current block has small/zero motion).
336 consec_zeromv = 0;
337 if (motion_level == 0) {
338 VP9_COMMON *const cm = &cpi->common;
339 int j, i;
340 // Loop through the 8x8 sub-blocks.
341 const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
342 const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
343 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
344 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
345 const int block_index = mi_row * cm->mi_cols + mi_col;
346 consec_zeromv = 100;
347 for (i = 0; i < ymis; i++) {
348 for (j = 0; j < xmis; j++) {
349 int bl_index = block_index + i * cm->mi_cols + j;
350 consec_zeromv = VPXMIN(cpi->consec_zero_mv[bl_index], consec_zeromv);
351 // No need to keep checking 8x8 blocks if any of the sub-blocks
352 // has small consec_zeromv (since threshold for no_skin based on
353 // zero/small motion in skin detection is high, i.e, > 4).
354 if (consec_zeromv < 4) {
355 i = ymis;
356 j = xmis;
357 }
358 }
359 }
360 }
361 // TODO(marpan): Compute skin detection over sub-blocks.
362 is_skin = vp9_compute_skin_block(
363 mb->plane[0].src.buf, mb->plane[1].src.buf, mb->plane[2].src.buf,
364 mb->plane[0].src.stride, mb->plane[1].src.stride, bs, consec_zeromv,
365 motion_level);
366 }
367 if (!is_skin && denoiser->denoising_level == kDenHigh) increase_denoising = 1;
368
369 // TODO(marpan): There is an issue with denoising for speed 5,
370 // due to the partitioning scheme based on pickmode.
371 // Remove this speed constraint when issue is resolved.
372 if (denoiser->denoising_level >= kDenLow && cpi->oxcf.speed > 5)
373 decision = perform_motion_compensation(
374 &cpi->common, denoiser, mb, bs, increase_denoising, mi_row, mi_col, ctx,
375 motion_magnitude, is_skin, &zeromv_filter, consec_zeromv,
376 cpi->svc.number_spatial_layers, cpi->Source->y_width);
377
378 if (decision == FILTER_BLOCK) {
379 decision = vp9_denoiser_filter(src.buf, src.stride, mc_avg_start,
380 mc_avg.y_stride, avg_start, avg.y_stride,
381 increase_denoising, bs, motion_magnitude);
382 }
383
384 if (decision == FILTER_BLOCK) {
385 vpx_convolve_copy(avg_start, avg.y_stride, src.buf, src.stride, NULL, 0,
386 NULL, 0, num_4x4_blocks_wide_lookup[bs] << 2,
387 num_4x4_blocks_high_lookup[bs] << 2);
388 } else { // COPY_BLOCK
389 vpx_convolve_copy(src.buf, src.stride, avg_start, avg.y_stride, NULL, 0,
390 NULL, 0, num_4x4_blocks_wide_lookup[bs] << 2,
391 num_4x4_blocks_high_lookup[bs] << 2);
392 }
393 *denoiser_decision = decision;
394 if (decision == FILTER_BLOCK && zeromv_filter == 1)
395 *denoiser_decision = FILTER_ZEROMV_BLOCK;
396 }
397
copy_frame(YV12_BUFFER_CONFIG * const dest,const YV12_BUFFER_CONFIG * const src)398 static void copy_frame(YV12_BUFFER_CONFIG *const dest,
399 const YV12_BUFFER_CONFIG *const src) {
400 int r;
401 const uint8_t *srcbuf = src->y_buffer;
402 uint8_t *destbuf = dest->y_buffer;
403
404 assert(dest->y_width == src->y_width);
405 assert(dest->y_height == src->y_height);
406
407 for (r = 0; r < dest->y_height; ++r) {
408 memcpy(destbuf, srcbuf, dest->y_width);
409 destbuf += dest->y_stride;
410 srcbuf += src->y_stride;
411 }
412 }
413
swap_frame_buffer(YV12_BUFFER_CONFIG * const dest,YV12_BUFFER_CONFIG * const src)414 static void swap_frame_buffer(YV12_BUFFER_CONFIG *const dest,
415 YV12_BUFFER_CONFIG *const src) {
416 uint8_t *tmp_buf = dest->y_buffer;
417 assert(dest->y_width == src->y_width);
418 assert(dest->y_height == src->y_height);
419 dest->y_buffer = src->y_buffer;
420 src->y_buffer = tmp_buf;
421 }
422
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 resized,int svc_base_is_key)423 void vp9_denoiser_update_frame_info(
424 VP9_DENOISER *denoiser, YV12_BUFFER_CONFIG src, FRAME_TYPE frame_type,
425 int refresh_alt_ref_frame, int refresh_golden_frame, int refresh_last_frame,
426 int resized, int svc_base_is_key) {
427 // Copy source into denoised reference buffers on KEY_FRAME or
428 // if the just encoded frame was resized. For SVC, copy source if the base
429 // spatial layer was key frame.
430 if (frame_type == KEY_FRAME || resized != 0 || denoiser->reset ||
431 svc_base_is_key) {
432 int i;
433 // Start at 1 so as not to overwrite the INTRA_FRAME
434 for (i = 1; i < MAX_REF_FRAMES; ++i)
435 copy_frame(&denoiser->running_avg_y[i], &src);
436 denoiser->reset = 0;
437 return;
438 }
439
440 // If more than one refresh occurs, must copy frame buffer.
441 if ((refresh_alt_ref_frame + refresh_golden_frame + refresh_last_frame) > 1) {
442 if (refresh_alt_ref_frame) {
443 copy_frame(&denoiser->running_avg_y[ALTREF_FRAME],
444 &denoiser->running_avg_y[INTRA_FRAME]);
445 }
446 if (refresh_golden_frame) {
447 copy_frame(&denoiser->running_avg_y[GOLDEN_FRAME],
448 &denoiser->running_avg_y[INTRA_FRAME]);
449 }
450 if (refresh_last_frame) {
451 copy_frame(&denoiser->running_avg_y[LAST_FRAME],
452 &denoiser->running_avg_y[INTRA_FRAME]);
453 }
454 } else {
455 if (refresh_alt_ref_frame) {
456 swap_frame_buffer(&denoiser->running_avg_y[ALTREF_FRAME],
457 &denoiser->running_avg_y[INTRA_FRAME]);
458 }
459 if (refresh_golden_frame) {
460 swap_frame_buffer(&denoiser->running_avg_y[GOLDEN_FRAME],
461 &denoiser->running_avg_y[INTRA_FRAME]);
462 }
463 if (refresh_last_frame) {
464 swap_frame_buffer(&denoiser->running_avg_y[LAST_FRAME],
465 &denoiser->running_avg_y[INTRA_FRAME]);
466 }
467 }
468 }
469
vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT * ctx)470 void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) {
471 ctx->zeromv_sse = UINT_MAX;
472 ctx->newmv_sse = UINT_MAX;
473 ctx->zeromv_lastref_sse = UINT_MAX;
474 ctx->best_sse_mv.as_int = 0;
475 }
476
vp9_denoiser_update_frame_stats(MODE_INFO * mi,unsigned int sse,PREDICTION_MODE mode,PICK_MODE_CONTEXT * ctx)477 void vp9_denoiser_update_frame_stats(MODE_INFO *mi, unsigned int sse,
478 PREDICTION_MODE mode,
479 PICK_MODE_CONTEXT *ctx) {
480 if (mi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) {
481 ctx->zeromv_sse = sse;
482 ctx->best_zeromv_reference_frame = mi->ref_frame[0];
483 if (mi->ref_frame[0] == LAST_FRAME) ctx->zeromv_lastref_sse = sse;
484 }
485
486 if (mi->mv[0].as_int != 0 && sse < ctx->newmv_sse) {
487 ctx->newmv_sse = sse;
488 ctx->best_sse_inter_mode = mode;
489 ctx->best_sse_mv = mi->mv[0];
490 ctx->best_reference_frame = mi->ref_frame[0];
491 }
492 }
493
vp9_denoiser_alloc(VP9_DENOISER * denoiser,int width,int height,int ssx,int ssy,int use_highbitdepth,int border)494 int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height, int ssx,
495 int ssy,
496 #if CONFIG_VP9_HIGHBITDEPTH
497 int use_highbitdepth,
498 #endif
499 int border) {
500 int i, fail;
501 const int legacy_byte_alignment = 0;
502 assert(denoiser != NULL);
503
504 for (i = 0; i < MAX_REF_FRAMES; ++i) {
505 fail = vpx_alloc_frame_buffer(&denoiser->running_avg_y[i], width, height,
506 ssx, ssy,
507 #if CONFIG_VP9_HIGHBITDEPTH
508 use_highbitdepth,
509 #endif
510 border, legacy_byte_alignment);
511 if (fail) {
512 vp9_denoiser_free(denoiser);
513 return 1;
514 }
515 #ifdef OUTPUT_YUV_DENOISED
516 make_grayscale(&denoiser->running_avg_y[i]);
517 #endif
518 }
519
520 fail = vpx_alloc_frame_buffer(&denoiser->mc_running_avg_y, width, height, ssx,
521 ssy,
522 #if CONFIG_VP9_HIGHBITDEPTH
523 use_highbitdepth,
524 #endif
525 border, legacy_byte_alignment);
526 if (fail) {
527 vp9_denoiser_free(denoiser);
528 return 1;
529 }
530
531 fail = vpx_alloc_frame_buffer(&denoiser->last_source, width, height, ssx, ssy,
532 #if CONFIG_VP9_HIGHBITDEPTH
533 use_highbitdepth,
534 #endif
535 border, legacy_byte_alignment);
536 if (fail) {
537 vp9_denoiser_free(denoiser);
538 return 1;
539 }
540 #ifdef OUTPUT_YUV_DENOISED
541 make_grayscale(&denoiser->running_avg_y[i]);
542 #endif
543 denoiser->frame_buffer_initialized = 1;
544 denoiser->denoising_level = kDenLow;
545 denoiser->prev_denoising_level = kDenLow;
546 denoiser->reset = 0;
547 return 0;
548 }
549
vp9_denoiser_free(VP9_DENOISER * denoiser)550 void vp9_denoiser_free(VP9_DENOISER *denoiser) {
551 int i;
552 if (denoiser == NULL) {
553 return;
554 }
555 denoiser->frame_buffer_initialized = 0;
556 for (i = 0; i < MAX_REF_FRAMES; ++i) {
557 vpx_free_frame_buffer(&denoiser->running_avg_y[i]);
558 }
559 vpx_free_frame_buffer(&denoiser->mc_running_avg_y);
560 vpx_free_frame_buffer(&denoiser->last_source);
561 }
562
vp9_denoiser_set_noise_level(VP9_DENOISER * denoiser,int noise_level)563 void vp9_denoiser_set_noise_level(VP9_DENOISER *denoiser, int noise_level) {
564 denoiser->denoising_level = noise_level;
565 if (denoiser->denoising_level > kDenLowLow &&
566 denoiser->prev_denoising_level == kDenLowLow)
567 denoiser->reset = 1;
568 else
569 denoiser->reset = 0;
570 denoiser->prev_denoising_level = denoiser->denoising_level;
571 }
572
573 // Scale/increase the partition threshold for denoiser speed-up.
vp9_scale_part_thresh(int64_t threshold,VP9_DENOISER_LEVEL noise_level,int content_state,int temporal_layer_id)574 int64_t vp9_scale_part_thresh(int64_t threshold, VP9_DENOISER_LEVEL noise_level,
575 int content_state, int temporal_layer_id) {
576 if ((content_state == kLowSadLowSumdiff) ||
577 (content_state == kHighSadLowSumdiff) ||
578 (content_state == kLowVarHighSumdiff) || (noise_level == kDenHigh) ||
579 (temporal_layer_id != 0)) {
580 int64_t scaled_thr =
581 (temporal_layer_id < 2) ? (3 * threshold) >> 1 : (7 * threshold) >> 2;
582 return scaled_thr;
583 } else {
584 return (5 * threshold) >> 2;
585 }
586 }
587
588 // Scale/increase the ac skip threshold for denoiser speed-up.
vp9_scale_acskip_thresh(int64_t threshold,VP9_DENOISER_LEVEL noise_level,int abs_sumdiff,int temporal_layer_id)589 int64_t vp9_scale_acskip_thresh(int64_t threshold,
590 VP9_DENOISER_LEVEL noise_level, int abs_sumdiff,
591 int temporal_layer_id) {
592 if (noise_level >= kDenLow && abs_sumdiff < 5)
593 return threshold *=
594 (noise_level == kDenLow) ? 2 : (temporal_layer_id == 2) ? 10 : 6;
595 else
596 return threshold;
597 }
598
599 #ifdef OUTPUT_YUV_DENOISED
make_grayscale(YV12_BUFFER_CONFIG * yuv)600 static void make_grayscale(YV12_BUFFER_CONFIG *yuv) {
601 int r, c;
602 uint8_t *u = yuv->u_buffer;
603 uint8_t *v = yuv->v_buffer;
604
605 for (r = 0; r < yuv->uv_height; ++r) {
606 for (c = 0; c < yuv->uv_width; ++c) {
607 u[c] = UINT8_MAX / 2;
608 v[c] = UINT8_MAX / 2;
609 }
610 u += yuv->uv_stride;
611 v += yuv->uv_stride;
612 }
613 }
614 #endif
615