blob: a2c829163739d5d4156c03a3a9318b0615e844af [file] [log] [blame]
Ronald S. Bultje1a89bc02012-10-04 16:38:21 -07001/*
2 * Copyright (c) 2012 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 <cmath>
11#include "third_party/googletest/src/include/gtest/gtest.h"
John Koleszar706cafe2013-01-18 11:51:12 -080012#include "test/codec_factory.h"
Ronald S. Bultje1a89bc02012-10-04 16:38:21 -070013#include "test/encode_test_driver.h"
14#include "test/i420_video_source.h"
John Koleszar706cafe2013-01-18 11:51:12 -080015#include "test/util.h"
16
17namespace {
Ronald S. Bultje1a89bc02012-10-04 16:38:21 -070018
19// CQ level range: [kCQLevelMin, kCQLevelMax).
20const int kCQLevelMin = 4;
21const int kCQLevelMax = 63;
22const int kCQLevelStep = 8;
23const int kCQTargetBitrate = 2000;
24
John Koleszar706cafe2013-01-18 11:51:12 -080025class CQTest : public ::libvpx_test::EncoderTest,
26 public ::libvpx_test::CodecTestWithParam<int> {
Ronald S. Bultje1a89bc02012-10-04 16:38:21 -070027 protected:
John Koleszar706cafe2013-01-18 11:51:12 -080028 CQTest() : EncoderTest(GET_PARAM(0)), cq_level_(GET_PARAM(1)) {
29 init_flags_ = VPX_CODEC_USE_PSNR;
30 }
31
Ronald S. Bultje1a89bc02012-10-04 16:38:21 -070032 virtual ~CQTest() {}
33
34 virtual void SetUp() {
35 InitializeConfig();
36 SetMode(libvpx_test::kTwoPassGood);
37 }
38
39 virtual void BeginPassHook(unsigned int /*pass*/) {
40 file_size_ = 0;
41 psnr_ = 0.0;
42 n_frames_ = 0;
43 }
44
Ronald S. Bultje1a89bc02012-10-04 16:38:21 -070045 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
46 libvpx_test::Encoder *encoder) {
47 if (video->frame() == 1) {
48 if (cfg_.rc_end_usage == VPX_CQ) {
49 encoder->Control(VP8E_SET_CQ_LEVEL, cq_level_);
50 }
51 encoder->Control(VP8E_SET_CPUUSED, 3);
52 }
53 }
54
55 virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
56 psnr_ += pow(10.0, pkt->data.psnr.psnr[0] / 10.0);
57 n_frames_++;
58 }
59
60 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
61 file_size_ += pkt->data.frame.sz;
62 }
63
64 double GetLinearPSNROverBitrate() const {
65 double avg_psnr = log10(psnr_ / n_frames_) * 10.0;
66 return pow(10.0, avg_psnr / 10.0) / file_size_;
67 }
68
69 int file_size() const { return file_size_; }
70 int n_frames() const { return n_frames_; }
71
72 private:
73 int cq_level_;
74 int file_size_;
75 double psnr_;
76 int n_frames_;
77};
78
79int prev_actual_bitrate = kCQTargetBitrate;
80TEST_P(CQTest, LinearPSNRIsHigherForCQLevel) {
81 const vpx_rational timebase = { 33333333, 1000000000 };
82 cfg_.g_timebase = timebase;
83 cfg_.rc_target_bitrate = kCQTargetBitrate;
84 cfg_.g_lag_in_frames = 25;
85
86 cfg_.rc_end_usage = VPX_CQ;
87 libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
88 timebase.den, timebase.num, 0, 30);
89 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
90 const double cq_psnr_lin = GetLinearPSNROverBitrate();
91 const int cq_actual_bitrate = file_size() * 8 * 30 / (n_frames() * 1000);
92 EXPECT_LE(cq_actual_bitrate, kCQTargetBitrate);
93 EXPECT_LE(cq_actual_bitrate, prev_actual_bitrate);
94 prev_actual_bitrate = cq_actual_bitrate;
95
96 // try targeting the approximate same bitrate with VBR mode
97 cfg_.rc_end_usage = VPX_VBR;
98 cfg_.rc_target_bitrate = cq_actual_bitrate;
99 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
100 const double vbr_psnr_lin = GetLinearPSNROverBitrate();
101 EXPECT_GE(cq_psnr_lin, vbr_psnr_lin);
102}
103
John Koleszar706cafe2013-01-18 11:51:12 -0800104VP8_INSTANTIATE_TEST_CASE(CQTest,
105 ::testing::Range(kCQLevelMin, kCQLevelMax,
106 kCQLevelStep));
Ronald S. Bultje1a89bc02012-10-04 16:38:21 -0700107} // namespace