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

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: Created 4 years, 9 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)),
20 ne_(new NoiseEstimation()) {}
20 21
21 void VideoDenoiser::TrailingReduction(int mb_rows, 22 #if EXPERIMENTAL
22 int mb_cols, 23 // Draft. This should be replaced with lib function.
23 const uint8_t* y_src, 24 static void CopyStatus(std::unique_ptr<uint8_t[]>* d1,
24 int stride_y, 25 const std::unique_ptr<uint8_t[]>& d2,
25 uint8_t* y_dst) { 26 int len) {
26 for (int mb_row = 1; mb_row < mb_rows - 1; ++mb_row) { 27 for (int i = 0; i < len; ++i) {
27 for (int mb_col = 1; mb_col < mb_cols - 1; ++mb_col) { 28 (*d1)[i] = d2[i];
28 int mb_index = mb_row * mb_cols + mb_col;
29 uint8_t* mb_dst = y_dst + (mb_row << 4) * stride_y + (mb_col << 4);
30 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4);
31 // If the number of denoised neighbors is less than a threshold,
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 } 29 }
59 } 30 }
60 31
32 // Check the mb position(1: close to the center, 3: close to the border).
33 static int PositionCheck(int mb_row, int mb_col, int mb_rows, int mb_cols) {
34 if ((mb_row >= (mb_rows >> 3)) && (mb_row <= (7 * mb_rows >> 3)) &&
35 (mb_col >= (mb_cols >> 3)) && (mb_col <= (7 * mb_cols >> 3)))
36 return 1;
37 else if ((mb_row >= (mb_rows >> 4)) && (mb_row <= (15 * mb_rows >> 4)) &&
38 (mb_col >= (mb_cols >> 4)) && (mb_col <= (15 * mb_cols >> 4)))
39 return 2;
40 else
41 return 3;
42 }
43 #endif
44
61 void VideoDenoiser::DenoiseFrame(const VideoFrame& frame, 45 void VideoDenoiser::DenoiseFrame(const VideoFrame& frame,
62 VideoFrame* denoised_frame) { 46 VideoFrame* denoised_frame,
47 VideoFrame* denoised_frame_track) {
63 int stride_y = frame.stride(kYPlane); 48 int stride_y = frame.stride(kYPlane);
64 int stride_u = frame.stride(kUPlane); 49 int stride_u = frame.stride(kUPlane);
65 int stride_v = frame.stride(kVPlane); 50 int stride_v = frame.stride(kVPlane);
66 // If previous width and height are different from current frame's, then no 51 // If previous width and height are different from current frame's, then no
67 // denoising for the current frame. 52 // denoising for the current frame.
68 if (width_ != frame.width() || height_ != frame.height()) { 53 if (width_ != frame.width() || height_ != frame.height()) {
69 width_ = frame.width(); 54 width_ = frame.width();
70 height_ = frame.height(); 55 height_ = frame.height();
71 denoised_frame->CreateFrame(frame.buffer(kYPlane), frame.buffer(kUPlane), 56 denoised_frame->CreateFrame(frame.buffer(kYPlane), frame.buffer(kUPlane),
72 frame.buffer(kVPlane), width_, height_, 57 frame.buffer(kVPlane), width_, height_,
73 stride_y, stride_u, stride_v, kVideoRotation_0); 58 stride_y, stride_u, stride_v, kVideoRotation_0);
59 denoised_frame_track->CreateFrame(
60 frame.buffer(kYPlane), frame.buffer(kUPlane), frame.buffer(kVPlane),
61 width_, height_, stride_y, stride_u, stride_v, kVideoRotation_0);
74 // Setting time parameters to the output frame. 62 // Setting time parameters to the output frame.
75 denoised_frame->set_timestamp(frame.timestamp()); 63 denoised_frame->set_timestamp(frame.timestamp());
76 denoised_frame->set_render_time_ms(frame.render_time_ms()); 64 denoised_frame->set_render_time_ms(frame.render_time_ms());
65 ne_->SetResolution(width_, height_);
77 return; 66 return;
78 } 67 }
79 // For 16x16 block. 68 // For 16x16 block.
80 int mb_cols = width_ >> 4; 69 int mb_cols = width_ >> 4;
81 int mb_rows = height_ >> 4; 70 int mb_rows = height_ >> 4;
82 if (metrics_.get() == nullptr) 71 if (metrics_.get() == nullptr)
83 metrics_.reset(new DenoiseMetrics[mb_cols * mb_rows]()); 72 metrics_.reset(new DenoiseMetrics[mb_cols * mb_rows]());
73 if (d_status_.get() == nullptr) {
74 d_status_.reset(new uint8_t[mb_cols * mb_rows]());
75 #if EXPERIMENTAL
76 d_status_tmp1_.reset(new uint8_t[mb_cols * mb_rows]());
77 d_status_tmp2_.reset(new uint8_t[mb_cols * mb_rows]());
78 #endif
79 x_density_.reset(new uint8_t[mb_cols]());
80 y_density_.reset(new uint8_t[mb_rows]());
81 }
82
84 // Denoise on Y plane. 83 // Denoise on Y plane.
85 uint8_t* y_dst = denoised_frame->buffer(kYPlane); 84 uint8_t* y_dst = denoised_frame->buffer(kYPlane);
86 uint8_t* u_dst = denoised_frame->buffer(kUPlane); 85 uint8_t* u_dst = denoised_frame->buffer(kUPlane);
87 uint8_t* v_dst = denoised_frame->buffer(kVPlane); 86 uint8_t* v_dst = denoised_frame->buffer(kVPlane);
87 uint8_t* y_dst_track = denoised_frame_track->buffer(kYPlane);
88 const uint8_t* y_src = frame.buffer(kYPlane); 88 const uint8_t* y_src = frame.buffer(kYPlane);
89 const uint8_t* u_src = frame.buffer(kUPlane); 89 const uint8_t* u_src = frame.buffer(kUPlane);
90 const uint8_t* v_src = frame.buffer(kVPlane); 90 const uint8_t* v_src = frame.buffer(kVPlane);
91 uint8_t noise_level = ne_->GetNoiseLevel();
91 // Temporary buffer to store denoising result. 92 // Temporary buffer to store denoising result.
92 uint8_t y_tmp[16 * 16] = {0}; 93 uint8_t y_tmp[16 * 16] = {0};
93 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) { 94 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
95 y_density_[mb_row] = 0;
96 }
97 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
marpan 2016/03/24 20:56:16 can you use memset here?
jackychen_ 2016/03/25 18:45:44 Done.
98 x_density_[mb_col] = 0;
99 }
100 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
marpan 2016/03/24 20:56:16 put more comments to explain what this block of co
jackychen_ 2016/03/25 18:45:44 Done.
101 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
102 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4);
103 uint8_t* mb_dst_track =
104 y_dst_track + (mb_row << 4) * stride_y + (mb_col << 4);
105 int mb_index = mb_row * mb_cols + mb_col;
106 #if EXPERIMENTAL
107 int pos_factor = PositionCheck(mb_row, mb_col, mb_rows, mb_cols);
108 uint32_t thr_var_adp = 16 * 16 * 5 * (noise_level ? pos_factor : 1);
109 #else
110 uint32_t thr_var_adp = 16 * 16 * 5 * (noise_level + 1);
111 #endif
112 int brightness = 0;
113 for (int i = 0; i < 16; ++i) {
114 for (int j = 0; j < 16; ++j) {
115 brightness += mb_src[i * stride_y + j];
116 }
117 }
118
119 // Get the averaged block.
marpan 2016/03/30 18:27:19 Get denoised block
120 filter_->MbDenoise(mb_dst_track, stride_y, y_tmp, 16, mb_src, stride_y, 0,
121 1, true);
122 metrics_[mb_index].var = filter_->Variance16x8(
123 mb_dst_track, stride_y, y_tmp, 16, &metrics_[mb_index].sad);
124
125 if (metrics_[mb_index].var > thr_var_adp) {
126 ne_->ResetConsecLowVar(mb_index);
127 d_status_[mb_index] = 1;
128 #if EXPERIMENTAL
129 if (noise_level == 0 || pos_factor < 3) {
130 x_density_[mb_col] += 1;
131 y_density_[mb_row] += 1;
132 }
133 #else
134 x_density_[mb_col] += 1;
135 y_density_[mb_row] += 1;
136 #endif
137 } else {
138 uint32_t sse_t = 0;
139 uint32_t noise_var = filter_->Variance16x8(mb_dst_track, stride_y,
140 mb_src, stride_y, &sse_t);
141 ne_->GetNoise(mb_index, noise_var, brightness);
142 d_status_[mb_index] = 0;
143 }
144 // Track averaged frame.
marpan 2016/03/24 20:56:16 Track denoised frame?
jackychen_ 2016/03/25 18:45:45 Done.
145 filter_->CopyMem16x16(y_tmp, 16, mb_dst_track, stride_y);
146 }
147 }
148
149 #if EXPERIMENTAL
150 // Draft. This can be optimized.
151 int mb_row_min = noise_level ? mb_rows >> 3 : 1;
152 int mb_col_min = noise_level ? mb_cols >> 3 : 1;
153 int mb_row_max = noise_level ? (7 * mb_rows >> 3) : mb_rows - 2;
154 int mb_col_max = noise_level ? (7 * mb_cols >> 3) : mb_cols - 2;
155 CopyStatus(&d_status_tmp1_, d_status_, mb_rows * mb_cols);
156 // Up left.
157 for (int mb_row = mb_row_min; mb_row <= mb_row_max; ++mb_row) {
158 for (int mb_col = mb_col_min; mb_col <= mb_col_max; ++mb_col) {
159 d_status_tmp1_[mb_row * mb_cols + mb_col] |=
160 (d_status_tmp1_[(mb_row - 1) * mb_cols + mb_col] |
161 d_status_tmp1_[mb_row * mb_cols + mb_col - 1]);
162 }
163 }
164 CopyStatus(&d_status_tmp2_, d_status_tmp1_, mb_rows * mb_cols);
165 CopyStatus(&d_status_tmp1_, d_status_, mb_rows * mb_cols);
166 // Bottom left.
167 for (int mb_row = mb_row_max; mb_row >= mb_row_min; --mb_row) {
168 for (int mb_col = mb_col_min; mb_col <= mb_col_max; ++mb_col) {
169 d_status_tmp1_[mb_row * mb_cols + mb_col] |=
170 (d_status_tmp1_[(mb_row + 1) * mb_cols + mb_col] |
171 d_status_tmp1_[mb_row * mb_cols + mb_col - 1]);
172 d_status_tmp2_[mb_row * mb_cols + mb_col] &=
173 d_status_tmp1_[mb_row * mb_cols + mb_col];
174 }
175 }
176 CopyStatus(&d_status_tmp1_, d_status_, mb_rows * mb_cols);
177 // Up right.
178 for (int mb_row = mb_row_min; mb_row <= mb_row_max; ++mb_row) {
179 for (int mb_col = mb_col_max; mb_col >= mb_col_min; --mb_col) {
180 d_status_tmp1_[mb_row * mb_cols + mb_col] |=
181 (d_status_tmp1_[(mb_row - 1) * mb_cols + mb_col] |
182 d_status_tmp1_[mb_row * mb_cols + mb_col + 1]);
183 d_status_tmp2_[mb_row * mb_cols + mb_col] &=
184 d_status_tmp1_[mb_row * mb_cols + mb_col];
185 }
186 }
187 CopyStatus(&d_status_tmp1_, d_status_, mb_rows * mb_cols);
188 // Bottom right.
189 for (int mb_row = mb_row_max; mb_row >= mb_row_min; --mb_row) {
190 for (int mb_col = mb_col_max; mb_col >= mb_col_min; --mb_col) {
191 d_status_tmp1_[mb_row * mb_cols + mb_col] |=
192 (d_status_tmp1_[(mb_row + 1) * mb_cols + mb_col] |
193 d_status_tmp1_[mb_row * mb_cols + mb_col + 1]);
194 d_status_tmp2_[mb_row * mb_cols + mb_col] &=
195 d_status_tmp1_[mb_row * mb_cols + mb_col];
196 }
197 }
198 #endif
199
200 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
marpan 2016/03/24 20:56:16 put comments to explain this block of code
jackychen_ 2016/03/25 18:45:45 Done.
94 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) { 201 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); 202 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); 203 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 = 204 const uint8_t* mb_src_u =
127 u_src + (mb_row << 3) * stride_u + (mb_col << 3); 205 u_src + (mb_row << 3) * stride_u + (mb_col << 3);
128 const uint8_t* mb_src_v = 206 const uint8_t* mb_src_v =
129 v_src + (mb_row << 3) * stride_v + (mb_col << 3); 207 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); 208 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); 209 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3);
210 #if EXPERIMENTAL
211 if (!d_status_tmp2_[mb_row * mb_cols + mb_col] ||
212 x_density_[mb_col] * y_density_[mb_row] == 0) {
213 #else
214 if (x_density_[mb_col] * y_density_[mb_row] == 0) {
215 #endif
216 if (filter_->MbDenoise(mb_dst, stride_y, y_tmp, 16, mb_src, stride_y, 0,
217 noise_level, false) == FILTER_BLOCK) {
218 filter_->CopyMem16x16(y_tmp, 16, mb_dst, stride_y);
219 } else {
220 // Copy y source.
221 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
222 }
223 } else {
224 // Copy y source.
225 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
226 }
132 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u); 227 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); 228 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
134 } 229 }
135 } 230 }
136 // Second round. 231
137 // This is to reduce the trailing artifact and blockiness by referring 232 #if DISPLAY // Rectangle diagnostics
138 // neighbors' denoising status. 233 // Show rectangular region
139 TrailingReduction(mb_rows, mb_cols, y_src, stride_y, y_dst); 234 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
235 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
236 int mb_index = mb_row * mb_cols + mb_col;
237 const uint8_t* mb_src_u =
238 u_src + (mb_row << 3) * stride_u + (mb_col << 3);
239 const uint8_t* mb_src_v =
240 v_src + (mb_row << 3) * stride_v + (mb_col << 3);
241 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u + (mb_col << 3);
242 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3);
243 uint8_t y_tmp_255[8 * 8];
244 memset(y_tmp_255, 200, 8 * 8);
245 // x_density_[mb_col] * y_density_[mb_row]
246 if (d_status_[mb_index] == 1) {
247 // Paint to red.
248 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
249 filter_->CopyMem8x8(y_tmp_255, 8, mb_dst_v, stride_v);
250 } else if (d_status_tmp2_[mb_row * mb_cols + mb_col] &&
251 x_density_[mb_col] * y_density_[mb_row]) {
252 // Paint to blue.
253 filter_->CopyMem8x8(y_tmp_255, 8, mb_dst_u, stride_u);
254 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
255 } else {
256 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
257 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
258 }
259 }
260 }
261 #endif
140 262
141 // Setting time parameters to the output frame. 263 // Setting time parameters to the output frame.
142 denoised_frame->set_timestamp(frame.timestamp()); 264 denoised_frame->set_timestamp(frame.timestamp());
143 denoised_frame->set_render_time_ms(frame.render_time_ms()); 265 denoised_frame->set_render_time_ms(frame.render_time_ms());
144 return; 266 return;
145 } 267 }
146 268
147 } // namespace webrtc 269 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698