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