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 }
59 for (int i = 0; i < totalCoeffs_ref; i++)
60 ASSERT_EQ (level[i], level_ref[i]);
61 for (int i = 0; i < totalCoeffs_ref - 1; i++)
62 ASSERT_EQ (run[i], run_ref[i]);
63 }
64
TestCavlcParamCal(PCavlcParamCalFunc func)65 void TestCavlcParamCal (PCavlcParamCalFunc func) {
66 const int endIdxes[] = { 3, 14, 15 };
67 const int num_test_repetitions = 10000;
68 for (std::size_t i = 0; i < sizeof endIdxes / sizeof * endIdxes; i++) {
69 for (int count = 0; count < num_test_repetitions; count++)
70 TestCavlcParamCalWithEndIdx (func, endIdxes[i], count == 0, count == 1);
71 }
72 }
73
74 } // anon ns.
75
TEST(CavlcTest,CavlcParamCal_c)76 TEST (CavlcTest, CavlcParamCal_c) {
77 TestCavlcParamCal (CavlcParamCal_c);
78 }
79
80 #ifdef X86_32_ASM
TEST(CavlcTest,CavlcParamCal_sse2)81 TEST (CavlcTest, CavlcParamCal_sse2) {
82 TestCavlcParamCal (CavlcParamCal_sse2);
83 }
84 #endif
85
86 #ifdef X86_ASM
TEST(CavlcTest,CavlcParamCal_sse42)87 TEST (CavlcTest, CavlcParamCal_sse42) {
88 if (WelsCPUFeatureDetect (0) & WELS_CPU_SSE42)
89 TestCavlcParamCal (CavlcParamCal_sse42);
90 }
91 #endif
92