niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 7dfa883 | 2012-02-08 08:27:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 6f3d8fc | 2013-05-27 14:12:16 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/video_processing/main/source/denoising.h" |
| 12 | #include "webrtc/system_wrappers/interface/trace.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
| 14 | #include <cstring> |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | enum { kSubsamplingTime = 0 }; // Down-sampling in time (unit: number of frames) |
stefan@webrtc.org | 7dfa883 | 2012-02-08 08:27:31 +0000 | [diff] [blame] | 19 | enum { kSubsamplingWidth = 0 }; // Sub-sampling in width (unit: power of 2) |
| 20 | enum { kSubsamplingHeight = 0 }; // Sub-sampling in height (unit: power of 2) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | enum { kDenoiseFiltParam = 179 }; // (Q8) De-noising filter parameter |
| 22 | enum { kDenoiseFiltParamRec = 77 }; // (Q8) 1 - filter parameter |
| 23 | enum { kDenoiseThreshold = 19200 }; // (Q8) De-noising threshold level |
| 24 | |
| 25 | VPMDenoising::VPMDenoising() : |
| 26 | _id(0), |
| 27 | _moment1(NULL), |
| 28 | _moment2(NULL) |
| 29 | { |
| 30 | Reset(); |
| 31 | } |
| 32 | |
| 33 | VPMDenoising::~VPMDenoising() |
| 34 | { |
| 35 | if (_moment1) |
| 36 | { |
| 37 | delete [] _moment1; |
| 38 | _moment1 = NULL; |
| 39 | } |
| 40 | |
| 41 | if (_moment2) |
| 42 | { |
| 43 | delete [] _moment2; |
| 44 | _moment2 = NULL; |
| 45 | } |
| 46 | } |
| 47 | |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 48 | int32_t |
| 49 | VPMDenoising::ChangeUniqueId(const int32_t id) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | { |
| 51 | _id = id; |
| 52 | return VPM_OK; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | VPMDenoising::Reset() |
| 57 | { |
| 58 | _frameSize = 0; |
| 59 | _denoiseFrameCnt = 0; |
| 60 | |
| 61 | if (_moment1) |
| 62 | { |
| 63 | delete [] _moment1; |
| 64 | _moment1 = NULL; |
| 65 | } |
| 66 | |
| 67 | if (_moment2) |
| 68 | { |
| 69 | delete [] _moment2; |
| 70 | _moment2 = NULL; |
| 71 | } |
| 72 | } |
| 73 | |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 74 | int32_t |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 75 | VPMDenoising::ProcessFrame(I420VideoFrame* frame) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | { |
mikhal@webrtc.org | 0e196e1 | 2012-10-19 15:43:31 +0000 | [diff] [blame] | 77 | assert(frame); |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 78 | int32_t thevar; |
mikhal@webrtc.org | 0e196e1 | 2012-10-19 15:43:31 +0000 | [diff] [blame] | 79 | int k; |
| 80 | int jsub, ksub; |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 81 | int32_t diff0; |
| 82 | uint32_t tmpMoment1; |
| 83 | uint32_t tmpMoment2; |
| 84 | uint32_t tmp; |
| 85 | int32_t numPixelsChanged = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 87 | if (frame->IsZeroSize()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | { |
mikhal@webrtc.org | 0e196e1 | 2012-10-19 15:43:31 +0000 | [diff] [blame] | 89 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoPreocessing, _id, |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 90 | "zero size frame"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | return VPM_GENERAL_ERROR; |
| 92 | } |
| 93 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 94 | int width = frame->width(); |
| 95 | int height = frame->height(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
| 97 | /* Size of luminance component */ |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 98 | const uint32_t ysize = height * width; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | |
| 100 | /* Initialization */ |
| 101 | if (ysize != _frameSize) |
| 102 | { |
| 103 | delete [] _moment1; |
| 104 | _moment1 = NULL; |
| 105 | |
| 106 | delete [] _moment2; |
| 107 | _moment2 = NULL; |
| 108 | } |
| 109 | _frameSize = ysize; |
| 110 | |
| 111 | if (!_moment1) |
| 112 | { |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 113 | _moment1 = new uint32_t[ysize]; |
| 114 | memset(_moment1, 0, sizeof(uint32_t)*ysize); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (!_moment2) |
| 118 | { |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 119 | _moment2 = new uint32_t[ysize]; |
| 120 | memset(_moment2, 0, sizeof(uint32_t)*ysize); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* Apply de-noising on each pixel, but update variance sub-sampled */ |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 124 | uint8_t* buffer = frame->buffer(kYPlane); |
mikhal@webrtc.org | 0e196e1 | 2012-10-19 15:43:31 +0000 | [diff] [blame] | 125 | for (int i = 0; i < height; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | { // Collect over height |
| 127 | k = i * width; |
| 128 | ksub = ((i >> kSubsamplingHeight) << kSubsamplingHeight) * width; |
mikhal@webrtc.org | 0e196e1 | 2012-10-19 15:43:31 +0000 | [diff] [blame] | 129 | for (int j = 0; j < width; j++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | { // Collect over width |
| 131 | jsub = ((j >> kSubsamplingWidth) << kSubsamplingWidth); |
| 132 | /* Update mean value for every pixel and every frame */ |
| 133 | tmpMoment1 = _moment1[k + j]; |
| 134 | tmpMoment1 *= kDenoiseFiltParam; // Q16 |
mikhal@webrtc.org | 0e196e1 | 2012-10-19 15:43:31 +0000 | [diff] [blame] | 135 | tmpMoment1 += ((kDenoiseFiltParamRec * |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 136 | ((uint32_t)buffer[k + j])) << 8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | tmpMoment1 >>= 8; // Q8 |
| 138 | _moment1[k + j] = tmpMoment1; |
| 139 | |
| 140 | tmpMoment2 = _moment2[ksub + jsub]; |
| 141 | if ((ksub == k) && (jsub == j) && (_denoiseFrameCnt == 0)) |
| 142 | { |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 143 | tmp = ((uint32_t)buffer[k + j] * |
| 144 | (uint32_t)buffer[k + j]); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | tmpMoment2 *= kDenoiseFiltParam; // Q16 |
| 146 | tmpMoment2 += ((kDenoiseFiltParamRec * tmp)<<8); |
| 147 | tmpMoment2 >>= 8; // Q8 |
| 148 | } |
| 149 | _moment2[k + j] = tmpMoment2; |
| 150 | /* Current event = deviation from mean value */ |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 151 | diff0 = ((int32_t)buffer[k + j] << 8) - _moment1[k + j]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | /* Recent events = variance (variations over time) */ |
| 153 | thevar = _moment2[k + j]; |
| 154 | thevar -= ((_moment1[k + j] * _moment1[k + j]) >> 8); |
| 155 | /*************************************************************************** |
| 156 | * De-noising criteria, i.e., when should we replace a pixel by its mean |
| 157 | * |
| 158 | * 1) recent events are minor |
| 159 | * 2) current events are minor |
| 160 | ***************************************************************************/ |
| 161 | if ((thevar < kDenoiseThreshold) |
| 162 | && ((diff0 * diff0 >> 8) < kDenoiseThreshold)) |
| 163 | { // Replace with mean |
pbos@webrtc.org | 1ab45f6 | 2013-04-09 13:38:10 +0000 | [diff] [blame] | 164 | buffer[k + j] = (uint8_t)(_moment1[k + j] >> 8); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | numPixelsChanged++; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* Update frame counter */ |
| 171 | _denoiseFrameCnt++; |
| 172 | if (_denoiseFrameCnt > kSubsamplingTime) |
| 173 | { |
| 174 | _denoiseFrameCnt = 0; |
| 175 | } |
| 176 | |
| 177 | return numPixelsChanged; |
| 178 | } |
| 179 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 180 | } // namespace |