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_VP9_ENCODER_VP9_DENOISER_H_
12 #define VPX_VP9_ENCODER_VP9_DENOISER_H_
13 
14 #include "vp9/encoder/vp9_block.h"
15 #include "vp9/encoder/vp9_skin_detection.h"
16 #include "vpx_scale/yv12config.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #define MOTION_MAGNITUDE_THRESHOLD (8 * 3)
23 
24 // Denoiser is used in non svc real-time mode which does not use alt-ref, so no
25 // need to allocate for it, and hence we need MAX_REF_FRAME - 1
26 #define NONSVC_REF_FRAMES MAX_REF_FRAMES - 1
27 
28 // Number of frame buffers when SVC is used. [0] for current denoised buffer and
29 // [1..8] for REF_FRAMES
30 #define SVC_REF_FRAMES 9
31 
32 typedef enum vp9_denoiser_decision {
33   COPY_BLOCK,
34   FILTER_BLOCK,
35   FILTER_ZEROMV_BLOCK
36 } VP9_DENOISER_DECISION;
37 
38 typedef enum vp9_denoiser_level {
39   kDenLowLow,
40   kDenLow,
41   kDenMedium,
42   kDenHigh
43 } VP9_DENOISER_LEVEL;
44 
45 typedef struct vp9_denoiser {
46   YV12_BUFFER_CONFIG *running_avg_y;
47   YV12_BUFFER_CONFIG *mc_running_avg_y;
48   YV12_BUFFER_CONFIG last_source;
49   int frame_buffer_initialized;
50   int reset;
51   int num_ref_frames;
52   int num_layers;
53   unsigned int current_denoiser_frame;
54   VP9_DENOISER_LEVEL denoising_level;
55   VP9_DENOISER_LEVEL prev_denoising_level;
56 } VP9_DENOISER;
57 
58 typedef struct {
59   int64_t zero_last_cost_orig;
60   int *ref_frame_cost;
61   int_mv (*frame_mv)[MAX_REF_FRAMES];
62   int reuse_inter_pred;
63   TX_SIZE best_tx_size;
64   PREDICTION_MODE best_mode;
65   MV_REFERENCE_FRAME best_ref_frame;
66   INTERP_FILTER best_pred_filter;
67   uint8_t best_mode_skip_txfm;
68 } VP9_PICKMODE_CTX_DEN;
69 
70 struct VP9_COMP;
71 struct SVC;
72 
73 void vp9_denoiser_update_frame_info(
74     VP9_DENOISER *denoiser, YV12_BUFFER_CONFIG src, struct SVC *svc,
75     FRAME_TYPE frame_type, int refresh_alt_ref_frame, int refresh_golden_frame,
76     int refresh_last_frame, int alt_fb_idx, int gld_fb_idx, int lst_fb_idx,
77     int resized, int svc_refresh_denoiser_buffers, int second_spatial_layer);
78 
79 void vp9_denoiser_denoise(struct VP9_COMP *cpi, MACROBLOCK *mb, int mi_row,
80                           int mi_col, BLOCK_SIZE bs, PICK_MODE_CONTEXT *ctx,
81                           VP9_DENOISER_DECISION *denoiser_decision,
82                           int use_gf_temporal_ref);
83 
84 void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx);
85 
86 void vp9_denoiser_update_frame_stats(MODE_INFO *mi, unsigned int sse,
87                                      PREDICTION_MODE mode,
88                                      PICK_MODE_CONTEXT *ctx);
89 
90 int vp9_denoiser_realloc_svc(VP9_COMMON *cm, VP9_DENOISER *denoiser,
91                              struct SVC *svc, int svc_buf_shift,
92                              int refresh_alt, int refresh_gld, int refresh_lst,
93                              int alt_fb_idx, int gld_fb_idx, int lst_fb_idx);
94 
95 int vp9_denoiser_alloc(VP9_COMMON *cm, struct SVC *svc, VP9_DENOISER *denoiser,
96                        int use_svc, int noise_sen, int width, int height,
97                        int ssx, int ssy,
98 #if CONFIG_VP9_HIGHBITDEPTH
99                        int use_highbitdepth,
100 #endif
101                        int border);
102 
103 #if CONFIG_VP9_TEMPORAL_DENOISING
104 // This function is used by both c and sse2 denoiser implementations.
105 // Define it as a static function within the scope where vp9_denoiser.h
106 // is referenced.
total_adj_strong_thresh(BLOCK_SIZE bs,int increase_denoising)107 static INLINE int total_adj_strong_thresh(BLOCK_SIZE bs,
108                                           int increase_denoising) {
109   return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 3 : 2);
110 }
111 #endif
112 
113 void vp9_denoiser_free(VP9_DENOISER *denoiser);
114 
115 void vp9_denoiser_set_noise_level(struct VP9_COMP *const cpi, int noise_level);
116 
117 void vp9_denoiser_reset_on_first_frame(struct VP9_COMP *const cpi);
118 
119 int64_t vp9_scale_part_thresh(int64_t threshold, VP9_DENOISER_LEVEL noise_level,
120                               int content_state, int temporal_layer_id);
121 
122 int64_t vp9_scale_acskip_thresh(int64_t threshold,
123                                 VP9_DENOISER_LEVEL noise_level, int abs_sumdiff,
124                                 int temporal_layer_id);
125 
126 void vp9_denoiser_update_ref_frame(struct VP9_COMP *const cpi);
127 
128 #ifdef __cplusplus
129 }  // extern "C"
130 #endif
131 
132 #endif  // VPX_VP9_ENCODER_VP9_DENOISER_H_
133