1 /*
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"
12 
13 #include <stdint.h>
14 
15 #include <string>
16 
17 #include "test/gtest.h"
18 #include "test/testsupport/file_utils.h"
19 
20 namespace webrtc {
21 namespace test {
22 
23 class Y4mFileReaderTest : public ::testing::Test {
24  public:
SetUp()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 
TEST_F(Y4mFileReaderTest,TestParsingFileHeader)55 TEST_F(Y4mFileReaderTest, TestParsingFileHeader) {
56   EXPECT_EQ(6, video->width());
57   EXPECT_EQ(4, video->height());
58 }
59 
TEST_F(Y4mFileReaderTest,TestParsingNumberOfFrames)60 TEST_F(Y4mFileReaderTest, TestParsingNumberOfFrames) {
61   EXPECT_EQ(2u, video->number_of_frames());
62 }
63 
TEST_F(Y4mFileReaderTest,TestPixelContent)64 TEST_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 
76 class YuvFileReaderTest : public ::testing::Test {
77  public:
SetUp()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 
TEST_F(YuvFileReaderTest,TestParsingFileHeader)105 TEST_F(YuvFileReaderTest, TestParsingFileHeader) {
106   EXPECT_EQ(6, video->width());
107   EXPECT_EQ(4, video->height());
108 }
109 
TEST_F(YuvFileReaderTest,TestParsingNumberOfFrames)110 TEST_F(YuvFileReaderTest, TestParsingNumberOfFrames) {
111   EXPECT_EQ(2u, video->number_of_frames());
112 }
113 
TEST_F(YuvFileReaderTest,TestPixelContent)114 TEST_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
128