xref: /aosp_15_r20/external/libaom/test/decode_test_driver.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "test/codec_factory.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "test/decode_test_driver.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "test/register_state_check.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "test/video_source.h"
18*77c1e3ccSAndroid Build Coastguard Worker 
19*77c1e3ccSAndroid Build Coastguard Worker namespace libaom_test {
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker const char kAV1Name[] = "AOMedia Project AV1 Decoder";
22*77c1e3ccSAndroid Build Coastguard Worker 
PeekStream(const uint8_t * cxdata,size_t size,aom_codec_stream_info_t * stream_info)23*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t Decoder::PeekStream(const uint8_t *cxdata, size_t size,
24*77c1e3ccSAndroid Build Coastguard Worker                                     aom_codec_stream_info_t *stream_info) {
25*77c1e3ccSAndroid Build Coastguard Worker   return aom_codec_peek_stream_info(CodecInterface(), cxdata, size,
26*77c1e3ccSAndroid Build Coastguard Worker                                     stream_info);
27*77c1e3ccSAndroid Build Coastguard Worker }
28*77c1e3ccSAndroid Build Coastguard Worker 
DecodeFrame(const uint8_t * cxdata,size_t size)29*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t Decoder::DecodeFrame(const uint8_t *cxdata, size_t size) {
30*77c1e3ccSAndroid Build Coastguard Worker   return DecodeFrame(cxdata, size, nullptr);
31*77c1e3ccSAndroid Build Coastguard Worker }
32*77c1e3ccSAndroid Build Coastguard Worker 
DecodeFrame(const uint8_t * cxdata,size_t size,void * user_priv)33*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t Decoder::DecodeFrame(const uint8_t *cxdata, size_t size,
34*77c1e3ccSAndroid Build Coastguard Worker                                      void *user_priv) {
35*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res_dec;
36*77c1e3ccSAndroid Build Coastguard Worker   InitOnce();
37*77c1e3ccSAndroid Build Coastguard Worker   API_REGISTER_STATE_CHECK(
38*77c1e3ccSAndroid Build Coastguard Worker       res_dec = aom_codec_decode(&decoder_, cxdata, size, user_priv));
39*77c1e3ccSAndroid Build Coastguard Worker   return res_dec;
40*77c1e3ccSAndroid Build Coastguard Worker }
41*77c1e3ccSAndroid Build Coastguard Worker 
IsAV1() const42*77c1e3ccSAndroid Build Coastguard Worker bool Decoder::IsAV1() const {
43*77c1e3ccSAndroid Build Coastguard Worker   const char *codec_name = GetDecoderName();
44*77c1e3ccSAndroid Build Coastguard Worker   return strncmp(kAV1Name, codec_name, sizeof(kAV1Name) - 1) == 0;
45*77c1e3ccSAndroid Build Coastguard Worker }
46*77c1e3ccSAndroid Build Coastguard Worker 
HandlePeekResult(Decoder * const,CompressedVideoSource *,const aom_codec_err_t res_peek)47*77c1e3ccSAndroid Build Coastguard Worker void DecoderTest::HandlePeekResult(Decoder *const /*decoder*/,
48*77c1e3ccSAndroid Build Coastguard Worker                                    CompressedVideoSource * /*video*/,
49*77c1e3ccSAndroid Build Coastguard Worker                                    const aom_codec_err_t res_peek) {
50*77c1e3ccSAndroid Build Coastguard Worker   /* The Av1 implementation of PeekStream returns an error only if the
51*77c1e3ccSAndroid Build Coastguard Worker    * data passed to it isn't a valid Av1 chunk. */
52*77c1e3ccSAndroid Build Coastguard Worker   ASSERT_EQ(AOM_CODEC_OK, res_peek)
53*77c1e3ccSAndroid Build Coastguard Worker       << "Peek return failed: " << aom_codec_err_to_string(res_peek);
54*77c1e3ccSAndroid Build Coastguard Worker }
55*77c1e3ccSAndroid Build Coastguard Worker 
RunLoop(CompressedVideoSource * video,const aom_codec_dec_cfg_t & dec_cfg)56*77c1e3ccSAndroid Build Coastguard Worker void DecoderTest::RunLoop(CompressedVideoSource *video,
57*77c1e3ccSAndroid Build Coastguard Worker                           const aom_codec_dec_cfg_t &dec_cfg) {
58*77c1e3ccSAndroid Build Coastguard Worker   Decoder *const decoder = codec_->CreateDecoder(dec_cfg, flags_);
59*77c1e3ccSAndroid Build Coastguard Worker   ASSERT_NE(decoder, nullptr);
60*77c1e3ccSAndroid Build Coastguard Worker   bool end_of_file = false;
61*77c1e3ccSAndroid Build Coastguard Worker   bool peeked_stream = false;
62*77c1e3ccSAndroid Build Coastguard Worker 
63*77c1e3ccSAndroid Build Coastguard Worker   // Decode frames.
64*77c1e3ccSAndroid Build Coastguard Worker   for (video->Begin(); !::testing::Test::HasFailure() && !end_of_file;
65*77c1e3ccSAndroid Build Coastguard Worker        video->Next()) {
66*77c1e3ccSAndroid Build Coastguard Worker     PreDecodeFrameHook(*video, decoder);
67*77c1e3ccSAndroid Build Coastguard Worker 
68*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_stream_info_t stream_info;
69*77c1e3ccSAndroid Build Coastguard Worker     stream_info.is_annexb = 0;
70*77c1e3ccSAndroid Build Coastguard Worker 
71*77c1e3ccSAndroid Build Coastguard Worker     if (video->cxdata() != nullptr) {
72*77c1e3ccSAndroid Build Coastguard Worker       if (!peeked_stream) {
73*77c1e3ccSAndroid Build Coastguard Worker         // TODO(yaowu): PeekStream returns error for non-sequence_header_obu,
74*77c1e3ccSAndroid Build Coastguard Worker         // therefore should only be tried once per sequence, this shall be fixed
75*77c1e3ccSAndroid Build Coastguard Worker         // once PeekStream is updated to properly operate on other obus.
76*77c1e3ccSAndroid Build Coastguard Worker         const aom_codec_err_t res_peek = decoder->PeekStream(
77*77c1e3ccSAndroid Build Coastguard Worker             video->cxdata(), video->frame_size(), &stream_info);
78*77c1e3ccSAndroid Build Coastguard Worker         HandlePeekResult(decoder, video, res_peek);
79*77c1e3ccSAndroid Build Coastguard Worker         ASSERT_FALSE(::testing::Test::HasFailure());
80*77c1e3ccSAndroid Build Coastguard Worker         peeked_stream = true;
81*77c1e3ccSAndroid Build Coastguard Worker       }
82*77c1e3ccSAndroid Build Coastguard Worker 
83*77c1e3ccSAndroid Build Coastguard Worker       aom_codec_err_t res_dec =
84*77c1e3ccSAndroid Build Coastguard Worker           decoder->DecodeFrame(video->cxdata(), video->frame_size());
85*77c1e3ccSAndroid Build Coastguard Worker       if (!HandleDecodeResult(res_dec, *video, decoder)) break;
86*77c1e3ccSAndroid Build Coastguard Worker     } else {
87*77c1e3ccSAndroid Build Coastguard Worker       // Signal end of the file to the decoder.
88*77c1e3ccSAndroid Build Coastguard Worker       const aom_codec_err_t res_dec = decoder->DecodeFrame(nullptr, 0);
89*77c1e3ccSAndroid Build Coastguard Worker       ASSERT_EQ(AOM_CODEC_OK, res_dec) << decoder->DecodeError();
90*77c1e3ccSAndroid Build Coastguard Worker       end_of_file = true;
91*77c1e3ccSAndroid Build Coastguard Worker     }
92*77c1e3ccSAndroid Build Coastguard Worker 
93*77c1e3ccSAndroid Build Coastguard Worker     DxDataIterator dec_iter = decoder->GetDxData();
94*77c1e3ccSAndroid Build Coastguard Worker     const aom_image_t *img = nullptr;
95*77c1e3ccSAndroid Build Coastguard Worker 
96*77c1e3ccSAndroid Build Coastguard Worker     // Get decompressed data
97*77c1e3ccSAndroid Build Coastguard Worker     while (!::testing::Test::HasFailure() && (img = dec_iter.Next()))
98*77c1e3ccSAndroid Build Coastguard Worker       DecompressedFrameHook(*img, video->frame_number());
99*77c1e3ccSAndroid Build Coastguard Worker   }
100*77c1e3ccSAndroid Build Coastguard Worker   delete decoder;
101*77c1e3ccSAndroid Build Coastguard Worker }
102*77c1e3ccSAndroid Build Coastguard Worker 
RunLoop(CompressedVideoSource * video)103*77c1e3ccSAndroid Build Coastguard Worker void DecoderTest::RunLoop(CompressedVideoSource *video) {
104*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_dec_cfg_t dec_cfg = aom_codec_dec_cfg_t();
105*77c1e3ccSAndroid Build Coastguard Worker   RunLoop(video, dec_cfg);
106*77c1e3ccSAndroid Build Coastguard Worker }
107*77c1e3ccSAndroid Build Coastguard Worker 
set_cfg(const aom_codec_dec_cfg_t & dec_cfg)108*77c1e3ccSAndroid Build Coastguard Worker void DecoderTest::set_cfg(const aom_codec_dec_cfg_t &dec_cfg) {
109*77c1e3ccSAndroid Build Coastguard Worker   memcpy(&cfg_, &dec_cfg, sizeof(cfg_));
110*77c1e3ccSAndroid Build Coastguard Worker }
111*77c1e3ccSAndroid Build Coastguard Worker 
set_flags(const aom_codec_flags_t flags)112*77c1e3ccSAndroid Build Coastguard Worker void DecoderTest::set_flags(const aom_codec_flags_t flags) { flags_ = flags; }
113*77c1e3ccSAndroid Build Coastguard Worker 
114*77c1e3ccSAndroid Build Coastguard Worker }  // namespace libaom_test
115