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