xref: /aosp_15_r20/external/libgav1/src/decoder_test.cc (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1*09537850SAkhilesh Sanikop // Copyright 2021 The libgav1 Authors
2*09537850SAkhilesh Sanikop //
3*09537850SAkhilesh Sanikop // Licensed under the Apache License, Version 2.0 (the "License");
4*09537850SAkhilesh Sanikop // you may not use this file except in compliance with the License.
5*09537850SAkhilesh Sanikop // You may obtain a copy of the License at
6*09537850SAkhilesh Sanikop //
7*09537850SAkhilesh Sanikop //      http://www.apache.org/licenses/LICENSE-2.0
8*09537850SAkhilesh Sanikop //
9*09537850SAkhilesh Sanikop // Unless required by applicable law or agreed to in writing, software
10*09537850SAkhilesh Sanikop // distributed under the License is distributed on an "AS IS" BASIS,
11*09537850SAkhilesh Sanikop // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*09537850SAkhilesh Sanikop // See the License for the specific language governing permissions and
13*09537850SAkhilesh Sanikop // limitations under the License.
14*09537850SAkhilesh Sanikop 
15*09537850SAkhilesh Sanikop #include "src/gav1/decoder.h"
16*09537850SAkhilesh Sanikop 
17*09537850SAkhilesh Sanikop #include <cstddef>
18*09537850SAkhilesh Sanikop #include <cstdint>
19*09537850SAkhilesh Sanikop #include <memory>
20*09537850SAkhilesh Sanikop #include <new>
21*09537850SAkhilesh Sanikop 
22*09537850SAkhilesh Sanikop #include "gtest/gtest.h"
23*09537850SAkhilesh Sanikop #include "src/decoder_test_data.h"
24*09537850SAkhilesh Sanikop 
25*09537850SAkhilesh Sanikop namespace libgav1 {
26*09537850SAkhilesh Sanikop namespace {
27*09537850SAkhilesh Sanikop 
28*09537850SAkhilesh Sanikop constexpr uint8_t kFrame1[] = {OBU_TEMPORAL_DELIMITER, OBU_SEQUENCE_HEADER,
29*09537850SAkhilesh Sanikop                                OBU_FRAME_1};
30*09537850SAkhilesh Sanikop 
31*09537850SAkhilesh Sanikop constexpr uint8_t kFrame2[] = {OBU_TEMPORAL_DELIMITER, OBU_FRAME_2};
32*09537850SAkhilesh Sanikop 
33*09537850SAkhilesh Sanikop constexpr uint8_t kFrame1WithHdrCllAndHdrMdcv[] = {
34*09537850SAkhilesh Sanikop     OBU_TEMPORAL_DELIMITER, OBU_SEQUENCE_HEADER, OBU_METADATA_HDR_CLL,
35*09537850SAkhilesh Sanikop     OBU_METADATA_HDR_MDCV, OBU_FRAME_1};
36*09537850SAkhilesh Sanikop 
37*09537850SAkhilesh Sanikop constexpr uint8_t kFrame2WithItutT35[] = {OBU_TEMPORAL_DELIMITER,
38*09537850SAkhilesh Sanikop                                           OBU_METADATA_ITUT_T35, OBU_FRAME_2};
39*09537850SAkhilesh Sanikop 
40*09537850SAkhilesh Sanikop class DecoderTest : public testing::Test {
41*09537850SAkhilesh Sanikop  public:
42*09537850SAkhilesh Sanikop   void SetUp() override;
IncrementFramesInUse()43*09537850SAkhilesh Sanikop   void IncrementFramesInUse() { ++frames_in_use_; }
DecrementFramesInUse()44*09537850SAkhilesh Sanikop   void DecrementFramesInUse() { --frames_in_use_; }
SetBufferPrivateData(void * buffer_private_data)45*09537850SAkhilesh Sanikop   void SetBufferPrivateData(void* buffer_private_data) {
46*09537850SAkhilesh Sanikop     buffer_private_data_ = buffer_private_data;
47*09537850SAkhilesh Sanikop   }
SetReleasedInputBuffer(void * released_input_buffer)48*09537850SAkhilesh Sanikop   void SetReleasedInputBuffer(void* released_input_buffer) {
49*09537850SAkhilesh Sanikop     released_input_buffer_ = released_input_buffer;
50*09537850SAkhilesh Sanikop   }
51*09537850SAkhilesh Sanikop 
52*09537850SAkhilesh Sanikop  protected:
53*09537850SAkhilesh Sanikop   std::unique_ptr<Decoder> decoder_;
54*09537850SAkhilesh Sanikop   int frames_in_use_ = 0;
55*09537850SAkhilesh Sanikop   void* buffer_private_data_ = nullptr;
56*09537850SAkhilesh Sanikop   void* released_input_buffer_ = nullptr;
57*09537850SAkhilesh Sanikop };
58*09537850SAkhilesh Sanikop 
59*09537850SAkhilesh Sanikop struct FrameBufferPrivate {
60*09537850SAkhilesh Sanikop   uint8_t* data[3];
61*09537850SAkhilesh Sanikop };
62*09537850SAkhilesh Sanikop 
63*09537850SAkhilesh Sanikop extern "C" {
64*09537850SAkhilesh Sanikop 
GetFrameBuffer(void * callback_private_data,int bitdepth,Libgav1ImageFormat image_format,int width,int height,int left_border,int right_border,int top_border,int bottom_border,int stride_alignment,Libgav1FrameBuffer * frame_buffer)65*09537850SAkhilesh Sanikop static Libgav1StatusCode GetFrameBuffer(
66*09537850SAkhilesh Sanikop     void* callback_private_data, int bitdepth, Libgav1ImageFormat image_format,
67*09537850SAkhilesh Sanikop     int width, int height, int left_border, int right_border, int top_border,
68*09537850SAkhilesh Sanikop     int bottom_border, int stride_alignment, Libgav1FrameBuffer* frame_buffer) {
69*09537850SAkhilesh Sanikop   Libgav1FrameBufferInfo info;
70*09537850SAkhilesh Sanikop   Libgav1StatusCode status = Libgav1ComputeFrameBufferInfo(
71*09537850SAkhilesh Sanikop       bitdepth, image_format, width, height, left_border, right_border,
72*09537850SAkhilesh Sanikop       top_border, bottom_border, stride_alignment, &info);
73*09537850SAkhilesh Sanikop   if (status != kLibgav1StatusOk) return status;
74*09537850SAkhilesh Sanikop 
75*09537850SAkhilesh Sanikop   std::unique_ptr<FrameBufferPrivate> buffer_private(new (std::nothrow)
76*09537850SAkhilesh Sanikop                                                          FrameBufferPrivate);
77*09537850SAkhilesh Sanikop   if (buffer_private == nullptr) return kLibgav1StatusOutOfMemory;
78*09537850SAkhilesh Sanikop 
79*09537850SAkhilesh Sanikop   for (int i = 0; i < 3; ++i) {
80*09537850SAkhilesh Sanikop     const size_t size = (i == 0) ? info.y_buffer_size : info.uv_buffer_size;
81*09537850SAkhilesh Sanikop     buffer_private->data[i] = new (std::nothrow) uint8_t[size];
82*09537850SAkhilesh Sanikop     if (buffer_private->data[i] == nullptr) {
83*09537850SAkhilesh Sanikop       return kLibgav1StatusOutOfMemory;
84*09537850SAkhilesh Sanikop     }
85*09537850SAkhilesh Sanikop   }
86*09537850SAkhilesh Sanikop 
87*09537850SAkhilesh Sanikop   uint8_t* const y_buffer = buffer_private->data[0];
88*09537850SAkhilesh Sanikop   uint8_t* const u_buffer =
89*09537850SAkhilesh Sanikop       (info.uv_buffer_size != 0) ? buffer_private->data[1] : nullptr;
90*09537850SAkhilesh Sanikop   uint8_t* const v_buffer =
91*09537850SAkhilesh Sanikop       (info.uv_buffer_size != 0) ? buffer_private->data[2] : nullptr;
92*09537850SAkhilesh Sanikop 
93*09537850SAkhilesh Sanikop   status = Libgav1SetFrameBuffer(&info, y_buffer, u_buffer, v_buffer,
94*09537850SAkhilesh Sanikop                                  buffer_private.release(), frame_buffer);
95*09537850SAkhilesh Sanikop   if (status != kLibgav1StatusOk) return status;
96*09537850SAkhilesh Sanikop 
97*09537850SAkhilesh Sanikop   auto* const decoder_test = static_cast<DecoderTest*>(callback_private_data);
98*09537850SAkhilesh Sanikop   decoder_test->IncrementFramesInUse();
99*09537850SAkhilesh Sanikop   decoder_test->SetBufferPrivateData(frame_buffer->private_data);
100*09537850SAkhilesh Sanikop   return kLibgav1StatusOk;
101*09537850SAkhilesh Sanikop }
102*09537850SAkhilesh Sanikop 
ReleaseFrameBuffer(void * callback_private_data,void * buffer_private_data)103*09537850SAkhilesh Sanikop static void ReleaseFrameBuffer(void* callback_private_data,
104*09537850SAkhilesh Sanikop                                void* buffer_private_data) {
105*09537850SAkhilesh Sanikop   auto* buffer_private = static_cast<FrameBufferPrivate*>(buffer_private_data);
106*09537850SAkhilesh Sanikop   for (auto& data : buffer_private->data) {
107*09537850SAkhilesh Sanikop     delete[] data;
108*09537850SAkhilesh Sanikop   }
109*09537850SAkhilesh Sanikop   delete buffer_private;
110*09537850SAkhilesh Sanikop   auto* const decoder_test = static_cast<DecoderTest*>(callback_private_data);
111*09537850SAkhilesh Sanikop   decoder_test->DecrementFramesInUse();
112*09537850SAkhilesh Sanikop }
113*09537850SAkhilesh Sanikop 
ReleaseInputBuffer(void * private_data,void * input_buffer)114*09537850SAkhilesh Sanikop static void ReleaseInputBuffer(void* private_data, void* input_buffer) {
115*09537850SAkhilesh Sanikop   auto* const decoder_test = static_cast<DecoderTest*>(private_data);
116*09537850SAkhilesh Sanikop   decoder_test->SetReleasedInputBuffer(input_buffer);
117*09537850SAkhilesh Sanikop }
118*09537850SAkhilesh Sanikop 
119*09537850SAkhilesh Sanikop }  // extern "C"
120*09537850SAkhilesh Sanikop 
SetUp()121*09537850SAkhilesh Sanikop void DecoderTest::SetUp() {
122*09537850SAkhilesh Sanikop   decoder_.reset(new (std::nothrow) Decoder());
123*09537850SAkhilesh Sanikop   ASSERT_NE(decoder_, nullptr);
124*09537850SAkhilesh Sanikop   DecoderSettings settings = {};
125*09537850SAkhilesh Sanikop   settings.frame_parallel = false;
126*09537850SAkhilesh Sanikop   settings.get_frame_buffer = GetFrameBuffer;
127*09537850SAkhilesh Sanikop   settings.release_frame_buffer = ReleaseFrameBuffer;
128*09537850SAkhilesh Sanikop   settings.callback_private_data = this;
129*09537850SAkhilesh Sanikop   settings.release_input_buffer = ReleaseInputBuffer;
130*09537850SAkhilesh Sanikop   ASSERT_EQ(decoder_->Init(&settings), kStatusOk);
131*09537850SAkhilesh Sanikop }
132*09537850SAkhilesh Sanikop 
TEST_F(DecoderTest,APIFlowForNonFrameParallelMode)133*09537850SAkhilesh Sanikop TEST_F(DecoderTest, APIFlowForNonFrameParallelMode) {
134*09537850SAkhilesh Sanikop   StatusCode status;
135*09537850SAkhilesh Sanikop   const DecoderBuffer* buffer;
136*09537850SAkhilesh Sanikop 
137*09537850SAkhilesh Sanikop   // Enqueue frame1 for decoding.
138*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame1, sizeof(kFrame1), 0,
139*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame1));
140*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
141*09537850SAkhilesh Sanikop 
142*09537850SAkhilesh Sanikop   // In non-frame-parallel mode, decoding happens only in the DequeueFrame call.
143*09537850SAkhilesh Sanikop   // So there should be no frames in use yet.
144*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
145*09537850SAkhilesh Sanikop 
146*09537850SAkhilesh Sanikop   // Dequeue the output of frame1.
147*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
148*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
149*09537850SAkhilesh Sanikop   ASSERT_NE(buffer, nullptr);
150*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame1);
151*09537850SAkhilesh Sanikop 
152*09537850SAkhilesh Sanikop   // libgav1 has decoded frame1 and is holding a reference to it.
153*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 1);
154*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer_private_data_, buffer->buffer_private_data);
155*09537850SAkhilesh Sanikop 
156*09537850SAkhilesh Sanikop   // Enqueue frame2 for decoding.
157*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame2, sizeof(kFrame2), 0,
158*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame2));
159*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
160*09537850SAkhilesh Sanikop 
161*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 1);
162*09537850SAkhilesh Sanikop 
163*09537850SAkhilesh Sanikop   // Dequeue the output of frame2.
164*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
165*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
166*09537850SAkhilesh Sanikop   ASSERT_NE(buffer, nullptr);
167*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame2);
168*09537850SAkhilesh Sanikop 
169*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 2);
170*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer_private_data_, buffer->buffer_private_data);
171*09537850SAkhilesh Sanikop 
172*09537850SAkhilesh Sanikop   // Signal end of stream (method 1). This should ensure that all the references
173*09537850SAkhilesh Sanikop   // are released.
174*09537850SAkhilesh Sanikop   status = decoder_->SignalEOS();
175*09537850SAkhilesh Sanikop   EXPECT_EQ(status, kStatusOk);
176*09537850SAkhilesh Sanikop 
177*09537850SAkhilesh Sanikop   // libgav1 should have released all the reference frames now.
178*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
179*09537850SAkhilesh Sanikop 
180*09537850SAkhilesh Sanikop   // Now, the decoder is ready to accept a new coded video sequence.
181*09537850SAkhilesh Sanikop 
182*09537850SAkhilesh Sanikop   // Enqueue frame1 for decoding.
183*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame1, sizeof(kFrame1), 0,
184*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame1));
185*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
186*09537850SAkhilesh Sanikop 
187*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
188*09537850SAkhilesh Sanikop 
189*09537850SAkhilesh Sanikop   // Dequeue the output of frame1.
190*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
191*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
192*09537850SAkhilesh Sanikop   ASSERT_NE(buffer, nullptr);
193*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame1);
194*09537850SAkhilesh Sanikop 
195*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 1);
196*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer_private_data_, buffer->buffer_private_data);
197*09537850SAkhilesh Sanikop 
198*09537850SAkhilesh Sanikop   // Enqueue frame2 for decoding.
199*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame2, sizeof(kFrame2), 0,
200*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame2));
201*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
202*09537850SAkhilesh Sanikop 
203*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 1);
204*09537850SAkhilesh Sanikop 
205*09537850SAkhilesh Sanikop   // Dequeue the output of frame2.
206*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
207*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
208*09537850SAkhilesh Sanikop   ASSERT_NE(buffer, nullptr);
209*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame2);
210*09537850SAkhilesh Sanikop 
211*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 2);
212*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer_private_data_, buffer->buffer_private_data);
213*09537850SAkhilesh Sanikop 
214*09537850SAkhilesh Sanikop   // Signal end of stream (method 2). This should ensure that all the references
215*09537850SAkhilesh Sanikop   // are released.
216*09537850SAkhilesh Sanikop   decoder_ = nullptr;
217*09537850SAkhilesh Sanikop 
218*09537850SAkhilesh Sanikop   // libgav1 should have released all the frames now.
219*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
220*09537850SAkhilesh Sanikop }
221*09537850SAkhilesh Sanikop 
TEST_F(DecoderTest,NonFrameParallelModeEnqueueMultipleFramesWithoutDequeuing)222*09537850SAkhilesh Sanikop TEST_F(DecoderTest, NonFrameParallelModeEnqueueMultipleFramesWithoutDequeuing) {
223*09537850SAkhilesh Sanikop   StatusCode status;
224*09537850SAkhilesh Sanikop   const DecoderBuffer* buffer;
225*09537850SAkhilesh Sanikop 
226*09537850SAkhilesh Sanikop   // Enqueue frame1 for decoding.
227*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame1, sizeof(kFrame1), 0,
228*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame1));
229*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
230*09537850SAkhilesh Sanikop 
231*09537850SAkhilesh Sanikop   // Until the output of frame1 is dequeued, no other frames can be enqueued.
232*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame2, sizeof(kFrame2), 0,
233*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame2));
234*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusTryAgain);
235*09537850SAkhilesh Sanikop 
236*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
237*09537850SAkhilesh Sanikop 
238*09537850SAkhilesh Sanikop   // Dequeue the output of frame1.
239*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
240*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
241*09537850SAkhilesh Sanikop   ASSERT_NE(buffer, nullptr);
242*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame1);
243*09537850SAkhilesh Sanikop 
244*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 1);
245*09537850SAkhilesh Sanikop 
246*09537850SAkhilesh Sanikop   // Delete the decoder instance.
247*09537850SAkhilesh Sanikop   decoder_ = nullptr;
248*09537850SAkhilesh Sanikop 
249*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
250*09537850SAkhilesh Sanikop }
251*09537850SAkhilesh Sanikop 
TEST_F(DecoderTest,NonFrameParallelModeEOSBeforeDequeuingLastFrame)252*09537850SAkhilesh Sanikop TEST_F(DecoderTest, NonFrameParallelModeEOSBeforeDequeuingLastFrame) {
253*09537850SAkhilesh Sanikop   StatusCode status;
254*09537850SAkhilesh Sanikop   const DecoderBuffer* buffer;
255*09537850SAkhilesh Sanikop 
256*09537850SAkhilesh Sanikop   // Enqueue frame1 for decoding.
257*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame1, sizeof(kFrame1), 0,
258*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame1));
259*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
260*09537850SAkhilesh Sanikop 
261*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
262*09537850SAkhilesh Sanikop 
263*09537850SAkhilesh Sanikop   // Dequeue the output of frame1.
264*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
265*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
266*09537850SAkhilesh Sanikop   ASSERT_NE(buffer, nullptr);
267*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame1);
268*09537850SAkhilesh Sanikop 
269*09537850SAkhilesh Sanikop   // Enqueue frame2 for decoding.
270*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame2, sizeof(kFrame2), 0,
271*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame2));
272*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
273*09537850SAkhilesh Sanikop 
274*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 1);
275*09537850SAkhilesh Sanikop 
276*09537850SAkhilesh Sanikop   // Signal end of stream before dequeuing the output of frame2.
277*09537850SAkhilesh Sanikop   status = decoder_->SignalEOS();
278*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
279*09537850SAkhilesh Sanikop 
280*09537850SAkhilesh Sanikop   // In this case, the output of the last frame that was enqueued is lost (which
281*09537850SAkhilesh Sanikop   // is intentional since end of stream was signaled without dequeueing it).
282*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
283*09537850SAkhilesh Sanikop }
284*09537850SAkhilesh Sanikop 
TEST_F(DecoderTest,NonFrameParallelModeInvalidFrameAfterEOS)285*09537850SAkhilesh Sanikop TEST_F(DecoderTest, NonFrameParallelModeInvalidFrameAfterEOS) {
286*09537850SAkhilesh Sanikop   StatusCode status;
287*09537850SAkhilesh Sanikop   const DecoderBuffer* buffer = nullptr;
288*09537850SAkhilesh Sanikop 
289*09537850SAkhilesh Sanikop   // Enqueue frame1 for decoding.
290*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame1, sizeof(kFrame1), 0,
291*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame1));
292*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
293*09537850SAkhilesh Sanikop 
294*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
295*09537850SAkhilesh Sanikop 
296*09537850SAkhilesh Sanikop   // Dequeue the output of frame1.
297*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
298*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
299*09537850SAkhilesh Sanikop   ASSERT_NE(buffer, nullptr);
300*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame1);
301*09537850SAkhilesh Sanikop 
302*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 1);
303*09537850SAkhilesh Sanikop 
304*09537850SAkhilesh Sanikop   // Signal end of stream.
305*09537850SAkhilesh Sanikop   status = decoder_->SignalEOS();
306*09537850SAkhilesh Sanikop   EXPECT_EQ(status, kStatusOk);
307*09537850SAkhilesh Sanikop 
308*09537850SAkhilesh Sanikop   // libgav1 should have released all the reference frames now.
309*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
310*09537850SAkhilesh Sanikop 
311*09537850SAkhilesh Sanikop   // Now, the decoder is ready to accept a new coded video sequence. But, we
312*09537850SAkhilesh Sanikop   // try to enqueue a frame that does not have a sequence header (which is not
313*09537850SAkhilesh Sanikop   // allowed).
314*09537850SAkhilesh Sanikop 
315*09537850SAkhilesh Sanikop   // Enqueue frame2 for decoding.
316*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(kFrame2, sizeof(kFrame2), 0,
317*09537850SAkhilesh Sanikop                                   const_cast<uint8_t*>(kFrame2));
318*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
319*09537850SAkhilesh Sanikop 
320*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
321*09537850SAkhilesh Sanikop 
322*09537850SAkhilesh Sanikop   // Dequeue the output of frame2 (this will fail since no sequence header has
323*09537850SAkhilesh Sanikop   // been seen since the last EOS signal).
324*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
325*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusBitstreamError);
326*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame2);
327*09537850SAkhilesh Sanikop 
328*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
329*09537850SAkhilesh Sanikop }
330*09537850SAkhilesh Sanikop 
TEST_F(DecoderTest,MetadataObu)331*09537850SAkhilesh Sanikop TEST_F(DecoderTest, MetadataObu) {
332*09537850SAkhilesh Sanikop   StatusCode status;
333*09537850SAkhilesh Sanikop   const DecoderBuffer* buffer;
334*09537850SAkhilesh Sanikop 
335*09537850SAkhilesh Sanikop   // Enqueue frame1 for decoding.
336*09537850SAkhilesh Sanikop   status = decoder_->EnqueueFrame(
337*09537850SAkhilesh Sanikop       kFrame1WithHdrCllAndHdrMdcv, sizeof(kFrame1WithHdrCllAndHdrMdcv), 0,
338*09537850SAkhilesh Sanikop       const_cast<uint8_t*>(kFrame1WithHdrCllAndHdrMdcv));
339*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
340*09537850SAkhilesh Sanikop 
341*09537850SAkhilesh Sanikop   // Dequeue the output of frame1.
342*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
343*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
344*09537850SAkhilesh Sanikop   ASSERT_NE(buffer, nullptr);
345*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer->has_hdr_cll, 1);
346*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer->has_hdr_mdcv, 1);
347*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer->has_itut_t35, 0);
348*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame1WithHdrCllAndHdrMdcv);
349*09537850SAkhilesh Sanikop 
350*09537850SAkhilesh Sanikop   // libgav1 has decoded frame1 and is holding a reference to it.
351*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 1);
352*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer_private_data_, buffer->buffer_private_data);
353*09537850SAkhilesh Sanikop 
354*09537850SAkhilesh Sanikop   // Enqueue frame2 for decoding.
355*09537850SAkhilesh Sanikop   status =
356*09537850SAkhilesh Sanikop       decoder_->EnqueueFrame(kFrame2WithItutT35, sizeof(kFrame2WithItutT35), 0,
357*09537850SAkhilesh Sanikop                              const_cast<uint8_t*>(kFrame2WithItutT35));
358*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
359*09537850SAkhilesh Sanikop 
360*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 1);
361*09537850SAkhilesh Sanikop 
362*09537850SAkhilesh Sanikop   // Dequeue the output of frame2.
363*09537850SAkhilesh Sanikop   status = decoder_->DequeueFrame(&buffer);
364*09537850SAkhilesh Sanikop   ASSERT_EQ(status, kStatusOk);
365*09537850SAkhilesh Sanikop   ASSERT_NE(buffer, nullptr);
366*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer->has_hdr_cll, 0);
367*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer->has_hdr_mdcv, 0);
368*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer->has_itut_t35, 1);
369*09537850SAkhilesh Sanikop   EXPECT_NE(buffer->itut_t35.payload_bytes, nullptr);
370*09537850SAkhilesh Sanikop   EXPECT_GT(buffer->itut_t35.payload_size, 0);
371*09537850SAkhilesh Sanikop   EXPECT_EQ(released_input_buffer_, &kFrame2WithItutT35);
372*09537850SAkhilesh Sanikop 
373*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 2);
374*09537850SAkhilesh Sanikop   EXPECT_EQ(buffer_private_data_, buffer->buffer_private_data);
375*09537850SAkhilesh Sanikop 
376*09537850SAkhilesh Sanikop   status = decoder_->SignalEOS();
377*09537850SAkhilesh Sanikop   EXPECT_EQ(status, kStatusOk);
378*09537850SAkhilesh Sanikop   EXPECT_EQ(frames_in_use_, 0);
379*09537850SAkhilesh Sanikop }
380*09537850SAkhilesh Sanikop 
381*09537850SAkhilesh Sanikop }  // namespace
382*09537850SAkhilesh Sanikop }  // namespace libgav1
383