Chromium Code Reviews

Side by Side Diff: webrtc/modules/video_processing/video_denoiser.cc

Issue 1466763002: Standalone denoiser (off by default). (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
(Empty)
1 /*
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
14 namespace webrtc {
15
16 VideoDenoiser::VideoDenoiser() : width_(0), height_(0), metrics_(nullptr) {
17 filter_.reset(DenoiserFilter::Create());
stefan-webrtc 2015/11/23 10:12:53 Assign in the initializer list instead.
jackychen 2015/11/23 19:45:00 Done.
18 }
19
20 VideoDenoiser::~VideoDenoiser() {
21 if (metrics_)
22 delete[] metrics_;
23 }
24
25 void VideoDenoiser::TrailingReduction(int mb_rows,
26 int mb_cols,
27 const uint8_t* y_src,
28 int stride_y,
29 uint8_t* y_dst) {
30 for (int mb_row = 1; mb_row < mb_rows - 1; ++mb_row) {
31 for (int mb_col = 1; mb_col < mb_cols - 1; ++mb_col) {
32 int mb_index = mb_row * mb_cols + mb_col;
33 uint8_t* mb_dst = y_dst + (mb_row << 4) * stride_y + (mb_col << 4);
34 const uint8_t* mb_src = y_src + (mb_row << 4) * stride_y + (mb_col << 4);
35 // If the number of denoised neighbors is less than a threshold,
36 // do NOT denoise for the block. Set different threshold for skin MB.
37 // The change of denoising status will not propagate.
38 if (metrics_[mb_index].is_skin) {
39 // The threshold is high (more strict) for non-skin MB where the trailing
40 // usually happen.
41 if (metrics_[mb_index].denoise &&
42 metrics_[mb_index + 1].denoise +
43 metrics_[mb_index - 1].denoise +
44 metrics_[mb_index + mb_cols].denoise +
45 metrics_[mb_index - mb_cols].denoise <= 2) {
46 metrics_[mb_index].denoise = 0;
47 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
48 }
49 } else if (metrics_[mb_index].denoise &&
50 metrics_[mb_index + 1].denoise +
51 metrics_[mb_index - 1].denoise +
52 metrics_[mb_index + mb_cols + 1].denoise +
53 metrics_[mb_index + mb_cols - 1].denoise +
54 metrics_[mb_index - mb_cols + 1].denoise +
55 metrics_[mb_index - mb_cols - 1].denoise +
56 metrics_[mb_index + mb_cols].denoise +
57 metrics_[mb_index - mb_cols].denoise <= 7) {
58 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
59 }
60 }
61 }
62 }
63
64 void VideoDenoiser::DenoiseFrame(const VideoFrame& frame,
65 VideoFrame* denoised_frame) {
66 int stride_y = frame.stride(kYPlane);
67 int stride_u = frame.stride(kUPlane);
68 int stride_v = frame.stride(kVPlane);
69 // If previous width and height are different from current frame's, then no
70 // denoising for the current frame.
71 if (width_ != frame.width() || height_ != frame.height()) {
72 width_ = frame.width();
73 height_ = frame.height();
74 denoised_frame->CreateFrame(frame.buffer(kYPlane), frame.buffer(kUPlane),
75 frame.buffer(kVPlane), width_, height_,
76 stride_y, stride_u, stride_v);
77 // Setting time parameters to the output frame.
78 denoised_frame->set_timestamp(frame.timestamp());
79 denoised_frame->set_render_time_ms(frame.render_time_ms());
80 return;
81 }
82 // For 16x16 block.
83 int mb_cols = width_ >> 4;
84 int mb_rows = height_ >> 4;
85 // Denoise on Y plane.
86 uint8_t* y_dst = denoised_frame->buffer(kYPlane);
87 uint8_t* u_dst = denoised_frame->buffer(kUPlane);
88 uint8_t* v_dst = denoised_frame->buffer(kVPlane);
89 const uint8_t* y_src = frame.buffer(kYPlane);
90 const uint8_t* u_src = frame.buffer(kUPlane);
91 const uint8_t* v_src = frame.buffer(kVPlane);
92 // Temporary buffer to store denoising result.
93 uint8_t y_tmp[16 * 16] = {0};
94 if (metrics_ == NULL)
95 metrics_ = new DenoiseMetrics[mb_cols * mb_rows];
96 for (int mb_row = 0; mb_row < mb_rows; ++mb_row) {
97 for (int mb_col = 0; mb_col < mb_cols; ++mb_col) {
98 const uint8_t* mb_src =
99 y_src + (mb_row << 4) * stride_y + (mb_col << 4);
100 uint8_t* mb_dst = y_dst + (mb_row << 4) * stride_y + (mb_col << 4);
101 int mb_index = mb_row * mb_cols + mb_col;
102 // Denoise each MB at the very start and save the result to a temporal
stefan-webrtc 2015/11/23 10:12:53 temporary buffer
jackychen 2015/11/23 19:45:00 Done.
103 // buffer.
104 if (filter_->MbDenoise(
105 mb_dst, stride_y, y_tmp, 16, mb_src, stride_y, 0, 1) ==
106 FILTER_BLOCK) {
107 uint32_t thr_var = 0;
108 // Save var and sad to the buffer.
109 metrics_[mb_index].var = filter_->Variance16x8(
110 mb_dst, stride_y, y_tmp, 16, &metrics_[mb_index].sad);
111 // Get skin map.
112 metrics_[mb_index].is_skin =
113 video_skin_pixel(y_src, u_src, v_src, stride_y, stride_u, stride_v,
114 mb_row, mb_col);
115 // Variance threshold for skin/non-skin MB is different.
116 // Skin MB use a small threshold to reduce blockiness.
117 thr_var = metrics_[mb_index].is_skin ? 128 : 12 * 128;
118 if (metrics_[mb_index].var > thr_var) {
119 metrics_[mb_index].denoise = 0;
120 // Use the source MB.
121 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
122 } else {
123 metrics_[mb_index].denoise = 1;
124 // Use the denoised MB.
125 filter_->CopyMem16x16(y_tmp, 16, mb_dst, stride_y);
126 }
127 } else {
128 metrics_[mb_index].denoise = 0;
129 filter_->CopyMem16x16(mb_src, stride_y, mb_dst, stride_y);
130 }
131 // Copy source U/V plane.
132 const uint8_t* mb_src_u =
133 u_src + (mb_row << 3) * stride_u + (mb_col << 3);
134 const uint8_t* mb_src_v =
135 v_src + (mb_row << 3) * stride_v + (mb_col << 3);
136 uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u + (mb_col << 3);
137 uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v + (mb_col << 3);
138 filter_->CopyMem8x8(mb_src_u, stride_u, mb_dst_u, stride_u);
139 filter_->CopyMem8x8(mb_src_v, stride_v, mb_dst_v, stride_v);
140 }
141 }
142 // Second round.
143 // This is to reduce the trailing artifact and blockiness by referring
144 // neighbors' denoising status.
145 TrailingReduction(mb_rows, mb_cols, y_src, stride_y, y_dst);
146
147 // Setting time parameters to the output frame.
148 denoised_frame->set_timestamp(frame.timestamp());
149 denoised_frame->set_render_time_ms(frame.render_time_ms());
150 return;
151 }
152
153 } // namespace webrtc
OLDNEW

Powered by Google App Engine