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