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

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: Fix some style issues. 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)) &&
29 (mb_col >= (mb_cols >> 4)) && (mb_col <= (15 * mb_cols >> 4)))
30 return 2;
31 else
32 return 3;
33 }
34
35 static void ReduceFalseDetection(const std::unique_ptr<uint8_t[]>& d_status,
36 std::unique_ptr<uint8_t[]>* d_status_tmp1,
37 std::unique_ptr<uint8_t[]>* d_status_tmp2,
38 int noise_level,
39 int mb_rows,
40 int mb_cols) {
41 // Draft. This can be optimized. This code block is to reduce false detection
42 // in moving object detection.
43 int mb_row_min = noise_level ? mb_rows >> 3 : 1;
44 int mb_col_min = noise_level ? mb_cols >> 3 : 1;
45 int mb_row_max = noise_level ? (7 * mb_rows >> 3) : mb_rows - 2;
46 int mb_col_max = noise_level ? (7 * mb_cols >> 3) : mb_cols - 2;
47 memcpy((*d_status_tmp1).get(), d_status.get(), mb_rows * mb_cols);
48 // Up left.
49 for (int mb_row = mb_row_min; mb_row <= mb_row_max; ++mb_row) {
50 for (int mb_col = mb_col_min; mb_col <= mb_col_max; ++mb_col) {
51 (*d_status_tmp1)[mb_row * mb_cols + mb_col] |=
52 ((*d_status_tmp1)[(mb_row - 1) * mb_cols + mb_col] |
53 (*d_status_tmp1)[mb_row * mb_cols + mb_col - 1]);
54 }
55 }
56 memcpy((*d_status_tmp2).get(), (*d_status_tmp1).get(), mb_rows * mb_cols);
57 memcpy((*d_status_tmp1).get(), d_status.get(), mb_rows * mb_cols);
58 // Bottom left.
59 for (int mb_row = mb_row_max; mb_row >= mb_row_min; --mb_row) {
60 for (int mb_col = mb_col_min; mb_col <= mb_col_max; ++mb_col) {
61 (*d_status_tmp1)[mb_row * mb_cols + mb_col] |=
62 ((*d_status_tmp1)[(mb_row + 1) * mb_cols + mb_col] |
63 (*d_status_tmp1)[mb_row * mb_cols + mb_col - 1]);
64 (*d_status_tmp2)[mb_row * mb_cols + mb_col] &=
65 (*d_status_tmp1)[mb_row * mb_cols + mb_col];
66 }
67 }
68 memcpy((*d_status_tmp1).get(), d_status.get(), mb_rows * mb_cols);
69 // Up right.
70 for (int mb_row = mb_row_min; mb_row <= mb_row_max; ++mb_row) {
71 for (int mb_col = mb_col_max; mb_col >= mb_col_min; --mb_col) {
72 (*d_status_tmp1)[mb_row * mb_cols + mb_col] |=
73 ((*d_status_tmp1)[(mb_row - 1) * mb_cols + mb_col] |
74 (*d_status_tmp1)[mb_row * mb_cols + mb_col + 1]);
75 (*d_status_tmp2)[mb_row * mb_cols + mb_col] &=
76 (*d_status_tmp1)[mb_row * mb_cols + mb_col];
77 }
78 }
79 memcpy((*d_status_tmp1).get(), d_status.get(), mb_rows * mb_cols);
80 // Bottom right.
81 for (int mb_row = mb_row_max; mb_row >= mb_row_min; --mb_row) {
82 for (int mb_col = mb_col_max; mb_col >= mb_col_min; --mb_col) {
83 (*d_status_tmp1)[mb_row * mb_cols + mb_col] |=
84 ((*d_status_tmp1)[(mb_row + 1) * mb_cols + mb_col] |
85 (*d_status_tmp1)[mb_row * mb_cols + mb_col + 1]);
86 (*d_status_tmp2)[mb_row * mb_cols + mb_col] &=
87 (*d_status_tmp1)[mb_row * mb_cols + mb_col];
88 }
89 }
90 }
91
92 static bool TrailingBlock(const std::unique_ptr<uint8_t[]>& d_status,
93 int mb_row,
94 int mb_col,
95 int mb_rows,
96 int mb_cols) {
97 int mb_index = mb_row * mb_cols + mb_col;
98 if (!mb_row || !mb_col || mb_row == mb_rows - 1 || mb_col == mb_cols - 1)
99 return false;
100 return d_status[mb_index + 1] || d_status[mb_index - 1] ||
101 d_status[mb_index + mb_cols] || d_status[mb_index - mb_cols];
102 }
103 #endif
104
105 #if DISPLAY
106 void ShowRect(const std::unique_ptr<DenoiserFilter> filter,
107 const std::unique_ptr<uint8_t[]>& d_status,
108 const std::unique_ptr<uint8_t[]>& d_status_tmp2,
109 const std::unique_ptr<uint8_t[]>& x_density,
110 const std::unique_ptr<uint8_t[]>& y_density,
111 const uint8_t* u_src,
112 const uint8_t* v_src,
113 const uint8_t* u_dst,
114 const uint8_t* v_dst,
115 int mb_rows,
116 int mb_cols) {
117 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
118 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
28 int mb_index = mb_row * mb_cols + mb_col; 119 int mb_index = mb_row * mb_cols + mb_col;
29 uint8_t* mb_dst = y_dst + (mb_row << 4) * stride_y + (mb_col << 4); 120 const uint8_t* mb_src_u =
30 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4); 121 u_src + (mb_row << 3) * stride_u + (mb_col << 3);
31 // If the number of denoised neighbors is less than a threshold, 122 const uint8_t* mb_src_v =
32 // do NOT denoise for the block. Set different threshold for skin MB. 123 v_src + (mb_row << 3) * stride_v + (mb_col << 3);
33 // The change of denoising status will not propagate. 124 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u + (mb_col << 3);
34 if (metrics_[mb_index].is_skin) { 125 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3);
35 // The threshold is high (more strict) for non-skin MB where the 126 uint8_t y_tmp_255[8 * 8];
36 // trailing usually happen. 127 memset(y_tmp_255, 200, 8 * 8);
37 if (metrics_[mb_index].denoise && 128 // x_density_[mb_col] * y_density_[mb_row]
38 metrics_[mb_index + 1].denoise + metrics_[mb_index - 1].denoise + 129 if (d_status[mb_index] == 1) {
39 metrics_[mb_index + mb_cols].denoise + 130 // Paint to red.
40 metrics_[mb_index - mb_cols].denoise <= 131 filter->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
41 2) { 132 filter->CopyMem8x8(y_tmp_255, 8, mb_dst_v, stride_v);
42 metrics_[mb_index].denoise = 0; 133 #if EXPERIMENTAL
43 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y); 134 } else if (d_status_tmp2[mb_row * mb_cols + mb_col] &&
44 } 135 x_density[mb_col] * y_density[mb_row]) {
45 } else if (metrics_[mb_index].denoise && 136 #else
46 metrics_[mb_index + 1].denoise + 137 } else if (x_density[mb_col] * y_density[mb_row]) {
47 metrics_[mb_index - 1].denoise + 138 #endif
48 metrics_[mb_index + mb_cols + 1].denoise + 139 // Paint to blue.
49 metrics_[mb_index + mb_cols - 1].denoise + 140 filter->CopyMem8x8(y_tmp_255, 8, mb_dst_u, stride_u);
50 metrics_[mb_index - mb_cols + 1].denoise + 141 filter->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
51 metrics_[mb_index - mb_cols - 1].denoise + 142 } else {
52 metrics_[mb_index + mb_cols].denoise + 143 filter->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
53 metrics_[mb_index - mb_cols].denoise <= 144 filter->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
54 7) {
55 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
56 } 145 }
57 } 146 }
58 } 147 }
59 } 148 }
149 #endif
60 150
61 void VideoDenoiser::DenoiseFrame(const VideoFrame& frame, 151 void VideoDenoiser::DenoiseFrame(const VideoFrame& frame,
62 VideoFrame* denoised_frame) { 152 VideoFrame* denoised_frame,
153 VideoFrame* denoised_frame_prev,
154 int noise_level_prev) {
63 int stride_y = frame.stride(kYPlane); 155 int stride_y = frame.stride(kYPlane);
64 int stride_u = frame.stride(kUPlane); 156 int stride_u = frame.stride(kUPlane);
65 int stride_v = frame.stride(kVPlane); 157 int stride_v = frame.stride(kVPlane);
66 // If previous width and height are different from current frame's, then no 158 // If previous width and height are different from current frame's, then no
67 // denoising for the current frame. 159 // denoising for the current frame.
68 if (width_ != frame.width() || height_ != frame.height()) { 160 if (width_ != frame.width() || height_ != frame.height()) {
69 width_ = frame.width(); 161 width_ = frame.width();
70 height_ = frame.height(); 162 height_ = frame.height();
71 denoised_frame->CreateFrame(frame.buffer(kYPlane), frame.buffer(kUPlane), 163 denoised_frame->CreateFrame(frame.buffer(kYPlane), frame.buffer(kUPlane),
72 frame.buffer(kVPlane), width_, height_, 164 frame.buffer(kVPlane), width_, height_,
73 stride_y, stride_u, stride_v, kVideoRotation_0); 165 stride_y, stride_u, stride_v, kVideoRotation_0);
166 denoised_frame_prev->CreateFrame(
167 frame.buffer(kYPlane), frame.buffer(kUPlane), frame.buffer(kVPlane),
168 width_, height_, stride_y, stride_u, stride_v, kVideoRotation_0);
74 // Setting time parameters to the output frame. 169 // Setting time parameters to the output frame.
75 denoised_frame->set_timestamp(frame.timestamp()); 170 denoised_frame->set_timestamp(frame.timestamp());
76 denoised_frame->set_render_time_ms(frame.render_time_ms()); 171 denoised_frame->set_render_time_ms(frame.render_time_ms());
172 ne_->Init(width_, height_, cpu_type_);
77 return; 173 return;
78 } 174 }
79 // For 16x16 block. 175 // For 16x16 block.
80 int mb_cols = width_ >> 4; 176 int mb_cols = width_ >> 4;
81 int mb_rows = height_ >> 4; 177 int mb_rows = height_ >> 4;
82 if (metrics_.get() == nullptr) 178 if (metrics_.get() == nullptr)
83 metrics_.reset(new DenoiseMetrics[mb_cols * mb_rows]()); 179 metrics_.reset(new DenoiseMetrics[mb_cols * mb_rows]());
180 if (d_status_.get() == nullptr) {
181 d_status_.reset(new uint8_t[mb_cols * mb_rows]());
182 #if EXPERIMENTAL
183 d_status_tmp1_.reset(new uint8_t[mb_cols * mb_rows]());
184 d_status_tmp2_.reset(new uint8_t[mb_cols * mb_rows]());
185 #endif
186 x_density_.reset(new uint8_t[mb_cols]());
187 y_density_.reset(new uint8_t[mb_rows]());
188 }
189
84 // Denoise on Y plane. 190 // Denoise on Y plane.
85 uint8_t* y_dst = denoised_frame->buffer(kYPlane); 191 uint8_t* y_dst = denoised_frame->buffer(kYPlane);
86 uint8_t* u_dst = denoised_frame->buffer(kUPlane); 192 uint8_t* u_dst = denoised_frame->buffer(kUPlane);
87 uint8_t* v_dst = denoised_frame->buffer(kVPlane); 193 uint8_t* v_dst = denoised_frame->buffer(kVPlane);
194 uint8_t* y_dst_prev = denoised_frame_prev->buffer(kYPlane);
88 const uint8_t* y_src = frame.buffer(kYPlane); 195 const uint8_t* y_src = frame.buffer(kYPlane);
89 const uint8_t* u_src = frame.buffer(kUPlane); 196 const uint8_t* u_src = frame.buffer(kUPlane);
90 const uint8_t* v_src = frame.buffer(kVPlane); 197 const uint8_t* v_src = frame.buffer(kVPlane);
198 uint8_t noise_level = noise_level_prev == -1 ? 0 : ne_->GetNoiseLevel();
91 // Temporary buffer to store denoising result. 199 // Temporary buffer to store denoising result.
92 uint8_t y_tmp[16 * 16] = {0}; 200 uint8_t y_tmp[16 * 16] = {0};
201 memset(x_density_.get(), 0, mb_cols);
202 memset(y_density_.get(), 0, mb_rows);
203
204 // Loop over blocks to accumulate/extract noise level and update x/y_density
205 // factors for moving object detection.
206 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
207 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
208 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4);
209 uint8_t* mb_dst_prev =
210 y_dst_prev + (mb_row << 4) * stride_y + (mb_col << 4);
211 int mb_index = mb_row * mb_cols + mb_col;
212 #if EXPERIMENTAL
213 int pos_factor = PositionCheck(mb_row, mb_col, mb_rows, mb_cols);
214 uint32_t thr_var_adp = 16 * 16 * 5 * (noise_level ? pos_factor : 1);
215 #else
216 uint32_t thr_var_adp = 16 * 16 * 5;
217 #endif
218 int brightness = 0;
219 for (int i = 0; i < 16; ++i) {
220 for (int j = 0; j < 16; ++j) {
221 brightness += mb_src[i * stride_y + j];
222 }
223 }
224
225 // Get the denoised block.
226 filter_->MbDenoise(mb_dst_prev, stride_y, y_tmp, 16, mb_src, stride_y, 0,
227 1, true);
228 // The variance is based on the denoised blocks in time T and T-1.
229 metrics_[mb_index].var = filter_->Variance16x8(
230 mb_dst_prev, stride_y, y_tmp, 16, &metrics_[mb_index].sad);
231
232 if (metrics_[mb_index].var > thr_var_adp) {
233 ne_->ResetConsecLowVar(mb_index);
234 d_status_[mb_index] = 1;
235 #if EXPERIMENTAL
236 if (noise_level == 0 || pos_factor < 3) {
237 x_density_[mb_col] += 1;
238 y_density_[mb_row] += 1;
239 }
240 #else
241 x_density_[mb_col] += 1;
242 y_density_[mb_row] += 1;
243 #endif
244 } else {
245 uint32_t sse_t = 0;
246 // The variance is based on the src blocks in time T and denoised block
247 // in time T-1.
248 uint32_t noise_var = filter_->Variance16x8(mb_dst_prev, stride_y,
249 mb_src, stride_y, &sse_t);
250 ne_->GetNoise(mb_index, noise_var, brightness);
251 d_status_[mb_index] = 0;
252 }
253 // Track denoised frame.
254 filter_->CopyMem16x16(y_tmp, 16, mb_dst_prev, stride_y);
255 }
256 }
257
258 #if EXPERIMENTAL
259 ReduceFalseDetection(d_status_, &d_status_tmp1, &d_status_tmp2, noise_level,
260 mb_rows, mb_cols);
261 #endif
262
263 // Denoise each MB based on the results of moving objects detection.
93 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) { 264 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
94 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) { 265 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); 266 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); 267 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 = 268 const uint8_t* mb_src_u =
127 u_src + (mb_row << 3) * stride_u + (mb_col << 3); 269 u_src + (mb_row << 3) * stride_u + (mb_col << 3);
128 const uint8_t* mb_src_v = 270 const uint8_t* mb_src_v =
129 v_src + (mb_row << 3) * stride_v + (mb_col << 3); 271 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); 272 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); 273 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3);
274 #if EXPERIMENTAL
275 if ((!d_status_tmp2_[mb_row * mb_cols + mb_col] ||
276 x_density_[mb_col] * y_density_[mb_row] == 0) &&
277 !TrailingBlock(d_status_, mb_row, mb_col, mb_rows, mb_cols)) {
278 #else
279 if (x_density_[mb_col] * y_density_[mb_row] == 0) {
280 #endif
281 if (filter_->MbDenoise(mb_dst, stride_y, y_tmp, 16, mb_src, stride_y, 0,
282 noise_level, false) == FILTER_BLOCK) {
283 filter_->CopyMem16x16(y_tmp, 16, mb_dst, stride_y);
284 } else {
285 // Copy y source.
286 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
287 }
288 } else {
289 // Copy y source.
290 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
291 }
132 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u); 292 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); 293 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
134 } 294 }
135 } 295 }
136 // Second round. 296
137 // This is to reduce the trailing artifact and blockiness by referring 297 #if DISPLAY // Rectangle diagnostics
138 // neighbors' denoising status. 298 // Show rectangular region
139 TrailingReduction(mb_rows, mb_cols, y_src, stride_y, y_dst); 299 ShowRect(filter_, d_status_, d_status_tmp2_, x_density_, y_density_, u_src,
300 v_src, u_dst, v_dst, mb_rows, mb_cols);
301 #endif
140 302
141 // Setting time parameters to the output frame. 303 // Setting time parameters to the output frame.
142 denoised_frame->set_timestamp(frame.timestamp()); 304 denoised_frame->set_timestamp(frame.timestamp());
143 denoised_frame->set_render_time_ms(frame.render_time_ms()); 305 denoised_frame->set_render_time_ms(frame.render_time_ms());
144 return; 306 return;
145 } 307 }
146 308
147 } // namespace webrtc 309 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698