2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/video_processing/main/source/denoising.h"
16 // Down-sampling in time (unit: number of frames)
17 enum { kSubsamplingTime = 0 };
18 // Sub-sampling in width (unit: power of 2.
19 enum { kSubsamplingWidth = 0 };
20 // Sub-sampling in height (unit: power of 2)
21 enum { kSubsamplingHeight = 0 };
22 // (Q8) De-noising filter parameter
23 enum { kDenoiseFiltParam = 179 };
24 // (Q8) 1 - filter parameter
25 enum { kDenoiseFiltParamRec = 77 };
26 // (Q8) De-noising threshold level
27 enum { kDenoiseThreshold = 19200 };
29 VPMDenoising::VPMDenoising()
36 VPMDenoising::~VPMDenoising() {
48 int32_t VPMDenoising::ChangeUniqueId(const int32_t id) {
53 void VPMDenoising::Reset() {
55 denoise_frame_cnt_ = 0;
68 int32_t VPMDenoising::ProcessFrame(I420VideoFrame* frame) {
77 int32_t num_pixels_changed = 0;
79 if (frame->IsZeroSize()) {
80 return VPM_GENERAL_ERROR;
83 int width = frame->width();
84 int height = frame->height();
86 /* Size of luminance component */
87 const uint32_t y_size = height * width;
90 if (y_size != frame_size_) {
100 moment1_ = new uint32_t[y_size];
101 memset(moment1_, 0, sizeof(uint32_t)*y_size);
105 moment2_ = new uint32_t[y_size];
106 memset(moment2_, 0, sizeof(uint32_t)*y_size);
109 /* Apply de-noising on each pixel, but update variance sub-sampled */
110 uint8_t* buffer = frame->buffer(kYPlane);
111 for (int i = 0; i < height; i++) { // Collect over height
113 ksub = ((i >> kSubsamplingHeight) << kSubsamplingHeight) * width;
114 for (int j = 0; j < width; j++) { // Collect over width
115 jsub = ((j >> kSubsamplingWidth) << kSubsamplingWidth);
116 /* Update mean value for every pixel and every frame */
117 tmp_moment1 = moment1_[k + j];
118 tmp_moment1 *= kDenoiseFiltParam; // Q16
119 tmp_moment1 += ((kDenoiseFiltParamRec * ((uint32_t)buffer[k + j])) << 8);
120 tmp_moment1 >>= 8; // Q8
121 moment1_[k + j] = tmp_moment1;
123 tmp_moment2 = moment2_[ksub + jsub];
124 if ((ksub == k) && (jsub == j) && (denoise_frame_cnt_ == 0)) {
125 tmp = ((uint32_t)buffer[k + j] *
126 (uint32_t)buffer[k + j]);
127 tmp_moment2 *= kDenoiseFiltParam; // Q16
128 tmp_moment2 += ((kDenoiseFiltParamRec * tmp) << 8);
129 tmp_moment2 >>= 8; // Q8
131 moment2_[k + j] = tmp_moment2;
132 /* Current event = deviation from mean value */
133 diff0 = ((int32_t)buffer[k + j] << 8) - moment1_[k + j];
134 /* Recent events = variance (variations over time) */
135 thevar = moment2_[k + j];
136 thevar -= ((moment1_[k + j] * moment1_[k + j]) >> 8);
137 // De-noising criteria, i.e., when should we replace a pixel by its mean.
138 // 1) recent events are minor.
139 // 2) current events are minor.
140 if ((thevar < kDenoiseThreshold)
141 && ((diff0 * diff0 >> 8) < kDenoiseThreshold)) {
142 // Replace with mean.
143 buffer[k + j] = (uint8_t)(moment1_[k + j] >> 8);
144 num_pixels_changed++;
149 denoise_frame_cnt_++;
150 if (denoise_frame_cnt_ > kSubsamplingTime)
151 denoise_frame_cnt_ = 0;
153 return num_pixels_changed;