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 { |
| 27 | const std::string filename = |
| 28 | webrtc::test::OutputPath() + "test_video_file.y4m"; |
| 29 | |
| 30 | // Create simple test video of size 6x4. |
| 31 | FILE* file = fopen(filename.c_str(), "wb"); |
| 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. |
| 47 | video = webrtc::test::OpenY4mFile(filename); |
| 48 | ASSERT_TRUE(video); |
| 49 | } |
| 50 | |
| 51 | // Write and read Y4M file. |
| 52 | void WriteVideoY4m() { |
| 53 | const std::string filename = |
| 54 | webrtc::test::OutputPath() + "test_video_file2.y4m"; |
| 55 | webrtc::test::WriteVideoToFile(video, filename, fps); |
| 56 | written_video = webrtc::test::OpenY4mFile(filename); |
| 57 | ASSERT_TRUE(written_video); |
| 58 | } |
| 59 | |
| 60 | // Write and read YUV file. |
| 61 | void WriteVideoYuv() { |
| 62 | const std::string filename = |
| 63 | webrtc::test::OutputPath() + "test_video_file2.yuv"; |
| 64 | webrtc::test::WriteVideoToFile(video, filename, fps); |
| 65 | written_video = webrtc::test::OpenYuvFile(filename, width, height); |
| 66 | ASSERT_TRUE(written_video); |
| 67 | } |
| 68 | |
| 69 | const int width = 6; |
| 70 | const int height = 4; |
| 71 | const int fps = 60; |
| 72 | rtc::scoped_refptr<webrtc::test::Video> video; |
| 73 | rtc::scoped_refptr<webrtc::test::Video> written_video; |
| 74 | }; |
| 75 | |
| 76 | TEST_F(VideoFileWriterTest, TestParsingFileHeaderY4m) { |
| 77 | WriteVideoY4m(); |
| 78 | EXPECT_EQ(video->width(), written_video->width()); |
| 79 | EXPECT_EQ(video->height(), written_video->height()); |
| 80 | } |
| 81 | |
| 82 | TEST_F(VideoFileWriterTest, TestParsingFileHeaderYuv) { |
| 83 | WriteVideoYuv(); |
| 84 | EXPECT_EQ(video->width(), written_video->width()); |
| 85 | EXPECT_EQ(video->height(), written_video->height()); |
| 86 | } |
| 87 | |
| 88 | TEST_F(VideoFileWriterTest, TestParsingNumberOfFramesY4m) { |
| 89 | WriteVideoY4m(); |
| 90 | EXPECT_EQ(video->number_of_frames(), written_video->number_of_frames()); |
| 91 | } |
| 92 | |
| 93 | TEST_F(VideoFileWriterTest, TestParsingNumberOfFramesYuv) { |
| 94 | WriteVideoYuv(); |
| 95 | EXPECT_EQ(video->number_of_frames(), written_video->number_of_frames()); |
| 96 | } |
| 97 | |
| 98 | TEST_F(VideoFileWriterTest, TestPixelContentY4m) { |
| 99 | WriteVideoY4m(); |
| 100 | int cnt = 0; |
| 101 | for (const rtc::scoped_refptr<I420BufferInterface> frame : *written_video) { |
| 102 | for (int i = 0; i < width * height; ++i, ++cnt) |
| 103 | EXPECT_EQ(cnt, frame->DataY()[i]); |
| 104 | for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt) |
| 105 | EXPECT_EQ(cnt, frame->DataU()[i]); |
| 106 | for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt) |
| 107 | EXPECT_EQ(cnt, frame->DataV()[i]); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | TEST_F(VideoFileWriterTest, TestPixelContentYuv) { |
| 112 | WriteVideoYuv(); |
| 113 | int cnt = 0; |
| 114 | for (const rtc::scoped_refptr<I420BufferInterface> frame : *written_video) { |
| 115 | for (int i = 0; i < width * height; ++i, ++cnt) |
| 116 | EXPECT_EQ(cnt, frame->DataY()[i]); |
| 117 | for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt) |
| 118 | EXPECT_EQ(cnt, frame->DataU()[i]); |
| 119 | for (int i = 0; i < width / 2 * height / 2; ++i, ++cnt) |
| 120 | EXPECT_EQ(cnt, frame->DataV()[i]); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } // namespace test |
| 125 | } // namespace webrtc |