blob: c1cab8187d28c4b3ad2c3c58b882818979931600 [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 */
jackychenafaae0d2016-04-12 23:02:55 -070010
jackychen8f9902a2015-11-26 02:59:48 -080011#include "webrtc/common_video/libyuv/include/scaler.h"
12#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
13#include "webrtc/modules/video_processing/video_denoiser.h"
14
jackychene42c0ae2016-04-16 10:44:16 -070015namespace webrtc {
16
jackychen8556c482016-04-20 16:04:31 -070017#if DISPLAY || DISPLAYNEON
jackychenafaae0d2016-04-12 23:02:55 -070018static void CopyMem8x8(const uint8_t* src,
19 int src_stride,
20 uint8_t* dst,
21 int dst_stride) {
22 for (int i = 0; i < 8; i++) {
23 memcpy(dst, src, 8);
24 src += src_stride;
25 dst += dst_stride;
26 }
27}
28
29static void ShowRect(const std::unique_ptr<DenoiserFilter>& filter,
30 const std::unique_ptr<uint8_t[]>& d_status,
31 const std::unique_ptr<uint8_t[]>& moving_edge_red,
32 const std::unique_ptr<uint8_t[]>& x_density,
33 const std::unique_ptr<uint8_t[]>& y_density,
34 const uint8_t* u_src,
35 const uint8_t* v_src,
36 uint8_t* u_dst,
37 uint8_t* v_dst,
38 int mb_rows_,
39 int mb_cols_,
40 int stride_u_,
41 int stride_v_) {
42 for (int mb_row = 0; mb_row < mb_rows_; ++mb_row) {
43 for (int mb_col = 0; mb_col < mb_cols_; ++mb_col) {
44 int mb_index = mb_row * mb_cols_ + mb_col;
45 const uint8_t* mb_src_u =
46 u_src + (mb_row << 3) * stride_u_ + (mb_col << 3);
47 const uint8_t* mb_src_v =
48 v_src + (mb_row << 3) * stride_v_ + (mb_col << 3);
49 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u_ + (mb_col << 3);
50 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v_ + (mb_col << 3);
51 uint8_t uv_tmp[8 * 8];
52 memset(uv_tmp, 200, 8 * 8);
53 if (d_status[mb_index] == 1) {
54 // Paint to red.
55 CopyMem8x8(mb_src_u, stride_u_, mb_dst_u, stride_u_);
56 CopyMem8x8(uv_tmp, 8, mb_dst_v, stride_v_);
57 } else if (moving_edge_red[mb_row * mb_cols_ + mb_col] &&
58 x_density[mb_col] * y_density[mb_row]) {
59 // Paint to blue.
60 CopyMem8x8(uv_tmp, 8, mb_dst_u, stride_u_);
61 CopyMem8x8(mb_src_v, stride_v_, mb_dst_v, stride_v_);
62 } else {
63 CopyMem8x8(mb_src_u, stride_u_, mb_dst_u, stride_u_);
64 CopyMem8x8(mb_src_v, stride_v_, mb_dst_v, stride_v_);
65 }
66 }
67 }
68}
69#endif
70
jackychen67e94fb2016-01-11 21:34:07 -080071VideoDenoiser::VideoDenoiser(bool runtime_cpu_detection)
72 : width_(0),
73 height_(0),
jackychenfa0befe2016-04-01 07:46:58 -070074 filter_(DenoiserFilter::Create(runtime_cpu_detection, &cpu_type_)),
75 ne_(new NoiseEstimation()) {}
jackychen8f9902a2015-11-26 02:59:48 -080076
jackychenafaae0d2016-04-12 23:02:55 -070077void VideoDenoiser::DenoiserReset(const VideoFrame& frame,
78 VideoFrame* denoised_frame,
79 VideoFrame* denoised_frame_prev) {
80 width_ = frame.width();
81 height_ = frame.height();
82 mb_cols_ = width_ >> 4;
83 mb_rows_ = height_ >> 4;
nisse5b3c4432016-04-29 02:39:24 -070084 stride_y_ = frame.stride(kYPlane);
85 stride_u_ = frame.stride(kUPlane);
86 stride_v_ = frame.stride(kVPlane);
jackychenafaae0d2016-04-12 23:02:55 -070087
88 // Allocate an empty buffer for denoised_frame_prev.
89 denoised_frame_prev->CreateEmptyFrame(width_, height_, stride_y_, stride_u_,
90 stride_v_);
91 // Allocate and initialize denoised_frame with key frame.
nisse5b3c4432016-04-29 02:39:24 -070092 denoised_frame->CreateFrame(frame.buffer(kYPlane), frame.buffer(kUPlane),
93 frame.buffer(kVPlane), width_, height_, stride_y_,
94 stride_u_, stride_v_, kVideoRotation_0);
jackychenafaae0d2016-04-12 23:02:55 -070095 // Set time parameters to the output frame.
96 denoised_frame->set_timestamp(frame.timestamp());
97 denoised_frame->set_render_time_ms(frame.render_time_ms());
98
99 // Init noise estimator and allocate buffers.
100 ne_->Init(width_, height_, cpu_type_);
101 moving_edge_.reset(new uint8_t[mb_cols_ * mb_rows_]);
102 mb_filter_decision_.reset(new DenoiserDecision[mb_cols_ * mb_rows_]);
103 x_density_.reset(new uint8_t[mb_cols_]);
104 y_density_.reset(new uint8_t[mb_rows_]);
105 moving_object_.reset(new uint8_t[mb_cols_ * mb_rows_]);
106}
107
108int VideoDenoiser::PositionCheck(int mb_row, int mb_col, int noise_level) {
109 if (noise_level == 0)
jackychenfa0befe2016-04-01 07:46:58 -0700110 return 1;
jackychenafaae0d2016-04-12 23:02:55 -0700111 if ((mb_row <= (mb_rows_ >> 4)) || (mb_col <= (mb_cols_ >> 4)) ||
112 (mb_col >= (15 * mb_cols_ >> 4)))
113 return 3;
114 else if ((mb_row <= (mb_rows_ >> 3)) || (mb_col <= (mb_cols_ >> 3)) ||
115 (mb_col >= (7 * mb_cols_ >> 3)))
jackychenfa0befe2016-04-01 07:46:58 -0700116 return 2;
117 else
jackychenafaae0d2016-04-12 23:02:55 -0700118 return 1;
jackychenfa0befe2016-04-01 07:46:58 -0700119}
120
jackychenafaae0d2016-04-12 23:02:55 -0700121void VideoDenoiser::ReduceFalseDetection(
122 const std::unique_ptr<uint8_t[]>& d_status,
123 std::unique_ptr<uint8_t[]>* moving_edge_red,
124 int noise_level) {
125 // From up left corner.
126 int mb_col_stop = mb_cols_ - 1;
127 for (int mb_row = 0; mb_row <= mb_rows_ - 1; ++mb_row) {
128 for (int mb_col = 0; mb_col <= mb_col_stop; ++mb_col) {
129 if (d_status[mb_row * mb_cols_ + mb_col]) {
130 mb_col_stop = mb_col - 1;
131 break;
132 }
133 (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
jackychenfa0befe2016-04-01 07:46:58 -0700134 }
135 }
jackychenafaae0d2016-04-12 23:02:55 -0700136 // From bottom left corner.
137 mb_col_stop = mb_cols_ - 1;
138 for (int mb_row = mb_rows_ - 1; mb_row >= 0; --mb_row) {
139 for (int mb_col = 0; mb_col <= mb_col_stop; ++mb_col) {
140 if (d_status[mb_row * mb_cols_ + mb_col]) {
141 mb_col_stop = mb_col - 1;
142 break;
143 }
144 (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
jackychenfa0befe2016-04-01 07:46:58 -0700145 }
146 }
jackychenafaae0d2016-04-12 23:02:55 -0700147 // From up right corner.
148 mb_col_stop = 0;
149 for (int mb_row = 0; mb_row <= mb_rows_ - 1; ++mb_row) {
150 for (int mb_col = mb_cols_ - 1; mb_col >= mb_col_stop; --mb_col) {
151 if (d_status[mb_row * mb_cols_ + mb_col]) {
152 mb_col_stop = mb_col + 1;
153 break;
154 }
155 (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
jackychenfa0befe2016-04-01 07:46:58 -0700156 }
157 }
jackychenafaae0d2016-04-12 23:02:55 -0700158 // From bottom right corner.
159 mb_col_stop = 0;
160 for (int mb_row = mb_rows_ - 1; mb_row >= 0; --mb_row) {
161 for (int mb_col = mb_cols_ - 1; mb_col >= mb_col_stop; --mb_col) {
162 if (d_status[mb_row * mb_cols_ + mb_col]) {
163 mb_col_stop = mb_col + 1;
164 break;
165 }
166 (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
jackychen8f9902a2015-11-26 02:59:48 -0800167 }
168 }
169}
170
jackychenafaae0d2016-04-12 23:02:55 -0700171bool VideoDenoiser::IsTrailingBlock(const std::unique_ptr<uint8_t[]>& d_status,
172 int mb_row,
173 int mb_col) {
174 bool ret = false;
175 int mb_index = mb_row * mb_cols_ + mb_col;
176 if (!mb_row || !mb_col || mb_row == mb_rows_ - 1 || mb_col == mb_cols_ - 1)
177 ret = false;
178 else
179 ret = d_status[mb_index + 1] || d_status[mb_index - 1] ||
180 d_status[mb_index + mb_cols_] || d_status[mb_index - mb_cols_];
181 return ret;
jackychenfa0befe2016-04-01 07:46:58 -0700182}
jackychenfa0befe2016-04-01 07:46:58 -0700183
jackychenafaae0d2016-04-12 23:02:55 -0700184void VideoDenoiser::CopySrcOnMOB(const uint8_t* y_src, uint8_t* y_dst) {
185 // Loop over to copy src block if the block is marked as moving object block
186 // or if the block may cause trailing artifacts.
187 for (int mb_row = 0; mb_row < mb_rows_; ++mb_row) {
188 const int mb_index_base = mb_row * mb_cols_;
189 const int offset_base = (mb_row << 4) * stride_y_;
190 const uint8_t* mb_src_base = y_src + offset_base;
191 uint8_t* mb_dst_base = y_dst + offset_base;
192 for (int mb_col = 0; mb_col < mb_cols_; ++mb_col) {
193 const int mb_index = mb_index_base + mb_col;
194 const uint32_t offset_col = mb_col << 4;
195 const uint8_t* mb_src = mb_src_base + offset_col;
196 uint8_t* mb_dst = mb_dst_base + offset_col;
197 // Check if the block is a moving object block or may cause a trailing
198 // artifacts.
199 if (mb_filter_decision_[mb_index] != FILTER_BLOCK ||
200 IsTrailingBlock(moving_edge_, mb_row, mb_col) ||
201 (x_density_[mb_col] * y_density_[mb_row] &&
202 moving_object_[mb_row * mb_cols_ + mb_col])) {
203 // Copy y source.
204 filter_->CopyMem16x16(mb_src, stride_y_, mb_dst, stride_y_);
jackychenfa0befe2016-04-01 07:46:58 -0700205 }
206 }
207 }
208}
jackychenfa0befe2016-04-01 07:46:58 -0700209
jackychen6650d6d2016-04-25 16:53:59 -0700210void VideoDenoiser::CopyLumaOnMargin(const uint8_t* y_src, uint8_t* y_dst) {
211 if ((mb_rows_ << 4) != height_) {
212 const uint8_t* margin_y_src = y_src + (mb_rows_ << 4) * stride_y_;
213 uint8_t* margin_y_dst = y_dst + (mb_rows_ << 4) * stride_y_;
214 memcpy(margin_y_dst, margin_y_src, (height_ - (mb_rows_ << 4)) * stride_y_);
215 }
216 if ((mb_cols_ << 4) != width_) {
217 const uint8_t* margin_y_src = y_src + (mb_cols_ << 4);
218 uint8_t* margin_y_dst = y_dst + (mb_cols_ << 4);
219 for (int i = 0; i < height_; ++i) {
220 for (int j = mb_cols_ << 4; j < width_; ++j) {
221 margin_y_dst[i * stride_y_ + j] = margin_y_src[i * stride_y_ + j];
222 }
223 }
224 }
225}
226
jackychen8f9902a2015-11-26 02:59:48 -0800227void VideoDenoiser::DenoiseFrame(const VideoFrame& frame,
jackychenfa0befe2016-04-01 07:46:58 -0700228 VideoFrame* denoised_frame,
229 VideoFrame* denoised_frame_prev,
jackychenafaae0d2016-04-12 23:02:55 -0700230 bool noise_estimation_enabled) {
231 // If previous width and height are different from current frame's, need to
232 // reallocate the buffers and no denoising for the current frame.
jackychen8f9902a2015-11-26 02:59:48 -0800233 if (width_ != frame.width() || height_ != frame.height()) {
jackychenafaae0d2016-04-12 23:02:55 -0700234 DenoiserReset(frame, denoised_frame, denoised_frame_prev);
jackychen8f9902a2015-11-26 02:59:48 -0800235 return;
236 }
jackychenfa0befe2016-04-01 07:46:58 -0700237
jackychenafaae0d2016-04-12 23:02:55 -0700238 // Set buffer pointers.
nisse5b3c4432016-04-29 02:39:24 -0700239 const uint8_t* y_src = frame.buffer(kYPlane);
240 const uint8_t* u_src = frame.buffer(kUPlane);
241 const uint8_t* v_src = frame.buffer(kVPlane);
242 uint8_t* y_dst = denoised_frame->buffer(kYPlane);
243 uint8_t* u_dst = denoised_frame->buffer(kUPlane);
244 uint8_t* v_dst = denoised_frame->buffer(kVPlane);
245 uint8_t* y_dst_prev = denoised_frame_prev->buffer(kYPlane);
jackychenafaae0d2016-04-12 23:02:55 -0700246 memset(x_density_.get(), 0, mb_cols_);
247 memset(y_density_.get(), 0, mb_rows_);
248 memset(moving_object_.get(), 1, mb_cols_ * mb_rows_);
jackychenfa0befe2016-04-01 07:46:58 -0700249
jackychenafaae0d2016-04-12 23:02:55 -0700250 uint8_t noise_level = noise_estimation_enabled ? ne_->GetNoiseLevel() : 0;
251 int thr_var_base = 16 * 16 * 5;
jackychenfa0befe2016-04-01 07:46:58 -0700252 // Loop over blocks to accumulate/extract noise level and update x/y_density
253 // factors for moving object detection.
jackychenafaae0d2016-04-12 23:02:55 -0700254 for (int mb_row = 0; mb_row < mb_rows_; ++mb_row) {
255 const int mb_index_base = mb_row * mb_cols_;
256 const int offset_base = (mb_row << 4) * stride_y_;
257 const uint8_t* mb_src_base = y_src + offset_base;
258 uint8_t* mb_dst_base = y_dst + offset_base;
259 uint8_t* mb_dst_prev_base = y_dst_prev + offset_base;
260 for (int mb_col = 0; mb_col < mb_cols_; ++mb_col) {
261 const int mb_index = mb_index_base + mb_col;
262 const bool ne_enable = (mb_index % NOISE_SUBSAMPLE_INTERVAL == 0);
263 const int pos_factor = PositionCheck(mb_row, mb_col, noise_level);
264 const uint32_t thr_var_adp = thr_var_base * pos_factor;
265 const uint32_t offset_col = mb_col << 4;
266 const uint8_t* mb_src = mb_src_base + offset_col;
267 uint8_t* mb_dst = mb_dst_base + offset_col;
268 uint8_t* mb_dst_prev = mb_dst_prev_base + offset_col;
269
270 // TODO(jackychen): Need SSE2/NEON opt.
271 int luma = 0;
272 if (ne_enable) {
273 for (int i = 4; i < 12; ++i) {
274 for (int j = 4; j < 12; ++j) {
275 luma += mb_src[i * stride_y_ + j];
276 }
jackychenfa0befe2016-04-01 07:46:58 -0700277 }
278 }
279
jackychenafaae0d2016-04-12 23:02:55 -0700280 // Get the filtered block and filter_decision.
281 mb_filter_decision_[mb_index] =
282 filter_->MbDenoise(mb_dst_prev, stride_y_, mb_dst, stride_y_, mb_src,
283 stride_y_, 0, noise_level);
jackychenfa0befe2016-04-01 07:46:58 -0700284
jackychenafaae0d2016-04-12 23:02:55 -0700285 // If filter decision is FILTER_BLOCK, no need to check moving edge.
286 // It is unlikely for a moving edge block to be filtered in current
287 // setting.
288 if (mb_filter_decision_[mb_index] == FILTER_BLOCK) {
289 uint32_t sse_t = 0;
290 if (ne_enable) {
291 // The variance used in noise estimation is based on the src block in
292 // time t (mb_src) and filtered block in time t-1 (mb_dist_prev).
293 uint32_t noise_var = filter_->Variance16x8(mb_dst_prev, stride_y_,
294 mb_src, stride_y_, &sse_t);
295 ne_->GetNoise(mb_index, noise_var, luma);
jackychenfa0befe2016-04-01 07:46:58 -0700296 }
jackychenafaae0d2016-04-12 23:02:55 -0700297 moving_edge_[mb_index] = 0; // Not a moving edge block.
jackychenfa0befe2016-04-01 07:46:58 -0700298 } else {
299 uint32_t sse_t = 0;
jackychenafaae0d2016-04-12 23:02:55 -0700300 // The variance used in MOD is based on the filtered blocks in time
301 // T (mb_dst) and T-1 (mb_dst_prev).
302 uint32_t noise_var = filter_->Variance16x8(mb_dst_prev, stride_y_,
303 mb_dst, stride_y_, &sse_t);
304 if (noise_var > thr_var_adp) { // Moving edge checking.
305 if (ne_enable) {
306 ne_->ResetConsecLowVar(mb_index);
307 }
308 moving_edge_[mb_index] = 1; // Mark as moving edge block.
309 x_density_[mb_col] += (pos_factor < 3);
310 y_density_[mb_row] += (pos_factor < 3);
jackychenfa0befe2016-04-01 07:46:58 -0700311 } else {
jackychenafaae0d2016-04-12 23:02:55 -0700312 moving_edge_[mb_index] = 0;
313 if (ne_enable) {
314 // The variance used in noise estimation is based on the src block
315 // in time t (mb_src) and filtered block in time t-1 (mb_dist_prev).
316 uint32_t noise_var = filter_->Variance16x8(
317 mb_dst_prev, stride_y_, mb_src, stride_y_, &sse_t);
318 ne_->GetNoise(mb_index, noise_var, luma);
319 }
jackychenfa0befe2016-04-01 07:46:58 -0700320 }
jackychenfa0befe2016-04-01 07:46:58 -0700321 }
jackychenafaae0d2016-04-12 23:02:55 -0700322 } // End of for loop
323 } // End of for loop
324
325 ReduceFalseDetection(moving_edge_, &moving_object_, noise_level);
326
327 CopySrcOnMOB(y_src, y_dst);
328
jackychen6650d6d2016-04-25 16:53:59 -0700329 // When frame width/height not divisible by 16, copy the margin to
330 // denoised_frame.
331 if ((mb_rows_ << 4) != height_ || (mb_cols_ << 4) != width_)
332 CopyLumaOnMargin(y_src, y_dst);
333
jackychenafaae0d2016-04-12 23:02:55 -0700334 // TODO(jackychen): Need SSE2/NEON opt.
335 // Copy u/v planes.
336 memcpy(u_dst, u_src, (height_ >> 1) * stride_u_);
337 memcpy(v_dst, v_src, (height_ >> 1) * stride_v_);
338
339 // Set time parameters to the output frame.
340 denoised_frame->set_timestamp(frame.timestamp());
341 denoised_frame->set_render_time_ms(frame.render_time_ms());
jackychenfa0befe2016-04-01 07:46:58 -0700342
jackychen8556c482016-04-20 16:04:31 -0700343#if DISPLAY || DISPLAYNEON
jackychenfa0befe2016-04-01 07:46:58 -0700344 // Show rectangular region
jackychenafaae0d2016-04-12 23:02:55 -0700345 ShowRect(filter_, moving_edge_, moving_object_, x_density_, y_density_, u_src,
346 v_src, u_dst, v_dst, mb_rows_, mb_cols_, stride_u_, stride_v_);
jackychenfa0befe2016-04-01 07:46:58 -0700347#endif
jackychen8f9902a2015-11-26 02:59:48 -0800348}
349
350} // namespace webrtc