John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | #ifndef TEST_ENCODE_TEST_DRIVER_H_ |
| 11 | #define TEST_ENCODE_TEST_DRIVER_H_ |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 12 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
James Zern | 51b7fd0 | 2013-05-03 19:08:08 -0700 | [diff] [blame] | 15 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 16 | #include "third_party/googletest/src/include/gtest/gtest.h" |
Jingning Han | 097d59c | 2015-07-29 14:51:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include "./vpx_config.h" |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 19 | #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER || CONFIG_VP10_ENCODER |
Alex Converse | a565469 | 2014-03-11 16:40:57 -0700 | [diff] [blame] | 20 | #include "vpx/vp8cx.h" |
| 21 | #endif |
Jingning Han | 097d59c | 2015-07-29 14:51:36 -0700 | [diff] [blame] | 22 | #include "vpx/vpx_encoder.h" |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 23 | |
| 24 | namespace libvpx_test { |
| 25 | |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 26 | class CodecFactory; |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 27 | class VideoSource; |
| 28 | |
| 29 | enum TestMode { |
| 30 | kRealTime, |
| 31 | kOnePassGood, |
| 32 | kOnePassBest, |
| 33 | kTwoPassGood, |
| 34 | kTwoPassBest |
| 35 | }; |
| 36 | #define ALL_TEST_MODES ::testing::Values(::libvpx_test::kRealTime, \ |
| 37 | ::libvpx_test::kOnePassGood, \ |
| 38 | ::libvpx_test::kOnePassBest, \ |
| 39 | ::libvpx_test::kTwoPassGood, \ |
| 40 | ::libvpx_test::kTwoPassBest) |
| 41 | |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 42 | #define ONE_PASS_TEST_MODES ::testing::Values(::libvpx_test::kRealTime, \ |
| 43 | ::libvpx_test::kOnePassGood, \ |
| 44 | ::libvpx_test::kOnePassBest) |
| 45 | |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 46 | #define TWO_PASS_TEST_MODES ::testing::Values(::libvpx_test::kTwoPassGood, \ |
| 47 | ::libvpx_test::kTwoPassBest) |
| 48 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 49 | |
| 50 | // Provides an object to handle the libvpx get_cx_data() iteration pattern |
| 51 | class CxDataIterator { |
| 52 | public: |
| 53 | explicit CxDataIterator(vpx_codec_ctx_t *encoder) |
James Zern | 51b7fd0 | 2013-05-03 19:08:08 -0700 | [diff] [blame] | 54 | : encoder_(encoder), iter_(NULL) {} |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 55 | |
| 56 | const vpx_codec_cx_pkt_t *Next() { |
| 57 | return vpx_codec_get_cx_data(encoder_, &iter_); |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | vpx_codec_ctx_t *encoder_; |
| 62 | vpx_codec_iter_t iter_; |
| 63 | }; |
| 64 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 65 | // Implements an in-memory store for libvpx twopass statistics |
| 66 | class TwopassStatsStore { |
| 67 | public: |
| 68 | void Append(const vpx_codec_cx_pkt_t &pkt) { |
| 69 | buffer_.append(reinterpret_cast<char *>(pkt.data.twopass_stats.buf), |
| 70 | pkt.data.twopass_stats.sz); |
| 71 | } |
| 72 | |
| 73 | vpx_fixed_buf_t buf() { |
| 74 | const vpx_fixed_buf_t buf = { &buffer_[0], buffer_.size() }; |
| 75 | return buf; |
| 76 | } |
| 77 | |
Adrian Grange | 30f58b5 | 2012-10-02 09:36:41 -0700 | [diff] [blame] | 78 | void Reset() { |
| 79 | buffer_.clear(); |
| 80 | } |
| 81 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 82 | protected: |
| 83 | std::string buffer_; |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | // Provides a simplified interface to manage one video encoding pass, given |
| 88 | // a configuration and video source. |
| 89 | // |
| 90 | // TODO(jkoleszar): The exact services it provides and the appropriate |
| 91 | // level of abstraction will be fleshed out as more tests are written. |
| 92 | class Encoder { |
| 93 | public: |
| 94 | Encoder(vpx_codec_enc_cfg_t cfg, unsigned long deadline, |
Adrian Grange | 4206c6d | 2012-10-02 11:03:09 -0700 | [diff] [blame] | 95 | const unsigned long init_flags, TwopassStatsStore *stats) |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 96 | : cfg_(cfg), deadline_(deadline), init_flags_(init_flags), stats_(stats) { |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 97 | memset(&encoder_, 0, sizeof(encoder_)); |
| 98 | } |
| 99 | |
James Zern | 51b7fd0 | 2013-05-03 19:08:08 -0700 | [diff] [blame] | 100 | virtual ~Encoder() { |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 101 | vpx_codec_destroy(&encoder_); |
| 102 | } |
| 103 | |
| 104 | CxDataIterator GetCxData() { |
| 105 | return CxDataIterator(&encoder_); |
| 106 | } |
| 107 | |
Yunqing Wang | 3666478 | 2014-12-12 14:34:30 -0800 | [diff] [blame] | 108 | void InitEncoder(VideoSource *video); |
| 109 | |
Yaowu Xu | c953aea | 2012-08-30 13:43:15 -0700 | [diff] [blame] | 110 | const vpx_image_t *GetPreviewFrame() { |
| 111 | return vpx_codec_get_preview_frame(&encoder_); |
| 112 | } |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 113 | // This is a thin wrapper around vpx_codec_encode(), so refer to |
| 114 | // vpx_encoder.h for its semantics. |
Adrian Grange | 4206c6d | 2012-10-02 11:03:09 -0700 | [diff] [blame] | 115 | void EncodeFrame(VideoSource *video, const unsigned long frame_flags); |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 116 | |
| 117 | // Convenience wrapper for EncodeFrame() |
| 118 | void EncodeFrame(VideoSource *video) { |
| 119 | EncodeFrame(video, 0); |
| 120 | } |
| 121 | |
John Koleszar | 606ac45 | 2012-07-10 15:43:44 -0700 | [diff] [blame] | 122 | void Control(int ctrl_id, int arg) { |
| 123 | const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg); |
| 124 | ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); |
| 125 | } |
| 126 | |
Yaowu Xu | 7c514e2 | 2015-09-28 15:55:46 -0700 | [diff] [blame] | 127 | void Control(int ctrl_id, int *arg) { |
| 128 | const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg); |
| 129 | ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); |
| 130 | } |
| 131 | |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 132 | void Control(int ctrl_id, struct vpx_scaling_mode *arg) { |
| 133 | const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg); |
| 134 | ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); |
| 135 | } |
| 136 | |
Marco Paniconi | 4864ab2 | 2014-02-06 09:23:17 -0800 | [diff] [blame] | 137 | void Control(int ctrl_id, struct vpx_svc_layer_id *arg) { |
| 138 | const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg); |
| 139 | ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); |
| 140 | } |
| 141 | |
Marco | c139b81 | 2015-05-21 16:15:37 -0700 | [diff] [blame] | 142 | void Control(int ctrl_id, struct vpx_svc_parameters *arg) { |
| 143 | const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg); |
| 144 | ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); |
| 145 | } |
Jingning Han | 3ee6db6 | 2015-08-05 19:00:31 -0700 | [diff] [blame] | 146 | #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER || CONFIG_VP10_ENCODER |
Alex Converse | a565469 | 2014-03-11 16:40:57 -0700 | [diff] [blame] | 147 | void Control(int ctrl_id, vpx_active_map_t *arg) { |
| 148 | const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg); |
| 149 | ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); |
| 150 | } |
| 151 | #endif |
| 152 | |
Alex Converse | 797a255 | 2015-01-15 13:56:55 -0800 | [diff] [blame] | 153 | void Config(const vpx_codec_enc_cfg_t *cfg) { |
| 154 | const vpx_codec_err_t res = vpx_codec_enc_config_set(&encoder_, cfg); |
| 155 | ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); |
| 156 | cfg_ = *cfg; |
| 157 | } |
| 158 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 159 | void set_deadline(unsigned long deadline) { |
| 160 | deadline_ = deadline; |
| 161 | } |
| 162 | |
| 163 | protected: |
James Zern | 4a70357 | 2014-01-31 16:32:42 -0800 | [diff] [blame] | 164 | virtual vpx_codec_iface_t* CodecInterface() const = 0; |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 165 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 166 | const char *EncoderError() { |
| 167 | const char *detail = vpx_codec_error_detail(&encoder_); |
| 168 | return detail ? detail : vpx_codec_error(&encoder_); |
| 169 | } |
| 170 | |
| 171 | // Encode an image |
Adrian Grange | 4206c6d | 2012-10-02 11:03:09 -0700 | [diff] [blame] | 172 | void EncodeFrameInternal(const VideoSource &video, |
| 173 | const unsigned long frame_flags); |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 174 | |
| 175 | // Flush the encoder on EOS |
| 176 | void Flush(); |
| 177 | |
| 178 | vpx_codec_ctx_t encoder_; |
| 179 | vpx_codec_enc_cfg_t cfg_; |
| 180 | unsigned long deadline_; |
Adrian Grange | 4206c6d | 2012-10-02 11:03:09 -0700 | [diff] [blame] | 181 | unsigned long init_flags_; |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 182 | TwopassStatsStore *stats_; |
| 183 | }; |
| 184 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 185 | // Common test functionality for all Encoder tests. |
| 186 | // |
| 187 | // This class is a mixin which provides the main loop common to all |
| 188 | // encoder tests. It provides hooks which can be overridden by subclasses |
| 189 | // to implement each test's specific behavior, while centralizing the bulk |
| 190 | // of the boilerplate. Note that it doesn't inherit the gtest testing |
| 191 | // classes directly, so that tests can be parameterized differently. |
| 192 | class EncoderTest { |
| 193 | protected: |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 194 | explicit EncoderTest(const CodecFactory *codec) |
| 195 | : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0), |
Frank Galligan | 45f9ee2 | 2015-04-14 15:01:56 -0700 | [diff] [blame] | 196 | last_pts_(0) { |
| 197 | // Default to 1 thread. |
| 198 | cfg_.g_threads = 1; |
| 199 | } |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 200 | |
| 201 | virtual ~EncoderTest() {} |
| 202 | |
| 203 | // Initialize the cfg_ member with the default configuration. |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 204 | void InitializeConfig(); |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 205 | |
| 206 | // Map the TestMode enum to the deadline_ and passes_ variables. |
| 207 | void SetMode(TestMode mode); |
| 208 | |
hkuang | 9353607 | 2014-11-20 15:39:56 -0800 | [diff] [blame] | 209 | // Set encoder flag. |
| 210 | void set_init_flags(unsigned long flag) { // NOLINT(runtime/int) |
| 211 | init_flags_ = flag; |
| 212 | } |
| 213 | |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 214 | // Main loop |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 215 | virtual void RunLoop(VideoSource *video); |
| 216 | |
| 217 | // Hook to be called at the beginning of a pass. |
James Zern | cae810a | 2014-08-22 11:58:48 -0700 | [diff] [blame] | 218 | virtual void BeginPassHook(unsigned int /*pass*/) {} |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 219 | |
| 220 | // Hook to be called at the end of a pass. |
| 221 | virtual void EndPassHook() {} |
| 222 | |
| 223 | // Hook to be called before encoding a frame. |
James Zern | cae810a | 2014-08-22 11:58:48 -0700 | [diff] [blame] | 224 | virtual void PreEncodeFrameHook(VideoSource* /*video*/) {} |
| 225 | virtual void PreEncodeFrameHook(VideoSource* /*video*/, |
| 226 | Encoder* /*encoder*/) {} |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 227 | |
| 228 | // Hook to be called on every compressed data packet. |
James Zern | cae810a | 2014-08-22 11:58:48 -0700 | [diff] [blame] | 229 | virtual void FramePktHook(const vpx_codec_cx_pkt_t* /*pkt*/) {} |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 230 | |
Adrian Grange | e6109db | 2012-10-02 11:27:29 -0700 | [diff] [blame] | 231 | // Hook to be called on every PSNR packet. |
James Zern | cae810a | 2014-08-22 11:58:48 -0700 | [diff] [blame] | 232 | virtual void PSNRPktHook(const vpx_codec_cx_pkt_t* /*pkt*/) {} |
Adrian Grange | e6109db | 2012-10-02 11:27:29 -0700 | [diff] [blame] | 233 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 234 | // Hook to determine whether the encode loop should continue. |
James Zern | 1c05e9d | 2013-06-25 17:53:20 -0700 | [diff] [blame] | 235 | virtual bool Continue() const { |
| 236 | return !(::testing::Test::HasFatalFailure() || abort_); |
| 237 | } |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 238 | |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 239 | const CodecFactory *codec_; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 240 | // Hook to determine whether to decode frame after encoding |
| 241 | virtual bool DoDecode() const { return 1; } |
| 242 | |
| 243 | // Hook to handle encode/decode mismatch |
| 244 | virtual void MismatchHook(const vpx_image_t *img1, |
| 245 | const vpx_image_t *img2); |
| 246 | |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 247 | // Hook to be called on every decompressed frame. |
James Zern | cae810a | 2014-08-22 11:58:48 -0700 | [diff] [blame] | 248 | virtual void DecompressedFrameHook(const vpx_image_t& /*img*/, |
| 249 | vpx_codec_pts_t /*pts*/) {} |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 250 | |
Jim Bankoski | 943e432 | 2014-07-17 06:31:50 -0700 | [diff] [blame] | 251 | // Hook to be called to handle decode result. Return true to continue. |
| 252 | virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec, |
James Zern | cae810a | 2014-08-22 11:58:48 -0700 | [diff] [blame] | 253 | const VideoSource& /*video*/, |
Jim Bankoski | 943e432 | 2014-07-17 06:31:50 -0700 | [diff] [blame] | 254 | Decoder *decoder) { |
| 255 | EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError(); |
| 256 | return VPX_CODEC_OK == res_dec; |
| 257 | } |
| 258 | |
John Koleszar | 522d4bf | 2013-03-05 12:23:34 -0800 | [diff] [blame] | 259 | // Hook that can modify the encoder's output data |
James Zern | cae810a | 2014-08-22 11:58:48 -0700 | [diff] [blame] | 260 | virtual const vpx_codec_cx_pkt_t *MutateEncoderOutputHook( |
John Koleszar | 522d4bf | 2013-03-05 12:23:34 -0800 | [diff] [blame] | 261 | const vpx_codec_cx_pkt_t *pkt) { |
| 262 | return pkt; |
| 263 | } |
| 264 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 265 | bool abort_; |
| 266 | vpx_codec_enc_cfg_t cfg_; |
Jim Bankoski | be7a285 | 2014-12-07 11:28:51 -0800 | [diff] [blame] | 267 | vpx_codec_dec_cfg_t dec_cfg_; |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 268 | unsigned int passes_; |
| 269 | unsigned long deadline_; |
| 270 | TwopassStatsStore stats_; |
Adrian Grange | 4206c6d | 2012-10-02 11:03:09 -0700 | [diff] [blame] | 271 | unsigned long init_flags_; |
| 272 | unsigned long frame_flags_; |
James Zern | 51ebb9a | 2012-08-08 14:16:08 -0700 | [diff] [blame] | 273 | vpx_codec_pts_t last_pts_; |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | } // namespace libvpx_test |
| 277 | |
John Koleszar | b9180fc | 2012-05-16 15:27:00 -0700 | [diff] [blame] | 278 | #endif // TEST_ENCODE_TEST_DRIVER_H_ |