blob: 22fa6d76e2797a6b26368a21bd58be993baa961f [file] [log] [blame]
brykt@google.comf5568902012-12-20 11:42:45 +00001/*
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>
12
13#include <cstdlib>
14
15#include "gtest/gtest.h"
16#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
17#include "webrtc/system_wrappers/interface/scoped_ptr.h"
18#include "webrtc/test/testsupport/fileutils.h"
19#include "webrtc/tools/frame_editing/frame_editing_lib.h"
20
21namespace webrtc {
22namespace test {
23
24const int kWidth = 352;
25const int kHeight = 288;
26const int kFrameSize = CalcBufferSize(kI420, kWidth, kHeight);
27const std::string kRefVideo = ResourcePath("foreman_cif", "yuv");
28const std::string kTestVideo = OutputPath() + "testvideo.yuv";
29
30class FrameEditingTest : public ::testing::Test {
31 protected:
32 virtual void SetUp() {
33 original_fid_ = fopen(kRefVideo.c_str(), "rb");
34 ASSERT_TRUE(original_fid_ != NULL);
35 edited_fid_ = fopen(kTestVideo.c_str(), "rb");
36 ASSERT_TRUE(edited_fid_ != NULL);
37 original_buffer_.reset(new int[kFrameSize]);
38 edited_buffer_.reset(new int[kFrameSize]);
39 num_frames_read_ = 0;
40 }
41 virtual void TearDown() {
42 fclose(original_fid_);
43 fclose(edited_fid_);
44 }
45 // Compares the frames in both streams to the end of one of the streams.
46 void CompareToTheEnd(FILE* test_video_fid, FILE* ref_video_fid,
47 scoped_array<int>* ref_buffer,
48 scoped_array<int>* test_buffer) {
49 while (!feof(test_video_fid) && !feof(ref_video_fid)) {
50 num_bytes_read_ = fread(ref_buffer->get(), 1, kFrameSize, ref_video_fid);
51 if (!feof(ref_video_fid)) {
52 EXPECT_EQ(kFrameSize, num_bytes_read_);
53 }
54 num_bytes_read_ = fread(test_buffer->get(), 1, kFrameSize,
55 test_video_fid);
56 if (!feof(test_video_fid)) {
57 EXPECT_EQ(kFrameSize, num_bytes_read_);
58 }
59 if (!feof(test_video_fid) && !feof(test_video_fid)) {
60 EXPECT_EQ(0, memcmp(ref_buffer->get(), test_buffer->get(),
61 kFrameSize));
62 }
63 }
64 // There should not be anything left in either stream.
65 EXPECT_EQ(!feof(test_video_fid), !feof(ref_video_fid));
66 }
67 FILE* original_fid_;
68 FILE* edited_fid_;
69 int kFrameSize_;
70 int num_bytes_read_;
71 scoped_array<int> original_buffer_;
72 scoped_array<int> edited_buffer_;
73 int num_frames_read_;
74};
75
76TEST_F(FrameEditingTest, ValidInPath) {
77 const int kFirstFrameToProcess = 160;
78 const int kInterval = -1;
79 const int kLastFrameToProcess = 240;
80
81 int result = EditFrames(kRefVideo, kWidth, kHeight, kFirstFrameToProcess,
82 kInterval, kLastFrameToProcess, kTestVideo);
83 EXPECT_EQ(0, result);
84
85 for (int i = 1; i < kFirstFrameToProcess; ++i) {
86 num_bytes_read_ = fread(original_buffer_.get(), 1, kFrameSize,
87 original_fid_);
88 EXPECT_EQ(kFrameSize, num_bytes_read_);
89
90 num_bytes_read_ = fread(edited_buffer_.get(), 1, kFrameSize, edited_fid_);
91 EXPECT_EQ(kFrameSize, num_bytes_read_);
92
93 EXPECT_EQ(0, memcmp(original_buffer_.get(), edited_buffer_.get(),
94 kFrameSize));
95 }
96 // Do not compare the frames that have been cut.
97 for (int i = kFirstFrameToProcess; i <= kLastFrameToProcess; ++i) {
98 num_bytes_read_ = fread(original_buffer_.get(), 1, kFrameSize,
99 original_fid_);
100 EXPECT_EQ(kFrameSize, num_bytes_read_);
101 }
102 CompareToTheEnd(edited_fid_, original_fid_, &original_buffer_,
103 &edited_buffer_);
104}
105
106TEST_F(FrameEditingTest, EmptySetToCut) {
107 const int kFirstFrameToProcess = 2;
108 const int kInterval = -1;
109 const int kLastFrameToProcess = 1;
110
111 int result = EditFrames(kRefVideo, kWidth, kHeight, kFirstFrameToProcess,
112 kInterval, kLastFrameToProcess, kTestVideo);
113 EXPECT_EQ(-10, result);
114}
115
116TEST_F(FrameEditingTest, InValidInPath) {
117 const std::string kRefVideo = "PATH/THAT/DOES/NOT/EXIST";
118
119 const int kFirstFrameToProcess = 30;
120 const int kInterval = 1;
121 const int kLastFrameToProcess = 120;
122
123 int result = EditFrames(kRefVideo, kWidth, kHeight, kFirstFrameToProcess,
124 kInterval, kLastFrameToProcess, kTestVideo);
125 EXPECT_EQ(-11, result);
126}
127
128TEST_F(FrameEditingTest, DeletingEverySecondFrame) {
129 const int kFirstFrameToProcess = 1;
130 const int kInterval = -2;
131 const int kLastFrameToProcess = 10000;
132 // Set kLastFrameToProcess to a large value so that all frame are processed.
133 int result = EditFrames(kRefVideo, kWidth, kHeight, kFirstFrameToProcess,
134 kInterval, kLastFrameToProcess, kTestVideo);
135 EXPECT_EQ(0, result);
136
137 while (!feof(original_fid_) && !feof(edited_fid_)) {
138 num_bytes_read_ =
139 fread(original_buffer_.get(), 1, kFrameSize, original_fid_);
140 if (!feof(original_fid_)) {
141 EXPECT_EQ(kFrameSize, num_bytes_read_);
142 num_frames_read_++;
143 }
144 // We want to compare every second frame of the original to the edited.
145 // kInterval=-2 and (num_frames_read_ - 1) % kInterval will be -1 for
146 // every second frame.
147 // num_frames_read_ - 1 because we have deleted frame number 2, 4 , 6 etc.
148 if ((num_frames_read_ - 1) % kInterval == -1) {
149 num_bytes_read_ = fread(edited_buffer_.get(), 1, kFrameSize,
150 edited_fid_);
151 if (!feof(edited_fid_)) {
152 EXPECT_EQ(kFrameSize, num_bytes_read_);
153 }
154 if (!feof(original_fid_) && !feof(edited_fid_)) {
155 EXPECT_EQ(0, memcmp(original_buffer_.get(),
156 edited_buffer_.get(), kFrameSize));
157 }
158 }
159 }
160}
161
162TEST_F(FrameEditingTest, RepeatFrames) {
163 const int kFirstFrameToProcess = 160;
164 const int kInterval = 2;
165 const int kLastFrameToProcess = 240;
166
167 int result = EditFrames(kRefVideo, kWidth, kHeight, kFirstFrameToProcess,
168 kInterval, kLastFrameToProcess, kTestVideo);
169 EXPECT_EQ(0, result);
170
171 for (int i = 1; i < kFirstFrameToProcess; ++i) {
172 num_bytes_read_ = fread(original_buffer_.get(), 1, kFrameSize,
173 original_fid_);
174 EXPECT_EQ(kFrameSize, num_bytes_read_);
175
176 num_bytes_read_ = fread(edited_buffer_.get(), 1, kFrameSize, edited_fid_);
177 EXPECT_EQ(kFrameSize, num_bytes_read_);
178
179 EXPECT_EQ(0, memcmp(original_buffer_.get(), edited_buffer_.get(),
180 kFrameSize));
181 }
182 // Do not compare the frames that have been repeated.
183 for (int i = kFirstFrameToProcess; i <= kLastFrameToProcess; ++i) {
184 num_bytes_read_ = fread(original_buffer_.get(), 1, kFrameSize,
185 original_fid_);
186 EXPECT_EQ(kFrameSize, num_bytes_read_);
187 for (int i = 1; i <= kInterval; ++i) {
188 num_bytes_read_ = fread(edited_buffer_.get(), 1, kFrameSize,
189 edited_fid_);
190 EXPECT_EQ(kFrameSize, num_bytes_read_);
191 EXPECT_EQ(0, memcmp(original_buffer_.get(), edited_buffer_.get(),
192 kFrameSize));
193 }
194 }
195 CompareToTheEnd(edited_fid_, original_fid_, &original_buffer_,
196 &edited_buffer_);
197}
198} // namespace test
199} // namespace webrtc
200