Merge "Ensure the error-concealment code is available"
[platform/upstream/libvpx.git] / test / vp9_encoder_parms_get_to_decoder.cc
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 "test/codec_factory.h"
12 #include "test/encode_test_driver.h"
13 #include "test/y4m_video_source.h"
14 #include "test/yuv_video_source.h"
15 #include "test/util.h"
16 #include "third_party/googletest/src/include/gtest/gtest.h"
17 #include "vp9/decoder/vp9_decoder.h"
18
19 typedef vpx_codec_stream_info_t vp9_stream_info_t;
20 struct vpx_codec_alg_priv {
21   vpx_codec_priv_t        base;
22   vpx_codec_dec_cfg_t     cfg;
23   vp9_stream_info_t       si;
24   struct VP9Decoder      *pbi;
25   int                     postproc_cfg_set;
26   vp8_postproc_cfg_t      postproc_cfg;
27   vpx_decrypt_cb          decrypt_cb;
28   void                   *decrypt_state;
29   vpx_image_t             img;
30   int                     img_avail;
31   int                     flushed;
32   int                     invert_tile_order;
33   int                     frame_parallel_decode;
34
35   // External frame buffer info to save for VP9 common.
36   void *ext_priv;  // Private data associated with the external frame buffers.
37   vpx_get_frame_buffer_cb_fn_t get_ext_fb_cb;
38   vpx_release_frame_buffer_cb_fn_t release_ext_fb_cb;
39 };
40
41 static vpx_codec_alg_priv_t *get_alg_priv(vpx_codec_ctx_t *ctx) {
42   return (vpx_codec_alg_priv_t *)ctx->priv;
43 }
44
45 namespace {
46
47 const unsigned int kFramerate = 50;
48 const int kCpuUsed = 2;
49
50 struct EncodePerfTestVideo {
51   const char *name;
52   uint32_t width;
53   uint32_t height;
54   uint32_t bitrate;
55   int frames;
56 };
57
58 const EncodePerfTestVideo kVP9EncodePerfTestVectors[] = {
59     {"niklas_1280_720_30.yuv", 1280, 720, 600, 10},
60 };
61
62 struct EncodeParameters {
63   int32_t tile_rows;
64   int32_t tile_cols;
65   int32_t lossless;
66   int32_t error_resilient;
67   int32_t frame_parallel;
68   vpx_color_space_t cs;
69   // TODO(JBB): quantizers / bitrate
70 };
71
72 const EncodeParameters kVP9EncodeParameterSet[] = {
73     {0, 0, 0, 1, 0, VPX_CS_BT_601},
74     {0, 0, 0, 0, 0, VPX_CS_BT_709},
75     {0, 0, 1, 0, 0, VPX_CS_BT_2020},
76     {0, 2, 0, 0, 1, VPX_CS_UNKNOWN},
77     // TODO(JBB): Test profiles (requires more work).
78 };
79
80 int is_extension_y4m(const char *filename) {
81   const char *dot = strrchr(filename, '.');
82   if (!dot || dot == filename)
83     return 0;
84   else
85     return !strcmp(dot, ".y4m");
86 }
87
88 class Vp9EncoderParmsGetToDecoder
89     : public ::libvpx_test::EncoderTest,
90       public ::libvpx_test::CodecTestWith2Params<EncodeParameters, \
91                                                  EncodePerfTestVideo> {
92  protected:
93   Vp9EncoderParmsGetToDecoder()
94       : EncoderTest(GET_PARAM(0)),
95         encode_parms(GET_PARAM(1)) {
96   }
97
98   virtual ~Vp9EncoderParmsGetToDecoder() {}
99
100   virtual void SetUp() {
101     InitializeConfig();
102     SetMode(::libvpx_test::kTwoPassGood);
103     cfg_.g_lag_in_frames = 25;
104     cfg_.g_error_resilient = encode_parms.error_resilient;
105     dec_cfg_.threads = 4;
106     test_video_ = GET_PARAM(2);
107     cfg_.rc_target_bitrate = test_video_.bitrate;
108   }
109
110   virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
111                                   ::libvpx_test::Encoder *encoder) {
112     if (video->frame() == 1) {
113       encoder->Control(VP9E_SET_COLOR_SPACE, encode_parms.cs);
114       encoder->Control(VP9E_SET_LOSSLESS, encode_parms.lossless);
115       encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING,
116                        encode_parms.frame_parallel);
117       encoder->Control(VP9E_SET_TILE_ROWS, encode_parms.tile_rows);
118       encoder->Control(VP9E_SET_TILE_COLUMNS, encode_parms.tile_cols);
119       encoder->Control(VP8E_SET_CPUUSED, kCpuUsed);
120       encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
121       encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
122       encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
123       encoder->Control(VP8E_SET_ARNR_TYPE, 3);
124     }
125   }
126
127   virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
128                                   const libvpx_test::VideoSource& video,
129                                   libvpx_test::Decoder *decoder) {
130     vpx_codec_ctx_t* vp9_decoder = decoder->GetDecoder();
131     vpx_codec_alg_priv_t* priv =
132         (vpx_codec_alg_priv_t*) get_alg_priv(vp9_decoder);
133
134     VP9Decoder* pbi = priv->pbi;
135     VP9_COMMON* common = &pbi->common;
136
137     if (encode_parms.lossless) {
138       EXPECT_EQ(common->base_qindex, 0);
139       EXPECT_EQ(common->y_dc_delta_q, 0);
140       EXPECT_EQ(common->uv_dc_delta_q, 0);
141       EXPECT_EQ(common->uv_ac_delta_q, 0);
142       EXPECT_EQ(common->tx_mode, ONLY_4X4);
143     }
144     EXPECT_EQ(common->error_resilient_mode, encode_parms.error_resilient);
145     if (encode_parms.error_resilient) {
146       EXPECT_EQ(common->frame_parallel_decoding_mode, 1);
147       EXPECT_EQ(common->use_prev_frame_mvs, 0);
148     } else {
149       EXPECT_EQ(common->frame_parallel_decoding_mode,
150                 encode_parms.frame_parallel);
151     }
152     EXPECT_EQ(common->color_space, encode_parms.cs);
153     EXPECT_EQ(common->log2_tile_cols, encode_parms.tile_cols);
154     EXPECT_EQ(common->log2_tile_rows, encode_parms.tile_rows);
155
156     EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
157     return VPX_CODEC_OK == res_dec;
158   }
159
160   EncodePerfTestVideo test_video_;
161
162  private:
163   EncodeParameters encode_parms;
164 };
165
166 // TODO(hkuang): This test conflicts with frame parallel decode. So disable it
167 // for now until fix.
168 TEST_P(Vp9EncoderParmsGetToDecoder, DISABLED_BitstreamParms) {
169   init_flags_ = VPX_CODEC_USE_PSNR;
170
171   libvpx_test::VideoSource *video;
172   if (is_extension_y4m(test_video_.name)) {
173     video = new libvpx_test::Y4mVideoSource(test_video_.name,
174                                             0, test_video_.frames);
175   } else {
176     video = new libvpx_test::YUVVideoSource(test_video_.name,
177                                             VPX_IMG_FMT_I420,
178                                             test_video_.width,
179                                             test_video_.height,
180                                             kFramerate, 1, 0,
181                                             test_video_.frames);
182   }
183
184   ASSERT_NO_FATAL_FAILURE(RunLoop(video));
185   delete(video);
186 }
187
188 VP9_INSTANTIATE_TEST_CASE(
189     Vp9EncoderParmsGetToDecoder,
190     ::testing::ValuesIn(kVP9EncodeParameterSet),
191     ::testing::ValuesIn(kVP9EncodePerfTestVectors));
192
193 }  // namespace