mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-13 06:24:27 +02:00
create a separate libwebpdsp under src/dsp
Gathers all DSP-related function (and SSE2 implementations). Clean-up some unwanted symbolic dependencies so that webp_encode, webp_decode and webp_dsp are truly independent libraries. + opportunistic clean-up: * remove unneeded VP8DspInitTables(), now integrated in VP8DspInit() * make consistent use of VP8GetCPUInfo() in the various DspInit() funcs * change OUT macro to DST
This commit is contained in:
committed by
James Zern
parent
ebeb412aa5
commit
e06ac0887f
14
src/dsp/Makefile.am
Normal file
14
src/dsp/Makefile.am
Normal file
@ -0,0 +1,14 @@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/src
|
||||
|
||||
libwebpdsp_la_SOURCES = dsp.h cpu.c \
|
||||
enc.c enc_sse2.c \
|
||||
dec.c dec_sse2.c \
|
||||
upsampling.c upsampling_sse2.c \
|
||||
yuv.h yuv.c
|
||||
libwebpdsp_la_LDFLAGS = -version-info 0:0:0 -lm
|
||||
libwebpdsp_la_CPPFLAGS = $(USE_EXPERIMENTAL_CODE)
|
||||
libwebpdspinclude_HEADERS = ../webp/types.h
|
||||
libwebpdspincludedir = $(includedir)/webp
|
||||
|
||||
noinst_HEADERS = dsp.h yuv.h
|
||||
noinst_LTLIBRARIES = libwebpdsp.la
|
61
src/dsp/cpu.c
Normal file
61
src/dsp/cpu.c
Normal file
@ -0,0 +1,61 @@
|
||||
// Copyright 2011 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// CPU detection
|
||||
//
|
||||
// Author: Christian Duvivier (cduvivier@google.com)
|
||||
|
||||
#include "./dsp.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// SSE2 detection.
|
||||
//
|
||||
|
||||
#if defined(__pic__) && defined(__i386__)
|
||||
static inline void GetCPUInfo(int cpu_info[4], int info_type) {
|
||||
__asm__ volatile (
|
||||
"mov %%ebx, %%edi\n"
|
||||
"cpuid\n"
|
||||
"xchg %%edi, %%ebx\n"
|
||||
: "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
|
||||
: "a"(info_type));
|
||||
}
|
||||
#elif defined(__i386__) || defined(__x86_64__)
|
||||
static inline void GetCPUInfo(int cpu_info[4], int info_type) {
|
||||
__asm__ volatile (
|
||||
"cpuid\n"
|
||||
: "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
|
||||
: "a"(info_type));
|
||||
}
|
||||
#elif defined(_MSC_VER) // Visual C++
|
||||
#define GetCPUInfo __cpuid
|
||||
#endif
|
||||
|
||||
#if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER)
|
||||
static int x86CPUInfo(CPUFeature feature) {
|
||||
int cpu_info[4];
|
||||
GetCPUInfo(cpu_info, 1);
|
||||
if (feature == kSSE2) {
|
||||
return 0 != (cpu_info[3] & 0x04000000);
|
||||
}
|
||||
if (feature == kSSE3) {
|
||||
return 0 != (cpu_info[2] & 0x00000001);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
VP8CPUInfo VP8GetCPUInfo = x86CPUInfo;
|
||||
#else
|
||||
VP8CPUInfo VP8GetCPUInfo = NULL;
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
724
src/dsp/dec.c
Normal file
724
src/dsp/dec.c
Normal file
@ -0,0 +1,724 @@
|
||||
// Copyright 2010 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Speed-critical decoding functions.
|
||||
//
|
||||
// Author: Skal (pascal.massimino@gmail.com)
|
||||
|
||||
#include "./dsp.h"
|
||||
#include "../dec/vp8i.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// run-time tables (~4k)
|
||||
|
||||
static uint8_t abs0[255 + 255 + 1]; // abs(i)
|
||||
static uint8_t abs1[255 + 255 + 1]; // abs(i)>>1
|
||||
static int8_t sclip1[1020 + 1020 + 1]; // clips [-1020, 1020] to [-128, 127]
|
||||
static int8_t sclip2[112 + 112 + 1]; // clips [-112, 112] to [-16, 15]
|
||||
static uint8_t clip1[255 + 510 + 1]; // clips [-255,510] to [0,255]
|
||||
|
||||
// We declare this variable 'volatile' to prevent instruction reordering
|
||||
// and make sure it's set to true _last_ (so as to be thread-safe)
|
||||
static volatile int tables_ok = 0;
|
||||
|
||||
static void DspInitTables(void) {
|
||||
if (!tables_ok) {
|
||||
int i;
|
||||
for (i = -255; i <= 255; ++i) {
|
||||
abs0[255 + i] = (i < 0) ? -i : i;
|
||||
abs1[255 + i] = abs0[255 + i] >> 1;
|
||||
}
|
||||
for (i = -1020; i <= 1020; ++i) {
|
||||
sclip1[1020 + i] = (i < -128) ? -128 : (i > 127) ? 127 : i;
|
||||
}
|
||||
for (i = -112; i <= 112; ++i) {
|
||||
sclip2[112 + i] = (i < -16) ? -16 : (i > 15) ? 15 : i;
|
||||
}
|
||||
for (i = -255; i <= 255 + 255; ++i) {
|
||||
clip1[255 + i] = (i < 0) ? 0 : (i > 255) ? 255 : i;
|
||||
}
|
||||
tables_ok = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint8_t clip_8b(int v) {
|
||||
return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Transforms (Paragraph 14.4)
|
||||
|
||||
#define STORE(x, y, v) \
|
||||
dst[x + y * BPS] = clip_8b(dst[x + y * BPS] + ((v) >> 3))
|
||||
|
||||
static const int kC1 = 20091 + (1 << 16);
|
||||
static const int kC2 = 35468;
|
||||
#define MUL(a, b) (((a) * (b)) >> 16)
|
||||
|
||||
static void TransformOne(const int16_t* in, uint8_t* dst) {
|
||||
int C[4 * 4], *tmp;
|
||||
int i;
|
||||
tmp = C;
|
||||
for (i = 0; i < 4; ++i) { // vertical pass
|
||||
const int a = in[0] + in[8]; // [-4096, 4094]
|
||||
const int b = in[0] - in[8]; // [-4095, 4095]
|
||||
const int c = MUL(in[4], kC2) - MUL(in[12], kC1); // [-3783, 3783]
|
||||
const int d = MUL(in[4], kC1) + MUL(in[12], kC2); // [-3785, 3781]
|
||||
tmp[0] = a + d; // [-7881, 7875]
|
||||
tmp[1] = b + c; // [-7878, 7878]
|
||||
tmp[2] = b - c; // [-7878, 7878]
|
||||
tmp[3] = a - d; // [-7877, 7879]
|
||||
tmp += 4;
|
||||
in++;
|
||||
}
|
||||
// Each pass is expanding the dynamic range by ~3.85 (upper bound).
|
||||
// The exact value is (2. + (kC1 + kC2) / 65536).
|
||||
// After the second pass, maximum interval is [-3794, 3794], assuming
|
||||
// an input in [-2048, 2047] interval. We then need to add a dst value
|
||||
// in the [0, 255] range.
|
||||
// In the worst case scenario, the input to clip_8b() can be as large as
|
||||
// [-60713, 60968].
|
||||
tmp = C;
|
||||
for (i = 0; i < 4; ++i) { // horizontal pass
|
||||
const int dc = tmp[0] + 4;
|
||||
const int a = dc + tmp[8];
|
||||
const int b = dc - tmp[8];
|
||||
const int c = MUL(tmp[4], kC2) - MUL(tmp[12], kC1);
|
||||
const int d = MUL(tmp[4], kC1) + MUL(tmp[12], kC2);
|
||||
STORE(0, 0, a + d);
|
||||
STORE(1, 0, b + c);
|
||||
STORE(2, 0, b - c);
|
||||
STORE(3, 0, a - d);
|
||||
tmp++;
|
||||
dst += BPS;
|
||||
}
|
||||
}
|
||||
#undef MUL
|
||||
|
||||
static void TransformTwo(const int16_t* in, uint8_t* dst, int do_two) {
|
||||
TransformOne(in, dst);
|
||||
if (do_two) {
|
||||
TransformOne(in + 16, dst + 4);
|
||||
}
|
||||
}
|
||||
|
||||
static void TransformUV(const int16_t* in, uint8_t* dst) {
|
||||
VP8Transform(in + 0 * 16, dst, 1);
|
||||
VP8Transform(in + 2 * 16, dst + 4 * BPS, 1);
|
||||
}
|
||||
|
||||
static void TransformDC(const int16_t *in, uint8_t* dst) {
|
||||
const int DC = in[0] + 4;
|
||||
int i, j;
|
||||
for (j = 0; j < 4; ++j) {
|
||||
for (i = 0; i < 4; ++i) {
|
||||
STORE(i, j, DC);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void TransformDCUV(const int16_t* in, uint8_t* dst) {
|
||||
if (in[0 * 16]) TransformDC(in + 0 * 16, dst);
|
||||
if (in[1 * 16]) TransformDC(in + 1 * 16, dst + 4);
|
||||
if (in[2 * 16]) TransformDC(in + 2 * 16, dst + 4 * BPS);
|
||||
if (in[3 * 16]) TransformDC(in + 3 * 16, dst + 4 * BPS + 4);
|
||||
}
|
||||
|
||||
#undef STORE
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Paragraph 14.3
|
||||
|
||||
static void TransformWHT(const int16_t* in, int16_t* out) {
|
||||
int tmp[16];
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
const int a0 = in[0 + i] + in[12 + i];
|
||||
const int a1 = in[4 + i] + in[ 8 + i];
|
||||
const int a2 = in[4 + i] - in[ 8 + i];
|
||||
const int a3 = in[0 + i] - in[12 + i];
|
||||
tmp[0 + i] = a0 + a1;
|
||||
tmp[8 + i] = a0 - a1;
|
||||
tmp[4 + i] = a3 + a2;
|
||||
tmp[12 + i] = a3 - a2;
|
||||
}
|
||||
for (i = 0; i < 4; ++i) {
|
||||
const int dc = tmp[0 + i * 4] + 3; // w/ rounder
|
||||
const int a0 = dc + tmp[3 + i * 4];
|
||||
const int a1 = tmp[1 + i * 4] + tmp[2 + i * 4];
|
||||
const int a2 = tmp[1 + i * 4] - tmp[2 + i * 4];
|
||||
const int a3 = dc - tmp[3 + i * 4];
|
||||
out[ 0] = (a0 + a1) >> 3;
|
||||
out[16] = (a3 + a2) >> 3;
|
||||
out[32] = (a0 - a1) >> 3;
|
||||
out[48] = (a3 - a2) >> 3;
|
||||
out += 64;
|
||||
}
|
||||
}
|
||||
|
||||
void (*VP8TransformWHT)(const int16_t* in, int16_t* out) = TransformWHT;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Intra predictions
|
||||
|
||||
#define DST(x, y) dst[(x) + (y) * BPS]
|
||||
|
||||
static inline void TrueMotion(uint8_t *dst, int size) {
|
||||
const uint8_t* top = dst - BPS;
|
||||
const uint8_t* const clip0 = clip1 + 255 - top[-1];
|
||||
int y;
|
||||
for (y = 0; y < size; ++y) {
|
||||
const uint8_t* const clip = clip0 + dst[-1];
|
||||
int x;
|
||||
for (x = 0; x < size; ++x) {
|
||||
dst[x] = clip[top[x]];
|
||||
}
|
||||
dst += BPS;
|
||||
}
|
||||
}
|
||||
static void TM4(uint8_t *dst) { TrueMotion(dst, 4); }
|
||||
static void TM8uv(uint8_t *dst) { TrueMotion(dst, 8); }
|
||||
static void TM16(uint8_t *dst) { TrueMotion(dst, 16); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// 16x16
|
||||
|
||||
static void VE16(uint8_t *dst) { // vertical
|
||||
int j;
|
||||
for (j = 0; j < 16; ++j) {
|
||||
memcpy(dst + j * BPS, dst - BPS, 16);
|
||||
}
|
||||
}
|
||||
|
||||
static void HE16(uint8_t *dst) { // horizontal
|
||||
int j;
|
||||
for (j = 16; j > 0; --j) {
|
||||
memset(dst, dst[-1], 16);
|
||||
dst += BPS;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void Put16(int v, uint8_t* dst) {
|
||||
int j;
|
||||
for (j = 0; j < 16; ++j) {
|
||||
memset(dst + j * BPS, v, 16);
|
||||
}
|
||||
}
|
||||
|
||||
static void DC16(uint8_t *dst) { // DC
|
||||
int DC = 16;
|
||||
int j;
|
||||
for (j = 0; j < 16; ++j) {
|
||||
DC += dst[-1 + j * BPS] + dst[j - BPS];
|
||||
}
|
||||
Put16(DC >> 5, dst);
|
||||
}
|
||||
|
||||
static void DC16NoTop(uint8_t *dst) { // DC with top samples not available
|
||||
int DC = 8;
|
||||
int j;
|
||||
for (j = 0; j < 16; ++j) {
|
||||
DC += dst[-1 + j * BPS];
|
||||
}
|
||||
Put16(DC >> 4, dst);
|
||||
}
|
||||
|
||||
static void DC16NoLeft(uint8_t *dst) { // DC with left samples not available
|
||||
int DC = 8;
|
||||
int i;
|
||||
for (i = 0; i < 16; ++i) {
|
||||
DC += dst[i - BPS];
|
||||
}
|
||||
Put16(DC >> 4, dst);
|
||||
}
|
||||
|
||||
static void DC16NoTopLeft(uint8_t *dst) { // DC with no top and left samples
|
||||
Put16(0x80, dst);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// 4x4
|
||||
|
||||
#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
|
||||
#define AVG2(a, b) (((a) + (b) + 1) >> 1)
|
||||
|
||||
static void VE4(uint8_t *dst) { // vertical
|
||||
const uint8_t* top = dst - BPS;
|
||||
const uint8_t vals[4] = {
|
||||
AVG3(top[-1], top[0], top[1]),
|
||||
AVG3(top[ 0], top[1], top[2]),
|
||||
AVG3(top[ 1], top[2], top[3]),
|
||||
AVG3(top[ 2], top[3], top[4])
|
||||
};
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
memcpy(dst + i * BPS, vals, sizeof(vals));
|
||||
}
|
||||
}
|
||||
|
||||
static void HE4(uint8_t *dst) { // horizontal
|
||||
const int A = dst[-1 - BPS];
|
||||
const int B = dst[-1];
|
||||
const int C = dst[-1 + BPS];
|
||||
const int D = dst[-1 + 2 * BPS];
|
||||
const int E = dst[-1 + 3 * BPS];
|
||||
*(uint32_t*)(dst + 0 * BPS) = 0x01010101U * AVG3(A, B, C);
|
||||
*(uint32_t*)(dst + 1 * BPS) = 0x01010101U * AVG3(B, C, D);
|
||||
*(uint32_t*)(dst + 2 * BPS) = 0x01010101U * AVG3(C, D, E);
|
||||
*(uint32_t*)(dst + 3 * BPS) = 0x01010101U * AVG3(D, E, E);
|
||||
}
|
||||
|
||||
static void DC4(uint8_t *dst) { // DC
|
||||
uint32_t dc = 4;
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i) dc += dst[i - BPS] + dst[-1 + i * BPS];
|
||||
dc >>= 3;
|
||||
for (i = 0; i < 4; ++i) memset(dst + i * BPS, dc, 4);
|
||||
}
|
||||
|
||||
static void RD4(uint8_t *dst) { // Down-right
|
||||
const int I = dst[-1 + 0 * BPS];
|
||||
const int J = dst[-1 + 1 * BPS];
|
||||
const int K = dst[-1 + 2 * BPS];
|
||||
const int L = dst[-1 + 3 * BPS];
|
||||
const int X = dst[-1 - BPS];
|
||||
const int A = dst[0 - BPS];
|
||||
const int B = dst[1 - BPS];
|
||||
const int C = dst[2 - BPS];
|
||||
const int D = dst[3 - BPS];
|
||||
DST(0, 3) = AVG3(J, K, L);
|
||||
DST(0, 2) = DST(1, 3) = AVG3(I, J, K);
|
||||
DST(0, 1) = DST(1, 2) = DST(2, 3) = AVG3(X, I, J);
|
||||
DST(0, 0) = DST(1, 1) = DST(2, 2) = DST(3, 3) = AVG3(A, X, I);
|
||||
DST(1, 0) = DST(2, 1) = DST(3, 2) = AVG3(B, A, X);
|
||||
DST(2, 0) = DST(3, 1) = AVG3(C, B, A);
|
||||
DST(3, 0) = AVG3(D, C, B);
|
||||
}
|
||||
|
||||
static void LD4(uint8_t *dst) { // Down-Left
|
||||
const int A = dst[0 - BPS];
|
||||
const int B = dst[1 - BPS];
|
||||
const int C = dst[2 - BPS];
|
||||
const int D = dst[3 - BPS];
|
||||
const int E = dst[4 - BPS];
|
||||
const int F = dst[5 - BPS];
|
||||
const int G = dst[6 - BPS];
|
||||
const int H = dst[7 - BPS];
|
||||
DST(0, 0) = AVG3(A, B, C);
|
||||
DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
|
||||
DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
|
||||
DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
|
||||
DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
|
||||
DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
|
||||
DST(3, 3) = AVG3(G, H, H);
|
||||
}
|
||||
|
||||
static void VR4(uint8_t *dst) { // Vertical-Right
|
||||
const int I = dst[-1 + 0 * BPS];
|
||||
const int J = dst[-1 + 1 * BPS];
|
||||
const int K = dst[-1 + 2 * BPS];
|
||||
const int X = dst[-1 - BPS];
|
||||
const int A = dst[0 - BPS];
|
||||
const int B = dst[1 - BPS];
|
||||
const int C = dst[2 - BPS];
|
||||
const int D = dst[3 - BPS];
|
||||
DST(0, 0) = DST(1, 2) = AVG2(X, A);
|
||||
DST(1, 0) = DST(2, 2) = AVG2(A, B);
|
||||
DST(2, 0) = DST(3, 2) = AVG2(B, C);
|
||||
DST(3, 0) = AVG2(C, D);
|
||||
|
||||
DST(0, 3) = AVG3(K, J, I);
|
||||
DST(0, 2) = AVG3(J, I, X);
|
||||
DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
|
||||
DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
|
||||
DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
|
||||
DST(3, 1) = AVG3(B, C, D);
|
||||
}
|
||||
|
||||
static void VL4(uint8_t *dst) { // Vertical-Left
|
||||
const int A = dst[0 - BPS];
|
||||
const int B = dst[1 - BPS];
|
||||
const int C = dst[2 - BPS];
|
||||
const int D = dst[3 - BPS];
|
||||
const int E = dst[4 - BPS];
|
||||
const int F = dst[5 - BPS];
|
||||
const int G = dst[6 - BPS];
|
||||
const int H = dst[7 - BPS];
|
||||
DST(0, 0) = AVG2(A, B);
|
||||
DST(1, 0) = DST(0, 2) = AVG2(B, C);
|
||||
DST(2, 0) = DST(1, 2) = AVG2(C, D);
|
||||
DST(3, 0) = DST(2, 2) = AVG2(D, E);
|
||||
|
||||
DST(0, 1) = AVG3(A, B, C);
|
||||
DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
|
||||
DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
|
||||
DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
|
||||
DST(3, 2) = AVG3(E, F, G);
|
||||
DST(3, 3) = AVG3(F, G, H);
|
||||
}
|
||||
|
||||
static void HU4(uint8_t *dst) { // Horizontal-Up
|
||||
const int I = dst[-1 + 0 * BPS];
|
||||
const int J = dst[-1 + 1 * BPS];
|
||||
const int K = dst[-1 + 2 * BPS];
|
||||
const int L = dst[-1 + 3 * BPS];
|
||||
DST(0, 0) = AVG2(I, J);
|
||||
DST(2, 0) = DST(0, 1) = AVG2(J, K);
|
||||
DST(2, 1) = DST(0, 2) = AVG2(K, L);
|
||||
DST(1, 0) = AVG3(I, J, K);
|
||||
DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
|
||||
DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
|
||||
DST(3, 2) = DST(2, 2) =
|
||||
DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
|
||||
}
|
||||
|
||||
static void HD4(uint8_t *dst) { // Horizontal-Down
|
||||
const int I = dst[-1 + 0 * BPS];
|
||||
const int J = dst[-1 + 1 * BPS];
|
||||
const int K = dst[-1 + 2 * BPS];
|
||||
const int L = dst[-1 + 3 * BPS];
|
||||
const int X = dst[-1 - BPS];
|
||||
const int A = dst[0 - BPS];
|
||||
const int B = dst[1 - BPS];
|
||||
const int C = dst[2 - BPS];
|
||||
|
||||
DST(0, 0) = DST(2, 1) = AVG2(I, X);
|
||||
DST(0, 1) = DST(2, 2) = AVG2(J, I);
|
||||
DST(0, 2) = DST(2, 3) = AVG2(K, J);
|
||||
DST(0, 3) = AVG2(L, K);
|
||||
|
||||
DST(3, 0) = AVG3(A, B, C);
|
||||
DST(2, 0) = AVG3(X, A, B);
|
||||
DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
|
||||
DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
|
||||
DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
|
||||
DST(1, 3) = AVG3(L, K, J);
|
||||
}
|
||||
|
||||
#undef DST
|
||||
#undef AVG3
|
||||
#undef AVG2
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Chroma
|
||||
|
||||
static void VE8uv(uint8_t *dst) { // vertical
|
||||
int j;
|
||||
for (j = 0; j < 8; ++j) {
|
||||
memcpy(dst + j * BPS, dst - BPS, 8);
|
||||
}
|
||||
}
|
||||
|
||||
static void HE8uv(uint8_t *dst) { // horizontal
|
||||
int j;
|
||||
for (j = 0; j < 8; ++j) {
|
||||
memset(dst, dst[-1], 8);
|
||||
dst += BPS;
|
||||
}
|
||||
}
|
||||
|
||||
// helper for chroma-DC predictions
|
||||
static inline void Put8x8uv(uint64_t v, uint8_t* dst) {
|
||||
int j;
|
||||
for (j = 0; j < 8; ++j) {
|
||||
*(uint64_t*)(dst + j * BPS) = v;
|
||||
}
|
||||
}
|
||||
|
||||
static void DC8uv(uint8_t *dst) { // DC
|
||||
int dc0 = 8;
|
||||
int i;
|
||||
for (i = 0; i < 8; ++i) {
|
||||
dc0 += dst[i - BPS] + dst[-1 + i * BPS];
|
||||
}
|
||||
Put8x8uv((uint64_t)((dc0 >> 4) * 0x0101010101010101ULL), dst);
|
||||
}
|
||||
|
||||
static void DC8uvNoLeft(uint8_t *dst) { // DC with no left samples
|
||||
int dc0 = 4;
|
||||
int i;
|
||||
for (i = 0; i < 8; ++i) {
|
||||
dc0 += dst[i - BPS];
|
||||
}
|
||||
Put8x8uv((uint64_t)((dc0 >> 3) * 0x0101010101010101ULL), dst);
|
||||
}
|
||||
|
||||
static void DC8uvNoTop(uint8_t *dst) { // DC with no top samples
|
||||
int dc0 = 4;
|
||||
int i;
|
||||
for (i = 0; i < 8; ++i) {
|
||||
dc0 += dst[-1 + i * BPS];
|
||||
}
|
||||
Put8x8uv((uint64_t)((dc0 >> 3) * 0x0101010101010101ULL), dst);
|
||||
}
|
||||
|
||||
static void DC8uvNoTopLeft(uint8_t *dst) { // DC with nothing
|
||||
Put8x8uv(0x8080808080808080ULL, dst);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// default C implementations
|
||||
|
||||
VP8PredFunc VP8PredLuma4[/* NUM_BMODES */] = {
|
||||
DC4, TM4, VE4, HE4, RD4, VR4, LD4, VL4, HD4, HU4
|
||||
};
|
||||
|
||||
VP8PredFunc VP8PredLuma16[/*NUM_B_DC_MODES */] = {
|
||||
DC16, TM16, VE16, HE16,
|
||||
DC16NoTop, DC16NoLeft, DC16NoTopLeft
|
||||
};
|
||||
|
||||
VP8PredFunc VP8PredChroma8[/*NUM_B_DC_MODES */] = {
|
||||
DC8uv, TM8uv, VE8uv, HE8uv,
|
||||
DC8uvNoTop, DC8uvNoLeft, DC8uvNoTopLeft
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Edge filtering functions
|
||||
|
||||
// 4 pixels in, 2 pixels out
|
||||
static inline void do_filter2(uint8_t* p, int step) {
|
||||
const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
|
||||
const int a = 3 * (q0 - p0) + sclip1[1020 + p1 - q1];
|
||||
const int a1 = sclip2[112 + ((a + 4) >> 3)];
|
||||
const int a2 = sclip2[112 + ((a + 3) >> 3)];
|
||||
p[-step] = clip1[255 + p0 + a2];
|
||||
p[ 0] = clip1[255 + q0 - a1];
|
||||
}
|
||||
|
||||
// 4 pixels in, 4 pixels out
|
||||
static inline void do_filter4(uint8_t* p, int step) {
|
||||
const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
|
||||
const int a = 3 * (q0 - p0);
|
||||
const int a1 = sclip2[112 + ((a + 4) >> 3)];
|
||||
const int a2 = sclip2[112 + ((a + 3) >> 3)];
|
||||
const int a3 = (a1 + 1) >> 1;
|
||||
p[-2*step] = clip1[255 + p1 + a3];
|
||||
p[- step] = clip1[255 + p0 + a2];
|
||||
p[ 0] = clip1[255 + q0 - a1];
|
||||
p[ step] = clip1[255 + q1 - a3];
|
||||
}
|
||||
|
||||
// 6 pixels in, 6 pixels out
|
||||
static inline void do_filter6(uint8_t* p, int step) {
|
||||
const int p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
|
||||
const int q0 = p[0], q1 = p[step], q2 = p[2*step];
|
||||
const int a = sclip1[1020 + 3 * (q0 - p0) + sclip1[1020 + p1 - q1]];
|
||||
const int a1 = (27 * a + 63) >> 7; // eq. to ((3 * a + 7) * 9) >> 7
|
||||
const int a2 = (18 * a + 63) >> 7; // eq. to ((2 * a + 7) * 9) >> 7
|
||||
const int a3 = (9 * a + 63) >> 7; // eq. to ((1 * a + 7) * 9) >> 7
|
||||
p[-3*step] = clip1[255 + p2 + a3];
|
||||
p[-2*step] = clip1[255 + p1 + a2];
|
||||
p[- step] = clip1[255 + p0 + a1];
|
||||
p[ 0] = clip1[255 + q0 - a1];
|
||||
p[ step] = clip1[255 + q1 - a2];
|
||||
p[ 2*step] = clip1[255 + q2 - a3];
|
||||
}
|
||||
|
||||
static inline int hev(const uint8_t* p, int step, int thresh) {
|
||||
const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
|
||||
return (abs0[255 + p1 - p0] > thresh) || (abs0[255 + q1 - q0] > thresh);
|
||||
}
|
||||
|
||||
static inline int needs_filter(const uint8_t* p, int step, int thresh) {
|
||||
const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
|
||||
return (2 * abs0[255 + p0 - q0] + abs1[255 + p1 - q1]) <= thresh;
|
||||
}
|
||||
|
||||
static inline int needs_filter2(const uint8_t* p, int step, int t, int it) {
|
||||
const int p3 = p[-4*step], p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
|
||||
const int q0 = p[0], q1 = p[step], q2 = p[2*step], q3 = p[3*step];
|
||||
if ((2 * abs0[255 + p0 - q0] + abs1[255 + p1 - q1]) > t)
|
||||
return 0;
|
||||
return abs0[255 + p3 - p2] <= it && abs0[255 + p2 - p1] <= it &&
|
||||
abs0[255 + p1 - p0] <= it && abs0[255 + q3 - q2] <= it &&
|
||||
abs0[255 + q2 - q1] <= it && abs0[255 + q1 - q0] <= it;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Simple In-loop filtering (Paragraph 15.2)
|
||||
|
||||
static void SimpleVFilter16(uint8_t* p, int stride, int thresh) {
|
||||
int i;
|
||||
for (i = 0; i < 16; ++i) {
|
||||
if (needs_filter(p + i, stride, thresh)) {
|
||||
do_filter2(p + i, stride);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void SimpleHFilter16(uint8_t* p, int stride, int thresh) {
|
||||
int i;
|
||||
for (i = 0; i < 16; ++i) {
|
||||
if (needs_filter(p + i * stride, 1, thresh)) {
|
||||
do_filter2(p + i * stride, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void SimpleVFilter16i(uint8_t* p, int stride, int thresh) {
|
||||
int k;
|
||||
for (k = 3; k > 0; --k) {
|
||||
p += 4 * stride;
|
||||
SimpleVFilter16(p, stride, thresh);
|
||||
}
|
||||
}
|
||||
|
||||
static void SimpleHFilter16i(uint8_t* p, int stride, int thresh) {
|
||||
int k;
|
||||
for (k = 3; k > 0; --k) {
|
||||
p += 4;
|
||||
SimpleHFilter16(p, stride, thresh);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Complex In-loop filtering (Paragraph 15.3)
|
||||
|
||||
static inline void FilterLoop26(uint8_t* p, int hstride, int vstride, int size,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
while (size-- > 0) {
|
||||
if (needs_filter2(p, hstride, thresh, ithresh)) {
|
||||
if (hev(p, hstride, hev_thresh)) {
|
||||
do_filter2(p, hstride);
|
||||
} else {
|
||||
do_filter6(p, hstride);
|
||||
}
|
||||
}
|
||||
p += vstride;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void FilterLoop24(uint8_t* p, int hstride, int vstride, int size,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
while (size-- > 0) {
|
||||
if (needs_filter2(p, hstride, thresh, ithresh)) {
|
||||
if (hev(p, hstride, hev_thresh)) {
|
||||
do_filter2(p, hstride);
|
||||
} else {
|
||||
do_filter4(p, hstride);
|
||||
}
|
||||
}
|
||||
p += vstride;
|
||||
}
|
||||
}
|
||||
|
||||
// on macroblock edges
|
||||
static void VFilter16(uint8_t* p, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
FilterLoop26(p, stride, 1, 16, thresh, ithresh, hev_thresh);
|
||||
}
|
||||
|
||||
static void HFilter16(uint8_t* p, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
FilterLoop26(p, 1, stride, 16, thresh, ithresh, hev_thresh);
|
||||
}
|
||||
|
||||
// on three inner edges
|
||||
static void VFilter16i(uint8_t* p, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
int k;
|
||||
for (k = 3; k > 0; --k) {
|
||||
p += 4 * stride;
|
||||
FilterLoop24(p, stride, 1, 16, thresh, ithresh, hev_thresh);
|
||||
}
|
||||
}
|
||||
|
||||
static void HFilter16i(uint8_t* p, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
int k;
|
||||
for (k = 3; k > 0; --k) {
|
||||
p += 4;
|
||||
FilterLoop24(p, 1, stride, 16, thresh, ithresh, hev_thresh);
|
||||
}
|
||||
}
|
||||
|
||||
// 8-pixels wide variant, for chroma filtering
|
||||
static void VFilter8(uint8_t* u, uint8_t* v, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
FilterLoop26(u, stride, 1, 8, thresh, ithresh, hev_thresh);
|
||||
FilterLoop26(v, stride, 1, 8, thresh, ithresh, hev_thresh);
|
||||
}
|
||||
|
||||
static void HFilter8(uint8_t* u, uint8_t* v, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
FilterLoop26(u, 1, stride, 8, thresh, ithresh, hev_thresh);
|
||||
FilterLoop26(v, 1, stride, 8, thresh, ithresh, hev_thresh);
|
||||
}
|
||||
|
||||
static void VFilter8i(uint8_t* u, uint8_t* v, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
FilterLoop24(u + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
|
||||
FilterLoop24(v + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
|
||||
}
|
||||
|
||||
static void HFilter8i(uint8_t* u, uint8_t* v, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
FilterLoop24(u + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
|
||||
FilterLoop24(v + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
VP8DecIdct2 VP8Transform;
|
||||
VP8DecIdct VP8TransformUV;
|
||||
VP8DecIdct VP8TransformDC;
|
||||
VP8DecIdct VP8TransformDCUV;
|
||||
|
||||
VP8LumaFilterFunc VP8VFilter16;
|
||||
VP8LumaFilterFunc VP8HFilter16;
|
||||
VP8ChromaFilterFunc VP8VFilter8;
|
||||
VP8ChromaFilterFunc VP8HFilter8;
|
||||
VP8LumaFilterFunc VP8VFilter16i;
|
||||
VP8LumaFilterFunc VP8HFilter16i;
|
||||
VP8ChromaFilterFunc VP8VFilter8i;
|
||||
VP8ChromaFilterFunc VP8HFilter8i;
|
||||
VP8SimpleFilterFunc VP8SimpleVFilter16;
|
||||
VP8SimpleFilterFunc VP8SimpleHFilter16;
|
||||
VP8SimpleFilterFunc VP8SimpleVFilter16i;
|
||||
VP8SimpleFilterFunc VP8SimpleHFilter16i;
|
||||
|
||||
extern void VP8DspInitSSE2(void);
|
||||
|
||||
void VP8DspInit(void) {
|
||||
DspInitTables();
|
||||
|
||||
VP8Transform = TransformTwo;
|
||||
VP8TransformUV = TransformUV;
|
||||
VP8TransformDC = TransformDC;
|
||||
VP8TransformDCUV = TransformDCUV;
|
||||
|
||||
VP8VFilter16 = VFilter16;
|
||||
VP8HFilter16 = HFilter16;
|
||||
VP8VFilter8 = VFilter8;
|
||||
VP8HFilter8 = HFilter8;
|
||||
VP8VFilter16i = VFilter16i;
|
||||
VP8HFilter16i = HFilter16i;
|
||||
VP8VFilter8i = VFilter8i;
|
||||
VP8HFilter8i = HFilter8i;
|
||||
VP8SimpleVFilter16 = SimpleVFilter16;
|
||||
VP8SimpleHFilter16 = SimpleHFilter16;
|
||||
VP8SimpleVFilter16i = SimpleVFilter16i;
|
||||
VP8SimpleHFilter16i = SimpleHFilter16i;
|
||||
|
||||
// If defined, use CPUInfo() to overwrite some pointers with faster versions.
|
||||
if (VP8GetCPUInfo) {
|
||||
#if defined(__SSE2__) || defined(_MSC_VER)
|
||||
if (VP8GetCPUInfo(kSSE2)) {
|
||||
VP8DspInitSSE2();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
898
src/dsp/dec_sse2.c
Normal file
898
src/dsp/dec_sse2.c
Normal file
@ -0,0 +1,898 @@
|
||||
// Copyright 2011 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// SSE2 version of some decoding functions (idct, loop filtering).
|
||||
//
|
||||
// Author: somnath@google.com (Somnath Banerjee)
|
||||
// cduvivier@google.com (Christian Duvivier)
|
||||
|
||||
#if defined(__SSE2__) || defined(_MSC_VER)
|
||||
|
||||
#include <emmintrin.h>
|
||||
#include "../dec/vp8i.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Transforms (Paragraph 14.4)
|
||||
|
||||
static void TransformSSE2(const int16_t* in, uint8_t* dst, int do_two) {
|
||||
// This implementation makes use of 16-bit fixed point versions of two
|
||||
// multiply constants:
|
||||
// K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16
|
||||
// K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16
|
||||
//
|
||||
// To be able to use signed 16-bit integers, we use the following trick to
|
||||
// have constants within range:
|
||||
// - Associated constants are obtained by subtracting the 16-bit fixed point
|
||||
// version of one:
|
||||
// k = K - (1 << 16) => K = k + (1 << 16)
|
||||
// K1 = 85267 => k1 = 20091
|
||||
// K2 = 35468 => k2 = -30068
|
||||
// - The multiplication of a variable by a constant become the sum of the
|
||||
// variable and the multiplication of that variable by the associated
|
||||
// constant:
|
||||
// (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x
|
||||
const __m128i k1 = _mm_set1_epi16(20091);
|
||||
const __m128i k2 = _mm_set1_epi16(-30068);
|
||||
__m128i T0, T1, T2, T3;
|
||||
|
||||
// Load and concatenate the transform coefficients (we'll do two transforms
|
||||
// in parallel). In the case of only one transform, the second half of the
|
||||
// vectors will just contain random value we'll never use nor store.
|
||||
__m128i in0, in1, in2, in3;
|
||||
{
|
||||
in0 = _mm_loadl_epi64((__m128i*)&in[0]);
|
||||
in1 = _mm_loadl_epi64((__m128i*)&in[4]);
|
||||
in2 = _mm_loadl_epi64((__m128i*)&in[8]);
|
||||
in3 = _mm_loadl_epi64((__m128i*)&in[12]);
|
||||
// a00 a10 a20 a30 x x x x
|
||||
// a01 a11 a21 a31 x x x x
|
||||
// a02 a12 a22 a32 x x x x
|
||||
// a03 a13 a23 a33 x x x x
|
||||
if (do_two) {
|
||||
const __m128i inB0 = _mm_loadl_epi64((__m128i*)&in[16]);
|
||||
const __m128i inB1 = _mm_loadl_epi64((__m128i*)&in[20]);
|
||||
const __m128i inB2 = _mm_loadl_epi64((__m128i*)&in[24]);
|
||||
const __m128i inB3 = _mm_loadl_epi64((__m128i*)&in[28]);
|
||||
in0 = _mm_unpacklo_epi64(in0, inB0);
|
||||
in1 = _mm_unpacklo_epi64(in1, inB1);
|
||||
in2 = _mm_unpacklo_epi64(in2, inB2);
|
||||
in3 = _mm_unpacklo_epi64(in3, inB3);
|
||||
// a00 a10 a20 a30 b00 b10 b20 b30
|
||||
// a01 a11 a21 a31 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 b02 b12 b22 b32
|
||||
// a03 a13 a23 a33 b03 b13 b23 b33
|
||||
}
|
||||
}
|
||||
|
||||
// Vertical pass and subsequent transpose.
|
||||
{
|
||||
// First pass, c and d calculations are longer because of the "trick"
|
||||
// multiplications.
|
||||
const __m128i a = _mm_add_epi16(in0, in2);
|
||||
const __m128i b = _mm_sub_epi16(in0, in2);
|
||||
// c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3
|
||||
const __m128i c1 = _mm_mulhi_epi16(in1, k2);
|
||||
const __m128i c2 = _mm_mulhi_epi16(in3, k1);
|
||||
const __m128i c3 = _mm_sub_epi16(in1, in3);
|
||||
const __m128i c4 = _mm_sub_epi16(c1, c2);
|
||||
const __m128i c = _mm_add_epi16(c3, c4);
|
||||
// d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3
|
||||
const __m128i d1 = _mm_mulhi_epi16(in1, k1);
|
||||
const __m128i d2 = _mm_mulhi_epi16(in3, k2);
|
||||
const __m128i d3 = _mm_add_epi16(in1, in3);
|
||||
const __m128i d4 = _mm_add_epi16(d1, d2);
|
||||
const __m128i d = _mm_add_epi16(d3, d4);
|
||||
|
||||
// Second pass.
|
||||
const __m128i tmp0 = _mm_add_epi16(a, d);
|
||||
const __m128i tmp1 = _mm_add_epi16(b, c);
|
||||
const __m128i tmp2 = _mm_sub_epi16(b, c);
|
||||
const __m128i tmp3 = _mm_sub_epi16(a, d);
|
||||
|
||||
// Transpose the two 4x4.
|
||||
// a00 a01 a02 a03 b00 b01 b02 b03
|
||||
// a10 a11 a12 a13 b10 b11 b12 b13
|
||||
// a20 a21 a22 a23 b20 b21 b22 b23
|
||||
// a30 a31 a32 a33 b30 b31 b32 b33
|
||||
const __m128i transpose0_0 = _mm_unpacklo_epi16(tmp0, tmp1);
|
||||
const __m128i transpose0_1 = _mm_unpacklo_epi16(tmp2, tmp3);
|
||||
const __m128i transpose0_2 = _mm_unpackhi_epi16(tmp0, tmp1);
|
||||
const __m128i transpose0_3 = _mm_unpackhi_epi16(tmp2, tmp3);
|
||||
// a00 a10 a01 a11 a02 a12 a03 a13
|
||||
// a20 a30 a21 a31 a22 a32 a23 a33
|
||||
// b00 b10 b01 b11 b02 b12 b03 b13
|
||||
// b20 b30 b21 b31 b22 b32 b23 b33
|
||||
const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
|
||||
const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
|
||||
// a00 a10 a20 a30 a01 a11 a21 a31
|
||||
// b00 b10 b20 b30 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 a03 a13 a23 a33
|
||||
// b02 b12 a22 b32 b03 b13 b23 b33
|
||||
T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
|
||||
T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
|
||||
T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
|
||||
T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
|
||||
// a00 a10 a20 a30 b00 b10 b20 b30
|
||||
// a01 a11 a21 a31 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 b02 b12 b22 b32
|
||||
// a03 a13 a23 a33 b03 b13 b23 b33
|
||||
}
|
||||
|
||||
// Horizontal pass and subsequent transpose.
|
||||
{
|
||||
// First pass, c and d calculations are longer because of the "trick"
|
||||
// multiplications.
|
||||
const __m128i four = _mm_set1_epi16(4);
|
||||
const __m128i dc = _mm_add_epi16(T0, four);
|
||||
const __m128i a = _mm_add_epi16(dc, T2);
|
||||
const __m128i b = _mm_sub_epi16(dc, T2);
|
||||
// c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3
|
||||
const __m128i c1 = _mm_mulhi_epi16(T1, k2);
|
||||
const __m128i c2 = _mm_mulhi_epi16(T3, k1);
|
||||
const __m128i c3 = _mm_sub_epi16(T1, T3);
|
||||
const __m128i c4 = _mm_sub_epi16(c1, c2);
|
||||
const __m128i c = _mm_add_epi16(c3, c4);
|
||||
// d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3
|
||||
const __m128i d1 = _mm_mulhi_epi16(T1, k1);
|
||||
const __m128i d2 = _mm_mulhi_epi16(T3, k2);
|
||||
const __m128i d3 = _mm_add_epi16(T1, T3);
|
||||
const __m128i d4 = _mm_add_epi16(d1, d2);
|
||||
const __m128i d = _mm_add_epi16(d3, d4);
|
||||
|
||||
// Second pass.
|
||||
const __m128i tmp0 = _mm_add_epi16(a, d);
|
||||
const __m128i tmp1 = _mm_add_epi16(b, c);
|
||||
const __m128i tmp2 = _mm_sub_epi16(b, c);
|
||||
const __m128i tmp3 = _mm_sub_epi16(a, d);
|
||||
const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);
|
||||
const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);
|
||||
const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);
|
||||
const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);
|
||||
|
||||
// Transpose the two 4x4.
|
||||
// a00 a01 a02 a03 b00 b01 b02 b03
|
||||
// a10 a11 a12 a13 b10 b11 b12 b13
|
||||
// a20 a21 a22 a23 b20 b21 b22 b23
|
||||
// a30 a31 a32 a33 b30 b31 b32 b33
|
||||
const __m128i transpose0_0 = _mm_unpacklo_epi16(shifted0, shifted1);
|
||||
const __m128i transpose0_1 = _mm_unpacklo_epi16(shifted2, shifted3);
|
||||
const __m128i transpose0_2 = _mm_unpackhi_epi16(shifted0, shifted1);
|
||||
const __m128i transpose0_3 = _mm_unpackhi_epi16(shifted2, shifted3);
|
||||
// a00 a10 a01 a11 a02 a12 a03 a13
|
||||
// a20 a30 a21 a31 a22 a32 a23 a33
|
||||
// b00 b10 b01 b11 b02 b12 b03 b13
|
||||
// b20 b30 b21 b31 b22 b32 b23 b33
|
||||
const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
|
||||
const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
|
||||
// a00 a10 a20 a30 a01 a11 a21 a31
|
||||
// b00 b10 b20 b30 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 a03 a13 a23 a33
|
||||
// b02 b12 a22 b32 b03 b13 b23 b33
|
||||
T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
|
||||
T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
|
||||
T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
|
||||
T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
|
||||
// a00 a10 a20 a30 b00 b10 b20 b30
|
||||
// a01 a11 a21 a31 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 b02 b12 b22 b32
|
||||
// a03 a13 a23 a33 b03 b13 b23 b33
|
||||
}
|
||||
|
||||
// Add inverse transform to 'dst' and store.
|
||||
{
|
||||
const __m128i zero = _mm_set1_epi16(0);
|
||||
// Load the reference(s).
|
||||
__m128i dst0, dst1, dst2, dst3;
|
||||
if (do_two) {
|
||||
// Load eight bytes/pixels per line.
|
||||
dst0 = _mm_loadl_epi64((__m128i*)&dst[0 * BPS]);
|
||||
dst1 = _mm_loadl_epi64((__m128i*)&dst[1 * BPS]);
|
||||
dst2 = _mm_loadl_epi64((__m128i*)&dst[2 * BPS]);
|
||||
dst3 = _mm_loadl_epi64((__m128i*)&dst[3 * BPS]);
|
||||
} else {
|
||||
// Load four bytes/pixels per line.
|
||||
dst0 = _mm_cvtsi32_si128(*(int*)&dst[0 * BPS]);
|
||||
dst1 = _mm_cvtsi32_si128(*(int*)&dst[1 * BPS]);
|
||||
dst2 = _mm_cvtsi32_si128(*(int*)&dst[2 * BPS]);
|
||||
dst3 = _mm_cvtsi32_si128(*(int*)&dst[3 * BPS]);
|
||||
}
|
||||
// Convert to 16b.
|
||||
dst0 = _mm_unpacklo_epi8(dst0, zero);
|
||||
dst1 = _mm_unpacklo_epi8(dst1, zero);
|
||||
dst2 = _mm_unpacklo_epi8(dst2, zero);
|
||||
dst3 = _mm_unpacklo_epi8(dst3, zero);
|
||||
// Add the inverse transform(s).
|
||||
dst0 = _mm_add_epi16(dst0, T0);
|
||||
dst1 = _mm_add_epi16(dst1, T1);
|
||||
dst2 = _mm_add_epi16(dst2, T2);
|
||||
dst3 = _mm_add_epi16(dst3, T3);
|
||||
// Unsigned saturate to 8b.
|
||||
dst0 = _mm_packus_epi16(dst0, dst0);
|
||||
dst1 = _mm_packus_epi16(dst1, dst1);
|
||||
dst2 = _mm_packus_epi16(dst2, dst2);
|
||||
dst3 = _mm_packus_epi16(dst3, dst3);
|
||||
// Store the results.
|
||||
if (do_two) {
|
||||
// Store eight bytes/pixels per line.
|
||||
_mm_storel_epi64((__m128i*)&dst[0 * BPS], dst0);
|
||||
_mm_storel_epi64((__m128i*)&dst[1 * BPS], dst1);
|
||||
_mm_storel_epi64((__m128i*)&dst[2 * BPS], dst2);
|
||||
_mm_storel_epi64((__m128i*)&dst[3 * BPS], dst3);
|
||||
} else {
|
||||
// Store four bytes/pixels per line.
|
||||
*((int32_t *)&dst[0 * BPS]) = _mm_cvtsi128_si32(dst0);
|
||||
*((int32_t *)&dst[1 * BPS]) = _mm_cvtsi128_si32(dst1);
|
||||
*((int32_t *)&dst[2 * BPS]) = _mm_cvtsi128_si32(dst2);
|
||||
*((int32_t *)&dst[3 * BPS]) = _mm_cvtsi128_si32(dst3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Loop Filter (Paragraph 15)
|
||||
|
||||
// Compute abs(p - q) = subs(p - q) OR subs(q - p)
|
||||
#define MM_ABS(p, q) _mm_or_si128( \
|
||||
_mm_subs_epu8((q), (p)), \
|
||||
_mm_subs_epu8((p), (q)))
|
||||
|
||||
// Shift each byte of "a" by N bits while preserving by the sign bit.
|
||||
//
|
||||
// It first shifts the lower bytes of the words and then the upper bytes and
|
||||
// then merges the results together.
|
||||
#define SIGNED_SHIFT_N(a, N) { \
|
||||
__m128i t = a; \
|
||||
t = _mm_slli_epi16(t, 8); \
|
||||
t = _mm_srai_epi16(t, N); \
|
||||
t = _mm_srli_epi16(t, 8); \
|
||||
\
|
||||
a = _mm_srai_epi16(a, N + 8); \
|
||||
a = _mm_slli_epi16(a, 8); \
|
||||
\
|
||||
a = _mm_or_si128(t, a); \
|
||||
}
|
||||
|
||||
#define FLIP_SIGN_BIT2(a, b) { \
|
||||
a = _mm_xor_si128(a, sign_bit); \
|
||||
b = _mm_xor_si128(b, sign_bit); \
|
||||
}
|
||||
|
||||
#define FLIP_SIGN_BIT4(a, b, c, d) { \
|
||||
FLIP_SIGN_BIT2(a, b); \
|
||||
FLIP_SIGN_BIT2(c, d); \
|
||||
}
|
||||
|
||||
#define GET_NOTHEV(p1, p0, q0, q1, hev_thresh, not_hev) { \
|
||||
const __m128i zero = _mm_setzero_si128(); \
|
||||
const __m128i t1 = MM_ABS(p1, p0); \
|
||||
const __m128i t2 = MM_ABS(q1, q0); \
|
||||
\
|
||||
const __m128i h = _mm_set1_epi8(hev_thresh); \
|
||||
const __m128i t3 = _mm_subs_epu8(t1, h); /* abs(p1 - p0) - hev_tresh */ \
|
||||
const __m128i t4 = _mm_subs_epu8(t2, h); /* abs(q1 - q0) - hev_tresh */ \
|
||||
\
|
||||
not_hev = _mm_or_si128(t3, t4); \
|
||||
not_hev = _mm_cmpeq_epi8(not_hev, zero); /* not_hev <= t1 && not_hev <= t2 */\
|
||||
}
|
||||
|
||||
#define GET_BASE_DELTA(p1, p0, q0, q1, o) { \
|
||||
const __m128i qp0 = _mm_subs_epi8(q0, p0); /* q0 - p0 */ \
|
||||
o = _mm_subs_epi8(p1, q1); /* p1 - q1 */ \
|
||||
o = _mm_adds_epi8(o, qp0); /* p1 - q1 + 1 * (q0 - p0) */ \
|
||||
o = _mm_adds_epi8(o, qp0); /* p1 - q1 + 2 * (q0 - p0) */ \
|
||||
o = _mm_adds_epi8(o, qp0); /* p1 - q1 + 3 * (q0 - p0) */ \
|
||||
}
|
||||
|
||||
#define DO_SIMPLE_FILTER(p0, q0, fl) { \
|
||||
const __m128i three = _mm_set1_epi8(3); \
|
||||
const __m128i four = _mm_set1_epi8(4); \
|
||||
__m128i v3 = _mm_adds_epi8(fl, three); \
|
||||
__m128i v4 = _mm_adds_epi8(fl, four); \
|
||||
\
|
||||
/* Do +4 side */ \
|
||||
SIGNED_SHIFT_N(v4, 3); /* v4 >> 3 */ \
|
||||
q0 = _mm_subs_epi8(q0, v4); /* q0 -= v4 */ \
|
||||
\
|
||||
/* Now do +3 side */ \
|
||||
SIGNED_SHIFT_N(v3, 3); /* v3 >> 3 */ \
|
||||
p0 = _mm_adds_epi8(p0, v3); /* p0 += v3 */ \
|
||||
}
|
||||
|
||||
// Updates values of 2 pixels at MB edge during complex filtering.
|
||||
// Update operations:
|
||||
// q = q - a and p = p + a; where a = [(a_hi >> 7), (a_lo >> 7)]
|
||||
#define UPDATE_2PIXELS(pi, qi, a_lo, a_hi) { \
|
||||
const __m128i a_lo7 = _mm_srai_epi16(a_lo, 7); \
|
||||
const __m128i a_hi7 = _mm_srai_epi16(a_hi, 7); \
|
||||
const __m128i a = _mm_packs_epi16(a_lo7, a_hi7); \
|
||||
pi = _mm_adds_epi8(pi, a); \
|
||||
qi = _mm_subs_epi8(qi, a); \
|
||||
}
|
||||
|
||||
static void NeedsFilter(const __m128i* p1, const __m128i* p0, const __m128i* q0,
|
||||
const __m128i* q1, int thresh, __m128i *mask) {
|
||||
__m128i t1 = MM_ABS(*p1, *q1); // abs(p1 - q1)
|
||||
*mask = _mm_set1_epi8(0xFE);
|
||||
t1 = _mm_and_si128(t1, *mask); // set lsb of each byte to zero
|
||||
t1 = _mm_srli_epi16(t1, 1); // abs(p1 - q1) / 2
|
||||
|
||||
*mask = MM_ABS(*p0, *q0); // abs(p0 - q0)
|
||||
*mask = _mm_adds_epu8(*mask, *mask); // abs(p0 - q0) * 2
|
||||
*mask = _mm_adds_epu8(*mask, t1); // abs(p0 - q0) * 2 + abs(p1 - q1) / 2
|
||||
|
||||
t1 = _mm_set1_epi8(thresh);
|
||||
*mask = _mm_subs_epu8(*mask, t1); // mask <= thresh
|
||||
*mask = _mm_cmpeq_epi8(*mask, _mm_setzero_si128());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Edge filtering functions
|
||||
|
||||
// Applies filter on 2 pixels (p0 and q0)
|
||||
static inline void DoFilter2(const __m128i* p1, __m128i* p0, __m128i* q0,
|
||||
const __m128i* q1, int thresh) {
|
||||
__m128i a, mask;
|
||||
const __m128i sign_bit = _mm_set1_epi8(0x80);
|
||||
const __m128i p1s = _mm_xor_si128(*p1, sign_bit);
|
||||
const __m128i q1s = _mm_xor_si128(*q1, sign_bit);
|
||||
|
||||
NeedsFilter(p1, p0, q0, q1, thresh, &mask);
|
||||
|
||||
// convert to signed values
|
||||
FLIP_SIGN_BIT2(*p0, *q0);
|
||||
|
||||
GET_BASE_DELTA(p1s, *p0, *q0, q1s, a);
|
||||
a = _mm_and_si128(a, mask); // mask filter values we don't care about
|
||||
DO_SIMPLE_FILTER(*p0, *q0, a);
|
||||
|
||||
// unoffset
|
||||
FLIP_SIGN_BIT2(*p0, *q0);
|
||||
}
|
||||
|
||||
// Applies filter on 4 pixels (p1, p0, q0 and q1)
|
||||
static inline void DoFilter4(__m128i* p1, __m128i *p0, __m128i* q0, __m128i* q1,
|
||||
const __m128i* mask, int hev_thresh) {
|
||||
__m128i not_hev;
|
||||
__m128i t1, t2, t3;
|
||||
const __m128i sign_bit = _mm_set1_epi8(0x80);
|
||||
|
||||
// compute hev mask
|
||||
GET_NOTHEV(*p1, *p0, *q0, *q1, hev_thresh, not_hev);
|
||||
|
||||
// convert to signed values
|
||||
FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);
|
||||
|
||||
t1 = _mm_subs_epi8(*p1, *q1); // p1 - q1
|
||||
t1 = _mm_andnot_si128(not_hev, t1); // hev(p1 - q1)
|
||||
t2 = _mm_subs_epi8(*q0, *p0); // q0 - p0
|
||||
t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 1 * (q0 - p0)
|
||||
t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 2 * (q0 - p0)
|
||||
t1 = _mm_adds_epi8(t1, t2); // hev(p1 - q1) + 3 * (q0 - p0)
|
||||
t1 = _mm_and_si128(t1, *mask); // mask filter values we don't care about
|
||||
|
||||
// Do +4 side
|
||||
t2 = _mm_set1_epi8(4);
|
||||
t2 = _mm_adds_epi8(t1, t2); // 3 * (q0 - p0) + (p1 - q1) + 4
|
||||
SIGNED_SHIFT_N(t2, 3); // (3 * (q0 - p0) + hev(p1 - q1) + 4) >> 3
|
||||
t3 = t2; // save t2
|
||||
*q0 = _mm_subs_epi8(*q0, t2); // q0 -= t2
|
||||
|
||||
// Now do +3 side
|
||||
t2 = _mm_set1_epi8(3);
|
||||
t2 = _mm_adds_epi8(t1, t2); // +3 instead of +4
|
||||
SIGNED_SHIFT_N(t2, 3); // (3 * (q0 - p0) + hev(p1 - q1) + 3) >> 3
|
||||
*p0 = _mm_adds_epi8(*p0, t2); // p0 += t2
|
||||
|
||||
t2 = _mm_set1_epi8(1);
|
||||
t3 = _mm_adds_epi8(t3, t2);
|
||||
SIGNED_SHIFT_N(t3, 1); // (3 * (q0 - p0) + hev(p1 - q1) + 4) >> 4
|
||||
|
||||
t3 = _mm_and_si128(not_hev, t3); // if !hev
|
||||
*q1 = _mm_subs_epi8(*q1, t3); // q1 -= t3
|
||||
*p1 = _mm_adds_epi8(*p1, t3); // p1 += t3
|
||||
|
||||
// unoffset
|
||||
FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);
|
||||
}
|
||||
|
||||
// Applies filter on 6 pixels (p2, p1, p0, q0, q1 and q2)
|
||||
static inline void DoFilter6(__m128i *p2, __m128i* p1, __m128i *p0,
|
||||
__m128i* q0, __m128i* q1, __m128i *q2,
|
||||
const __m128i* mask, int hev_thresh) {
|
||||
__m128i a, not_hev;
|
||||
const __m128i sign_bit = _mm_set1_epi8(0x80);
|
||||
|
||||
// compute hev mask
|
||||
GET_NOTHEV(*p1, *p0, *q0, *q1, hev_thresh, not_hev);
|
||||
|
||||
// convert to signed values
|
||||
FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);
|
||||
FLIP_SIGN_BIT2(*p2, *q2);
|
||||
|
||||
GET_BASE_DELTA(*p1, *p0, *q0, *q1, a);
|
||||
|
||||
{ // do simple filter on pixels with hev
|
||||
const __m128i m = _mm_andnot_si128(not_hev, *mask);
|
||||
const __m128i f = _mm_and_si128(a, m);
|
||||
DO_SIMPLE_FILTER(*p0, *q0, f);
|
||||
}
|
||||
{ // do strong filter on pixels with not hev
|
||||
const __m128i zero = _mm_setzero_si128();
|
||||
const __m128i nine = _mm_set1_epi16(0x0900);
|
||||
const __m128i sixty_three = _mm_set1_epi16(63);
|
||||
|
||||
const __m128i m = _mm_and_si128(not_hev, *mask);
|
||||
const __m128i f = _mm_and_si128(a, m);
|
||||
const __m128i f_lo = _mm_unpacklo_epi8(zero, f);
|
||||
const __m128i f_hi = _mm_unpackhi_epi8(zero, f);
|
||||
|
||||
const __m128i f9_lo = _mm_mulhi_epi16(f_lo, nine); // Filter (lo) * 9
|
||||
const __m128i f9_hi = _mm_mulhi_epi16(f_hi, nine); // Filter (hi) * 9
|
||||
const __m128i f18_lo = _mm_add_epi16(f9_lo, f9_lo); // Filter (lo) * 18
|
||||
const __m128i f18_hi = _mm_add_epi16(f9_hi, f9_hi); // Filter (hi) * 18
|
||||
|
||||
const __m128i a2_lo = _mm_add_epi16(f9_lo, sixty_three); // Filter * 9 + 63
|
||||
const __m128i a2_hi = _mm_add_epi16(f9_hi, sixty_three); // Filter * 9 + 63
|
||||
|
||||
const __m128i a1_lo = _mm_add_epi16(f18_lo, sixty_three); // F... * 18 + 63
|
||||
const __m128i a1_hi = _mm_add_epi16(f18_hi, sixty_three); // F... * 18 + 63
|
||||
|
||||
const __m128i a0_lo = _mm_add_epi16(f18_lo, a2_lo); // Filter * 27 + 63
|
||||
const __m128i a0_hi = _mm_add_epi16(f18_hi, a2_hi); // Filter * 27 + 63
|
||||
|
||||
UPDATE_2PIXELS(*p2, *q2, a2_lo, a2_hi);
|
||||
UPDATE_2PIXELS(*p1, *q1, a1_lo, a1_hi);
|
||||
UPDATE_2PIXELS(*p0, *q0, a0_lo, a0_hi);
|
||||
}
|
||||
|
||||
// unoffset
|
||||
FLIP_SIGN_BIT4(*p1, *p0, *q0, *q1);
|
||||
FLIP_SIGN_BIT2(*p2, *q2);
|
||||
}
|
||||
|
||||
// reads 8 rows across a vertical edge.
|
||||
//
|
||||
// TODO(somnath): Investigate _mm_shuffle* also see if it can be broken into
|
||||
// two Load4x4() to avoid code duplication.
|
||||
static inline void Load8x4(const uint8_t* b, int stride,
|
||||
__m128i* p, __m128i* q) {
|
||||
__m128i t1, t2;
|
||||
|
||||
// Load 0th, 1st, 4th and 5th rows
|
||||
__m128i r0 = _mm_cvtsi32_si128(*((int*)&b[0 * stride])); // 03 02 01 00
|
||||
__m128i r1 = _mm_cvtsi32_si128(*((int*)&b[1 * stride])); // 13 12 11 10
|
||||
__m128i r4 = _mm_cvtsi32_si128(*((int*)&b[4 * stride])); // 43 42 41 40
|
||||
__m128i r5 = _mm_cvtsi32_si128(*((int*)&b[5 * stride])); // 53 52 51 50
|
||||
|
||||
r0 = _mm_unpacklo_epi32(r0, r4); // 43 42 41 40 03 02 01 00
|
||||
r1 = _mm_unpacklo_epi32(r1, r5); // 53 52 51 50 13 12 11 10
|
||||
|
||||
// t1 = 53 43 52 42 51 41 50 40 13 03 12 02 11 01 10 00
|
||||
t1 = _mm_unpacklo_epi8(r0, r1);
|
||||
|
||||
// Load 2nd, 3rd, 6th and 7th rows
|
||||
r0 = _mm_cvtsi32_si128(*((int*)&b[2 * stride])); // 23 22 21 22
|
||||
r1 = _mm_cvtsi32_si128(*((int*)&b[3 * stride])); // 33 32 31 30
|
||||
r4 = _mm_cvtsi32_si128(*((int*)&b[6 * stride])); // 63 62 61 60
|
||||
r5 = _mm_cvtsi32_si128(*((int*)&b[7 * stride])); // 73 72 71 70
|
||||
|
||||
r0 = _mm_unpacklo_epi32(r0, r4); // 63 62 61 60 23 22 21 20
|
||||
r1 = _mm_unpacklo_epi32(r1, r5); // 73 72 71 70 33 32 31 30
|
||||
|
||||
// t2 = 73 63 72 62 71 61 70 60 33 23 32 22 31 21 30 20
|
||||
t2 = _mm_unpacklo_epi8(r0, r1);
|
||||
|
||||
// t1 = 33 23 13 03 32 22 12 02 31 21 11 01 30 20 10 00
|
||||
// t2 = 73 63 53 43 72 62 52 42 71 61 51 41 70 60 50 40
|
||||
r0 = t1;
|
||||
t1 = _mm_unpacklo_epi16(t1, t2);
|
||||
t2 = _mm_unpackhi_epi16(r0, t2);
|
||||
|
||||
// *p = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00
|
||||
// *q = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02
|
||||
*p = _mm_unpacklo_epi32(t1, t2);
|
||||
*q = _mm_unpackhi_epi32(t1, t2);
|
||||
}
|
||||
|
||||
static inline void Load16x4(const uint8_t* r0, const uint8_t* r8, int stride,
|
||||
__m128i* p1, __m128i* p0,
|
||||
__m128i* q0, __m128i* q1) {
|
||||
__m128i t1, t2;
|
||||
// Assume the pixels around the edge (|) are numbered as follows
|
||||
// 00 01 | 02 03
|
||||
// 10 11 | 12 13
|
||||
// ... | ...
|
||||
// e0 e1 | e2 e3
|
||||
// f0 f1 | f2 f3
|
||||
//
|
||||
// r0 is pointing to the 0th row (00)
|
||||
// r8 is pointing to the 8th row (80)
|
||||
|
||||
// Load
|
||||
// p1 = 71 61 51 41 31 21 11 01 70 60 50 40 30 20 10 00
|
||||
// q0 = 73 63 53 43 33 23 13 03 72 62 52 42 32 22 12 02
|
||||
// p0 = f1 e1 d1 c1 b1 a1 91 81 f0 e0 d0 c0 b0 a0 90 80
|
||||
// q1 = f3 e3 d3 c3 b3 a3 93 83 f2 e2 d2 c2 b2 a2 92 82
|
||||
Load8x4(r0, stride, p1, q0);
|
||||
Load8x4(r8, stride, p0, q1);
|
||||
|
||||
t1 = *p1;
|
||||
t2 = *q0;
|
||||
// p1 = f0 e0 d0 c0 b0 a0 90 80 70 60 50 40 30 20 10 00
|
||||
// p0 = f1 e1 d1 c1 b1 a1 91 81 71 61 51 41 31 21 11 01
|
||||
// q0 = f2 e2 d2 c2 b2 a2 92 82 72 62 52 42 32 22 12 02
|
||||
// q1 = f3 e3 d3 c3 b3 a3 93 83 73 63 53 43 33 23 13 03
|
||||
*p1 = _mm_unpacklo_epi64(t1, *p0);
|
||||
*p0 = _mm_unpackhi_epi64(t1, *p0);
|
||||
*q0 = _mm_unpacklo_epi64(t2, *q1);
|
||||
*q1 = _mm_unpackhi_epi64(t2, *q1);
|
||||
}
|
||||
|
||||
static inline void Store4x4(__m128i* x, uint8_t* dst, int stride) {
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i, dst += stride) {
|
||||
*((int32_t*)dst) = _mm_cvtsi128_si32(*x);
|
||||
*x = _mm_srli_si128(*x, 4);
|
||||
}
|
||||
}
|
||||
|
||||
// Transpose back and store
|
||||
static inline void Store16x4(uint8_t* r0, uint8_t* r8, int stride, __m128i* p1,
|
||||
__m128i* p0, __m128i* q0, __m128i* q1) {
|
||||
__m128i t1;
|
||||
|
||||
// p0 = 71 70 61 60 51 50 41 40 31 30 21 20 11 10 01 00
|
||||
// p1 = f1 f0 e1 e0 d1 d0 c1 c0 b1 b0 a1 a0 91 90 81 80
|
||||
t1 = *p0;
|
||||
*p0 = _mm_unpacklo_epi8(*p1, t1);
|
||||
*p1 = _mm_unpackhi_epi8(*p1, t1);
|
||||
|
||||
// q0 = 73 72 63 62 53 52 43 42 33 32 23 22 13 12 03 02
|
||||
// q1 = f3 f2 e3 e2 d3 d2 c3 c2 b3 b2 a3 a2 93 92 83 82
|
||||
t1 = *q0;
|
||||
*q0 = _mm_unpacklo_epi8(t1, *q1);
|
||||
*q1 = _mm_unpackhi_epi8(t1, *q1);
|
||||
|
||||
// p0 = 33 32 31 30 23 22 21 20 13 12 11 10 03 02 01 00
|
||||
// q0 = 73 72 71 70 63 62 61 60 53 52 51 50 43 42 41 40
|
||||
t1 = *p0;
|
||||
*p0 = _mm_unpacklo_epi16(t1, *q0);
|
||||
*q0 = _mm_unpackhi_epi16(t1, *q0);
|
||||
|
||||
// p1 = b3 b2 b1 b0 a3 a2 a1 a0 93 92 91 90 83 82 81 80
|
||||
// q1 = f3 f2 f1 f0 e3 e2 e1 e0 d3 d2 d1 d0 c3 c2 c1 c0
|
||||
t1 = *p1;
|
||||
*p1 = _mm_unpacklo_epi16(t1, *q1);
|
||||
*q1 = _mm_unpackhi_epi16(t1, *q1);
|
||||
|
||||
Store4x4(p0, r0, stride);
|
||||
r0 += 4 * stride;
|
||||
Store4x4(q0, r0, stride);
|
||||
|
||||
Store4x4(p1, r8, stride);
|
||||
r8 += 4 * stride;
|
||||
Store4x4(q1, r8, stride);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Simple In-loop filtering (Paragraph 15.2)
|
||||
|
||||
static void SimpleVFilter16SSE2(uint8_t* p, int stride, int thresh) {
|
||||
// Load
|
||||
__m128i p1 = _mm_loadu_si128((__m128i*)&p[-2 * stride]);
|
||||
__m128i p0 = _mm_loadu_si128((__m128i*)&p[-stride]);
|
||||
__m128i q0 = _mm_loadu_si128((__m128i*)&p[0]);
|
||||
__m128i q1 = _mm_loadu_si128((__m128i*)&p[stride]);
|
||||
|
||||
DoFilter2(&p1, &p0, &q0, &q1, thresh);
|
||||
|
||||
// Store
|
||||
_mm_storeu_si128((__m128i*)&p[-stride], p0);
|
||||
_mm_storeu_si128((__m128i*)p, q0);
|
||||
}
|
||||
|
||||
static void SimpleHFilter16SSE2(uint8_t* p, int stride, int thresh) {
|
||||
__m128i p1, p0, q0, q1;
|
||||
|
||||
p -= 2; // beginning of p1
|
||||
|
||||
Load16x4(p, p + 8 * stride, stride, &p1, &p0, &q0, &q1);
|
||||
DoFilter2(&p1, &p0, &q0, &q1, thresh);
|
||||
Store16x4(p, p + 8 * stride, stride, &p1, &p0, &q0, &q1);
|
||||
}
|
||||
|
||||
static void SimpleVFilter16iSSE2(uint8_t* p, int stride, int thresh) {
|
||||
int k;
|
||||
for (k = 3; k > 0; --k) {
|
||||
p += 4 * stride;
|
||||
SimpleVFilter16SSE2(p, stride, thresh);
|
||||
}
|
||||
}
|
||||
|
||||
static void SimpleHFilter16iSSE2(uint8_t* p, int stride, int thresh) {
|
||||
int k;
|
||||
for (k = 3; k > 0; --k) {
|
||||
p += 4;
|
||||
SimpleHFilter16SSE2(p, stride, thresh);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Complex In-loop filtering (Paragraph 15.3)
|
||||
|
||||
#define MAX_DIFF1(p3, p2, p1, p0, m) { \
|
||||
m = MM_ABS(p3, p2); \
|
||||
m = _mm_max_epu8(m, MM_ABS(p2, p1)); \
|
||||
m = _mm_max_epu8(m, MM_ABS(p1, p0)); \
|
||||
}
|
||||
|
||||
#define MAX_DIFF2(p3, p2, p1, p0, m) { \
|
||||
m = _mm_max_epu8(m, MM_ABS(p3, p2)); \
|
||||
m = _mm_max_epu8(m, MM_ABS(p2, p1)); \
|
||||
m = _mm_max_epu8(m, MM_ABS(p1, p0)); \
|
||||
}
|
||||
|
||||
#define LOAD_H_EDGES4(p, stride, e1, e2, e3, e4) { \
|
||||
e1 = _mm_loadu_si128((__m128i*)&(p)[0 * stride]); \
|
||||
e2 = _mm_loadu_si128((__m128i*)&(p)[1 * stride]); \
|
||||
e3 = _mm_loadu_si128((__m128i*)&(p)[2 * stride]); \
|
||||
e4 = _mm_loadu_si128((__m128i*)&(p)[3 * stride]); \
|
||||
}
|
||||
|
||||
#define LOADUV_H_EDGE(p, u, v, stride) { \
|
||||
p = _mm_loadl_epi64((__m128i*)&(u)[(stride)]); \
|
||||
p = _mm_unpacklo_epi64(p, _mm_loadl_epi64((__m128i*)&(v)[(stride)])); \
|
||||
}
|
||||
|
||||
#define LOADUV_H_EDGES4(u, v, stride, e1, e2, e3, e4) { \
|
||||
LOADUV_H_EDGE(e1, u, v, 0 * stride); \
|
||||
LOADUV_H_EDGE(e2, u, v, 1 * stride); \
|
||||
LOADUV_H_EDGE(e3, u, v, 2 * stride); \
|
||||
LOADUV_H_EDGE(e4, u, v, 3 * stride); \
|
||||
}
|
||||
|
||||
#define STOREUV(p, u, v, stride) { \
|
||||
_mm_storel_epi64((__m128i*)&u[(stride)], p); \
|
||||
p = _mm_srli_si128(p, 8); \
|
||||
_mm_storel_epi64((__m128i*)&v[(stride)], p); \
|
||||
}
|
||||
|
||||
#define COMPLEX_FL_MASK(p1, p0, q0, q1, thresh, ithresh, mask) { \
|
||||
__m128i fl_yes; \
|
||||
const __m128i it = _mm_set1_epi8(ithresh); \
|
||||
mask = _mm_subs_epu8(mask, it); \
|
||||
mask = _mm_cmpeq_epi8(mask, _mm_setzero_si128()); \
|
||||
NeedsFilter(&p1, &p0, &q0, &q1, thresh, &fl_yes); \
|
||||
mask = _mm_and_si128(mask, fl_yes); \
|
||||
}
|
||||
|
||||
// on macroblock edges
|
||||
static void VFilter16SSE2(uint8_t* p, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
__m128i t1;
|
||||
__m128i mask;
|
||||
__m128i p2, p1, p0, q0, q1, q2;
|
||||
|
||||
// Load p3, p2, p1, p0
|
||||
LOAD_H_EDGES4(p - 4 * stride, stride, t1, p2, p1, p0);
|
||||
MAX_DIFF1(t1, p2, p1, p0, mask);
|
||||
|
||||
// Load q0, q1, q2, q3
|
||||
LOAD_H_EDGES4(p, stride, q0, q1, q2, t1);
|
||||
MAX_DIFF2(t1, q2, q1, q0, mask);
|
||||
|
||||
COMPLEX_FL_MASK(p1, p0, q0, q1, thresh, ithresh, mask);
|
||||
DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
|
||||
|
||||
// Store
|
||||
_mm_storeu_si128((__m128i*)&p[-3 * stride], p2);
|
||||
_mm_storeu_si128((__m128i*)&p[-2 * stride], p1);
|
||||
_mm_storeu_si128((__m128i*)&p[-1 * stride], p0);
|
||||
_mm_storeu_si128((__m128i*)&p[0 * stride], q0);
|
||||
_mm_storeu_si128((__m128i*)&p[1 * stride], q1);
|
||||
_mm_storeu_si128((__m128i*)&p[2 * stride], q2);
|
||||
}
|
||||
|
||||
static void HFilter16SSE2(uint8_t* p, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
__m128i mask;
|
||||
__m128i p3, p2, p1, p0, q0, q1, q2, q3;
|
||||
|
||||
uint8_t* const b = p - 4;
|
||||
Load16x4(b, b + 8 * stride, stride, &p3, &p2, &p1, &p0); // p3, p2, p1, p0
|
||||
MAX_DIFF1(p3, p2, p1, p0, mask);
|
||||
|
||||
Load16x4(p, p + 8 * stride, stride, &q0, &q1, &q2, &q3); // q0, q1, q2, q3
|
||||
MAX_DIFF2(q3, q2, q1, q0, mask);
|
||||
|
||||
COMPLEX_FL_MASK(p1, p0, q0, q1, thresh, ithresh, mask);
|
||||
DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
|
||||
|
||||
Store16x4(b, b + 8 * stride, stride, &p3, &p2, &p1, &p0);
|
||||
Store16x4(p, p + 8 * stride, stride, &q0, &q1, &q2, &q3);
|
||||
}
|
||||
|
||||
// on three inner edges
|
||||
static void VFilter16iSSE2(uint8_t* p, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
int k;
|
||||
__m128i mask;
|
||||
__m128i t1, t2, p1, p0, q0, q1;
|
||||
|
||||
for (k = 3; k > 0; --k) {
|
||||
// Load p3, p2, p1, p0
|
||||
LOAD_H_EDGES4(p, stride, t2, t1, p1, p0);
|
||||
MAX_DIFF1(t2, t1, p1, p0, mask);
|
||||
|
||||
p += 4 * stride;
|
||||
|
||||
// Load q0, q1, q2, q3
|
||||
LOAD_H_EDGES4(p, stride, q0, q1, t1, t2);
|
||||
MAX_DIFF2(t2, t1, q1, q0, mask);
|
||||
|
||||
COMPLEX_FL_MASK(p1, p0, q0, q1, thresh, ithresh, mask);
|
||||
DoFilter4(&p1, &p0, &q0, &q1, &mask, hev_thresh);
|
||||
|
||||
// Store
|
||||
_mm_storeu_si128((__m128i*)&p[-2 * stride], p1);
|
||||
_mm_storeu_si128((__m128i*)&p[-1 * stride], p0);
|
||||
_mm_storeu_si128((__m128i*)&p[0 * stride], q0);
|
||||
_mm_storeu_si128((__m128i*)&p[1 * stride], q1);
|
||||
}
|
||||
}
|
||||
|
||||
static void HFilter16iSSE2(uint8_t* p, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
int k;
|
||||
uint8_t* b;
|
||||
__m128i mask;
|
||||
__m128i t1, t2, p1, p0, q0, q1;
|
||||
|
||||
for (k = 3; k > 0; --k) {
|
||||
b = p;
|
||||
Load16x4(b, b + 8 * stride, stride, &t2, &t1, &p1, &p0); // p3, p2, p1, p0
|
||||
MAX_DIFF1(t2, t1, p1, p0, mask);
|
||||
|
||||
b += 4; // beginning of q0
|
||||
Load16x4(b, b + 8 * stride, stride, &q0, &q1, &t1, &t2); // q0, q1, q2, q3
|
||||
MAX_DIFF2(t2, t1, q1, q0, mask);
|
||||
|
||||
COMPLEX_FL_MASK(p1, p0, q0, q1, thresh, ithresh, mask);
|
||||
DoFilter4(&p1, &p0, &q0, &q1, &mask, hev_thresh);
|
||||
|
||||
b -= 2; // beginning of p1
|
||||
Store16x4(b, b + 8 * stride, stride, &p1, &p0, &q0, &q1);
|
||||
|
||||
p += 4;
|
||||
}
|
||||
}
|
||||
|
||||
// 8-pixels wide variant, for chroma filtering
|
||||
static void VFilter8SSE2(uint8_t* u, uint8_t* v, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
__m128i mask;
|
||||
__m128i t1, p2, p1, p0, q0, q1, q2;
|
||||
|
||||
// Load p3, p2, p1, p0
|
||||
LOADUV_H_EDGES4(u - 4 * stride, v - 4 * stride, stride, t1, p2, p1, p0);
|
||||
MAX_DIFF1(t1, p2, p1, p0, mask);
|
||||
|
||||
// Load q0, q1, q2, q3
|
||||
LOADUV_H_EDGES4(u, v, stride, q0, q1, q2, t1);
|
||||
MAX_DIFF2(t1, q2, q1, q0, mask);
|
||||
|
||||
COMPLEX_FL_MASK(p1, p0, q0, q1, thresh, ithresh, mask);
|
||||
DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
|
||||
|
||||
// Store
|
||||
STOREUV(p2, u, v, -3 * stride);
|
||||
STOREUV(p1, u, v, -2 * stride);
|
||||
STOREUV(p0, u, v, -1 * stride);
|
||||
STOREUV(q0, u, v, 0 * stride);
|
||||
STOREUV(q1, u, v, 1 * stride);
|
||||
STOREUV(q2, u, v, 2 * stride);
|
||||
}
|
||||
|
||||
static void HFilter8SSE2(uint8_t* u, uint8_t* v, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
__m128i mask;
|
||||
__m128i p3, p2, p1, p0, q0, q1, q2, q3;
|
||||
|
||||
uint8_t* const tu = u - 4;
|
||||
uint8_t* const tv = v - 4;
|
||||
Load16x4(tu, tv, stride, &p3, &p2, &p1, &p0); // p3, p2, p1, p0
|
||||
MAX_DIFF1(p3, p2, p1, p0, mask);
|
||||
|
||||
Load16x4(u, v, stride, &q0, &q1, &q2, &q3); // q0, q1, q2, q3
|
||||
MAX_DIFF2(q3, q2, q1, q0, mask);
|
||||
|
||||
COMPLEX_FL_MASK(p1, p0, q0, q1, thresh, ithresh, mask);
|
||||
DoFilter6(&p2, &p1, &p0, &q0, &q1, &q2, &mask, hev_thresh);
|
||||
|
||||
Store16x4(tu, tv, stride, &p3, &p2, &p1, &p0);
|
||||
Store16x4(u, v, stride, &q0, &q1, &q2, &q3);
|
||||
}
|
||||
|
||||
static void VFilter8iSSE2(uint8_t* u, uint8_t* v, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
__m128i mask;
|
||||
__m128i t1, t2, p1, p0, q0, q1;
|
||||
|
||||
// Load p3, p2, p1, p0
|
||||
LOADUV_H_EDGES4(u, v, stride, t2, t1, p1, p0);
|
||||
MAX_DIFF1(t2, t1, p1, p0, mask);
|
||||
|
||||
u += 4 * stride;
|
||||
v += 4 * stride;
|
||||
|
||||
// Load q0, q1, q2, q3
|
||||
LOADUV_H_EDGES4(u, v, stride, q0, q1, t1, t2);
|
||||
MAX_DIFF2(t2, t1, q1, q0, mask);
|
||||
|
||||
COMPLEX_FL_MASK(p1, p0, q0, q1, thresh, ithresh, mask);
|
||||
DoFilter4(&p1, &p0, &q0, &q1, &mask, hev_thresh);
|
||||
|
||||
// Store
|
||||
STOREUV(p1, u, v, -2 * stride);
|
||||
STOREUV(p0, u, v, -1 * stride);
|
||||
STOREUV(q0, u, v, 0 * stride);
|
||||
STOREUV(q1, u, v, 1 * stride);
|
||||
}
|
||||
|
||||
static void HFilter8iSSE2(uint8_t* u, uint8_t* v, int stride,
|
||||
int thresh, int ithresh, int hev_thresh) {
|
||||
__m128i mask;
|
||||
__m128i t1, t2, p1, p0, q0, q1;
|
||||
Load16x4(u, v, stride, &t2, &t1, &p1, &p0); // p3, p2, p1, p0
|
||||
MAX_DIFF1(t2, t1, p1, p0, mask);
|
||||
|
||||
u += 4; // beginning of q0
|
||||
v += 4;
|
||||
Load16x4(u, v, stride, &q0, &q1, &t1, &t2); // q0, q1, q2, q3
|
||||
MAX_DIFF2(t2, t1, q1, q0, mask);
|
||||
|
||||
COMPLEX_FL_MASK(p1, p0, q0, q1, thresh, ithresh, mask);
|
||||
DoFilter4(&p1, &p0, &q0, &q1, &mask, hev_thresh);
|
||||
|
||||
u -= 2; // beginning of p1
|
||||
v -= 2;
|
||||
Store16x4(u, v, stride, &p1, &p0, &q0, &q1);
|
||||
}
|
||||
|
||||
extern void VP8DspInitSSE2(void);
|
||||
|
||||
void VP8DspInitSSE2(void) {
|
||||
VP8Transform = TransformSSE2;
|
||||
|
||||
VP8VFilter16 = VFilter16SSE2;
|
||||
VP8HFilter16 = HFilter16SSE2;
|
||||
VP8VFilter8 = VFilter8SSE2;
|
||||
VP8HFilter8 = HFilter8SSE2;
|
||||
VP8VFilter16i = VFilter16iSSE2;
|
||||
VP8HFilter16i = HFilter16iSSE2;
|
||||
VP8VFilter8i = VFilter8iSSE2;
|
||||
VP8HFilter8i = HFilter8iSSE2;
|
||||
|
||||
VP8SimpleVFilter16 = SimpleVFilter16SSE2;
|
||||
VP8SimpleHFilter16 = SimpleHFilter16SSE2;
|
||||
VP8SimpleVFilter16i = SimpleVFilter16iSSE2;
|
||||
VP8SimpleHFilter16i = SimpleHFilter16iSSE2;
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif //__SSE2__ || _MSC_VER
|
174
src/dsp/dsp.h
Normal file
174
src/dsp/dsp.h
Normal file
@ -0,0 +1,174 @@
|
||||
// Copyright 2011 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Speed-critical functions.
|
||||
//
|
||||
// Author: Skal (pascal.massimino@gmail.com)
|
||||
|
||||
#ifndef WEBP_DSP_DSP_H_
|
||||
#define WEBP_DSP_DSP_H_
|
||||
|
||||
#include "../webp/types.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// CPU detection
|
||||
|
||||
typedef enum {
|
||||
kSSE2,
|
||||
kSSE3
|
||||
} CPUFeature;
|
||||
// returns true if the CPU supports the feature.
|
||||
typedef int (*VP8CPUInfo)(CPUFeature feature);
|
||||
extern VP8CPUInfo VP8GetCPUInfo;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Encoding
|
||||
|
||||
int VP8GetAlpha(const int histo[]);
|
||||
|
||||
// Transforms
|
||||
// VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
|
||||
// will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
|
||||
typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
|
||||
int do_two);
|
||||
typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
|
||||
typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
|
||||
extern VP8Idct VP8ITransform;
|
||||
extern VP8Fdct VP8FTransform;
|
||||
extern VP8WHT VP8ITransformWHT;
|
||||
extern VP8WHT VP8FTransformWHT;
|
||||
// Predictions
|
||||
// *dst is the destination block. *top and *left can be NULL.
|
||||
typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left,
|
||||
const uint8_t* top);
|
||||
typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top);
|
||||
extern VP8Intra4Preds VP8EncPredLuma4;
|
||||
extern VP8IntraPreds VP8EncPredLuma16;
|
||||
extern VP8IntraPreds VP8EncPredChroma8;
|
||||
|
||||
typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
|
||||
extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
|
||||
typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
|
||||
const uint16_t* const weights);
|
||||
extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
|
||||
|
||||
typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
|
||||
extern VP8BlockCopy VP8Copy4x4;
|
||||
extern VP8BlockCopy VP8Copy8x8;
|
||||
extern VP8BlockCopy VP8Copy16x16;
|
||||
// Quantization
|
||||
struct VP8Matrix; // forward declaration
|
||||
typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
|
||||
int n, const struct VP8Matrix* const mtx);
|
||||
extern VP8QuantizeBlock VP8EncQuantizeBlock;
|
||||
|
||||
// Compute susceptibility based on DCT-coeff histograms:
|
||||
// the higher, the "easier" the macroblock is to compress.
|
||||
typedef int (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
|
||||
int start_block, int end_block);
|
||||
extern const int VP8DspScan[16 + 4 + 4];
|
||||
extern VP8CHisto VP8CollectHistogram;
|
||||
|
||||
void VP8EncDspInit(void); // must be called before using any of the above
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Decoding
|
||||
|
||||
typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
|
||||
// when doing two transforms, coeffs is actually int16_t[2][16].
|
||||
typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
|
||||
extern VP8DecIdct2 VP8Transform;
|
||||
extern VP8DecIdct VP8TransformUV;
|
||||
extern VP8DecIdct VP8TransformDC;
|
||||
extern VP8DecIdct VP8TransformDCUV;
|
||||
extern void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
|
||||
|
||||
// *dst is the destination block, with stride BPS. Boundary samples are
|
||||
// assumed accessible when needed.
|
||||
typedef void (*VP8PredFunc)(uint8_t* dst);
|
||||
extern VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
|
||||
extern VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
|
||||
extern VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
|
||||
|
||||
// simple filter (only for luma)
|
||||
typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
|
||||
extern VP8SimpleFilterFunc VP8SimpleVFilter16;
|
||||
extern VP8SimpleFilterFunc VP8SimpleHFilter16;
|
||||
extern VP8SimpleFilterFunc VP8SimpleVFilter16i; // filter 3 inner edges
|
||||
extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
|
||||
|
||||
// regular filter (on both macroblock edges and inner edges)
|
||||
typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
|
||||
int thresh, int ithresh, int hev_t);
|
||||
typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
|
||||
int thresh, int ithresh, int hev_t);
|
||||
// on outer edge
|
||||
extern VP8LumaFilterFunc VP8VFilter16;
|
||||
extern VP8LumaFilterFunc VP8HFilter16;
|
||||
extern VP8ChromaFilterFunc VP8VFilter8;
|
||||
extern VP8ChromaFilterFunc VP8HFilter8;
|
||||
|
||||
// on inner edge
|
||||
extern VP8LumaFilterFunc VP8VFilter16i; // filtering 3 inner edges altogether
|
||||
extern VP8LumaFilterFunc VP8HFilter16i;
|
||||
extern VP8ChromaFilterFunc VP8VFilter8i; // filtering u and v altogether
|
||||
extern VP8ChromaFilterFunc VP8HFilter8i;
|
||||
|
||||
// must be called before anything using the above
|
||||
extern void VP8DspInit(void);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// WebP I/O
|
||||
|
||||
#define FANCY_UPSAMPLING // undefined to remove fancy upsampling support
|
||||
|
||||
#ifdef FANCY_UPSAMPLING
|
||||
typedef void (*WebPUpsampleLinePairFunc)(
|
||||
const uint8_t* top_y, const uint8_t* bottom_y,
|
||||
const uint8_t* top_u, const uint8_t* top_v,
|
||||
const uint8_t* cur_u, const uint8_t* cur_v,
|
||||
uint8_t* top_dst, uint8_t* bottom_dst, int len);
|
||||
|
||||
|
||||
// Fancy upsampling functions to convert YUV to RGB(A) modes
|
||||
extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
|
||||
extern WebPUpsampleLinePairFunc WebPUpsamplersKeepAlpha[/* MODE_LAST */];
|
||||
|
||||
// Initializes SSE2 version of the fancy upsamplers.
|
||||
void WebPInitUpsamplersSSE2(void);
|
||||
|
||||
#endif // FANCY_UPSAMPLING
|
||||
|
||||
// Point-sampling methods.
|
||||
typedef void (*WebPSampleLinePairFunc)(
|
||||
const uint8_t* top_y, const uint8_t* bottom_y,
|
||||
const uint8_t* u, const uint8_t* v,
|
||||
uint8_t* top_dst, uint8_t* bottom_dst, int len);
|
||||
|
||||
extern const WebPSampleLinePairFunc WebPSamplers[/* MODE_LAST */];
|
||||
|
||||
// YUV444->RGB converters
|
||||
typedef void (*WebPYUV444Converter)(const uint8_t* y,
|
||||
const uint8_t* u, const uint8_t* v,
|
||||
uint8_t* dst, int len);
|
||||
|
||||
extern const WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
|
||||
|
||||
// Main function to be called
|
||||
void WebPInitUpsamplers(void);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif // WEBP_DSP_DSP_H_
|
744
src/dsp/enc.c
Normal file
744
src/dsp/enc.c
Normal file
@ -0,0 +1,744 @@
|
||||
// Copyright 2011 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Speed-critical encoding functions.
|
||||
//
|
||||
// Author: Skal (pascal.massimino@gmail.com)
|
||||
|
||||
#include "../enc/vp8enci.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Compute susceptibility based on DCT-coeff histograms:
|
||||
// the higher, the "easier" the macroblock is to compress.
|
||||
|
||||
static int ClipAlpha(int alpha) {
|
||||
return alpha < 0 ? 0 : alpha > 255 ? 255 : alpha;
|
||||
}
|
||||
|
||||
int VP8GetAlpha(const int histo[MAX_COEFF_THRESH + 1]) {
|
||||
int num = 0, den = 0, val = 0;
|
||||
int k;
|
||||
int alpha;
|
||||
// note: changing this loop to avoid the numerous "k + 1" slows things down.
|
||||
for (k = 0; k < MAX_COEFF_THRESH; ++k) {
|
||||
if (histo[k + 1]) {
|
||||
val += histo[k + 1];
|
||||
num += val * (k + 1);
|
||||
den += (k + 1) * (k + 1);
|
||||
}
|
||||
}
|
||||
// we scale the value to a usable [0..255] range
|
||||
alpha = den ? 10 * num / den - 5 : 0;
|
||||
return ClipAlpha(alpha);
|
||||
}
|
||||
|
||||
const int VP8DspScan[16 + 4 + 4] = {
|
||||
// Luma
|
||||
0 + 0 * BPS, 4 + 0 * BPS, 8 + 0 * BPS, 12 + 0 * BPS,
|
||||
0 + 4 * BPS, 4 + 4 * BPS, 8 + 4 * BPS, 12 + 4 * BPS,
|
||||
0 + 8 * BPS, 4 + 8 * BPS, 8 + 8 * BPS, 12 + 8 * BPS,
|
||||
0 + 12 * BPS, 4 + 12 * BPS, 8 + 12 * BPS, 12 + 12 * BPS,
|
||||
|
||||
0 + 0 * BPS, 4 + 0 * BPS, 0 + 4 * BPS, 4 + 4 * BPS, // U
|
||||
8 + 0 * BPS, 12 + 0 * BPS, 8 + 4 * BPS, 12 + 4 * BPS // V
|
||||
};
|
||||
|
||||
static int CollectHistogram(const uint8_t* ref, const uint8_t* pred,
|
||||
int start_block, int end_block) {
|
||||
int histo[MAX_COEFF_THRESH + 1] = { 0 };
|
||||
int16_t out[16];
|
||||
int j, k;
|
||||
for (j = start_block; j < end_block; ++j) {
|
||||
VP8FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
|
||||
|
||||
// Convert coefficients to bin (within out[]).
|
||||
for (k = 0; k < 16; ++k) {
|
||||
const int v = abs(out[k]) >> 2;
|
||||
out[k] = (v > MAX_COEFF_THRESH) ? MAX_COEFF_THRESH : v;
|
||||
}
|
||||
|
||||
// Use bin to update histogram.
|
||||
for (k = 0; k < 16; ++k) {
|
||||
histo[out[k]]++;
|
||||
}
|
||||
}
|
||||
|
||||
return VP8GetAlpha(histo);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// run-time tables (~4k)
|
||||
|
||||
static uint8_t clip1[255 + 510 + 1]; // clips [-255,510] to [0,255]
|
||||
|
||||
// We declare this variable 'volatile' to prevent instruction reordering
|
||||
// and make sure it's set to true _last_ (so as to be thread-safe)
|
||||
static volatile int tables_ok = 0;
|
||||
|
||||
static void InitTables(void) {
|
||||
if (!tables_ok) {
|
||||
int i;
|
||||
for (i = -255; i <= 255 + 255; ++i) {
|
||||
clip1[255 + i] = (i < 0) ? 0 : (i > 255) ? 255 : i;
|
||||
}
|
||||
tables_ok = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint8_t clip_8b(int v) {
|
||||
return (!(v & ~0xff)) ? v : v < 0 ? 0 : 255;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Transforms (Paragraph 14.4)
|
||||
|
||||
#define STORE(x, y, v) \
|
||||
dst[(x) + (y) * BPS] = clip_8b(ref[(x) + (y) * BPS] + ((v) >> 3))
|
||||
|
||||
static const int kC1 = 20091 + (1 << 16);
|
||||
static const int kC2 = 35468;
|
||||
#define MUL(a, b) (((a) * (b)) >> 16)
|
||||
|
||||
static inline void ITransformOne(const uint8_t* ref, const int16_t* in,
|
||||
uint8_t* dst) {
|
||||
int C[4 * 4], *tmp;
|
||||
int i;
|
||||
tmp = C;
|
||||
for (i = 0; i < 4; ++i) { // vertical pass
|
||||
const int a = in[0] + in[8];
|
||||
const int b = in[0] - in[8];
|
||||
const int c = MUL(in[4], kC2) - MUL(in[12], kC1);
|
||||
const int d = MUL(in[4], kC1) + MUL(in[12], kC2);
|
||||
tmp[0] = a + d;
|
||||
tmp[1] = b + c;
|
||||
tmp[2] = b - c;
|
||||
tmp[3] = a - d;
|
||||
tmp += 4;
|
||||
in++;
|
||||
}
|
||||
|
||||
tmp = C;
|
||||
for (i = 0; i < 4; ++i) { // horizontal pass
|
||||
const int dc = tmp[0] + 4;
|
||||
const int a = dc + tmp[8];
|
||||
const int b = dc - tmp[8];
|
||||
const int c = MUL(tmp[4], kC2) - MUL(tmp[12], kC1);
|
||||
const int d = MUL(tmp[4], kC1) + MUL(tmp[12], kC2);
|
||||
STORE(0, i, a + d);
|
||||
STORE(1, i, b + c);
|
||||
STORE(2, i, b - c);
|
||||
STORE(3, i, a - d);
|
||||
tmp++;
|
||||
}
|
||||
}
|
||||
|
||||
static void ITransform(const uint8_t* ref, const int16_t* in, uint8_t* dst,
|
||||
int do_two) {
|
||||
ITransformOne(ref, in, dst);
|
||||
if (do_two) {
|
||||
ITransformOne(ref + 4, in + 16, dst + 4);
|
||||
}
|
||||
}
|
||||
|
||||
static void FTransform(const uint8_t* src, const uint8_t* ref, int16_t* out) {
|
||||
int i;
|
||||
int tmp[16];
|
||||
for (i = 0; i < 4; ++i, src += BPS, ref += BPS) {
|
||||
const int d0 = src[0] - ref[0];
|
||||
const int d1 = src[1] - ref[1];
|
||||
const int d2 = src[2] - ref[2];
|
||||
const int d3 = src[3] - ref[3];
|
||||
const int a0 = (d0 + d3) << 3;
|
||||
const int a1 = (d1 + d2) << 3;
|
||||
const int a2 = (d1 - d2) << 3;
|
||||
const int a3 = (d0 - d3) << 3;
|
||||
tmp[0 + i * 4] = (a0 + a1);
|
||||
tmp[1 + i * 4] = (a2 * 2217 + a3 * 5352 + 14500) >> 12;
|
||||
tmp[2 + i * 4] = (a0 - a1);
|
||||
tmp[3 + i * 4] = (a3 * 2217 - a2 * 5352 + 7500) >> 12;
|
||||
}
|
||||
for (i = 0; i < 4; ++i) {
|
||||
const int a0 = (tmp[0 + i] + tmp[12 + i]);
|
||||
const int a1 = (tmp[4 + i] + tmp[ 8 + i]);
|
||||
const int a2 = (tmp[4 + i] - tmp[ 8 + i]);
|
||||
const int a3 = (tmp[0 + i] - tmp[12 + i]);
|
||||
out[0 + i] = (a0 + a1 + 7) >> 4;
|
||||
out[4 + i] = ((a2 * 2217 + a3 * 5352 + 12000) >> 16) + (a3 != 0);
|
||||
out[8 + i] = (a0 - a1 + 7) >> 4;
|
||||
out[12+ i] = ((a3 * 2217 - a2 * 5352 + 51000) >> 16);
|
||||
}
|
||||
}
|
||||
|
||||
static void ITransformWHT(const int16_t* in, int16_t* out) {
|
||||
int tmp[16];
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
const int a0 = in[0 + i] + in[12 + i];
|
||||
const int a1 = in[4 + i] + in[ 8 + i];
|
||||
const int a2 = in[4 + i] - in[ 8 + i];
|
||||
const int a3 = in[0 + i] - in[12 + i];
|
||||
tmp[0 + i] = a0 + a1;
|
||||
tmp[8 + i] = a0 - a1;
|
||||
tmp[4 + i] = a3 + a2;
|
||||
tmp[12 + i] = a3 - a2;
|
||||
}
|
||||
for (i = 0; i < 4; ++i) {
|
||||
const int dc = tmp[0 + i * 4] + 3; // w/ rounder
|
||||
const int a0 = dc + tmp[3 + i * 4];
|
||||
const int a1 = tmp[1 + i * 4] + tmp[2 + i * 4];
|
||||
const int a2 = tmp[1 + i * 4] - tmp[2 + i * 4];
|
||||
const int a3 = dc - tmp[3 + i * 4];
|
||||
out[ 0] = (a0 + a1) >> 3;
|
||||
out[16] = (a3 + a2) >> 3;
|
||||
out[32] = (a0 - a1) >> 3;
|
||||
out[48] = (a3 - a2) >> 3;
|
||||
out += 64;
|
||||
}
|
||||
}
|
||||
|
||||
static void FTransformWHT(const int16_t* in, int16_t* out) {
|
||||
int tmp[16];
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i, in += 64) {
|
||||
const int a0 = (in[0 * 16] + in[2 * 16]) << 2;
|
||||
const int a1 = (in[1 * 16] + in[3 * 16]) << 2;
|
||||
const int a2 = (in[1 * 16] - in[3 * 16]) << 2;
|
||||
const int a3 = (in[0 * 16] - in[2 * 16]) << 2;
|
||||
tmp[0 + i * 4] = (a0 + a1) + (a0 != 0);
|
||||
tmp[1 + i * 4] = a3 + a2;
|
||||
tmp[2 + i * 4] = a3 - a2;
|
||||
tmp[3 + i * 4] = a0 - a1;
|
||||
}
|
||||
for (i = 0; i < 4; ++i) {
|
||||
const int a0 = (tmp[0 + i] + tmp[8 + i]);
|
||||
const int a1 = (tmp[4 + i] + tmp[12+ i]);
|
||||
const int a2 = (tmp[4 + i] - tmp[12+ i]);
|
||||
const int a3 = (tmp[0 + i] - tmp[8 + i]);
|
||||
const int b0 = a0 + a1;
|
||||
const int b1 = a3 + a2;
|
||||
const int b2 = a3 - a2;
|
||||
const int b3 = a0 - a1;
|
||||
out[ 0 + i] = (b0 + (b0 > 0) + 3) >> 3;
|
||||
out[ 4 + i] = (b1 + (b1 > 0) + 3) >> 3;
|
||||
out[ 8 + i] = (b2 + (b2 > 0) + 3) >> 3;
|
||||
out[12 + i] = (b3 + (b3 > 0) + 3) >> 3;
|
||||
}
|
||||
}
|
||||
|
||||
#undef MUL
|
||||
#undef STORE
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Intra predictions
|
||||
|
||||
#define DST(x, y) dst[(x) + (y) * BPS]
|
||||
|
||||
static inline void Fill(uint8_t* dst, int value, int size) {
|
||||
int j;
|
||||
for (j = 0; j < size; ++j) {
|
||||
memset(dst + j * BPS, value, size);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void VerticalPred(uint8_t* dst, const uint8_t* top, int size) {
|
||||
int j;
|
||||
if (top) {
|
||||
for (j = 0; j < size; ++j) memcpy(dst + j * BPS, top, size);
|
||||
} else {
|
||||
Fill(dst, 127, size);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void HorizontalPred(uint8_t* dst, const uint8_t* left, int size) {
|
||||
if (left) {
|
||||
int j;
|
||||
for (j = 0; j < size; ++j) {
|
||||
memset(dst + j * BPS, left[j], size);
|
||||
}
|
||||
} else {
|
||||
Fill(dst, 129, size);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void TrueMotion(uint8_t* dst, const uint8_t* left,
|
||||
const uint8_t* top, int size) {
|
||||
int y;
|
||||
if (left) {
|
||||
if (top) {
|
||||
const uint8_t* const clip = clip1 + 255 - left[-1];
|
||||
for (y = 0; y < size; ++y) {
|
||||
const uint8_t* const clip_table = clip + left[y];
|
||||
int x;
|
||||
for (x = 0; x < size; ++x) {
|
||||
dst[x] = clip_table[top[x]];
|
||||
}
|
||||
dst += BPS;
|
||||
}
|
||||
} else {
|
||||
HorizontalPred(dst, left, size);
|
||||
}
|
||||
} else {
|
||||
// true motion without left samples (hence: with default 129 value)
|
||||
// is equivalent to VE prediction where you just copy the top samples.
|
||||
// Note that if top samples are not available, the default value is
|
||||
// then 129, and not 127 as in the VerticalPred case.
|
||||
if (top) {
|
||||
VerticalPred(dst, top, size);
|
||||
} else {
|
||||
Fill(dst, 129, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void DCMode(uint8_t* dst, const uint8_t* left,
|
||||
const uint8_t* top,
|
||||
int size, int round, int shift) {
|
||||
int DC = 0;
|
||||
int j;
|
||||
if (top) {
|
||||
for (j = 0; j < size; ++j) DC += top[j];
|
||||
if (left) { // top and left present
|
||||
for (j = 0; j < size; ++j) DC += left[j];
|
||||
} else { // top, but no left
|
||||
DC += DC;
|
||||
}
|
||||
DC = (DC + round) >> shift;
|
||||
} else if (left) { // left but no top
|
||||
for (j = 0; j < size; ++j) DC += left[j];
|
||||
DC += DC;
|
||||
DC = (DC + round) >> shift;
|
||||
} else { // no top, no left, nothing.
|
||||
DC = 0x80;
|
||||
}
|
||||
Fill(dst, DC, size);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Chroma 8x8 prediction (paragraph 12.2)
|
||||
|
||||
static void IntraChromaPreds(uint8_t* dst, const uint8_t* left,
|
||||
const uint8_t* top) {
|
||||
// U block
|
||||
DCMode(C8DC8 + dst, left, top, 8, 8, 4);
|
||||
VerticalPred(C8VE8 + dst, top, 8);
|
||||
HorizontalPred(C8HE8 + dst, left, 8);
|
||||
TrueMotion(C8TM8 + dst, left, top, 8);
|
||||
// V block
|
||||
dst += 8;
|
||||
if (top) top += 8;
|
||||
if (left) left += 16;
|
||||
DCMode(C8DC8 + dst, left, top, 8, 8, 4);
|
||||
VerticalPred(C8VE8 + dst, top, 8);
|
||||
HorizontalPred(C8HE8 + dst, left, 8);
|
||||
TrueMotion(C8TM8 + dst, left, top, 8);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// luma 16x16 prediction (paragraph 12.3)
|
||||
|
||||
static void Intra16Preds(uint8_t* dst,
|
||||
const uint8_t* left, const uint8_t* top) {
|
||||
DCMode(I16DC16 + dst, left, top, 16, 16, 5);
|
||||
VerticalPred(I16VE16 + dst, top, 16);
|
||||
HorizontalPred(I16HE16 + dst, left, 16);
|
||||
TrueMotion(I16TM16 + dst, left, top, 16);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// luma 4x4 prediction
|
||||
|
||||
#define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
|
||||
#define AVG2(a, b) (((a) + (b) + 1) >> 1)
|
||||
|
||||
static void VE4(uint8_t* dst, const uint8_t* top) { // vertical
|
||||
const uint8_t vals[4] = {
|
||||
AVG3(top[-1], top[0], top[1]),
|
||||
AVG3(top[ 0], top[1], top[2]),
|
||||
AVG3(top[ 1], top[2], top[3]),
|
||||
AVG3(top[ 2], top[3], top[4])
|
||||
};
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
memcpy(dst + i * BPS, vals, 4);
|
||||
}
|
||||
}
|
||||
|
||||
static void HE4(uint8_t* dst, const uint8_t* top) { // horizontal
|
||||
const int X = top[-1];
|
||||
const int I = top[-2];
|
||||
const int J = top[-3];
|
||||
const int K = top[-4];
|
||||
const int L = top[-5];
|
||||
*(uint32_t*)(dst + 0 * BPS) = 0x01010101U * AVG3(X, I, J);
|
||||
*(uint32_t*)(dst + 1 * BPS) = 0x01010101U * AVG3(I, J, K);
|
||||
*(uint32_t*)(dst + 2 * BPS) = 0x01010101U * AVG3(J, K, L);
|
||||
*(uint32_t*)(dst + 3 * BPS) = 0x01010101U * AVG3(K, L, L);
|
||||
}
|
||||
|
||||
static void DC4(uint8_t* dst, const uint8_t* top) {
|
||||
uint32_t dc = 4;
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i];
|
||||
Fill(dst, dc >> 3, 4);
|
||||
}
|
||||
|
||||
static void RD4(uint8_t* dst, const uint8_t* top) {
|
||||
const int X = top[-1];
|
||||
const int I = top[-2];
|
||||
const int J = top[-3];
|
||||
const int K = top[-4];
|
||||
const int L = top[-5];
|
||||
const int A = top[0];
|
||||
const int B = top[1];
|
||||
const int C = top[2];
|
||||
const int D = top[3];
|
||||
DST(0, 3) = AVG3(J, K, L);
|
||||
DST(0, 2) = DST(1, 3) = AVG3(I, J, K);
|
||||
DST(0, 1) = DST(1, 2) = DST(2, 3) = AVG3(X, I, J);
|
||||
DST(0, 0) = DST(1, 1) = DST(2, 2) = DST(3, 3) = AVG3(A, X, I);
|
||||
DST(1, 0) = DST(2, 1) = DST(3, 2) = AVG3(B, A, X);
|
||||
DST(2, 0) = DST(3, 1) = AVG3(C, B, A);
|
||||
DST(3, 0) = AVG3(D, C, B);
|
||||
}
|
||||
|
||||
static void LD4(uint8_t* dst, const uint8_t* top) {
|
||||
const int A = top[0];
|
||||
const int B = top[1];
|
||||
const int C = top[2];
|
||||
const int D = top[3];
|
||||
const int E = top[4];
|
||||
const int F = top[5];
|
||||
const int G = top[6];
|
||||
const int H = top[7];
|
||||
DST(0, 0) = AVG3(A, B, C);
|
||||
DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
|
||||
DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
|
||||
DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
|
||||
DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
|
||||
DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
|
||||
DST(3, 3) = AVG3(G, H, H);
|
||||
}
|
||||
|
||||
static void VR4(uint8_t* dst, const uint8_t* top) {
|
||||
const int X = top[-1];
|
||||
const int I = top[-2];
|
||||
const int J = top[-3];
|
||||
const int K = top[-4];
|
||||
const int A = top[0];
|
||||
const int B = top[1];
|
||||
const int C = top[2];
|
||||
const int D = top[3];
|
||||
DST(0, 0) = DST(1, 2) = AVG2(X, A);
|
||||
DST(1, 0) = DST(2, 2) = AVG2(A, B);
|
||||
DST(2, 0) = DST(3, 2) = AVG2(B, C);
|
||||
DST(3, 0) = AVG2(C, D);
|
||||
|
||||
DST(0, 3) = AVG3(K, J, I);
|
||||
DST(0, 2) = AVG3(J, I, X);
|
||||
DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
|
||||
DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
|
||||
DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
|
||||
DST(3, 1) = AVG3(B, C, D);
|
||||
}
|
||||
|
||||
static void VL4(uint8_t* dst, const uint8_t* top) {
|
||||
const int A = top[0];
|
||||
const int B = top[1];
|
||||
const int C = top[2];
|
||||
const int D = top[3];
|
||||
const int E = top[4];
|
||||
const int F = top[5];
|
||||
const int G = top[6];
|
||||
const int H = top[7];
|
||||
DST(0, 0) = AVG2(A, B);
|
||||
DST(1, 0) = DST(0, 2) = AVG2(B, C);
|
||||
DST(2, 0) = DST(1, 2) = AVG2(C, D);
|
||||
DST(3, 0) = DST(2, 2) = AVG2(D, E);
|
||||
|
||||
DST(0, 1) = AVG3(A, B, C);
|
||||
DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
|
||||
DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
|
||||
DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
|
||||
DST(3, 2) = AVG3(E, F, G);
|
||||
DST(3, 3) = AVG3(F, G, H);
|
||||
}
|
||||
|
||||
static void HU4(uint8_t* dst, const uint8_t* top) {
|
||||
const int I = top[-2];
|
||||
const int J = top[-3];
|
||||
const int K = top[-4];
|
||||
const int L = top[-5];
|
||||
DST(0, 0) = AVG2(I, J);
|
||||
DST(2, 0) = DST(0, 1) = AVG2(J, K);
|
||||
DST(2, 1) = DST(0, 2) = AVG2(K, L);
|
||||
DST(1, 0) = AVG3(I, J, K);
|
||||
DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
|
||||
DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
|
||||
DST(3, 2) = DST(2, 2) =
|
||||
DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
|
||||
}
|
||||
|
||||
static void HD4(uint8_t* dst, const uint8_t* top) {
|
||||
const int X = top[-1];
|
||||
const int I = top[-2];
|
||||
const int J = top[-3];
|
||||
const int K = top[-4];
|
||||
const int L = top[-5];
|
||||
const int A = top[0];
|
||||
const int B = top[1];
|
||||
const int C = top[2];
|
||||
|
||||
DST(0, 0) = DST(2, 1) = AVG2(I, X);
|
||||
DST(0, 1) = DST(2, 2) = AVG2(J, I);
|
||||
DST(0, 2) = DST(2, 3) = AVG2(K, J);
|
||||
DST(0, 3) = AVG2(L, K);
|
||||
|
||||
DST(3, 0) = AVG3(A, B, C);
|
||||
DST(2, 0) = AVG3(X, A, B);
|
||||
DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
|
||||
DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
|
||||
DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
|
||||
DST(1, 3) = AVG3(L, K, J);
|
||||
}
|
||||
|
||||
static void TM4(uint8_t* dst, const uint8_t* top) {
|
||||
int x, y;
|
||||
const uint8_t* const clip = clip1 + 255 - top[-1];
|
||||
for (y = 0; y < 4; ++y) {
|
||||
const uint8_t* const clip_table = clip + top[-2 - y];
|
||||
for (x = 0; x < 4; ++x) {
|
||||
dst[x] = clip_table[top[x]];
|
||||
}
|
||||
dst += BPS;
|
||||
}
|
||||
}
|
||||
|
||||
#undef DST
|
||||
#undef AVG3
|
||||
#undef AVG2
|
||||
|
||||
// Left samples are top[-5 .. -2], top_left is top[-1], top are
|
||||
// located at top[0..3], and top right is top[4..7]
|
||||
static void Intra4Preds(uint8_t* dst, const uint8_t* top) {
|
||||
DC4(I4DC4 + dst, top);
|
||||
TM4(I4TM4 + dst, top);
|
||||
VE4(I4VE4 + dst, top);
|
||||
HE4(I4HE4 + dst, top);
|
||||
RD4(I4RD4 + dst, top);
|
||||
VR4(I4VR4 + dst, top);
|
||||
LD4(I4LD4 + dst, top);
|
||||
VL4(I4VL4 + dst, top);
|
||||
HD4(I4HD4 + dst, top);
|
||||
HU4(I4HU4 + dst, top);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Metric
|
||||
|
||||
static inline int GetSSE(const uint8_t* a, const uint8_t* b, int w, int h) {
|
||||
int count = 0;
|
||||
int y, x;
|
||||
for (y = 0; y < h; ++y) {
|
||||
for (x = 0; x < w; ++x) {
|
||||
const int diff = (int)a[x] - b[x];
|
||||
count += diff * diff;
|
||||
}
|
||||
a += BPS;
|
||||
b += BPS;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
static int SSE16x16(const uint8_t* a, const uint8_t* b) {
|
||||
return GetSSE(a, b, 16, 16);
|
||||
}
|
||||
static int SSE16x8(const uint8_t* a, const uint8_t* b) {
|
||||
return GetSSE(a, b, 16, 8);
|
||||
}
|
||||
static int SSE8x8(const uint8_t* a, const uint8_t* b) {
|
||||
return GetSSE(a, b, 8, 8);
|
||||
}
|
||||
static int SSE4x4(const uint8_t* a, const uint8_t* b) {
|
||||
return GetSSE(a, b, 4, 4);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Texture distortion
|
||||
//
|
||||
// We try to match the spectral content (weighted) between source and
|
||||
// reconstructed samples.
|
||||
|
||||
// Hadamard transform
|
||||
// Returns the weighted sum of the absolute value of transformed coefficients.
|
||||
static int TTransform(const uint8_t* in, const uint16_t* w) {
|
||||
int sum = 0;
|
||||
int tmp[16];
|
||||
int i;
|
||||
// horizontal pass
|
||||
for (i = 0; i < 4; ++i, in += BPS) {
|
||||
const int a0 = (in[0] + in[2]) << 2;
|
||||
const int a1 = (in[1] + in[3]) << 2;
|
||||
const int a2 = (in[1] - in[3]) << 2;
|
||||
const int a3 = (in[0] - in[2]) << 2;
|
||||
tmp[0 + i * 4] = a0 + a1 + (a0 != 0);
|
||||
tmp[1 + i * 4] = a3 + a2;
|
||||
tmp[2 + i * 4] = a3 - a2;
|
||||
tmp[3 + i * 4] = a0 - a1;
|
||||
}
|
||||
// vertical pass
|
||||
for (i = 0; i < 4; ++i, ++w) {
|
||||
const int a0 = (tmp[0 + i] + tmp[8 + i]);
|
||||
const int a1 = (tmp[4 + i] + tmp[12+ i]);
|
||||
const int a2 = (tmp[4 + i] - tmp[12+ i]);
|
||||
const int a3 = (tmp[0 + i] - tmp[8 + i]);
|
||||
const int b0 = a0 + a1;
|
||||
const int b1 = a3 + a2;
|
||||
const int b2 = a3 - a2;
|
||||
const int b3 = a0 - a1;
|
||||
// abs((b + (b<0) + 3) >> 3) = (abs(b) + 3) >> 3
|
||||
sum += w[ 0] * ((abs(b0) + 3) >> 3);
|
||||
sum += w[ 4] * ((abs(b1) + 3) >> 3);
|
||||
sum += w[ 8] * ((abs(b2) + 3) >> 3);
|
||||
sum += w[12] * ((abs(b3) + 3) >> 3);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
static int Disto4x4(const uint8_t* const a, const uint8_t* const b,
|
||||
const uint16_t* const w) {
|
||||
const int sum1 = TTransform(a, w);
|
||||
const int sum2 = TTransform(b, w);
|
||||
return (abs(sum2 - sum1) + 8) >> 4;
|
||||
}
|
||||
|
||||
static int Disto16x16(const uint8_t* const a, const uint8_t* const b,
|
||||
const uint16_t* const w) {
|
||||
int D = 0;
|
||||
int x, y;
|
||||
for (y = 0; y < 16 * BPS; y += 4 * BPS) {
|
||||
for (x = 0; x < 16; x += 4) {
|
||||
D += Disto4x4(a + x + y, b + x + y, w);
|
||||
}
|
||||
}
|
||||
return D;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Quantization
|
||||
//
|
||||
|
||||
static const uint8_t kZigzag[16] = {
|
||||
0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
|
||||
};
|
||||
|
||||
// Simple quantization
|
||||
static int QuantizeBlock(int16_t in[16], int16_t out[16],
|
||||
int n, const VP8Matrix* const mtx) {
|
||||
int last = -1;
|
||||
for (; n < 16; ++n) {
|
||||
const int j = kZigzag[n];
|
||||
const int sign = (in[j] < 0);
|
||||
int coeff = (sign ? -in[j] : in[j]) + mtx->sharpen_[j];
|
||||
if (coeff > 2047) coeff = 2047;
|
||||
if (coeff > mtx->zthresh_[j]) {
|
||||
const int Q = mtx->q_[j];
|
||||
const int iQ = mtx->iq_[j];
|
||||
const int B = mtx->bias_[j];
|
||||
out[n] = QUANTDIV(coeff, iQ, B);
|
||||
if (sign) out[n] = -out[n];
|
||||
in[j] = out[n] * Q;
|
||||
if (out[n]) last = n;
|
||||
} else {
|
||||
out[n] = 0;
|
||||
in[j] = 0;
|
||||
}
|
||||
}
|
||||
return (last >= 0);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Block copy
|
||||
|
||||
static inline void Copy(const uint8_t* src, uint8_t* dst, int size) {
|
||||
int y;
|
||||
for (y = 0; y < size; ++y) {
|
||||
memcpy(dst, src, size);
|
||||
src += BPS;
|
||||
dst += BPS;
|
||||
}
|
||||
}
|
||||
|
||||
static void Copy4x4(const uint8_t* src, uint8_t* dst) { Copy(src, dst, 4); }
|
||||
static void Copy8x8(const uint8_t* src, uint8_t* dst) { Copy(src, dst, 8); }
|
||||
static void Copy16x16(const uint8_t* src, uint8_t* dst) { Copy(src, dst, 16); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Initialization
|
||||
|
||||
// Speed-critical function pointers. We have to initialize them to the default
|
||||
// implementations within VP8EncDspInit().
|
||||
VP8CHisto VP8CollectHistogram;
|
||||
VP8Idct VP8ITransform;
|
||||
VP8Fdct VP8FTransform;
|
||||
VP8WHT VP8ITransformWHT;
|
||||
VP8WHT VP8FTransformWHT;
|
||||
VP8Intra4Preds VP8EncPredLuma4;
|
||||
VP8IntraPreds VP8EncPredLuma16;
|
||||
VP8IntraPreds VP8EncPredChroma8;
|
||||
VP8Metric VP8SSE16x16;
|
||||
VP8Metric VP8SSE8x8;
|
||||
VP8Metric VP8SSE16x8;
|
||||
VP8Metric VP8SSE4x4;
|
||||
VP8WMetric VP8TDisto4x4;
|
||||
VP8WMetric VP8TDisto16x16;
|
||||
VP8QuantizeBlock VP8EncQuantizeBlock;
|
||||
VP8BlockCopy VP8Copy4x4;
|
||||
VP8BlockCopy VP8Copy8x8;
|
||||
VP8BlockCopy VP8Copy16x16;
|
||||
|
||||
extern void VP8EncDspInitSSE2(void);
|
||||
|
||||
void VP8EncDspInit(void) {
|
||||
InitTables();
|
||||
|
||||
// default C implementations
|
||||
VP8CollectHistogram = CollectHistogram;
|
||||
VP8ITransform = ITransform;
|
||||
VP8FTransform = FTransform;
|
||||
VP8ITransformWHT = ITransformWHT;
|
||||
VP8FTransformWHT = FTransformWHT;
|
||||
VP8EncPredLuma4 = Intra4Preds;
|
||||
VP8EncPredLuma16 = Intra16Preds;
|
||||
VP8EncPredChroma8 = IntraChromaPreds;
|
||||
VP8SSE16x16 = SSE16x16;
|
||||
VP8SSE8x8 = SSE8x8;
|
||||
VP8SSE16x8 = SSE16x8;
|
||||
VP8SSE4x4 = SSE4x4;
|
||||
VP8TDisto4x4 = Disto4x4;
|
||||
VP8TDisto16x16 = Disto16x16;
|
||||
VP8EncQuantizeBlock = QuantizeBlock;
|
||||
VP8Copy4x4 = Copy4x4;
|
||||
VP8Copy8x8 = Copy8x8;
|
||||
VP8Copy16x16 = Copy16x16;
|
||||
|
||||
// If defined, use CPUInfo() to overwrite some pointers with faster versions.
|
||||
if (VP8GetCPUInfo) {
|
||||
#if defined(__SSE2__) || defined(_MSC_VER)
|
||||
if (VP8GetCPUInfo(kSSE2)) {
|
||||
VP8EncDspInitSSE2();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
834
src/dsp/enc_sse2.c
Normal file
834
src/dsp/enc_sse2.c
Normal file
@ -0,0 +1,834 @@
|
||||
// Copyright 2011 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// SSE2 version of speed-critical encoding functions.
|
||||
//
|
||||
// Author: Christian Duvivier (cduvivier@google.com)
|
||||
|
||||
#if defined(__SSE2__) || defined(_MSC_VER)
|
||||
#include <emmintrin.h>
|
||||
|
||||
#include "../enc/vp8enci.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Compute susceptibility based on DCT-coeff histograms:
|
||||
// the higher, the "easier" the macroblock is to compress.
|
||||
|
||||
static int CollectHistogramSSE2(const uint8_t* ref, const uint8_t* pred,
|
||||
int start_block, int end_block) {
|
||||
int histo[MAX_COEFF_THRESH + 1] = { 0 };
|
||||
int16_t out[16];
|
||||
int j, k;
|
||||
const __m128i max_coeff_thresh = _mm_set1_epi16(MAX_COEFF_THRESH);
|
||||
for (j = start_block; j < end_block; ++j) {
|
||||
VP8FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
|
||||
|
||||
// Convert coefficients to bin (within out[]).
|
||||
{
|
||||
// Load.
|
||||
const __m128i out0 = _mm_loadu_si128((__m128i*)&out[0]);
|
||||
const __m128i out1 = _mm_loadu_si128((__m128i*)&out[8]);
|
||||
// sign(out) = out >> 15 (0x0000 if positive, 0xffff if negative)
|
||||
const __m128i sign0 = _mm_srai_epi16(out0, 15);
|
||||
const __m128i sign1 = _mm_srai_epi16(out1, 15);
|
||||
// abs(out) = (out ^ sign) - sign
|
||||
const __m128i xor0 = _mm_xor_si128(out0, sign0);
|
||||
const __m128i xor1 = _mm_xor_si128(out1, sign1);
|
||||
const __m128i abs0 = _mm_sub_epi16(xor0, sign0);
|
||||
const __m128i abs1 = _mm_sub_epi16(xor1, sign1);
|
||||
// v = abs(out) >> 2
|
||||
const __m128i v0 = _mm_srai_epi16(abs0, 2);
|
||||
const __m128i v1 = _mm_srai_epi16(abs1, 2);
|
||||
// bin = min(v, MAX_COEFF_THRESH)
|
||||
const __m128i bin0 = _mm_min_epi16(v0, max_coeff_thresh);
|
||||
const __m128i bin1 = _mm_min_epi16(v1, max_coeff_thresh);
|
||||
// Store.
|
||||
_mm_storeu_si128((__m128i*)&out[0], bin0);
|
||||
_mm_storeu_si128((__m128i*)&out[8], bin1);
|
||||
}
|
||||
|
||||
// Use bin to update histogram.
|
||||
for (k = 0; k < 16; ++k) {
|
||||
histo[out[k]]++;
|
||||
}
|
||||
}
|
||||
|
||||
return VP8GetAlpha(histo);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Transforms (Paragraph 14.4)
|
||||
|
||||
// Does one or two inverse transforms.
|
||||
static void ITransformSSE2(const uint8_t* ref, const int16_t* in, uint8_t* dst,
|
||||
int do_two) {
|
||||
// This implementation makes use of 16-bit fixed point versions of two
|
||||
// multiply constants:
|
||||
// K1 = sqrt(2) * cos (pi/8) ~= 85627 / 2^16
|
||||
// K2 = sqrt(2) * sin (pi/8) ~= 35468 / 2^16
|
||||
//
|
||||
// To be able to use signed 16-bit integers, we use the following trick to
|
||||
// have constants within range:
|
||||
// - Associated constants are obtained by subtracting the 16-bit fixed point
|
||||
// version of one:
|
||||
// k = K - (1 << 16) => K = k + (1 << 16)
|
||||
// K1 = 85267 => k1 = 20091
|
||||
// K2 = 35468 => k2 = -30068
|
||||
// - The multiplication of a variable by a constant become the sum of the
|
||||
// variable and the multiplication of that variable by the associated
|
||||
// constant:
|
||||
// (x * K) >> 16 = (x * (k + (1 << 16))) >> 16 = ((x * k ) >> 16) + x
|
||||
const __m128i k1 = _mm_set1_epi16(20091);
|
||||
const __m128i k2 = _mm_set1_epi16(-30068);
|
||||
__m128i T0, T1, T2, T3;
|
||||
|
||||
// Load and concatenate the transform coefficients (we'll do two inverse
|
||||
// transforms in parallel). In the case of only one inverse transform, the
|
||||
// second half of the vectors will just contain random value we'll never
|
||||
// use nor store.
|
||||
__m128i in0, in1, in2, in3;
|
||||
{
|
||||
in0 = _mm_loadl_epi64((__m128i*)&in[0]);
|
||||
in1 = _mm_loadl_epi64((__m128i*)&in[4]);
|
||||
in2 = _mm_loadl_epi64((__m128i*)&in[8]);
|
||||
in3 = _mm_loadl_epi64((__m128i*)&in[12]);
|
||||
// a00 a10 a20 a30 x x x x
|
||||
// a01 a11 a21 a31 x x x x
|
||||
// a02 a12 a22 a32 x x x x
|
||||
// a03 a13 a23 a33 x x x x
|
||||
if (do_two) {
|
||||
const __m128i inB0 = _mm_loadl_epi64((__m128i*)&in[16]);
|
||||
const __m128i inB1 = _mm_loadl_epi64((__m128i*)&in[20]);
|
||||
const __m128i inB2 = _mm_loadl_epi64((__m128i*)&in[24]);
|
||||
const __m128i inB3 = _mm_loadl_epi64((__m128i*)&in[28]);
|
||||
in0 = _mm_unpacklo_epi64(in0, inB0);
|
||||
in1 = _mm_unpacklo_epi64(in1, inB1);
|
||||
in2 = _mm_unpacklo_epi64(in2, inB2);
|
||||
in3 = _mm_unpacklo_epi64(in3, inB3);
|
||||
// a00 a10 a20 a30 b00 b10 b20 b30
|
||||
// a01 a11 a21 a31 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 b02 b12 b22 b32
|
||||
// a03 a13 a23 a33 b03 b13 b23 b33
|
||||
}
|
||||
}
|
||||
|
||||
// Vertical pass and subsequent transpose.
|
||||
{
|
||||
// First pass, c and d calculations are longer because of the "trick"
|
||||
// multiplications.
|
||||
const __m128i a = _mm_add_epi16(in0, in2);
|
||||
const __m128i b = _mm_sub_epi16(in0, in2);
|
||||
// c = MUL(in1, K2) - MUL(in3, K1) = MUL(in1, k2) - MUL(in3, k1) + in1 - in3
|
||||
const __m128i c1 = _mm_mulhi_epi16(in1, k2);
|
||||
const __m128i c2 = _mm_mulhi_epi16(in3, k1);
|
||||
const __m128i c3 = _mm_sub_epi16(in1, in3);
|
||||
const __m128i c4 = _mm_sub_epi16(c1, c2);
|
||||
const __m128i c = _mm_add_epi16(c3, c4);
|
||||
// d = MUL(in1, K1) + MUL(in3, K2) = MUL(in1, k1) + MUL(in3, k2) + in1 + in3
|
||||
const __m128i d1 = _mm_mulhi_epi16(in1, k1);
|
||||
const __m128i d2 = _mm_mulhi_epi16(in3, k2);
|
||||
const __m128i d3 = _mm_add_epi16(in1, in3);
|
||||
const __m128i d4 = _mm_add_epi16(d1, d2);
|
||||
const __m128i d = _mm_add_epi16(d3, d4);
|
||||
|
||||
// Second pass.
|
||||
const __m128i tmp0 = _mm_add_epi16(a, d);
|
||||
const __m128i tmp1 = _mm_add_epi16(b, c);
|
||||
const __m128i tmp2 = _mm_sub_epi16(b, c);
|
||||
const __m128i tmp3 = _mm_sub_epi16(a, d);
|
||||
|
||||
// Transpose the two 4x4.
|
||||
// a00 a01 a02 a03 b00 b01 b02 b03
|
||||
// a10 a11 a12 a13 b10 b11 b12 b13
|
||||
// a20 a21 a22 a23 b20 b21 b22 b23
|
||||
// a30 a31 a32 a33 b30 b31 b32 b33
|
||||
const __m128i transpose0_0 = _mm_unpacklo_epi16(tmp0, tmp1);
|
||||
const __m128i transpose0_1 = _mm_unpacklo_epi16(tmp2, tmp3);
|
||||
const __m128i transpose0_2 = _mm_unpackhi_epi16(tmp0, tmp1);
|
||||
const __m128i transpose0_3 = _mm_unpackhi_epi16(tmp2, tmp3);
|
||||
// a00 a10 a01 a11 a02 a12 a03 a13
|
||||
// a20 a30 a21 a31 a22 a32 a23 a33
|
||||
// b00 b10 b01 b11 b02 b12 b03 b13
|
||||
// b20 b30 b21 b31 b22 b32 b23 b33
|
||||
const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
|
||||
const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
|
||||
// a00 a10 a20 a30 a01 a11 a21 a31
|
||||
// b00 b10 b20 b30 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 a03 a13 a23 a33
|
||||
// b02 b12 a22 b32 b03 b13 b23 b33
|
||||
T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
|
||||
T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
|
||||
T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
|
||||
T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
|
||||
// a00 a10 a20 a30 b00 b10 b20 b30
|
||||
// a01 a11 a21 a31 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 b02 b12 b22 b32
|
||||
// a03 a13 a23 a33 b03 b13 b23 b33
|
||||
}
|
||||
|
||||
// Horizontal pass and subsequent transpose.
|
||||
{
|
||||
// First pass, c and d calculations are longer because of the "trick"
|
||||
// multiplications.
|
||||
const __m128i four = _mm_set1_epi16(4);
|
||||
const __m128i dc = _mm_add_epi16(T0, four);
|
||||
const __m128i a = _mm_add_epi16(dc, T2);
|
||||
const __m128i b = _mm_sub_epi16(dc, T2);
|
||||
// c = MUL(T1, K2) - MUL(T3, K1) = MUL(T1, k2) - MUL(T3, k1) + T1 - T3
|
||||
const __m128i c1 = _mm_mulhi_epi16(T1, k2);
|
||||
const __m128i c2 = _mm_mulhi_epi16(T3, k1);
|
||||
const __m128i c3 = _mm_sub_epi16(T1, T3);
|
||||
const __m128i c4 = _mm_sub_epi16(c1, c2);
|
||||
const __m128i c = _mm_add_epi16(c3, c4);
|
||||
// d = MUL(T1, K1) + MUL(T3, K2) = MUL(T1, k1) + MUL(T3, k2) + T1 + T3
|
||||
const __m128i d1 = _mm_mulhi_epi16(T1, k1);
|
||||
const __m128i d2 = _mm_mulhi_epi16(T3, k2);
|
||||
const __m128i d3 = _mm_add_epi16(T1, T3);
|
||||
const __m128i d4 = _mm_add_epi16(d1, d2);
|
||||
const __m128i d = _mm_add_epi16(d3, d4);
|
||||
|
||||
// Second pass.
|
||||
const __m128i tmp0 = _mm_add_epi16(a, d);
|
||||
const __m128i tmp1 = _mm_add_epi16(b, c);
|
||||
const __m128i tmp2 = _mm_sub_epi16(b, c);
|
||||
const __m128i tmp3 = _mm_sub_epi16(a, d);
|
||||
const __m128i shifted0 = _mm_srai_epi16(tmp0, 3);
|
||||
const __m128i shifted1 = _mm_srai_epi16(tmp1, 3);
|
||||
const __m128i shifted2 = _mm_srai_epi16(tmp2, 3);
|
||||
const __m128i shifted3 = _mm_srai_epi16(tmp3, 3);
|
||||
|
||||
// Transpose the two 4x4.
|
||||
// a00 a01 a02 a03 b00 b01 b02 b03
|
||||
// a10 a11 a12 a13 b10 b11 b12 b13
|
||||
// a20 a21 a22 a23 b20 b21 b22 b23
|
||||
// a30 a31 a32 a33 b30 b31 b32 b33
|
||||
const __m128i transpose0_0 = _mm_unpacklo_epi16(shifted0, shifted1);
|
||||
const __m128i transpose0_1 = _mm_unpacklo_epi16(shifted2, shifted3);
|
||||
const __m128i transpose0_2 = _mm_unpackhi_epi16(shifted0, shifted1);
|
||||
const __m128i transpose0_3 = _mm_unpackhi_epi16(shifted2, shifted3);
|
||||
// a00 a10 a01 a11 a02 a12 a03 a13
|
||||
// a20 a30 a21 a31 a22 a32 a23 a33
|
||||
// b00 b10 b01 b11 b02 b12 b03 b13
|
||||
// b20 b30 b21 b31 b22 b32 b23 b33
|
||||
const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
|
||||
const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
|
||||
// a00 a10 a20 a30 a01 a11 a21 a31
|
||||
// b00 b10 b20 b30 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 a03 a13 a23 a33
|
||||
// b02 b12 a22 b32 b03 b13 b23 b33
|
||||
T0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
|
||||
T1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
|
||||
T2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
|
||||
T3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
|
||||
// a00 a10 a20 a30 b00 b10 b20 b30
|
||||
// a01 a11 a21 a31 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 b02 b12 b22 b32
|
||||
// a03 a13 a23 a33 b03 b13 b23 b33
|
||||
}
|
||||
|
||||
// Add inverse transform to 'ref' and store.
|
||||
{
|
||||
const __m128i zero = _mm_set1_epi16(0);
|
||||
// Load the reference(s).
|
||||
__m128i ref0, ref1, ref2, ref3;
|
||||
if (do_two) {
|
||||
// Load eight bytes/pixels per line.
|
||||
ref0 = _mm_loadl_epi64((__m128i*)&ref[0 * BPS]);
|
||||
ref1 = _mm_loadl_epi64((__m128i*)&ref[1 * BPS]);
|
||||
ref2 = _mm_loadl_epi64((__m128i*)&ref[2 * BPS]);
|
||||
ref3 = _mm_loadl_epi64((__m128i*)&ref[3 * BPS]);
|
||||
} else {
|
||||
// Load four bytes/pixels per line.
|
||||
ref0 = _mm_cvtsi32_si128(*(int*)&ref[0 * BPS]);
|
||||
ref1 = _mm_cvtsi32_si128(*(int*)&ref[1 * BPS]);
|
||||
ref2 = _mm_cvtsi32_si128(*(int*)&ref[2 * BPS]);
|
||||
ref3 = _mm_cvtsi32_si128(*(int*)&ref[3 * BPS]);
|
||||
}
|
||||
// Convert to 16b.
|
||||
ref0 = _mm_unpacklo_epi8(ref0, zero);
|
||||
ref1 = _mm_unpacklo_epi8(ref1, zero);
|
||||
ref2 = _mm_unpacklo_epi8(ref2, zero);
|
||||
ref3 = _mm_unpacklo_epi8(ref3, zero);
|
||||
// Add the inverse transform(s).
|
||||
ref0 = _mm_add_epi16(ref0, T0);
|
||||
ref1 = _mm_add_epi16(ref1, T1);
|
||||
ref2 = _mm_add_epi16(ref2, T2);
|
||||
ref3 = _mm_add_epi16(ref3, T3);
|
||||
// Unsigned saturate to 8b.
|
||||
ref0 = _mm_packus_epi16(ref0, ref0);
|
||||
ref1 = _mm_packus_epi16(ref1, ref1);
|
||||
ref2 = _mm_packus_epi16(ref2, ref2);
|
||||
ref3 = _mm_packus_epi16(ref3, ref3);
|
||||
// Store the results.
|
||||
if (do_two) {
|
||||
// Store eight bytes/pixels per line.
|
||||
_mm_storel_epi64((__m128i*)&dst[0 * BPS], ref0);
|
||||
_mm_storel_epi64((__m128i*)&dst[1 * BPS], ref1);
|
||||
_mm_storel_epi64((__m128i*)&dst[2 * BPS], ref2);
|
||||
_mm_storel_epi64((__m128i*)&dst[3 * BPS], ref3);
|
||||
} else {
|
||||
// Store four bytes/pixels per line.
|
||||
*((int32_t *)&dst[0 * BPS]) = _mm_cvtsi128_si32(ref0);
|
||||
*((int32_t *)&dst[1 * BPS]) = _mm_cvtsi128_si32(ref1);
|
||||
*((int32_t *)&dst[2 * BPS]) = _mm_cvtsi128_si32(ref2);
|
||||
*((int32_t *)&dst[3 * BPS]) = _mm_cvtsi128_si32(ref3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void FTransformSSE2(const uint8_t* src, const uint8_t* ref,
|
||||
int16_t* out) {
|
||||
const __m128i zero = _mm_setzero_si128();
|
||||
const __m128i seven = _mm_set1_epi16(7);
|
||||
const __m128i k7500 = _mm_set1_epi32(7500);
|
||||
const __m128i k14500 = _mm_set1_epi32(14500);
|
||||
const __m128i k51000 = _mm_set1_epi32(51000);
|
||||
const __m128i k12000_plus_one = _mm_set1_epi32(12000 + (1 << 16));
|
||||
const __m128i k5352_2217 = _mm_set_epi16(5352, 2217, 5352, 2217,
|
||||
5352, 2217, 5352, 2217);
|
||||
const __m128i k2217_5352 = _mm_set_epi16(2217, -5352, 2217, -5352,
|
||||
2217, -5352, 2217, -5352);
|
||||
|
||||
__m128i v01, v32;
|
||||
|
||||
// Difference between src and ref and initial transpose.
|
||||
{
|
||||
// Load src and convert to 16b.
|
||||
const __m128i src0 = _mm_loadl_epi64((__m128i*)&src[0 * BPS]);
|
||||
const __m128i src1 = _mm_loadl_epi64((__m128i*)&src[1 * BPS]);
|
||||
const __m128i src2 = _mm_loadl_epi64((__m128i*)&src[2 * BPS]);
|
||||
const __m128i src3 = _mm_loadl_epi64((__m128i*)&src[3 * BPS]);
|
||||
const __m128i src_0 = _mm_unpacklo_epi8(src0, zero);
|
||||
const __m128i src_1 = _mm_unpacklo_epi8(src1, zero);
|
||||
const __m128i src_2 = _mm_unpacklo_epi8(src2, zero);
|
||||
const __m128i src_3 = _mm_unpacklo_epi8(src3, zero);
|
||||
// Load ref and convert to 16b.
|
||||
const __m128i ref0 = _mm_loadl_epi64((__m128i*)&ref[0 * BPS]);
|
||||
const __m128i ref1 = _mm_loadl_epi64((__m128i*)&ref[1 * BPS]);
|
||||
const __m128i ref2 = _mm_loadl_epi64((__m128i*)&ref[2 * BPS]);
|
||||
const __m128i ref3 = _mm_loadl_epi64((__m128i*)&ref[3 * BPS]);
|
||||
const __m128i ref_0 = _mm_unpacklo_epi8(ref0, zero);
|
||||
const __m128i ref_1 = _mm_unpacklo_epi8(ref1, zero);
|
||||
const __m128i ref_2 = _mm_unpacklo_epi8(ref2, zero);
|
||||
const __m128i ref_3 = _mm_unpacklo_epi8(ref3, zero);
|
||||
// Compute difference.
|
||||
const __m128i diff0 = _mm_sub_epi16(src_0, ref_0);
|
||||
const __m128i diff1 = _mm_sub_epi16(src_1, ref_1);
|
||||
const __m128i diff2 = _mm_sub_epi16(src_2, ref_2);
|
||||
const __m128i diff3 = _mm_sub_epi16(src_3, ref_3);
|
||||
|
||||
// Transpose.
|
||||
// 00 01 02 03 0 0 0 0
|
||||
// 10 11 12 13 0 0 0 0
|
||||
// 20 21 22 23 0 0 0 0
|
||||
// 30 31 32 33 0 0 0 0
|
||||
const __m128i transpose0_0 = _mm_unpacklo_epi16(diff0, diff1);
|
||||
const __m128i transpose0_1 = _mm_unpacklo_epi16(diff2, diff3);
|
||||
// 00 10 01 11 02 12 03 13
|
||||
// 20 30 21 31 22 32 23 33
|
||||
const __m128i v23 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
|
||||
v01 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
|
||||
v32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2));
|
||||
// a02 a12 a22 a32 a03 a13 a23 a33
|
||||
// a00 a10 a20 a30 a01 a11 a21 a31
|
||||
// a03 a13 a23 a33 a02 a12 a22 a32
|
||||
}
|
||||
|
||||
// First pass and subsequent transpose.
|
||||
{
|
||||
// Same operations are done on the (0,3) and (1,2) pairs.
|
||||
// b0 = (a0 + a3) << 3
|
||||
// b1 = (a1 + a2) << 3
|
||||
// b3 = (a0 - a3) << 3
|
||||
// b2 = (a1 - a2) << 3
|
||||
const __m128i a01 = _mm_add_epi16(v01, v32);
|
||||
const __m128i a32 = _mm_sub_epi16(v01, v32);
|
||||
const __m128i b01 = _mm_slli_epi16(a01, 3);
|
||||
const __m128i b32 = _mm_slli_epi16(a32, 3);
|
||||
const __m128i b11 = _mm_unpackhi_epi64(b01, b01);
|
||||
const __m128i b22 = _mm_unpackhi_epi64(b32, b32);
|
||||
|
||||
// e0 = b0 + b1
|
||||
// e2 = b0 - b1
|
||||
const __m128i e0 = _mm_add_epi16(b01, b11);
|
||||
const __m128i e2 = _mm_sub_epi16(b01, b11);
|
||||
const __m128i e02 = _mm_unpacklo_epi64(e0, e2);
|
||||
|
||||
// e1 = (b3 * 5352 + b2 * 2217 + 14500) >> 12
|
||||
// e3 = (b3 * 2217 - b2 * 5352 + 7500) >> 12
|
||||
const __m128i b23 = _mm_unpacklo_epi16(b22, b32);
|
||||
const __m128i c1 = _mm_madd_epi16(b23, k5352_2217);
|
||||
const __m128i c3 = _mm_madd_epi16(b23, k2217_5352);
|
||||
const __m128i d1 = _mm_add_epi32(c1, k14500);
|
||||
const __m128i d3 = _mm_add_epi32(c3, k7500);
|
||||
const __m128i e1 = _mm_srai_epi32(d1, 12);
|
||||
const __m128i e3 = _mm_srai_epi32(d3, 12);
|
||||
const __m128i e13 = _mm_packs_epi32(e1, e3);
|
||||
|
||||
// Transpose.
|
||||
// 00 01 02 03 20 21 22 23
|
||||
// 10 11 12 13 30 31 32 33
|
||||
const __m128i transpose0_0 = _mm_unpacklo_epi16(e02, e13);
|
||||
const __m128i transpose0_1 = _mm_unpackhi_epi16(e02, e13);
|
||||
// 00 10 01 11 02 12 03 13
|
||||
// 20 30 21 31 22 32 23 33
|
||||
const __m128i v23 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
|
||||
v01 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
|
||||
v32 = _mm_shuffle_epi32(v23, _MM_SHUFFLE(1, 0, 3, 2));
|
||||
// 02 12 22 32 03 13 23 33
|
||||
// 00 10 20 30 01 11 21 31
|
||||
// 03 13 23 33 02 12 22 32
|
||||
}
|
||||
|
||||
// Second pass
|
||||
{
|
||||
// Same operations are done on the (0,3) and (1,2) pairs.
|
||||
// a0 = v0 + v3
|
||||
// a1 = v1 + v2
|
||||
// a3 = v0 - v3
|
||||
// a2 = v1 - v2
|
||||
const __m128i a01 = _mm_add_epi16(v01, v32);
|
||||
const __m128i a32 = _mm_sub_epi16(v01, v32);
|
||||
const __m128i a11 = _mm_unpackhi_epi64(a01, a01);
|
||||
const __m128i a22 = _mm_unpackhi_epi64(a32, a32);
|
||||
|
||||
// d0 = (a0 + a1 + 7) >> 4;
|
||||
// d2 = (a0 - a1 + 7) >> 4;
|
||||
const __m128i b0 = _mm_add_epi16(a01, a11);
|
||||
const __m128i b2 = _mm_sub_epi16(a01, a11);
|
||||
const __m128i c0 = _mm_add_epi16(b0, seven);
|
||||
const __m128i c2 = _mm_add_epi16(b2, seven);
|
||||
const __m128i d0 = _mm_srai_epi16(c0, 4);
|
||||
const __m128i d2 = _mm_srai_epi16(c2, 4);
|
||||
|
||||
// f1 = ((b3 * 5352 + b2 * 2217 + 12000) >> 16)
|
||||
// f3 = ((b3 * 2217 - b2 * 5352 + 51000) >> 16)
|
||||
const __m128i b23 = _mm_unpacklo_epi16(a22, a32);
|
||||
const __m128i c1 = _mm_madd_epi16(b23, k5352_2217);
|
||||
const __m128i c3 = _mm_madd_epi16(b23, k2217_5352);
|
||||
const __m128i d1 = _mm_add_epi32(c1, k12000_plus_one);
|
||||
const __m128i d3 = _mm_add_epi32(c3, k51000);
|
||||
const __m128i e1 = _mm_srai_epi32(d1, 16);
|
||||
const __m128i e3 = _mm_srai_epi32(d3, 16);
|
||||
const __m128i f1 = _mm_packs_epi32(e1, e1);
|
||||
const __m128i f3 = _mm_packs_epi32(e3, e3);
|
||||
// f1 = f1 + (a3 != 0);
|
||||
// The compare will return (0xffff, 0) for (==0, !=0). To turn that into the
|
||||
// desired (0, 1), we add one earlier through k12000_plus_one.
|
||||
const __m128i g1 = _mm_add_epi16(f1, _mm_cmpeq_epi16(a32, zero));
|
||||
|
||||
_mm_storel_epi64((__m128i*)&out[ 0], d0);
|
||||
_mm_storel_epi64((__m128i*)&out[ 4], g1);
|
||||
_mm_storel_epi64((__m128i*)&out[ 8], d2);
|
||||
_mm_storel_epi64((__m128i*)&out[12], f3);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Metric
|
||||
|
||||
static int SSE4x4SSE2(const uint8_t* a, const uint8_t* b) {
|
||||
const __m128i zero = _mm_set1_epi16(0);
|
||||
|
||||
// Load values.
|
||||
const __m128i a0 = _mm_loadl_epi64((__m128i*)&a[BPS * 0]);
|
||||
const __m128i a1 = _mm_loadl_epi64((__m128i*)&a[BPS * 1]);
|
||||
const __m128i a2 = _mm_loadl_epi64((__m128i*)&a[BPS * 2]);
|
||||
const __m128i a3 = _mm_loadl_epi64((__m128i*)&a[BPS * 3]);
|
||||
const __m128i b0 = _mm_loadl_epi64((__m128i*)&b[BPS * 0]);
|
||||
const __m128i b1 = _mm_loadl_epi64((__m128i*)&b[BPS * 1]);
|
||||
const __m128i b2 = _mm_loadl_epi64((__m128i*)&b[BPS * 2]);
|
||||
const __m128i b3 = _mm_loadl_epi64((__m128i*)&b[BPS * 3]);
|
||||
|
||||
// Combine pair of lines and convert to 16b.
|
||||
const __m128i a01 = _mm_unpacklo_epi32(a0, a1);
|
||||
const __m128i a23 = _mm_unpacklo_epi32(a2, a3);
|
||||
const __m128i b01 = _mm_unpacklo_epi32(b0, b1);
|
||||
const __m128i b23 = _mm_unpacklo_epi32(b2, b3);
|
||||
const __m128i a01s = _mm_unpacklo_epi8(a01, zero);
|
||||
const __m128i a23s = _mm_unpacklo_epi8(a23, zero);
|
||||
const __m128i b01s = _mm_unpacklo_epi8(b01, zero);
|
||||
const __m128i b23s = _mm_unpacklo_epi8(b23, zero);
|
||||
|
||||
// Compute differences; (a-b)^2 = (abs(a-b))^2 = (sat8(a-b) + sat8(b-a))^2
|
||||
// TODO(cduvivier): Dissassemble and figure out why this is fastest. We don't
|
||||
// need absolute values, there is no need to do calculation
|
||||
// in 8bit as we are already in 16bit, ... Yet this is what
|
||||
// benchmarks the fastest!
|
||||
const __m128i d0 = _mm_subs_epu8(a01s, b01s);
|
||||
const __m128i d1 = _mm_subs_epu8(b01s, a01s);
|
||||
const __m128i d2 = _mm_subs_epu8(a23s, b23s);
|
||||
const __m128i d3 = _mm_subs_epu8(b23s, a23s);
|
||||
|
||||
// Square and add them all together.
|
||||
const __m128i madd0 = _mm_madd_epi16(d0, d0);
|
||||
const __m128i madd1 = _mm_madd_epi16(d1, d1);
|
||||
const __m128i madd2 = _mm_madd_epi16(d2, d2);
|
||||
const __m128i madd3 = _mm_madd_epi16(d3, d3);
|
||||
const __m128i sum0 = _mm_add_epi32(madd0, madd1);
|
||||
const __m128i sum1 = _mm_add_epi32(madd2, madd3);
|
||||
const __m128i sum2 = _mm_add_epi32(sum0, sum1);
|
||||
int32_t tmp[4];
|
||||
_mm_storeu_si128((__m128i*)tmp, sum2);
|
||||
return (tmp[3] + tmp[2] + tmp[1] + tmp[0]);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Texture distortion
|
||||
//
|
||||
// We try to match the spectral content (weighted) between source and
|
||||
// reconstructed samples.
|
||||
|
||||
// Hadamard transform
|
||||
// Returns the difference between the weighted sum of the absolute value of
|
||||
// transformed coefficients.
|
||||
static int TTransformSSE2(const uint8_t* inA, const uint8_t* inB,
|
||||
const uint16_t* const w) {
|
||||
int32_t sum[4];
|
||||
__m128i tmp_0, tmp_1, tmp_2, tmp_3;
|
||||
const __m128i zero = _mm_setzero_si128();
|
||||
const __m128i one = _mm_set1_epi16(1);
|
||||
const __m128i three = _mm_set1_epi16(3);
|
||||
|
||||
// Load, combine and tranpose inputs.
|
||||
{
|
||||
const __m128i inA_0 = _mm_loadl_epi64((__m128i*)&inA[BPS * 0]);
|
||||
const __m128i inA_1 = _mm_loadl_epi64((__m128i*)&inA[BPS * 1]);
|
||||
const __m128i inA_2 = _mm_loadl_epi64((__m128i*)&inA[BPS * 2]);
|
||||
const __m128i inA_3 = _mm_loadl_epi64((__m128i*)&inA[BPS * 3]);
|
||||
const __m128i inB_0 = _mm_loadl_epi64((__m128i*)&inB[BPS * 0]);
|
||||
const __m128i inB_1 = _mm_loadl_epi64((__m128i*)&inB[BPS * 1]);
|
||||
const __m128i inB_2 = _mm_loadl_epi64((__m128i*)&inB[BPS * 2]);
|
||||
const __m128i inB_3 = _mm_loadl_epi64((__m128i*)&inB[BPS * 3]);
|
||||
|
||||
// Combine inA and inB (we'll do two transforms in parallel).
|
||||
const __m128i inAB_0 = _mm_unpacklo_epi8(inA_0, inB_0);
|
||||
const __m128i inAB_1 = _mm_unpacklo_epi8(inA_1, inB_1);
|
||||
const __m128i inAB_2 = _mm_unpacklo_epi8(inA_2, inB_2);
|
||||
const __m128i inAB_3 = _mm_unpacklo_epi8(inA_3, inB_3);
|
||||
// a00 b00 a01 b01 a02 b03 a03 b03 0 0 0 0 0 0 0 0
|
||||
// a10 b10 a11 b11 a12 b12 a13 b13 0 0 0 0 0 0 0 0
|
||||
// a20 b20 a21 b21 a22 b22 a23 b23 0 0 0 0 0 0 0 0
|
||||
// a30 b30 a31 b31 a32 b32 a33 b33 0 0 0 0 0 0 0 0
|
||||
|
||||
// Transpose the two 4x4, discarding the filling zeroes.
|
||||
const __m128i transpose0_0 = _mm_unpacklo_epi8(inAB_0, inAB_2);
|
||||
const __m128i transpose0_1 = _mm_unpacklo_epi8(inAB_1, inAB_3);
|
||||
// a00 a20 b00 b20 a01 a21 b01 b21 a02 a22 b02 b22 a03 a23 b03 b23
|
||||
// a10 a30 b10 b30 a11 a31 b11 b31 a12 a32 b12 b32 a13 a33 b13 b33
|
||||
const __m128i transpose1_0 = _mm_unpacklo_epi8(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_1 = _mm_unpackhi_epi8(transpose0_0, transpose0_1);
|
||||
// a00 a10 a20 a30 b00 b10 b20 b30 a01 a11 a21 a31 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 b02 b12 b22 b32 a03 a13 a23 a33 b03 b13 b23 b33
|
||||
|
||||
// Convert to 16b.
|
||||
tmp_0 = _mm_unpacklo_epi8(transpose1_0, zero);
|
||||
tmp_1 = _mm_unpackhi_epi8(transpose1_0, zero);
|
||||
tmp_2 = _mm_unpacklo_epi8(transpose1_1, zero);
|
||||
tmp_3 = _mm_unpackhi_epi8(transpose1_1, zero);
|
||||
// a00 a10 a20 a30 b00 b10 b20 b30
|
||||
// a01 a11 a21 a31 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 b02 b12 b22 b32
|
||||
// a03 a13 a23 a33 b03 b13 b23 b33
|
||||
}
|
||||
|
||||
// Horizontal pass and subsequent transpose.
|
||||
{
|
||||
// Calculate a and b (two 4x4 at once).
|
||||
const __m128i a0 = _mm_slli_epi16(_mm_add_epi16(tmp_0, tmp_2), 2);
|
||||
const __m128i a1 = _mm_slli_epi16(_mm_add_epi16(tmp_1, tmp_3), 2);
|
||||
const __m128i a2 = _mm_slli_epi16(_mm_sub_epi16(tmp_1, tmp_3), 2);
|
||||
const __m128i a3 = _mm_slli_epi16(_mm_sub_epi16(tmp_0, tmp_2), 2);
|
||||
// b0_extra = (a0 != 0);
|
||||
const __m128i b0_extra = _mm_andnot_si128(_mm_cmpeq_epi16 (a0, zero), one);
|
||||
const __m128i b0_base = _mm_add_epi16(a0, a1);
|
||||
const __m128i b1 = _mm_add_epi16(a3, a2);
|
||||
const __m128i b2 = _mm_sub_epi16(a3, a2);
|
||||
const __m128i b3 = _mm_sub_epi16(a0, a1);
|
||||
const __m128i b0 = _mm_add_epi16(b0_base, b0_extra);
|
||||
// a00 a01 a02 a03 b00 b01 b02 b03
|
||||
// a10 a11 a12 a13 b10 b11 b12 b13
|
||||
// a20 a21 a22 a23 b20 b21 b22 b23
|
||||
// a30 a31 a32 a33 b30 b31 b32 b33
|
||||
|
||||
// Transpose the two 4x4.
|
||||
const __m128i transpose0_0 = _mm_unpacklo_epi16(b0, b1);
|
||||
const __m128i transpose0_1 = _mm_unpacklo_epi16(b2, b3);
|
||||
const __m128i transpose0_2 = _mm_unpackhi_epi16(b0, b1);
|
||||
const __m128i transpose0_3 = _mm_unpackhi_epi16(b2, b3);
|
||||
// a00 a10 a01 a11 a02 a12 a03 a13
|
||||
// a20 a30 a21 a31 a22 a32 a23 a33
|
||||
// b00 b10 b01 b11 b02 b12 b03 b13
|
||||
// b20 b30 b21 b31 b22 b32 b23 b33
|
||||
const __m128i transpose1_0 = _mm_unpacklo_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_1 = _mm_unpacklo_epi32(transpose0_2, transpose0_3);
|
||||
const __m128i transpose1_2 = _mm_unpackhi_epi32(transpose0_0, transpose0_1);
|
||||
const __m128i transpose1_3 = _mm_unpackhi_epi32(transpose0_2, transpose0_3);
|
||||
// a00 a10 a20 a30 a01 a11 a21 a31
|
||||
// b00 b10 b20 b30 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 a03 a13 a23 a33
|
||||
// b02 b12 a22 b32 b03 b13 b23 b33
|
||||
tmp_0 = _mm_unpacklo_epi64(transpose1_0, transpose1_1);
|
||||
tmp_1 = _mm_unpackhi_epi64(transpose1_0, transpose1_1);
|
||||
tmp_2 = _mm_unpacklo_epi64(transpose1_2, transpose1_3);
|
||||
tmp_3 = _mm_unpackhi_epi64(transpose1_2, transpose1_3);
|
||||
// a00 a10 a20 a30 b00 b10 b20 b30
|
||||
// a01 a11 a21 a31 b01 b11 b21 b31
|
||||
// a02 a12 a22 a32 b02 b12 b22 b32
|
||||
// a03 a13 a23 a33 b03 b13 b23 b33
|
||||
}
|
||||
|
||||
// Vertical pass and difference of weighted sums.
|
||||
{
|
||||
// Load all inputs.
|
||||
// TODO(cduvivier): Make variable declarations and allocations aligned so
|
||||
// we can use _mm_load_si128 instead of _mm_loadu_si128.
|
||||
const __m128i w_0 = _mm_loadu_si128((__m128i*)&w[0]);
|
||||
const __m128i w_8 = _mm_loadu_si128((__m128i*)&w[8]);
|
||||
|
||||
// Calculate a and b (two 4x4 at once).
|
||||
const __m128i a0 = _mm_add_epi16(tmp_0, tmp_2);
|
||||
const __m128i a1 = _mm_add_epi16(tmp_1, tmp_3);
|
||||
const __m128i a2 = _mm_sub_epi16(tmp_1, tmp_3);
|
||||
const __m128i a3 = _mm_sub_epi16(tmp_0, tmp_2);
|
||||
const __m128i b0 = _mm_add_epi16(a0, a1);
|
||||
const __m128i b1 = _mm_add_epi16(a3, a2);
|
||||
const __m128i b2 = _mm_sub_epi16(a3, a2);
|
||||
const __m128i b3 = _mm_sub_epi16(a0, a1);
|
||||
|
||||
// Separate the transforms of inA and inB.
|
||||
__m128i A_b0 = _mm_unpacklo_epi64(b0, b1);
|
||||
__m128i A_b2 = _mm_unpacklo_epi64(b2, b3);
|
||||
__m128i B_b0 = _mm_unpackhi_epi64(b0, b1);
|
||||
__m128i B_b2 = _mm_unpackhi_epi64(b2, b3);
|
||||
|
||||
{
|
||||
// sign(b) = b >> 15 (0x0000 if positive, 0xffff if negative)
|
||||
const __m128i sign_A_b0 = _mm_srai_epi16(A_b0, 15);
|
||||
const __m128i sign_A_b2 = _mm_srai_epi16(A_b2, 15);
|
||||
const __m128i sign_B_b0 = _mm_srai_epi16(B_b0, 15);
|
||||
const __m128i sign_B_b2 = _mm_srai_epi16(B_b2, 15);
|
||||
|
||||
// b = abs(b) = (b ^ sign) - sign
|
||||
A_b0 = _mm_xor_si128(A_b0, sign_A_b0);
|
||||
A_b2 = _mm_xor_si128(A_b2, sign_A_b2);
|
||||
B_b0 = _mm_xor_si128(B_b0, sign_B_b0);
|
||||
B_b2 = _mm_xor_si128(B_b2, sign_B_b2);
|
||||
A_b0 = _mm_sub_epi16(A_b0, sign_A_b0);
|
||||
A_b2 = _mm_sub_epi16(A_b2, sign_A_b2);
|
||||
B_b0 = _mm_sub_epi16(B_b0, sign_B_b0);
|
||||
B_b2 = _mm_sub_epi16(B_b2, sign_B_b2);
|
||||
}
|
||||
|
||||
// b = abs(b) + 3
|
||||
A_b0 = _mm_add_epi16(A_b0, three);
|
||||
A_b2 = _mm_add_epi16(A_b2, three);
|
||||
B_b0 = _mm_add_epi16(B_b0, three);
|
||||
B_b2 = _mm_add_epi16(B_b2, three);
|
||||
|
||||
// abs((b + (b<0) + 3) >> 3) = (abs(b) + 3) >> 3
|
||||
// b = (abs(b) + 3) >> 3
|
||||
A_b0 = _mm_srai_epi16(A_b0, 3);
|
||||
A_b2 = _mm_srai_epi16(A_b2, 3);
|
||||
B_b0 = _mm_srai_epi16(B_b0, 3);
|
||||
B_b2 = _mm_srai_epi16(B_b2, 3);
|
||||
|
||||
// weighted sums
|
||||
A_b0 = _mm_madd_epi16(A_b0, w_0);
|
||||
A_b2 = _mm_madd_epi16(A_b2, w_8);
|
||||
B_b0 = _mm_madd_epi16(B_b0, w_0);
|
||||
B_b2 = _mm_madd_epi16(B_b2, w_8);
|
||||
A_b0 = _mm_add_epi32(A_b0, A_b2);
|
||||
B_b0 = _mm_add_epi32(B_b0, B_b2);
|
||||
|
||||
// difference of weighted sums
|
||||
A_b0 = _mm_sub_epi32(A_b0, B_b0);
|
||||
_mm_storeu_si128((__m128i*)&sum[0], A_b0);
|
||||
}
|
||||
return sum[0] + sum[1] + sum[2] + sum[3];
|
||||
}
|
||||
|
||||
static int Disto4x4SSE2(const uint8_t* const a, const uint8_t* const b,
|
||||
const uint16_t* const w) {
|
||||
const int diff_sum = TTransformSSE2(a, b, w);
|
||||
return (abs(diff_sum) + 8) >> 4;
|
||||
}
|
||||
|
||||
static int Disto16x16SSE2(const uint8_t* const a, const uint8_t* const b,
|
||||
const uint16_t* const w) {
|
||||
int D = 0;
|
||||
int x, y;
|
||||
for (y = 0; y < 16 * BPS; y += 4 * BPS) {
|
||||
for (x = 0; x < 16; x += 4) {
|
||||
D += Disto4x4SSE2(a + x + y, b + x + y, w);
|
||||
}
|
||||
}
|
||||
return D;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Quantization
|
||||
//
|
||||
|
||||
// Simple quantization
|
||||
static int QuantizeBlockSSE2(int16_t in[16], int16_t out[16],
|
||||
int n, const VP8Matrix* const mtx) {
|
||||
const __m128i max_coeff_2047 = _mm_set1_epi16(2047);
|
||||
const __m128i zero = _mm_set1_epi16(0);
|
||||
__m128i sign0, sign8;
|
||||
__m128i coeff0, coeff8;
|
||||
__m128i out0, out8;
|
||||
__m128i packed_out;
|
||||
|
||||
// Load all inputs.
|
||||
// TODO(cduvivier): Make variable declarations and allocations aligned so that
|
||||
// we can use _mm_load_si128 instead of _mm_loadu_si128.
|
||||
__m128i in0 = _mm_loadu_si128((__m128i*)&in[0]);
|
||||
__m128i in8 = _mm_loadu_si128((__m128i*)&in[8]);
|
||||
const __m128i sharpen0 = _mm_loadu_si128((__m128i*)&mtx->sharpen_[0]);
|
||||
const __m128i sharpen8 = _mm_loadu_si128((__m128i*)&mtx->sharpen_[8]);
|
||||
const __m128i iq0 = _mm_loadu_si128((__m128i*)&mtx->iq_[0]);
|
||||
const __m128i iq8 = _mm_loadu_si128((__m128i*)&mtx->iq_[8]);
|
||||
const __m128i bias0 = _mm_loadu_si128((__m128i*)&mtx->bias_[0]);
|
||||
const __m128i bias8 = _mm_loadu_si128((__m128i*)&mtx->bias_[8]);
|
||||
const __m128i q0 = _mm_loadu_si128((__m128i*)&mtx->q_[0]);
|
||||
const __m128i q8 = _mm_loadu_si128((__m128i*)&mtx->q_[8]);
|
||||
const __m128i zthresh0 = _mm_loadu_si128((__m128i*)&mtx->zthresh_[0]);
|
||||
const __m128i zthresh8 = _mm_loadu_si128((__m128i*)&mtx->zthresh_[8]);
|
||||
|
||||
// sign(in) = in >> 15 (0x0000 if positive, 0xffff if negative)
|
||||
sign0 = _mm_srai_epi16(in0, 15);
|
||||
sign8 = _mm_srai_epi16(in8, 15);
|
||||
|
||||
// coeff = abs(in) = (in ^ sign) - sign
|
||||
coeff0 = _mm_xor_si128(in0, sign0);
|
||||
coeff8 = _mm_xor_si128(in8, sign8);
|
||||
coeff0 = _mm_sub_epi16(coeff0, sign0);
|
||||
coeff8 = _mm_sub_epi16(coeff8, sign8);
|
||||
|
||||
// coeff = abs(in) + sharpen
|
||||
coeff0 = _mm_add_epi16(coeff0, sharpen0);
|
||||
coeff8 = _mm_add_epi16(coeff8, sharpen8);
|
||||
|
||||
// if (coeff > 2047) coeff = 2047
|
||||
coeff0 = _mm_min_epi16(coeff0, max_coeff_2047);
|
||||
coeff8 = _mm_min_epi16(coeff8, max_coeff_2047);
|
||||
|
||||
// out = (coeff * iQ + B) >> QFIX;
|
||||
{
|
||||
// doing calculations with 32b precision (QFIX=17)
|
||||
// out = (coeff * iQ)
|
||||
__m128i coeff_iQ0H = _mm_mulhi_epu16(coeff0, iq0);
|
||||
__m128i coeff_iQ0L = _mm_mullo_epi16(coeff0, iq0);
|
||||
__m128i coeff_iQ8H = _mm_mulhi_epu16(coeff8, iq8);
|
||||
__m128i coeff_iQ8L = _mm_mullo_epi16(coeff8, iq8);
|
||||
__m128i out_00 = _mm_unpacklo_epi16(coeff_iQ0L, coeff_iQ0H);
|
||||
__m128i out_04 = _mm_unpackhi_epi16(coeff_iQ0L, coeff_iQ0H);
|
||||
__m128i out_08 = _mm_unpacklo_epi16(coeff_iQ8L, coeff_iQ8H);
|
||||
__m128i out_12 = _mm_unpackhi_epi16(coeff_iQ8L, coeff_iQ8H);
|
||||
// expand bias from 16b to 32b
|
||||
__m128i bias_00 = _mm_unpacklo_epi16(bias0, zero);
|
||||
__m128i bias_04 = _mm_unpackhi_epi16(bias0, zero);
|
||||
__m128i bias_08 = _mm_unpacklo_epi16(bias8, zero);
|
||||
__m128i bias_12 = _mm_unpackhi_epi16(bias8, zero);
|
||||
// out = (coeff * iQ + B)
|
||||
out_00 = _mm_add_epi32(out_00, bias_00);
|
||||
out_04 = _mm_add_epi32(out_04, bias_04);
|
||||
out_08 = _mm_add_epi32(out_08, bias_08);
|
||||
out_12 = _mm_add_epi32(out_12, bias_12);
|
||||
// out = (coeff * iQ + B) >> QFIX;
|
||||
out_00 = _mm_srai_epi32(out_00, QFIX);
|
||||
out_04 = _mm_srai_epi32(out_04, QFIX);
|
||||
out_08 = _mm_srai_epi32(out_08, QFIX);
|
||||
out_12 = _mm_srai_epi32(out_12, QFIX);
|
||||
// pack result as 16b
|
||||
out0 = _mm_packs_epi32(out_00, out_04);
|
||||
out8 = _mm_packs_epi32(out_08, out_12);
|
||||
}
|
||||
|
||||
// get sign back (if (sign[j]) out_n = -out_n)
|
||||
out0 = _mm_xor_si128(out0, sign0);
|
||||
out8 = _mm_xor_si128(out8, sign8);
|
||||
out0 = _mm_sub_epi16(out0, sign0);
|
||||
out8 = _mm_sub_epi16(out8, sign8);
|
||||
|
||||
// in = out * Q
|
||||
in0 = _mm_mullo_epi16(out0, q0);
|
||||
in8 = _mm_mullo_epi16(out8, q8);
|
||||
|
||||
// if (coeff <= mtx->zthresh_) {in=0; out=0;}
|
||||
{
|
||||
__m128i cmp0 = _mm_cmpgt_epi16(coeff0, zthresh0);
|
||||
__m128i cmp8 = _mm_cmpgt_epi16(coeff8, zthresh8);
|
||||
in0 = _mm_and_si128(in0, cmp0);
|
||||
in8 = _mm_and_si128(in8, cmp8);
|
||||
_mm_storeu_si128((__m128i*)&in[0], in0);
|
||||
_mm_storeu_si128((__m128i*)&in[8], in8);
|
||||
out0 = _mm_and_si128(out0, cmp0);
|
||||
out8 = _mm_and_si128(out8, cmp8);
|
||||
}
|
||||
|
||||
// zigzag the output before storing it.
|
||||
//
|
||||
// The zigzag pattern can almost be reproduced with a small sequence of
|
||||
// shuffles. After it, we only need to swap the 7th (ending up in third
|
||||
// position instead of twelfth) and 8th values.
|
||||
{
|
||||
__m128i outZ0, outZ8;
|
||||
outZ0 = _mm_shufflehi_epi16(out0, _MM_SHUFFLE(2, 1, 3, 0));
|
||||
outZ0 = _mm_shuffle_epi32 (outZ0, _MM_SHUFFLE(3, 1, 2, 0));
|
||||
outZ0 = _mm_shufflehi_epi16(outZ0, _MM_SHUFFLE(3, 1, 0, 2));
|
||||
outZ8 = _mm_shufflelo_epi16(out8, _MM_SHUFFLE(3, 0, 2, 1));
|
||||
outZ8 = _mm_shuffle_epi32 (outZ8, _MM_SHUFFLE(3, 1, 2, 0));
|
||||
outZ8 = _mm_shufflelo_epi16(outZ8, _MM_SHUFFLE(1, 3, 2, 0));
|
||||
_mm_storeu_si128((__m128i*)&out[0], outZ0);
|
||||
_mm_storeu_si128((__m128i*)&out[8], outZ8);
|
||||
packed_out = _mm_packs_epi16(outZ0, outZ8);
|
||||
}
|
||||
{
|
||||
const int16_t outZ_12 = out[12];
|
||||
const int16_t outZ_3 = out[3];
|
||||
out[3] = outZ_12;
|
||||
out[12] = outZ_3;
|
||||
}
|
||||
|
||||
// detect if all 'out' values are zeroes or not
|
||||
{
|
||||
int32_t tmp[4];
|
||||
_mm_storeu_si128((__m128i*)tmp, packed_out);
|
||||
if (n) {
|
||||
tmp[0] &= ~0xff;
|
||||
}
|
||||
return (tmp[3] || tmp[2] || tmp[1] || tmp[0]);
|
||||
}
|
||||
}
|
||||
|
||||
extern void VP8EncDspInitSSE2(void);
|
||||
void VP8EncDspInitSSE2(void) {
|
||||
VP8CollectHistogram = CollectHistogramSSE2;
|
||||
VP8EncQuantizeBlock = QuantizeBlockSSE2;
|
||||
VP8ITransform = ITransformSSE2;
|
||||
VP8FTransform = FTransformSSE2;
|
||||
VP8SSE4x4 = SSE4x4SSE2;
|
||||
VP8TDisto4x4 = Disto4x4SSE2;
|
||||
VP8TDisto16x16 = Disto16x16SSE2;
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif //__SSE2__
|
226
src/dsp/upsampling.c
Normal file
226
src/dsp/upsampling.c
Normal file
@ -0,0 +1,226 @@
|
||||
// Copyright 2011 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// YUV to RGB upsampling functions.
|
||||
//
|
||||
// Author: somnath@google.com (Somnath Banerjee)
|
||||
|
||||
#include "./dsp.h"
|
||||
#include "./yuv.h"
|
||||
#include "../dec/webpi.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Fancy upsampler
|
||||
|
||||
#ifdef FANCY_UPSAMPLING
|
||||
|
||||
// Fancy upsampling functions to convert YUV to RGB
|
||||
WebPUpsampleLinePairFunc WebPUpsamplers[MODE_LAST];
|
||||
WebPUpsampleLinePairFunc WebPUpsamplersKeepAlpha[MODE_LAST];
|
||||
|
||||
// Given samples laid out in a square as:
|
||||
// [a b]
|
||||
// [c d]
|
||||
// we interpolate u/v as:
|
||||
// ([9*a + 3*b + 3*c + d 3*a + 9*b + 3*c + d] + [8 8]) / 16
|
||||
// ([3*a + b + 9*c + 3*d a + 3*b + 3*c + 9*d] [8 8]) / 16
|
||||
|
||||
// We process u and v together stashed into 32bit (16bit each).
|
||||
#define LOAD_UV(u,v) ((u) | ((v) << 16))
|
||||
|
||||
#define UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \
|
||||
static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \
|
||||
const uint8_t* top_u, const uint8_t* top_v, \
|
||||
const uint8_t* cur_u, const uint8_t* cur_v, \
|
||||
uint8_t* top_dst, uint8_t* bottom_dst, int len) { \
|
||||
int x; \
|
||||
const int last_pixel_pair = (len - 1) >> 1; \
|
||||
uint32_t tl_uv = LOAD_UV(top_u[0], top_v[0]); /* top-left sample */ \
|
||||
uint32_t l_uv = LOAD_UV(cur_u[0], cur_v[0]); /* left-sample */ \
|
||||
if (top_y) { \
|
||||
const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \
|
||||
FUNC(top_y[0], uv0 & 0xff, (uv0 >> 16), top_dst); \
|
||||
} \
|
||||
if (bottom_y) { \
|
||||
const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \
|
||||
FUNC(bottom_y[0], uv0 & 0xff, (uv0 >> 16), bottom_dst); \
|
||||
} \
|
||||
for (x = 1; x <= last_pixel_pair; ++x) { \
|
||||
const uint32_t t_uv = LOAD_UV(top_u[x], top_v[x]); /* top sample */ \
|
||||
const uint32_t uv = LOAD_UV(cur_u[x], cur_v[x]); /* sample */ \
|
||||
/* precompute invariant values associated with first and second diagonals*/\
|
||||
const uint32_t avg = tl_uv + t_uv + l_uv + uv + 0x00080008u; \
|
||||
const uint32_t diag_12 = (avg + 2 * (t_uv + l_uv)) >> 3; \
|
||||
const uint32_t diag_03 = (avg + 2 * (tl_uv + uv)) >> 3; \
|
||||
if (top_y) { \
|
||||
const uint32_t uv0 = (diag_12 + tl_uv) >> 1; \
|
||||
const uint32_t uv1 = (diag_03 + t_uv) >> 1; \
|
||||
FUNC(top_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \
|
||||
top_dst + (2 * x - 1) * XSTEP); \
|
||||
FUNC(top_y[2 * x - 0], uv1 & 0xff, (uv1 >> 16), \
|
||||
top_dst + (2 * x - 0) * XSTEP); \
|
||||
} \
|
||||
if (bottom_y) { \
|
||||
const uint32_t uv0 = (diag_03 + l_uv) >> 1; \
|
||||
const uint32_t uv1 = (diag_12 + uv) >> 1; \
|
||||
FUNC(bottom_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \
|
||||
bottom_dst + (2 * x - 1) * XSTEP); \
|
||||
FUNC(bottom_y[2 * x + 0], uv1 & 0xff, (uv1 >> 16), \
|
||||
bottom_dst + (2 * x + 0) * XSTEP); \
|
||||
} \
|
||||
tl_uv = t_uv; \
|
||||
l_uv = uv; \
|
||||
} \
|
||||
if (!(len & 1)) { \
|
||||
if (top_y) { \
|
||||
const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \
|
||||
FUNC(top_y[len - 1], uv0 & 0xff, (uv0 >> 16), \
|
||||
top_dst + (len - 1) * XSTEP); \
|
||||
} \
|
||||
if (bottom_y) { \
|
||||
const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \
|
||||
FUNC(bottom_y[len - 1], uv0 & 0xff, (uv0 >> 16), \
|
||||
bottom_dst + (len - 1) * XSTEP); \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
// All variants implemented.
|
||||
UPSAMPLE_FUNC(UpsampleRgbLinePair, VP8YuvToRgb, 3)
|
||||
UPSAMPLE_FUNC(UpsampleBgrLinePair, VP8YuvToBgr, 3)
|
||||
UPSAMPLE_FUNC(UpsampleRgbaLinePair, VP8YuvToRgba, 4)
|
||||
UPSAMPLE_FUNC(UpsampleBgraLinePair, VP8YuvToBgra, 4)
|
||||
UPSAMPLE_FUNC(UpsampleArgbLinePair, VP8YuvToArgb, 4)
|
||||
UPSAMPLE_FUNC(UpsampleRgba4444LinePair, VP8YuvToRgba4444, 2)
|
||||
UPSAMPLE_FUNC(UpsampleRgb565LinePair, VP8YuvToRgb565, 2)
|
||||
// These two don't erase the alpha value
|
||||
UPSAMPLE_FUNC(UpsampleRgbKeepAlphaLinePair, VP8YuvToRgb, 4)
|
||||
UPSAMPLE_FUNC(UpsampleBgrKeepAlphaLinePair, VP8YuvToBgr, 4)
|
||||
UPSAMPLE_FUNC(UpsampleArgbKeepAlphaLinePair, VP8YuvToArgbKeepA, 4)
|
||||
UPSAMPLE_FUNC(UpsampleRgba4444KeepAlphaLinePair, VP8YuvToRgba4444KeepA, 2)
|
||||
|
||||
#undef LOAD_UV
|
||||
#undef UPSAMPLE_FUNC
|
||||
|
||||
#endif // FANCY_UPSAMPLING
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// simple point-sampling
|
||||
|
||||
#define SAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \
|
||||
static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \
|
||||
const uint8_t* u, const uint8_t* v, \
|
||||
uint8_t* top_dst, uint8_t* bottom_dst, int len) { \
|
||||
int i; \
|
||||
for (i = 0; i < len - 1; i += 2) { \
|
||||
FUNC(top_y[0], u[0], v[0], top_dst); \
|
||||
FUNC(top_y[1], u[0], v[0], top_dst + XSTEP); \
|
||||
FUNC(bottom_y[0], u[0], v[0], bottom_dst); \
|
||||
FUNC(bottom_y[1], u[0], v[0], bottom_dst + XSTEP); \
|
||||
top_y += 2; \
|
||||
bottom_y += 2; \
|
||||
u++; \
|
||||
v++; \
|
||||
top_dst += 2 * XSTEP; \
|
||||
bottom_dst += 2 * XSTEP; \
|
||||
} \
|
||||
if (i == len - 1) { /* last one */ \
|
||||
FUNC(top_y[0], u[0], v[0], top_dst); \
|
||||
FUNC(bottom_y[0], u[0], v[0], bottom_dst); \
|
||||
} \
|
||||
}
|
||||
|
||||
// All variants implemented.
|
||||
SAMPLE_FUNC(SampleRgbLinePair, VP8YuvToRgb, 3)
|
||||
SAMPLE_FUNC(SampleBgrLinePair, VP8YuvToBgr, 3)
|
||||
SAMPLE_FUNC(SampleRgbaLinePair, VP8YuvToRgba, 4)
|
||||
SAMPLE_FUNC(SampleBgraLinePair, VP8YuvToBgra, 4)
|
||||
SAMPLE_FUNC(SampleArgbLinePair, VP8YuvToArgb, 4)
|
||||
SAMPLE_FUNC(SampleRgba4444LinePair, VP8YuvToRgba4444, 2)
|
||||
SAMPLE_FUNC(SampleRgb565LinePair, VP8YuvToRgb565, 2)
|
||||
|
||||
#undef SAMPLE_FUNC
|
||||
|
||||
const WebPSampleLinePairFunc WebPSamplers[MODE_LAST] = {
|
||||
SampleRgbLinePair, // MODE_RGB
|
||||
SampleRgbaLinePair, // MODE_RGBA
|
||||
SampleBgrLinePair, // MODE_BGR
|
||||
SampleBgraLinePair, // MODE_BGRA
|
||||
SampleArgbLinePair, // MODE_ARGB
|
||||
SampleRgba4444LinePair, // MODE_RGBA_4444
|
||||
SampleRgb565LinePair // MODE_RGB_565
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// YUV444 converter
|
||||
|
||||
#define YUV444_FUNC(FUNC_NAME, FUNC, XSTEP) \
|
||||
static void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v, \
|
||||
uint8_t* dst, int len) { \
|
||||
int i; \
|
||||
for (i = 0; i < len; ++i) FUNC(y[i], u[i], v[i], &dst[i * XSTEP]); \
|
||||
}
|
||||
|
||||
YUV444_FUNC(Yuv444ToRgb, VP8YuvToRgb, 3)
|
||||
YUV444_FUNC(Yuv444ToBgr, VP8YuvToBgr, 3)
|
||||
YUV444_FUNC(Yuv444ToRgba, VP8YuvToRgba, 4)
|
||||
YUV444_FUNC(Yuv444ToBgra, VP8YuvToBgra, 4)
|
||||
YUV444_FUNC(Yuv444ToArgb, VP8YuvToArgb, 4)
|
||||
YUV444_FUNC(Yuv444ToRgba4444, VP8YuvToRgba4444, 2)
|
||||
YUV444_FUNC(Yuv444ToRgb565, VP8YuvToRgb565, 2)
|
||||
|
||||
#undef YUV444_FUNC
|
||||
|
||||
const WebPYUV444Converter WebPYUV444Converters[MODE_LAST] = {
|
||||
Yuv444ToRgb, // MODE_RGB
|
||||
Yuv444ToRgba, // MODE_RGBA
|
||||
Yuv444ToBgr, // MODE_BGR
|
||||
Yuv444ToBgra, // MODE_BGRA
|
||||
Yuv444ToArgb, // MODE_ARGB
|
||||
Yuv444ToRgba4444, // MODE_RGBA_4444
|
||||
Yuv444ToRgb565 // MODE_RGB_565
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Main call
|
||||
|
||||
void WebPInitUpsamplers(void) {
|
||||
#ifdef FANCY_UPSAMPLING
|
||||
WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair;
|
||||
WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePair;
|
||||
WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair;
|
||||
WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePair;
|
||||
WebPUpsamplers[MODE_ARGB] = UpsampleArgbLinePair;
|
||||
WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair;
|
||||
WebPUpsamplers[MODE_RGB_565] = UpsampleRgb565LinePair;
|
||||
|
||||
WebPUpsamplersKeepAlpha[MODE_RGB] = UpsampleRgbLinePair;
|
||||
WebPUpsamplersKeepAlpha[MODE_RGBA] = UpsampleRgbKeepAlphaLinePair;
|
||||
WebPUpsamplersKeepAlpha[MODE_BGR] = UpsampleBgrLinePair;
|
||||
WebPUpsamplersKeepAlpha[MODE_BGRA] = UpsampleBgrKeepAlphaLinePair;
|
||||
WebPUpsamplersKeepAlpha[MODE_ARGB] = UpsampleArgbKeepAlphaLinePair;
|
||||
WebPUpsamplersKeepAlpha[MODE_RGBA_4444] = UpsampleRgba4444KeepAlphaLinePair;
|
||||
WebPUpsamplersKeepAlpha[MODE_RGB_565] = UpsampleRgb565LinePair;
|
||||
|
||||
// If defined, use CPUInfo() to overwrite some pointers with faster versions.
|
||||
if (VP8GetCPUInfo) {
|
||||
#if defined(__SSE2__) || defined(_MSC_VER)
|
||||
if (VP8GetCPUInfo(kSSE2)) {
|
||||
WebPInitUpsamplersSSE2();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif // FANCY_UPSAMPLING
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
215
src/dsp/upsampling_sse2.c
Normal file
215
src/dsp/upsampling_sse2.c
Normal file
@ -0,0 +1,215 @@
|
||||
// Copyright 2011 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// SSE2 version of YUV to RGB upsampling functions.
|
||||
//
|
||||
// Author: somnath@google.com (Somnath Banerjee)
|
||||
|
||||
#if defined(__SSE2__) || defined(_MSC_VER)
|
||||
|
||||
#include <assert.h>
|
||||
#include <emmintrin.h>
|
||||
#include <string.h>
|
||||
#include "./dsp.h"
|
||||
#include "./yuv.h"
|
||||
#include "../dec/webpi.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef FANCY_UPSAMPLING
|
||||
|
||||
// We compute (9*a + 3*b + 3*c + d + 8) / 16 as follows
|
||||
// u = (9*a + 3*b + 3*c + d + 8) / 16
|
||||
// = (a + (a + 3*b + 3*c + d) / 8 + 1) / 2
|
||||
// = (a + m + 1) / 2
|
||||
// where m = (a + 3*b + 3*c + d) / 8
|
||||
// = ((a + b + c + d) / 2 + b + c) / 4
|
||||
//
|
||||
// Let's say k = (a + b + c + d) / 4.
|
||||
// We can compute k as
|
||||
// k = (s + t + 1) / 2 - ((a^d) | (b^c) | (s^t)) & 1
|
||||
// where s = (a + d + 1) / 2 and t = (b + c + 1) / 2
|
||||
//
|
||||
// Then m can be written as
|
||||
// m = (k + t + 1) / 2 - (((b^c) & (s^t)) | (k^t)) & 1
|
||||
|
||||
// Computes out = (k + in + 1) / 2 - ((ij & (s^t)) | (k^in)) & 1
|
||||
#define GET_M(ij, in, out) do { \
|
||||
const __m128i tmp0 = _mm_avg_epu8(k, (in)); /* (k + in + 1) / 2 */ \
|
||||
const __m128i tmp1 = _mm_and_si128((ij), st); /* (ij) & (s^t) */ \
|
||||
const __m128i tmp2 = _mm_xor_si128(k, (in)); /* (k^in) */ \
|
||||
const __m128i tmp3 = _mm_or_si128(tmp1, tmp2); /* ((ij) & (s^t)) | (k^in) */\
|
||||
const __m128i tmp4 = _mm_and_si128(tmp3, one); /* & 1 -> lsb_correction */ \
|
||||
(out) = _mm_sub_epi8(tmp0, tmp4); /* (k + in + 1) / 2 - lsb_correction */ \
|
||||
} while (0)
|
||||
|
||||
// pack and store two alterning pixel rows
|
||||
#define PACK_AND_STORE(a, b, da, db, out) do { \
|
||||
const __m128i ta = _mm_avg_epu8(a, da); /* (9a + 3b + 3c + d + 8) / 16 */ \
|
||||
const __m128i tb = _mm_avg_epu8(b, db); /* (3a + 9b + c + 3d + 8) / 16 */ \
|
||||
const __m128i t1 = _mm_unpacklo_epi8(ta, tb); \
|
||||
const __m128i t2 = _mm_unpackhi_epi8(ta, tb); \
|
||||
_mm_store_si128(((__m128i*)(out)) + 0, t1); \
|
||||
_mm_store_si128(((__m128i*)(out)) + 1, t2); \
|
||||
} while (0)
|
||||
|
||||
// Loads 17 pixels each from rows r1 and r2 and generates 32 pixels.
|
||||
#define UPSAMPLE_32PIXELS(r1, r2, out) { \
|
||||
const __m128i one = _mm_set1_epi8(1); \
|
||||
const __m128i a = _mm_loadu_si128((__m128i*)&(r1)[0]); \
|
||||
const __m128i b = _mm_loadu_si128((__m128i*)&(r1)[1]); \
|
||||
const __m128i c = _mm_loadu_si128((__m128i*)&(r2)[0]); \
|
||||
const __m128i d = _mm_loadu_si128((__m128i*)&(r2)[1]); \
|
||||
\
|
||||
const __m128i s = _mm_avg_epu8(a, d); /* s = (a + d + 1) / 2 */ \
|
||||
const __m128i t = _mm_avg_epu8(b, c); /* t = (b + c + 1) / 2 */ \
|
||||
const __m128i st = _mm_xor_si128(s, t); /* st = s^t */ \
|
||||
\
|
||||
const __m128i ad = _mm_xor_si128(a, d); /* ad = a^d */ \
|
||||
const __m128i bc = _mm_xor_si128(b, c); /* bc = b^c */ \
|
||||
\
|
||||
const __m128i t1 = _mm_or_si128(ad, bc); /* (a^d) | (b^c) */ \
|
||||
const __m128i t2 = _mm_or_si128(t1, st); /* (a^d) | (b^c) | (s^t) */ \
|
||||
const __m128i t3 = _mm_and_si128(t2, one); /* (a^d) | (b^c) | (s^t) & 1 */ \
|
||||
const __m128i t4 = _mm_avg_epu8(s, t); \
|
||||
const __m128i k = _mm_sub_epi8(t4, t3); /* k = (a + b + c + d) / 4 */ \
|
||||
__m128i diag1, diag2; \
|
||||
\
|
||||
GET_M(bc, t, diag1); /* diag1 = (a + 3b + 3c + d) / 8 */ \
|
||||
GET_M(ad, s, diag2); /* diag2 = (3a + b + c + 3d) / 8 */ \
|
||||
\
|
||||
/* pack the alternate pixels */ \
|
||||
PACK_AND_STORE(a, b, diag1, diag2, &(out)[0 * 32]); \
|
||||
PACK_AND_STORE(c, d, diag2, diag1, &(out)[2 * 32]); \
|
||||
}
|
||||
|
||||
// Turn the macro into a function for reducing code-size when non-critical
|
||||
static void Upsample32Pixels(const uint8_t r1[], const uint8_t r2[],
|
||||
uint8_t* const out) {
|
||||
UPSAMPLE_32PIXELS(r1, r2, out);
|
||||
}
|
||||
|
||||
#define UPSAMPLE_LAST_BLOCK(tb, bb, num_pixels, out) { \
|
||||
uint8_t r1[17], r2[17]; \
|
||||
memcpy(r1, (tb), (num_pixels)); \
|
||||
memcpy(r2, (bb), (num_pixels)); \
|
||||
/* replicate last byte */ \
|
||||
memset(r1 + (num_pixels), r1[(num_pixels) - 1], 17 - (num_pixels)); \
|
||||
memset(r2 + (num_pixels), r2[(num_pixels) - 1], 17 - (num_pixels)); \
|
||||
/* using the shared function instead of the macro saves ~3k code size */ \
|
||||
Upsample32Pixels(r1, r2, out); \
|
||||
}
|
||||
|
||||
#define CONVERT2RGB(FUNC, XSTEP, top_y, bottom_y, uv, \
|
||||
top_dst, bottom_dst, cur_x, num_pixels) { \
|
||||
int n; \
|
||||
if (top_y) { \
|
||||
for (n = 0; n < (num_pixels); ++n) { \
|
||||
FUNC(top_y[(cur_x) + n], (uv)[n], (uv)[32 + n], \
|
||||
top_dst + ((cur_x) + n) * XSTEP); \
|
||||
} \
|
||||
} \
|
||||
if (bottom_y) { \
|
||||
for (n = 0; n < (num_pixels); ++n) { \
|
||||
FUNC(bottom_y[(cur_x) + n], (uv)[64 + n], (uv)[64 + 32 + n], \
|
||||
bottom_dst + ((cur_x) + n) * XSTEP); \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#define SSE2_UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \
|
||||
static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \
|
||||
const uint8_t* top_u, const uint8_t* top_v, \
|
||||
const uint8_t* cur_u, const uint8_t* cur_v, \
|
||||
uint8_t* top_dst, uint8_t* bottom_dst, int len) { \
|
||||
int b; \
|
||||
/* 16 byte aligned array to cache reconstructed u and v */ \
|
||||
uint8_t uv_buf[4 * 32 + 15]; \
|
||||
uint8_t* const r_uv = (uint8_t*)((uintptr_t)(uv_buf + 15) & ~15); \
|
||||
const int uv_len = (len + 1) >> 1; \
|
||||
/* 17 pixels must be read-able for each block */ \
|
||||
const int num_blocks = (uv_len - 1) >> 4; \
|
||||
const int leftover = uv_len - num_blocks * 16; \
|
||||
const int last_pos = 1 + 32 * num_blocks; \
|
||||
\
|
||||
const int u_diag = ((top_u[0] + cur_u[0]) >> 1) + 1; \
|
||||
const int v_diag = ((top_v[0] + cur_v[0]) >> 1) + 1; \
|
||||
\
|
||||
assert(len > 0); \
|
||||
/* Treat the first pixel in regular way */ \
|
||||
if (top_y) { \
|
||||
const int u0 = (top_u[0] + u_diag) >> 1; \
|
||||
const int v0 = (top_v[0] + v_diag) >> 1; \
|
||||
FUNC(top_y[0], u0, v0, top_dst); \
|
||||
} \
|
||||
if (bottom_y) { \
|
||||
const int u0 = (cur_u[0] + u_diag) >> 1; \
|
||||
const int v0 = (cur_v[0] + v_diag) >> 1; \
|
||||
FUNC(bottom_y[0], u0, v0, bottom_dst); \
|
||||
} \
|
||||
\
|
||||
for (b = 0; b < num_blocks; ++b) { \
|
||||
UPSAMPLE_32PIXELS(top_u, cur_u, r_uv + 0 * 32); \
|
||||
UPSAMPLE_32PIXELS(top_v, cur_v, r_uv + 1 * 32); \
|
||||
CONVERT2RGB(FUNC, XSTEP, top_y, bottom_y, r_uv, top_dst, bottom_dst, \
|
||||
32 * b + 1, 32) \
|
||||
top_u += 16; \
|
||||
cur_u += 16; \
|
||||
top_v += 16; \
|
||||
cur_v += 16; \
|
||||
} \
|
||||
\
|
||||
UPSAMPLE_LAST_BLOCK(top_u, cur_u, leftover, r_uv + 0 * 32); \
|
||||
UPSAMPLE_LAST_BLOCK(top_v, cur_v, leftover, r_uv + 1 * 32); \
|
||||
CONVERT2RGB(FUNC, XSTEP, top_y, bottom_y, r_uv, top_dst, bottom_dst, \
|
||||
last_pos, len - last_pos); \
|
||||
}
|
||||
|
||||
// SSE2 variants of the fancy upsampler.
|
||||
SSE2_UPSAMPLE_FUNC(UpsampleRgbLinePairSSE2, VP8YuvToRgb, 3)
|
||||
SSE2_UPSAMPLE_FUNC(UpsampleBgrLinePairSSE2, VP8YuvToBgr, 3)
|
||||
SSE2_UPSAMPLE_FUNC(UpsampleRgbaLinePairSSE2, VP8YuvToRgba, 4)
|
||||
SSE2_UPSAMPLE_FUNC(UpsampleBgraLinePairSSE2, VP8YuvToBgra, 4)
|
||||
// These two don't erase the alpha value
|
||||
SSE2_UPSAMPLE_FUNC(UpsampleRgbKeepAlphaLinePairSSE2, VP8YuvToRgb, 4)
|
||||
SSE2_UPSAMPLE_FUNC(UpsampleBgrKeepAlphaLinePairSSE2, VP8YuvToBgr, 4)
|
||||
|
||||
#undef GET_M
|
||||
#undef PACK_AND_STORE
|
||||
#undef UPSAMPLE_32PIXELS
|
||||
#undef UPSAMPLE_LAST_BLOCK
|
||||
#undef CONVERT2RGB
|
||||
#undef SSE2_UPSAMPLE_FUNC
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
|
||||
extern WebPUpsampleLinePairFunc WebPUpsamplersKeepAlpha[/* MODE_LAST */];
|
||||
|
||||
#endif // FANCY_UPSAMPLING
|
||||
|
||||
void WebPInitUpsamplersSSE2(void) {
|
||||
#ifdef FANCY_UPSAMPLING
|
||||
WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePairSSE2;
|
||||
WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePairSSE2;
|
||||
WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePairSSE2;
|
||||
WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePairSSE2;
|
||||
|
||||
WebPUpsamplersKeepAlpha[MODE_RGB] = UpsampleRgbLinePairSSE2;
|
||||
WebPUpsamplersKeepAlpha[MODE_RGBA] = UpsampleRgbKeepAlphaLinePairSSE2;
|
||||
WebPUpsamplersKeepAlpha[MODE_BGR] = UpsampleBgrLinePairSSE2;
|
||||
WebPUpsamplersKeepAlpha[MODE_BGRA] = UpsampleBgrKeepAlphaLinePairSSE2;
|
||||
#endif // FANCY_UPSAMPLING
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif //__SSE2__ || _MSC_VER
|
52
src/dsp/yuv.c
Normal file
52
src/dsp/yuv.c
Normal file
@ -0,0 +1,52 @@
|
||||
// Copyright 2010 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// YUV->RGB conversion function
|
||||
//
|
||||
// Author: Skal (pascal.massimino@gmail.com)
|
||||
|
||||
#include "./yuv.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum { YUV_HALF = 1 << (YUV_FIX - 1) };
|
||||
|
||||
int16_t VP8kVToR[256], VP8kUToB[256];
|
||||
int32_t VP8kVToG[256], VP8kUToG[256];
|
||||
uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN];
|
||||
uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN];
|
||||
|
||||
static int done = 0;
|
||||
|
||||
static inline uint8_t clip(int v, int max_value) {
|
||||
return v < 0 ? 0 : v > max_value ? max_value : v;
|
||||
}
|
||||
|
||||
void VP8YUVInit(void) {
|
||||
int i;
|
||||
if (done) {
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < 256; ++i) {
|
||||
VP8kVToR[i] = (89858 * (i - 128) + YUV_HALF) >> YUV_FIX;
|
||||
VP8kUToG[i] = -22014 * (i - 128) + YUV_HALF;
|
||||
VP8kVToG[i] = -45773 * (i - 128);
|
||||
VP8kUToB[i] = (113618 * (i - 128) + YUV_HALF) >> YUV_FIX;
|
||||
}
|
||||
for (i = YUV_RANGE_MIN; i < YUV_RANGE_MAX; ++i) {
|
||||
const int k = ((i - 16) * 76283 + YUV_HALF) >> YUV_FIX;
|
||||
VP8kClip[i - YUV_RANGE_MIN] = clip(k, 255);
|
||||
VP8kClip4Bits[i - YUV_RANGE_MIN] = clip((k + 8) >> 4, 15);
|
||||
}
|
||||
done = 1;
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
109
src/dsp/yuv.h
Normal file
109
src/dsp/yuv.h
Normal file
@ -0,0 +1,109 @@
|
||||
// Copyright 2010 Google Inc.
|
||||
//
|
||||
// This code is licensed under the same terms as WebM:
|
||||
// Software License Agreement: http://www.webmproject.org/license/software/
|
||||
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// inline YUV->RGB conversion function
|
||||
//
|
||||
// Author: Skal (pascal.massimino@gmail.com)
|
||||
|
||||
#ifndef WEBP_DSP_YUV_H_
|
||||
#define WEBP_DSP_YUV_H_
|
||||
|
||||
#include "../webp/decode_vp8.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum { YUV_FIX = 16, // fixed-point precision
|
||||
YUV_RANGE_MIN = -227, // min value of r/g/b output
|
||||
YUV_RANGE_MAX = 256 + 226 // max value of r/g/b output
|
||||
};
|
||||
extern int16_t VP8kVToR[256], VP8kUToB[256];
|
||||
extern int32_t VP8kVToG[256], VP8kUToG[256];
|
||||
extern uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN];
|
||||
extern uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN];
|
||||
|
||||
static inline void VP8YuvToRgb(uint8_t y, uint8_t u, uint8_t v,
|
||||
uint8_t* const rgb) {
|
||||
const int r_off = VP8kVToR[v];
|
||||
const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
|
||||
const int b_off = VP8kUToB[u];
|
||||
rgb[0] = VP8kClip[y + r_off - YUV_RANGE_MIN];
|
||||
rgb[1] = VP8kClip[y + g_off - YUV_RANGE_MIN];
|
||||
rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN];
|
||||
}
|
||||
|
||||
static inline void VP8YuvToRgb565(uint8_t y, uint8_t u, uint8_t v,
|
||||
uint8_t* const rgb) {
|
||||
const int r_off = VP8kVToR[v];
|
||||
const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
|
||||
const int b_off = VP8kUToB[u];
|
||||
rgb[0] = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) |
|
||||
(VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5));
|
||||
rgb[1] = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) |
|
||||
(VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3));
|
||||
}
|
||||
|
||||
static inline void VP8YuvToArgbKeepA(uint8_t y, uint8_t u, uint8_t v,
|
||||
uint8_t* const argb) {
|
||||
// Don't update Aplha (argb[0])
|
||||
VP8YuvToRgb(y, u, v, argb + 1);
|
||||
}
|
||||
|
||||
static inline void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v,
|
||||
uint8_t* const argb) {
|
||||
argb[0] = 0xff;
|
||||
VP8YuvToArgbKeepA(y, u, v, argb);
|
||||
}
|
||||
|
||||
static inline void VP8YuvToRgba4444KeepA(uint8_t y, uint8_t u, uint8_t v,
|
||||
uint8_t* const argb) {
|
||||
const int r_off = VP8kVToR[v];
|
||||
const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
|
||||
const int b_off = VP8kUToB[u];
|
||||
// Don't update Aplha (last 4 bits of argb[1])
|
||||
argb[0] = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) |
|
||||
VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]);
|
||||
argb[1] = (argb[1] & 0x0f) | (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4);
|
||||
}
|
||||
|
||||
static inline void VP8YuvToRgba4444(uint8_t y, uint8_t u, uint8_t v,
|
||||
uint8_t* const argb) {
|
||||
argb[1] = 0x0f;
|
||||
VP8YuvToRgba4444KeepA(y, u, v, argb);
|
||||
}
|
||||
|
||||
static inline void VP8YuvToBgr(uint8_t y, uint8_t u, uint8_t v,
|
||||
uint8_t* const bgr) {
|
||||
const int r_off = VP8kVToR[v];
|
||||
const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
|
||||
const int b_off = VP8kUToB[u];
|
||||
bgr[0] = VP8kClip[y + b_off - YUV_RANGE_MIN];
|
||||
bgr[1] = VP8kClip[y + g_off - YUV_RANGE_MIN];
|
||||
bgr[2] = VP8kClip[y + r_off - YUV_RANGE_MIN];
|
||||
}
|
||||
|
||||
static inline void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v,
|
||||
uint8_t* const bgra) {
|
||||
VP8YuvToBgr(y, u, v, bgra);
|
||||
bgra[3] = 0xff;
|
||||
}
|
||||
|
||||
static inline void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v,
|
||||
uint8_t* const rgba) {
|
||||
VP8YuvToRgb(y, u, v, rgba);
|
||||
rgba[3] = 0xff;
|
||||
}
|
||||
|
||||
// Must be called before everything, to initialize the tables.
|
||||
void VP8YUVInit(void);
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif // WEBP_DSP_YUV_H_
|
Reference in New Issue
Block a user