1/*
2 *  Copyright (c) 2014 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 <string>
12#include <vector>
13#include "third_party/googletest/src/include/gtest/gtest.h"
14#include "test/codec_factory.h"
15#include "test/encode_test_driver.h"
16#include "test/md5_helper.h"
17#include "test/util.h"
18#include "test/y4m_video_source.h"
19
20namespace {
21class VPxEncoderThreadTest
22    : public ::libvpx_test::EncoderTest,
23      public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
24 protected:
25  VPxEncoderThreadTest()
26      : EncoderTest(GET_PARAM(0)),
27        encoder_initialized_(false),
28        tiles_(2),
29        encoding_mode_(GET_PARAM(1)),
30        set_cpu_used_(GET_PARAM(2)) {
31    init_flags_ = VPX_CODEC_USE_PSNR;
32    vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
33    cfg.w = 1280;
34    cfg.h = 720;
35    decoder_ = codec_->CreateDecoder(cfg, 0);
36
37    md5_.clear();
38  }
39  virtual ~VPxEncoderThreadTest() {
40    delete decoder_;
41  }
42
43  virtual void SetUp() {
44    InitializeConfig();
45    SetMode(encoding_mode_);
46
47    if (encoding_mode_ != ::libvpx_test::kRealTime) {
48      cfg_.g_lag_in_frames = 3;
49      cfg_.rc_end_usage = VPX_VBR;
50      cfg_.rc_2pass_vbr_minsection_pct = 5;
51      cfg_.rc_2pass_vbr_minsection_pct = 2000;
52    } else {
53      cfg_.g_lag_in_frames = 0;
54      cfg_.rc_end_usage = VPX_CBR;
55      cfg_.g_error_resilient = 1;
56    }
57    cfg_.rc_max_quantizer = 56;
58    cfg_.rc_min_quantizer = 0;
59  }
60
61  virtual void BeginPassHook(unsigned int /*pass*/) {
62    encoder_initialized_ = false;
63  }
64
65  virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
66                                  ::libvpx_test::Encoder *encoder) {
67    if (!encoder_initialized_) {
68      // Encode 4 column tiles.
69      encoder->Control(VP9E_SET_TILE_COLUMNS, tiles_);
70      encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
71      if (encoding_mode_ != ::libvpx_test::kRealTime) {
72        encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
73        encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
74        encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
75        encoder->Control(VP8E_SET_ARNR_TYPE, 3);
76      } else {
77        encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 0);
78        encoder->Control(VP9E_SET_AQ_MODE, 3);
79      }
80      encoder_initialized_ = true;
81    }
82  }
83
84  virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
85    const vpx_codec_err_t res = decoder_->DecodeFrame(
86        reinterpret_cast<uint8_t*>(pkt->data.frame.buf), pkt->data.frame.sz);
87    if (res != VPX_CODEC_OK) {
88      abort_ = true;
89      ASSERT_EQ(VPX_CODEC_OK, res);
90    }
91    const vpx_image_t *img = decoder_->GetDxData().Next();
92
93    if (img) {
94      ::libvpx_test::MD5 md5_res;
95      md5_res.Add(img);
96      md5_.push_back(md5_res.Get());
97    }
98  }
99
100  bool encoder_initialized_;
101  int tiles_;
102  ::libvpx_test::TestMode encoding_mode_;
103  int set_cpu_used_;
104  ::libvpx_test::Decoder *decoder_;
105  std::vector<std::string> md5_;
106};
107
108TEST_P(VPxEncoderThreadTest, EncoderResultTest) {
109  std::vector<std::string> single_thr_md5, multi_thr_md5;
110
111  ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 15, 20);
112
113  cfg_.rc_target_bitrate = 1000;
114
115  // Encode using single thread.
116  cfg_.g_threads = 1;
117  init_flags_ = VPX_CODEC_USE_PSNR;
118  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
119  single_thr_md5 = md5_;
120  md5_.clear();
121
122  // Encode using multiple threads.
123  cfg_.g_threads = 4;
124  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
125  multi_thr_md5 = md5_;
126  md5_.clear();
127
128  // Compare to check if two vectors are equal.
129  ASSERT_EQ(single_thr_md5, multi_thr_md5);
130}
131
132VP9_INSTANTIATE_TEST_CASE(
133    VPxEncoderThreadTest,
134    ::testing::Values(::libvpx_test::kTwoPassGood, ::libvpx_test::kOnePassGood,
135                      ::libvpx_test::kRealTime),
136    ::testing::Range(1, 9));
137
138VP10_INSTANTIATE_TEST_CASE(
139    VPxEncoderThreadTest,
140    ::testing::Values(::libvpx_test::kTwoPassGood, ::libvpx_test::kOnePassGood),
141    ::testing::Range(1, 3));
142}  // namespace
143