1 /*
2 * Copyright (c) 2021, 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
12 #include <cstring>
13 #include <tuple>
14
15 #include "aom/aom_codec.h"
16 #include "aom/aom_decoder.h"
17 #include "aom/aom_encoder.h"
18 #include "aom/aomcx.h"
19 #include "aom/aomdx.h"
20 #include "config/aom_config.h"
21 #include "gtest/gtest.h"
22
23 namespace {
24 typedef std::tuple<const char *, const char *> KeyValParam;
25
26 class BaseKeyValAPI : public testing::Test {
27 public:
SetUp()28 void SetUp() override {
29 #if CONFIG_AV1_ENCODER
30 aom_codec_iface_t *iface_cx = aom_codec_av1_cx();
31 aom_codec_enc_cfg_t enc_cfg;
32 #if CONFIG_REALTIME_ONLY
33 const int usage = 1;
34 #else
35 const int usage = 0;
36 #endif
37 EXPECT_EQ(AOM_CODEC_OK,
38 aom_codec_enc_config_default(iface_cx, &enc_cfg, usage));
39 EXPECT_EQ(AOM_CODEC_OK,
40 aom_codec_enc_init(&enc_, iface_cx, &enc_cfg, usage));
41 #endif
42 #if CONFIG_AV1_DECODER
43 aom_codec_iface_t *iface_dx = aom_codec_av1_dx();
44 aom_codec_dec_cfg_t dec_cfg = { 0, 0, 0, !FORCE_HIGHBITDEPTH_DECODING };
45
46 EXPECT_EQ(AOM_CODEC_OK, aom_codec_dec_init(&dec_, iface_dx, &dec_cfg, 0));
47 #endif
48 }
49
TearDown()50 void TearDown() override {
51 #if CONFIG_AV1_ENCODER
52 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&enc_));
53 #endif
54 #if CONFIG_AV1_DECODER
55 EXPECT_EQ(AOM_CODEC_OK, aom_codec_destroy(&dec_));
56 #endif
57 }
58
59 protected:
60 #if CONFIG_AV1_ENCODER
61 aom_codec_ctx_t enc_;
62 #endif
63 #if CONFIG_AV1_DECODER
64 aom_codec_ctx_t dec_;
65 #endif
66 };
67
68 // Tests on encoder options.
69 // Need to add ones for the decoder in the future if it is also supported in the
70 // key & value API.
71 #if CONFIG_AV1_ENCODER
72 class EncValidTest : public BaseKeyValAPI,
73 public testing::WithParamInterface<KeyValParam> {};
74 class EncInvalidTest : public BaseKeyValAPI,
75 public testing::WithParamInterface<KeyValParam> {};
76
TEST_P(EncValidTest,Valid)77 TEST_P(EncValidTest, Valid) {
78 const char *key = std::get<0>(GetParam());
79 const char *val = std::get<1>(GetParam());
80 EXPECT_EQ(AOM_CODEC_OK, aom_codec_set_option(&enc_, key, val));
81 }
82
TEST_P(EncInvalidTest,NullArg)83 TEST_P(EncInvalidTest, NullArg) {
84 const char *key = std::get<0>(GetParam());
85 const char *val = std::get<1>(GetParam());
86 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_set_option(nullptr, key, val));
87 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_set_option(&enc_, nullptr, val));
88 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_set_option(&enc_, key, nullptr));
89 }
90
TEST_P(EncInvalidTest,InvalidParam)91 TEST_P(EncInvalidTest, InvalidParam) {
92 const char *key = std::get<0>(GetParam());
93 const char *val = std::get<1>(GetParam());
94 EXPECT_EQ(AOM_CODEC_INVALID_PARAM, aom_codec_set_option(&enc_, key, val));
95 ASSERT_NE(aom_codec_error_detail(&enc_), nullptr);
96 EXPECT_GT(strlen(aom_codec_error_detail(&enc_)), 0u);
97 }
98
99 // No test for ratio / list for now since the API does not support any of the
100 // parameters of these type.
101 // The string type typically involves reading a path/file, which brings
102 // potential fails.
103 const KeyValParam enc_valid_params[] = {
104 std::make_tuple("auto-intra-tools-off", "1"), // uint
105 std::make_tuple("min-gf-interval", "10"), // uint
106 std::make_tuple("min-partition-size", "4"), // int
107 std::make_tuple("tune", "psnr"), // enum
108 };
109
110 const KeyValParam enc_invalid_params[] = {
111 // no match
112 std::make_tuple("a-b-c", "10"),
113 // uint
114 std::make_tuple("min-gf-interval", "-1"),
115 std::make_tuple("min-gf-interval", "1.1"),
116 std::make_tuple("min-gf-interval", "abc"),
117 // int
118 std::make_tuple("min-partition-size", "1.1"),
119 std::make_tuple("min-partition-size", "abc"),
120 // enum
121 std::make_tuple("tune", "PsnR1"),
122 // out of range
123 std::make_tuple("cq-level", "1000"),
124 };
125
126 INSTANTIATE_TEST_SUITE_P(KeyValAPI, EncValidTest,
127 testing::ValuesIn(enc_valid_params));
128
129 INSTANTIATE_TEST_SUITE_P(KeyValAPI, EncInvalidTest,
130 testing::ValuesIn(enc_invalid_params));
131 #endif // CONFIG_AV1_ENCODER
132
133 } // namespace
134