Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | #include <cstdio> |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 13 | #include <string> |
| 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include "api/video/video_frame_buffer.h" |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 16 | #include "rtc_tools/video_file_reader.h" |
| 17 | #include "rtc_tools/video_file_writer.h" |
| 18 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "test/testsupport/file_utils.h" |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
| 24 | class VideoFileWriterTest : public ::testing::Test { |
| 25 | public: |
| 26 | void SetUp() override { |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 27 | video_filename_ = |
| 28 | TempFilename(webrtc::test::OutputPath(), "test_video_file.y4m"); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 29 | |
| 30 | // Create simple test video of size 6x4. |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 31 | FILE* file = fopen(video_filename_.c_str(), "wb"); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 32 | ASSERT_TRUE(file != nullptr); |
| 33 | fprintf(file, "YUV4MPEG2 W6 H4 F60:1 C420 dummyParam\n"); |
| 34 | fprintf(file, "FRAME\n"); |
| 35 | |
| 36 | const int i420_size = width * height * 3 / 2; |
| 37 | // First frame. |
| 38 | for (int i = 0; i < i420_size; ++i) |
| 39 | fputc(static_cast<char>(i), file); |
| 40 | fprintf(file, "FRAME\n"); |
| 41 | // Second frame. |
| 42 | for (int i = 0; i < i420_size; ++i) |
| 43 | fputc(static_cast<char>(i + i420_size), file); |
| 44 | fclose(file); |
| 45 | |
| 46 | // Open the newly created file. |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 47 | video_ = webrtc::test::OpenY4mFile(video_filename_); |
| 48 | ASSERT_TRUE(video_); |
| 49 | ASSERT_EQ(video_->number_of_frames(), 2u); |
| 50 | } |
| 51 | |
| 52 | void TearDown() override { |
| 53 | if (!video_filename_.empty()) { |
| 54 | RemoveFile(video_filename_); |
| 55 | } |
| 56 | if (!written_video_filename_.empty()) { |
| 57 | RemoveFile(written_video_filename_); |
| 58 | } |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Write and read Y4M file. |
| 62 | void WriteVideoY4m() { |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 63 | // Cleanup existing file if any. |
| 64 | if (!written_video_filename_.empty()) { |
| 65 | RemoveFile(written_video_filename_); |
| 66 | } |
| 67 | // Create an unique filename, e.g. test_video_file2.y4mZapata. |
| 68 | written_video_filename_ = |
| 69 | TempFilename(webrtc::test::OutputPath(), "test_video_file2.y4m"); |
| 70 | webrtc::test::WriteY4mVideoToFile(video_, written_video_filename_, fps); |
| 71 | written_video_ = webrtc::test::OpenY4mFile(written_video_filename_); |
| 72 | ASSERT_TRUE(written_video_); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Write and read YUV file. |
| 76 | void WriteVideoYuv() { |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 77 | // Cleanup existing file if any. |
| 78 | if (!written_video_filename_.empty()) { |
| 79 | RemoveFile(written_video_filename_); |
| 80 | } |
| 81 | // Create an unique filename, e.g. test_video_file2.yuvZapata. |
| 82 | written_video_filename_ = |
| 83 | TempFilename(webrtc::test::OutputPath(), "test_video_file2.yuv"); |
| 84 | webrtc::test::WriteYuvVideoToFile(video_, written_video_filename_, fps); |
| 85 | written_video_ = |
| 86 | webrtc::test::OpenYuvFile(written_video_filename_, width, height); |
| 87 | ASSERT_TRUE(written_video_); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | const int width = 6; |
| 91 | const int height = 4; |
| 92 | const int fps = 60; |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 93 | rtc::scoped_refptr<webrtc::test::Video> video_; |
| 94 | rtc::scoped_refptr<webrtc::test::Video> written_video_; |
| 95 | // Each video object must be backed by file! |
| 96 | std::string video_filename_; |
| 97 | std::string written_video_filename_; |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | TEST_F(VideoFileWriterTest, TestParsingFileHeaderY4m) { |
| 101 | WriteVideoY4m(); |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 102 | EXPECT_EQ(video_->width(), written_video_->width()); |
| 103 | EXPECT_EQ(video_->height(), written_video_->height()); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST_F(VideoFileWriterTest, TestParsingFileHeaderYuv) { |
| 107 | WriteVideoYuv(); |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 108 | EXPECT_EQ(video_->width(), written_video_->width()); |
| 109 | EXPECT_EQ(video_->height(), written_video_->height()); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST_F(VideoFileWriterTest, TestParsingNumberOfFramesY4m) { |
| 113 | WriteVideoY4m(); |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 114 | EXPECT_EQ(video_->number_of_frames(), written_video_->number_of_frames()); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST_F(VideoFileWriterTest, TestParsingNumberOfFramesYuv) { |
| 118 | WriteVideoYuv(); |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 119 | EXPECT_EQ(video_->number_of_frames(), written_video_->number_of_frames()); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | TEST_F(VideoFileWriterTest, TestPixelContentY4m) { |
| 123 | WriteVideoY4m(); |
| 124 | int cnt = 0; |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 125 | for (const rtc::scoped_refptr<I420BufferInterface> frame : *written_video_) { |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 126 | for (int i = 0; i < width * height; ++i, ++cnt) |
| 127 | EXPECT_EQ(cnt, frame->DataY()[i]); |
| 128 | for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt) |
| 129 | EXPECT_EQ(cnt, frame->DataU()[i]); |
| 130 | for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt) |
| 131 | EXPECT_EQ(cnt, frame->DataV()[i]); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | TEST_F(VideoFileWriterTest, TestPixelContentYuv) { |
| 136 | WriteVideoYuv(); |
| 137 | int cnt = 0; |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 138 | for (const rtc::scoped_refptr<I420BufferInterface> frame : *written_video_) { |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 139 | for (int i = 0; i < width * height; ++i, ++cnt) |
| 140 | EXPECT_EQ(cnt, frame->DataY()[i]); |
| 141 | for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt) |
| 142 | EXPECT_EQ(cnt, frame->DataU()[i]); |
| 143 | for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt) |
| 144 | EXPECT_EQ(cnt, frame->DataV()[i]); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } // namespace test |
| 149 | } // namespace webrtc |