You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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 "modules/video_processing/video_denoiser.h"
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include "api/video/i420_buffer.h"
  14. #include "third_party/libyuv/include/libyuv/planar_functions.h"
  15. namespace webrtc {
  16. #if DISPLAY || DISPLAYNEON
  17. static 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. static void ShowRect(const std::unique_ptr<DenoiserFilter>& filter,
  28. const std::unique_ptr<uint8_t[]>& d_status,
  29. const std::unique_ptr<uint8_t[]>& moving_edge_red,
  30. const std::unique_ptr<uint8_t[]>& x_density,
  31. const std::unique_ptr<uint8_t[]>& y_density,
  32. const uint8_t* u_src,
  33. int stride_u_src,
  34. const uint8_t* v_src,
  35. int stride_v_src,
  36. uint8_t* u_dst,
  37. int stride_u_dst,
  38. uint8_t* v_dst,
  39. int stride_v_dst,
  40. int mb_rows_,
  41. int mb_cols_) {
  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_src + (mb_col << 3);
  47. const uint8_t* mb_src_v =
  48. v_src + (mb_row << 3) * stride_v_src + (mb_col << 3);
  49. uint8_t* mb_dst_u = u_dst + (mb_row << 3) * stride_u_dst + (mb_col << 3);
  50. uint8_t* mb_dst_v = v_dst + (mb_row << 3) * stride_v_dst + (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_src, mb_dst_u, stride_u_dst);
  56. CopyMem8x8(uv_tmp, 8, mb_dst_v, stride_v_dst);
  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_dst);
  61. CopyMem8x8(mb_src_v, stride_v_src, mb_dst_v, stride_v_dst);
  62. } else {
  63. CopyMem8x8(mb_src_u, stride_u_src, mb_dst_u, stride_u_dst);
  64. CopyMem8x8(mb_src_v, stride_v_src, mb_dst_v, stride_v_dst);
  65. }
  66. }
  67. }
  68. }
  69. #endif
  70. VideoDenoiser::VideoDenoiser(bool runtime_cpu_detection)
  71. : width_(0),
  72. height_(0),
  73. filter_(DenoiserFilter::Create(runtime_cpu_detection, &cpu_type_)),
  74. ne_(new NoiseEstimation()) {}
  75. void VideoDenoiser::DenoiserReset(
  76. rtc::scoped_refptr<I420BufferInterface> frame) {
  77. width_ = frame->width();
  78. height_ = frame->height();
  79. mb_cols_ = width_ >> 4;
  80. mb_rows_ = height_ >> 4;
  81. // Init noise estimator and allocate buffers.
  82. ne_->Init(width_, height_, cpu_type_);
  83. moving_edge_.reset(new uint8_t[mb_cols_ * mb_rows_]);
  84. mb_filter_decision_.reset(new DenoiserDecision[mb_cols_ * mb_rows_]);
  85. x_density_.reset(new uint8_t[mb_cols_]);
  86. y_density_.reset(new uint8_t[mb_rows_]);
  87. moving_object_.reset(new uint8_t[mb_cols_ * mb_rows_]);
  88. }
  89. int VideoDenoiser::PositionCheck(int mb_row, int mb_col, int noise_level) {
  90. if (noise_level == 0)
  91. return 1;
  92. if ((mb_row <= (mb_rows_ >> 4)) || (mb_col <= (mb_cols_ >> 4)) ||
  93. (mb_col >= (15 * mb_cols_ >> 4)))
  94. return 3;
  95. else if ((mb_row <= (mb_rows_ >> 3)) || (mb_col <= (mb_cols_ >> 3)) ||
  96. (mb_col >= (7 * mb_cols_ >> 3)))
  97. return 2;
  98. else
  99. return 1;
  100. }
  101. void VideoDenoiser::ReduceFalseDetection(
  102. const std::unique_ptr<uint8_t[]>& d_status,
  103. std::unique_ptr<uint8_t[]>* moving_edge_red,
  104. int noise_level) {
  105. // From up left corner.
  106. int mb_col_stop = mb_cols_ - 1;
  107. for (int mb_row = 0; mb_row <= mb_rows_ - 1; ++mb_row) {
  108. for (int mb_col = 0; mb_col <= mb_col_stop; ++mb_col) {
  109. if (d_status[mb_row * mb_cols_ + mb_col]) {
  110. mb_col_stop = mb_col - 1;
  111. break;
  112. }
  113. (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
  114. }
  115. }
  116. // From bottom left corner.
  117. mb_col_stop = mb_cols_ - 1;
  118. for (int mb_row = mb_rows_ - 1; mb_row >= 0; --mb_row) {
  119. for (int mb_col = 0; mb_col <= mb_col_stop; ++mb_col) {
  120. if (d_status[mb_row * mb_cols_ + mb_col]) {
  121. mb_col_stop = mb_col - 1;
  122. break;
  123. }
  124. (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
  125. }
  126. }
  127. // From up right corner.
  128. mb_col_stop = 0;
  129. for (int mb_row = 0; mb_row <= mb_rows_ - 1; ++mb_row) {
  130. for (int mb_col = mb_cols_ - 1; mb_col >= mb_col_stop; --mb_col) {
  131. if (d_status[mb_row * mb_cols_ + mb_col]) {
  132. mb_col_stop = mb_col + 1;
  133. break;
  134. }
  135. (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
  136. }
  137. }
  138. // From bottom right corner.
  139. mb_col_stop = 0;
  140. for (int mb_row = mb_rows_ - 1; mb_row >= 0; --mb_row) {
  141. for (int mb_col = mb_cols_ - 1; mb_col >= mb_col_stop; --mb_col) {
  142. if (d_status[mb_row * mb_cols_ + mb_col]) {
  143. mb_col_stop = mb_col + 1;
  144. break;
  145. }
  146. (*moving_edge_red)[mb_row * mb_cols_ + mb_col] = 0;
  147. }
  148. }
  149. }
  150. bool VideoDenoiser::IsTrailingBlock(const std::unique_ptr<uint8_t[]>& d_status,
  151. int mb_row,
  152. int mb_col) {
  153. bool ret = false;
  154. int mb_index = mb_row * mb_cols_ + mb_col;
  155. if (!mb_row || !mb_col || mb_row == mb_rows_ - 1 || mb_col == mb_cols_ - 1)
  156. ret = false;
  157. else
  158. ret = d_status[mb_index + 1] || d_status[mb_index - 1] ||
  159. d_status[mb_index + mb_cols_] || d_status[mb_index - mb_cols_];
  160. return ret;
  161. }
  162. void VideoDenoiser::CopySrcOnMOB(const uint8_t* y_src,
  163. int stride_src,
  164. uint8_t* y_dst,
  165. int stride_dst) {
  166. // Loop over to copy src block if the block is marked as moving object block
  167. // or if the block may cause trailing artifacts.
  168. for (int mb_row = 0; mb_row < mb_rows_; ++mb_row) {
  169. const int mb_index_base = mb_row * mb_cols_;
  170. const uint8_t* mb_src_base = y_src + (mb_row << 4) * stride_src;
  171. uint8_t* mb_dst_base = y_dst + (mb_row << 4) * stride_dst;
  172. for (int mb_col = 0; mb_col < mb_cols_; ++mb_col) {
  173. const int mb_index = mb_index_base + mb_col;
  174. const uint32_t offset_col = mb_col << 4;
  175. const uint8_t* mb_src = mb_src_base + offset_col;
  176. uint8_t* mb_dst = mb_dst_base + offset_col;
  177. // Check if the block is a moving object block or may cause a trailing
  178. // artifacts.
  179. if (mb_filter_decision_[mb_index] != FILTER_BLOCK ||
  180. IsTrailingBlock(moving_edge_, mb_row, mb_col) ||
  181. (x_density_[mb_col] * y_density_[mb_row] &&
  182. moving_object_[mb_row * mb_cols_ + mb_col])) {
  183. // Copy y source.
  184. filter_->CopyMem16x16(mb_src, stride_src, mb_dst, stride_dst);
  185. }
  186. }
  187. }
  188. }
  189. void VideoDenoiser::CopyLumaOnMargin(const uint8_t* y_src,
  190. int stride_src,
  191. uint8_t* y_dst,
  192. int stride_dst) {
  193. int height_margin = height_ - (mb_rows_ << 4);
  194. if (height_margin > 0) {
  195. const uint8_t* margin_y_src = y_src + (mb_rows_ << 4) * stride_src;
  196. uint8_t* margin_y_dst = y_dst + (mb_rows_ << 4) * stride_dst;
  197. libyuv::CopyPlane(margin_y_src, stride_src, margin_y_dst, stride_dst,
  198. width_, height_margin);
  199. }
  200. int width_margin = width_ - (mb_cols_ << 4);
  201. if (width_margin > 0) {
  202. const uint8_t* margin_y_src = y_src + (mb_cols_ << 4);
  203. uint8_t* margin_y_dst = y_dst + (mb_cols_ << 4);
  204. libyuv::CopyPlane(margin_y_src, stride_src, margin_y_dst, stride_dst,
  205. width_ - (mb_cols_ << 4), mb_rows_ << 4);
  206. }
  207. }
  208. rtc::scoped_refptr<I420BufferInterface> VideoDenoiser::DenoiseFrame(
  209. rtc::scoped_refptr<I420BufferInterface> frame,
  210. bool noise_estimation_enabled) {
  211. // If previous width and height are different from current frame's, need to
  212. // reallocate the buffers and no denoising for the current frame.
  213. if (!prev_buffer_ || width_ != frame->width() || height_ != frame->height()) {
  214. DenoiserReset(frame);
  215. prev_buffer_ = frame;
  216. return frame;
  217. }
  218. // Set buffer pointers.
  219. const uint8_t* y_src = frame->DataY();
  220. int stride_y_src = frame->StrideY();
  221. rtc::scoped_refptr<I420Buffer> dst =
  222. buffer_pool_.CreateBuffer(width_, height_);
  223. uint8_t* y_dst = dst->MutableDataY();
  224. int stride_y_dst = dst->StrideY();
  225. const uint8_t* y_dst_prev = prev_buffer_->DataY();
  226. int stride_prev = prev_buffer_->StrideY();
  227. memset(x_density_.get(), 0, mb_cols_);
  228. memset(y_density_.get(), 0, mb_rows_);
  229. memset(moving_object_.get(), 1, mb_cols_ * mb_rows_);
  230. uint8_t noise_level = noise_estimation_enabled ? ne_->GetNoiseLevel() : 0;
  231. int thr_var_base = 16 * 16 * 2;
  232. // Loop over blocks to accumulate/extract noise level and update x/y_density
  233. // factors for moving object detection.
  234. for (int mb_row = 0; mb_row < mb_rows_; ++mb_row) {
  235. const int mb_index_base = mb_row * mb_cols_;
  236. const uint8_t* mb_src_base = y_src + (mb_row << 4) * stride_y_src;
  237. uint8_t* mb_dst_base = y_dst + (mb_row << 4) * stride_y_dst;
  238. const uint8_t* mb_dst_prev_base = y_dst_prev + (mb_row << 4) * stride_prev;
  239. for (int mb_col = 0; mb_col < mb_cols_; ++mb_col) {
  240. const int mb_index = mb_index_base + mb_col;
  241. const bool ne_enable = (mb_index % NOISE_SUBSAMPLE_INTERVAL == 0);
  242. const int pos_factor = PositionCheck(mb_row, mb_col, noise_level);
  243. const uint32_t thr_var_adp = thr_var_base * pos_factor;
  244. const uint32_t offset_col = mb_col << 4;
  245. const uint8_t* mb_src = mb_src_base + offset_col;
  246. uint8_t* mb_dst = mb_dst_base + offset_col;
  247. const uint8_t* mb_dst_prev = mb_dst_prev_base + offset_col;
  248. // TODO(jackychen): Need SSE2/NEON opt.
  249. int luma = 0;
  250. if (ne_enable) {
  251. for (int i = 4; i < 12; ++i) {
  252. for (int j = 4; j < 12; ++j) {
  253. luma += mb_src[i * stride_y_src + j];
  254. }
  255. }
  256. }
  257. // Get the filtered block and filter_decision.
  258. mb_filter_decision_[mb_index] =
  259. filter_->MbDenoise(mb_dst_prev, stride_prev, mb_dst, stride_y_dst,
  260. mb_src, stride_y_src, 0, noise_level);
  261. // If filter decision is FILTER_BLOCK, no need to check moving edge.
  262. // It is unlikely for a moving edge block to be filtered in current
  263. // setting.
  264. if (mb_filter_decision_[mb_index] == FILTER_BLOCK) {
  265. uint32_t sse_t = 0;
  266. if (ne_enable) {
  267. // The variance used in noise estimation is based on the src block in
  268. // time t (mb_src) and filtered block in time t-1 (mb_dist_prev).
  269. uint32_t noise_var = filter_->Variance16x8(
  270. mb_dst_prev, stride_y_dst, mb_src, stride_y_src, &sse_t);
  271. ne_->GetNoise(mb_index, noise_var, luma);
  272. }
  273. moving_edge_[mb_index] = 0; // Not a moving edge block.
  274. } else {
  275. uint32_t sse_t = 0;
  276. // The variance used in MOD is based on the filtered blocks in time
  277. // T (mb_dst) and T-1 (mb_dst_prev).
  278. uint32_t noise_var = filter_->Variance16x8(
  279. mb_dst_prev, stride_prev, mb_dst, stride_y_dst, &sse_t);
  280. if (noise_var > thr_var_adp) { // Moving edge checking.
  281. if (ne_enable) {
  282. ne_->ResetConsecLowVar(mb_index);
  283. }
  284. moving_edge_[mb_index] = 1; // Mark as moving edge block.
  285. x_density_[mb_col] += (pos_factor < 3);
  286. y_density_[mb_row] += (pos_factor < 3);
  287. } else {
  288. moving_edge_[mb_index] = 0;
  289. if (ne_enable) {
  290. // The variance used in noise estimation is based on the src block
  291. // in time t (mb_src) and filtered block in time t-1 (mb_dist_prev).
  292. uint32_t noise_var = filter_->Variance16x8(
  293. mb_dst_prev, stride_prev, mb_src, stride_y_src, &sse_t);
  294. ne_->GetNoise(mb_index, noise_var, luma);
  295. }
  296. }
  297. }
  298. } // End of for loop
  299. } // End of for loop
  300. ReduceFalseDetection(moving_edge_, &moving_object_, noise_level);
  301. CopySrcOnMOB(y_src, stride_y_src, y_dst, stride_y_dst);
  302. // When frame width/height not divisible by 16, copy the margin to
  303. // denoised_frame.
  304. if ((mb_rows_ << 4) != height_ || (mb_cols_ << 4) != width_)
  305. CopyLumaOnMargin(y_src, stride_y_src, y_dst, stride_y_dst);
  306. // Copy u/v planes.
  307. libyuv::CopyPlane(frame->DataU(), frame->StrideU(), dst->MutableDataU(),
  308. dst->StrideU(), (width_ + 1) >> 1, (height_ + 1) >> 1);
  309. libyuv::CopyPlane(frame->DataV(), frame->StrideV(), dst->MutableDataV(),
  310. dst->StrideV(), (width_ + 1) >> 1, (height_ + 1) >> 1);
  311. #if DISPLAY || DISPLAYNEON
  312. // Show rectangular region
  313. ShowRect(filter_, moving_edge_, moving_object_, x_density_, y_density_,
  314. frame->DataU(), frame->StrideU(), frame->DataV(), frame->StrideV(),
  315. dst->MutableDataU(), dst->StrideU(), dst->MutableDataV(),
  316. dst->StrideV(), mb_rows_, mb_cols_);
  317. #endif
  318. prev_buffer_ = dst;
  319. return dst;
  320. }
  321. } // namespace webrtc