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 
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include <memory>
16 #include <string>
17 
18 #include "api/scoped_refptr.h"
19 #include "api/video/i420_buffer.h"
20 #include "api/video/video_frame_buffer.h"
21 #include "modules/video_processing/util/denoiser_filter.h"
22 #include "modules/video_processing/util/skin_detection.h"
23 #include "modules/video_processing/video_denoiser.h"
24 #include "test/frame_utils.h"
25 #include "test/gtest.h"
26 #include "test/testsupport/file_utils.h"
27 
28 namespace webrtc {
29 
TEST(VideoDenoiserTest,CopyMem)30 TEST(VideoDenoiserTest, CopyMem) {
31   std::unique_ptr<DenoiserFilter> df_c(DenoiserFilter::Create(false, nullptr));
32   std::unique_ptr<DenoiserFilter> df_sse_neon(
33       DenoiserFilter::Create(true, nullptr));
34   uint8_t src[16 * 16], dst[16 * 16];
35   for (int i = 0; i < 16; ++i) {
36     for (int j = 0; j < 16; ++j) {
37       src[i * 16 + j] = i * 16 + j;
38     }
39   }
40 
41   memset(dst, 0, 16 * 16);
42   df_c->CopyMem16x16(src, 16, dst, 16);
43   EXPECT_EQ(0, memcmp(src, dst, 16 * 16));
44 
45   memset(dst, 0, 16 * 16);
46   df_sse_neon->CopyMem16x16(src, 16, dst, 16);
47   EXPECT_EQ(0, memcmp(src, dst, 16 * 16));
48 }
49 
TEST(VideoDenoiserTest,Variance)50 TEST(VideoDenoiserTest, Variance) {
51   std::unique_ptr<DenoiserFilter> df_c(DenoiserFilter::Create(false, nullptr));
52   std::unique_ptr<DenoiserFilter> df_sse_neon(
53       DenoiserFilter::Create(true, nullptr));
54   uint8_t src[16 * 16], dst[16 * 16];
55   uint32_t sum = 0, sse = 0, var;
56   for (int i = 0; i < 16; ++i) {
57     for (int j = 0; j < 16; ++j) {
58       src[i * 16 + j] = i * 16 + j;
59     }
60   }
61   // Compute the 16x8 variance of the 16x16 block.
62   for (int i = 0; i < 8; ++i) {
63     for (int j = 0; j < 16; ++j) {
64       sum += (i * 32 + j);
65       sse += (i * 32 + j) * (i * 32 + j);
66     }
67   }
68   var = sse - ((sum * sum) >> 7);
69   memset(dst, 0, 16 * 16);
70   EXPECT_EQ(var, df_c->Variance16x8(src, 16, dst, 16, &sse));
71   EXPECT_EQ(var, df_sse_neon->Variance16x8(src, 16, dst, 16, &sse));
72 }
73 
TEST(VideoDenoiserTest,MbDenoise)74 TEST(VideoDenoiserTest, MbDenoise) {
75   std::unique_ptr<DenoiserFilter> df_c(DenoiserFilter::Create(false, nullptr));
76   std::unique_ptr<DenoiserFilter> df_sse_neon(
77       DenoiserFilter::Create(true, nullptr));
78   uint8_t running_src[16 * 16], src[16 * 16];
79   uint8_t dst[16 * 16], dst_sse_neon[16 * 16];
80 
81   // Test case: |diff| <= |3 + shift_inc1|
82   for (int i = 0; i < 16; ++i) {
83     for (int j = 0; j < 16; ++j) {
84       running_src[i * 16 + j] = i * 11 + j;
85       src[i * 16 + j] = i * 11 + j + 2;
86     }
87   }
88   memset(dst, 0, 16 * 16);
89   df_c->MbDenoise(running_src, 16, dst, 16, src, 16, 0, 1);
90   memset(dst_sse_neon, 0, 16 * 16);
91   df_sse_neon->MbDenoise(running_src, 16, dst_sse_neon, 16, src, 16, 0, 1);
92   EXPECT_EQ(0, memcmp(dst, dst_sse_neon, 16 * 16));
93 
94   // Test case: |diff| >= |4 + shift_inc1|
95   for (int i = 0; i < 16; ++i) {
96     for (int j = 0; j < 16; ++j) {
97       running_src[i * 16 + j] = i * 11 + j;
98       src[i * 16 + j] = i * 11 + j + 5;
99     }
100   }
101   memset(dst, 0, 16 * 16);
102   df_c->MbDenoise(running_src, 16, dst, 16, src, 16, 0, 1);
103   memset(dst_sse_neon, 0, 16 * 16);
104   df_sse_neon->MbDenoise(running_src, 16, dst_sse_neon, 16, src, 16, 0, 1);
105   EXPECT_EQ(0, memcmp(dst, dst_sse_neon, 16 * 16));
106 
107   // Test case: |diff| >= 8
108   for (int i = 0; i < 16; ++i) {
109     for (int j = 0; j < 16; ++j) {
110       running_src[i * 16 + j] = i * 11 + j;
111       src[i * 16 + j] = i * 11 + j + 8;
112     }
113   }
114   memset(dst, 0, 16 * 16);
115   df_c->MbDenoise(running_src, 16, dst, 16, src, 16, 0, 1);
116   memset(dst_sse_neon, 0, 16 * 16);
117   df_sse_neon->MbDenoise(running_src, 16, dst_sse_neon, 16, src, 16, 0, 1);
118   EXPECT_EQ(0, memcmp(dst, dst_sse_neon, 16 * 16));
119 
120   // Test case: |diff| > 15
121   for (int i = 0; i < 16; ++i) {
122     for (int j = 0; j < 16; ++j) {
123       running_src[i * 16 + j] = i * 11 + j;
124       src[i * 16 + j] = i * 11 + j + 16;
125     }
126   }
127   memset(dst, 0, 16 * 16);
128   DenoiserDecision decision =
129       df_c->MbDenoise(running_src, 16, dst, 16, src, 16, 0, 1);
130   EXPECT_EQ(COPY_BLOCK, decision);
131   decision = df_sse_neon->MbDenoise(running_src, 16, dst, 16, src, 16, 0, 1);
132   EXPECT_EQ(COPY_BLOCK, decision);
133 }
134 
TEST(VideoDenoiserTest,Denoiser)135 TEST(VideoDenoiserTest, Denoiser) {
136   const int kWidth = 352;
137   const int kHeight = 288;
138 
139   const std::string video_file =
140       webrtc::test::ResourcePath("foreman_cif", "yuv");
141   FILE* source_file = fopen(video_file.c_str(), "rb");
142   ASSERT_TRUE(source_file != nullptr)
143       << "Cannot open source file: " << video_file;
144 
145   // Create pure C denoiser.
146   VideoDenoiser denoiser_c(false);
147   // Create SSE or NEON denoiser.
148   VideoDenoiser denoiser_sse_neon(true);
149 
150   for (;;) {
151     rtc::scoped_refptr<I420BufferInterface> video_frame_buffer(
152         test::ReadI420Buffer(kWidth, kHeight, source_file));
153     if (!video_frame_buffer)
154       break;
155 
156     rtc::scoped_refptr<I420BufferInterface> denoised_frame_c(
157         denoiser_c.DenoiseFrame(video_frame_buffer, false));
158     rtc::scoped_refptr<I420BufferInterface> denoised_frame_sse_neon(
159         denoiser_sse_neon.DenoiseFrame(video_frame_buffer, false));
160 
161     // Denoising results should be the same for C and SSE/NEON denoiser.
162     ASSERT_TRUE(
163         test::FrameBufsEqual(denoised_frame_c, denoised_frame_sse_neon));
164   }
165   ASSERT_NE(0, feof(source_file)) << "Error reading source file";
166 }
167 
168 }  // namespace webrtc
169