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 "third_party/googletest/src/include/gtest/gtest.h"
12 
13 #include "test/codec_factory.h"
14 #include "test/encode_test_driver.h"
15 #include "test/util.h"
16 #include "test/y4m_video_source.h"
17 #include "vp9/vp9_dx_iface.h"
18 
19 namespace {
20 
21 const int kCpuUsed = 2;
22 
23 struct EncodePerfTestVideo {
24   const char *name;
25   uint32_t width;
26   uint32_t height;
27   uint32_t bitrate;
28   int frames;
29 };
30 
31 const EncodePerfTestVideo kVP9EncodePerfTestVectors[] = {
32   { "niklas_1280_720_30.y4m", 1280, 720, 600, 10 },
33 };
34 
35 struct EncodeParameters {
36   int32_t tile_rows;
37   int32_t tile_cols;
38   int32_t lossless;
39   int32_t error_resilient;
40   int32_t frame_parallel;
41   vpx_color_range_t color_range;
42   vpx_color_space_t cs;
43   int render_size[2];
44   // TODO(JBB): quantizers / bitrate
45 };
46 
47 const EncodeParameters kVP9EncodeParameterSet[] = {
48   { 0, 0, 0, 1, 0, VPX_CR_STUDIO_RANGE, VPX_CS_BT_601, { 0, 0 } },
49   { 0, 0, 0, 0, 0, VPX_CR_FULL_RANGE, VPX_CS_BT_709, { 0, 0 } },
50   { 0, 0, 1, 0, 0, VPX_CR_FULL_RANGE, VPX_CS_BT_2020, { 0, 0 } },
51   { 0, 2, 0, 0, 1, VPX_CR_STUDIO_RANGE, VPX_CS_UNKNOWN, { 640, 480 } },
52   // TODO(JBB): Test profiles (requires more work).
53 };
54 
55 class VpxEncoderParmsGetToDecoder
56     : public ::libvpx_test::EncoderTest,
57       public ::libvpx_test::CodecTestWith2Params<EncodeParameters,
58                                                  EncodePerfTestVideo> {
59  protected:
VpxEncoderParmsGetToDecoder()60   VpxEncoderParmsGetToDecoder()
61       : EncoderTest(GET_PARAM(0)), encode_parms(GET_PARAM(1)) {}
62 
~VpxEncoderParmsGetToDecoder()63   virtual ~VpxEncoderParmsGetToDecoder() {}
64 
SetUp()65   virtual void SetUp() {
66     InitializeConfig();
67     SetMode(::libvpx_test::kTwoPassGood);
68     cfg_.g_lag_in_frames = 25;
69     cfg_.g_error_resilient = encode_parms.error_resilient;
70     dec_cfg_.threads = 4;
71     test_video_ = GET_PARAM(2);
72     cfg_.rc_target_bitrate = test_video_.bitrate;
73   }
74 
PreEncodeFrameHook(::libvpx_test::VideoSource * video,::libvpx_test::Encoder * encoder)75   virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
76                                   ::libvpx_test::Encoder *encoder) {
77     if (video->frame() == 1) {
78       encoder->Control(VP9E_SET_COLOR_SPACE, encode_parms.cs);
79       encoder->Control(VP9E_SET_COLOR_RANGE, encode_parms.color_range);
80       encoder->Control(VP9E_SET_LOSSLESS, encode_parms.lossless);
81       encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING,
82                        encode_parms.frame_parallel);
83       encoder->Control(VP9E_SET_TILE_ROWS, encode_parms.tile_rows);
84       encoder->Control(VP9E_SET_TILE_COLUMNS, encode_parms.tile_cols);
85       encoder->Control(VP8E_SET_CPUUSED, kCpuUsed);
86       encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
87       encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
88       encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
89       encoder->Control(VP8E_SET_ARNR_TYPE, 3);
90       if (encode_parms.render_size[0] > 0 && encode_parms.render_size[1] > 0) {
91         encoder->Control(VP9E_SET_RENDER_SIZE, encode_parms.render_size);
92       }
93     }
94   }
95 
HandleDecodeResult(const vpx_codec_err_t res_dec,const libvpx_test::VideoSource &,libvpx_test::Decoder * decoder)96   virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
97                                   const libvpx_test::VideoSource & /*video*/,
98                                   libvpx_test::Decoder *decoder) {
99     vpx_codec_ctx_t *const vp9_decoder = decoder->GetDecoder();
100     vpx_codec_alg_priv_t *const priv =
101         reinterpret_cast<vpx_codec_alg_priv_t *>(vp9_decoder->priv);
102     FrameWorkerData *const worker_data =
103         reinterpret_cast<FrameWorkerData *>(priv->frame_workers[0].data1);
104     VP9_COMMON *const common = &worker_data->pbi->common;
105 
106     if (encode_parms.lossless) {
107       EXPECT_EQ(0, common->base_qindex);
108       EXPECT_EQ(0, common->y_dc_delta_q);
109       EXPECT_EQ(0, common->uv_dc_delta_q);
110       EXPECT_EQ(0, common->uv_ac_delta_q);
111       EXPECT_EQ(ONLY_4X4, common->tx_mode);
112     }
113     EXPECT_EQ(encode_parms.error_resilient, common->error_resilient_mode);
114     if (encode_parms.error_resilient) {
115       EXPECT_EQ(1, common->frame_parallel_decoding_mode);
116       EXPECT_EQ(0, common->use_prev_frame_mvs);
117     } else {
118       EXPECT_EQ(encode_parms.frame_parallel,
119                 common->frame_parallel_decoding_mode);
120     }
121     EXPECT_EQ(encode_parms.color_range, common->color_range);
122     EXPECT_EQ(encode_parms.cs, common->color_space);
123     if (encode_parms.render_size[0] > 0 && encode_parms.render_size[1] > 0) {
124       EXPECT_EQ(encode_parms.render_size[0], common->render_width);
125       EXPECT_EQ(encode_parms.render_size[1], common->render_height);
126     }
127     EXPECT_EQ(encode_parms.tile_cols, common->log2_tile_cols);
128     EXPECT_EQ(encode_parms.tile_rows, common->log2_tile_rows);
129 
130     EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
131     return VPX_CODEC_OK == res_dec;
132   }
133 
134   EncodePerfTestVideo test_video_;
135 
136  private:
137   EncodeParameters encode_parms;
138 };
139 
TEST_P(VpxEncoderParmsGetToDecoder,BitstreamParms)140 TEST_P(VpxEncoderParmsGetToDecoder, BitstreamParms) {
141   init_flags_ = VPX_CODEC_USE_PSNR;
142 
143   testing::internal::scoped_ptr<libvpx_test::VideoSource> video(
144       new libvpx_test::Y4mVideoSource(test_video_.name, 0, test_video_.frames));
145   ASSERT_TRUE(video.get() != NULL);
146 
147   ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
148 }
149 
150 VP9_INSTANTIATE_TEST_CASE(VpxEncoderParmsGetToDecoder,
151                           ::testing::ValuesIn(kVP9EncodeParameterSet),
152                           ::testing::ValuesIn(kVP9EncodePerfTestVectors));
153 }  // namespace
154