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 #include "gtest/gtest.h"
11 #include "test/codec_factory.h"
12 #include "test/encode_test_driver.h"
13 #include "test/i420_video_source.h"
14 #include "test/util.h"
15 #include "vpx_config.h"
16
17 namespace {
18 class LevelTest
19 : public ::libvpx_test::EncoderTest,
20 public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
21 protected:
LevelTest()22 LevelTest()
23 : EncoderTest(GET_PARAM(0)), encoding_mode_(GET_PARAM(1)),
24 cpu_used_(GET_PARAM(2)), min_gf_internal_(24), target_level_(0),
25 level_(0) {}
26 ~LevelTest() override = default;
27
SetUp()28 void SetUp() override {
29 InitializeConfig();
30 SetMode(encoding_mode_);
31 if (encoding_mode_ != ::libvpx_test::kRealTime) {
32 cfg_.g_lag_in_frames = 25;
33 cfg_.rc_end_usage = VPX_VBR;
34 } else {
35 cfg_.g_lag_in_frames = 0;
36 cfg_.rc_end_usage = VPX_CBR;
37 }
38 cfg_.rc_2pass_vbr_minsection_pct = 5;
39 cfg_.rc_2pass_vbr_maxsection_pct = 2000;
40 cfg_.rc_target_bitrate = 400;
41 cfg_.rc_max_quantizer = 63;
42 cfg_.rc_min_quantizer = 0;
43 }
44
PreEncodeFrameHook(::libvpx_test::VideoSource * video,::libvpx_test::Encoder * encoder)45 void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
46 ::libvpx_test::Encoder *encoder) override {
47 if (video->frame() == 0) {
48 encoder->Control(VP8E_SET_CPUUSED, cpu_used_);
49 encoder->Control(VP9E_SET_TARGET_LEVEL, target_level_);
50 encoder->Control(VP9E_SET_MIN_GF_INTERVAL, min_gf_internal_);
51 if (encoding_mode_ != ::libvpx_test::kRealTime) {
52 encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
53 encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
54 encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
55 encoder->Control(VP8E_SET_ARNR_TYPE, 3);
56 }
57 }
58 encoder->Control(VP9E_GET_LEVEL, &level_);
59 ASSERT_LE(level_, 51);
60 ASSERT_GE(level_, 0);
61 }
62
63 ::libvpx_test::TestMode encoding_mode_;
64 int cpu_used_;
65 int min_gf_internal_;
66 int target_level_;
67 int level_;
68 };
69
TEST_P(LevelTest,TestTargetLevel11Large)70 TEST_P(LevelTest, TestTargetLevel11Large) {
71 #if CONFIG_REALTIME_ONLY
72 GTEST_SKIP();
73 #else
74 ASSERT_NE(encoding_mode_, ::libvpx_test::kRealTime);
75 ::libvpx_test::I420VideoSource video("hantro_odd.yuv", 208, 144, 30, 1, 0,
76 60);
77 target_level_ = 11;
78 cfg_.rc_target_bitrate = 150;
79 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
80 ASSERT_GE(target_level_, level_);
81 #endif
82 }
83
TEST_P(LevelTest,TestTargetLevel20Large)84 TEST_P(LevelTest, TestTargetLevel20Large) {
85 #if CONFIG_REALTIME_ONLY
86 GTEST_SKIP();
87 #else
88 ASSERT_NE(encoding_mode_, ::libvpx_test::kRealTime);
89 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
90 30, 1, 0, 60);
91 target_level_ = 20;
92 cfg_.rc_target_bitrate = 1200;
93 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
94 ASSERT_GE(target_level_, level_);
95 #endif
96 }
97
TEST_P(LevelTest,TestTargetLevel31Large)98 TEST_P(LevelTest, TestTargetLevel31Large) {
99 #if CONFIG_REALTIME_ONLY
100 GTEST_SKIP();
101 #else
102 ASSERT_NE(encoding_mode_, ::libvpx_test::kRealTime);
103 ::libvpx_test::I420VideoSource video("niklas_1280_720_30.y4m", 1280, 720, 30,
104 1, 0, 60);
105 target_level_ = 31;
106 cfg_.rc_target_bitrate = 8000;
107 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
108 ASSERT_GE(target_level_, level_);
109 #endif
110 }
111
112 // Test for keeping level stats only
TEST_P(LevelTest,TestTargetLevel0)113 TEST_P(LevelTest, TestTargetLevel0) {
114 ::libvpx_test::I420VideoSource video("hantro_odd.yuv", 208, 144, 30, 1, 0,
115 40);
116 target_level_ = 0;
117 min_gf_internal_ = 4;
118 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
119 ASSERT_GE(11, level_);
120
121 cfg_.rc_target_bitrate = 1600;
122 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
123 ASSERT_GE(20, level_);
124 }
125
126 // Test for level control being turned off
TEST_P(LevelTest,TestTargetLevel255)127 TEST_P(LevelTest, TestTargetLevel255) {
128 ::libvpx_test::I420VideoSource video("hantro_odd.yuv", 208, 144, 30, 1, 0,
129 30);
130 target_level_ = 255;
131 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
132 }
133
TEST_P(LevelTest,TestTargetLevelApi)134 TEST_P(LevelTest, TestTargetLevelApi) {
135 ::libvpx_test::I420VideoSource video("hantro_odd.yuv", 208, 144, 30, 1, 0, 1);
136 static vpx_codec_iface_t *codec = &vpx_codec_vp9_cx_algo;
137 vpx_codec_ctx_t enc;
138 vpx_codec_enc_cfg_t cfg;
139 EXPECT_EQ(VPX_CODEC_OK, vpx_codec_enc_config_default(codec, &cfg, 0));
140 cfg.rc_target_bitrate = 100;
141 EXPECT_EQ(VPX_CODEC_OK, vpx_codec_enc_init(&enc, codec, &cfg, 0));
142 for (int level = 0; level <= 256; ++level) {
143 if (level == 10 || level == 11 || level == 20 || level == 21 ||
144 level == 30 || level == 31 || level == 40 || level == 41 ||
145 level == 50 || level == 51 || level == 52 || level == 60 ||
146 level == 61 || level == 62 || level == 0 || level == 1 || level == 255)
147 EXPECT_EQ(VPX_CODEC_OK,
148 vpx_codec_control(&enc, VP9E_SET_TARGET_LEVEL, level));
149 else
150 EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
151 vpx_codec_control(&enc, VP9E_SET_TARGET_LEVEL, level));
152 }
153 EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&enc));
154 }
155
156 VP9_INSTANTIATE_TEST_SUITE(LevelTest, ONE_OR_TWO_PASS_TEST_MODES,
157 ::testing::Range(0, 9));
158 } // namespace
159