brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 <stdio.h> |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 13 | |
kjellander@webrtc.org | 8126602 | 2013-01-22 22:45:59 +0000 | [diff] [blame] | 14 | #include <fstream> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 15 | #include <memory> |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 16 | |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 17 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 18 | #include "webrtc/rtc_tools/frame_editing/frame_editing_lib.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 19 | #include "webrtc/test/gtest.h" |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 20 | #include "webrtc/test/testsupport/fileutils.h" |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | namespace test { |
| 24 | |
| 25 | const int kWidth = 352; |
| 26 | const int kHeight = 288; |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 27 | const size_t kFrameSize = CalcBufferSize(VideoType::kI420, kWidth, kHeight); |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 28 | |
| 29 | class FrameEditingTest : public ::testing::Test { |
| 30 | protected: |
| 31 | virtual void SetUp() { |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 32 | reference_video_ = ResourcePath("foreman_cif", "yuv"); |
kjellander@webrtc.org | e794c36 | 2014-09-29 11:47:28 +0000 | [diff] [blame] | 33 | test_video_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), |
| 34 | "frame_editing_unittest.yuv"); |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 35 | |
| 36 | original_fid_ = fopen(reference_video_.c_str(), "rb"); |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 37 | ASSERT_TRUE(original_fid_ != NULL); |
kjellander@webrtc.org | 8126602 | 2013-01-22 22:45:59 +0000 | [diff] [blame] | 38 | |
| 39 | // Ensure the output file exists on disk. |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 40 | std::ofstream(test_video_.c_str(), std::ios::out); |
kjellander@webrtc.org | 8126602 | 2013-01-22 22:45:59 +0000 | [diff] [blame] | 41 | // Open the output file for reading. |
| 42 | // TODO(holmer): Figure out why this file has to be opened here (test fails |
| 43 | // if it's opened after the write operation performed in EditFrames). |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 44 | edited_fid_ = fopen(test_video_.c_str(), "rb"); |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 45 | ASSERT_TRUE(edited_fid_ != NULL); |
kjellander@webrtc.org | 8126602 | 2013-01-22 22:45:59 +0000 | [diff] [blame] | 46 | |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 47 | original_buffer_.reset(new int[kFrameSize]); |
| 48 | edited_buffer_.reset(new int[kFrameSize]); |
| 49 | num_frames_read_ = 0; |
| 50 | } |
| 51 | virtual void TearDown() { |
| 52 | fclose(original_fid_); |
| 53 | fclose(edited_fid_); |
kjellander@webrtc.org | e794c36 | 2014-09-29 11:47:28 +0000 | [diff] [blame] | 54 | remove(test_video_.c_str()); |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 55 | } |
| 56 | // Compares the frames in both streams to the end of one of the streams. |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 57 | void CompareToTheEnd(FILE* test_video_fid, |
| 58 | FILE* ref_video_fid, |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 59 | std::unique_ptr<int[]>* ref_buffer, |
| 60 | std::unique_ptr<int[]>* test_buffer) { |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 61 | while (!feof(test_video_fid) && !feof(ref_video_fid)) { |
| 62 | num_bytes_read_ = fread(ref_buffer->get(), 1, kFrameSize, ref_video_fid); |
| 63 | if (!feof(ref_video_fid)) { |
| 64 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 65 | } |
| 66 | num_bytes_read_ = fread(test_buffer->get(), 1, kFrameSize, |
| 67 | test_video_fid); |
| 68 | if (!feof(test_video_fid)) { |
| 69 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 70 | } |
| 71 | if (!feof(test_video_fid) && !feof(test_video_fid)) { |
| 72 | EXPECT_EQ(0, memcmp(ref_buffer->get(), test_buffer->get(), |
| 73 | kFrameSize)); |
| 74 | } |
| 75 | } |
| 76 | // There should not be anything left in either stream. |
| 77 | EXPECT_EQ(!feof(test_video_fid), !feof(ref_video_fid)); |
| 78 | } |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 79 | std::string reference_video_; |
| 80 | std::string test_video_; |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 81 | FILE* original_fid_; |
| 82 | FILE* edited_fid_; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 83 | size_t num_bytes_read_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 84 | std::unique_ptr<int[]> original_buffer_; |
| 85 | std::unique_ptr<int[]> edited_buffer_; |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 86 | int num_frames_read_; |
| 87 | }; |
| 88 | |
| 89 | TEST_F(FrameEditingTest, ValidInPath) { |
| 90 | const int kFirstFrameToProcess = 160; |
| 91 | const int kInterval = -1; |
| 92 | const int kLastFrameToProcess = 240; |
| 93 | |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 94 | int result = EditFrames(reference_video_, kWidth, kHeight, |
| 95 | kFirstFrameToProcess, kInterval, kLastFrameToProcess, |
| 96 | test_video_); |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 97 | EXPECT_EQ(0, result); |
| 98 | |
| 99 | for (int i = 1; i < kFirstFrameToProcess; ++i) { |
| 100 | num_bytes_read_ = fread(original_buffer_.get(), 1, kFrameSize, |
| 101 | original_fid_); |
| 102 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 103 | |
| 104 | num_bytes_read_ = fread(edited_buffer_.get(), 1, kFrameSize, edited_fid_); |
| 105 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 106 | |
| 107 | EXPECT_EQ(0, memcmp(original_buffer_.get(), edited_buffer_.get(), |
| 108 | kFrameSize)); |
| 109 | } |
| 110 | // Do not compare the frames that have been cut. |
| 111 | for (int i = kFirstFrameToProcess; i <= kLastFrameToProcess; ++i) { |
| 112 | num_bytes_read_ = fread(original_buffer_.get(), 1, kFrameSize, |
| 113 | original_fid_); |
| 114 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 115 | } |
| 116 | CompareToTheEnd(edited_fid_, original_fid_, &original_buffer_, |
| 117 | &edited_buffer_); |
| 118 | } |
| 119 | |
| 120 | TEST_F(FrameEditingTest, EmptySetToCut) { |
| 121 | const int kFirstFrameToProcess = 2; |
| 122 | const int kInterval = -1; |
| 123 | const int kLastFrameToProcess = 1; |
| 124 | |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 125 | int result = EditFrames(reference_video_, kWidth, kHeight, |
| 126 | kFirstFrameToProcess, kInterval, kLastFrameToProcess, |
| 127 | test_video_); |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 128 | EXPECT_EQ(-10, result); |
| 129 | } |
| 130 | |
| 131 | TEST_F(FrameEditingTest, InValidInPath) { |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 132 | const std::string kRefVideo_ = "PATH/THAT/DOES/NOT/EXIST"; |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 133 | |
| 134 | const int kFirstFrameToProcess = 30; |
| 135 | const int kInterval = 1; |
| 136 | const int kLastFrameToProcess = 120; |
| 137 | |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 138 | int result = EditFrames(kRefVideo_, kWidth, kHeight, kFirstFrameToProcess, |
| 139 | kInterval, kLastFrameToProcess, test_video_); |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 140 | EXPECT_EQ(-11, result); |
| 141 | } |
| 142 | |
| 143 | TEST_F(FrameEditingTest, DeletingEverySecondFrame) { |
| 144 | const int kFirstFrameToProcess = 1; |
| 145 | const int kInterval = -2; |
| 146 | const int kLastFrameToProcess = 10000; |
| 147 | // Set kLastFrameToProcess to a large value so that all frame are processed. |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 148 | int result = EditFrames(reference_video_, kWidth, kHeight, |
| 149 | kFirstFrameToProcess, kInterval, kLastFrameToProcess, |
| 150 | test_video_); |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 151 | EXPECT_EQ(0, result); |
| 152 | |
| 153 | while (!feof(original_fid_) && !feof(edited_fid_)) { |
| 154 | num_bytes_read_ = |
| 155 | fread(original_buffer_.get(), 1, kFrameSize, original_fid_); |
| 156 | if (!feof(original_fid_)) { |
| 157 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 158 | num_frames_read_++; |
| 159 | } |
| 160 | // We want to compare every second frame of the original to the edited. |
| 161 | // kInterval=-2 and (num_frames_read_ - 1) % kInterval will be -1 for |
| 162 | // every second frame. |
| 163 | // num_frames_read_ - 1 because we have deleted frame number 2, 4 , 6 etc. |
| 164 | if ((num_frames_read_ - 1) % kInterval == -1) { |
| 165 | num_bytes_read_ = fread(edited_buffer_.get(), 1, kFrameSize, |
| 166 | edited_fid_); |
| 167 | if (!feof(edited_fid_)) { |
| 168 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 169 | } |
| 170 | if (!feof(original_fid_) && !feof(edited_fid_)) { |
| 171 | EXPECT_EQ(0, memcmp(original_buffer_.get(), |
| 172 | edited_buffer_.get(), kFrameSize)); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | TEST_F(FrameEditingTest, RepeatFrames) { |
| 179 | const int kFirstFrameToProcess = 160; |
| 180 | const int kInterval = 2; |
| 181 | const int kLastFrameToProcess = 240; |
| 182 | |
kjellander@webrtc.org | 3de3146 | 2013-03-11 15:30:33 +0000 | [diff] [blame] | 183 | int result = EditFrames(reference_video_, kWidth, kHeight, |
| 184 | kFirstFrameToProcess, kInterval, kLastFrameToProcess, |
| 185 | test_video_); |
brykt@google.com | f556890 | 2012-12-20 11:42:45 +0000 | [diff] [blame] | 186 | EXPECT_EQ(0, result); |
| 187 | |
| 188 | for (int i = 1; i < kFirstFrameToProcess; ++i) { |
| 189 | num_bytes_read_ = fread(original_buffer_.get(), 1, kFrameSize, |
| 190 | original_fid_); |
| 191 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 192 | |
| 193 | num_bytes_read_ = fread(edited_buffer_.get(), 1, kFrameSize, edited_fid_); |
| 194 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 195 | |
| 196 | EXPECT_EQ(0, memcmp(original_buffer_.get(), edited_buffer_.get(), |
| 197 | kFrameSize)); |
| 198 | } |
| 199 | // Do not compare the frames that have been repeated. |
| 200 | for (int i = kFirstFrameToProcess; i <= kLastFrameToProcess; ++i) { |
| 201 | num_bytes_read_ = fread(original_buffer_.get(), 1, kFrameSize, |
| 202 | original_fid_); |
| 203 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 204 | for (int i = 1; i <= kInterval; ++i) { |
| 205 | num_bytes_read_ = fread(edited_buffer_.get(), 1, kFrameSize, |
| 206 | edited_fid_); |
| 207 | EXPECT_EQ(kFrameSize, num_bytes_read_); |
| 208 | EXPECT_EQ(0, memcmp(original_buffer_.get(), edited_buffer_.get(), |
| 209 | kFrameSize)); |
| 210 | } |
| 211 | } |
| 212 | CompareToTheEnd(edited_fid_, original_fid_, &original_buffer_, |
| 213 | &edited_buffer_); |
| 214 | } |
| 215 | } // namespace test |
| 216 | } // namespace webrtc |