vp9[loongarch]: Optimize fdct4x4/8x8_lsx
[platform/upstream/libvpx.git] / 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 <climits>
12 #include <cstring>
13 #include <initializer_list>
14
15 #include "third_party/googletest/src/include/gtest/gtest.h"
16
17 #include "./vpx_config.h"
18 #include "test/video_source.h"
19 #include "vpx/vp8cx.h"
20 #include "vpx/vpx_encoder.h"
21
22 namespace {
23
24 const vpx_codec_iface_t *kCodecIfaces[] = {
25 #if CONFIG_VP8_ENCODER
26   &vpx_codec_vp8_cx_algo,
27 #endif
28 #if CONFIG_VP9_ENCODER
29   &vpx_codec_vp9_cx_algo,
30 #endif
31 };
32
33 bool IsVP9(const vpx_codec_iface_t *iface) {
34   static const char kVP9Name[] = "WebM Project VP9";
35   return strncmp(kVP9Name, vpx_codec_iface_name(iface), sizeof(kVP9Name) - 1) ==
36          0;
37 }
38
39 TEST(EncodeAPI, InvalidParams) {
40   uint8_t buf[1] = { 0 };
41   vpx_image_t img;
42   vpx_codec_ctx_t enc;
43   vpx_codec_enc_cfg_t cfg;
44
45   EXPECT_EQ(&img, vpx_img_wrap(&img, VPX_IMG_FMT_I420, 1, 1, 1, buf));
46
47   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
48             vpx_codec_enc_init(nullptr, nullptr, nullptr, 0));
49   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
50             vpx_codec_enc_init(&enc, nullptr, nullptr, 0));
51   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
52             vpx_codec_encode(nullptr, nullptr, 0, 0, 0, 0));
53   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
54             vpx_codec_encode(nullptr, &img, 0, 0, 0, 0));
55   EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_destroy(nullptr));
56   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
57             vpx_codec_enc_config_default(nullptr, nullptr, 0));
58   EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
59             vpx_codec_enc_config_default(nullptr, &cfg, 0));
60   EXPECT_NE(vpx_codec_error(nullptr), nullptr);
61
62   for (const auto *iface : kCodecIfaces) {
63     SCOPED_TRACE(vpx_codec_iface_name(iface));
64     EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
65               vpx_codec_enc_init(nullptr, iface, nullptr, 0));
66     EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
67               vpx_codec_enc_init(&enc, iface, nullptr, 0));
68     EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
69               vpx_codec_enc_config_default(iface, &cfg, 1));
70
71     EXPECT_EQ(VPX_CODEC_OK, vpx_codec_enc_config_default(iface, &cfg, 0));
72     EXPECT_EQ(VPX_CODEC_OK, vpx_codec_enc_init(&enc, iface, &cfg, 0));
73     EXPECT_EQ(VPX_CODEC_OK, vpx_codec_encode(&enc, nullptr, 0, 0, 0, 0));
74
75     EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&enc));
76   }
77 }
78
79 TEST(EncodeAPI, HighBitDepthCapability) {
80 // VP8 should not claim VP9 HBD as a capability.
81 #if CONFIG_VP8_ENCODER
82   const vpx_codec_caps_t vp8_caps = vpx_codec_get_caps(&vpx_codec_vp8_cx_algo);
83   EXPECT_EQ(vp8_caps & VPX_CODEC_CAP_HIGHBITDEPTH, 0);
84 #endif
85
86 #if CONFIG_VP9_ENCODER
87   const vpx_codec_caps_t vp9_caps = vpx_codec_get_caps(&vpx_codec_vp9_cx_algo);
88 #if CONFIG_VP9_HIGHBITDEPTH
89   EXPECT_EQ(vp9_caps & VPX_CODEC_CAP_HIGHBITDEPTH, VPX_CODEC_CAP_HIGHBITDEPTH);
90 #else
91   EXPECT_EQ(vp9_caps & VPX_CODEC_CAP_HIGHBITDEPTH, 0);
92 #endif
93 #endif
94 }
95
96 #if CONFIG_VP8_ENCODER
97 TEST(EncodeAPI, ImageSizeSetting) {
98   const int width = 711;
99   const int height = 360;
100   const int bps = 12;
101   vpx_image_t img;
102   vpx_codec_ctx_t enc;
103   vpx_codec_enc_cfg_t cfg;
104   uint8_t *img_buf = reinterpret_cast<uint8_t *>(
105       calloc(width * height * bps / 8, sizeof(*img_buf)));
106   vpx_codec_enc_config_default(vpx_codec_vp8_cx(), &cfg, 0);
107
108   cfg.g_w = width;
109   cfg.g_h = height;
110
111   vpx_img_wrap(&img, VPX_IMG_FMT_I420, width, height, 1, img_buf);
112
113   vpx_codec_enc_init(&enc, vpx_codec_vp8_cx(), &cfg, 0);
114
115   EXPECT_EQ(VPX_CODEC_OK, vpx_codec_encode(&enc, &img, 0, 1, 0, 0));
116
117   free(img_buf);
118
119   vpx_codec_destroy(&enc);
120 }
121 #endif
122
123 // Set up 2 spatial streams with 2 temporal layers per stream, and generate
124 // invalid configuration by setting the temporal layer rate allocation
125 // (ts_target_bitrate[]) to 0 for both layers. This should fail independent of
126 // CONFIG_MULTI_RES_ENCODING.
127 TEST(EncodeAPI, MultiResEncode) {
128   const int width = 1280;
129   const int height = 720;
130   const int width_down = width / 2;
131   const int height_down = height / 2;
132   const int target_bitrate = 1000;
133   const int framerate = 30;
134
135   for (const auto *iface : kCodecIfaces) {
136     vpx_codec_ctx_t enc[2];
137     vpx_codec_enc_cfg_t cfg[2];
138     vpx_rational_t dsf[2] = { { 2, 1 }, { 2, 1 } };
139
140     memset(enc, 0, sizeof(enc));
141
142     for (int i = 0; i < 2; i++) {
143       vpx_codec_enc_config_default(iface, &cfg[i], 0);
144     }
145
146     /* Highest-resolution encoder settings */
147     cfg[0].g_w = width;
148     cfg[0].g_h = height;
149     cfg[0].rc_dropframe_thresh = 0;
150     cfg[0].rc_end_usage = VPX_CBR;
151     cfg[0].rc_resize_allowed = 0;
152     cfg[0].rc_min_quantizer = 2;
153     cfg[0].rc_max_quantizer = 56;
154     cfg[0].rc_undershoot_pct = 100;
155     cfg[0].rc_overshoot_pct = 15;
156     cfg[0].rc_buf_initial_sz = 500;
157     cfg[0].rc_buf_optimal_sz = 600;
158     cfg[0].rc_buf_sz = 1000;
159     cfg[0].g_error_resilient = 1; /* Enable error resilient mode */
160     cfg[0].g_lag_in_frames = 0;
161
162     cfg[0].kf_mode = VPX_KF_AUTO;
163     cfg[0].kf_min_dist = 3000;
164     cfg[0].kf_max_dist = 3000;
165
166     cfg[0].rc_target_bitrate = target_bitrate; /* Set target bitrate */
167     cfg[0].g_timebase.num = 1;                 /* Set fps */
168     cfg[0].g_timebase.den = framerate;
169
170     memcpy(&cfg[1], &cfg[0], sizeof(cfg[0]));
171     cfg[1].rc_target_bitrate = 500;
172     cfg[1].g_w = width_down;
173     cfg[1].g_h = height_down;
174
175     for (int i = 0; i < 2; i++) {
176       cfg[i].ts_number_layers = 2;
177       cfg[i].ts_periodicity = 2;
178       cfg[i].ts_rate_decimator[0] = 2;
179       cfg[i].ts_rate_decimator[1] = 1;
180       cfg[i].ts_layer_id[0] = 0;
181       cfg[i].ts_layer_id[1] = 1;
182       // Invalid parameters.
183       cfg[i].ts_target_bitrate[0] = 0;
184       cfg[i].ts_target_bitrate[1] = 0;
185     }
186
187     // VP9 should report incapable, VP8 invalid for all configurations.
188     EXPECT_EQ(IsVP9(iface) ? VPX_CODEC_INCAPABLE : VPX_CODEC_INVALID_PARAM,
189               vpx_codec_enc_init_multi(&enc[0], iface, &cfg[0], 2, 0, &dsf[0]));
190
191     for (int i = 0; i < 2; i++) {
192       vpx_codec_destroy(&enc[i]);
193     }
194   }
195 }
196
197 TEST(EncodeAPI, SetRoi) {
198   static struct {
199     const vpx_codec_iface_t *iface;
200     int ctrl_id;
201   } kCodecs[] = {
202 #if CONFIG_VP8_ENCODER
203     { &vpx_codec_vp8_cx_algo, VP8E_SET_ROI_MAP },
204 #endif
205 #if CONFIG_VP9_ENCODER
206     { &vpx_codec_vp9_cx_algo, VP9E_SET_ROI_MAP },
207 #endif
208   };
209   constexpr int kWidth = 64;
210   constexpr int kHeight = 64;
211
212   for (const auto &codec : kCodecs) {
213     SCOPED_TRACE(vpx_codec_iface_name(codec.iface));
214     vpx_codec_ctx_t enc;
215     vpx_codec_enc_cfg_t cfg;
216
217     EXPECT_EQ(vpx_codec_enc_config_default(codec.iface, &cfg, 0), VPX_CODEC_OK);
218     cfg.g_w = kWidth;
219     cfg.g_h = kHeight;
220     EXPECT_EQ(vpx_codec_enc_init(&enc, codec.iface, &cfg, 0), VPX_CODEC_OK);
221
222     vpx_roi_map_t roi = {};
223     uint8_t roi_map[kWidth * kHeight] = {};
224     if (IsVP9(codec.iface)) {
225       roi.rows = (cfg.g_w + 7) >> 3;
226       roi.cols = (cfg.g_h + 7) >> 3;
227     } else {
228       roi.rows = (cfg.g_w + 15) >> 4;
229       roi.cols = (cfg.g_h + 15) >> 4;
230     }
231     EXPECT_EQ(vpx_codec_control_(&enc, codec.ctrl_id, &roi), VPX_CODEC_OK);
232
233     roi.roi_map = roi_map;
234     // VP8 only. This value isn't range checked.
235     roi.static_threshold[1] = 1000;
236     roi.static_threshold[2] = INT_MIN;
237     roi.static_threshold[3] = INT_MAX;
238
239     for (const auto delta : { -63, -1, 0, 1, 63 }) {
240       for (int i = 0; i < 8; ++i) {
241         roi.delta_q[i] = delta;
242         roi.delta_lf[i] = delta;
243         // VP9 only.
244         roi.skip[i] ^= 1;
245         roi.ref_frame[i] = (roi.ref_frame[i] + 1) % 4;
246         EXPECT_EQ(vpx_codec_control_(&enc, codec.ctrl_id, &roi), VPX_CODEC_OK);
247       }
248     }
249
250     vpx_codec_err_t expected_error;
251     for (const auto delta : { -64, 64, INT_MIN, INT_MAX }) {
252       expected_error = VPX_CODEC_INVALID_PARAM;
253       for (int i = 0; i < 8; ++i) {
254         roi.delta_q[i] = delta;
255         // The max segment count for VP8 is 4, the remainder of the entries are
256         // ignored.
257         if (i >= 4 && !IsVP9(codec.iface)) expected_error = VPX_CODEC_OK;
258
259         EXPECT_EQ(vpx_codec_control_(&enc, codec.ctrl_id, &roi), expected_error)
260             << "delta_q[" << i << "]: " << delta;
261         roi.delta_q[i] = 0;
262
263         roi.delta_lf[i] = delta;
264         EXPECT_EQ(vpx_codec_control_(&enc, codec.ctrl_id, &roi), expected_error)
265             << "delta_lf[" << i << "]: " << delta;
266         roi.delta_lf[i] = 0;
267       }
268     }
269
270     // VP8 should ignore skip[] and ref_frame[] values.
271     expected_error =
272         IsVP9(codec.iface) ? VPX_CODEC_INVALID_PARAM : VPX_CODEC_OK;
273     for (const auto skip : { -2, 2, INT_MIN, INT_MAX }) {
274       for (int i = 0; i < 8; ++i) {
275         roi.skip[i] = skip;
276         EXPECT_EQ(vpx_codec_control_(&enc, codec.ctrl_id, &roi), expected_error)
277             << "skip[" << i << "]: " << skip;
278         roi.skip[i] = 0;
279       }
280     }
281
282     // VP9 allows negative values to be used to disable segmentation.
283     for (int ref_frame = -3; ref_frame < 0; ++ref_frame) {
284       for (int i = 0; i < 8; ++i) {
285         roi.ref_frame[i] = ref_frame;
286         EXPECT_EQ(vpx_codec_control_(&enc, codec.ctrl_id, &roi), VPX_CODEC_OK)
287             << "ref_frame[" << i << "]: " << ref_frame;
288         roi.ref_frame[i] = 0;
289       }
290     }
291
292     for (const auto ref_frame : { 4, INT_MIN, INT_MAX }) {
293       for (int i = 0; i < 8; ++i) {
294         roi.ref_frame[i] = ref_frame;
295         EXPECT_EQ(vpx_codec_control_(&enc, codec.ctrl_id, &roi), expected_error)
296             << "ref_frame[" << i << "]: " << ref_frame;
297         roi.ref_frame[i] = 0;
298       }
299     }
300
301     EXPECT_EQ(vpx_codec_destroy(&enc), VPX_CODEC_OK);
302   }
303 }
304
305 void InitCodec(const vpx_codec_iface_t &iface, int width, int height,
306                vpx_codec_ctx_t *enc, vpx_codec_enc_cfg_t *cfg) {
307   ASSERT_EQ(vpx_codec_enc_config_default(&iface, cfg, 0), VPX_CODEC_OK);
308   cfg->g_w = width;
309   cfg->g_h = height;
310   cfg->g_lag_in_frames = 0;
311   cfg->g_pass = VPX_RC_ONE_PASS;
312   ASSERT_EQ(vpx_codec_enc_init(enc, &iface, cfg, 0), VPX_CODEC_OK);
313
314   ASSERT_EQ(vpx_codec_control_(enc, VP8E_SET_CPUUSED, 2), VPX_CODEC_OK);
315 }
316
317 // Encodes 1 frame of size |cfg.g_w| x |cfg.g_h| setting |enc|'s configuration
318 // to |cfg|.
319 void EncodeWithConfig(const vpx_codec_enc_cfg_t &cfg, vpx_codec_ctx_t *enc) {
320   libvpx_test::DummyVideoSource video;
321   video.SetSize(cfg.g_w, cfg.g_h);
322   video.Begin();
323   EXPECT_EQ(vpx_codec_enc_config_set(enc, &cfg), VPX_CODEC_OK)
324       << vpx_codec_error_detail(enc);
325
326   EXPECT_EQ(vpx_codec_encode(enc, video.img(), video.pts(), video.duration(),
327                              /*flags=*/0, VPX_DL_GOOD_QUALITY),
328             VPX_CODEC_OK)
329       << vpx_codec_error_detail(enc);
330 }
331
332 TEST(EncodeAPI, ConfigChangeThreadCount) {
333   constexpr int kWidth = 1920;
334   constexpr int kHeight = 1080;
335
336   for (const auto *iface : kCodecIfaces) {
337     SCOPED_TRACE(vpx_codec_iface_name(iface));
338     for (int i = 0; i < (IsVP9(iface) ? 2 : 1); ++i) {
339       vpx_codec_enc_cfg_t cfg;
340       struct Encoder {
341         ~Encoder() { EXPECT_EQ(vpx_codec_destroy(&ctx), VPX_CODEC_OK); }
342         vpx_codec_ctx_t ctx = {};
343       } enc;
344
345       EXPECT_NO_FATAL_FAILURE(
346           InitCodec(*iface, kWidth, kHeight, &enc.ctx, &cfg));
347       if (IsVP9(iface)) {
348         EXPECT_EQ(vpx_codec_control_(&enc.ctx, VP9E_SET_TILE_COLUMNS, 6),
349                   VPX_CODEC_OK);
350         EXPECT_EQ(vpx_codec_control_(&enc.ctx, VP9E_SET_ROW_MT, i),
351                   VPX_CODEC_OK);
352       }
353
354       for (const auto threads : { 1, 4, 8, 6, 2, 1 }) {
355         cfg.g_threads = threads;
356         EXPECT_NO_FATAL_FAILURE(EncodeWithConfig(cfg, &enc.ctx))
357             << "iteration: " << i << " threads: " << threads;
358       }
359     }
360   }
361 }
362
363 }  // namespace