• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "cpu.h"
2 #include "macros.h"
3 #include "set_mb_syn_cavlc.h"
4 #include <gtest/gtest.h>
5 #include <cmath>
6 #include <cstddef>
7 
8 using namespace WelsEnc;
9 
10 namespace {
11 
CavlcParamCal_ref(int16_t * pCoffLevel,uint8_t * pRun,int16_t * pLevel,int32_t * pTotalCoeff,int32_t iLastIndex)12 int32_t CavlcParamCal_ref (int16_t* pCoffLevel, uint8_t* pRun, int16_t* pLevel, int32_t* pTotalCoeff,
13                            int32_t iLastIndex) {
14   int32_t iTotalZeros = 0;
15   int32_t iTotalCoeffs = 0;
16 
17   while (iLastIndex >= 0 && pCoffLevel[iLastIndex] == 0) {
18     -- iLastIndex;
19   }
20 
21   while (iLastIndex >= 0) {
22     int32_t iCountZero = 0;
23     pLevel[iTotalCoeffs] = pCoffLevel[iLastIndex--];
24 
25     while (iLastIndex >= 0 && pCoffLevel[iLastIndex] == 0) {
26       ++ iCountZero;
27       -- iLastIndex;
28     }
29     iTotalZeros += iCountZero;
30     pRun[iTotalCoeffs++] = iCountZero;
31   }
32   *pTotalCoeff = iTotalCoeffs;
33   return iTotalZeros;
34 }
35 
TestCavlcParamCalWithEndIdx(PCavlcParamCalFunc func,int endIdx,bool allZero,bool allNonZero)36 void TestCavlcParamCalWithEndIdx (PCavlcParamCalFunc func, int endIdx, bool allZero, bool allNonZero) {
37   ENFORCE_STACK_ALIGN_1D(int16_t, coeffLevel, 16, 16);
38   ENFORCE_STACK_ALIGN_1D(int16_t, level, 16, 16);
39   ENFORCE_STACK_ALIGN_1D(uint8_t, run, 16, 16);
40   uint8_t run_ref[16];
41   int16_t level_ref[16];
42   int32_t totalCoeffs = 0;
43   int32_t totalCoeffs_ref = 0;
44   for (int i = 0; i < 16; i++) {
45     const int r = std::rand();
46     if (allZero || (i > endIdx && endIdx > 7))
47       coeffLevel[i] = 0;
48     else if (allNonZero)
49       coeffLevel[i] = r % 0xFFFF - 0x8000 ? r % 0xFFFF - 0x8000 : 0x7FFF;
50     else
51       coeffLevel[i] = (r >> 16 & 1) * ((r & 0xFFFF) - 0x8000);
52   }
53   const int32_t totalZeros_ref = CavlcParamCal_ref (coeffLevel, run_ref, level_ref, &totalCoeffs_ref, endIdx);
54   const int32_t totalZeros = func (coeffLevel, run, level, &totalCoeffs, endIdx);
55   ASSERT_EQ (totalCoeffs, totalCoeffs_ref);
56   if (totalCoeffs > 0)
57     ASSERT_EQ (totalZeros, totalZeros_ref);
58   for (int i = 0; i < totalCoeffs_ref; i++)
59     ASSERT_EQ (level[i], level_ref[i]);
60   for (int i = 0; i < totalCoeffs_ref - 1; i++)
61     ASSERT_EQ (run[i], run_ref[i]);
62 }
63 
TestCavlcParamCal(PCavlcParamCalFunc func)64 void TestCavlcParamCal (PCavlcParamCalFunc func) {
65   const int endIdxes[] = { 3, 14, 15 };
66   const int num_test_repetitions = 10000;
67   for (std::size_t i = 0; i < sizeof endIdxes / sizeof *endIdxes; i++) {
68     for (int count = 0; count < num_test_repetitions; count++)
69       TestCavlcParamCalWithEndIdx (func, endIdxes[i], count == 0, count == 1);
70   }
71 }
72 
73 } // anon ns.
74 
TEST(CavlcTest,CavlcParamCal_c)75 TEST (CavlcTest, CavlcParamCal_c) {
76   TestCavlcParamCal (CavlcParamCal_c);
77 }
78 
79 #ifdef X86_32_ASM
TEST(CavlcTest,CavlcParamCal_sse2)80 TEST (CavlcTest, CavlcParamCal_sse2) {
81   TestCavlcParamCal (CavlcParamCal_sse2);
82 }
83 #endif
84 
85 #ifdef X86_ASM
TEST(CavlcTest,CavlcParamCal_sse42)86 TEST (CavlcTest, CavlcParamCal_sse42) {
87   if (WelsCPUFeatureDetect (0) & WELS_CPU_SSE42)
88     TestCavlcParamCal (CavlcParamCal_sse42);
89 }
90 #endif
91