xref: /aosp_15_r20/external/libaom/test/codec_factory.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 #ifndef AOM_TEST_CODEC_FACTORY_H_
12 #define AOM_TEST_CODEC_FACTORY_H_
13 
14 #include <tuple>
15 
16 #include "config/aom_config.h"
17 
18 #include "aom/aom_decoder.h"
19 #include "aom/aom_encoder.h"
20 #if CONFIG_AV1_ENCODER
21 #include "aom/aomcx.h"
22 #endif
23 #if CONFIG_AV1_DECODER
24 #include "aom/aomdx.h"
25 #endif
26 
27 #include "test/decode_test_driver.h"
28 #include "test/encode_test_driver.h"
29 namespace libaom_test {
30 
31 const int kCodecFactoryParam = 0;
32 
33 class CodecFactory {
34  public:
35   CodecFactory() = default;
36 
37   virtual ~CodecFactory() = default;
38 
39   virtual Decoder *CreateDecoder(aom_codec_dec_cfg_t cfg) const = 0;
40 
41   virtual Decoder *CreateDecoder(aom_codec_dec_cfg_t cfg,
42                                  const aom_codec_flags_t flags) const = 0;
43 
44   virtual Encoder *CreateEncoder(aom_codec_enc_cfg_t cfg,
45                                  const aom_codec_flags_t init_flags,
46                                  TwopassStatsStore *stats) const = 0;
47 
48   virtual aom_codec_err_t DefaultEncoderConfig(aom_codec_enc_cfg_t *cfg,
49                                                unsigned int usage) const = 0;
50 };
51 
52 /* Provide CodecTestWith<n>Params classes for a variable number of parameters
53  * to avoid having to include a pointer to the CodecFactory in every test
54  * definition.
55  */
56 template <class T1>
57 class CodecTestWithParam
58     : public ::testing::TestWithParam<
59           std::tuple<const libaom_test::CodecFactory *, T1> > {};
60 
61 template <class T1, class T2>
62 class CodecTestWith2Params
63     : public ::testing::TestWithParam<
64           std::tuple<const libaom_test::CodecFactory *, T1, T2> > {};
65 
66 template <class T1, class T2, class T3>
67 class CodecTestWith3Params
68     : public ::testing::TestWithParam<
69           std::tuple<const libaom_test::CodecFactory *, T1, T2, T3> > {};
70 
71 template <class T1, class T2, class T3, class T4>
72 class CodecTestWith4Params
73     : public ::testing::TestWithParam<
74           std::tuple<const libaom_test::CodecFactory *, T1, T2, T3, T4> > {};
75 
76 template <class T1, class T2, class T3, class T4, class T5>
77 class CodecTestWith5Params
78     : public ::testing::TestWithParam<
79           std::tuple<const libaom_test::CodecFactory *, T1, T2, T3, T4, T5> > {
80 };
81 
82 template <class T1, class T2, class T3, class T4, class T5, class T6>
83 class CodecTestWith6Params
84     : public ::testing::TestWithParam<std::tuple<
85           const libaom_test::CodecFactory *, T1, T2, T3, T4, T5, T6> > {};
86 
87 /*
88  * AV1 Codec Definitions
89  */
90 class AV1Decoder : public Decoder {
91  public:
AV1Decoder(aom_codec_dec_cfg_t cfg)92   explicit AV1Decoder(aom_codec_dec_cfg_t cfg) : Decoder(cfg) {}
93 
AV1Decoder(aom_codec_dec_cfg_t cfg,const aom_codec_flags_t flag)94   AV1Decoder(aom_codec_dec_cfg_t cfg, const aom_codec_flags_t flag)
95       : Decoder(cfg, flag) {}
96 
97  protected:
CodecInterface()98   aom_codec_iface_t *CodecInterface() const override {
99 #if CONFIG_AV1_DECODER
100     return aom_codec_av1_dx();
101 #else
102     return nullptr;
103 #endif
104   }
105 };
106 
107 class AV1Encoder : public Encoder {
108  public:
AV1Encoder(aom_codec_enc_cfg_t cfg,const aom_codec_flags_t init_flags,TwopassStatsStore * stats)109   AV1Encoder(aom_codec_enc_cfg_t cfg, const aom_codec_flags_t init_flags,
110              TwopassStatsStore *stats)
111       : Encoder(cfg, init_flags, stats) {}
112 
113  protected:
CodecInterface()114   aom_codec_iface_t *CodecInterface() const override {
115 #if CONFIG_AV1_ENCODER
116     return aom_codec_av1_cx();
117 #else
118     return nullptr;
119 #endif
120   }
121 };
122 
123 class AV1CodecFactory : public CodecFactory {
124  public:
AV1CodecFactory()125   AV1CodecFactory() : CodecFactory() {}
126 
CreateDecoder(aom_codec_dec_cfg_t cfg)127   Decoder *CreateDecoder(aom_codec_dec_cfg_t cfg) const override {
128     return CreateDecoder(cfg, 0);
129   }
130 
CreateDecoder(aom_codec_dec_cfg_t cfg,const aom_codec_flags_t flags)131   Decoder *CreateDecoder(aom_codec_dec_cfg_t cfg,
132                          const aom_codec_flags_t flags) const override {
133 #if CONFIG_AV1_DECODER
134     return new AV1Decoder(cfg, flags);
135 #else
136     (void)cfg;
137     (void)flags;
138     return nullptr;
139 #endif
140   }
141 
CreateEncoder(aom_codec_enc_cfg_t cfg,const aom_codec_flags_t init_flags,TwopassStatsStore * stats)142   Encoder *CreateEncoder(aom_codec_enc_cfg_t cfg,
143                          const aom_codec_flags_t init_flags,
144                          TwopassStatsStore *stats) const override {
145 #if CONFIG_AV1_ENCODER
146     return new AV1Encoder(cfg, init_flags, stats);
147 #else
148     (void)cfg;
149     (void)init_flags;
150     (void)stats;
151     return nullptr;
152 #endif
153   }
154 
DefaultEncoderConfig(aom_codec_enc_cfg_t * cfg,unsigned int usage)155   aom_codec_err_t DefaultEncoderConfig(aom_codec_enc_cfg_t *cfg,
156                                        unsigned int usage) const override {
157 #if CONFIG_AV1_ENCODER
158     return aom_codec_enc_config_default(aom_codec_av1_cx(), cfg, usage);
159 #else
160     (void)cfg;
161     (void)usage;
162     return AOM_CODEC_INCAPABLE;
163 #endif
164   }
165 };
166 
167 const libaom_test::AV1CodecFactory kAV1;
168 
169 #define AV1_INSTANTIATE_TEST_SUITE(test, ...)                               \
170   INSTANTIATE_TEST_SUITE_P(                                                 \
171       AV1, test,                                                            \
172       ::testing::Combine(                                                   \
173           ::testing::Values(static_cast<const libaom_test::CodecFactory *>( \
174               &libaom_test::kAV1)),                                         \
175           __VA_ARGS__))
176 
177 }  // namespace libaom_test
178 #endif  // AOM_TEST_CODEC_FACTORY_H_
179