blob: 11b1c37fca2c7477cd1b992bae34f5ddc6669bb0 [file] [log] [blame]
Paulina Hensmanb671d462018-09-14 11:32:00 +02001/*
2 * Copyright (c) 2018 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 <string>
12
13#include "rtc_base/refcountedobject.h"
14#include "rtc_tools/video_file_reader.h"
15#include "rtc_tools/video_file_writer.h"
16#include "test/gtest.h"
17#include "test/testsupport/fileutils.h"
18
19namespace webrtc {
20namespace test {
21
22class VideoFileWriterTest : public ::testing::Test {
23 public:
24 void SetUp() override {
25 const std::string filename =
26 webrtc::test::OutputPath() + "test_video_file.y4m";
27
28 // Create simple test video of size 6x4.
29 FILE* file = fopen(filename.c_str(), "wb");
30 ASSERT_TRUE(file != nullptr);
31 fprintf(file, "YUV4MPEG2 W6 H4 F60:1 C420 dummyParam\n");
32 fprintf(file, "FRAME\n");
33
34 const int i420_size = width * height * 3 / 2;
35 // First frame.
36 for (int i = 0; i < i420_size; ++i)
37 fputc(static_cast<char>(i), file);
38 fprintf(file, "FRAME\n");
39 // Second frame.
40 for (int i = 0; i < i420_size; ++i)
41 fputc(static_cast<char>(i + i420_size), file);
42 fclose(file);
43
44 // Open the newly created file.
45 video = webrtc::test::OpenY4mFile(filename);
46 ASSERT_TRUE(video);
47 }
48
49 // Write and read Y4M file.
50 void WriteVideoY4m() {
51 const std::string filename =
52 webrtc::test::OutputPath() + "test_video_file2.y4m";
53 webrtc::test::WriteVideoToFile(video, filename, fps);
54 written_video = webrtc::test::OpenY4mFile(filename);
55 ASSERT_TRUE(written_video);
56 }
57
58 // Write and read YUV file.
59 void WriteVideoYuv() {
60 const std::string filename =
61 webrtc::test::OutputPath() + "test_video_file2.yuv";
62 webrtc::test::WriteVideoToFile(video, filename, fps);
63 written_video = webrtc::test::OpenYuvFile(filename, width, height);
64 ASSERT_TRUE(written_video);
65 }
66
67 const int width = 6;
68 const int height = 4;
69 const int fps = 60;
70 rtc::scoped_refptr<webrtc::test::Video> video;
71 rtc::scoped_refptr<webrtc::test::Video> written_video;
72};
73
74TEST_F(VideoFileWriterTest, TestParsingFileHeaderY4m) {
75 WriteVideoY4m();
76 EXPECT_EQ(video->width(), written_video->width());
77 EXPECT_EQ(video->height(), written_video->height());
78}
79
80TEST_F(VideoFileWriterTest, TestParsingFileHeaderYuv) {
81 WriteVideoYuv();
82 EXPECT_EQ(video->width(), written_video->width());
83 EXPECT_EQ(video->height(), written_video->height());
84}
85
86TEST_F(VideoFileWriterTest, TestParsingNumberOfFramesY4m) {
87 WriteVideoY4m();
88 EXPECT_EQ(video->number_of_frames(), written_video->number_of_frames());
89}
90
91TEST_F(VideoFileWriterTest, TestParsingNumberOfFramesYuv) {
92 WriteVideoYuv();
93 EXPECT_EQ(video->number_of_frames(), written_video->number_of_frames());
94}
95
96TEST_F(VideoFileWriterTest, TestPixelContentY4m) {
97 WriteVideoY4m();
98 int cnt = 0;
99 for (const rtc::scoped_refptr<I420BufferInterface> frame : *written_video) {
100 for (int i = 0; i < width * height; ++i, ++cnt)
101 EXPECT_EQ(cnt, frame->DataY()[i]);
102 for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt)
103 EXPECT_EQ(cnt, frame->DataU()[i]);
104 for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt)
105 EXPECT_EQ(cnt, frame->DataV()[i]);
106 }
107}
108
109TEST_F(VideoFileWriterTest, TestPixelContentYuv) {
110 WriteVideoYuv();
111 int cnt = 0;
112 for (const rtc::scoped_refptr<I420BufferInterface> frame : *written_video) {
113 for (int i = 0; i < width * height; ++i, ++cnt)
114 EXPECT_EQ(cnt, frame->DataY()[i]);
115 for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt)
116 EXPECT_EQ(cnt, frame->DataU()[i]);
117 for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt)
118 EXPECT_EQ(cnt, frame->DataV()[i]);
119 }
120}
121
122} // namespace test
123} // namespace webrtc