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 #ifndef VPX_VP8_ENCODER_DENOISING_H_
12 #define VPX_VP8_ENCODER_DENOISING_H_
13 
14 #include "block.h"
15 #include "vp8/common/loopfilter.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #define SUM_DIFF_THRESHOLD 512
22 #define SUM_DIFF_THRESHOLD_HIGH 600
23 #define MOTION_MAGNITUDE_THRESHOLD (8 * 3)
24 
25 #define SUM_DIFF_THRESHOLD_UV (96)  // (8 * 8 * 1.5)
26 #define SUM_DIFF_THRESHOLD_HIGH_UV (8 * 8 * 2)
27 #define SUM_DIFF_FROM_AVG_THRESH_UV (8 * 8 * 8)
28 #define MOTION_MAGNITUDE_THRESHOLD_UV (8 * 3)
29 
30 #define MAX_GF_ARF_DENOISE_RANGE (8)
31 
32 enum vp8_denoiser_decision { COPY_BLOCK, FILTER_BLOCK };
33 
34 enum vp8_denoiser_filter_state { kNoFilter, kFilterZeroMV, kFilterNonZeroMV };
35 
36 enum vp8_denoiser_mode {
37   kDenoiserOff,
38   kDenoiserOnYOnly,
39   kDenoiserOnYUV,
40   kDenoiserOnYUVAggressive,
41   kDenoiserOnAdaptive
42 };
43 
44 typedef struct {
45   // Scale factor on sse threshold above which no denoising is done.
46   unsigned int scale_sse_thresh;
47   // Scale factor on motion magnitude threshold above which no
48   // denoising is done.
49   unsigned int scale_motion_thresh;
50   // Scale factor on motion magnitude below which we increase the strength of
51   // the temporal filter (in function vp8_denoiser_filter).
52   unsigned int scale_increase_filter;
53   // Scale factor to bias to ZEROMV for denoising.
54   unsigned int denoise_mv_bias;
55   // Scale factor to bias to ZEROMV for coding mode selection.
56   unsigned int pickmode_mv_bias;
57   // Quantizer threshold below which we use the segmentation map to switch off
58   // loop filter for blocks that have been coded as ZEROMV-LAST a certain number
59   // (consec_zerolast) of consecutive frames. Note that the delta-QP is set to
60   // 0 when segmentation map is used for shutting off loop filter.
61   unsigned int qp_thresh;
62   // Threshold for number of consecutive frames for blocks coded as ZEROMV-LAST.
63   unsigned int consec_zerolast;
64   // Threshold for amount of spatial blur on Y channel. 0 means no spatial blur.
65   unsigned int spatial_blur;
66 } denoise_params;
67 
68 typedef struct vp8_denoiser {
69   YV12_BUFFER_CONFIG yv12_running_avg[MAX_REF_FRAMES];
70   YV12_BUFFER_CONFIG yv12_mc_running_avg;
71   // TODO(marpan): Should remove yv12_last_source and use vp8_lookahead_peak.
72   YV12_BUFFER_CONFIG yv12_last_source;
73   unsigned char *denoise_state;
74   int num_mb_cols;
75   int denoiser_mode;
76   int threshold_aggressive_mode;
77   int nmse_source_diff;
78   int nmse_source_diff_count;
79   int qp_avg;
80   int qp_threshold_up;
81   int qp_threshold_down;
82   int bitrate_threshold;
83   denoise_params denoise_pars;
84 } VP8_DENOISER;
85 
86 int vp8_denoiser_allocate(VP8_DENOISER *denoiser, int width, int height,
87                           int num_mb_rows, int num_mb_cols, int mode);
88 
89 void vp8_denoiser_free(VP8_DENOISER *denoiser);
90 
91 void vp8_denoiser_set_parameters(VP8_DENOISER *denoiser, int mode);
92 
93 void vp8_denoiser_denoise_mb(VP8_DENOISER *denoiser, MACROBLOCK *x,
94                              unsigned int best_sse, unsigned int zero_mv_sse,
95                              int recon_yoffset, int recon_uvoffset,
96                              loop_filter_info_n *lfi_n, int mb_row, int mb_col,
97                              int block_index, int consec_zero_last);
98 
99 #ifdef __cplusplus
100 }  // extern "C"
101 #endif
102 
103 #endif  // VPX_VP8_ENCODER_DENOISING_H_
104