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