• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020, 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 #ifndef AOM_AV1_ENCODER_AV1_TEMPORAL_DENOISER_H_
13 #define AOM_AV1_ENCODER_AV1_TEMPORAL_DENOISER_H_
14 
15 #include "av1/encoder/block.h"
16 #include "aom_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 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 av1_denoiser_decision {
33   COPY_BLOCK,
34   FILTER_BLOCK,
35   FILTER_ZEROMV_BLOCK
36 } AV1_DENOISER_DECISION;
37 
38 typedef enum av1_denoiser_level {
39   kDenLowLow,
40   kDenLow,
41   kDenMedium,
42   kDenHigh
43 } AV1_DENOISER_LEVEL;
44 
45 typedef struct av1_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   AV1_DENOISER_LEVEL denoising_level;
55   AV1_DENOISER_LEVEL prev_denoising_level;
56 } AV1_DENOISER;
57 
58 typedef struct {
59   int64_t zero_last_cost_orig;
60   unsigned int *ref_frame_cost;
61   int_mv (*frame_mv)[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   int_interpfilters best_pred_filter;
67   uint8_t best_mode_skip_txfm;
68 } AV1_PICKMODE_CTX_DEN;
69 
70 struct AV1_COMP;
71 struct SVC;
72 
73 void av1_denoiser_update_frame_info(
74     AV1_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 av1_denoiser_denoise(struct AV1_COMP *cpi, MACROBLOCK *mb, int mi_row,
80                           int mi_col, BLOCK_SIZE bs, PICK_MODE_CONTEXT *ctx,
81                           AV1_DENOISER_DECISION *denoiser_decision,
82                           int use_gf_temporal_ref);
83 
84 void av1_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx);
85 
86 void av1_denoiser_update_frame_stats(MB_MODE_INFO *mi, int64_t sse,
87                                      PREDICTION_MODE mode,
88                                      PICK_MODE_CONTEXT *ctx);
89 
90 int av1_denoiser_realloc_svc(AV1_COMMON *cm, AV1_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 av1_denoiser_alloc(AV1_COMMON *cm, struct SVC *svc, AV1_DENOISER *denoiser,
96                        int use_svc, int noise_sen, int width, int height,
97                        int ssx, int ssy, int use_highbitdepth, int border);
98 
99 #if CONFIG_AV1_TEMPORAL_DENOISING
100 // This function is used by both c and sse2 denoiser implementations.
101 // Define it as a static function within the scope where av1_denoiser.h
102 // is referenced.
total_adj_strong_thresh(BLOCK_SIZE bs,int increase_denoising)103 static INLINE int total_adj_strong_thresh(BLOCK_SIZE bs,
104                                           int increase_denoising) {
105   return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 3 : 2);
106 }
107 #endif
108 
109 void av1_denoiser_free(AV1_DENOISER *denoiser);
110 
111 void av1_denoiser_set_noise_level(struct AV1_COMP *const cpi, int noise_level);
112 
113 void av1_denoiser_reset_on_first_frame(struct AV1_COMP *const cpi);
114 
115 int64_t av1_scale_part_thresh(int64_t threshold, AV1_DENOISER_LEVEL noise_level,
116                               CONTENT_STATE_SB content_state,
117                               int temporal_layer_id);
118 
119 int64_t av1_scale_acskip_thresh(int64_t threshold,
120                                 AV1_DENOISER_LEVEL noise_level, int abs_sumdiff,
121                                 int temporal_layer_id);
122 
123 void av1_denoiser_update_ref_frame(struct AV1_COMP *const cpi);
124 
125 void aom_write_yuv_frame(FILE *yuv_file, YV12_BUFFER_CONFIG *s);
126 
127 #ifdef __cplusplus
128 }  // extern "C"
129 #endif
130 
131 #endif  // AOM_AV1_ENCODER_AV1_TEMPORAL_DENOISER_H_
132