encode_test_driver.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 VPX_TEST_ENCODE_TEST_DRIVER_H_
  11. #define VPX_TEST_ENCODE_TEST_DRIVER_H_
  12. #include <string>
  13. #include <vector>
  14. #include "third_party/googletest/src/include/gtest/gtest.h"
  15. #include "./vpx_config.h"
  16. #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
  17. #include "vpx/vp8cx.h"
  18. #endif
  19. #include "vpx/vpx_encoder.h"
  20. namespace libvpx_test {
  21. class CodecFactory;
  22. class VideoSource;
  23. enum TestMode {
  24. kRealTime,
  25. kOnePassGood,
  26. kOnePassBest,
  27. kTwoPassGood,
  28. kTwoPassBest
  29. };
  30. #define ALL_TEST_MODES \
  31. ::testing::Values(::libvpx_test::kRealTime, ::libvpx_test::kOnePassGood, \
  32. ::libvpx_test::kOnePassBest, ::libvpx_test::kTwoPassGood, \
  33. ::libvpx_test::kTwoPassBest)
  34. #define ONE_PASS_TEST_MODES \
  35. ::testing::Values(::libvpx_test::kRealTime, ::libvpx_test::kOnePassGood, \
  36. ::libvpx_test::kOnePassBest)
  37. #define TWO_PASS_TEST_MODES \
  38. ::testing::Values(::libvpx_test::kTwoPassGood, ::libvpx_test::kTwoPassBest)
  39. // Provides an object to handle the libvpx get_cx_data() iteration pattern
  40. class CxDataIterator {
  41. public:
  42. explicit CxDataIterator(vpx_codec_ctx_t *encoder)
  43. : encoder_(encoder), iter_(NULL) {}
  44. const vpx_codec_cx_pkt_t *Next() {
  45. return vpx_codec_get_cx_data(encoder_, &iter_);
  46. }
  47. private:
  48. vpx_codec_ctx_t *encoder_;
  49. vpx_codec_iter_t iter_;
  50. };
  51. // Implements an in-memory store for libvpx twopass statistics
  52. class TwopassStatsStore {
  53. public:
  54. void Append(const vpx_codec_cx_pkt_t &pkt) {
  55. buffer_.append(reinterpret_cast<char *>(pkt.data.twopass_stats.buf),
  56. pkt.data.twopass_stats.sz);
  57. }
  58. vpx_fixed_buf_t buf() {
  59. const vpx_fixed_buf_t buf = { &buffer_[0], buffer_.size() };
  60. return buf;
  61. }
  62. void Reset() { buffer_.clear(); }
  63. protected:
  64. std::string buffer_;
  65. };
  66. // Provides a simplified interface to manage one video encoding pass, given
  67. // a configuration and video source.
  68. //
  69. // TODO(jkoleszar): The exact services it provides and the appropriate
  70. // level of abstraction will be fleshed out as more tests are written.
  71. class Encoder {
  72. public:
  73. Encoder(vpx_codec_enc_cfg_t cfg, unsigned long deadline,
  74. const unsigned long init_flags, TwopassStatsStore *stats)
  75. : cfg_(cfg), deadline_(deadline), init_flags_(init_flags), stats_(stats) {
  76. memset(&encoder_, 0, sizeof(encoder_));
  77. }
  78. virtual ~Encoder() { vpx_codec_destroy(&encoder_); }
  79. CxDataIterator GetCxData() { return CxDataIterator(&encoder_); }
  80. void InitEncoder(VideoSource *video);
  81. const vpx_image_t *GetPreviewFrame() {
  82. return vpx_codec_get_preview_frame(&encoder_);
  83. }
  84. // This is a thin wrapper around vpx_codec_encode(), so refer to
  85. // vpx_encoder.h for its semantics.
  86. void EncodeFrame(VideoSource *video, const unsigned long frame_flags);
  87. // Convenience wrapper for EncodeFrame()
  88. void EncodeFrame(VideoSource *video) { EncodeFrame(video, 0); }
  89. void Control(int ctrl_id, int arg) {
  90. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  91. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  92. }
  93. void Control(int ctrl_id, int *arg) {
  94. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  95. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  96. }
  97. void Control(int ctrl_id, struct vpx_scaling_mode *arg) {
  98. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  99. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  100. }
  101. void Control(int ctrl_id, struct vpx_svc_layer_id *arg) {
  102. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  103. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  104. }
  105. void Control(int ctrl_id, struct vpx_svc_ref_frame_config *arg) {
  106. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  107. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  108. }
  109. void Control(int ctrl_id, struct vpx_svc_parameters *arg) {
  110. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  111. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  112. }
  113. void Control(int ctrl_id, struct vpx_svc_frame_drop *arg) {
  114. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  115. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  116. }
  117. void Control(int ctrl_id, struct vpx_svc_spatial_layer_sync *arg) {
  118. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  119. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  120. }
  121. #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
  122. void Control(int ctrl_id, vpx_active_map_t *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. void Control(int ctrl_id, vpx_roi_map_t *arg) {
  127. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  128. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  129. }
  130. #endif
  131. void Config(const vpx_codec_enc_cfg_t *cfg) {
  132. const vpx_codec_err_t res = vpx_codec_enc_config_set(&encoder_, cfg);
  133. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  134. cfg_ = *cfg;
  135. }
  136. void set_deadline(unsigned long deadline) { deadline_ = deadline; }
  137. protected:
  138. virtual vpx_codec_iface_t *CodecInterface() const = 0;
  139. const char *EncoderError() {
  140. const char *detail = vpx_codec_error_detail(&encoder_);
  141. return detail ? detail : vpx_codec_error(&encoder_);
  142. }
  143. // Encode an image
  144. void EncodeFrameInternal(const VideoSource &video,
  145. const unsigned long frame_flags);
  146. // Flush the encoder on EOS
  147. void Flush();
  148. vpx_codec_ctx_t encoder_;
  149. vpx_codec_enc_cfg_t cfg_;
  150. unsigned long deadline_;
  151. unsigned long init_flags_;
  152. TwopassStatsStore *stats_;
  153. };
  154. // Common test functionality for all Encoder tests.
  155. //
  156. // This class is a mixin which provides the main loop common to all
  157. // encoder tests. It provides hooks which can be overridden by subclasses
  158. // to implement each test's specific behavior, while centralizing the bulk
  159. // of the boilerplate. Note that it doesn't inherit the gtest testing
  160. // classes directly, so that tests can be parameterized differently.
  161. class EncoderTest {
  162. protected:
  163. explicit EncoderTest(const CodecFactory *codec)
  164. : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0),
  165. last_pts_(0) {
  166. // Default to 1 thread.
  167. cfg_.g_threads = 1;
  168. }
  169. virtual ~EncoderTest() {}
  170. // Initialize the cfg_ member with the default configuration.
  171. void InitializeConfig();
  172. // Map the TestMode enum to the deadline_ and passes_ variables.
  173. void SetMode(TestMode mode);
  174. // Set encoder flag.
  175. void set_init_flags(unsigned long flag) { // NOLINT(runtime/int)
  176. init_flags_ = flag;
  177. }
  178. // Main loop
  179. virtual void RunLoop(VideoSource *video);
  180. // Hook to be called at the beginning of a pass.
  181. virtual void BeginPassHook(unsigned int /*pass*/) {}
  182. // Hook to be called at the end of a pass.
  183. virtual void EndPassHook() {}
  184. // Hook to be called before encoding a frame.
  185. virtual void PreEncodeFrameHook(VideoSource * /*video*/) {}
  186. virtual void PreEncodeFrameHook(VideoSource * /*video*/,
  187. Encoder * /*encoder*/) {}
  188. virtual void PreDecodeFrameHook(VideoSource * /*video*/,
  189. Decoder * /*decoder*/) {}
  190. virtual void PostEncodeFrameHook(Encoder * /*encoder*/) {}
  191. // Hook to be called on every compressed data packet.
  192. virtual void FramePktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
  193. // Hook to be called on every PSNR packet.
  194. virtual void PSNRPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
  195. // Hook to be called on every first pass stats packet.
  196. virtual void StatsPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
  197. // Hook to determine whether the encode loop should continue.
  198. virtual bool Continue() const {
  199. return !(::testing::Test::HasFatalFailure() || abort_);
  200. }
  201. const CodecFactory *codec_;
  202. // Hook to determine whether to decode frame after encoding
  203. virtual bool DoDecode() const { return 1; }
  204. // Hook to handle encode/decode mismatch
  205. virtual void MismatchHook(const vpx_image_t *img1, const vpx_image_t *img2);
  206. // Hook to be called on every decompressed frame.
  207. virtual void DecompressedFrameHook(const vpx_image_t & /*img*/,
  208. vpx_codec_pts_t /*pts*/) {}
  209. // Hook to be called to handle decode result. Return true to continue.
  210. virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
  211. const VideoSource & /*video*/,
  212. Decoder *decoder) {
  213. EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
  214. return VPX_CODEC_OK == res_dec;
  215. }
  216. // Hook that can modify the encoder's output data
  217. virtual const vpx_codec_cx_pkt_t *MutateEncoderOutputHook(
  218. const vpx_codec_cx_pkt_t *pkt) {
  219. return pkt;
  220. }
  221. bool abort_;
  222. vpx_codec_enc_cfg_t cfg_;
  223. vpx_codec_dec_cfg_t dec_cfg_;
  224. unsigned int passes_;
  225. unsigned long deadline_;
  226. TwopassStatsStore stats_;
  227. unsigned long init_flags_;
  228. unsigned long frame_flags_;
  229. vpx_codec_pts_t last_pts_;
  230. };
  231. } // namespace libvpx_test
  232. #endif // VPX_TEST_ENCODE_TEST_DRIVER_H_