]> granicus.if.org Git - libvpx/blob - test/encode_api_test.cc
svc: turn off use_base_mv on non base layer.
[libvpx] / test / encode_api_test.cc
1 /*
2  *  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "third_party/googletest/src/include/gtest/gtest.h"
12
13 #include "./vpx_config.h"
14 #include "vpx/vp8cx.h"
15 #include "vpx/vpx_encoder.h"
16
17 namespace {
18
19 #define NELEMENTS(x) static_cast<int>(sizeof(x) / sizeof(x[0]))
20
21 TEST(EncodeAPI, InvalidParams) {
22   static const vpx_codec_iface_t *kCodecs[] = {
23 #if CONFIG_VP8_ENCODER
24     &vpx_codec_vp8_cx_algo,
25 #endif
26 #if CONFIG_VP9_ENCODER
27     &vpx_codec_vp9_cx_algo,
28 #endif
29   };
30   uint8_t buf[1] = { 0 };
31   vpx_image_t img;
32   vpx_codec_ctx_t enc;
33   vpx_codec_enc_cfg_t cfg;
34
35   EXPECT_EQ(&img, vpx_img_wrap(&img, VPX_IMG_FMT_I420, 1, 1, 1, buf));
36
37   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
38             vpx_codec_enc_init(nullptr, nullptr, nullptr, 0));
39   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
40             vpx_codec_enc_init(&enc, nullptr, nullptr, 0));
41   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
42             vpx_codec_encode(nullptr, nullptr, 0, 0, 0, 0));
43   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
44             vpx_codec_encode(nullptr, &img, 0, 0, 0, 0));
45   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_destroy(nullptr));
46   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
47             vpx_codec_enc_config_default(nullptr, nullptr, 0));
48   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
49             vpx_codec_enc_config_default(nullptr, &cfg, 0));
50   EXPECT_NE(vpx_codec_error(nullptr), nullptr);
51
52   for (int i = 0; i < NELEMENTS(kCodecs); ++i) {
53     SCOPED_TRACE(vpx_codec_iface_name(kCodecs[i]));
54     EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
55               vpx_codec_enc_init(nullptr, kCodecs[i], nullptr, 0));
56     EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
57               vpx_codec_enc_init(&enc, kCodecs[i], nullptr, 0));
58     EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
59               vpx_codec_enc_config_default(kCodecs[i], &cfg, 1));
60
61     EXPECT_EQ(VPX_CODEC_OK, vpx_codec_enc_config_default(kCodecs[i], &cfg, 0));
62     EXPECT_EQ(VPX_CODEC_OK, vpx_codec_enc_init(&enc, kCodecs[i], &cfg, 0));
63     EXPECT_EQ(VPX_CODEC_OK, vpx_codec_encode(&enc, nullptr, 0, 0, 0, 0));
64
65     EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&enc));
66   }
67 }
68
69 TEST(EncodeAPI, HighBitDepthCapability) {
70 // VP8 should not claim VP9 HBD as a capability.
71 #if CONFIG_VP8_ENCODER
72   const vpx_codec_caps_t vp8_caps = vpx_codec_get_caps(&vpx_codec_vp8_cx_algo);
73   EXPECT_EQ(vp8_caps & VPX_CODEC_CAP_HIGHBITDEPTH, 0);
74 #endif
75
76 #if CONFIG_VP9_ENCODER
77   const vpx_codec_caps_t vp9_caps = vpx_codec_get_caps(&vpx_codec_vp9_cx_algo);
78 #if CONFIG_VP9_HIGHBITDEPTH
79   EXPECT_EQ(vp9_caps & VPX_CODEC_CAP_HIGHBITDEPTH, VPX_CODEC_CAP_HIGHBITDEPTH);
80 #else
81   EXPECT_EQ(vp9_caps & VPX_CODEC_CAP_HIGHBITDEPTH, 0);
82 #endif
83 #endif
84 }
85
86 #if CONFIG_VP8_ENCODER
87 TEST(EncodeAPI, ImageSizeSetting) {
88   const int width = 711;
89   const int height = 360;
90   const int bps = 12;
91   vpx_image_t img;
92   vpx_codec_ctx_t enc;
93   vpx_codec_enc_cfg_t cfg;
94   uint8_t *img_buf = reinterpret_cast<uint8_t *>(
95       calloc(width * height * bps / 8, sizeof(*img_buf)));
96   vpx_codec_enc_config_default(vpx_codec_vp8_cx(), &cfg, 0);
97
98   cfg.g_w = width;
99   cfg.g_h = height;
100
101   vpx_img_wrap(&img, VPX_IMG_FMT_I420, width, height, 1, img_buf);
102
103   vpx_codec_enc_init(&enc, vpx_codec_vp8_cx(), &cfg, 0);
104
105   EXPECT_EQ(VPX_CODEC_OK, vpx_codec_encode(&enc, &img, 0, 1, 0, 0));
106
107   free(img_buf);
108
109   vpx_codec_destroy(&enc);
110 }
111 #endif
112
113 // Set up 2 spatial streams with 2 temporal layers per stream, and generate
114 // invalid configuration by setting the temporal layer rate allocation
115 // (ts_target_bitrate[]) to 0 for both layers. This should fail independent of
116 // CONFIG_MULTI_RES_ENCODING.
117 TEST(EncodeAPI, MultiResEncode) {
118   static const vpx_codec_iface_t *kCodecs[] = {
119 #if CONFIG_VP8_ENCODER
120     &vpx_codec_vp8_cx_algo,
121 #endif
122 #if CONFIG_VP9_ENCODER
123     &vpx_codec_vp9_cx_algo,
124 #endif
125   };
126   const int width = 1280;
127   const int height = 720;
128   const int width_down = width / 2;
129   const int height_down = height / 2;
130   const int target_bitrate = 1000;
131   const int framerate = 30;
132
133   for (int c = 0; c < NELEMENTS(kCodecs); ++c) {
134     const vpx_codec_iface_t *const iface = kCodecs[c];
135     vpx_codec_ctx_t enc[2];
136     vpx_codec_enc_cfg_t cfg[2];
137     vpx_rational_t dsf[2] = { { 2, 1 }, { 2, 1 } };
138
139     memset(enc, 0, sizeof(enc));
140
141     for (int i = 0; i < 2; i++) {
142       vpx_codec_enc_config_default(iface, &cfg[i], 0);
143     }
144
145     /* Highest-resolution encoder settings */
146     cfg[0].g_w = width;
147     cfg[0].g_h = height;
148     cfg[0].rc_dropframe_thresh = 0;
149     cfg[0].rc_end_usage = VPX_CBR;
150     cfg[0].rc_resize_allowed = 0;
151     cfg[0].rc_min_quantizer = 2;
152     cfg[0].rc_max_quantizer = 56;
153     cfg[0].rc_undershoot_pct = 100;
154     cfg[0].rc_overshoot_pct = 15;
155     cfg[0].rc_buf_initial_sz = 500;
156     cfg[0].rc_buf_optimal_sz = 600;
157     cfg[0].rc_buf_sz = 1000;
158     cfg[0].g_error_resilient = 1; /* Enable error resilient mode */
159     cfg[0].g_lag_in_frames = 0;
160
161     cfg[0].kf_mode = VPX_KF_AUTO;
162     cfg[0].kf_min_dist = 3000;
163     cfg[0].kf_max_dist = 3000;
164
165     cfg[0].rc_target_bitrate = target_bitrate; /* Set target bitrate */
166     cfg[0].g_timebase.num = 1;                 /* Set fps */
167     cfg[0].g_timebase.den = framerate;
168
169     memcpy(&cfg[1], &cfg[0], sizeof(cfg[0]));
170     cfg[1].rc_target_bitrate = 500;
171     cfg[1].g_w = width_down;
172     cfg[1].g_h = height_down;
173
174     for (int i = 0; i < 2; i++) {
175       cfg[i].ts_number_layers = 2;
176       cfg[i].ts_periodicity = 2;
177       cfg[i].ts_rate_decimator[0] = 2;
178       cfg[i].ts_rate_decimator[1] = 1;
179       cfg[i].ts_layer_id[0] = 0;
180       cfg[i].ts_layer_id[1] = 1;
181       // Invalid parameters.
182       cfg[i].ts_target_bitrate[0] = 0;
183       cfg[i].ts_target_bitrate[1] = 0;
184     }
185
186     // VP9 should report incapable, VP8 invalid for all configurations.
187     const char kVP9Name[] = "WebM Project VP9";
188     const bool is_vp9 = strncmp(kVP9Name, vpx_codec_iface_name(iface),
189                                 sizeof(kVP9Name) - 1) == 0;
190     EXPECT_EQ(is_vp9 ? VPX_CODEC_INCAPABLE : VPX_CODEC_INVALID_PARAM,
191               vpx_codec_enc_init_multi(&enc[0], iface, &cfg[0], 2, 0, &dsf[0]));
192
193     for (int i = 0; i < 2; i++) {
194       vpx_codec_destroy(&enc[i]);
195     }
196   }
197 }
198
199 }  // namespace