vpx_scale_test.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #ifndef TEST_VPX_SCALE_TEST_H_
  11. #define TEST_VPX_SCALE_TEST_H_
  12. #include "third_party/googletest/src/include/gtest/gtest.h"
  13. #include "./vpx_config.h"
  14. #include "./vpx_scale_rtcd.h"
  15. #include "test/clear_system_state.h"
  16. #include "test/register_state_check.h"
  17. #include "vpx_mem/vpx_mem.h"
  18. #include "vpx_scale/yv12config.h"
  19. namespace libvpx_test {
  20. class VpxScaleBase {
  21. public:
  22. virtual ~VpxScaleBase() { libvpx_test::ClearSystemState(); }
  23. void ResetImage(YV12_BUFFER_CONFIG *const img, const int width,
  24. const int height) {
  25. memset(img, 0, sizeof(*img));
  26. ASSERT_EQ(
  27. 0, vp8_yv12_alloc_frame_buffer(img, width, height, VP8BORDERINPIXELS));
  28. memset(img->buffer_alloc, kBufFiller, img->frame_size);
  29. }
  30. void ResetImages(const int width, const int height) {
  31. ResetImage(&img_, width, height);
  32. ResetImage(&ref_img_, width, height);
  33. ResetImage(&dst_img_, width, height);
  34. FillPlane(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
  35. img_.y_stride);
  36. FillPlane(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
  37. img_.uv_stride);
  38. FillPlane(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
  39. img_.uv_stride);
  40. }
  41. void ResetScaleImage(YV12_BUFFER_CONFIG *const img, const int width,
  42. const int height) {
  43. memset(img, 0, sizeof(*img));
  44. #if CONFIG_VP9_HIGHBITDEPTH
  45. ASSERT_EQ(0, vpx_alloc_frame_buffer(img, width, height, 1, 1, 0,
  46. VP9_ENC_BORDER_IN_PIXELS, 0));
  47. #else
  48. ASSERT_EQ(0, vpx_alloc_frame_buffer(img, width, height, 1, 1,
  49. VP9_ENC_BORDER_IN_PIXELS, 0));
  50. #endif
  51. memset(img->buffer_alloc, kBufFiller, img->frame_size);
  52. }
  53. void ResetScaleImages(const int src_width, const int src_height,
  54. const int dst_width, const int dst_height) {
  55. ResetScaleImage(&img_, src_width, src_height);
  56. ResetScaleImage(&ref_img_, dst_width, dst_height);
  57. ResetScaleImage(&dst_img_, dst_width, dst_height);
  58. FillPlane(img_.y_buffer, img_.y_crop_width, img_.y_crop_height,
  59. img_.y_stride);
  60. FillPlane(img_.u_buffer, img_.uv_crop_width, img_.uv_crop_height,
  61. img_.uv_stride);
  62. FillPlane(img_.v_buffer, img_.uv_crop_width, img_.uv_crop_height,
  63. img_.uv_stride);
  64. }
  65. void DeallocImages() {
  66. vp8_yv12_de_alloc_frame_buffer(&img_);
  67. vp8_yv12_de_alloc_frame_buffer(&ref_img_);
  68. vp8_yv12_de_alloc_frame_buffer(&dst_img_);
  69. }
  70. void DeallocScaleImages() {
  71. vpx_free_frame_buffer(&img_);
  72. vpx_free_frame_buffer(&ref_img_);
  73. vpx_free_frame_buffer(&dst_img_);
  74. }
  75. protected:
  76. static const int kBufFiller = 123;
  77. static const int kBufMax = kBufFiller - 1;
  78. static void FillPlane(uint8_t *buf, int width, int height, int stride) {
  79. for (int y = 0; y < height; ++y) {
  80. for (int x = 0; x < width; ++x) {
  81. buf[x + (y * stride)] = (x + (width * y)) % kBufMax;
  82. }
  83. }
  84. }
  85. static void ExtendPlane(uint8_t *buf, int crop_width, int crop_height,
  86. int width, int height, int stride, int padding) {
  87. // Copy the outermost visible pixel to a distance of at least 'padding.'
  88. // The buffers are allocated such that there may be excess space outside the
  89. // padding. As long as the minimum amount of padding is achieved it is not
  90. // necessary to fill this space as well.
  91. uint8_t *left = buf - padding;
  92. uint8_t *right = buf + crop_width;
  93. const int right_extend = padding + (width - crop_width);
  94. const int bottom_extend = padding + (height - crop_height);
  95. // Fill the border pixels from the nearest image pixel.
  96. for (int y = 0; y < crop_height; ++y) {
  97. memset(left, left[padding], padding);
  98. memset(right, right[-1], right_extend);
  99. left += stride;
  100. right += stride;
  101. }
  102. left = buf - padding;
  103. uint8_t *top = left - (stride * padding);
  104. // The buffer does not always extend as far as the stride.
  105. // Equivalent to padding + width + padding.
  106. const int extend_width = padding + crop_width + right_extend;
  107. // The first row was already extended to the left and right. Copy it up.
  108. for (int y = 0; y < padding; ++y) {
  109. memcpy(top, left, extend_width);
  110. top += stride;
  111. }
  112. uint8_t *bottom = left + (crop_height * stride);
  113. for (int y = 0; y < bottom_extend; ++y) {
  114. memcpy(bottom, left + (crop_height - 1) * stride, extend_width);
  115. bottom += stride;
  116. }
  117. }
  118. void ReferenceExtendBorder() {
  119. ExtendPlane(ref_img_.y_buffer, ref_img_.y_crop_width,
  120. ref_img_.y_crop_height, ref_img_.y_width, ref_img_.y_height,
  121. ref_img_.y_stride, ref_img_.border);
  122. ExtendPlane(ref_img_.u_buffer, ref_img_.uv_crop_width,
  123. ref_img_.uv_crop_height, ref_img_.uv_width, ref_img_.uv_height,
  124. ref_img_.uv_stride, ref_img_.border / 2);
  125. ExtendPlane(ref_img_.v_buffer, ref_img_.uv_crop_width,
  126. ref_img_.uv_crop_height, ref_img_.uv_width, ref_img_.uv_height,
  127. ref_img_.uv_stride, ref_img_.border / 2);
  128. }
  129. void ReferenceCopyFrame() {
  130. // Copy img_ to ref_img_ and extend frame borders. This will be used for
  131. // verifying extend_fn_ as well as copy_frame_fn_.
  132. EXPECT_EQ(ref_img_.frame_size, img_.frame_size);
  133. for (int y = 0; y < img_.y_crop_height; ++y) {
  134. for (int x = 0; x < img_.y_crop_width; ++x) {
  135. ref_img_.y_buffer[x + y * ref_img_.y_stride] =
  136. img_.y_buffer[x + y * img_.y_stride];
  137. }
  138. }
  139. for (int y = 0; y < img_.uv_crop_height; ++y) {
  140. for (int x = 0; x < img_.uv_crop_width; ++x) {
  141. ref_img_.u_buffer[x + y * ref_img_.uv_stride] =
  142. img_.u_buffer[x + y * img_.uv_stride];
  143. ref_img_.v_buffer[x + y * ref_img_.uv_stride] =
  144. img_.v_buffer[x + y * img_.uv_stride];
  145. }
  146. }
  147. ReferenceExtendBorder();
  148. }
  149. void CompareImages(const YV12_BUFFER_CONFIG actual) {
  150. EXPECT_EQ(ref_img_.frame_size, actual.frame_size);
  151. EXPECT_EQ(0, memcmp(ref_img_.buffer_alloc, actual.buffer_alloc,
  152. ref_img_.frame_size));
  153. }
  154. YV12_BUFFER_CONFIG img_;
  155. YV12_BUFFER_CONFIG ref_img_;
  156. YV12_BUFFER_CONFIG dst_img_;
  157. };
  158. } // namespace libvpx_test
  159. #endif // TEST_VPX_SCALE_TEST_H_