blob: c0258d1cd8975182985c9d3cb93db4eff2270bb8 [file] [log] [blame]
Magnus Jedvert10e829a2018-09-05 10:46:18 +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 "rtc_tools/video_file_reader.h"
Yves Gerey3e707812018-11-28 16:47:49 +010012
13#include <stdint.h>
14#include <string>
15
Magnus Jedvert10e829a2018-09-05 10:46:18 +020016#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "test/testsupport/file_utils.h"
Magnus Jedvert10e829a2018-09-05 10:46:18 +020018
19namespace webrtc {
20namespace test {
21
22class Y4mFileReaderTest : public ::testing::Test {
23 public:
24 void SetUp() override {
25 const std::string filename =
26 TempFilename(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 F57:10 C420 dummyParam\n");
32 fprintf(file, "FRAME\n");
33
34 const int width = 6;
35 const int height = 4;
36 const int i40_size = width * height * 3 / 2;
37 // First frame.
38 for (int i = 0; i < i40_size; ++i)
39 fputc(static_cast<char>(i), file);
40 fprintf(file, "FRAME\n");
41 // Second frame.
42 for (int i = 0; i < i40_size; ++i)
43 fputc(static_cast<char>(i + i40_size), file);
44 fclose(file);
45
46 // Open the newly created file.
47 video = webrtc::test::OpenY4mFile(filename);
48 ASSERT_TRUE(video);
49 }
50
51 rtc::scoped_refptr<webrtc::test::Video> video;
52};
53
54TEST_F(Y4mFileReaderTest, TestParsingFileHeader) {
55 EXPECT_EQ(6, video->width());
56 EXPECT_EQ(4, video->height());
57}
58
59TEST_F(Y4mFileReaderTest, TestParsingNumberOfFrames) {
60 EXPECT_EQ(2u, video->number_of_frames());
61}
62
63TEST_F(Y4mFileReaderTest, TestPixelContent) {
64 int cnt = 0;
65 for (const rtc::scoped_refptr<I420BufferInterface> frame : *video) {
66 for (int i = 0; i < 6 * 4; ++i, ++cnt)
67 EXPECT_EQ(cnt, frame->DataY()[i]);
68 for (int i = 0; i < 3 * 2; ++i, ++cnt)
69 EXPECT_EQ(cnt, frame->DataU()[i]);
70 for (int i = 0; i < 3 * 2; ++i, ++cnt)
71 EXPECT_EQ(cnt, frame->DataV()[i]);
72 }
73}
74
75class YuvFileReaderTest : public ::testing::Test {
76 public:
77 void SetUp() override {
78 const std::string filename =
79 TempFilename(webrtc::test::OutputPath(), "test_video_file.yuv");
80
81 // Create simple test video of size 6x4.
82 FILE* file = fopen(filename.c_str(), "wb");
83 ASSERT_TRUE(file != nullptr);
84
85 const int width = 6;
86 const int height = 4;
87 const int i40_size = width * height * 3 / 2;
88 // First frame.
89 for (int i = 0; i < i40_size; ++i)
90 fputc(static_cast<char>(i), file);
91 // Second frame.
92 for (int i = 0; i < i40_size; ++i)
93 fputc(static_cast<char>(i + i40_size), file);
94 fclose(file);
95
96 // Open the newly created file.
97 video = webrtc::test::OpenYuvFile(filename, 6, 4);
98 ASSERT_TRUE(video);
99 }
100
101 rtc::scoped_refptr<webrtc::test::Video> video;
102};
103
104TEST_F(YuvFileReaderTest, TestParsingFileHeader) {
105 EXPECT_EQ(6, video->width());
106 EXPECT_EQ(4, video->height());
107}
108
109TEST_F(YuvFileReaderTest, TestParsingNumberOfFrames) {
110 EXPECT_EQ(2u, video->number_of_frames());
111}
112
113TEST_F(YuvFileReaderTest, TestPixelContent) {
114 int cnt = 0;
115 for (const rtc::scoped_refptr<I420BufferInterface> frame : *video) {
116 for (int i = 0; i < 6 * 4; ++i, ++cnt)
117 EXPECT_EQ(cnt, frame->DataY()[i]);
118 for (int i = 0; i < 3 * 2; ++i, ++cnt)
119 EXPECT_EQ(cnt, frame->DataU()[i]);
120 for (int i = 0; i < 3 * 2; ++i, ++cnt)
121 EXPECT_EQ(cnt, frame->DataV()[i]);
122 }
123}
124
125} // namespace test
126} // namespace webrtc