Home | History | Annotate | Download | only in test
      1 /*
      2  *  Copyright (c) 2013 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 #include <math.h>
     12 #include <stddef.h>
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <string.h>
     16 #include <sys/types.h>
     17 
     18 #include "./vp8_rtcd.h"
     19 
     20 #include "test/acm_random.h"
     21 #include "third_party/googletest/src/include/gtest/gtest.h"
     22 #include "vpx/vpx_integer.h"
     23 
     24 namespace {
     25 
     26 const int cospi8sqrt2minus1 = 20091;
     27 const int sinpi8sqrt2 = 35468;
     28 
     29 void reference_idct4x4(const int16_t *input, int16_t *output) {
     30   const int16_t *ip = input;
     31   int16_t *op = output;
     32 
     33   for (int i = 0; i < 4; ++i) {
     34     const int a1 = ip[0] + ip[8];
     35     const int b1 = ip[0] - ip[8];
     36     const int temp1 = (ip[4] * sinpi8sqrt2) >> 16;
     37     const int temp2 = ip[12] + ((ip[12] * cospi8sqrt2minus1) >> 16);
     38     const int c1 = temp1 - temp2;
     39     const int temp3 = ip[4] + ((ip[4] * cospi8sqrt2minus1) >> 16);
     40     const int temp4 = (ip[12] * sinpi8sqrt2) >> 16;
     41     const int d1 = temp3 + temp4;
     42     op[0] = a1 + d1;
     43     op[12] = a1 - d1;
     44     op[4] = b1 + c1;
     45     op[8] = b1 - c1;
     46     ++ip;
     47     ++op;
     48   }
     49   ip = output;
     50   op = output;
     51   for (int i = 0; i < 4; ++i) {
     52     const int a1 = ip[0] + ip[2];
     53     const int b1 = ip[0] - ip[2];
     54     const int temp1 = (ip[1] * sinpi8sqrt2) >> 16;
     55     const int temp2 = ip[3] + ((ip[3] * cospi8sqrt2minus1) >> 16);
     56     const int c1 = temp1 - temp2;
     57     const int temp3 = ip[1] + ((ip[1] * cospi8sqrt2minus1) >> 16);
     58     const int temp4 = (ip[3] * sinpi8sqrt2) >> 16;
     59     const int d1 = temp3 + temp4;
     60     op[0] = (a1 + d1 + 4) >> 3;
     61     op[3] = (a1 - d1 + 4) >> 3;
     62     op[1] = (b1 + c1 + 4) >> 3;
     63     op[2] = (b1 - c1 + 4) >> 3;
     64     ip += 4;
     65     op += 4;
     66   }
     67 }
     68 
     69 using libvpx_test::ACMRandom;
     70 
     71 TEST(VP8FdctTest, SignBiasCheck) {
     72   ACMRandom rnd(ACMRandom::DeterministicSeed());
     73   int16_t test_input_block[16];
     74   int16_t test_output_block[16];
     75   const int pitch = 8;
     76   int count_sign_block[16][2];
     77   const int count_test_block = 1000000;
     78 
     79   memset(count_sign_block, 0, sizeof(count_sign_block));
     80 
     81   for (int i = 0; i < count_test_block; ++i) {
     82     // Initialize a test block with input range [-255, 255].
     83     for (int j = 0; j < 16; ++j)
     84       test_input_block[j] = rnd.Rand8() - rnd.Rand8();
     85 
     86     vp8_short_fdct4x4_c(test_input_block, test_output_block, pitch);
     87 
     88     for (int j = 0; j < 16; ++j) {
     89       if (test_output_block[j] < 0)
     90         ++count_sign_block[j][0];
     91       else if (test_output_block[j] > 0)
     92         ++count_sign_block[j][1];
     93     }
     94   }
     95 
     96   bool bias_acceptable = true;
     97   for (int j = 0; j < 16; ++j)
     98     bias_acceptable = bias_acceptable &&
     99     (abs(count_sign_block[j][0] - count_sign_block[j][1]) < 10000);
    100 
    101   EXPECT_EQ(true, bias_acceptable)
    102     << "Error: 4x4 FDCT has a sign bias > 1% for input range [-255, 255]";
    103 
    104   memset(count_sign_block, 0, sizeof(count_sign_block));
    105 
    106   for (int i = 0; i < count_test_block; ++i) {
    107     // Initialize a test block with input range [-15, 15].
    108     for (int j = 0; j < 16; ++j)
    109       test_input_block[j] = (rnd.Rand8() >> 4) - (rnd.Rand8() >> 4);
    110 
    111     vp8_short_fdct4x4_c(test_input_block, test_output_block, pitch);
    112 
    113     for (int j = 0; j < 16; ++j) {
    114       if (test_output_block[j] < 0)
    115         ++count_sign_block[j][0];
    116       else if (test_output_block[j] > 0)
    117         ++count_sign_block[j][1];
    118     }
    119   }
    120 
    121   bias_acceptable = true;
    122   for (int j = 0; j < 16; ++j)
    123     bias_acceptable = bias_acceptable &&
    124     (abs(count_sign_block[j][0] - count_sign_block[j][1]) < 100000);
    125 
    126   EXPECT_EQ(true, bias_acceptable)
    127     << "Error: 4x4 FDCT has a sign bias > 10% for input range [-15, 15]";
    128 };
    129 
    130 TEST(VP8FdctTest, RoundTripErrorCheck) {
    131   ACMRandom rnd(ACMRandom::DeterministicSeed());
    132   int max_error = 0;
    133   double total_error = 0;
    134   const int count_test_block = 1000000;
    135   for (int i = 0; i < count_test_block; ++i) {
    136     int16_t test_input_block[16];
    137     int16_t test_temp_block[16];
    138     int16_t test_output_block[16];
    139 
    140     // Initialize a test block with input range [-255, 255].
    141     for (int j = 0; j < 16; ++j)
    142       test_input_block[j] = rnd.Rand8() - rnd.Rand8();
    143 
    144     const int pitch = 8;
    145     vp8_short_fdct4x4_c(test_input_block, test_temp_block, pitch);
    146     reference_idct4x4(test_temp_block, test_output_block);
    147 
    148     for (int j = 0; j < 16; ++j) {
    149       const int diff = test_input_block[j] - test_output_block[j];
    150       const int error = diff * diff;
    151       if (max_error < error)
    152         max_error = error;
    153       total_error += error;
    154     }
    155   }
    156 
    157   EXPECT_GE(1, max_error )
    158     << "Error: FDCT/IDCT has an individual roundtrip error > 1";
    159 
    160   EXPECT_GE(count_test_block, total_error)
    161     << "Error: FDCT/IDCT has average roundtrip error > 1 per block";
    162 };
    163 
    164 }  // namespace
    165