1/*
2 *  Copyright (c) 2013 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 "third_party/googletest/src/include/gtest/gtest.h"
12
13#include "./vpx_config.h"
14#include "test/codec_factory.h"
15#include "test/encode_test_driver.h"
16#include "test/i420_video_source.h"
17#include "test/util.h"
18#include "test/y4m_video_source.h"
19
20namespace {
21
22const int kMaxPsnr = 100;
23
24class LosslessTest
25    : public ::libvpx_test::EncoderTest,
26      public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
27 protected:
28  LosslessTest()
29      : EncoderTest(GET_PARAM(0)), psnr_(kMaxPsnr), nframes_(0),
30        encoding_mode_(GET_PARAM(1)) {}
31
32  virtual ~LosslessTest() {}
33
34  virtual void SetUp() {
35    InitializeConfig();
36    SetMode(encoding_mode_);
37  }
38
39  virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
40                                  ::libvpx_test::Encoder *encoder) {
41    if (video->frame() == 1) {
42      // Only call Control if quantizer > 0 to verify that using quantizer
43      // alone will activate lossless
44      if (cfg_.rc_max_quantizer > 0 || cfg_.rc_min_quantizer > 0) {
45        encoder->Control(VP9E_SET_LOSSLESS, 1);
46      }
47    }
48  }
49
50  virtual void BeginPassHook(unsigned int /*pass*/) {
51    psnr_ = kMaxPsnr;
52    nframes_ = 0;
53  }
54
55  virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
56    if (pkt->data.psnr.psnr[0] < psnr_) psnr_ = pkt->data.psnr.psnr[0];
57  }
58
59  double GetMinPsnr() const { return psnr_; }
60
61 private:
62  double psnr_;
63  unsigned int nframes_;
64  libvpx_test::TestMode encoding_mode_;
65};
66
67TEST_P(LosslessTest, TestLossLessEncoding) {
68  const vpx_rational timebase = { 33333333, 1000000000 };
69  cfg_.g_timebase = timebase;
70  cfg_.rc_target_bitrate = 2000;
71  cfg_.g_lag_in_frames = 25;
72  cfg_.rc_min_quantizer = 0;
73  cfg_.rc_max_quantizer = 0;
74
75  init_flags_ = VPX_CODEC_USE_PSNR;
76
77  // intentionally changed the dimension for better testing coverage
78  libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
79                                     timebase.den, timebase.num, 0, 10);
80  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
81  const double psnr_lossless = GetMinPsnr();
82  EXPECT_GE(psnr_lossless, kMaxPsnr);
83}
84
85TEST_P(LosslessTest, TestLossLessEncoding444) {
86  libvpx_test::Y4mVideoSource video("rush_hour_444.y4m", 0, 10);
87
88  cfg_.g_profile = 1;
89  cfg_.g_timebase = video.timebase();
90  cfg_.rc_target_bitrate = 2000;
91  cfg_.g_lag_in_frames = 25;
92  cfg_.rc_min_quantizer = 0;
93  cfg_.rc_max_quantizer = 0;
94
95  init_flags_ = VPX_CODEC_USE_PSNR;
96
97  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
98  const double psnr_lossless = GetMinPsnr();
99  EXPECT_GE(psnr_lossless, kMaxPsnr);
100}
101
102TEST_P(LosslessTest, TestLossLessEncodingCtrl) {
103  const vpx_rational timebase = { 33333333, 1000000000 };
104  cfg_.g_timebase = timebase;
105  cfg_.rc_target_bitrate = 2000;
106  cfg_.g_lag_in_frames = 25;
107  // Intentionally set Q > 0, to make sure control can be used to activate
108  // lossless
109  cfg_.rc_min_quantizer = 10;
110  cfg_.rc_max_quantizer = 20;
111
112  init_flags_ = VPX_CODEC_USE_PSNR;
113
114  libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
115                                     timebase.den, timebase.num, 0, 10);
116  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
117  const double psnr_lossless = GetMinPsnr();
118  EXPECT_GE(psnr_lossless, kMaxPsnr);
119}
120
121VP9_INSTANTIATE_TEST_CASE(LosslessTest,
122                          ::testing::Values(::libvpx_test::kRealTime,
123                                            ::libvpx_test::kOnePassGood,
124                                            ::libvpx_test::kTwoPassGood));
125}  // namespace
126