vpx_scale_test.h 7.1 KB

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