xref: /aosp_15_r20/external/webrtc/test/testsupport/yuv_frame_reader_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 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 <stdint.h>
12 #include <stdio.h>
13 
14 #include <memory>
15 #include <string>
16 
17 #include "api/scoped_refptr.h"
18 #include "api/video/i420_buffer.h"
19 #include "api/video/video_frame_buffer.h"
20 #include "test/gtest.h"
21 #include "test/testsupport/file_utils.h"
22 #include "test/testsupport/frame_reader.h"
23 
24 namespace webrtc {
25 namespace test {
26 
27 namespace {
28 const std::string kInputFileContents = "bazouk";
29 
30 const size_t kFrameWidth = 2;
31 const size_t kFrameHeight = 2;
32 const size_t kFrameLength = 3 * kFrameWidth * kFrameHeight / 2;  // I420.
33 }  // namespace
34 
35 class YuvFrameReaderTest : public ::testing::Test {
36  protected:
37   YuvFrameReaderTest() = default;
38   ~YuvFrameReaderTest() override = default;
39 
SetUp()40   void SetUp() override {
41     temp_filename_ = webrtc::test::TempFilename(webrtc::test::OutputPath(),
42                                                 "yuv_frame_reader_unittest");
43     FILE* dummy = fopen(temp_filename_.c_str(), "wb");
44     fprintf(dummy, "%s", kInputFileContents.c_str());
45     fclose(dummy);
46 
47     frame_reader_.reset(
48         new YuvFrameReaderImpl(temp_filename_, kFrameWidth, kFrameHeight));
49     ASSERT_TRUE(frame_reader_->Init());
50   }
51 
TearDown()52   void TearDown() override { remove(temp_filename_.c_str()); }
53 
54   std::unique_ptr<FrameReader> frame_reader_;
55   std::string temp_filename_;
56 };
57 
TEST_F(YuvFrameReaderTest,InitSuccess)58 TEST_F(YuvFrameReaderTest, InitSuccess) {}
59 
TEST_F(YuvFrameReaderTest,FrameLength)60 TEST_F(YuvFrameReaderTest, FrameLength) {
61   EXPECT_EQ(kFrameLength, frame_reader_->FrameLength());
62 }
63 
TEST_F(YuvFrameReaderTest,NumberOfFrames)64 TEST_F(YuvFrameReaderTest, NumberOfFrames) {
65   EXPECT_EQ(1, frame_reader_->NumberOfFrames());
66 }
67 
TEST_F(YuvFrameReaderTest,ReadFrame)68 TEST_F(YuvFrameReaderTest, ReadFrame) {
69   rtc::scoped_refptr<I420BufferInterface> buffer = frame_reader_->ReadFrame();
70   ASSERT_TRUE(buffer);
71   // Expect I420 packed as YUV.
72   EXPECT_EQ(kInputFileContents[0], buffer->DataY()[0]);
73   EXPECT_EQ(kInputFileContents[1], buffer->DataY()[1]);
74   EXPECT_EQ(kInputFileContents[2], buffer->DataY()[2]);
75   EXPECT_EQ(kInputFileContents[3], buffer->DataY()[3]);
76   EXPECT_EQ(kInputFileContents[4], buffer->DataU()[0]);
77   EXPECT_EQ(kInputFileContents[5], buffer->DataV()[0]);
78   EXPECT_FALSE(frame_reader_->ReadFrame());  // End of file.
79 }
80 
TEST_F(YuvFrameReaderTest,ReadFrameUninitialized)81 TEST_F(YuvFrameReaderTest, ReadFrameUninitialized) {
82   YuvFrameReaderImpl file_reader(temp_filename_, kFrameWidth, kFrameHeight);
83   EXPECT_FALSE(file_reader.ReadFrame());
84 }
85 
86 }  // namespace test
87 }  // namespace webrtc
88