blob: a7aa7d3b60c27de2aee479153a111e2a0732f580 [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/webrtc_libyuv.h"
12#include "webrtc/modules/video_processing/video_denoiser.h"
13
jackychene42c0ae2016-04-16 10:44:16 -070014namespace webrtc {
15
jackychen8556c482016-04-20 16:04:31 -070016#if DISPLAY || DISPLAYNEON
jackychenafaae0d2016-04-12 23:02:55 -070017static void CopyMem8x8(const uint8_t* src,
18 int src_stride,
19 uint8_t* dst,
20 int dst_stride) {
21 for (int i = 0; i < 8; i++) {
22 memcpy(dst, src, 8);
23 src += src_stride;
24 dst += dst_stride;
25 }
26}
27
28static void ShowRect(const std::unique_ptr<DenoiserFilter>& filter,
29 const std::unique_ptr<uint8_t[]>& d_status,
30 const std::unique_ptr<uint8_t[]>& moving_edge_red,
31 const std::unique_ptr<uint8_t[]>& x_density,
32 const std::unique_ptr<uint8_t[]>& y_density,
33 const uint8_t* u_src,
34 const uint8_t* v_src,
35 uint8_t* u_dst,
36 uint8_t* v_dst,
37 int mb_rows_,
38 int mb_cols_,
39 int stride_u_,
40 int stride_v_) {
41 for (int mb_row = 0; mb_row < mb_rows_; ++mb_row) {
42 for (int mb_col = 0; mb_col < mb_cols_; ++mb_col) {
43 int mb_index = mb_row * mb_cols_ + mb_col;
44 const uint8_t* mb_src_u =
45 u_src + (mb_row << 3) * stride_u_ + (mb_col << 3);
46 const uint8_t* mb_src_v =
47 v_src + (mb_row << 3) * stride_v_ + (mb_col << 3);
48 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u_ + (mb_col << 3);
49 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v_ + (mb_col << 3);
50 uint8_t uv_tmp[8 * 8];
51 memset(uv_tmp, 200, 8 * 8);
52 if (d_status[mb_index] == 1) {
53 // Paint to red.
54 CopyMem8x8(mb_src_u, stride_u_, mb_dst_u, stride_u_);
55 CopyMem8x8(uv_tmp, 8, mb_dst_v, stride_v_);
56 } else if (moving_edge_red[mb_row * mb_cols_ + mb_col] &&
57 x_density[mb_col] * y_density[mb_row]) {
58 // Paint to blue.
59 CopyMem8x8(uv_tmp, 8, mb_dst_u, stride_u_);
60 CopyMem8x8(mb_src_v, stride_v_, mb_dst_v, stride_v_);
61 } else {
62 CopyMem8x8(mb_src_u, stride_u_, mb_dst_u, stride_u_);
63 CopyMem8x8(mb_src_v, stride_v_, mb_dst_v, stride_v_);
64 }
65 }
66 }
67}
68#endif
69
jackychen67e94fb2016-01-11 21:34:07 -080070VideoDenoiser::VideoDenoiser(bool runtime_cpu_detection)
71 : width_(0),
72 height_(0),
jackychenfa0befe2016-04-01 07:46:58 -070073 filter_(DenoiserFilter::Create(runtime_cpu_detection, &cpu_type_)),
74 ne_(new NoiseEstimation()) {}
jackychen8f9902a2015-11-26 02:59:48 -080075
jackychenafaae0d2016-04-12 23:02:55 -070076void VideoDenoiser::DenoiserReset(const VideoFrame& frame,
77 VideoFrame* denoised_frame,
78 VideoFrame* denoised_frame_prev) {
79 width_ = frame.width();
80 height_ = frame.height();
81 mb_cols_ = width_ >> 4;
82 mb_rows_ = height_ >> 4;
nissec9c142f2016-05-17 04:05:47 -070083 stride_y_ = frame.video_frame_buffer()->StrideY();
84 stride_u_ = frame.video_frame_buffer()->StrideU();
85 stride_v_ = frame.video_frame_buffer()->StrideV();
jackychenafaae0d2016-04-12 23:02:55 -070086
87 // Allocate an empty buffer for denoised_frame_prev.
88 denoised_frame_prev->CreateEmptyFrame(width_, height_, stride_y_, stride_u_,
89 stride_v_);
90 // Allocate and initialize denoised_frame with key frame.
nissec9c142f2016-05-17 04:05:47 -070091 denoised_frame->CreateFrame(
92 frame.video_frame_buffer()->DataY(),
93 frame.video_frame_buffer()->DataU(),
94 frame.video_frame_buffer()->DataV(),
95 width_, height_, stride_y_, stride_u_, stride_v_, kVideoRotation_0);
jackychenafaae0d2016-04-12 23:02:55 -070096 // Set time parameters to the output frame.
97 denoised_frame->set_timestamp(frame.timestamp());
98 denoised_frame->set_render_time_ms(frame.render_time_ms());
99
100 // Init noise estimator and allocate buffers.
101 ne_->Init(width_, height_, cpu_type_);
102 moving_edge_.reset(new uint8_t[mb_cols_ * mb_rows_]);
103 mb_filter_decision_.reset(new DenoiserDecision[mb_cols_ * mb_rows_]);
104 x_density_.reset(new uint8_t[mb_cols_]);
105 y_density_.reset(new uint8_t[mb_rows_]);
106 moving_object_.reset(new uint8_t[mb_cols_ * mb_rows_]);
107}
108
109int VideoDenoiser::PositionCheck(int mb_row, int mb_col, int noise_level) {
110 if (noise_level == 0)
jackychenfa0befe2016-04-01 07:46:58 -0700111 return 1;
jackychenafaae0d2016-04-12 23:02:55 -0700112 if ((mb_row <= (mb_rows_ >> 4)) || (mb_col <= (mb_cols_ >> 4)) ||
113 (mb_col >= (15 * mb_cols_ >> 4)))
114 return 3;
115 else if ((mb_row <= (mb_rows_ >> 3)) || (mb_col <= (mb_cols_ >> 3)) ||
116 (mb_col >= (7 * mb_cols_ >> 3)))
jackychenfa0befe2016-04-01 07:46:58 -0700117 return 2;
118 else
jackychenafaae0d2016-04-12 23:02:55 -0700119 return 1;
jackychenfa0befe2016-04-01 07:46:58 -0700120}
121
jackychenafaae0d2016-04-12 23:02:55 -0700122void VideoDenoiser::ReduceFalseDetection(
123 const std::unique_ptr<uint8_t[]>& d_status,
124 std::unique_ptr<uint8_t[]>* moving_edge_red,
125 int noise_level) {
126 // From up left corner.
127 int mb_col_stop = mb_cols_ - 1;
128 for (int mb_row = 0; mb_row <= mb_rows_ - 1; ++mb_row) {
129 for (int mb_col = 0; mb_col <= mb_col_stop; ++mb_col) {
130 if (d_status[mb_row * mb_cols_ + mb_col]) {
131 mb_col_stop = mb_col - 1;
132 break;
133 }
134 (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
jackychenfa0befe2016-04-01 07:46:58 -0700135 }
136 }
jackychenafaae0d2016-04-12 23:02:55 -0700137 // From bottom left corner.
138 mb_col_stop = mb_cols_ - 1;
139 for (int mb_row = mb_rows_ - 1; mb_row >= 0; --mb_row) {
140 for (int mb_col = 0; mb_col <= mb_col_stop; ++mb_col) {
141 if (d_status[mb_row * mb_cols_ + mb_col]) {
142 mb_col_stop = mb_col - 1;
143 break;
144 }
145 (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
jackychenfa0befe2016-04-01 07:46:58 -0700146 }
147 }
jackychenafaae0d2016-04-12 23:02:55 -0700148 // From up right corner.
149 mb_col_stop = 0;
150 for (int mb_row = 0; mb_row <= mb_rows_ - 1; ++mb_row) {
151 for (int mb_col = mb_cols_ - 1; mb_col >= mb_col_stop; --mb_col) {
152 if (d_status[mb_row * mb_cols_ + mb_col]) {
153 mb_col_stop = mb_col + 1;
154 break;
155 }
156 (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
jackychenfa0befe2016-04-01 07:46:58 -0700157 }
158 }
jackychenafaae0d2016-04-12 23:02:55 -0700159 // From bottom right corner.
160 mb_col_stop = 0;
161 for (int mb_row = mb_rows_ - 1; mb_row >= 0; --mb_row) {
162 for (int mb_col = mb_cols_ - 1; mb_col >= mb_col_stop; --mb_col) {
163 if (d_status[mb_row * mb_cols_ + mb_col]) {
164 mb_col_stop = mb_col + 1;
165 break;
166 }
167 (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
jackychen8f9902a2015-11-26 02:59:48 -0800168 }
169 }
170}
171
jackychenafaae0d2016-04-12 23:02:55 -0700172bool VideoDenoiser::IsTrailingBlock(const std::unique_ptr<uint8_t[]>& d_status,
173 int mb_row,
174 int mb_col) {
175 bool ret = false;
176 int mb_index = mb_row * mb_cols_ + mb_col;
177 if (!mb_row || !mb_col || mb_row == mb_rows_ - 1 || mb_col == mb_cols_ - 1)
178 ret = false;
179 else
180 ret = d_status[mb_index + 1] || d_status[mb_index - 1] ||
181 d_status[mb_index + mb_cols_] || d_status[mb_index - mb_cols_];
182 return ret;
jackychenfa0befe2016-04-01 07:46:58 -0700183}
jackychenfa0befe2016-04-01 07:46:58 -0700184
jackychenafaae0d2016-04-12 23:02:55 -0700185void VideoDenoiser::CopySrcOnMOB(const uint8_t* y_src, uint8_t* y_dst) {
186 // Loop over to copy src block if the block is marked as moving object block
187 // or if the block may cause trailing artifacts.
188 for (int mb_row = 0; mb_row < mb_rows_; ++mb_row) {
189 const int mb_index_base = mb_row * mb_cols_;
190 const int offset_base = (mb_row << 4) * stride_y_;
191 const uint8_t* mb_src_base = y_src + offset_base;
192 uint8_t* mb_dst_base = y_dst + offset_base;
193 for (int mb_col = 0; mb_col < mb_cols_; ++mb_col) {
194 const int mb_index = mb_index_base + mb_col;
195 const uint32_t offset_col = mb_col << 4;
196 const uint8_t* mb_src = mb_src_base + offset_col;
197 uint8_t* mb_dst = mb_dst_base + offset_col;
198 // Check if the block is a moving object block or may cause a trailing
199 // artifacts.
200 if (mb_filter_decision_[mb_index] != FILTER_BLOCK ||
201 IsTrailingBlock(moving_edge_, mb_row, mb_col) ||
202 (x_density_[mb_col] * y_density_[mb_row] &&
203 moving_object_[mb_row * mb_cols_ + mb_col])) {
204 // Copy y source.
205 filter_->CopyMem16x16(mb_src, stride_y_, mb_dst, stride_y_);
jackychenfa0befe2016-04-01 07:46:58 -0700206 }
207 }
208 }
209}
jackychenfa0befe2016-04-01 07:46:58 -0700210
jackychen6650d6d2016-04-25 16:53:59 -0700211void VideoDenoiser::CopyLumaOnMargin(const uint8_t* y_src, uint8_t* y_dst) {
212 if ((mb_rows_ << 4) != height_) {
213 const uint8_t* margin_y_src = y_src + (mb_rows_ << 4) * stride_y_;
214 uint8_t* margin_y_dst = y_dst + (mb_rows_ << 4) * stride_y_;
215 memcpy(margin_y_dst, margin_y_src, (height_ - (mb_rows_ << 4)) * stride_y_);
216 }
217 if ((mb_cols_ << 4) != width_) {
218 const uint8_t* margin_y_src = y_src + (mb_cols_ << 4);
219 uint8_t* margin_y_dst = y_dst + (mb_cols_ << 4);
220 for (int i = 0; i < height_; ++i) {
221 for (int j = mb_cols_ << 4; j < width_; ++j) {
222 margin_y_dst[i * stride_y_ + j] = margin_y_src[i * stride_y_ + j];
223 }
224 }
225 }
226}
227
jackychen8f9902a2015-11-26 02:59:48 -0800228void VideoDenoiser::DenoiseFrame(const VideoFrame& frame,
jackychenfa0befe2016-04-01 07:46:58 -0700229 VideoFrame* denoised_frame,
230 VideoFrame* denoised_frame_prev,
jackychenafaae0d2016-04-12 23:02:55 -0700231 bool noise_estimation_enabled) {
232 // If previous width and height are different from current frame's, need to
233 // reallocate the buffers and no denoising for the current frame.
jackychen8f9902a2015-11-26 02:59:48 -0800234 if (width_ != frame.width() || height_ != frame.height()) {
jackychenafaae0d2016-04-12 23:02:55 -0700235 DenoiserReset(frame, denoised_frame, denoised_frame_prev);
jackychen8f9902a2015-11-26 02:59:48 -0800236 return;
237 }
jackychenfa0befe2016-04-01 07:46:58 -0700238
jackychenafaae0d2016-04-12 23:02:55 -0700239 // Set buffer pointers.
nissec9c142f2016-05-17 04:05:47 -0700240 const uint8_t* y_src = frame.video_frame_buffer()->DataY();
241 const uint8_t* u_src = frame.video_frame_buffer()->DataU();
242 const uint8_t* v_src = frame.video_frame_buffer()->DataV();
243 uint8_t* y_dst = denoised_frame->video_frame_buffer()->MutableDataY();
244 uint8_t* u_dst = denoised_frame->video_frame_buffer()->MutableDataU();
245 uint8_t* v_dst = denoised_frame->video_frame_buffer()->MutableDataV();
246 uint8_t* y_dst_prev =
247 denoised_frame_prev->video_frame_buffer()->MutableDataY();
jackychenafaae0d2016-04-12 23:02:55 -0700248 memset(x_density_.get(), 0, mb_cols_);
249 memset(y_density_.get(), 0, mb_rows_);
250 memset(moving_object_.get(), 1, mb_cols_ * mb_rows_);
jackychenfa0befe2016-04-01 07:46:58 -0700251
jackychenafaae0d2016-04-12 23:02:55 -0700252 uint8_t noise_level = noise_estimation_enabled ? ne_->GetNoiseLevel() : 0;
jackychen9bfa1062016-05-03 11:21:26 -0700253 int thr_var_base = 16 * 16 * 2;
jackychenfa0befe2016-04-01 07:46:58 -0700254 // Loop over blocks to accumulate/extract noise level and update x/y_density
255 // factors for moving object detection.
jackychenafaae0d2016-04-12 23:02:55 -0700256 for (int mb_row = 0; mb_row < mb_rows_; ++mb_row) {
257 const int mb_index_base = mb_row * mb_cols_;
258 const int offset_base = (mb_row << 4) * stride_y_;
259 const uint8_t* mb_src_base = y_src + offset_base;
260 uint8_t* mb_dst_base = y_dst + offset_base;
261 uint8_t* mb_dst_prev_base = y_dst_prev + offset_base;
262 for (int mb_col = 0; mb_col < mb_cols_; ++mb_col) {
263 const int mb_index = mb_index_base + mb_col;
264 const bool ne_enable = (mb_index % NOISE_SUBSAMPLE_INTERVAL == 0);
265 const int pos_factor = PositionCheck(mb_row, mb_col, noise_level);
266 const uint32_t thr_var_adp = thr_var_base * pos_factor;
267 const uint32_t offset_col = mb_col << 4;
268 const uint8_t* mb_src = mb_src_base + offset_col;
269 uint8_t* mb_dst = mb_dst_base + offset_col;
270 uint8_t* mb_dst_prev = mb_dst_prev_base + offset_col;
271
272 // TODO(jackychen): Need SSE2/NEON opt.
273 int luma = 0;
274 if (ne_enable) {
275 for (int i = 4; i < 12; ++i) {
276 for (int j = 4; j < 12; ++j) {
277 luma += mb_src[i * stride_y_ + j];
278 }
jackychenfa0befe2016-04-01 07:46:58 -0700279 }
280 }
281
jackychenafaae0d2016-04-12 23:02:55 -0700282 // Get the filtered block and filter_decision.
283 mb_filter_decision_[mb_index] =
284 filter_->MbDenoise(mb_dst_prev, stride_y_, mb_dst, stride_y_, mb_src,
285 stride_y_, 0, noise_level);
jackychenfa0befe2016-04-01 07:46:58 -0700286
jackychenafaae0d2016-04-12 23:02:55 -0700287 // If filter decision is FILTER_BLOCK, no need to check moving edge.
288 // It is unlikely for a moving edge block to be filtered in current
289 // setting.
290 if (mb_filter_decision_[mb_index] == FILTER_BLOCK) {
291 uint32_t sse_t = 0;
292 if (ne_enable) {
293 // The variance used in noise estimation is based on the src block in
294 // time t (mb_src) and filtered block in time t-1 (mb_dist_prev).
295 uint32_t noise_var = filter_->Variance16x8(mb_dst_prev, stride_y_,
296 mb_src, stride_y_, &sse_t);
297 ne_->GetNoise(mb_index, noise_var, luma);
jackychenfa0befe2016-04-01 07:46:58 -0700298 }
jackychenafaae0d2016-04-12 23:02:55 -0700299 moving_edge_[mb_index] = 0; // Not a moving edge block.
jackychenfa0befe2016-04-01 07:46:58 -0700300 } else {
301 uint32_t sse_t = 0;
jackychenafaae0d2016-04-12 23:02:55 -0700302 // The variance used in MOD is based on the filtered blocks in time
303 // T (mb_dst) and T-1 (mb_dst_prev).
304 uint32_t noise_var = filter_->Variance16x8(mb_dst_prev, stride_y_,
305 mb_dst, stride_y_, &sse_t);
306 if (noise_var > thr_var_adp) { // Moving edge checking.
307 if (ne_enable) {
308 ne_->ResetConsecLowVar(mb_index);
309 }
310 moving_edge_[mb_index] = 1; // Mark as moving edge block.
311 x_density_[mb_col] += (pos_factor < 3);
312 y_density_[mb_row] += (pos_factor < 3);
jackychenfa0befe2016-04-01 07:46:58 -0700313 } else {
jackychenafaae0d2016-04-12 23:02:55 -0700314 moving_edge_[mb_index] = 0;
315 if (ne_enable) {
316 // The variance used in noise estimation is based on the src block
317 // in time t (mb_src) and filtered block in time t-1 (mb_dist_prev).
318 uint32_t noise_var = filter_->Variance16x8(
319 mb_dst_prev, stride_y_, mb_src, stride_y_, &sse_t);
320 ne_->GetNoise(mb_index, noise_var, luma);
321 }
jackychenfa0befe2016-04-01 07:46:58 -0700322 }
jackychenfa0befe2016-04-01 07:46:58 -0700323 }
jackychenafaae0d2016-04-12 23:02:55 -0700324 } // End of for loop
325 } // End of for loop
326
327 ReduceFalseDetection(moving_edge_, &moving_object_, noise_level);
328
329 CopySrcOnMOB(y_src, y_dst);
330
jackychen6650d6d2016-04-25 16:53:59 -0700331 // When frame width/height not divisible by 16, copy the margin to
332 // denoised_frame.
333 if ((mb_rows_ << 4) != height_ || (mb_cols_ << 4) != width_)
334 CopyLumaOnMargin(y_src, y_dst);
335
jackychenafaae0d2016-04-12 23:02:55 -0700336 // TODO(jackychen): Need SSE2/NEON opt.
337 // Copy u/v planes.
338 memcpy(u_dst, u_src, (height_ >> 1) * stride_u_);
339 memcpy(v_dst, v_src, (height_ >> 1) * stride_v_);
340
341 // Set time parameters to the output frame.
342 denoised_frame->set_timestamp(frame.timestamp());
343 denoised_frame->set_render_time_ms(frame.render_time_ms());
jackychenfa0befe2016-04-01 07:46:58 -0700344
jackychen8556c482016-04-20 16:04:31 -0700345#if DISPLAY || DISPLAYNEON
jackychenfa0befe2016-04-01 07:46:58 -0700346 // Show rectangular region
jackychenafaae0d2016-04-12 23:02:55 -0700347 ShowRect(filter_, moving_edge_, moving_object_, x_density_, y_density_, u_src,
348 v_src, u_dst, v_dst, mb_rows_, mb_cols_, stride_u_, stride_v_);
jackychenfa0befe2016-04-01 07:46:58 -0700349#endif
jackychen8f9902a2015-11-26 02:59:48 -0800350}
351
352} // namespace webrtc