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 "config/aom_config.h"
15 
16 #include "aom/aom_decoder.h"
17 #include "aom/aom_encoder.h"
18 #if CONFIG_AV1_ENCODER
19 #include "aom/aomcx.h"
20 #endif
21 #if CONFIG_AV1_DECODER
22 #include "aom/aomdx.h"
23 #endif
24 
25 #include "test/decode_test_driver.h"
26 #include "test/encode_test_driver.h"
27 namespace libaom_test {
28 
29 const int kCodecFactoryParam = 0;
30 
31 class CodecFactory {
32  public:
CodecFactory()33   CodecFactory() {}
34 
~CodecFactory()35   virtual ~CodecFactory() {}
36 
37   virtual Decoder *CreateDecoder(aom_codec_dec_cfg_t cfg) const = 0;
38 
39   virtual Decoder *CreateDecoder(aom_codec_dec_cfg_t cfg,
40                                  const aom_codec_flags_t flags) const = 0;
41 
42   virtual Encoder *CreateEncoder(aom_codec_enc_cfg_t cfg,
43                                  const unsigned long init_flags,
44                                  TwopassStatsStore *stats) const = 0;
45 
46   virtual aom_codec_err_t DefaultEncoderConfig(aom_codec_enc_cfg_t *cfg,
47                                                int usage) const = 0;
48 };
49 
50 /* Provide CodecTestWith<n>Params classes for a variable number of parameters
51  * to avoid having to include a pointer to the CodecFactory in every test
52  * definition.
53  */
54 template <class T1>
55 class CodecTestWithParam
56     : public ::testing::TestWithParam<
57           ::testing::tuple<const libaom_test::CodecFactory *, T1> > {};
58 
59 template <class T1, class T2>
60 class CodecTestWith2Params
61     : public ::testing::TestWithParam<
62           ::testing::tuple<const libaom_test::CodecFactory *, T1, T2> > {};
63 
64 template <class T1, class T2, class T3>
65 class CodecTestWith3Params
66     : public ::testing::TestWithParam<
67           ::testing::tuple<const libaom_test::CodecFactory *, T1, T2, T3> > {};
68 
69 template <class T1, class T2, class T3, class T4>
70 class CodecTestWith4Params
71     : public ::testing::TestWithParam< ::testing::tuple<
72           const libaom_test::CodecFactory *, T1, T2, T3, T4> > {};
73 
74 template <class T1, class T2, class T3, class T4, class T5>
75 class CodecTestWith5Params
76     : public ::testing::TestWithParam< ::testing::tuple<
77           const libaom_test::CodecFactory *, T1, T2, T3, T4, T5> > {};
78 
79 /*
80  * AV1 Codec Definitions
81  */
82 class AV1Decoder : public Decoder {
83  public:
AV1Decoder(aom_codec_dec_cfg_t cfg)84   explicit AV1Decoder(aom_codec_dec_cfg_t cfg) : Decoder(cfg) {}
85 
AV1Decoder(aom_codec_dec_cfg_t cfg,const aom_codec_flags_t flag)86   AV1Decoder(aom_codec_dec_cfg_t cfg, const aom_codec_flags_t flag)
87       : Decoder(cfg, flag) {}
88 
89  protected:
CodecInterface()90   virtual aom_codec_iface_t *CodecInterface() const {
91 #if CONFIG_AV1_DECODER
92     return aom_codec_av1_dx();
93 #else
94     return NULL;
95 #endif
96   }
97 };
98 
99 class AV1Encoder : public Encoder {
100  public:
AV1Encoder(aom_codec_enc_cfg_t cfg,const uint32_t init_flags,TwopassStatsStore * stats)101   AV1Encoder(aom_codec_enc_cfg_t cfg, const uint32_t init_flags,
102              TwopassStatsStore *stats)
103       : Encoder(cfg, init_flags, stats) {}
104 
105  protected:
CodecInterface()106   virtual aom_codec_iface_t *CodecInterface() const {
107 #if CONFIG_AV1_ENCODER
108     return aom_codec_av1_cx();
109 #else
110     return NULL;
111 #endif
112   }
113 };
114 
115 class AV1CodecFactory : public CodecFactory {
116  public:
AV1CodecFactory()117   AV1CodecFactory() : CodecFactory() {}
118 
CreateDecoder(aom_codec_dec_cfg_t cfg)119   virtual Decoder *CreateDecoder(aom_codec_dec_cfg_t cfg) const {
120     return CreateDecoder(cfg, 0);
121   }
122 
CreateDecoder(aom_codec_dec_cfg_t cfg,const aom_codec_flags_t flags)123   virtual Decoder *CreateDecoder(aom_codec_dec_cfg_t cfg,
124                                  const aom_codec_flags_t flags) const {
125 #if CONFIG_AV1_DECODER
126     return new AV1Decoder(cfg, flags);
127 #else
128     (void)cfg;
129     (void)flags;
130     return NULL;
131 #endif
132   }
133 
CreateEncoder(aom_codec_enc_cfg_t cfg,const unsigned long init_flags,TwopassStatsStore * stats)134   virtual Encoder *CreateEncoder(aom_codec_enc_cfg_t cfg,
135                                  const unsigned long init_flags,
136                                  TwopassStatsStore *stats) const {
137 #if CONFIG_AV1_ENCODER
138     return new AV1Encoder(cfg, init_flags, stats);
139 #else
140     (void)cfg;
141     (void)init_flags;
142     (void)stats;
143     return NULL;
144 #endif
145   }
146 
DefaultEncoderConfig(aom_codec_enc_cfg_t * cfg,int usage)147   virtual aom_codec_err_t DefaultEncoderConfig(aom_codec_enc_cfg_t *cfg,
148                                                int usage) const {
149 #if CONFIG_AV1_ENCODER
150     return aom_codec_enc_config_default(aom_codec_av1_cx(), cfg, usage);
151 #else
152     (void)cfg;
153     (void)usage;
154     return AOM_CODEC_INCAPABLE;
155 #endif
156   }
157 };
158 
159 const libaom_test::AV1CodecFactory kAV1;
160 
161 #define AV1_INSTANTIATE_TEST_CASE(test, ...)                                \
162   INSTANTIATE_TEST_CASE_P(                                                  \
163       AV1, test,                                                            \
164       ::testing::Combine(                                                   \
165           ::testing::Values(static_cast<const libaom_test::CodecFactory *>( \
166               &libaom_test::kAV1)),                                         \
167           __VA_ARGS__))
168 
169 }  // namespace libaom_test
170 #endif  // AOM_TEST_CODEC_FACTORY_H_
171