blob: b00da5c90a16c9808149d3c1b562165492a752c2 [file] [log] [blame]
jackychen8f9902a2015-11-26 02:59:48 -08001/*
2 * Copyright (c) 2015 The WebRTC 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#include "webrtc/common_video/libyuv/include/scaler.h"
11#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
12#include "webrtc/modules/video_processing/video_denoiser.h"
13
14namespace webrtc {
15
jackychen67e94fb2016-01-11 21:34:07 -080016VideoDenoiser::VideoDenoiser(bool runtime_cpu_detection)
17 : width_(0),
18 height_(0),
jackychenfa0befe2016-04-01 07:46:58 -070019 filter_(DenoiserFilter::Create(runtime_cpu_detection, &cpu_type_)),
20 ne_(new NoiseEstimation()) {}
jackychen8f9902a2015-11-26 02:59:48 -080021
jackychenfa0befe2016-04-01 07:46:58 -070022#if EXPERIMENTAL
23// Check the mb position(1: close to the center, 3: close to the border).
24static int PositionCheck(int mb_row, int mb_col, int mb_rows, int mb_cols) {
25 if ((mb_row >= (mb_rows >> 3)) && (mb_row <= (7 * mb_rows >> 3)) &&
26 (mb_col >= (mb_cols >> 3)) && (mb_col <= (7 * mb_cols >> 3)))
27 return 1;
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
35static 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];
jackychen8f9902a2015-11-26 02:59:48 -080088 }
89 }
90}
91
jackychenfa0befe2016-04-01 07:46:58 -070092static 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
106void 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 uint8_t* u_dst,
114 uint8_t* v_dst,
115 int mb_rows,
116 int mb_cols,
117 int stride_u,
118 int stride_v) {
119 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
120 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
121 int mb_index = mb_row * mb_cols + mb_col;
122 const uint8_t* mb_src_u =
123 u_src + (mb_row << 3) * stride_u + (mb_col << 3);
124 const uint8_t* mb_src_v =
125 v_src + (mb_row << 3) * stride_v + (mb_col << 3);
126 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u + (mb_col << 3);
127 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3);
128 uint8_t y_tmp_255[8 * 8];
129 memset(y_tmp_255, 200, 8 * 8);
130 // x_density_[mb_col] * y_density_[mb_row]
131 if (d_status[mb_index] == 1) {
132 // Paint to red.
133 filter->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
134 filter->CopyMem8x8(y_tmp_255, 8, mb_dst_v, stride_v);
135#if EXPERIMENTAL
136 } else if (d_status_tmp2[mb_row * mb_cols + mb_col] &&
137 x_density[mb_col] * y_density[mb_row]) {
138#else
139 } else if (x_density[mb_col] * y_density[mb_row]) {
140#endif
141 // Paint to blue.
142 filter->CopyMem8x8(y_tmp_255, 8, mb_dst_u, stride_u);
143 filter->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
144 } else {
145 filter->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
146 filter->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
147 }
148 }
149 }
150}
151#endif
152
jackychen8f9902a2015-11-26 02:59:48 -0800153void VideoDenoiser::DenoiseFrame(const VideoFrame& frame,
jackychenfa0befe2016-04-01 07:46:58 -0700154 VideoFrame* denoised_frame,
155 VideoFrame* denoised_frame_prev,
156 int noise_level_prev) {
jackychen8f9902a2015-11-26 02:59:48 -0800157 int stride_y = frame.stride(kYPlane);
158 int stride_u = frame.stride(kUPlane);
159 int stride_v = frame.stride(kVPlane);
160 // If previous width and height are different from current frame's, then no
161 // denoising for the current frame.
162 if (width_ != frame.width() || height_ != frame.height()) {
163 width_ = frame.width();
164 height_ = frame.height();
165 denoised_frame->CreateFrame(frame.buffer(kYPlane), frame.buffer(kUPlane),
166 frame.buffer(kVPlane), width_, height_,
Niels Möller739fcb92016-02-29 13:11:45 +0100167 stride_y, stride_u, stride_v, kVideoRotation_0);
jackychenfa0befe2016-04-01 07:46:58 -0700168 denoised_frame_prev->CreateFrame(
169 frame.buffer(kYPlane), frame.buffer(kUPlane), frame.buffer(kVPlane),
170 width_, height_, stride_y, stride_u, stride_v, kVideoRotation_0);
jackychen8f9902a2015-11-26 02:59:48 -0800171 // Setting time parameters to the output frame.
172 denoised_frame->set_timestamp(frame.timestamp());
173 denoised_frame->set_render_time_ms(frame.render_time_ms());
jackychenfa0befe2016-04-01 07:46:58 -0700174 ne_->Init(width_, height_, cpu_type_);
jackychen8f9902a2015-11-26 02:59:48 -0800175 return;
176 }
177 // For 16x16 block.
178 int mb_cols = width_ >> 4;
179 int mb_rows = height_ >> 4;
180 if (metrics_.get() == nullptr)
jackychen67e94fb2016-01-11 21:34:07 -0800181 metrics_.reset(new DenoiseMetrics[mb_cols * mb_rows]());
jackychenfa0befe2016-04-01 07:46:58 -0700182 if (d_status_.get() == nullptr) {
183 d_status_.reset(new uint8_t[mb_cols * mb_rows]());
184#if EXPERIMENTAL
185 d_status_tmp1_.reset(new uint8_t[mb_cols * mb_rows]());
186 d_status_tmp2_.reset(new uint8_t[mb_cols * mb_rows]());
187#endif
188 x_density_.reset(new uint8_t[mb_cols]());
189 y_density_.reset(new uint8_t[mb_rows]());
190 }
191
jackychen8f9902a2015-11-26 02:59:48 -0800192 // Denoise on Y plane.
193 uint8_t* y_dst = denoised_frame->buffer(kYPlane);
194 uint8_t* u_dst = denoised_frame->buffer(kUPlane);
195 uint8_t* v_dst = denoised_frame->buffer(kVPlane);
jackychenfa0befe2016-04-01 07:46:58 -0700196 uint8_t* y_dst_prev = denoised_frame_prev->buffer(kYPlane);
jackychen8f9902a2015-11-26 02:59:48 -0800197 const uint8_t* y_src = frame.buffer(kYPlane);
198 const uint8_t* u_src = frame.buffer(kUPlane);
199 const uint8_t* v_src = frame.buffer(kVPlane);
jackychenfa0befe2016-04-01 07:46:58 -0700200 uint8_t noise_level = noise_level_prev == -1 ? 0 : ne_->GetNoiseLevel();
jackychen8f9902a2015-11-26 02:59:48 -0800201 // Temporary buffer to store denoising result.
202 uint8_t y_tmp[16 * 16] = {0};
jackychenfa0befe2016-04-01 07:46:58 -0700203 memset(x_density_.get(), 0, mb_cols);
204 memset(y_density_.get(), 0, mb_rows);
205
206 // Loop over blocks to accumulate/extract noise level and update x/y_density
207 // factors for moving object detection.
208 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
209 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
210 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4);
211 uint8_t* mb_dst_prev =
212 y_dst_prev + (mb_row << 4) * stride_y + (mb_col << 4);
213 int mb_index = mb_row * mb_cols + mb_col;
214#if EXPERIMENTAL
215 int pos_factor = PositionCheck(mb_row, mb_col, mb_rows, mb_cols);
216 uint32_t thr_var_adp = 16 * 16 * 5 * (noise_level ? pos_factor : 1);
217#else
218 uint32_t thr_var_adp = 16 * 16 * 5;
219#endif
220 int brightness = 0;
221 for (int i = 0; i < 16; ++i) {
222 for (int j = 0; j < 16; ++j) {
223 brightness += mb_src[i * stride_y + j];
224 }
225 }
226
227 // Get the denoised block.
228 filter_->MbDenoise(mb_dst_prev, stride_y, y_tmp, 16, mb_src, stride_y, 0,
229 1, true);
230 // The variance is based on the denoised blocks in time T and T-1.
231 metrics_[mb_index].var = filter_->Variance16x8(
232 mb_dst_prev, stride_y, y_tmp, 16, &metrics_[mb_index].sad);
233
234 if (metrics_[mb_index].var > thr_var_adp) {
235 ne_->ResetConsecLowVar(mb_index);
236 d_status_[mb_index] = 1;
237#if EXPERIMENTAL
238 if (noise_level == 0 || pos_factor < 3) {
239 x_density_[mb_col] += 1;
240 y_density_[mb_row] += 1;
241 }
242#else
243 x_density_[mb_col] += 1;
244 y_density_[mb_row] += 1;
245#endif
246 } else {
247 uint32_t sse_t = 0;
248 // The variance is based on the src blocks in time T and denoised block
249 // in time T-1.
250 uint32_t noise_var = filter_->Variance16x8(mb_dst_prev, stride_y,
251 mb_src, stride_y, &sse_t);
252 ne_->GetNoise(mb_index, noise_var, brightness);
253 d_status_[mb_index] = 0;
254 }
255 // Track denoised frame.
256 filter_->CopyMem16x16(y_tmp, 16, mb_dst_prev, stride_y);
257 }
258 }
259
260#if EXPERIMENTAL
261 ReduceFalseDetection(d_status_, &d_status_tmp1_, &d_status_tmp2_, noise_level,
262 mb_rows, mb_cols);
263#endif
264
265 // Denoise each MB based on the results of moving objects detection.
jackychen8f9902a2015-11-26 02:59:48 -0800266 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
267 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
mflodman99ab9442015-12-07 22:54:50 -0800268 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4);
jackychen8f9902a2015-11-26 02:59:48 -0800269 uint8_t* mb_dst = y_dst + (mb_row << 4) * stride_y + (mb_col << 4);
jackychen8f9902a2015-11-26 02:59:48 -0800270 const uint8_t* mb_src_u =
271 u_src + (mb_row << 3) * stride_u + (mb_col << 3);
272 const uint8_t* mb_src_v =
273 v_src + (mb_row << 3) * stride_v + (mb_col << 3);
274 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u + (mb_col << 3);
275 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3);
jackychenfa0befe2016-04-01 07:46:58 -0700276#if EXPERIMENTAL
277 if ((!d_status_tmp2_[mb_row * mb_cols + mb_col] ||
278 x_density_[mb_col] * y_density_[mb_row] == 0) &&
279 !TrailingBlock(d_status_, mb_row, mb_col, mb_rows, mb_cols)) {
280#else
281 if (x_density_[mb_col] * y_density_[mb_row] == 0) {
282#endif
283 if (filter_->MbDenoise(mb_dst, stride_y, y_tmp, 16, mb_src, stride_y, 0,
284 noise_level, false) == FILTER_BLOCK) {
285 filter_->CopyMem16x16(y_tmp, 16, mb_dst, stride_y);
286 } else {
287 // Copy y source.
288 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
289 }
290 } else {
291 // Copy y source.
292 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
293 }
jackychen8f9902a2015-11-26 02:59:48 -0800294 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
295 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
296 }
297 }
jackychenfa0befe2016-04-01 07:46:58 -0700298
299#if DISPLAY // Rectangle diagnostics
300 // Show rectangular region
301 ShowRect(filter_, d_status_, d_status_tmp2_, x_density_, y_density_, u_src,
302 v_src, u_dst, v_dst, mb_rows, mb_cols, stride_u, stride_v);
303#endif
jackychen8f9902a2015-11-26 02:59:48 -0800304
305 // Setting time parameters to the output frame.
306 denoised_frame->set_timestamp(frame.timestamp());
307 denoised_frame->set_render_time_ms(frame.render_time_ms());
308 return;
309}
310
311} // namespace webrtc