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