• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/internal_frame_buffer_list.h"
16 
17 #include <cstdint>
18 
19 #include "gtest/gtest.h"
20 #include "src/gav1/decoder_buffer.h"
21 #include "src/gav1/frame_buffer.h"
22 
23 namespace libgav1 {
24 namespace {
25 
26 class InternalFrameBufferListTest : public testing::Test {
27  protected:
28   static constexpr int kBufferListSize = 10;
29 
InternalFrameBufferListTest()30   InternalFrameBufferListTest() {
31     on_frame_buffer_size_changed_ = OnInternalFrameBufferSizeChanged;
32     get_frame_buffer_ = GetInternalFrameBuffer;
33     release_frame_buffer_ = ReleaseInternalFrameBuffer;
34     callback_private_data_ = &buffer_list_;
35   }
36 
37   // Frame buffer callbacks.
38   FrameBufferSizeChangedCallback on_frame_buffer_size_changed_;
39   GetFrameBufferCallback get_frame_buffer_;
40   ReleaseFrameBufferCallback release_frame_buffer_;
41   // Private data associated with the frame buffer callbacks.
42   void* callback_private_data_;
43 
44  private:
45   InternalFrameBufferList buffer_list_;
46 };
47 
TEST_F(InternalFrameBufferListTest,ReleaseInRandomOrder)48 TEST_F(InternalFrameBufferListTest, ReleaseInRandomOrder) {
49   const int bitdepth = 8;
50   const Libgav1ImageFormat image_format = kLibgav1ImageFormatYuv420;
51   const int width = 100;
52   const int height = 50;
53   const int left_border = 0;
54   const int right_border = 0;
55   const int top_border = 0;
56   const int bottom_border = 0;
57   const int stride_alignment = 16;
58 
59   EXPECT_EQ(on_frame_buffer_size_changed_(callback_private_data_, bitdepth,
60                                           image_format, width, height,
61                                           left_border, right_border, top_border,
62                                           bottom_border, stride_alignment),
63             0);
64 
65   FrameBuffer frame_buffers[kBufferListSize];
66   for (auto& frame_buffer : frame_buffers) {
67     EXPECT_EQ(
68         get_frame_buffer_(callback_private_data_, bitdepth, image_format, width,
69                           height, left_border, right_border, top_border,
70                           bottom_border, stride_alignment, &frame_buffer),
71         0);
72     EXPECT_NE(frame_buffer.plane[0], nullptr);
73     EXPECT_GE(frame_buffer.stride[0], 112);
74     EXPECT_NE(frame_buffer.plane[1], nullptr);
75     EXPECT_GE(frame_buffer.stride[1], 64);
76     EXPECT_NE(frame_buffer.plane[2], nullptr);
77     EXPECT_GE(frame_buffer.stride[2], 64);
78   }
79 
80   // Release and get a few buffers at indexes <= 5 in random order.
81   static_assert(5 < kBufferListSize, "");
82   static constexpr int indexes[] = {1, 4, 5, 5, 4, 3, 2, 3, 5, 0};
83   for (int index : indexes) {
84     release_frame_buffer_(callback_private_data_,
85                           frame_buffers[index].private_data);
86 
87     EXPECT_EQ(get_frame_buffer_(callback_private_data_, bitdepth, image_format,
88                                 width, height, left_border, right_border,
89                                 top_border, bottom_border, stride_alignment,
90                                 &frame_buffers[index]),
91               0);
92     EXPECT_NE(frame_buffers[index].plane[0], nullptr);
93     EXPECT_GE(frame_buffers[index].stride[0], 112);
94     EXPECT_NE(frame_buffers[index].plane[1], nullptr);
95     EXPECT_GE(frame_buffers[index].stride[1], 64);
96     EXPECT_NE(frame_buffers[index].plane[2], nullptr);
97     EXPECT_GE(frame_buffers[index].stride[2], 64);
98   }
99 
100   for (auto& frame_buffer : frame_buffers) {
101     release_frame_buffer_(callback_private_data_, frame_buffer.private_data);
102   }
103 }
104 
TEST_F(InternalFrameBufferListTest,VaryingBufferSizes)105 TEST_F(InternalFrameBufferListTest, VaryingBufferSizes) {
106   const int bitdepth = 8;
107   const Libgav1ImageFormat image_format = kLibgav1ImageFormatYuv420;
108   const int width = 64;
109   const int height = 48;
110   const int left_border = 16;
111   const int right_border = 16;
112   const int top_border = 16;
113   const int bottom_border = 16;
114   const int stride_alignment = 16;
115 
116   EXPECT_EQ(on_frame_buffer_size_changed_(callback_private_data_, bitdepth,
117                                           image_format, 16 * width, 16 * height,
118                                           left_border, right_border, top_border,
119                                           bottom_border, stride_alignment),
120             0);
121 
122   FrameBuffer frame_buffer;
123   for (int i = 1; i <= 16; ++i) {
124     EXPECT_EQ(get_frame_buffer_(callback_private_data_, bitdepth, image_format,
125                                 i * width, i * height, left_border,
126                                 right_border, top_border, bottom_border,
127                                 stride_alignment, &frame_buffer),
128               0);
129     EXPECT_NE(frame_buffer.plane[0], nullptr);
130     EXPECT_GE(frame_buffer.stride[0], i * width + left_border + right_border);
131     EXPECT_NE(frame_buffer.plane[1], nullptr);
132     EXPECT_GE(frame_buffer.stride[1],
133               (i * width + left_border + right_border) >> 1);
134     EXPECT_NE(frame_buffer.plane[2], nullptr);
135     EXPECT_GE(frame_buffer.stride[2],
136               (i * width + left_border + right_border) >> 1);
137     release_frame_buffer_(callback_private_data_, frame_buffer.private_data);
138   }
139   for (int i = 16; i >= 1; --i) {
140     EXPECT_EQ(get_frame_buffer_(callback_private_data_, bitdepth, image_format,
141                                 i * width, i * height, left_border,
142                                 right_border, top_border, bottom_border,
143                                 stride_alignment, &frame_buffer),
144               0);
145     EXPECT_NE(frame_buffer.plane[0], nullptr);
146     EXPECT_GE(frame_buffer.stride[0], i * width + left_border + right_border);
147     EXPECT_NE(frame_buffer.plane[1], nullptr);
148     EXPECT_GE(frame_buffer.stride[1],
149               (i * width + left_border + right_border) >> 1);
150     EXPECT_NE(frame_buffer.plane[2], nullptr);
151     EXPECT_GE(frame_buffer.stride[2],
152               (i * width + left_border + right_border) >> 1);
153     release_frame_buffer_(callback_private_data_, frame_buffer.private_data);
154   }
155 }
156 
157 }  // namespace
158 }  // namespace libgav1
159