mirror of
https://github.com/ultravideo/uvg266.git
synced 2024-11-27 19:24:06 +00:00
292 lines
12 KiB
C
292 lines
12 KiB
C
|
/**
|
||
|
* HEVC Encoder
|
||
|
* - Marko Viitanen ( fador at iki.fi ), Tampere University of Technology, Department of Computer Systems.
|
||
|
*/
|
||
|
|
||
|
/*! \file transform.c
|
||
|
\brief Transform functions
|
||
|
\author Marko Viitanen
|
||
|
\date 2012-06
|
||
|
|
||
|
Transform functions
|
||
|
*/
|
||
|
#include <string.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include "global.h"
|
||
|
|
||
|
|
||
|
const int16_t g_aiT4[4][4] =
|
||
|
{
|
||
|
{ 64, 64, 64, 64},
|
||
|
{ 83, 36,-36,-83},
|
||
|
{ 64,-64,-64, 64},
|
||
|
{ 36,-83, 83,-36}
|
||
|
};
|
||
|
|
||
|
const int16_t g_aiT8[8][8] =
|
||
|
{
|
||
|
{ 64, 64, 64, 64, 64, 64, 64, 64},
|
||
|
{ 89, 75, 50, 18,-18,-50,-75,-89},
|
||
|
{ 83, 36,-36,-83,-83,-36, 36, 83},
|
||
|
{ 75,-18,-89,-50, 50, 89, 18,-75},
|
||
|
{ 64,-64,-64, 64, 64,-64,-64, 64},
|
||
|
{ 50,-89, 18, 75,-75,-18, 89,-50},
|
||
|
{ 36,-83, 83,-36,-36, 83,-83, 36},
|
||
|
{ 18,-50, 75,-89, 89,-75, 50,-18}
|
||
|
};
|
||
|
|
||
|
const int16_t g_aiT16[16][16] =
|
||
|
{
|
||
|
{ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
|
||
|
{ 90, 87, 80, 70, 57, 43, 25, 9, -9,-25,-43,-57,-70,-80,-87,-90},
|
||
|
{ 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89},
|
||
|
{ 87, 57, 9,-43,-80,-90,-70,-25, 25, 70, 90, 80, 43, -9,-57,-87},
|
||
|
{ 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83},
|
||
|
{ 80, 9,-70,-87,-25, 57, 90, 43,-43,-90,-57, 25, 87, 70, -9,-80},
|
||
|
{ 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75},
|
||
|
{ 70,-43,-87, 9, 90, 25,-80,-57, 57, 80,-25,-90, -9, 87, 43,-70},
|
||
|
{ 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64},
|
||
|
{ 57,-80,-25, 90, -9,-87, 43, 70,-70,-43, 87, 9,-90, 25, 80,-57},
|
||
|
{ 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50},
|
||
|
{ 43,-90, 57, 25,-87, 70, 9,-80, 80, -9,-70, 87,-25,-57, 90,-43},
|
||
|
{ 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36},
|
||
|
{ 25,-70, 90,-80, 43, 9,-57, 87,-87, 57, -9,-43, 80,-90, 70,-25},
|
||
|
{ 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18},
|
||
|
{ 9,-25, 43,-57, 70,-80, 87,-90, 90,-87, 80,-70, 57,-43, 25, -9}
|
||
|
};
|
||
|
|
||
|
const int16_t g_aiT32[32][32] =
|
||
|
{
|
||
|
{ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
|
||
|
{ 90, 90, 88, 85, 82, 78, 73, 67, 61, 54, 46, 38, 31, 22, 13, 4, -4,-13,-22,-31,-38,-46,-54,-61,-67,-73,-78,-82,-85,-88,-90,-90},
|
||
|
{ 90, 87, 80, 70, 57, 43, 25, 9, -9,-25,-43,-57,-70,-80,-87,-90,-90,-87,-80,-70,-57,-43,-25, -9, 9, 25, 43, 57, 70, 80, 87, 90},
|
||
|
{ 90, 82, 67, 46, 22, -4,-31,-54,-73,-85,-90,-88,-78,-61,-38,-13, 13, 38, 61, 78, 88, 90, 85, 73, 54, 31, 4,-22,-46,-67,-82,-90},
|
||
|
{ 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89, 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89},
|
||
|
{ 88, 67, 31,-13,-54,-82,-90,-78,-46, -4, 38, 73, 90, 85, 61, 22,-22,-61,-85,-90,-73,-38, 4, 46, 78, 90, 82, 54, 13,-31,-67,-88},
|
||
|
{ 87, 57, 9,-43,-80,-90,-70,-25, 25, 70, 90, 80, 43, -9,-57,-87,-87,-57, -9, 43, 80, 90, 70, 25,-25,-70,-90,-80,-43, 9, 57, 87},
|
||
|
{ 85, 46,-13,-67,-90,-73,-22, 38, 82, 88, 54, -4,-61,-90,-78,-31, 31, 78, 90, 61, 4,-54,-88,-82,-38, 22, 73, 90, 67, 13,-46,-85},
|
||
|
{ 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83},
|
||
|
{ 82, 22,-54,-90,-61, 13, 78, 85, 31,-46,-90,-67, 4, 73, 88, 38,-38,-88,-73, -4, 67, 90, 46,-31,-85,-78,-13, 61, 90, 54,-22,-82},
|
||
|
{ 80, 9,-70,-87,-25, 57, 90, 43,-43,-90,-57, 25, 87, 70, -9,-80,-80, -9, 70, 87, 25,-57,-90,-43, 43, 90, 57,-25,-87,-70, 9, 80},
|
||
|
{ 78, -4,-82,-73, 13, 85, 67,-22,-88,-61, 31, 90, 54,-38,-90,-46, 46, 90, 38,-54,-90,-31, 61, 88, 22,-67,-85,-13, 73, 82, 4,-78},
|
||
|
{ 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75, 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75},
|
||
|
{ 73,-31,-90,-22, 78, 67,-38,-90,-13, 82, 61,-46,-88, -4, 85, 54,-54,-85, 4, 88, 46,-61,-82, 13, 90, 38,-67,-78, 22, 90, 31,-73},
|
||
|
{ 70,-43,-87, 9, 90, 25,-80,-57, 57, 80,-25,-90, -9, 87, 43,-70,-70, 43, 87, -9,-90,-25, 80, 57,-57,-80, 25, 90, 9,-87,-43, 70},
|
||
|
{ 67,-54,-78, 38, 85,-22,-90, 4, 90, 13,-88,-31, 82, 46,-73,-61, 61, 73,-46,-82, 31, 88,-13,-90, -4, 90, 22,-85,-38, 78, 54,-67},
|
||
|
{ 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64},
|
||
|
{ 61,-73,-46, 82, 31,-88,-13, 90, -4,-90, 22, 85,-38,-78, 54, 67,-67,-54, 78, 38,-85,-22, 90, 4,-90, 13, 88,-31,-82, 46, 73,-61},
|
||
|
{ 57,-80,-25, 90, -9,-87, 43, 70,-70,-43, 87, 9,-90, 25, 80,-57,-57, 80, 25,-90, 9, 87,-43,-70, 70, 43,-87, -9, 90,-25,-80, 57},
|
||
|
{ 54,-85, -4, 88,-46,-61, 82, 13,-90, 38, 67,-78,-22, 90,-31,-73, 73, 31,-90, 22, 78,-67,-38, 90,-13,-82, 61, 46,-88, 4, 85,-54},
|
||
|
{ 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50, 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50},
|
||
|
{ 46,-90, 38, 54,-90, 31, 61,-88, 22, 67,-85, 13, 73,-82, 4, 78,-78, -4, 82,-73,-13, 85,-67,-22, 88,-61,-31, 90,-54,-38, 90,-46},
|
||
|
{ 43,-90, 57, 25,-87, 70, 9,-80, 80, -9,-70, 87,-25,-57, 90,-43,-43, 90,-57,-25, 87,-70, -9, 80,-80, 9, 70,-87, 25, 57,-90, 43},
|
||
|
{ 38,-88, 73, -4,-67, 90,-46,-31, 85,-78, 13, 61,-90, 54, 22,-82, 82,-22,-54, 90,-61,-13, 78,-85, 31, 46,-90, 67, 4,-73, 88,-38},
|
||
|
{ 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36},
|
||
|
{ 31,-78, 90,-61, 4, 54,-88, 82,-38,-22, 73,-90, 67,-13,-46, 85,-85, 46, 13,-67, 90,-73, 22, 38,-82, 88,-54, -4, 61,-90, 78,-31},
|
||
|
{ 25,-70, 90,-80, 43, 9,-57, 87,-87, 57, -9,-43, 80,-90, 70,-25,-25, 70,-90, 80,-43, -9, 57,-87, 87,-57, 9, 43,-80, 90,-70, 25},
|
||
|
{ 22,-61, 85,-90, 73,-38, -4, 46,-78, 90,-82, 54,-13,-31, 67,-88, 88,-67, 31, 13,-54, 82,-90, 78,-46, 4, 38,-73, 90,-85, 61,-22},
|
||
|
{ 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18, 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18},
|
||
|
{ 13,-38, 61,-78, 88,-90, 85,-73, 54,-31, 4, 22,-46, 67,-82, 90,-90, 82,-67, 46,-22, -4, 31,-54, 73,-85, 90,-88, 78,-61, 38,-13},
|
||
|
{ 9,-25, 43,-57, 70,-80, 87,-90, 90,-87, 80,-70, 57,-43, 25, -9, -9, 25,-43, 57,-70, 80,-87, 90,-90, 87,-80, 70,-57, 43,-25, 9},
|
||
|
{ 4,-13, 22,-31, 38,-46, 54,-61, 67,-73, 78,-82, 85,-88, 90,-90, 90,-90, 88,-85, 82,-78, 73,-67, 61,-54, 46,-38, 31,-22, 13, -4}
|
||
|
};
|
||
|
|
||
|
|
||
|
void partialButterfly4(short *src,short *dst,int shift, int line)
|
||
|
{
|
||
|
int j;
|
||
|
int E[2],O[2];
|
||
|
int add = 1<<(shift-1);
|
||
|
|
||
|
for (j=0; j<line; j++)
|
||
|
{
|
||
|
/* E and O */
|
||
|
E[0] = src[0] + src[3];
|
||
|
O[0] = src[0] - src[3];
|
||
|
E[1] = src[1] + src[2];
|
||
|
O[1] = src[1] - src[2];
|
||
|
|
||
|
dst[0] = (g_aiT4[0][0]*E[0] + g_aiT4[0][1]*E[1] + add)>>shift;
|
||
|
dst[2*line] = (g_aiT4[2][0]*E[0] + g_aiT4[2][1]*E[1] + add)>>shift;
|
||
|
dst[line] = (g_aiT4[1][0]*O[0] + g_aiT4[1][1]*O[1] + add)>>shift;
|
||
|
dst[3*line] = (g_aiT4[3][0]*O[0] + g_aiT4[3][1]*O[1] + add)>>shift;
|
||
|
|
||
|
src += 4;
|
||
|
dst ++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Fast DST Algorithm. Full matrix multiplication for DST and Fast DST algorithm
|
||
|
// give identical results
|
||
|
void fastForwardDst(short *block,short *coeff,int shift) // input block, output coeff
|
||
|
{
|
||
|
int i, c[4];
|
||
|
int rnd_factor = 1<<(shift-1);
|
||
|
for (i=0; i<4; i++)
|
||
|
{
|
||
|
// Intermediate Variables
|
||
|
c[0] = block[4*i+0] + block[4*i+3];
|
||
|
c[1] = block[4*i+1] + block[4*i+3];
|
||
|
c[2] = block[4*i+0] - block[4*i+1];
|
||
|
c[3] = 74* block[4*i+2];
|
||
|
|
||
|
coeff[ i] = ( 29 * c[0] + 55 * c[1] + c[3] + rnd_factor ) >> shift;
|
||
|
coeff[ 4+i] = ( 74 * (block[4*i+0]+ block[4*i+1] - block[4*i+3]) + rnd_factor ) >> shift;
|
||
|
coeff[ 8+i] = ( 29 * c[2] + 55 * c[0] - c[3] + rnd_factor ) >> shift;
|
||
|
coeff[12+i] = ( 55 * c[2] - 29 * c[1] + c[3] + rnd_factor ) >> shift;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void partialButterfly8(short *src,short *dst,int shift, int line)
|
||
|
{
|
||
|
int j,k;
|
||
|
int E[4],O[4];
|
||
|
int EE[2],EO[2];
|
||
|
int add = 1<<(shift-1);
|
||
|
|
||
|
for (j=0; j<line; j++)
|
||
|
{
|
||
|
/* E and O*/
|
||
|
for (k=0;k<4;k++)
|
||
|
{
|
||
|
E[k] = src[k] + src[7-k];
|
||
|
O[k] = src[k] - src[7-k];
|
||
|
}
|
||
|
/* EE and EO */
|
||
|
EE[0] = E[0] + E[3];
|
||
|
EO[0] = E[0] - E[3];
|
||
|
EE[1] = E[1] + E[2];
|
||
|
EO[1] = E[1] - E[2];
|
||
|
|
||
|
dst[0] = (g_aiT8[0][0]*EE[0] + g_aiT8[0][1]*EE[1] + add)>>shift;
|
||
|
dst[4*line] = (g_aiT8[4][0]*EE[0] + g_aiT8[4][1]*EE[1] + add)>>shift;
|
||
|
dst[2*line] = (g_aiT8[2][0]*EO[0] + g_aiT8[2][1]*EO[1] + add)>>shift;
|
||
|
dst[6*line] = (g_aiT8[6][0]*EO[0] + g_aiT8[6][1]*EO[1] + add)>>shift;
|
||
|
|
||
|
dst[line] = (g_aiT8[1][0]*O[0] + g_aiT8[1][1]*O[1] + g_aiT8[1][2]*O[2] + g_aiT8[1][3]*O[3] + add)>>shift;
|
||
|
dst[3*line] = (g_aiT8[3][0]*O[0] + g_aiT8[3][1]*O[1] + g_aiT8[3][2]*O[2] + g_aiT8[3][3]*O[3] + add)>>shift;
|
||
|
dst[5*line] = (g_aiT8[5][0]*O[0] + g_aiT8[5][1]*O[1] + g_aiT8[5][2]*O[2] + g_aiT8[5][3]*O[3] + add)>>shift;
|
||
|
dst[7*line] = (g_aiT8[7][0]*O[0] + g_aiT8[7][1]*O[1] + g_aiT8[7][2]*O[2] + g_aiT8[7][3]*O[3] + add)>>shift;
|
||
|
|
||
|
src += 8;
|
||
|
dst ++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void partialButterfly16(short *src,short *dst,int shift, int line)
|
||
|
{
|
||
|
int j,k;
|
||
|
int E[8],O[8];
|
||
|
int EE[4],EO[4];
|
||
|
int EEE[2],EEO[2];
|
||
|
int add = 1<<(shift-1);
|
||
|
|
||
|
for (j=0; j<line; j++)
|
||
|
{
|
||
|
/* E and O*/
|
||
|
for (k=0;k<8;k++)
|
||
|
{
|
||
|
E[k] = src[k] + src[15-k];
|
||
|
O[k] = src[k] - src[15-k];
|
||
|
}
|
||
|
/* EE and EO */
|
||
|
for (k=0;k<4;k++)
|
||
|
{
|
||
|
EE[k] = E[k] + E[7-k];
|
||
|
EO[k] = E[k] - E[7-k];
|
||
|
}
|
||
|
/* EEE and EEO */
|
||
|
EEE[0] = EE[0] + EE[3];
|
||
|
EEO[0] = EE[0] - EE[3];
|
||
|
EEE[1] = EE[1] + EE[2];
|
||
|
EEO[1] = EE[1] - EE[2];
|
||
|
|
||
|
dst[ 0 ] = (g_aiT16[ 0][0]*EEE[0] + g_aiT16[ 0][1]*EEE[1] + add)>>shift;
|
||
|
dst[ 8*line ] = (g_aiT16[ 8][0]*EEE[0] + g_aiT16[ 8][1]*EEE[1] + add)>>shift;
|
||
|
dst[ 4*line ] = (g_aiT16[ 4][0]*EEO[0] + g_aiT16[ 4][1]*EEO[1] + add)>>shift;
|
||
|
dst[ 12*line] = (g_aiT16[12][0]*EEO[0] + g_aiT16[12][1]*EEO[1] + add)>>shift;
|
||
|
|
||
|
for (k=2;k<16;k+=4)
|
||
|
{
|
||
|
dst[ k*line ] = (g_aiT16[k][0]*EO[0] + g_aiT16[k][1]*EO[1] + g_aiT16[k][2]*EO[2] + g_aiT16[k][3]*EO[3] + add)>>shift;
|
||
|
}
|
||
|
|
||
|
for (k=1;k<16;k+=2)
|
||
|
{
|
||
|
dst[ k*line ] = (g_aiT16[k][0]*O[0] + g_aiT16[k][1]*O[1] + g_aiT16[k][2]*O[2] + g_aiT16[k][3]*O[3] +
|
||
|
g_aiT16[k][4]*O[4] + g_aiT16[k][5]*O[5] + g_aiT16[k][6]*O[6] + g_aiT16[k][7]*O[7] + add)>>shift;
|
||
|
}
|
||
|
|
||
|
src += 16;
|
||
|
dst ++;
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void partialButterfly32(short *src,short *dst,int shift, int line)
|
||
|
{
|
||
|
int j,k;
|
||
|
int E[16],O[16];
|
||
|
int EE[8],EO[8];
|
||
|
int EEE[4],EEO[4];
|
||
|
int EEEE[2],EEEO[2];
|
||
|
int add = 1<<(shift-1);
|
||
|
|
||
|
for (j=0; j<line; j++)
|
||
|
{
|
||
|
/* E and O*/
|
||
|
for (k=0;k<16;k++)
|
||
|
{
|
||
|
E[k] = src[k] + src[31-k];
|
||
|
O[k] = src[k] - src[31-k];
|
||
|
}
|
||
|
/* EE and EO */
|
||
|
for (k=0;k<8;k++)
|
||
|
{
|
||
|
EE[k] = E[k] + E[15-k];
|
||
|
EO[k] = E[k] - E[15-k];
|
||
|
}
|
||
|
/* EEE and EEO */
|
||
|
for (k=0;k<4;k++)
|
||
|
{
|
||
|
EEE[k] = EE[k] + EE[7-k];
|
||
|
EEO[k] = EE[k] - EE[7-k];
|
||
|
}
|
||
|
/* EEEE and EEEO */
|
||
|
EEEE[0] = EEE[0] + EEE[3];
|
||
|
EEEO[0] = EEE[0] - EEE[3];
|
||
|
EEEE[1] = EEE[1] + EEE[2];
|
||
|
EEEO[1] = EEE[1] - EEE[2];
|
||
|
|
||
|
dst[ 0 ] = (g_aiT32[ 0][0]*EEEE[0] + g_aiT32[ 0][1]*EEEE[1] + add)>>shift;
|
||
|
dst[ 16*line ] = (g_aiT32[16][0]*EEEE[0] + g_aiT32[16][1]*EEEE[1] + add)>>shift;
|
||
|
dst[ 8*line ] = (g_aiT32[ 8][0]*EEEO[0] + g_aiT32[ 8][1]*EEEO[1] + add)>>shift;
|
||
|
dst[ 24*line ] = (g_aiT32[24][0]*EEEO[0] + g_aiT32[24][1]*EEEO[1] + add)>>shift;
|
||
|
for (k=4;k<32;k+=8)
|
||
|
{
|
||
|
dst[ k*line ] = (g_aiT32[k][0]*EEO[0] + g_aiT32[k][1]*EEO[1] + g_aiT32[k][2]*EEO[2] + g_aiT32[k][3]*EEO[3] + add)>>shift;
|
||
|
}
|
||
|
for (k=2;k<32;k+=4)
|
||
|
{
|
||
|
dst[ k*line ] = (g_aiT32[k][0]*EO[0] + g_aiT32[k][1]*EO[1] + g_aiT32[k][2]*EO[2] + g_aiT32[k][3]*EO[3] +
|
||
|
g_aiT32[k][4]*EO[4] + g_aiT32[k][5]*EO[5] + g_aiT32[k][6]*EO[6] + g_aiT32[k][7]*EO[7] + add)>>shift;
|
||
|
}
|
||
|
for (k=1;k<32;k+=2)
|
||
|
{
|
||
|
dst[ k*line ] = (g_aiT32[k][ 0]*O[ 0] + g_aiT32[k][ 1]*O[ 1] + g_aiT32[k][ 2]*O[ 2] + g_aiT32[k][ 3]*O[ 3] +
|
||
|
g_aiT32[k][ 4]*O[ 4] + g_aiT32[k][ 5]*O[ 5] + g_aiT32[k][ 6]*O[ 6] + g_aiT32[k][ 7]*O[ 7] +
|
||
|
g_aiT32[k][ 8]*O[ 8] + g_aiT32[k][ 9]*O[ 9] + g_aiT32[k][10]*O[10] + g_aiT32[k][11]*O[11] +
|
||
|
g_aiT32[k][12]*O[12] + g_aiT32[k][13]*O[13] + g_aiT32[k][14]*O[14] + g_aiT32[k][15]*O[15] + add)>>shift;
|
||
|
}
|
||
|
src += 32;
|
||
|
dst ++;
|
||
|
}
|
||
|
}
|