//======================================================================================= // // encoder of still images // /// vika.h // // developer: Henry Guennadi Levkin // //======================================================================================= #include #include #include //======================================================================================= typedef unsigned char byte; //======================================================================================= short reord4[16] = { 1, 2, 6, 7, 3, 5, 8, 13, 4, 9, 12, 14, 10, 11, 15, 16, }; short reord8[64] = { 1, 2, 6, 7, 15, 16, 28, 29, 3, 5, 8, 14, 17, 27, 30, 43, 4, 9, 13, 18, 26, 31, 42, 44, 10, 12, 19, 25, 32, 41, 45, 54, 11, 20, 24, 33, 40, 46, 53, 55, 21, 23, 34, 39, 47, 52, 56, 61, 22, 35, 38, 48, 51, 57, 60, 62, 36, 37, 49, 50, 58, 59, 63, 64 }; //======================================================================================= // transform matrices short hadam4[16]= { 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, }; short hadam8[64]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, }; short hadam16[256]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }; //======================================================================================= // quantazation table int gnQuantNumer[65] = { 1, 12, 19, 13, 17, 3, 5, 11, 2, 24, 19, 13, 17, 3, 10, 11, 4, 48, 19, 26, 17, 6, 20, 22, 8, 96, 19, 52, 34, 12, 40, 15, 16, 35, 19, 21, 23, 25, 27, 29, 32, 35, 38, 41, 45, 49, 53, 58, 64, 70, 76, 83, 90, 98, 107, 117, 128, 140, 153, 167, 181, 196, 213, 233, 256, }; int gnQuantDenom[65] = { 1, 11, 16, 10, 12, 2, 3, 6, 1, 11, 8, 5, 6, 1, 3, 3, 1, 11, 4, 5, 3, 1, 3, 3, 1, 11, 2, 5, 3, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }; //============================================================================= double CalculateSNR(byte* pFrame1, byte* pFrame2, int nWidth, int nHeight) { int i; short delta; int nSize = nWidth*nHeight; double fSNR = 0.; for(i=0; i 0) { // send out a bit '1' bitCounter++; gOpposite_bits--; } } else if( gLow >= gQ2 ) { // send out a bit '1' bitCounter++; while(gOpposite_bits > 0) { // send out a bit '0' bitCounter++; gOpposite_bits--; } gLow -= gQ2; gHigh -= gQ2; } else if(gLow>=gQ1 && gHigh < gQ3) { gOpposite_bits++; gLow -= gQ1; gHigh -= gQ1; } else break; gLow *= 2; gHigh = 2*gHigh + 1; } } void ArithEncoderReset() { gLow = 0; gHigh = gTop; gOpposite_bits = 0; bitCounter = 0; } //======================================================================================= double EntropyCalculation(int dim, int* pHist) { double fEntropy= 0.; double x; int nn = 0; int i; for(i=0; i255) pMat3[i] =255; else pMat3[i] = r; } } //============================================================================= int CalcSAD(byte* pMatr1, byte* pMatr2, int blkDim) { int i; int SAD = 0; for(i=0; i255) acc = 255; pChromaB[i] = acc; acc = 0.713*pRed[i] - 0.713*pLuma[i] + 128; if(acc<0) acc = 0; else if(acc>255) acc = 255; pChromaR[i] = acc; } } //======================================================================================= // 4:4:4 void TransformLuCbCrToRGB(byte* pLuma, byte* pChromaB, byte* pChromaR, byte* pRed, byte* pGreen, byte* pBlue, int nWidth, int nHeight) { int i; int size = nWidth * nHeight; int acc; for(i=0; i255) acc = 255; pRed[i] = acc; acc = (double) pLuma[i] - 0.344*(pChromaB[i]-128.) - 0.714*(pChromaR[i]-128.); if(acc<0) acc = 0; else if(acc>255) acc = 255; pGreen[i] = acc; acc = (double) pLuma[i] + 1.772*(pChromaB[i]-128.); if(acc<0) acc = 0; else if(acc>255) acc = 255; pBlue[i] = acc; } }