encode_test_driver.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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_
  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_parameters *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. #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
  110. void Control(int ctrl_id, vpx_active_map_t *arg) {
  111. const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
  112. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  113. }
  114. #endif
  115. void Config(const vpx_codec_enc_cfg_t *cfg) {
  116. const vpx_codec_err_t res = vpx_codec_enc_config_set(&encoder_, cfg);
  117. ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  118. cfg_ = *cfg;
  119. }
  120. void set_deadline(unsigned long deadline) { deadline_ = deadline; }
  121. protected:
  122. virtual vpx_codec_iface_t *CodecInterface() const = 0;
  123. const char *EncoderError() {
  124. const char *detail = vpx_codec_error_detail(&encoder_);
  125. return detail ? detail : vpx_codec_error(&encoder_);
  126. }
  127. // Encode an image
  128. void EncodeFrameInternal(const VideoSource &video,
  129. const unsigned long frame_flags);
  130. // Flush the encoder on EOS
  131. void Flush();
  132. vpx_codec_ctx_t encoder_;
  133. vpx_codec_enc_cfg_t cfg_;
  134. unsigned long deadline_;
  135. unsigned long init_flags_;
  136. TwopassStatsStore *stats_;
  137. };
  138. // Common test functionality for all Encoder tests.
  139. //
  140. // This class is a mixin which provides the main loop common to all
  141. // encoder tests. It provides hooks which can be overridden by subclasses
  142. // to implement each test's specific behavior, while centralizing the bulk
  143. // of the boilerplate. Note that it doesn't inherit the gtest testing
  144. // classes directly, so that tests can be parameterized differently.
  145. class EncoderTest {
  146. protected:
  147. explicit EncoderTest(const CodecFactory *codec)
  148. : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0),
  149. last_pts_(0) {
  150. // Default to 1 thread.
  151. cfg_.g_threads = 1;
  152. }
  153. virtual ~EncoderTest() {}
  154. // Initialize the cfg_ member with the default configuration.
  155. void InitializeConfig();
  156. // Map the TestMode enum to the deadline_ and passes_ variables.
  157. void SetMode(TestMode mode);
  158. // Set encoder flag.
  159. void set_init_flags(unsigned long flag) { // NOLINT(runtime/int)
  160. init_flags_ = flag;
  161. }
  162. // Main loop
  163. virtual void RunLoop(VideoSource *video);
  164. // Hook to be called at the beginning of a pass.
  165. virtual void BeginPassHook(unsigned int /*pass*/) {}
  166. // Hook to be called at the end of a pass.
  167. virtual void EndPassHook() {}
  168. // Hook to be called before encoding a frame.
  169. virtual void PreEncodeFrameHook(VideoSource * /*video*/) {}
  170. virtual void PreEncodeFrameHook(VideoSource * /*video*/,
  171. Encoder * /*encoder*/) {}
  172. // Hook to be called on every compressed data packet.
  173. virtual void FramePktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
  174. // Hook to be called on every PSNR packet.
  175. virtual void PSNRPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
  176. // Hook to be called on every first pass stats packet.
  177. virtual void StatsPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
  178. // Hook to determine whether the encode loop should continue.
  179. virtual bool Continue() const {
  180. return !(::testing::Test::HasFatalFailure() || abort_);
  181. }
  182. const CodecFactory *codec_;
  183. // Hook to determine whether to decode frame after encoding
  184. virtual bool DoDecode() const { return 1; }
  185. // Hook to handle encode/decode mismatch
  186. virtual void MismatchHook(const vpx_image_t *img1, const vpx_image_t *img2);
  187. // Hook to be called on every decompressed frame.
  188. virtual void DecompressedFrameHook(const vpx_image_t & /*img*/,
  189. vpx_codec_pts_t /*pts*/) {}
  190. // Hook to be called to handle decode result. Return true to continue.
  191. virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
  192. const VideoSource & /*video*/,
  193. Decoder *decoder) {
  194. EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
  195. return VPX_CODEC_OK == res_dec;
  196. }
  197. // Hook that can modify the encoder's output data
  198. virtual const vpx_codec_cx_pkt_t *MutateEncoderOutputHook(
  199. const vpx_codec_cx_pkt_t *pkt) {
  200. return pkt;
  201. }
  202. bool abort_;
  203. vpx_codec_enc_cfg_t cfg_;
  204. vpx_codec_dec_cfg_t dec_cfg_;
  205. unsigned int passes_;
  206. unsigned long deadline_;
  207. TwopassStatsStore stats_;
  208. unsigned long init_flags_;
  209. unsigned long frame_flags_;
  210. vpx_codec_pts_t last_pts_;
  211. };
  212. } // namespace libvpx_test
  213. #endif // TEST_ENCODE_TEST_DRIVER_H_