xref: /aosp_15_r20/external/libwebm/testing/video_frame_tests.cc (revision 103e46e4cd4b6efcf6001f23fa8665fb110abf8d)
1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "common/video_frame.h"
9 
10 #include "gtest/gtest.h"
11 
12 namespace {
13 const libwebm::VideoFrame::Codec kCodec = libwebm::VideoFrame::kVP8;
14 const std::int64_t kPts = 12345;
15 const std::size_t kSize = 1;
16 const std::size_t kEmptySize = 0;
17 
TEST(VideoFrameTests,DefaultsTest)18 TEST(VideoFrameTests, DefaultsTest) {
19   libwebm::VideoFrame frame;
20   EXPECT_EQ(kEmptySize, frame.buffer().capacity);
21   EXPECT_EQ(kEmptySize, frame.buffer().length);
22   EXPECT_EQ(nullptr, frame.buffer().data.get());
23   EXPECT_FALSE(frame.keyframe());
24   EXPECT_EQ(0, frame.nanosecond_pts());
25   EXPECT_EQ(libwebm::VideoFrame::kVP9, frame.codec());
26 }
27 
TEST(VideoFrameTests,SizeTest)28 TEST(VideoFrameTests, SizeTest) {
29   libwebm::VideoFrame frame;
30   EXPECT_TRUE(frame.Init(kSize));
31 
32   // Buffer inits empty, length should be 0, aka |kEmpty|.
33   EXPECT_GT(kSize, frame.buffer().length);
34   EXPECT_EQ(kEmptySize, frame.buffer().length);
35 
36   // Capacity should be equal to |kSize|.
37   EXPECT_EQ(kSize, frame.buffer().capacity);
38   EXPECT_FALSE(frame.SetBufferLength(kSize + 1));
39 
40   // Write a byte into the buffer via the raw data pointer, update length, and
41   // verify expected behavior.
42   uint8_t* write_ptr = reinterpret_cast<uint8_t*>(frame.buffer().data.get());
43   *write_ptr = 0xFF;
44   EXPECT_TRUE(frame.SetBufferLength(1));
45   EXPECT_EQ(frame.buffer().length, frame.buffer().capacity);
46 }
47 
TEST(VideoFrameTests,OverloadsTest)48 TEST(VideoFrameTests, OverloadsTest) {
49   const bool kKeyframe = true;
50 
51   // Test VideoFrame::VideoFrame(bool keyframe, int64_t nano_pts, Codec c).
52   libwebm::VideoFrame keyframe(kKeyframe, kPts, kCodec);
53   EXPECT_EQ(kKeyframe, keyframe.keyframe());
54   EXPECT_EQ(kPts, keyframe.nanosecond_pts());
55   EXPECT_EQ(kCodec, keyframe.codec());
56   EXPECT_EQ(kEmptySize, keyframe.buffer().capacity);
57   EXPECT_EQ(kEmptySize, keyframe.buffer().length);
58   EXPECT_EQ(nullptr, keyframe.buffer().data.get());
59 
60   // Test VideoFrame::Init(std::size_t length).
61   EXPECT_TRUE(keyframe.Init(kSize));
62   EXPECT_EQ(kKeyframe, keyframe.keyframe());
63   EXPECT_EQ(kPts, keyframe.nanosecond_pts());
64   EXPECT_EQ(kCodec, keyframe.codec());
65   EXPECT_NE(nullptr, keyframe.buffer().data.get());
66 
67   // Test VideoFrame::Init(size_t length, int64_t nano_pts, Codec c).
68   EXPECT_TRUE(keyframe.Init(kSize, kPts + 1, libwebm::VideoFrame::kVP9));
69   EXPECT_EQ(kSize, keyframe.buffer().capacity);
70   EXPECT_GT(kSize, keyframe.buffer().length);
71   EXPECT_NE(kPts, keyframe.nanosecond_pts());
72   EXPECT_NE(kCodec, keyframe.codec());
73 }
74 
75 }  // namespace
76