Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(732)

Side by Side Diff: webrtc/modules/video_processing/video_denoiser.cc

Issue 1822333003: External denoiser based on noise estimation and moving object detection. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Raise noise threshold to 200 to be safe for now. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 #include "webrtc/common_video/libyuv/include/scaler.h" 10 #include "webrtc/common_video/libyuv/include/scaler.h"
11 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" 11 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
12 #include "webrtc/modules/video_processing/video_denoiser.h" 12 #include "webrtc/modules/video_processing/video_denoiser.h"
13 13
14 namespace webrtc { 14 namespace webrtc {
15 15
16 VideoDenoiser::VideoDenoiser(bool runtime_cpu_detection) 16 VideoDenoiser::VideoDenoiser(bool runtime_cpu_detection)
17 : width_(0), 17 : width_(0),
18 height_(0), 18 height_(0),
19 filter_(DenoiserFilter::Create(runtime_cpu_detection)) {} 19 filter_(DenoiserFilter::Create(runtime_cpu_detection, &cpu_type_)),
20 ne_(new NoiseEstimation()) {}
20 21
21 void VideoDenoiser::TrailingReduction(int mb_rows, 22 #if EXPERIMENTAL
22 int mb_cols, 23 // Check the mb position(1: close to the center, 3: close to the border).
23 const uint8_t* y_src, 24 static int PositionCheck(int mb_row, int mb_col, int mb_rows, int mb_cols) {
24 int stride_y, 25 if ((mb_row >= (mb_rows >> 3)) && (mb_row <= (7 * mb_rows >> 3)) &&
25 uint8_t* y_dst) { 26 (mb_col >= (mb_cols >> 3)) && (mb_col <= (7 * mb_cols >> 3)))
26 for (int mb_row = 1; mb_row < mb_rows - 1; ++mb_row) { 27 return 1;
27 for (int mb_col = 1; mb_col < mb_cols - 1; ++mb_col) { 28 else if ((mb_row >= (mb_rows >> 4)) && (mb_row <= (15 * mb_rows >> 4)) &&
28 int mb_index = mb_row * mb_cols + mb_col; 29 (mb_col >= (mb_cols >> 4)) && (mb_col <= (15 * mb_cols >> 4)))
29 uint8_t* mb_dst = y_dst + (mb_row << 4) * stride_y + (mb_col << 4); 30 return 2;
30 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4); 31 else
31 // If the number of denoised neighbors is less than a threshold, 32 return 3;
32 // do NOT denoise for the block. Set different threshold for skin MB.
33 // The change of denoising status will not propagate.
34 if (metrics_[mb_index].is_skin) {
35 // The threshold is high (more strict) for non-skin MB where the
36 // trailing usually happen.
37 if (metrics_[mb_index].denoise &&
38 metrics_[mb_index + 1].denoise + metrics_[mb_index - 1].denoise +
39 metrics_[mb_index + mb_cols].denoise +
40 metrics_[mb_index - mb_cols].denoise <=
41 2) {
42 metrics_[mb_index].denoise = 0;
43 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
44 }
45 } else if (metrics_[mb_index].denoise &&
46 metrics_[mb_index + 1].denoise +
47 metrics_[mb_index - 1].denoise +
48 metrics_[mb_index + mb_cols + 1].denoise +
49 metrics_[mb_index + mb_cols - 1].denoise +
50 metrics_[mb_index - mb_cols + 1].denoise +
51 metrics_[mb_index - mb_cols - 1].denoise +
52 metrics_[mb_index + mb_cols].denoise +
53 metrics_[mb_index - mb_cols].denoise <=
54 7) {
55 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
56 }
57 }
58 }
59 } 33 }
60 34
35 static bool TrailingBlock(const std::unique_ptr<uint8_t[]>& d_status,
36 int mb_row,
37 int mb_col,
38 int mb_rows,
39 int mb_cols) {
40 int mb_index = mb_row * mb_cols + mb_col;
41 if (!mb_row || !mb_col || mb_row == mb_rows - 1 || mb_col == mb_cols - 1)
42 return false;
43 return d_status[mb_index + 1] || d_status[mb_index - 1] ||
44 d_status[mb_index + mb_cols] || d_status[mb_index - mb_cols];
45 }
46 #endif
47
61 void VideoDenoiser::DenoiseFrame(const VideoFrame& frame, 48 void VideoDenoiser::DenoiseFrame(const VideoFrame& frame,
62 VideoFrame* denoised_frame) { 49 VideoFrame* denoised_frame,
50 VideoFrame* denoised_frame_prev) {
63 int stride_y = frame.stride(kYPlane); 51 int stride_y = frame.stride(kYPlane);
64 int stride_u = frame.stride(kUPlane); 52 int stride_u = frame.stride(kUPlane);
65 int stride_v = frame.stride(kVPlane); 53 int stride_v = frame.stride(kVPlane);
66 // If previous width and height are different from current frame's, then no 54 // If previous width and height are different from current frame's, then no
67 // denoising for the current frame. 55 // denoising for the current frame.
68 if (width_ != frame.width() || height_ != frame.height()) { 56 if (width_ != frame.width() || height_ != frame.height()) {
69 width_ = frame.width(); 57 width_ = frame.width();
70 height_ = frame.height(); 58 height_ = frame.height();
71 denoised_frame->CreateFrame(frame.buffer(kYPlane), frame.buffer(kUPlane), 59 denoised_frame->CreateFrame(frame.buffer(kYPlane), frame.buffer(kUPlane),
72 frame.buffer(kVPlane), width_, height_, 60 frame.buffer(kVPlane), width_, height_,
73 stride_y, stride_u, stride_v, kVideoRotation_0); 61 stride_y, stride_u, stride_v, kVideoRotation_0);
62 denoised_frame_prev->CreateFrame(
63 frame.buffer(kYPlane), frame.buffer(kUPlane), frame.buffer(kVPlane),
64 width_, height_, stride_y, stride_u, stride_v, kVideoRotation_0);
74 // Setting time parameters to the output frame. 65 // Setting time parameters to the output frame.
75 denoised_frame->set_timestamp(frame.timestamp()); 66 denoised_frame->set_timestamp(frame.timestamp());
76 denoised_frame->set_render_time_ms(frame.render_time_ms()); 67 denoised_frame->set_render_time_ms(frame.render_time_ms());
68 ne_->Init(width_, height_, cpu_type_);
77 return; 69 return;
78 } 70 }
79 // For 16x16 block. 71 // For 16x16 block.
80 int mb_cols = width_ >> 4; 72 int mb_cols = width_ >> 4;
81 int mb_rows = height_ >> 4; 73 int mb_rows = height_ >> 4;
82 if (metrics_.get() == nullptr) 74 if (metrics_.get() == nullptr)
83 metrics_.reset(new DenoiseMetrics[mb_cols * mb_rows]()); 75 metrics_.reset(new DenoiseMetrics[mb_cols * mb_rows]());
76 if (d_status_.get() == nullptr) {
77 d_status_.reset(new uint8_t[mb_cols * mb_rows]());
78 #if EXPERIMENTAL
79 d_status_tmp1_.reset(new uint8_t[mb_cols * mb_rows]());
80 d_status_tmp2_.reset(new uint8_t[mb_cols * mb_rows]());
81 #endif
82 x_density_.reset(new uint8_t[mb_cols]());
83 y_density_.reset(new uint8_t[mb_rows]());
84 }
85
84 // Denoise on Y plane. 86 // Denoise on Y plane.
85 uint8_t* y_dst = denoised_frame->buffer(kYPlane); 87 uint8_t* y_dst = denoised_frame->buffer(kYPlane);
86 uint8_t* u_dst = denoised_frame->buffer(kUPlane); 88 uint8_t* u_dst = denoised_frame->buffer(kUPlane);
87 uint8_t* v_dst = denoised_frame->buffer(kVPlane); 89 uint8_t* v_dst = denoised_frame->buffer(kVPlane);
90 uint8_t* y_dst_prev = denoised_frame_prev->buffer(kYPlane);
88 const uint8_t* y_src = frame.buffer(kYPlane); 91 const uint8_t* y_src = frame.buffer(kYPlane);
89 const uint8_t* u_src = frame.buffer(kUPlane); 92 const uint8_t* u_src = frame.buffer(kUPlane);
90 const uint8_t* v_src = frame.buffer(kVPlane); 93 const uint8_t* v_src = frame.buffer(kVPlane);
94 uint8_t noise_level = ne_->GetNoiseLevel();
91 // Temporary buffer to store denoising result. 95 // Temporary buffer to store denoising result.
92 uint8_t y_tmp[16 * 16] = {0}; 96 uint8_t y_tmp[16 * 16] = {0};
97 memset(x_density_.get(), 0, mb_cols);
98 memset(y_density_.get(), 0, mb_rows);
99
100 // Loop over blocks to accumulate/extract noise level and update x/y_density
101 // factors for moving object detection.
102 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
103 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
104 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4);
105 uint8_t* mb_dst_prev =
106 y_dst_prev + (mb_row << 4) * stride_y + (mb_col << 4);
107 int mb_index = mb_row * mb_cols + mb_col;
108 #if EXPERIMENTAL
109 int pos_factor = PositionCheck(mb_row, mb_col, mb_rows, mb_cols);
110 uint32_t thr_var_adp = 16 * 16 * 5 * (noise_level ? pos_factor : 1);
111 #else
112 uint32_t thr_var_adp = 16 * 16 * 5;
113 #endif
114 int brightness = 0;
115 for (int i = 0; i < 16; ++i) {
116 for (int j = 0; j < 16; ++j) {
117 brightness += mb_src[i * stride_y + j];
118 }
119 }
120
121 // Get the denoised block.
122 filter_->MbDenoise(mb_dst_prev, stride_y, y_tmp, 16, mb_src, stride_y, 0,
123 1, true);
124 // The variance is based on the denoised blocks in time T and T-1.
125 metrics_[mb_index].var = filter_->Variance16x8(
126 mb_dst_prev, stride_y, y_tmp, 16, &metrics_[mb_index].sad);
127
128 if (metrics_[mb_index].var > thr_var_adp) {
129 ne_->ResetConsecLowVar(mb_index);
130 d_status_[mb_index] = 1;
131 #if EXPERIMENTAL
132 if (noise_level == 0 || pos_factor < 3) {
133 x_density_[mb_col] += 1;
134 y_density_[mb_row] += 1;
135 }
136 #else
137 x_density_[mb_col] += 1;
138 y_density_[mb_row] += 1;
139 #endif
140 } else {
141 uint32_t sse_t = 0;
142 // The variance is based on the src blocks in time T and denoised block
143 // in time T-1.
144 uint32_t noise_var = filter_->Variance16x8(mb_dst_prev, stride_y,
145 mb_src, stride_y, &sse_t);
146 ne_->GetNoise(mb_index, noise_var, brightness);
147 d_status_[mb_index] = 0;
148 }
149 // Track denoised frame.
150 filter_->CopyMem16x16(y_tmp, 16, mb_dst_prev, stride_y);
151 }
152 }
153
154 #if EXPERIMENTAL
155 // Draft. This can be optimized. This code block is to reduce false detection
marpan 2016/03/30 18:27:20 is it better to put this code block into separate
jackychen_ 2016/03/30 22:36:38 Done.
156 // in moving object detection.
157 int mb_row_min = noise_level ? mb_rows >> 3 : 1;
158 int mb_col_min = noise_level ? mb_cols >> 3 : 1;
159 int mb_row_max = noise_level ? (7 * mb_rows >> 3) : mb_rows - 2;
160 int mb_col_max = noise_level ? (7 * mb_cols >> 3) : mb_cols - 2;
161 memcpy(d_status_tmp1_.get(), d_status_.get(), mb_rows * mb_cols);
162 // Up left.
163 for (int mb_row = mb_row_min; mb_row <= mb_row_max; ++mb_row) {
164 for (int mb_col = mb_col_min; mb_col <= mb_col_max; ++mb_col) {
165 d_status_tmp1_[mb_row * mb_cols + mb_col] |=
166 (d_status_tmp1_[(mb_row - 1) * mb_cols + mb_col] |
167 d_status_tmp1_[mb_row * mb_cols + mb_col - 1]);
168 }
169 }
170 memcpy(d_status_tmp2_.get(), d_status_tmp1_.get(), mb_rows * mb_cols);
171 memcpy(d_status_tmp1_.get(), d_status_.get(), mb_rows * mb_cols);
172 // Bottom left.
173 for (int mb_row = mb_row_max; mb_row >= mb_row_min; --mb_row) {
174 for (int mb_col = mb_col_min; mb_col <= mb_col_max; ++mb_col) {
175 d_status_tmp1_[mb_row * mb_cols + mb_col] |=
176 (d_status_tmp1_[(mb_row + 1) * mb_cols + mb_col] |
177 d_status_tmp1_[mb_row * mb_cols + mb_col - 1]);
178 d_status_tmp2_[mb_row * mb_cols + mb_col] &=
179 d_status_tmp1_[mb_row * mb_cols + mb_col];
180 }
181 }
182 memcpy(d_status_tmp1_.get(), d_status_.get(), mb_rows * mb_cols);
183 // Up right.
184 for (int mb_row = mb_row_min; mb_row <= mb_row_max; ++mb_row) {
185 for (int mb_col = mb_col_max; mb_col >= mb_col_min; --mb_col) {
186 d_status_tmp1_[mb_row * mb_cols + mb_col] |=
187 (d_status_tmp1_[(mb_row - 1) * mb_cols + mb_col] |
188 d_status_tmp1_[mb_row * mb_cols + mb_col + 1]);
189 d_status_tmp2_[mb_row * mb_cols + mb_col] &=
190 d_status_tmp1_[mb_row * mb_cols + mb_col];
191 }
192 }
193 memcpy(d_status_tmp1_.get(), d_status_.get(), mb_rows * mb_cols);
194 // Bottom right.
195 for (int mb_row = mb_row_max; mb_row >= mb_row_min; --mb_row) {
196 for (int mb_col = mb_col_max; mb_col >= mb_col_min; --mb_col) {
197 d_status_tmp1_[mb_row * mb_cols + mb_col] |=
198 (d_status_tmp1_[(mb_row + 1) * mb_cols + mb_col] |
199 d_status_tmp1_[mb_row * mb_cols + mb_col + 1]);
200 d_status_tmp2_[mb_row * mb_cols + mb_col] &=
201 d_status_tmp1_[mb_row * mb_cols + mb_col];
202 }
203 }
204 #endif
205
206 // Denoise each MB based on the results of moving objects detection.
93 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) { 207 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
94 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) { 208 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
95 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4); 209 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4);
96 uint8_t* mb_dst = y_dst + (mb_row << 4) * stride_y + (mb_col << 4); 210 uint8_t* mb_dst = y_dst + (mb_row << 4) * stride_y + (mb_col << 4);
97 int mb_index = mb_row * mb_cols + mb_col;
98 // Denoise each MB at the very start and save the result to a temporary
99 // buffer.
100 if (filter_->MbDenoise(mb_dst, stride_y, y_tmp, 16, mb_src, stride_y, 0,
101 1) == FILTER_BLOCK) {
102 uint32_t thr_var = 0;
103 // Save var and sad to the buffer.
104 metrics_[mb_index].var = filter_->Variance16x8(
105 mb_dst, stride_y, y_tmp, 16, &metrics_[mb_index].sad);
106 // Get skin map.
107 metrics_[mb_index].is_skin = MbHasSkinColor(
108 y_src, u_src, v_src, stride_y, stride_u, stride_v, mb_row, mb_col);
109 // Variance threshold for skin/non-skin MB is different.
110 // Skin MB use a small threshold to reduce blockiness.
111 thr_var = metrics_[mb_index].is_skin ? 128 : 12 * 128;
112 if (metrics_[mb_index].var > thr_var) {
113 metrics_[mb_index].denoise = 0;
114 // Use the source MB.
115 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
116 } else {
117 metrics_[mb_index].denoise = 1;
118 // Use the denoised MB.
119 filter_->CopyMem16x16(y_tmp, 16, mb_dst, stride_y);
120 }
121 } else {
122 metrics_[mb_index].denoise = 0;
123 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
124 }
125 // Copy source U/V plane.
126 const uint8_t* mb_src_u = 211 const uint8_t* mb_src_u =
127 u_src + (mb_row << 3) * stride_u + (mb_col << 3); 212 u_src + (mb_row << 3) * stride_u + (mb_col << 3);
128 const uint8_t* mb_src_v = 213 const uint8_t* mb_src_v =
129 v_src + (mb_row << 3) * stride_v + (mb_col << 3); 214 v_src + (mb_row << 3) * stride_v + (mb_col << 3);
130 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u + (mb_col << 3); 215 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u + (mb_col << 3);
131 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3); 216 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3);
217 #if EXPERIMENTAL
218 if ((!d_status_tmp2_[mb_row * mb_cols + mb_col] ||
219 x_density_[mb_col] * y_density_[mb_row] == 0) &&
220 !TrailingBlock(d_status_, mb_row, mb_col, mb_rows, mb_cols)) {
221 #else
222 if (x_density_[mb_col] * y_density_[mb_row] == 0) {
223 #endif
224 if (filter_->MbDenoise(mb_dst, stride_y, y_tmp, 16, mb_src, stride_y, 0,
225 noise_level, false) == FILTER_BLOCK) {
226 filter_->CopyMem16x16(y_tmp, 16, mb_dst, stride_y);
227 } else {
228 // Copy y source.
229 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
230 }
231 } else {
232 // Copy y source.
233 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
234 }
132 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u); 235 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
133 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v); 236 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
134 } 237 }
135 } 238 }
136 // Second round. 239
137 // This is to reduce the trailing artifact and blockiness by referring 240 #if DISPLAY // Rectangle diagnostics
marpan 2016/03/30 18:27:20 good to put this "display code" into separate func
jackychen_ 2016/03/30 22:36:38 Done.
138 // neighbors' denoising status. 241 // Show rectangular region
139 TrailingReduction(mb_rows, mb_cols, y_src, stride_y, y_dst); 242 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
243 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
244 int mb_index = mb_row * mb_cols + mb_col;
245 const uint8_t* mb_src_u =
246 u_src + (mb_row << 3) * stride_u + (mb_col << 3);
247 const uint8_t* mb_src_v =
248 v_src + (mb_row << 3) * stride_v + (mb_col << 3);
249 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u + (mb_col << 3);
250 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3);
251 uint8_t y_tmp_255[8 * 8];
252 memset(y_tmp_255, 200, 8 * 8);
253 // x_density_[mb_col] * y_density_[mb_row]
254 if (d_status_[mb_index] == 1) {
255 // Paint to red.
256 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
257 filter_->CopyMem8x8(y_tmp_255, 8, mb_dst_v, stride_v);
258 #if EXPERIMENTAL
259 } else if (d_status_tmp2_[mb_row * mb_cols + mb_col] &&
260 x_density_[mb_col] * y_density_[mb_row]) {
261 #else
262 } else if (x_density_[mb_col] * y_density_[mb_row]) {
263 #endif
264 // Paint to blue.
265 filter_->CopyMem8x8(y_tmp_255, 8, mb_dst_u, stride_u);
266 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
267 } else {
268 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
269 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
270 }
271 }
272 }
273 #endif
140 274
141 // Setting time parameters to the output frame. 275 // Setting time parameters to the output frame.
142 denoised_frame->set_timestamp(frame.timestamp()); 276 denoised_frame->set_timestamp(frame.timestamp());
143 denoised_frame->set_render_time_ms(frame.render_time_ms()); 277 denoised_frame->set_render_time_ms(frame.render_time_ms());
144 return; 278 return;
145 } 279 }
146 280
147 } // namespace webrtc 281 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698