xref: /aosp_15_r20/external/libvpx/test/vp9_arf_freq_test.cc (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2015 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 <memory>
12 
13 #include "gtest/gtest.h"
14 
15 #include "test/codec_factory.h"
16 #include "test/encode_test_driver.h"
17 #include "test/util.h"
18 #include "test/y4m_video_source.h"
19 #include "test/yuv_video_source.h"
20 #include "vp9/encoder/vp9_ratectrl.h"
21 #include "vpx_config.h"
22 
23 namespace {
24 
25 const unsigned int kFrames = 100;
26 const int kBitrate = 500;
27 
28 #define ARF_NOT_SEEN 1000001
29 #define ARF_SEEN_ONCE 1000000
30 
31 typedef struct {
32   const char *filename;
33   unsigned int width;
34   unsigned int height;
35   unsigned int framerate_num;
36   unsigned int framerate_den;
37   unsigned int input_bit_depth;
38   vpx_img_fmt fmt;
39   vpx_bit_depth_t bit_depth;
40   unsigned int profile;
41 } TestVideoParam;
42 
43 typedef struct {
44   libvpx_test::TestMode mode;
45   int cpu_used;
46 } TestEncodeParam;
47 
48 const TestVideoParam kTestVectors[] = {
49   // artificially increase framerate to trigger default check
50   { "hantro_collage_w352h288.yuv", 352, 288, 5000, 1, 8, VPX_IMG_FMT_I420,
51     VPX_BITS_8, 0 },
52   { "hantro_collage_w352h288.yuv", 352, 288, 30, 1, 8, VPX_IMG_FMT_I420,
53     VPX_BITS_8, 0 },
54   { "rush_hour_444.y4m", 352, 288, 30, 1, 8, VPX_IMG_FMT_I444, VPX_BITS_8, 1 },
55 #if CONFIG_VP9_HIGHBITDEPTH
56 // Add list of profile 2/3 test videos here ...
57 #endif  // CONFIG_VP9_HIGHBITDEPTH
58 };
59 
60 const TestEncodeParam kEncodeVectors[] = {
61   { ::libvpx_test::kOnePassGood, 2 }, { ::libvpx_test::kOnePassGood, 5 },
62   { ::libvpx_test::kTwoPassGood, 1 }, { ::libvpx_test::kTwoPassGood, 2 },
63   { ::libvpx_test::kTwoPassGood, 5 }, { ::libvpx_test::kRealTime, 5 },
64 };
65 
66 const int kMinArfVectors[] = {
67   // NOTE: 0 refers to the default built-in logic in:
68   //       vp9_rc_get_default_min_gf_interval(...)
69   0, 4, 8, 12, 15
70 };
71 
is_extension_y4m(const char * filename)72 int is_extension_y4m(const char *filename) {
73   const char *dot = strrchr(filename, '.');
74   if (!dot || dot == filename) {
75     return 0;
76   } else {
77     return !strcmp(dot, ".y4m");
78   }
79 }
80 
81 class ArfFreqTest
82     : public ::libvpx_test::EncoderTest,
83       public ::libvpx_test::CodecTestWith3Params<TestVideoParam,
84                                                  TestEncodeParam, int> {
85  protected:
ArfFreqTest()86   ArfFreqTest()
87       : EncoderTest(GET_PARAM(0)), test_video_param_(GET_PARAM(1)),
88         test_encode_param_(GET_PARAM(2)), min_arf_requested_(GET_PARAM(3)) {}
89 
90   ~ArfFreqTest() override = default;
91 
SetUp()92   void SetUp() override {
93     InitializeConfig();
94     SetMode(test_encode_param_.mode);
95     if (test_encode_param_.mode != ::libvpx_test::kRealTime) {
96       cfg_.g_lag_in_frames = 25;
97       cfg_.rc_end_usage = VPX_VBR;
98     } else {
99       cfg_.g_lag_in_frames = 0;
100       cfg_.rc_end_usage = VPX_CBR;
101       cfg_.rc_buf_sz = 1000;
102       cfg_.rc_buf_initial_sz = 500;
103       cfg_.rc_buf_optimal_sz = 600;
104     }
105     dec_cfg_.threads = 4;
106   }
107 
BeginPassHook(unsigned int)108   void BeginPassHook(unsigned int) override {
109     min_run_ = ARF_NOT_SEEN;
110     run_of_visible_frames_ = 0;
111   }
112 
GetNumFramesInPkt(const vpx_codec_cx_pkt_t * pkt)113   int GetNumFramesInPkt(const vpx_codec_cx_pkt_t *pkt) {
114     const uint8_t *buffer = reinterpret_cast<uint8_t *>(pkt->data.frame.buf);
115     const uint8_t marker = buffer[pkt->data.frame.sz - 1];
116     const int mag = ((marker >> 3) & 3) + 1;
117     int frames = (marker & 0x7) + 1;
118     const unsigned int index_sz = 2 + mag * frames;
119     // Check for superframe or not.
120     // Assume superframe has only one visible frame, the rest being
121     // invisible. If superframe index is not found, then there is only
122     // one frame.
123     if (!((marker & 0xe0) == 0xc0 && pkt->data.frame.sz >= index_sz &&
124           buffer[pkt->data.frame.sz - index_sz] == marker)) {
125       frames = 1;
126     }
127     return frames;
128   }
129 
FramePktHook(const vpx_codec_cx_pkt_t * pkt)130   void FramePktHook(const vpx_codec_cx_pkt_t *pkt) override {
131     if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) return;
132     const int frames = GetNumFramesInPkt(pkt);
133     if (frames == 1) {
134       run_of_visible_frames_++;
135     } else if (frames == 2) {
136       if (min_run_ == ARF_NOT_SEEN) {
137         min_run_ = ARF_SEEN_ONCE;
138       } else if (min_run_ == ARF_SEEN_ONCE ||
139                  run_of_visible_frames_ < min_run_) {
140         min_run_ = run_of_visible_frames_;
141       }
142       run_of_visible_frames_ = 1;
143     } else {
144       min_run_ = 0;
145       run_of_visible_frames_ = 1;
146     }
147   }
148 
PreEncodeFrameHook(::libvpx_test::VideoSource * video,::libvpx_test::Encoder * encoder)149   void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
150                           ::libvpx_test::Encoder *encoder) override {
151     if (video->frame() == 0) {
152       encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING, 1);
153       encoder->Control(VP9E_SET_TILE_COLUMNS, 4);
154       encoder->Control(VP8E_SET_CPUUSED, test_encode_param_.cpu_used);
155       encoder->Control(VP9E_SET_MIN_GF_INTERVAL, min_arf_requested_);
156       if (test_encode_param_.mode != ::libvpx_test::kRealTime) {
157         encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
158         encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
159         encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
160         encoder->Control(VP8E_SET_ARNR_TYPE, 3);
161       }
162     }
163   }
164 
GetMinVisibleRun() const165   int GetMinVisibleRun() const { return min_run_; }
166 
GetMinArfDistanceRequested() const167   int GetMinArfDistanceRequested() const {
168     if (min_arf_requested_) {
169       return min_arf_requested_;
170     } else {
171       return vp9_rc_get_default_min_gf_interval(
172           test_video_param_.width, test_video_param_.height,
173           (double)test_video_param_.framerate_num /
174               test_video_param_.framerate_den);
175     }
176   }
177 
178   TestVideoParam test_video_param_;
179   TestEncodeParam test_encode_param_;
180 
181  private:
182   int min_arf_requested_;
183   int min_run_;
184   int run_of_visible_frames_;
185 };
186 
TEST_P(ArfFreqTest,MinArfFreqTest)187 TEST_P(ArfFreqTest, MinArfFreqTest) {
188   cfg_.rc_target_bitrate = kBitrate;
189   cfg_.g_error_resilient = 0;
190   cfg_.g_profile = test_video_param_.profile;
191   cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
192   cfg_.g_bit_depth = test_video_param_.bit_depth;
193   init_flags_ = VPX_CODEC_USE_PSNR;
194   if (cfg_.g_bit_depth > 8) init_flags_ |= VPX_CODEC_USE_HIGHBITDEPTH;
195 
196   std::unique_ptr<libvpx_test::VideoSource> video;
197   if (is_extension_y4m(test_video_param_.filename)) {
198     video.reset(new libvpx_test::Y4mVideoSource(test_video_param_.filename, 0,
199                                                 kFrames));
200   } else {
201     video.reset(new libvpx_test::YUVVideoSource(
202         test_video_param_.filename, test_video_param_.fmt,
203         test_video_param_.width, test_video_param_.height,
204         test_video_param_.framerate_num, test_video_param_.framerate_den, 0,
205         kFrames));
206   }
207 
208   ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
209   const int min_run = GetMinVisibleRun();
210   const int min_arf_dist_requested = GetMinArfDistanceRequested();
211   if (min_run != ARF_NOT_SEEN && min_run != ARF_SEEN_ONCE) {
212     const int min_arf_dist = min_run + 1;
213     EXPECT_GE(min_arf_dist, min_arf_dist_requested);
214   }
215 }
216 
217 VP9_INSTANTIATE_TEST_SUITE(ArfFreqTest, ::testing::ValuesIn(kTestVectors),
218                            ::testing::ValuesIn(kEncodeVectors),
219                            ::testing::ValuesIn(kMinArfVectors));
220 }  // namespace
221